COMPILE FUNCTION EXPORT_IR(ShipNo, AutoFlag, CustPath)

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

* 12/2/2008 John C. Henry, J.C. Henry & Co., Inc.  Initial coding

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, 
DECLARE FUNCTION Popup, obj_WM_Out, obj_WO_Mat

$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')

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

CONVERT @VM TO '' IN TestWMOutKeys

Result = ''

IF TestWMOutKeys = '' THEN
	* ASM reactor
	FOR I = 1 TO CassCnt
		WOMatKey		= WOMatKeys<1,I>
		CassRDSNos		= obj_WO_Mat('CassRDSNos',WOMatKey)
		CassRDSWfrCnts	= 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>
			CassRDSWfrCnt = CassRDSWfrCnts<1,N>
			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')
			
			MetResults = obj_RDS_Test('ExpIR',L1MetNo):@VM:obj_RDS_Test('ExpIR',L2MetNo)
				
			Result<-1> = '"IREPI"':@VM:QUOTE(CustPartNo):@VM:ShipNo:@VM:CassID:@VM:CassRDSNo:@VM:CassRDSWfrCnt:@VM:PSNo:@VM:MetResults
				
		NEXT N
	NEXT I
END ELSE
	* EpiPro
	FOR I = 1 TO CassCnt
		WMOutKey = WMOutKeys<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>
			
			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')

			MetResults = obj_RDS_Test('ExpIR',L1MetNo):@VM:obj_RDS_Test('ExpIR',L2MetNo)
				
			Result<-1> = '"IREPI"':@VM:QUOTE(CustPartNo):@VM:ShipNo:@VM:CassID:@VM:CassRDSNo:@VM:CassRDSWfrCnt:@VM:PSNo:@VM:MetResults
				
				
		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$>
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)

IF FileExt = '' THEN FileExt = 'csv'							;* Uses Comma Separated Variables by default
		
SAPDelNo = Xlate('COC', ShipNo, 'SAP_DEL_NO', 'X')
* DosTable = CustPath : ShipNo:'.':FileExt
DosTable = CustPath : SAPDelNo:'.':FileExt

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  = '"Vendor"':@VM
HeaderOut := '"Product"':@VM
HeaderOut := '"Ship No"':@VM
HeaderOut := '"Cass ID"':@VM
HeaderOut := '"RDS"':@VM
HeaderOut := '"Wfr Qty"':@VM
HeaderOut := '"PSN"':@VM

HeaderOut := '"L80 Thick"':@VM
HeaderOut := '"L70 Thick"':@VM
HeaderOut := '"L60 Thick"':@VM
HeaderOut := '"L50 Thick"':@VM
HeaderOut := '"L40 Thick"':@VM
HeaderOut := '"L30 Thick"':@VM
HeaderOut := '"L20 Thick"':@VM
HeaderOut := '"L10 Thick"':@VM
HeaderOut := '"Cen Thick"':@VM
HeaderOut := '"R10 Thick"':@VM
HeaderOut := '"R20 Thick"':@VM
HeaderOut := '"R30 Thick"':@VM
HeaderOut := '"R40 Thick"':@VM
HeaderOut := '"R50 Thick"':@VM
HeaderOut := '"R60 Thick"':@VM
HeaderOut := '"R70 Thick"':@VM

HeaderOut := '"R80 Res"':@VM
HeaderOut := '"L80 Res"':@VM
HeaderOut := '"L70 Res"':@VM
HeaderOut := '"L60 Res"':@VM
HeaderOut := '"L50 Res"':@VM
HeaderOut := '"L40 Res"':@VM
HeaderOut := '"L30 Res"':@VM
HeaderOut := '"L20 Res"':@VM
HeaderOut := '"L10 Res"':@VM
HeaderOut := '"Cen Res"':@VM
HeaderOut := '"R10 Res"':@VM
HeaderOut := '"R20 Res"':@VM
HeaderOut := '"R30 Res"':@VM
HeaderOut := '"R40 Res"':@VM
HeaderOut := '"R50 Res"':@VM
HeaderOut := '"R60 Res"':@VM
HeaderOut := '"R70 Res"':@VM
HeaderOut := '"R80 Res"':@VM

