Adding Duration to Date based on Maximo Calendar Java/Python

Add date with duration with / without Maximo calendar (Java/Python)

To work this solution calendar based, user has to supply the all valid values of organization , shift, calendar values to find the work periods.

Calculation Date= Reported Date + Duration (in Mins) + Maximo  Calendar Shift [Optional]




In Maximo define calendar, shift, Non working days. then apply shift/non working days to calendar.
Maximo Calendars Application Screen after applied of calendar, shift and Non-working days.

Method Calling:

 

Java

#Date Calculation using calendar. 
Date calculatedate= getcalculateDate("EAGLESA","DAY","EVENING",MXServer.getMXServer().getDate(), mins);

#Date Calculation With out calendar. 
Date calculatedate= getcalculateDate(null,null,null,MXServer.getMXServer().getDate(), mins);

 

Python 

#Date Calculation using calendar. 
calculatedate= getcalculateDate("EAGLESA","DAY","EVENING",MXServer.getMXServer().getDate(), mins);

#Date Calulation with out Calendar.
calulatedate= getcalculateDate(None,None,None,MXServer.getMXServer().getDate(), mins);


 

Method Definition:

 

Java


import java.util.Date;
import psdi.app.common.DateUtility;

private Date getcalculateDate(String orgid, String calendar, String shift,
            Date reportDate, int totalMinutes) throws MXException,
            RemoteException {

        System.out.println(">> Calender Details");
        System.out.println("orgid: " + orgid);
        System.out.println("calendar: " + calendar);
        System.out.println("shift: " + shift);

        if (orgid != null || calendar != null || shift != null) {

            System.out.println("No Calender Defined");
            return DateUtility.addMinutes(reportDate, totalMinutes);
        }

        Date today = DateUtility.getDate(reportDate);

        Date yesterday = DateUtility.addDays(today, -1);

        System.out.println(today);

        System.out.println(yesterday);

        String where = " workdate >= :1";

        SqlFormat sqf = new SqlFormat(where);
        sqf.setDate(1, yesterday);
        where = sqf.format();

        System.out.println("Where::: " + where);

        MboSetRemote workPeriods = this.getMboSet("$workperiod2", "WORKPERIOD",
                where);
        workPeriods.setWhere("calnum = '" + calendar + "' and shiftnum = '"
                + shift + "' and orgid = '" + orgid + "'");
        workPeriods.reset();
        workPeriods.setOrderBy("workdate");
        System.out.println("workPeriods::: " + workPeriods.getCompleteWhere()
                + ", " + workPeriods.count());

        int accumulatedMinutes = 0;

        Date workdate = null;
        Date start = null;
        Date end = null;
        for (int m = 0;; m++) {
            MboRemote workPeriod = workPeriods.getMbo(m);
            if (workPeriod == null) {
                break;
            }

            workdate = workPeriod.getDate("workdate");

            start = workPeriod.getDate("starttime");
            end = workPeriod.getDate("endtime");
            double wHours = workPeriod.getDouble("workhours");
            wHours *= 60.0D;
            int workHours = (int) wHours;
            if (workdate.getTime() < today.getTime()) {
                if (DateUtility.compareTime(start, end) > 0) {
                    int diff = DateUtility.timeDiff(end, reportDate);
                    if (diff > 0) {
                        accumulatedMinutes += diff;
                        if (accumulatedMinutes >= totalMinutes) {
                            return DateUtility.addMinutes(reportDate,
                                    totalMinutes);
                        }
                    }
                }
            } else if (workdate.getTime() == today.getTime()) {
                if (DateUtility.compareTime(start, reportDate) >= 0) {
                    accumulatedMinutes += workHours;
                    if (accumulatedMinutes >= totalMinutes) {
                        Date date = DateUtility.combineDate(workdate, start);
                        int t = accumulatedMinutes - totalMinutes;
                        t = workHours - t;
                        return DateUtility.addMinutes(date, t);
                    }
                } else if ((DateUtility.compareTime(start, end) >= 0)
                        || (DateUtility.compareTime(end, reportDate) > 0)) {
                    if (DateUtility.compareTime(start, end) == 0) {
                        return DateUtility.addMinutes(reportDate, totalMinutes);
                    }
                    int diff = DateUtility.timeDiff(end, reportDate);
                    if (DateUtility.compareTime(start, end) > 0) {
                        diff += 1440;
                    }
                    if (diff > 0) {
                        accumulatedMinutes += diff;
                        if (accumulatedMinutes >= totalMinutes) {
                            return DateUtility.addMinutes(reportDate,
                                    totalMinutes);
                        }
                    }
                }

            } else {

                accumulatedMinutes += workHours;
                if (accumulatedMinutes >= totalMinutes) {
                    Date date = DateUtility.combineDate(workdate, start);
                    int t = accumulatedMinutes - totalMinutes;
                    t = workHours - t;
                    return DateUtility.addMinutes(date, t);
                }
            }
        }
        workPeriods.close();

        if (workdate == null) {

            return DateUtility.addMinutes(reportDate, totalMinutes);
        }
        Date date = DateUtility.combineDate(workdate, end);
        return DateUtility.addMinutes(date, totalMinutes - accumulatedMinutes);
    }

 

