Skip to content

Commit

Permalink
Merge PR #542 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by aleuffre
  • Loading branch information
OCA-git-bot committed Dec 7, 2023
2 parents 0b89e71 + 3bf2edb commit 8906ec1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
43 changes: 22 additions & 21 deletions purchase_sale_inter_company/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def _action_done(self):
pick.write({"intercompany_picking_id": purchase.picking_ids[0]})
for move in pick.move_lines:
move_lines = move.move_line_ids
po_move_lines = move.sale_line_id.auto_purchase_line_id.move_ids.mapped(
"move_line_ids"
po_move_lines = (
move.sale_line_id.auto_purchase_line_id.move_ids.filtered(
lambda x, ic_pick=pick.intercompany_picking_id: x.picking_id
== ic_pick
).mapped("move_line_ids")
)
if not len(move_lines) == len(po_move_lines):
raise UserError(
Expand Down Expand Up @@ -61,9 +64,10 @@ def _action_done(self):

def button_validate(self):
res = super().button_validate()
is_intercompany = self.env["res.company"].search(
company_obj_sudo = self.env["res.company"].sudo()
is_intercompany = company_obj_sudo.search(
[("partner_id", "=", self.partner_id.id)]
) or self.env["res.company"].search(
) or company_obj_sudo.search(
[("partner_id", "=", self.partner_id.parent_id.id)]
)
if (
Expand All @@ -90,26 +94,23 @@ def _sync_receipt_with_delivery(self, dest_company, sale_order):
raise UserError(_("PO does not exist or has no receipts"))
if self.intercompany_picking_id:
dest_picking = self.intercompany_picking_id.with_user(intercompany_user.id)
for picking_line in self.move_line_ids_without_package.sorted("qty_done"):
dest_picking_line = (
dest_picking.sudo().move_line_ids_without_package.filtered(
lambda l: l.product_id.id == picking_line.product_id.id
)
for move in self.move_ids_without_package.sudo():
# To identify the correct move to write to,
# use both the SO-PO link and the intercompany_picking_id link
dest_move = move.sale_line_id.auto_purchase_line_id.move_ids.filtered(
lambda x, pick=dest_picking: x.picking_id == pick
)
dest_picking_line.sudo().write(
{
"qty_done": picking_line.qty_done,
}
)
for picking_move in self.move_ids_without_package.sorted("quantity_done"):
dest_picking_move = (
dest_picking.sudo().move_ids_without_package.filtered(
lambda l: l.product_id.id == picking_move.product_id.id
for line, dest_line in zip(move.move_line_ids, dest_move.move_line_ids):
# Assuming the order of move lines is the same on both moves
# is risky but what would be a better option?
dest_line.sudo().write(
{
"qty_done": line.qty_done,
}
)
)
dest_picking_move.sudo().write(
dest_move.write(
{
"quantity_done": picking_move.quantity_done,
"quantity_done": move.quantity_done,
}
)
dest_picking.sudo().with_context(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,33 @@ def test_sync_picking_lot(self):
po_lots,
msg="Serial 333 already existed, a new one shouldn't have been created",
)

def test_sync_picking_same_product_multiple_lines(self):
"""
Picking synchronization should work even when there
are multiple lines of the same product in the PO/SO/picking
"""
self.company_a.sync_picking = True
self.company_b.sync_picking = True

purchase = self._create_purchase_order(
self.partner_company_b, self.consumable_product
)
purchase.order_line += purchase.order_line.copy({"product_qty": 2})
sale = self._approve_po(purchase)
sale.action_confirm()

# validate the SO picking
po_picking_id = purchase.picking_ids
so_picking_id = sale.picking_ids

# Set quantities done on the picking and validate
for move in so_picking_id.move_lines:
move.quantity_done = move.product_uom_qty
so_picking_id.button_validate()

self.assertEqual(
po_picking_id.mapped("move_lines.quantity_done"),
so_picking_id.mapped("move_lines.quantity_done"),
msg="The quantities are not the same in both pickings.",
)

0 comments on commit 8906ec1

Please sign in to comment.