<%@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.TimeZone,
 java.util.Date,
 java.util.Calendar,
 java.lang.Math,
 java.lang.*,
 psdi.util.MXSession,
 psdi.security.UserInfo"%>
<%

String retour = "";

try {

	// get main record id
	int talPlanifId = Integer.parseInt(request.getParameter("talPlanifId"));
	String whereClauseParam = request.getParameter("whereClauseParam");
	// 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 eventsArray=new JSONArray();
	if(talPlanif != null)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");	
		SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");	
		SimpleDateFormat sdfMaximo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date paramStartDateTime=sdf.parse(request.getParameter("start"));
		Date paramEndDateTime=sdf.parse(request.getParameter("end"));

		String wo_wc = "WORKORDERID IS NOT NULL";
		if(!talPlanif.isNull("TAL_WO_WC"))
			wo_wc = talPlanif.getString("TAL_WO_WC");
		// get only assignments with dates
		String startDate= "{ts '"+sdfMaximo.format(paramStartDateTime)+"'}";
		String endDate= "{ts '"+sdfMaximo.format(paramEndDateTime)+"'}";
		wo_wc = wo_wc + "AND (SCHEDULEDATE IS NOT NULL AND SCHEDULEDATE >= "+startDate+" AND SCHEDULEDATE <= "+endDate+")";
		if (whereClauseParam != null)
		{
			wo_wc = wo_wc + "AND ("+whereClauseParam+")";
		}
		MboSetRemote woAssSet = mxServer.getMboSet("TAL_ASSIGNMENT",userInfo);
		woAssSet.setWhere(wo_wc);
		MboRemote woAss=null;
		for(int i = 0; ((woAss=woAssSet.getMbo(i)))!=null; i++)
		{
			JSONObject jsonWoS = new JSONObject();
			JSONObject jsonextendedProps = new JSONObject();
			Integer assingmentId = woAss.getInt("ASSIGNMENTID");		
			String title=woAss.getString("WONUM")+" - "+woAss.getString("DESCRIPTION");
			String labor=woAss.getString("LABORCODE");
			Date STARTDATE = woAss.getDate("STARTDATE");
			Date FINISHDATE = woAss.getDate("FINISHDATE");
			Date SCHEDULEDATE = woAss.getDate("SCHEDULEDATE");
			String assingmentStatus=woAss.getString("STATUS");
			String assignmentSysStatus=woAss.getString("ASSIGNMENTSYSSTATUS");
			Double hours=woAss.getDouble("LABORHRS");

			if (hours == null || hours <= 0)
				hours=1.0;

			Date start = SCHEDULEDATE;
			Date end_ = FINISHDATE;
			if (start == null)
				start=STARTDATE;
			// Case of interrupted assignment
			if (assignmentSysStatus.equals("INTERPT")) {
				int minutes = (int) Math.round((hours * 60));
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(start);
				calendar.add(Calendar.MINUTE, minutes);
				end_ = calendar.getTime();
			}

			if (end_ == null) {
				int minutes = (int) Math.round((hours * 60));
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(start);
				calendar.add(Calendar.MINUTE, minutes);
				end_ = calendar.getTime();
			}		

			// Extended Props		
			jsonextendedProps.put("assingmentId",assingmentId);
			jsonextendedProps.put("assingmentStatus",assignmentSysStatus);	
			jsonextendedProps.put("laborHours",hours);


			if (labor != null && !labor.isEmpty()) {
				jsonextendedProps.put("laborCode",labor);
				jsonextendedProps.put("displayName",woAss.getString("DISPLAYNAME"));
				jsonextendedProps.put("laborCRAFT",woAss.getString("LABORCRAFT"));
				jsonextendedProps.put("laborSKILLLEVEL",woAss.getString("LABORSKILLLEVEL"));
				jsonextendedProps.put("laborVENDOR",woAss.getString("LABORVENDOR"));
				jsonextendedProps.put("laborCONTRACTNUM",woAss.getString("LABORCONTRACTNUM"));
				title=title+" ("+labor+")";
			}
			Integer taskID = woAss.getInt("TASKID");
			if (taskID != null && taskID !=0)
				jsonextendedProps.put("taskID",taskID);

			String craft = woAss.getString("CRAFT");
			if (craft != null && !craft.isEmpty())
				jsonextendedProps.put("craft",craft);
			jsonextendedProps.put("JP_CRAFT",woAss.getString("JP_CRAFT"));
			jsonextendedProps.put("JP_SKILLLEVEL",woAss.getString("JP_SKILLLEVEL"));
			jsonextendedProps.put("JP_VENDOR",woAss.getString("JP_VENDOR"));
			jsonextendedProps.put("JP_CONTRACTNUM",woAss.getString("JP_CONTRACTNUM"));
			jsonextendedProps.put("JP_LABORCODE",woAss.getString("JP_LABORCODE"));
			jsonextendedProps.put("ASS_LABORHRS",woAss.getString("ASS_LABORHRS"));
			
			jsonextendedProps.put("workorderID",woAss.getInt("WORKORDERID"));
			jsonextendedProps.put("WOGROUP",woAss.getString("WOGROUP"));
			jsonextendedProps.put("WONUM",woAss.getString("WONUM"));
			jsonextendedProps.put("workorderSystemStatus",woAss.getString("WORKORDERSYSTEMSTATUS"));
			Integer estDur = woAss.getInt("ESTDUR");
			if (estDur != null && estDur !=0)
			jsonextendedProps.put("estDur",estDur);
			 // Get Dates
	        Date TARGSTARTDATE = woAss.getDate("TARGSTARTDATE");
	        Date TARGCOMPDATE = woAss.getDate("TARGCOMPDATE");
	        Date SCHEDSTART = woAss.getDate("SCHEDSTART");
	        Date SCHEDFINISH = woAss.getDate("SCHEDFINISH");
	        Date SNECONSTRAINT = woAss.getDate("SNECONSTRAINT");
	        Date FNLCONSTRAINT = woAss.getDate("FNLCONSTRAINT");

	
	        if (TARGSTARTDATE!=null)
	            jsonextendedProps.put("woTargStartDate",sdf_date.format(TARGSTARTDATE));
	        if (TARGCOMPDATE!=null)
	            jsonextendedProps.put("woTargCompDate",sdf_date.format(TARGCOMPDATE));
	        if (SCHEDSTART!=null)
	            jsonextendedProps.put("woSchedStart",sdf_date.format(SCHEDSTART));
	        if (SCHEDFINISH!=null)
	            jsonextendedProps.put("woSchedFinish",sdf_date.format(SCHEDFINISH));
	        if (SNECONSTRAINT!=null)
	            jsonextendedProps.put("woSNEConstraint",sdf_date.format(SNECONSTRAINT));
	        if (FNLCONSTRAINT!=null)
	            jsonextendedProps.put("woFNLConstraint",sdf_date.format(FNLCONSTRAINT));
			jsonWoS.put("extendedProps",jsonextendedProps);

			// Standard Props
			jsonWoS.put("id",assingmentId);
			jsonWoS.put("title", title);
			jsonWoS.put("start", sdf.format(start));
			jsonWoS.put("end", sdf.format(end_));
			eventsArray.add(jsonWoS);
	}

} retour =   eventsArray.toString();
} catch (Exception e) {
out.println("Erreur serveur: " + e.getMessage());
e.printStackTrace();
}

%><%=retour%>