From 4448348f7cfc9233e7a22188eb53d8b299c9e152 Mon Sep 17 00:00:00 2001 From: Steve Ireland Date: Tue, 4 Jul 2017 11:19:54 -0400 Subject: [PATCH] Improve sale status reporting --- application/language/en/reports_lang.php | 1 + application/models/Sale.php | 27 ++++++++++----- application/models/reports/Detailed_sales.php | 27 ++++++++++++--- .../models/reports/Specific_customer.php | 24 ++++++++++--- .../models/reports/Specific_discount.php | 24 ++++++++++--- .../models/reports/Specific_employee.php | 34 ++++++++++++++----- application/models/reports/Summary_report.php | 16 +++++++-- application/models/reports/Summary_taxes.php | 8 ++--- application/views/reports/date_input.php | 1 + application/views/reports/specific_input.php | 1 + 10 files changed, 125 insertions(+), 38 deletions(-) diff --git a/application/language/en/reports_lang.php b/application/language/en/reports_lang.php index a3c8634b7..e45eca380 100644 --- a/application/language/en/reports_lang.php +++ b/application/language/en/reports_lang.php @@ -52,6 +52,7 @@ $lang["reports_payments_summary_report"] = "Payments Summary Report"; $lang["reports_profit"] = "Profit"; $lang["reports_quantity"] = "Quantity"; $lang["reports_quantity_purchased"] = "Quantity Purchased"; +$lang["reports_quotes"] = "Quotes"; $lang["reports_received_by"] = "Received By"; $lang["reports_receiving_id"] = "Receiving ID"; $lang["reports_receiving_type"] = "Receiving Type"; diff --git a/application/models/Sale.php b/application/models/Sale.php index 411c14db2..702a6a1df 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -120,11 +120,11 @@ class Sale extends CI_Model if(empty($this->config->item('date_or_time_format'))) { - $where .= 'DATE(sales.sale_time) BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date']) . ' AND sales.sale_status = 0 '; + $where .= 'DATE(sales.sale_time) BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date']) . ' '; } else { - $where .= 'sales.sale_time BETWEEN ' . $this->db->escape(rawurldecode($filters['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($filters['end_date'])) . ' AND sales.sale_status = 0 '; + $where .= 'sales.sale_time BETWEEN ' . $this->db->escape(rawurldecode($filters['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($filters['end_date'])) . ' '; } // NOTE: temporary tables are created to speed up searches due to the fact that they are ortogonal to the main query @@ -240,11 +240,15 @@ class Sale extends CI_Model if($filters['sale_type'] == 'sales') { - $this->db->where('sales_items.quantity_purchased > 0'); + $this->db->where('sales.sale_status = 0 and sales_items.quantity_purchased > 0'); + } + elseif($filters['sale_type'] == 'quotes') + { + $this->db->where('sales.sale_status = 1 and sales.quote_number IS NOT NULL'); } elseif($filters['sale_type'] == 'returns') { - $this->db->where('sales_items.quantity_purchased < 0'); + $this->db->where('sales.sale_status = 0 and sales_items.quantity_purchased < 0'); } if($filters['only_invoices'] != FALSE) @@ -321,11 +325,15 @@ class Sale extends CI_Model if($filters['sale_type'] == 'sales') { - $this->db->where('payment_amount > 0'); + $this->db->where('sales.sale_status = 0 and payment_amount > 0'); + } + elseif($filters['sale_type'] == 'quotes') + { + $this->db->where('sales.sale_status = 1 and sales.quote_number IS NOT NULL'); } elseif($filters['sale_type'] == 'returns') { - $this->db->where('payment_amount < 0'); + $this->db->where('sales.sale_status = 0 and payment_amount < 0'); } if($filters['only_invoices'] != FALSE) @@ -1111,7 +1119,7 @@ class Sale extends CI_Model ON sales.sale_id = sales_items_taxes.sale_id INNER JOIN ' . $this->db->dbprefix('sales_items') . ' AS sales_items ON sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.line = sales_items_taxes.line - WHERE sales.sale_status = 0 AND ' . $where . ' + WHERE ' . $where . ' GROUP BY sale_id, item_id, line )' ); @@ -1126,7 +1134,7 @@ class Sale extends CI_Model FROM ' . $this->db->dbprefix('sales_payments') . ' AS payments INNER JOIN ' . $this->db->dbprefix('sales') . ' AS sales ON sales.sale_id = payments.sale_id - WHERE sales.sale_status = 0 AND ' . $where . ' + WHERE ' . $where . ' GROUP BY payments.sale_id )' ); @@ -1138,6 +1146,7 @@ class Sale extends CI_Model MAX(DATE(sales.sale_time)) AS sale_date, MAX(sales.sale_time) AS sale_time, sales.sale_id AS sale_id, + MAX(sales.sale_status) AS sale_status, MAX(sales.comment) AS comment, MAX(sales.invoice_number) AS invoice_number, MAX(sales.quote_number) AS quote_number, @@ -1188,7 +1197,7 @@ class Sale extends CI_Model ON sales.employee_id = employee.person_id LEFT OUTER JOIN ' . $this->db->dbprefix('sales_items_taxes_temp') . ' AS sales_items_taxes ON sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.item_id = sales_items_taxes.item_id AND sales_items.line = sales_items_taxes.line - WHERE sales.sale_status = 0 AND ' . $where . ' + WHERE ' . $where . ' GROUP BY sale_id, item_id, line )' ); diff --git a/application/models/reports/Detailed_sales.php b/application/models/reports/Detailed_sales.php index 97f8d41d8..d1eb34217 100644 --- a/application/models/reports/Detailed_sales.php +++ b/application/models/reports/Detailed_sales.php @@ -67,6 +67,7 @@ class Detailed_sales extends Report public function getData(array $inputs) { $this->db->select('sale_id, + MAX(sale_status) as sale_status, MAX(sale_date) AS sale_date, SUM(quantity_purchased) AS items_purchased, MAX(employee_name) AS employee_name, @@ -87,11 +88,19 @@ class Detailed_sales extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } $this->db->group_by('sale_id'); @@ -104,7 +113,7 @@ class Detailed_sales extends Report foreach($data['summary'] as $key=>$value) { - $this->db->select('name, category, quantity_purchased, item_location, serialnumber, description, subtotal, tax, total, cost, profit, discount_percent'); + $this->db->select('name, category, quantity_purchased, item_location, serialnumber, description, subtotal, tax, total, cost, profit, discount_percent, sale_status'); $this->db->from('sales_items_temp'); $this->db->where('sale_id', $value['sale_id']); $data['details'][$key] = $this->db->get()->result_array(); @@ -129,11 +138,19 @@ class Detailed_sales extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } return $this->db->get()->row_array(); diff --git a/application/models/reports/Specific_customer.php b/application/models/reports/Specific_customer.php index 17c34b0c0..2d3a19295 100644 --- a/application/models/reports/Specific_customer.php +++ b/application/models/reports/Specific_customer.php @@ -61,11 +61,19 @@ class Specific_customer extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } $this->db->group_by('sale_id'); @@ -99,11 +107,19 @@ class Specific_customer extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } return $this->db->get()->row_array(); diff --git a/application/models/reports/Specific_discount.php b/application/models/reports/Specific_discount.php index 36a22195c..03ad11ce7 100755 --- a/application/models/reports/Specific_discount.php +++ b/application/models/reports/Specific_discount.php @@ -59,11 +59,19 @@ class Specific_discount extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } $this->db->group_by('sale_id'); @@ -97,11 +105,19 @@ class Specific_discount extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } return $this->db->get()->row_array(); diff --git a/application/models/reports/Specific_employee.php b/application/models/reports/Specific_employee.php index 350df8283..1969b6ed0 100644 --- a/application/models/reports/Specific_employee.php +++ b/application/models/reports/Specific_employee.php @@ -60,13 +60,21 @@ class Specific_employee extends Report $this->db->where('employee_id', $inputs['employee_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('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); + } + elseif($inputs['sale_type'] == 'returns') + { + $this->db->where('sale_status = 0 and quantity_purchased < 0'); + } $this->db->group_by('sale_id'); $this->db->order_by('MAX(sale_date)'); @@ -99,11 +107,19 @@ class Specific_employee extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('quantity_purchased > 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } return $this->db->get()->row_array(); diff --git a/application/models/reports/Summary_report.php b/application/models/reports/Summary_report.php index e8c967514..0c36a32ce 100644 --- a/application/models/reports/Summary_report.php +++ b/application/models/reports/Summary_report.php @@ -51,7 +51,7 @@ abstract class Summary_report extends Report ON sales.sale_id = sales_items_taxes.sale_id INNER JOIN ' . $this->db->dbprefix('sales_items') . ' AS sales_items ON sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.line = sales_items_taxes.line - WHERE sales.sale_status = 0 AND ' . $where . ' + WHERE ' . $where . ' GROUP BY sale_id, item_id, line )' ); @@ -92,12 +92,22 @@ abstract class Summary_report extends Report if($inputs['sale_type'] == 'sales') { - $this->db->where('sales_items.quantity_purchased >= 0'); + $this->db->where('sale_status = 0 and quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'all') + { + $this->db->where('sale_status = 0'); + } + elseif($inputs['sale_type'] == 'quotes') + { + $this->db->where('sale_status = 1 and quote_number IS NOT NULL'); } elseif($inputs['sale_type'] == 'returns') { - $this->db->where('sales_items.quantity_purchased < 0'); + $this->db->where('sale_status = 0 and quantity_purchased < 0'); } + + } /** diff --git a/application/models/reports/Summary_taxes.php b/application/models/reports/Summary_taxes.php index 4fec642c0..d57a61510 100644 --- a/application/models/reports/Summary_taxes.php +++ b/application/models/reports/Summary_taxes.php @@ -18,11 +18,11 @@ class Summary_taxes extends Summary_report { if(empty($this->config->item('date_or_time_format'))) { - $this->db->where('sale_status = 0 AND DATE(sales.sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date'])); + $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('sale_status = 0 AND sales.sale_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date']))); + $this->db->where('sales.sale_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date']))); } } @@ -32,11 +32,11 @@ class Summary_taxes extends Summary_report if(empty($this->config->item('date_or_time_format'))) { - $where .= 'WHERE sale_status = 0 AND DATE(sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']); + $where .= 'WHERE DATE(sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']); } else { - $where .= 'WHERE sale_status = 0 AND sale_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date'])); + $where .= 'WHERE sale_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date'])); } if($this->config->item('tax_included')) diff --git a/application/views/reports/date_input.php b/application/views/reports/date_input.php index 98aecc8bf..42db42037 100644 --- a/application/views/reports/date_input.php +++ b/application/views/reports/date_input.php @@ -31,6 +31,7 @@ if(isset($error))
$this->lang->line('reports_all'), 'sales' => $this->lang->line('reports_sales'), + 'quotes' => $this->lang->line('reports_quotes'), 'returns' => $this->lang->line('reports_returns')), 'all', array('id'=>'input_type', 'class'=>'form-control')); ?>
$this->lang->line('reports_all'), 'sales' => $this->lang->line('reports_sales'), + 'quotes' => $this->lang->line('reports_quotes'), 'returns' => $this->lang->line('reports_returns')), 'all', 'id="input_type" class="form-control"'); ?>