COMPILE FUNCTION WO_Mat_Reship(EntID,Event,Parm1,Parm2,Parm3,Parm4,Parm5)

/*
	Commuter module for the WO_MAT_RESHIP window.  ** This uses the WO_LOG table! **
	
	12/10/2014 John C. Henry, J.C. Henry & Co., Inc.
	
*/


DECLARE SUBROUTINE  obj_Appwindow, ErrMsg, Send_Message, Set_Property, Obj_Wo_Mat, Msg, obj_Tables, RList, Print_SAP_Cass_Ship_Label, Print_Cass_Labels
Declare subroutine  SRP_JSON, Form_Services
DECLARE FUNCTION    Get_Property, obj_Tables, Send_Message, Memberof, Work_Order_Services, Error_Services, SRP_JSON

$insert LOGICAL
$INSERT APPCOLORS
$INSERT RTI_STYLE_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WO_LOG_EQUATES
$insert WO_STEP_EQUATES
$insert RDS_EQUATES

EQU CRLF$	TO \0D0A\
EQU TAB$	TO \09\
EQU TARGET_ACTIVELIST$ TO 5

EQU COL$CASS_NO			TO 1
EQU COL$LOT_NO			TO 2
EQU COL$QTY				TO 3
EQU COL$EPI_PART_NO		TO 4
EQU COL$CURR_STATUS		TO 5
EQU COL$BATCH_NO		TO 6
EQU COL$SAP_CASS_ID		TO 7
EQU COL$SHIP_RDS_NO		TO 8
EQU COL$RDS_CUST_NO		TO 9
EQU COL$RDS_CUST_PN		TO 10


ErrTitle = 'Error in PROD_VER Commuter PROD_VER'
ErrorMsg = ''

Result = ''

BEGIN CASE
	CASE EntID = @WINDOW
		BEGIN CASE
			CASE Event = 'CLEAR'			; GOSUB Clear
			CASE Event = 'CREATE' 			; GOSUB Create
			CASE Event = 'CLOSE'			; GOSUB Close
			
		END CASE

	CASE EntID = @WINDOW:'.CASS_NO'
		BEGIN CASE 
			CASE Event = 'POSCHANGED'		; GOSUB CassPC
			CASE Event = 'DBLCLK'			; GOSUB CassDC
		END CASE
		
	Case EntID = @Window : '.WO' AND Event = 'LOSTFOCUS'                    ; GoSub WO.LOSTFOCUS
	Case EntID = @Window : '.WO' AND Event = 'CHANGED'                      ; GoSub WO.CHANGED
	CASE EntID = @WINDOW:'.POST_BUTTON' AND Event = 'CLICK'	                ; GOSUB PostChanges
	CASE EntID = @WINDOW:'.PRINT_RDS_WMO_LABELS' AND Event = 'CLICK'        ; GOSUB PrintRDSWMOLabels
	CASE EntID = @WINDOW:'.PRINT_CASSETTE_LABELS' AND Event = 'CLICK'       ; GOSUB PrintCassShipLabels
	CASE EntID = @WINDOW:'.RESHIP_CUST_NO' AND Event = 'LOSTFOCUS'          ; GoSub UpdateCustomerName
	
	CASE 1
		ErrorMsg = 'Unknown Parameters ':EntID:' - ':Event:' passed to commuter'
		ErrMsg(ErrorMsg)
		
END CASE

IF ErrorMsg NE '' THEN
	ErrMsg(ErrTitle:@SVM:ErrorMsg)
END

RETURN Result



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

    obj_Appwindow('Create',@WINDOW)

    If Memberof(@User4, 'CONFIG_LIMITED') then
        Set_Property(@Window : '.POST_BUTTON', 'ENABLED', True$)
        Set_Property(@Window : '.PRINT_RDS_WMO_LABELS', 'ENABLED', True$)
        Set_Property(@Window : '.PRINT_CASSETTE_LABELS', 'ENABLED', True$)
    end


GOSUB Refresh

RETURN



* * * * * * *
Clear:
* * * * * * *

    Form_Services('CloseControlMessage', EntId)

