What is the best way to stock products with different purchase prices & expiration date?

╄→尐↘猪︶ㄣ 提交于 2021-02-08 04:37:24

问题


I'm developing a stock inventory system using PHP, Mysql. This system have purchase module to add/update/increase quantity and expire date of products & sale module to decrease/out the quantity of products.

Now what is creating confusion to me and what is my real question is that

  • Suppose we have PRODUCT A1 in our stock with quantity 20 and the price of PRODUCT A1 was $15 when we purchase it.
  • Now we want to stock this product more for example we want to buy 100 more items of PRODUCT A1 but now new price of PRODUCT A1 is $18.

What is best way/logic to make these items separate to know how much products are purchased with price $15 and new price $18 on sale page.

  1. Should I have to store 2 or more quantity column in database?

  2. Should I have to store same product more then once in my database?

Currently I'm generating average of $15 and $18 then adding this value as purchase price of product.


回答1:


Oh dear, your questions indicate that you're really at the beginning of database design. Should you tackle a problem of this complexity at such an early stage? Well, yes, why not...

The answers to your questions are: 1. NO!!! 2. Perhaps.

The first idea is just stupid. Think about it. What if you get three different prices for a product, then you would need three columns, and so on... not a good idea.

The second option seems like a good solution. You have one row of 20 items at $15 and another row of 100 items at $18. You could do most things with this. The average price? Well: (20*15 + 100*18)/(20 + 100). Easy. But it quickly gets more complex. What if you sold 110 items to a customer? You will need to change two rows, but how? 20 items from the first row and 90 from the second? Or all 100 from the second and 10 from the first. You cannot ignore this problem, you have to set strict rules on how to do this. And you will find that the complexity just will keep on increasing the more options you add to your system. It will work, but it will be unyielding.

There is however a third option you haven't mentioned: Give every item in stock its own row in your table. This seems like a waste of database space, but hang on, it does simplify things. The average price? Simple, just compute the average price of the column containing the price. Even if every item has another price, it will be equally complex. Rules to fetch items from stock can be straightforward too: just use a FIFO or FILO system.

So this last solution is clearly the way to go.




回答2:


Could you just do a 'one to many' relation to another table? You'd have one table of your products (product_id, product_name, etc). Then another table, say product_inventory (product_inventory_id, product_id(foreign key to product.product_id), purchase_price (price you purchased them for), quantity (quantity of that item at the purchase price)).



来源:https://stackoverflow.com/questions/28373292/what-is-the-best-way-to-stock-products-with-different-purchase-prices-expirati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!