Skip to Content
Menu
This question has been flagged
1 Reply
86 Views

please fix the code below. when creating a new stock.picking, if there are 2 lines of the same product name then in stock.picking it will become 1 line. it should remain 2 lines. is there an error in the code below? or are there any other suggestions?


def _create_delivery_order(self):

        picking_obj = self.env['stock.picking']

        warehouse = self._default_warehouse()

        destination_id = self._default_destination_id()


        # stock = self.env['stock.location'].search([

        #      ('warehouse_id', '=', warehouse.id),

        #     ('usage', '=', 'internal')

        # ], limit=1)


        picking = picking_obj.create({

            'rma_order_id': self.id,

            'partner_id': self.partner_id.id,

            'partner_invoice_id': self.partner_invoice_id.id,

            'branch_id': self.branch_id.id,

            'picking_type_id': self._default_picking_id(),

            'location_id': self.env.ref('stock.stock_location_customers').id,

            'location_dest_id': destination_id,

            'move_lines': [(0, 0, {

                'product_id': line.product_id.id,

                'name': line.product_id.name,

                'price_unit': line.price_unit,

                'product_uom_qty': line.quantity,

                'product_uom': line.product_id.uom_id.id,

                'location_id': self.env.ref('stock.stock_location_customers').id,

                'location_dest_id': destination_id,

                'reference_picking': line.picking_id.name,

                'rma_line_id': line.id,

            }) for line in self.order_lines],

            'origin': self.name,

        })


        if self.request_type == 'cancel_so':

            picking.name = self._sequence_delivery_order('C')

        else:

            picking.name = self._sequence_delivery_order('RET')


        return picking

Avatar
Discard
Best Answer

Hi,

Please check with this code,

def _create_delivery_order(self):

picking_obj = self.env['stock.picking']

warehouse = self._default_warehouse()

destination_id = self._default_destination_id()

# stock = self.env['stock.location'].search([

# ('warehouse_id', '=', warehouse.id),

# ('usage', '=', 'internal')

# ], limit=1)

# Create the picking without move lines
picking = self.env['stock.picking'].create({
'rma_order_id': self.id,

'partner_id': self.partner_id.id,

'partner_invoice_id': self.partner_invoice_id.id,

'branch_id': self.branch_id.id,

'picking_type_id': self._default_picking_id(),

'location_id': self.env.ref('stock.stock_location_customers').id,

'location_dest_id': destination_id,
'origin': self.name,
})
# Track processed product IDs to avoid duplication
copy = []
for line in self.order_lines:
# Check if move line for the same product already exists
existing_move = self.order_lines.filtered(
lambda m: m.product_id.id == line.product_id.id)
if line.id not in copy:
copy.append(line.id)
# Create a stock move (not move_line) for the combined quantity
picking.move_lines.create({
'picking_id': picking.id,
'product_id': line.product_id.id,
'name': line.product_id.name,
'price_unit': line.price_unit,
'product_uom_qty': sum(existing_move.mapped('qunatity')),#Quantity sum
'product_uom': line.product_id.uom_id.id,
'location_id': self.env.ref(
'stock.stock_location_customers').id,
'location_dest_id': destination_id,
'reference_picking': line.picking_id.name,
'rma_line_id': line.id,
})

if self.request_type == 'cancel_so':

picking.name = self._sequence_delivery_order('C')

else:

picking.name = self._sequence_delivery_order('RET')

return picking

Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
1
May 25
129
3
May 25
175
0
Sep 17
3944
1
Nov 24
17614
0
Sep 24
614