Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
2592 Zobrazení

Hello

I am working  on loan limitation, the function must do the following:

if the employee requests a loan less than the limitation, a new limitation must be calculated by subtracting the loan amount.

if an employee requests another loan, the limitation must equal the new limitation - the sum of the loan amount.

here's my code:



limit = fields.Float('limitation',compute='_check_contract_limitation')
@api.onchange('loan_amount', 'employee_id','limit')
def _check_contract_limitation(self):
for rec in self:
#hr_contract = self.env['hr.contract'].search([('employee_id.name', '=', self.employee_id.name)], limit=1)
if not rec.employee_id.contract_id:
raise ValidationError("you dont have contract record.")
elif rec.employee_id.contract_id:
rec.limit = rec.employee_id.contract_id.wage
# rec.a = hr_contract.date_start
if rec.employee_id.contract_id.date_start:
fmt = '%Y-%m-%d'
d1 = rec.employee_id.contract_id.date_start
d2 = datetime.datetime.now().date()
r = relativedelta.relativedelta(d2, d1)
contract_time = r.years
rec.a = contract_time
if contract_time == None:
raise ValidationError("check contract duration.")
else:
if contract_time >= 5:

rec.limit = rec.employee_id.contract_id.wage * 5 - rec.loan_amount
if rec.loan_amount > rec.limit:
rec.loan_amount = False
raise
ValidationError("please correct loan amount is greater than limitaion.")
return

elif
contract_time >= 0 and contract_time < 5:
rec.limit = rec.employee_id.contract_id.wage * contract_time - rec.loan_amount
if rec.loan_amount > rec.limit:
rec.loan_amount = False
raise
ValidationError("please correct loan amount is greater than limitaion.")
return





Avatar
Zrušit
Nejlepší odpověď

Hi,

You can try this code:

import datetime
from dateutil.relativedelta
import relativedelta

limit = fields.Float('Limitation', compute='_check_contract_limitation')

@api.onchange('loan_amount', 'employee_id')
def _check_contract_limitation(self):
for
rec in self:
   
if not rec.employee_id.contract_id:
        raise ValidationError(
"You don't have a contract record.")
   
   
contract = rec.employee_id.contract_id
   
if not contract.date_start:
        raise ValidationError(
"Contract start date is missing.")
   
   
contract_start_date = datetime.datetime.strptime(contract.date_start, '%Y-%m-%d').date()
   
today = datetime.datetime.now().date()
   
contract_duration = relativedelta(today, contract_start_date).years

   
if contract_duration is None:
        raise ValidationError(
"Check contract duration.")
   
   
if contract_duration >= 5:
       
rec.limit = contract.wage * 5 - rec.loan_amount
    elif
0 <= contract_duration < 5:
       
rec.limit = contract.wage * contract_duration - rec.loan_amount
   
   
if rec.loan_amount > rec.limit:
       
rec.loan_amount = False
        raise ValidationError(
"Please correct the loan amount; it is greater than the limitation.")


Hope it helps

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
3
říj 23
7326
1
kvě 23
1579
2
dub 23
1974
1
bře 23
1539
0
bře 23
952