# 经济采购优先化

## 优先级列表的脚本模板

read "/sample/Lokad_Items.tsv" with Supplier: text, Category: text, SubCategory: text

orderingLeadtime := 7 // 7 days

where PO.DeliveryDate > PO.Date // exclude non-delivered POs
hierarchy: Category, SubCategory
present: (max(Orders.Date) by 1) + 1
leadtimeValue: PO.DeliveryDate - PO.Date + 1)

Demand = forecast.demand(
hierarchy: Category, SubCategory
present: (max(Orders.Date) by 1) + 1
demandDate: Orders.Date
demandValue: Orders.Quantity)

show form "Purchase simulator" a1b3 tomato with Form.budget as "Max budget"

M = SellPrice - BuyPrice
S = - 0.25 * SellPrice // stock-out penalty
C = - 0.3 * BuyPrice * mean(Leadtime) / 365 // % '0.3' as annual carrying cost
MB = 0.5 * SellPrice // back-order case
SB = 0.5 * SellPrice // back-order case
AM = 0.3 // opportunity to buy later
AC = 1 - 0.2 * mean(Leadtime) / 365 // % '0.2' as annual economic discount

RM = MB * uniform(1, Backorder) + (stockrwd.m(Demand, AM) * M) >> Backorder
RS = SB * uniform(1, Backorder) + zoz(stockrwd.s(Demand) * S) >> Backorder
RC = (stockrwd.c(Demand AC) * C) >> BackOrder
R = RM + RS + RC // plain recomposition

table G = extend.distrib(Demand >> BackOrder, StockOnHand + StockOnOrder, LotMultiplier)

where G.Max > StockOnHand + StockOnOrder
G.Q = G.Max - G.Min + 1
G.Reward = int(R, G.Min, G.Max) // integral of the stock reward function
G.Score = G.Reward / max(1, BuyPrice * G.Q)

G.Rank = rank(G.Score, Id, -G.Max) // top scores first, but preserve ordering of pairs (Id, G.Max)
G.Invest = cumsum(BuyPrice * G.Q, G.Rank)

where G.Invest < Form.budget + 0
where exists(G.Q)
show table "Purchase priority list with $\{Form.budget}" c1g3 tomato with Id as "Id" Supplier as "Supplier" StockOnHand as "OnHand" StockOnOrder as "OnOrder" sum(G.Q) as "Qty" mean(Leadtime) as "Leadtime" sum(G.Reward) as "Reward" unit:"$"
sum(BuyPrice * G.Q) as "Cost" unit:"\$"
group by Id
order by avg(G.Score) desc


• SellPrice：销售单价（不含税）
• BuyPrice：采购单价（不含税）

• M：库存中供应的每个库存单位的奖赏（毛利润）。
• S：库存无法满足需求时每个库存单位的惩罚。
• C：每个库存单位的单位年度持有成本。
• AM：边际部分的折扣系数。
• AC：承载成本部分的折扣系数。

• G.Reward：表示与库存中 G.Q 个额外库存单位相关的预期经济效益（G.Q 通常等于 1）。
• G.Score：表示投入库存的每一美元与收益额的比率。

• G.Rank：根据网格条目的计分对所有网格条目排序。但排序操作要进行调整：每个项目的顺序很大程度上与再订货点相一致。
• G.Invest：说明如何创建累积计算。在这里将计算总投资（累积），计算的前提是假定根据列表顺序来采购每个项目。