COMPILE FUNCTION Comm_Dialog_PO_No_Change(Method, Parm1)
#pragma precomp SRP_PreCompiler

/*
	Commuter module for Dialog_PO_No_Change window.
	
	02/20/2017 - David Klotz & Francois Rivard
*/

DECLARE SUBROUTINE Set_Property, End_Dialog, Set_Status, ErrMsg
DECLARE SUBROUTINE obj_Appwindow, Start_Window, Msg, Btree.Extract
DECLARE SUBROUTINE RList, obj_Tables
DECLARE SUBROUTINE Print_WO_Mat_In_Labels, Print_WMI_Labels, Print_WMO_Labels, Print_Cass_Labels

DECLARE FUNCTION Get_Property, Get_Status, Dialog_Box, Popup, Send_Message, MemberOf
DECLARE FUNCTION obj_Tables
DECLARE FUNCTION Msg, Obj_WO_Step	

EQU CRLF$	TO \0D0A\

$INSERT APPCOLORS
$INSERT COMPANY_EQUATES
$INSERT CUST_EPI_PART_EQUATES
$INSERT EPI_PART_EQUATES
$INSERT LSL_USERS_EQU
$INSERT MSG_EQUATES
$INSERT NOTIFICATION_EQU
$INSERT POPUP_EQUATES
$INSERT LOGICAL
$INSERT PROD_SPEC_EQU
$INSERT PROD_VER_EQUATES
$INSERT QUOTE_EQU
$INSERT QUOTE_DET_EQU
$INSERT QUOTE_SIGS_EQU
$INSERT RDS_EQUATES
$INSERT REACT_RUN_EQUATES
$INSERT SECURITY_RIGHTS_EQU
$INSERT WM_IN_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT WO_LOG_EQUATES
$INSERT WO_STEP_EQU
$INSERT WO_MAT_EQUATES

$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU

EQU COL$CASSETTES TO 1
EQU TARGET_ACTIVELIST$ TO 5

EQU COL$WO_PARAM_CASSETTE TO 1
EQU COL$WO_PARAM_RDS	  TO 2
EQU COL$WO_PARAM_LOT	  TO 3
EQU COL$WO_PARAM_PO 	  TO 4
* EQU COL$WO_PARAM_HOT_LOT  TO 5

ErrTitle = 'Error in Comm_Dialog_PO_No_Change'
ErrorMsg = ''
ErrCode  = ''

Result = ''

BEGIN CASE
	CASE Method = 'Create'				; GOSUB Create
	CASE Method = 'NewPONoLF'			; GOSUB NewPONoLF
	CASE Method = 'SelectAll'			; GOSUB SelectAll
	CASE Method = 'ClearAll'			; GOSUB ClearAll
	CASE Method = 'PrintLabels'			; GOSUB PrintLabels
	CASE Method = 'Update'				; GOSUB Update
	CASE Method = 'Exit'				; GOSUB Exit
	CASE Method = 'Close'				; GOSUB Close
	
	CASE 1
		ErrMsg(ErrTitle:@SVM:'Unknown method ':QUOTE(Method):' passed to routine.')
		
END CASE

RETURN Result


* * * * * * *
Create:
* * * * * * *

obj_AppWindow('Create')

WONo = Parm1[1,@FM]

/* Initialize the window's header with the Work Order number */
Title = 'Work Order: ':WONo:' - Change PO Number'
Set_Property(@WINDOW,'TEXT',Title)

Set_Property(@WINDOW:'.WO_NO','TEXT',WONo)

GOSUB RefreshCassettesList

RETURN


**********
NewPONoLF:
**********
RETURN


************
PrintLabels:
************

RDSListCtrl = @WINDOW:'.WO_NO'
WONo = Get_Property(RDSListCtrl, 'TEXT')

