pythonodooodoo-13odoo-14odoo-15

Create Delivery Operation From My Custom Module Odoo 15


I try to within action confirm I will pass data from my module to stock.move and stock.picking so I can create complete cycle from stock.move passing with stock.picking to stock.quant to decrease quantity on hand. The problem that stock move creates working good and the stock picking too but when we should decrease quantity on hand by delivered qty it makes the opposite and it increases not decrease

 def action_confirm(self):
        for rec in self:
            move = self.env['stock.move'].create({
                'name': 'Use on MyLocation',
                'location_id': rec.stock_delivery_id.id,
                'location_dest_id': rec.stock_delivery_id.default_location_dest_id.id,
                'product_id': rec.product_id.id,
                'product_uom': rec.product_id.uom_id.id,
                'product_uom_qty': rec.quantity,
                'quantity_done': rec.quantity,
            })
            stock_picking = self.env['stock.picking'].create({
                            'partner_id': rec.partner_id.id,
                            'picking_type_id': rec.stock_delivery_id.id,
                            'location_id': rec.stock_delivery_id.default_location_src_id.id,
                            'location_dest_id': rec.stock_delivery_id.default_location_dest_id.id,
                            'move_ids_without_package': move,
            })
            stock_picking.action_confirm()
            stock_picking.action_set_quantities_to_reservation()
            stock_picking.button_validate()

            rec.state = 'confirmed'

Solution

  • I found that I don`t need to create stock.picking manually to affect stock.quant model so you can only do the stock move cycle correctly and it will automatically affect the quantity on hand with transfer operation so we only have to confirm and done stock move with existing methods _action_confirm(self) and _action_done(self) to create stock.move in a right way as follow :

    def action_confirm(self):
        for rec in self:
            move = self.env['stock.move'].create({
                'name': 'Use on MyLocation',
                'location_id': rec.stock_delivery_id.id,
                'location_dest_id': rec.stock_delivery_id.default_location_dest_id.id,
                'product_id': rec.product_id.id,
                'product_uom': rec.product_id.uom_id.id,
                'product_uom_qty': rec.quantity,
                'quantity_done': rec.quantity,
            })
            move._action_confirm()
            move._action_done()
        rec.state = 'confirmed'