COMPILE SUBROUTINE Export_WO_Log_Orig( Dummy )

DECLARE FUNCTION Msg, Dialog_Box, Utility, obj_Export, obj_WO_Log
DECLARE FUNCTION Set_Printer, Get_Printer, obj_Install, Utility, obj_MUWafers

DECLARE SUBROUTINE Btree.Extract, RList, END_Dialog, Make.List, Msg, ErrMsg, SetInitDirOptions

$INSERT LSL_USERS_EQU
$INSERT MSG_EQUATES
$INSERT RLIST_EQUATES
$INSERT OIPRINT_EQUATES
$INSERT WO_LOG_EQU
$INSERT WO_STEP_EQU
$INSERT WO_MAT_EQUATES
$INSERT COMPANY_EQU
$INSERT QUOTE_EQU
$INSERT QUOTE_SPEC_EQU
$INSERT RECIPE_EQU
$INSERT ORDER_DET_EQU
$INSERT PROD_SPEC_EQUATES
$INSERT EXCEL_EQU

CRLF$ = \0D0A\

OPEN 'WO_STEP' TO WOStepTable ELSE
	ErrMsg('Unable to open "WO_STEP" table in EXPORT_WO_LOG routine.')
	RETURN
END

OPEN 'DICT.WO_STEP' TO DictWOStep ELSE
	ErrMsg('Unable to open "DICT.WO_STEP" table in EXPORT_WO_LOG routine.')
	RETURN
END

Def = ""
Def<MTEXT$> = "Selecting Uncheduled Work Orders..."
Def<MTYPE$> = "U"

MsgUp = Msg(@window, Def)

SelectSent = 'SELECT WO_STEP WITH SCHEDULED NE "Yes" '
RList(SelectSent,TARGET_ACTIVELIST$,'','','')

*SelectSent = 'SELECT WO_STEP WITH CURR_STATUS NE "CL" "COMP" '
*SelectSent = 'SELECT WO_STEP WITH CURR_STATUS = "NEW" "RTP" "RTS" "RX" "INPR" '
*RList(SelectSent,TARGET_ACTIVELIST$,'','','')

WOStepKeys = ''
Done = 0
LOOP
	READNEXT WOStepKey ELSE Done = 1
UNTIL Done
	WOStepKeys<-1> = WOStepKey
REPEAT

CONVERT @VM TO @FM IN WOStepKeys

IF WOStepKeys = '' THEN
	Msg(@window, MsgUp)
	ErrMsg('No Work Orders remain unscheduled.')
	RETURN
END


Make.List('',WOStepKeys,WOStepTable,DictWOStep)

IF Get_Status(errCode) THEN
	ErrMsg(errCode)
END

Results = ''
Results<1,1> = 'Spec Type'
Results<1,2> = 'WO'
Results<1,3> = 'Customer'
Results<1,4> = 'Qty'
Results<1,5> = 'PSN'
Results<1,6> = 'Cust Part Numbers'
Results<1,7> = 'Reactor Type'
Results<1,8> = 'Sched Reacts'
Results<1,9> = 'Inch'
Results<1,10> = 'Tube Press Type'
Results<1,11> = 'Epi Gases'
Results<1,12> = 'Dopant L1'
Results<1,13> = 'Cap/Burst Gases'
Results<1,14> = 'Thick Target'
Results<1,15> = 'Res Target'
Results<1,16> = 'Expected Rx Dt'
Results<1,17> = 'Received Date'
Results<1,18> = 'Promised Ship'
Results<1,19> = 'Qual'
Results<1,20> = 'Blkd'
Results<1,21> = 'MU Wfrs'

Done = 0
LineCnt = 1
LOOP
	READNEXT WOStepKey ELSE Done = 1
