COMPILE SUBROUTINE EXPORT_MU_INV(dummy)
#pragma precomp SRP_PreCompiler

*  Makeup Wafer Inventory Report - Excel

* 10/3/2014 John C. Henry, J.C. Henry & Co., Inc.  Initial coding

DECLARE SUBROUTINE ErrMsg, Btree.Extract, Set_Status, SetInitDirOptions, RList, Make.List, Msg, SRP_Stopwatch
DECLARE FUNCTION Msg, Get_Status, obj_Calendar, Utility, SetInitDirOptions, Dialog_Box, obj_WO_Log, 
DECLARE FUNCTION Popup, obj_WM_Out, obj_WO_Mat, Collect.IXVals, obj_Export, Msg


$INSERT WO_LOG_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT PROD_VER_EQUATES
$INSERT RDS_EQUATES
$INSERT RLIST_EQUATES
$INSERT EXCEL_EQU
$INSERT MSG_EQUATES

EQU COL$SAP_BATCH_NO			TO  1
EQU COL$SAP_CASS_ID				TO	2
EQU COL$WAFER_CNT				TO  3
EQU COL$$EPI_PART_NO			TO  4
EQU COL$WO_NO					TO  5
EQU COL$CASS_NO					TO  6
EQU COL$PROD_ORD_NO				TO  7
EQU COL$SHIP_PSN				TO 	8
EQU COL$CUST_NO					TO  9												;* Added -dkk per eng request 10/13/14
EQU COL$WAFER_INCHES			TO 10												;* Added -dkk per eng request 10/13/14
EQU COL$FINAL_QA_DTM			TO 11 	
EQU COL$CURR_LOCATION_DESC		TO 12 												;* Added -dkk per quality request 3/30/15


EQU TAB$	TO \09\
EQU CRLF$	TO \0D0A\

OPEN 'WO_MAT' TO WOMatTable ELSE
	ErrMsg('Unable to open WO_MAT for Export')
	RETURN
END

OPEN 'DICT.WO_MAT' TO WOMatDict ELSE
	ErrMsg('Unable to open DICT.WO_MAT for index read')
	RETURN
END

OPEN 'DICT.WM_OUT' TO WMOutDict ELSE
	ErrMsg('Unable to open DICT.WM_OUT for index read')
	RETURN
END

TypeOver = ""
TypeOver<MTEXT$> = "Building Export..."
TypeOver<MTYPE$> = "U"

MsgUp = Msg(@WINDOW, TypeOver)	;* display the processing message


WOMatKeys 	= ''
WMOutKeys	= ''
Options 	= ''
Flag		= ''


SRP_Stopwatch('Reset')
SRP_Stopwatch('Start', 'SlowIndex')
SearchString    = 'MAKEUP_BOX':@VM:'1':@FM
TempWOMatKeys = ''
Btree.Extract(SearchString, 'WO_MAT', WOMatDict, TempWOMatKeys, Options, Flag)
WOMatKeys       = ''
For Each WOMatKey in TempWOMatKeys using @VM
	CurrStatus  = Xlate('WO_MAT', WOMatKey, 'CURR_STATUS', 'X')
	If CurrStatus EQ 'RTU' then WOMatKeys := WOMatKey : @VM
Next WOMatKey
WOMatKeys[-1, 1]    = ''

SearchString    = 'MAKEUP_BOX':@VM:'1':@FM
Btree.Extract(SearchString, 'WM_OUT', WMOutDict, TempWMOutKeys, Options, Flag)
WMOutKeys       = ''
For Each WMOutKey in TempWMOutKeys using @VM
	CurrStatus  = Xlate('WM_OUT', WMOutKey, 'CURR_STATUS', 'X')
	If CurrStatus EQ 'RTU' then WMOutKeys := WMOutKey : @VM
Next WMOutKey
WMOutKeys[-1, 1]    = ''
NumWOMatKeys    = DCount(WOMatKeys, @VM)
NumWMOutKeys    = DCount(WMOutKeys, @VM)
SRP_Stopwatch('Stop', 'SlowIndex')

wmoCnt = COUNT(WMOutKeys,@VM) + (WMOutKeys NE '')

