SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Invoice script - Add VAT

    Hi, I am using a PHP script that uses register_global.

    It has a function where you input a number and it works out the VAT. The problem is it deducts the VAT from the number (price). Does any one know maybe how to reverse it so it adds the VAT rather than take it away.

    This is the line in the script for the VAT:

    PHP Code:
    <?php print "$CurrencyUnit"?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?>
    If any more code is needed let me know.

    Cheers,

    Paul

  2. #2
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    This is basic math that everyone has learned at school.

    (full amount / (percentage + 100)) * 100 = what the percentage is of the price.

    With other words, remove "-($price[$l]/($vat_at+100)*100)" from the code.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That didn't seem to work, it stayed the same and also in the VAT field it displayed the same numbers of the Cost did.

    I removed the "-($price[$l]/($vat_at+100)*100)" part like you said.

    PHP Code:
    <?php print "$CurrencyUnit"?><?php print number_format($price[$l],2); ?>
    <?php 
    print "$CurrencyUnit"?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?>
    <?php 
    print "$CurrencyUnit"?><?php print number_format($price[$l]/($vat_at+100)*1002); ?>

  4. #4
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Take a look on those three lines then take a look on the explanation of the algorithm I wrote and you should be able to figure it out.

    I can guide your hand, but I wont do the work for you Im afraid.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, no help what so ever lol. Not everyone in this world is gifted with a good common knowledge, unfortunately for me I fall under that category.

    I can't see what has to be changed.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    PHP (if not many languages) can lose a little accuracy when performing math with floats, to compensate, work in pence (UK bias).

    PHP Code:
    <?php
    define
    ('VAT_RATE'.175); #old vat 17.5%

    $price 4999#49.99

    echo number_format(($price + ($price VAT_RATE)) / 1002'.'','); #58.74
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Ah, sorry jpopuk. I assumed the price contained the total price including the VAT amount. Had to reread your question a few times after reading Anthony's reply before I understood what you were asking for.

    The code below should most probably work.

    The first thing we do is add the VAT to the price, as by default in this script its assumed you created the product price including VAT. Then the first price will show the total with VAT, the second without VAT and the third the VAT by itself.

    PHP Code:
    <?php 
    $price
    [$l] *= (($vat_at 100) + 1);
    ?>

    <?php print "$CurrencyUnit"?><?php print number_format($price[$l],2); ?> 
    <?php print "$CurrencyUnit"?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?> 
    <?php print "$CurrencyUnit"?><?php print number_format($price[$l]/($vat_at+100)*1002); ?>
    If your interested in updating/modifying php scripts, I would recommend that you get a hold of a book on the subject as it makes modifications like this very simple. A good book to get into php is PHP and MySQL Web Development.

  8. #8
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is all the code on the page. I did not create this script.

    Where would I need to implement the define('VAT_RATE') etc... ?

    Many thanks

    PHP Code:
    <?php
    }
    function 
    mainform() {
    global 
    $HeaderDistance;
    global 
    $BorderColour;
    global 
    $TableBGcolour;
    global 
    $PageTitle;
    global 
    $CurrencyUnit;
    global 
    $DisplayVat;
    global 
    $TaxName;
    global 
    $VatRate;
    global 
    $InvoiceSignature;
    global 
    $n_order;
    global 
    $n_price;
    global 
    $vat_at;
    global 
    $customer_id;
    global 
    $customer_address;
    global 
    $customer_name;
    global 
    $date;
    global 
    $order;
    global 
    $price;
    global 
    $notes;

    ?>

    <TABLE WIDTH="50%" BORDER="0" CELLPADDING="2" CELLSPACING="2" ALIGN="CENTER">
        <TR> 
          <TD align="center" valign="top" colspan="2"><img src="images/gm-invoicelogo.jpg" width="397" height="187"><br> 
            <FORM method="post">
              <table>
                <tr>
                  <td colspan="4" align="center"><img src="images/header.jpg" width="559" height="18"><br><br></td>
                </tr>
                <tr>
                  <td align="left"><strong>Name:</strong></td>
                  <td align="left"><input type="text" name="customer_name" size="38" value="<?php if($customer_name) { print tag($customer_name); } else { print "Insert Customer Name"; } ?>"></td>
                  <td align="left"><b>Invoice Date</b><strong>:</strong></td>
                  <td align="left"><input type="text" name="date" size="15" value="<?php if($date){ print tag($date); } else { print date("d/m/Y"); } ?>"></td>
                </tr>
                <tr>
                  <td align="left"><strong>Address:</strong></td>
                  <td align="left"><textarea name="customer_address" rows="5" cols="40"><?php if($customer_address) { print $customer_address; } else { print "Insert Customer Address"; } ?></textarea></td> 
                  <td align="left"><b>Invoice Number:</b></td>
                  <td align="left"><input type="text" name="customer_id"  size="15" value="<?php if($customer_id){ print tag($customer_id); } else { print "Customer Ref"; } ?>"></td>
                </tr>
                <tr>
                  <td></td> 
                </tr>
                
                <tr>
                  <td colspan="4"> <table width="100%" CELLPADDING="2" CELLSPACING="0" border="1" bordercolor="<?PHP print "$BorderColour"?>">
                      <tr bgcolor="<?PHP print "$TableBGcolour"?>"> 
                        <th>Description</th>
                        <th>Cost</th>
                        <th><?PHP print "$TaxName"?></th>
                        <th>Total</th>
                      </tr>
                      <tr> 
                        <td><img src="images/spacer.gif" width="450" height="14"></td>
                        <td><img src="images/spacer.gif" width="60" height="14"></td>
                        <td><img src="images/spacer.gif" width="60" height="14"></td>
                        <td><img src="images/spacer.gif" width="60" height="14"></td>
                      </tr>
    <?php
    $tot_cost 
    0;
    $tot_vat 0;
    $tot_price 0;
    for(
    $l=0$l<sizeof($order); $l++){
    ?>
                      <input type="hidden" name="order[<?php print $l?>]" value="<?php print tag($order[$l]); ?>">
                      <input type="hidden" name="price[<?php print $l?>]" value="<?php print tag($price[$l]); ?>">
                      <tr> 
                        <td><?php print $order[$l]; ?></td>
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l],2); ?></td>
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?></td>
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]/($vat_at+100)*1002); ?></td>
                      </tr>&nbsp;
                      <?php
    $tot_cost 
    += $price[$l]/($vat_at+100)*100;
    $tot_vat += $price[$l]-($price[$l]/($vat_at+100)*100);
    $tot_price += $price[$l];
    }
    ?>
                      <tr> 
                        <td>
                          <input type="text" name="order[<?php print sizeof($order); ?>]" size="70"></td>
                        <td align="right"><?php print "$CurrencyUnit"?>
                          <input type="text" name="price[<?php print sizeof($price); ?>]" size="4"></td>
                        <td align="right"><img src="images/spacer.gif" width="60" height="14"></td>
                        <td align="right"><img src="images/spacer.gif" width="60" height="14"></td>
                      </tr> <tr> 
                        <td colspan=4><?php if($DisplayVat == "Y") {
                        print 
    "<input type=\"hidden\" name=\"vat_at\" value=\"$VatRate\" size=\"4\">
                        <i>
    $TaxName charged at a rate of $VatRate%</i>"; } else print "<img src=\"images/spacer.gif\" width=\"1\" height=\"14\">"?></td>
                      </tr>
                    </table>                <table align="right" width="0"  CELLPADDING="2" CELLSPACING="1" border="1" bordercolor="<?PHP print "$BorderColour"?>">
                      <br>
                      <tr> 
                        <td width="60" align="right"><b><?php print "$CurrencyUnit"?><?php print number_format($tot_price,2); ?></b></td>
                        <td align="right" width="60"><b><?php print "$CurrencyUnit"?><?php print number_format($tot_vat,2); ?></b></td>
                        <td align="right" width="60"><b><?php print "$CurrencyUnit"?><?php print number_format($tot_cost,2); ?></b></td>
                      </tr>
                  </table></td> 
                </tr>
                <tr>
                  <td colspan="4"><br><div align="center"><img src="images/footer.jpg" width="488" height="60"><br><br><br>
    Green Media Advertising Ltd, company reg: 06594457, vat reg: 93211665</div><br>
                    <div align="center"><?php print "$InvoiceSignature"?></div><br> 
                    <br><div align="center"><input name="action" type="submit" value="Update Invoice" class="button"> &nbsp; 
                      <input name="action" type="submit" value="Process Invoice" class="button">
                  </div></td> 
                </tr></table>
            </FORM> </TD>
        </TR>
      </TABLE>

  9. #9
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php $price[$l] *= (($vat_at 100) + 1); ?>
    I placed this at the top of the page and all the results displayed at 00.00 for all the fields.

    Paul

  10. #10
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The $price is part of an array, so you will need to paste that code right under

    for($l=0; $l<sizeof($order); $l++){

  11. #11
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, that has worked, wahoo!

    Some more problems now: http://www.lostmisfitdesign.com/invoice/

    I tested it with 100 and updated and it showed 17.50 and added it which is great. When I did another 100 it added on to the above total which should stay the same.

    Also the bottom VAT shows a - before it. And the TOTAL prices are displayed reversed.

    Sorry to be a pain!

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    can you post your code as it stands so we are all on the same page
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have sussed the TOTAL prices to show the correct way.

    The big problem is when creating a new 'Cost' it seems to change the numbers that are already there. And there is a - sign before the VAT in the bold text.

    This is all the code

    PHP Code:
    <?php
    }
    function 
    mainform() {
    global 
    $HeaderDistance;
    global 
    $BorderColour;
    global 
    $TableBGcolour;
    global 
    $PageTitle;
    global 
    $CurrencyUnit;
    global 
    $DisplayVat;
    global 
    $TaxName;
    global 
    $VatRate;
    global 
    $InvoiceSignature;
    global 
    $n_order;
    global 
    $n_price;
    global 
    $vat_at;
    global 
    $customer_id;
    global 
    $customer_address;
    global 
    $customer_name;
    global 
    $date;
    global 
    $order;
    global 
    $price;
    global 
    $notes;

    ?>
    <HTML>
    <HEAD>
    <TITLE>Green-Media - Create Invoice</TITLE>
    <LINK REL="STYLESHEET" HREF="includes/styles.css">
    </HEAD>
    <BODY>
    <DIV align="center"> 
      <TABLE WIDTH="50%" BORDER="0" CELLPADDING="2" CELLSPACING="2" ALIGN="CENTER">
        <TR> 
          <TD align="center" valign="top" colspan="2"><img src="images/gm-invoicelogo.jpg" width="397" height="187"><br> 
            <FORM method="post">
              <table>
                <tr>
                  <td colspan="4" align="center"><img src="images/header.jpg" width="559" height="18"><br><br></td>
                </tr>
                <tr>
                  <td align="left"><strong>Name:</strong></td>
                  <td align="left"><input type="text" name="customer_name" size="38" value="<?php if($customer_name) { print tag($customer_name); } else { print "Insert Customer Name"; } ?>"></td>
                  <td align="left"><b>Invoice Date</b><strong>:</strong></td>
                  <td align="left"><input type="text" name="date" size="15" value="<?php if($date){ print tag($date); } else { print date("d/m/Y"); } ?>"></td>
                </tr>
                <tr>
                  <td align="left"><strong>Address:</strong></td>
                  <td align="left"><textarea name="customer_address" rows="5" cols="40"><?php if($customer_address) { print $customer_address; } else { print "Insert Customer Address"; } ?></textarea></td> 
                  <td align="left"><b>Invoice Number:</b></td>
                  <td align="left"><input type="text" name="customer_id"  size="15" value="<?php if($customer_id){ print tag($customer_id); } else { print "Customer Ref"; } ?>"></td>
                </tr>
                <tr>
                  <td></td> 
                </tr>
                
                <tr>
                  <td colspan="4"> <table width="100%" CELLPADDING="2" CELLSPACING="0" border="1" bordercolor="<?PHP print "$BorderColour"?>">
                      <tr bgcolor="<?PHP print "$TableBGcolour"?>"> 
                        <th>Description</th>
                        <th>Cost</th>
                        <th><?PHP print "$TaxName"?></th>
                        <th>Total</th>
                      </tr>
                      <tr> 
                        <td><img src="images/spacer.gif" width="450" height="14"></td>
                        <td><img src="images/spacer.gif" width="60" height="14"></td>
                        <td><img src="images/spacer.gif" width="60" height="14"></td>
                        <td><img src="images/spacer.gif" width="60" height="14"></td>
                      </tr>
    <?php
    $tot_cost 
    0;
    $tot_vat 0;
    $tot_price 0;
    for(
    $l=0$l<sizeof($order); $l++){
    $price[$l] *= (($vat_at 100) + 1);
    ?>
                      <input type="hidden" name="order[<?php print $l?>]" value="<?php print tag($order[$l]); ?>">
                      <input type="hidden" name="price[<?php print $l?>]" value="<?php print tag($price[$l]); ?>">
                      <tr> 
                        <td><?php print $order[$l]; ?></td>
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]/($vat_at+100)*1002); ?></td>
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?> </td>
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l],2); ?></td>
                      </tr>&nbsp;
                      <?php
    $tot_cost 
    += $price[$l]/($vat_at+100)*100;
    $tot_vat += $price[$l]-($price[$l]/($vat_at+100)*135);
    $tot_price += $price[$l];
    }
    ?>
                      <tr> 
                        <td>
                          <input type="text" name="order[<?php print sizeof($order); ?>]" size="70"></td>
                        <td align="right"><?php print "$CurrencyUnit"?>
                          <input type="text" name="price[<?php print sizeof($price); ?>]" size="4"></td>
                        <td align="right"><img src="images/spacer.gif" width="60" height="14"></td>
                        <td align="right"><img src="images/spacer.gif" width="60" height="14"></td>
                      </tr> <tr> 
                        <td colspan=4><?php if($DisplayVat == "Y") {
                        print 
    "<input type=\"hidden\" name=\"vat_at\" value=\"$VatRate\" size=\"4\">
                        <i>
    $TaxName charged at a rate of $VatRate%</i>"; } else print "<img src=\"images/spacer.gif\" width=\"1\" height=\"14\">"?></td>
                      </tr>
                    </table>                <table align="right" width="0"  CELLPADDING="2" CELLSPACING="1" border="1" bordercolor="<?PHP print "$BorderColour"?>">
                      <br>
                      <tr> 
                        <td width="60" align="right"><b><?php print "$CurrencyUnit"?><?php print number_format($tot_cost,2); ?></b></td>
                        <td align="right" width="60"><b><?php print "$CurrencyUnit"?><?php print number_format($tot_vat,2); ?></b></td>
                        <td align="right" width="60"><b><?php print "$CurrencyUnit"?><?php print number_format($tot_price,2); ?></b></td>
                      </tr>
                  </table></td> 
                </tr>
                <tr>
                  <td colspan="4"><br><div align="center"><img src="images/footer.jpg" width="488" height="60"><br><br><br>
    Green Media Advertising Ltd, company reg: 06594457, vat reg: 93211665</div><br>
                    <div align="center"><?php print "$InvoiceSignature"?></div><br> 
                    <br><div align="center"><input name="action" type="submit" value="Update Invoice" class="button"> &nbsp; 
                      <input name="action" type="submit" value="Process Invoice" class="button">
                  </div></td> 
                </tr></table>
            </FORM> </TD>
        </TR>
      </TABLE>
    </DIV>
    </BODY>
    </HTML>

  14. #14
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    There is an error on this line:
    $tot_vat += $price[$l]-($price[$l]/($vat_at+100)*135);

    Replace 135 in the end with 100. Most probably that solve your issue.

  15. #15
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have done that, it has solved the - sign but when you add a new 'Cost' it stills changes the numbers that are there. (Does that make sense?)

    http://www.lostmisfitdesign.com/invoice/

    Cheers,

    Paul

  16. #16
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Yes it does.

    Change:
    PHP Code:
    for($l=0; $l<sizeof($order); $l++){ 
    $price[$l] *= (($vat_at / 100) + 1); 
    ?> 
                      <input type="hidden" name="order[<?php print $l?>]" value="<?php print tag($order[$l]); ?>"> 
                      <input type="hidden" name="price[<?php print $l?>]" value="<?php print tag($price[$l]); ?>"> 
                      <tr> 
                        <td><?php print $order[$l]; ?></td> 
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]/($vat_at+100)*1002); ?></td> 
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?> </td> 
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l],2); ?></td> 
                      </tr>&nbsp; 
                      <?php 
    $tot_cost 
    += $price[$l]/($vat_at+100)*100
    $tot_vat += $price[$l]-($price[$l]/($vat_at+100)*135); 
    $tot_price += $price[$l];
    to

    PHP Code:
    for($l=0; $l<sizeof($order); $l++){ 
    ?> 
                      <input type="hidden" name="order[<?php print $l?>]" value="<?php print tag($order[$l]); ?>"> 
                      <input type="hidden" name="price[<?php print $l?>]" value="<?php print tag($price[$l]); $price[$l] *= (($vat_at 100) + 1); ?>"> 
                      <tr> 
                        <td><?php print $order[$l]; ?></td> 
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]/($vat_at+100)*1002); ?></td> 
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?> </td> 
                        <td align="right"><?php print "$CurrencyUnit"?><?php print number_format($price[$l],2); ?></td> 
                      </tr>&nbsp; 
                      <?php 
    $tot_cost 
    += $price[$l]/($vat_at+100)*100
    $tot_vat += $price[$l]-($price[$l]/($vat_at+100)*135); 
    $tot_price += $price[$l];
    Note the change of the placement of the calculation adding the VAT to the price.

  17. #17
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wicked, you PHP guru!! Thank you very very much for all your help. I am very much appreciated for your help, and I have learnt a thing or 2.

    Quick question, with the end result page, is there a way I can create a button that turns the content into a PDF document?

    Cheers,

    Paul

  18. #18
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is possible but it is not an easy task. If you want to do that, I would really recommend that you try to learn some more php first.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •