From ded68362e7c5969357fddf3acf6c6cd0d3780584 Mon Sep 17 00:00:00 2001 From: Christopher Ormaza Date: Wed, 26 Apr 2023 08:05:47 -0500 Subject: [PATCH] [14.0][IMP] sale_global_discount, added init hook to avoid memory issues on big database installation --- sale_global_discount/__init__.py | 1 + sale_global_discount/__manifest__.py | 1 + sale_global_discount/hooks.py | 40 ++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 sale_global_discount/hooks.py diff --git a/sale_global_discount/__init__.py b/sale_global_discount/__init__.py index 0650744f6bc..79b7e880446 100644 --- a/sale_global_discount/__init__.py +++ b/sale_global_discount/__init__.py @@ -1 +1,2 @@ from . import models +from .hooks import _pre_init_global_discount_fields diff --git a/sale_global_discount/__manifest__.py b/sale_global_discount/__manifest__.py index eef8551a07c..458331b06b8 100644 --- a/sale_global_discount/__manifest__.py +++ b/sale_global_discount/__manifest__.py @@ -12,4 +12,5 @@ "data": ["views/sale_order_views.xml", "views/report_sale_order.xml"], "application": False, "installable": True, + "pre_init_hook": "_pre_init_global_discount_fields", } diff --git a/sale_global_discount/hooks.py b/sale_global_discount/hooks.py new file mode 100644 index 00000000000..7738bfe0612 --- /dev/null +++ b/sale_global_discount/hooks.py @@ -0,0 +1,40 @@ +from odoo.tools.sql import column_exists + + +def _pre_init_global_discount_fields(cr): + if not column_exists(cr, "sale_order", "amount_global_discount"): + cr.execute( + """ + ALTER TABLE "sale_order" + ADD COLUMN "amount_global_discount" double precision DEFAULT 0 + """ + ) + cr.execute( + """ + ALTER TABLE "sale_order" ALTER COLUMN "amount_global_discount" DROP DEFAULT + """ + ) + if not column_exists(cr, "sale_order", "amount_untaxed_before_global_discounts"): + cr.execute( + """ + ALTER TABLE "sale_order" + ADD COLUMN "amount_untaxed_before_global_discounts" double precision + """ + ) + cr.execute( + """ + update sale_order set amount_untaxed_before_global_discounts = amount_untaxed + """ + ) + if not column_exists(cr, "sale_order", "amount_total_before_global_discounts"): + cr.execute( + """ + ALTER TABLE "sale_order" + ADD COLUMN "amount_total_before_global_discounts" double precision + """ + ) + cr.execute( + """ + update sale_order set amount_total_before_global_discounts = amount_total + """ + )