COMPILE FUNCTION EXPORT_CDS(ShipNo, AutoFlag,CustPath)
#pragma precomp SRP_PreCompiler

*  EPIPro Data Report - goes straight to CSV for FTP transmission to customer
*  This is for Casette Data Sheets

DECLARE SUBROUTINE ErrMsg, Btree.Extract, Set_Status, SetInitDirOptions, Database_Services
DECLARE FUNCTION Msg, Get_Status, obj_Calendar, Utility, SetInitDirOptions, Dialog_Box, obj_RDS_Test, obj_WO_Log, Getnewscandata, Getnewscandataavg
DECLARE FUNCTION Popup, obj_WM_Out, obj_WO_Mat, Database_Services, Clean_Insp_Services
DECLARE FUNCTION Environment_Services

$Insert APP_INSERTS
$INSERT COC_EQUATES
$INSERT RDS_EQU
$INSERT RDS_LAYER_EQUATES
$INSERT RDS_TEST_EQUATES
$INSERT COMPANY_EQUATES
$INSERT REACTOR_EQUATES
$INSERT MSG_EQUATES
$INSERT POPUP_EQUATES
$INSERT LOGICAL
$INSERT RLIST_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT ORDER_DET_EQU
$INSERT WM_OUT_EQUATES
$Insert CLEAN_INSP_EQUATES
$Insert REACT_RUN_EQUATES


EQU COL$WMO_NO					TO 1
EQU COL$IN_LOT_NO				TO 2
EQU COL$IN_PART_NO				TO 3
EQU COL$CASS_RDS_NO				TO 4
EQU COL$CASS_RDS_WFR_CNT		TO 5
EQU COL$CASS_RDS_REACT_NO		TO 6
EQU COL$CASS_RDS_UNLOAD_DTM		TO 7
EQU COL$CASS_RDS_MET_KEYS		TO 8
EQU COL$CRM_RDS_NO				TO 9
EQU COL$CRM_LS_ID				TO 10
EQU COL$CRM_ZONE				TO 11
EQU COL$CRM_THICK_AVG			TO 12
EQU COL$CRM_HGCV1_RES_AVG		TO 13


If Assigned(AutoFlag) else AutoFlag = False$
If AutoFlag EQ True$ else AutoFlag = False$

ShipRec = XLATE('COC',ShipNo,'','X')
SendDate = OCONV(Date(), 'D4-')

CassNos = ShipRec<COC_CASS_NO$>
WONo 	= ShipRec<COC_WO$>
WOStep	= ShipRec<COC_WO_STEP$>
PartNo	= ShipRec<COC_PART_NUM$>

CustNo = XLATE('COC',ShipNo,'CUST_NO','X')

CassCnt = COUNT(CassNos,@VM) + (CassNos NE '')

WOMatKeys = ''
SortedCassNos = ''

FOR I = 1 TO CassCnt
	CassNo = CassNos<1,I>
	LOCATE CassNo IN SortedCassNos BY 'AR' USING @VM SETTING Pos ELSE
		SortedCassNos	= INSERT(SortedCassNos,1,Pos,0,CassNo)
		WOMatKeys 		= INSERT(WOMatKeys,1,Pos,0,WONo:'*':CassNo)
	END
NEXT I

WMOutKeys		= XLATE('WO_MAT',WOMatKeys,WO_MAT_WMO_KEY$,'X')
TestWMOutKeys	= WMOutKeys

CustPartNo = XLATE('WO_MAT',WONo:'*1',WO_MAT_CUST_PART_NO$,'X')
RevNo = XLATE('CUST_EPI_PART', CustNo : '*' : custPartNo, 3, 'X')