RETURN



* * * * * * *
Refresh:
* * * * * * *

* Turn edit table symbolic column backgrounds to green

ETSymbolics = Get_Property(@WINDOW,'@ET_SYMBOLICS')		;* Loaded during 'Create' in obj_Appwindow

ETCtrls	= ETSymbolics<1>
ETCols	= ETSymbolics<2>

FOR I = 1 TO COUNT(ETCtrls,@VM) + (ETCtrls NE '')
	ETCtrl	= ETCtrls<1,I>
	ETList = Get_Property(ETCtrl,'LIST')
	FOR Line = 1 TO COUNT(ETList,@FM) + (ETList NE '')
		IF ETList<Line,1> NE '' THEN
			FOR N = 1 TO COUNT(ETCols<1,I>,@SVM) + (ETCols<1,I> NE '')
				stat = Send_Message(ETCtrl,'COLOR_BY_POS',ETCols<1,I,N>,Line,GREEN$)
			NEXT N
		END
	NEXT Line
NEXT I

/* Reference code


StatusArray = Get_Property(@WINDOW:'.PROC_STEP_NO','ARRAY')<COL$PSN_STATUS>

LOOP
	StatusVal = StatusArray[-1,1]
UNTIL StatusVal NE @VM OR StatusArray = ''
	StatusArray[-1,1] = ''
REPEAT

StatCnt = COUNT(StatusArray,@VM) + (StatusArray NE '')

FOR I = 1 TO StatCnt
	StatusVal = StatusArray<1,I>
	
	IF StatusVal[1,1] = 'I' THEN
		stat = Send_Message(@WINDOW:'.PROC_STEP_NO','COLOR_BY_POS',COL$PSN_STATUS,I,RED$)
	END
	IF StatusVal[1,1] = 'H' THEN
		stat = Send_Message(@WINDOW:'.PROC_STEP_NO','COLOR_BY_POS',COL$PSN_STATUS,I,ORANGE$)
	END
	
NEXT I



*/


RETURN



* * * * * * *
Close:
* * * * * * *

    Form_Services('CloseControlMessage', EntId)

RETURN


WO.LOSTFOCUS:

    Form_Services('CloseControlMessage', EntId)
    FocusControl    = Get_Property(@Window, 'FOCUS')
    If FocusControl NE @Window : '.CLOSE' then
        // Focus did not go to the Close button. Validate work order.
        WONo    = Get_Property(EntID, 'TEXT')
        If WONo NE '' then
            WorkOrder   = Work_Order_Services('GetWorkOrder', WONo, False$)
            If Error_Services('NoError') then
                If SRP_JSON(objWorkOrder, 'PARSE', WorkOrder) EQ '' then
                    Closed  = SRP_JSON(objWorkOrder, 'GETVALUE', 'Closed')
                    SRP_JSON(objWorkOrder, 'RELEASE')
                    If Closed _NEC 'Yes' then
                        Set_Property(EntID, 'FOCUS', True$)
                        Message = 'Work Order ' : WONo : ' is still open. Re-shipping is only allowed after a work order has been closed.'
                        Form_Services('DisplayControlMessage', Message, '', EntID, 'VALIDATION', 'RGB(229,20,0)')
                    end
                end
            end
        end
    end

return


WO.CHANGED:

    Form_Services('CloseControlMessage', EntId)

return


UpdateCustomerName:
    NewCustNo   = Get_Property(@Window : '.RESHIP_CUST_NO', 'TEXT')
    Name        = Xlate('COMPANY', NewCustNo, 'CO_NAME', 'X')
    Set_Property(@Window : '.RESHIP_CUST_NAME', 'TEXT', Name)
return

