COMPILE FUNCTION EXPORT_COA(ShipNo, AutoFlag, CustPath)

*  EPIPro Data Report - goes straight to CSV for FTP transmission to customer

* 12/2/2008 John C. Henry, J.C. Henry & Co., Inc.  Initial coding
* 04/15/2020 Jonathan R. Ouellette, COVID-19 Quarantine from my couch, Inc. Modified this from EXPORT_IR.

DECLARE SUBROUTINE ErrMsg, Btree.Extract, Set_Status, SetInitDirOptions
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
DECLARE FUNCTION Environment_Services

$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


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

EQU TAB$ 	TO CHAR(9)
EQU CRLF$	TO \0D0A\

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 = ''

IF TestWMOutKeys = '' THEN
	* ASM reactor
	FOR I = 1 TO CassCnt
		WOMatKey		= WOMatKeys<1,I>
		WOMatInfo = obj_WO_MAT('ExpCOA' , WOMatKey)
		CassRDSNos		= obj_WO_Mat('CassRDSNos',WOMatKey)
		CassRDSWfrCnts	= SUM(obj_WO_Mat('CassRDSWfrCnts',WOMatKey))
		CassID			= XLATE('WO_MAT',WOMatKey,'SHIP_RDS','X')
		
		RDSCnt = COUNT(CassRDSNos,@VM) + (CassRDSNos NE '')
		
		FOR N = 1 TO RDSCnt
		    
			CassRDSNo = CassRDSNos<1,N>
			IF CassRDSNo EQ WOMatInfo<WO_MAT_RDS_NO$> THEN
			CassRDSWfrCnt = CassRDSWfrCnts

			L1MetNo = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_L1','X')[1,@VM]	;* Take the first one in the layer - ignore zone
			L2MetNo = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_L2','X')[1,@VM]	;* Take the first one in the layer - ignore zone
			PSNo	= XLATE('REACT_RUN',CassRDSNo,'PS_NO','X')
			CINo    = XLATE('REACT_RUN',CassRDSNo,'CI_NO','X')

			MetResults = obj_RDS_Test('ExpCOA',L1MetNo,'X')
			IF L2MetNo NE '' THEN
			    MetResults2 = obj_RDS_Test('ExpCOA',L2MetNo,'X')
			END ELSE
			    MetResults2 = ''
			end
			WOMatInfo = obj_WO_MAT('ExpCOA' , WOMatKey)
			

			
			TimeStamp = OConV(WOMatInfo<WO_MAT_WIP_START_DTM$>,'D4/H')
			SAPBatchNo = WOMatInfo<WO_MAT_SAP_BATCH_NO$>
			SubPartNum = WOMatInfo<WO_MAT_SUB_PART_NO$>
			SubSuppCd = WOMatInfo<WO_MAT_SUB_VEND_CD$>
			SubPartMfg = XLATE('SEMI_VEND_CODE',SubSuppCd,1,'X')
			SubLotNo = WOMatInfo<WO_MAT_LOT_NO$>

		    *Tencor Stuff
		    DefAvg = OCONV(XLATE('CLEAN_INSP',CINo,'SCAN_SUM_OF_DEF_AVG','X')[1,@VM], 'MD3')
		    DefMin = XLATE('CLEAN_INSP',CINo,'SCAN_SUM_OF_DEF_Min','X')[1,@VM]
		    DefMax = XLATE('CLEAN_INSP',CINo,'SCAN_SUM_OF_DEF_MAX','X')[1,@VM]

			Result<-1> = QUOTE(CustPartNo): @VM :RevNo:@VM: TimeStamp :@VM:WONo:@VM:SAPBatchNo:@VM:CassRDSNo:@VM:CassRDSWfrCnt: @VM : SubPartMfg : @VM : SubPartNum : @VM: SubLotNo :@VM : MetResults : @VM: DefAvg	: @VM : DefMin	: @VM : DefMax : @VM : MetResults2
			END
		NEXT N
	NEXT I
