Home arrow PHP arrow Page 6 - Invoice Management in a PHP Invoicing System

Creating a new invoice - PHP

If you're running a business in which you're invoicing clients, you need some way to keep track of which invoices have gone out and which clients have paid. In this second article of a four-part series that covers the creation of a PHP invoicing system, we create the parts that deal with this kind of invoice management.

TABLE OF CONTENTS:
  1. Invoice Management in a PHP Invoicing System
  2. Finishing the Invoice Page
  3. Generating a Table
  4. Code for Creating an Invoice
  5. Creating Unpaid.php
  6. Creating a new invoice
By: Leidago
Rating: starstarstarstarstar / 31
September 13, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: