Minor refactoring tax story

This commit is contained in:
jekkos
2017-07-05 00:19:27 +02:00
parent 47486b980f
commit a92d60d99f
18 changed files with 144 additions and 208 deletions

View File

@@ -204,7 +204,7 @@ class Config extends Secure_Controller
$data['logo_exists'] = $this->config->item('company_logo') != '';
$data['line_sequence_options'] = $this->sale_lib->get_line_sequence_options();
$data['register_mode_options'] = $this->sale_lib->get_register_mode_options();
$data['rounding_options'] = Rounding_code::get_rounding_options();
$data['rounding_options'] = Rounding_mode::get_rounding_options();
$data['tax_codes'] = $this->get_tax_code_options();
$data = $this->xss_clean($data);

View File

@@ -8,7 +8,7 @@ class Taxes extends Secure_Controller
{
parent::__construct('taxes');
$this->load->model('enums/Rounding_code');
$this->load->model('enums/Rounding_mode');
}
@@ -82,7 +82,7 @@ class Taxes extends Secure_Controller
$tax_rate_info = $this->Tax->get_rate_info($tax_code, $default_tax_category_id);
$data['rounding_options'] = Rounding_code::get_rounding_options();
$data['rounding_options'] = Rounding_mode::get_rounding_options();
if ($tax_code == -1)
{
@@ -134,7 +134,7 @@ class Taxes extends Secure_Controller
public static function get_html_rounding_options()
{
return Rounding_code::get_html_rounding_options();
return Rounding_mode::get_html_rounding_options();
}
public function save($tax_code = -1)
@@ -151,7 +151,7 @@ class Taxes extends Secure_Controller
'rate_tax_code' => $this->input->post('tax_code'),
'rate_tax_category_id' => 0,
'tax_rate' => parse_decimals($this->input->post('tax_rate')),
'rounding_code' => parse_decimals($this->input->post('rounding_code'))
'rounding_code' => $this->input->post('rounding_code')
);
if($this->Tax->save($tax_code_data, $tax_rate_data, $tax_code))
@@ -217,11 +217,5 @@ class Taxes extends Secure_Controller
echo json_encode($suggestions);
}
public function get_rounding_code_name($rounding_code)
{
return Rounding_code::get_rounding_code_name($rounding_code);
}
}
?>

View File

@@ -412,7 +412,7 @@ function get_tax_data_row($tax_code_row, $controller)
'tax_rate' => $tax_code_row->tax_rate,
'rounding_code' =>$tax_code_row->rounding_code,
'tax_code_type_name' => $CI->Tax->get_tax_code_type_name($tax_code_row->tax_code_type),
'rounding_code_name' => $CI->get_rounding_code_name($tax_code_row->rounding_code),
'rounding_code_name' => Rounding_mode::get_rounding_code_name($tax_code_row->rounding_code),
'city' => $tax_code_row->city,
'state' => $tax_code_row->state,
'edit' => anchor($controller_name."/view/$tax_code_row->tax_code", '<span class="glyphicon glyphicon-edit"></span>',

View File

@@ -14,7 +14,7 @@ class Sale_lib
{
$this->CI =& get_instance();
$this->CI->load->library('tax_lib');
$this->CI->load->model('enums/Rounding_code');
$this->CI->load->model('enums/Rounding_mode');
}
public function get_line_sequence_options()
@@ -1036,7 +1036,7 @@ class Sale_lib
$tax_category = '';
$tax_rate = '';
$rounding_code = Rounding_code::HALF_UP;
$rounding_code = Rounding_mode::HALF_UP;
$tax_group_sequence = 0;
$tax_code = '';
@@ -1215,38 +1215,7 @@ class Sale_lib
$cash_rounding_code = $this->CI->config->item('cash_rounding_code');
$rounded_total = $total;
if($cash_rounding_code == Rounding_code::HALF_UP)
{
$rounded_total = round ( $total, $cash_decimals, PHP_ROUND_HALF_UP);
}
elseif($cash_rounding_code == Rounding_code::HALF_DOWN)
{
$rounded_total = round ( $total, $cash_decimals, PHP_ROUND_HALF_DOWN);
}
elseif($cash_rounding_code == Rounding_code::HALF_EVEN)
{
$rounded_total = round ( $total, $cash_decimals, PHP_ROUND_HALF_EVEN);
}
elseif($cash_rounding_code == Rounding_code::HALF_ODD)
{
$rounded_total = round ( $total, $cash_decimals, PHP_ROUND_HALF_UP);
}
elseif($cash_rounding_code == Rounding_code::ROUND_UP)
{
$fig = (int) str_pad('1', $cash_decimals, '0');
$rounded_total = (ceil($total * $fig) / $fig);
}
elseif($cash_rounding_code == Rounding_code::ROUND_DOWN)
{
$fig = (int) str_pad('1', $cash_decimals, '0');
$rounded_total = (floor($total * $fig) / $fig);
}
elseif($cash_rounding_code == Rounding_code::HALF_FIVE)
{
$rounded_total = round($total / 5) * 5;
}
return $rounded_total;
return Rounding_mode::round_number($total, $cash_decimals);
}
}