* * * * * * *
PostChanges:
* * * * * * *

    Ctrls  = @WINDOW:'.WO':@RM						; Props  = 'DEFPROP':@RM
    Ctrls := @WINDOW:'.RESHIP_CUST_NO':@RM			; Props := 'DEFPROP':@RM
    Ctrls := @WINDOW:'.RESHIP_CUST_PART_NO':@RM		; Props := 'DEFPROP':@RM
    Ctrls := @WINDOW:'.RESHIP_REASON':@RM			; Props := 'DEFPROP':@RM
    Ctrls := @WINDOW:'.CASS_NO':@RM					; Props := 'LIST':@RM
    Ctrls := @WINDOW:'.RESHIP_CUST_EPI_PART_NO':@RM ; Props := 'DEFPROP':@RM
    Ctrls := @WINDOW:'.RESHIP_CUST_PO':@RM          ; Props := 'DEFPROP':@RM
    Ctrls := @WINDOW:'.RESHIP_CUST_PSN':@RM         ; Props := 'DEFPROP':@RM
    Ctrls := @WINDOW:'.RESHIP_DATE'                 ; Props := 'DEFPROP'

    Vals = Get_Property(Ctrls,Props)

    WONo				= Vals[1,@RM]
    ReshipCustNo		= Vals[COL2()+1,@RM]
    ReshipCustPartNo	= Vals[COL2()+1,@RM]
    ReshipReason		= Vals[COL2()+1,@RM]
    SlotList			= Vals[COL2()+1,@RM]
    ReshipEpiPartNo     = Vals[COL2()+1,@RM]
    ReshipCustPO        = Vals[COL2()+1,@RM]
    ReshipCustPSN       = Vals[COL2()+1,@RM]
    ReshipDate          = Vals[COL2()+1,@RM]

    * Get list of selected line numbers  *

    SlotSelection	= Get_Property(@Window: '.CASS_NO','SELPOS')
    SelectedRows	= SlotSelection<2>

    CONVERT @VM TO @FM in SelectedRows

    SelCnt		= COUNT(SelectedRows,@FM) + (SelectedRows NE '')
    IF SelCnt	= 0 THEN
        ErrorMsg = 'You must select at least one cassette row in order to Reship.'
        FocusCtrl = @Window : '.CASS_NO'
    END

    FocusCtrl   = ''
    IF ReshipReason = ''		THEN ErrorMsg = 'Reship Reason is a required field.' ; FocusCtrl = @Window : '.RESHIP_REASON'
    IF ReshipDate = ''          THEN ErrorMsg = 'Reship Date is a required field.' ; FocusCtrl = @Window : '.RESHIP_DATE'
    IF ReshipCustPSN = ''       THEN ErrorMsg = 'Reship Cust PSN is a required field.' ; FocusCtrl = @Window : '.RESHIP_CUST_PSN'
    IF ReshipCustPO = ''        THEN ErrorMsg = 'Reship Cust PO is a required field.' ; FocusCtrl = @Window : '.RESHIP_CUST_PO'
    IF ReshipEpiPartNo = ''     THEN ErrorMsg = 'Reship Epi Part No is a required field.' ; FocusCtrl = @Window : '.RESHIP_CUST_EPI_PART_NO'
    IF ReshipCustPartNo = ''	THEN ErrorMsg = 'Reship Cust Part No is a required field.' ; FocusCtrl = @Window : '.RESHIP_CUST_PART_NO'
    IF ReshipCustNo = ''		THEN ErrorMsg = 'Reship Cust No is a required field.' ; FocusCtrl = @Window : '.RESHIP_CUST_NO'
    IF WONo = ''				THEN ErrorMsg = 'WO_No is a required field.' ; FocusCtrl = @Window : '.WO'


    WOMatKeys = ''
    ReshipNo = ''

    If ErrorMsg EQ '' then
        GoSub AddReship

        Msg(@Window, 'Finished with the re-ship process.')
    end else
        Set_Property(FocusCtrl, 'FOCUS', True$)
    end

RETURN


PrintRDSWMOLabels:

    WONo        = Get_Property(@Window : '.WO', 'TEXT')
    CassList    = Get_Property(@Window : '.CASS_NO', 'LIST')
    SlotSelection	= Get_Property(@Window: '.CASS_NO','SELPOS')
    SelectedRows	= SlotSelection<2>
    CONVERT @VM TO @FM in SelectedRows
    SelCnt		= COUNT(SelectedRows,@FM) + (SelectedRows NE '')
    RDSNos      = ''
    FOR I = 1 TO SelCnt
 	    RowNo       = SelectedRows<I>
 	    RDSNos     := CassList<RowNo,COL$SHIP_RDS_NO> : @FM
    Next I
    RDSNos[-1, 1]   = ''
    WOSteps         = XLATE('WO_LOG',WONo,WO_LOG_WO_STEP_KEY$,'X')
    IF INDEX(WOSteps,@VM,1) THEN
        MultiStepFlag = 1
    END ELSE
        MultiStepFlag = 0
    END
    Print_Cass_Labels(RDSNos,MultiStepFlag)

return


PrintCassShipLabels:

    WONo        = Get_Property(@Window : '.WO', 'TEXT')
    CassList    = Get_Property(@Window : '.CASS_NO', 'LIST')
    SlotSelection	= Get_Property(@Window: '.CASS_NO','SELPOS')
    SelectedRows	= SlotSelection<2>
    CONVERT @VM TO @FM in SelectedRows
    SelCnt		= COUNT(SelectedRows,@FM) + (SelectedRows NE '')
    WOSteps     = ''
    CassNos     = ''
    RDSNos      = ''
    WOStep      = XLATE('WO_LOG',WONo,WO_LOG_WO_STEP_KEY$,'X')[-1,'B':@VM]
	StepNo	    = WOStep[-1,'B*']
    FOR I = 1 TO SelCnt
 	    RowNo       = SelectedRows<I>
 	    WOSteps    := StepNo : @VM
 	    CassNos    := CassList<RowNo,COL$CASS_NO> : @VM
 	    RDSNos     := CassList<RowNo,COL$SHIP_RDS_NO> : @VM
    Next I
    WOSteps[-1, 1]  = ''
    CassNos[-1, 1]  = ''
    RDSNos[-1, 1]   = ''
    Print_SAP_Cass_Ship_Label( WONo,WOSteps,CassNos,RDSNos )

return

* * * * * * *
CassPC:
* * * * * * *

DEBUG


RETURN


* * * * * * *
CassDC:
* * * * * * *


RETURN


AddReship:
    ********************
    * Input Parameters *
    ********************
    RONos   = ''
    FOR I = 1 TO SelCnt
 	    RowNo   = SelectedRows<I>
 	    RONos  := SlotList<RowNo,COL$CASS_NO> : @VM
    Next I
    RONos[-1, 1]    = ''

    WOMatKeys = ''
    ReshipNo = ''
    ReshipSAPDelNo = ''

    NewPONo      = ReshipCustPO
    NewCustNo    = ReshipCustNo
    NewPSNNo	 = ReshipCustPSN
    NewPartNo    = ReshipCustPartNo
    NewEpiPartNo = ReshipEpiPartNo
    NewShipDate  = ReshipDate
    
    CONVERT ',' TO @VM IN RONos
    
    **********************
    * Fix WO_MAT Records *
    **********************
    OPEN 'WO_MAT' TO WOMatTable THEN
        CassettesCount = COUNT(RONos,@VM) + (RONos NE '')
        FOR CassettesLoopIndex = 1 TO CassettesCount
            WOMatKey = WONo:'*':RONos<1,CassettesLoopIndex>
            READ WOMatRec FROM WOMatTable,WOMatKey THEN
