Function DBW_WO_MAST_SCHED_EVENTS(CtrlEntId, Event, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15)

/******************************************************************************************************** 

    This program is proprietary and is not to be used by or disclosed to others, nor is it to
    be copied without written permission from SRP Computer Solutions, Inc.

    Name        :   WO Master Scheduler Events

    Description :
    
	Tags		:	[SRP]

    Parameters  :

    History (Date, Initials, Notes)
        09/17/2008	fjt     Initial development

********************************************************************************************************/

$insert SRP_APP_INSERTS
$insert SRP_EVENT_SETUP
$insert WO_SCHEDULER_INSERTS

Equ PRS_LAYER_RECIPE$	to 2

Declare subroutine SRP_Show_Window, SRP_Redirect_OLE_Events
Declare function   SVC_Scheduler, SVC_Dopant, SVC_Option_Button, SVC_SubClass_DropDown, SVC_Change_Log
Declare function   SVC_Process_Time, Win_Check_Req_Fields, obj_Prod_Spec, Utility_Services, NextKey

Begin Case

	Case Event _eqc "Click"
		Begin Case
			Case Control EQ "PUB_CANCEL"		; GoSub Click.PUB_CANCEL
			Case Control EQ "PUB_DELETE"		; GoSub Click.PUB_DELETE
			Case Control EQ "PUB_SAVE"			; GoSub Click.PUB_SAVE
		End Case

	Case Event _eqc "LostFocus"
		Begin Case
			Case Control EQ "EDL_REACT_NO"		; GoSub LostFocus.EDL_REACT_NO
			Case Control EQ "EDL_SCHED_DT"		; GoSub LostFocus.EDL_SCHED_DT
			Case Control EQ "EDL_WO_NO"			; GoSub LostFocus.EDL_WO_NO
		End Case

	Case Event _eqc "OnOptionClick"
		Control = Field(Param1, ".", 2)
		Begin Case
			Case Control EQ "EDL_END_DATE"		; GoSub OnOptionClick.EDL_END_DATE
			Case Control EQ "EDL_PROC_TIME"		; GoSub OnOptionClick.EDL_PROC_TIME
			Case Control EQ "EDL_SCHED_DT"		; GoSub OnOptionClick.EDL_SCHED_DT
		End Case

    Case Control EQ Window
        Begin Case
        	Case Event EQ "CLOSE"				; Gosub Window.CLOSE
            Case Event EQ "CREATE"              ; GoSub Window.CREATE
            Case Event EQ "READ"            	; GoSub Window.READ
            Case Event EQ "SIZE"				; Gosub Window.SIZE
            Case Event EQ "WRITE"				; GoSub Window.WRITE
        End Case

End Case

If Assigned(Ans) else Ans = 1
Return Ans


!----- EVENTS -------------------------------------------------------------------------------------------


CLICK.PUB_CANCEL:
	Set_Property(@Window, "SAVEWARN", No$)
	Send_Event(@Window, "CLOSE")
	Ans = 0
return


CLICK.PUB_DELETE:
	Key = Get_Property(@Window, "ID")
	Rec = Get_Property(@Window, "ATRECORD")
	
	Send_Event(@Window, "DELETE")
	End_Dialog(@Window, Key :@RM: "DELETE")
	rv  = SVC_Scheduler("UnFlagSched", Key, Rec)
	rv  = SVC_Scheduler("RemoveAppts", Key, Rec)
	rv  = SVC_Change_Log("RemoveAppts", Key, Rec, Prev)
	Ans = 0
return


