From 0658b503bde80f8e843bd479e15a0d15aec5229d Mon Sep 17 00:00:00 2001 From: Firman Ramdhani <33869609+firmanramdhani@users.noreply.github.com> Date: Wed, 7 Aug 2024 11:05:37 +0700 Subject: [PATCH] feat(SPG-656): allow input number on formula --- .../helpers/calculation-formula.helper.ts | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper.ts b/src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper.ts index e27a9b7..f83e7b0 100644 --- a/src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper.ts +++ b/src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper.ts @@ -9,7 +9,9 @@ export function calculateSalesFormula( throwError = false, ) { try { - let { value, variable, tax_datas } = mappingTaxes(taxes, formula, total); + const mapTaxes = mappingTaxes(taxes, formula, total); + let { value } = mapTaxes; + const { variable, tax_datas } = mapTaxes; const x1 = math.simplify(formula, variable).toString(); console.log('Formula ', x1); @@ -41,13 +43,10 @@ export function calculateProfitFormula( throwError = false, ) { try { - let { value, variable, tax_datas } = mappingTaxes( - taxes, - formula, - total, - profit_share, - ); - + const mapTaxes = mappingTaxes(taxes, formula, total, profit_share); + let { value } = mapTaxes; + const { variable, tax_datas } = mapTaxes; + console.log(formula, variable); const result = math.simplify(formula, variable).toString(); console.log(result, 'formula'); @@ -64,9 +63,9 @@ export function calculateProfitFormula( } function mappingTaxes(taxes, formula, total, profit_share = 0) { - let value = 0; + const value = 0; const variable = {}; - let tax_datas = []; + const tax_datas = []; const const_variable = ['profit_share', 'item_share', 'dpp']; const regex = /([a-zA-Z0-9_]+)/g; @@ -78,14 +77,17 @@ function mappingTaxes(taxes, formula, total, profit_share = 0) { for (const key of keys) { if (!const_variable.includes(key)) { const keyData = taxes.find((tax) => tax.name == key); - variable[key] = keyData.value / 100; - - tax_datas.push({ - tax_id: keyData.id, - tax_name: keyData.name, - tax_value: keyData.value, - tax_total_value: (keyData.value / 100) * Number(total), - }); + if (!keyData) { + variable[key] = key; + } else { + variable[key] = keyData.value / 100; + tax_datas.push({ + tax_id: keyData.id, + tax_name: keyData.name, + tax_value: keyData.value, + tax_total_value: (keyData.value / 100) * Number(total), + }); + } } else { switch (key) { case 'profit_share':