Change Sale Overview to become a daily takings view with totals.

This commit is contained in:
FrancescoUK
2015-09-01 23:36:29 +01:00
parent 3e24b9caff
commit c88f37f8e7
6 changed files with 255 additions and 216 deletions

View File

@@ -14,20 +14,41 @@ class Sales extends Secure_area
$this->_reload();
}
function manage($payment_type = 0, $limit_from = 0)
function manage($only_invoices = FALSE, $limit_from = 0)
{
$data['controller_name']=strtolower($this->uri->segment(1));
$data['payment_types'] = array($this->lang->line('sales_no_filter'), $this->lang->line('sales_invoice'));
$data['search_section_state']=$this->input->post('search_section_state');
$model = $this->Sale;
// Create the temp tables to work with the data
$model->create_sales_items_temp_table();
$data['controller_name'] = strtolower($this->uri->segment(1));
$data['only_invoices'] = array($this->lang->line('sales_no_filter'), $this->lang->line('sales_invoice'));
$data['search_section_state'] = $this->input->post('search_section_state');
$lines_per_page = $this->Appconfig->get('lines_per_page');
$sales = $this->Sale->get_all($payment_type,$lines_per_page,$limit_from);
$total_rows = $this->Sale->get_found_rows($payment_type);
$data['payment_type'] = $payment_type;
$data['links'] = $this->_initialize_pagination($this->Sale, $lines_per_page, $limit_from, -1, 'manage', $payment_type);
$data['manage_table']=get_sales_manage_table($sales,$this);
$this->load->view($data['controller_name'] . '/manage',$data);
$today = date('Y-m-d');
// $yesterday = date('Y-m-d', mktime(0,0,0,date("m"),date("d")-1,date("Y")));
// $tomorrow = date('Y-m-d', mktime(0,0,0,date("m"),date("d")+1,date("Y")));
$start_of_time = date('Y-m-d', 0);
$start_date = $start_of_time;
$end_date = $today;
$sale_type = 'sales';
$location_id = 'all';
$report_data = $model->get_data(array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id,
'only_invoices' => $only_invoices, 'lines_per_page' => $lines_per_page, 'limit_from' => $limit_from));
$sales = $report_data['summary'];
$payments = $report_data['payments'];
$details = $report_data['details'];
$payments_summary = $report_data['payments_summary'];
$data['only_invoices'] = $only_invoices;
$data['links'] = $this->_initialize_pagination($model, $lines_per_page, $limit_from, -1, 'manage', $only_invoices);
$data['manage_table'] = get_sales_manage_table($sales, $payments, $details, $this);
$data['payments_summary_data'] = get_sales_manage_payments_summary($payments_summary, $this);
$this->load->view($data['controller_name'] . '/manage', $data);
$this->_remove_duplicate_cookies();
}
@@ -51,16 +72,7 @@ class Sales extends Secure_area
function search()
{
$payment_type = $this->input->post('payment_type', TRUE);
$limit_from = $this->input->post('limit_from', TRUE);
$search = $this->input->post('search', TRUE);
$lines_per_page = $this->Appconfig->get('lines_per_page');
$sales = $this->Sale->search($search, $payment_type, $lines_per_page, $limit_from, $search);
$total_rows = $this->Sale->get_found_rows($search);
$links = $this->_initialize_pagination($this->Sale,$lines_per_page,$limit_from,$total_rows,'search',$payment_type);
$data_rows=get_sales_manage_table_data_rows($sales,$this);
echo json_encode(array('total_rows' => $total_rows, 'rows' => $data_rows, 'pagination' => $links));
$this->_remove_duplicate_cookies();
$this->manage($this->input->post('only_invoices', TRUE));
}
function item_search()
@@ -81,14 +93,6 @@ class Sales extends Secure_area
echo implode("\n",$suggestions);
}
function suggest()
{
$search = $this->input->post('q', TRUE);
$limit = $this->input->post('limit', TRUE);
$suggestions = $this->Sale->get_search_suggestions($search, $limit);
echo implode("\n",$suggestions);
}
function select_customer()
{
$customer_id = $this->input->post("customer");
@@ -133,7 +137,7 @@ class Sales extends Secure_area
function set_email_receipt()
{
$this->sale_lib->set_email_receipt($this->input->post('email_receipt'));
$this->sale_lib->set_email_receipt($this->input->post('email_receipt'));
}
//Alain Multiple Payments
@@ -318,10 +322,10 @@ class Sales extends Secure_area
$data['customer_location'] = $cust_info->zip . ' ' . $cust_info->city;
$data['account_number'] = $cust_info->account_number;
$data['customer_info'] = implode("\n", array(
$data['customer'],
$data['customer_address'],
$data['customer_location'],
$data['account_number']
$data['customer'],
$data['customer_address'],
$data['customer_location'],
$data['account_number']
));
}
$invoice_number=$this->_substitute_invoice_number($cust_info);
@@ -399,7 +403,8 @@ class Sales extends Secure_area
return $filename;
}
function invoice_email($sale_id) {
function invoice_email($sale_id)
{
$sale_data = $this->_load_sale_data($sale_id);
$sale_data['image_prefix'] = base_url();
$this->load->view('sales/invoice_email', $sale_data);
@@ -407,7 +412,8 @@ class Sales extends Secure_area
$this->_remove_duplicate_cookies();
}
function send_invoice($sale_id) {
function send_invoice($sale_id)
{
$sale_data = $this->_load_sale_data($sale_id);
$text = $this->config->item('invoice_email_message');
$text = str_replace('$INV', $sale_data['invoice_number'], $text);
@@ -485,7 +491,8 @@ class Sales extends Secure_area
return $invoice_number;
}
function _load_sale_data($sale_id) {
function _load_sale_data($sale_id)
{
$this->sale_lib->clear_all();
$sale_info = $this->Sale->get_info($sale_id)->row_array();
$this->sale_lib->copy_entire_sale($sale_id);
@@ -526,19 +533,19 @@ class Sales extends Secure_area
$data['customer_email'] = $cust_info->email;
$data['account_number'] = $cust_info->account_number;
$data['customer_info'] = implode("\n", array(
$data['customer'],
$data['customer_address'],
$data['customer_location'],
$data['account_number']
$data['customer'],
$data['customer_address'],
$data['customer_location'],
$data['account_number']
));
}
$data['sale_id']='POS '.$sale_id;
$data['comments'] = $sale_info[ 'comment' ];
$data['invoice_number'] = $sale_info['invoice_number'];
$data['company_info'] = implode("\n", array(
$this->config->item('address'),
$this->config->item('phone'),
$this->config->item('account_number')
$this->config->item('address'),
$this->config->item('phone'),
$this->config->item('account_number')
));
// static barcode config for receipts + invoices
$barcode_config=array('barcode_type'=>2,'barcode_width'=>200, 'barcode_height'=>30, 'barcode_quality'=>100);
@@ -583,7 +590,8 @@ class Sales extends Secure_area
$this->load->view('sales/form', $data);
}
function delete($sale_id = -1, $update_inventory=TRUE) {
function delete($sale_id = -1, $update_inventory=TRUE)
{
$employee_id=$this->Employee->get_logged_in_employee_info()->person_id;
$sale_ids= $sale_id == -1 ? $this->input->post('ids') : array($sale_id);

View File

@@ -1,16 +1,18 @@
<?php
function get_sales_manage_table($sales,$controller)
function get_sales_manage_table($sales, $payments, $details, $controller)
{
$CI =& get_instance();
$table='<table class="tablesorter" id="sortable_table">';
$headers = array('&nbsp;',
$CI->lang->line('sales_receipt_number'),
$CI->lang->line('sales_sale_time'),
$CI->lang->line('customers_customer'),
$CI->lang->line('sales_amount_tendered'),
$CI->lang->line('sales_amount_due'),
$CI->lang->line('sales_receipt_number'),
$CI->lang->line('sales_change_due'),
$CI->lang->line('sales_payment'),
$CI->lang->line('sales_invoice_number'),
'&nbsp');
@@ -20,45 +22,87 @@ function get_sales_manage_table($sales,$controller)
$table.="<th>$header</th>";
}
$table.='</tr></thead><tbody>';
$table.=get_sales_manage_table_data_rows($sales,$controller);
$table.=get_sales_manage_table_data_rows($sales, $payments, $details, $controller);
$table.='</tbody></table>';
return $table;
}
/*
Gets the html data rows for the people.
Gets the html data rows for the sales.
*/
function get_sales_manage_table_data_rows($sales,$controller)
function get_sales_manage_table_data_rows($sales, $payments, $details, $controller)
{
$CI =& get_instance();
$table_data_rows='';
$sum_amount_tendered = 0;
$sum_amount_due = 0;
$sum_change_due = 0;
foreach($sales->result_array() as $sale)
foreach($sales as $key=>$sale)
{
$table_data_rows.=get_sale_data_row($sale,$controller);
$table_data_rows.=get_sales_manage_sale_data_row($sale, $payments[$key], $controller);
$sum_amount_tendered += $sale['amount_tendered'];
$sum_amount_due += $sale['amount_due'];
$sum_change_due += $sale['change_due'];
}
if($sales->num_rows()==0)
if($table_data_rows == '')
{
$table_data_rows.="<tr><td colspan='8'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('sales_no_sales_to_display')."</div></tr></tr>";
}
else
{
// show a line in bold with the totals
$table_data_rows.="<tr><th>&nbsp;</th><th>".$CI->lang->line('sales_total')."</th><th>&nbsp;</th><th>&nbsp;</th><th>".to_currency($sum_amount_tendered)."</th><th>".to_currency($sum_amount_due)."</th><th>".to_currency($sum_change_due)."</th></tr>";
}
return $table_data_rows;
}
function get_sale_data_row($sale,$controller)
function get_sales_manage_sale_data_row($sale, $payments, $controller)
{
$CI =& get_instance();
$controller_name=$CI->uri->segment(1);
$controller_name = $CI->uri->segment(1);
$width = $controller->get_form_width();
$payment = '';
// set in one string all the payments type and amount
foreach( $payments as $key=>$row )
{
$payment.= $row['payment_type'] . " ";
// the assumption here is that any change due is deducted from the cash amount paid, not from any other form of payment
if( $row['payment_type'] == $CI->lang->line('sales_cash') )
{
$payment.= to_currency($row['payment_amount'] - $sale['change_due']);
}
else
{
$payment.= to_currency($row['payment_amount']);
}
$payment.= ", ";
}
// make sure we have something in the string
if( $payment != '' )
{
// remove the last ", " from the string
$payment = substr($payment, 0, strlen($payment) - 2 );
}
$table_data_row='<tr>';
$table_data_row.='<td width="3%"><input type="checkbox" id="sale_"' . $sale[ 'sale_id' ] . ' value="' . $sale[ 'sale_id' ]. '" /></td>';
$table_data_row.='<td width="15%">'.'POS ' . $sale[ 'sale_id' ]. '</td>';
$table_data_row.='<td width="17%">'.date($CI->config->item('dateformat').' '.$CI->config->item('timeformat'), strtotime($sale[ 'sale_time' ])).'</td>';
$table_data_row.='<td width="23%">'.character_limiter( $sale[ 'last_name' ] . " " . $sale[ 'first_name' ] ,25).'</td>';
$table_data_row.='<td width="23%">'.character_limiter( $sale[ 'customer_name' ], 25).'</td>';
$table_data_row.='<td width="10%">'.to_currency( $sale[ 'amount_tendered' ] ).'</td>';
$table_data_row.='<td width="10%">'.to_currency( $sale[ 'amount_due' ] ).'</td>';
$table_data_row.='<td width="15%">'.'Ticket ' . $sale[ 'sale_id' ]. '</td>';
$table_data_row.='<td width="10%">'.to_currency( $sale[ 'change_due' ] ).'</td>';
$table_data_row.='<td width="20%">'.$payment.'</td>';
$table_data_row.='<td width="10%">'.$sale[ 'invoice_number' ].'</td>';
$table_data_row.='<td width="12%">';
$table_data_row.=anchor($controller_name."/edit/" . $sale[ 'sale_id' ] . "/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update')));
@@ -72,6 +116,31 @@ function get_sale_data_row($sale,$controller)
return $table_data_row;
}
/*
Get the sales payments summary
*/
function get_sales_manage_payments_summary($payments_summary, $controller)
{
$CI =& get_instance();
$table='<table class="tablesorter" id="sortable_table">';
$table.='<thead><tr>';
$table.='<th>&nbsp;</th>';
foreach($payments_summary as $key=>$summary)
{
$table.='<th>'.$summary['payment_type'].'</th>';
}
$table.='</tr></thead><tbody><tr>';
$table.='<th>'.$CI->lang->line('sales_total').'</th>';
foreach($payments_summary as $key=>$summary)
{
$table.='<td>'.to_currency( $summary['payment_amount'] ).'</td>';
}
$table.='</tr></tbody></table>';
return $table;
}
/*
Gets the html table to manage people.
*/
@@ -95,6 +164,7 @@ function get_people_manage_table($people,$controller)
$table.='</tr></thead><tbody>';
$table.=get_people_manage_table_data_rows($people,$controller);
$table.='</tbody></table>';
return $table;
}
@@ -148,6 +218,7 @@ function get_detailed_data_row($row, $controller)
$table_data_row.='</td>';
}
$table_data_row.='</tr>';
return $table_data_row;
}
@@ -177,6 +248,7 @@ function get_supplier_manage_table($suppliers,$controller)
$table.='</tr></thead><tbody>';
$table.=get_supplier_manage_table_data_rows($suppliers,$controller);
$table.='</tbody></table>';
return $table;
}
@@ -252,6 +324,7 @@ function get_items_manage_table($items,$controller)
$table.='</tr></thead><tbody>';
$table.=get_items_manage_table_data_rows($items,$controller);
$table.='</tbody></table>';
return $table;
}
@@ -318,6 +391,7 @@ function get_item_data_row($item,$controller)
$table_data_row.='<td width="5%">'*/'&nbsp;&nbsp;&nbsp;&nbsp;'.anchor($controller_name."/count_details/$item->item_id/width:$width", $CI->lang->line('common_det'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_details_count'))).'</td>';//inventory details
$table_data_row.='</tr>';
return $table_data_row;
}
@@ -327,7 +401,6 @@ Gets the html table to manage giftcards.
function get_giftcards_manage_table( $giftcards, $controller )
{
$CI =& get_instance();
$table='<table class="tablesorter" id="sortable_table">';
$headers = array('<input type="checkbox" id="select_all" />',
@@ -346,6 +419,7 @@ function get_giftcards_manage_table( $giftcards, $controller )
$table.='</tr></thead><tbody>';
$table.=get_giftcards_manage_table_data_rows( $giftcards, $controller );
$table.='</tbody></table>';
return $table;
}
@@ -383,9 +457,9 @@ function get_giftcard_data_row($giftcard,$controller)
$table_data_row.='<td width="15%">'.$giftcard->first_name.'</td>';
$table_data_row.='<td width="15%">'.$giftcard->giftcard_number.'</td>';
$table_data_row.='<td width="20%">'.to_currency($giftcard->value).'</td>';
$table_data_row.='<td width="5%">'.anchor($controller_name."/view/$giftcard->giftcard_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).'</td>';
$table_data_row.='<td width="5%">'.anchor($controller_name."/view/$giftcard->giftcard_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).'</td>';
$table_data_row.='</tr>';
return $table_data_row;
}
/** END GARRISON MODIFIED **/
@@ -396,7 +470,6 @@ Gets the html table to manage item kits.
function get_item_kits_manage_table( $item_kits, $controller )
{
$CI =& get_instance();
$table='<table class="tablesorter" id="sortable_table">';
$headers = array('<input type="checkbox" id="select_all" />',
@@ -413,6 +486,7 @@ function get_item_kits_manage_table( $item_kits, $controller )
$table.='</tr></thead><tbody>';
$table.=get_item_kits_manage_table_data_rows( $item_kits, $controller );
$table.='</tbody></table>';
return $table;
}
@@ -447,9 +521,9 @@ function get_item_kit_data_row($item_kit,$controller)
$table_data_row.="<td width='3%'><input type='checkbox' id='item_kit_$item_kit->item_kit_id' value='".$item_kit->item_kit_id."'/></td>";
$table_data_row.='<td width="15%">'.$item_kit->name.'</td>';
$table_data_row.='<td width="20%">'.character_limiter($item_kit->description, 25).'</td>';
$table_data_row.='<td width="5%">'.anchor($controller_name."/view/$item_kit->item_kit_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).'</td>';
$table_data_row.='<td width="5%">'.anchor($controller_name."/view/$item_kit->item_kit_id/width:$width", $CI->lang->line('common_edit'),array('class'=>'thickbox','title'=>$CI->lang->line($controller_name.'_update'))).'</td>';
$table_data_row.='</tr>';
return $table_data_row;
}

View File

@@ -333,9 +333,9 @@ class Sale_lib
//$item = $this->CI->Item->get_info($item_id);
$item_quantity = $this->CI->Item_quantities->get_item_quantity($item_id,$item_location)->quantity;
$quanity_added = $this->get_quantity_already_added($item_id,$item_location);
$quantity_added = $this->get_quantity_already_added($item_id,$item_location);
if ($item_quantity - $quanity_added < 0)
if ($item_quantity - $quantity_added < 0)
{
return true;
}

View File

@@ -18,69 +18,83 @@ class Sale extends CI_Model
$this->db->where('sales.sale_id',$sale_id);
$this->db->order_by('sale_time', 'desc');
$this->db->group_by('sale_id');
return $this->db->get();
}
function get_all($only_invoices = 0, $rows = 0, $limit_from = 0)
// get the sales data for the takings table
function get_data($inputs)
{
$this->db->select('first_name, last_name, invoice_number, amount_tendered, sale_time');
$this->db->select("DATE_FORMAT( sale_time, '%d-%m-%Y' ) AS sale_date", FALSE);
$this->db->select("sales.sale_id AS sale_id");
$this->db->select("SUM(item_unit_price * quantity_purchased * (1 - discount_percent / 100)) AS amount_due");
$this->db->from('sales');
$this->db->join('people', 'people.person_id = sales.customer_id', 'left');
$this->db->join('sales_items', 'sales_items.sale_id = sales.sale_id');
$this->db->join("(SELECT sale_id, SUM(payment_amount) AS amount_tendered " .
" FROM " . $this->db->dbprefix('sales_payments') ." WHERE payment_type <> '" .
$this->lang->line('sales_check') . "' GROUP BY sale_id) AS payments",
"payments.sale_id = sales.sale_id", 'left');
$this->db->order_by('sale_time', 'desc');
$this->db->group_by('sale_id');
if ($rows > 0) {
$this->db->limit($rows, $limit_from);
$this->db->select('sale_id, sale_date, sale_time, sum(quantity_purchased) as items_purchased, CONCAT(employee.first_name," ",employee.last_name) as employee_name,
CONCAT(customer.first_name," ",customer.last_name) as customer_name, sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(cost) as cost, sum(profit) as profit,
sale_payment_amount as amount_tendered, total as amount_due, (sale_payment_amount - total) as change_due, invoice_number', false);
$this->db->from('sales_items_temp');
$this->db->join('people as employee', 'sales_items_temp.employee_id = employee.person_id');
$this->db->join('people as customer', 'sales_items_temp.customer_id = customer.person_id', 'left');
$this->db->where('sale_date BETWEEN '. $this->db->escape($inputs['start_date']). ' and '. $this->db->escape($inputs['end_date']));
if ($inputs['location_id'] != 'all')
{
$this->db->where('item_location', $inputs['location_id']);
}
if ($only_invoices != 0) {
if ($inputs['sale_type'] == 'sales')
{
$this->db->where('quantity_purchased > 0');
}
elseif ($inputs['sale_type'] == 'returns')
{
$this->db->where('quantity_purchased < 0');
}
if ($inputs['only_invoices'] != FALSE)
{
$this->db->where('invoice_number <> ', 'NULL');
}
return $this->db->get();
}
function search($search, $only_invoices = FALSE, $rows = 0, $limit_from = 0)
{
$valid_receipt = $this->sale_lib->is_valid_receipt($search);
$this->db->select('first_name, last_name, invoice_number, amount_tendered, sale_time');
$this->db->select("DATE_FORMAT( sale_time, '%d-%m-%Y' ) AS sale_date", FALSE);
$this->db->select("sales.sale_id AS sale_id");
$this->db->select("SUM(item_unit_price * quantity_purchased * (1 - discount_percent / 100)) AS amount_due");
$this->db->from('sales');
$this->db->join('people', 'people.person_id = sales.customer_id', 'left');
$this->db->join('sales_items', 'sales_items.sale_id = sales.sale_id');
$this->db->join("(SELECT sale_id, SUM(payment_amount) AS amount_tendered " .
" FROM " . $this->db->dbprefix('sales_payments') ." WHERE payment_type <> '" .
$this->lang->line('sales_check') . "' GROUP BY sale_id) AS payments",
"payments.sale_id = sales.sale_id", 'left');
$this->db->group_by('sale_id');
if (!empty($search)) {
// if barcode scanned, explode and search for second term which will be the id
if ($valid_receipt) {
$pieces = explode(' ',$search);
$this->db->where('sales.sale_id', $pieces[1]);
} else {
// open parentheses
$this->db->where("( last_name LIKE '%" . $search . "%' OR ".
"first_name LIKE '%" . $search . "%' OR " .
"CONCAT( first_name,' ',last_name ) LIKE '%" . $search . "%')");
// close parentheses
}
$this->db->order_by('sale_date', 'desc');
if ($inputs['lines_per_page'] > 0)
{
$this->db->limit($inputs['lines_per_page'], $inputs['limit_from']);
}
if ($only_invoices != 0) {
$data = array();
$data['summary'] = $this->db->get()->result_array();
$data['details'] = array();
$data['payments'] = array();
foreach($data['summary'] as $key=>$value)
{
$data['payments'][$key] = $this->get_sale_payments($value['sale_id'])->result_array();
}
// get payment summary
$this->db->select('sales_payments.payment_type, count(*) as count, sum(payment_amount) as payment_amount', false);
$this->db->from('sales_payments');
$this->db->join('sales_items_temp', 'sales_items_temp.sale_id=sales_payments.sale_id');
$this->db->where('date(sale_time) BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
if ($inputs['sale_type'] == 'sales')
{
$this->db->where('payment_amount > 0');
}
elseif ($inputs['sale_type'] == 'returns')
{
$this->db->where('payment_amount < 0');
}
if ($inputs['only_invoices'] != FALSE)
{
$this->db->where('invoice_number <> ', 'NULL');
}
$this->db->order_by('sale_time DESC');
if ($rows > 0) {
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
$this->db->group_by("payment_type");
$data['payments_summary'] = $this->db->get()->result_array();
return $data;
}
function get_total_rows()
@@ -89,59 +103,6 @@ class Sale extends CI_Model
return $this->db->count_all_results();
}
function get_search_suggestions($search,$limit=25)
{
$suggestions = array();
if (!$this->sale_lib->is_valid_receipt($search)) {
$this->db->distinct();
$this->db->select('first_name, last_name, invoice_number, sale_time');
$this->db->select("DATE_FORMAT( sale_time, '%d-%m-%Y' ) AS sale_date", FALSE);
$this->db->select("sales.sale_id AS sale_id");
$this->db->from('sales');
$this->db->join('people', 'people.person_id = sales.customer_id', 'left');
$this->db->where("( last_name LIKE '%" . $search . "%' OR ".
"first_name LIKE '%" . $search . "%' OR " .
"CONCAT( first_name, last_name ))");
$this->db->order_by('last_name', "asc");
foreach($this->db->get()->result_array() as $result)
{
$suggestions[]=$result[ 'first_name' ].' '.$result[ 'last_name' ];
}
} else {
$suggestions[]=$search;
}
return $suggestions;
}
function get_found_rows($search, $only_invoices = FALSE)
{
$valid_receipt = $this->sale_lib->is_valid_receipt($search);
$this->db->from('sales');
$this->db->join('sales_items', 'sales_items.sale_id = sales.sale_id');
$this->db->join('people', 'people.person_id = sales.customer_id', 'left');
$this->db->group_by('sales.sale_id');
if (!empty($search)) {
// if barcode scanned, explode and search for second term which will be the id
$pieces = explode(' ',$search);
if ($valid_receipt && count($pieces) == 2) {
$this->db->where('sales.sale_id', $pieces[1]);
} else {
// open parentheses
$this->db->where("( last_name LIKE '%" . $search . "%' OR ".
"first_name LIKE '%" . $search . "%' OR " .
"CONCAT( first_name,' ',last_name ) LIKE '%" . $search . "%')");
// close parentheses
}
}
if ($only_invoices != 0) {
$this->db->where('invoice_number <> ', 'NULL');
}
return $this->db->get()->num_rows();
}
function get_invoice_count()
{
$this->db->from('sales');
@@ -184,7 +145,7 @@ class Sale extends CI_Model
return $success;
}
function save ($items,$customer_id,$employee_id,$comment,$invoice_number,$payments,$sale_id=false)
function save($items, $customer_id, $employee_id, $comment, $invoice_number, $payments, $sale_id=false)
{
if(count($items)==0)
return -1;
@@ -244,7 +205,7 @@ class Sale extends CI_Model
//Update stock quantity
$item_quantity = $this->Item_quantities->get_item_quantity($item['item_id'], $item['item_location']);
$this->Item_quantities->save(array('quantity'=>$item_quantity->quantity - $item['quantity'],
$this->Item_quantities->save(array('quantity'=>$item_quantity->quantity - $item['quantity'],
'item_id'=>$item['item_id'],
'location_id'=>$item['item_location']), $item['item_id'], $item['item_location']);
@@ -290,7 +251,7 @@ class Sale extends CI_Model
return $sale_id;
}
function delete_list($sale_ids, $employee_id,$update_inventory=TRUE)
function delete_list($sale_ids, $employee_id, $update_inventory=TRUE)
{
$result = TRUE;
foreach($sale_ids as $sale_id) {
@@ -299,7 +260,7 @@ class Sale extends CI_Model
return $result;
}
function delete($sale_id,$employee_id,$update_inventory=TRUE)
function delete($sale_id, $employee_id, $update_inventory=TRUE)
{
// start a transaction to assure data integrity
$this->db->trans_start();
@@ -381,29 +342,29 @@ class Sale extends CI_Model
//We create a temp table that allows us to do easy report/sales queries
public function create_sales_items_temp_table()
{
if ($this->config->item('tax_included'))
{
$total = "1";
$subtotal = "(1 - (SUM(1 - 100/(100+percent))))";
$tax="(SUM(1 - 100/(100+percent)))";
}
else
{
$tax = "(SUM(percent)/100)";
$total = "(1+(SUM(percent/100)))";
$subtotal = "1";
}
if ($this->config->item('tax_included'))
{
$total = "1";
$subtotal = "(1 - (SUM(1 - 100/(100+percent))))";
$tax="(SUM(1 - 100/(100+percent)))";
}
else
{
$tax = "(SUM(percent)/100)";
$total = "(1+(SUM(percent/100)))";
$subtotal = "1";
}
$this->db->query("CREATE TEMPORARY TABLE IF NOT EXISTS ".$this->db->dbprefix('sales_items_temp')."
(SELECT date(sale_time) as sale_date, sale_time, ".$this->db->dbprefix('sales_items').".sale_id, comment,payments.payment_type, item_location, customer_id, employee_id,
(SELECT date(sale_time) as sale_date, sale_time, ".$this->db->dbprefix('sales_items').".sale_id, comment, payments.payment_type, payments.sale_payment_amount, item_location, customer_id, employee_id,
".$this->db->dbprefix('items').".item_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, SUM(percent) as item_tax_percent,
discount_percent, ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*$subtotal,2) as subtotal,
".$this->db->dbprefix('sales_items').".line as line, serialnumber, ".$this->db->dbprefix('sales_items').".description as description,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*$total, 2) as total,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*$tax, 2) as tax,
ROUND((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)- (item_cost_price*quantity_purchased), 2) as profit,
(item_cost_price*quantity_purchased) as cost
(item_cost_price*quantity_purchased) as cost,
invoice_number
FROM ".$this->db->dbprefix('sales_items')."
INNER JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales').'.sale_id'."
INNER JOIN ".$this->db->dbprefix('items')." ON ".$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('items').'.item_id'."

View File

@@ -119,7 +119,6 @@ function expand_handler(event)
if ($(event.target).text() == '+')
{
$(event.target).text('-');
}
else

View File

@@ -1,4 +1,5 @@
<?php $this->load->view("partial/header"); ?>
<script type="text/javascript">
$(document).ready(function()
{
@@ -7,26 +8,25 @@ $(document).ready(function()
enable_row_selection();
enable_search('<?php echo site_url("$controller_name/suggest")?>','<?php echo $this->lang->line("common_confirm_search")?>');
enable_delete('<?php echo $this->lang->line($controller_name."_confirm_delete")?>','<?php echo $this->lang->line($controller_name."_none_selected")?>');
$("#search_filter_section select").change(function() {
do_search(true);
return false;
});
var show_renumber = function() {
var value = $("#payment_type").val();
var value = $("#only_invoices").val();
var $button = $("#update_invoice_numbers").parents("li");
$button.toggle(value === "1");
};
$("#payment_type").change(show_renumber);
$("#only_invoices").change(show_renumber);
show_renumber();
$("#update_invoice_numbers").click(function() {
$.ajax({url : "<?php echo site_url('sales') ?>/update_invoice_numbers", dataType: 'json', success : post_bulk_form_submit });
return false;
});
});
function post_form_submit(response)
@@ -58,18 +58,19 @@ function post_bulk_form_submit(response)
}
}
function show_hide_search_filter(search_filter_section, switchImgTag) {
function show_hide_search_filter(search_filter_section, switchImgTag)
{
var ele = document.getElementById(search_filter_section);
var imageEle = document.getElementById(switchImgTag);
if(ele.style.display == "block")
{
ele.style.display = "none";
imageEle.innerHTML = '<img src=" <?php echo base_url()?>images/plus.png" style="border:0;outline:none;padding:0px;margin:0px;position:relative;top:-5px;" >';
ele.style.display = "none";
imageEle.innerHTML = '<img src=" <?php echo base_url()?>images/plus.png" style="border:0;outline:none;padding:0px;margin:0px;position:relative;top:-5px;" >';
}
else
{
ele.style.display = "block";
imageEle.innerHTML = '<img src=" <?php echo base_url()?>images/minus.png" style="border:0;outline:none;padding:0px;margin:0px;position:relative;top:-5px;" >';
ele.style.display = "block";
imageEle.innerHTML = '<img src=" <?php echo base_url()?>images/minus.png" style="border:0;outline:none;padding:0px;margin:0px;position:relative;top:-5px;" >';
}
}
@@ -121,23 +122,20 @@ function init_table_sorting()
});
//Only init if there is more than one row
if($('.tablesorter tbody tr').length >1)
if($('.tablesorter tbody tr').length > 1)
{
$("#sortable_table").tablesorter(
{
sortList: [[1,1], [5,1]],
sortList: [[1,0]],
dateFormat: 'dd-mm-yyyy',
headers:
{
1: { sorter: 'datetime'},
6: { sorter: 'invoice_number'},
7: { sorter: false}
0: { sorter: false},
8: { sorter: false}
}
});
}
}
</script>
<div id="title_bar">
@@ -151,20 +149,14 @@ function init_table_sorting()
</div>
<?php echo form_open("$controller_name/search",array('id'=>'search_form')); ?>
<div id="search_filter_section" style="display: <?php echo isset($search_section_state)? ( ($search_section_state)? 'block' : 'none') : 'none';?>;background-color:#EEEEEE;">
<?php echo form_label($this->lang->line('sales_invoice_filter').' '.':', 'invoices_filter');?>
&nbsp;
<?php echo form_dropdown('payment_type', $payment_types, $payment_type, 'id="payment_type"');?>
<?php echo form_label($this->lang->line('sales_invoice_filter').' '.':', 'invoices_filter');?>
<?php echo form_checkbox(array('name'=>'only_invoices','id'=>'only_invoices','value'=>1,'checked'=> isset($only_invoices)? ( ($only_invoices)? 1 : 0) : 0));?>
<input type="hidden" name="search_section_state" id="search_section_state" value="<?php echo isset($search_section_state)? ( ($search_section_state)? 'block' : 'none') : 'none';?>" />
</div>
<div id="table_action_header">
<ul>
<li class="float_left"><span><?php echo anchor($controller_name . "/delete",$this->lang->line("common_delete"),array('id'=>'delete')); ?></span></li>
<!-- li class="float_left"><span><?php echo anchor($controller_name . "/update_invoice_numbers", $this->lang->line('sales_invoice_update'),array('id'=>'update_invoice_numbers')); ?></span></li-->
<li class="float_right">
<img src='<?php echo base_url()?>images/spinner_small.gif' alt='spinner' id='spinner' />
<input type="text" name ='search' id='search'/>
<input type="hidden" name ='limit_from' id='limit_from'/>
</li>
<!-- li class="float_left"><span><?php echo anchor($controller_name . "/update_invoice_numbers", $this->lang->line('sales_invoice_update'),array('id'=>'update_invoice_numbers')); ?></span></li -->
</ul>
</div>
<?php echo form_close(); ?>
@@ -172,5 +164,10 @@ function init_table_sorting()
<div id="table_holder">
<?php echo $manage_table; ?>
</div>
<div id="table_summary">
<?php echo $payments_summary_data; ?>
</div>
<div id="feedback_bar"></div>
<?php $this->load->view("partial/footer"); ?>