COMPILE Subroutine Move_WO_STEP(Dummy)

DECLARE SUBROUTINE SEND_INFO, Utility, Msg, RList, ErrMsg
DECLARE FUNCTION Printer_Select, Get_Printer, Set_Printer, obj_Install

$INSERT ORDER_EQU
$INSERT WO_LOG_EQU
$INSERT PROD_SPEC_EQU
$INSERT WO_STEP_EQU
$INSERT SCHEDULE_EQU
$INSERT RLIST_EQUATES
$INSERT APPCOLORS

ErrorTitle = 'Error in Stored Procedure Move_WO_Step'
ErrorMsg = ''

OPEN 'ORDER' TO OrderTable ELSE
	ErrorMsg = 'Unable to open "ORDER" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

OPEN 'DICT.ORDER' TO @DICT ELSE
	ErrorMsg = 'Unable to open "DICT.ORDER" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END


OPEN 'WO_LOG' TO WOLogTable ELSE
	ErrorMsg = 'Unable to open "WO_LOG" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

OPEN 'PROD_SPEC' TO ProdSpecTable ELSE
	ErrorMsg = 'Unable to open "PROD_SPEC" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

OPEN 'WO_STEP' TO WOStepTable ELSE
	ErrorMsg = 'Unable to open "WO_STEP" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

OPEN 'SCHEDULE' TO SchedTable ELSE
	ErrorMsg = 'Unable to open "SCHEDULE" table.'
	ErrMsg(ErrorTitle:@SVM:ErrorMsg)
	RETURN
END	

SelectSent  =  'SELECT ORDER WITH ENTRY_DATE > =  "1 1 2002" BY LOT_NUM BY ENTRY_DATE BY WO'

RList(SelectSent,TARGET_ACTIVELIST$,'','','')

* Start of printing process

FileName		= 'Move WO Step Report'
Title			= 'Printing Move WO Step Report'

TopMargin		= 1.0
BottomMargin	= 0.75
LeftMargin		= 0.5
RightMargin		= 0.5

Margins = LeftMargin:@FM:TopMargin:@FM:RightMargin:@FM:BottomMargin

PageSetup	= '0'	;* Landscape = 1
PrintSetup	= '2'	;* Preview Normal
PrintPath	= Printer_Select()

stat = Set_Printer('INIT',FileName,Title,Margins,PageSetup,PrintSetup,PrintPath)
IF stat < 0 THEN GOTO OIPrintErr


Header = @VM:obj_Install('Get_Prop','CompTitle'):' Move WO Step Process'
Header<-1> = ''																;* Blank line following heading

font	= 'Arial'
font<2>	= '10'
font<4>	= '1'		;* Bold

stat = Set_Printer('FONTHEADFOOT',font)	; IF stat < 0 THEN GOTO OIPrintErr
stat = Set_Printer('HEADER',Header)		; IF stat < 0 THEN GOTO OIPrintErr

Footer = " 'D' 'T'":@VM:@VM:"Page:  'P'"
stat = Set_Printer('FOOTER',Footer)		; IF stat < 0 THEN GOTO OIPrintErr

@RECCOUNT = 0
FirstPass = 1
LastRecord = 0
FirstLine = 1

* Make Column Heading

colHead			= 'Lot Num'				; colFmt		= '^2160'
colHead<1,2>	= 'Entry Dt'			; colFmt<1,2>	= '<1440'
colHead<1,3>	= 'Ord No'				; colFmt<1,3>	= '^720'
colHead<1,4>	= 'Part Num'			; colFmt<1,4>	= '^2160'
colHead<1,5>	= 'WO Log'				; colFmt<1,5>	= '^720'
colHead<1,6>	= 'Sched ID'			; colFmt<1,6>	= '^720'
colHead<1,7>	= 'Quantity'			; colFmt<1,7>	= '>720'


* Zero Accumulators For Each Break

Quantity.LotNum.Total = 0
Quantity.LotNum.Count = 0
Quantity.OrdNo.Total = 0
Quantity.OrdNo.Count = 0
RunSet = ''

