@ubnt/store.core 11.28.0-custom-tax.1 → 11.28.0

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 (219) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/lib/_virtual/_virtual_multi-entry.js +6 -2
  3. package/lib/_virtual/_virtual_multi-entry.js.map +1 -1
  4. package/lib/_virtual/_virtual_multi-entry.mjs +6 -2
  5. package/lib/_virtual/_virtual_multi-entry.mjs.map +1 -1
  6. package/lib/assets/ai-360.jpg +0 -0
  7. package/lib/assets/ai-3602.jpg +0 -0
  8. package/lib/assets/ai-dslr.jpg +0 -0
  9. package/lib/assets/ai-dslr2.jpg +0 -0
  10. package/lib/assets/attention.svg +1 -1
  11. package/lib/assets/attention2.svg +1 -1
  12. package/lib/assets/compact.svg +1 -1
  13. package/lib/assets/compact2.svg +1 -1
  14. package/lib/assets/compact3.svg +1 -0
  15. package/lib/assets/door-hub-mini.jpg +0 -0
  16. package/lib/assets/door-hub-mini2.jpg +0 -0
  17. package/lib/assets/floodlight.svg +1 -1
  18. package/lib/assets/floodlight2.svg +1 -1
  19. package/lib/assets/g2-reader.jpg +0 -0
  20. package/lib/assets/g2-reader2.jpg +0 -0
  21. package/lib/assets/g3-reader.jpg +0 -0
  22. package/lib/assets/g3-reader2.jpg +0 -0
  23. package/lib/assets/g3-starter-kit-pro.jpg +0 -0
  24. package/lib/assets/g3-starter-kit-pro2.jpg +0 -0
  25. package/lib/assets/g3-starter-kit.jpg +0 -0
  26. package/lib/assets/g3-starter-kit2.jpg +0 -0
  27. package/lib/assets/g4-ptz.jpg +0 -0
  28. package/lib/assets/g4-ptz2.jpg +0 -0
  29. package/lib/assets/g5-dome.jpg +0 -0
  30. package/lib/assets/g5-dome2.jpg +0 -0
  31. package/lib/assets/g5-ptz.jpg +0 -0
  32. package/lib/assets/g5-ptz2.jpg +0 -0
  33. package/lib/assets/globe.svg +1 -1
  34. package/lib/assets/globe2.svg +1 -1
  35. package/lib/assets/interior-design.svg +1 -1
  36. package/lib/assets/interior-design2.svg +1 -1
  37. package/lib/assets/power.svg +1 -1
  38. package/lib/assets/power2.svg +1 -1
  39. package/lib/assets/rack.svg +1 -1
  40. package/lib/assets/rack2.svg +1 -1
  41. package/lib/components/Header/Header.js +1 -1
  42. package/lib/components/Header/Header.mjs +1 -1
  43. package/lib/components/Header/ProHeader/ProHeader.js +1 -1
  44. package/lib/components/Header/ProHeader/ProHeader.mjs +1 -1
  45. package/lib/components/MicrositeHeader/components/HeaderContent/HeaderContent.js +1 -1
  46. package/lib/components/MicrositeHeader/components/HeaderContent/HeaderContent.mjs +1 -1
  47. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.js +1 -1
  48. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.mjs +1 -1
  49. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/attention.svg.js +1 -1
  50. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/attention.svg.mjs +2 -2
  51. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/ProgressBarChart.js +1 -1
  52. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/ProgressBarChart.mjs +1 -1
  53. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/assets/attention.svg.js +1 -1
  54. package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/assets/attention.svg.mjs +2 -2
  55. package/lib/constants/icons/header/globe.svg.js +1 -1
  56. package/lib/constants/icons/header/globe.svg.mjs +2 -2
  57. package/lib/constants/icons/header/index.js +1 -1
  58. package/lib/constants/icons/header/index.mjs +1 -1
  59. package/lib/constants/icons/menu/floodlight.svg.js +5 -5
  60. package/lib/constants/icons/menu/floodlight.svg.mjs +2 -2
  61. package/lib/constants/icons/menu/index.js +9 -9
  62. package/lib/constants/icons/menu/index.mjs +3 -3
  63. package/lib/constants/icons/menu/power.svg.js +5 -5
  64. package/lib/constants/icons/menu/power.svg.mjs +2 -2
  65. package/lib/constants/icons/menu/rack.svg.js +5 -5
  66. package/lib/constants/icons/menu/rack.svg.mjs +2 -2
  67. package/lib/constants/modelCompareData/assets/Bullet/ai-dslr.jpg.js +1 -1
  68. package/lib/constants/modelCompareData/assets/Bullet/ai-dslr.jpg.mjs +2 -2
  69. package/lib/constants/modelCompareData/assets/DomeTurret/ai-360.jpg.js +1 -1
  70. package/lib/constants/modelCompareData/assets/DomeTurret/ai-360.jpg.mjs +2 -2
  71. package/lib/constants/modelCompareData/assets/DomeTurret/g5-dome.jpg.js +1 -1
  72. package/lib/constants/modelCompareData/assets/DomeTurret/g5-dome.jpg.mjs +2 -2
  73. package/lib/constants/modelCompareData/assets/DoorAccess/door-hub-mini.jpg.js +1 -1
  74. package/lib/constants/modelCompareData/assets/DoorAccess/door-hub-mini.jpg.mjs +2 -2
  75. package/lib/constants/modelCompareData/assets/DoorAccess/g2-reader.jpg.js +1 -1
  76. package/lib/constants/modelCompareData/assets/DoorAccess/g2-reader.jpg.mjs +2 -2
  77. package/lib/constants/modelCompareData/assets/DoorAccess/g3-reader.jpg.js +1 -1
  78. package/lib/constants/modelCompareData/assets/DoorAccess/g3-reader.jpg.mjs +2 -2
  79. package/lib/constants/modelCompareData/assets/DoorAccess/g3-starter-kit-pro.jpg.js +1 -1
  80. package/lib/constants/modelCompareData/assets/DoorAccess/g3-starter-kit-pro.jpg.mjs +2 -2
  81. package/lib/constants/modelCompareData/assets/DoorAccess/g3-starter-kit.jpg.js +1 -1
  82. package/lib/constants/modelCompareData/assets/DoorAccess/g3-starter-kit.jpg.mjs +2 -2
  83. package/lib/constants/modelCompareData/assets/PTZ/g4-ptz.jpg.js +1 -1
  84. package/lib/constants/modelCompareData/assets/PTZ/g4-ptz.jpg.mjs +2 -2
  85. package/lib/constants/modelCompareData/assets/PTZ/g5-ptz.jpg.js +1 -1
  86. package/lib/constants/modelCompareData/assets/PTZ/g5-ptz.jpg.mjs +2 -2
  87. package/lib/constants/modelCompareData/assets/compact.svg.js +1 -1
  88. package/lib/constants/modelCompareData/assets/compact.svg.mjs +2 -2
  89. package/lib/constants/modelCompareData/assets/floodlight.svg.js +1 -1
  90. package/lib/constants/modelCompareData/assets/floodlight.svg.mjs +2 -2
  91. package/lib/constants/modelCompareData/assets/globe.svg.js +1 -1
  92. package/lib/constants/modelCompareData/assets/globe.svg.mjs +2 -2
  93. package/lib/constants/modelCompareData/assets/interior-design.svg.js +1 -1
  94. package/lib/constants/modelCompareData/assets/interior-design.svg.mjs +2 -2
  95. package/lib/constants/modelCompareData/assets/power.svg.js +1 -1
  96. package/lib/constants/modelCompareData/assets/power.svg.mjs +2 -2
  97. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/bullet/ai-dslr.jpg.js +1 -1
  98. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/bullet/ai-dslr.jpg.mjs +2 -2
  99. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-360.jpg.js +1 -1
  100. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-360.jpg.mjs +2 -2
  101. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/g5-dome.jpg.js +1 -1
  102. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/g5-dome.jpg.mjs +2 -2
  103. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/door-hub-mini.jpg.js +1 -1
  104. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/door-hub-mini.jpg.mjs +2 -2
  105. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g2-reader.jpg.js +1 -1
  106. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g2-reader.jpg.mjs +2 -2
  107. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-reader.jpg.js +1 -1
  108. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-reader.jpg.mjs +2 -2
  109. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-starter-kit-pro.jpg.js +1 -1
  110. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-starter-kit-pro.jpg.mjs +2 -2
  111. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-starter-kit.jpg.js +1 -1
  112. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-starter-kit.jpg.mjs +2 -2
  113. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/ptz/g4-ptz.jpg.js +1 -1
  114. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/ptz/g4-ptz.jpg.mjs +2 -2
  115. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/ptz/g5-ptz.jpg.js +1 -1
  116. package/lib/constants/modelCompareData/assets/product-list-assets/cameras/ptz/g5-ptz.jpg.mjs +2 -2
  117. package/lib/constants/modelCompareData/assets/product-list-assets/compact.svg.js +1 -1
  118. package/lib/constants/modelCompareData/assets/product-list-assets/compact.svg.mjs +2 -2
  119. package/lib/constants/modelCompareData/assets/product-list-assets/compact2.svg.js +1 -1
  120. package/lib/constants/modelCompareData/assets/product-list-assets/compact2.svg.mjs +2 -2
  121. package/lib/constants/modelCompareData/assets/product-list-assets/interior-design.svg.js +1 -1
  122. package/lib/constants/modelCompareData/assets/product-list-assets/interior-design.svg.mjs +2 -2
  123. package/lib/constants/modelCompareData/assets/rack.svg.js +1 -1
  124. package/lib/constants/modelCompareData/assets/rack.svg.mjs +2 -2
  125. package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/Compact.js +1 -1
  126. package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/Compact.mjs +1 -1
  127. package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/InteriorDesign.js +1 -1
  128. package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/InteriorDesign.mjs +1 -1
  129. package/lib/constants/modelCompareData/data/product-list-groups/compact.js +1 -1
  130. package/lib/constants/modelCompareData/data/product-list-groups/compact.mjs +1 -1
  131. package/lib/constants/modelCompareData/data/product-list-groups/interiorDesign.js +1 -1
  132. package/lib/constants/modelCompareData/data/product-list-groups/interiorDesign.mjs +1 -1
  133. package/lib/constants/modelCompareData/data/product-list-groups/productListSwitchingCompact.js +1 -1
  134. package/lib/constants/modelCompareData/data/product-list-groups/productListSwitchingCompact.mjs +1 -1
  135. package/lib/constants/modelCompareData/data/products/ai360.js +2 -2
  136. package/lib/constants/modelCompareData/data/products/ai360.mjs +2 -2
  137. package/lib/constants/modelCompareData/data/products/aiDslr.js +2 -2
  138. package/lib/constants/modelCompareData/data/products/aiDslr.mjs +2 -2
  139. package/lib/constants/modelCompareData/data/products/doorHubMini.js +2 -2
  140. package/lib/constants/modelCompareData/data/products/doorHubMini.mjs +2 -2
  141. package/lib/constants/modelCompareData/data/products/g2Reader.js +2 -2
  142. package/lib/constants/modelCompareData/data/products/g2Reader.mjs +2 -2
  143. package/lib/constants/modelCompareData/data/products/g3Reader.js +2 -2
  144. package/lib/constants/modelCompareData/data/products/g3Reader.mjs +2 -2
  145. package/lib/constants/modelCompareData/data/products/g3StarterKit.js +2 -2
  146. package/lib/constants/modelCompareData/data/products/g3StarterKit.mjs +2 -2
  147. package/lib/constants/modelCompareData/data/products/g3StarterKitPro.js +2 -2
  148. package/lib/constants/modelCompareData/data/products/g3StarterKitPro.mjs +2 -2
  149. package/lib/constants/modelCompareData/data/products/g4Ptz.js +2 -2
  150. package/lib/constants/modelCompareData/data/products/g4Ptz.mjs +2 -2
  151. package/lib/constants/modelCompareData/data/products/g5Dome.js +2 -2
  152. package/lib/constants/modelCompareData/data/products/g5Dome.mjs +2 -2
  153. package/lib/constants/modelCompareData/data/products/g5Ptz.js +2 -2
  154. package/lib/constants/modelCompareData/data/products/g5Ptz.mjs +2 -2
  155. package/lib/constants/modelCompareData/data/properties/accessPowerMethod.js +1 -1
  156. package/lib/constants/modelCompareData/data/properties/accessPowerMethod.mjs +1 -1
  157. package/lib/constants/modelCompareData/data/properties/availability.js +1 -1
  158. package/lib/constants/modelCompareData/data/properties/availability.mjs +1 -1
  159. package/lib/constants/modelCompareData/data/properties/builtInFloodlight.js +1 -1
  160. package/lib/constants/modelCompareData/data/properties/builtInFloodlight.mjs +1 -1
  161. package/lib/constants/modelCompareData/data/properties/dcPowerOutput.js +1 -1
  162. package/lib/constants/modelCompareData/data/properties/dcPowerOutput.mjs +1 -1
  163. package/lib/constants/modelCompareData/data/properties/mounting.js +1 -1
  164. package/lib/constants/modelCompareData/data/properties/mounting.mjs +1 -1
  165. package/lib/constants/modelCompareData/data/properties/unasPowerMethod.js +1 -1
  166. package/lib/constants/modelCompareData/data/properties/unasPowerMethod.mjs +1 -1
  167. package/lib/constants/modelCompareData/data/properties/webMode.js +1 -1
  168. package/lib/constants/modelCompareData/data/properties/webMode.mjs +1 -1
  169. package/lib/contexts/MenuContext/mapping.js +3 -3
  170. package/lib/contexts/MenuContext/mapping.mjs +2 -2
  171. package/lib/contexts/MicrositeMenuContext/mapping.js +3 -3
  172. package/lib/contexts/MicrositeMenuContext/mapping.mjs +2 -2
  173. package/lib/utils/{map.js → KeyMap.js} +1 -1
  174. package/lib/utils/KeyMap.js.map +1 -0
  175. package/lib/utils/{map.mjs → KeyMap.mjs} +1 -1
  176. package/lib/utils/KeyMap.mjs.map +1 -0
  177. package/lib/utils/index.d.ts +3 -1
  178. package/lib/utils/index.js +6 -2
  179. package/lib/utils/index.js.map +1 -1
  180. package/lib/utils/index.mjs +6 -2
  181. package/lib/utils/index.mjs.map +1 -1
  182. package/lib/utils/makeMap.d.ts +44 -0
  183. package/lib/utils/makeMap.js +28 -0
  184. package/lib/utils/makeMap.js.map +1 -0
  185. package/lib/utils/makeMap.mjs +25 -0
  186. package/lib/utils/makeMap.mjs.map +1 -0
  187. package/lib/utils/makeSet.d.ts +14 -0
  188. package/lib/utils/makeSet.js +8 -0
  189. package/lib/utils/makeSet.js.map +1 -0
  190. package/lib/utils/makeSet.mjs +7 -0
  191. package/lib/utils/makeSet.mjs.map +1 -0
  192. package/lib/utils/makeSet.test.d.ts +1 -0
  193. package/lib/utils/money/index.d.ts +0 -1
  194. package/lib/utils/money/index.js +0 -2
  195. package/lib/utils/money/index.js.map +1 -1
  196. package/lib/utils/money/index.mjs +0 -1
  197. package/lib/utils/money/index.mjs.map +1 -1
  198. package/lib/utils/money/tax.d.ts +3 -6
  199. package/lib/utils/money/tax.js +10 -3
  200. package/lib/utils/money/tax.js.map +1 -1
  201. package/lib/utils/money/tax.mjs +13 -6
  202. package/lib/utils/money/tax.mjs.map +1 -1
  203. package/lib/utils/money/tax.private.d.ts +2 -7
  204. package/lib/utils/money/tax.private.js +6 -21
  205. package/lib/utils/money/tax.private.js.map +1 -1
  206. package/lib/utils/money/tax.private.mjs +8 -22
  207. package/lib/utils/money/tax.private.mjs.map +1 -1
  208. package/package.json +1 -1
  209. package/lib/assets/compact22.svg +0 -1
  210. package/lib/utils/map.js.map +0 -1
  211. package/lib/utils/map.mjs.map +0 -1
  212. package/lib/utils/money/allocators/totalAmount.d.ts +0 -17
  213. package/lib/utils/money/allocators/totalAmount.js +0 -40
  214. package/lib/utils/money/allocators/totalAmount.js.map +0 -1
  215. package/lib/utils/money/allocators/totalAmount.mjs +0 -38
  216. package/lib/utils/money/allocators/totalAmount.mjs.map +0 -1
  217. /package/lib/utils/{map.d.ts → KeyMap.d.ts} +0 -0
  218. /package/lib/utils/{map.test.d.ts → KeyMap.test.d.ts} +0 -0
  219. /package/lib/utils/{money/allocators/totalAmount.test.d.ts → makeMap.test.d.ts} +0 -0
