diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-23 10:27:11 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-23 10:27:11 +0700 |
| commit | 1a50d80c1070f009d93d58f2ff469fbc3daa5623 (patch) | |
| tree | ca7d0a0c33371f7d5c2a79d7a4ddac57bda3228c | |
| parent | 5065ffb7c1dc7113e3d951c0f5a59f2268a88375 (diff) | |
push
| -rw-r--r-- | indoteknik_custom/models/approval_unreserve.py | 39 | ||||
| -rw-r--r-- | indoteknik_custom/views/approval_unreserve.xml | 1 |
2 files changed, 23 insertions, 17 deletions
diff --git a/indoteknik_custom/models/approval_unreserve.py b/indoteknik_custom/models/approval_unreserve.py index 51bd52d0..72e3ff51 100644 --- a/indoteknik_custom/models/approval_unreserve.py +++ b/indoteknik_custom/models/approval_unreserve.py @@ -82,27 +82,32 @@ class ApprovalUnreserve(models.Model): stock_move_obj = self.env['stock.move'] for line in self.approval_line: - move = stock_move_obj.browse(line.move_id.id) - # Step 1: Unreserve the product from the current picking + move = stock_move_obj.browse(line.move_id.id) move._do_unreserve(product=line.product_id, quantity=line.unreserve_qty) - # Step 2: Reserve the product in the destination picking (dest_picking_id) + # Step 2: Update existing stock move in dest_picking_id if line.dest_picking_id: - # Find or create the stock move for the destination picking - dest_move = stock_move_obj.create({ - 'product_id': line.product_id.id, - 'product_uom_qty': line.unreserve_qty, - 'picking_id': line.dest_picking_id.id, - 'name': line.product_id.name, - 'location_id': move.location_dest_id.id, # From the unreserved location - 'location_dest_id': line.dest_picking_id.location_id.id, # To the destination picking's location - 'state': 'draft' # Initial state, to be confirmed later - }) - - # Confirm and reserve the product in the new move - dest_move._action_confirm() # Confirm the move - dest_move._action_assign() # Reserve the product + dest_move = stock_move_obj.search([ + ('picking_id', '=', line.dest_picking_id.id), + ('product_id', '=', line.product_id.id), + ('state', 'not in', ['cancel', 'done']) # Only draft or assigned moves can be updated + ], limit=1) + + if dest_move: + # Add the unreserved quantity to the existing stock move + dest_move.write({ + 'product_uom_qty': dest_move.product_uom_qty + line.unreserve_qty + }) + + # If the move is in draft state, confirm it and reserve the product + if dest_move.state == 'draft': + dest_move._action_confirm() + dest_move._action_assign() # Reserve the product + + else: + # If no move is found, raise an error or handle accordingly + raise UserError(f"No stock move found for product {line.product_id.display_name} in the destination picking.") class ApprovalUnreserveLine(models.Model): _name = 'approval.unreserve.line' diff --git a/indoteknik_custom/views/approval_unreserve.xml b/indoteknik_custom/views/approval_unreserve.xml index 5ffa1294..52de785b 100644 --- a/indoteknik_custom/views/approval_unreserve.xml +++ b/indoteknik_custom/views/approval_unreserve.xml @@ -21,6 +21,7 @@ <tree editable="bottom"> <field name="move_id"/> <field name="product_id"/> + <field name="dest_picking_id"/> <field name="unreserve_qty"/> </tree> </field> |
