From 4d52e19bfa93752c2984a7fbcc60e7d913c0dd85 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Thu, 30 Apr 2020 18:07:18 +0200 Subject: [PATCH 1/2] FIX] bad order in pos order / pos order line field computation ; [IMP] Do not recompute pos order line price fields, if pos_pricelist was installed ; [IMP] recompute pos order amount fields with SQL request to make it faster --- .../migrations/12.0.1.0.1/post-migration.py | 65 +++++++++++++++++-- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py b/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py index 6c18465c69c4..2d1ad0e11ab2 100644 --- a/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py +++ b/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py @@ -1,22 +1,77 @@ # Copyright 2019 Eficent # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging from openupgradelib import openupgrade - -def fill_pos_order_amounts(env): - """Avoid null values in required fields.""" - env["pos.order"].search([])._compute_batch_amount_all() +_logger = logging.getLogger(__name__) def fill_pos_order_line_prices(env): """Avoid null values in required fields.""" + env.cr.execute( + """ + SELECT count(*) + FROM ir_module_module + WHERE name = 'pos_pricelist' + AND state = 'to upgrade'; + """ + ) + if env.cr.fetchone()[0] == 1: + _logger.info( + "Skipped the recompute of price_subtotal and price_subtotal_incl" + " for all pos.order.line, because pos_pricelist was installed") + return + env["pos.order.line"].search([])._onchange_amount_line_all() +def fill_pos_order_amounts(env): + """Avoid null values in required fields.""" + openupgrade.logged_query( + env.cr, + """ + UPDATE pos_order po + SET amount_paid = tmp.amount_paid, + amount_return = tmp.amount_return + FROM ( + SELECT + po.id, + sum(absl.amount) AS amount_paid, + sum(LEAST(absl.amount, 0)) AS amount_return + FROM pos_order po + INNER JOIN account_bank_statement_line absl + ON absl.pos_statement_id = po.id + GROUP BY po.id + ) AS tmp + WHERE po.id = tmp.id; + """ + ) + + openupgrade.logged_query( + env.cr, + """ + UPDATE pos_order po + SET amount_total = tmp.amount_total, + amount_tax = tmp.amount_total - tmp.amount_untaxed + FROM ( + SELECT + po.id, + sum(pol.price_subtotal_incl) AS amount_total, + sum(pol.price_subtotal) AS amount_untaxed + FROM pos_order po + INNER JOIN pos_order_line pol + ON pol.order_id = po.id + GROUP BY po.id + ) AS tmp + WHERE po.id = tmp.id; + """ + ) + + @openupgrade.migrate() def migrate(env, version): cr = env.cr - fill_pos_order_amounts(env) fill_pos_order_line_prices(env) + fill_pos_order_amounts(env) openupgrade.load_data( cr, 'point_of_sale', 'migrations/12.0.1.0.1/noupdate_changes.xml') From 4e09097a10001d62281c415d07ae6f34bed9a4a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A0n=20Todorovich?= Date: Wed, 3 Jun 2020 12:11:52 +0200 Subject: [PATCH 2/2] [IMP] Use chunked() to process pos lines in batches. --- .../point_of_sale/migrations/12.0.1.0.1/post-migration.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py b/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py index 2d1ad0e11ab2..ce8dd0f68f5b 100644 --- a/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py +++ b/addons/point_of_sale/migrations/12.0.1.0.1/post-migration.py @@ -22,7 +22,11 @@ def fill_pos_order_line_prices(env): " for all pos.order.line, because pos_pricelist was installed") return - env["pos.order.line"].search([])._onchange_amount_line_all() + for lines in openupgrade.chunked( + env["pos.order.line"].search([]), + single=False + ): + lines._onchange_amount_line_all() def fill_pos_order_amounts(env):