Author Topic: Invoices  (Read 1845 times)

0 Members and 1 Guest are viewing this topic.

Offline clay1Topic starter

  • Enthusiast
  • Posts: 161
    • View Profile
Invoices
« on: March 10, 2010, 05:30:21 PM »
I am creating a table to store some information about invoices I am sending to clients. The table is updated when the invoice is sent.

One of the things I would like to store is which products the invoice was for, this way I can pull up a duplicate of the invoice at a later date if needed, view the product, and also for accounting purposes.

Each product has a id number

How should I go about storing this in the invoices table?

Offline btherl

  • Guru
  • Fanatic
  • *
  • Posts: 3,791
  • Gender: Male
  • Matt is the best!
    • View Profile
Re: Invoices
« Reply #1 on: March 10, 2010, 07:36:11 PM »
If the invoices table has primary key invoice_id and the products table has primary key product_id, then you can create a table

CREATE TABLE invoice_products
(
  invoice_id integer,
  product_id integer,
  product_count integer,
  cost numeric(9,2),
);

For example.  This will let you record any number of products along with an invoice, along with a count of how many products there were and the price (which can be per-product or a total)
Your php questions answered at Flingbits

Offline clay1Topic starter

  • Enthusiast
  • Posts: 161
    • View Profile
Re: Invoices
« Reply #2 on: March 10, 2010, 08:11:34 PM »
So would I just have multiple records with the same invoice_id for each product?

such as:

invoice1 product123
invoice1 product234
invoice1 product566

etc?

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Invoices
« Reply #3 on: March 10, 2010, 08:16:51 PM »
That's the correct way to do it, yes.

Offline clay1Topic starter

  • Enthusiast
  • Posts: 161
    • View Profile
Re: Invoices
« Reply #4 on: March 10, 2010, 08:37:51 PM »
OK.

So how do I generate the invoice_id so that it's sequential, not unique, but matches with the correct invoice?

Offline btherl

  • Guru
  • Fanatic
  • *
  • Posts: 3,791
  • Gender: Male
  • Matt is the best!
    • View Profile
Re: Invoices
« Reply #5 on: March 15, 2010, 02:40:57 AM »
invoice_id is generated by the invoice table.  You would never have the invoice_products table generating it, because all that table does is refer to items in the other two tables.
Your php questions answered at Flingbits

Offline zenlord

  • Enthusiast
  • Posts: 54
    • View Profile
Re: Invoices
« Reply #6 on: April 26, 2010, 06:33:26 AM »
I have a question regarding the data type for 'cost'.

You (btherl) said it to be numeric(9,2), but there is a data type 'money' available , as I found out this weekend. Using it has proven to be a challenge (that has not been met at this moment), but I wonder if it has advantages over your approach?

Vincent

Offline btherl

  • Guru
  • Fanatic
  • *
  • Posts: 3,791
  • Gender: Male
  • Matt is the best!
    • View Profile
Re: Invoices
« Reply #7 on: May 06, 2010, 05:11:47 PM »
Sorry, I didn't see your reply until now.  I'm not familiar with the money type myself so I can't comment.  We use numeric(9,2) at my workplace where exact money types are required.  Sometimes we need more precision (eg we may need to account for partial cents), so we use a double precision there.  That can give unexpected results occasionally, but it's ok for the situations we use it in.
Your php questions answered at Flingbits

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Invoices
« Reply #8 on: May 06, 2010, 06:51:20 PM »
@btherl
For partial cents why not use numeric(9,3) or numeric(9,4) along with arbitrary precision math libraries?

Oh.  I just saw but it's ok for the situations we use it in, which I suppose answers my question.  But I'm still replying so the OP can see alternative solutions.

Offline zenlord

  • Enthusiast
  • Posts: 54
    • View Profile
Re: Invoices
« Reply #9 on: May 07, 2010, 08:26:33 AM »
thanks for your reply. Since I was not able to get the money-type working (and since I learned it is deprecated anyway), I have switched to numeric(9,2). All I need it for is to store exact amounts - no calculations other than adding and subtracting need to be made, so I don't expect much trouble.

Offline roopurt18

  • Guru
  • Fanatic
  • *
  • Posts: 3,658
  • Gender: Male
  • le sigh
    • View Profile
    • rbredlau
Re: Invoices
« Reply #10 on: May 07, 2010, 01:28:36 PM »
If you are adding and subtracting dollar amounts, then you should be using arbitrary precision functions like those in bcmath or gmp.

People on the business end of things can be very forgiving about a lot of things.  One thing they are not forgiving about is when the money amounts are wrong.