CLICK.PUB_SAVE:
	Key = Get_Property(@Window, "ID")
	Rec = Get_Property(@Window, "ATRECORD")
	OK.Fields = Win_Check_Req_Fields(@Window)
	
	PSN 	 = Get_Property(@Window:".EDL_PSN_NO", "DEFPROP")
	Wafer    = Field(Xlate("PROD_SPEC", PSN, "SUB_WAFER_INCHES", "X"), " ", 1)
	Event    = Get_Property(@Window:".EDL_EVENT", "DEFPROP")
	Reactor  = Get_Property(@Window:".COB_REACT_NO", "TEXT")
	OK.Wafer = SVC_Scheduler("CheckWaferSize", "PUB_SAVE", Key, Reactor, Wafer, Event)
	
	GoSub Dopant_Check
	If Gas.Check.On$ then OK.Dopant = rv<3> else OK.Dopant = Yes$
	If OK.Dopant EQ No$ then
		Err = "Appointment save is not allowed due to a source/dopant mismatch."
		rv  = Utility_Services("Balloon", Error, @Window:".EDL_SOURCE_GAS", "", Err)
	end
	
	If OK.Fields and OK.Wafer and OK.Dopant then
		GoSub Valid_Date
		If Valid then GoSub Write_and_Update
	end
return


LOSTFOCUS.EDL_REACT_NO:
	rv = Get_Property(CtrlEntID, "DEFPROP")
	If rv EQ "" then Set_Property(CtrlEntID, "FOCUS", Yes$)
return


LOSTFOCUS.EDL_SCHED_DT:
	GoSub Process_Date_and_Time
	rv = Get_Property(CtrlEntID, "DEFPROP")
	If rv EQ "" then Set_Property(CtrlEntID, "FOCUS", Yes$)
return


LOSTFOCUS.EDL_WO_NO:
	Key   = Get_Property(CtrlEntId, "ID")
	WO_No = Get_Property(CtrlEntId, "DEFPROP")
	Begin Case
		Case WO_No EQ ""
		Case RowExists("WO_MAST_SCHED", Key)
			rv = Get_Property(@Window, "@WO", WO_No)
			If WO_No NE rv then Gosub Prefill_WO
		Case Otherwise$
			Gosub Prefill_WO
	End Case
	Set_Property(@Window, "@WO", WO_No)
return


OnOptionClick.EDL_PROC_TIME:
	WO_No = Get_Property(@Window:".EDL_WO_NO", "DEFPROP")
	Key   = Get_Property(@Window:".EDL_APPT", "DEFPROP")
	GoSub Prefill_WO
	Set_Property(@Window, "@WO", WO_No)
return


OnOptionClick.EDL_END_DATE:
OnOptionClick.EDL_SCHED_DT:
	GoSub Calendar
return


Window.CLOSE:
	Set_Property(@Window, "SAVEWARN", No$)
return


Window.CREATE:
	// Check required fields on save
	rv = Get_Property(@Window, "IOOPTIONS")
	rv<10> = Yes$
	Set_Property(@Window, "IOOPTIONS", rv)
	
	rv = SVC_SubClass_DropDown("Sched Events", @Window:".EDL_EVENT")
	rv = SVC_Option_Button("Add", @Window, "EDL_PROC_TIME",  "bmps/refresh1.bmp")
	rv = SVC_Option_Button("Add", @Window, "EDL_TOTAL_PACE", "bmps/blank.bmp")
	rv = SVC_Option_Button("Add", @Window, "EDL_SCHED_DT",   "bmps/dropcal.bmp")
	rv = SVC_Option_Button("Add", @Window, "EDL_END_DATE",   "bmps/dropcal.bmp")

	// Reactor dropdown
	rv = ""
	For i = 20 to 74
		rv := i :@FM
	Next i
	If @USER4 EQ "SRP_FRANK" then rv := 99 :@FM
	rv[-1,1] = ""
	Set_Property(@Window:".COB_REACT_NO", "LIST", rv)

	If Param1 GT "" then
		// Passed in information
		Key = Field(Param1, @RM, 1)
		Begin Case
			Case Key EQ "REACTOR"
				React = Field(Param1, @RM, 2)
				Key   = NextKey("WO_MAST_SCHED")
				Set_Property(@Window:".EDL_APPT", "DEFPROP", Key)
				Send_Event(@Window, "READ")
				Set_Property(@Window:".COB_REACT_NO", "TEXT", React)
				Ctrl = @Window:".EDL_WO_NO"
 				Set_Property(Ctrl, "FOCUS", Yes$)
			Case Otherwise$
				Set_Property(@Window:".EDL_APPT", "DEFPROP", Key)
				Send_Event(@Window, "READ")
		End Case
	end

	SRP_Redirect_OLE_Events()
	SRP_Show_Window(@Window, "", "C", "C", 1)
