Skip to content

Commit

Permalink
fix(core): Correct handling of discounts & taxes when prices include tax
Browse files Browse the repository at this point in the history
Relates to #573
  • Loading branch information
michaelbromley committed Dec 3, 2020
1 parent 4ceac0f commit c04b1c7
Show file tree
Hide file tree
Showing 4 changed files with 541 additions and 220 deletions.
16 changes: 12 additions & 4 deletions packages/core/src/entity/order-item/order-item.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,26 @@ export class OrderItem extends VendureEntity {

@Calculated()
get discountedUnitPrice(): number {
return this.unitPrice + this.getAdjustmentsTotal(AdjustmentType.PROMOTION);
const result = this.listPrice + this.getAdjustmentsTotal(AdjustmentType.PROMOTION);
return this.listPriceIncludesTax ? netPriceOf(result, this.taxRate) : result;
}

@Calculated()
get discountedUnitPriceWithTax(): number {
return grossPriceOf(this.discountedUnitPrice, this.taxRate);
const result = this.listPrice + this.getAdjustmentsTotal(AdjustmentType.PROMOTION);
return this.listPriceIncludesTax ? result : grossPriceOf(result, this.taxRate);
}

@Calculated()
get proratedUnitPrice(): number {
return this.unitPrice + this.getAdjustmentsTotal();
const result = this.listPrice + this.getAdjustmentsTotal();
return this.listPriceIncludesTax ? netPriceOf(result, this.taxRate) : result;
}

@Calculated()
get proratedUnitPriceWithTax(): number {
return grossPriceOf(this.proratedUnitPrice, this.taxRate);
const result = this.listPrice + this.getAdjustmentsTotal();
return this.listPriceIncludesTax ? result : grossPriceOf(result, this.taxRate);
}

@Calculated()
Expand All @@ -129,6 +133,10 @@ export class OrderItem extends VendureEntity {
.reduce((total, a) => total + a.amount, 0);
}

addAdjustment(adjustment: Adjustment) {
this.adjustments = this.adjustments.concat(adjustment);
}

clearAdjustments(type?: AdjustmentType) {
if (!type) {
this.adjustments = [];
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/entity/order-line/order-line.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
return this.firstActiveItemPropOr('unitPriceWithTax', 0);
}

@Calculated()
get discountedUnitPrice(): number {
return this.firstActiveItemPropOr('discountedUnitPrice', 0);
}

@Calculated()
get discountedUnitPriceWithTax(): number {
return this.firstActiveItemPropOr('discountedUnitPriceWithTax', 0);
}

@Calculated()
get quantity(): number {
return this.activeItems.length;
Expand Down Expand Up @@ -99,9 +109,10 @@ export class OrderLine extends VendureEntity implements HasCustomFields {

@Calculated()
get discounts(): Adjustment[] {
const priceIncludesTax = this.items?.[0]?.listPriceIncludesTax ?? false;
return this.adjustments.map(adjustment => ({
...adjustment,
amount: grossPriceOf(adjustment.amount, this.taxRate),
amount: priceIncludesTax ? adjustment.amount : grossPriceOf(adjustment.amount, this.taxRate),
}));
}

Expand Down
Loading

0 comments on commit c04b1c7

Please sign in to comment.