IF (WONo NE '') THEN

	WOSteps = XLATE('WO_LOG',WONo,WO_LOG_WO_STEP_KEY$,'X')

	IF INDEX(WOSteps,@VM,1) THEN
		Buttons = ''
		FOR I = 1 TO COUNT(WOSteps,@VM) + (WOSteps NE '')
			Buttons<I> = 'Step ':I
		NEXT I
		SWAP @FM WITH ',' IN Buttons
		Buttons = 'B':Buttons
		TypeOver = ''
		TypeOver<MTEXT$> 	= 'Which Work Order step do you wish to print labels for?'
		TypeOver<MTYPE$> 	= Buttons
		TypeOver<MICON$> 	= '?'
		TypeOver<MCAPTION$>	= 'MultiStep WorkOrder Selection'
		WOStep = Msg(@window, TypeOver)
		MultiStepFlag = WOStep

	END ELSE
		WOStep = 1
		MultiStepFlag = 0
	END

	IF (WOStep = 1) THEN
		PSNo = XLATE('WO_STEP',WONo:'*1',WO_STEP_PROD_SPEC_ID$,'X')
		ReactorType = XLATE('PROD_SPEC',PSNo,PROD_SPEC_REACTOR_TYPE$,'X')
		BEGIN CASE
			CASE ReactorType = 'GAN'
				Print_WO_Mat_In_Labels(WONo,'')

			CASE ReactorType = 'EPP' OR ReactorType = 'P'
				Print_WMI_Labels(WONo,1)
				Print_WMO_Labels(WONo,1)

			CASE 1
				RDSNos = XLATE('WO_STEP',WOSteps<1,1>,WO_STEP_RDS_KEY$,'X')
				IF INDEX(RDSNos,@VM,1) THEN
					TypeOver = ''
					TypeOver<PDISPLAY$> = RDSNos
					TypeOver<PFIELD$>	= 2
					TypeOver<PTYPE$>	= 'K'
					RDSKeys = Popup(@WINDOW,TypeOver,'WO_LOG_RTS')
				END ELSE
					RDSKeys = RDSNos
				END

				IF RDSKeys = '' THEN 
					ErrMsg('No RDS Numbers Selected or Work Order Not Yet Released to Production.')
				END ELSE
					CONVERT @VM TO @FM IN RDSKeys
					Print_Cass_Labels(RDSKeys,MultiStepFlag)				;* Updated labels with Akrion Bar Codes
				END
		END CASE
		
	END ELSE
		Def = ""
		Def<MTEXT$> = "Adjusting Step ":WOStep:" Scheduled Quantities..."
		Def<MTYPE$> = "U"
		MsgUp = Msg(@window, Def)
		RDSNos = obj_WO_Step('AdjStepRDSQtys', WOStep:@RM:WOSteps)
		
		IF Get_Status(errCode) THEN
			ErrMsg(errCode)											;* Display error from the update
			Msg(@WINDOW,MsgUp)										;* Take down the processing message
			obj_AppWindow('LoadFormKeys',@WINDOW:@RM:WONo)			;* Reload the data in the form
			RETURN
		END
		Msg(@WINDOW,MsgUp)

		IF INDEX(RDSNos,@VM,1) THEN
			TypeOver = ''
			TypeOver<PDISPLAY$> = RDSNos
			TypeOver<PFIELD$>	= 2
			TypeOver<PTYPE$>	= 'K'
			RDSKeys = Popup(@WINDOW,TypeOver,'WO_LOG_RTS')
		END ELSE
			RDSKeys = RDSNos
		END

		IF RDSKeys = '' THEN 
			ErrMsg('No RDS Numbers Selected or Work Order Not Yet Released to Production.')
		END ELSE
			CONVERT @VM TO @FM IN RDSKeys
			Print_Cass_Labels(RDSKeys,MultiStepFlag)				;* Updated labels with Akrion Bar Codes
		END
	END

END ELSE
	ErrMsg('WARNING: WO number is missing.')
END

RETURN


* * * * * * *
SelectAll:
* * * * * * *

RDSListCtrl = @WINDOW:'.RDSNOS'
SelectedItems = Get_Property(RDSListCtrl, 'SELPOS')

