Subroutine FIX_WALES(Dummy)
#pragma precomp SRP_PreCompiler

DECLARE SUBROUTINE SEND_INFO, SEND_DYN, RList,Btree.Extract, Set_Status, Get_Status, ErrMsg, obj_Export, Activate_Save_Select, Database_Services, FTP_Services
DECLARE FUNCTION obj_Export, Utility, Environment_Services, Database_Services, Error_Services

$INSERT RLIST_EQUATES
$INSERT COC_EQUATES
$Insert WO_MAT_EQUATES
$insert WO_LOG_EQUATES
$insert LOGICAL

EQU CRLF$	TO \0D0A\
EQU TAB$	TO CHAR(9)
Debug
GoSub GetRDSNosByProdOrdNo
* GoSub GetRDSNosHardList
* GoSub GetRDSNosByWOMat
* GoSub GetRDSNosByCOC

GoSub SendRDSNos

Return


SendRDSNos:

    If RDSNos EQ '' then
        ErrMsg('There are no RDS records attached to this shipment.  No data will be sent to the customer. (EpiPRO process)')
        return
    end

    DosTable = Environment_Services('GetFTPRootPath') : '\FTP_OUT\W_FIX':'.csv'

    ExportID = 'WALES_SYSTEM_RDS_DATA'
    NoHeader = 1	

    obj_Export('ExportDelimited', 'Comma' : @RM : RDSNos : @RM : ExportID : @RM : DosTable : @RM : @RM : @RM : NoHeader)

    // Default Remote Directory "/NPTirep"
    ToWalesScript   = 'open 10.131.104.18'					    : CRLF$	; *New EU Newport FTP server deployed 7/26/16  dkk
    ToWalesScript  := 'user'									: CRLF$
    ToWalesScript  := 'EU\NPTirep'							    : CRLF$
    ToWalesScript  := 'Wk4F6X61!2016Friday1'					: CRLF$
    ToWalesScript  := 'pwd '									: CRLF$
    ToWalesScript  := 'append ':DosTable:' EpiMesa_2018.csv'    : CRLF$	; *Append to master file D E P R E C A T E D; *Changed from EpiMesa to EpiMesa_2017 - dkk 4/4/15
    * ToWalesScript := 'bye'									: CRLF$

    * OSWrite ToWalesScript ON 'C:\OIReports\ToWales.scr'
*    OSWrite ToWalesScript ON Environment_Services('GetReportsRootPath') : '\ToWales.scr'

    * stat = UTILITY('RUNWIN',"ftp -i -n -s:C:\OIReport\ToWales.scr", 3)	;***********************
*    stat = 'DOSOPEN'
*    call SRP_Run_Command("ftp -i -n -s:C:\OIReports\ToWales.scr", stat, '', '', '', 'Fix_Wales')
* 
*    If Get_Status(errCode) then
*        ErrMsg(errCode)
*    end
            
    *OSDelete DosTable		;* Removes  table from C: drive 


    Host            = 'sFTPNA.extra.infineon.com'
    LocalFile       = DosTable
    Username        = 'DNAMesaFI-FTP'
    Password        = 'OpenInsight2018....!'
    LocalDirectory  = ''
    RemoteDirectory = '/Newport'
    RemoteFile      = 'EpiMesa_2018.csv'
    ScriptPath      = Environment_Services('GetReportsRootPath') : '\ToWalesNew.scr'
    DeleteScript    = False$
    SSH             = True$
*    FTP_Services('AppendFile', Host, LocalFile, '', Username, Password, LocalDirectory, RemoteDirectory, RemoteFile, ScriptPath, DeleteScript, SSH)
*    Message         = Error_Services('GetMessage')
    FTP_Services('PostRequest', 'append', Host, 'MESSA01EC', LocalFile, '', Username, Password, LocalDirectory, RemoteDirectory, RemoteFile, ScriptPath, DeleteScript, SSH, '', 3, False$)

return