CONVERT @VM TO '' IN TestWMOutKeys
Result = ''
	* EpiPro
	*Loop Through Each Casette
	FOR I = 1 TO CassCnt
	    
		WMOutKey = WMOutKeys<1,I>
		WOMatKey = WOMatKeys<1,I>
		CassRDSNos		= obj_WM_Out('CassRDSNos',WMOutKey)
		CassRDSWfrCnts	= obj_WM_Out('CassRDSWfrCnts',WMOutKey)
		CassID = WMOutKey
		CassNo = SortedCassNos<1,I>
		WMOutRec = Xlate('WM_OUT', CassID, '', 'X')
		SlotNos = WMOutRec<WM_OUT_SLOT_NO$>
		CONVERT '*' TO '.' IN CassID
		RDSCnt = COUNT(CassRDSNos,@VM) + (CassRDSNos NE '')
		SlotCount = COUNT(SlotNos,@VM)
		*Loop Through Each Slot
		For N = 1 To SlotCount + 1
		    WMInKey    = WONo : '*' : WMOutRec<WM_OUT_IN_CASS_NO$, N>
		    SlotNo     = N
		    RDSNo      = WMOutRec<2,N>
            Pocket     = WMOutRec<3,N>
            Zone       = WMOutRec<4,N>
            LotNo      = Xlate('WO_MAT', WMInKey, WO_MAT_LOT_NO$, 'X')
            PartNo     = XLate('WO_MAT',WOMatKey, WO_MAT_SUB_PART_NO$,'X')
            CassNo     = WMOutRec<5,N>
            InSlot     = WMOutRec<6,N>
		    Line       = CassID : @VM : SlotNo : @VM : RDSNo : @VM : Pocket : @VM : Zone : @VM : LotNo : @VM : PartNo : @VM : CassNo : @VM : InSlot : @VM
		    If CustNo EQ 7108 then
		        //This is data specifically for EpiSil
		        WOMatRec     = Database_Services('ReadDataRow', 'WO_MAT', WOMatKey, True$, 0, False$)
		        SAPBatchNo   = WOMatRec<WO_MAT_SAP_BATCH_NO$>
		        SubSupplier  = WOMatRec<WO_MAT_SUB_SUPPL_BY$>
		        
		        //Now Layer and Zone Metrology
		        Layer1ResRhoAvg     = ''
		        Layer2ResRhoAvg     = ''
		        Layer1ResRhoStdDev  = ''
		        Layer2ResRhoStdDev  = ''
		        Layer1ThkAvg        = ''
		        Layer2ThkAvg        = ''
		        LayerCombinedThkAvg = ''
		        RDSLayerKeys        = Database_Services('ReadDataColumn', 'REACT_RUN', RDSNo, REACT_RUN_RDS_LAYER_KEYS$, True$, 0, False$)
		        for each RDSLayerKey in RDSLayerKeys using @VM
		            ThisLayer            = Field(RDSLayerKey, '*', 2)
		            ThisLayerRDSTestKeys = Database_Services('ReadDataColumn', 'RDS_LAYER', RDSLayerKey, RDS_LAYER_RDS_TEST_KEYS$, True$, 0, False$)
		            For each RDSTestKey in ThisLayerRDSTestKeys using @VM
		                ThisRDSTestRec   = Database_Services('ReadDataRow', 'RDS_TEST', RDSTestKey, True$, 0, False$)
		                ThisRDSTestZone  = ThisRDSTestRec<RDS_TEST_ZONE$>
		                If ThisRDSTestZone EQ Zone then
		                    //It's a matching zone to the wafer we're currently working on.
		                    ThisRDSTestLayer = ThisRDSTestRec<RDS_TEST_LS_ID$>
		                    ThisSpecResTool  = ThisRDSTestRec<RDS_TEST_SPEC_RES_MTOOL$>
		                    Begin Case
		                        Case ThisRDSTestLayer EQ 'L1'
		                            if ThisRDSTestRec<RDS_TEST_RES_AVG$> NE '' then
                                        Layer1ResRhoAvg    = OCONV(ThisRDSTestRec<RDS_TEST_RES_AVG$>, 'MD4')
                                        Layer1ResRhoStdDev = OCONV(ThisRDSTestRec<RDS_TEST_RES_STDV$>, 'MD4')
                                    end else
                                        Layer1ResRhoAvg    = OCONV(ThisRDSTestRec<RDS_TEST_HGCV1_RES_AVG$>, 'MD3')
                                        Layer1ResRhoStdDev = OCONV(ThisRDSTestRec<RDS_TEST_HGCV1_RES_STDV$ >, 'MD3')
                                    end
                                    Layer1ThkAvg = OCONV(ThisRDSTestRec<RDS_TEST_THICK_AVG$>, 'MD2')
		                        Case ThisRDSTestLayer EQ 'L2'
		                            if ThisRDSTestRec<RDS_TEST_RES_AVG$> NE '' then
                                        Layer2ResRhoAvg    = OCONV(ThisRDSTestRec<RDS_TEST_RES_AVG$>, 'MD4')
                                        Layer2ResRhoStdDev = OCONV(ThisRDSTestRec<RDS_TEST_RES_STDV$>, 'MD4')
                                    end else
                                        Layer2ResRhoAvg    = OCONV(ThisRDSTestRec<RDS_TEST_HGCV1_RES_AVG$>, 'MD3')
                                        Layer2ResRhoStdDev = OCONV(ThisRDSTestRec<RDS_TEST_HGCV1_RES_STDV$ >, 'MD3')
                                    end
		                            Layer2ThkAvg       = OCONV(ThisRDSTestRec<RDS_TEST_THICK_AVG$>, 'MD2')
		                        Case ThisRDSTestLayer  EQ '2'
		                            LayerCombinedThkAvg = OCONV(ThisRDSTestRec<RDS_TEST_THICK_AVG$>, 'MD2')
		                    End Case
		                end
		                
		            Next RDSTestKey
		        Next RDSLayerKey
		        Line := Layer1ResRhoAvg : @VM : Layer2ResRhoAvg : @VM : Layer1ResRhoStdDev : @VM : Layer2ResRhoStdDev : @VM : Layer1ThkAvg : @VM : Layer2ThkAvg : @VM : LayerCombinedThkAvg : @VM
		        LatestCIKey = Clean_Insp_Services('GetLatestDefectCINoByRDSId', RDSNo)
		        ThisCleanInspRec = Database_Services('ReadDataRow', 'CLEAN_INSP', LatestCIKey, True$, 0, False$)
		        ScanCount = DCount(ThisCleanInspRec<CLEAN_INSP_SCAN_SUM_OF_DEF_AVG$>, @VM)
		        If ScanCount GT 1 then
					DefectivityAvg   = OConv(ThisCleanInspRec<CLEAN_INSP_SCAN_SUM_OF_DEF_AVG$, ScanCount>, 'MD3')
					DefectivityMin   = ThisCleanInspRec<CLEAN_INSP_SCAN_SUM_OF_DEF_MIN$, ScanCount>
					DefectivityMax   = ThisCleanInspRec<CLEAN_INSP_SCAN_SUM_OF_DEF_MAX$, ScanCount>
		        end else
					DefectivityAvg   = OConv(ThisCleanInspRec<CLEAN_INSP_SCAN_SUM_OF_DEF_AVG$>, 'MD3')
					DefectivityMin   = ThisCleanInspRec<CLEAN_INSP_SCAN_SUM_OF_DEF_MIN$>
					DefectivityMax   = ThisCleanInspRec<CLEAN_INSP_SCAN_SUM_OF_DEF_MAX$>
		        end
		        
		        Line := DefectivityAvg : @VM : DefectivityMin : @VM : DefectivityMax : @VM
		    end
		    Result<-1> = Line
		Next N
	NEXT I