View File

@@ -52,38 +52,7 @@ class Tax_lib
$tax_amount = bcmul($tax_basis, $tax_fraction);
$rounded_tax_amount = $tax_amount;
if($rounding_code == Rounding_code::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);
}
elseif($rounding_code == Rounding_code::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);
}
elseif($rounding_code == Rounding_code::ROUND_UP) // ROUND_UP
{
$fig = (int) str_pad('1', $decimals, '0');
$rounded_tax_amount = (ceil($tax_amount * $fig) / $fig);
}
elseif($rounding_code == Rounding_code::ROUND_DOWN) // ROUND_DOWN
{
$fig = (int) str_pad('1', $decimals, '0');
$rounded_tax_amount = (floor($tax_amount * $fig) / $fig);
}
elseif($rounding_code == Rounding_code::HALF_FIVE)
{
$rounded_tax_amount = round($tax_amount / 5) * 5;
}
return $rounded_tax_amount;
return Rounding_mode::round_number($tax_amount, $decimals);
}
/*
@@ -168,33 +137,33 @@ 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_mode::HALF_UP)
{
$rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_UP);
}
elseif($rounding_code == Rounding_code::HALF_DOWN)
elseif($rounding_code == Rounding_mode::HALF_DOWN)
{
$rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_DOWN);
}
elseif($rounding_code == Rounding_code::HALF_EVEN)
elseif($rounding_code == Rounding_mode::HALF_EVEN)
{
$rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_EVEN);
}
elseif($rounding_code == Rounding_code::HALF_ODD)
elseif($rounding_code == Rounding_mode::HALF_ODD)
{
$rounded_sale_tax_amount = round($sale_tax_amount, $decimals, PHP_ROUND_HALF_UP);
}
elseif($rounding_code == Rounding_code::ROUND_UP)
elseif($rounding_code == Rounding_mode::ROUND_UP)
{
$fig = (int) str_pad('1', $decimals, '0');
$rounded_sale_tax_amount = (ceil($sale_tax_amount * $fig) / $fig);
}
elseif($rounding_code == Rounding_code::ROUND_DOWN)
elseif($rounding_code == Rounding_mode::ROUND_DOWN)
{
$fig = (int) str_pad('1', $decimals, '0');
$rounded_sale_tax_amount = (floor($sale_tax_amount * $fig) / $fig);
}
elseif($rounding_code == Rounding_code::HALF_FIVE)
elseif($rounding_code == Rounding_mode::HALF_FIVE)
{
$rounded_sale_tax_amount = round($sale_tax_amount / 5) * 5;
}
@@ -216,7 +185,7 @@ class Tax_lib
if($tax_code != '' && $item['price'] != 0)
{
$tax_rate = 0.0000;
$rounding_code = Rounding_code::HALF_UP;
$rounding_code = Rounding_mode::HALF_UP;
$tax_code_obj = $this->CI->Tax->get_info($tax_code);
$tax_category_id = $item['tax_category_id'];

View File

@@ -16,47 +16,32 @@ class Token
{
protected $CI;
private $token_list = array(new Token_customer(), new Token_invoice_count(), new Token_invoice_sequence(),
new Token_quote_sequence(), new Token_suspended_invoice_count(), new Token_quote_sequence(), new Token_year_invoice_count());
public function __construct()
{
$this->CI =& get_instance();
}
function matches($token_id)
{
return false;
}
public function replace($token_id)
{
if($token_id == 'CU')
foreach($token_list as $token)
{
return (new Token_customer())->get_value();
}
elseif($token_id == 'CO')
{
return (new Token_invoice_count())->get_value();
}
elseif($token_id == 'ISEQ')
{
return (new Token_invoice_sequence())->get_value();
}
elseif($token_id == 'ISEQ')
{
return (new Token_invoice_sequence())->get_value();
}
elseif($token_id == 'QSEQ')
{
return (new Token_quote_sequence())->get_value();
}
elseif($token_id == 'SCO')
{
return (new Token_suspended_invoice_count())->get_value();
}
elseif($token_id == 'YCO')
{
return (new Token_year_invoice_count())->get_value();
if ($token->token_id() == $token_id)
{
return $token->get_value();
}
}
return '';
}
public function get_value()
{
return '';
}
}
?>

View File

@@ -13,6 +13,11 @@ class Token_customer extends Token
$this->CI->load->library('sale_lib');
}
public function token_id()
{
return 'CU';
}
public function get_value()
{
// substitute customer info

View File

@@ -13,6 +13,11 @@ class Token_invoice_count extends Token
$this->CI->load->model('Sale');
}
public function token_id()
{
return 'CO';
}
public function get_value()
{
return $this->CI->Sale->get_invoice_count();

View File

@@ -6,6 +6,11 @@
class Token_invoice_sequence extends Token
{
public function token_id()
{
return 'ISEQ';
}
public function get_value()
{
return $this->CI->Appconfig->acquire_save_next_invoice_sequence();

View File

@@ -6,6 +6,11 @@
class Token_quote_sequence extends Token
{
public function token_id()
{
return 'QSEQ';
}
public function get_value()
{
return $this->CI->Appconfig->acquire_save_next_quote_sequence();

View File

@@ -13,6 +13,11 @@ class Token_suspended_invoice_count extends Token
$this->CI->load->model('Sale');
}
public function token_id()
{
return 'SCO';
}
public function get_value()
{
return $this->CI->Sale->get_suspended_invoice_count();

View File

@@ -13,6 +13,11 @@ class Token_year_invoice_count extends Token
$this->CI->load->model('Sale');
}
public function token_id()
{
return 'YCO';
)
public function get_value()
{
return $this->CI->Sale->get_invoice_number_for_year();

View File

@@ -675,7 +675,7 @@ class Sale extends CI_Model
{
$tax_type = Tax_lib::TAX_TYPE_SALES;
}
$rounding_code = Rounding_code::HALF_UP; // half adjust
$rounding_code = Rounding_mode::HALF_UP; // half adjust
$tax_group_sequence = 0;
$item_total = $this->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], TRUE);
$tax_basis = $item_total;

View File

@@ -1,84 +0,0 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Rounding_code class
*/
class Rounding_code
{
const HALF_UP = 0;
const HALF_DOWN = 1;
const HALF_EVEN = 2;
const HALF_ODD = 3;
const ROUND_UP = 4;
const ROUND_DOWN = 5;
const HALF_FIVE = 6;
public static function get_rounding_options()
{
$CI =& get_instance();
$CI->load->helper('language');
return array(
Rounding_code::HALF_UP => lang('enum_half_up'),
Rounding_code::HALF_DOWN => lang('enum_half_down'),
Rounding_code::HALF_EVEN => lang('enum_half_even'),
Rounding_code::HALF_ODD => lang('enum_half_odd'),
Rounding_code::ROUND_UP => lang('enum_round_up'),
Rounding_code::ROUND_DOWN => lang('enum_round_down'),
Rounding_code::HALF_FIVE => lang('enum_half_five')
);
}
public static function get_rounding_code_name($rounding_code)
{
$CI =& get_instance();
$CI->load->helper('language');
if($rounding_code == Rounding_code::HALF_UP)
{
return lang('enum_half_up');
}
elseif($rounding_code == Rounding_code::HALF_DOWN)
{
return lang('enum_half_down');
}
elseif($rounding_code == Rounding_code::HALF_EVEN)
{
return lang('enum_half_even');
}
elseif($rounding_code == Rounding_code::HALF_ODD)
{
return lang('enum_half_odd');
}
elseif($rounding_code == Rounding_code::ROUND_UP)
{
return lang('enum_round_up');
}
elseif($rounding_code == Rounding_code::ROUND_DOWN)
{
return lang('enum_round_down');
}
elseif($rounding_code == Rounding_code::HALF_FIVE)
{
return lang('enum_half_five');
}
else
{
return lang('common_unknown');
}
}
public static function get_html_rounding_options()
{
$CI =& get_instance();
$CI->load->helper('language');
$x = "<option value='0' selected='selected'>".lang('enum_half_up')."</option>" .
"<option value='1'>".lang('enum_half_down')."</option>" .
"<option value='2'>".lang('enum_half_even')."</option>" .
"<option value='3'>".lang('enum_half_odd')."</option>" .
"<option value='4'>".lang('enum_round_up')."</option>" .
"<option value='5'>".lang('enum_round_down')."</option>" .
"<option value='6'>".lang('enum_half_five')."</option>";
return $x;
}
}

