@bookipi/htmlgenerator 1.14.1-alpha.5 → 1.14.1-alpha.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.
@@ -11973,7 +11973,8 @@ var DEFAULT_CONFIG = {
11973
11973
 
11974
11974
  /** French e-invoice rules; other French documents use the default config. */
11975
11975
  var FRANCE_E_INVOICE_CONFIG = {
11976
- // Our server rejects a negative subtotal before the PDP; Factur-X lines are clamped to 0.
11976
+ // Our server rejects a negative subtotal before the PDP, so a line discount stops at 0. A
11977
+ // negative price stays a negative line; Factur-X writes it as a negative quantity (BR-27).
11977
11978
  allowNegativeLines: false,
11978
11979
  // Factur-X has no after-tax discount, so the app hides "After-tax" on French e-invoices.
11979
11980
  allowDiscountAfterTax: false,
@@ -12059,7 +12060,9 @@ var getCountryConfig = function getCountryConfig(countryCode, _temp) {
12059
12060
  var code = (countryCode || "").toUpperCase();
12060
12061
  return _extends$1({}, DEFAULT_CONFIG, COUNTRY_CONFIGS[code] && _extends$1({
12061
12062
  countryCode: code
12062
- }, COUNTRY_CONFIGS[code]), eInvoice && E_INVOICE_CONFIGS[code]);
12063
+ }, COUNTRY_CONFIGS[code]), eInvoice && E_INVOICE_CONFIGS[code] && _extends$1({
12064
+ countryCode: code
12065
+ }, E_INVOICE_CONFIGS[code]));
12063
12066
  };
12064
12067
 
12065
12068
  /** E-invoice tax codes per country; a country listed here taxes its e-invoices by code. */
