S++Core systems, human-reviewable
DEMO SOURCE · SPP / APP / PHYRead the book

inventory.spp

inventory/inventory.spp

Download original file · Petstore source contents

1domain inventory;23Reference Goods from petshop.Goods {4    business("Goods number used for selection, inventory and settled order lines") String sku;5    String category;6    String name;7    String description;8    BigDecimal(19, 2) price;9    boolean active;10}1112Reference Buyer from petshop.Buyer {13    business("Membership account number used to identify the buyer and verify order ownership") String membershipNumber;14    business("Registration number of the linked customer-domain subject") String customerNumber;15    String name;16    String email;17    String address;18    MemberTier tier;19    BigDecimal(19, 2) wallet;20}2122Reference Merchant from petshop.Merchant {23    String customerNumber;24    String name;25    String email;26    String address;27    String tradingName;28    String shopName;29    BigDecimal(19, 2) cash;30}3132Reference OrderLine from petshop.OrderLine {33    String lineNumber;34    Goods goods;35    String name;36    int quantity;37    BigDecimal(19, 2) unitPrice;38    BigDecimal(19, 2) amount;39}4041Reference ShopOrder from petshop.ShopOrder {42    business("Order reference presented to the buyer") String orderNumber;43    Buyer buyer;44    OrderStatus status;45    OrderLine[] lines;46    int reservedQuantity;47    BigDecimal(19, 2) subtotal;48    BigDecimal(19, 2) discount;49    BigDecimal(19, 2) shipping;50    BigDecimal(19, 2) total;51    String address;52    String tracking;53    String reason;54}5556Reference Receipt from petshop.Receipt {57    business("Business receipt number") String receiptNumber;58    Buyer buyer;59    ShopOrder order;60    ReceiptStatus status;61    OrderStatus orderStatus;62}6364Reference ShipmentAgreement from petshop.ShipmentAgreement {65    Buyer buyer;66    ShopOrder order;67    String tracking;68    Receipt receipt;69    int shippedQuantity;70}7172Reference UnpaidCancellationAgreement from petshop.UnpaidCancellationAgreement {73    Buyer buyer;74    ShopOrder order;75    String reason;76    Receipt receipt;77    int releasedQuantity;78}7980Reference PaidCancellationAgreement from petshop.PaidCancellationAgreement {81    Buyer buyer;82    Merchant seller;83    ShopOrder order;84    String reason;85    Receipt receipt;86    int releasedQuantity;87    BigDecimal(19, 2) refundAmount;88}8990Reference ReturnRefundAgreement from petshop.ReturnRefundAgreement {91    Buyer buyer;92    Merchant seller;93    ShopOrder order;94    Receipt receipt;95    int returnedQuantity;96    BigDecimal(19, 2) refundAmount;97}9899Role StockSupply {100    Goods;101    quantum int balance;102}103104Role StockLine {105    Goods;106    quantum int available;107    quantum int reserved;108    quantum int sold;109}110111Contract InventoryOpeningAgreement {112    Goods[] goods;113    StockLine[] inventory;114}115116atomic calculate initializeInventory(Goods[], StockLine[], StockSupply[]) {117    stockLines = new StockLine[] {118    };119    stockSupplys = new StockSupply[] {120    };121    for (Goods product : goodss) {122        StockLine stock = new StockLine();123        stock.goods = product;124125        stockLines = append(stockLines, stock);126        StockSupply source = new StockSupply();127        source.goods = product;128129        stockSupplys = append(stockSupplys, source);130    }131}132133quantum update reserveInventory(StockLine[], ShopOrder) {134    for (StockLine stock : stockLines) {135        for (OrderLine line : shopOrder.lines) {136            if (stock.goods.sku == line.goods.sku) {137                stock.available -> stock.reserved : line.quantity;138            }139        }140    }141}142143quantum update shipInventory(StockLine[], ShipmentAgreement) {144    for (StockLine stock : stockLines) {145        for (OrderLine line : shipmentAgreement.order.lines) {146            if (stock.goods.sku == line.goods.sku) {147                stock.reserved -> stock.sold : line.quantity;148            }149        }150    }151}152153quantum update releaseUnpaidInventory(StockLine[], UnpaidCancellationAgreement) {154    for (StockLine stock : stockLines) {155        for (OrderLine line : unpaidCancellationAgreement.order.lines) {156            if (stock.goods.sku == line.goods.sku) {157                stock.reserved -> stock.available : line.quantity;158            }159        }160    }161}162163quantum update releasePaidInventory(StockLine[], PaidCancellationAgreement) {164    for (StockLine stock : stockLines) {165        for (OrderLine line : paidCancellationAgreement.order.lines) {166            if (stock.goods.sku == line.goods.sku) {167                stock.reserved -> stock.available : line.quantity;168            }169        }170    }171}172173quantum update returnInventory(StockLine[], ReturnRefundAgreement) {174    for (StockLine stock : stockLines) {175        for (OrderLine line : returnRefundAgreement.order.lines) {176            if (stock.goods.sku == line.goods.sku) {177                stock.sold -> stock.available : line.quantity;178            }179        }180    }181}182183atomic calculate prepareInventoryOpening(Goods[], StockLine[], InventoryOpeningAgreement) {184    inventoryOpeningAgreement.goods = goodss;185    inventoryOpeningAgreement.inventory = stockLines;186}187188Scenario serial openInventory(Goods[], StockLine[], StockSupply[], InventoryOpeningAgreement) {189    initializeInventory;190    prepareInventoryOpening;191    transaction {192        insertStockLine;193        insertStockSupply;194    }195}196197Reference PetReservationAgreement from petshop.PetReservationAgreement {198    Goods goods;199    int quantity;200}201202quantum update reserveRequestedInventory(StockLine[], PetReservationAgreement) {203    for (StockLine item : stockLines) {204        if (item.goods.sku == petReservationAgreement.goods.sku) {205            item.available -> item.reserved : petReservationAgreement.quantity;206        }207    }208}209210Reference StockReceiptAgreement from petshop.StockReceiptAgreement {211    Goods goods;212    StockLine stock;213    StockSupply source;214    int quantity;215}216217quantum update receiveInventory(StockLine, StockSupply, StockReceiptAgreement) {218    stockSupply.balance -> stockLine.available : stockReceiptAgreement.quantity;219}