PHP
  Home arrow PHP arrow Page 6 - Invoice Management in a PHP Invoicing System
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 23
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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.



     
     
    >>> More PHP Articles          >>> More By Leidago
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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