<?php
function money_round ($amount) {
# convert to string
$string = (string)$amount;
## find out if negative ##
if (substr($string, 0, 1) == '-') {
$negative = true;
$string = substr($string, 1, strlen($string) - 1);
} else {
$negative = false;
}
# if there's no decimal point add one on the end
if (substr_count($string, '.') == 0) {
$string .= '.';
}
# add a few trailing '0's in case there aren't enough there already
$string .= '000';
# find the decimal point
$dotpos = strpos($string, '.');
# separate pounds
$pounds = substr($string, 0, $dotpos);
# separate pence
$pence = substr($string, $dotpos + 1, 2);
# find out how many tenths of a penny
$tenths = substr($string, $dotpos + 3, 1);
if ($tenths >= 5) {
$pence++;
if ($pence == 100) {
$pence = '0';
$pounds++;
}
if ($pence < 10) {
$pence = '0' . $pence;
}
}
if ($negative) {
return '-' . $pounds . '.' . $pence;
} else {
return $pounds . '.' . $pence;
}
}
function CalcLineTaxExc ($quantity, $unit_price, $tax_percentage) {
#########################################################################################################
## !! IMPORTANT !! READ THIS BEFORE YOU USE THIS FUNCTION ##
#########################################################################################################
## ##
## $unit_price MUST *EXCLUDE* ANY TAX AND MUST *NOT* BE ROUNDED BEFORE BEING PASSED TO THIS FUNCTION ##
## ##
## THIS FUNCTION RETURNS THE EXACT AMOUNT OF TAX TO BE ADDED (FOR THE LINE) ROUNDED TO TWO D.P. ##
## ##
## TAX IS CALCULATED BY TAKING THE (PRICE INCL. TAX * QTY) AND DEDUCTING THE (PRICE EXCL. TAX * QTY), ##
## THIS METHOD ENSURES THAT CUSTOMERS PAY THE INTENDED AMOUNT INCL. TAX PER LINE AND IS SAFE WHETHER ##
## THE CUSTOMER HAS SEEN THE PRICE INCL. OR EXCL. TAX. ##
## ##
## TAX ON SOME LINES MAY END UP A PENNY OUT WHEN COMPARED TO ($unit_price * $qty * $tax_percentage) ##
## BUT THIS IS THE CORRECT WAY TO DO IT AND ON AVERAGE THE PENNYS WILL BALANCE EACH OTHER OUT ANYWAY ##
## ##
#########################################################################################################
$temp_inc_tax = $unit_price * (1 + $tax_percentage);
$line_tax_amount = money_round($quantity * $temp_inc_tax) - money_round($quantity * $unit_price);
$line_tax_amount = money_round($line_tax_amount); # just to be safe in case of rounding error on last calc #
return $line_tax_amount;
}
function CalcLineTaxInc ($quantity, $unit_price, $tax_percentage) {
#########################################################################################################
## !! IMPORTANT !! READ THIS BEFORE YOU USE THIS FUNCTION ##
#########################################################################################################
## ##
## $unit_price MUST *INCLUDE* ANY TAX AND MUST *NOT* BE ROUNDED BEFORE BEING PASSED TO THIS FUNCTION ##
## ##
## THIS FUNCTION RETURNS THE EXACT AMOUNT OF TAX TO BE ADDED (FOR THE LINE) ROUNDED TO TWO D.P. ##
## ##
## TAX IS CALCULATED BY TAKING THE (PRICE INCL. TAX * QTY) AND DEDUCTING THE (PRICE EXCL. TAX * QTY), ##
## THIS METHOD ENSURES THAT CUSTOMERS PAY THE INTENDED AMOUNT INCL. TAX PER LINE AND IS SAFE WHETHER ##
## THE CUSTOMER HAS SEEN THE PRICE INCL. OR EXCL. TAX. ##
## ##
## TAX ON SOME LINES MAY END UP A PENNY OUT WHEN COMPARED TO ($unit_price * $qty * $tax_percentage) ##
## BUT THIS IS THE CORRECT WAY TO DO IT AND ON AVERAGE THE PENNYS WILL BALANCE EACH OTHER OUT ANYWAY ##
## ##
#########################################################################################################
$temp_inc_tax = $unit_price;
$temp_exc_tax = $temp_inc_tax / (1 + $tax_percentage);
$line_tax_amount = money_round($quantity * $temp_inc_tax) - money_round($quantity * $temp_exc_tax);
$line_tax_amount = money_round($line_tax_amount); # just to be safe in case of rounding error on last calc #
return $line_tax_amount;
}
$tests = array(1,10,100,1000,5,15,105,1005,1.5,11.5,101.5,374.75,874.76,39665894.97);
$results = array();
foreach($tests as $test) {
$result = array();
$result['input_unit_price'] = $test;
$result['tax_amount'] = CalcLineTaxExc(1, $test, 0.2);
$result['input_price_incl_tax'] = $test + $result['tax_amount'];
$result['price_exl_tax'] = $result['input_price_incl_tax'] - CalcLineTaxInc(1,$test,0.2);
$results[] = $result;
}
var_dump($results);