From 5267eadc565e70c65f96f7f35ae0b1af3df26cbb Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Fri, 18 Nov 2016 16:34:41 +0000 Subject: [PATCH] Make Summary reports more OOD (#976) --- .../models/reports/Detailed_receivings.php | 4 +- application/models/reports/Detailed_sales.php | 4 +- application/models/reports/Inventory_low.php | 4 +- .../models/reports/Inventory_summary.php | 4 +- application/models/reports/Report.php | 3 +- .../models/reports/Specific_customer.php | 4 +- .../models/reports/Specific_discount.php | 4 +- .../models/reports/Specific_employee.php | 4 +- .../models/reports/Summary_categories.php | 22 ++++--- .../models/reports/Summary_customers.php | 22 ++++--- .../models/reports/Summary_discounts.php | 10 +-- .../models/reports/Summary_employees.php | 24 +++++--- application/models/reports/Summary_items.php | 24 +++++--- .../models/reports/Summary_payments.php | 10 +-- application/models/reports/Summary_report.php | 61 ++++++++++++++++--- application/models/reports/Summary_sales.php | 21 +++---- .../models/reports/Summary_suppliers.php | 22 ++++--- application/models/reports/Summary_taxes.php | 13 ++-- 18 files changed, 173 insertions(+), 87 deletions(-) diff --git a/application/models/reports/Detailed_receivings.php b/application/models/reports/Detailed_receivings.php index c02e85163..a1d4e7064 100644 --- a/application/models/reports/Detailed_receivings.php +++ b/application/models/reports/Detailed_receivings.php @@ -1,5 +1,7 @@ -lang->line('reports_category'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - public function getData(array $inputs) + protected function _select(array $inputs) { - $this->commonSelect($inputs); + parent::_select($inputs); $this->db->select(' items.category AS category, SUM(sales_items.quantity_purchased) AS quantity_purchased '); + } - $this->commonFrom(); + protected function _from() + { + parent::_from(); $this->db->join('items AS items', 'sales_items.item_id = items.item_id', 'inner'); + } - $this->commonWhere($inputs); - + protected function _group_order() + { $this->db->group_by('category'); $this->db->order_by('category'); - - return $this->db->get()->result_array(); } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_customers.php b/application/models/reports/Summary_customers.php index a479e8f61..fcd38ffa5 100644 --- a/application/models/reports/Summary_customers.php +++ b/application/models/reports/Summary_customers.php @@ -1,5 +1,7 @@ -lang->line('reports_customer'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - public function getData(array $inputs) + protected function _select(array $inputs) { - $this->commonSelect($inputs); + parent::_select($inputs); $this->db->select(' CONCAT(customer_p.first_name, " ", customer_p.last_name) AS customer, SUM(sales_items.quantity_purchased) AS quantity_purchased '); + } - $this->commonFrom(); + protected function _from() + { + parent::_from(); $this->db->join('people AS customer_p', 'sales.customer_id = customer_p.person_id'); + } - $this->commonWhere($inputs); - + protected function _group_order() + { $this->db->group_by('sales.customer_id'); $this->db->order_by('customer_p.last_name'); - - return $this->db->get()->result_array(); } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_discounts.php b/application/models/reports/Summary_discounts.php index 1ac12f688..ff014a8ba 100644 --- a/application/models/reports/Summary_discounts.php +++ b/application/models/reports/Summary_discounts.php @@ -1,5 +1,7 @@ -lang->line('reports_discount_percent'), $this->lang->line('reports_count')); } - + public function getData(array $inputs) { $this->db->select('CONCAT(sales_items.discount_percent, "%") AS discount_percent, count(*) AS count'); @@ -20,7 +22,7 @@ class Summary_discounts extends Summary_report $this->db->where('discount_percent > 0'); - $this->commonWhere($inputs); + $this->_where($inputs); $this->db->group_by('sales_items.discount_percent'); $this->db->order_by('sales_items.discount_percent'); diff --git a/application/models/reports/Summary_employees.php b/application/models/reports/Summary_employees.php index 1cc275670..dd3e73ba2 100644 --- a/application/models/reports/Summary_employees.php +++ b/application/models/reports/Summary_employees.php @@ -1,5 +1,7 @@ -lang->line('reports_employee'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - - public function getData(array $inputs) + + protected function _select(array $inputs) { - $this->commonSelect($inputs); + parent::_select($inputs); $this->db->select(' CONCAT(employee_p.first_name, " ", employee_p.last_name) AS employee, SUM(sales_items.quantity_purchased) AS quantity_purchased '); + } - $this->commonFrom(); + protected function _from() + { + parent::_from(); $this->db->join('people AS employee_p', 'sales.employee_id = employee_p.person_id'); + } - $this->commonWhere($inputs); - + protected function _group_order() + { $this->db->group_by('sales.employee_id'); $this->db->order_by('employee_p.last_name'); - - return $this->db->get()->result_array(); } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_items.php b/application/models/reports/Summary_items.php index 9da526a58..4b4904517 100644 --- a/application/models/reports/Summary_items.php +++ b/application/models/reports/Summary_items.php @@ -1,5 +1,7 @@ -lang->line('reports_item'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - - public function getData(array $inputs) + + protected function _select(array $inputs) { - $this->commonSelect($inputs); + parent::_select($inputs); $this->db->select(' items.name AS name, SUM(sales_items.quantity_purchased) AS quantity_purchased '); + } - $this->commonFrom(); + protected function _from() + { + parent::_from(); $this->db->join('items AS items', 'sales_items.item_id = items.item_id', 'inner'); + } - $this->commonWhere($inputs); - + protected function _group_order() + { $this->db->group_by('items.item_id'); $this->db->order_by('items.name'); - - return $this->db->get()->result_array(); } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_payments.php b/application/models/reports/Summary_payments.php index f494f8e18..306e3a62d 100644 --- a/application/models/reports/Summary_payments.php +++ b/application/models/reports/Summary_payments.php @@ -1,5 +1,7 @@ -lang->line('reports_payment_type'), $this->lang->line('reports_count'), $this->lang->line('sales_amount_tendered')); } @@ -18,7 +20,7 @@ class Summary_payments extends Summary_report $this->db->from('sales_payments'); $this->db->join('sales AS sales', 'sales.sale_id = sales_payments.sale_id'); - $this->commonWhere($inputs); + $this->_where($inputs); $this->db->group_by("payment_type"); @@ -39,7 +41,7 @@ class Summary_payments extends Summary_report } } - if( $gift_card_count > 0 ) + if($gift_card_count > 0) { $payments[] = array('payment_type' => $this->lang->line('sales_giftcard'), 'count' => $gift_card_count, 'payment_amount' => $gift_card_amount); } diff --git a/application/models/reports/Summary_report.php b/application/models/reports/Summary_report.php index 2e4b9dd1e..1521acfab 100644 --- a/application/models/reports/Summary_report.php +++ b/application/models/reports/Summary_report.php @@ -1,5 +1,7 @@ -db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('sales_items_taxes_temp') . ' (INDEX(sale_id), INDEX(item_id)) ( @@ -52,14 +60,14 @@ abstract class Summary_report extends Report "); } - protected function commonFrom() + private function _common_from() { $this->db->from('sales_items AS sales_items'); $this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner'); $this->db->join('sales_items_taxes_temp AS sales_items_taxes', 'sales_items.sale_id = sales_items_taxes.sale_id AND sales_items.item_id = sales_items_taxes.item_id', 'left outer'); } - protected function commonWhere(array $inputs) + private function _common_where(array $inputs) { $this->db->where('DATE(sales.sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date'])); @@ -78,13 +86,50 @@ abstract class Summary_report extends Report } } + /* + + Protected class interface implemented by derived classes + + */ + + abstract protected function _get_data_columns(); + + protected function _select(array $inputs) { $this->_common_select($inputs); } + protected function _from() { $this->_common_from(); } + protected function _where(array $inputs) { $this->_common_where($inputs); } + protected function _group_order() {} + + /* + + Public interface implementing the base abstract class, in general it should not be extended unless there is a valid reason + + */ + + public function getDataColumns() + { + return $this->_get_data_columns(); + } + + public function getData(array $inputs) + { + $this->_select($inputs); + + $this->_from(); + + $this->_where($inputs); + + $this->_group_order(); + + return $this->db->get()->result_array(); + } + public function getSummaryData(array $inputs) { - $this->commonSelect($inputs); + $this->_common_select($inputs); - $this->commonFrom(); + $this->_common_from(); - $this->commonWhere($inputs); + $this->_common_where($inputs); return $this->db->get()->row_array(); } diff --git a/application/models/reports/Summary_sales.php b/application/models/reports/Summary_sales.php index fafb3da51..eb024a32a 100644 --- a/application/models/reports/Summary_sales.php +++ b/application/models/reports/Summary_sales.php @@ -1,5 +1,7 @@ -lang->line('reports_date'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - public function getData(array $inputs) + protected function _select(array $inputs) { - $this->commonSelect($inputs); + parent::_select($inputs); $this->db->select(' DATE(sales.sale_time) AS sale_date, SUM(sales_items.quantity_purchased) AS quantity_purchased '); - - $this->commonFrom(); - - $this->commonWhere($inputs); - + } + + protected function _group_order() + { $this->db->group_by('sale_date'); $this->db->order_by('sale_date'); - - return $this->db->get()->result_array(); } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_suppliers.php b/application/models/reports/Summary_suppliers.php index b073bef4d..a7c2da374 100644 --- a/application/models/reports/Summary_suppliers.php +++ b/application/models/reports/Summary_suppliers.php @@ -1,5 +1,7 @@ -lang->line('reports_supplier'), $this->lang->line('reports_quantity'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), $this->lang->line('reports_cost'), $this->lang->line('reports_profit')); } - public function getData(array $inputs) + protected function _select(array $inputs) { - $this->commonSelect($inputs); + parent::_select($inputs); $this->db->select(' CONCAT(supplier_c.company_name, " (", supplier_p.first_name, " ", supplier_p.last_name, ")") AS supplier, SUM(sales_items.quantity_purchased) AS quantity_purchased '); + } - $this->commonFrom(); + protected function _from() + { + parent::_from(); $this->db->join('items AS items', 'sales_items.item_id = items.item_id', 'inner'); $this->db->join('suppliers AS supplier_c', 'supplier_c.person_id = items.supplier_id'); $this->db->join('people AS supplier_p', 'supplier_c.person_id = supplier_p.person_id'); + } - $this->commonWhere($inputs); - + protected function _group_order() + { $this->db->group_by('items.supplier_id'); $this->db->order_by('supplier_p.last_name'); - - return $this->db->get()->result_array(); } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_taxes.php b/application/models/reports/Summary_taxes.php index f57fb696b..fe8d0b24f 100644 --- a/application/models/reports/Summary_taxes.php +++ b/application/models/reports/Summary_taxes.php @@ -1,5 +1,7 @@ -lang->line('reports_tax_percent'), $this->lang->line('reports_count'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax')); } @@ -15,16 +17,17 @@ class Summary_taxes extends Summary_report public function getData(array $inputs) { $quantity_cond = ''; - if ($inputs['sale_type'] == 'sales') + + if($inputs['sale_type'] == 'sales') { $quantity_cond = 'AND quantity_purchased > 0'; } - elseif ($inputs['sale_type'] == 'returns') + elseif($inputs['sale_type'] == 'returns') { $quantity_cond = 'AND quantity_purchased < 0'; } - if ($inputs['location_id'] != 'all') + if($inputs['location_id'] != 'all') { $quantity_cond .= 'AND item_location = '. $this->db->escape($inputs['location_id']); }