From 9168b3b0e8be0afeb45a6c42534675a831b6e1c4 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 1 Sep 2023 14:57:12 +0530 Subject: [PATCH 1/6] fix: deduplicate gain/loss JE creation for journals as payment (cherry picked from commit 79c6f0165bc8db4c41173b88ea0c02326e108760) # Conflicts: # erpnext/controllers/accounts_controller.py --- erpnext/accounts/utils.py | 9 +- erpnext/controllers/accounts_controller.py | 107 ++++++++++++++++++--- 2 files changed, 98 insertions(+), 18 deletions(-) diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 0c01ff78c8c..2e9eca2e4a7 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -458,10 +458,12 @@ def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # n # update ref in advance entry if voucher_type == "Journal Entry": - update_reference_in_journal_entry(entry, doc, do_not_save=True) + referenced_row = update_reference_in_journal_entry(entry, doc, do_not_save=False) # advance section in sales/purchase invoice and reconciliation tool,both pass on exchange gain/loss # amount and account in args - doc.make_exchange_gain_loss_journal(args) + # referenced_row is used to deduplicate gain/loss journal + entry.update({"referenced_row": referenced_row}) + doc.make_exchange_gain_loss_journal([entry]) else: update_reference_in_payment_entry( entry, doc, do_not_save=True, skip_ref_details_update_for_pe=skip_ref_details_update_for_pe @@ -605,6 +607,8 @@ def update_reference_in_journal_entry(d, journal_entry, do_not_save=False): if not do_not_save: journal_entry.save(ignore_permissions=True) + return new_row.name + def update_reference_in_payment_entry( d, payment_entry, do_not_save=False, skip_ref_details_update_for_pe=False @@ -1871,6 +1875,7 @@ def create_gain_loss_journal( journal_entry.company = company journal_entry.posting_date = nowdate() journal_entry.multi_currency = 1 + journal_entry.is_system_generated = True party_account_currency = frappe.get_cached_value("Account", party_account, "account_currency") diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index b225e3d5f1e..c56c0a240c3 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -969,6 +969,74 @@ class AccountsController(TransactionBase): d.exchange_gain_loss = difference +<<<<<<< HEAD +======= + def make_precision_loss_gl_entry(self, gl_entries): + round_off_account, round_off_cost_center = get_round_off_account_and_cost_center( + self.company, "Purchase Invoice", self.name, self.use_company_roundoff_cost_center + ) + + precision_loss = self.get("base_net_total") - flt( + self.get("net_total") * self.conversion_rate, self.precision("net_total") + ) + + credit_or_debit = "credit" if self.doctype == "Purchase Invoice" else "debit" + against = self.supplier if self.doctype == "Purchase Invoice" else self.customer + + if precision_loss: + gl_entries.append( + self.get_gl_dict( + { + "account": round_off_account, + "against": against, + credit_or_debit: precision_loss, + "cost_center": round_off_cost_center + if self.use_company_roundoff_cost_center + else self.cost_center or round_off_cost_center, + "remarks": _("Net total calculation precision loss"), + } + ) + ) + + def gain_loss_journal_already_booked( + self, + gain_loss_account, + exc_gain_loss, + ref2_dt, + ref2_dn, + ref2_detail_no, + ) -> bool: + """ + Check if gain/loss is booked + """ + if res := frappe.db.get_all( + "Journal Entry Account", + filters={ + "docstatus": 1, + "account": gain_loss_account, + "reference_type": ref2_dt, # this will be Journal Entry + "reference_name": ref2_dn, + "reference_detail_no": ref2_detail_no, + }, + pluck="parent", + ): + # deduplicate + res = list({x for x in res}) + if exc_vouchers := frappe.db.get_all( + "Journal Entry", + filters={"name": ["in", res], "voucher_type": "Exchange Gain Or Loss"}, + fields=["voucher_type", "total_debit", "total_credit"], + ): + booked_voucher = exc_vouchers[0] + if ( + booked_voucher.total_debit == exc_gain_loss + and booked_voucher.total_credit == exc_gain_loss + and booked_voucher.voucher_type == "Exchange Gain Or Loss" + ): + return True + return False + +>>>>>>> 79c6f0165b (fix: deduplicate gain/loss JE creation for journals as payment) def make_exchange_gain_loss_journal(self, args: dict = None) -> None: """ Make Exchange Gain/Loss journal for Invoices and Payments @@ -997,27 +1065,34 @@ class AccountsController(TransactionBase): reverse_dr_or_cr = "debit" if dr_or_cr == "credit" else "credit" - je = create_gain_loss_journal( - self.company, - arg.get("party_type"), - arg.get("party"), - party_account, + if not self.gain_loss_journal_already_booked( gain_loss_account, difference_amount, - dr_or_cr, - reverse_dr_or_cr, - arg.get("against_voucher_type"), - arg.get("against_voucher"), - arg.get("idx"), self.doctype, self.name, - arg.get("idx"), - ) - frappe.msgprint( - _("Exchange Gain/Loss amount has been booked through {0}").format( - get_link_to_form("Journal Entry", je) + arg.get("referenced_row"), + ): + je = create_gain_loss_journal( + self.company, + arg.get("party_type"), + arg.get("party"), + party_account, + gain_loss_account, + difference_amount, + dr_or_cr, + reverse_dr_or_cr, + arg.get("against_voucher_type"), + arg.get("against_voucher"), + arg.get("idx"), + self.doctype, + self.name, + arg.get("referenced_row"), + ) + frappe.msgprint( + _("Exchange Gain/Loss amount has been booked through {0}").format( + get_link_to_form("Journal Entry", je) + ) ) - ) if self.get("doctype") == "Payment Entry": # For Payment Entry, exchange_gain_loss field in the `references` table is the trigger for journal creation From 475750302b97cdfeae05fdb5ab4d3144bf6860c0 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sat, 2 Sep 2023 13:28:51 +0530 Subject: [PATCH 2/6] test: deduplicate gain/loss JE on reconciling journals against inv (cherry picked from commit cb6da6ec590114711161b79a2dc52af7b4ccbdde) --- .../tests/test_accounts_controller.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/erpnext/controllers/tests/test_accounts_controller.py b/erpnext/controllers/tests/test_accounts_controller.py index 0f8e133e0fd..e4f741424c4 100644 --- a/erpnext/controllers/tests/test_accounts_controller.py +++ b/erpnext/controllers/tests/test_accounts_controller.py @@ -941,6 +941,51 @@ class TestAccountsController(FrappeTestCase): self.assertEqual(exc_je_for_si, []) self.assertEqual(exc_je_for_je, []) + def test_24_journal_against_multiple_invoices(self): + si1 = self.create_sales_invoice(qty=1, conversion_rate=80, rate=1) + si2 = self.create_sales_invoice(qty=1, conversion_rate=80, rate=1) + + # Payment + je = self.create_journal_entry( + acc1=self.debit_usd, + acc1_exc_rate=75, + acc2=self.cash, + acc1_amount=-2, + acc2_amount=-150, + acc2_exc_rate=1, + ) + je.accounts[0].party_type = "Customer" + je.accounts[0].party = self.customer + je = je.save().submit() + + pr = self.create_payment_reconciliation() + pr.get_unreconciled_entries() + self.assertEqual(len(pr.invoices), 2) + self.assertEqual(len(pr.payments), 1) + invoices = [x.as_dict() for x in pr.invoices] + payments = [x.as_dict() for x in pr.payments] + pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments})) + pr.reconcile() + self.assertEqual(len(pr.invoices), 0) + self.assertEqual(len(pr.payments), 0) + + si1.reload() + si2.reload() + + self.assertEqual(si1.outstanding_amount, 0) + self.assertEqual(si2.outstanding_amount, 0) + self.assert_ledger_outstanding(si1.doctype, si1.name, 0.0, 0.0) + self.assert_ledger_outstanding(si2.doctype, si2.name, 0.0, 0.0) + + # Exchange Gain/Loss Journal should've been cancelled + # remove payment JE from list + exc_je_for_si1 = [x for x in self.get_journals_for(si1.doctype, si1.name) if x.parent != je.name] + exc_je_for_si2 = [x for x in self.get_journals_for(si2.doctype, si2.name) if x.parent != je.name] + exc_je_for_je = [x for x in self.get_journals_for(je.doctype, je.name) if x.parent != je.name] + self.assertEqual(len(exc_je_for_si1), 1) + self.assertEqual(len(exc_je_for_si2), 1) + self.assertEqual(len(exc_je_for_je), 2) + def test_30_cr_note_against_sales_invoice(self): """ Reconciling Cr Note against Sales Invoice, both having different exchange rates From 7fd96d011647d16a5ecc477541a5a7d08247224a Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sat, 2 Sep 2023 14:43:25 +0530 Subject: [PATCH 3/6] test: extend test to cancellation (cherry picked from commit 79fa562004a0754d361509d0dd6149d316fa2640) --- erpnext/controllers/tests/test_accounts_controller.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/erpnext/controllers/tests/test_accounts_controller.py b/erpnext/controllers/tests/test_accounts_controller.py index e4f741424c4..0c112aeedd3 100644 --- a/erpnext/controllers/tests/test_accounts_controller.py +++ b/erpnext/controllers/tests/test_accounts_controller.py @@ -977,7 +977,7 @@ class TestAccountsController(FrappeTestCase): self.assert_ledger_outstanding(si1.doctype, si1.name, 0.0, 0.0) self.assert_ledger_outstanding(si2.doctype, si2.name, 0.0, 0.0) - # Exchange Gain/Loss Journal should've been cancelled + # Exchange Gain/Loss Journal should've been created # remove payment JE from list exc_je_for_si1 = [x for x in self.get_journals_for(si1.doctype, si1.name) if x.parent != je.name] exc_je_for_si2 = [x for x in self.get_journals_for(si2.doctype, si2.name) if x.parent != je.name] @@ -986,6 +986,15 @@ class TestAccountsController(FrappeTestCase): self.assertEqual(len(exc_je_for_si2), 1) self.assertEqual(len(exc_je_for_je), 2) + si1.cancel() + # Gain/Loss JE of si1 should've been cancelled + exc_je_for_si1 = [x for x in self.get_journals_for(si1.doctype, si1.name) if x.parent != je.name] + exc_je_for_si2 = [x for x in self.get_journals_for(si2.doctype, si2.name) if x.parent != je.name] + exc_je_for_je = [x for x in self.get_journals_for(je.doctype, je.name) if x.parent != je.name] + self.assertEqual(len(exc_je_for_si1), 0) + self.assertEqual(len(exc_je_for_si2), 1) + self.assertEqual(len(exc_je_for_je), 1) + def test_30_cr_note_against_sales_invoice(self): """ Reconciling Cr Note against Sales Invoice, both having different exchange rates From d24c8b1bbcf076960352fb906fec68f7981d1205 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sun, 3 Sep 2023 07:21:09 +0530 Subject: [PATCH 4/6] refactor: use payment's CC for gain/loss if company default is unset (cherry picked from commit d6a3b9a5c7306b71584594872b06349f76951cd4) # Conflicts: # erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py --- .../payment_reconciliation.py | 16 +++++++++++++++- .../payment_reconciliation_allocation.json | 11 +++++++++-- .../payment_reconciliation_payment.json | 11 +++++++++-- erpnext/accounts/utils.py | 7 +++++-- erpnext/controllers/accounts_controller.py | 2 ++ 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py index abffd262961..a19e4267fdb 100644 --- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py +++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py @@ -100,7 +100,7 @@ class PaymentReconciliation(Document): "Journal Entry" as reference_type, t1.name as reference_name, t1.posting_date, t1.remark as remarks, t2.name as reference_row, {dr_or_cr} as amount, t2.is_advance, t2.exchange_rate, - t2.account_currency as currency + t2.account_currency as currency, t2.cost_center as cost_center from `tabJournal Entry` t1, `tabJournal Entry Account` t2 where @@ -196,6 +196,7 @@ class PaymentReconciliation(Document): "amount": -(inv.outstanding_in_account_currency), "posting_date": inv.posting_date, "currency": inv.currency, + "cost_center": inv.cost_center, } ) ) @@ -344,6 +345,7 @@ class PaymentReconciliation(Document): "allocated_amount": allocated_amount, "difference_amount": pay.get("difference_amount"), "currency": inv.get("currency"), + "cost_center": pay.get("cost_center"), } ) @@ -418,6 +420,7 @@ class PaymentReconciliation(Document): "allocated_amount": flt(row.get("allocated_amount")), "difference_amount": flt(row.get("difference_amount")), "difference_account": row.get("difference_account"), + "cost_center": row.get("cost_center"), } ) @@ -590,7 +593,12 @@ def reconcile_dr_cr_note(dr_cr_notes, company): inv.dr_or_cr: abs(inv.allocated_amount), "reference_type": inv.against_voucher_type, "reference_name": inv.against_voucher, +<<<<<<< HEAD "cost_center": erpnext.get_default_cost_center(company), +======= + "cost_center": inv.cost_center or erpnext.get_default_cost_center(company), + "exchange_rate": inv.exchange_rate, +>>>>>>> d6a3b9a5c7 (refactor: use payment's CC for gain/loss if company default is unset) "user_remark": f"{fmt_money(flt(inv.allocated_amount), currency=company_currency)} against {inv.against_voucher}", "exchange_rate": inv.exchange_rate, }, @@ -605,7 +613,12 @@ def reconcile_dr_cr_note(dr_cr_notes, company): ), "reference_type": inv.voucher_type, "reference_name": inv.voucher_no, +<<<<<<< HEAD "cost_center": erpnext.get_default_cost_center(company), +======= + "cost_center": inv.cost_center or erpnext.get_default_cost_center(company), + "exchange_rate": inv.exchange_rate, +>>>>>>> d6a3b9a5c7 (refactor: use payment's CC for gain/loss if company default is unset) "user_remark": f"{fmt_money(flt(inv.allocated_amount), currency=company_currency)} from {inv.voucher_no}", "exchange_rate": inv.exchange_rate, }, @@ -644,4 +657,5 @@ def reconcile_dr_cr_note(dr_cr_notes, company): inv.against_voucher_type, inv.against_voucher, None, + inv.cost_center, ) diff --git a/erpnext/accounts/doctype/payment_reconciliation_allocation/payment_reconciliation_allocation.json b/erpnext/accounts/doctype/payment_reconciliation_allocation/payment_reconciliation_allocation.json index 0f7e47acfee..ec718aa70d3 100644 --- a/erpnext/accounts/doctype/payment_reconciliation_allocation/payment_reconciliation_allocation.json +++ b/erpnext/accounts/doctype/payment_reconciliation_allocation/payment_reconciliation_allocation.json @@ -22,7 +22,8 @@ "column_break_7", "difference_account", "exchange_rate", - "currency" + "currency", + "cost_center" ], "fields": [ { @@ -144,11 +145,17 @@ "fieldtype": "Float", "label": "Exchange Rate", "read_only": 1 + }, + { + "fieldname": "cost_center", + "fieldtype": "Link", + "label": "Cost Center", + "options": "Cost Center" } ], "istable": 1, "links": [], - "modified": "2022-12-24 21:01:14.882747", + "modified": "2023-09-03 07:52:33.684217", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Reconciliation Allocation", diff --git a/erpnext/accounts/doctype/payment_reconciliation_payment/payment_reconciliation_payment.json b/erpnext/accounts/doctype/payment_reconciliation_payment/payment_reconciliation_payment.json index d300ea97abc..17f3900880c 100644 --- a/erpnext/accounts/doctype/payment_reconciliation_payment/payment_reconciliation_payment.json +++ b/erpnext/accounts/doctype/payment_reconciliation_payment/payment_reconciliation_payment.json @@ -16,7 +16,8 @@ "sec_break1", "remark", "currency", - "exchange_rate" + "exchange_rate", + "cost_center" ], "fields": [ { @@ -98,11 +99,17 @@ "fieldtype": "Float", "hidden": 1, "label": "Exchange Rate" + }, + { + "fieldname": "cost_center", + "fieldtype": "Link", + "label": "Cost Center", + "options": "Cost Center" } ], "istable": 1, "links": [], - "modified": "2022-11-08 18:18:36.268760", + "modified": "2023-09-03 07:43:29.965353", "modified_by": "Administrator", "module": "Accounts", "name": "Payment Reconciliation Payment", diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 2e9eca2e4a7..267f22d119b 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -1724,6 +1724,7 @@ class QueryPaymentLedger(object): ple.posting_date, ple.due_date, ple.account_currency.as_("currency"), + ple.cost_center.as_("cost_center"), Sum(ple.amount).as_("amount"), Sum(ple.amount_in_account_currency).as_("amount_in_account_currency"), ) @@ -1786,6 +1787,7 @@ class QueryPaymentLedger(object): ).as_("paid_amount_in_account_currency"), Table("vouchers").due_date, Table("vouchers").currency, + Table("vouchers").cost_center.as_("cost_center"), ) .where(Criterion.all(filter_on_outstanding_amount)) ) @@ -1869,6 +1871,7 @@ def create_gain_loss_journal( ref2_dt, ref2_dn, ref2_detail_no, + cost_center, ) -> str: journal_entry = frappe.new_doc("Journal Entry") journal_entry.voucher_type = "Exchange Gain Or Loss" @@ -1894,7 +1897,7 @@ def create_gain_loss_journal( "party": party, "account_currency": party_account_currency, "exchange_rate": 0, - "cost_center": erpnext.get_default_cost_center(company), + "cost_center": cost_center or erpnext.get_default_cost_center(company), "reference_type": ref1_dt, "reference_name": ref1_dn, "reference_detail_no": ref1_detail_no, @@ -1910,7 +1913,7 @@ def create_gain_loss_journal( "account": gain_loss_account, "account_currency": gain_loss_account_currency, "exchange_rate": 1, - "cost_center": erpnext.get_default_cost_center(company), + "cost_center": cost_center or erpnext.get_default_cost_center(company), "reference_type": ref2_dt, "reference_name": ref2_dn, "reference_detail_no": ref2_detail_no, diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index c56c0a240c3..a319f1914e7 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -1087,6 +1087,7 @@ class AccountsController(TransactionBase): self.doctype, self.name, arg.get("referenced_row"), + arg.get("cost_center"), ) frappe.msgprint( _("Exchange Gain/Loss amount has been booked through {0}").format( @@ -1165,6 +1166,7 @@ class AccountsController(TransactionBase): self.doctype, self.name, d.idx, + self.cost_center, ) frappe.msgprint( _("Exchange Gain/Loss amount has been booked through {0}").format( From c8d81cc52d5da73491590a15ec0f18205fa96ee4 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sun, 3 Sep 2023 08:07:02 +0530 Subject: [PATCH 5/6] test: cost center inheritance from payment (cherry picked from commit 0366928db5737f6c22525485ed3a3c423c4815ec) --- .../tests/test_accounts_controller.py | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/erpnext/controllers/tests/test_accounts_controller.py b/erpnext/controllers/tests/test_accounts_controller.py index 0c112aeedd3..391258fde77 100644 --- a/erpnext/controllers/tests/test_accounts_controller.py +++ b/erpnext/controllers/tests/test_accounts_controller.py @@ -55,6 +55,7 @@ class TestAccountsController(FrappeTestCase): 10 series - Sales Invoice against Payment Entries 20 series - Sales Invoice against Journals 30 series - Sales Invoice against Credit Notes + 40 series - Company default Cost center is unset """ def setUp(self): @@ -1051,3 +1052,139 @@ class TestAccountsController(FrappeTestCase): si.reload() self.assertEqual(si.outstanding_amount, 1) self.assert_ledger_outstanding(si.doctype, si.name, 80.0, 1.0) + + def test_40_cost_center_from_payment_entry(self): + """ + Gain/Loss JE should inherit cost center from payment if company default is unset + """ + # remove default cost center + cc = frappe.db.get_value("Company", self.company, "cost_center") + frappe.db.set_value("Company", self.company, "cost_center", None) + + rate_in_account_currency = 1 + si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True) + si.cost_center = None + si.save().submit() + + pe = get_payment_entry(si.doctype, si.name) + pe.source_exchange_rate = 75 + pe.received_amount = 75 + pe.cost_center = self.cost_center + pe = pe.save().submit() + + # Exchange Gain/Loss Journal should've been created. + exc_je_for_si = self.get_journals_for(si.doctype, si.name) + exc_je_for_pe = self.get_journals_for(pe.doctype, pe.name) + self.assertNotEqual(exc_je_for_si, []) + self.assertEqual(len(exc_je_for_si), 1) + self.assertEqual(len(exc_je_for_pe), 1) + self.assertEqual(exc_je_for_si[0], exc_je_for_pe[0]) + + self.assertEqual( + [self.cost_center, self.cost_center], + frappe.db.get_all( + "Journal Entry Account", filters={"parent": exc_je_for_si[0].parent}, pluck="cost_center" + ), + ) + frappe.db.set_value("Company", self.company, "cost_center", cc) + + def test_41_cost_center_from_journal_entry(self): + """ + Gain/Loss JE should inherit cost center from payment if company default is unset + """ + # remove default cost center + cc = frappe.db.get_value("Company", self.company, "cost_center") + frappe.db.set_value("Company", self.company, "cost_center", None) + + rate_in_account_currency = 1 + si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True) + si.cost_center = None + si.save().submit() + + je = self.create_journal_entry( + acc1=self.debit_usd, + acc1_exc_rate=75, + acc2=self.cash, + acc1_amount=-1, + acc2_amount=-75, + acc2_exc_rate=1, + ) + je.accounts[0].party_type = "Customer" + je.accounts[0].party = self.customer + je.accounts[0].cost_center = self.cost_center + je = je.save().submit() + + # Reconcile + pr = self.create_payment_reconciliation() + pr.get_unreconciled_entries() + self.assertEqual(len(pr.invoices), 1) + self.assertEqual(len(pr.payments), 1) + invoices = [x.as_dict() for x in pr.invoices] + payments = [x.as_dict() for x in pr.payments] + pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments})) + pr.reconcile() + self.assertEqual(len(pr.invoices), 0) + self.assertEqual(len(pr.payments), 0) + + # Exchange Gain/Loss Journal should've been created. + exc_je_for_si = [x for x in self.get_journals_for(si.doctype, si.name) if x.parent != je.name] + exc_je_for_je = [x for x in self.get_journals_for(je.doctype, je.name) if x.parent != je.name] + self.assertNotEqual(exc_je_for_si, []) + self.assertEqual(len(exc_je_for_si), 1) + self.assertEqual(len(exc_je_for_je), 1) + self.assertEqual(exc_je_for_si[0], exc_je_for_je[0]) + + self.assertEqual( + [self.cost_center, self.cost_center], + frappe.db.get_all( + "Journal Entry Account", filters={"parent": exc_je_for_si[0].parent}, pluck="cost_center" + ), + ) + frappe.db.set_value("Company", self.company, "cost_center", cc) + + def test_42_cost_center_from_cr_note(self): + """ + Gain/Loss JE should inherit cost center from payment if company default is unset + """ + # remove default cost center + cc = frappe.db.get_value("Company", self.company, "cost_center") + frappe.db.set_value("Company", self.company, "cost_center", None) + + rate_in_account_currency = 1 + si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True) + si.cost_center = None + si.save().submit() + + cr_note = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True) + cr_note.cost_center = self.cost_center + cr_note.is_return = 1 + cr_note.save().submit() + + # Reconcile + pr = self.create_payment_reconciliation() + pr.get_unreconciled_entries() + self.assertEqual(len(pr.invoices), 1) + self.assertEqual(len(pr.payments), 1) + invoices = [x.as_dict() for x in pr.invoices] + payments = [x.as_dict() for x in pr.payments] + pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments})) + pr.reconcile() + self.assertEqual(len(pr.invoices), 0) + self.assertEqual(len(pr.payments), 0) + + # Exchange Gain/Loss Journal should've been created. + exc_je_for_si = self.get_journals_for(si.doctype, si.name) + exc_je_for_cr_note = self.get_journals_for(cr_note.doctype, cr_note.name) + self.assertNotEqual(exc_je_for_si, []) + self.assertEqual(len(exc_je_for_si), 2) + self.assertEqual(len(exc_je_for_cr_note), 2) + self.assertEqual(exc_je_for_si, exc_je_for_cr_note) + + for x in exc_je_for_si + exc_je_for_cr_note: + with self.subTest(x=x): + self.assertEqual( + [self.cost_center, self.cost_center], + frappe.db.get_all("Journal Entry Account", filters={"parent": x.parent}, pluck="cost_center"), + ) + + frappe.db.set_value("Company", self.company, "cost_center", cc) From 5523bc5081cfafaf1f17a7abfa1d7081d35ab88e Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sun, 3 Sep 2023 10:26:23 +0530 Subject: [PATCH 6/6] chore: resolve merge conflict --- .../payment_reconciliation.py | 10 ------- erpnext/controllers/accounts_controller.py | 30 ------------------- 2 files changed, 40 deletions(-) diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py index a19e4267fdb..e57c3259d1f 100644 --- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py +++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py @@ -593,12 +593,7 @@ def reconcile_dr_cr_note(dr_cr_notes, company): inv.dr_or_cr: abs(inv.allocated_amount), "reference_type": inv.against_voucher_type, "reference_name": inv.against_voucher, -<<<<<<< HEAD - "cost_center": erpnext.get_default_cost_center(company), -======= "cost_center": inv.cost_center or erpnext.get_default_cost_center(company), - "exchange_rate": inv.exchange_rate, ->>>>>>> d6a3b9a5c7 (refactor: use payment's CC for gain/loss if company default is unset) "user_remark": f"{fmt_money(flt(inv.allocated_amount), currency=company_currency)} against {inv.against_voucher}", "exchange_rate": inv.exchange_rate, }, @@ -613,12 +608,7 @@ def reconcile_dr_cr_note(dr_cr_notes, company): ), "reference_type": inv.voucher_type, "reference_name": inv.voucher_no, -<<<<<<< HEAD - "cost_center": erpnext.get_default_cost_center(company), -======= "cost_center": inv.cost_center or erpnext.get_default_cost_center(company), - "exchange_rate": inv.exchange_rate, ->>>>>>> d6a3b9a5c7 (refactor: use payment's CC for gain/loss if company default is unset) "user_remark": f"{fmt_money(flt(inv.allocated_amount), currency=company_currency)} from {inv.voucher_no}", "exchange_rate": inv.exchange_rate, }, diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index a319f1914e7..b476d0ffb48 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -969,35 +969,6 @@ class AccountsController(TransactionBase): d.exchange_gain_loss = difference -<<<<<<< HEAD -======= - def make_precision_loss_gl_entry(self, gl_entries): - round_off_account, round_off_cost_center = get_round_off_account_and_cost_center( - self.company, "Purchase Invoice", self.name, self.use_company_roundoff_cost_center - ) - - precision_loss = self.get("base_net_total") - flt( - self.get("net_total") * self.conversion_rate, self.precision("net_total") - ) - - credit_or_debit = "credit" if self.doctype == "Purchase Invoice" else "debit" - against = self.supplier if self.doctype == "Purchase Invoice" else self.customer - - if precision_loss: - gl_entries.append( - self.get_gl_dict( - { - "account": round_off_account, - "against": against, - credit_or_debit: precision_loss, - "cost_center": round_off_cost_center - if self.use_company_roundoff_cost_center - else self.cost_center or round_off_cost_center, - "remarks": _("Net total calculation precision loss"), - } - ) - ) - def gain_loss_journal_already_booked( self, gain_loss_account, @@ -1036,7 +1007,6 @@ class AccountsController(TransactionBase): return True return False ->>>>>>> 79c6f0165b (fix: deduplicate gain/loss JE creation for journals as payment) def make_exchange_gain_loss_journal(self, args: dict = None) -> None: """ Make Exchange Gain/Loss journal for Invoices and Payments