RDSKeyList = Get_Property(RDSListCtrl, 'LIST')
RDSListCount = COUNT(RDSKeyList,@FM) + (RDSKeyList NE '')

FOR RDSListIndex = 1 TO RDSListCount
	IF NOT(Inlist(SelectedItems<2>, RDSListIndex, @VM)) THEN
		Set_Property(RDSListCtrl, 'SELPOS', '1':@FM:RDSListindex)
	END
NEXT RDSListIndex

RETURN


*********
ClearAll:
*********

RDSListCtrl = @WINDOW:'.RDSNOS'
SelectedItems = Get_Property(RDSListCtrl, 'SELPOS')

RDSKeyList = Get_Property(RDSListCtrl, 'LIST')
RDSListCount = COUNT(RDSKeyList,@FM) + (RDSKeyList NE '')

FOR RDSListIndex = 1 TO RDSListCount
	IF Inlist(SelectedItems<2>, RDSListIndex, @VM) THEN
		Set_Property(RDSListCtrl, 'SELPOS', '1':@FM:RDSListindex)
	END
NEXT RDSListIndex

RETURN


*********************
RefreshCassettesList:
*********************

OPEN 'DICT.RDS' TO DictVar THEN
	RDSNos = ''
	Flag = ''
	SearchString = 'WO':@VM:WONo:@FM
	Btree.Extract(SearchString, 'RDS', DictVar, RDSNos, '', Flag)
	IF Get_Status(errCode) THEN
		ErrMsg(errCode)
		RETURN
	END
	
END ELSE
	ErrMsg('Unable to open "DICT.RDS" for index lookup in COMM_DIALOG_PO_NO_CHANGE routine.')
	RETURN
END

IF INDEX(RDSNos,@VM,1) THEN				/* Sort only if two or more records are found */
	RDSNos := @VM						/* Need a trailer delimiter */
	CONVERT @VM TO @RM IN RDSNos
	CALL V119('S','','A','R',RDSNos,'') /* Sort the RDS numbers in ascending order */
	CONVERT @RM TO @VM IN RDSNos
	RDSNos[-1,1] = ''					/* Strip previous added trailing delimiter	*/
END ELSE
	NULL
END	

OPEN 'RDS' TO RDS_Table ELSE
	ErrMsg('Unable to open "RDS" for reads in COMM_DIALOG_PO_NO_CHANGE - Create Event')
	RETURN
END

RDS_List = ''
RDSListCount = 0
kCnt = COUNT(RDSNos,@VM) + (RDSNos NE '')
	
FOR I = 1 TO kCnt
	RDSNo = RDSNos<1,I>
	READ RDS_Rec FROM RDS_Table,RDSNo THEN
		PONo 	= RDS_Rec<RDS_PO$>
		CassNo 	= RDS_Rec<RDS_CASS_NO$>
		LotNo	= RDS_Rec<RDS_LOT_NUM$>

		OPEN 'DICT.WO_MAT' TO DictVar THEN
			SearchString  = 'WO_NO':@VM:WONo:@FM
			WO_MAT_Keys = ''
			Flag = ''
			Btree.Extract(SearchString, 'WO_MAT', DictVar, WO_MAT_Keys, '', Flag)
			IF Get_Status(errCode) THEN
				ErrMsg(errCode)
				RETURN
			END
			
		END ELSE
			ErrMsg('Unable to open "DICT.WO_MAT" for index lookup in COMM_DIALOG_PO_NO_CHANGE routine.')
			RETURN
		END
		
		OPEN 'WO_MAT' TO WO_MAT_Table ELSE
			ErrMsg('Unable to open "WO_MAT" for reads in COMM_DIALOG_PO_NO_CHANGE - Create Event')
			RETURN
		END

		CassetteFound = 0
		CassettesCount = COUNT(WO_MAT_Keys,@VM) + (WO_MAT_Keys NE '')
		FOR CassettesLoopIndex = 1 TO CassettesCount
			WO_MAT_Key = WO_MAT_Keys<1, CassettesLoopIndex>
			WO_MAT_Key_Cass = FIELD(WO_MAT_Key,'*',2)
			IF (WO_MAT_Key_Cass = CassNo) THEN
				READ WO_MAT_Rec FROM WO_MAT_Table,WO_MAT_Key THEN
					IF (WO_MAT_Rec<WO_MAT_SHIP_NO$> = '') THEN
						RDSListCount = RDSListCount + 1
						IF (WO_MAT_Rec<WO_MAT_HOT_LOT$> = 1) THEN
							HotLot = 'Hot'							
						END ELSE
							HotLot = ''
						END
						RDS_List<RDSListCount> = CassNo:@VM:RDSNo:@VM:LotNo:@VM:PONo:@VM:HotLot
					END
				END
				CassetteFound = 1
			END
		UNTIL CassetteFound = 1
		NEXT CassettesLoopIndex
	END
