COMPILE FUNCTION EXPORT_CDS(ShipNo, AutoFlag,CustPath)

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

* 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.
* 05/29/2020 Jonathan R. Ouellette, Still COVID-19 Quarantine from my couch. Further Modified from EXPORT_COA to output only Cassette Info.

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
$INSERT WM_OUT_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

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

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