END ELSE
	* EpiPro

	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
		CONVERT '*' TO '.' IN CassID
		
		RDSCnt = COUNT(CassRDSNos,@VM) + (CassRDSNos NE '')
		
		FOR N = 1 TO RDSCnt
			CassRDSNo = CassRDSNos<1,N>
			CassRDSWfrCnt = CassRDSWfrCnts<1,N>
			MetResultsL1Z1 = ''
			MetResultsL1Z2 = ''
			MetResultsL2Z1 = ''
			MetResultsL2Z2 = ''
			
			*This is the data for layer 1 metrology
			L1MetNoZone1 = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_L1_Z1','X')[1,@VM]	;
			L1MetNoZone2 = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_L1_Z2','X')[1,@VM]	;

			*This is the data for layer 2 metrology
			L2MetNoZone1 = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_L2_Z1','X')[1,@VM]	;
			L2MetNoZone2 = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_L2_Z2','X')[1,@VM]	;

			PSNo	= XLATE('REACT_RUN',CassRDSNo,'PS_NO','X')
			CINo    = XLATE('REACT_RUN',CassRDSNo,'CI_NO','X')

            IF L1MetNoZone1 NE ''  THEN
			    MetResultsL1Z1 = obj_RDS_Test('ExpCOA',L1MetNoZone1,'X');*Layer 1 zone 1 metrology
            end
            
			IF L1MetNoZone2 NE '' THEN
			    MetResultsL1Z2 = obj_RDS_Test('ExpCOA',L1MetNoZone2,'X');*Layer 1 zone 2 metrology
			end 

			IF L2MetNoZone1 NE ''  THEN
			    MetResultsL2Z1 = obj_RDS_Test('ExpCOA',L2MetNoZone1,'X');*Layer 1 zone 1 metrology
            end
            
			IF L2MetNoZone2 NE '' THEN
			    MetResultsL2Z2 = obj_RDS_Test('ExpCOA',L2MetNoZone2,'X');*Layer 1 zone 2 metrology
			end 
			WOMatInfo = obj_WO_MAT('ExpCOA' , WOMatKey)
				
			SubSuppCd = WOMatInfo<WO_MAT_SUB_VEND_CD$>
		    TimeStamp = OConV(WOMatInfo<WO_MAT_WIP_START_DTM$>,'D4/H')
			SAPBatchNo = WOMatInfo<WO_MAT_SAP_BATCH_NO$>
			SubPartNum = WOMatInfo<WO_MAT_SUB_PART_NO$>
			SubPartMfg = XLATE('SEMI_VEND_CODE',SubSuppCd,1,'X')
			SubLotNo = WOMatInfo<WO_MAT_LOT_NO$>
			MUWafer = XLATE('WM_OUT',WMOutKey,36,'X')
			
		
		    *Tencor Stuff
		    NCRNo = Xlate('RDS', CassRDSNo, 257, 'X')
		    DefAvg = XLATE('CLEAN_INSP',CINo,'SCAN_SUM_OF_DEF_AVG','X')[1,@VM]
		    DefMin = XLATE('CLEAN_INSP',CINo,'SCAN_SUM_OF_DEF_Min','X')[1,@VM]
		    DefMax = XLATE('CLEAN_INSP',CINo,'SCAN_SUM_OF_DEF_MAX','X')[1,@VM]

		    RecalcDefMax = GetNewScanData(CINo, DefMax, NCRNo)
		    RecalcDefAvg = Getnewscandataavg(CINo, DefMax, NCRNo, DefAvg)
			Line         = ''
			Line<1, 1>   = CustPartNo
			Line<1, 2>   = TimeStamp
			Line<1, 3>   = WONo
			Line<1, 4>   = CassRDSNo
			Line<1, 5>   = SAPBatchNo
			Line<1, 6>   = SubPartMfg
			Line<1, 7>   = SubPartNum
			Line<1, 8>   = SubLotNo
			Line<1, 9>   = CassRDSWfrCnt
			If MetResultsL1Z1 NE '' then
				Line<1,10> = MetResultsL1Z1<1,1>
				Line<1,11> = MetResultsL1Z1<1,2>
				Line<1,12> = MetResultsL1Z1<1,3>
				Line<1,13> = MetResultsL1Z1<1,4>
				Line<1,14> = MetResultsL1Z1<1,5>
				Line<1,15> = MetResultsL1Z1<1,6>
				Line<1,16> = MetResultsL1Z1<1,7>
				Line<1,17> = MetResultsL1Z1<1,8>
			end
			If MetResultsL1Z2 NE '' then
				Line<1,18> = MetResultsL1Z2<1,1>
				Line<1,19> = MetResultsL1Z2<1,2>
				Line<1,20> = MetResultsL1Z2<1,3>
				Line<1,21> = MetResultsL1Z2<1,4>
				Line<1,22> = MetResultsL1Z2<1,5>
				Line<1,23> = MetResultsL1Z2<1,6>
				Line<1,24> = MetResultsL1Z2<1,7>
				Line<1,25> = MetResultsL1Z2<1,8>
			end
			If MetResultsL2Z1 NE '' then
				Line<1,26> = MetResultsL2Z1<1,1>
				Line<1,27> = MetResultsL2Z1<1,2>
				Line<1,28> = MetResultsL2Z1<1,3>
				Line<1,29> = MetResultsL2Z1<1,4>
				Line<1,30> = MetResultsL2Z1<1,5>
				Line<1,31> = MetResultsL2Z1<1,6>
				Line<1,32> = MetResultsL2Z1<1,7>
				Line<1,33> = MetResultsL2Z1<1,8>
			end
			If MetResultsL2Z2 NE '' then
				Line<1,34> = MetResultsL2Z2<1,1>
				Line<1,35> = MetResultsL2Z2<1,2>
				Line<1,36> = MetResultsL2Z2<1,3>
				Line<1,37> = MetResultsL2Z2<1,4>
				Line<1,38> = MetResultsL2Z2<1,5>
				Line<1,39> = MetResultsL2Z2<1,6>
				Line<1,40> = MetResultsL2Z2<1,7>
				Line<1,41> = MetResultsL2Z2<1,8>
			end
			Line<1,42> = RecalcDefAvg
			Line<1,43> = DefMin
			Line<1,44> = RecalcDefMax
			Result<-1> = Line
		NEXT N
	NEXT I
