COMPILE SUBROUTINE Export_Passdown(Type,Yesterday,ReactNo)

DECLARE FUNCTION Msg, Dialog_Box, Utility, obj_Export, UTF8_UNICODE
DECLARE FUNCTION Set_Printer, Get_Printer, obj_Install, obj_OEE

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

$INSERT LSL_USERS_EQU
$INSERT MSG_EQUATES
$INSERT RLIST_EQUATES
$INSERT OIPRINT_EQUATES
$INSERT REACT_EVENT_EQUATES
$INSERT DICT_EQUATES
$INSERT EXCEL_EQU

CRLF$	= \0D0A\
LF$		= \0A\
TAB$	= \09\

ErrorTitle		= 'Error in Store Procedure Export_Passdown'

OPEN 'REACT_EVENT' TO ReactEventTable ELSE
	ErrorMsg = 'Unable to Open "REACT_EVENT" table!'
	Set_Status(1,ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

OPEN 'DICT.REACT_EVENT' TO @DICT ELSE
	ErrorMsg = 'Unable to Open "DICT.REACT_EVENT" table!'
	Set_Status(1,ErrorTitle:@SVM:ErrorMsg)
	RETURN
END

IF NOT(Assigned(Yesterday))	THEN Yesterday = ''
IF NOT(Assigned(ReactNo))	THEN ReactNo = ''

IF NOT(Assigned(Type)) THEN
	ErrMsg('Undefined Type passed to Export_Passdown routine.')
END

IF Type = 'Last24'THEN
	Yesterday = OCONV(Date()-1,'D4/')
END

* Generate list of EventKeys to export *

IF Yesterday NE '' THEN

	IF ReactNo NE '' THEN
		SelectString = 'SELECT REACT_EVENT WITH EVENT_DT GE ':QUOTE(Yesterday):'AND WITH REACT_NO EQ ':QUOTE(ReactNo):' BY-DSND EVENT_DTM'
	END ELSE
		SelectString = 'SELECT REACT_EVENT WITH EVENT_DT GE ':QUOTE(Yesterday):'BY REACT_NO BY-DSND EVENT_DTM'
	END

	RList(SelectString,TARGET_ACTIVELIST$,'','','')

	IF Get_Status(errCode) THEN
		ErrMsg(errCode)
		RETURN
	END
	
	EventKeys = ''
	Done = 0
	LOOP
		READNEXT EventKey ELSE Done = 1
	UNTIL Done
		EventKeys := EventKey:@VM
	REPEAT

	EventKeys[-1,1]	= ''
	ReactNos		= ReactNo
	StartDt			= Yesterday
	EndDt			= OCONV(Date(),'D4/')
	RptHeader		= 'Reactor Events Since   ':Yesterday:' 00:00:01AM'
	
END ELSE

	ReportParms =  Dialog_Box('DIALOG_PASSDOWN_RPT',@WINDOW)

	EventKeys	= ReportParms<1>
	ReactNos	= ReportParms<2>
	StartDt		= ReportParms<3>
	EndDt		= ReportParms<4>
	
	RptHeader	= ' Reactor Passdown Report '
END

Today		= OCONV(Date(),'D4/')

* Begin building export data by building column headings *

ExportCols  	= 'EVENT_DTM'
ExportCols<1,2> = 'TYPE_DESC'
ExportCols<1,3> = 'REACT_NO'
ExportCols<1,4> = 'REACT_ESC_HRS'
ExportCols<1,5> = 'EVENT_USER'
ExportCols<1,6> = 'REACT_MODE'
ExportCols<1,7> = 'LAST_READ_WFRS_VAL'
ExportCols<1,8> = 'LAST_READ_WFRS_DTM'
ExportCols<1,9> = 'REACT_PROB_CAT'
ExportCols<1,10> = 'REACT_SERV_DESC'
ExportCols<1,11> = 'WO_NO'
ExportCols<1,12> = 'TYPE_NOTE'

ExportColCnt = COUNT(ExportCols,@VM ) + (ExportCols NE '')
BytePos = 0

PasteOut 	= ''
PasteBlob 	= ''

ColNames = ExportCols

CONVERT @VM TO @FM IN ColNames

ColHeads = XLATE( 'DICT.REACT_EVENT', ColNames, DICT_DISPLAY$, 'X' )	;* Get column headings 
CONVERT @VM TO ' ' IN ColHeads								;* Convert multi-lined headings to single line 

ExportData = ''												;* Used when writing export data to DOS
DataCols = ''

FOR I = 1 TO ExportColCnt
	PasteOut<1,I> = ColHeads<I>
	DataCols<1,I> = I
NEXT I

PasteBlob<-1> = PasteOut

RecCnt = COUNT(EventKeys,@VM) + (EventKeys NE '')

FOR RecNo = 1 TO RecCnt
	@ID = EventKeys<1,RecNo>
	
	READ @RECORD FROM ReactEventTable,@ID THEN
		ReactNo			= @ID[1,'*']
		EventDtm		= OCONV(@ID[COL2()+1,'*'],'DT4/^HS')
		
		EventType		= {EVENT_TYPE}
		EventTypeDesc	= {TYPE_DESC}
		EventUser		= {EVENT_USER}
		ReactMode		= OCONV(@RECORD<REACT_EVENT_REACT_MODE$>,'[REACT_MODE_CONV]')
		ReactEscHrs		= OCONV(@RECORD<REACT_EVENT_REACT_ESC_HRS$>,'MD2,')
		ReactWfrCnt		= OCONV(@RECORD<REACT_EVENT_LAST_READ_WFRS_VAL$>,'MD0,')
		ReactWfrCntDtm	= OCONV(@RECORD<REACT_EVENT_LAST_READ_WFRS_DTM$>,'DT4/^HS')
		ReactProbCat	= {REACT_PROB_CAT_DESC}			;* This is only the first value
		ReactServID		= {REACT_SERV_DESC}				;* This is only the first value
		WONo			= @RECORD<REACT_EVENT_WO_NO$>
		TypeNote		= {TYPE_NOTE}
		
		SWAP 'CRLF$' WITH @TM IN TypeNote
		SWAP @TM WITH ' | ' IN TypeNote
		
		PasteOut  		= EventDtm
		PasteOut<1,2>	= EventTypeDesc
		PasteOut<1,3>	= ReactNo
		PasteOut<1,4>	= ReactEscHrs
		PasteOut<1,5>	= EventUser
		PasteOut<1,6>	= ReactMode
		PasteOut<1,7>	= ReactWfrCnt
		PasteOut<1,8>	= ReactWfrCntDtm
		PasteOut<1,9>	= ReactProbCat
		PasteOut<1,10>	= ReactServID
		PasteOut<1,11>	= WONo
		PasteOut<1,12>	= TypeNote
		
		PasteBlob<-1> = PasteOut
				
	END	;* End of READ
NEXT RecNo

SWAP @VM WITH CHAR(9)	IN PasteBlob
SWAP @FM WITH CRLF$		IN PasteBlob

CALL Set_Property('CLIPBOARD', 'TEXT', PasteBlob)

* * *  Paste it into a blank Excel Sheet

TextColNos = 1:@VM

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)
	
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',DataCols)	;* Returns alpha Excel columns from numeric columns

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

FirstColumn		= FirstLastCols[1,@VM]
LastColumn		= FirstLastCols[COL2()+1,@VM]
SecondColumn	= FIELD(eXcelCols,@VM,2)

NextToLastColumn = FIELD(eXcelCols,@VM,ExportColCnt - 1)

* Format the column headings

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

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

* Set all columns to "Center" alignemnt

column = OleGetProperty(xlActiveSheet,'Columns',FirstColumn:':':LastColumn)
OlePutProperty(column,'VerticalAlignment',xlCenter)

* Autofit  columns in the middle
	
column	= OleGetProperty(xlActiveSheet,'Columns',SecondColumn:':':NextToLastColumn)
void 	= OleCallMethod(column,'AutoFit')

* Set the first column with the Timestamp so it is wrapped into two lines

column	= OleGetProperty(xlActiveSheet,'Columns','A:A')
OlePutProperty(column,'ColumnWidth',11)
OlePutProperty(column,'WrapText',xlTrue)

* Set the last column (notes) to width and word wrap it

column	= OleGetProperty(xlActiveSheet,'Columns',LastColumn:':':LastColumn)
OlePutProperty(column,'ColumnWidth',50)
OlePutProperty(column,'WrapText',xlTrue)

RETURN
