COMPILE FUNCTION Comm_Dialog_Cass_Met_Export(Method, Parm1)

/*
	Commuter module for Dialog_Cass_Met_Exportwindow.
	
	10/28/2005 - John C. Henry, J.C. Henry & Co., Inc.
*/

$INSERT LOGICAL
$INSERT WM_OUT_EQUATES
$INSERT PS_EQUATES
$INSERT POPUP_EQUATES
$INSERT MSG_EQUATES

DECLARE SUBROUTINE Set_Property, End_Dialog, Send_Event, Set_Status, ErrMsg, Btree.Extract, Msg, Send_Info
DECLARE SUBROUTINE obj_Appwindow, Start_Window
DECLARE FUNCTION   Get_Property, Get_Status, Dialog_Box, Utility, Send_Message, Popup, Collect.IXVals, Msg
DECLARE FUNCTION   SRP_Array

EQU CRLF$	TO \0D0A\

EQU COL$CUST_NO		TO 1		;* Customer edit table column equates
EQU COL$CUST_NAME	TO 2

EQU LTGREY$		TO	229 + (229*256) + (229*65536)	;* JCH standard colors for edittable backgrounds
EQU GREY$		TO	192 + (192*256) + (192*65536)
EQU GREEN$		TO	192 + (220*256) + (192*65536)
EQU RED$		TO	255 + (128*256) + (128*65536)
EQU BLUE$		TO	128 + (255*256) + (255*65536)
EQU WHITE$		TO	255 + (255*256) + (255*65536)
EQU YELLOW$		TO	255 + (255*256) + (202*65536)
EQU LTBLUE$		TO	128 + (255*256) + (255*65536)
EQU PURPLE$		TO	225 + (181*256) + (255*65536)

EQU DTS_MULTIROW$	TO 512
EQU DTS_LARGEDATA$	TO 4096

ErrTitle = 'Error in Comm_Dialog_Order_Find'
ErrorMsg = ''

Result = ''

BEGIN CASE
	CASE Method = 'Create'			; GOSUB Create
	CASE Method = 'CustChar'		; GOSUB CustChar
	CASE Method = 'CustPC'			; GOSUB CustPC
	CASE Method = 'CustDC'			; GOSUB CustDC
	CASE Method = 'CustPNDC'		; GOSUB CustPNDC
	CASE Method = 'SubPNDC'			; GOSUB SubPNDC
	CASE Method = 'LotNoDC'			; GOSUB LotNoDC
	CASE Method = 'PSNoDC'			; GOSUB PSNoDC
	CASE Method = 'WONoDC'			; GOSUB WONoDC
	CASE Method = 'LUDate'			; GOSUB LUDate
	CASE Method = 'Cancel'			; GOSUB Cancel
	CASE Method = 'Refresh'			; GOSUB Refresh
	CASE Method = 'PerformQuery'	; GOSUB PerformQuery
	CASE 1
		ErrMsg(ErrTitle:@SVM:'Unknown method ':QUOTE(Method):' passed to routine.')
		
END CASE

RETURN Result


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

    obj_AppWindow('Create')
    Set_Property(@WINDOW,'STATUSLINE',@WINDOW:'.STATUSLINE_FIX')
    GOSUB Refresh

RETURN


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

    * Turn edit table symbolic column backgrounds to green
    stat = Send_Message(@WINDOW:'.CUST_INFO','COLOR_BY_POS',COL$CUST_NAME,0,GREEN$)

RETURN


* * * * * * *
CustChar:
* * * * * * *

    CtrlName  = @WINDOW:'.CUST_INFO'
    CustArray = Get_Property(CtrlName,'ARRAY')		;* Customer Information table
    CurrPos   = Get_Property(CtrlName,'SELPOS')
    CurrRow   = CurrPos<2>
    DataIn    = CustArray<COL$CUST_NO,CurrRow>
    IF LEN(DataIn) > 2 THEN
        ReturnToCtrl = CtrlName
        ReturnToPos  = CurrPos
        IF NOT(NUM(DataIn)) THEN 
            Set_Property(CtrlName,'CELLPOS','',CurrPos)	;* Clear characters input
            Start_Window('COMP_LOOKUP',@WINDOW,DataIn:@VM:'C':@FM:ReturnToCtrl:@FM:ReturnToPos,'','')
        END
    END

