From 8530a28c62e935b735932767d669374db7d5b9e9 Mon Sep 17 00:00:00 2001 From: Gursheen Anand Date: Tue, 18 Jul 2023 15:51:01 +0530 Subject: [PATCH] test: PI offsetting entry for accounting dimension (cherry picked from commit 77deac4fb904a6a727afbd0eb174c2abbaed8834) # Conflicts: # erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py --- .../purchase_invoice/test_purchase_invoice.py | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index f60c83dcf5cc..df371c9ef771 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -1725,10 +1725,108 @@ def test_gl_entries_for_standalone_debit_note(self): rate = flt(sle.stock_value_difference) / flt(sle.actual_qty) self.assertAlmostEqual(returned_inv.items[0].rate, rate) +<<<<<<< HEAD def test_payment_allocation_for_payment_terms(self): from erpnext.buying.doctype.purchase_order.test_purchase_order import ( create_pr_against_po, create_purchase_order, +======= + def test_offsetting_entries_for_accounting_dimensions(self): + from erpnext.accounts.doctype.account.test_account import create_account + + create_account( + account_name="Offsetting", + company="_Test Company", + parent_account="Temporary Accounts - _TC", + ) + + clear_dimension_defaults("Branch") + accounting_dimension = frappe.get_doc("Accounting Dimension", "Branch") + accounting_dimension.disabled = 0 + accounting_dimension.append( + "dimension_defaults", + { + "company": "_Test Company", + "automatically_post_balancing_accounting_entry": 1, + "offsetting_account": "Offsetting - _TC", + }, + ) + accounting_dimension.save() + + branch1 = frappe.new_doc("Branch") + branch1.branch = "Location 1" + branch1.insert(ignore_if_duplicate=True) + branch2 = frappe.new_doc("Branch") + branch2.branch = "Location 2" + branch2.insert(ignore_if_duplicate=True) + + pi = make_purchase_invoice( + company="_Test Company", + customer="_Test Supplier", + do_not_save=True, + do_not_submit=True, + rate=1000, + price_list_rate=1000, + qty=1, + ) + pi.branch = branch1.branch + pi.items[0].branch = branch2.branch + pi.save() + pi.submit() + + expected_gle = [ + ["_Test Account Cost for Goods Sold - _TC", 1000, 0.0, nowdate(), {"branch": branch2.branch}], + ["Creditors - _TC", 0.0, 1000, nowdate(), {"branch": branch1.branch}], + ["Offsetting - _TC", 1000, 0.0, nowdate(), {"branch": branch1.branch}], + ["Offsetting - _TC", 0.0, 1000, nowdate(), {"branch": branch2.branch}], + ] + + check_gl_entries( + self, + pi.name, + expected_gle, + nowdate(), + voucher_type="Purchase Invoice", + check_acc_dimensions=True, + ) + clear_dimension_defaults("Branch") + + +def clear_dimension_defaults(dimension_name): + accounting_dimension = frappe.get_doc("Accounting Dimension", dimension_name) + accounting_dimension.dimension_defaults = [] + accounting_dimension.save() + + +def set_advance_flag(company, flag, default_account): + frappe.db.set_value( + "Company", + company, + { + "book_advance_payments_in_separate_party_account": flag, + "default_advance_paid_account": default_account, + }, + ) + + +def check_gl_entries( + doc, + voucher_no, + expected_gle, + posting_date, + voucher_type="Purchase Invoice", + check_acc_dimensions=False, +): + gl = frappe.qb.DocType("GL Entry") + query = ( + frappe.qb.from_(gl) + .select(gl.account, gl.debit, gl.credit, gl.posting_date) + .where( + (gl.voucher_type == voucher_type) + & (gl.voucher_no == voucher_no) + & (gl.posting_date >= posting_date) + & (gl.is_cancelled == 0) +>>>>>>> 77deac4fb9 (test: PI offsetting entry for accounting dimension) ) from erpnext.selling.doctype.sales_order.test_sales_order import ( automatically_fetch_payment_terms, @@ -1781,12 +1879,22 @@ def check_gl_entries(doc, voucher_no, expected_gle, posting_date): (voucher_no, posting_date), as_dict=1, ) +<<<<<<< HEAD +======= + if check_acc_dimensions: + for col in list(expected_gle[0][4].keys()): + query = query.select(col) + gl_entries = query.run(as_dict=True) +>>>>>>> 77deac4fb9 (test: PI offsetting entry for accounting dimension) for i, gle in enumerate(gl_entries): doc.assertEqual(expected_gle[i][0], gle.account) doc.assertEqual(expected_gle[i][1], gle.debit) doc.assertEqual(expected_gle[i][2], gle.credit) doc.assertEqual(getdate(expected_gle[i][3]), gle.posting_date) + if check_acc_dimensions: + for acc_dimension in expected_gle[i][4]: + doc.assertEqual(expected_gle[i][4][acc_dimension], gle[acc_dimension]) def create_tax_witholding_category(category_name, company, account):