* NULL PREVIOUS BREAK BUCKETS

Prev_LotNum = ''
Last_LotNum_Break  =  1

Prev_OrdNo = ''
Last_OrdNo_Break = 1

* DEFINE DATE CONSTANTS FOR PRINT LIMITING

Today  =  Date()
Sunday  =  Today - MOD(Today,7)
LastSunday  =  Today - ( MOD(Today-1,7) + 1 )

* * * * * * * 
READRECORD:
* * * * * * *

* ZERO BREAK FLAGS TO FALSE
 
LotNum_Break = 0
OrdNo_Break = 0
 

READNEXT @ID, WhichValue ELSE
	LastRecord = 1
	LotNum_Break = 1
	LotNum = Prev_LotNum
	OrdNo_Break = 1
	OrdNo = Prev_OrdNo
END

S_OrdNo  =  @ID
 
IF FirstPass AND LastRecord THEN
	GOTO Bail
END

IF LastRecord THEN GOTO BREAKS
 
IF @REDUCTION_DONE THEN
	READO @RECORD FROM OrderTable, @ID ELSE
		GOTO READRECORD
	END
END

@RECCOUNT + =  1

* CALCULATE VALUE(S) FOR COLUMN(S)

S_OrdNo = {@ID}
I_OrdNo = S_OrdNo

S_EntryDate = {ENTRY_DATE}
I_EntryDate = S_EntryDate

RawPartNum = {PART_NUM}
CONVERT @LOWER_CASE TO @UPPER_CASE IN RawPartNum

M_PartNum = RawPartNum<1,WhichValue> ; P_PartNum = 1 ; C_PartNum = 7
S_PartNum  =  M_PartNum

M_LotNum = {LOT_NUM}<1,WhichValue> ; P_LotNum = 1 ; C_LotNum = 7
S_LotNum  =  M_LotNum

M_WO = {WO}<1,WhichValue> ; P_WO = 1 ; C_WO = 7
S_WO  =  M_WO

M_ScheduleIDs = {SCHEDULE_IDS}<1,WhichValue> ; P_ScheduleIDs = 1 ; C_ScheduleIDs = 7
S_ScheduleIDs  =  M_ScheduleIDs

M_Quantity = {QUANTITY}<1,WhichValue> ; P_Quantity = 1 ; C_Quantity = 7
S_Quantity  =  M_Quantity


PreviousMarkLevel = 0
 
* TEST FOR CONTROL BREAK(S)
 
IF (S_LotNum NE Prev_LotNum) OR LotNum_Break THEN
	LotNum = Prev_LotNum
	Prev_LotNum = S_LotNum
	LotNum_Break += 1
	OrdNo_Break = 3
END

IF (S_OrdNo NE Prev_OrdNo) OR OrdNo_Break THEN
	OrdNo = Prev_OrdNo
	Prev_OrdNo = S_OrdNo
	OrdNo_Break = 1
END


IF FirstPass THEN
	FirstPass = 0
	GOTO DETAIL
END

* * * * * * *
BREAKS:
* * * * * * *

* PRINT BREAK TOTAL(S) AND ACCUMULATE TOTAL(S)