@@ -1,17 +1,24 @@
1
- import { subtract, multiply, toUnit, halfEven, dinero } from 'dinero.js';
1
+ import { toSnapshot, dinero, multiply, toUnit, halfEven } from 'dinero.js';
2
2
  import { assert } from '../errors.mjs';
3
3
  import { isFloat, toFixedRate } from '../number.mjs';
4
- import { storesWithPerItemTaxRate, TAX_RATE_DINERO_CURRENCY } from './constants.mjs';
5
- import { calculatePerQtyLineItemTax, calculateWholeQtyLineItemTax, getTaxFromFinalAmount } from './tax.private.mjs';
4
+ import { storesWithPerItemTaxRate, Round, TAX_RATE_DINERO_CURRENCY } from './constants.mjs';
5
+ import { calculatePerQtyLineItemTax, calculateWholeQtyLineItemTax } from './tax.private.mjs';
6
6
  "use strict";
7
7
  const calculateLineItemTax = ({
8
8
  itemSummary,
9
9
  taxRate,
10
- options
10
+ storeId
11
11
  }) => {
12
- return storesWithPerItemTaxRate.has(options.storeId) ? calculatePerQtyLineItemTax(itemSummary, taxRate, options) : calculateWholeQtyLineItemTax(itemSummary, taxRate, options);
12
+ return storesWithPerItemTaxRate.has(storeId) ? calculatePerQtyLineItemTax(itemSummary, taxRate) : calculateWholeQtyLineItemTax(itemSummary, taxRate);
13
+ };
14
+ const removeTaxAmount = (baseAmountWithTaxObj, taxRate) => {
15
+ const snapshot = toSnapshot(baseAmountWithTaxObj);
16
+ const taxAmount = Round.Financial(snapshot.amount - snapshot.amount / (1 + taxRate));
17
+ return dinero({
18
+ ...snapshot,
19
+ amount: snapshot.amount - taxAmount
20
+ });
13
21
  };
14
- const removeTaxAmount = (amountWithTax, taxRate) => subtract(amountWithTax, getTaxFromFinalAmount(amountWithTax, taxRate));
15
22
  const convertDecimalTaxRateToPercentage = taxRate => {
16
23
  const dineroObj = decimalTaxRateToDinero(taxRate);
17
24
  const t = multiply(dineroObj, 100);
@@ -1 +1 @@
1
- {"version":3,"file":"tax.mjs","sources":["../../../src/utils/money/tax.ts"],"sourcesContent":["import {\n dinero,\n // FIXME: move usage to rounding\n halfEven,\n multiply,\n subtract,\n toUnit,\n} from \"dinero.js\";\n\nimport type { StoreId } from \"../../types\";\nimport { assert } from \"../errors\";\nimport { isFloat, toFixedRate } from \"../number\";\nimport {\n storesWithPerItemTaxRate,\n TAX_RATE_DINERO_CURRENCY,\n} from \"./constants\";\nimport {\n calculatePerQtyLineItemTax,\n calculateWholeQtyLineItemTax,\n getTaxFromFinalAmount,\n} from \"./tax.private\";\nimport type { Dino, LineItemSummary, TaxRate } from \"./types\";\n\n/**\n * Function to calculate the tax amount for a given line item depending on the store's tax rate calculation strategy\n */\nexport const calculateLineItemTax = ({\n itemSummary,\n taxRate,\n options,\n}: {\n itemSummary: LineItemSummary;\n taxRate: TaxRate;\n options: { storeId: StoreId; hasTaxIncludedPrice: boolean };\n}): Dino => {\n return storesWithPerItemTaxRate.has(options.storeId)\n ? calculatePerQtyLineItemTax(itemSummary, taxRate, options)\n : calculateWholeQtyLineItemTax(itemSummary, taxRate, options);\n};\n\nexport const removeTaxAmount = (amountWithTax: Dino, taxRate: TaxRate): Dino =>\n subtract(amountWithTax, getTaxFromFinalAmount(amountWithTax, taxRate));\n\n/**\n * Will return a percentage tax rate from a decimal tax rate (usually returned by DB)\n *\n * Use for math-related calculations ONLY! Use `formatDecimalTaxRate` for display purposes e.g. invoices.\n *\n * @example convertDecimalTaxRateToPercentage(\"0.01\") // 1\n * @example convertDecimalTaxRateToPercentage(\"0.1023\") // 10.23\n * @example convertDecimalTaxRateToPercentage(\"0.12345678\") // 12.345678\n */\nexport const convertDecimalTaxRateToPercentage = (\n taxRate: string | number,\n): number => {\n const dineroObj = decimalTaxRateToDinero(taxRate);\n const t = multiply(dineroObj, 100);\n return toUnit(t, {\n digits: TAX_RATE_DINERO_CURRENCY.exponent,\n round: halfEven,\n });\n};\n\nexport const decimalTaxRateToDinero = (taxRate: string | number): Dino => {\n const parsedNumber =\n typeof taxRate === \"number\" ? taxRate : Number.parseFloat(taxRate);\n isFloat(parsedNumber);\n\n const parsedNumberInt = Number.parseInt(\n (\n parsedNumber *\n Math.pow(TAX_RATE_DINERO_CURRENCY.base, TAX_RATE_DINERO_CURRENCY.exponent)\n ).toFixed(0),\n );\n return dinero({\n amount: parsedNumberInt,\n currency: TAX_RATE_DINERO_CURRENCY,\n });\n};\n\n/**\n * Will return a percentage string from a decimal tax rate (usually returned by DB).\n *\n * Use for display purposes e.g. invoices.\n *\n * @example formatDecimalTaxRate(\"0.01\") // \"1%\"\n * @example formatDecimalTaxRate(\"0.1023\") // \"10.23%\"\n * @example formatDecimalTaxRate(\"0.12345678\") // \"12.345678%\"\n */\nexport const formatDecimalTaxRate = <TaxRateFloat extends string | number>(\n taxRate: TaxRateFloat,\n): `${number}%` => {\n return `${convertDecimalTaxRateToPercentage(taxRate)}%`;\n};\n\nexport const stringTaxRateToFloat = (rate: string): number =>\n toFixedRate(+rate, TAX_RATE_DINERO_CURRENCY.exponent, true);\n\n/**\n * Calculate total Tax(LV: \"PVN\") for country, which consists of multiple tax entries\n * Returning fixed to ${TAX_RATE_DINERO_CURRENCY.exponent} 4 decimal places, to for example 0.2100 represent 21.00%\n */\nexport const calculateTotalTaxRate = (rates: number[]): number => {\n const rateSum = rates.reduce((sum, rate) => sum + rate, 0);\n const result = toFixedRate(rateSum, TAX_RATE_DINERO_CURRENCY.exponent, true);\n assert(result >= 0, \"Tax rate must be positive\");\n assert(result < 1, \"Tax rate must be less than 1\");\n return result;\n};\n"],"names":["calculateLineItemTax","itemSummary","taxRate","options","storesWithPerItemTaxRate","has","storeId","calculatePerQtyLineItemTax","calculateWholeQtyLineItemTax","removeTaxAmount","amountWithTax","subtract","getTaxFromFinalAmount","convertDecimalTaxRateToPercentage","dineroObj","decimalTaxRateToDinero","t","multiply","toUnit","digits","TAX_RATE_DINERO_CURRENCY","exponent","round","halfEven","parsedNumber","Number","parseFloat","isFloat","parsedNumberInt","parseInt","Math","pow","base","toFixed","dinero","amount","currency","formatDecimalTaxRate","stringTaxRateToFloat","rate","toFixedRate","calculateTotalTaxRate","rates","rateSum","reduce","sum","result","assert"],"mappings":";;;;;;AA0BO,MAAMA,uBAAuBA,CAAC;EACnCC,WAAA;EACAC,OAAA;EACAC;AACF,CAAA,KAIY;EACV,OAAOC,wBAAA,CAAyBC,GAAA,CAAIF,OAAA,CAAQG,OAAO,CAAA,GAC/CC,0BAAA,CAA2BN,WAAA,E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MAAA,CAAO,CAACC,KAAKN,IAAA,KAASM,GAAA,GAAMN,MAAM,CAAC,CAAA;EACzD,MAAMO,MAAA,GAASN,WAAA,CAAYG,OAAA,EAASvB,wBAAA,CAAyBC,UAAU,IAAI,CAAA;EAC3E0B,MAAA,CAAOD,MAAA,IAAU,GAAG,2BAA2B,CAAA;EAC/CC,MAAA,CAAOD,MAAA,GAAS,GAAG,8BAA8B,CAAA;EACjD,OAAOA,MAAA;AACT,CAAA;"}
1
+ {"version":3,"file":"tax.mjs","sources":["../../../src/utils/money/tax.ts"],"sourcesContent":["import {\n dinero,\n // FIXME: move usage to rounding\n halfEven,\n multiply,\n toSnapshot,\n toUnit,\n} from \"dinero.js\";\n\nimport type { StoreId } from \"../../types\";\nimport { assert } from \"../errors\";\nimport { isFloat, toFixedRate } from \"../number\";\nimport {\n Round,\n storesWithPerItemTaxRate,\n TAX_RATE_DINERO_CURRENCY,\n} from \"./constants\";\nimport {\n calculatePerQtyLineItemTax,\n calculateWholeQtyLineItemTax,\n} from \"./tax.private\";\nimport type { Dino, LineItemSummary, TaxRate } from \"./types\";\n\n/**\n * Function to calculate the tax amount for a given line item depending on the store's tax rate calculation strategy\n */\nexport const calculateLineItemTax = ({\n itemSummary,\n taxRate,\n storeId,\n}: {\n itemSummary: LineItemSummary;\n taxRate: TaxRate;\n storeId: StoreId;\n}) => {\n return storesWithPerItemTaxRate.has(storeId)\n ? calculatePerQtyLineItemTax(itemSummary, taxRate)\n : calculateWholeQtyLineItemTax(itemSummary, taxRate);\n};\n\nexport const removeTaxAmount = (\n baseAmountWithTaxObj: Dino,\n taxRate: TaxRate,\n) => {\n const snapshot = toSnapshot(baseAmountWithTaxObj);\n\n // Calculate the tax amount and apply rounding on it so that behavior for reverse process is consistent\n const taxAmount = Round.Financial(\n snapshot.amount - snapshot.amount / (1 + taxRate),\n );\n\n return dinero({\n ...snapshot,\n amount: snapshot.amount - taxAmount,\n });\n};\n\n/**\n * Will return a percentage tax rate from a decimal tax rate (usually returned by DB)\n *\n * Use for math-related calculations ONLY! Use `formatDecimalTaxRate` for display purposes e.g. invoices.\n *\n * @example convertDecimalTaxRateToPercentage(\"0.01\") // 1\n * @example convertDecimalTaxRateToPercentage(\"0.1023\") // 10.23\n * @example convertDecimalTaxRateToPercentage(\"0.12345678\") // 12.345678\n */\nexport const convertDecimalTaxRateToPercentage = (\n taxRate: string | number,\n): number => {\n const dineroObj = decimalTaxRateToDinero(taxRate);\n const t = multiply(dineroObj, 100);\n return toUnit(t, {\n digits: TAX_RATE_DINERO_CURRENCY.exponent,\n round: halfEven,\n });\n};\n\nexport const decimalTaxRateToDinero = (taxRate: string | number): Dino => {\n const parsedNumber =\n typeof taxRate === \"number\" ? taxRate : Number.parseFloat(taxRate);\n isFloat(parsedNumber);\n\n const parsedNumberInt = Number.parseInt(\n (\n parsedNumber *\n Math.pow(TAX_RATE_DINERO_CURRENCY.base, TAX_RATE_DINERO_CURRENCY.exponent)\n ).toFixed(0),\n );\n return dinero({\n amount: parsedNumberInt,\n currency: TAX_RATE_DINERO_CURRENCY,\n });\n};\n\n/**\n * Will return a percentage string from a decimal tax rate (usually returned by DB).\n *\n * Use for display purposes e.g. invoices.\n *\n * @example formatDecimalTaxRate(\"0.01\") // \"1%\"\n * @example formatDecimalTaxRate(\"0.1023\") // \"10.23%\"\n * @example formatDecimalTaxRate(\"0.12345678\") // \"12.345678%\"\n */\nexport const formatDecimalTaxRate = <TaxRateFloat extends string | number>(\n taxRate: TaxRateFloat,\n): `${number}%` => {\n return `${convertDecimalTaxRateToPercentage(taxRate)}%`;\n};\n\nexport const stringTaxRateToFloat = (rate: string): number =>\n toFixedRate(+rate, TAX_RATE_DINERO_CURRENCY.exponent, true);\n\n/**\n * Calculate total Tax(LV: \"PVN\") for country, which consists of multiple tax entries\n * Returning fixed to ${TAX_RATE_DINERO_CURRENCY.exponent} 4 decimal places, to for example 0.2100 represent 21.00%\n */\nexport const calculateTotalTaxRate = (rates: number[]): number => {\n const rateSum = rates.reduce((sum, rate) => sum + rate, 0);\n const result = toFixedRate(rateSum, TAX_RATE_DINERO_CURRENCY.exponent, true);\n assert(result >= 0, \"Tax rate must be positive\");\n assert(result < 1, \"Tax rate must be less than 1\");\n return result;\n};\n"],"names":["calculateLineItemTax","itemSummary","taxRate","storeId","storesWithPerItemTaxRate","has","calculatePerQtyLineItemTax","calculateWholeQtyLineItemTax","removeTaxAmount","baseAmountWithTaxObj","snapshot","toSnapshot","taxAmount","Round","Financial","amount","dinero","convertDecimalTaxRateToPercentage","dineroObj","decimalTaxRateToDinero","t","multiply","toUnit","digits","TAX_RATE_DINERO_CURRENCY","exponent","round","halfEven","parsedNumber","Number","parseFloat","isFloat","parsedNumberInt","parseInt","Math","pow","base","toFixed","currency","formatDecimalTaxRate","stringTaxRateToFloat","rate","toFixedRate","calculateTotalTaxRate","rates","rateSum","reduce","sum","result","assert"],"mappings":";;;;;;AA0BO,MAAMA,uBAAuBA,CAAC;EACnCC,WAAA;EACAC,OAAA;EACAC;AACF,CAAA,KAIM;EACJ,OAAOC,wBAAA,CAAyBC,GAAA,CAAIF,OAAO,CAAA,GACvCG,0BAAA,CAA2BL,aAAaC,OAAO,CAAA,GAC/CK,4BAAA,CAA6BN,WAAA,EAAaC,OAAO,CAAA;AACvD,CAAA;AAEO,MAAMM,eAAA,GAAkBA,CAC7BC,oBAAA,EACAP,OAAA,KACG;EACH,MAAMQ,QAAA,GAAWC,WAAWF,oBAAoB,CAAA;EAGhD,MAAMG,YAAYC,KAAA,CAAMC,SAAA,CACtBJ,QAAA,CAASK,MAAA,GAASL,QAAA,CAASK,MAAA,IAAU,CAAA,GAAIb,OAAA,CAAA,CAC3C;EAEA,OAAOc,MAAA,CAAO;IACZ,GAAGN,QAAA;IACHK,MAAA,EAAQL,SAASK,MAAA,GAASH;EAAA,CAC3B,CAAA;AACH,CAAA;AAWO,MAAMK,iCAAA,GACXf,OAAA,IACW;EACX,MAAMgB,SAAA,GAAYC,uBAAuBjB,OAAO,CAAA;EACh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@@ import type { Dino, LineItemSummary, SingleLineItemSummary, TaxRate } from "./ty
4
4
  * rate calculations
5
5
  */
6
6
  export declare const allocatePerItemTaxableAmount: (itemSummary: LineItemSummary) => SingleLineItemSummary[];
7
- export declare const getTaxFromFinalAmount: (amountWithTax: Dino, taxRate: TaxRate) => import("dinero.js").Dinero<number>;
8
- export declare const calculateWholeQtyLineItemTax: (itemSummary: LineItemSummary, taxRate: TaxRate, options: {
9
- hasTaxIncludedPrice: boolean;
10
- }) => Dino;
11
- export declare const calculatePerQtyLineItemTax: (itemSummary: LineItemSummary, taxRate: TaxRate, options: {
12
- hasTaxIncludedPrice: boolean;
13
- }) => Dino;
7
+ export declare const calculateWholeQtyLineItemTax: (itemSummary: LineItemSummary, taxRate: TaxRate) => Dino;
8
+ export declare const calculatePerQtyLineItemTax: (itemSummary: LineItemSummary, taxRate: TaxRate) => Dino;
@@ -29,37 +29,23 @@ const allocatePerItemTaxableAmount = itemSummary => {
29
29
  };
30
30
  });
31
31
  };
