diff --git a/jey_erp/patches.txt b/jey_erp/patches.txt index 36ccad3..c27500a 100644 --- a/jey_erp/patches.txt +++ b/jey_erp/patches.txt @@ -13,4 +13,5 @@ jey_erp.patches.remove_won_lost_breaks jey_erp.patches.remove_won_lost_checkboxes jey_erp.patches.drop_bank_integration_account_mapping jey_erp.patches.backfill_si_tax_article_row_keys -jey_erp.patches.backfill_sales_invoice_tax_articles_history \ No newline at end of file +jey_erp.patches.backfill_sales_invoice_tax_articles_history +jey_erp.patches.add_tax_articles_to_si_grid_views \ No newline at end of file diff --git a/jey_erp/patches/add_tax_articles_to_si_grid_views.py b/jey_erp/patches/add_tax_articles_to_si_grid_views.py new file mode 100644 index 0000000..fdcaa9f --- /dev/null +++ b/jey_erp/patches/add_tax_articles_to_si_grid_views.py @@ -0,0 +1,54 @@ +import json + +import frappe + + +def execute(): + """Inject the new "Tax Articles" column into saved Sales Invoice Item grid layouts. + + Per-user grid column layouts live in `__UserSettings` (GridView). A layout saved + before the multi tax-article feature lists the legacy `tax_article` column but not + the new `custom_tax_articles_display`. Frappe renders that saved list verbatim and + ignores the new in_list_view field, so the "Tax Articles" cell never appears for + those users until they add it manually via the grid gear. + + Replace the legacy `tax_article` entry (or, lacking it, insert after + `item_tax_template`) with `custom_tax_articles_display` in every saved layout that + doesn't already have it. Idempotent. + """ + rows = frappe.db.sql( + "select user, data from `__UserSettings` where doctype = 'Sales Invoice'", + as_dict=True, + ) + for r in rows: + try: + data = json.loads(r.data or "{}") + except (ValueError, TypeError): + continue + + grid_view = data.get("GridView") or {} + cols = grid_view.get("Sales Invoice Item") + if not cols: + continue + + names = [c.get("fieldname") for c in cols] + if "custom_tax_articles_display" in names: + continue + + new_col = {"fieldname": "custom_tax_articles_display", "columns": 2} + if "tax_article" in names: + cols[names.index("tax_article")] = new_col + elif "item_tax_template" in names: + cols.insert(names.index("item_tax_template") + 1, new_col) + else: + cols.append(new_col) + + grid_view["Sales Invoice Item"] = cols + data["GridView"] = grid_view + frappe.db.sql( + "update `__UserSettings` set data = %s where user = %s and doctype = 'Sales Invoice'", + (json.dumps(data), r.user), + ) + + frappe.db.commit() + frappe.cache().delete_value("_user_settings")