How to handle price fluctuations in an invoice application?

女生的网名这么多〃 提交于 2019-12-21 17:24:16

问题


In an invoicing application, consider following:

I have a products table that also contains the price of the product. And then I have an invoice and invoice_lines table and in each invoice line, I refer to the product id along with quantity. In this case, I am not storing the price of the product with the invoice line.

Now few months later, if the price of the product changes, any report would show the volume of the sales based on current price instead of the price on which the product was actually sold.

One solution that comes to my mind is that we keep a separate table named prices that links to each product and whenever the price of the product is changed a record is inserted into this prices table. The latest record in this table always is considered as the current price for new invoices. Each invoice line indicates its price by indicating the id of the price so that it shows correctly later in reports.

What are the best practices for handling such situations?


回答1:


I would always store the actual name, quantity and price at the time of purchase against each order item, effectively de-normalising the data. An order is a snap-shot in time and shouldn't be related back to something that can change. Not only can prices fluctuate, but what happens if you delete or rename the original product and your invoice relates back to it?




回答2:


Add a price field to the invoice line.




回答3:


I would echo your suggestion: Breaking out Prices into a separate table with these columns:

PriceId, ProductId, Price, StartDate, EndDate.

This way you can also plan ahead for future price changes. Current data could be gotten using (Sql Server syntax):

SELECT *
FROM Products
   Inner Join Prices ON Products.ProductId = Prices.ProductId 
      And GetDate() Between StartDate and EndDate


来源:https://stackoverflow.com/questions/3469803/how-to-handle-price-fluctuations-in-an-invoice-application

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