From e22e081865ca3d6ff1d593610a57a480f62a4c69 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Fri, 11 Nov 2016 19:24:23 +0000 Subject: [PATCH] Refactoring and performance improvement of Summary reports (#964) --- application/controllers/Reports.php | 40 --------- application/models/reports/Inventory_low.php | 4 - .../models/reports/Inventory_summary.php | 4 - application/models/reports/Report.php | 3 - .../models/reports/Summary_categories.php | 61 ++++---------- .../models/reports/Summary_customers.php | 65 ++++----------- .../models/reports/Summary_discounts.php | 56 ++----------- .../models/reports/Summary_employees.php | 61 ++++---------- application/models/reports/Summary_items.php | 61 ++++---------- .../models/reports/Summary_payments.php | 49 +---------- application/models/reports/Summary_report.php | 71 ++++++++++++++++ application/models/reports/Summary_sales.php | 61 ++++---------- .../models/reports/Summary_suppliers.php | 71 +++++----------- application/models/reports/Summary_taxes.php | 82 +++++++------------ 14 files changed, 207 insertions(+), 482 deletions(-) create mode 100644 application/models/reports/Summary_report.php diff --git a/application/controllers/Reports.php b/application/controllers/Reports.php index b10e4056c..b89c3de0e 100644 --- a/application/controllers/Reports.php +++ b/application/controllers/Reports.php @@ -45,8 +45,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_sales'); $model = $this->Summary_sales; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -82,8 +80,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_categories'); $model = $this->Summary_categories; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -119,8 +115,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_customers'); $model = $this->Summary_customers; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -156,8 +150,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_suppliers'); $model = $this->Summary_suppliers; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -193,8 +185,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_items'); $model = $this->Summary_items; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -230,8 +220,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_employees'); $model = $this->Summary_employees; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -267,8 +255,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_taxes'); $model = $this->Summary_taxes; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -302,8 +288,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_discounts'); $model = $this->Summary_discounts; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -334,8 +318,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_payments'); $model = $this->Summary_payments; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -402,8 +384,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_sales'); $model = $this->Summary_sales; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -441,8 +421,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_items'); $model = $this->Summary_items; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -479,8 +457,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_categories'); $model = $this->Summary_categories; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -515,8 +491,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_suppliers'); $model = $this->Summary_suppliers; - $model->create($inputs); - $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -550,8 +524,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_employees'); $model = $this->Summary_employees; - - $model->create($inputs); $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -586,8 +558,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_taxes'); $model = $this->Summary_taxes; - - $model->create($inputs); $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -622,8 +592,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_customers'); $model = $this->Summary_customers; - - $model->create($inputs); $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -660,8 +628,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_discounts'); $model = $this->Summary_discounts; - - $model->create($inputs); $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -698,8 +664,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Summary_payments'); $model = $this->Summary_payments; - - $model->create($inputs); $report_data = $model->getData($inputs); $summary = $this->xss_clean($model->getSummaryData($inputs)); @@ -1074,8 +1038,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Inventory_low'); $model = $this->Inventory_low; - $model->create($inputs); - $report_data = $model->getData($inputs); $tabular_data = array(); @@ -1122,8 +1084,6 @@ class Reports extends Secure_Controller $this->load->model('reports/Inventory_summary'); $model = $this->Inventory_summary; - $model->create($inputs); - $report_data = $model->getData($inputs); $tabular_data = array(); diff --git a/application/models/reports/Inventory_low.php b/application/models/reports/Inventory_low.php index 8cce9ec51..3e5123fa0 100644 --- a/application/models/reports/Inventory_low.php +++ b/application/models/reports/Inventory_low.php @@ -7,10 +7,6 @@ class Inventory_low extends Report parent::__construct(); } - public function create(array $inputs) - { - } - public function getDataColumns() { return array($this->lang->line('reports_item_name'), diff --git a/application/models/reports/Inventory_summary.php b/application/models/reports/Inventory_summary.php index d5d5c399b..27af10bcc 100644 --- a/application/models/reports/Inventory_summary.php +++ b/application/models/reports/Inventory_summary.php @@ -7,10 +7,6 @@ class Inventory_summary extends Report parent::__construct(); } - public function create(array $inputs) - { - } - public function getDataColumns() { return array($this->lang->line('reports_item_name'), diff --git a/application/models/reports/Report.php b/application/models/reports/Report.php index 8042581c7..a9d872b10 100644 --- a/application/models/reports/Report.php +++ b/application/models/reports/Report.php @@ -12,9 +12,6 @@ abstract class Report extends CI_Model $this->output->set_header('Pragma: no-cache'); } - // Create temporary table for fast queries - public abstract function create(array $inputs); - // Returns the column names used for the report public abstract function getDataColumns(); diff --git a/application/models/reports/Summary_categories.php b/application/models/reports/Summary_categories.php index 6f7750af6..482de4914 100644 --- a/application/models/reports/Summary_categories.php +++ b/application/models/reports/Summary_categories.php @@ -1,68 +1,37 @@ Sale->create_temp_table($inputs); - } - + public function getDataColumns() { return array($this->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) { - $this->db->select('category, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + items.category AS category, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $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('items AS items', 'sales_items.item_id = items.item_id', 'inner'); + $this->db->join('sales_items_taxes AS sales_items_taxes', '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', 'left outer'); + + $this->commonWhere($inputs); $this->db->group_by('category'); $this->db->order_by('category'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_customers.php b/application/models/reports/Summary_customers.php index b2dfab6f0..bbe7accf9 100644 --- a/application/models/reports/Summary_customers.php +++ b/application/models/reports/Summary_customers.php @@ -1,68 +1,37 @@ Sale->create_temp_table($inputs); - } - + public function getDataColumns() { return array($this->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) { - $this->db->select('customer_name AS customer, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + CONCAT(customer_p.first_name, " ", customer_p.last_name) AS customer, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $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('people AS customer_p', 'sales.customer_id = customer_p.person_id'); + $this->db->join('sales_items_taxes AS sales_items_taxes', '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', 'left outer'); - $this->db->group_by('customer_id'); - $this->db->order_by('customer_last_name'); + $this->commonWhere($inputs); + + $this->db->group_by('sales.customer_id'); + $this->db->order_by('customer_p.last_name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_discounts.php b/application/models/reports/Summary_discounts.php index 8388aaad9..1ac12f688 100644 --- a/application/models/reports/Summary_discounts.php +++ b/application/models/reports/Summary_discounts.php @@ -1,18 +1,12 @@ Sale->create_temp_table($inputs); - } - public function getDataColumns() { return array($this->lang->line('reports_discount_percent'), $this->lang->line('reports_count')); @@ -20,50 +14,18 @@ class Summary_discounts extends Report public function getData(array $inputs) { - $this->db->select('CONCAT(discount_percent, "%") AS discount_percent, count(*) AS count'); - $this->db->from('sales_items_temp'); + $this->db->select('CONCAT(sales_items.discount_percent, "%") AS discount_percent, count(*) AS count'); + $this->db->from('sales_items AS sales_items'); + $this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner'); + $this->db->where('discount_percent > 0'); - if ($inputs['location_id'] != 'all') - { - $this->db->where('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->commonWhere($inputs); - $this->db->group_by('sales_items_temp.discount_percent'); - $this->db->order_by('discount_percent'); + $this->db->group_by('sales_items.discount_percent'); + $this->db->order_by('sales_items.discount_percent'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_employees.php b/application/models/reports/Summary_employees.php index 595e33827..e42f05d45 100644 --- a/application/models/reports/Summary_employees.php +++ b/application/models/reports/Summary_employees.php @@ -1,18 +1,12 @@ Sale->create_temp_table($inputs); - } - public function getDataColumns() { return array($this->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')); @@ -20,49 +14,24 @@ class Summary_employees extends Report public function getData(array $inputs) { - $this->db->select('employee_name AS employee, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + CONCAT(employee_p.first_name, " ", employee_p.last_name) AS employee, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $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('people AS employee_p', 'sales.employee_id = employee_p.person_id'); + $this->db->join('sales_items_taxes AS sales_items_taxes', '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', 'left outer'); - $this->db->group_by('employee_id'); - $this->db->order_by('employee_name'); + $this->commonWhere($inputs); + + $this->db->group_by('sales.employee_id'); + $this->db->order_by('employee_p.last_name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_items.php b/application/models/reports/Summary_items.php index e694898f8..4a2c3ffa1 100644 --- a/application/models/reports/Summary_items.php +++ b/application/models/reports/Summary_items.php @@ -1,17 +1,11 @@ Sale->create_temp_table($inputs); - } public function getDataColumns() { @@ -20,49 +14,24 @@ class Summary_items extends Report public function getData(array $inputs) { - $this->db->select('name, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + items.name AS name, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $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('items AS items', 'sales_items.item_id = items.item_id', 'inner'); + $this->db->join('sales_items_taxes AS sales_items_taxes', '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', 'left outer'); - $this->db->group_by('item_id'); - $this->db->order_by('name'); + $this->commonWhere($inputs); + + $this->db->group_by('items.item_id'); + $this->db->order_by('items.name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_payments.php b/application/models/reports/Summary_payments.php index c4b4c9963..f494f8e18 100644 --- a/application/models/reports/Summary_payments.php +++ b/application/models/reports/Summary_payments.php @@ -1,18 +1,12 @@ Sale->create_temp_table($inputs); - } - public function getDataColumns() { return array($this->lang->line('reports_payment_type'), $this->lang->line('reports_count'), $this->lang->line('sales_amount_tendered')); @@ -22,22 +16,9 @@ class Summary_payments extends Report { $this->db->select('sales_payments.payment_type, count(*) AS count, SUM(payment_amount) AS payment_amount'); $this->db->from('sales_payments'); - $this->db->join('sales', 'sales.sale_id=sales_payments.sale_id'); - $this->db->where('DATE(sale_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date'])); + $this->db->join('sales AS sales', 'sales.sale_id = sales_payments.sale_id'); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } - - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('payment_amount > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('payment_amount < 0'); - } + $this->commonWhere($inputs); $this->db->group_by("payment_type"); @@ -65,27 +46,5 @@ class Summary_payments extends Report return $payments; } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_report.php b/application/models/reports/Summary_report.php new file mode 100644 index 000000000..b1e40c1ff --- /dev/null +++ b/application/models/reports/Summary_report.php @@ -0,0 +1,71 @@ +config->item('tax_included')) + { + $total = '1'; + $subtotal = '(1 - (SUM(1 - 100 / (100 + sales_items_taxes.percent))))'; + $tax = '(SUM(1 - 100 / (100 + sales_items_taxes.percent)))'; + } + else + { + $tax = '(SUM(sales_items_taxes.percent) / 100)'; + $total = '(1 + (SUM(sales_items_taxes.percent / 100)))'; + $subtotal = '1'; + } + + $sale_total = 'SUM(sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.item_unit_price * sales_items.quantity_purchased * sales_items.discount_percent / 100)'; + $sale_cost = 'SUM(sales_items.item_cost_price * sales_items.quantity_purchased)'; + + $decimals = totals_decimals(); + + $this->db->select(" + ROUND($sale_total * $subtotal, $decimals) AS subtotal, + IFNULL(ROUND($sale_total * $total, $decimals), ROUND($sale_total * $subtotal, $decimals)) AS total, + IFNULL(ROUND($sale_total * $tax, $decimals), 0) AS tax, + ROUND($sale_cost, $decimals) AS cost, + ROUND($sale_total - $sale_cost, $decimals) AS profit + "); + } + + protected function commonWhere(array $inputs) + { + $this->db->where('DATE(sales.sale_time) 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($inputs['sale_type'] == 'sales') + { + $this->db->where('quantity_purchased > 0'); + } + elseif($inputs['sale_type'] == 'returns') + { + $this->db->where('quantity_purchased < 0'); + } + } + + public function getSummaryData(array $inputs) + { + $this->commonSelect($inputs); + + $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 AS sales_items_taxes', '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', 'left outer'); + + $this->commonWhere($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 c1cf0bf06..9fd6027ae 100644 --- a/application/models/reports/Summary_sales.php +++ b/application/models/reports/Summary_sales.php @@ -1,69 +1,36 @@ Sale->create_temp_table($inputs); - } public function getDataColumns() { return array($this->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) - { - $this->db->select('sale_date, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); + { + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $this->db->select(' + DATE(sales.sale_time) AS sale_date, + SUM(sales_items.quantity_purchased) AS quantity_purchased + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $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 AS sales_items_taxes', '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', 'left outer'); + + $this->commonWhere($inputs); $this->db->group_by('sale_date'); $this->db->order_by('sale_date'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } - } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_suppliers.php b/application/models/reports/Summary_suppliers.php index 0a9449243..f91e521b0 100644 --- a/application/models/reports/Summary_suppliers.php +++ b/application/models/reports/Summary_suppliers.php @@ -1,72 +1,39 @@ Sale->create_temp_table($inputs); - } - + public function getDataColumns() { return array($this->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) { - $this->db->select('CONCAT(company_name, " (", first_name, " ", last_name, ")") AS supplier, SUM(quantity_purchased) AS quantity_purchased, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->join('suppliers', 'suppliers.person_id = sales_items_temp.supplier_id'); - $this->db->join('people', 'suppliers.person_id = people.person_id'); + $this->commonSelect($inputs); - if ($inputs['location_id'] != 'all') - { - $this->db->where('item_location', $inputs['location_id']); - } + $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 + '); - if ($inputs['sale_type'] == 'sales') - { - $this->db->where('quantity_purchased > 0'); - } - elseif ($inputs['sale_type'] == 'returns') - { - $this->db->where('quantity_purchased < 0'); - } + $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('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->db->join('sales_items_taxes AS sales_items_taxes', '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', 'left outer'); - $this->db->group_by('supplier_id'); - $this->db->order_by('last_name'); + $this->commonWhere($inputs); + + $this->db->group_by('items.supplier_id'); + $this->db->order_by('supplier_p.last_name'); return $this->db->get()->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - $this->db->join('suppliers', 'suppliers.person_id = sales_items_temp.supplier_id'); - $this->db->join('people', 'suppliers.person_id = people.person_id'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file diff --git a/application/models/reports/Summary_taxes.php b/application/models/reports/Summary_taxes.php index b91fbf06f..045b16dd0 100644 --- a/application/models/reports/Summary_taxes.php +++ b/application/models/reports/Summary_taxes.php @@ -1,92 +1,66 @@ Sale->create_temp_table($inputs); - } - + public function getDataColumns() { return array($this->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')); } - + public function getData(array $inputs) { $quantity_cond = ''; if ($inputs['sale_type'] == 'sales') { - $quantity_cond = 'and quantity_purchased > 0'; + $quantity_cond = 'AND quantity_purchased > 0'; } elseif ($inputs['sale_type'] == 'returns') { - $quantity_cond = 'and quantity_purchased < 0'; + $quantity_cond = 'AND quantity_purchased < 0'; } if ($inputs['location_id'] != 'all') { - $quantity_cond .= 'and item_location = '. $this->db->escape($inputs['location_id']); + $quantity_cond .= 'AND item_location = '. $this->db->escape($inputs['location_id']); } if ($this->config->item('tax_included')) { - $total = "1"; - $subtotal = "(100/(100+percent))"; - $tax = "(1 - (100/(100 +percent)))"; + $total = '1'; + $subtotal = '(100/(100+percent))'; + $tax = '(1 - (100/(100 +percent)))'; } else { - $tax = "(percent/100)"; - $total = "(1+(percent/100))"; - $subtotal = "1"; + $tax = '(percent/100)'; + $total = '(1+(percent/100))'; + $subtotal = '1'; } - + $decimals = totals_decimals(); $query = $this->db->query("SELECT percent, count(*) AS count, SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax - FROM (SELECT name, CONCAT(ROUND(percent, $decimals), '%') AS percent, - ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent /100) * $subtotal, $decimals) AS subtotal, - ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent /100) * $total, $decimals) AS total, - ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent /100) * $tax, $decimals) AS tax - FROM ".$this->db->dbprefix('sales_items_taxes')." - JOIN ".$this->db->dbprefix('sales_items')." ON " - .$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales_items_taxes').'.sale_id'." AND " - .$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('sales_items_taxes').'.item_id'." AND " - .$this->db->dbprefix('sales_items').'.line='.$this->db->dbprefix('sales_items_taxes').'.line' - ." JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items_taxes').".sale_id=".$this->db->dbprefix('sales').".sale_id - WHERE date(sale_time) BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " $quantity_cond) AS temp_taxes - GROUP BY percent"); + FROM ( + SELECT + CONCAT(ROUND(percent, $decimals), '%') AS percent, + ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent /100) * $subtotal, $decimals) AS subtotal, + ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent /100) * $total, $decimals) AS total, + ROUND((item_unit_price * quantity_purchased - item_unit_price * quantity_purchased * discount_percent /100) * $tax, $decimals) AS tax + FROM ".$this->db->dbprefix('sales_items_taxes')." + JOIN ".$this->db->dbprefix('sales_items')." ON " + .$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales_items_taxes').'.sale_id'." AND " + .$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('sales_items_taxes').'.item_id'." AND " + .$this->db->dbprefix('sales_items').'.line='.$this->db->dbprefix('sales_items_taxes').'.line'." + JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items_taxes').".sale_id=".$this->db->dbprefix('sales').".sale_id + WHERE date(sale_time) BETWEEN " . $this->db->escape($inputs['start_date']) . " AND " . $this->db->escape($inputs['end_date']) . " $quantity_cond) AS temp_taxes + GROUP BY percent"); return $query->result_array(); } - - public function getSummaryData(array $inputs) - { - $this->db->select('SUM(subtotal) AS subtotal, SUM(total) AS total, SUM(tax) AS tax, SUM(cost) AS cost, SUM(profit) AS profit'); - $this->db->from('sales_items_temp'); - - if ($inputs['location_id'] != 'all') - { - $this->db->where('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'); - } - - return $this->db->get()->row_array(); - } } ?> \ No newline at end of file