Skip to content

Commit

Permalink
Ensure computation of before discount totals takes into account all p…
Browse files Browse the repository at this point in the history
…rices and not just the discounted ones (#144)

Co-authored-by: José Carneiro <[email protected]>
  • Loading branch information
josecarneiro and José Carneiro authored Dec 9, 2024
1 parent 0d97fc9 commit 2c8fba8
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 45 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/fixtures/price.samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3166,6 +3166,31 @@ export const compositePriceWithComponentsWithCoupons: CompositePriceItemDto = {
description: 'Wallbox 11 kW',
_coupons: [percentage10DiscountCoupon],
},
{
_id: 'price#4-comp#3',
pricing_model: 'per_unit',
unit_amount: 10000,
unit_amount_currency: 'EUR',
unit_amount_decimal: '100.00',
sales_tax: 'standard',
active: true,
price_display_in_journeys: 'show_price',
type: 'one_time',
_schema: 'price',
_title: 'Wallbox 11 kW',
tax: [tax10percent],
is_tax_inclusive: true,
_org: '739224',
_created_at: '2022-06-29T13:38:53.619Z',
_updated_at: '2022-06-29T20:34:50.964Z',
billing_period: 'weekly',
billing_duration_unit: 'months',
notice_time_unit: 'months',
termination_time_unit: 'months',
renewal_duration_unit: 'months',
description: 'Wallbox 11 kW',
_coupons: [],
},
],
_id: 'price#4',
_org: '739224',
Expand Down
186 changes: 144 additions & 42 deletions src/__tests__/fixtures/pricing.results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6653,8 +6653,8 @@ export const computedResultWithPricesWithAndWithoutCoupons = {
amount_subtotal_decimal: '3889803.753636364',
amount_total_decimal: '3889817.39',
amount_tax: 1364,
before_discount_amount_total: 20000,
before_discount_amount_total_decimal: '200',
before_discount_amount_total: 388986739,
before_discount_amount_total_decimal: '3889867.39',
discount_amount: 5000,
discount_amount_decimal: '50',
},
Expand Down Expand Up @@ -7352,11 +7352,11 @@ export const computedRecurringPriceWithFixedAmountCashbackCoupon = {
* Composite price with a coupon applied to it resulting in a fixed discount
*/
export const computedCompositePriceWithComponentsWithDiscounts = {
amount_subtotal: 8636,
amount_total: 9500,
amount_tax: 864,
amount_subtotal: 17727,
amount_total: 19500,
amount_tax: 1773,
total_details: {
amount_tax: 864,
amount_tax: 1773,
breakdown: {
taxes: [
{
Expand All @@ -7365,7 +7365,7 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
type: 'VAT',
rate: 10,
},
amount: 864,
amount: 1773,
},
],
recurrences: [
Expand All @@ -7386,15 +7386,15 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
},
{
type: 'one_time',
unit_amount_gross: 9000,
unit_amount_net: 8182,
amount_subtotal: 8182,
amount_total: 9000,
amount_subtotal_decimal: '81.818181818182',
amount_total_decimal: '90',
amount_tax: 818,
before_discount_amount_total: 10000,
before_discount_amount_total_decimal: '100',
unit_amount_gross: 19000,
unit_amount_net: 17273,
amount_subtotal: 17273,
amount_total: 19000,
amount_subtotal_decimal: '172.727272727273',
amount_total_decimal: '190',
amount_tax: 1727,
before_discount_amount_total: 20000,
before_discount_amount_total_decimal: '200',
discount_amount: 1000,
discount_amount_decimal: '10',
},
Expand All @@ -7412,21 +7412,21 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
type: 'VAT',
rate: 10,
},
amount: 864,
amount: 1773,
},
},
{
type: 'one_time',
amount_total: 9000,
amount_subtotal: 8182,
amount_tax: 818,
amount_total: 19000,
amount_subtotal: 17273,
amount_tax: 1727,
tax: {
tax: {
_id: '10',
type: 'VAT',
rate: 10,
},
amount: 864,
amount: 1773,
},
},
],
Expand Down Expand Up @@ -7538,6 +7538,38 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
},
],
},
{
_id: 'price#4-comp#3',
pricing_model: 'per_unit',
unit_amount: 10000,
unit_amount_currency: 'EUR',
unit_amount_decimal: '100.00',
sales_tax: 'standard',
active: true,
price_display_in_journeys: 'show_price',
type: 'one_time',
_title: 'Wallbox 11 kW',
tax: [
{
_id: '10',
rate: 10,
type: 'VAT',
_schema: 'tax',
_org: '739224',
_title: '',
_created_at: '2022-06-29T20:26:19.020Z',
_updated_at: '2022-06-29T20:26:19.020Z',
},
],
is_tax_inclusive: true,
billing_period: 'weekly',
billing_duration_unit: 'months',
notice_time_unit: 'months',
termination_time_unit: 'months',
renewal_duration_unit: 'months',
description: 'Wallbox 11 kW',
_coupons: [],
},
],
_id: 'price#4',
price_display_in_journeys: 'show_price',
Expand Down Expand Up @@ -7764,12 +7796,82 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
discount_amount_decimal: '10',
before_discount_amount_total_decimal: '100',
},
{
pricing_model: 'per_unit',
quantity: 1,
type: 'one_time',
price_id: 'price#4-comp#3',
product_id: 'prod-id#1234',
_price: {
_id: 'price#4-comp#3',
pricing_model: 'per_unit',
unit_amount: 10000,
unit_amount_currency: 'EUR',
unit_amount_decimal: '100.00',
sales_tax: 'standard',
active: true,
price_display_in_journeys: 'show_price',
type: 'one_time',
_title: 'Wallbox 11 kW',
tax: [
{
_id: '10',
rate: 10,
type: 'VAT',
_schema: 'tax',
_org: '739224',
_title: '',
_created_at: '2022-06-29T20:26:19.020Z',
_updated_at: '2022-06-29T20:26:19.020Z',
},
],
is_tax_inclusive: true,
billing_period: 'weekly',
billing_duration_unit: 'months',
notice_time_unit: 'months',
termination_time_unit: 'months',
renewal_duration_unit: 'months',
description: 'Wallbox 11 kW',
_coupons: [],
},
_product: {},
taxes: [
{
tax: {
_id: '10',
rate: 10,
type: 'VAT',
_schema: 'tax',
_org: '739224',
_title: '',
_created_at: '2022-06-29T20:26:19.020Z',
_updated_at: '2022-06-29T20:26:19.020Z',
},
amount: 909,
},
],
_coupons: [],
currency: 'EUR',
description: 'Wallbox 11 kW',
unit_amount: 10000,
unit_amount_net: 9091,
unit_amount_net_decimal: '90.909090909091',
unit_amount_gross: 10000,
unit_amount_gross_decimal: '100',
unit_amount_decimal: '100.00',
amount_subtotal: 9091,
amount_total: 10000,
amount_tax: 909,
is_tax_inclusive: true,
amount_subtotal_decimal: '90.909090909091',
amount_total_decimal: '100',
},
],
amount_subtotal: 8636,
amount_total: 9500,
amount_tax: 864,
amount_subtotal: 17727,
amount_total: 19500,
amount_tax: 1773,
total_details: {
amount_tax: 864,
amount_tax: 1773,
breakdown: {
taxes: [
{
Expand All @@ -7778,7 +7880,7 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
type: 'VAT',
rate: 10,
},
amount: 864,
amount: 1773,
},
],
recurrences: [
Expand All @@ -7799,15 +7901,15 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
},
{
type: 'one_time',
unit_amount_gross: 9000,
unit_amount_net: 8182,
amount_subtotal: 8182,
amount_total: 9000,
amount_subtotal_decimal: '81.818181818182',
amount_total_decimal: '90',
amount_tax: 818,
before_discount_amount_total: 10000,
before_discount_amount_total_decimal: '100',
unit_amount_gross: 19000,
unit_amount_net: 17273,
amount_subtotal: 17273,
amount_total: 19000,
amount_subtotal_decimal: '172.727272727273',
amount_total_decimal: '190',
amount_tax: 1727,
before_discount_amount_total: 20000,
before_discount_amount_total_decimal: '200',
discount_amount: 1000,
discount_amount_decimal: '10',
},
Expand All @@ -7825,29 +7927,29 @@ export const computedCompositePriceWithComponentsWithDiscounts = {
type: 'VAT',
rate: 10,
},
amount: 864,
amount: 1773,
},
},
{
type: 'one_time',
amount_total: 9000,
amount_subtotal: 8182,
amount_tax: 818,
amount_total: 19000,
amount_subtotal: 17273,
amount_tax: 1727,
tax: {
tax: {
_id: '10',
type: 'VAT',
rate: 10,
},
amount: 864,
amount: 1773,
},
},
],
cashbacks: [],
},
},
amount_subtotal_decimal: '86.363636363637',
amount_total_decimal: '95',
amount_subtotal_decimal: '177.272727272728',
amount_total_decimal: '195',
},
],
currency: 'EUR',
Expand Down
7 changes: 4 additions & 3 deletions src/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ const recomputeDetailTotals = (
const subTotalAmount = toDineroFromInteger(recurrence.amount_subtotal);
const totalAmount = toDineroFromInteger(recurrence.amount_total);
const taxAmount = toDineroFromInteger(recurrence.amount_tax!);
const beforeDiscountAmountTotal =
const existingRecurrenceBeforeDiscountAmountTotal =
typeof recurrence.before_discount_amount_total !== 'undefined'
? toDineroFromInteger(recurrence.before_discount_amount_total)
: undefined;
Expand All @@ -548,9 +548,10 @@ const recomputeDetailTotals = (
recurrence.amount_total_decimal = totalAmount.add(priceTotal).toUnit().toString();
recurrence.amount_tax = taxAmount.add(priceTax).getAmount();

if (priceBeforeDiscountAmountTotal) {
if (priceBeforeDiscountAmountTotal || existingRecurrenceBeforeDiscountAmountTotal) {
const baseAmount = priceBeforeDiscountAmountTotal || priceTotal;
const recurrenceBeforeDiscountAmountTotal =
beforeDiscountAmountTotal?.add(priceBeforeDiscountAmountTotal) ?? priceBeforeDiscountAmountTotal;
existingRecurrenceBeforeDiscountAmountTotal?.add(baseAmount) ?? baseAmount;
recurrence.before_discount_amount_total = recurrenceBeforeDiscountAmountTotal.getAmount();
recurrence.before_discount_amount_total_decimal = recurrenceBeforeDiscountAmountTotal.toUnit().toString();
}
Expand Down

0 comments on commit 2c8fba8

Please sign in to comment.