RETURN


* * * * * * *
CustPC:
* * * * * * *

    CtrlName = @WINDOW:'.CUST_INFO'
    CustList = Get_Property(CtrlName,'LIST')		;* Customer Information table
    CurrPos  = Get_Property(CtrlName,'SELPOS')
    CurrCol  = CurrPos<1>
    CurrRow  = CurrPos<2>
    CustNo   = CustList<CurrRow,COL$CUST_NO>
    IF CustNo NE '' THEN
        Set_Property(CtrlName,'CELLPOS',XLATE('COMPANY',CustNo,4,'X'),COL$CUST_NAME:@FM:CurrRow)
    END

RETURN


* * * * * * *
CustDC:
* * * * * * *

    CtrlName = @WINDOW:'.CUST_INFO'
    CustList = Get_Property(CtrlName,'LIST')		;* Customer Information table
    CurrPos  = Get_Property(CtrlName,'SELPOS')
    CurrCol  = CurrPos<1>
    CurrRow  = CurrPos<2>
    CustNo   = CustList<CurrRow,COL$CUST_NO>
    LineCnt  = COUNT(CustList,@FM) + (CustList NE '')
    IF CustNo = '' THEN
        CustNos = Popup(@WINDOW,'','CUSTOMER')
        CustCnt = COUNT(CustNos,@VM) + (CustNos NE '')
        IF CustCnt > LineCnt THEN
            FOR I = 1 TO CustCnt
                stat = Send_Message(CtrlName,'INSERT',-1,@VM:@FM)	; * Add blank lines
            NEXT I
        END
        FOR I = CurrRow TO CurrRow + CustCnt
            CustNo = CustNos<1,I>
            CustName = XLATE('COMPANY',CustNo,4,'X')
            Set_Property(CtrlName,'CELLPOS',CustNo,COL$CUST_NO:@FM:I)
            Set_Property(CtrlName,'CELLPOS',CustName,COL$CUST_NAME:@FM:I)
            Set_Property(CtrlName,'SELPOS',COL$CUST_NO:@FM:I+1)
        NEXT I
    END ELSE
        ErrMsg('DoubleClick on the first empty Cust No field to view a popup of all Customers')
    END
    
RETURN


* * * * * * *
SubPNDC:
* * * * * * *

    CustArray = Get_Property(@WINDOW:'.CUST_INFO','ARRAY')<COL$CUST_NO>
    CustNos = CustArray<COL$CUST_NO>
    LOOP
        LastCustNo = CustNos[-1,'B':@VM]
    UNTIL LastCustNo NE '' OR CustNos = ''
        CustNos[COL1(),99] = ''
    REPEAT
    IF CustNos NE '' then 
        OPEN 'DICT.WO_LOG' TO DictVar THEN
            SearchString  = 'CUST_NO':@VM:CustNos:@VM:@FM
            SearchString := 'ORD_SUB_PART_NO':@VM:'#':@FM
            Btree.Extract(SearchString, 'WO_LOG', DictVar, WOKeys, '', Flag)
            IF Not(Get_Status(errCode)) THEN
                PartNos = ''
                IF WOKeys NE '' THEN
                    RawPartNos = XLATE('WO_LOG',WOKeys,'ORD_SUB_PART_NO','X')
                    IF Get_Status(errCode) THEN ErrMsg(errCode)
                    FOR I = 1 TO COUNT(RawPartNos,@VM) + (RawPartNos NE '')
                        RawPartNo = RawPartNos<1,I>
                        LOCATE RawPartNo IN PartNos BY 'AL' USING @VM SETTING Pos ELSE
                            PartNos = INSERT(PartNos,1,Pos,0,RawPartNo)
                        END
                    NEXT I
                END
                IF PartNos NE '' THEN
                    PartNos             := @VM
                    CONVERT @VM TO @RM IN PartNos
                    CALL V119('S','','D','R',PartNos,'')
                    IF Get_Status(errCode) THEN ErrMsg(errCode)
                    CONVERT @RM TO @VM IN PartNos
                    PartNos[-1,1]        = ''	
                    TypeOver             = ''
                    TypeOver<PDISPLAY$>  = PartNos
                    PartNos              = Popup(@WINDow,TypeOver,'CUST_PO')
                    IF Get_Status(errCode) THEN ErrMsg(errCode)
                    IF PartNos NE '' THEN Set_Property(@WINDOW:'.SUB_PART_NO','DEFPROP',PartNos)
                END else
                    ErrMsg('No Substrate Part Numbers on file for specified customer.')
                end
            end else
                ErrMsg(errCode)
            end
        end else
            ErrMsg('Unable to open DICT.WO_LOG in COMM_DIALOG_CASS_MET_EXPORT.')
        end
    end