Python


from java.util import Date
from psdi.app.common import DateUtility

def getcalculateDate(orgid,calendar,shift, reportDate, totalMinutes):
    service.log(">> Calender Details")
    service.log("orgid: "+orgid)
    service.log("calendar: "+calendar)
    service.log("shift: "+shift)
   
    if (orgid is None or calendar is None or shift is None):
        service.log(">>> No Calender Defined")
        return DateUtility.addMinutes(reportDate, totalMinutes)
   
    service.log(">>> Calender Defined")
    today = DateUtility.getDate(reportDate)
    yesterday = DateUtility.addDays(today, -1)
    service.log(str(today))
    service.log(str(yesterday))
    where = " workdate >= :1"
    sqf = SqlFormat(where)
    sqf.setDate(1, yesterday)
    where = sqf.format()
    service.log("Where::: "+str(where))
    workPeriods = mbo.getMboSet("$workperiod2", "WORKPERIOD", where)
    workPeriods.setWhere("calnum = '"+calendar+"' and shiftnum = '"+shift+"' and orgid = '"+orgid+"'")
    workPeriods.reset()
    workPeriods.setOrderBy("workdate")
    service.log("workPeriods::: "+workPeriods.getCompleteWhere()+", "+str(workPeriods.count()));
    accumulatedMinutes = 0
    workdate = None
    start = None
    end = None
    for m in range(workPeriods.count()):
        workPeriod = workPeriods.getMbo(m)
        if (workPeriod is None):
            break;
        workdate = workPeriod.getDate("workdate")
          start = workPeriod.getDate("starttime")
        end = workPeriod.getDate("endtime")
        wHours = workPeriod.getDouble("workhours")
        wHours *= 60.0;
        workHours = int(wHours)
        if (workdate.getTime() < today.getTime()):
            if (DateUtility.compareTime(start, end) > 0):
                diff = DateUtility.timeDiff(end, reportDate)
                if (diff > 0):
                    accumulatedMinutes += diff
                    if (accumulatedMinutes >= totalMinutes):
                        return DateUtility.addMinutes(reportDate, totalMinutes)
        elif (workdate.getTime() == today.getTime()):
            if (DateUtility.compareTime(start, reportDate) >= 0):
                accumulatedMinutes += workHours
                 if (accumulatedMinutes >= totalMinutes):
                    date = DateUtility.combineDate(workdate, start)
                    t = accumulatedMinutes - totalMinutes
                    t = workHours - t
                    return DateUtility.addMinutes(date, t)
            elif ((DateUtility.compareTime(start, end) >= 0) or (DateUtility.compareTime(end, reportDate) > 0)):
                if (DateUtility.compareTime(start, end) == 0) :
                    return DateUtility.addMinutes(reportDate, totalMinutes)
                diff = DateUtility.timeDiff(end, reportDate)
                if (DateUtility.compareTime(start, end) > 0):
                    diff += 1440;
                if (diff > 0):
                    accumulatedMinutes += diff
                    if (accumulatedMinutes >= totalMinutes):
                        return DateUtility.addMinutes(reportDate, totalMinutes)
        else:
            accumulatedMinutes += workHours
            if (accumulatedMinutes >= totalMinutes):
                date = DateUtility.combineDate(workdate, start)
                t = accumulatedMinutes - totalMinutes
                t = workHours - t
                return DateUtility.addMinutes(date, t)
    workPeriods.close()
    if (workdate == null):
        return DateUtility.addMinutes(reportDate, totalMinutes)
    date = DateUtility.combineDate(workdate, end)
    return DateUtility.addMinutes(date, totalMinutes - accumulatedMinutes)

Comments

Post a Comment