From ff6d5ad7b5bb9f3474ba252c2a16bd5b63e405ce Mon Sep 17 00:00:00 2001 From: Jacques-Etienne Baudoux Date: Wed, 7 Aug 2024 14:45:33 +0200 Subject: [PATCH] account_cutoff_accrual_*: decrease memory usage --- .../models/purchase_order_line.py | 21 ++++++++++++-- .../models/purchase_order_line.py | 29 ++++++++++++------- .../models/sale_order_line.py | 21 ++++++++++++-- .../models/sale_order_line.py | 28 +++++++++++------- 4 files changed, 72 insertions(+), 27 deletions(-) diff --git a/account_cutoff_accrual_purchase/models/purchase_order_line.py b/account_cutoff_accrual_purchase/models/purchase_order_line.py index 386afafc31e..46a6caa68a9 100644 --- a/account_cutoff_accrual_purchase/models/purchase_order_line.py +++ b/account_cutoff_accrual_purchase/models/purchase_order_line.py @@ -70,9 +70,24 @@ def _get_cutoff_accrual_lines_invoiced_after(self, cutoff): _logger.debug( "Purchase Invoice Lines done after cutoff: %s" % len(invoice_line_after) ) - purchase_ids = set(invoice_line_after.purchase_line_id.order_id.ids) - purchases = self.env["purchase.order"].browse(purchase_ids) - return purchases.order_line + # In SQL to reduce memory usage as we could process large dataset + self.env.cr.execute( + """ + SELECT order_id + FROM purchase_order_line + WHERE id in ( + SELECT purchase_line_id + FROM account_move_line + WHERE id in %s + ) + """, + (tuple(invoice_line_after.ids),), + ) + purchase_ids = [x[0] for x in self.env.cr.fetchall()] + lines = self.env["purchase.order.line"].search( + [("order_id", "in", purchase_ids)], order="id" + ) + return lines def _get_cutoff_accrual_delivered_service_quantity(self, cutoff): # By default, no cutoff on purchase. Set received as invoiced. diff --git a/account_cutoff_accrual_purchase_stock/models/purchase_order_line.py b/account_cutoff_accrual_purchase_stock/models/purchase_order_line.py index 074122838a3..917e0c59faf 100644 --- a/account_cutoff_accrual_purchase_stock/models/purchase_order_line.py +++ b/account_cutoff_accrual_purchase_stock/models/purchase_order_line.py @@ -16,18 +16,25 @@ def _get_cutoff_accrual_lines_delivered_after(self, cutoff): lines = super()._get_cutoff_accrual_lines_delivered_after(cutoff) cutoff_nextday = cutoff._nextday_start_dt() # Take all moves done after the cutoff date - moves_after = self.env["stock.move"].search( - [ - ("state", "=", "done"), - ("date", ">=", cutoff_nextday), - ("purchase_line_id", "!=", False), - ], - order="id", + # In SQL to reduce memory usage as we could process large dataset + self.env.cr.execute( + """ + SELECT order_id + FROM purchase_order_line + WHERE id in ( + SELECT purchase_line_id + FROM stock_move + WHERE state='done' + AND date >= %s + AND sale_line_id IS NOT NULL + ) + """, + (cutoff_nextday,), + ) + purchase_ids = [x[0] for x in self.env.cr.fetchall()] + lines = self.env["purchase.order.line"].search( + ["|", ("order_id", "in", purchase_ids), ("id", "in", lines.ids)], order="id" ) - _logger.debug("Moves done after cutoff: %s" % len(moves_after)) - purchase_ids = set(moves_after.purchase_line_id.order_id.ids) - purchases = self.env["purchase.order"].browse(purchase_ids) - lines |= purchases.order_line return lines def _get_cutoff_accrual_delivered_min_date(self): diff --git a/account_cutoff_accrual_sale/models/sale_order_line.py b/account_cutoff_accrual_sale/models/sale_order_line.py index 036770585bd..47351980cae 100644 --- a/account_cutoff_accrual_sale/models/sale_order_line.py +++ b/account_cutoff_accrual_sale/models/sale_order_line.py @@ -69,9 +69,24 @@ def _get_cutoff_accrual_lines_invoiced_after(self, cutoff): _logger.debug( "Sales Invoice Lines done after cutoff: %s" % len(invoice_line_after) ) - sale_ids = set(invoice_line_after.sale_line_ids.order_id.ids) - sales = self.env["sale.order"].browse(sale_ids) - return sales.order_line + # In SQL to reduce memory usage as we could process large dataset + self.env.cr.execute( + """ + SELECT order_id + FROM sale_order_line + WHERE id in ( + SELECT order_line_id + FROM sale_order_line_invoice_rel + WHERE invoice_line_id in %s + ) + """, + (tuple(invoice_line_after.ids),), + ) + sale_ids = [x[0] for x in self.env.cr.fetchall()] + lines = self.env["sale.order.line"].search( + [("order_id", "in", sale_ids)], order="id" + ) + return lines def _get_cutoff_accrual_delivered_service_quantity(self, cutoff): self.ensure_one() diff --git a/account_cutoff_accrual_sale_stock/models/sale_order_line.py b/account_cutoff_accrual_sale_stock/models/sale_order_line.py index 60ccc6ba330..ea3d7e5b436 100644 --- a/account_cutoff_accrual_sale_stock/models/sale_order_line.py +++ b/account_cutoff_accrual_sale_stock/models/sale_order_line.py @@ -16,17 +16,25 @@ def _get_cutoff_accrual_lines_delivered_after(self, cutoff): lines = super()._get_cutoff_accrual_lines_delivered_after(cutoff) cutoff_nextday = cutoff._nextday_start_dt() # Take all moves done after the cutoff date - moves_after = self.env["stock.move"].search( - [ - ("state", "=", "done"), - ("date", ">=", cutoff_nextday), - ("sale_line_id", "!=", False), - ], - order="id", + # In SQL to reduce memory usage as we could process large dataset + self.env.cr.execute( + """ + SELECT order_id + FROM sale_order_line + WHERE id in ( + SELECT sale_line_id + FROM stock_move + WHERE state='done' + AND date >= %s + AND sale_line_id IS NOT NULL + ) + """, + (cutoff_nextday,), + ) + sale_ids = [x[0] for x in self.env.cr.fetchall()] + lines = self.env["sale.order.line"].search( + ["|", ("order_id", "in", sale_ids), ("id", "in", lines.ids)], order="id" ) - sale_ids = set(moves_after.sale_line_id.order_id.ids) - sales = self.env["sale.order"].browse(sale_ids) - lines |= sales.order_line return lines def _get_cutoff_accrual_delivered_min_date(self):