@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
112 lines • 4.83 kB
JavaScript
import { OrderPricingAdapter, OrderPricingDirector, OrderPricingRowCategory, } from '@unchainedshop/core';
export const OrderPriceRound = {
...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, precision) => 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, currency) => {
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);
//# sourceMappingURL=order-round.js.map