COMPILE FUNCTION EXPORT_TOWER_MET(ShipNo, AutoFlag)

*  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, ANSI_UTF8

$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

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

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\


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>
			RDSMetNos = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_ALL','X')
			
			MetCnt = COUNT(RDSMetNos,@VM) + (RDSMetNos NE '')
			
			FOR M = 1 TO MetCnt
				RDSMetNo = RDSMetNos<1,M>
				MetResults = obj_RDS_Test('ExpTower',RDSMetNo)
				MetResultCnt = COUNT(MetResults,@FM) + (MetResults NE '')
				
				FOR J = 1 TO MetResultCnt
					Result<-1> = '"IREPI"':@VM:QUOTE(CustPartNo):@VM:ShipNo:@VM:CassID:@VM:CassRDSNo:@VM:CassRDSWfrCnt:@VM:MetResults<J>
				NEXT J
			NEXT M
				
		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>
			RDSMetNos = XLATE('REACT_RUN',CassRDSNo,'MET_KEYS_ALL','X')
			
			MetCnt = COUNT(RDSMetNos,@VM) + (RDSMetNos NE '')
			
			FOR M = 1 TO MetCnt
				RDSMetNo = RDSMetNos<1,M>
				MetResults = obj_RDS_Test('ExpTower',RDSMetNo)
				MetResultCnt = COUNT(MetResults,@FM) + (MetResults NE '')
				
				FOR J = 1 TO MetResultCnt
					Result<-1> = '"IREPI"':@VM:QUOTE(CustPartNo):@VM:ShipNo:@VM:CassID:@VM:CassRDSNo:@VM:CassRDSWfrCnt:@VM:MetResults<J>
				NEXT J
			NEXT M
				
		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
		
DosTable = DataDir:'\':ShipNo:'.':FileExt

*DosTable = 'C:\OIReports\IREPI':ShipNo:'.spr'

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_TSV 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 := '"Parameter"':@VM
HeaderOut := '"Average"':@VM
HeaderOut := '"STDev"':@VM
HeaderOut := '"Min"':@VM
HeaderOut := '"Max"':@VM
HeaderOut := '"Sample Size"':@VM
HeaderOut := '"Spec Low"':@VM
HeaderOut := '"Spec High"':@VM
HeaderOut := '"Ctrl Low"':@VM
HeaderOut := '"Ctrl High"':@VM
HeaderOut := '"Units"':CRLF$

SWAP @VM WITH ',' IN HeaderOut

HeaderOut = ANSI_UTF8(HeaderOut)

FilePointer = 0

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

DataOut = Result

DataOut = ANSI_UTF8(DataOut)

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


