Reputation: 1382
I want to show in sale.order.line for each product quantity that was assigned. It works when I have 1 sale order line, but if I have more it's the same qty for all product. but my code is bad because there is more than 1 move, so how can I do it right?
reserved_qty = fields.Float(string='Reserved Qty', compute='_get_qtys')
@api.depends('order_id.picking_id.move_lines.state')
def _get_qtys(self):
for line in self:
domain = [('id', '=', line.order_id.picking_id.move_lines.id),
('product_id', '=', line.product_id.id)
]
movez = self.env['stock.move'].search(domain)
line.not_fulfiled_qty = line.product_uom_qty - line.qty_delivered
if line.order_id.picking_id:
for picking in line.order_id.picking_id:
for move in picking.move_lines:
if move.state == 'assigned':
line.reserved_qty = movez.product_uom_qty
UPDATE:
if found a solution it works fine, but still have a question can there be more than 1 procurement_ids? will there be singleton errors in some scenarios?
@api.depends('order_id.picking_id.move_lines.state')
def _get_qtys(self):
for line in self:
line.not_fulfiled_qty = line.product_uom_qty - line.qty_delivered
if line.order_id.picking_id:
for move in line.procurement_ids.move_ids:
if move.state == 'assigned':
line.reserved_qty = move.product_uom_qty
Upvotes: 1
Views: 407
Reputation: 14776
Even if there are more procurements, just change your code to handle such situations:
for move in line.procurement_ids.mapped('move_ids'):
if move.state == 'assigned':
Upvotes: 1