Posted by atchoo666 on December 11, 2008 at 4:22pm
Jump to:
| Project: | Invoice |
| Version: | 6.x-1.0-rc2 |
| Component: | Code |
| Category: | bug report |
| Priority: | critical |
| Assigned: | P.Vogelaar |
| Status: | closed (fixed) |
Issue Summary
If you try to use VAT percentage less than 10% , the VAT percentage used to calculate is multiplied by 10..
example :
1) unitcost excl VAT : 100€ VAT : 6% -> unitcost incl = 160€ ... weird
2) unitcost excl VAT : 100€ VAT : 60% -> unitcost incl = 160€ ... very weird :)
I tried with quantity more than 1 and it s the same behaviour...
Did you already fix it , in dev version... else I could try to find time to fix it..
Other problem, I can't use VAT with decimal like 21.5%.
If I try to use VAT with decimal then the VAT used is rounded to the floor
Rach
Comments
#1
Hi,
Thanks for reporting! I didn't notice or were aware of this behaviour before. I think I can look into it by this weekend. But maybe if you could investe some time earlier that would be nice.
Could you provide me with as much information possible:
What is you PHP version?
Linux or Windows server?
MySQL version?
Pieter
#2
Hi,
You did a great work with this module.
A problem with VAT is critical..So, I found some some minutes to check it ..
I did a little test case with a the code about VAT and computation of values :
<?php
function _invoice_round($value, $precision=0) {
$rounded_value = round(round($value*pow(10, $precision+1), 0), -1)/pow(10, $precision+1);
return $rounded_value;
}
$vat=6.5;
$quantity=1;
$unitcost =100;
echo "UNITCOST excl VAT: ".money_format('%.3n', _invoice_round($unitcost, 3));
echo "<br/>";
echo "UNITCOST incl VAT: ".money_format('%.2n', _invoice_round($unitcost * ('1.'. $vat), 2));
echo "<br/>";
echo "COST excl VAT: ".money_format('%.2n', _invoice_round($quantity * $unitcost, 2));
echo "<br/>";
echo "COST incl VAT: ".money_format('%.2n', _invoice_round($quantity * $unitcost * ('1.'. $vat), 2));
echo "<br/>";
?>
So, you can see the same behaviour that I explained before..
But it s normal because you use concat with VAT : ('1.'. $vat)
example : $vat = 6 ('1.'. $vat) -> 1.6 so 60%
$vat = 21.5 ('1.'. $vat) -> 1.21.5 mmmmh weird format number so it don't consider decimal
I changed (1+($vat/100)) and now with it, the result are correct the results are corrects.
invoice_form.inc : line 722 & 724
ps :Why did you use your own rounding_function and not php round with precision 2 ... just to be curious
Rachi
#3
Hello,
Thank you for your help, atchoo666, I was actually having this problem as well.
After looking through the codes, there are actually more codes to change in order to get this to work correctly.
In file invoice.module, line 342 - 344, Change to this
/*----- Patch Begin http://drupal.org/node/345800#comment-1150680'formatted_incunitcost' => money_format('%.2n', _invoice_round($row->unitcost * ('1.'. $row->vat), 2)),
'formatted_extotal' => money_format('%.2n', _invoice_round($row->quantity * $row->unitcost, 2)),
'formatted_inctotal' => money_format('%.2n', _invoice_round($row->quantity * $row->unitcost * ('1.'. $row->vat), 2)),
*/
'formatted_incunitcost' => money_format('%.2n', _invoice_round($row->unitcost * (1 + ($row->vat /100)), 2)),
'formatted_extotal' => money_format('%.2n', _invoice_round($row->quantity * $row->unitcost, 2)),
'formatted_inctotal' => money_format('%.2n', _invoice_round($row->quantity * $row->unitcost * (1 + ($row->vat /100)), 2)),
//----- Patch End
In file invoice_form.inc, line 721 - 724, Change to this
/*----- Patch Begin http://drupal.org/node/345800#comment-1150680money_format('%.3n', _invoice_round($row->unitcost, 3)),
money_format('%.2n', _invoice_round($row->unitcost * ('1.'. $row->vat), 2)),
money_format('%.2n', _invoice_round($row->quantity * $row->unitcost, 2)),
money_format('%.2n', _invoice_round($row->quantity * $row->unitcost * ('1.'. $row->vat), 2)),
*/
money_format('%.2n', _invoice_round($row->unitcost, 2)),
money_format('%.2n', _invoice_round($row->unitcost * (1 + ($row->vat /100)), 2)),
money_format('%.2n', _invoice_round($row->quantity * $row->unitcost, 2)),
money_format('%.2n', _invoice_round($row->quantity * $row->unitcost * (1 + ($row->vat /100)), 2)),
//----- Patch End
And lastly, in file invoice_ajax.inc, line 353 - 356, change to this
/*----- Patch Begin http://drupal.org/node/345800#comment-1150680$a_data['exunitcost'] = money_format('%.3n', _invoice_round($unitcost, 3));
$a_data['incunitcost'] = money_format('%.2n', _invoice_round($unitcost * ('1.'. $fv['vat']), 2));
$a_data['exsubtotal'] = money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost, 2));
$a_data['incsubtotal'] = money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost * ('1.'. $fv['vat']), 2));
*/
$a_data['exunitcost'] = money_format('%.2n', _invoice_round($unitcost, 2));
$a_data['incunitcost'] = money_format('%.2n', _invoice_round($unitcost * (1 + ($fv['vat'] /100)), 2));
$a_data['exsubtotal'] = money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost, 2));
$a_data['incsubtotal'] = money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost * (1 + ($fv['vat'] /100)), 2));
//----- Patch End
And in the same file, invoice_ajax.inc, line 376 - 380, change to this
/*----- Patch Begin http://drupal.org/node/345800#comment-1150680money_format('%.3n', _invoice_round($unitcost, 3)),
money_format('%.2n', _invoice_round($unitcost * ('1.'. $fv['vat']), 2)),
money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost, 2)),
money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost * ('1.'. $fv['vat']), 3)),
*/
money_format('%.2n', _invoice_round($unitcost, 2)),
money_format('%.2n', _invoice_round($unitcost * (1 + ($fv['vat'] /100)), 2)),
money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost, 2)),
money_format('%.2n', _invoice_round($fv['quantity'] * $unitcost * (1 + ($fv['vat'] /100)), 2)),
//----- Patch End
I have changed all %.3n to %.2n and the 2nd argument of _invoice_round from 3 to 2 since I don't need to display 3 digit decimal.
Hope this helps.
Thank you,
Leon
#4
atchoo666 and leon85321, thanks for investigating!
@atchoo666 "Why did you use your own rounding_function and not php round with precision 2 ... just to be curious"
The explanation was in the code above that function :)
/**
* Helper function to get a rounded value
*
* Read the comments on http://www.php.net/round why the
* standard PHP round() function doesn't work right.
*
* For example with PHP round(38.675, 2) gives 38.67, but that must be 38.68
*
* @param mixed $value
* @param integer $precision
* @return float
*/
round(..., 2) is not very reliable. I copied own of the functions in the comments on that page, that gave me the right results.
I fixed the VAT bug. It all seems to work correct now. Please do some testing for yourself and please report back here if everything is indeed working fine.
The code was committed to the development version in CVS DRUPAL-6--1 branch tag.
Some comment I gave to my latest commit:
"Since I made a development snapshot on 2 august 2008 I kept comitting to CVS HEAD. Yesterday I noticed that the date of the development snapshot still remained 2 august 2008 however I made changes. Now I figured out since I created a development branch DRUPAL-6--1 that I have to checkout that branch and add and commit my code to that branch. The development snapshot will now be updated within 12 hours if my assumptions are right."
So if you know how to do it, please checkout/export the code out of CVS DRUPAL-6--1 already and test it. Anonymous users have the right to checkout a module in CVS right? (i'm not sure of this) Otherwise the code will be in the development snapshot soon.
Thanks for the help!
Pieter
#5
#6
#7
Automatically closed -- issue fixed for two weeks with no activity.