IF OrdNo_Break THEN
	IF Quantity.OrdNo.Total NE 0 OR Quantity.OrdNo.Count NE 0 THEN
		RSCount = COUNT(RunSet,@FM) + (RunSet NE '')
		IF RSCount > 1 THEN
			BaseWO = RunSet<1,5>
			FOR I = 1 TO RSCount
				
				BEGIN CASE
					CASE INDEX(RunSet<I,4>,'1ST',1)				; Step = 1
					CASE INDEX(RunSet<I,4>,'2ND',1)				; Step = 2
					CASE INDEX(RunSet<I,4>,'LAYER 1',1)			; Step = 1
					CASE INDEX(RunSet<I,4>,'LAYER 2',1)			; Step = 2
					CASE INDEX(RunSet<I,4>,'LAYER1',1)			; Step = 1
					CASE INDEX(RunSet<I,4>,'LAYER2',1)			; Step = 2
					CASE INDEX(RunSet<I,4>,'LAYER3',1)			; Step = 1
					CASE INDEX(RunSet<I,4>,'LAYERS A + B',1)	; Step = 1
					CASE INDEX(RunSet<I,4>,'LAYER C',1)			; Step = 1
					CASE INDEX(RunSet<I,4>,'FRONTSIDE',1)		; Step = 1
					CASE INDEX(RunSet<I,4>,'BACKSIDE',1)		; Step = 1
					CASE 1	; Step = 1
				END CASE
				OrgWONo = RunSet<I,5>
				OrgWORec = XLATE('WO_LOG',OrgWONo,'','X')
				WOStepRec = ''
				WOStepKey = BaseWO:'*':Step
				
				WOStepRec<1> = OrgWORec<5>		;* PSN
				WOStepRec<2> = OrgWORec<12>		;* Reactors
				WOStepRec<3> = OrgWORec<15>		;* Cycle Time
				WOStepRec<4> = OrgWORec<22>		;* Schedule ID's
				WOStepRec<7> = OrgWONo
				
				*READ Test FROM WOStepTable,WOStepKey ELSE
					WRITE WOStepRec ON WOStepTable,WOStepKey ELSE Null
					SchedKeys = XLATE('WO_LOG',OrgWONo,22,'X')	;* Schedule Keys
					
					FOR S = 1 TO COUNT(SchedKeys,@VM) + (SchedKeys NE '')
						READ SchedRec FROM SchedTable,SchedKeys<1,S> THEN
							WRITEV Step ON SchedTable,SchedKeys<1,S>,SCHEDULE_WO_STEP_KEY$ ELSE Null
						END	
					NEXT S
				*END
				
				
			NEXT I
		END
		
		stat = Set_Printer('TEXT')
		Quantity.LotNum.Total += Quantity.OrdNo.Total	; Quantity.OrdNo.Total = 0
		Quantity.LotNum.Count += Quantity.OrdNo.Count	; Quantity.OrdNo.Count = 0
		RunSet = ''
	END
END

IF LotNum_Break THEN

	IF Quantity.LotNum.Total NE 0 OR Quantity.LotNum.Count NE 0 THEN
		stat = Set_Printer('TEXT')
		Quantity.LotNum.Total = 0
		Quantity.LotNum.Count = 0
	END
	
END

* PERFORM LAST RECORD OUTPUT IF DONE

IF LastRecord THEN
		
	status = Set_Printer('TEXT','')
	status = Set_Printer('TEXT',@RECCOUNT:' Records Processed')
	
	GOTO Bail
END

* * * * * * *
DETAIL:
* * * * * * *

* REMOVE APPROPRIATE VALUE FROM MULTI-VALUED COLUMN(S)

NewMarkLevel = 0

IF C_PartNum GE PreviousMarkLevel THEN
	REMOVE I_PartNum FROM M_PartNum AT P_PartNum SETTING C_PartNum
	S_PartNum = I_PartNum
END
IF C_PartNum GT NewMarkLevel THEN NewMarkLevel = C_PartNum

IF C_LotNum GE PreviousMarkLevel THEN
	REMOVE I_LotNum FROM M_LotNum AT P_LotNum SETTING C_LotNum
	S_LotNum = I_LotNum
END
IF C_LotNum GT NewMarkLevel THEN NewMarkLevel = C_LotNum

IF C_WO GE PreviousMarkLevel THEN
	REMOVE I_WO FROM M_WO AT P_WO SETTING C_WO
	S_WO = I_WO
END
IF C_WO GT NewMarkLevel THEN NewMarkLevel = C_WO

IF C_ScheduleIDs GE PreviousMarkLevel THEN
	REMOVE I_ScheduleIDs FROM M_ScheduleIDs AT P_ScheduleIDs SETTING C_ScheduleIDs
	S_ScheduleIDs = I_ScheduleIDs
END
IF C_ScheduleIDs GT NewMarkLevel THEN NewMarkLevel = C_ScheduleIDs

