<%@page contentType="text/html;charset=UTF-8" autoFlush="true"%>
<%@page contentType="text/html;charset=UTF-8" autoFlush="true"%>
<%@page import="psdi.webclient.system.controller.*,psdi.webclient.system.beans.*, psdi.mbo.*, java.io.*,
 com.ibm.json.java.JSONArray,
 com.ibm.json.java.JSONObject,
 java.lang.Exception,
 psdi.webclient.system.session.*,
 psdi.server.MXServer,
 psdi.security.UserInfo,
 java.text.SimpleDateFormat,
 java.util.Date,
java.util.Calendar,
 java.lang.*"%>
 <%

 String retour = "";

try {
    // get main record id
	int talPlanifId = Integer.parseInt(request.getParameter("talPlanifId"));
	
	// get mboSet for main object
	MXServer mxServer = MXServer.getMXServer();
	UserInfo userInfo = mxServer.getSystemUserInfo();

	MboSetRemote	talPlanifSet = mxServer.getMboSet("TAL_PLANIF",userInfo);
	talPlanifSet.setWhere("TAL_PLANIFID="+talPlanifId);
	MboRemote		talPlanif	= talPlanifSet.getMbo(0);

    JSONArray workordersArray=new JSONArray();
    JSONObject jsonWOS = new JSONObject();
    if (talPlanif != null) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); 
        JSONObject jsonFIN = new JSONObject();
        String wo_wc = "rownum<2";
        if (!talPlanif.isNull("TAL_WO_WC"))
            wo_wc = talPlanif.getString("TAL_WO_WC");
        wo_wc = wo_wc;
        MboSetRemote WoRemoteSet = mxServer.getMboSet("TAL_WORKORDER",userInfo);
        WoRemoteSet.setWhere(wo_wc);
        MboRemote WoRemote = null;

        for (int i = 0; ((WoRemote = WoRemoteSet.getMbo(i))) != null; i++) {

            JSONObject jsonWO = new JSONObject();
            jsonWO.put("workorderID", WoRemote.getInt("WORKORDERID"));
            jsonWO.put("WONUM", WoRemote.getString("WONUM"));
            jsonWO.put("WOGROUP", WoRemote.getString("WOGROUP"));
            jsonWO.put("siteID", WoRemote.getString("SITEID"));
            jsonWO.put("siteDescription", WoRemote.getString("SITEDESCRIPTION"));
            jsonWO.put("orgID", WoRemote.getString("ORGID"));
            jsonWO.put("workorderDescription", WoRemote.getString("DESCRIPTION"));
            jsonWO.put("workorderStatus", WoRemote.getString("STATUS"));
            jsonWO.put("woStatusDescription", WoRemote.getString("WOSTATUSDESCRIPTION"));
            jsonWO.put("workorderSystemStatus",WoRemote.getString("WORKORDERSYSTEMSTATUS"));
            jsonWO.put("istask", WoRemote.getInt("ISTASK"));
            jsonWO.put("woTotalAssignments", WoRemote.getInt("WOTOTALASSIGNMENTS"));
            jsonWO.put("woNonAssignedAssignments", WoRemote.getInt("WONONASSIGNEDASSIGNMENTS"));
            jsonWO.put("woAssignedAssignments", WoRemote.getInt("WOASSIGNEDASSIGNMENTS"));
            jsonWO.put("STATUS", WoRemote.getString("STATUS"));
            jsonWO.put("JPNUM", WoRemote.getString("JPNUM"));
            jsonWO.put("LOCATION", WoRemote.getString("LOCATION"));
            jsonWO.put("ASSETNUM", WoRemote.getString("ASSETNUM"));
            jsonWO.put("WOEQ5", WoRemote.getString("WOEQ5"));
            jsonWO.put("WOEQ7", WoRemote.getString("WOEQ7"));
            String assetNum = WoRemote.getString("ASSETNUM");
            if (assetNum != null && !assetNum.isEmpty()) {
                jsonWO.put("assetNum",assetNum );
                jsonWO.put("assetDescription", WoRemote.getString("ASSETDESCRIPTION"));
            }            

            String workorderPriority = WoRemote.getString("WOPRIORITY");
            if (workorderPriority != null && !workorderPriority.isEmpty())
                jsonWO.put("workorderPriority", workorderPriority);
			
            String woParent = WoRemote.getString("PARENT");
            if (woParent != null && !woParent.isEmpty()) {
                jsonWO.put("woParent",woParent);
                jsonWO.put("woParentDescription",WoRemote.getString("WOPARENTDESCRIPTION"));
            }

            String woJobPlan = WoRemote.getString("JPNUM");
            if (woJobPlan != null && !woJobPlan.isEmpty()) {
                jsonWO.put("woJobPlan",woJobPlan);
                jsonWO.put("woJobPlanDescription",WoRemote.getString("WOJOBPLANDESCRIPTION"));
            }
            
            String woPm = WoRemote.getString("PMNUM");
            if (woPm != null && !woPm.isEmpty()) {
                jsonWO.put("woPm",woPm);
                jsonWO.put("woPmDescription",WoRemote.getString("WOPMDESCRIPTION"));
            }

            String woWorkType = WoRemote.getString("WORKTYPE");
            if (woWorkType != null && !woWorkType.isEmpty()) {
                jsonWO.put("woWorkType",woWorkType);
                jsonWO.put("woWorkTypeDescription",WoRemote.getString("WOWORKTYPEDESCRIPTION"));
            }

            String woLocation = WoRemote.getString("LOCATION");
            if (woLocation != null && !woLocation.isEmpty()) {
                jsonWO.put("woLocation",woLocation);
                jsonWO.put("woLocationDescription",WoRemote.getString("WOLOCATIONDESCRIPTION"));
            }

            // Get Dates
            Date TARGSTARTDATE = WoRemote.getDate("TARGSTARTDATE");
            Date TARGCOMPDATE = WoRemote.getDate("TARGCOMPDATE");
            Date SCHEDSTART = WoRemote.getDate("SCHEDSTART");
            Date SCHEDFINISH = WoRemote.getDate("SCHEDFINISH");
            Date SNECONSTRAINT = WoRemote.getDate("SNECONSTRAINT");
            Date FNLCONSTRAINT = WoRemote.getDate("FNLCONSTRAINT");
            
            if (TARGSTARTDATE!=null)
                jsonWO.put("woTargStartDate",sdf.format(TARGSTARTDATE));
            if (TARGCOMPDATE!=null)
                jsonWO.put("woTargCompDate",sdf.format(TARGCOMPDATE));
            if (SCHEDSTART!=null)
                jsonWO.put("woSchedStart",sdf.format(SCHEDSTART));
            if (SCHEDFINISH!=null)
                jsonWO.put("woSchedFinish",sdf.format(SCHEDFINISH));
            if (SNECONSTRAINT!=null)
                jsonWO.put("woSNEConstraint",sdf.format(SNECONSTRAINT));
            if (FNLCONSTRAINT!=null)
                jsonWO.put("woFNLConstraint",sdf.format(FNLCONSTRAINT));
            workordersArray.add(jsonWO);
        }
    }
    retour = workordersArray.toString();
} catch (Exception e) {
    out.println("Erreur serveur: " + e.getMessage());
    e.printStackTrace();
}
%><%=retour%>
