MySQL 数据库设计:库存和销售系统?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8059682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Database Design: inventory and sales system?
提问by StackOverflowNewbie
I need to develop a inventory and sales system.
我需要开发一个库存和销售系统。
For inventory, I need to be able to keep track of ideal stock levels, current stock levels, reorder point, cost, selling price, etc.
对于库存,我需要能够跟踪理想的库存水平、当前的库存水平、再订货点、成本、售价等。
Not every item in the inventory is "sellable." For example, I may want to keep inventory of plastic cups used for sodas. Meaning, each time I sell a soda, I need to subtract one from the plastic cup's inventory count. Thus, a "medium coke" is actually the plastic cup, some napkins and the fluid, each item having its own current stock levels, cost, etc.
并非库存中的每件商品都是“可销售的”。例如,我可能想要保存用于苏打水的塑料杯的库存。意思是,每次我出售苏打水时,我都需要从塑料杯的库存数量中减去一个。因此,“中等焦炭”实际上是塑料杯、一些餐巾纸和液体,每件物品都有自己当前的库存水平、成本等。
Then there is the concept of "combos." Perhaps a $1 medium Coke and a $3 hamburger are sold together as a combo for just $3.50 (a $0.50 savings). The Coke was mentioned to include some napkins. Say the hamburger also includes napkins on its own. However, as a combo, the buyer does not get the napkin for the Coke and the hamburger; rather the buyer only gets the same amount of napkins as if he/she were buying just the Coke.
然后是“组合”的概念。也许 1 美元的中等可乐和 3 美元的汉堡包作为组合一起出售,只需 3.50 美元(节省 0.50 美元)。提到可乐包括一些餐巾纸。假设汉堡包本身还包括餐巾纸。然而,作为一个组合,购买者没有得到可乐和汉堡包的餐巾纸;相反,买家只会得到与他/她只购买可乐一样数量的餐巾纸。
For the sales system, I need to keep track of every sale and possibly maintain a relationship with the inventory records (this would mean that I could never really delete an item in the inventory once a sale is made -- for historical purposes). When I sell a "medium Coke" for $1, perhaps I should break it down as $0.90 for the fluid and $0.10 for the plastic cup.
对于销售系统,我需要跟踪每笔销售,并可能保持与库存记录的关系(这意味着一旦销售完成,我就永远无法真正删除库存中的项目——出于历史目的)。当我以 1 美元的价格出售“中等可乐”时,也许我应该将其分解为液体 0.90 美元和塑料杯 0.10 美元。
And when I sell a "combo", maybe I need to be able to specify that the hamburger actually sold for $3 and the medium Coke was just $0.50 (only the soda was discounted to make the combo more appealing).
当我出售“组合”时,也许我需要能够说明汉堡包的实际售价为 3 美元,而中等可乐的售价仅为 0.50 美元(只有苏打水打折以使组合更有吸引力)。
This can't be a new problem. Does anyone have any ideas (or examples) I can look at to solve this problem?I'm not sure how to model inventory, the sellable items (especially the combos), and how to record the sales.
这不可能是一个新问题。有没有人有任何想法(或例子)我可以看看来解决这个问题?我不确定如何对库存、可售商品(尤其是组合)以及如何记录销售额进行建模。
回答by Joel Brown
The solution you are looking for will rely on an accounting style model and a couple of bills of materials (BOM). Your major entity types will include:
您正在寻找的解决方案将依赖于会计风格模型和几个物料清单 (BOM)。您的主要实体类型将包括:
SKUThis is the list of things that you sell. It's properties will include things like product description and current retail price. You can get fancy and break price out into a child table that gives prices over time. Let's assume that you are going to leave that wrinkle out for now. Some SKUs can be "combos" of the sort you are talking about.
COMPONENTThis is the list of things that make up a SKU, such as napkins, cups, buns, patties, coke syrup etc. - to use your example. Just as SKU has descriptions and prices, COMPONENTs have descriptions and unit costs. (Which can also be historized in a child table.) This table is where you would typically store your ROP too.
COMPOSITIONThis is a BOM which intersects SKU and COMPONENT and says how many units of each COMPONENT go into a unit of a SKU. You need one of these to intersect two SKUs too (for combos). You can either use one table or two tables for this. Two tables will keep the purists happy, one table will be expedient from a coder point of view.
SALEThis is a transaction table that provides a header for recording a sale of one or more SKUs. This table would have things like transaction date, cashier ID, and other header items.
SALE_ITEMThis is the transaction detail table that would include which SKU was sold (and how many) and for how much. The how much is a denormalization of the SKU price at time of sale, but could also include any special overrides to the price. The price actually charged for the SKU is a good thing to denormalize because someone could edit the list price in SKU and then you'd lose track of how much was actually charged for the item at the time.
INVENTORY_HDRThis is a transactional table that is similar to the SALE conceptually, but it is the header for an inventory transaction, such as receiving new inventory, using up inventory (as in selling it) and for inventory adjustments. Again, this would be date/description stuff, but it can include a direct link to a SALE_ITEM for inventory movements that are sales if you like. You don't have to do it that way, but some people like to establish the connection between revenues and costs on a transaction by transaction basis.
INVENTORY_DTLThis is the detail for an inventory transaction. This indicates which COMPONENT is going in or out, the quantity that went in or out, and the INVENTORY_HDR transaction that this movement applied to. This would also be where you keep the actual cost paid for the component item.
LOCATIONYou can (if you wish) also track the physical location of the inventory that you receive and use/sell. In a restaurant this may not be important but if you have a chain or if your restaurant has an offsite warehouse for component ingredients then you might care.
SKU这是您销售的商品清单。它的属性将包括产品描述和当前零售价格等内容。您可以想象并将价格分解为一个子表,该表给出了一段时间内的价格。让我们假设你现在要去掉皱纹。某些 SKU 可以是您所谈论的那种“组合”。
组件这是组成 SKU 的物品列表,例如餐巾纸、杯子、面包、肉饼、可乐糖浆等 - 以您的示例为例。正如 SKU 有描述和价格一样,COMPONENT 也有描述和单位成本。(也可以在子表中对其进行历史记录。)该表也是您通常存储 ROP 的地方。
COMPOSITION这是一个 BOM,它与 SKU 和 COMPONENT 相交,并说明每个 COMPONENT 的单元有多少进入一个 SKU 的单元。您也需要其中之一来与两个 SKU 相交(用于组合)。为此,您可以使用一张桌子或两张桌子。两张桌子会让纯粹主义者开心,一张桌子从编码员的角度来看是权宜之计。
SALE这是一个交易表,提供用于记录一个或多个 SKU 的销售的标题。该表将包含交易日期、收银员 ID 和其他标题项目等内容。
SALE_ITEM这是交易明细表,其中包括售出的 SKU(和数量)和销售量。销售时 SKU 价格的非规范化程度,但也可以包括对价格的任何特殊覆盖。为 SKU 实际收取的价格是非规范化的一件好事,因为有人可以编辑 SKU 中的标价,然后您就会忘记当时为该项目实际收取了多少费用。
INVENTORY_HDR这是一个在概念上类似于 SALE 的交易表,但它是库存交易的标题,例如接收新库存、用完库存(如销售)和库存调整。同样,这将是日期/描述内容,但如果您愿意,它可以包含指向 SALE_ITEM 的直接链接,用于作为销售的库存移动。您不必这样做,但有些人喜欢在逐笔交易的基础上建立收入和成本之间的联系。
INVENTORY_DTL这是库存交易的详细信息。这表明哪个 COMPONENT 进入或退出,进入或退出的数量,以及此移动应用于的 INVENTORY_HDR 事务。这也是您保留为组件项目支付的实际成本的地方。
位置您还可以(如果愿意)跟踪您收到和使用/出售的库存的物理位置。在餐厅,这可能并不重要,但如果您有连锁店,或者您的餐厅有一个用于存放成分的异地仓库,那么您可能会关心。
To do your revenue accounting you would be adding up the money recorded in the SALE_ITEM table.
要进行收入核算,您需要将 SALE_ITEM 表中记录的金额相加。
Stock levels are calculatedbased on adding up the INVENTORY_DTL ins and outs for each COMPONENT. (Don't store current stock levels in a table - This is doomed to cause reconciliation problems.)
库存水平是根据将每个 COMPONENT 的 INVENTORY_DTL 进出量相加而计算出来的。(不要将当前库存水平存储在表格中 - 这注定会导致对帐问题。)
To do your cost accounting you would be adding up the money recorded in the INVENTORY_DTL table. Note that you won't usually know exactly whichnapkin or bun you sold, so it won't be possible to link specific component reciepts with specific SKU sales. Instead, you need to have a convention for determining which components were used for any given SKU. You may have accounting rules that specify what convention you are required to use. Most people use FIFO. Some industries use LIFO and I've even seen weighted average cost accounting.
要进行成本核算,您需要将 INVENTORY_DTL 表中记录的金额相加。请注意,您通常不会确切地知道您销售的是哪种餐巾纸或面包,因此不可能将特定组件收货与特定 SKU 销售联系起来。相反,您需要有一个约定来确定用于任何给定 SKU 的组件。您可能有指定需要使用的约定的会计规则。大多数人使用先进先出。一些行业使用 LIFO,我什至见过加权平均成本会计。
回答by Shlomo
I would suggest completely separating the inventory tables from the money accounting tables. For example, the example you gave I know to be ridiculous: For your average fast food restaurant a $.90 cent Coke costs about $.05-$.07 for the cup, and less than a penny for the liquid, leaving a tidy profit of $.83ish. Why do the costs have to add up to $.90?
我建议将库存表与货币会计表完全分开。例如,我知道你举的例子很荒谬:对于普通的快餐店来说,一杯 0.90 美分的可乐杯的成本约为 0.05-0.07 美元,而液体的成本不到 1 美分,留下整洁的0.83 美元的利润。为什么成本必须加起来为 0.90 美元?
Tables:
表格:
InventoryItems
fields: InventoryItemId, Name, CurrentInventoryLevel, IdealInventoryLevel
InventoryItems
字段:InventoryItemId、Name、CurrentInventoryLevel、IdealInventoryLevel
InventoryChangeRecords
fields: InventoryChangeId, InventoryItemId, Change (int)
InventoryChangeRecords
字段:InventoryChangeId、InventoryItemId、Change (int)
RetailItems
fields: RetailItemId, Name, Price
RetailItems
字段:RetailItemId、名称、价格
RetailItemMakeup
fields: RetailItemId, InventoryItemId, Quantity
RetailItemMakeup
字段:RetailItemId、InventoryItemId、Quantity
SaleTransactions
fields: SaleTransactionId, DateTime, TotalSale
SaleTransactions
字段:SaleTransactionId、DateTime、TotalSale
SaleTransactionItems
fields: SaleTransactionId, RetailItemId, Quantity
SaleTransactionItems
字段:SaleTransactionId、RetailItemId、数量
For each sale, you should use a sproc (or trigger) to update CurrentInventoryLevel, and insert records into InventoryChangeRecords. You can easily figure out how any sale impacted inventory by joining from SaleTransaction to SaleTransactionItems to RetailItemMakeup.
对于每次销售,您应该使用 sproc(或触发器)来更新 CurrentInventoryLevel,并将记录插入 InventoryChangeRecords。通过从 SaleTransaction 到 SaleTransactionItems 再到 RetailItemMakeup,您可以轻松了解任何销售对库存的影响。
If you wanted to do it in an even stronger (but IMO extraneous) manner, you could create another many-to-many table between SaleTransactionItems and InventoryChangeRecords.
如果您想以更强大(但与 IMO 无关)的方式执行此操作,您可以在 SaleTransactionItems 和 InventoryChangeRecords 之间创建另一个多对多表。