FOR I = 1 TO wmoCnt
	WMOutKey = WMOutKeys<1,I>
	WONo 	= WMOutKey[1,'*']
	CassNo	= WMOutKey[-1,'B*']
	
	WOMatKey = WONo:'*':CassNo
	
	LOCATE WOMatKey IN WOMatKeys USING @VM SETTING Pos ELSE	
		WOMatKeys = INSERT(WOMatKeys,1,Pos,0,WOMatKey)
	END
	
NEXT I

CONVERT @VM TO @FM IN WOMatKeys

Cursor = 0
Make.List(Cursor, WOMatKeys, WOMatTable, WOMatDict)

IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	RETURN
END

SelectSent = "SELECT WO_MAT WITH MAKEUP_BOX EQ '1' BY EPI_PART_NO BY WO_NO"

RList(SelectSent,TARGET_ACTIVELIST$,'','','')

IF Get_Status(errCode) THEN
	ErrMsg(errCode)
	RETURN
END


TextColNos = ''

FOR I = 1 TO COL$FINAL_QA_DTM
	TextColNos<1,I> = I
NEXT I

ColHeads = ''
ColHeads<1,COL$SAP_BATCH_NO>	= 'SAP Batch No'
ColHeads<1,COL$SAP_CASS_ID>		= 'SAP Cass ID'
ColHeads<1,COL$WAFER_CNT>		= 'Wafer Cnt'
ColHeads<1,COL$$EPI_PART_NO>	= 'Epi Part No'
ColHeads<1,COL$WO_NO>			= 'WO No'
ColHeads<1,COL$CASS_NO>			= 'Cass No '
ColHeads<1,COL$PROD_ORD_NO>		= 'Prod Ord No' 	
ColHeads<1,COL$SHIP_PSN>		= 'Ship PSN'
ColHeads<1,COL$CUST_NO>			= 'Cust No'											;* Added -dkk per eng request 10/13/14
ColHeads<1,COL$WAFER_INCHES>	= 'Wafer Size'										;* Added -dkk per eng request 10/13/14
ColHeads<1,COL$FINAL_QA_DTM>	= 'Final QA Dtm'
ColHeads<1,COL$CURR_LOCATION_DESC>	= 'Location'									;* Added -dkk per quality request 3/30/15

ExportText = ColHeads

Done = 0
ExportLine = 2
MULoc = ''


LOOP
	READNEXT WOMatKey ELSE Done = 1
