From 95093ba7cdd2f8a5f5ad9bd6695e3e4436d6e20a Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Tue, 12 Mar 2024 16:05:50 +0530 Subject: [PATCH] chore: patch for updating flag in Cr/Dr notes (cherry picked from commit 849f47889498bb8c6cbf237dad711fbc0475ef93) # Conflicts: # erpnext/patches.txt --- erpnext/patches.txt | 4 ++ .../v14_0/update_flag_for_return_invoices.py | 62 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 erpnext/patches/v14_0/update_flag_for_return_invoices.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index d5c0a4e80e4..67d4693fb5a 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -353,7 +353,11 @@ erpnext.patches.v14_0.update_zero_asset_quantity_field execute:frappe.db.set_single_value("Buying Settings", "project_update_frequency", "Each Transaction") erpnext.patches.v14_0.update_total_asset_cost_field erpnext.patches.v14_0.create_accounting_dimensions_in_reconciliation_tool +<<<<<<< HEAD erpnext.patches.v15_0.allow_on_submit_dimensions_for_repostable_doctypes +======= +erpnext.patches.v14_0.update_flag_for_return_invoices +>>>>>>> 849f478894 (chore: patch for updating flag in Cr/Dr notes) # 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 diff --git a/erpnext/patches/v14_0/update_flag_for_return_invoices.py b/erpnext/patches/v14_0/update_flag_for_return_invoices.py new file mode 100644 index 00000000000..feb43beacf8 --- /dev/null +++ b/erpnext/patches/v14_0/update_flag_for_return_invoices.py @@ -0,0 +1,62 @@ +from frappe import qb + + +def execute(): + # Set "update_outstanding_for_self" flag in Credit/Debit Notes + # Fetch Credit/Debit notes that does have 'return_against' but still post ledger entries against themselves. + + gle = qb.DocType("GL Entry") + + # Use hardcoded 'creation' date to isolate Credit/Debit notes created post v14 backport + # https://github.com/frappe/erpnext/pull/39497 + creation_date = "2024-01-25" + + si = qb.DocType("Sales Invoice") + if cr_notes := ( + qb.from_(si) + .select(si.name) + .where( + (si.creation.gte(creation_date)) + & (si.docstatus == 1) + & (si.is_return == True) + & (si.return_against.notnull()) + ) + .run() + ): + cr_notes = [x[0] for x in cr_notes] + if docs_that_require_update := ( + qb.from_(gle) + .select(gle.voucher_no) + .distinct() + .where((gle.voucher_no.isin(cr_notes)) & (gle.voucher_no == gle.against_voucher)) + .run() + ): + docs_that_require_update = [x[0] for x in docs_that_require_update] + qb.update(si).set(si.update_outstanding_for_self, True).where( + si.name.isin(docs_that_require_update) + ).run() + + pi = qb.DocType("Purchase Invoice") + if dr_notes := ( + qb.from_(pi) + .select(pi.name) + .where( + (pi.creation.gte(creation_date)) + & (pi.docstatus == 1) + & (pi.is_return == True) + & (pi.return_against.notnull()) + ) + .run() + ): + dr_notes = [x[0] for x in dr_notes] + if docs_that_require_update := ( + qb.from_(gle) + .select(gle.voucher_no) + .distinct() + .where((gle.voucher_no.isin(dr_notes)) & (gle.voucher_no == gle.against_voucher)) + .run() + ): + docs_that_require_update = [x[0] for x in docs_that_require_update] + qb.update(pi).set(pi.update_outstanding_for_self, True).where( + pi.name.isin(docs_that_require_update) + ).run()