From 714a432c1c1fc5d61f3480600f6513c146231fa9 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 12 Aug 2024 11:29:39 +0530 Subject: [PATCH 1/6] fix: ledger entries for pos return with update outstanding for self (cherry picked from commit 2cd9b28e5bbf376a8d57f61486e37f1fe88d068a) --- erpnext/accounts/doctype/sales_invoice/sales_invoice.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index a254c917189..9dd62d76fef 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -1294,6 +1294,10 @@ class SalesInvoice(SellingController): if skip_change_gl_entries and payment_mode.account == self.account_for_change_amount: payment_mode.base_amount -= flt(self.change_amount) + against_voucher = self.name + if self.is_return and self.return_against and not self.update_outstanding_for_self: + against_voucher = self.return_against + if payment_mode.base_amount: # POS, make payment entries gl_entries.append( @@ -1307,7 +1311,7 @@ class SalesInvoice(SellingController): "credit_in_account_currency": payment_mode.base_amount if self.party_account_currency == self.company_currency else payment_mode.amount, - "against_voucher": self.name, + "against_voucher": against_voucher, "against_voucher_type": self.doctype, "cost_center": self.cost_center, }, From 54735469c1ef47eb0eeba3e01faa22f97ad65319 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 12 Aug 2024 13:15:57 +0530 Subject: [PATCH 2/6] fix: patch to fix incorrect against_voucher references in ledger (cherry picked from commit 487d0a55f551a9b92ad662eed9ba94521e97766e) # Conflicts: # erpnext/patches/v14_0/update_pos_return_ledger_entries.py --- .../v14_0/update_pos_return_ledger_entries.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 erpnext/patches/v14_0/update_pos_return_ledger_entries.py diff --git a/erpnext/patches/v14_0/update_pos_return_ledger_entries.py b/erpnext/patches/v14_0/update_pos_return_ledger_entries.py new file mode 100644 index 00000000000..60e867d1bcb --- /dev/null +++ b/erpnext/patches/v14_0/update_pos_return_ledger_entries.py @@ -0,0 +1,61 @@ +import frappe +from frappe import qb + + +def execute(): + sinv = qb.DocType("Sales Invoice") + pos_returns_without_self = ( + qb.from_(sinv) + .select(sinv.name) + .where( + sinv.docstatus.eq(1) + & sinv.is_pos.eq(1) + & sinv.is_return.eq(1) + & sinv.return_against.notnull() + & sinv.update_outstanding_for_self.eq(0) + ) + .run() + ) + if pos_returns_without_self: + pos_returns_without_self = [x[0] for x in pos_returns_without_self] + + gle = qb.DocType("GL Entry") + gl_against_references = ( + qb.from_(gle) + .select(gle.voucher_no, gle.against_voucher) + .where( + gle.voucher_no.isin(pos_returns_without_self) + & gle.against_voucher.notnull() + & gle.against_voucher.eq(gle.voucher_no) + & gle.is_cancelled.eq(0) + ) + .run() + ) + + _vouchers = list(set([x[0] for x in gl_against_references])) + invoice_return_against = ( + qb.from_(sinv) + .select(sinv.name, sinv.return_against) + .where(sinv.name.isin(_vouchers) & sinv.return_against.notnull()) + .orderby(sinv.name) + .run() + ) + + valid_references = set(invoice_return_against) + actual_references = set(gl_against_references) + + invalid_references = actual_references.difference(valid_references) + + if invalid_references: + # Repost Accounting Ledger + pos_for_reposting = ( + qb.from_(sinv) + .select(sinv.company, sinv.name) + .where(sinv.name.isin([x[0] for x in invalid_references])) + .run(as_dict=True) + ) + for x in pos_for_reposting: + ral = frappe.new_doc("Repost Accounting Ledger") + ral.company = x.company + ral.append("vouchers", {"voucher_type": "Sales Invoice", "voucher_no": x.name}) + ral.save().submit() From fa44b0d745125b25802a0e52a4615f509eb84014 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Mon, 12 Aug 2024 13:58:04 +0530 Subject: [PATCH 3/6] refactor: update patches.txt (cherry picked from commit 4dc0d3a003720f6bd1f6d87e5a3540d4a03a2fa9) # Conflicts: # erpnext/patches.txt --- erpnext/patches.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 1f6363d56cb..b5266c56a54 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -357,6 +357,11 @@ erpnext.patches.v14_0.clear_reconciliation_values_from_singles erpnext.patches.v14_0.update_total_asset_cost_field erpnext.patches.v14_0.create_accounting_dimensions_in_reconciliation_tool erpnext.patches.v14_0.update_flag_for_return_invoices #2024-03-22 +<<<<<<< HEAD +======= +erpnext.patches.v15_0.create_accounting_dimensions_in_payment_request +erpnext.patches.v15_0.update_pos_return_ledger_entries +>>>>>>> 4dc0d3a003 (refactor: update patches.txt) # below migration patch should always run last erpnext.patches.v14_0.migrate_gl_to_payment_ledger erpnext.stock.doctype.delivery_note.patches.drop_unused_return_against_index # 2023-12-20 From aaa6d666669c1ceb544967969339158ca4f84be6 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Wed, 14 Aug 2024 11:32:26 +0530 Subject: [PATCH 4/6] test: against_voucher for pos_returns without updating for self (cherry picked from commit 3fb08583218fe9890ab2703292a327fea047ee97) --- .../sales_invoice/test_sales_invoice.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index dd61a594706..bb51223db16 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -5,6 +5,7 @@ import copy import json import frappe +from frappe import qb from frappe.model.dynamic_links import get_dynamic_link_map from frappe.model.naming import make_autoname from frappe.tests.utils import FrappeTestCase, change_settings @@ -3590,6 +3591,40 @@ class TestSalesInvoice(FrappeTestCase): ] self.assertEqual(expected, actual) + def test_pos_returns_without_update_outstanding_for_self(self): + from erpnext.accounts.doctype.sales_invoice.sales_invoice import make_sales_return + + pos_profile = make_pos_profile() + pos_profile.payments = [] + pos_profile.append("payments", {"default": 1, "mode_of_payment": "Cash"}) + pos_profile.save() + + pos = create_sales_invoice(qty=10, do_not_save=True) + pos.is_pos = 1 + pos.pos_profile = pos_profile.name + pos.append( + "payments", {"mode_of_payment": "Bank Draft", "account": "_Test Bank - _TC", "amount": 500} + ) + pos.append("payments", {"mode_of_payment": "Cash", "account": "Cash - _TC", "amount": 500}) + pos.save().submit() + + pos_return = make_sales_return(pos.name) + pos_return.update_outstanding_for_self = False + pos_return.save().submit() + + gle = qb.DocType("GL Entry") + res = ( + qb.from_(gle) + .select(gle.against_voucher) + .distinct() + .where( + gle.is_cancelled.eq(0) & gle.voucher_no.eq(pos_return.name) & gle.against_voucher.notnull() + ) + .run(as_list=1) + ) + self.assertEqual(len(res), 1) + self.assertEqual(res[0][0], pos_return.return_against) + def check_gl_entries(doc, voucher_no, expected_gle, posting_date): gl_entries = frappe.db.sql( From 1cddb4ff3998cb323e926f4c128d39c7a3661348 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Wed, 14 Aug 2024 11:35:05 +0530 Subject: [PATCH 5/6] refactor: move patch to v14 and update patches.txt (cherry picked from commit da2286802a1216bffe3a7d832340124eb091be4b) # Conflicts: # erpnext/patches.txt --- erpnext/patches.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index b5266c56a54..b8a08a363cc 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -360,8 +360,12 @@ erpnext.patches.v14_0.update_flag_for_return_invoices #2024-03-22 <<<<<<< HEAD ======= erpnext.patches.v15_0.create_accounting_dimensions_in_payment_request +<<<<<<< HEAD erpnext.patches.v15_0.update_pos_return_ledger_entries >>>>>>> 4dc0d3a003 (refactor: update patches.txt) +======= +erpnext.patches.v14_0.update_pos_return_ledger_entries +>>>>>>> da2286802a (refactor: move patch to v14 and update patches.txt) # below migration patch should always run last erpnext.patches.v14_0.migrate_gl_to_payment_ledger erpnext.stock.doctype.delivery_note.patches.drop_unused_return_against_index # 2023-12-20 From 8882b85888be1c62a0a05a2a0691dd2d66c81e47 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Wed, 14 Aug 2024 12:04:30 +0530 Subject: [PATCH 6/6] chore: resolve conflict --- erpnext/patches.txt | 8 -------- 1 file changed, 8 deletions(-) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index b8a08a363cc..3e17a07c5c1 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -357,15 +357,7 @@ erpnext.patches.v14_0.clear_reconciliation_values_from_singles erpnext.patches.v14_0.update_total_asset_cost_field erpnext.patches.v14_0.create_accounting_dimensions_in_reconciliation_tool erpnext.patches.v14_0.update_flag_for_return_invoices #2024-03-22 -<<<<<<< HEAD -======= -erpnext.patches.v15_0.create_accounting_dimensions_in_payment_request -<<<<<<< HEAD -erpnext.patches.v15_0.update_pos_return_ledger_entries ->>>>>>> 4dc0d3a003 (refactor: update patches.txt) -======= erpnext.patches.v14_0.update_pos_return_ledger_entries ->>>>>>> da2286802a (refactor: move patch to v14 and update patches.txt) # below migration patch should always run last erpnext.patches.v14_0.migrate_gl_to_payment_ledger erpnext.stock.doctype.delivery_note.patches.drop_unused_return_against_index # 2023-12-20