Skip to Content
Menu
This question has been flagged
2 Replies
2424 Views

I have this model

class crm_claim(models.Model):
    _inherit = 'crm.claim'

    needs_credit_note = fields.Boolean(string="Needs Credit Note?")
    partner_id = fields.Many2one('res.partner', string='Partner')

I need to create a function that, when needs_credit_note == True, creates a new record con account.invoice with the same partner_id, and when needs_credit_note == False, delete this record. This is what I did but is not working yet.

@api.depends("needs_credit_note")
def create(self, vals):
    self.ensure_one()
    if self.needs_credit_note:
        self.env['account.invoice'].create({
            'partner_id': self.partner_id.id,
        })
Avatar
Discard
Best Answer

Hi,

For creating and deleting records in account.invoice model, according to the value of "needs_credit_note" field, you can just use a onchange_function which will trigger whenever the needs_credit_note field value changes.

Also add a new field in account.invoice for relating the "crm.claim" model.
for example, here "crm_claim_id" will relate account.invoice with the "crm.claim" model.

@api.onchange('needs_credit_note')
def onchange_needs_credit_note(self):
if self.needs_credit_note:
invoice = self.env['account.invoice'].create({
'partner_id': self.partner_id.id,
'crm_claim_id': self.id
})
else:
invoice = self.env['account.invoice'].search([('crm_claim_id', '=' self.id)])
invoice.unlink()

Regards

Avatar
Discard
Best Answer

Hello 21m,

Please used bellow code..
I hope this will be helpful.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

class crm_claim(models.Model):
_inherit = 'crm.claim'

needs_credit_note = fields.Boolean(string="Needs Credit Note?")
partner_id = fields.Many2one('res.partner', string='Partner')

@api.model
def create(self, vals):
res = super(crm_claim, self),create(vals)
# Add your logic here...
return res

Related Posts Replies Views Activity
1
May 21
2865
4
Oct 19
8647
1
Jan 18
7893
1
Feb 17
3901
0
Jan 17
3406