32
- const calculateTaxOnTaxableAmount = (itemSummary, taxRate) => {
32
+ const calculateWholeQtyLineItemTax = (itemSummary, taxRate) => {
33
+ if (taxRate === 0) {
34
+ return misc.getDinero(0, itemSummary.currency);
35
+ }
33
36
  return dinero_js.dinero({
34
37
  amount: constants.Round.Financial(itemSummary.taxableAmount * taxRate),
35
38
  currency: misc.getDineroCurrency(itemSummary.currency)
36
39
  });
37
40
  };
38
- const getTaxFromFinalAmount = (amountWithTax, taxRate) => {
39
- const snapshot = dinero_js.toSnapshot(amountWithTax);
40
- const taxAmount = constants.Round.Financial(snapshot.amount - snapshot.amount / (1 + taxRate));
41
- return dinero_js.dinero({
42
- ...snapshot,
43
- amount: taxAmount
44
- });
45
- };
46
- const calculateTaxFromTaxIncludedPrice = (itemSummary, taxRate) => {
47
- return getTaxFromFinalAmount(misc.getDinero(itemSummary.taxableAmount, itemSummary.currency), taxRate);
48
- };
49
- const calculateWholeQtyLineItemTax = (itemSummary, taxRate, options) => {
50
- if (taxRate === 0) {
51
- return misc.getDinero(0, itemSummary.currency);
52
- }
53
- return options.hasTaxIncludedPrice ? calculateTaxFromTaxIncludedPrice(itemSummary, taxRate) : calculateTaxOnTaxableAmount(itemSummary, taxRate);
54
- };
55
- const calculatePerQtyLineItemTax = (itemSummary, taxRate, options) => {
41
+ const calculatePerQtyLineItemTax = (itemSummary, taxRate) => {
56
42
  if (taxRate === 0) {
57
43
  return misc.getDinero(0, itemSummary.currency);
58
44
  }
59
45
  const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);
60
46
  let totalTaxAmount = misc.getDinero(0, itemSummary.currency);
61
47
  singleItemSummaries.forEach(itemSummary2 => {
62
- const taxAmount = calculateWholeQtyLineItemTax(itemSummary2, taxRate, options);
48
+ const taxAmount = calculateWholeQtyLineItemTax(itemSummary2, taxRate);
63
49
  totalTaxAmount = dinero_js.add(totalTaxAmount, taxAmount);
64
50
  });
65
51
  return totalTaxAmount;
@@ -67,5 +53,4 @@ const calculatePerQtyLineItemTax = (itemSummary, taxRate, options) => {
67
53
  exports.allocatePerItemTaxableAmount = allocatePerItemTaxableAmount;
68
54
  exports.calculatePerQtyLineItemTax = calculatePerQtyLineItemTax;
69
55
  exports.calculateWholeQtyLineItemTax = calculateWholeQtyLineItemTax;
70
- exports.getTaxFromFinalAmount = getTaxFromFinalAmount;
71
56
  //# sourceMappingURL=tax.private.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tax.private.js","sources":["../../../src/utils/money/tax.private.ts"],"sourcesContent":["// #############################################################################\n// Internal helper functions not for consumption outside of store.core!\n// Wrote as separate files so that they can be easily unit tested without exposing them\n// #############################################################################\nimport { add, dinero, subtract, toSnapshot } from \"dinero.js\";\n\nimport { FixedAmountAllocator } from \"./allocators/fixedAmount\";\nimport { Round } from \"./constants\";\nimport { amount, getDinero, getDineroCurrency } from \"./misc\";\nimport type {\n Dino,\n LineItemSummary,\n SingleLineItemSummary,\n TaxRate,\n} from \"./types\";\n\n/**\n * Reverse engineer the taxable amount from \"whole item\" taxable amount into \"per item\" taxable amount for custom tax\n * rate calculations\n */\nexport const allocatePerItemTaxableAmount = (\n itemSummary: LineItemSummary,\n): SingleLineItemSummary[] => {\n /**\n * If the item quantity is 1, we can just return the item summary as is\n */\n if (itemSummary.quantity === 1) {\n return [itemSummary as SingleLineItemSummary];\n }\n\n // Reverse engineer price before discount\n const preDiscountAmount = add(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n getDinero(itemSummary.discountAmount, itemSummary.currency),\n );\n\n const allocator = new FixedAmountAllocator({\n initialAllocateAmount: itemSummary.discountAmount,\n initialTotalAmount: amount(preDiscountAmount),\n currency: itemSummary.currency,\n });\n\n // Iterate over the amount of qty to reverse engineer the discount price\n return Array.from({ length: itemSummary.quantity }, () => {\n const discountAllocatedAmount = allocator.allocate(\n itemSummary.itemPrice,\n itemSummary.currency,\n );\n\n const taxableAmount = subtract(\n getDinero(itemSummary.itemPrice, itemSummary.currency),\n discountAllocatedAmount,\n );\n\n return {\n itemPrice: itemSummary.itemPrice,\n taxableAmount: amount(taxableAmount),\n discountAmount: amount(discountAllocatedAmount),\n currency: itemSummary.currency,\n quantity: 1,\n } as SingleLineItemSummary;\n });\n};\n\nconst calculateTaxOnTaxableAmount = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n) => {\n /**\n * Historically we calculate the tax by using dinero allocation, but we did not have control over how we round things.\n * So instead as we are already working with currency in cents we can multiply and round it ourselves.\n */\n return dinero({\n amount: Round.Financial(itemSummary.taxableAmount * taxRate),\n currency: getDineroCurrency(itemSummary.currency),\n });\n};\n\nexport const getTaxFromFinalAmount = (\n amountWithTax: Dino,\n taxRate: TaxRate,\n) => {\n const snapshot = toSnapshot(amountWithTax);\n\n // Calculate the tax amount and apply rounding on it so that behavior for reverse process is consistent\n const taxAmount = Round.Financial(\n snapshot.amount - snapshot.amount / (1 + taxRate),\n );\n\n return dinero({ ...snapshot, amount: taxAmount });\n};\n\nconst calculateTaxFromTaxIncludedPrice = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n) => {\n return getTaxFromFinalAmount(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n taxRate,\n );\n};\n\nexport const calculateWholeQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n options: { hasTaxIncludedPrice: boolean },\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n return options.hasTaxIncludedPrice\n ? calculateTaxFromTaxIncludedPrice(itemSummary, taxRate)\n : calculateTaxOnTaxableAmount(itemSummary, taxRate);\n};\n\nexport const calculatePerQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n options: { hasTaxIncludedPrice: boolean },\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n // Split the item summary into per individual item qty summaries\n const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);\n\n // Calculate individual item qty tax and sum them up\n let totalTaxAmount = getDinero(0, itemSummary.currency);\n\n singleItemSummaries.forEach((itemSummary) => {\n // Probably a bit confusing at first, but calling the whole qty tax calculation function here as we can safely use\n // it as item summary is always a single item summary here\n const taxAmount = calculateWholeQtyLineItemTax(\n itemSummary,\n taxRate,\n options,\n );\n totalTaxAmount = add(totalTaxAmount, taxAmount);\n });\n\n return totalTaxAmount;\n};\n"],"names":["allocatePerItemTaxableAmount","itemSummary","quantity","preDiscountAmount","add","getDinero","taxableAmount","currency","discountAmount","allocator","FixedAmountAllocator","initialAllocateAmount","initialTotalAmount","amount","Array","from","length","discountAllocatedAmount","allocate","itemPrice","subtract","calculateTaxOnTaxableAmount","taxRate","dinero","Round","Financial","getDineroCurrency","getTaxFromFinalAmount","amountWithTax","snapshot","toSnapshot","taxAmount","calculateTaxFromTaxIncludedPrice","calculateWholeQtyLineItemTax","options","hasTaxIncludedPrice","calculatePerQtyLineItemTax","singleItemSummaries","totalTaxAmount","forEach"],"mappings":";;;;;;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AAYM,QAAQ,CAAA;EAC1C;EAGA,MAAM8B,mBAAA,GAAsBrC,6BAA6BC,WAAW,CAAA;EAGpE,IAAIqC,cAAA,GAAiBjC,IAAAA,CAAAA,SAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAEtD8B,mBAAA,CAAoBE,OAAA,CAAStC,YAAAA,IAAgB;IAG3C,MAAM8B,SAAA,GAAYE,4BAAA,CAChBhC,YAAAA,EACAqB,OAAA,EACAY,OAAA,CACF;IACAI,cAAA,GAAiBlC,SAAAA,CAAAA,GAAA,CAAIkC,gBAAgBP,SAAS,CAAA;EAChD,CAAC,CAAA;EAED,OAAOO,cAAA;AACT,CAAA;;;;"}
1
+ {"version":3,"file":"tax.private.js","sources":["../../../src/utils/money/tax.private.ts"],"sourcesContent":["// #############################################################################\n// Internal helper functions not for consumption outside of store.core!\n// Wrote as separate files so that they can be easily unit tested without exposing them\n// #############################################################################\nimport { add, dinero, subtract } from \"dinero.js\";\n\nimport { FixedAmountAllocator } from \"./allocators/fixedAmount\";\nimport { Round } from \"./constants\";\nimport { amount, getDinero, getDineroCurrency } from \"./misc\";\nimport type {\n Dino,\n LineItemSummary,\n SingleLineItemSummary,\n TaxRate,\n} from \"./types\";\n\n/**\n * Reverse engineer the taxable amount from \"whole item\" taxable amount into \"per item\" taxable amount for custom tax\n * rate calculations\n */\nexport const allocatePerItemTaxableAmount = (\n itemSummary: LineItemSummary,\n): SingleLineItemSummary[] => {\n /**\n * If the item quantity is 1, we can just return the item summary as is\n */\n if (itemSummary.quantity === 1) {\n return [itemSummary as SingleLineItemSummary];\n }\n\n // Reverse engineer price before discount\n const preDiscountAmount = add(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n getDinero(itemSummary.discountAmount, itemSummary.currency),\n );\n\n const allocator = new FixedAmountAllocator({\n initialAllocateAmount: itemSummary.discountAmount,\n initialTotalAmount: amount(preDiscountAmount),\n currency: itemSummary.currency,\n });\n\n // Iterate over the amount of qty to reverse engineer the discount price\n return Array.from({ length: itemSummary.quantity }, () => {\n const discountAllocatedAmount = allocator.allocate(\n itemSummary.itemPrice,\n itemSummary.currency,\n );\n\n const taxableAmount = subtract(\n getDinero(itemSummary.itemPrice, itemSummary.currency),\n discountAllocatedAmount,\n );\n\n return {\n itemPrice: itemSummary.itemPrice,\n taxableAmount: amount(taxableAmount),\n discountAmount: amount(discountAllocatedAmount),\n currency: itemSummary.currency,\n quantity: 1,\n } as SingleLineItemSummary;\n });\n};\n\nexport const calculateWholeQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n /**\n * Historically we calculate the tax by using dinero allocation, but we did not have control over how we round things.\n * So instead as we are already working with currency in cents we can multiply and round it ourselves.\n */\n return dinero({\n amount: Round.Financial(itemSummary.taxableAmount * taxRate),\n currency: getDineroCurrency(itemSummary.currency),\n });\n};\n\nexport const calculatePerQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n // Split the item summary into per individual item qty summaries\n const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);\n\n // Calculate individual item qty tax and sum them up\n let totalTaxAmount = getDinero(0, itemSummary.currency);\n\n singleItemSummaries.forEach((itemSummary) => {\n // Probably a bit confusing at first, but calling the whole qty tax calculation function here as we can safely use\n // it as item summary is always a single item summary here\n const taxAmount = calculateWholeQtyLineItemTax(itemSummary, taxRate);\n totalTaxAmount = add(totalTaxAmount, taxAmount);\n });\n\n return totalTaxAmount;\n};\n"],"names":["allocatePerItemTaxableAmount","itemSummary","quantity","preDiscountAmount","add","getDinero","taxableAmount","currency","discountAmount","allocator","FixedAmountAllocator","initialAllocateAmount","initialTotalAmount","amount","Array","from","length","discountAllocatedAmount","allocate","itemPrice","subtract","calculateWholeQtyLineItemTax","taxRate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;AAEO,MAAMmB,4BAAA,GAA+BA,CAC1CpB,WAAA,EACAqB,OAAA,KACS;EAET,IAAIA,YAAY,CAAA,EAAG;IACjB,OAAOjB,cAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAMA,OAAOgB,gBAAA,CAAO;IACZV,MAAA,EAAQW,SAAAA,CAAAA,KAAA,CAAMC,SAAA,CAAUxB,WAAA,CAAYK,gBAAgBgB,OAAO,CAAA;IAC3Df,QAAA,EAAUmB,IAAAA,CAAAA,iBAAA,CAAkBzB,WAAA,CAAYM,QAAQ;EAAA,CACjD,CAAA;AACH,CAAA;AAEO,MAAMoB,0BAAA,GAA6BA,CACxC1B,WAAA,EACAqB,OAAA,KACS;EAET,IAAIA,YAAY,CAAA,EAAG;IACjB,OAAOjB,cAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAGA,MAAMqB,mBAAA,GAAsB5B,6BAA6BC,WAAW,CAAA;EAGpE,IAAI4B,cAAA,GAAiBxB,IAAAA,CAAAA,SAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAEtDqB,mBAAA,CAAoBE,OAAA,CAAS7B,YAAAA,IAAgB;IAG3C,MAAM8B,SAAA,GAAYV,4BAAA,CAA6BpB,YAAAA,EAAaqB,OAAO,CAAA;IACnEO,cAAA,GAAiBzB,SAAAA,CAAAA,GAAA,CAAIyB,gBAAgBE,SAAS,CAAA;EAChD,CAAC,CAAA;EAED,OAAOF,cAAA;AACT,CAAA;;;"}
@@ -1,4 +1,4 @@
1
- import { add, subtract, dinero, toSnapshot } from 'dinero.js';
1
+ import { add, subtract, dinero } from 'dinero.js';
2
2
  import { FixedAmountAllocator } from './allocators/fixedAmount.mjs';
3
3
  import { Round } from './constants.mjs';
4
4
  import { getDinero, amount, getDineroCurrency } from './misc.mjs';
@@ -27,40 +27,26 @@ const allocatePerItemTaxableAmount = itemSummary => {
27
27
  };
28
28
  });
29
29
  };
30
- const calculateTaxOnTaxableAmount = (itemSummary, taxRate) => {
30
+ const calculateWholeQtyLineItemTax = (itemSummary, taxRate) => {
31
+ if (taxRate === 0) {
32
+ return getDinero(0, itemSummary.currency);
33
+ }
31
34
  return dinero({
32
35
  amount: Round.Financial(itemSummary.taxableAmount * taxRate),
33
36
  currency: getDineroCurrency(itemSummary.currency)
34
37
  });
35
38
  };
36
- const getTaxFromFinalAmount = (amountWithTax, taxRate) => {
37
- const snapshot = toSnapshot(amountWithTax);
38
- const taxAmount = Round.Financial(snapshot.amount - snapshot.amount / (1 + taxRate));
39
- return dinero({
40
- ...snapshot,
41
- amount: taxAmount
42
- });
43
- };
44
- const calculateTaxFromTaxIncludedPrice = (itemSummary, taxRate) => {
45
- return getTaxFromFinalAmount(getDinero(itemSummary.taxableAmount, itemSummary.currency), taxRate);
46
- };
47
- const calculateWholeQtyLineItemTax = (itemSummary, taxRate, options) => {
48
- if (taxRate === 0) {
49
- return getDinero(0, itemSummary.currency);
50
- }
51
- return options.hasTaxIncludedPrice ? calculateTaxFromTaxIncludedPrice(itemSummary, taxRate) : calculateTaxOnTaxableAmount(itemSummary, taxRate);
52
- };
53
- const calculatePerQtyLineItemTax = (itemSummary, taxRate, options) => {
39
+ const calculatePerQtyLineItemTax = (itemSummary, taxRate) => {
54
40
  if (taxRate === 0) {
55
41
  return getDinero(0, itemSummary.currency);
56
42
  }
57
43
  const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);
58
44
  let totalTaxAmount = getDinero(0, itemSummary.currency);
59
45
  singleItemSummaries.forEach(itemSummary2 => {
60
- const taxAmount = calculateWholeQtyLineItemTax(itemSummary2, taxRate, options);
46
+ const taxAmount = calculateWholeQtyLineItemTax(itemSummary2, taxRate);
61
47
  totalTaxAmount = add(totalTaxAmount, taxAmount);
62
48
  });
63
49
  return totalTaxAmount;
64
50
  };
65
- export { allocatePerItemTaxableAmount, calculatePerQtyLineItemTax, calculateWholeQtyLineItemTax, getTaxFromFinalAmount };
51
+ export { allocatePerItemTaxableAmount, calculatePerQtyLineItemTax, calculateWholeQtyLineItemTax };
66
52
  //# sourceMappingURL=tax.private.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"tax.private.mjs","sources":["../../../src/utils/money/tax.private.ts"],"sourcesContent":["// #############################################################################\n// Internal helper functions not for consumption outside of store.core!\n// Wrote as separate files so that they can be easily unit tested without exposing them\n// #############################################################################\nimport { add, dinero, subtract, toSnapshot } from \"dinero.js\";\n\nimport { FixedAmountAllocator } from \"./allocators/fixedAmount\";\nimport { Round } from \"./constants\";\nimport { amount, getDinero, getDineroCurrency } from \"./misc\";\nimport type {\n Dino,\n LineItemSummary,\n SingleLineItemSummary,\n TaxRate,\n} from \"./types\";\n\n/**\n * Reverse engineer the taxable amount from \"whole item\" taxable amount into \"per item\" taxable amount for custom tax\n * rate calculations\n */\nexport const allocatePerItemTaxableAmount = (\n itemSummary: LineItemSummary,\n): SingleLineItemSummary[] => {\n /**\n * If the item quantity is 1, we can just return the item summary as is\n */\n if (itemSummary.quantity === 1) {\n return [itemSummary as SingleLineItemSummary];\n }\n\n // Reverse engineer price before discount\n const preDiscountAmount = add(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n getDinero(itemSummary.discountAmount, itemSummary.currency),\n );\n\n const allocator = new FixedAmountAllocator({\n initialAllocateAmount: itemSummary.discountAmount,\n initialTotalAmount: amount(preDiscountAmount),\n currency: itemSummary.currency,\n });\n\n // Iterate over the amount of qty to reverse engineer the discount price\n return Array.from({ length: itemSummary.quantity }, () => {\n const discountAllocatedAmount = allocator.allocate(\n itemSummary.itemPrice,\n itemSummary.currency,\n );\n\n const taxableAmount = subtract(\n getDinero(itemSummary.itemPrice, itemSummary.currency),\n discountAllocatedAmount,\n );\n\n return {\n itemPrice: itemSummary.itemPrice,\n taxableAmount: amount(taxableAmount),\n discountAmount: amount(discountAllocatedAmount),\n currency: itemSummary.currency,\n quantity: 1,\n } as SingleLineItemSummary;\n });\n};\n\nconst calculateTaxOnTaxableAmount = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n) => {\n /**\n * Historically we calculate the tax by using dinero allocation, but we did not have control over how we round things.\n * So instead as we are already working with currency in cents we can multiply and round it ourselves.\n */\n return dinero({\n amount: Round.Financial(itemSummary.taxableAmount * taxRate),\n currency: getDineroCurrency(itemSummary.currency),\n });\n};\n\nexport const getTaxFromFinalAmount = (\n amountWithTax: Dino,\n taxRate: TaxRate,\n) => {\n const snapshot = toSnapshot(amountWithTax);\n\n // Calculate the tax amount and apply rounding on it so that behavior for reverse process is consistent\n const taxAmount = Round.Financial(\n snapshot.amount - snapshot.amount / (1 + taxRate),\n );\n\n return dinero({ ...snapshot, amount: taxAmount });\n};\n\nconst calculateTaxFromTaxIncludedPrice = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n) => {\n return getTaxFromFinalAmount(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n taxRate,\n );\n};\n\nexport const calculateWholeQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n options: { hasTaxIncludedPrice: boolean },\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n return options.hasTaxIncludedPrice\n ? calculateTaxFromTaxIncludedPrice(itemSummary, taxRate)\n : calculateTaxOnTaxableAmount(itemSummary, taxRate);\n};\n\nexport const calculatePerQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n options: { hasTaxIncludedPrice: boolean },\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n // Split the item summary into per individual item qty summaries\n const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);\n\n // Calculate individual item qty tax and sum them up\n let totalTaxAmount = getDinero(0, itemSummary.currency);\n\n singleItemSummaries.forEach((itemSummary) => {\n // Probably a bit confusing at first, but calling the whole qty tax calculation function here as we can safely use\n // it as item summary is always a single item summary here\n const taxAmount = calculateWholeQtyLineItemTax(\n itemSummary,\n taxRate,\n options,\n );\n totalTaxAmount = add(totalTaxAmount, taxAmount);\n });\n\n return totalTaxAmount;\n};\n"],"names":["allocatePerItemTaxableAmount","itemSummary","quantity","preDiscountAmount","add","getDinero","taxableAmount","currency","discountAmount","allocator","FixedAmountAllocator","initialAllocateAmount","initialTotalAmount","amount","Array","from","length","discountAllocatedAmount","allocate","itemPrice","subtract","calculateTaxOnTaxableAmount","taxRate","dinero","Round","Financial","getDineroCurrency","getTaxFromFinalAmount","amountWithTax","snapshot","toSnapshot","taxAmount","calculateTaxFromTaxIncludedPrice","calculateWholeQtyLineItemTax","options","hasTaxIncludedPrice","calculatePerQtyLineItemTax","singleItemSummaries","totalTaxAmount","forEach"],"mappings":";;;;;AAoBO,MAAMA,4BAAA,GACXC,WAAA,IAC4B;EAI5B,IAAIA,WAAA,CAAYC,aAAa,CAAA,EAAG;IAC9B,OAAO,CAACD,WAAoC,CAAA;EAC9C;EAGA,MAAME,iBAAA,GAAoBC,GAAA,CACxBC,SAAA,CAAUJ,WAAA,CAAYK,aAAA,EAAeL,WAAA,CAAYM,QAAQ,CAAA,EACzDF,SAAA,CAAUJ,WAAA,CAAYO,cAAA,EAAgBP,WAAA,CAAYM,QAAQ,CAAA,CAC5D;EAEA,MAAME,SAAA,GAAY,IAAIC,oBAAA,CAAqB;IACzCC,uBAAuBV,WAAA,CAAYO,cAAA;IACnCI,kBAAA,EAAoBC,OAAOV,iBAAiB,CAAA;IAC5CI,UAAUN,WAAA,CAAYM;EAAA,CACvB,CAAA;EAGD,OAAOO,MAAMC,IAAA,CAAK;IAAEC,QAAQf,WAAA,CAAYC;EAAA,GAAY,MAAM;IACxD,MAAMe,0BAA0BR,SAAA,CAAUS,QAAA,CACxCjB,WAAA,CAAYkB,SAAA,EACZlB,WAAA,CAAYM,QAAA,CACd;IAEA,MAAMD,aAAA,GAAgBc,QAAA,CACpBf,SAAA,CAAUJ,WAAA,CAAYkB,SAAA,EAAWlB,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AAA,CAAQC,sBACXH,gCAAA,CAAiC/B,WAAA,EAAaqB,OAAO,CAAA,GACrDD,2BAAA,CAA4BpB,aAAaqB,OAAO,CAAA;AACtD,CAAA;AAEO,MAAMc,0BAAA,GAA6BA,CACxCnC,WAAA,EACAqB,OAAA,EACAY,OAAA,KACS;EAET,IAAIZ,YAAY,CAAA,EAAG;IACjB,OAAOjB,SAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAGA,MAAM8B,mBAAA,GAAsBrC,6BAA6BC,WAAW,CAAA;EAGpE,IAAIqC,cAAA,GAAiBjC,SAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAEtD8B,mBAAA,CAAoBE,OAAA,CAAStC,YAAAA,IAAgB;IAG3C,MAAM8B,SAAA,GAAYE,4BAAA,CAChBhC,YAAAA,EACAqB,OAAA,EACAY,OAAA,CACF;IACAI,cAAA,GAAiBlC,GAAA,CAAIkC,gBAAgBP,SAAS,CAAA;EAChD,CAAC,CAAA;EAED,OAAOO,cAAA;AACT,CAAA;"}
1
+ {"version":3,"file":"tax.private.mjs","sources":["../../../src/utils/money/tax.private.ts"],"sourcesContent":["// #############################################################################\n// Internal helper functions not for consumption outside of store.core!\n// Wrote as separate files so that they can be easily unit tested without exposing them\n// #############################################################################\nimport { add, dinero, subtract } from \"dinero.js\";\n\nimport { FixedAmountAllocator } from \"./allocators/fixedAmount\";\nimport { Round } from \"./constants\";\nimport { amount, getDinero, getDineroCurrency } from \"./misc\";\nimport type {\n Dino,\n LineItemSummary,\n SingleLineItemSummary,\n TaxRate,\n} from \"./types\";\n\n/**\n * Reverse engineer the taxable amount from \"whole item\" taxable amount into \"per item\" taxable amount for custom tax\n * rate calculations\n */\nexport const allocatePerItemTaxableAmount = (\n itemSummary: LineItemSummary,\n): SingleLineItemSummary[] => {\n /**\n * If the item quantity is 1, we can just return the item summary as is\n */\n if (itemSummary.quantity === 1) {\n return [itemSummary as SingleLineItemSummary];\n }\n\n // Reverse engineer price before discount\n const preDiscountAmount = add(\n getDinero(itemSummary.taxableAmount, itemSummary.currency),\n getDinero(itemSummary.discountAmount, itemSummary.currency),\n );\n\n const allocator = new FixedAmountAllocator({\n initialAllocateAmount: itemSummary.discountAmount,\n initialTotalAmount: amount(preDiscountAmount),\n currency: itemSummary.currency,\n });\n\n // Iterate over the amount of qty to reverse engineer the discount price\n return Array.from({ length: itemSummary.quantity }, () => {\n const discountAllocatedAmount = allocator.allocate(\n itemSummary.itemPrice,\n itemSummary.currency,\n );\n\n const taxableAmount = subtract(\n getDinero(itemSummary.itemPrice, itemSummary.currency),\n discountAllocatedAmount,\n );\n\n return {\n itemPrice: itemSummary.itemPrice,\n taxableAmount: amount(taxableAmount),\n discountAmount: amount(discountAllocatedAmount),\n currency: itemSummary.currency,\n quantity: 1,\n } as SingleLineItemSummary;\n });\n};\n\nexport const calculateWholeQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n /**\n * Historically we calculate the tax by using dinero allocation, but we did not have control over how we round things.\n * So instead as we are already working with currency in cents we can multiply and round it ourselves.\n */\n return dinero({\n amount: Round.Financial(itemSummary.taxableAmount * taxRate),\n currency: getDineroCurrency(itemSummary.currency),\n });\n};\n\nexport const calculatePerQtyLineItemTax = (\n itemSummary: LineItemSummary,\n taxRate: TaxRate,\n): Dino => {\n // Nothing to calculate\n if (taxRate === 0) {\n return getDinero(0, itemSummary.currency);\n }\n\n // Split the item summary into per individual item qty summaries\n const singleItemSummaries = allocatePerItemTaxableAmount(itemSummary);\n\n // Calculate individual item qty tax and sum them up\n let totalTaxAmount = getDinero(0, itemSummary.currency);\n\n singleItemSummaries.forEach((itemSummary) => {\n // Probably a bit confusing at first, but calling the whole qty tax calculation function here as we can safely use\n // it as item summary is always a single item summary here\n const taxAmount = calculateWholeQtyLineItemTax(itemSummary, taxRate);\n totalTaxAmount = add(totalTaxAmount, taxAmount);\n });\n\n return totalTaxAmount;\n};\n"],"names":["allocatePerItemTaxableAmount","itemSummary","quantity","preDiscountAmount","add","getDinero","taxableAmount","currency","discountAmount","allocator","FixedAmountAllocator","initialAllocateAmount","initialTotalAmount","amount","Array","from","length","discountAllocatedAmount","allocate","itemPrice","subtract","calculateWholeQtyLineItemTax","taxRate","dinero","Round","Financial","getDineroCurrency","calculatePerQtyLineItemTax","singleItemSummaries","totalTaxAmount","forEach","taxAmount"],"mappings":";;;;;AAoBO,MAAMA,4BAAA,GACXC,WAAA,IAC4B;EAI5B,IAAIA,WAAA,CAAYC,aAAa,CAAA,EAAG;IAC9B,OAAO,CAACD,WAAoC,CAAA;EAC9C;EAGA,MAAME,iBAAA,GAAoBC,GAAA,CACxBC,SAAA,CAAUJ,WAAA,CAAYK,aAAA,EAAeL,WAAA,CAAYM,QAAQ,CAAA,EACzDF,SAAA,CAAUJ,WAAA,CAAYO,cAAA,EAAgBP,WAAA,CAAYM,QAAQ,CAAA,CAC5D;EAEA,MAAME,SAAA,GAAY,IAAIC,oBAAA,CAAqB;IACzCC,uBAAuBV,WAAA,CAAYO,cAAA;IACnCI,kBAAA,EAAoBC,OAAOV,iBAAiB,CAAA;IAC5CI,UAAUN,WAAA,CAAYM;EAAA,CACvB,CAAA;EAGD,OAAOO,MAAMC,IAAA,CAAK;IAAEC,QAAQf,WAAA,CAAYC;EAAA,GAAY,MAAM;IACxD,MAAMe,0BAA0BR,SAAA,CAAUS,QAAA,CACxCjB,WAAA,CAAYkB,SAAA,EACZlB,WAAA,CAAYM,QAAA,CACd;IAEA,MAAMD,aAAA,GAAgBc,QAAA,CACpBf,SAAA,CAAUJ,WAAA,CAAYkB,SAAA,EAAWlB,WAAA,CAAYM,QAAQ,CAAA,EACrDU,uBAAA,CACF;IAEA,OAAO;MACLE,WAAWlB,WAAA,CAAYkB,SAAA;MACvBb,aAAA,EAAeO,OAAOP,aAAa,CAAA;MACnCE,cAAA,EAAgBK,OAAOI,uBAAuB,CAAA;MAC9CV,UAAUN,WAAA,CAAYM,QAAA;MACtBL,QAAA,EAAU;IAAA,CACZ;EACF,CAAC,CAAA;AACH,CAAA;AAEO,MAAMmB,4BAAA,GAA+BA,CAC1CpB,WAAA,EACAqB,OAAA,KACS;EAET,IAAIA,YAAY,CAAA,EAAG;IACjB,OAAOjB,SAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAMA,OAAOgB,MAAA,CAAO;IACZV,MAAA,EAAQW,KAAA,CAAMC,SAAA,CAAUxB,WAAA,CAAYK,gBAAgBgB,OAAO,CAAA;IAC3Df,QAAA,EAAUmB,iBAAA,CAAkBzB,WAAA,CAAYM,QAAQ;EAAA,CACjD,CAAA;AACH,CAAA;AAEO,MAAMoB,0BAAA,GAA6BA,CACxC1B,WAAA,EACAqB,OAAA,KACS;EAET,IAAIA,YAAY,CAAA,EAAG;IACjB,OAAOjB,SAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAC1C;EAGA,MAAMqB,mBAAA,GAAsB5B,6BAA6BC,WAAW,CAAA;EAGpE,IAAI4B,cAAA,GAAiBxB,SAAA,CAAU,CAAA,EAAGJ,WAAA,CAAYM,QAAQ,CAAA;EAEtDqB,mBAAA,CAAoBE,OAAA,CAAS7B,YAAAA,IAAgB;IAG3C,MAAM8B,SAAA,GAAYV,4BAAA,CAA6BpB,YAAAA,EAAaqB,OAAO,CAAA;IACnEO,cAAA,GAAiBzB,GAAA,CAAIyB,gBAAgBE,SAAS,CAAA;EAChD,CAAC,CAAA;EAED,OAAOF,cAAA;AACT,CAAA;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ubnt/store.core",
3
- "version": "11.28.0-custom-tax.1",
3
+ "version": "11.28.0",
4
4
  "description": "",
5
5
  "sideEffects": false,
6
6
  "scripts": {
@@ -1 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20"><path fill="#808893" fill-rule="evenodd" d="M2.008 12.973q0-.134.017-.263a2 2 0 0 1 1.983-1.737h12A2 2 0 0 1 17.99 12.7v.007q.018.13.018.266v1a2 2 0 0 1-2 2h-12a2 2 0 0 1-2-2zM5 13h1v1H5zm3 0h1v1H8zm4 0h-1v1h1zm2 0h1v1h-1z" clip-rule="evenodd"/><path fill="#808893" d="M4.008 9.973c-.572 0-1.108.16-1.563.439l.833-4.38A2.5 2.5 0 0 1 5.734 4h8.532a2.5 2.5 0 0 1 2.455 2.033l.831 4.367a3 3 0 0 0-1.544-.427z"/></svg>
@@ -1 +0,0 @@
1
- {"version":3,"file":"map.js","sources":["../../src/utils/map.ts"],"sourcesContent":["import { createArrayFromInterfaceKeys } from \"./array\";\n\n/**\n * Type-safe key parser that ensures all properties of the key are used\n */\nexport type KeyParser<Key> = (key: Key) => string;\n\n/**\n * Type-safe KeyMap implementation with compile-time validation\n */\nexport class KeyMap<Key, Value> {\n #map: Map<string, Value>;\n #keyParser: KeyParser<Key>;\n #keyValidator: (key: Key) => void;\n\n constructor(\n parser: KeyParser<Key>,\n entries?: readonly (readonly [Key, Value])[] | null | undefined,\n validator?: (key: Key) => void,\n ) {\n this.#keyParser = parser;\n this.#keyValidator = validator || (() => {});\n this.#map = Array.isArray(entries)\n ? new Map(\n entries.map(([key, value]) => {\n this.#keyValidator(key);\n return [this.#keyParser(key), value];\n }),\n )\n : new Map();\n }\n\n /**\n * Get a value by key with runtime validation\n */\n get(key: Key): Value | undefined {\n this.#keyValidator(key);\n return this.#map.get(this.#keyParser(key));\n }\n\n /**\n * Set a value by key with runtime validation\n */\n set(key: Key, value: Value): void {\n this.#keyValidator(key);\n this.#map.set(this.#keyParser(key), value);\n }\n\n /**\n * Check if a key exists with runtime validation\n */\n has(key: Key): boolean {\n this.#keyValidator(key);\n return this.#map.has(this.#keyParser(key));\n }\n\n /**\n * Get all values\n */\n values(): IterableIterator<Value> {\n return this.#map.values();\n }\n\n /**\n * Get all keys (returns stringified keys)\n */\n keys(): IterableIterator<string> {\n return this.#map.keys();\n }\n\n /**\n * Get all entries (returns stringified keys)\n */\n entries(): IterableIterator<[string, Value]> {\n return this.#map.entries();\n }\n\n /**\n * Get the size of the map\n */\n get size(): number {\n return this.#map.size;\n }\n\n /**\n * Clear all entries\n */\n clear(): void {\n this.#map.clear();\n }\n\n /**\n * Delete an entry by key\n */\n delete(key: Key): boolean {\n this.#keyValidator(key);\n return this.#map.delete(this.#keyParser(key));\n }\n\n /**\n * Create a KeyMap for database composite keys with automatic validation\n */\n static createForCompositeKey<\n Key extends Record<string, string | number>,\n Value,\n >(\n keyFieldObject: Record<keyof Key, true>,\n entries?: readonly (readonly [Key, Value])[] | null | undefined,\n ): KeyMap<Key, Value> {\n const keyFields = createArrayFromInterfaceKeys(keyFieldObject);\n\n const parser: KeyParser<Key> = (key) => {\n return keyFields.map((field) => String(key[field])).join(\"_\");\n };\n\n const validator = (key: Key) => {\n for (const field of keyFields) {\n if (!(field in key)) {\n throw new Error(`Missing required key field: ${String(field)}`);\n }\n if (key[field] == null) {\n throw new Error(\n `Key field ${String(field)} cannot be null or undefined`,\n );\n }\n }\n };\n\n return new KeyMap(parser, entries, validator);\n }\n}\n"],"names":["KeyMap","map","keyParser","keyValidator","constructor","parser","entries","validator","Array","isArray","Map","key","value","get","set","has","values","keys","size","clear","delete","createForCompositeKey","keyFieldObject","keyFields","createArrayFromInterfaceKeys","field","String","join","Error"],"mappings":";;;;AAUO,MAAMA,MAAA,CAAmB;EAC9B,CAAAC,GAAA;EACA,CAAAC,SAAA;EACA,CAAAC,YAAA;EAEAC,WAAAA,CACEC,MAAA,EACAC,OAAA,EACAC,SAAA,EACA;IACA,IAAA,CAAK,CAAAL,SAAA,GAAaG,MAAA;IAClB,IAAA,CAAK,CAAAF,YAAA,GAAgBI,cAAc,MAAM,CAAC,CAAA,CAAA;IAC1C,IAAA,CAAK,CAAAN,GAAA,GAAOO,KAA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file":"map.mjs","sources":["../../src/utils/map.ts"],"sourcesContent":["import { createArrayFromInterfaceKeys } from \"./array\";\n\n/**\n * Type-safe key parser that ensures all properties of the key are used\n */\nexport type KeyParser<Key> = (key: Key) => string;\n\n/**\n * Type-safe KeyMap implementation with compile-time validation\n */\nexport class KeyMap<Key, Value> {\n #map: Map<string, Value>;\n #keyParser: KeyParser<Key>;\n #keyValidator: (key: Key) => void;\n\n constructor(\n parser: KeyParser<Key>,\n entries?: readonly (readonly [Key, Value])[] | null | undefined,\n validator?: (key: Key) => void,\n ) {\n this.#keyParser = parser;\n this.#keyValidator = validator || (() => {});\n this.#map = Array.isArray(entries)\n ? new Map(\n entries.map(([key, value]) => {\n this.#keyValidator(key);\n return [this.#keyParser(key), value];\n }),\n )\n : new Map();\n }\n\n /**\n * Get a value by key with runtime validation\n */\n get(key: Key): Value | undefined {\n this.#keyValidator(key);\n return this.#map.get(this.#keyParser(key));\n }\n\n /**\n * Set a value by key with runtime validation\n */\n set(key: Key, value: Value): void {\n this.#keyValidator(key);\n this.#map.set(this.#keyParser(key), value);\n }\n\n /**\n * Check if a key exists with runtime validation\n */\n has(key: Key): boolean {\n this.#keyValidator(key);\n return this.#map.has(this.#keyParser(key));\n }\n\n /**\n * Get all values\n */\n values(): IterableIterator<Value> {\n return this.#map.values();\n }\n\n /**\n * Get all keys (returns stringified keys)\n */\n keys(): IterableIterator<string> {\n return this.#map.keys();\n }\n\n /**\n * Get all entries (returns stringified keys)\n */\n entries(): IterableIterator<[string, Value]> {\n return this.#map.entries();\n }\n\n /**\n * Get the size of the map\n */\n get size(): number {\n return this.#map.size;\n }\n\n /**\n * Clear all entries\n */\n clear(): void {\n this.#map.clear();\n }\n\n /**\n * Delete an entry by key\n */\n delete(key: Key): boolean {\n this.#keyValidator(key);\n return this.#map.delete(this.#keyParser(key));\n }\n\n /**\n * Create a KeyMap for database composite keys with automatic validation\n */\n static createForCompositeKey<\n Key extends Record<string, string | number>,\n Value,\n >(\n keyFieldObject: Record<keyof Key, true>,\n entries?: readonly (readonly [Key, Value])[] | null | undefined,\n ): KeyMap<Key, Value> {\n const keyFields = createArrayFromInterfaceKeys(keyFieldObject);\n\n const parser: KeyParser<Key> = (key) => {\n return keyFields.map((field) => String(key[field])).join(\"_\");\n };\n\n const validator = (key: Key) => {\n for (const field of keyFields) {\n if (!(field in key)) {\n throw new Error(`Missing required key field: ${String(field)}`);\n }\n if (key[field] == null) {\n throw new Error(\n `Key field ${String(field)} cannot be null or undefined`,\n );\n }\n }\n };\n\n return new KeyMap(parser, entries, validator);\n }\n}\n"],"names":["KeyMap","map","keyParser","keyValidator","constructor","parser","entries","validator","Array","isArray","Map","key","value","get","set","has","values","keys","size","clear","delete","createForCompositeKey","keyFieldObject","keyFields","createArrayFromInterfaceKeys","field","String","join","Error"],"mappings":";;AAUO,MAAMA,MAAA,CAAmB;EAC9B,CAAAC,GAAA;EACA,CAAAC,SAAA;EACA,CAAAC,YAAA;EAEAC,WAAAA,CACEC,MAAA,EACAC,OAAA,EACAC,SAAA,EACA;IACA,IAAA,CAAK,CAAAL,SAAA,GAAaG,MAAA;IAClB,IAAA,CAAK,CAAAF,YAAA,GAAgBI,cAAc,MAAM,CAAC,CAAA,CAAA;IAC1C,IAAA,CAAK,CAAAN,GAAA,GAAOO,KAAA,CAAMC,OAAA,CAAQH,OAAO,IAC7B,IAAII,GAAA,CACFJ,QAAQL,GAAA,CAAI,CAAC,CAACU,GAAA,EAAKC,KAAK,CAAA,KAAM;MAC5B,IAAA,CAAK,CAAAT,aAAcQ,GAAG,CAAA;MACtB,OAAO,CAAC,IAAA,CAAK,CAAAT,SAAA,CAAWS,GAAG,GAAGC,KAAK,CAAA;IACrC,CAAC,CAAA,CACH,GAAA,mBACIF,GAAA,CAAA,CAAI;EACd;EAAA;AAAA;AAAA;EAKAG,IAAIF,GAAA,EAA6B;IAC/B,IAAA,CAAK,CAAAR,aAAcQ,GAAG,CAAA;IACtB,OAAO,KAAK,CAAAV,GAAA,CAAKY,G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A;AAAA;AAAA;EAKA,OAAOU,qBAAAA,CAILC,cAAA,EACAhB,OAAA,EACoB;IACpB,MAAMiB,SAAA,GAAYC,6BAA6BF,cAAc,CAAA;IAE7D,MAAMjB,MAAA,GAA0BM,GAAA,IAAQ;MACtC,OAAOY,SAAA,CAAUtB,GAAA,CAAKwB,KAAA,IAAUC,MAAA,CAAOf,GAAA,CAAIc,KAAK,CAAC,CAAC,CAAA,CAAEE,IAAA,CAAK,GAAG,CAAA;IAC9D,CAAA;IAEA,MAAMpB,SAAA,GAAaI,GAAA,IAAa;MAC9B,KAAA,MAAWc,SAASF,SAAA,EAAW;QAC7B,IAAI,EAAEE,SAASd,GAAA,CAAA,EAAM;UACnB,MAAM,IAAIiB,KAAA,CAAM,+BAA+BF,MAAA,CAAOD,KAAK,CAAC,EAAE,CAAA;QAChE;QACA,IAAId,GAAA,CAAIc,KAAK,CAAA,IAAK,IAAA,EAAM;UACtB,MAAM,IAAIG,KAAA,CACR,aAAaF,MAAA,CAAOD,KAAK,CAAC,8BAAA,CAC5B;QACF;MACF;IACF,CAAA;IAEA,OAAO,IAAIzB,MAAA,CAAOK,MAAA,EAAQC,OAAA,EAASC,SAAS,CAAA;EAC9C;AACF;"}
@@ -1,17 +0,0 @@
1
- import type { Currency } from "../../../types";
2
- import type { Cents, Dino } from "../types";
3
- /**
4
- * Helper class to distribute a total amount across a quantity of items.
5
- */
6
- export declare class TotalAmountAllocator {
7
- #private;
8
- constructor({ amount, quantity, currency, }: {
9
- quantity: number;
10
- amount: Cents;
11
- currency: Currency;
12
- });
13
- allocate(quantity: number): {
14
- qtyAllocated: number;
15
- amount: Dino;
16
- };
17
- }
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- var dinero_js = require('dinero.js');
4
- var errors = require('../../errors.js');
5
- var misc = require('../misc.js');
6
- "use strict";
7
- class TotalAmountAllocator {
8
- #remainingAmount;
9
- #remainingQuantity;
10
- #currency;
11
- constructor({
12
- amount,
13
- quantity,
14
- currency
15
- }) {
16
- this.#remainingAmount = misc.getDinero(amount, currency);
17
- this.#remainingQuantity = quantity;
18
- this.#currency = currency;
19
- }
20
- allocate(quantity) {
21
- const quantityToAllocate = Math.min(quantity, this.#remainingQuantity);
22
- if (quantityToAllocate === 0) {
23
- return {
24
- qtyAllocated: 0,
25
- amount: misc.getDinero(0, this.#currency)
26
- };
27
- }
28
- const [allocated, newRemainingAmount] = dinero_js.allocate(this.#remainingAmount, [quantityToAllocate, this.#remainingQuantity - quantityToAllocate]);
29
- errors.assert(!!allocated, "Couldn't allocate amount");
30
- errors.assert(!!newRemainingAmount, "Couldn't retrieve remaining amount");
31
- this.#remainingAmount = newRemainingAmount;
32
- this.#remainingQuantity -= quantityToAllocate;
33
- return {
34
- qtyAllocated: quantityToAllocate,
35
- amount: allocated
36
- };
37
- }
38
- }
39
- exports.TotalAmountAllocator = TotalAmountAllocator;
40
- //# sourceMappingURL=totalAmount.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"totalAmount.js","sources":["../../../../src/utils/money/allocators/totalAmount.ts"],"sourcesContent":["import { allocate } from \"dinero.js\";\n\nimport type { Currency } from \"../../../types\";\nimport { assert } from \"../../errors\";\nimport { getDinero } from \"../misc\";\nimport type { Cents, Dino } from \"../types\";\n\n/**\n * Helper class to distribute a total amount across a quantity of items.\n */\nexport class TotalAmountAllocator {\n #remainingAmount: Dino;\n #remainingQuantity: number;\n #currency: Currency;\n\n constructor({\n amount,\n quantity,\n currency,\n }: {\n quantity: number;\n amount: Cents;\n currency: Currency;\n }) {\n this.#remainingAmount = getDinero(amount, currency);\n this.#remainingQuantity = quantity;\n this.#currency = currency;\n }\n\n allocate(quantity: number): { qtyAllocated: number; amount: Dino } {\n // Normalize quantity to not exceed remaining\n const quantityToAllocate = Math.min(quantity, this.#remainingQuantity);\n\n if (quantityToAllocate === 0) {\n return { qtyAllocated: 0, amount: getDinero(0, this.#currency) };\n }\n\n const [allocated, newRemainingAmount] = allocate(this.#remainingAmount, [\n quantityToAllocate,\n this.#remainingQuantity - quantityToAllocate,\n ]);\n\n assert(!!allocated, \"Couldn't allocate amount\");\n assert(!!newRemainingAmount, \"Couldn't retrieve remaining amount\");\n\n // Update remaining amounts\n this.#remainingAmount = newRemainingAmount;\n this.#remainingQuantity -= quantityToAllocate;\n\n return { qtyAllocated: quantityToAllocate, amount: allocated };\n }\n}\n"],"names":["TotalAmountAllocator","remainingAmount","remainingQuantity","currency","constructor","amount","quantity","getDinero","allocate","quantityToAllocate","Math","min","qtyAllocated","allocated","newRemainingAmount","assert"],"mappings":";;;;;;AAUO,MAAMA,oBAAA,CAAqB;EAChC,CAAAC,eAAA;EACA,CAAAC,iBAAA;EACA,CAAAC,QAAA;EAEAC,WAAAA,CAAY;IACVC,MAAA;IACAC,QAAA;IACAH;EAAA,CACF,EAIG;IACD,IAAA,CAAK,CAAAF,eAAA,GAAmBM,cAAA,CAAUF,MAAA,EAAQF,QAAQ,CAAA;IAClD,IAAA,CAAK,CAAAD,iBAAA,GAAqBI,QAAA;IAC1B,IAAA,CAAK,CAAAH,QAAA,GAAYA,QAAA;EACnB;EAEAK,SAASF,QAAA,EAA0D;IAEjE,MAAMG,kBAAA,GAAqBC,IAAA,CAAKC,GAAA,CAAIL,QAAA,EAAU,KAAK,CAAAJ,iBAAkB,CAAA;IAErE,IAAIO,uBAAuB,CAAA,EAAG;MAC5B,OAAO;QAAEG,cAAc,CAAA;QAAGP,MAAA,EAAQE,IAAAA,CAAAA,UAAU,CAAA,EAAG,IAAA,CAAK,CAAAJ,QAAS;MAAA,CAAE;IACjE;IAEA,MAAM,CAACU,SAAA,EAAWC,kBAAkB,CAAA,GAAIN,SAAAA,CAAAA,QAAA,CAAS,KAAK,CAAAP,eAAA,EAAkB,CACtEQ,kBAAA,EACA,KAAK,CAAAP,iBAAA,GAAqBO,kBAAA,CAC3B,CAAA;IAEDM,aAAA,CAAO,CAAC,CAACF,SAAA,EAAW,0BAA0B,CAAA;IAC9CE,aAAA,CAAO,CAAC,CAACD,kBAAA,EAAoB,oCAAoC,CAAA;IAGjE,IAAA,CAAK,CAAAb,eAAA,GAAmBa,kBAAA;IACxB,IAAA,CAAK,CAAAZ,iBAAA,IAAsBO,kBAAA;IAE3B,OAAO;MAAEG,YAAA,EAAcH,kBAAA;MAAoBJ,MAAA,EAAQQ;IAAA,CAAU;EAC/D;AACF;"}
@@ -1,38 +0,0 @@
1
- import { allocate } from 'dinero.js';
2
- import { assert } from '../../errors.mjs';
3
- import { getDinero } from '../misc.mjs';
4
- "use strict";
5
- class TotalAmountAllocator {
6
- #remainingAmount;
7
- #remainingQuantity;
8
- #currency;
9
- constructor({
10
- amount,
11
- quantity,
12
- currency
13
- }) {
14
- this.#remainingAmount = getDinero(amount, currency);
15
- this.#remainingQuantity = quantity;
16
- this.#currency = currency;
17
- }
18
- allocate(quantity) {
19
- const quantityToAllocate = Math.min(quantity, this.#remainingQuantity);
20
- if (quantityToAllocate === 0) {
21
- return {
22
- qtyAllocated: 0,
23
- amount: getDinero(0, this.#currency)
24
- };
25
- }
26
- const [allocated, newRemainingAmount] = allocate(this.#remainingAmount, [quantityToAllocate, this.#remainingQuantity - quantityToAllocate]);
27
- assert(!!allocated, "Couldn't allocate amount");
28
- assert(!!newRemainingAmount, "Couldn't retrieve remaining amount");
29
- this.#remainingAmount = newRemainingAmount;
30
- this.#remainingQuantity -= quantityToAllocate;
31
- return {
32
- qtyAllocated: quantityToAllocate,
33
- amount: allocated
34
- };
35
- }
36
- }
37
- export { TotalAmountAllocator };
38
- //# sourceMappingURL=totalAmount.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"totalAmount.mjs","sources":["../../../../src/utils/money/allocators/totalAmount.ts"],"sourcesContent":["import { allocate } from \"dinero.js\";\n\nimport type { Currency } from \"../../../types\";\nimport { assert } from \"../../errors\";\nimport { getDinero } from \"../misc\";\nimport type { Cents, Dino } from \"../types\";\n\n/**\n * Helper class to distribute a total amount across a quantity of items.\n */\nexport class TotalAmountAllocator {\n #remainingAmount: Dino;\n #remainingQuantity: number;\n #currency: Currency;\n\n constructor({\n amount,\n quantity,\n currency,\n }: {\n quantity: number;\n amount: Cents;\n currency: Currency;\n }) {\n this.#remainingAmount = getDinero(amount, currency);\n this.#remainingQuantity = quantity;\n this.#currency = currency;\n }\n\n allocate(quantity: number): { qtyAllocated: number; amount: Dino } {\n // Normalize quantity to not exceed remaining\n const quantityToAllocate = Math.min(quantity, this.#remainingQuantity);\n\n if (quantityToAllocate === 0) {\n return { qtyAllocated: 0, amount: getDinero(0, this.#currency) };\n }\n\n const [allocated, newRemainingAmount] = allocate(this.#remainingAmount, [\n quantityToAllocate,\n this.#remainingQuantity - quantityToAllocate,\n ]);\n\n assert(!!allocated, \"Couldn't allocate amount\");\n assert(!!newRemainingAmount, \"Couldn't retrieve remaining amount\");\n\n // Update remaining amounts\n this.#remainingAmount = newRemainingAmount;\n this.#remainingQuantity -= quantityToAllocate;\n\n return { qtyAllocated: quantityToAllocate, amount: allocated };\n }\n}\n"],"names":["TotalAmountAllocator","remainingAmount","remainingQuantity","currency","constructor","amount","quantity","getDinero","allocate","quantityToAllocate","Math","min","qtyAllocated","allocated","newRemainingAmount","assert"],"mappings":";;;;AAUO,MAAMA,oBAAA,CAAqB;EAChC,CAAAC,eAAA;EACA,CAAAC,iBAAA;EACA,CAAAC,QAAA;EAEAC,WAAAA,CAAY;IACVC,MAAA;IACAC,QAAA;IACAH;EAAA,CACF,EAIG;IACD,IAAA,CAAK,CAAAF,eAAA,GAAmBM,SAAA,CAAUF,MAAA,EAAQF,QAAQ,CAAA;IAClD,IAAA,CAAK,CAAAD,iBAAA,GAAqBI,QAAA;IAC1B,IAAA,CAAK,CAAAH,QAAA,GAAYA,QAAA;EACnB;EAEAK,SAASF,QAAA,EAA0D;IAEjE,MAAMG,kBAAA,GAAqBC,IAAA,CAAKC,GAAA,CAAIL,QAAA,EAAU,KAAK,CAAAJ,iBAAkB,CAAA;IAErE,IAAIO,uBAAuB,CAAA,EAAG;MAC5B,OAAO;QAAEG,cAAc,CAAA;QAAGP,MAAA,EAAQE,UAAU,CAAA,EAAG,IAAA,CAAK,CAAAJ,QAAS;MAAA,CAAE;IACjE;IAEA,MAAM,CAACU,SAAA,EAAWC,kBAAkB,CAAA,GAAIN,QAAA,CAAS,KAAK,CAAAP,eAAA,EAAkB,CACtEQ,kBAAA,EACA,KAAK,CAAAP,iBAAA,GAAqBO,kBAAA,CAC3B,CAAA;IAEDM,MAAA,CAAO,CAAC,CAACF,SAAA,EAAW,0BAA0B,CAAA;IAC9CE,MAAA,CAAO,CAAC,CAACD,kBAAA,EAAoB,oCAAoC,CAAA;IAGjE,IAAA,CAAK,CAAAb,eAAA,GAAmBa,kBAAA;IACxB,IAAA,CAAK,CAAAZ,iBAAA,IAAsBO,kBAAA;IAE3B,OAAO;MAAEG,YAAA,EAAcH,kBAAA;MAAoBJ,MAAA,EAAQQ;IAAA,CAAU;EAC/D;AACF;"}
File without changes
File without changes