简体   繁体   中英

float object has no attribute 'number_of_day error in payroll module' in Odoo 15

I got error ValueError('<class \'AttributeError\'>: "\'float\' object has no attribute \'number_of_days\'" while evaluating\n\'result =-(contract.wage/31) * worked_days.Unpaid.number_of_days\'') when I'm trying to deduct Unpaid Leave from Payroll. I'm new in Odoo. Please help me.

I recently installed https://apps.odoo.com/apps/modules/15.0/om_hr_payroll/ this third party community edition.

I have created Salary Rule as like below Snapshot

工资规则

now, then I added that salary rule in Salary Structures

在此处输入图像描述

Now then when I click Compute Sheet in Employee Payslips then I got error as like below snapshot

在此处输入图像描述

I was seen this Blog post of this Module Provider Company https://www.cybrosys.com/blog/hr-unpaid-leaves-payroll-management-in-odoo-10

but still I got error in payroll

My Odoo Version is latest odoo_15.0 (Community Edition)

Odoo will loop over Worked Days lines to add the lines to the worked_days_dict using their code

worked_days_dict = {}

payslip = self.env['hr.payslip'].browse(payslip_id)

for worked_days_line in payslip.worked_days_line_ids:
    worked_days_dict[worked_days_line.code] = worked_days_line

To use the following expression, you need to add a worked days line with Unpaid code :

result = (contract.wage/30) * worked_days.Unpaid.number_of_days

Note that the field name is : number_of_days

If you are using this https://apps.odoo.com/apps/modules/15.0/om_hr_payroll/ payroll module, then there are many object variables available for customize salary rule using python code section. see below I shared available variables list

# Available variables:
#----------------------
# payslip: object containing the payslips
# employee: hr.employee object
# contract: hr.contract object
# rules: object containing the rules code (previously computed)
# categories: object containing the computed salary rule categories (sum of amount of all rules belonging to that category).
# worked_days: object containing the computed worked days.
# inputs: object containing the computed inputs.

# Note: returned value have to be set in the variable 'result'

wagePerDay = contract.wage // for get Wage from Contract

totalWorkingDays = worked_days.WORK100.number_of_days // for get total working days (excluded public holidays, Sunday also... if you set Saturday off then Saturdays also)

date2 = payslip.date_to // for get selected Payslip date_to

date1 = payslip.date_from // for get selected Payslip date_from

Leaves = 0
for line in payslip.worked_days_line_ids:
   Leaves += line.number_of_days // get number of leave (category wise like, total unpaid leave, total of paid leaves, total of sick leaves, total of Global Leaves) 

Now below I shared my Final Answered for Deduct Unpaid Leave from salary

This is Python condition field input

unpaidLeaves = 0
for line in payslip.worked_days_line_ids:
  if line.name == "Unpaid" and line.code == "UNPAID" :
    result = line.number_of_days

This is Python code field input

# Available variables:
#----------------------
# payslip: object containing the payslips
# employee: hr.employee object
# contract: hr.contract object
# rules: object containing the rules code (previously computed)
# categories: object containing the computed salary rule categories (sum of amount of all rules belonging to that category).
# worked_days: object containing the computed worked days.
# inputs: object containing the computed inputs.

# Note: returned value have to be set in the variable 'result'
# ---------------------

# totalWorkingDays = worked_days.WORK100.number_of_days
# date2 = payslip.date_to
# date1 = payslip.date_from
# sec_Of_1Day = 86400
# wagePerHour = contract.wage / 30 / employee.resource_calendar_id.hours_per_day

wagePerDay = contract.wage / 30
unpaidLeaves = 0
if worked_days.Unpaid and worked_days.Unpaid.number_of_days or False:
  result= wagePerDay * unpaidLeaves
else:
  for line in payslip.worked_days_line_ids:
    if line.name == "Unpaid" and line.code == "UNPAID" :
       unpaidLeaves = line.number_of_days

  
  result_qty = round(unpaidLeaves, 2)
  result = round(wagePerDay, 2)
  # result = wagePerDay
  # result = wagePerDay * unpaidLeaves

See below Snapshot of Output

输出快照

you can see below snapshot of my answer

-: python condition :- python条件的快照 -: python code :- python代码的快照

In addition to the suggested answers, One can calculate in python the unpaid leave amount with this formular :

wagePerDay = contract.wage / worked_days.WORK100.number_of_days      
totalUnpaidWage = wagePerDay*worked_days.LEAVE90.number_of_days;
result = round(totalUnpaidWage, 2)

Where: worked_days.WORK100.number_of_days is the total number of days worked for that month as recroded by odoo.

worked_days.LEAVE90.number_of_days is the number of unpaid leave. The code LEAVE90 can be configured under the tab Unpaid Work Entry Type which can be found when editing Salary Structure.

Find attached the example image from Odoo

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM