跳至内容
菜单
此问题已终结
2 回复
3097 查看

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,
        })
形象
丢弃
最佳答案

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

形象
丢弃
最佳答案

Hello 21m,

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

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

形象
丢弃

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

相关帖文 回复 查看 活动
1
5月 21
3351
4
10月 19
9149
1
1月 18
8432
1
2月 17
4477
0
1月 17
3864