diff --git a/application/controllers/sales.php b/application/controllers/sales.php index 19c3378c4..a04489119 100644 --- a/application/controllers/sales.php +++ b/application/controllers/sales.php @@ -39,15 +39,10 @@ class Sales extends Secure_area $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); + $data['manage_table'] = get_sales_manage_table($report_data['sales'], $this); + $data['payments_summary'] = get_sales_manage_payments_summary($report_data['payments'], $report_data['sales'], $this); $this->load->view($data['controller_name'] . '/manage', $data); $this->_remove_duplicate_cookies(); } diff --git a/application/helpers/table_helper.php b/application/helpers/table_helper.php index 89b99bf86..6ab598787 100644 --- a/application/helpers/table_helper.php +++ b/application/helpers/table_helper.php @@ -1,6 +1,6 @@ '; @@ -22,7 +22,7 @@ function get_sales_manage_table($sales, $payments, $details, $controller) $table.="$header"; } $table.=''; - $table.=get_sales_manage_table_data_rows($sales, $payments, $details, $controller); + $table.=get_sales_manage_table_data_rows($sales, $controller); $table.=''; return $table; @@ -31,17 +31,17 @@ function get_sales_manage_table($sales, $payments, $details, $controller) /* Gets the html data rows for the sales. */ -function get_sales_manage_table_data_rows($sales, $payments, $details, $controller) +function get_sales_manage_table_data_rows($sales, $controller) { $CI =& get_instance(); - $table_data_rows=''; + $table_data_rows = ''; $sum_amount_tendered = 0; $sum_amount_due = 0; $sum_change_due = 0; foreach($sales as $key=>$sale) { - $table_data_rows.=get_sales_manage_sale_data_row($sale, $payments[$key], $controller); + $table_data_rows .= get_sales_manage_sale_data_row($sale, $controller); $sum_amount_tendered += $sale['amount_tendered']; $sum_amount_due += $sale['amount_due']; @@ -50,66 +50,41 @@ function get_sales_manage_table_data_rows($sales, $payments, $details, $controll if($table_data_rows == '') { - $table_data_rows.="
".$CI->lang->line('sales_no_sales_to_display')."
"; + $table_data_rows .= "
".$CI->lang->line('sales_no_sales_to_display')."
"; } else { + // empty line + //$table_data_rows .= "          "; // show a line in bold with the totals - $table_data_rows.=" ".$CI->lang->line('sales_total')."  ".to_currency($sum_amount_tendered)."".to_currency($sum_amount_due)."".to_currency($sum_change_due).""; + $table_data_rows .= " ".$CI->lang->line('sales_total')."  ".to_currency($sum_amount_tendered)."".to_currency($sum_amount_due)."".to_currency($sum_change_due).""; } return $table_data_rows; } -function get_sales_manage_sale_data_row($sale, $payments, $controller) +function get_sales_manage_sale_data_row($sale, $controller) { $CI =& get_instance(); $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=''; - $table_data_row.=''; - $table_data_row.=''.'POS ' . $sale[ 'sale_id' ]. ''; - $table_data_row.=''.date($CI->config->item('dateformat').' '.$CI->config->item('timeformat'), strtotime($sale[ 'sale_time' ])).''; - $table_data_row.=''.character_limiter( $sale[ 'customer_name' ], 25).''; - $table_data_row.=''.to_currency( $sale[ 'amount_tendered' ] ).''; - $table_data_row.=''.to_currency( $sale[ 'amount_due' ] ).''; - $table_data_row.=''.to_currency( $sale[ 'change_due' ] ).''; - $table_data_row.=''.$payment.''; - $table_data_row.=''.$sale[ 'invoice_number' ].''; + $table_data_row.=''; + $table_data_row.=''.'POS ' . $sale['sale_id'] . ''; + $table_data_row.=''.date( $CI->config->item('dateformat') . ' ' . $CI->config->item('timeformat'), strtotime($sale['sale_time']) ).''; + $table_data_row.=''.character_limiter( $sale['customer_name'], 25).''; + $table_data_row.=''.to_currency( $sale['amount_tendered'] ).''; + $table_data_row.=''.to_currency( $sale['amount_due'] ).''; + $table_data_row.=''.to_currency( $sale['change_due'] ).''; + $table_data_row.=''.$sale['payment_type'].''; + $table_data_row.=''.$sale['invoice_number'].''; $table_data_row.=''; - $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'))); + $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'))); $table_data_row.='    '; - $table_data_row.='' . $CI->lang->line('sales_show_receipt') . ''; + $table_data_row.='' . $CI->lang->line('sales_show_receipt') . ''; $table_data_row.='    '; - $table_data_row.='' . $CI->lang->line('sales_show_invoice') . ''; + $table_data_row.='' . $CI->lang->line('sales_show_invoice') . ''; $table_data_row.=''; $table_data_row.=''; @@ -119,11 +94,11 @@ function get_sales_manage_sale_data_row($sale, $payments, $controller) /* Get the sales payments summary */ -function get_sales_manage_payments_summary($payments_summary, $controller) +function get_sales_manage_payments_summary($payments_summary, $sales, $controller) { $CI =& get_instance(); $table=''; - + $table.=''; $table.=''; foreach($payments_summary as $key=>$summary) @@ -134,7 +109,19 @@ function get_sales_manage_payments_summary($payments_summary, $controller) $table.=''; foreach($payments_summary as $key=>$summary) { - $table.=''; + $amount = $summary['payment_amount']; + + // WARNING: the strong assumption here is that if a change is due it was a cash transaction always + // therefore we remove from the total cash amount any change due + if( $summary['payment_type'] == $CI->lang->line('sales_cash') ) + { + foreach($sales as $key=>$sale) + { + $amount -= $sale['change_due']; + } + } + + $table.=''; } $table.='
 '.$CI->lang->line('sales_total').''.to_currency( $summary['payment_amount'] ).''.to_currency( $amount ).'
'; @@ -267,7 +254,7 @@ function get_supplier_manage_table_data_rows($suppliers,$controller) if($suppliers->num_rows()==0) { - $table_data_rows.="
".$CI->lang->line('common_no_persons_to_display')."
"; + $table_data_rows.="
".$CI->lang->line('common_no_persons_to_display')."
"; } return $table_data_rows; @@ -343,7 +330,7 @@ function get_items_manage_table_data_rows($items,$controller) if($items->num_rows()==0) { - $table_data_rows.="
".$CI->lang->line('items_no_items_to_display')."
"; + $table_data_rows.="
".$CI->lang->line('items_no_items_to_display')."
"; } return $table_data_rows; @@ -438,7 +425,7 @@ function get_giftcards_manage_table_data_rows( $giftcards, $controller ) if($giftcards->num_rows()==0) { - $table_data_rows.="
".$CI->lang->line('giftcards_no_giftcards_to_display')."
"; + $table_data_rows.="
".$CI->lang->line('giftcards_no_giftcards_to_display')."
"; } return $table_data_rows; @@ -505,7 +492,7 @@ function get_item_kits_manage_table_data_rows( $item_kits, $controller ) if($item_kits->num_rows()==0) { - $table_data_rows.="
".$CI->lang->line('item_kits_no_item_kits_to_display')."
"; + $table_data_rows.="
".$CI->lang->line('item_kits_no_item_kits_to_display')."
"; } return $table_data_rows; diff --git a/application/models/sale.php b/application/models/sale.php index 50e85baf2..9f36ea41c 100644 --- a/application/models/sale.php +++ b/application/models/sale.php @@ -23,11 +23,11 @@ class Sale extends CI_Model } // get the sales data for the takings table - function get_data($inputs) + public function get_data($inputs) { $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); + sale_payment_amount as amount_tendered, total as amount_due, (sale_payment_amount - total) as change_due, payment_type, 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'); @@ -61,14 +61,7 @@ class Sale extends CI_Model } $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(); - } + $data['sales'] = $this->db->get()->result_array(); // get payment summary $this->db->select('sales_payments.payment_type, count(*) as count, sum(payment_amount) as payment_amount', false); @@ -92,7 +85,7 @@ class Sale extends CI_Model $this->db->group_by("payment_type"); - $data['payments_summary'] = $this->db->get()->result_array(); + $data['payments'] = $this->db->get()->result_array(); return $data; } @@ -117,7 +110,7 @@ class Sale extends CI_Model return $this->db->get(); } - function get_invoice_number_for_year($year='', $start_from = 0) + function get_invoice_number_for_year($year = '', $start_from = 0) { $year = $year == '' ? date('Y') : $year; $this->db->select("COUNT( 1 ) AS invoice_number_year", FALSE); @@ -369,7 +362,7 @@ class Sale extends CI_Model 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'." INNER JOIN (SELECT sale_id, SUM(payment_amount) AS sale_payment_amount, - GROUP_CONCAT(payment_type SEPARATOR ', ') AS payment_type FROM " .$this->db->dbprefix('sales_payments') . " GROUP BY sale_id) AS payments + GROUP_CONCAT(concat(payment_type,' ',payment_amount) SEPARATOR ', ') AS payment_type FROM " .$this->db->dbprefix('sales_payments') . " GROUP BY sale_id) AS payments ON " . $this->db->dbprefix('sales_items') . '.sale_id'. "=" . "payments.sale_id LEFT OUTER JOIN ".$this->db->dbprefix('suppliers')." ON ".$this->db->dbprefix('items').'.supplier_id='.$this->db->dbprefix('suppliers').'.person_id'." LEFT OUTER JOIN ".$this->db->dbprefix('sales_items_taxes')." ON " diff --git a/application/views/receivings/receiving.php b/application/views/receivings/receiving.php index bece3ccc2..6f7ef3154 100644 --- a/application/views/receivings/receiving.php +++ b/application/views/receivings/receiving.php @@ -78,7 +78,7 @@ if(count($cart)==0) ?>
lang->line('sales_no_items_in_cart'); ?>
- + - @@ -49,7 +48,6 @@ if($export_excel == 1){ - diff --git a/application/views/sales/manage.php b/application/views/sales/manage.php index d7eca4671..48f8f17ed 100755 --- a/application/views/sales/manage.php +++ b/application/views/sales/manage.php @@ -166,7 +166,7 @@ function init_table_sorting()
- +