END


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


CompRec		= XLATE('COMPANY',CustNo,'','X')
	
DataDir 	= CompRec<COMPANY_SHIP_DATA_DIR$>

SuppHeader	= CompRec<COMPANY_SHIP_DATA_HEADER$>				;* Supress column Header if this is set (not used in this program)
FileExt = 'csv'


DosTable = CustPath : ShipNo:'+': SendDate : '+SHIPMENT.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       = ''
HeaderOut<1, 1> = '"Epi Part No"':@VM
HeaderOut<1, 2> = '"Date Out"':@VM
HeaderOut<1, 3> = '"WO"':@VM
HeaderOut<1, 4> = '"RDS"':@VM
HeaderOut<1, 5> = '"SAP Batch No"':@VM
HeaderOut<1, 6> = '"Sub Part Mfg"':@VM
HeaderOut<1, 7> = '"Sub Part Num"':@VM
HeaderOut<1, 8> ='"Lot Number"':@VM
HeaderOut<1, 9> ='"Qty"':@VM


HeaderOut<1, 10> = '"Thickness Avg Layer 1 Zone 1"':@VM
HeaderOut<1, 11> = '"Thickness Stdev Layer 1 Zone 1"':@VM
HeaderOut<1, 12> = '"Thickness Min Layer 1 Zone 1"':@VM
HeaderOut<1, 13> = '"Thickness Max Layer 1 Zone 1"':@VM
HeaderOut<1, 14> = '"Res/RHO Avg Layer 1 Zone 1"':@VM
HeaderOut<1, 15> = '"Res/RHO Stdev Layer 1 Zone 1"':@VM
HeaderOut<1, 16> ='"Res/RHO Min Layer 1 Zone 1"':@VM
HeaderOut<1, 17> = '"Res/RHO Max Layer 1 Zone 1"':@VM


HeaderOut<1, 18> = '"Thickness Avg Layer 1 Zone 2"':@VM
HeaderOut<1, 19> = '"Thickness Stdev Layer 1 Zone 2"':@VM
HeaderOut<1, 20> = '"Thickness Min Layer 1 Zone 2"':@VM
HeaderOut<1, 21> = '"Thickness Max Layer 1 Zone 2"':@VM
HeaderOut<1, 22> = '"Res/RHO Avg Layer 1 Zone 2"':@VM
HeaderOut<1, 23> = '"Res/RHO Stdev Layer 1 Zone 2"':@VM
HeaderOut<1, 24> = '"Res/RHO Min Layer 1 Zone 2"':@VM
HeaderOut<1, 25> = '"Res/RHO Max Layer 1 Zone 2"':@VM


HeaderOut<1, 26> = '"Thickness Avg Layer 2 Zone 1"':@VM
HeaderOut<1, 27> ='"Thickness Stdev Layer 2 Zone 1"':@VM
HeaderOut<1, 28> = '"Thickness Min Layer 2 Zone 1"':@VM
HeaderOut<1, 29> ='"Thickness Max Layer 2 Zone 1"':@VM
HeaderOut<1, 30> = '"Res/RHO Avg Layer 2 Zone 1"':@VM
HeaderOut<1, 31> ='"Res/RHO Stdev Layer 2 Zone 1"':@VM
HeaderOut<1, 32> ='"Res/RHO Min Layer 2 Zone 1"':@VM
HeaderOut<1, 33> ='"Res/RHO Max Layer 2 Zone 1"':@VM


HeaderOut<1, 34> ='"Thickness Avg Layer 2 Zone 2"':@VM
HeaderOut<1, 35> ='"Thickness Stdev Layer 2 Zone 2"':@VM
HeaderOut<1, 36> ='"Thickness Min Layer 2 Zone 2"':@VM
HeaderOut<1, 37> ='"Thickness Max Layer 2 Zone 2"':@VM
HeaderOut<1, 38> ='"Res/RHO Avg Layer 2 Zone 2"':@VM
HeaderOut<1, 39> ='"Res/RHO Stdev Layer 2 Zone 2"':@VM
HeaderOut<1, 40> ='"Res/RHO Min Layer 2 Zone 2"':@VM
HeaderOut<1, 41> ='"Res/RHO Max Layer 2 Zone 2"':@VM


HeaderOut<1, 42>  ='"Defectivity Avg"':@VM
HeaderOut<1, 43>  ='"Defectivity Min"':@VM
HeaderOut<1, 44>  ='"Defectivity Max"':@VM
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



