extended the hr module to display a wizard when the button "Archive" is pressed in the Employee form. That form is intended do add some information to the employee, including some documents. As you can see in the next code the 'hr.cancelation_wizard' adds 'date_cancel', 'reason' and 'documents_cancel_ids' to the employee when the 'action_cancel' method is called from the wizard (A button from the wizard form calls that method). But when a press that button it shows the following error:
2021-03-10 17:11:15,187 7702 ERROR odoo12 odoo.sql_db: bad query: INSERT INTO "hr_cancel_document" ("id", "create_uid", "create_date", "write_uid", "write_date", "attachment_id", "employee_id", "user_id") VALUES (nextval('hr_cancel_document_employee_id_fkey'), 2, (now() at time zone 'UTC'), 2, (now() at time zone 'UTC'), 699, 63, 2) RETURNING id
ERROR: insert or update on table "hr_cancel_document" violates foreign key constraint "hr_cancel_document_employee_id_fkey"
DETAIL: Key (employee_id)=(63) is not present in table "hr_employee".
Here is my code:
class UtepdaHr(models.Model):
_inherit = 'hr.employee'
documents_cancel_ids = fields.One2many('hr.cancel_document',
'employee_id',
string="Documents")
date_cancel = fields.Date(string="Cancelation Date", required=True)
reason = fields.Selection(selection=[
('cancelation', 'Cancelation'),
('resign', 'Resign'),
('passing', 'Passed away'),
],required=True, string="Cancelation reason")
@api.multi
def toggle_active(self):
self.ensure_one()
if self.active==True:
return {
'name': 'Cancel employee',
'res_model': 'hr.cancelacion_wizard',
'view_mode': 'form',
'target': 'new',
'context': {
'default_employee_id': self.id
},
'type': 'ir.actions.act_window',
}
else:
self.active = True
self.reason= False
self.date_cancel= False
self.documents_cancel_ids = [(6, 0, [])]
return True
class CancelationDocument(models.Model):
_name = 'hr.cancel_document'
_description = 'Save some documents when the employeed leaves the company'
_inherits = {'ir.attachment': 'attachment_id'}
user_id = fields.Many2one('res.users',
'Uploaded by',
default=lambda self: self.env.user)
employee_id = fields.Many2one('hr.employee', string="Employee", ondelete="cascade")
@api.model
def create(self, vals):
if 'user_id' not in vals:
vals['user_id'] = self.env.user.id
return super(CancelationDocument, self).create(vals)
@api.onchange('datas_fname')
def _change_tipo(self):
self.name = self.datas_fname
class UtepdaHrCancelacionWizard(models.TransientModel):
_name = 'hr.cancelation_wizard'
_description = 'Wizard intended to add some information to an Employee'
employee_id = fields.Many2one('hr.employee')
date_cancel = fields.Date(string="Cancelation Date", required=True)
reason = fields.Selection(selection=[
('cancelation', 'Cancelation'),
('resign', 'Resign'),
('passing', 'Passed away'),
],required=True, string="Cancelation reason")
documentw_ids = fields.One2many(
'hr.cancel_document',
'employee_id',
realted="employee_id.documents_cancel_ids")
def action_cancel(self):
if self.date_cancel and self.reason:
vals = {}
vals['date_cancel'] = self.date_cancel
vals['reason'] = self.reason
vals['active'] = False
if vals:
self.empleado_id.write(vals)
message="You deactivated the employee"
self.empleado_id.message_post(body=message, subtype="mail.mt_note")