return


Window.READ:
	// Set original WO number
	WO_No = Get_Property(CtrlEntId, "DEFPROP")
	Rec   = Get_Property(CtrlEntId, "ATRECORD")
	Set_Property(@Window, "@WO", WO_No)
	Set_Property(@Window, "@PREV_REC", Rec)
	Set_Property(@Window, "SAVEWARN", No$)

	GoSub Dopant_Check
	GoSub Qualify_Check
return


Window.SIZE:
	// Make sure scheduler window redraws after moving this window around (prevents ghost image)
	Set_Property("WO_MASTER_SCHEDULER.OLE_SCHEDULE", "OLE.Redraw", Yes$)
return


Window.WRITE:
return


!----- INTERNAL ROUTINES --------------------------------------------------------------------------------


Calendar:
	Default	  = Iconv(Get_Property(Param1, "DEFPROP"), "D4/")
	WinPos	  = Get_Property(@Window, "SIZE")
	CtrlPos	  = Get_Property(Param1, "SIZE")
	InitParam = Default:@FM:@Window:@FM:(CtrlPos<1> + WinPos<1>) + 3 :@FM:(CtrlPos<2> + WinPos<2> + CtrlPos<4> + 30)
	Date	  = Dialog_Box("POPUP_MONTH", @Window, InitParam)
	If (Date) then
		Set_Property(Param1, "DEFPROP", Oconv(Date, "D4/"))
		Ans = 0
	end
return


Write_and_Update:
	New  = Not(RowExists("WO_MAST_SCHED", Key))
	Prev = Get_Property(@Window, "@PREV_REC")
	Send_Event(@Window, "WRITE")
	End_Dialog(@Window, Key :@RM: Rec)
	Begin Case
		Case New ; rv = SVC_Scheduler("AddAppts", Key, Rec)
		Case 1	 ; rv = SVC_Scheduler("Appt", Key, Rec)
	End Case
	rv = SVC_Scheduler("FlagSched", Key, Rec)
	rv = SVC_Change_Log("Compare", Key, Rec, Prev)
	Ans = 0
return


