1
votes

I want to change the value of Ordered Qty (product_uom_qty) and prix unitaire (price_unit) when changing the Ligne Contrat. But my onchange functions only work for the Ordered Qty, although both functions are called correctly. enter image description here

Here is my code :

from datetime import datetime, timedelta
from odoo import api, fields, models, _

# Contrat model
class Contrat(models.Model):
    _name = 'contrat.contrat'
    _rec_name = 'contrat_name'
    contrat_name = fields.Char(string='Nom')
    contrat_number = fields.Integer(string="Numero")
    date_start = fields.Date(string='Date Debut')
    date_end = fields.Date(string="Date Fin")
    date_deadline = fields.Date(string="date echeance")
    ligne_id = fields.One2many('contrat.lignes','ligne_ids',"mylignes")
    # notebook_ids = fields.Many2one('contrat.lignes','ligne_id',string='Notebook')
    client_name = fields.Many2one('res.partner',string="Client")
    bons_ligne_ids = fields.One2many('sale.order.line', 'contrat_name_id', String='Contrat bons')


class ContratOrder(models.Model):
    _name = 'contrat.lignes'
    _rec_name = 'ligne_name'
    ligne_ids = fields.Many2one('contrat.contrat',string="Contrat")
    ligne_name = fields.Char(string="Nom de Ligne")
    unit_price = fields.Float(string='Prix Unitaire')
    article_name = fields.Many2one('product.template', string="Article")
    quantity = fields.Float(string='Quantite')

# modifier les lignes des bons de commands
class bons_lignes(models.Model):
    _inherit="sale.order.line"
    bons_po = fields.Integer('PO')
    contrat_name_id = fields.Many2one('contrat.contrat', string='Contrat')
    contrat_lignes_id = fields.Many2one('contrat.lignes', string='Ligne contrat')
    product_uom_qty = fields.Float('qtt', related='contrat_lignes_id.quantity')
    price_unit = fields.Float(string='Prix Unitaire')

    @api.onchange('contrat_lignes_id')
    def onchange_conrat_lignes_id(self):
        self.price_unit = self.contrat_lignes_id.unit_price

How is it possible to change both fields with one or more onchange functions.

EDIT: Odoo already has a onchange trigger on field product_uom_qty which is changing the field price_unit after calling my onchange methods. How is it possible to get my onchange functions get called later, or just forbid Odoo to call its other functions?

1
please delete the images with code and add inline code. If you don't know how, read the f****** manual ;-)CZoellner
@CZoellner he is a new member he will learn about this in timeCharif DZ
I know, the RTFM was just a well known little hint for him ;-)CZoellner
@CZoellner I add the inline code , thanksMa7

1 Answers

0
votes

Just use a single onchange method to change both fields.

@api.onchange('contrat_lignes_id')
def onchange_conrat_lignes_id(self):
    self.product_uom_qty = self.contrat_lignes_id.quantity
    self.price_unit = self.contrat_lignes_id.unit_price