1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Tax_lib
{
const TAX_TYPE_SALES = 1;
const TAX_TYPE_SALES_BY_INVOICE = 2;
const TAX_TYPE_VAT = 0;
private $CI;
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('sale_lib');
}
public function get_tax_types()
{
return array(
TAX_TYPE_SALES => $this->CI->lang->line('taxes_sales_tax'),
TAX_TYPE_SALES_BY_INVOICE => $this->CI->lang->line('taxes_sales_tax_by_invoice'),
TAX_TYPE_VAT => $this->CI->lang->line('taxes_vat_tax')
);
}
public function get_item_sales_tax($quantity, $price, $discount_percentage, $tax_percentage, $rounding_code)
{
$tax_decimals = $this->CI->config->config['tax_decimals'];
$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);
}
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);
$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)
{
$fig = (int) str_pad('1', $decimals, '0');
$rounded_tax_amount = (ceil($tax_amount * $fig) / $fig);
}
elseif($rounding_code == Rounding_code::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;
}
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 ($item_tax_amount != 0)
{
if(!array_key_exists($tax_group_index, $sales_taxes))
{
$insertkey = $tax_group_index;
$sales_tax = array($insertkey => array(
'sale_id' => $sale_id,
'tax_type' => $tax_type,
'tax_group' => $tax_group,
'sale_tax_basis' => $tax_basis,
'sale_tax_amount' => $item_tax_amount,
'print_sequence' => $tax_group_sequence,
'name' => $name,
'tax_rate' => $tax_rate,
'sales_tax_code' => $tax_code,
'rounding_code' => $rounding_code
));
$sales_taxes += $sales_tax;
}
else
{
$sales_taxes[$tax_group_index]['sale_tax_basis'] = bcadd($sales_taxes[$tax_group_index]['sale_tax_basis'], $tax_basis, 4);
$sales_taxes[$tax_group_index]['sale_tax_amount'] = bcadd($sales_taxes[$tax_group_index]['sale_tax_amount'], $item_tax_amount, 4);
}
}
}
public function apply_invoice_taxing(&$sales_taxes)
{
if(!empty($sales_taxes))
{
$sort = array();
foreach($sales_taxes as $k => $v)
{
$sort['print_sequence'][$k] = $v['print_sequence'];
}
array_multisort($sort['print_sequence'], SORT_ASC, $sales_taxes);
}
$currency_decimals = $this->CI->config->config['currency_decimals'];
foreach($sales_taxes as $row_number => $sales_tax)
{
$sales_tax['sale_tax_amount'] = get_sales_tax_for_amount($sales_tax['sale_tax_basis'], $sales_tax['sale_tax_rate'], $sales_tax['rounding_code'], $currency_decimals);
}
}
public function round_sales_taxes(&$sales_taxes)
{
if(!empty($sales_taxes))
{
$sort = array();
foreach($sales_taxes as $k=>$v)
{
$sort['print_sequence'][$k] = $v['print_sequence'];
}
array_multisort($sort['print_sequence'], SORT_ASC, $sales_taxes);
}
$currency_decimals = $this->CI->config->config['currency_decimals'];
foreach($sales_taxes as $row_number => $sales_tax)
{
$sale_tax_amount = $sales_tax['sale_tax_amount'];
$rounding_code = $sales_tax['rounding_code'];
$rounded_sale_tax_amount = $sale_tax_amount;
if ($rounding_code == Rounding_code::HALF_UP)
{
$rounded_sale_tax_amount = round ( $sale_tax_amount, $currency_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);
}
elseif($rounding_code == Rounding_code::HALF_EVEN)
{
$rounded_sale_tax_amount = round ( $sale_tax_amount, $currency_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);
}
elseif($rounding_code == Rounding_code::ROUND_UP)
{
$fig = (int) str_pad('1', $currency_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');
$rounded_sale_tax_amount = (floor($sale_tax_amount * $fig) / $fig);
}
elseif($rounding_code == Rounding_code::HALF_FIVE)
{
$rounded_sale_tax_amount = round($sale_tax_amount / 5) * 5;
}
$sales_taxes[$row_number]['sale_tax_amount'] = $rounded_sale_tax_amount;
}
}
public function apply_sales_tax(&$item, &$city, &$state, &$sales_tax_code, $register_mode, $sale_id, &$sales_taxes)
{
$tax_code = $this->get_applicable_tax_mode($register_mode, $city, $state, $sales_tax_code);
if($tax_code != '' && $item['price'] != 0)
{
$tax_rate = 0.0000;
$rounding_code = Rounding_code::HALF_UP;
$tax_code_obj = $this->CI->Tax->get_info($tax_code);
$tax_category_id = $item['tax_category_id'];
if($tax_category_id != 0)
{
$tax_rate_info = $this->CI->Tax->get_rate_info($tax_code, $tax_category_id);
if ($tax_rate_info)
{
$tax_rate = $tax_rate_info->tax_rate;
$rounding_code = $tax_rate_info->rounding_code;
}
else
{
$tax_rate = $tax_code_obj->tax_rate;
$rounding_code = $tax_code_obj->rounding_code;
}
}
if($tax_category_id != 0)
{
$tax_rate_info = $this->CI->Tax->get_rate_info($tax_code, $tax_category_id);
$tax_rate = $tax_rate_info->tax_rate;
$rounding_code = $tax_rate_info->rounding_code;
$tax_group_sequence = $tax_rate_info->tax_group_sequence;
$tax_category = $tax_rate_info->tax_category;
}
else
{
$tax_rate = $tax_code_obj->tax_rate;
$rounding_code = $tax_code_obj->rounding_code;
$tax_group_sequence = $tax_code_obj->tax_group_sequence;
$tax_category = $tax_code_obj->tax_category;
}
$tax_decimals = $this->CI->config->config['tax_decimals'];
$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_group = (float)$tax_rate . '% ' . $tax_category;
$tax_type = Tax_lib::TAX_TYPE_SALES;
if($tax_amount != 0)
{
$this->update_sales_taxes($sales_taxes, $tax_type, $tax_group, $tax_rate, $tax_basis, $tax_amount, $tax_group_sequence, $rounding_code, $sale_id, $tax_category, $tax_code);
}
$tax_details = array('item_tax_amount' => $tax_amount, 'tax_group' => $tax_group, 'tax_name' => $tax_category, 'tax_rate' => $tax_rate, 'rounding_code' => $rounding_code, 'tax_group_sequence' => $tax_group_sequence, 'tax_code' => $tax_code);
return $tax_details;
}
else
{
$tax_details = array('item_tax_amount' => 0.0000, 'tax_group' => '', 'tax_name' => '', 'tax_rate' => 0.0000, 'rounding_code' => '0', 'tax_group_sequence' => '0', 'tax_code' => '');
return $tax_details;
}
}
public function get_applicable_tax_mode($register_mode, $city, $state, $sales_tax_code)
{
if ($register_mode == "SALE")
{
$tax_code = $this->CI->config->config['default_origin_tax_code'];
}
else
{
if ($sales_tax_code == '')
{
$tax_code = $this->CI->Tax->get_sales_tax_code($city, $state);
}
else
{
$tax_code = $sales_tax_code;
}
}
return $tax_code;
}
function clean($string) {
$string = str_replace(' ', '-', $string);
return preg_replace('/[^A-Za-z0-9\-]/', '', $string);
}
}
?>