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 : goodss) {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, RejectionCode.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}