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

I have made a one2many field to add the spare parts of a vehicle in a form and i want to modify the product qty in the stock based on the ordered qty so the available qty = the available qty - the ordered qty the constraints match the first condition but not the else case

class One2manyProductLine(models.Model): _inherit = 'fleet.vehicle.log.services'

custom_field = fields.One2many("product.line.details", "new_field", string="Test", copy=True, auto_join=True)

class ProductLineDetails(models.Model): _name = 'product.line.details'

product_id = fields.Many2one('product.product', string="Product")
avail_qty = fields.Float(related='product_id.qty_available', string="Quantity")
exact_cost = fields.Float(related='product_id.standard_price')
product_desc = fields.Text()
ordered_qty = fields.Float(store=True)
total = fields.Float(compute='_compute_total')
new_field = fields.Many2one('fleet.vehicle.log.services')

 @api.constrains('ordered_qty')
def _check_ordered_qty(self):
    for rec in self:
        if rec.ordered_qty > rec.avail_qty:
            raise ValidationError("There is no enough amount in stock")
        else:
            self.avail_qty = rec.avail_qty - rec.ordered_qty
Avatar
Discard
Best Answer

Hello, 

The functionality which you trying to achieve is bit difficult. Reason Product available qty is compute field which calculates stock quant.

and in your object it is coming using related field. which updates visa-versa. So i think you need to think again for your code.

Avatar
Discard