S++Core systems, human-reviewable
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 : goodss) {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, RejectionCode.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}