简体   繁体   中英

Invoicing and Billing System in PHP

I have made a script to create order and create invoice. Here are two mysql tables tbl_order and tbl_invoice.

tbl_invoice:

`id`, `customer_id`, `status`, `invoicedate`, `total`, `payment_method`, `duedate`, `paiddate`, `details`, `order_id`

tbl_order

`id`, `customer_id`, `status`, `orderdate`, `invoice_id`, `total`, `reseller`, `nextdue`

So basically, When the order is being placed after client click proceed to payment, It will generate an invoice in tbl_invoice and add the order details to tbl_order.

However, in tbl_invoice their is one field called 'order_id' and in tbl_order their is a field called 'invoice_id'.

When the client clicks Proceed to payment, as it is generating both an Invoice and order, I have used the following script to update both tables:

$invoice = mysql_query("INSERT INTO `tbl_invoice` (customer_id, status, invoicedate, payment_method, total, duedate) VALUES ('$client', '$status', '$date', '$method', '$total', '$duedate')") or die("Failed to connect invoice query " .mysql_error());


$order = mysql_query("INSERT INTO `tbl_order` (customer_id, status, total, orderdate) VALUES ('$client', '$status', '$total', '$date')") or die("Failed to connect order query " .mysql_error());

$invoice_query =mysql_query("SELECT * FROM `tbl_invoice` ORDER BY `id` DESC LIMIT 1" .mysql_error());
$invoice_result = mysql_fetch_assoc($invoice_query);
$invoice_id = $invoice_result['id'];

$order_query =mysql_query("SELECT * FROM `tbl_order` ORDER BY `id` DESC LIMIT 1" .mysql_error());
$order_result = mysql_fetch_assoc($order_query);
$order_id = $order_result['id'];

$invoice = mysql_query("UPDATE `tbl_invoice` SET `order_id` = '$order_id' ORDER BY `id` DESC LIMIT 1") or die("Failed to connect invoice query 1" .mysql_error());
$order = mysql_query("UPDATE `tbl_order` SET `invoice_id` = '$invoice_id' ORDER BY `id` DESC LIMIT 1") or die("Failed to connect order query 1 " .mysql_error());

This is how it is working:

  • First it is generating an invoice. (order_id field is kept empty)
  • Then it generates an order. (invoice id field is kept empty)
  • With $invoice_query i am getting last (by using ORDER BY id DESC LIMIT 1) row added to invoice table and then I am Updating 'order_id' with the order id so generated.

  • With $order_query i am getting last row added to order table and then I am Updating 'invoice_id' with the invoice id so generated.

Now, the issue arises if at a particular second 2 clients create order and invoice, This will cause wrong data to go inside wrong fields.

Can anyone suggest me a better way to update those tables?

Cyclic relations here - invoice refers to order while order refers to invoice. That's a really bad anti-pattern in relational DB design. Moreover, only achievable (in case like yours) when you surrender the relation enforcement through foreign keys. Thus, you may end up having your data inconsistent. How good is that in the area related to customer's (and company's) money?

Did you ever ask yourself why you may ever need that kind of cyclic relation, after all?

I can guess that instant answer would be "I don't know", followed by "I actually don't need it!". :-)

So suggestion is - rebuild your DB, make the relation one-way

like, invoice is tied to order - that seems more logical, because there exist period of time when order is made but not yet invoiced, but basically that doesn't matter, as you have 1:1 here

then enforce it by making a foreign key out of order_id in the invoice table.

Or - again, as soon as you have 1:1 relation here - combine invoice and order in one table & create the record at once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM