-1

I am trying to define a salary rule which will calculate the salary of employee on the base of date_to and date_from But in these object. I am only able get days. By using days I can get worked days from them. I am unable to get month year there.

datej = str(payslip.date_to)
datek = str(payslip.date_from)
dj = datej[-2:]
dk = datek[-2:]
working_days=dj-dk
one_daySalary=contract.wage/30
salary=working_days*one_daySalary
result = salary

I am only able to calculate the salary on bases of fixed 30 days month.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hassan ALi
  • 1,313
  • 1
  • 23
  • 51

3 Answers3

2

You can get both month and year if you import datetime library and convert the dates to datetime objects. I guess payslip.date_to should be a string before you do str(payslip.date_to), so you can do this using Odoo special methods:

datej = fields.Date.from_string(payslip.date_to)
datek = fields.Date.from_string(payslip.date_from)

Now you have two datetime objects and you can use datetime methods to get what you want.

>>> datej.month
1
>>> datej.year
2018
forvas
  • 9,801
  • 7
  • 62
  • 158
1

I had found the month in payslip.name .Then i simple tried that is month in that payslip name or not but in this solution we have to force user not to change payslip name pattern . I had changed the payslip name to read only to achieve this

 datej = str(payslip.date_to)
    datek = str(payslip.date_from)
    dj = int(datej[-2:])
    dk = int(datek[-2:])
    working_days=dj - dk
    month=0
    if "January" in payslip.name: month= 31
    if "February" in payslip.name: month= 28
    if "March" in payslip.name: month= 31
    if "April" in payslip.name: month= 30
    if "May" in payslip.name: month= 31
    if "June" in payslip.name: month= 30
    if "July" in payslip.name: month= 31
    if "August" in payslip.name: month= 31
    if "September" in payslip.name: month= 30
    if "October" in payslip.name: month= 31
    if "November" in payslip.name: month= 30
    if "December" in payslip.name: month= 31
    one_daySalary=contract.wage/month
    salary=one_daySalary*working_days
    result = salary
Hassan ALi
  • 1,313
  • 1
  • 23
  • 51
  • 2
    Take a look to the [date python methods](https://docs.python.org/3.7/library/datetime.html) or odoo date methods. Your code maybe work, but you are programming badly – ChesuCR Jan 09 '18 at 18:00
  • i am using odoo available object there . direct python modules are available there . – Hassan ALi Jan 09 '18 at 18:15
  • Your code will fail if the year is an leap-year, thats why the are well done libraries like time, datetime, etc for python, like @ChesuCr says, 'you are programming badly'. – Juan Salcedo Jan 09 '18 at 21:28
  • Well, actually I have never worked with salary rules. I am curious, where do you access to these salary rules? Are they different from the security rules? It looks like there is a special sandbox and you only can use some functions. Can you use the object `time` in these rules? that may help you – ChesuCR Jan 10 '18 at 16:56
  • No i cannot use Object time there . Odoo had provided a set of object we only can use them . – Hassan ALi Jan 11 '18 at 13:05
0

On top of the program , import datetime library. and using its method if the object of date you received is in string format,convert it into proper format using the following function.

timeFormattedObject = timeObject.strftime("%Y-%m-%d %H:%M:%S")
timeFormattedObject = datetime.strptime(timeFormattedObject ,DEFAULT_SERVER_DATETIME_FORMAT)

For getting the year of that object use the following function.

yearValue = timeFormattedObject.year 
asfandyar24
  • 78
  • 2
  • 8