I'll-Be-Back
I'll-Be-Back

Reputation: 10828

Database Design feedback

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.

Upvotes: 0

Views: 739

Answers (4)

HLGEM
HLGEM

Reputation: 96572

Ok, you have some serious issues here. Orders have mulitple items, invoices have multiple orders and payments may apply to mulitple orders and invoices. Orders may appear on multiple invoices (if they don't pay right aways which is common).

So what you need are linking tables. You should start with an ORDERINVOICE table which has both the order id and the invoice ID. Then an ORDERPAYMENT table with paymentid and Order id.

You also need to consider that in an ordering situation, you must record the details of the order as it occurred at the time. That means that while you should have the user_id to link to the current user, you should record the user's name, billing address and shipping addres as it was at the time of the order. You will need this information later to deal with any questions on the order. Further you need to ensure that you store the details of the order in a separate table called ORDERDETAILS which store the indivdual line items, the price at the time of the order and the name of the item ordered. You will need this for accounting reasons. You do not under any cuircumstances want to rely on a join to a product table to figure out the price of an order in the past. This will cause your finanacial records to be inaccurate.

Upvotes: 3

ChrisLively
ChrisLively

Reputation: 88064

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.

Upvotes: 2

Bob Probst
Bob Probst

Reputation: 9641

Without knowing more about your requirements, so far so good.

Be sure to store your Invoice Status and Payment Type decodes in a lookup table so that they can be enforced in the database and don't have to rely on programmers coding it correctly.

Upvotes: 0

Manuel
Manuel

Reputation: 10303

It looks alright to me, this is what i would have done aswell.

(I would think a payment is linked with an order, but if you intended to link it to an invoice this is fine)

Regards, MN

Upvotes: 0

Related Questions