diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-23 09:38:08 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-23 09:38:08 +0700 |
| commit | 5065ffb7c1dc7113e3d951c0f5a59f2268a88375 (patch) | |
| tree | 51fa7f2c428955dfdfc68fb8f6917009454212dc | |
| parent | b473179e5adde097bc9ec43ee4566b850937f1ed (diff) | |
update code
| -rw-r--r-- | indoteknik_custom/models/approval_unreserve.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/indoteknik_custom/models/approval_unreserve.py b/indoteknik_custom/models/approval_unreserve.py index 8c232d9c..51bd52d0 100644 --- a/indoteknik_custom/models/approval_unreserve.py +++ b/indoteknik_custom/models/approval_unreserve.py @@ -30,7 +30,7 @@ class ApprovalUnreserve(models.Model): if not self.picking_id: raise ValidationError("Picking is required") - stock_move = self.env['stock.move'].search([('picking_id', '=', self.picking_id.id)]) + stock_move = self.env['stock.move'].search([('picking_id', '=', self.picking_id.id), ('state', '=', 'assigned')]) if not stock_move: raise ValidationError("Picking is not found") @@ -58,6 +58,8 @@ class ApprovalUnreserve(models.Model): if self.state != 'waiting_approval': raise UserError("Approval can only be done in 'Waiting for Approval' state") + + self.write({ 'state': 'approved', 'approved_by': self.env.user.id @@ -77,13 +79,31 @@ class ApprovalUnreserve(models.Model): }) def _trigger_unreserve(self): - # Get the related stock moves and perform the unreserve for approved lines stock_move_obj = self.env['stock.move'] + for line in self.approval_line: move = stock_move_obj.browse(line.move_id.id) - # Use the custom _do_unreserve method you defined earlier + + # Step 1: Unreserve the product from the current picking move._do_unreserve(product=line.product_id, quantity=line.unreserve_qty) + # Step 2: Reserve the product in the destination picking (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 + class ApprovalUnreserveLine(models.Model): _name = 'approval.unreserve.line' _description = 'Approval Unreserve Line' @@ -93,3 +113,4 @@ class ApprovalUnreserveLine(models.Model): move_id = fields.Many2one('stock.move', string="Stock Move", required=True) product_id = fields.Many2one('product.product', string="Product", related='move_id.product_id', readonly=True) unreserve_qty = fields.Float(string="Quantity to Unreserve") + dest_picking_id = fields.Many2one('stock.picking', string="Destination Picking", tracking=True) |
