0

I've created a custom field called caldays in payslip screen and in this field I want to get days between from_date and to_date in openerp with onchange function please help me with code I got below code but how can I put it onchange function

datej = str(payslip.date_to)

datek = str(payslip.date_from)

dj = datej[-2:]

x=0

new = 0

while (x<=31):

x= x+1

if str(x) == dj or "0"+str(x) == dj:

new= x

dk = datek[-2:]

y=0

old = 0

while (y<=31):

y= y+1

if str(y) == dk or "0"+str(y) == dk:

old= y

caldays = new-old + 1

result = caldays
SAT
  • 647
  • 1
  • 12
  • 23
keshav
  • 11
  • 1
  • 6

1 Answers1

1

Try this, this will give no. of days, you have to give two arguments in this, i.e date_from and date_to. Don't forget to import (import datetime and import math) you can put your fields in xml,

<field name="date_from"  on_change="get_number_of_days(date_from,date_to)"/>
<field name="date_to"  on_change="get_number_of_days(date_from,date_to)"/>
<field name="number_of_days_temp"/>

and for your py file,

def get_number_of_days(self, date_from, date_to):
    """Returns a float equals to the timedelta between two dates given as string."""
    if (date_to and date_from) and (date_from <= date_to):
        DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
        from_dt = datetime.datetime.strptime(date_from, DATETIME_FORMAT)
        to_dt = datetime.datetime.strptime(date_to, DATETIME_FORMAT)
        timedelta = to_dt - from_dt
        diff_day = timedelta.days + float(timedelta.seconds) / 86400
        result['value']['number_of_days_temp'] = round(math.floor(diff_day))+1
    else:
        result['value']['number_of_days_temp'] = 0

    return result

Hope this will help you.

Mansi
  • 656
  • 5
  • 16
  • thanks for replaying could you please tell me how can I add it from front end(gui) as in developer mode, because I am new for this thank you – keshav Jan 31 '14 at 10:57
  • if you are using developer mode then you can see, debug view option on the top of your view, in that select "edit formview" option, you will get a window where your view is defined, add your fields there.and save it. now reload tab. you will be able to see those fields. for reference I have put those fields in my answer for xml. – Mansi Jan 31 '14 at 11:08