0

I have inherited account.move model and added job_card_id field(many2one) in it, as shown as below : enter image description here

Below given is Image of Selected Job Card :

enter image description here

Below given is code of inherited model :

class CustomInvoice(models.Model):
_inherit = "account.move"

job_card_id = fields.Many2one('job.card', string="Job Card", domain="[('customer_id','=',partner_id)]", tracking=True)

Below given is code of Job Card Line :

class JobCardLine(models.Model):
_name = "job.card.line"

job_card_id = fields.Many2one('job.card', string="Job Card Id", tracking=True)
product_id = fields.Many2one('product.template', string="Product", tracking=True)
quantity = fields.Integer(string="Quantity", tracking=True)
price = fields.Float(
    'Sales Price',
    digits='Product Price')
total = fields.Integer(string='Total', compute='_compute_total', tracking=True,
                       help="This field will be calculated from dob !")
employee_id = fields.Many2one('hr.employee', string="Employee", tracking=True)

Now I wanted to add product line of selected job card into Invoice product line automatically when I select the job card.

1 Answers1

1

You need to create an onchange method depending on the job_card_id. In this onchange, you will add a line to invoice_line_ids. This will triggers all the other onchange and compute methods and set ups everything needed.

Here is a small example:

class CustomInvoice(models.Model):
    _inherit = "account.move"

    job_card_id = fields.Many2one('job.card', string="Job Card", domain="[('customer_id','=',partner_id)]", tracking=True)

    @api.onchange('job_card_id')
    def _onchange_job_card_id(self):
        # creates your invoice lines vals according to your values
        invoice_lines_vals = self.job_card_id.get_invoice_line_vals()
        self.write({'invoice_line_ids': [(6, 0, 0)] + [(0, 0, vals) for vals in invoice_lines_vals]})

The 6 is a command that will deletes all previous invoice_line and the 0 is a creation command. more info on ORM command

Now, you just need to create the get_invoice_line_vals method in your JobCard class. It should return a list of vals. Each vals should be a dict containing some information such as the quantity and the price_unit.

It depends mostly on how you want your data to be calculate. You just need to return a list like this:

def get_invoice_line_vals(self):
    vals_list = []
    for job_card_line in self.line_ids:
        vals_list.append({
            'price_unit': job_card_line.price_unit,
            'quantity': job_card_line.quantity
        })
    return vals_list
Levizar
  • 75
  • 1
  • 8