SWAP @FM WITH CRLF$ IN Result
SWAP @VM WITH ',' IN Result

*Modify Here for final publish
CompRec		= XLATE('COMPANY',CustNo,'','X')
DataDir 	= CompRec<COMPANY_SHIP_DATA_DIR$>
*FileExt 	= CompRec<COMPANY_SHIP_DATA_FILE_NAME_SUFFIX$>
SuppHeader	= CompRec<COMPANY_SHIP_DATA_HEADER$>				;* Supress column Header if this is set (not used in this program)
FileExt = 'csv'

*IF FileExt = '' THEN FileExt = 'csv'							;* Uses Comma Separated Variables by default
		
*DosTable = DataDir:'\':ShipNo:'.':FileExt
DosTable = CustPath:ShipNo:'-CDS+': SendDate : '+SHIPMENT.csv'
*DosTable = 'C:\Users\ecouellette\desktop\': ShipNo:'-1.csv'
DataOut = ''

OSOPEN DosTable TO DOSFile THEN
	OSWrite DataOut ON DosTable			;* Clear file it was already there
END ELSE
	OSWrite DataOut ON DosTable			;* Create the file if it wasn't
	OSOPEN DosTable TO DOSFile ELSE
		ErrorMsg = "Unable to open ":QUOTE(DosTable):" in Export_IR routine."
		RETURN ''
	END
END  

HeaderOut  = '"Casette ID"':@VM
HeaderOut := '"Slot No"':@VM
HeaderOut := '"RDSNo"':@VM
HeaderOut := '"Pocket"':@VM
HeaderOut := '"Zone"':@VM
HeaderOut := '"Lot No"':@VM
HeaderOut := '"Part Num"':@VM
HeaderOut := '"Cass Num"':@VM
HeaderOut := '"In Slot"':@VM
if CustNo EQ 7108 then
    //This is EpiSil specific headers
    HeaderOut := '"Res/Rho Avg Layer 1"':@VM
    HeaderOut := '"Res/Rho Avg Layer 2"':@VM
    HeaderOut := '"Res/Rho Std. Dev. Layer 1"':@VM
    HeaderOut := '"Res/Rho Std. Dev. Layer 2"':@VM
    HeaderOut := '"Thk Avg Layer 1"':@VM
    HeaderOut := '"Thk Avg Layer 2"':@VM
    HeaderOut := '"Thk Avg Combined"':@VM
    HeaderOut := '"Defectivity Avg"':@VM
    HeaderOut := '"Defectivity Min"':@VM
    HeaderOut := '"Defectivity Max"':@VM
end
HeaderOut := CRLF$

SWAP @VM WITH ',' IN HeaderOut

FilePointer = 0

OSBWrite HeaderOut ON DOSFile AT FilePointer
FilePointer += LEN(HeaderOut)


DataOut = Result	

OSBWrite DataOut ON DOSFile AT FilePointer


Def = ''
Def<MTEXT$> = 'File ':DosTable:' written.'
Def<MTYPE$>	= 'TA3'

If Not(AutoFlag) then MsgUp = Msg(@WINDOW,Def)

OSClose DOSFile

RETURN DOSTable




