From 87c242b35f0bc6cc41a5ef3e8d7bbca4ac54dc6f Mon Sep 17 00:00:00 2001 From: jekkos-t520 Date: Mon, 15 Sep 2014 20:14:37 +0200 Subject: [PATCH 1/2] Discount field in receiving made editable again --- application/views/receivings/receiving.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/views/receivings/receiving.php b/application/views/receivings/receiving.php index bb19ce513..578fe5268 100644 --- a/application/views/receivings/receiving.php +++ b/application/views/receivings/receiving.php @@ -117,7 +117,7 @@ else ?> - 'discount','value'=>$item['discount'],'size'=>'3'));?> From ff7b3050589719598d242a64b55a4ace85246f82 Mon Sep 17 00:00:00 2001 From: jekkos-t520 Date: Mon, 15 Sep 2014 20:16:05 +0200 Subject: [PATCH 2/2] Added tax inclusive pricing to sales and receivings --- application/controllers/config.php | 1 + application/language/en/config_lang.php | 1 + application/libraries/Receiving_lib.php | 11 ++- application/libraries/Sale_lib.php | 98 ++++++++++++++++++++----- application/views/config.php | 11 +++ 5 files changed, 104 insertions(+), 18 deletions(-) diff --git a/application/controllers/config.php b/application/controllers/config.php index 476c223ed..1a6a42e04 100644 --- a/application/controllers/config.php +++ b/application/controllers/config.php @@ -38,6 +38,7 @@ class Config extends Secure_area 'language'=>$this->input->post('language'), 'timezone'=>$this->input->post('timezone'), 'print_after_sale'=>$this->input->post('print_after_sale'), + 'tax_included'=>$this->input->post('tax_included'), 'custom1_name'=>$this->input->post('custom1_name'),/**GARRISON ADDED 4/20/2013**/ 'custom2_name'=>$this->input->post('custom2_name'),/**GARRISON ADDED 4/20/2013**/ 'custom3_name'=>$this->input->post('custom3_name'),/**GARRISON ADDED 4/20/2013**/ diff --git a/application/language/en/config_lang.php b/application/language/en/config_lang.php index eb9bb7c95..bc82d0288 100644 --- a/application/language/en/config_lang.php +++ b/application/language/en/config_lang.php @@ -35,4 +35,5 @@ $lang['config_custom10'] = 'Custom Field 10'; //GARRISON ADDED 4/21/2013 $lang['config_stock_location'] = 'Stock location'; $lang['config_stock_location_required'] = 'Stock location number is a required field'; +$lang['config_tax_included'] = 'Tax Included'; ?> \ No newline at end of file diff --git a/application/libraries/Receiving_lib.php b/application/libraries/Receiving_lib.php index a00d14fda..5b58023b1 100644 --- a/application/libraries/Receiving_lib.php +++ b/application/libraries/Receiving_lib.php @@ -1,4 +1,5 @@ empty_cart(); $this->delete_supplier(); } + + function get_item_total($quantity, $price, $discount_percentage) + { + $total = bcmul($quantity, $price, PRECISION); + $discount_fraction = bcdiv($discount_percentage, 100, PRECISION); + $discount_amount = bcmul($total, $discount_fraction, PRECISION); + return bcsub($total, $discount_amount, PRECISION); + } function get_total() { $total = 0; foreach($this->get_cart() as $item) { - $total+=($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); + $total =+ $this->get_item_total($item['quantity'], $item['price'], $item['discount']); } return $total; diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index c965d4f3d..5736df515 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -1,4 +1,6 @@ empty_payments(); $this->remove_customer(); } - - function get_taxes() + + function is_customer_taxable() { $customer_id = $this->get_customer(); $customer = $this->CI->Customer->get_info($customer_id); - + //Do not charge sales tax if we have a customer that is not taxable - if (!$customer->taxable and $customer_id!=-1) + return $customer->taxable or $customer_id==-1; + } + + function get_taxes() + { + //Do not charge sales tax if we have a customer that is not taxable + if (!$this->is_customer_taxable()) { return array(); } @@ -472,14 +480,14 @@ class Sale_lib foreach($tax_info as $tax) { $name = $tax['percent'].'% ' . $tax['name']; - $tax_amount=($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100)*(($tax['percent'])/100); - + $tax_percentage = $tax_info[0]['percent']; + $tax_amount = $this->get_item_tax($item['quantity'], $item['price'], $item['discount'], $tax_percentage); if (!isset($taxes[$name])) { $taxes[$name] = 0; } - $taxes[$name] += $tax_amount; + $taxes[$name] = bcadd($taxes[$name], $tax_amount, PRECISION); } } @@ -487,26 +495,82 @@ class Sale_lib } function get_subtotal() + { + $subtotal = $this->calculate_subtotal(); + return to_currency_no_money($subtotal); + } + + function get_item_total_tax_exclusive($quantity, $price, $discount_percentage) + { + $tax_info = $this->CI->Item_taxes->get_info($item['item_id']); + $item_price = $this->get_item_total($quantity, $price, $discount_percentage); + // only additive tax here + foreach($tax_info as $tax) + { + $tax_percentage = $tax_info[0]['percent']; + $item_price =- $this->get_item_tax($quantity, $price, $discount_percentage, $tax_percentage); + } + + return $item_price; + } + + function get_item_total($quantity, $price, $discount_percentage) + { + $total = bcmul($quantity, $price, PRECISION); + $discount_fraction = bcdiv($discount_percentage, 100, PRECISION); + $discount_amount = bcmul($total, $discount_fraction, PRECISION); + return bcsub($total, $discount_amount, PRECISION); + } + + function get_item_tax($quantity, $price, $discount_percentage, $tax_percentage) + { + $price = $this->get_item_total($quantity, $price, $discount_percentage); + + $tax_fraction = bcdiv($tax_percentage, 100, PRECISION); + if ($this->CI->config->config['tax_included']) + { + $tax_fraction = bcadd(1, $tax_fraction, PRECISION); + $tax_fraction = bcdiv(1, $tax_fraction, PRECISION); + $price_tax_excl = bcmul($price, $tax_fraction, PRECISION); + return bcsub($price, $price_tax_excl, PRECISION); + } + return bcmul($price, $tax_fraction, PRECISION); + } + + function calculate_subtotal() { $subtotal = 0; foreach($this->get_cart() as $item) { - $subtotal+=($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); + if ($this->is_customer_taxable()) + { + $subtotal =+ $this->get_item_total($item['quantity'], $item['price'], $item['discount']); + } + else + { + if ($CI->config->config['tax_included']) + { + $subtotal =+ $this->get_item_total($item['quantity'], $item['price'], $item['discount']); + } + else + { + $subtotal =+ $this->get_item_total_tax_exclusive($item['quantity'], $item['price'], $item['discount']); + } + } } - return to_currency_no_money($subtotal); + return $subtotal; } function get_total() { - $total = 0; - foreach($this->get_cart() as $item) + $total = $this->calculate_subtotal(); + + if (!$this->CI->config->config['tax_included']) { - $total += ( $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100); - } - - foreach($this->get_taxes() as $tax) - { - $total+=$tax; + foreach($this->get_taxes() as $tax) + { + $total = bcadd($total, $tax, 4); + } } return to_currency_no_money($total); diff --git a/application/views/config.php b/application/views/config.php index 83637776f..0ca907b18 100644 --- a/application/views/config.php +++ b/application/views/config.php @@ -279,6 +279,17 @@ echo form_open('config/save/',array('id'=>'config_form')); +
+lang->line('config_tax_included').':', 'tax_included',array('class'=>'wide')); ?> +
+ 'tax_included', + 'id'=>'tax_included', + 'value'=>'tax_included', + 'checked'=>$this->config->item('tax_included')));?> +
+
+
lang->line('config_custom1').':', 'website',array('class'=>'wide')); ?>