From 490ce59bbb838719f966da7a2648621b8f6e2bf7 Mon Sep 17 00:00:00 2001 From: jekkos Date: Mon, 4 Nov 2013 23:44:53 +0100 Subject: [PATCH] Add specific discount report --- application/config/routes.php | 1 + application/controllers/reports.php | 52 +++++++++++++- application/language/english/reports_lang.php | 2 + application/language/english/sales_lang.php | 4 ++ .../models/reports/specific_discount.php | 70 +++++++++++++++++++ application/models/sale.php | 7 +- application/views/reports/date_input.php | 22 ++++-- application/views/reports/listing.php | 6 +- css/reports.css | 4 +- css/tables.css | 1 + 10 files changed, 155 insertions(+), 14 deletions(-) create mode 100755 application/models/reports/specific_discount.php diff --git a/application/config/routes.php b/application/config/routes.php index a56a9e9fe..331fff2ff 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -54,6 +54,7 @@ $route['reports/detailed_receivings'] = "reports/date_input"; $route['reports/(specific_:any)/(:any)/(:any)/(:any)'] = "reports/$1/$2/$3/$4"; $route['reports/specific_customer'] = "reports/specific_customer_input"; $route['reports/specific_employee'] = "reports/specific_employee_input"; +$route['reports/specific_discount'] = "reports/specific_discount_input"; $route['scaffolding_trigger'] = ""; diff --git a/application/controllers/reports.php b/application/controllers/reports.php index ed759f047..560f16c76 100644 --- a/application/controllers/reports.php +++ b/application/controllers/reports.php @@ -703,6 +703,56 @@ class Reports extends Secure_area $this->load->view("reports/tabular_details",$data); } + + function specific_discount_input() + { + $data = $this->_get_common_report_data(); + $data['specific_input_name'] = $this->lang->line('reports_discount'); + + $discounts = array(); + for($i = 0; $i <= 100; $i += 10) + { + $discounts[$i] = $i . '%'; + } + $data['specific_input_data'] = $discounts; + $this->load->view("reports/specific_input",$data); + } + + function specific_discount($start_date, $end_date, $discount, $sale_type, $export_excel = 0) + { + $this->load->model('reports/Specific_discount'); + $model = $this->Specific_discount; + + $headers = $model->getDataColumns(); + $report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'discount' =>$discount, 'sale_type' => $sale_type)); + + $summary_data = array(); + $details_data = array(); + + foreach($report_data['summary'] as $key=>$row) + { + $summary_data[] = array(anchor('sales/receipt/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target' => '_blank')), $row['sale_date'], $row['items_purchased'], $row['customer_name'], to_currency($row['subtotal']), to_currency($row['total']), to_currency($row['tax']),/*to_currency($row['profit']),*/ $row['payment_type'], $row['comment']); + + foreach($report_data['details'][$key] as $drow) + { + $details_data[$key][] = array($drow['name'], $drow['category'], $drow['description'], $drow['quantity_purchased'], to_currency($drow['subtotal']), to_currency($drow['total']), to_currency($drow['tax']),/*to_currency($drow['profit']),*/ $drow['discount_percent'].'%'); + } + } + + $data = array( + "title" => $discount. '% '.$this->lang->line('reports_discount') . ' ' . $this->lang->line('reports_report'), + "subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)), + "headers" => $headers, + "summary_data" => $summary_data, + "details_data" => $details_data, + "header_width" => intval(100 / count($headers['summary'])), + "overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date,'discount' =>$discount, 'sale_type' => $sale_type)), + "export_excel" => $export_excel + ); + + $this->load->view("reports/tabular_details",$data); + + } function detailed_sales($start_date, $end_date, $sale_type, $export_excel=0) { @@ -737,7 +787,7 @@ class Reports extends Secure_area $this->load->view("reports/tabular_details",$data); } - + function detailed_receivings($start_date, $end_date, $sale_type, $export_excel=0) { $this->load->model('reports/Detailed_receivings'); diff --git a/application/language/english/reports_lang.php b/application/language/english/reports_lang.php index 12e1f3598..e41d34242 100644 --- a/application/language/english/reports_lang.php +++ b/application/language/english/reports_lang.php @@ -28,7 +28,9 @@ $lang['reports_total'] = 'Total'; $lang['reports_tax'] = 'Tax'; $lang['reports_profit'] = 'Profit'; $lang['reports_report_input'] = 'Report Input'; +$lang['reports_discount'] = 'Discount'; $lang['reports_type'] = 'Type'; +$lang['reports_discount'] = 'A discount greater than '; $lang['reports_date_range'] = 'Date Range'; $lang['reports_today'] = 'Today'; $lang['reports_yesterday'] = 'Yesterday'; diff --git a/application/language/english/sales_lang.php b/application/language/english/sales_lang.php index 836446372..dc39c937f 100644 --- a/application/language/english/sales_lang.php +++ b/application/language/english/sales_lang.php @@ -14,7 +14,11 @@ $lang['sales_tax_percent']='Tax %'; $lang['sales_price']='Price'; $lang['sales_quantity']='Qty.'; $lang['sales_discount']='Disc %'; +$lang['sales_discount_short']='%'; +$lang["sales_discount_included"]='% korting inbegrepen'; +$lang['sales_sale_time']='Tijdstip'; $lang['sales_edit']='Edit'; +$lang['sales_sale_time']='Time'; $lang['sales_payment']='Payment Type'; $lang['sales_edit_item']='Edit Item'; $lang['sales_find_or_scan_item']='Find/Scan Item'; diff --git a/application/models/reports/specific_discount.php b/application/models/reports/specific_discount.php new file mode 100755 index 000000000..679275756 --- /dev/null +++ b/application/models/reports/specific_discount.php @@ -0,0 +1,70 @@ + array($this->lang->line('reports_sale_id'), $this->lang->line('reports_date'), $this->lang->line('reports_items_purchased'), $this->lang->line('reports_customer'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), /*$this->lang->line('reports_profit'),*/ $this->lang->line('reports_payment_type'), $this->lang->line('reports_comments')), + 'details' => array($this->lang->line('reports_name'), $this->lang->line('reports_category'), $this->lang->line('reports_description'), $this->lang->line('reports_quantity_purchased'), $this->lang->line('reports_subtotal'), $this->lang->line('reports_total'), $this->lang->line('reports_tax'), /*$this->lang->line('reports_profit'),*/$this->lang->line('reports_discount')) + ); + } + + public function getData(array $inputs) + { + $this->db->select('sale_id, DATE_FORMAT(sale_time, "%d-%m-%Y") AS sale_date, sum(quantity_purchased) as items_purchased, CONCAT(first_name," ",last_name) as customer_name, sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(profit) as profit, payment_type, comment', false); + $this->db->from('sales_items_temp'); + $this->db->join('people as customer', 'sales_items_temp.customer_id = customer.person_id', 'left'); + $this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'" and discount_percent >='.$inputs['discount']); + + if ($inputs['sale_type'] == 'sales') + { + $this->db->where('quantity_purchased > 0'); + } + elseif ($inputs['sale_type'] == 'returns') + { + $this->db->where('quantity_purchased < 0'); + } + + $this->db->group_by('sale_id'); + $this->db->order_by('sale_date'); + + $data = array(); + $data['summary'] = $this->db->get()->result_array(); + $data['details'] = array(); + + foreach($data['summary'] as $key=>$value) + { + $this->db->select('name, serialnumber, sales_items_temp.description, quantity_purchased, subtotal,total, tax, profit, discount_percent'); + $this->db->from('sales_items_temp'); + $this->db->join('items', 'sales_items_temp.item_id = items.item_id'); + $this->db->where('sale_id = '.$value['sale_id'] . " AND discount_percent >= " . $inputs['discount']); + $data['details'][$key] = $this->db->get()->result_array(); + } + + return $data; + } + + public function getSummaryData(array $inputs) + { + $this->db->select('sum(subtotal) as subtotal, sum(total) as total, sum(tax) as tax, sum(profit) as profit'); + $this->db->from('sales_items_temp'); + $this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'" and discount_percent >= '.$inputs['discount']); + + 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/sale.php b/application/models/sale.php index 6867fc803..3a03775f0 100644 --- a/application/models/sale.php +++ b/application/models/sale.php @@ -173,8 +173,9 @@ class Sale extends CI_Model //We create a temp table that allows us to do easy report/sales queries public function create_sales_items_temp_table() { + $this->db->query("DROP TABLE IF EXISTS phppos_sales_items_temp"); $this->db->query("CREATE TEMPORARY TABLE ".$this->db->dbprefix('sales_items_temp')." - (SELECT date(sale_time) as sale_date, ".$this->db->dbprefix('sales_items').".sale_id, comment,payment_type, customer_id, employee_id, + (SELECT date(sale_time) as sale_date, sale_time, ".$this->db->dbprefix('sales_items').".sale_id, comment,payments.payment_type, customer_id, employee_id, ".$this->db->dbprefix('items').".item_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, SUM(percent) as item_tax_percent, discount_percent, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal, ".$this->db->dbprefix('sales_items').".line as line, serialnumber, ".$this->db->dbprefix('sales_items').".description as description, @@ -184,6 +185,10 @@ class Sale extends CI_Model FROM ".$this->db->dbprefix('sales_items')." INNER JOIN ".$this->db->dbprefix('sales')." ON ".$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales').'.sale_id'." INNER JOIN ".$this->db->dbprefix('items')." ON ".$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('items').'.item_id'." + INNER JOIN (SELECT sale_id, SUM(payment_amount) AS sale_payment_amount, + GROUP_CONCAT(payment_type SEPARATOR ', ') AS payment_type FROM " .$this->db->dbprefix('sales_payments') . " + WHERE payment_type <> '" . $this->lang->line('sales_check') . "' GROUP BY sale_id) AS payments + ON " . $this->db->dbprefix('sales_items') . '.sale_id'. "=" . "payments.sale_id LEFT OUTER JOIN ".$this->db->dbprefix('suppliers')." ON ".$this->db->dbprefix('items').'.supplier_id='.$this->db->dbprefix('suppliers').'.person_id'." LEFT OUTER JOIN ".$this->db->dbprefix('sales_items_taxes')." ON " .$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales_items_taxes').'.sale_id'." and " diff --git a/application/views/reports/date_input.php b/application/views/reports/date_input.php index 7e98c9bed..3f2095ad2 100644 --- a/application/views/reports/date_input.php +++ b/application/views/reports/date_input.php @@ -14,6 +14,7 @@ if(isset($error))
+ @@ -21,11 +22,22 @@ if(isset($error)) -
- - lang->line('reports_sale_type'), 'reports_sale_type_label', array('class'=>'required')); ?> -
- $this->lang->line('reports_all'), 'sales' => $this->lang->line('reports_sales'), 'returns' => $this->lang->line('reports_returns')), 'all', 'id="sale_type"'); ?> + + +
+ + lang->line('reports_discount_prefix') .' ' .form_input(array( + 'name'=>'selected_discount', + 'id'=>'selected_discount', + 'value'=>'0')). ' '. $this->lang->line('reports_discount_suffix') + ?> + +
+
lang->line('reports_sales'); ?>
  • lang->line('reports_receivings'); ?>
  • lang->line('reports_customer'); ?>
  • +
  • lang->line('reports_discount'); ?>
  • lang->line('reports_employee'); ?>
  • @@ -56,8 +57,3 @@ if(isset($error)) ?> load->view("partial/footer"); ?> - diff --git a/css/reports.css b/css/reports.css index 088071ab3..88b175a32 100644 --- a/css/reports.css +++ b/css/reports.css @@ -2,9 +2,9 @@ { margin-left: 35px; } -#report_date_range_simple +#report_date_range_simple, #report_date_range_complex { - margin-bottom: 3px; + margin-bottom: 10px; } .report { diff --git a/css/tables.css b/css/tables.css index d734ab881..0f71cb853 100644 --- a/css/tables.css +++ b/css/tables.css @@ -113,6 +113,7 @@ table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSo table.innertable { display: none; + width: 100%; } table.innertable thead tr th