View File

@@ -0,0 +1,73 @@
<?php
class Rounding_mode
{
const HALF_UP = PHP_ROUND_HALF_UP;
const HALF_DOWN = PHP_ROUND_HALF_DOWN;
const HALF_EVEN = PHP_ROUND_HALF_EVEN;
const HALF_ODD = PHP_ROUND_HALF_ODD;
const ROUND_UP = 5;
const ROUND_DOWN = 6;
const HALF_FIVE = 7;
public static function get_rounding_options()
{
$CI =& get_instance();
$CI->load->helper('language');
$class = new ReflectionClass(__CLASS__);
$result = array();
foreach($class->getConstants() as $key => $value)
{
$result[$value] = lang(strtolower('ENUM_'. $key));
}
return $result;
}
public static function get_rounding_code_name($code)
{
$CI =& get_instance();
$CI->load->helper('language');
if (empty($code))
{
return lang('common_unknown');
}
return Rounding_mode::get_rounding_options()[$code];
}
public static function get_html_rounding_options()
{
$CI =& get_instance();
$CI->load->helper('language');
foreach (Rounding_mode::get_rounding_options() as $option => $label)
{
$x .= "<option value='$option'>".$label."</option>";
}
return $x;
}
public static function round_number($amount, $decimals)
{
if($cash_rounding_code != Rounding_mode::ROUND_UP)
{
$fig = (int) str_pad('1', $decimals, '0');
$rounded_total = (ceil($amount * $fig) / $fig);
}
elseif($cash_rounding_code == Rounding_mode::ROUND_DOWN)
{
$fig = (int) str_pad('1', $decimals, '0');
$rounded_total = (floor($amount * $fig) / $fig);
}
elseif($cash_rounding_code == Rounding_mode::HALF_FIVE)
{
$rounded_total = round($amount / 5) * 5;
}
else
{
$rounded_total = round ( $amount, $decimals, $cash_rounding_code);
}
return $rounded_total;
}
}

View File

@@ -74,7 +74,7 @@
<link rel="stylesheet" type="text/css" href="dist/style.css"/>
<!-- end mincss template tags -->
<!-- start minjs template tags -->
<script type="text/javascript" src="dist/opensourcepos.min.js?rel=2a9cf1bf65"></script>
<script type="text/javascript" src="dist/opensourcepos.min.js?rel=e467cdbc24"></script>
<!-- end minjs template tags -->
<?php endif; ?>

View File

File diff suppressed because one or more lines are too long

View File

File diff suppressed because one or more lines are too long