Function WO_Set_Schedule(void)

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

    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 Set Schedule

    Description :   

	Tags		:	[SRP]

    Parameters  :

    History (Date, Initials, Notes)
        09/29/08    fjt     Initial development
        05/28/09	fjt		Last updated

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

$insert SRP_APP_INSERTS

Declare subroutine	Update_Index, Rlist
Declare function	SVC_Scheduler, SVC_Process_Time, SVC_Dates, ReadNextA
Declare function	SRP_Sort_Array, SRP_Clean_Array, SRP_Wait, MemberOf

Process = 0
Begin Case
	
	Case @USER4 _eqc "SRP_FRANK"
		Loop
			Process += 1
			On Process GoSub Init, Open, Build_Appt_List, Term
		Until (Error OR Done)
		Repeat
		
	Case MemberOf(@USER4, "MASTER_SCHED")
		Loop
			Process += 1
			On Process GoSub Init, Open, Time_Checks, Bump, Build_Appt_List, Term
		Until (Error OR Done)
		Repeat
		
	Case 1
		Loop
			Process += 1
			On Process GoSub Init, Open, Build_Appt_List, Term
		Until (Error OR Done)
		Repeat
		
End Case
Wait = SRP_Wait("Term", Wait)

Return Ans


!----- METHODS ------------------------------------------------------------------------------------------


Init:
	Ans    = ""
	Done   = No$
	Error  = ""
	Prefs  = ""
	LenPos = 1
	Delim  = ""
	Locked = ""
	
	// Historical to display
	Config  = Xlate("CONFIG", "MSC_CONFIGURATION", "", "X")
	History = Config<10>
	If History EQ "" then History = 4
return


Open:
	// Open all the tables
	Table = "WO_MAST_SCHED"
	Open Table			to hTable else hTable = ""
	Open "DICT.":Table	to hDict  else hDict = ""
	
	// Update indexes
	Wait = SRP_Wait("Init", "Scheduler Load":@RM:"Updating indexes...")
	Update_Index(Table, "SCHED_DT", No$, Yes$)
return


Time_Checks:
	// Check to see if any in-process appointments need to have an estimate number and check the pace
	Ctr  = 0
	List = SVC_Process_Time("ProcToday")
	Cnt  = Count(List, @FM) + (List NE "")
	EOL  = Len(List)
	Pos  = 1
	
	List.B = ""

	Loop
		Ctr += 1
		Row  = ReadNextA(List, Pos, @FM)
		Key  = Row<0,2>
		Txt  = "Checking appointment time estimates and pace... "
		rv   = SRP_Wait("Update", Wait :@RM: Ctr/Cnt :@RM: Txt :@RM: Key)
		Read Rec from hTable, Key then
			
			GoSub Check_Estimate
			GoSub Check_Pace
			
		end
	While Pos LT EOL
	Repeat
return



Bump:
	// This process adjusts appointments to the right of the appointment in process
	Ctr = 0
	Pos = 1
	
 	Loop
		Ctr  += 1
		Row   = ReadNextA(List, Pos, @FM)
		React = Row<0,1>
		Appts = ""

		// Build a list of appointments for the reactor from the current day forward
		For Date = Date() to Date()+60
			Txt = "Bumping... "
			rv  = SRP_Wait("Update", Wait :@RM: Ctr/Cnt :@RM: Txt :@RM: Key)
			rv  = Xlate("WO_MAST_SCHED_DSR", Date:"*":React, 1, "X")
		Until rv EQ ""
			Cnt.A = Count(rv, @VM) + (rv NE "")
			For a = 1 to Cnt.A
				Appt = rv<0,a>
				Rec = Xlate(Table, Appt, "", "X")
				Gosub Define_Schedule_Times
				Appts<-1> = Appt :@VM: T_Start :@VM: T_End :@VM: Stoppage :@VM: T_End - T_Start
			Next a
		Next Date

		Appts = SRP_Sort_Array(Appts, "AR2", Yes$)
		Appts = SRP_Clean_Array(Appts, @FM, "UNIQUE")
		
		// The first appointment is really the only one the is subject to change based on the stoppage
		OK = (Appts<1,3> + Appts<1,4>) EQ (Appts<2,2>)
		Begin Case
			Case Appts<2> EQ ""
			Case OK
			Case Otherwise$	; GoSub Bump_Sched
		End Case

 	While Pos LT EOL
 	Repeat