*                Msg(WOMatKey : ' Replace ' : WOMatRec<WO_MAT_CUST_PART_NO$> : ' with ' : NewPartNo)
*                WOMatRec<WO_MAT_CUST_PART_NO$>              = NewPartNo                 /* Line #003 */
                WOMatRec<WO_MAT_RESHIP_CUST_NO$>            = NewCustNo				    /* Line #117 */
                WOMatRec<WO_MAT_RESHIP_DT$>                 = ICONV(NewShipDate,'D4/')  /* Line #115 */
                WOMatRec<WO_MAT_RESHIP_CUST_EPI_PART_NO$>   = NewEpiPartNo   		    /* Line #119 */
                WOMatRec<WO_MAT_RESHIP_CUST_PO$>            = NewPONo				    /* Line #114 */
                WOMatRec<WO_MAT_RESHIP_CUST_PART_NO$>       = NewPartNo  			    /* Line #118 */
                Reason      = WOMatRec<WO_MAT_RESHIP_REASON$>
                Reason     := ' ' : ReshipReason
                Reason      = Trim(Reason)
                WOMatRec<WO_MAT_RESHIP_REASON$>             = Reason                    /* Line #116 */
                obj_Tables('WriteRec','WO_MAT':@RM:WOMatKey:@RM:@RM:WOMatRec)
            END
        NEXT CassettesLoopIndex
    END
    
    ***********************
    * Fix WO_STEP Records *
    ***********************
    OPEN 'WO_STEP' TO WOStepTable THEN
*        WOStepKey = WONo:'*1'
        WOStepKey = XLATE('WO_LOG',WONo,WO_LOG_WO_STEP_KEY$,'X')[-1,'B':@VM]
        READ WOStepRec FROM WOStepTable,WOStepKey THEN
*            Msg(WOStepKey : ' Replace ' : WOStepRec<WO_STEP_PROD_SPEC_ID$> : ' with ' : NewPSNNo)
            WOStepRec<WO_STEP_PROD_SPEC_ID$> = NewPSNNo  /* Line #001 */
            obj_Tables('WriteRec','WO_STEP':@RM:WOStepKey:@RM:@RM:WOStepRec)
        END
    END
    
    *******************
    * Fix RDS Records *
    *******************
    OPEN 'RDS' TO RDSTable THEN
        Done = 0
        RLIST('SELECT REACT_RUN WITH WO_NO = ':QUOTE(WONo):' ',TARGET_ACTIVELIST$,'','','')
        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 RONos USING @VM SETTING Pos THEN
*                    Msg('WONo: ' : WONo : ' / RDSNo: ' : RDSNo : ' / RONo: ' : RONo : ' Replace ' : RDSRec<RDS_PROD_SPEC_ID$> : ' with ' : NewPSNNo)
                    RDSRec<RDS_PO$>       	  = NewPONo     /* Line #008 */
                    RDSRec<RDS_CUST_NO$>  	  = NewCustNo   /* Line #009 */
                    RDSRec<RDS_PROD_SPEC_ID$> = NewPSNNo    /* Line #010 */
                    RDSRec<RDS_PART_NUM$>     = NewPartNo   /* Line #114 */
                    obj_Tables('WriteRec','RDS':@RM:RDSNo:@RM:@RM:RDSRec)
                END
            END
        REPEAT
    END 
    
    **********************
    * Fix WO_LOG Records *
    **********************
*    OPEN 'WO_LOG' TO WOLogTable THEN
*        WOLogKey = WONo
*        READ WOLogRec FROM WOLogTable,WOLogKey THEN
*            Msg('WOLogKey: ':WOLogKey:' Replace ' : WOLogRec<WO_LOG_CUST_NO$> : ' with ' : NewCustNo)
*            WOLogRec<WO_LOG_CUST_NO$>      = NewCustNo				  /* Line #003 */
*            WOLogRec<WO_LOG_COMMIT_DATE$>  = ICONV(NewShipDate,'D4/') /* Line #014 */
*            WOLogRec<WO_LOG_EPI_PART_NO$>  = NewEpiPartNo   		  /* Line #048 */
*            WOLogRec<WO_LOG_CUST_PO_NO$>   = NewPONo				  /* Line #051 */
*            WOLogRec<WO_LOG_CUST_PART_NO$> = NewPartNo  			  /* Line #054 */
*            Notes = WOLogRec<WO_LOG_NOTES$>
*            Notes := ' ' : ReshipReason
*            Notes   = Trim(Notes)
*            WOLogRec<WO_LOG_NOTES$>         = Notes                    /* Line #006 */
*            obj_Tables('WriteRec','WO_LOG':@RM:WOLogKey:@RM:@RM:WOLogRec)
*        END
*    END
return


