S++Business systems language
DEMO SOURCE · SPP / APP / PHYRead the book

petshop-20-calculations.spp

petshop/order/petshop-20-calculations.spp

Download original file · Petstore source contents

1domain petshop;23atomic calculate priceLine(OrderLine) {4    OrderLine.amount = roundHalfUp(OrderLine.unitPrice * OrderLine.quantity, 2);5}67atomic calculate quoteOrder(Buyer, Cart, Goods[], ShopOrder) {8    ShopOrder.buyer = Buyer;9    ShopOrder.reservedQuantity = 0;10    ShopOrder.address = Buyer.address;11    ShopOrder.status = OrderStatus.UNPAID;12    ShopOrder.lines = new OrderLine[] {13    };14    ShopOrder.subtotal = 0;15    for (CartLine cartLine : Cart.lines) {16        for (Goods product : Goods[]) {17            if (product.sku == cartLine.goods.sku) {18                OrderLine line = new OrderLine();19                line.goods = product;20                line.name = product.name;21                line.quantity = cartLine.quantity;22                line.unitPrice = product.price;23                priceLine(line);24                ShopOrder.lines = append(ShopOrder.lines, line);25                ShopOrder.subtotal += line.amount;26            }27        }28    }29    ShopOrder.discount = 0;30    if (Buyer.tier == MemberTier.VIP) {31        ShopOrder.discount = roundHalfUp(ShopOrder.subtotal * 0.1, 2);32    }33    ShopOrder.shipping = 8;34    if (ShopOrder.subtotal - ShopOrder.discount >= 100) {35        ShopOrder.shipping = 0;36    }37    ShopOrder.total = roundHalfUp(ShopOrder.subtotal - ShopOrder.discount + ShopOrder.shipping, 2);38}3940atomic calculate reserveOrder(Cart, ShopOrder) {41    ShopOrder.reservedQuantity = 0;42    for (OrderLine line : ShopOrder.lines) {43        ShopOrder.reservedQuantity += line.quantity;44    }45    Cart.lines = new CartLine[] {46    };47}4849atomic check checkOrderOwner(Buyer, ShopOrder) {50    require(ShopOrder.buyer == Buyer, "ORDER_NOT_FOUND");51}5253atomic calculate prepareUnpaidCancellation(ShopOrder, UnpaidCancellationAgreement) {54    UnpaidCancellationAgreement.releasedQuantity = 0;55    for (OrderLine line : ShopOrder.lines) {56        UnpaidCancellationAgreement.releasedQuantity += line.quantity;57    }58    ShopOrder.status = OrderStatus.CANCELLED;59    ShopOrder.reason = UnpaidCancellationAgreement.reason;60}6162atomic calculate preparePaidCancellation(ShopOrder, PaidCancellationAgreement) {63    PaidCancellationAgreement.releasedQuantity = 0;64    for (OrderLine line : ShopOrder.lines) {65        PaidCancellationAgreement.releasedQuantity += line.quantity;66    }67    ShopOrder.status = OrderStatus.CANCELLED;68    ShopOrder.reason = PaidCancellationAgreement.reason;69}7071atomic calculate prepareCancellationRefund(ShopOrder, PaidCancellationAgreement) {72    PaidCancellationAgreement.refundAmount = ShopOrder.total;73}