Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to net #611

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/core-orders/src/director/OrderPricingSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ export const OrderPricingSheet = (
});
},

gross() {
// tax is included 2 times, this is only true for Order Pricing!
return basePricingSheet.sum() - this.taxSum();
},

net() {
return basePricingSheet.sum() - this.taxSum() - this.taxSum();
discountSum(discountId) {
return basePricingSheet.sum({
category: OrderPricingRowCategory.Discounts,
discountId,
});
},

total({ category, useNetPrice, discountId } = { useNetPrice: false }) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/src/pricing/order-delivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const OrderDelivery: IOrderPricingAdapter<UnchainedCore> = {
params.context,
);
const tax = pricing.taxSum();
const shipping = pricing.gross();
const shipping = pricing.net();

pricingAdapter
.resultSheet()
Expand Down
7 changes: 6 additions & 1 deletion packages/plugins/src/pricing/order-discount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ export const OrderDiscount: IOrderPricingAdapter<UnchainedCore, OrderDiscountCon
Math.max(0, Math.min(amountLeft, leftInFeesToSplit)),
);
amountLeft -= deliveryAndPaymentDiscountAmount;
const discountAmount = (itemsDiscountAmount + deliveryAndPaymentDiscountAmount) * -1;
const discountAmount =
(itemsDiscountAmount +
deliveryAndPaymentDiscountAmount -
itemsTaxAmount -
deliveryAndPaymentTaxAmount) *
-1;
const taxAmount = (itemsTaxAmount + deliveryAndPaymentTaxAmount) * -1;
if (discountAmount) {
pricingAdapter.resultSheet().addDiscount({
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/src/pricing/order-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const OrderItems: IOrderPricingAdapter<UnchainedCore> = {
params.context,
);
const tax = pricing.taxSum();
const items = pricing.gross();
const items = pricing.net();
return {
taxes: current.taxes + tax,
items: current.items + items,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/src/pricing/order-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const OrderPayment: IOrderPricingAdapter<UnchainedCore> = {
params.context,
);
const tax = pricing.taxSum();
const paymentFees = pricing.gross();
const paymentFees = pricing.net();

pricingAdapter
.resultSheet()
Expand Down
16 changes: 9 additions & 7 deletions packages/utils/src/director/BasePricingSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ export interface PricingSheetParams<Calculation extends PricingCalculation> {
export const BasePricingSheet = <Calculation extends PricingCalculation>(
params: PricingSheetParams<Calculation>,
): IBasePricingSheet<Calculation> => {
const calculation = params.calculation || [];

const pricingSheet: IBasePricingSheet<Calculation> = {
calculation: params.calculation || [],
calculation,
currency: params.currency,
quantity: params.quantity,

getRawPricingSheet() {
return this.calculation;
return calculation;
},

isValid() {
return this.calculation.length > 0;
return calculation.length > 0;
},

sum(filter) {
Expand Down Expand Up @@ -89,7 +91,7 @@ export const BasePricingSheet = <Calculation extends PricingCalculation>(

return {
amount: Math.round(useNetPrice ? netAmount : netAmount + taxAmountForCategory),
currency: this.currency,
currency: params.currency,
};
},

Expand All @@ -100,20 +102,20 @@ export const BasePricingSheet = <Calculation extends PricingCalculation>(
(row: Calculation) =>
!!row && (filter[filterKey] === undefined || row[filterKey] === filter[filterKey]),
),
this.calculation,
calculation,
);

return filteredCalculation;
},

resetCalculation(calculationSheet) {
calculationSheet.filterBy().forEach(({ amount, ...row }: Calculation) => {
this.calculation.push({
pricingSheet.calculation.push({
...row,
amount: amount * -1,
} as Calculation);
});
return this.calculation;
return pricingSheet.calculation;
},
};

Expand Down
18 changes: 17 additions & 1 deletion tests/seeds/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const SimpleOrder = {
category: 'ITEMS',
amount: 30000,
},
{ category: 'TAXES', amount: 2144.8467966573808 },
{ category: 'ITEMS', amount: -2144.8467966573808 },
{ category: 'TAXES', amount: 2144.8467966573808, baseCategory: 'ITEMS' },
{
category: 'PAYMENT',
amount: 0,
Expand Down Expand Up @@ -115,6 +116,7 @@ export const SimplePosition = {
isTaxable: false,
isNetPrice: false,
rate: 0.077,
baseCategory: 'ITEM',
meta: {
adapter: 'shop.unchained.pricing.product-swiss-tax',
},
Expand Down Expand Up @@ -214,9 +216,14 @@ export const DiscountedOrder = {
category: 'ITEMS',
amount: 100000,
},
{
category: 'ITEMS',
amount: -7149.489322191272,
},
{
category: 'TAXES',
amount: 7149.489322191272,
baseCategory: 'ITEMS',
},
{
category: 'PAYMENT',
Expand All @@ -231,9 +238,16 @@ export const DiscountedOrder = {
amount: -10000,
discountId: 'discounted-order-discount',
},
{
category: 'DISCOUNTS',
amount: 714.9489322191266,
discountId: 'discounted-order-discount',
},
{
category: 'TAXES',
amount: -714.9489322191266,
baseCategory: 'DISCOUNTS',
discountId: 'discounted-order-discount',
},
],
};
Expand Down Expand Up @@ -304,6 +318,7 @@ export const DiscountedPosition = {
category: 'TAX',
amount: 8579.387186629523,
rate: 0.077,
baseCategory: 'ITEM',
meta: {
adapter: 'shop.unchained.pricing.product-swiss-tax',
},
Expand All @@ -321,6 +336,7 @@ export const DiscountedPosition = {
category: 'TAX',
amount: -4289.6935933147615,
rate: 0.077,
baseCategory: 'DISCOUNT',
meta: {
adapter: 'shop.unchained.pricing.product-swiss-tax',
},
Expand Down
Loading