IF C_Quantity GE PreviousMarkLevel THEN
	REMOVE I_Quantity FROM M_Quantity AT P_Quantity SETTING C_Quantity
	S_Quantity = I_Quantity
END
IF C_Quantity GT NewMarkLevel THEN NewMarkLevel = C_Quantity

* DO CONVERSIONS IF ANY

IF S_EntryDate NE ""	THEN S_EntryDate = OCONV(S_EntryDate,"D2/")
IF S_WO NE ""			THEN S_WO = OCONV(S_WO,"MD0")
IF S_ScheduleIDs NE ""	THEN S_ScheduleIDs = OCONV(S_ScheduleIDs,"MD0")
IF S_Quantity NE ""		THEN S_Quantity = OCONV(S_Quantity,"MD0")
 
* DO PRINT LIMITING
 
IF ( INDEX( S_PartNum , "LAYER" ,1) ) OR ( INDEX(S_PartNum,"SIDE",1) ) THEN

	IF NUM(I_Quantity) ANd I_Quantity NE '' THEN
		Quantity.OrdNo.Total += I_Quantity
		Quantity.OrdNo.Count += 1
	END
	GOTO ENDLIMIT
END

IF NewMarkLevel	THEN GOTO DETAIL
IF WhichValue THEN GOTO READRECORD
IF PreviousMarkLevel THEN
	GOTO READRECORD
END

S_PartNum = ''
I_PartNum = ''
S_LotNum = ''
I_LotNum = ''
S_WO = ''
I_WO = ''
S_ScheduleIDs = ''
I_ScheduleIDs = ''
S_Quantity = ''
I_Quantity = ''

* * * * * * *
ENDLIMIT:
* * * * * * *

* PRINT DETAIL LINE


colData = ""
colData<1,1> = S_LotNum
colData<1,2> = S_EntryDate
colData<1,3> = S_OrdNo
colData<1,4> = S_PartNum
colData<1,5> = S_WO
colData<1,6> = S_ScheduleIds
colData<1,7> = S_Quantity

RunSet<-1> = colData

GOSUB PrintTable


IF NewMarkLevel THEN
	S_OrdNo = ''
	S_EntryDate = ''
	S_PartNum = ''
	S_LotNum = ''
	S_WO = ''
	S_ScheduleIDs = ''
	S_Quantity = ''
	PreviousMarkLevel = NewMarkLevel
	GOTO DETAIL
END

GOTO READRECORD
 

* * * * * * *
Bail:
* * * * * * *

stat = Set_Printer('TERM',1)

RETURN


* * * * * * *
OIPrintErr:
* * * * * * *

ErrMsg(ErrorTitle:@SVM:ErrorMsg)
ErrMsg(ErrorTitle:@SVM:'Set_Printer returned errorcode ':stat)
stat = Set_Printer('TERM',1)

RETURN


* * * * * *
PrintTable:
* * * * * * 


stat = Set_Printer('CALCTABLE',colFmt:@FM:colData)
IF stat = -7 THEN GOTO Bail				;* User canceled
TableSize = Get_Printer('CALCTABLE')
TableHeight = TableSize<2>
Test = Get_Printer('POS')<2>

IF Get_Printer('POS')<2> + TableHeight > 9.00 OR FirstLine THEN
	IF NOT(FirstLine) THEN
		stat = Set_Printer('PAGEBREAK')
	END
	FirstLine = 0
	font<2> = 8
	font<4> = 1		;* Bold
	stat = Set_Printer('FONT',font,'100')

	stat = Set_Printer('ADDTABLE',colFmt,colHead,'',LTGREY$,'',0,TB_ALL)
	
	
	font<4> = 0
	stat = Set_Printer('FONT',font,fontSpacing)
	
	stat = Set_Printer('ADDTABLE',colFmt,'',colData,LTGREY$,'',0,7)
	
END ELSE
	font<2> = 8
	font<4> = 0
	stat = Set_Printer('FONT',font,fontSpacing)
	stat = Set_Printer('ADDTABLE',colFmt,'',colData,LTGREY$,'',1,TB_ALL)

END
	
							
RETURN	 