RETURN


* * * * * * *
LotNoDC:
* * * * * * *

    LotNos  = ''
    WOKeys  = ''
    CustNos = Get_Property(@WINDOW:'.CUST_INFO','ARRAY')<COL$CUST_NO>
    CustNos = SRP_Array('Clean', CustNos, 'TrimAndMakeUnique', @VM)
    IF CustNos NE '' then 
        OPEN 'DICT.WO_LOG' TO DictVar THEN
            SearchString  = 'CUST_NO':@VM:CustNos:@VM:@FM
            Btree.Extract(SearchString, 'WO_LOG', DictVar, WOKeys, '', Flag)
            IF Not(Get_Status(errCode)) THEN
                Query      = 'LOT_NO':@VM:'#':@FM
                dWoMat     = ''
                WOMatWONos = ''
                Open 'DICT.WO_MAT' to dWoMat then
                    Flag      = ''
                    WOMatKeys = ''
                    Btree.Extract(Query, 'WO_MAT', dWoMat, WOMatKeys, 'E', Flag)
                    If Not(Get_status(errCode)) then
                        If WOMatKeys NE '' then
                            WOMatWONos = SRP_Array('Rotate', WOMatKeys, @VM, '*')
                            WOMatWONos = Delete(WOMatWONos, 0, 2, 0)
                            Convert '*' to @VM in WOMatWONos   
                            WOMatWONos = SRP_Array('Clean', WOMatWONos, 'TrimAndMakeUnique', @VM)
                            WOKeys     = SRP_Array('Join', WOKeys, WOMatWONos, 'OR', @VM)
                        end
                    end else
                        ErrMsg('Error querying LOT_NO field of the WO_MAT table.')
                    end
                end else
                    ErrMsg('Error opening DICT.WO_MAT table in order to query on LOT_NO field')
                end            
            end else
                ErrMsg(errCode)
            end
            IF WOKeys NE '' THEN
                RawLotNos = XLATE('WO_LOG',WOKeys,'LOT_NO','X')
                IF Get_Status(errCode) THEN ErrMsg(errCode)
                FOR I = 1 TO COUNT(RawLotNos,@VM) + (RawLotNos NE '')
                    RawLotNo = RawLotNos<1,I>
                    LOCATE RawLotNo IN LotNos BY 'AL' USING @VM SETTING Pos ELSE
                        LotNos = INSERT(LotNos,1,Pos,0,RawLotNo)
                    END
                NEXT I
            END
            IF LotNos NE '' THEN
                LotNos              := @VM
                CONVERT @VM TO @RM IN LotNos
                CALL V119('S','','D','R',LotNos,'')
                IF Get_Status(errCode) THEN ErrMsg(errCode)
                CONVERT @RM TO @VM IN LotNos
                LotNos[-1,1]         = ''
                TypeOver             = ''
                TypeOver<PDISPLAY$>  = LotNos
                LotNos               = Popup(@WINDow,TypeOver,'CUST_PO')
                IF Get_Status(errCode) THEN ErrMsg(errCode)
                IF LotNos NE '' THEN Set_Property(@WINDOW:'.LOT_NO','DEFPROP',LotNos)
            END else
                ErrMsg('No Lot Numbers on file for specified customer.')
            end
        END ELSE
            ErrMsg('Unable to open DICT.WO_LOG in COMM_DIALOG_CASS_MET_EXPORT.')
        END
    end
    