return


Bump_Sched:
	Sched = ""

	Cnt.A = Count(Appts, @FM) + (Appts NE "")
	For a = 1 to Cnt.A
		Begin Case
			Case a EQ 1
				// First appointment remains the same
				Sched<-1> = Appts<1,1> :@VM: Appts<1,2> :@VM: Appts<1,3>
			Case a EQ 2
				Prev_End  = Appts<1,3> + Appts<1,4>
				New_End   = Prev_End + Appts<2,5>
				Sched<-1> = Appts<a,1> :@VM: Prev_End :@VM: New_End
			Case Otherwise$
				Prev_End  = Sched<a-1,3>
				New_End   = Prev_End + Appts<a,5>
				Sched<-1> = Appts<a,1> :@VM: Prev_End :@VM: New_End
		End Case
	Next a
	
	Cnt.S = Count(Sched, @FM) + (Sched NE "")
	For s = 1 to Cnt.S
		Key = Sched<s,1>
		Read Rec from hTable, Key then
			T_Start = SVC_Dates("ICONV", Sched<s,2>)
			T_End   = SVC_Dates("ICONV", Sched<s,3>)
			Rec<14> = T_Start<1>
			Rec<16> = T_Start<2>
			Rec<21> = T_End<1>
			Rec<22> = T_End<2>
			Write Rec to hTable, Key else null
		end
	Next s
return


Define_Schedule_Times:
	StartDate  = Rec<14>
	StartTime  = Rec<16>
	EndDate    = Rec<21>
	EndTime    = Rec<22>
	Stoppage   = Oconv(Rec<29>, "MD1")
	
	If EndTime EQ 0 then EndTime = Iconv("11:59:59PM", "MT")

    T_Start    = Oconv(StartDate, "D4/")
    T_Start<2> = Oconv(StartTime, "MTS")
    T_Start    = Iconv(T_Start<1> :" ": T_Start<2>, "DT")
    T_End	   = Oconv(EndDate, "D4/")
    T_End<2>   = Oconv(EndTime, "MTS")
	T_End      = Iconv(T_End<1> :" ": T_End<2>, "DT")
return


Build_Appt_List:
	Ctr = 0
	EOL = No$
	
	// Select
	Stmt = "SELECT WO_MAST_SCHED WITH SCHED_DT GT " : Quote(OConv((Date() - History), 'D4/'))
	Rlist(Stmt, 5, "", "", "")

	Data  = Str(\00\, 64000)
	Ptr   = 1

	Loop
		Readnext Key then Ctr += 1 else EOL = Yes$
	Until EOL
		Call Yield()
		Read Record from hTable, Key else Record = ""
		rv = SRP_Wait("Update", Wait :@RM: Ctr/@RECCOUNT :@RM: "Building appointments, please wait... ":@RM:Key)
		
		//  Build the appointment record
		Row = SVC_Scheduler("Build_Appt_Rec", Key, Record)
		If Record<17> then Locked := Key :@FM
		
		// Append the data
		PutBinaryValue(Data, Ptr, CHAR, Row :@FM)
		Ptr += GetByteSize(Row :@FM)
            
	Repeat
	
	If Data[-1,1] EQ @FM then Data[-1,1] = ""
	If Locked[-1,1] EQ @FM then Locked[-1,1] = ""
	Ans = Data :@RM: Locked
return


Term:
	Done = Yes$
return


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


Check_Estimate:
	WO = Rec<02>
	If Rec<20> LE 0 then
		rv = SVC_Process_Time("ProcTime", WO, Rec)
		Rec<20> = Field(rv, @RM, 1)
		Write Rec to hTable, Key else null
	end
return


Check_Pace:
	Pace = SVC_Process_Time("RDSpace", WO)
	If Pace GT 0 then
		Rel = Xlate("WO_MAST_SCHED_REL", WO, "REACTOR_COUNT", "X")
		If Rel LT 1 then Rel = 1
		Est  = Rec<20> * Rel
		Diff = Pace - Est
		
		If Diff GE 120 then
			Stoppage = Int(Diff / 240)
			Half = Mod(Diff, 240)
			
			If Half LE 120 then
				Stoppage = Stoppage:"5"
			end else
				Stoppage = Stoppage+1:"0"
			end
			
			List.B<-1> = Key :@VM: Stoppage
			Rec<29> = Stoppage
			Write Rec to hTable, Key else null
		end
	end
return

