From 181d9043600e60d5a6d107b82cd82d605ddd8313 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Thu, 6 Jul 2017 20:31:10 +0100 Subject: [PATCH] Visualise the taxes according to the selected number of decimals (#1133) --- application/controllers/Reports.php | 76 +++++++++++++++----- application/helpers/locale_helper.php | 12 ++++ application/helpers/table_helper.php | 2 +- application/hooks/load_config.php | 4 +- application/libraries/Sale_lib.php | 16 ++--- application/libraries/Tax_lib.php | 46 ++++++------ application/models/Sale.php | 2 +- application/views/configs/general_config.php | 4 +- application/views/sales/invoice.php | 2 +- application/views/sales/invoice_email.php | 2 +- application/views/sales/quote.php | 2 +- application/views/sales/quote_email.php | 2 +- application/views/sales/receipt_default.php | 2 +- application/views/sales/receipt_email.php | 2 +- application/views/sales/receipt_short.php | 2 +- application/views/sales/register.php | 2 +- 16 files changed, 116 insertions(+), 62 deletions(-) diff --git a/application/controllers/Reports.php b/application/controllers/Reports.php index fdd5a8473..426d510f2 100644 --- a/application/controllers/Reports.php +++ b/application/controllers/Reports.php @@ -55,7 +55,7 @@ class Reports extends Secure_Controller 'sale_date' => $row['sale_date'], 'quantity' => to_quantity_decimals($row['quantity_purchased']), 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']) @@ -91,7 +91,7 @@ class Reports extends Secure_Controller 'category' => $row['category'], 'quantity' => to_quantity_decimals($row['quantity_purchased']), 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']) @@ -127,7 +127,7 @@ class Reports extends Secure_Controller 'customer_name' => $row['customer'], 'quantity' => to_quantity_decimals($row['quantity_purchased']), 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']) @@ -163,7 +163,7 @@ class Reports extends Secure_Controller 'supplier_name' => $row['supplier'], 'quantity' => to_quantity_decimals($row['quantity_purchased']), 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']) @@ -199,7 +199,7 @@ class Reports extends Secure_Controller 'item_name' => $row['name'], 'quantity' => to_quantity_decimals($row['quantity_purchased']), 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']) @@ -235,7 +235,7 @@ class Reports extends Secure_Controller 'employee_name' => $row['employee'], 'quantity' => to_quantity_decimals($row['quantity_purchased']), 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']) @@ -271,7 +271,7 @@ class Reports extends Secure_Controller 'tax_percent' => $row['percent'], 'report_count' => $row['count'], 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']) )); } @@ -739,7 +739,7 @@ class Reports extends Secure_Controller 'quantity' => to_quantity_decimals($row['items_purchased']), 'employee_name' => $row['employee_name'], 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']), @@ -748,7 +748,18 @@ class Reports extends Secure_Controller foreach($report_data['details'][$key] as $drow) { - $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], to_quantity_decimals($drow['quantity_purchased']), to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), $drow['discount_percent'].'%')); + $details_data[$row['sale_id']][] = $this->xss_clean(array( + $drow['name'], + $drow['category'], + $drow['serialnumber'], + $drow['description'], + to_quantity_decimals($drow['quantity_purchased']), + to_currency($drow['subtotal']), + to_currency_tax($drow['tax']), + to_currency($drow['total']), + to_currency($drow['cost']), + to_currency($drow['profit']), + $drow['discount_percent'].'%')); } if(isset($report_data['rewards'][$key])) @@ -813,7 +824,7 @@ class Reports extends Secure_Controller 'quantity' => to_quantity_decimals($row['items_purchased']), 'customer_name' => $row['customer_name'], 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']), @@ -822,7 +833,18 @@ class Reports extends Secure_Controller foreach($report_data['details'][$key] as $drow) { - $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], to_quantity_decimals($drow['quantity_purchased']), to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), $drow['discount_percent'].'%')); + $details_data[$row['sale_id']][] = $this->xss_clean(array( + $drow['name'], + $drow['category'], + $drow['serialnumber'], + $drow['description'], + to_quantity_decimals($drow['quantity_purchased']), + to_currency($drow['subtotal']), + to_currency_tax($drow['tax']), + to_currency($drow['total']), + to_currency($drow['cost']), + to_currency($drow['profit']), + $drow['discount_percent'].'%')); } if(isset($report_data['rewards'][$key])) @@ -889,7 +911,7 @@ class Reports extends Secure_Controller 'quantity' => to_quantity_decimals($row['items_purchased']), 'customer_name' => $row['customer_name'], 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'profit' => to_currency($row['profit']), 'payment_type' => $row['payment_type'], @@ -898,7 +920,17 @@ class Reports extends Secure_Controller foreach($report_data['details'][$key] as $drow) { - $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], to_quantity_decimals($drow['quantity_purchased']), to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['profit']), $drow['discount_percent'].'%')); + $details_data[$row['sale_id']][] = $this->xss_clean(array( + $drow['name'], + $drow['category'], + $drow['serialnumber'], + $drow['description'], + to_quantity_decimals($drow['quantity_purchased']), + to_currency($drow['subtotal']), + to_currency_tax($drow['tax']), + to_currency($drow['total']), + to_currency($drow['profit']), + $drow['discount_percent'].'%')); } if(isset($report_data['rewards'][$key])) @@ -941,7 +973,7 @@ class Reports extends Secure_Controller 'employee_name' => $report_data['employee_name'], 'customer_name' => $report_data['customer_name'], 'subtotal' => to_currency($report_data['subtotal']), - 'tax' => to_currency($report_data['tax']), + 'tax' => to_currency_tax($report_data['tax']), 'total' => to_currency($report_data['total']), 'cost' => to_currency($report_data['cost']), 'profit' => to_currency($report_data['profit']), @@ -983,7 +1015,7 @@ class Reports extends Secure_Controller 'employee_name' => $row['employee_name'], 'customer_name' => $row['customer_name'], 'subtotal' => to_currency($row['subtotal']), - 'tax' => to_currency($row['tax']), + 'tax' => to_currency_tax($row['tax']), 'total' => to_currency($row['total']), 'cost' => to_currency($row['cost']), 'profit' => to_currency($row['profit']), @@ -1001,7 +1033,18 @@ class Reports extends Secure_Controller { $quantity_purchased .= ' [' . $this->Stock_location->get_location_name($drow['item_location']) . ']'; } - $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], $quantity_purchased, to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), $drow['discount_percent'].'%')); + $details_data[$row['sale_id']][] = $this->xss_clean(array( + $drow['name'], + $drow['category'], + $drow['serialnumber'], + $drow['description'], + $quantity_purchased, + to_currency($drow['subtotal']), + to_currency_tax($drow['tax']), + to_currency($drow['total']), + to_currency($drow['cost']), + to_currency($drow['profit']), + $drow['discount_percent'].'%')); } if(isset($report_data['rewards'][$key])) @@ -1195,6 +1238,7 @@ class Reports extends Secure_Controller $this->load->view('reports/tabular', $data); } + // Returns subtitle for the reports private function _get_subtitle_report($inputs) { diff --git a/application/helpers/locale_helper.php b/application/helpers/locale_helper.php index b763a4e4e..190fc128e 100644 --- a/application/helpers/locale_helper.php +++ b/application/helpers/locale_helper.php @@ -38,6 +38,13 @@ function totals_decimals() return $config->item('currency_decimals') ? $config->item('currency_decimals') : 0; } +function cash_decimals() +{ + $config = get_instance()->config; + + return $config->item('cash_decimals') ? $config->item('cash_decimals') : 0; +} + function tax_decimals() { $config = get_instance()->config; @@ -55,6 +62,11 @@ function to_currency_no_money($number) return to_decimals($number, 'currency_decimals'); } +function to_currency_tax($number) +{ + return to_decimals($number, 'tax_decimals', \NumberFormatter::CURRENCY); +} + function to_tax_decimals($number) { // taxes that are NULL, '' or 0 don't need to be displayed diff --git a/application/helpers/table_helper.php b/application/helpers/table_helper.php index 060cf8e3d..a348dc98b 100644 --- a/application/helpers/table_helper.php +++ b/application/helpers/table_helper.php @@ -103,7 +103,7 @@ function get_sales_manage_payments_summary($payments, $sales, $controller) $amount -= $sale['change_due']; } } - $table .= '
' . $payment['payment_type'] . ': ' . to_currency( $amount ) . '
'; + $table .= '
' . $payment['payment_type'] . ': ' . to_currency($amount) . '
'; } $table .= ''; diff --git a/application/hooks/load_config.php b/application/hooks/load_config.php index d8f8af720..269c75f40 100644 --- a/application/hooks/load_config.php +++ b/application/hooks/load_config.php @@ -33,7 +33,7 @@ function load_config() date_default_timezone_set('America/New_York'); } - bcscale(max(2, $CI->config->item('currency_decimals') + $CI->config->item('tax_decimals'))); + bcscale(max(2, totals_decimals() + tax_decimals())); } /** @@ -55,4 +55,4 @@ function load_language_files($path, $language) } } -?> \ No newline at end of file +?> diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index 0e13b3be7..a02fefe3e 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -411,7 +411,7 @@ class Sale_lib } // 0 decimal -> 1 / 2 = 0.5, 1 decimals -> 0.1 / 2 = 0.05, 2 decimals -> 0.01 / 2 = 0.005 - $threshold = bcpow(10, -$this->CI->config->item('currency_decimals')) / 2; + $threshold = bcpow(10, -totals_decimals()) / 2; if($this->get_mode() == 'return') { @@ -433,7 +433,7 @@ class Sale_lib $payment_total = $this->get_payments_total(); $sales_total = $this->get_total(); $amount_due = bcsub($sales_total, $payment_total); - $precision = $this->CI->config->item('currency_decimals'); + $precision = totals_decimals(); $rounded_due = bccomp(round($amount_due, $precision, PHP_ROUND_HALF_EVEN), 0, $precision); // take care of rounding error introduced by round tripping payment amount to the browser return $rounded_due == 0 ? 0 : $amount_due; @@ -976,7 +976,7 @@ class Sale_lib } $this->CI->session->set_userdata('cash_mode', $cash_mode); - if($this->CI->config->item('cash_decimals') < $this->CI->config->item('currency_decimals')) + if(cash_decimals() < totals_decimals()) { $cash_rounding = 1; } @@ -1002,7 +1002,7 @@ class Sale_lib public function get_taxes() { $register_mode = $this->CI->config->item('default_register_mode'); - $tax_decimals = $this->CI->config->item('tax_decimals'); + $tax_decimals = tax_decimals(); $customer_id = $this->get_customer(); $customer = $this->CI->Customer->get_info($customer_id); $sales_taxes = array(); @@ -1053,7 +1053,6 @@ class Sale_lib } $this->CI->tax_lib->round_sales_taxes($sales_taxes); - } return $sales_taxes; @@ -1144,6 +1143,7 @@ class Sale_lib public function get_extended_amount($quantity, $price, $discount_amount = 0) { $extended_amount = bcmul($quantity, $price); + return bcsub($extended_amount, $discount_amount); } @@ -1202,9 +1202,9 @@ class Sale_lib if($cash_rounding) { - $rounded_total = $this->check_for_cash_rounding($total); - return $rounded_total; + $total = $this->check_for_cash_rounding($total); } + return $total; } @@ -1215,7 +1215,7 @@ class Sale_lib public function check_for_cash_rounding($total) { - $cash_decimals = $this->CI->config->item('cash_decimals'); + $cash_decimals = cash_decimals(); $cash_rounding_code = $this->CI->config->item('cash_rounding_code'); $rounded_total = $total; diff --git a/application/libraries/Tax_lib.php b/application/libraries/Tax_lib.php index df417ab8a..b22d1822b 100644 --- a/application/libraries/Tax_lib.php +++ b/application/libraries/Tax_lib.php @@ -1,4 +1,4 @@ -CI->config->config['tax_decimals']; + $decimals = tax_decimals(); // The tax basis should be returned at the currency scale $tax_basis = $this->CI->sale_lib->get_item_total($quantity, $price, $discount_percentage, TRUE); - return $this->get_sales_tax_for_amount($tax_basis, $tax_percentage, $rounding_code, $tax_decimals); + return $this->get_sales_tax_for_amount($tax_basis, $tax_percentage, $rounding_code, $decimals); } /* @@ -51,7 +51,6 @@ class Tax_lib */ public function get_sales_tax_for_amount($tax_basis, $tax_percentage, $rounding_code, $decimals) { - $tax_fraction = bcdiv($tax_percentage, 100); $tax_amount = bcmul($tax_basis, $tax_fraction); @@ -59,19 +58,19 @@ class Tax_lib if($rounding_code == Rounding_code::HALF_UP) { - $rounded_tax_amount = round ( $tax_amount, $decimals, PHP_ROUND_HALF_UP); + $rounded_tax_amount = round($tax_amount, $decimals, PHP_ROUND_HALF_UP); } elseif($rounding_code == Rounding_code::HALF_DOWN) { - $rounded_tax_amount = round ( $tax_amount, $decimals, PHP_ROUND_HALF_DOWN); + $rounded_tax_amount = round($tax_amount, $decimals, PHP_ROUND_HALF_DOWN); } elseif($rounding_code == Rounding_code::HALF_EVEN) { - $rounded_tax_amount = round ( $tax_amount, $decimals, PHP_ROUND_HALF_EVEN); + $rounded_tax_amount = round($tax_amount, $decimals, PHP_ROUND_HALF_EVEN); } elseif($rounding_code == Rounding_code::HALF_ODD) { - $rounded_tax_amount = round ( $tax_amount, $decimals, PHP_ROUND_HALF_UP); + $rounded_tax_amount = round($tax_amount, $decimals, PHP_ROUND_HALF_UP); } elseif($rounding_code == Rounding_code::ROUND_UP) // ROUND_UP { @@ -96,7 +95,6 @@ class Tax_lib */ public function update_sales_taxes(&$sales_taxes, $tax_type, $tax_group, $tax_rate, $tax_basis, $item_tax_amount, $tax_group_sequence, $rounding_code, $sale_id, $name='', $tax_code='') { - $tax_group_index = $this->clean('X'.$tax_group); if(!array_key_exists($tax_group_index, $sales_taxes)) @@ -143,11 +141,11 @@ class Tax_lib array_multisort($sort['print_sequence'], SORT_ASC, $sales_taxes); } - $currency_decimals = $this->CI->config->config['currency_decimals']; + $decimals = totals_decimals(); foreach($sales_taxes as $row_number => $sales_tax) { - $sales_taxes[$row_number]['sale_tax_amount'] = $this->get_sales_tax_for_amount($sales_tax['sale_tax_basis'], $sales_tax['tax_rate'], $sales_tax['rounding_code'], $currency_decimals); + $sales_taxes[$row_number]['sale_tax_amount'] = $this->get_sales_tax_for_amount($sales_tax['sale_tax_basis'], $sales_tax['tax_rate'], $sales_tax['rounding_code'], $decimals); } } @@ -166,7 +164,7 @@ class Tax_lib array_multisort($sort['print_sequence'], SORT_ASC, $sales_taxes); } - $currency_decimals = $this->CI->config->config['currency_decimals']; + $decimals = totals_decimals(); foreach($sales_taxes as $row_number => $sales_tax) { @@ -174,30 +172,30 @@ class Tax_lib $rounding_code = $sales_tax['rounding_code']; $rounded_sale_tax_amount = $sale_tax_amount; - if ($rounding_code == Rounding_code::HALF_UP) + if($rounding_code == Rounding_code::HALF_UP) { - $rounded_sale_tax_amount = round ( $sale_tax_amount, $currency_decimals, PHP_ROUND_HALF_UP); + $rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_UP); } elseif($rounding_code == Rounding_code::HALF_DOWN) { - $rounded_sale_tax_amount = round ( $sale_tax_amount, $currency_decimals, PHP_ROUND_HALF_DOWN); + $rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_DOWN); } elseif($rounding_code == Rounding_code::HALF_EVEN) { - $rounded_sale_tax_amount = round ( $sale_tax_amount, $currency_decimals, PHP_ROUND_HALF_EVEN); + $rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_EVEN); } elseif($rounding_code == Rounding_code::HALF_ODD) { - $rounded_sale_tax_amount = round ( $sale_tax_amount, $currency_decimals, PHP_ROUND_HALF_UP); + $rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_UP); } elseif($rounding_code == Rounding_code::ROUND_UP) { - $fig = (int) str_pad('1', $currency_decimals, '0'); + $fig = (int) str_pad('1', $decimals, '0'); $rounded_sale_tax_amount = (ceil($sale_tax_amount * $fig) / $fig); } elseif($rounding_code == Rounding_code::ROUND_DOWN) { - $fig = (int) str_pad('1', $currency_decimals, '0'); + $fig = (int) str_pad('1', $decimals, '0'); $rounded_sale_tax_amount = (floor($sale_tax_amount * $fig) / $fig); } elseif($rounding_code == Rounding_code::HALF_FIVE) @@ -230,7 +228,7 @@ class Tax_lib if($tax_category_id != 0) { $tax_rate_info = $this->CI->Tax->get_rate_info($tax_code, $tax_category_id); - if ($tax_rate_info) + if($tax_rate_info) { $tax_rate = $tax_rate_info->tax_rate; $rounding_code = $tax_rate_info->rounding_code; @@ -258,11 +256,11 @@ class Tax_lib $tax_category = $tax_code_obj->tax_category; } - $tax_decimals = $this->CI->config->config['tax_decimals']; + $decimals = tax_decimals(); // The tax basis should be returned at the currency scale $tax_basis = $this->CI->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], TRUE); - $tax_amount = $this->get_sales_tax_for_amount($tax_basis, $tax_rate, $rounding_code, $tax_decimals); + $tax_amount = $this->get_sales_tax_for_amount($tax_basis, $tax_rate, $rounding_code, $decimals); $tax_group = (float)$tax_rate . '% ' . $tax_category; $tax_type = Tax_lib::TAX_TYPE_SALES; @@ -300,13 +298,13 @@ class Tax_lib public function get_applicable_tax_mode($register_mode, $city, $state, $sales_tax_code) { - if ($register_mode == "SALE") + if($register_mode == "SALE") { $tax_code = $this->CI->config->config['default_origin_tax_code']; // overrides customer assigned code } else { - if ($sales_tax_code == '') + if($sales_tax_code == '') { $tax_code = $this->CI->Tax->get_sales_tax_code($city, $state); } diff --git a/application/models/Sale.php b/application/models/Sale.php index 702a6a1df..2b4964ab5 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -541,7 +541,7 @@ class Sale extends CI_Model */ public function save(&$sale_status, &$items, $customer_id, $employee_id, $comment, $invoice_number, $quote_number, $payments, $dinner_table, &$sales_taxes, $sale_id = FALSE) { - $tax_decimals = $this->config->item('tax_decimals'); + $tax_decimals = tax_decimals(); if(count($items) == 0) { diff --git a/application/views/configs/general_config.php b/application/views/configs/general_config.php index 0c4ad8c25..3f33e7850 100644 --- a/application/views/configs/general_config.php +++ b/application/views/configs/general_config.php @@ -25,7 +25,7 @@ 'name' => 'default_tax_1_rate', 'id' => 'default_tax_1_rate', 'class' => 'form-control input-sm', - 'value'=>to_tax_decimals($this->config->item('default_tax_1_rate')))); ?> + 'value'=> to_tax_decimals($this->config->item('default_tax_1_rate')))); ?> % @@ -44,7 +44,7 @@ 'name' => 'default_tax_2_rate', 'id' => 'default_tax_2_rate', 'class' => 'form-control input-sm', - 'value'=>to_tax_decimals($this->config->item('default_tax_2_rate')))); ?> + 'value'=> to_tax_decimals($this->config->item('default_tax_2_rate')))); ?> % diff --git a/application/views/sales/invoice.php b/application/views/sales/invoice.php index fc3a47779..8ae31b476 100755 --- a/application/views/sales/invoice.php +++ b/application/views/sales/invoice.php @@ -140,7 +140,7 @@ $(document).ready(function() - + - + diff --git a/application/views/sales/quote.php b/application/views/sales/quote.php index 63beda8da..fab826e72 100644 --- a/application/views/sales/quote.php +++ b/application/views/sales/quote.php @@ -140,7 +140,7 @@ if (isset($error_message)) - + - + diff --git a/application/views/sales/receipt_default.php b/application/views/sales/receipt_default.php index 062be5cbd..40e0b9a7b 100644 --- a/application/views/sales/receipt_default.php +++ b/application/views/sales/receipt_default.php @@ -128,7 +128,7 @@ ?> : - + : - + : - + - +