Hello
i have the following models . i would like to create a new record in model b once the state of record a switches to unit manager approved . i was able to achieve that with automated actions but dont know how to bring the parts from model a_lines to model b_lines . i would appreciate any pointers
model a
class ServiceRequest(models.Model):
_name = 'servicerequest.rider'
_rec_name = 'jobcard_no'
_inherit = ['mail.thread', 'mail.activity.mixin']
vehicle_id = fields.Many2one('vehicles.rider')
state = fields.Selection(string="", selection=[('check-in', 'check-in'), ('Technician service completed', 'Technician service completed'), ('Unit Manager parts approved', 'Unit Manager parts approved'), ('store officer parts released', 'store officer parts released'), ('Unit manager quality check', 'Unit manager quality checked'), ('Checked out', 'Checked out'), ], default='check-in', required=False, track_visibility=True, trace_visibility='onchange', )
jobcard_no = fields.Char(string="Jobcard Number",
default=lambda self: self.env['ir.sequence'].next_by_code('increment_jobcard'),
requires=False, readonly=True, )
operations = fields.One2many('jobcard.partsline', 'servicerequest_id', 'Parts',
copy=True, readonly=True, states={'check-in': [('readonly', False)]})
@api.multi
def write(self, vals):
if vals.get('state'):
if vals.get('state') == 'Confirm':
lines_dict = {
'jobcard_id': self.id,
'operations': [(6, 0, self.operations.parts_id.ids)],
}
self.env['fundrequestw.rider'].create(lines_dict)
return super(ServiceRequest, self).write(vals)
Model a_lines
class JobcardParts(models.Model):
_name = 'jobcard.partsline'
_description = 'Parts Required used'
name = fields.Text(string='Description', required=False)
servicerequest_id = fields.Many2one(comodel_name="servicerequest.rider", index=True, ondelete='cascade')
parts_id = fields.Many2one('product.product', string='Parts',
ondelete='restrict', index=True)
quantity = fields.Integer(string="Quantity", required=False, )
model b
class FundRequestWorkshop(models.Model):
_name = 'fundrequestw.rider'
_rec_name = 'request_no'
_inherit = ['mail.thread']
_description = 'Fund request workshop'
date = fields.Date(string="Date", default=date.today(), required=False, readonly=True, states={'draft': [('readonly', False)]})
request_no = fields.Char(string="Request Number", default=lambda self: self.env['ir.sequence'].next_by_code('increment_fund_request'), requires=False, readonly=True, trace_visibility='onchange',)
programme_id = fields.Many2one(comodel_name="programme.rider", string="Programme ID", required=False, readonly=True, states={'draft': [('readonly', False)]})
jobcard_id = fields.Many2one(comodel_name="servicerequest.rider", string="Job Card ref", required=False, readonly=True, states={'draft': [('readonly', False)]})
state = fields.Selection(string="", selection=[('draft', 'draft'), ('Requested', 'Requested'), ('Approved', 'Approved'), ('Rejected', 'Rejected'),], required=False, copy=False, default='draft', readonly=True, track_visibility='onchange', )
operations = fields.One2many(
'jobcard.partsline', 'fundrequest_id', 'Parts',
copy=True, readonly=True, states={'draft': [('readonly', False)]},)
part_qty = fields.Float(string="Quantity", required=False, )
Model b_lines
class FundrequestLine(models.Model):
_name = 'fundrequest.partsline'
_description = 'Parts request line'
name = fields.Text(string='Description', required=False)
fundrequest_id = fields.Many2one(comodel_name="fundrequestw.rider", index=True, ondelete='cascade')
parts_id = fields.Many2one('product.product', string='Parts',
ondelete='restrict', index=True)