NEXT I

Set_Property(@WINDOW:'.RDSNOS','LIST',RDS_List)

RETURN


******
Close:
******
End_Dialog(@WINDOW,'Exit')
RETURN


*****
Exit:
*****
End_Dialog(@WINDOW,'Exit')
RETURN


*******
Update:
*******

RDSListCtrl = @WINDOW:'.WO_NO'
WONo = Get_Property(RDSListCtrl, 'TEXT')

IF (WONo NE '') THEN

	NewPONumberCtrl = @WINDOW:'.NEW_PO_NO'
	NewPONumber = TRIM(Get_Property(NewPONumberCtrl, 'DEFPROP'))

	RDSListCtrl = @WINDOW:'.RDSNOS'
	SelectedItems = Get_Property(RDSListCtrl, 'SELPOS')
	RDSKeyList = Get_Property(RDSListCtrl, 'LIST')
	RDSListCount = COUNT(RDSKeyList,@FM) + (RDSKeyList NE '')
	SelectedItemsCount = COUNT(SelectedItems<2>,@VM) + (SelectedItems<2> NE '')

	IF (SelectedItemsCount > 0) THEN

		Cassettes = ''
		FOR RDSListIndex = 1 TO RDSListCount
			IF (Inlist(SelectedItems<2>, RDSListIndex, @VM)) THEN
				Cassettes := RDSKeyList<RDSListIndex, COL$CASSETTES>:@VM
			END
		NEXT RDSListIndex
		Cassettes[-1,1] = ''

		***********************************************
		* Update the RDS table with the new PO number *
		***********************************************
		IF (NewPONumber NE '') THEN
			OPEN 'RDS' TO RDSTable ELSE
				ErrMsg("ERROR!  OBJ_WO_LOG :ChangePONo : Unable to open the RDS Table!")
				RETURN
			END

			RLIST('SELECT REACT_RUN WITH WO_NO = ':QUOTE(WONo):' ',TARGET_ACTIVELIST$,'','','')
			Done = 0
			LOOP
				READNEXT RDSNo ELSE Done = 1
			UNTIL Done 
				READ RDSRec FROM RDSTable,RDSNo THEN
					PONo	= RDSRec<RDS_PO$>
					RONo  	= XLATE('RDS', RDSNo, 'RUN_ORDER_NUM', 'X')
					LOCATE RONo IN Cassettes USING @VM SETTING Pos THEN
						RDSRec<RDS_PO$> = NewPONumber
						WRITE RDSRec ON RDSTable,RDSNo THEN
							Null
						END
						obj_Tables('WriteRec','RDS':@RM:RDSNo:@RM:@RM:RDSRec)
						Result = 'PO Number Changed'
					END
				END
			REPEAT
			
			/* After making the update, refresh the LIST of cassettes */
			GOSUB RefreshCassettesList
		END			
			
	END ELSE
		ErrMsg('INFO: A new PO number value is required.')
		Set_Property(@WINDOW,'FOCUS',@WINDOW:'.NEW_PO_NO')
	END

END ELSE
	ErrMsg('WARNING: WO number is missing.')
END

RETURN

