[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1021
  • Last Modified:

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--
0
socross
Asked:
socross
1 Solution
 
GawaiCommented:
can you post your code here ?
0
 
socrossAuthor Commented:
                       //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';
0
 
socrossAuthor Commented:
Managed to get it working,

It was my database format all along, needed to store the net value as FLOAT(8,2) and then used the number_format php function to reformat the values after calculations

                        
                        //CALCULATE INVOICE TOTALS HERE
                        
                        //EMPTY TOTAL VALUE
                        $net = '';
                        $vat = '';                        

                        if(!empty($items)) {                              
                              foreach($items as $row2){
                                                      
                                    $net = $net + $row2['item_net'];
                                    
                                    if($row2['item_vat'] == 1)
                                          $vat = $vat + ($row2['item_net'] * '0.175');
                                    
                              }
                              $net = number_format($net,2);
                              $vat = number_format($vat,2);                                    
                              $total = $net + $vat;      
                        }
                        else
                              $net = $vat = $total = '0.00';


Thanks anyway!!

-s-
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rmaranhaoCommented:
Why don't you use sum(total), sum(vat), sum(total+vat) when retrieving daa from the db?
0
 
GawaiCommented:
well u can request to delete this Q and refund points
0
 
Computer101Commented:
Closed, 500 points refunded.
Computer101
EE Admin
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now