Technical archivos de la lista de correo

technical@mail.odoo.com

Avatar

Adding elements to a One2Many relation while the form is open

por
joao.jeronimo.pro@gmail.com
- 06/06/2019 04:15:30
Hello,

In the Payslip application of ODOO12 I want the Worked Days list to be 
populated with a default WORK100 line when a contract is selected. So I 
created the following inherit class:

> class HrPayslip(models.Model):
>     _inherit = 'hr.payslip'
>
>     @api.onchange('contract_id')
>     def fill_in_work100(self):
>         if not self.contract_id:
>             return
>
>         # Add WORK100 entries to payslip if it does not already have them:
>         for thisPayslip in self:
>             if len(thisPayslip.worked_days_line_ids) == 0:
>                 thisPayslip.write({
>                     'worked_days_line_ids': [
>                         (0, _, {
>                             'name': "Numero de dias pagos a 100%",
>                             'code': 'WORK100',
>                             'number_of_days': 22.0, 'number_of_hours': 
> 22*8,
>                             'contract_id': thisPayslip.contract_id.id
>                         })
>                     ]
>                 })
But this doesn't work. When a change the contract in the form, ODOO says:

"Odoo Server Error - Access Error
The requested operation cannot be completed due to security 
restrictions. Please contact your system administrator.
(Document type: Pay Slip, Operation: write)"

Before this, I tried to do the same from a button in the Payslip Batch 
(hr.payslip.run) form. This one worked:

> class HrPayslipRun(models.Model):
>     _inherit = 'hr.payslip.run'
>
>     @api.multi
>     def assign_default_contracts(self):
>         for thisPayslip in self.slip_ids:
>             # Add WORK100 entries to payslip if it does not already 
> have them:
>             if self.env['hr.payslip.worked_days'].search_count([
>                             ('payslip_id', "=", thisPayslip.id),
>                             ('code', "=", 'WORK100') ]) == 0:
>                 thisPayslip.write({
>                     'worked_days_line_ids': [
>                         (0, _, {
>                             'name': "Numero de dias pagos a 100%",
>                             'code': 'WORK100',
>                             'number_of_days': 22.0, 'number_of_hours': 
> 22*8,
>                             'contract_id': thisPayslip.contract_id.id
>                         })
>                     ]
>                 })
But I want this to work even if the payslip is not in a batch. How can I 
do this?

Note: I don't want attendance data to be filled-in automatically. What I 
want is a form that fills-in default values that can be altered if needed.

Thanks.

João Jerónimo

Seguimientos