PHP
  Home arrow PHP arrow Page 6 - Invoice Management in a PHP Invoicing ...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Invoice Management in a PHP Invoicing System
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 22
    2006-09-13

    Table of Contents:
  • Invoice Management in a PHP Invoicing System
  • Finishing the Invoice Page
  • Generating a Table
  • Code for Creating an Invoice
  • Creating Unpaid.php
  • Creating a new invoice

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Invoice Management in a PHP Invoicing System - Creating a new invoice


    (Page 6 of 6 )

    To create a invoice is relatively simple. All we need to do is find out how much your invoice is, who you are invoicing and what the invoice is for. In terms of coding we will of course need a lot more information, such as who is making the invoice, and we also have to work out the total amount with and without the VAT.

    Before we go further, take a look at what the "newinvoice.php" page looks like:

     

    So, create a new document and save it as newinvoice.php. As always, go right to the top of the page and add the following code:

    Code6 newinvoice.php

    <?
    ob_start();
    include "config.php";
    include "FCKeditor/fckeditor.php";
    A
    if(isset($_POST['submit'])){
    $tot = $_POST['totxvat'] * 17.5;
    $rem = $tot / 100;
    $totwvat = $rem +$_POST['totxvat'];
    B
    $query_ins = "INSERT INTO invoices SET status='Unpaid',";
    $query_ins .= "inv_date = '".$td."',VAT = '17.5',cid = '".$_POST
    ['cname']."',uID = '".$_SESSION['u_id']."'";
    mysql_query($query_ins);
    $newID=mysql_insert_id();
    $query_inv="INSERT INTO clientinv SET descr='".trim(addslashes
    ($_POST['descr']))."',";
    $query_inv .= "totwVAT = '".$totwvat."',totxVAT = '".trim
    (addslashes($_POST['totxvat']))."',finv='".$newID."'";
    if(mysql_query($query_inv)){
    header("location:allinv.php");
    }else{
    echo mysql_error();
    }
    }
    ?>

    If you look at the second line of the code, you should see the following:

    include "FCKeditor/fckeditor.php";

    http://images.devshed.com/ds/stories/PHP_Invoice/FCKeditor.zip

    The FCKEditor is, as the name implies, a text editor for web applications. It basically provides HTML text formatting and many other features such as smilies, image management, and so on. It is freely available online; just google for it and you should get a lot of links to it.  Now I’ve marked sections of the code with the letters A and B; this is so that you know what section I’m talking about when I explain the code. It goes without saying that you should remove them when testing the code.

    Section A deals with calculating the total amount with VAT included. Currently in the UK, the VAT is at 17.5; it may be different where you live. So, to find out what 17.5 of the total is, we multiply the total by 17.5 and then divide the result by 100; that should give us the answer we need. Then we simply add the answer to the total entered in the form:

    $tot = $_POST['totxvat'] * 17.5;
    $rem = $tot / 100;
    $totwvat = $rem +$_POST['totxvat'];

    On the form is a dropdown box that contains all the names in the clients table. The dropdown box is dynamically filled with the names, like so:

    <select name="cname" id="cname">
           <?
                            $cl_query = "SELECT name,id FROM client
    ORDER BY id ASC";
                            $cl_result = mysql_query($cl_query);
                            while($company_list = mysql_fetch_assoc
    ($cl_result)) {
                                        echo "<option value=\"" .
    $company_list['id'] . "\"";
                                                   echo ">" . stripslashes(htmlspecialchars($company_list
    ['name'])) . "</option>";
                            }
                            ?>
                </select>

    When the form is submitted, the id of the selected client is sent to the script at the top of the page. So, after calculating the VAT, a query is executed to find the name of the client matching the submitted id, and the name of that client is stored in the "$thename" variable :

    $queryname="SELECT name FROM client WHERE id = '".$_POST
    ['cname']."'";
    $result=mysql_query($queryname);
    $row=mysql_fetch_assoc($result);
    $thename=$row['name'];

    Section B primarily inserts the newly created invoice into the appropriate database tables:

    $query_ins = "INSERT INTO invoices SET status='Unpaid',";
    $query_ins .= "inv_date = '".$td."',VAT = '17.5',cid = '".$_POST
    ['cname']."',uID = '".$_SESSION['u_id']."'";
    mysql_query($query_ins);
    $newID=mysql_insert_id();
    $query_inv="INSERT INTO clientinv SET descr='".trim(addslashes
    ($_POST['descr']))."',";
    $query_inv .= "totwVAT = '".$totwvat."',totxVAT = '".trim
    (addslashes($_POST['totxvat']))."',finv='".$newID."'";
    if(mysql_query($query_inv)){
    header("location:allinv.php");
    }else{
    echo mysql_error();
    }

    I think for the most part the fields used here are self explanatory, except maybe for the cid and uID fields. The cid field identifies the client and the uID field identifies the user who issued the invoice. The reason I use numbers instead of the actual names of the client and user is because it is generally faster to work with numbers than text when using databases.

    The form that takes input from the user looks like this:

    <form action="NewInvoice.php" method="post" name="newinv">
                <table width="100%" border="0" class="block">
        <tr>
        <td width="122" align="center"><span class="style1">Select  Client to Invoice
    </span></td>
        <td width="426" align="center"> Invoice Amount(excluding VAT) </td>
      </tr>
      <tr>
        <td><select name="cname" id="cname">
           <?
                            $cl_query = "SELECT name,id FROM client ORDER BY id ASC";
                            $cl_result = mysql_query($cl_query);
                            while($company_list = mysql_fetch_assoc($cl_result)) {
                                        echo "<option value=\"" . $company_list['id'] . "\"";
                                                                            echo ">" . stripslashes
    (htmlspecialchars($company_list['name'])) . "</option>";
                            }
                            ?>
                </select>
        <span class="smalltext"><a href="NewClient.php">Add New Client
    </a></span></td>
        <td align="center"><input name="totxvat" type="text" id="totxvat" size="40" />
         </td>
      </tr>

    Here we take the description of the invoice using the FCKeditor:

      <tr>
        <td colspan="2">Description:<br />
                <?php
    $sBasePath = 'FCKeditor/' ;
    $oFCKeditor = new FCKeditor('descr') ;
    $oFCKeditor->BasePath         = $sBasePath ;
    //$oFCKeditor->Value             = 'This is some <strong>sample text</strong>. You are using <a href="http://www.fckeditor.net/">FCKeditor</a>.' ;
    $oFCKeditor->Create() ;
    ?></td>
                </tr>
      <tr>
        <td colspan="2"><div align="center" class="style1">
          <input name="submit" type="submit" id="submit" value="Submit New Invoice">
        </div></td>
      </tr>
    </table>
    </form>  

    You do not have to use the fckeditor. You can simply use a normal HTML text box, if you like. That’s basically it for creating a new invoice. In the next article, we will be looking at client management.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · As one can read in the comments on the first of this series, there is no data...
       · Minor Snark - 17.5% does equal x times 17.5 divided by 100, but it would be better...
       · Thanks for the observation. Will keep it in mind next time around!
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT