diff --git a/application/models/reports/Summary_categories.php b/application/models/reports/Summary_categories.php index c642faab7..e7b3b7401 100644 --- a/application/models/reports/Summary_categories.php +++ b/application/models/reports/Summary_categories.php @@ -44,4 +44,4 @@ class Summary_categories extends Summary_report $this->db->order_by('category'); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_customers.php b/application/models/reports/Summary_customers.php index 3b41c1301..5a644b531 100644 --- a/application/models/reports/Summary_customers.php +++ b/application/models/reports/Summary_customers.php @@ -44,4 +44,4 @@ class Summary_customers extends Summary_report $this->db->order_by('customer_p.last_name'); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_discounts.php b/application/models/reports/Summary_discounts.php index 156f58238..338fec5b7 100644 --- a/application/models/reports/Summary_discounts.php +++ b/application/models/reports/Summary_discounts.php @@ -32,4 +32,4 @@ class Summary_discounts extends Summary_report return $this->db->get()->result_array(); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_employees.php b/application/models/reports/Summary_employees.php index 788c2b720..2e3997a1b 100644 --- a/application/models/reports/Summary_employees.php +++ b/application/models/reports/Summary_employees.php @@ -44,4 +44,4 @@ class Summary_employees extends Summary_report $this->db->order_by('employee_p.last_name'); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_items.php b/application/models/reports/Summary_items.php index c107479f4..30a57039f 100644 --- a/application/models/reports/Summary_items.php +++ b/application/models/reports/Summary_items.php @@ -44,4 +44,4 @@ class Summary_items extends Summary_report $this->db->order_by('items.name'); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_payments.php b/application/models/reports/Summary_payments.php index 1df4763bd..aae3131cc 100644 --- a/application/models/reports/Summary_payments.php +++ b/application/models/reports/Summary_payments.php @@ -17,37 +17,29 @@ class Summary_payments extends Summary_report array('amount_tendered' => $this->lang->line('sales_amount_tendered'), 'sorter' => 'number_sorter')); } + protected function _where(array $inputs) + { + $this->db->where('DATE(sales.sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date'])); + } + public function getData(array $inputs) { - $w = 'WHERE DATE(salesx.sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']) . ' '; + $this->db->select('sales_payments.payment_type, count(*) AS count, SUM(sales_payments.payment_amount) AS payment_amount'); + $this->db->from('sales_payments AS sales_payments'); + $this->db->join('sales AS sales', 'sales.sale_id = sales_payments.sale_id'); - if (!empty($inputs['sale_type']) && $inputs['sale_type'] != 'all') - { - $w .= ' and salesx.sales_type = ' . $this->db->escape($inputs['sale_type']) . ' '; - } + $this->_where($inputs); - if (!empty($inputs['location']) && $inputs['location'] != 'all') - { - $w .= ' and salesx.location = ' . $this->db->escape($inputs['location']) . ' '; - } + $this->db->group_by("payment_type"); - $sql = 'select sales_payments.payment_type, count(*) AS count, SUM(sales_payments.payment_amount) AS payment_amount - from ' . $this->db->dbprefix('sales_payments') . ' as sales_payments - join (select sales_items.sale_id, case when sum(sales_items.quantity_purchased) > 0 then \'sales\' else \'returns\' end as sales_type, sales_items.item_location as location, max(sales.sale_time) as sale_time - from ' . $this->db->dbprefix('sales_items') . ' as sales_items - join ' . $this->db->dbprefix('sales') . ' as sales - group by sales_items.sale_id, sales_items.item_location) as salesx on sales_payments.sale_id = salesx.sale_id ' - . $w - . ' group by payment_type'; - - $payments = $this->db->query($sql)->result_array(); + $payments = $this->db->get()->result_array(); // consider Gift Card as only one type of payment and do not show "Gift Card: 1, Gift Card: 2, etc." in the total $gift_card_count = 0; $gift_card_amount = 0; foreach($payments as $key=>$payment) { - if( strstr($payment['payment_type'], $this->lang->line('sales_giftcard')) != FALSE ) + if(strstr($payment['payment_type'], $this->lang->line('sales_giftcard')) != FALSE) { $gift_card_count += $payment['count']; $gift_card_amount += $payment['payment_amount']; @@ -65,4 +57,4 @@ class Summary_payments extends Summary_report return $payments; } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_report.php b/application/models/reports/Summary_report.php index 0c59ea55e..33773f089 100644 --- a/application/models/reports/Summary_report.php +++ b/application/models/reports/Summary_report.php @@ -90,17 +90,17 @@ abstract class Summary_report extends Report if($inputs['location_id'] != 'all') { - $this->db->where('item_location', $inputs['location_id']); + $this->db->where('sales_items.item_location', $inputs['location_id']); } if($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + { + $this->db->where('sales_items.quantity_purchased >= 0'); + } + elseif($inputs['sale_type'] == 'returns') + { + $this->db->where('sales_items.quantity_purchased < 0'); + } } /* @@ -146,9 +146,9 @@ abstract class Summary_report extends Report $this->_common_from(); - $this->_common_where($inputs); + $this->_where($inputs); return $this->db->get()->row_array(); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_sales.php b/application/models/reports/Summary_sales.php index db565b320..692df096d 100644 --- a/application/models/reports/Summary_sales.php +++ b/application/models/reports/Summary_sales.php @@ -26,7 +26,7 @@ class Summary_sales extends Summary_report parent::_select($inputs); $this->db->select(' - DATE(sales.sale_time) AS sale_date, + DATE(sales.sale_time) AS sale_date, SUM(sales_items.quantity_purchased) AS quantity_purchased '); } @@ -37,4 +37,4 @@ class Summary_sales extends Summary_report $this->db->order_by('sale_date'); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_suppliers.php b/application/models/reports/Summary_suppliers.php index c639d626b..97b478c51 100644 --- a/application/models/reports/Summary_suppliers.php +++ b/application/models/reports/Summary_suppliers.php @@ -46,4 +46,4 @@ class Summary_suppliers extends Summary_report $this->db->order_by('supplier_p.last_name'); } } -?> \ No newline at end of file +?> diff --git a/application/models/reports/Summary_taxes.php b/application/models/reports/Summary_taxes.php index 7dbafe6f9..92ddbe01e 100644 --- a/application/models/reports/Summary_taxes.php +++ b/application/models/reports/Summary_taxes.php @@ -19,6 +19,18 @@ class Summary_taxes extends Summary_report array('total' => $this->lang->line('reports_total'), 'sorter' => 'number_sorter')); } + protected function _where(array $inputs) + { + if(empty($this->config->item('date_or_time_format'))) + { + $this->db->where('DATE(sales.sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date'])); + } + else + { + $this->db->where('sales.sale_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date'])); + } + } + public function getData(array $inputs) { $where = ''; @@ -31,21 +43,6 @@ class Summary_taxes extends Summary_report { $where .= 'WHERE sale_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date'])); } - - if($inputs['sale_type'] == 'sales') - { - $where = 'AND quantity_purchased > 0'; - } - elseif($inputs['sale_type'] == 'returns') - { - $where = 'AND quantity_purchased < 0'; - } - - if($inputs['location_id'] != 'all') - { - $where .= 'AND item_location = '. $this->db->escape($inputs['location_id']); - } - if($this->config->item('tax_included')) { $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; @@ -81,4 +78,4 @@ class Summary_taxes extends Summary_report return $query->result_array(); } } -?> \ No newline at end of file +?>