Skip to content

Commit

Permalink
Merge PR #965 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by sebalix
  • Loading branch information
OCA-git-bot committed Jan 6, 2025
2 parents 93177da + 26c29ef commit db0e3ba
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions stock_release_channel/models/stock_release_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import logging
from collections import defaultdict
from copy import deepcopy
from operator import itemgetter

from pytz import timezone

from odoo import _, api, exceptions, fields, models
from odoo.osv.expression import NEGATIVE_TERM_OPERATORS
from odoo.tools import groupby
from odoo.tools.safe_eval import (
datetime as safe_datetime,
dateutil as safe_dateutil,
Expand Down Expand Up @@ -460,15 +462,18 @@ def _query_get_chain(self, pickings):
WITH RECURSIVE
pickings AS (
SELECT move.picking_id,
stock_picking.release_channel_id,
true as outgoing,
''::varchar as state, -- no need it, we exclude outgoing
move.id as move_orig_id
FROM stock_move move
INNER JOIN stock_picking ON move.picking_id = stock_picking.id
WHERE move.picking_id in %s
UNION
SELECT move.picking_id,
pickings.release_channel_id,
false as outgoing,
picking.state,
rel.move_orig_id
Expand All @@ -480,7 +485,7 @@ def _query_get_chain(self, pickings):
INNER JOIN stock_picking picking
ON picking.id = move.picking_id
)
SELECT DISTINCT picking_id, state FROM pickings
SELECT DISTINCT release_channel_id, picking_id, state FROM pickings
WHERE outgoing is false;
"""
return (query, (tuple(pickings.ids),))
Expand All @@ -490,20 +495,24 @@ def _compute_picking_chain(self):
["move_dest_ids", "move_orig_ids", "picking_id"]
)
self.env["stock.picking"].flush_model(["state"])
for channel in self:
domain = self._field_picking_domains()["released"]
domain += [("release_channel_id", "=", channel.id)]
released = self.env["stock.picking"].search(domain)

if not released:
domain = self._field_picking_domains()["released"]
domain += [("release_channel_id", "in", self.ids)]
released = self.env["stock.picking"].search(domain)

if not released:
for channel in self:
channel.picking_chain_ids = False
channel.count_picking_chain = 0
channel.count_picking_chain_in_progress = 0
channel.count_picking_chain_done = 0
continue
return

self.env.cr.execute(*self._query_get_chain(released))
rows = self.env.cr.dictfetchall()
self.env.cr.execute(*self._query_get_chain(released))
rows = self.env.cr.dictfetchall()
rows_by_channel = dict(groupby(rows, key=itemgetter("release_channel_id")))
for channel in self:
rows = rows_by_channel.get(channel.id, [])
channel.picking_chain_ids = [row["picking_id"] for row in rows]
channel.count_picking_chain_in_progress = sum(
[1 for row in rows if row["state"] not in ("cancel", "done")]
Expand Down

0 comments on commit db0e3ba

Please sign in to comment.