Invoice Management System Using PHP & MySQL

I am extending my previous tutorials. I’ll add an invoice management system.

What’s Invoice Management Systems

Because the majority of transactions are now done online, invoice or billing management systems are increasingly popular. Every vendor and buyer now requires an invoicing system to manage online billing.

Create Billing Management Systems

Let’s create a PHP project to develop invoice and billing system using PHP and MySQL.I am extending previous tutorial Create Admin Login and Logout Page.

The above post will have normal login as well as admin, I am just using the above source code and functionality.

This article will walk you through the process of creating a full invoice system, including how to create and edit invoices, as well as how to print invoices and convert them to PDF format. We will also make the source code available for download.

Database changes

I’ll update login table, I have added new mobile column into the ‘login’ table:

`mobile` varchar(255) DEFAULT NULL,
I have created two tables for order and order_details:

-- -- Table structure for table `orders` -- CREATE TABLE `orders` ( `order_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, `order_date` timestamp NOT NULL DEFAULT current_timestamp(), `order_receiver_name` varchar(250) NOT NULL, `order_receiver_address` text NOT NULL, `order_total_before_tax` decimal(10,2) NOT NULL, `order_total_tax` decimal(10,2) NOT NULL, `order_tax_per` varchar(250) NOT NULL, `order_total_after_tax` double(10,2) NOT NULL, `order_amount_paid` decimal(10,2) NOT NULL, `order_total_amount_due` decimal(10,2) NOT NULL, `note` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `orders` -- INSERT INTO `orders` (`order_id`, `user_id`, `order_date`, `order_receiver_name`, `order_receiver_address`, `order_total_before_tax`, `order_total_tax`, `order_tax_per`, `order_total_after_tax`, `order_amount_paid`, `order_total_amount_due`, `note`) VALUES (684, 1, '2021-11-15 15:33:26', '1', 'test', '157.00', '0.00', '', 157.00, '0.00', '157.00', ''); -- -------------------------------------------------------- -- -- Table structure for table `order_details` -- CREATE TABLE `order_details` ( `order_item_id` int(11) NOT NULL, `order_id` int(11) NOT NULL, `item_code` varchar(250) NOT NULL, `item_name` varchar(250) NOT NULL, `order_item_quantity` decimal(10,2) NOT NULL, `order_item_price` decimal(10,2) NOT NULL, `order_item_final_amount` decimal(10,2) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `order_details` -- INSERT INTO `order_details` (`order_item_id`, `order_id`, `item_code`, `item_name`, `order_item_quantity`, `order_item_price`, `order_item_final_amount`) VALUES (4368, 684, '1', 'test', '1.00', '45.00', '45.00'), (4369, 684, '3', 'test2', '2.00', '56.00', '112.00');

The files & folders structure for invoice management:

We’ll create an invoice module in the admin folder.

Let’s create the above blank files:

admin/ajax_admin.php : This file contains admin ajax logic.
invoice/action.php : It contains all actions method that will fetach invloices,create,edit and delete method.
invoice/create.php : The HTML UI for add a invoice.
invoice/edit.php : The HTML UI for editing a invoice.
invoice/index.php : The HTML UI for listing all invoices.
invoice/print.php : The HTML UI to display invoice details and print.

Create Invoice Form

Open create.php file and added below HTML code into this file.I have included all files that need to use to create a new invoice.

 if (isset($_GET['logout'])) < session_destroy(); unset($_SESSION['user_info']); header("location: ../../login.php"); >include 'action.php'; $invoice = new Invoice(); if(!empty($_POST['companyName']) && $_POST['companyName']) < $invoice->saveInvoice($_POST); header("Location:index.php"); > ?>  

Create an Invoice

From,




To,

Item No Item Name Quantity Price Total

Notes:


Subtotal:($)
Tax Rate:(%)
Tax Amount:($)
Total:($)
Amount Paid:($)
Amount Due:($)

PHP method To Create a new invoice

Let’s add the below code into the action.php file:

include_once("../../db/connection.php"); class Invoice< private $invoiceOrderTable = 'orders'; private $invoiceOrderItemTable = 'order_details'; private $dbConnect = false; public function __construct()< $db = new dbObj(); $this->dbConnect = $db->getConnstring(); > public function saveInvoice($POST) < $sqlInsert = " INSERT INTO ".$this->invoiceOrderTable."(user_id, order_receiver_name, order_receiver_address, order_total_before_tax, order_total_tax, order_tax_per, order_total_after_tax, order_amount_paid, order_total_amount_due, note) VALUES ('".$POST['userId']."', '".$POST['companyName']."', '".$POST['address']."', '".$POST['subTotal']."', '".$POST['taxAmount']."', '".$POST['taxRate']."', '".$POST['totalAftertax']."', '".$POST['amountPaid']."', '".$POST['amountDue']."', '".$POST['notes']."')"; mysqli_query($this->dbConnect, $sqlInsert); $lastInsertId = mysqli_insert_id($this->dbConnect); for ($i = 0; $i < count($POST['productCode']); $i++) < $sqlInsertItem = " INSERT INTO ".$this->invoiceOrderItemTable."(order_id, item_code, item_name, order_item_quantity, order_item_price, order_item_final_amount) VALUES ('".$lastInsertId."', '".$POST['productCode'][$i]."', '".$POST['productName'][$i]."', '".$POST['quantity'][$i]."', '".$POST['price'][$i]."', '".$POST['total'][$i]."')"; mysqli_query($this->dbConnect, $sqlInsertItem); > > >

Listing all invoices

Let’s create a listing of all invoices into the index.php file, also create methods to get data from database. The getInvoiceList() to get list logged in user’s invoices list.

 if (isset($_GET['logout'])) < session_destroy(); unset($_SESSION['user_info']); header("location: ../../login.php"); >include 'action.php'; $invoice = new Invoice(); ?>        

PHP Invoice System

getInvoiceList(); foreach($invoiceList as $invoiceDetails) < $invoiceDate = date("d/M/Y, H:i:s", strtotime($invoiceDetails["order_date"])); echo ' '; > ?>
Invoice No. Create Date Customer Name Invoice Total Action
'.$invoiceDetails["order_id"].' '.$invoiceDate.' '.$invoiceDetails["order_receiver_name"].' '.$invoiceDetails["order_total_after_tax"].'

We have also added action methods to edit, print and delete invoices.

Action method to get All invoices

Let’s add the below methods into the action.php file:

private function getData($sqlQuery) < $result = mysqli_query($this->dbConnect, $sqlQuery); if(!$result)< die('Error in query: '. mysqli_error($this->dbConnect)); > $data= array(); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) < $data[]=$row; >return $data; > public function getInvoiceList()< $sqlQuery = " SELECT * FROM ".$this->invoiceOrderTable." WHERE user_id = '".$_SESSION['user_info']['id']."'"; return $this->getData($sqlQuery); >

Edit an Invoice

Let’s create a edit UI and php method to update existing invoice. open edit.php file and added below code:

 if (isset($_GET['logout'])) < session_destroy(); unset($_SESSION['user_info']); header("location: ../../login.php"); >include 'action.php'; $invoice = new Invoice(); if(!empty($_GET['update_id']) && $_GET['update_id']) < $invoiceValues = $invoice->getInvoice($_GET['update_id']); $invoiceItems = $invoice->getInvoiceItems($_GET['update_id']); > if(!empty($_POST['invoice_btn']) && $_POST['invoice_btn']) < $invoice->updateInvoice($_POST); > ?>  

Create an Invoice

From,




To,

" name="itemId[]"> ?>
Item No Item Name Quantity Price Total
" name="productCode[]" echo $count; ?>" autocomplete="off"> " name="productName[]" echo $count; ?>" autocomplete="off"> " name="quantity[]" echo $count; ?>" quantity" autocomplete="off"> " name="price[]" echo $count; ?>" price" autocomplete="off"> " name="total[]" echo $count; ?>" total" autocomplete="off">

Notes:


" name="invoiceId" data-loading-text="Updating Invoice. " type="submit" name="invoice_btn" value="Save Invoice" btn-success submit_btn invoice-save-btm">
$
" type="number" name="subTotal" placeholder="Subtotal">
$
" type="number" name="taxAmount" placeholder="Tax Amount">
$
" type="number" name="totalAftertax" placeholder="Total">
$
" type="number" name="amountPaid" placeholder="Amount Paid">
$
" type="number" name="amountDue" placeholder="Amount Due">

PHP method To Update an invoice

Let’s add the below code into the action.php file:

public function getInvoice($invoiceId)< $sqlQuery = " SELECT * FROM ".$this->invoiceOrderTable." WHERE user_id = '".$_SESSION['user_info']['id']."' AND order_id = '$invoiceId'"; $result = mysqli_query($this->dbConnect, $sqlQuery); $row = mysqli_fetch_array($result, MYSQLI_ASSOC); return $row; > public function getInvoiceItems($invoiceId)< $sqlQuery = " SELECT * FROM ".$this->invoiceOrderItemTable." WHERE order_id = '$invoiceId'"; return $this->getData($sqlQuery); > public function updateInvoice($POST) < if($POST['invoiceId']) < $sqlInsert = " UPDATE ".$this->invoiceOrderTable." SET order_receiver_name = '".$POST['companyName']."', order_receiver_address= '".$POST['address']."', order_total_before_tax = '".$POST['subTotal']."', order_total_tax = '".$POST['taxAmount']."', order_tax_per = '".$POST['taxRate']."', order_total_after_tax = '".$POST['totalAftertax']."', order_amount_paid = '".$POST['amountPaid']."', order_total_amount_due = '".$POST['amountDue']."', note = '".$POST['notes']."' WHERE user_id = '".$POST['userId']."' AND order_id = '".$POST['invoiceId']."'"; mysqli_query($this->dbConnect, $sqlInsert); > $this->deleteInvoiceItems($POST['invoiceId']); for ($i = 0; $i < count($POST['productCode']); $i++) < $sqlInsertItem = " INSERT INTO ".$this->invoiceOrderItemTable."(order_id, item_code, item_name, order_item_quantity, order_item_price, order_item_final_amount) VALUES ('".$POST['invoiceId']."', '".$POST['productCode'][$i]."', '".$POST['productName'][$i]."', '".$POST['quantity'][$i]."', '".$POST['price'][$i]."', '".$POST['total'][$i]."')"; mysqli_query($this->dbConnect, $sqlInsertItem); > >

Delete an Invoice

Let’s create a delete invoice functionality using jquery ajax, the user will click the delete icon from the listing, the confirmation box will display, once the user confirms the action, the invoice ll delete from the database.

PHP method To Delete an invoice

Let’s add the below code into the ajax_admin.php file:

deleteInvoice($_POST['id']); $jsonResponse = array( "status" => 1 ); echo json_encode($jsonResponse); >

Action method to delete Invoice

Let’s create two methods into action.php , that’ll delete order and its details from mysql database table:

public function deleteInvoiceItems($invoiceId)< $sqlQuery = " DELETE FROM ".$this->invoiceOrderItemTable." WHERE order_id = '".$invoiceId."'"; mysqli_query($this->dbConnect, $sqlQuery); > public function deleteInvoice($invoiceId)< $sqlQuery = " DELETE FROM ".$this->invoiceOrderTable." WHERE order_id = '".$invoiceId."'"; mysqli_query($this->dbConnect, $sqlQuery); $this->deleteInvoiceItems($invoiceId); return 1; >

Print and Download an Invoice

Let’s create a print and download invoice functionality, I am using PHP library Dompdf to create PDF from HTML.

In the print.php file, we will add functionality to create invoice PDFs so that users may print or download invoices. We’ll use the invoice methods getInvoice() and getInvoiceItems() to get invoice details from database tables.

Added below code into the print.php file:

getInvoice($_GET['invoice_id']); $invoiceItems = $invoice->getInvoiceItems($_GET['invoice_id']); > $invoiceDate = date("d/M/Y, H:i:s", strtotime($invoiceValues['order_date'])); $output = ''; $output .= ' 
Invoice
To,
RECEIVER (BILL TO)
Name : '.$invoiceValues['order_receiver_name'].'
Billing Address : '.$invoiceValues['order_receiver_address'].'
Invoice No. : '.$invoiceValues['order_id'].'
Invoice Date : '.$invoiceDate.'

'; $count = 0; foreach($invoiceItems as $invoiceItem) < $count++; $output .= ' '; > $output .= ' '; $output .= '
Sr No. Item Code Item Name Quantity Price Actual Amt.
'.$count.' '.$invoiceItem["item_code"].' '.$invoiceItem["item_name"].' '.$invoiceItem["order_item_quantity"].' '.$invoiceItem["order_item_price"].' '.$invoiceItem["order_item_final_amount"].'
Sub Total '.$invoiceValues['order_total_before_tax'].'
Tax Rate : '.$invoiceValues['order_tax_per'].'
Tax Amount: '.$invoiceValues['order_total_tax'].'
Total: '.$invoiceValues['order_total_after_tax'].'
Amount Paid: '.$invoiceValues['order_amount_paid'].'
Amount Due: '.$invoiceValues['order_total_amount_due'].'
'; // create pdf of invoice $invoiceFileName = 'Invoice-'.$invoiceValues['order_id'].'.pdf'; require_once $_SERVER["DOCUMENT_ROOT"].'/invoice-management/libs/dompdf/src/Autoloader.php'; Dompdf\Autoloader::register(); use Dompdf\Dompdf; $dompdf = new Dompdf(); $dompdf->loadHtml(html_entity_decode($output)); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); $dompdf->stream($invoiceFileName, array("Attachment" => false)); ?>

8 thoughts on “ Invoice Management System Using PHP & MySQL ”

Thank you for this nice work.
Question: What do you need to do if you want to display a logo at the top right or left side of the the printed invoice?

Parvez says:

create image container and paste logo url as a src attribute, You can use bootstrap image container.

Jeevan Pendhari says:

hi nice code, I would like to add button beside Add Item, copy previous invoice items, where on click previous invoice list will appear and on select, it should insert all items from previous selected invoice. user than can remove not required items or add additional items.

can you refer to sample code for such requirement.

Parvez says:

sorry, i do not hv right now