From 75b009e2504d0c349725431fc0d5e9042006e789 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 22 Nov 2020 04:51:53 +0000 Subject: [PATCH] fix(JW Import): Handle negative amounts properly --- .../doctype/jw_import/jw_import.py | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/justworks_erpnext/justworks_erpnext_connector/doctype/jw_import/jw_import.py b/justworks_erpnext/justworks_erpnext_connector/doctype/jw_import/jw_import.py index faee50d..f65fc9c 100644 --- a/justworks_erpnext/justworks_erpnext_connector/doctype/jw_import/jw_import.py +++ b/justworks_erpnext/justworks_erpnext_connector/doctype/jw_import/jw_import.py @@ -67,7 +67,7 @@ class JWImport(Document): if fieldname[-4:] == 'date': val = get_datetime(parse_date(val)).date() elif fieldname == 'amount': - val = re.sub('^[^.,\d]*', '', val) + val = re.sub('^[^-.,\d]*', '', val) temp[fieldname] = val if (si_date and temp['invoice_date'] < si_date): continue @@ -383,6 +383,9 @@ def record_entries(doc): amnt = int(rec_amt*share.percentage)/100.0 if s == 0: amnt = rec_amt - so_far so_far += amnt + cred = 0.0; deb = amnt; + if (amnt < 0.0): + cred = -amnt; deb = 0.0; if importer.dim_balance_account: imb_key = *((dim,share.get(dim)) for dim in rebalance_dims), @@ -394,8 +397,8 @@ def record_entries(doc): party_type=rec.reference_doctype, party=rec.reference_doc, account_currency=importer.justworks_currency, - debit_in_account_currency=amnt, - credit_in_account_currency=0.0, + debit_in_account_currency=deb, + credit_in_account_currency=cred, user_remark=jea_remarks)) rec.set('disposition', 'Duplicate') # Should we emit a journal entry? @@ -419,8 +422,8 @@ def record_entries(doc): # Now generate "other sides": if importer.dim_balance_account: for dimvec in current_dim_imbalance: - if not current_dim_imbalance[dimvec] > 0.0: - continue + amnt = current_dim_imbalance[dimvec] + if amnt == 0.0: continue jea = je.append('accounts', {}) jea.set('account', importer.dim_balance_account) @@ -428,9 +431,10 @@ def record_entries(doc): jea.set(dim, val) jea.set('account_currency', importer.justworks_currency) - jea.set('debit_in_account_currency', 0.0) + jea.set('debit_in_account_currency', + 0.0 if amnt > 0.0 else -amnt) jea.set('credit_in_account_currency', - current_dim_imbalance[dimvec]) + amnt if amnt > 0.0 else 0.0) jea.set('user_remark', standard_remark) oja = je.append('accounts', {}) oja.set('account', @@ -440,16 +444,19 @@ def record_entries(doc): oja.set('account_currency', importer.justworks_currency) oja.set('debit_in_account_currency', - current_dim_imbalance[dimvec]) - oja.set('credit_in_account_currency', 0.0) + amnt if amnt > 0.0 else 0.0) + oja.set('credit_in_account_currency', + 0.0 if amnt > 0.0 else -amnt) oja.set('user_remark', standard_remark) bja = je.append('accounts', {}) bja.set('account', importer.jw_obligation_account) for (dim,val) in standard_dims.items(): bja.set(dim,val) bja.set('account_currency', importer.justworks_currency) - bja.set('debit_in_account_currency', 0.0) - bja.set('credit_in_account_currency', current_total) + bja.set('debit_in_account_currency', + 0.0 if current_total > 0.0 else -current_total) + bja.set('credit_in_account_currency', + current_total if current_total > 0.0 else 0.0) bja.set('user_remark', 'Total Justworks expenses') je.insert() if importer.auto_submit: je.submit()