Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
447 Vistas

I'm experiencing an issue with internal transfers in the Inventory app. When I create an internal transfer for a quantity greater than what is available in stock, Odoo prompts me to create a backorder upon validation. However, when the stock is already at zero and I attempt an internal transfer, Odoo does not ask to create a backorder. Instead, it simply moves the requested quantity, setting the original location to a negative stock value and the destination location to a positive value, resulting in a net stock of zero.

I would like Odoo to prompt for a backorder even when the stock is at zero. Is there a setting I might have overlooked, or is this the default behavior? Any insights would be greatly appreciated.

Avatar
Descartar
Mejor respuesta

Hi,

Please refer to the code below:


class StockPicking(models.Model):

    _inherit = 'stock.picking'


    def button_validate(self):

        for picking in self:

            if picking.picking_type_code != 'internal':

                # Default behavior for delivery/receipt

                return super().button_validate()


            # Check product quantities at source location

            for move in picking.move_lines:

                product = move.product_id

                source_location = move.location_id


                # Get on-hand quantity at source location using stock.quant

                quant = self.env['stock.quant'].search([

                    ('product_id', '=', product.id),

                    ('location_id', '=', source_location.id)

                ], limit=1)


                qty_available = quant.quantity if quant else 0.0


                if qty_available <= 0:

                    # Create backorder by splitting move lines

                    move_to_backorder = move.copy(default={

                        'product_uom_qty': move.product_uom_qty,

                        'picking_id': False

                    })


                    # Reduce original move to 0 (nothing to deliver now)

                    move.write({'product_uom_qty': 0.0})


                    # Create backorder picking

                    backorder = picking.copy(default={

                        'move_lines': [(6, 0, [move_to_backorder.id])],

                        'origin': picking.name,

                        'state': 'draft'

                    })


                    backorder.action_confirm()

                    picking.message_post(body=_(

                        "Backorder created due to zero stock for %s.") % product.display_name)


            # Continue with original picking (which now only has valid moves)

            return super().button_validate()



Hope it helps.

Avatar
Descartar