@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
98 lines (82 loc) • 2.93 kB
text/typescript
import { calculation as calcUtils } from '@unchainedshop/utils';
import {
ProductPricingDirector,
ProductPricingAdapter,
ProductDiscountConfiguration,
IProductPricingAdapter,
ProductPricingRowCategory,
Discount,
} from '@unchainedshop/core';
const ProductDiscount: IProductPricingAdapter<ProductDiscountConfiguration> = {
...ProductPricingAdapter,
key: 'shop.unchained.pricing.product-discount',
version: '1.0.0',
label: 'Apply Discounts on Product Price',
orderIndex: 30,
isActivatedFor: () => {
return true;
},
actions: (params) => {
const pricingAdapter = ProductPricingAdapter.actions(params);
const addDiscounts = (
discount: Discount<ProductDiscountConfiguration>,
total: number,
resolvedConfiguration: ProductDiscountConfiguration,
) => {
const { discountId } = discount;
const { isNetPrice = false, taxRate, ...meta } = resolvedConfiguration;
const amount = calcUtils.applyRate(resolvedConfiguration, total);
const isTaxable = taxRate === undefined || taxRate === null;
pricingAdapter.resultSheet().addDiscount({
amount: amount * -1,
discountId,
isNetPrice,
isTaxable,
meta: { adapter: ProductDiscount.key, ...meta },
});
if (!isTaxable && taxRate) {
const taxAmount = calcUtils.getTaxAmount(amount, taxRate, isNetPrice);
pricingAdapter.resultSheet().addTax({
amount: taxAmount * -1,
rate: taxRate,
discountId,
baseCategory: ProductPricingRowCategory.Discount,
meta: { adapter: ProductDiscount.key, discountId, ...meta },
});
if (!isNetPrice) {
pricingAdapter.resultSheet().addDiscount({
amount: taxAmount,
discountId,
isNetPrice: false,
isTaxable: false,
meta: { adapter: ProductDiscount.key, discountId, ...meta },
});
}
}
};
return {
...pricingAdapter,
calculate: async () => {
params.discounts.forEach((discount) => {
if (!discount) return;
const { configuration } = discount;
const resolvedConfiguration = configuration.customPriceConfigurationResolver
? configuration.customPriceConfigurationResolver(
params.context.product,
params.context.quantity,
params.context.configuration,
)
: configuration;
if (!resolvedConfiguration) return;
const total = params.calculationSheet.total({
category: ProductPricingRowCategory.Item,
useNetPrice: resolvedConfiguration.isNetPrice,
});
addDiscounts(discount, total.amount, resolvedConfiguration);
});
return pricingAdapter.calculate();
},
};
},
};
ProductPricingDirector.registerAdapter(ProductDiscount);