RETURN


* * * * * * *
PSNoDC:
* * * * * * *

    WOKeys    = ''
    PSNs      = ''
    CustArray = Get_Property(@WINDOW:'.CUST_INFO','ARRAY')
    CustNos   = CustArray<COL$CUST_NO>
    CustNos   = SRP_Array('Clean', CustNos, 'TrimAndMakeUnique', @VM)
    If CustNos NE '' then 
        OPEN 'DICT.WO_LOG' TO DictVar THEN
            SearchString  = 'CUST_NO':@VM:CustNos:@VM:@FM
            Btree.Extract(SearchString, 'WO_LOG', DictVar, WOKeys, '', Flag)
            IF Not(Get_Status(errCode)) THEN
                Query       = 'PROD_SPEC_ID':@VM:'#':@FM
                dWOStep     = ''
                WOStepWONos = ''
                Open 'DICT.WO_STEP' to dWOStep then
                    Flag       = ''
                    WOStepKeys = ''
                    Btree.Extract(Query, 'WO_STEP', dWOStep, WOStepKeys, 'E', Flag)
                    If Not(Get_Status(ErrCode)) then
                        If WOStepKeys NE '' then
                            WOStepWONos = SRP_Array('Rotate', WOStepKeys, @VM, '*')
                            WOStepWONos = Delete(WOStepWONos, 0, 2, 0)
                            Convert '*' to @VM in WOStepWONos                               
                            WOStepWONos = SRP_Array('Clean', WOStepWONos, 'TrimAndMakeUnique', @VM)
                            WOKeys      = SRP_Array('Join', WOKeys, WOStepWONos, 'OR', @VM)
                        end
                    end else
                        ErrorMsg = 'Error querying PROD_SPEC_ID field in WO_STEP table.'
                    end
                end else
                    ErrorMsg = 'Error opening DICT.WO_STEP table in order to query on PROD_SPEC_ID field. Error code: ':ErrCode
                end            
            END else
                ErrMsg(errCode)
            end
            IF WOKeys NE '' THEN
                FOR N = 1 TO COUNT(WOKeys,@VM) + (WOKeys NE '')
                    WOKey = WOKeys<1,N>
                    RawPSNs = XLATE('WO_LOG',WOKey,'PS_NO','X')
                    FOR I = 1 TO COUNT(RawPSNs,@VM) + (RawPSNs NE '')
                        RawPSN = RawPSNs<1,I>
                        LOCATE RawPSN IN PSNs BY 'AL' USING @VM SETTING Pos ELSE
                            PSNs = INSERT(PSNs,1,Pos,0,RawPSN)
                        END
                    NEXT I
                NEXT N
            END
            IF PSNs NE '' THEN
                PSNs                := @VM
                CONVERT @VM TO @RM IN PSNs
                CALL V119('S','','D','R',PSNs,'')
                IF Get_Status(errCode) THEN ErrMsg(errCode)
                CONVERT @RM TO @VM IN PSNs
                PSNs[-1,1]           = ''					;* Strip trailing delimiter	
                TypeOver             = ''
                TypeOver<PDISPLAY$>  = PSNs
                PSNs                 = Popup(@WINDow,TypeOver,'CUST_PO')
                IF Get_Status(errCode) THEN ErrMsg(errCode)
                IF PSNs NE '' THEN
                    CONVERT @VM TO @FM IN PSNs
                    Set_Property(@WINDOW:'.PS_NO','LIST',PSNs)
                END                
            END else
                ErrMsg('No Product Specifications on file for specified customer.')                
            end
        END ELSE
            ErrMsg('Unable to open DICT.WO_LOG in COMM_DIALOG_WO_LOG_FIND.')
        END
    end
    