UNTIL Done

	READ WOStepRec FROM WOStepTable,WOStepKey THEN
	
		IF WOStepRec<WO_STEP_SCHEDULED$> NE '1' THEN
			
			WONo = WOStepKey[1,'*']
			
			WORec = XLATE('WO_LOG',WONo,'','X')
			WOMatKeys = WORec<WO_LOG_WO_MAT_KEY$>
			
			CassCustPNs = XLATE('WO_MAT',WOMatKeys,WO_MAT_CUST_PART_NO$,'X')
			CustPNs = ''
			FOR I = 1 TO COUNT(CassCustPNs,@VM) + (CassCustPNs NE '')
				LOCATE CassCustPNs<1,I> IN CustPNs USING @VM SETTING POS ELSE
					CustPNs = INSERT(CustPNs,1,Pos,0,CassCustPNs<1,I>)
				END
			NEXT I
			
			CompanyName = XLATE( 'COMPANY', WORec<WO_LOG_CUST_NO$>,COMPANY_CO_NAME$,'X')
			SWAP ',' WITH '-' IN CompanyName
			
			WaferQty = SUM(XLATE('WO_MAT',WOMatKeys,WO_MAT_WAFER_QTY$,'X'))
			IF WaferQty > 0 THEN
				ExpectedRxDts = ''
			END ELSE
				OrderNo = WORec<WO_LOG_ORDER_NO$>
				OrderItems = WORec<WO_LOG_ORDER_ITEM$>
				OrderDetKeys = ''
				FOR N = 1 TO COUNT(OrderItems,@VM) + (OrderItems NE '')
					OrderDetKeys<1,N> = OrderNo:'*':OrderItems<1,N>
				NEXT N
				WaferQty = SUM(XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_ITEM_QTY$,'X'))
				ExpectedRxDts = XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_EXP_RX_DT$,'X')
			END
			
			MUWafers		= obj_MUWafers('AvailWafers',WONo:@RM:WORec:@RM:0:@RM:1)
			ShipThickTarget	= XLATE('WO_LOG',WONo,'SHIP_THICK_TARGET','X')
			ShipResTarget	= XLATE('WO_LOG',WONo,'SHIP_RES_TARGET','X')
				
			CustPSNs = XLATE('WO_STEP',@RECORD<34>,1,'X')
			
			SWAP @VM WITH '\' IN CustPNs
			
			PSNo = WOStepRec<WO_STEP_PROD_SPEC_ID$>
			
			PSReactType = OCONV(XLATE('PROD_SPEC',PSNo,PROD_SPEC_REACTOR_TYPE$,'X'),'[REACT_TYPE_CONV]')
		
			WaferSize = xlate( 'PROD_SPEC', PSNo, 'SUB_WAFER_SIZE', 'X' )
			SWAP 75		WITH '' IN WaferSize
			SWAP 100	WITH '' IN WaferSize
			SWAP 125	WITH '' IN WaferSize
			SWAP 150	WITH '' IN WaferSize
			SWAP 200	WITH '' IN WaferSize
			SWAP 'mm'	WITH '' IN WaferSize
			WaferSize = trim( WaferSize )
			
			RecipeId = XLATE( 'PROD_SPEC', PSNo, 'RECIPE_NO_L1', 'X' )
			EPIGases = XLATE( 'RECIPE', RecipeId, RECIPE_EPI_GASES$, 'X' )

			Reactors		= WOStepRec<WO_STEP_REACTORS$>
			BlockedReactors	= WOStepRec<WO_STEP_BLOCKED_REACTORS$>
			SchedReactors	= XLATE('WO_LOG',WONo,'SCHED_REACTS','X')
			
			CONVERT @VM TO ',' IN Reactors
			CONVERT @VM TO ',' IN BlockedReactors
			
			IF WaferQty > 0 THEN
				LineCnt += 1
				Results<LineCnt,1> = OCONV(XLATE( 'PROD_SPEC', PSNo, 'SPEC_TYPE', 'X' ),'[SPEC_TYPE_CONV]')				;* Spec Type
				Results<LineCnt,2> = OCONV(WONo,'MD0')																	;* WO
				Results<LineCnt,3> = CompanyName																		;* Company Name
				Results<LineCnt,4> = OCONV(WaferQty,'MDO')																;* Wafer Qty
				Results<LineCnt,5> = OCONV(PSNo,'MD0')																	;* Prod Spec ID
				Results<LineCnt,6> = CustPNs																			;* Customer Part Numbers
				Results<LineCnt,7> = PSReactType																		;* Reactor type for Prod Spec
				Results<LineCnt,8> = SchedReactors
				Results<LineCnt,9> = WaferSize																			;* Wafer Size
				Results<LineCnt,10> = XLATE( 'PROD_SPEC', PSNo, 'TUBE_PRESSURE_TYPE', 'X' )								;* Tube Pressure Type
				Results<LineCnt,11> = EPIGases
				Results<LineCnt,12> = XLATE( 'PROD_SPEC', PSNo, 'DOPANT_L1', 'X' )										;* Dopant L1
				Results<LineCnt,13> = XLATE( 'RECIPE', RecipeId, RECIPE_CAP_BURST_GASES$, 'X' )							;* Cap Burst Gases
				Results<LineCnt,14> = ShipThickTarget
				Results<LineCnt,15> = ShipResTarget
				Results<LineCnt,16> = OCONV(ExpectedRxDts<1,1>,'D4/')
				Results<LineCnt,17> = OCONV(XLATE('WO_MAT',WONo:'*1',WO_MAT_RX_DTM$,'X')[1,'.'],'D4/')
				Results<LineCnt,18> = OCONV(WORec<WO_LOG_PROMISE_SHIP_DT$>,'D4/')
				Results<LineCnt,19> = Reactors
				Results<LineCnt,20> = BlockedReactors
				Results<LineCnt,21> = MUWafers
				TempLine = Results<LineCnt>
				CONVERT '"' TO '' IN TempLine
				Results<LineCnt> = TempLine
			END
		END
	END
REPEAT

PasteBlob = Results

* * * * * * 

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


ExportColCnt = 20


* * *  Paste it into a blank Excel Sheet

xlApp		= OleCreateInstance("excel.Application")

IF OleStatus() THEN

	Msg(@window, MsgUp)
	
	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')

Msg(@window, MsgUp)

RETURN



