Thursday, 11 February 2021

Overdue Duration Calculation Between Two Dates (Excluding Weekends) using Automation Scripts | Maximo

We have two dates, the status date: rcareviewdate and Etc date:rcainprgdate. The script below calculates the duration in Hours excluding weekends between these two dates based on attribute status date change. 

When we change the status it will calculate overdue for previous status ETC date and current status change date.

Launch Point Type:     Attribute Launch Point

Attribute:                     CHANGEDATE

  #Imports


from java.util import Date 

from java.text import SimpleDateFormat

from psdi.mbo import MboConstants 

from psdi.server import MXServer

from java.util import Calendar

from psdi.app.common import DateUtility



#Milliseconds Constants

MILLISECONDS_DAY= 86400000 

MILLISECONDS_HOUR = 3600000

MILLISECONDS_MINUTE = 60000 


#Default hours and minutes values

days = 0

hours = 0

minutes = 0 


#calculate Estimated Duration

if not mbo.isNull('CHANGEDATE'and mbo.getString("STATUS"in ["RCAREVIEW"]  :

    FRACSet = MXServer.getMXServer().getMboSet("FRACSTATUS", mbo.getUserInfo())



    FRACSet.setWhere("FRACASNUM='"+ mbo.getString("FRACASNUM")+"' and STATUS='RCAINPROG' ")



    rcainprg = FRACSet.moveLast()



    rcainprgdate=rcainprg.getDate('FRACASETC')



    rcareviewdate=mbo.getDate('CHANGEDATE')



    if rcareviewdate >= rcainprgdate:



        cal2 = Calendar.getInstance();



        cal = Calendar.getInstance();



        cal2.setTime(rcareviewdate)



        etcdate=rcainprgdate.getTime() 



        cal.setTime(rcainprgdate)



        if (cal2.get(cal2.DAY_OF_WEEK)) in  [cal2.FRIDAY]:



            rcareviewdate = DateUtility.getDate(rcareviewdate)



        if (cal2.get(cal2.DAY_OF_WEEK)) in  [cal2.SATURDAY]:



            rcareviewdate = DateUtility.addDays(rcareviewdate,-1)



            rcareviewdate = DateUtility.getDate(rcareviewdate)



        cal = Calendar.getInstance();

        cal2.setTime(rcareviewdate)

        cal.setTime(rcainprgdate)

        diff = rcareviewdate.getTime() - rcainprgdate.getTime() 



        #Gets the days hours and minutes from the milliseconds difference



        days= long(diff)/MILLISECONDS_DAY



        hours = long(diff) / MILLISECONDS_HOUR



        minutes = (long(diff) / MILLISECONDS_MINUTE) - (long(hours) * 60



        loop=1



        days=abs(days)



        overdue=hours



        while (loop<=days):



                    cal.add(cal.DATE, +1)



                    if (cal.get(cal.DAY_OF_WEEK)) in [cal.FRIDAY,cal.SATURDAY] : 

                            overdue=overdue-24



    #Sets Estimated Duration value as String without access check

                    loop=loop+1

        if  SimpleDateFormat("MM/dd/yyyy").format(rcareviewdate)  ==SimpleDateFormat("MM/dd/yyyy").format(rcainprgdate):
                    

             rcainprg.setValue("OVERDUEDURATION",str(hours) + ':' + str(minutes), MboConstants.NOACCESSCHECK)



        else:  



            rcainprg.setValue("OVERDUEDURATION",str(overdue) + ':' + str(minutes), MboConstants.NOACCESSCHECK)



            rcainprg.setValue("ISOVERDUE","1", MboConstants.NOACCESSCHECK)



        FRACSet.save()


Click Here to see Full Code


 

2 comments: