AKHIL MATHEW AKHIL MATHEW - 6 months ago 53
Python Question

Record is not creating in another model while creating a record in odoo

Currently i am creating a custom module, in which i need to create a demo record while a customer invoice is created.

This is my custom model.

class csm(models.Model):
_name= 'csm'
invoice_date = fields.Datetime('Invoice date',readonly=True)
delivery_date = fields.Datetime('Delivery date',readonly=True)


In account.invoice i have added as,

class invoice_csm(models.Model):
_inherit = 'account.invoice'

delivery_date = fields.Datetime('Delivery date')
date_invoice= fields.Datetime('Invoice date')

@api.multi
def write(self,values):
record = super(invoice_csm, self).write(values)
if self.state == 'paid':
if self.delivery_date and self.installation_date:
self.env['csm'].write({
'invoice_date': record .date_invoice,
'delivery_date': record .delivery_date,
})
return record


What i want is when the status of customer invoice is become paid, i need to create a record in my csm model. But it is not creating. But if i use directly create function then record is creating in csm, but the condtion state=='paid' wont work because we can change the state even after creating invoice. That's why i used write method, but here it is not creating the record.

(Delivery and invoice dates have values)

############ CODE FOR REFERENCE #########

class customer(models.Model):
_name = "csm"
_inherit = ['mail.thread']


name = fields.Char(string='Subject', required=True)


## RETAIL ##

invoice_date = fields.Datetime('Invoice date',readonly=True)
delivery_date = fields.Datetime('Delivery date',readonly=True)
installation_date = fields.Datetime('Installation date',readonly=True)
service1_date = fields.Datetime('First service date',readonly=True)
service2_date = fields.Datetime('Second service date',readonly=True)
service3_date = fields.Datetime('Third service date',readonly=True)



@api.multi
def unlink(self):
if self.pool['res.users'].has_group(self._cr, self.env.user.id, 'customer_service_management.group_managerz'):
pass
elif self.create_uid == self.env.user:
pass
else:
if self.create_uid != self.env.user:
raise Warning('You are not authorized to delete this record. Contact administrator/manager')
return super(customer, self).unlink()


INVOICE

class invoice_csm(models.Model):
_inherit = 'account.invoice'


delivery_date = fields.Datetime('Delivery date')
installation_date = fields.Datetime('Installation date')
service1_date = fields.Datetime('First service date')
service2_date = fields.Datetime('Second service date')
service3_date = fields.Datetime('Third service date')



@api.multi
def write(self,values):
record = super(invoice_csm, self).write(values)
if 'state' in values and values['state'] == 'paid':
self.env['csm'].create({
'name': values['number'],
'delivery_date': values['delivery_date'],
'installation_date': values['installation_date'],
'service1_date': values['service1_date'],
'service2_date': values['service2_date'],
'service3_date': values['service3_date'],
})
return record

Answer

self.env['csm'].create , instead of self.env['csm'].write

@api.multi
def write(self,values):
     record = super(invoice_csm, self).write(values)
     if 'state' in values and values['state'] == 'paid':
         if values.get('date_invoice') and values.get('delivery_date'):
             self.env['csm'].create({
                 'invoice_date': values['date_invoice'],
                 'delivery_date': values['delivery_date'],
                 })
    return record