Refactoring and performance improvement of Summary reports (#964)

This commit is contained in:
FrancescoUK
2016-11-11 19:24:23 +00:00
parent 123cb25bf5
commit e22e081865
14 changed files with 207 additions and 482 deletions

View File

@@ -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();

View File

@@ -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'),

View File

@@ -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'),

View File

@@ -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();

View File

@@ -1,68 +1,37 @@
<?php
require_once("Report.php");
class Summary_categories extends Report
require_once("Summary_report.php");
class Summary_categories extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -1,68 +1,37 @@
<?php
require_once("Report.php");
class Summary_customers extends Report
require_once("Summary_report.php");
class Summary_customers extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -1,18 +1,12 @@
<?php
require_once("Report.php");
class Summary_discounts extends Report
require_once("Summary_report.php");
class Summary_discounts extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -1,18 +1,12 @@
<?php
require_once("Report.php");
class Summary_employees extends Report
require_once("Summary_report.php");
class Summary_employees extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -1,17 +1,11 @@
<?php
require_once("Report.php");
class Summary_items extends Report
require_once("Summary_report.php");
class Summary_items extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -1,18 +1,12 @@
<?php
require_once("Report.php");
class Summary_payments extends Report
require_once("Summary_report.php");
class Summary_payments extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -0,0 +1,71 @@
<?php
require_once("Report.php");
abstract class Summary_report extends Report
{
function __construct()
{
parent::__construct();
}
protected function commonSelect(array $inputs)
{
if($this->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();
}
}
?>

View File

@@ -1,69 +1,36 @@
<?php
require_once("Report.php");
class Summary_sales extends Report
require_once("Summary_report.php");
class Summary_sales extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -1,72 +1,39 @@
<?php
require_once("Report.php");
class Summary_suppliers extends Report
require_once("Summary_report.php");
class Summary_suppliers extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>

View File

@@ -1,92 +1,66 @@
<?php
require_once("Report.php");
class Summary_taxes extends Report
require_once("Summary_report.php");
class Summary_taxes extends Summary_report
{
function __construct()
{
parent::__construct();
}
public function create(array $inputs)
{
//Create our temp tables to work with the data in our report
$this->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();
}
}
?>