Skip to content

Commit

Permalink
account_cutoff_accrual_*: decrease memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaudoux committed Aug 7, 2024
1 parent 71a736b commit ff6d5ad
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
21 changes: 18 additions & 3 deletions account_cutoff_accrual_purchase/models/purchase_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 18 additions & 11 deletions account_cutoff_accrual_purchase_stock/models/purchase_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
21 changes: 18 additions & 3 deletions account_cutoff_accrual_sale/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 18 additions & 10 deletions account_cutoff_accrual_sale_stock/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ff6d5ad

Please sign in to comment.