GetRDSNosByProdOrdNo:

    // This method uses the missing SAP batch numbers and converts them into product order numbers and then searches
    // WO_LOG for a match. From here, all shipments (COC table) are searched for a matching SAP batch number which will
    // then allow the associated RDS number to be derived.
    UnmatchedBatchNos   = ''
    RDSNos              = ''
    COCNos              = ''
    EOF                 = False$
    Activate_Save_Select('SAP_BATCH_NOS')
    Loop
        Readnext SAPBatchNo else EOF = True$
    Until EOF
        RDSFound    = False$
        ProdOrderNo = SAPBatchNo[1, 'F.'] : '.1'
        WorkOrderNo = Database_Services('SearchIndex', 'WO_LOG', 'PROD_ORD_NO', ProdOrderNo, False$)
        If Error_Services('NoError') then
            ShipmentNos = Database_Services('SearchIndex', 'COC', 'WO', WorkOrderNo, False$)
            If Error_Services('NoError') then
                For Each ShipmentNo in ShipmentNos using @FM
                    COCRow  = Database_Services('ReadDataRow', 'COC', ShipmentNo)
                    CassNos = COCRow<COC_CASS_NO$>
                    For Each CassNo in CassNos using @VM setting vPos
                        WOMatRow        = Database_Services('ReadDataRow', 'WO_MAT', WorkOrderNo : '*' : CassNo)
                        ThisSAPBatchNo  = WOMatRow<87>
                        If ThisSAPBatchno EQ SAPBatchNo then
                            If WOMatRow<WO_MAT_SHIP_NO$> EQ '' then
                                WOMatRow<WO_MAT_SHIP_NO$>   = ShipmentNo
                                Database_Services('WriteDataRow', 'WO_MAT', WorkOrderNo : '*' : CassNo, WOMatRow, True$, '', True$)
                            end else
                                If ShipmentNo NE WOMatRow<WO_MAT_SHIP_NO$> then Debug
                            end
                            RDSNo       = COCRow<COC_RDS_NO$, vPos>
                            If RDSNo NE '' then
                                RDSNos     := RDSNo : @VM
                                COCNos     := ShipmentNo : @FM
                            end else
                                Debug
                            end
                            RDSFound    = True$
                        end
                    Until RDSFound
                    Next CassNo
                Until RDSFound
                Next ShipmentNo
            end
        end
        If Not(RDSFound) then UnmatchedBatchNos := SAPBatchNo : @FM
    Repeat
    RDSNos[-1, 1]               = ''
    COCNos[-1, 1]               = ''
    Database_Services('WriteDataRow', 'SYSLISTS', 'COC_NOS', COCNos, True$, '', True$)
    UnmatchedBatchNos[-1, 1]    = ''
Debug
return


GetRDSNosHardList:

    // This method just hardcodes a list of RDS numbers.
    RDSNos  = 118500 : @VM
    RDSNos := 118647 : @VM
    RDSNos := 118947 : @VM
    RDSNos := 119373 : @VM
    RDSNos := 119879 : @VM
    RDSNos := 121245 : @VM
    RDSNos := 126190 : @VM
    RDSNos := 126372 : @VM
    RDSNos := 131528 : @VM
    RDSNos := 134709 : @VM
    RDSNos := 135747 : @VM
    RDSNos := 141739 : @VM
    RDSNos := 141753 : @VM
    RDSNos := 143486

return


GetRDSNosByWOMat:

    // This method uses Work Order Material Key ID and pulls the RDS numbers directly from these rows. See the TEST
    // routine for code that produces the list of Work Order Material Key IDs.
    RDSNos  = ''
    EOF     = False$
    Activate_Save_Select('SAP_WO_MATS')
    Loop
        Readnext WOMatKeyID else EOF = True$
    Until EOF
        WOMatRow    = Database_Services('ReadDataRow', 'WO_MAT', WOMatKeyID)
        RDSNos     := WOMatRow<WO_MAT_RDS_NO$> : @VM
    Repeat
    RDSNos[-1, 1]   = ''
    RDSCount        = DCount(RDSNos, @VM)

return


GetRDSNosByCOC:
Debug
    // This method uses the missing SAP batch numbers and converts them into product order numbers and then searches
    // WO_LOG for a match. From here, all shipments (COC table) are searched for a matching SAP batch number which will
    // then allow the associated RDS number to be derived.
    RDSNos              = ''
    EOF                 = False$
    Activate_Save_Select('COC_NOS')
    Loop
        Readnext ShipmentNo else EOF = True$
    Until EOF
        COCRow      = Database_Services('ReadDataRow', 'COC', ShipmentNo)
        CassNos     = COCRow<COC_CASS_NO$>
        WorkOrderNo = COCRow<COC_WO$>
        WorkOrderRow    = Database_Services('ReadDataRow', 'WO_LOG', WorkOrderNo)
        CustNo          = WorkOrderRow<WO_LOG_CUST_NO$>
        If CustNo = '6874' OR CustNo = '7055' then
            RDSNos     := COCRow<COC_RDS_NO$> : @VM
        end
    Repeat
    RDSNos[-1, 1]   = ''

return

