Search code examples
pythonodooodoo-15

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

salary rule

now, then I added that salary rule in Salary Structures

enter image description here

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

enter image description here

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)


Solution

  • 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

    snapshot of Output

    you can see below snapshot of my answer

    -: python condition :- snapshot of python condition -: python code :- snapshot of python code