Prefill_WO:
	Equ Prod_Spec_Spec_Epi$	to 15
	Equ QSEpiDopant$		to 10

	WO_Log  = Xlate("WO_LOG", WO_No, "", "X")
	OrderNo = WO_Log<29>
	ItemNos = WO_Log<30>
	StepKey = WO_No:"*1" ; // * StepKey = WO_Log<34>
	
	// WO Log
	WO_Mat   = Xlate("WO_MAT", StepKey, "", "X")
	WO_Step  = Xlate("WO_STEP", StepKey, "", "X")
	Status   = Xlate("WO_LOG", WO_No, "CURR_STATUS", "X")
	Released = Xlate("WO_LOG", WO_No, "REL_STAMP", "X") GT ""
	EPIpart  = Xlate("WO_LOG", WO_No, "ORDER_ITEM", "X")<1,1>

	// PSN
	PSN		= Xlate("QUOTE", WO_Log<27>, 33, "X")<1,1>
	PSrec   = Xlate("PROD_SPEC", PSN, "", "X")
	RunType = Xlate("PROD_SPEC", PSN, "SPEC_TYPE", "X")
	SpecEpi = PSrec<Prod_Spec_Spec_Epi$>
	Convert Char(247) to @FM in SpecEpi
	Convert Char(248) to @FM in SpecEpi
	Dopant = SpecEpi<1,QSEpiDopant$>
	
	// Part Numbers
	PartNo  = Xlate("ORDER_DET", OrderNo:"*":ItemNos<1,1>, 8, "X")
	* EPIpart = Xlate("PROD_SPEC", PSN, 5, "X")<1,1>
	Wafer   = Xlate("PROD_SPEC", PSN, "SUB_WAFER_INCHES", "X")
	MinPer  = Xlate("PROD_SPEC", PSN, 96, "X")
	MinPer  = Oconv(Iconv(Oconv(MinPer, "MD3"), "MD1"), "MD1")
	
	// RDS
	RDS_Keys   = WO_Step<06>
	RDS_Status = Xlate("REACT_RUN", RDS_Keys, "RUN_STATUS", "X")
	RDS_Cnt    = Count(RDS_Status, @VM) + (RDS_Status NE "")
	RDS_Comp   = Count(RDS_Status, "COMP") + Count(RDS_Status, "RFQ")
	
	// RDS Completed
	RDS_Comp   = 0
	RDS_Keys   = WO_Step<06>
	RDS_Unload = Xlate("REACT_RUN", RDS_Keys, "UNLOAD_SIG", "X")
	RDS_Cnt    = Count(RDS_Keys, @VM) + (RDS_Keys NE "")
	Pos.x = 0 ; Delim.x = ""
	Loop
		Remove Val from RDS_Unload at Pos.x setting Delim
		If Val GT "" then RDS_Comp += 1
	Until Delim EQ 0
	Repeat
	
	// Fields
	Set_Property(@Window:".EDL_CUST_NO",	 "DEFPROP", WO_Log<03>)
	Set_Property(@Window:".EDL_PART_NO",	 "DEFPROP", PartNo)
	Set_Property(@Window:".EDL_EPI_PART_NO", "DEFPROP", EPIpart)
	Set_Property(@Window:".EDL_PSN_NO",		 "DEFPROP", PSN)
	Set_Property(@Window:".EDL_WO_STATUS",	 "DEFPROP", Status)
	Set_Property(@Window:".EDL_STEP_KEY",	 "DEFPROP", StepKey)
	Set_Property(@Window:".EDL_RDS_COUNT",	 "DEFPROP", RDS_Cnt)
	Set_Property(@Window:".EDL_RDS_COMP",	 "DEFPROP", RDS_Comp)
	Set_Property(@Window:".EDL_RUN_TYPE",	 "INVALUE", RunType)
	Set_Property(@Window:".CHK_RELEASED",	 "CHECK",   Released)
	
	GoSub Process_Date_and_Time
	GoSub Dopant_Add
	GoSub Dopant_Check
	GoSub Qualify_Check

 	Send_Event(@Window:".EDL_CUST_NAME",  "CALCULATE")
 	Send_Event(@Window:".EDL_WAFER_SIZE", "CALCULATE")
 	Send_Event(@Window:".EDL_WAFER_QTY",  "CALCULATE")
 	Send_Event(@Window:".EDL_COMPLETED",  "CALCULATE")
 	Ans = 0
return


Process_Date_and_Time:
	// Process Time
	WO_No     = Get_Property(@Window:".EDL_WO_NO", "INVALUE")
	StartDate = Get_Property(@Window:".EDL_SCHED_DT", "INVALUE")
	StartTime = Get_Property(@Window:".COB_START_TIME", "INVALUE")
	React     = Get_Property(@Window:".COB_REACT_NO", "TEXT")
	PSN 	  = Get_Property(@Window:".EDL_PSN_NO", "DEFPROP")
	Last.Appt = ""
	
	If StartDate EQ "" then	StartDate = Date()
	If StartTime else StartTime = 0
	
	// Next available open date and time
	Loop
		rv = Xlate("WO_MAST_SCHED_DSR", StartDate:"*":React, 1, "X")
	Until rv EQ ""
		StartDate += 1
		Last.Appt = rv
	Repeat
	
	Last.Rec  = Get_Property(Ctrl$, "OLE.Appt[":Last.Appt:"]")
	Last.End  = Last.Rec<03>
	Last.Date = Field(Last.End, ".", 1)
	
	Begin Case
		Case Last.Appt EQ ""
			StartDate = Get_Property(@Window:".EDL_SCHED_DT",   "INVALUE")
		Case Field(Last.End, ".", 2) EQ 0
			StartDate = Last.Date
			StartTime = 0
		Case Field(Last.End, ".", 2)[1,1] EQ 5
			StartDate = Last.Date
			StartTime = Iconv("12:00PM", "MT")
	End Case
	
	Rec = ""
	Rec<06> = PSN
	rv = SVC_Process_Time("ProcTime", WO_No, Rec)
	Proc.Hrs = Field(rv, @RM, 1)
	
	rv = SVC_Process_Time("CalcRange", StartDate, StartTime, Proc.Hrs)
	Set_Property(@Window:".EDL_SCHED_DT",   "INVALUE", rv<1>)
	Set_Property(@Window:".COB_START_TIME", "INVALUE", rv<2>)
	Set_Property(@Window:".EDL_END_DATE",   "INVALUE", rv<3>)
	Set_Property(@Window:".COB_END_TIME",	"INVALUE", rv<4>)
	Set_Property(@Window:".EDL_PROC_TIME",	"INVALUE", Proc.Hrs)
return


Dopant_Add:
	LayerSpecs  = obj_Prod_Spec("GetLayerProp", PSN:@RM:@RM:1)	;// Returns specs for all layers in internal format
	
	LayerSpec 	= Field(LayerSpecs, @RM, 1)						;// Take the first Layer
	LayerSet	= Field(LayerSpec, @FM, 1)						;// Not used here but shown for clarity
	LayerSpec	= Field(LayerSpec, @FM, 2, 99)					;// LayerSpec without the LayerSet 
	RecipeNo	= LayerSpec<PRS_LAYER_RECIPE$>
	Recipe  	= Xlate("RECIPE", RecipeNo, "", "X")
	Dopant  	= Recipe<1>
	Source		= Recipe<5>
	
	Set_Property(@Window:".EDL_DOPANT", "DEFPROP", Dopant)
	Set_Property(@Window:".EDL_SOURCE_GAS", "DEFPROP", Source)
return


Dopant_Check:
	Dopant = Get_Property(@Window:".EDL_DOPANT", "DEFPROP")
	Source = Get_Property(@Window:".EDL_SOURCE_GAS", "DEFPROP")
	React  = Get_Property(@Window:".COB_REACT_NO", "TEXT")
 	rv     = SVC_Dopant("Check", Dopant, Source, React)

 	Set_Property(@Window:".EDL_DOPANT", "DEFPROP", Dopant)
	Set_Property(@Window:".EDL_SOURCE_GAS", "DEFPROP", Source)
	Set_Property(@Window:".PIC_SOURCE", "OLE.BackgroundColor", rv<1>)
	Set_Property(@Window:".PIC_DOPANT", "OLE.BackgroundColor", rv<2>)
return


Qualify_Check:
	React  = Get_Property(@Window:".COB_REACT_NO", "TEXT")
	PSN    = Get_Property(@Window:".EDL_PSN_NO", "DEFPROP")
	rv     = SVC_Dopant("Qualify", Qualified, Blocked, React, PSN)

 	Set_Property(@Window:".EDL_QUALIFIED", "DEFPROP", Qualified)
	Set_Property(@Window:".EDL_BLOCKED", "DEFPROP", Blocked)
 	Set_Property(@Window:".PIC_QUALIFIED", "OLE.BackgroundColor", rv<1>)
 	Set_Property(@Window:".PIC_BLOCKED", "OLE.BackgroundColor", rv<2>)
return



Valid_Date:
	Valid = Yes$

	DT.Sdate = Get_Property(@Window:".EDL_SCHED_DT",	"INVALUE")
	DT.Stime = Get_Property(@Window:".COB_START_TIME",	"INVALUE")
	DT.Edate = Get_Property(@Window:".EDL_END_DATE",	"INVALUE")
	DT.Etime = Get_Property(@Window:".COB_END_TIME",	"INVALUE")
	
    DT.Start = Iconv(Oconv(DT.Sdate, "D4/"):" ":Oconv(DT.Stime, "MTS"), "DT")
    DT.End   = Iconv(Oconv(DT.Edate, "D4/"):" ":Oconv(DT.Etime, "MTS"), "DT")

	Begin Case
		Case DT.Sdate and DT.Edate and DT.Stime and DT.Etime
		Case DT.End GT DT.Start
		Case Otherwise$
			Valid = No$
	End Case

	If Not(Valid) then
		rv = Utility_Services("Balloon", Error, @Window:".EDL_SCHED_DT", "Valid Date", "The date, time, or date range is missing or invalid.")
	end
return
