S++Business systems language
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 goods;101    quantum int balance;102}103104take Goods as StockSupply {105    Goods goods = Goods;106}107108Role StockLine {109    Goods goods;110    quantum int available;111    quantum int reserved;112    quantum int sold;113}114115take Goods as StockLine {116    Goods goods = Goods;117}118119Contract InventoryOpeningAgreement {120    Goods[] goods;121    StockLine[] inventory;122}123124atomic calculate initializeInventory(Goods[], StockLine[], StockSupply[]) {125    StockLine[] = new StockLine[] {126    };127    StockSupply[] = new StockSupply[] {128    };129    for (Goods product : Goods[]) {130        StockLine stock = new StockLine();131        stock.goods = product;132        stock.available = 0;133        stock.reserved = 0;134        stock.sold = 0;135        StockLine[] = append(StockLine[], stock);136        StockSupply source = new StockSupply();137        source.goods = product;138        source.balance = 0;139        StockSupply[] = append(StockSupply[], source);140    }141}142143quantum update reserveInventory(StockLine[], ShopOrder) {144    for (StockLine stock : StockLine[]) {145        for (OrderLine line : ShopOrder.lines) {146            if (stock.goods.sku == line.goods.sku) {147                stock.available -= line.quantity;148                stock.reserved += line.quantity;149            }150        }151    }152}153154quantum update shipInventory(StockLine[], ShipmentAgreement) {155    for (StockLine stock : StockLine[]) {156        for (OrderLine line : ShipmentAgreement.order.lines) {157            if (stock.goods.sku == line.goods.sku) {158                stock.reserved -= line.quantity;159                stock.sold += line.quantity;160            }161        }162    }163}164165quantum update releaseUnpaidInventory(StockLine[], UnpaidCancellationAgreement) {166    for (StockLine stock : StockLine[]) {167        for (OrderLine line : UnpaidCancellationAgreement.order.lines) {168            if (stock.goods.sku == line.goods.sku) {169                stock.reserved -= line.quantity;170                stock.available += line.quantity;171            }172        }173    }174}175176quantum update releasePaidInventory(StockLine[], PaidCancellationAgreement) {177    for (StockLine stock : StockLine[]) {178        for (OrderLine line : PaidCancellationAgreement.order.lines) {179            if (stock.goods.sku == line.goods.sku) {180                stock.reserved -= line.quantity;181                stock.available += line.quantity;182            }183        }184    }185}186187quantum update returnInventory(StockLine[], ReturnRefundAgreement) {188    for (StockLine stock : StockLine[]) {189        for (OrderLine line : ReturnRefundAgreement.order.lines) {190            if (stock.goods.sku == line.goods.sku) {191                stock.sold -= line.quantity;192                stock.available += line.quantity;193            }194        }195    }196}197198atomic calculate prepareInventoryOpening(Goods[], StockLine[], InventoryOpeningAgreement) {199    InventoryOpeningAgreement.goods = Goods[];200    InventoryOpeningAgreement.inventory = StockLine[];201}202203Scenario serial openInventory(Goods[], StockLine[], StockSupply[], InventoryOpeningAgreement) {204    initializeInventory;205    prepareInventoryOpening;206    transaction {207        insertStockLine;208        insertStockSupply;209    }210}211212Reference PetReservationAgreement from petshop.PetReservationAgreement {213    Goods goods;214    int quantity;215}216217quantum update reserveRequestedInventory(StockLine[], PetReservationAgreement) {218    for (StockLine item : StockLine[]) {219        if (item.goods.sku == PetReservationAgreement.goods.sku) {220            item.available -= PetReservationAgreement.quantity;221            item.reserved += PetReservationAgreement.quantity;222        }223    }224}225226Reference StockReceiptAgreement from petshop.StockReceiptAgreement {227    Goods goods;228    StockLine stock;229    StockSupply source;230    int quantity;231}232233quantum update receiveInventory(StockLine, StockSupply, StockReceiptAgreement) {234    StockSupply.balance -= StockReceiptAgreement.quantity;235    StockLine.available += StockReceiptAgreement.quantity;236}