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