socross
asked on
PHP CURRENCY CALCULATIONS
Hi
I have a database table which stores items for an invoice, there are 4 cols (where item_net is a float (4,2) and stores the amount before tax and item_vat is a true or false (1 or 0) which identifys whether vat is required on that item.
invoice_id | item_details | item_net | item_vat
When i pull out all items related to an invoice (invoice_id) i need to do some calculations to pull out three results.
1. total net ($net)
2. total vat ($vat)
3 Total ($net +$vat)
So i need to take each row of the $item array that i create when i pull all the values out of the database and then for each row calculate the vat if required and then add to the running total.
I have tried creating some empty vars for the net and the vat and then adding to them but i cant seem to get it working, could this be something to do with the float format in the db.
Any suggestions, example code would be much appriciated, i can post my current attempt, but i would rather see how a pro would approach it first.
Many thanks
--s--
I have a database table which stores items for an invoice, there are 4 cols (where item_net is a float (4,2) and stores the amount before tax and item_vat is a true or false (1 or 0) which identifys whether vat is required on that item.
invoice_id | item_details | item_net | item_vat
When i pull out all items related to an invoice (invoice_id) i need to do some calculations to pull out three results.
1. total net ($net)
2. total vat ($vat)
3 Total ($net +$vat)
So i need to take each row of the $item array that i create when i pull all the values out of the database and then for each row calculate the vat if required and then add to the running total.
I have tried creating some empty vars for the net and the vat and then adding to them but i cant seem to get it working, could this be something to do with the float format in the db.
Any suggestions, example code would be much appriciated, i can post my current attempt, but i would rather see how a pro would approach it first.
Many thanks
--s--
can you post your code here ?
ASKER
//EMPTY TOTAL VALUE
$net = '';
$vat = '';
//GET TOTALS
if(!empty($items)) {
foreach($items as $row){
$net = $net + $row['item_net'];
if($row['item_vat'] == 1)
$vat = $vat + ($row['item_net'] * '0.175');
}
$total = $net + $vat;
}
else
$net = $vat = $total = 'n/a';
$net = '';
$vat = '';
//GET TOTALS
if(!empty($items)) {
foreach($items as $row){
$net = $net + $row['item_net'];
if($row['item_vat'] == 1)
$vat = $vat + ($row['item_net'] * '0.175');
}
$total = $net + $vat;
}
else
$net = $vat = $total = 'n/a';
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you use sum(total), sum(vat), sum(total+vat) when retrieving daa from the db?
well u can request to delete this Q and refund points
Closed, 500 points refunded.
Computer101
EE Admin
Computer101
EE Admin