UNPKG

@unchainedshop/plugins

Version:

Because of a Typescript issue with upstream "postfinancecheckout", the Postfinance plugin has been disabled from transpilation, import the source ts files from src and enable node_module tsc or copy over the src/payment/postfinance-checkout to your projec

144 lines (126 loc) 4.24 kB
import { OrderPricingAdapter, OrderPricingDirector, IOrderPricingAdapter, OrderPricingRowCategory, } from '@unchainedshop/core'; interface PriceRoundSettings { defaultPrecision: number; roundTo: (value: number, precision: number, currency: string) => number; } export const OrderPriceRound: IOrderPricingAdapter & { configure: (params: PriceRoundSettings) => void; settings: PriceRoundSettings; } = { ...OrderPricingAdapter, key: 'shop.unchained.pricing.order-round', version: '1.0.0', label: 'Round order price to the next precision number', orderIndex: 90, isActivatedFor: () => { return true; }, settings: { defaultPrecision: 5, roundTo: (value: number, precision: number) => precision !== 0 ? Math.round(value / precision) * precision : value, }, configure({ defaultPrecision, roundTo }) { if (defaultPrecision) this.settings.defaultPrecision = defaultPrecision; if (roundTo) this.settings.roundTo = roundTo; }, actions: (params) => { const pricingAdapter = OrderPricingAdapter.actions(params); const calculateDifference = (amount: number, currency: string) => { const roundedAmount = OrderPriceRound.settings.roundTo( amount, OrderPriceRound.settings.defaultPrecision, currency, ); return roundedAmount - amount; }; return { ...pricingAdapter, calculate: async () => { const { currency } = params.context; const { amount: deliveryAmount } = params.calculationSheet.total({ category: OrderPricingRowCategory.Delivery, useNetPrice: true, }); if (deliveryAmount) { pricingAdapter.resultSheet().addDelivery({ amount: calculateDifference(deliveryAmount, currency), taxAmount: 0, meta: { adapter: OrderPriceRound.key, }, }); } const { amount: discountAmount } = params.calculationSheet.total({ category: OrderPricingRowCategory.Discounts, useNetPrice: true, }); if (discountAmount) { pricingAdapter.resultSheet().addDiscount({ amount: calculateDifference(discountAmount, currency), taxAmount: 0, meta: { adapter: OrderPriceRound.key, }, discountId: null, }); } const { amount: itemsAmount } = params.calculationSheet.total({ category: OrderPricingRowCategory.Items, useNetPrice: true, }); if (itemsAmount) { pricingAdapter.resultSheet().addItems({ amount: calculateDifference(itemsAmount, currency), taxAmount: 0, meta: { adapter: OrderPriceRound.key, }, }); } const { amount: paymentAmount } = params.calculationSheet.total({ category: OrderPricingRowCategory.Payment, useNetPrice: true, }); if (paymentAmount) { pricingAdapter.resultSheet().addPayment({ amount: calculateDifference(paymentAmount, currency), taxAmount: 0, meta: { adapter: OrderPriceRound.key, }, }); } const taxesAmount = params.calculationSheet.taxSum({ category: OrderPricingRowCategory.Taxes, }); if (taxesAmount) { const taxDifference = calculateDifference(taxesAmount, currency); pricingAdapter.resultSheet().calculation.push({ category: OrderPricingRowCategory.Taxes, amount: taxDifference, meta: { adapter: OrderPriceRound.key, }, }); // WORKAROUND BECAUSE ORDER TOTAL GROSS PRICE IS CALCULATED WITH A SUM OF EVERYTHING EXCEPT TAXES // AS OF UNCHAINED <= 2.6 pricingAdapter.resultSheet().calculation.push({ category: null, amount: taxDifference, meta: { adapter: OrderPriceRound.key, }, }); } return pricingAdapter.calculate(); }, }; }, }; OrderPricingDirector.registerAdapter(OrderPriceRound);