cumsub
cumsub(..), process
def process cumsub(
item: text,
stock: number,
quantity: number,
rank: number) : number
The cumsub()
process computes the remaining stock
for each item
by processing grid lines grouped into bundles ordered by increasing rank
. For each bundle, if the available stock
is sufficient to cover the required quantity
for all grid lines, it decrements the stock
; otherwise, it marks the bundle as unpurchased.
It takes 4 vectors corresponding to the following parameters:
item
: the item identifier. All grid lines sharing the same value belong to the same item.stock
: the initial stock for the item. All grid lines for the sameitem
should have identicalstock
values.quantity
: the required quantity for purchasing the grid line.rank
: the bundle identifier. Grid lines with the samerank
form a bundle, and each(item, rank)
pair must be unique. Bundles are processed in order of increasingrank
.
Initially, the stock is set from the stock
vector. For each bundle, the process checks if the current stock is sufficient to cover the quantity
required by all grid lines. If so, it decrements the stock
for each item
and assigns the updated value to the corresponding grid lines. Otherwise, if there is insufficient stock, the function leaves the stock unchanged and outputs -(S+1)
for each grid line, where S
is the current remaining stock. This negative result signals that the grid line was not purchased and indicates the missing quantity via the expression quantity + S + 1
.
Example
table G = with
[| as Item, as Stock, as Quantity, as Rank |]
[| "A", 10, 4, 1 |]
[| "B", 5, 2, 1 |]
[| "A", 10, 3, 2 |]
[| "B", 5, 3, 2 |]
[| "A", 10, 2, 3 |]
[| "B", 5, 1, 4 |]
G.Remaining = cumsub(G.Item, G.Stock, G.Quantity, G.Rank)
show table "Result Inventory" with
G.Item
G.Stock
G.Quantity
G.Rank
G.Remaining
Which produces the following output:
Item | Stock | Quantity | Rank | Remaining |
---|---|---|---|---|
A | 10 | 4 | 1 | 6 |
B | 5 | 2 | 1 | 3 |
A | 10 | 3 | 2 | 3 |
B | 5 | 3 | 2 | 0 |
A | 10 | 2 | 3 | 1 |
B | 5 | 1 | 4 | -1 |