@@ -13864,9 +13867,10 @@ var currencies = {
13864
13867
 
13865
13868
  var currencyTable = currencies;
13866
13869
 
13867
- /** The code when it is a known currency, otherwise USD. */
13870
+ /** The upper-case code when it is a known currency, otherwise USD. */
13868
13871
  var getCurrencyCode = function getCurrencyCode(currencyCode) {
13869
- return currencyCode && currencyTable[currencyCode] ? currencyCode : "USD";
13872
+ var code = (currencyCode || "").toUpperCase();
13873
+ return currencyTable[code] ? code : "USD";
13870
13874
  };
13871
13875
 
13872
13876
  /** Digits after the decimal point. Unknown codes use USD. */
@@ -13982,14 +13986,29 @@ var calculateAdjustment = function calculateAdjustment(adjustment, base) {
13982
13986
  };
13983
13987
 
13984
13988
  /**
13985
- * Splits `amount` across `weights` pro rata; no share exceeds its weight, and weights ≤ 0 get none.
13989
+ * Splits `amount` across `weights` pro rata; no share exceeds its weight, and weights of the
13990
+ * other sign (or 0) get none. A negative amount (a discount on a negative document) is split
13991
+ * over the negative weights the same way.
13986
13992
  * With `decimals`, shares are rounded so they still add up exactly (largest remainder method).
13987
13993
  */
13988
- var allocateProportionally = function allocateProportionally(amount, weights, decimals) {
13994
+ var _allocateProportionally = function allocateProportionally(amount, weights, decimals) {
13995
+ if (amount.lt(0)) {
13996
+ var _allocateProportional = _allocateProportionally(amount.neg(), weights.map(function (weight) {
13997
+ return weight.neg();
13998
+ }), decimals),
13999
+ shares = _allocateProportional.shares,
14000
+ excess = _allocateProportional.excess;
14001
+ return {
14002
+ shares: shares.map(function (share) {
14003
+ return share.neg();
14004
+ }),
14005
+ excess: excess.neg()
14006
+ };
14007
+ }
13989
14008
  var noShares = weights.map(function () {
13990
14009
  return ZERO;
13991
14010
  });
13992
- if (amount.lte(0)) return {
14011
+ if (amount.eq(0)) return {
13993
14012
  shares: noShares,
13994
14013
  excess: ZERO
13995
14014
  };
@@ -14057,10 +14076,11 @@ var allocateDocumentDiscount = function allocateDocumentDiscount(_ref) {
14057
14076
  shipping = _ref.shipping,
14058
14077
  decimals = _ref.decimals;
14059
14078
  var taxableDiscount = documentDiscount != null && documentDiscount.afterTax ? ZERO : discount;
14060
- var _allocateProportional = allocateProportionally(taxableDiscount, lineAmounts, decimals),
14079
+ var _allocateProportional = _allocateProportionally(taxableDiscount, lineAmounts, decimals),
14061
14080
  shares = _allocateProportional.shares,
14062
14081
  excess = _allocateProportional.excess;
14063
- var shippingShare = shipping.gt(0) ? excess.gt(shipping) ? shipping : excess : ZERO;
14082
+ // Only a positive discount left over lowers a positive shipping cost.
14083
+ var shippingShare = shipping.gt(0) && excess.gt(0) ? excess.gt(shipping) ? shipping : excess : ZERO;
14064
14084
  return {
14065
14085
  lineShares: shares,
14066
14086
  shippingShare: shippingShare
@@ -14090,6 +14110,7 @@ var calculatePaymentSummary = function calculatePaymentSummary(_ref) {
14090
14110
  total = _ref.total,
14091
14111
  decimals = _ref.decimals,
14092
14112
  isInvoice = _ref.isInvoice,
14113
+ hasLines = _ref.hasLines,
14093
14114
  round = _ref.round;
14094
14115
  var surchargeRate;
14095
14116
  var totalPayment = round(sumMonetaryDecimals(payments.map(function (payment) {
@@ -14106,9 +14127,9 @@ var calculatePaymentSummary = function calculatePaymentSummary(_ref) {
14106
14127
  var amountAfterPayment = amountBeforePayment.minus(totalPayment);
14107
14128
 
14108
14129
  // Within one minor unit counts as fully paid, matching the legacy +0.01 tolerance.
14109
- // No payment counts as not paid.
14130
+ // Nothing owed (e.g. a 100% discount) is paid; an empty document with no payment is not.
14110
14131
  var tolerance = new MonetaryDecimal(10).pow(-decimals);
14111
- var isPaid = totalPayment.gt(0) && totalPayment.plus(tolerance).gte(amountBeforePayment);
14132
+ var isPaid = (totalPayment.gt(0) || hasLines && amountBeforePayment.lte(0)) && totalPayment.plus(tolerance).gte(amountBeforePayment);
14112
14133
  var paymentStatus = isPaid ? 2 : totalPayment.eq(0) ? 0 : 1;
14113
14134
  return {
14114
14135
  totalPayment: totalPayment,
@@ -14399,6 +14420,7 @@ var calculate = function calculate(document, options) {
14399
14420
  total: total,
14400
14421
  decimals: decimals,
14401
14422
  isInvoice: !!document.isInvoice,
14423
+ hasLines: lineItems.length > 0,
14402
14424
  round: round
14403
14425
  });
14404
14426
  return {
@@ -14762,21 +14784,43 @@ var calculateEN16931Totals = function calculateEN16931Totals(document, company)
14762
14784
  if (eInvoiceStandard !== "en16931") {
14763
14785
  throw new Error("calculateEN16931Totals: \"" + (countryCode || "") + "\" has no EN 16931 e-invoice.");
14764
14786
  }
14787
+ var taxCodes = getTaxCodes(countryCode);
14788
+ var findTaxCode = function findTaxCode(code) {
14789
+ return taxCodes.find(function (taxCode) {
14790
+ return taxCode.code === code;
14791
+ });
14792
+ };
14793
+ // Every amount needs a listed code: an uncoded line has no VAT breakdown, and an unlisted
14794
+ // code has no VAT category, so the totals would not reconcile. Untaxed shipping keeps no
14795
+ // code (toCalcDocument drops it; it is reported as untaxedChargeTaxCode), so it is not checked.
14796
+ var lineItems = readField(document.it, document.items) || [];
14797
+ var shippingNonTaxable = !!readField(document.sntx, document.shippingNonTax);
14798
+ var shippingTaxCode = shippingNonTaxable ? null : document.shippingTaxCode;
14799
+ var unlistedCodes = [].concat(lineItems.map(function (lineItem) {
14800
+ return lineItem.taxCode;
14801
+ }), shippingTaxCode || !shippingNonTaxable && toMonetaryDecimal(readField(document.sp, document.shippingCost)).gt(0) ? [shippingTaxCode] : []).filter(function (code) {
14802
+ return !findTaxCode(code);
14803
+ });
14804
+ if (unlistedCodes.length > 0) {
14805
+ throw new Error("calculateEN16931Totals: every line needs a \"" + countryCode + "\" tax code; got " + unlistedCodes.map(function (code) {
14806
+ return JSON.stringify(code != null ? code : null);
14807
+ }).join(", ") + ".");
14808
+ }
14765
14809
  var _calculateDocumentWit = calculateDocumentWith(document, company, {}, {
14766
14810
  eInvoice: true,
14767
14811
  taxCodeRates: getTaxCodeRates(countryCode)
14768
14812
  }),
14769
14813
  calculation = _calculateDocumentWit.result;
14814
+ // A discount past the items and shipping leaves a negative total on zero tax bases, which
14815
+ // cannot reconcile (BT-112 = BT-109 + BT-110); that is a credit note, not an invoice.
14816
+ var discountable = new MonetaryDecimal(calculation.subtotal).plus(calculation.shipping);
14817
+ if (new MonetaryDecimal(calculation.discount).gt(discountable)) {
14818
+ throw new Error("calculateEN16931Totals: the discount (" + calculation.discount + ") is more than the items and shipping (" + discountable.toNumber() + ").");
14819
+ }
14770
14820
  var decimals = calculation.decimals;
14771
14821
  var round = function round(amount) {
14772
14822
  return roundMonetaryDecimal(amount, decimals, calculation.config.roundingMethod);
14773
14823
  };
14774
- var taxCodes = getTaxCodes(countryCode);
14775
- var findTaxCode = function findTaxCode(code) {
14776
- return taxCodes.find(function (taxCode) {
14777
- return taxCode.code === code;
14778
- });
14779
- };
14780
14824
 
14781
14825
  // EN 16931 needs the line nets to add up to their tax code's basis to the cent.
14782
14826
  var lineNets = calculation.lines.map(function (line) {
@@ -14851,15 +14895,15 @@ var calculateEN16931Totals = function calculateEN16931Totals(document, company)
14851
14895
  return {
14852
14896
  currency: calculation.currency,
14853
14897
  decimals: decimals,
14898
+ // Every line has exactly one tax: its code, checked above.
14854
14899
  lines: calculation.lines.map(function (line) {
14855
- var _line$taxes$, _line$taxes$0$rate, _line$taxes$2, _line$taxes$3;
14856
14900
  return {
14857
14901
  index: line.index,
14858
14902
  net: lineNets[line.index].toNumber(),
14859
14903
  documentDiscount: line.documentDiscount,
14860
- taxCode: (_line$taxes$ = line.taxes[0]) == null ? void 0 : _line$taxes$.code,
14861
- rate: (_line$taxes$0$rate = (_line$taxes$2 = line.taxes[0]) == null ? void 0 : _line$taxes$2.rate) != null ? _line$taxes$0$rate : 0,
14862
- inclusive: !!((_line$taxes$3 = line.taxes[0]) != null && _line$taxes$3.inclusive)
14904
+ taxCode: line.taxes[0].code,
14905
+ rate: line.taxes[0].rate,
14906
+ inclusive: line.taxes[0].inclusive
14863
14907
  };
14864
14908
  }),
14865
14909
  shipping: shipping,
@@ -14918,11 +14962,13 @@ var calculateProposal = function calculateProposal(proposal, company) {
14918
14962
  company = {};
14919
14963
  }
14920
14964
  var decimals = getCurrencyDecimals(company.cr || company.currency);
14965
+ var _getCountryConfig = getCountryConfig(company.co || company.country),
14966
+ roundingMethod = _getCountryConfig.roundingMethod;
14921
14967
  var serviceItems = proposal.serviceItems,
14922
14968
  proposalFields = _objectWithoutPropertiesLoose(proposal, _excluded$1);
14923
14969
  var lineItems = (serviceItems || []).map(function (serviceItem) {
14924
14970
  var discountPercentage = toMonetaryDecimal(serviceItem.discountPercentage);
14925
- var discount = discountPercentage.gt(0) ? roundMonetaryDecimal(toMonetaryDecimal(serviceItem.price).times(toMonetaryDecimal(isBlank(serviceItem.quantity) ? 1 : serviceItem.quantity)).times(discountPercentage).div(100), decimals, "half-up").toNumber() : serviceItem.discount;
14971
+ var discount = discountPercentage.gt(0) ? roundMonetaryDecimal(toMonetaryDecimal(serviceItem.price).times(toMonetaryDecimal(isBlank(serviceItem.quantity) ? 1 : serviceItem.quantity)).times(discountPercentage).div(100), decimals, roundingMethod).toNumber() : serviceItem.discount;
14926
14972
  return {
14927
14973
  price: serviceItem.price,
14928
14974
  quantity: serviceItem.quantity,