HomePHP Invoice Management in a PHP Invoicing System
Invoice Management in a PHP Invoicing System
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.
Downloadable files for this article are available here and here.
Continuing with the PHP Invoicing series, we will now concentrate on how to create, view and print an invoice. So let’s get started!
Create a new PHP document and save it as main.php. Refer to fig2 to see what the main page looks like. There are essentially three pages that deal with invoices:
Allinv.php – Lists all invoices in the database.
Unpaid.php – Lists all unpaid invoices.
NewInvoice.php – Enables you to generate a new invoice.
There are also related pages that handle deleting and updating invoices.
Create a new document and save it as allinvoices.php. The page consists of both PHP and HTML code. Add the following code:
Code4 Allinv.php <? include "config.php"; $query_invoice = "SELECT *,DATE_FORMAT(inv_date,'%D %M %Y') as idate,DATE_ADD(inv_date,INTERVAL 30 DAY) as duedate FROM invoices ORDER BY invno DESC"; $result_invoice = mysql_query($query_invoice); $num_invoice = mysql_num_rows($result_invoice); ?>
This is a standard query that retrieves all the invoices in the database. The only things worth explaining are the DATE_FORMAT() and DATE_ADD() functions. The DATE_FORMAT function does exactly what the name implies: it formats the date that is stored in the way that you want it. The DATE_ADD() function adds a specified number of days (in this case it is 30) to the date column in the database.
I wanted the date formatted as dd mm yyyy which is displayed as, for example, "18th June 2005." There are a variety of ways in which you can format a date. Check http://www.mysql.com/ for more details. The 30 days that I’ve specified in the DATE_ADD() function will help us determine whether an invoice is overdue.
The number of rows returned by the query is stored in the "$num_invoices" variable. As you’ll see in a little while, it will be used to determine how many rows a table should have.