Fix stock picking batch date and does not split batchs by consumer group
This commit is contained in:
parent
b73f031dfb
commit
828278573d
2 changed files with 48 additions and 59 deletions
|
|
@ -890,47 +890,41 @@ class GroupOrder(models.Model):
|
|||
self.ensure_one()
|
||||
StockPickingBatch = self.env["stock.picking.batch"].sudo()
|
||||
|
||||
# Group sale orders by consumer_group_id
|
||||
groups = {}
|
||||
for so in sale_orders:
|
||||
group_id = so.consumer_group_id.id or False
|
||||
if group_id not in groups:
|
||||
groups[group_id] = self.env["sale.order"]
|
||||
groups[group_id] |= so
|
||||
|
||||
for consumer_group_id, group_sale_orders in groups.items():
|
||||
# Get pickings without batch
|
||||
pickings = group_sale_orders.picking_ids.filtered(
|
||||
lambda p: p.state not in ("done", "cancel") and not p.batch_id
|
||||
# Create batches per group order, not per consumer group.
|
||||
# If multiple picking types exist, keep one batch per picking type.
|
||||
grouped_pickings = {}
|
||||
pickings = sale_orders.picking_ids.filtered(
|
||||
lambda p: p.state not in ("done", "cancel") and not p.batch_id
|
||||
)
|
||||
for picking in pickings:
|
||||
grouped_pickings.setdefault(
|
||||
picking.picking_type_id.id, self.env["stock.picking"]
|
||||
)
|
||||
grouped_pickings[picking.picking_type_id.id] |= picking
|
||||
|
||||
scheduled_date = self.pickup_date
|
||||
if not scheduled_date and self.delivery_date:
|
||||
scheduled_date = self.delivery_date - timedelta(days=1)
|
||||
|
||||
for picking_type_id, pickings in grouped_pickings.items():
|
||||
if not pickings:
|
||||
continue
|
||||
|
||||
# Get consumer group name for batch description
|
||||
consumer_group = self.env["res.partner"].browse(consumer_group_id)
|
||||
batch_desc = (
|
||||
f"{self.name} - {consumer_group.name}" if consumer_group else self.name
|
||||
)
|
||||
|
||||
# Create the batch
|
||||
batch_desc = self.name
|
||||
batch = StockPickingBatch.create(
|
||||
{
|
||||
"description": batch_desc,
|
||||
"company_id": self.company_id.id,
|
||||
"picking_type_id": pickings[0].picking_type_id.id,
|
||||
"scheduled_date": self.pickup_date,
|
||||
"picking_type_id": picking_type_id,
|
||||
"scheduled_date": scheduled_date,
|
||||
}
|
||||
)
|
||||
|
||||
# Assign pickings to the batch
|
||||
pickings.write({"batch_id": batch.id})
|
||||
|
||||
_logger.info(
|
||||
"Cron: Created batch %s with %d pickings for group order %s, "
|
||||
"consumer group %s",
|
||||
"Cron: Created batch %s with %d pickings for group order %s",
|
||||
batch.name,
|
||||
len(pickings),
|
||||
self.name,
|
||||
consumer_group.name if consumer_group else "N/A",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue