Overview

Namespaces

  • emberlabs
    • Barcode
  • None

Classes

  • Appconfig
  • Barcode_lib
  • Config
  • Customer
  • Customer_rewards
  • Customers
  • Detailed_receivings
  • Detailed_sales
  • Dinner_table
  • Email_lib
  • emberlabs\Barcode\BarcodeBase
  • emberlabs\Barcode\Code128
  • emberlabs\Barcode\Code39
  • emberlabs\Barcode\Ean13
  • emberlabs\Barcode\Ean8
  • Employee
  • Employees
  • Giftcard
  • Giftcards
  • Home
  • Inventory
  • Inventory_low
  • Inventory_summary
  • Item
  • Item_kit
  • Item_kit_items
  • Item_kits
  • Item_lib
  • Item_quantity
  • Item_taxes
  • Items
  • Login
  • Mailchimp_lib
  • MailchimpConnector
  • Messages
  • Module
  • MY_Lang
  • No_Access
  • Person
  • Persons
  • Receiving
  • Receiving_lib
  • Receivings
  • Report
  • Reports
  • Rewards
  • Rounding_code
  • Sale
  • Sale_lib
  • Sale_suspended
  • Sales
  • Secure_Controller
  • Sms_lib
  • Specific_customer
  • Specific_discount
  • Specific_employee
  • Stock_location
  • Summary_categories
  • Summary_customers
  • Summary_discounts
  • Summary_employees
  • Summary_items
  • Summary_payments
  • Summary_report
  • Summary_sales
  • Summary_suppliers
  • Summary_taxes
  • Supplier
  • Suppliers
  • Tax
  • Tax_lib
  • Taxes
  • Token
  • Token_customer
  • Token_invoice_count
  • Token_invoice_sequence
  • Token_lib
  • Token_quote_sequence
  • Token_suspended_invoice_count
  • Token_year_invoice_count
  • Tracking_lib

Functions

  • currency_side
  • current_language
  • current_language_code
  • dateformat_bootstrap
  • dateformat_momentjs
  • db_log_queries
  • get_customer_data_row
  • get_customer_manage_table_headers
  • get_giftcard_data_row
  • get_giftcards_manage_table_headers
  • get_item_data_row
  • get_item_kit_data_row
  • get_item_kits_manage_table_headers
  • get_items_manage_table_headers
  • get_people_manage_table_headers
  • get_person_data_row
  • get_sale_data_last_row
  • get_sale_data_row
  • get_sales_manage_payments_summary
  • get_sales_manage_table_headers
  • get_supplier_data_row
  • get_suppliers_manage_table_headers
  • get_tax_data_row
  • get_taxes_manage_table_headers
  • load_config
  • load_language_files
  • load_stats
  • parse_decimals
  • pdf_create
  • quantity_decimals
  • show_report
  • show_report_if_allowed
  • to_currency
  • to_currency_no_money
  • to_decimals
  • to_quantity_decimals
  • to_tax_decimals
  • totals_decimals
  • transform_headers
  • transform_headers_readonly
  • Overview
  • Namespace
  • Class
  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')
        );
    }

    /*
     * Compute the tax basis and returns the tax amount
     */
    public function get_item_sales_tax($quantity, $price, $discount_percentage, $tax_percentage, $rounding_code)
    {
        $tax_decimals = $this->CI->config->config['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);
    }

    /*
     * Computes the item level sales tax amount for a given tax basis
     */
    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) // 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;
    }

    /*
    * Updates the sales_tax array which is later saved to the `sales_taxes` table and used for printing taxes on receipts and invoices
    */
    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
                ));

                //add to existing array
                $sales_taxes += $sales_tax;
            }
            else
            {
                // Important ... the sales amounts are accumulated for the group at the maximum configurable scale value of 4
                // but the scale will in reality be the scale specified by the tax_decimal configuration value  used for sales_items_taxes
                $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);
            }
        }
    }

    /*
    * If invoice taxing (as opposed to invoice_item_taxing) rules apply then recalculate the sales tax after tax group totals are final
    */
    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);
        }
    }

    /*
     * Apply rounding rules to the accumulated sales tax amounts
     */
    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;
        }
    }


    /**
     * Determine the applicable tax code and then determine the tax amount to be applied.
     * If a tax amount was identified then accumulate into the sales_taxes array
     */
    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 cannot be determined or the price is zero then skip this item
        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'];

            // 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_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);
            }

            // input : register_mode
            // input : city
            // input : state
            // input : sales_tax_code
            // input : $item['price']
            // input : $item['tax_category_id']
            // input : $item['quantity']
            // input : $item['price']
            // input : $item['discount']
            // both : $sales_taxes
            // output : tax_details['tax_rate']
            // output : tax_details['rounding_code']
            // output : tax_details['tax_group_sequence']
            // output : tax_details['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']; // overrides customer assigned code
        }
        else
        {
            if ($sales_tax_code == '')
            {
                $tax_code = $this->CI->Tax->get_sales_tax_code($city, $state);
            }
            else
            {
                // Use the customer assigned tax rate code
                $tax_code = $sales_tax_code;
            }
        }

        return $tax_code;
    }

    function clean($string) {
        $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

        return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
    }
}

?>
API documentation generated by ApiGen