An invoice can contain 1 or more orders, how to archive this?
Example of Invoice:
OrderID | Order Date | Amount
31 10/02/2011 £1.50
43 12/02/2011 £1.50
74 13/02/2011 £5.00
=======
Total £8.00
If the Total is minus (eg: -8.00), it mean client owes me money.
Without minus, I pay client some money.
Here what I came up with:
Orders Table
CREATE TABLE IF NOT EXISTS `orders` (
`OrderID` int(11) NOT NULL AUTO_INCREMENT,
`Total` decimal(6,2) NOT NULL,
`OrderDate` datetime NOT NULL,
`Status` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`InvoiceID` int(11) NOT NULL,
PRIMARY KEY (`OrderID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Invoice Table
CREATE TABLE IF NOT EXISTS `invoice` (
`InvoiceID` int(11) NOT NULL DEFAULT '0',
`InvoiceDate` datetime NOT NULL,
`Amount` decimal(6,2) NOT NULL,
`Status` int(11) NOT NULL,
PRIMARY KEY (`InvoiceID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
invoice.Status (0 Processing, 1 Invoice Sent, 2 Cancelled, 3 Completed)
or what better status can be?
Payment Table
CREATE TABLE IF NOT EXISTS `payment` (
`PaymentID` int(11) NOT NULL AUTO_INCREMENT,
`InvoiceID` int(11) NOT NULL,
`Amount` decimal(6,2) NOT NULL,
`DatePayment` datetime NOT NULL,
`PaymentType` int(11) NOT NULL,
PRIMARY KEY (`PaymentID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
payment.PaymentType = (1: Payment Received From Customer (Owes Money), 2: Payment Sent To Customer)
Database Result:
mysql> select * from orders;
+---------+-------+---------------------+--------+--------+-----------+
| OrderID | Total | OrderDate | Status | userID | InvoiceID |
+---------+-------+---------------------+--------+--------+-----------+
| 1 | 20.00 | 2011-06-18 15:51:51 | 1 | 123 | 1 |
| 2 | 10.00 | 2011-06-19 15:51:57 | 1 | 123 | 1 |
| 3 | 5.00 | 2011-06-20 15:52:00 | 1 | 123 | 1 |
+---------+-------+---------------------+--------+--------+-----------+
mysql> select * from invoice;
+-----------+---------------------+--------+--------+
| InvoiceID | InvoiceDate | Amount | Status |
+-----------+---------------------+--------+--------+
| 1 | 2011-06-30 15:55:21 | 35.00 | 1 |
+-----------+---------------------+--------+--------+
mysql> select * from payment;
+-----------+-----------+--------+---------------------+-------------+
| PaymentID | InvoiceID | Amount | DatePayment | PaymentType |
+-----------+-----------+--------+---------------------+-------------+
| 1 | 1 | 35.00 | 2011-06-29 15:56:16 | 1 |
+-----------+-----------+--------+---------------------+-------------+
Im I on the right path? What can be improved/changed or suggestion?
Thanks.
Looks good.
The only thing I would add are some details like transaction id / check number to the payment table. This way you keep all the payment details together.