RETURN


* * * * * * *
WONoDC:
* * * * * * *

    WOKeys = Collect.IXVals('WM_OUT', 'WO_NO')
    CONVERT @FM TO @VM IN WOKeys
    IF Not(Get_Status(errCode)) THEN 
        IF WOKeys NE '' THEN
            TypeOver = ''
            TypeOver<PDISPLAY$> = WOKeys
            TypeOver<PMODE$> = 'K'
            WOKeys = Popup(@WINDOW,TypeOver,'WO_LOG_QUERY')
            IF Not(Get_Status(errCode)) THEN
                IF WOKeys NE '' THEN
                    CONVERT @VM TO @FM IN WOKeys
                    Set_Property(@WINDOW:'.WO_NO','LIST',WOKeys)
                END
            END else
                ErrMsg(errCode)
            end
        END
    end else
        ErrMsg(ErrCode)
    end
    
RETURN


* * * * * * *
CustPNDC:
* * * * * * *

    CustArray = Get_Property(@WINDOW:'.CUST_INFO','ARRAY')
    CustNos   = CustArray<COL$CUST_NO>
    CustNos   = SRP_Array('Clean', CustNos, 'TrimAndMakeUnique', @VM)
    IF CustNos NE '' then 
        OPEN 'DICT.WO_LOG' TO DictVar THEN
            SearchString  = 'CUST_NO':@VM:CustNos:@VM:@FM
            SearchString := 'CUST_PART_NO':@VM:'#':@FM
            Btree.Extract(SearchString, 'WO_LOG', DictVar, WOKeys, '', Flag)
            IF Not(Get_Status(errCode)) THEN
                IF WOKeys NE '' THEN
                    PartNos = ''
                    FOR N = 1 TO COUNT(WOKeys,@VM) + (WOKeys NE '')
                        WOKey = WOKeys<1,N>
                        RawPartNos = XLATE('WO_LOG',WOKey,'PART_NO','X')
                        FOR I = 1 TO COUNT(RawPartNos,@VM) + (RawPartNos NE '')
                            RawPartNo = RawPartNos<1,I>
                            LOCATE RawPartNo IN PartNos BY 'AL' USING @VM SETTING Pos ELSE
                                PartNos = INSERT(PartNos,1,Pos,0,RawPartNo)
                            END
                        NEXT I
                    NEXT N
                END ELSE
                    PartNos = ''
                END
                IF PartNos NE '' THEN
                    PartNos             := @VM
                    CONVERT @VM TO @RM IN PartNos
                    CALL V119('S','','D','R',PartNos,'')
                    IF Get_Status(errCode) THEN ErrMsg(errCode)
                    CONVERT @RM TO @VM IN PartNos
                    PartNos[-1,1]        = ''					
                    TypeOver             = ''
                    TypeOver<PDISPLAY$>  = PartNos
                    PartNos              = Popup(@WINDow,TypeOver,'CUST_PO')
                    IF Get_Status(errCode) THEN ErrMsg(errCode)
                    IF PartNos NE '' THEN Set_Property(@WINDOW:'.CUST_PART_NO','DEFPROP',PartNos)                       
                END else
                    ErrMsg('No Customer Part Numbers on file for specified customer.')
                end
            END else
                ErrMsg(errCode)
            end
        END ELSE
            ErrMsg('Unable to open DICT.WO_LOG in COMM_DIALOG_WO_LOG_FIND.')
        END
    end
    
RETURN


