Technical mailing list archives

technical@mail.odoo.com

Avatar

Re: Adding elements to a One2Many relation while the form is open

by
sudhir
- 2019年06月06日 04时43分21秒
You should not use create/write/delete functions (functions which are performed on the server side) on onchange because onchange performs on the client side.
You can set the number of records to o2m field in onchange. 

There is similar functionality in the purchase module. When you select a Purchase Order in Vendor Invoice, onchange pull all the data from the purchase order and create the invoice lines (o2m).

Please check the purchase_order_change method for reference.


On Thu, Jun 6, 2019 at 1:46 PM João Jerónimo <joao.jeronimo.pro@gmail.com> wrote:
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

_______________________________________________
Mailing-List: https://www.odoo.com/groups/technical-62
Post to: mailto:technical@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe



--


Sudhir Arya
Director
ERP Harbor Consulting Services
In the Top 5 Position in Odoo Community Forum
Skype:sudhir@erpharbor.com
Website: http://www.erpharbor.com

The company accepts no liability for the content of this email, or for the consequences of any actions taken on the basis of the information provided unless that information is subsequently confirmed in writing. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

Reference