b23-lib 3.0.4 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/dist/Auth/index.js.map +1 -1
  2. package/dist/Auth/index.mjs +1 -1
  3. package/dist/Classes/Cart.js +1 -1
  4. package/dist/Classes/Cart.js.map +1 -1
  5. package/dist/Classes/Cart.mjs +1 -1
  6. package/dist/Classes/Cart.mjs.map +1 -1
  7. package/dist/Classes/Charge.d.mts +8 -3
  8. package/dist/Classes/Charge.d.ts +8 -3
  9. package/dist/Classes/Charge.js +1 -1
  10. package/dist/Classes/Charge.js.map +1 -1
  11. package/dist/Classes/Charge.mjs +1 -1
  12. package/dist/Classes/Coupon.js.map +1 -1
  13. package/dist/Classes/Coupon.mjs +1 -1
  14. package/dist/Classes/Inventory.js.map +1 -1
  15. package/dist/Classes/Inventory.mjs +1 -1
  16. package/dist/Classes/LineItem.js +1 -1
  17. package/dist/Classes/LineItem.js.map +1 -1
  18. package/dist/Classes/LineItem.mjs +1 -1
  19. package/dist/Classes/Order.js +1 -1
  20. package/dist/Classes/Order.js.map +1 -1
  21. package/dist/Classes/Order.mjs +1 -1
  22. package/dist/Classes/Price.js.map +1 -1
  23. package/dist/Classes/Price.mjs +1 -1
  24. package/dist/Classes/Product.js.map +1 -1
  25. package/dist/Classes/Product.mjs +1 -1
  26. package/dist/Classes/ShoppingContainer.d.mts +1 -7
  27. package/dist/Classes/ShoppingContainer.d.ts +1 -7
  28. package/dist/Classes/ShoppingContainer.js +1 -1
  29. package/dist/Classes/ShoppingContainer.js.map +1 -1
  30. package/dist/Classes/ShoppingContainer.mjs +1 -1
  31. package/dist/Classes/TaxRule.d.mts +46 -58
  32. package/dist/Classes/TaxRule.d.ts +46 -58
  33. package/dist/Classes/TaxRule.js +1 -1
  34. package/dist/Classes/TaxRule.js.map +1 -1
  35. package/dist/Classes/TaxRule.mjs +1 -1
  36. package/dist/Classes/TieredPrice.js.map +1 -1
  37. package/dist/Classes/TieredPrice.mjs +1 -1
  38. package/dist/{chunk-RSMXGZD4.mjs → chunk-2ZQIRKRB.mjs} +2 -2
  39. package/dist/{chunk-RSMXGZD4.mjs.map → chunk-2ZQIRKRB.mjs.map} +1 -1
  40. package/dist/{chunk-ZIP2TWP7.mjs → chunk-52Y5EI7O.mjs} +2 -2
  41. package/dist/chunk-52Y5EI7O.mjs.map +1 -0
  42. package/dist/chunk-6VRUVGN3.mjs +2 -0
  43. package/dist/chunk-6VRUVGN3.mjs.map +1 -0
  44. package/dist/chunk-DLLGHKS5.mjs +2 -0
  45. package/dist/chunk-DLLGHKS5.mjs.map +1 -0
  46. package/dist/{chunk-U7DES3CI.mjs → chunk-HR5HHV6O.mjs} +2 -2
  47. package/dist/{chunk-U7DES3CI.mjs.map → chunk-HR5HHV6O.mjs.map} +1 -1
  48. package/dist/{chunk-6X5O25W5.mjs → chunk-XHGUVYQO.mjs} +2 -2
  49. package/dist/{chunk-6X5O25W5.mjs.map → chunk-XHGUVYQO.mjs.map} +1 -1
  50. package/dist/{chunk-JE7BAYI5.mjs → chunk-Y5D6RHTJ.mjs} +2 -2
  51. package/dist/{chunk-JE7BAYI5.mjs.map → chunk-Y5D6RHTJ.mjs.map} +1 -1
  52. package/dist/chunk-YKRWZ24N.mjs +2 -0
  53. package/dist/chunk-YKRWZ24N.mjs.map +1 -0
  54. package/dist/index.js.map +1 -1
  55. package/dist/index.mjs +1 -1
  56. package/package.json +1 -11
  57. package/dist/Classes/TaxCategory.d.mts +0 -42
  58. package/dist/Classes/TaxCategory.d.ts +0 -42
  59. package/dist/Classes/TaxCategory.js +0 -2
  60. package/dist/Classes/TaxCategory.js.map +0 -1
  61. package/dist/Classes/TaxCategory.mjs +0 -2
  62. package/dist/Classes/TaxCategory.mjs.map +0 -1
  63. package/dist/chunk-EKVV7PTI.mjs +0 -2
  64. package/dist/chunk-EKVV7PTI.mjs.map +0 -1
  65. package/dist/chunk-T3C4WXF2.mjs +0 -2
  66. package/dist/chunk-T3C4WXF2.mjs.map +0 -1
  67. package/dist/chunk-Y5OXMUZU.mjs +0 -2
  68. package/dist/chunk-Y5OXMUZU.mjs.map +0 -1
  69. package/dist/chunk-ZIP2TWP7.mjs.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/Classes/Charge.ts"],"names":["ChargeModel","CustomFieldModel","data","PriceModel","r","FixedTaxRuleModel","discountId","discount","taxRuleId","taxBreakdown","taxRules","taxRule","InvalidTaxRuleError","appliedDiscounts","chargeDiscounts","zero","price","totalDiscount","sum","s","grossAmount","totalRate","taxableBase","totalTaxAmount","distributedTax","maxRateRule","prev","current","maxRateRuleId","rate","taxAmount","maxRateTaxAmount","netPrice","grandTotal","Utils_default","k","v"],"mappings":"uMAqEA,IAAqBA,CAArB,CAAA,cAAyCC,CAAiB,CAC5C,GACA,IACA,CAAA,QAAA,CACA,UACA,CAAA,UAAA,CACA,QAKA,KAEV,CAAA,WAAA,CAAYC,CAAwB,CAAA,CAChC,MAAMA,CAAI,CAAA,CACV,IAAK,CAAA,EAAA,CAAKA,EAAK,EACf,CAAA,IAAA,CAAK,IAAOA,CAAAA,CAAAA,CAAK,KACjB,IAAK,CAAA,QAAA,CAAWA,CAAK,CAAA,QAAA,CACrB,KAAK,UAAaA,CAAAA,CAAAA,CAAK,UACvB,CAAA,IAAA,CAAK,WAAaA,CAAK,CAAA,UAAA,CACvB,IAAK,CAAA,OAAA,CAAU,CACX,KAAO,CAAA,IAAIC,GAAWD,CAAAA,CAAAA,CAAK,QAAQ,KAAK,CAAA,CACxC,WAAaA,CAAAA,CAAAA,CAAK,QAAQ,WAC1B,CAAA,iBAAA,CAAmBA,CAAK,CAAA,OAAA,CAAQ,kBAAkB,GAAIE,CAAAA,CAAAA,EAAK,IAAIC,CAAAA,CAAkBD,CAAC,CAAC,CACvF,CAEA,CAAA,IAAA,CAAK,MAAQ,CACT,KAAA,CAAO,IAAID,GAAAA,CAAWD,EAAK,KAAM,CAAA,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WAAA,CAAY,OAAO,OAAQ,CAAA,IAAA,CAAK,KAAM,CAAA,YAAY,EAAE,GAAI,CAAA,CAAC,CAACD,CAAAA,CAAGC,CAAC,CAAM,GAAA,CAACD,CAAG,CAAA,CACzF,KAAMC,CAAE,CAAA,IAAA,CACR,YAAcA,CAAAA,CAAAA,CAAE,aAAa,UAAW,EAAA,CACxC,MAAQA,CAAAA,CAAAA,CAAE,OAAO,UAAW,EAAA,CAC5B,MAAQA,CAAAA,CAAAA,CAAE,OACV,SAAWA,CAAAA,CAAAA,CAAE,SACjB,CAAC,CAAC,CAAC,CAAA,CACH,QAAU,CAAA,IAAA,CAAK,MAAM,QAAS,CAAA,UAAA,EAC9B,CAAA,UAAA,CAAY,KAAK,KAAM,CAAA,UAAA,CAAW,UAAW,EACjD,EACA,YAAc,CAAA,IAAA,CAAK,kBAAmB,EAC1C,CAEJ,CACJ","file":"chunk-YKRWZ24N.mjs","sourcesContent":["import BaseModel, { BaseAttributes, BaseData, CustomFieldAttributes, CustomFieldModel } from \"./Base\";\r\nimport Utils from \"../Utils\";\r\nimport { LocalizedString } from \"./Common\";\r\nimport { ChargeType, TaxSystem } from \"./Enum\";\r\nimport { InvalidTaxRuleError } from \"./Error\";\r\nimport PriceModel, { PriceData } from \"./Price\";\r\nimport { FixedTaxRuleData, FixedTaxRuleModel, TaxRuleData, TaxRuleModel } from \"./TaxRule\";\r\nimport CouponModel from \"./Coupon\";\r\n\r\nexport type ChargePricing = {\r\n price: PriceData;\r\n taxCategory: string;\r\n applicableTaxRule: FixedTaxRuleData[];\r\n};\r\n\r\nexport type ChargeTaxBreakdown = {\r\n rate: number;\r\n taxablePrice: PriceData;\r\n amount: PriceData;\r\n system: TaxSystem;\r\n subSystem: string;\r\n};\r\n\r\nexport type ChargeTaxBreakdownModel = {\r\n rate: number;\r\n taxablePrice: PriceModel;\r\n amount: PriceModel;\r\n system: TaxSystem;\r\n subSystem: string;\r\n};\r\n\r\nexport type ChargeTotals = {\r\n price: PriceData;\r\n discounts: Record<string, PriceData>;\r\n totalDiscount: PriceData;\r\n netPrice: PriceData;\r\n taxBreakdown: Record<string, ChargeTaxBreakdown>;\r\n taxTotal: PriceData;\r\n grandTotal: PriceData;\r\n};\r\n\r\nexport type ChargeTotalsModel = {\r\n price: PriceModel;\r\n discounts: Record<string, PriceModel>;\r\n totalDiscount: PriceModel;\r\n netPrice: PriceModel;\r\n taxBreakdown: Record<string, ChargeTaxBreakdownModel>;\r\n taxTotal: PriceModel;\r\n grandTotal: PriceModel;\r\n};\r\n\r\nexport type ChargeAttributes = CustomFieldAttributes & {\r\n id: string;\r\n name: LocalizedString;\r\n category: string;\r\n lineItemId?: string;\r\n chargeType: ChargeType;\r\n pricing: ChargePricing;\r\n total: ChargeTotals;\r\n};\r\n\r\nexport type ChargeData = Omit<Required<ChargeAttributes>, 'lineItemId'> & {\r\n lineItemId?: string;\r\n};\r\n\r\n/**\r\n * Represents an additional charge (or discount if amount is negative) in a shopping container.\r\n * Supports its own tax rules.\r\n */\r\nexport default class ChargeModel extends CustomFieldModel {\r\n protected id: string;\r\n protected name: LocalizedString;\r\n protected category: string;\r\n protected lineItemId?: string;\r\n protected chargeType: ChargeType;\r\n protected pricing: {\r\n price: PriceModel;\r\n taxCategory: string;\r\n applicableTaxRule: FixedTaxRuleModel[];\r\n };\r\n protected total: ChargeTotalsModel;\r\n\r\n constructor(data: ChargeAttributes) {\r\n super(data);\r\n this.id = data.id;\r\n this.name = data.name;\r\n this.category = data.category;\r\n this.lineItemId = data.lineItemId;\r\n this.chargeType = data.chargeType;\r\n this.pricing = {\r\n price: new PriceModel(data.pricing.price),\r\n taxCategory: data.pricing.taxCategory,\r\n applicableTaxRule: data.pricing.applicableTaxRule.map(r => new FixedTaxRuleModel(r))\r\n };\r\n\r\n this.total = {\r\n price: new PriceModel(data.total.price),\r\n discounts: Object.fromEntries(\r\n Object.entries(data.total.discounts).map(([discountId, discount]) => [\r\n discountId,\r\n new PriceModel(discount)\r\n ])\r\n ),\r\n totalDiscount: new PriceModel(data.total.totalDiscount),\r\n netPrice: new PriceModel(data.total.netPrice),\r\n taxBreakdown: Object.fromEntries(\r\n Object.entries(data.total.taxBreakdown).map(([taxRuleId, taxBreakdown]) => [\r\n taxRuleId,\r\n {\r\n rate: taxBreakdown.rate,\r\n taxablePrice: new PriceModel(taxBreakdown.taxablePrice),\r\n amount: new PriceModel(taxBreakdown.amount),\r\n system: taxBreakdown.system,\r\n subSystem: taxBreakdown.subSystem\r\n }\r\n ])\r\n ),\r\n taxTotal: new PriceModel(data.total.taxTotal),\r\n grandTotal: new PriceModel(data.total.grandTotal),\r\n };\r\n }\r\n\r\n public getId(): string { return this.id; }\r\n public getName(): LocalizedString { return this.name; }\r\n public getCategory(): string { return this.category; }\r\n public getChargeType(): ChargeType { return this.chargeType; }\r\n public getLineItemId(): string | undefined { return this.lineItemId; }\r\n public getPricing() { return this.pricing; }\r\n public getTotal(): ChargeTotalsModel { return this.total; }\r\n\r\n public getApplicableTaxRules(): FixedTaxRuleModel[] {\r\n return this.pricing.applicableTaxRule;\r\n }\r\n\r\n /**\r\n * Updates the tax rule for this line item and recalculates totals.\r\n * @param taxRule - The new tax rule to apply.\r\n * @throws {Error} If the tax rule category does not match the item's tax category.\r\n */\r\n public updateTax(taxRules: FixedTaxRuleModel[]): void {\r\n taxRules.forEach(taxRule => {\r\n if (!taxRule.appliesTo(this.pricing.taxCategory, taxRule.getTaxCountry())) {\r\n throw new InvalidTaxRuleError();\r\n }\r\n });\r\n\r\n // Remove the old taxableUnitPrice calculation - no longer needed\r\n this.pricing.applicableTaxRule = taxRules;\r\n this.calculateTotals();\r\n }\r\n\r\n /**\r\n * Updates the discounts applied to this line item and recalculates totals.\r\n * @param appliedDiscounts - List of coupons and their allocated discount amounts.\r\n */\r\n public updateDiscounts(appliedDiscounts: { coupon: CouponModel, amount: PriceModel }[]): void {\r\n let chargeDiscounts = {} as Record<string, PriceModel>;\r\n appliedDiscounts.forEach(discount => {\r\n chargeDiscounts[discount.coupon.getCode()] = discount.amount;\r\n });\r\n\r\n this.total.discounts = chargeDiscounts;\r\n this.calculateTotals();\r\n }\r\n\r\n public calculateTotals(): void {\r\n const zero = this.pricing.price.zero();\r\n const price = this.pricing.price;\r\n\r\n const totalDiscount = Object.values(this.total.discounts).reduce((sum, s) => sum.add(s), zero);\r\n const grossAmount = price.subtract(totalDiscount);\r\n\r\n const taxBreakdown: Record<string, ChargeTaxBreakdownModel> = {};\r\n\r\n // 1. Calculate total rate for inclusive back-calculation\r\n const totalRate = this.pricing.applicableTaxRule.reduce((sum, r) => sum + r.getApplicableTaxRate(), 0);\r\n\r\n // 2. Calculate BASE and TOTAL TAX using balancing logic\r\n // We round the base first, then define tax as the residual (Gross - Base)\r\n // This guarantees Base + Tax === GrandTotal (inclusive price)\r\n const taxableBase = grossAmount.divide(1 + totalRate).round();\r\n const totalTaxAmount = totalRate > 0 ? grossAmount.subtract(taxableBase) : zero;\r\n\r\n // 3. Distribute total tax among rules\r\n // We find the rule with the largest rate to absorb the rounding residual\r\n let distributedTax = zero;\r\n const maxRateRule = this.pricing.applicableTaxRule.reduce((prev, current) =>\r\n (current.getApplicableTaxRate() > prev.getApplicableTaxRate()) ? current : prev\r\n );\r\n const maxRateRuleId = maxRateRule.getTaxRuleId();\r\n\r\n // Calculate all other rules first\r\n this.pricing.applicableTaxRule.forEach((taxRule) => {\r\n if (taxRule.getTaxRuleId() === maxRateRuleId) return;\r\n\r\n const rate = taxRule.getApplicableTaxRate();\r\n const taxAmount = totalRate > 0 ? totalTaxAmount.multiply(rate).divide(totalRate).round() : zero;\r\n\r\n taxBreakdown[taxRule.getTaxRuleId()] = {\r\n rate: rate,\r\n taxablePrice: taxableBase,\r\n amount: taxAmount,\r\n system: taxRule.getTaxSystem(),\r\n subSystem: taxRule.getTaxSubSystem()\r\n };\r\n distributedTax = distributedTax.add(taxAmount);\r\n });\r\n\r\n // Assign residual to the largest rate rule\r\n const maxRateTaxAmount = totalTaxAmount.subtract(distributedTax);\r\n taxBreakdown[maxRateRuleId] = {\r\n rate: maxRateRule.getApplicableTaxRate(),\r\n taxablePrice: taxableBase,\r\n amount: maxRateTaxAmount,\r\n system: maxRateRule.getTaxSystem(),\r\n subSystem: maxRateRule.getTaxSubSystem()\r\n };\r\n\r\n const netPrice = taxableBase;\r\n const grandTotal = grossAmount;\r\n\r\n this.total = {\r\n price: price,\r\n discounts: this.total.discounts,\r\n netPrice: netPrice,\r\n totalDiscount: totalDiscount,\r\n taxBreakdown: taxBreakdown,\r\n taxTotal: totalTaxAmount,\r\n grandTotal: grandTotal\r\n };\r\n }\r\n\r\n public getDetails(): ChargeData {\r\n const details: ChargeData = {\r\n id: this.id,\r\n name: Utils.deepClone(this.name),\r\n category: this.category,\r\n lineItemId: this.lineItemId,\r\n chargeType: this.chargeType,\r\n pricing: {\r\n price: this.pricing.price.getDetails(),\r\n taxCategory: this.pricing.taxCategory,\r\n applicableTaxRule: this.pricing.applicableTaxRule.map(r => r.getDetails())\r\n },\r\n total: {\r\n price: this.total.price.getDetails(),\r\n discounts: Object.fromEntries(Object.entries(this.total.discounts).map(([k, v]) => [k, v.getDetails()])),\r\n totalDiscount: this.total.totalDiscount.getDetails(),\r\n netPrice: this.total.netPrice.getDetails(),\r\n taxBreakdown: Object.fromEntries(Object.entries(this.total.taxBreakdown).map(([k, v]) => [k, {\r\n rate: v.rate,\r\n taxablePrice: v.taxablePrice.getDetails(),\r\n amount: v.amount.getDetails(),\r\n system: v.system,\r\n subSystem: v.subSystem\r\n }])),\r\n taxTotal: this.total.taxTotal.getDetails(),\r\n grandTotal: this.total.grandTotal.getDetails()\r\n },\r\n customFields: this.getAllCustomFields()\r\n };\r\n return details;\r\n }\r\n}\r\n"]}