* * * * * * *
LUDate:
* * * * * * *

    ReturnCtrl   = Parm1[1,@RM]
    IF ReturnCtrl = '' THEN ReturnCtrl = Get_Property(@WINDOW,'FOCUS')
    DateSelected =  Dialog_Box('POPUP_YEAR',@WINDOW)
    RetVal       = OCONV(DateSelected, 'D4/')
    IF RetVal NE '' THEN obj_Appwindow('LUValReturn',RetVal:@RM:ReturnCtrl)

RETURN


* * * * * * *
Cancel:
* * * * * * *

RETURN


* * * * * * *
PerformQuery:
* * * * * * *

    SearchString = 'MAKEUP_BOX':@VM:'0':@VM:'':@FM

    CustNos = Get_Property(@WINDOW:'.CUST_INFO','ARRAY')<COL$CUST_NO>
    CustNos = SRP_Array('Clean', CustNos, 'TrimAndMakeUnique', @VM)
    IF CustNos NE '' THEN SearchString := 'CUST_NO':@VM:CustNos:@FM			

    LotNos = Get_Property(@WINDOW:'.LOT_NO','ARRAY')<1>
    LotNos = SRP_Array('Clean', LotNos, 'TrimAndMakeUnique', @VM)
    IF LotNos NE '' THEN SearchString := 'LOT_NO':@VM:LotNos:@FM

    CustPNs = Get_Property(@WINDOW:'.CUST_PART_NO','ARRAY')<1>
    CustPNs = SRP_Array('Clean', CustPNs, 'TrimAndMakeUnique', @VM)
    IF CustPNs NE '' THEN SearchString := 'CUST_PART_NO':@VM:CustPNs:@FM

    SubPartNos = Get_Property(@WINDOW:'.SUB_PART_NO','ARRAY')<1>
    SubPartNos = SRP_Array('Clean', SubPartNos, 'TrimAndMakeUnique', @VM)
    IF SubPartNos NE '' THEN SearchString := 'SUB_PART_NO':@VM:SubPartNos:@FM

    PS_NOs = Get_Property(@WINDOW:'.PS_NO','ARRAY')<1>
    PS_NOs = SRP_Array('Clean', PS_NOs, 'TrimAndMakeUnique', @VM)
    IF PS_NOs NE '' THEN SearchString := 'SHIP_PSN':@VM:PS_NOs:@FM

    WO_NOs = Get_Property(@WINDOW:'.WO_NO','ARRAY')<1>
    WO_NOs = SRP_Array('Clean', WO_NOs, 'TrimAndMakeUnique', @VM)
    IF WO_NOs NE '' THEN SearchString := 'WO_NO':@VM:WO_NOs:@FM

    StartDt = ICONV(Get_Property(@WINDOW:'.START_DT','TEXT'),'D')
    EndDt	= ICONV(Get_Property(@WINDOW:'.END_DT','TEXT'),'D')

    BEGIN CASE
        CASE StartDt NE '' AND EndDt = ''
            SearchString := 'RDS_FINAL_SIG_DT':@VM:'>=':OConv(StartDt, 'D4/'):@FM
        
        CASE StartDt = '' AND EndDt NE ''
            SearchString := 'RDS_FINAL_SIG_DT':@VM:'<=':OConv(EndDt, 'D4/'):@FM
        
        CASE StartDt NE '' AND EndDt NE ''
            * Fudge the dates - '~' is not inclusive of the end dates
            StartDt -= 1
            EndDt += 1
            SearchString := 'RDS_FINAL_SIG_DT':@VM:OConv(StartDt, 'D4/'):'~':OConv(EndDt, 'D4/'):@FM
            
        CASE Otherwise$
            NULL
    END CASE


    OPEN 'DICT.WO_MAT' TO DictVar THEN
        Btree.Extract(SearchString,'WO_MAT',DictVar,WMOKeys,'',flag)
        IF Not(Get_Status(errCode)) THEN
            End_Dialog(@WINDOW,WMOKeys)
        end else
            ErrMsg(errCode)
        end
    END ELSE
        ErrMsg('Unable to open DICT.WO_MAT in routine COMM_DIALOG_CASS_MET_EXPORT')
    END

RETURN

