clay1 Posted March 10, 2010 Share Posted March 10, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/ Share on other sites More sharing options...
btherl Posted March 11, 2010 Share Posted March 11, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1024508 Share on other sites More sharing options...
clay1 Posted March 11, 2010 Author Share Posted March 11, 2010 So would I just have multiple records with the same invoice_id for each product? such as: invoice1 product123 invoice1 product234 invoice1 product566 etc? Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1024530 Share on other sites More sharing options...
roopurt18 Posted March 11, 2010 Share Posted March 11, 2010 That's the correct way to do it, yes. Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1024533 Share on other sites More sharing options...
clay1 Posted March 11, 2010 Author Share Posted March 11, 2010 OK. So how do I generate the invoice_id so that it's sequential, not unique, but matches with the correct invoice? Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1024539 Share on other sites More sharing options...
btherl Posted March 15, 2010 Share Posted March 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1026192 Share on other sites More sharing options...
zenlord Posted April 26, 2010 Share Posted April 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1048457 Share on other sites More sharing options...
btherl Posted May 6, 2010 Share Posted May 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1054307 Share on other sites More sharing options...
roopurt18 Posted May 6, 2010 Share Posted May 6, 2010 @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. Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1054354 Share on other sites More sharing options...
zenlord Posted May 7, 2010 Share Posted May 7, 2010 THX 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. Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1054550 Share on other sites More sharing options...
roopurt18 Posted May 7, 2010 Share Posted May 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194830-invoices/#findComment-1054710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.