UNTIL Done
	READ WOMatRec FROM WOMatTable,WOMatKey THEN
		
		SAPBatchNo	= WOMatRec<WO_MAT_SAP_BATCH_NO$>
		SAPCassID	= XLATE('WO_MAT',WOMatKey,'CASS_ID_SAP','X')
		
		IF WOMatRec<WO_MAT_WMO_KEY$> NE '' THEN
			WafersOut = XLATE('WM_OUT',WOMatRec<WO_MAT_WMO_KEY$>,'WFRS_OUT','X')
		END ELSE
			WafersOut = XLATE('WO_MAT',WOMatKey,'CURR_WFR_CNT','X')
		END
		
		WONo 	= WOMatKey[1,'*']
		CassNo	= WOMatKey[COL2()+1,'*']
		
		WORec = XLATE('WO_LOG',WONo,'','X')
		
		EpiPartNo	= WORec<WO_LOG_EPI_PART_NO$>
		ProdOrdNo	= WORec<WO_LOG_PROD_ORD_NO$>
		
		ProdVerNo	= WORec<WO_LOG_PROD_VER_NO$>
		ProdVerPSNs = XLATE('PROD_VER',ProdVerNo,PROD_VER_PROC_STEP_PSN$,'X')
		
		ShipPSN		= ProdVerPSNs[-1,'B':@VM]
		CustNo		= WORec<WO_LOG_CUST_NO$>										;* Added -dkk per eng request 10/13/14
		
		WaferSize	= XLATE('PROD_SPEC', ShipPSN, 'SUB_WAFER_SIZE', 'X')<1,1>		;* Added -dkk per eng request 10/13/14
		SigProfile	= WOMatRec<WO_MAT_SIG_PROFILE$>
		
		profCnt = COUNT(SigProfile,@VM) + (SigProfile NE '')
		*debug
		qaIdx = Index(WOMatRec<WO_MAT_INV_LOCATION$>, 'QA', 1)
		//FinalQADtm	= ReactRunRec<REACT_RUN_FINAL_SIG_DTM$,profCnt>					;
		FinalQADtm	= WOMatRec<WO_MAT_SIG_DTM$,profCnt>								;* Final QA is always the last entry in the Sig Profile
		//FinalQADtm	= WOMatRec<WO_MAT_INV_DTM$,qaIdx>								
		MULoc		= Xlate('WO_MAT',WOMatKey,'CURR_LOCATION_DESC', 'X' )			;* Added -dkk per quality request 3/30/15
		
		DataLine						= ''
		DataLine<1,COL$SAP_BATCH_NO>	= SAPBatchNo
		DataLine<1,COL$SAP_CASS_ID>		= SAPCassID
		DataLine<1,COL$WAFER_CNT>		= WafersOut
		DataLine<1,COL$$EPI_PART_NO>	= EpiPartNo
		DataLine<1,COL$WO_NO>			= WONo
		DataLine<1,COL$CASS_NO>			= CassNo 
		DataLine<1,COL$PROD_ORD_NO>		= ProdOrdNo 	
		DataLine<1,COL$SHIP_PSN>		= ShipPSN
		DataLine<1,COL$CUST_NO>			= CustNo									;* Added -dkk per eng request 10/13/14
		DataLine<1,COL$WAFER_INCHES>	= WaferSize									;* Added -dkk per eng request 10/13/14
		DataLine<1,COL$FINAL_QA_DTM>	= OCONV(FinalQADtm,'DT4/^S')
		DataLine<1,COL$CURR_LOCATION_DESC>	= MULoc									;* Added -dkk per quality request 3/30/15
		
		ExportText<ExportLine> = DataLine
		
		ExportLine += 1
		
	END	;* End of WOMatRec read
REPEAT

Msg(@WINDOW, MsgUp)				;* take down the processing message

* * * * * * 

SWAP @VM WITH CHAR(9) IN ExportText
SWAP @FM WITH CRLF$ IN ExportText
CALL Set_Property('CLIPBOARD', 'TEXT', ExportText)

If @USER4 EQ 'DANIEL_ST' then
	OSWrite ExportText to 'D:\FITemp\MUInv.csv'
end

ExportColCnt = 12			;* Orginally 11   Changed -dkk per quality request 3/30/15


* * *  Paste it into a blank Excel Sheet

xlApp		= OleCreateInstance("excel.Application")

IF OleStatus() THEN
	
	
	ErrorMsg  = 'Excel failed to start.':CRLF$:CRLF$
	ErrorMsg := 'The data from this export is on your clipboard and can be pasted into Excel on your local machine using <Ctrl><V> .'
	
	ErrMsg(ErrorMsg)
	
	RETURN
	
END

xlWorkBooks = OleGetProperty(xlApp, "Workbooks")
xlWorkBook	= OleCallMethod(xlWorkBooks,'Add')

* * *  wait a second

Now = Time()
LOOP
	CALL Yield()
WHILE Time() EQ Now
REPEAT

OlePutProperty(XlApp, 'Visible', xlSheetVisible)

void			= OleCallMethod(xlWorkBook,'Activate')
xlActiveSheet	= OleGetProperty(xlWorkBook,'ActiveSheet')
void			= OleCallMethod(xlActiveSheet,'Paste')

eXcelCols = obj_Export('ExcelCol',TextColNos)	;* Returns alpha Excel columns from numeric columns

FirstLastCols = obj_Export('ExcelCol',1:@VM:ExportColCnt)

FirstColumn	= FirstLastCols[1,@VM]
LastColumn	= FirstLastCols[COL2()+1,@VM]

range	= OleGetProperty(xlActiveSheet,'Range',FirstColumn:'1:':LastColumn:'1')
font	= OleGetProperty(range,'Font')

OlePutProperty(font,'FontStyle','Bold')
OlePutProperty(font,'UnderLine',xlUnderlineStyleSingle)

column	= OleGetProperty(xlActiveSheet,'Columns',FirstColumn:':':LastColumn)	
void 	= OleCallMethod(column,'AutoFit')


RETURN