HeaderOut := '"L80 HgCv1"':@VM
HeaderOut := '"L70 HgCv1"':@VM
HeaderOut := '"L60 HgCv1"':@VM
HeaderOut := '"L50 HgCv1"':@VM
HeaderOut := '"L40 HgCv1"':@VM
HeaderOut := '"L30 HgCv1"':@VM
HeaderOut := '"L20 HgCv1"':@VM
HeaderOut := '"L10 HgCv1"':@VM
HeaderOut := '"Cen HgCv1"':@VM
HeaderOut := '"R10 HgCv1"':@VM
HeaderOut := '"R20 HgCv1"':@VM
HeaderOut := '"R30 HgCv1"':@VM
HeaderOut := '"R40 HgCv1"':@VM
HeaderOut := '"R50 HgCv1"':@VM
HeaderOut := '"R60 HgCv1"':@VM
HeaderOut := '"R70 HgCv1"':@VM
HeaderOut := '"R80 HgCv1"':@VM

HeaderOut := '"L80 Thick_L2"':@VM
HeaderOut := '"L70 Thick_L2"':@VM
HeaderOut := '"L60 Thick_L2"':@VM
HeaderOut := '"L50 Thick_L2"':@VM
HeaderOut := '"L40 Thick_L2"':@VM
HeaderOut := '"L30 Thick_L2"':@VM
HeaderOut := '"L20 Thick_L2"':@VM
HeaderOut := '"L10 Thick_L2"':@VM
HeaderOut := '"Cen Thick_L2"':@VM
HeaderOut := '"R10 Thick_L2"':@VM
HeaderOut := '"R20 Thick_L2"':@VM
HeaderOut := '"R30 Thick_L2"':@VM
HeaderOut := '"R40 Thick_L2"':@VM
HeaderOut := '"R50 Thick_L2"':@VM
HeaderOut := '"R60 Thick_L2"':@VM
HeaderOut := '"R70 Thick_L2"':@VM
HeaderOut := '"R80 Thick_L2"':@VM

HeaderOut := '"L80 Res_L2"':@VM
HeaderOut := '"L70 Res_L2"':@VM
HeaderOut := '"L60 Res_L2"':@VM
HeaderOut := '"L50 Res_L2"':@VM
HeaderOut := '"L40 Res_L2"':@VM
HeaderOut := '"L30 Res_L2"':@VM
HeaderOut := '"L20 Res_L2"':@VM
HeaderOut := '"L10 Res_L2"':@VM
HeaderOut := '"Cen Res_L2"':@VM
HeaderOut := '"R10 Res_L2"':@VM
HeaderOut := '"R20 Res_L2"':@VM
HeaderOut := '"R30 Res_L2"':@VM
HeaderOut := '"R40 Res_L2"':@VM
HeaderOut := '"R50 Res_L2"':@VM
HeaderOut := '"R60 Res_L2"':@VM
HeaderOut := '"R70 Res_L2"':@VM
HeaderOut := '"R80 Res_L2"':@VM

HeaderOut := '"L80 HgCv1_L2"':@VM
HeaderOut := '"L70 HgCv1_L2"':@VM
HeaderOut := '"L60 HgCv1_L2"':@VM
HeaderOut := '"L50 HgCv1_L2"':@VM
HeaderOut := '"L40 HgCv1_L2"':@VM
HeaderOut := '"L30 HgCv1_L2"':@VM
HeaderOut := '"L20 HgCv1_L2"':@VM
HeaderOut := '"L10 HgCv1_L2"':@VM
HeaderOut := '"Cen HgCv1_L2"':@VM
HeaderOut := '"R10 HgCv1_L2"':@VM
HeaderOut := '"R20 HgCv1_L2"':@VM
HeaderOut := '"R30 HgCv1_L2"':@VM
HeaderOut := '"R40 HgCv1_L2"':@VM
HeaderOut := '"R50 HgCv1_L2"':@VM
HeaderOut := '"R60 HgCv1_L2"':@VM
HeaderOut := '"R70 HgCv1_L2"':@VM
HeaderOut := '"R80 HgCv1_L2"':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


