COMPILE FUNCTION Comm_Dialog_Tool_Status(Method, Parm1)

/*
	Commuter module for Tool_Status window.
	
	10/18/2007 - John C. Henry, J.C. Henry & Co., Inc.
*/
#pragma precomp SRP_PreCompiler

DECLARE SUBROUTINE Set_Property, End_Dialog, Send_Event, Set_Status, ErrMsg, Tool_Services, Error_Services
DECLARE SUBROUTINE obj_Appwindow, Start_Window, Btree.Extract, Msg, obj_Tool_Log
DECLARE FUNCTION Get_Property, Get_Status, Dialog_Box, Utility, Send_Message, Popup, Collect.IXVals, Msg
DECLARE FUNCTION obj_Tool, SRP_Array, Error_Services

EQU CRLF$	TO \0D0A\

EQU FONTBLUE$	TO 16711680   ;* \FF0000\
EQU FONTRED$	TO 255        ;* \0000FF\
EQU APPYELLOW$	TO 255 + (255*256) + (202*65536)

$INSERT PS_EQUATES
$INSERT POPUP_EQUATES
$INSERT MSG_EQUATES
$INSERT TOOL_EQUATES
$INSERT APPCOLORS

ErrTitle = 'Error in Comm_Dialog_Tool_Status'
ErrorMsg = ''

Result = ''

BEGIN CASE
	CASE Method = 'Create'			; GOSUB Create
	CASE Method = 'TBClick'			; GOSUB TBClick
	CASE Method = 'Timer'			; GOSUB Refresh
	CASE 1
		ErrMsg(ErrTitle:@SVM:'Unknown method ':QUOTE(Method):' passed to routine.')
		
END CASE

RETURN Result


* * * * * * *
Create:
* * * * * * *

ScreenSize = Get_Property('SYSTEM','SIZE')
DialogSize = Get_Property(@WINDOW,'SIZE')

wScreen = ScreenSize<3>
hScreen = ScreenSize<4>

xDialog = DialogSize<1>
yDialog = DialogSize<2>
wDialog = DialogSize<3>
hDialog = DialogSize<4>

DialogSize<1> = (wScreen - wDialog)/2
DialogSize<2> = 580; //496							;* 50 pixels @ top + 444 pixel dialog above + 2 pixel space

Set_Property(@WINDOW,'SIZE',DialogSize:@FM:-1)

CurSize = Get_Property(@Window, "SIZE")
MinW = CurSize<3>
MinH = CurSize<4>
MaxW = CurSize<3>
MaxH = CurSize<4>
Set_Property(@Window,"TRACKINGSIZE",MinW:@FM:MinH:@FM:MaxW:@FM:MaxH)

GOSUB Refresh

Set_Property(@WINDOW,'TIMER',60000:@FM:40000)

Set_Property(@WINDOW,'VISIBLE',1)

RETURN


* * * * * * *
Refresh:
* * * * * * *

ToolIDs = obj_Tool('KeysByType')		;* All tool IDs sorted by TOOL_TYPE

CleansTools		= ''
MetrologyTools	= ''
InspectionTools	= ''
TransferTools	= ''
QuartzTools		= ''
OtherTools		= ''

CTCnt		= 0
MTCnt		= 0
ITCnt		= 0
TRCnt		= 0
QZCnt		= 0
OtherCnt	= 0

DispTools   = ''

For Each ToolID in ToolIDs using @VM
	ToolRec  = XLATE('TOOL',ToolID,'','X')
	ToolType = ToolRec<TOOL_TOOL_TYPE$>
	ToolDesc = ToolRec<TOOL_TOOL_DESC$>
	ToolMode = XLATE('TOOL', ToolID, 'CURR_MODE', 'X')<1,1>
	ToolHrs  = OCONV(XLATE('TOOL', ToolID, 'CURR_MODE_HRS', 'X'), 'MD2')
	PMStatus = XLATE('TOOL', ToolID, 'PM_STATUS', 'X')
	IF ToolRec<TOOL_ACTIVE_ESCALATION$> = '' THEN
		ToolEsc = 0 
	END ELSE
		ToolEsc = 1
		ToolHrs = XLATE('TOOL_ESC',ToolRec<TOOL_ACTIVE_ESCALATION$>,'ELAPSED_HRS','X')
		ToolHrs = OCONV(ToolHrs,'MD2')
	END
	
 	BEGIN CASE
		CASE INDEX(ToolType,'Cleans',1)
		    SortPriority    = 1
*			CTCnt += 1
*			CleansTools<CTCnt,1> = ToolID
*			CleansTools<CTCnt,2> = ToolDesc
*			CleansTools<CTCnt,3> = ToolMode
*			CleansTools<CTCnt,4> = ToolHrs
*			CleansTools<CTCnt,5> = ToolEsc
* 
		CASE ToolType = 'Metrology'
		    SortPriority    = 2
*			MTCnt += 1
*			MetrologyTools<MTCnt,1> = ToolID
*			MetrologyTools<MTCnt,2> = ToolDesc
*			MetrologyTools<MTCnt,3> = ToolMode
*			MetrologyTools<MTCnt,4> = ToolHrs
*			MetrologyTools<MTCnt,5> = ToolEsc
* 
		CASE ToolType = 'Inspection'
		    SortPriority    = 3
*			ITCnt += 1
*			InspectionTools<ITCnt,1> = ToolID
*			InspectionTools<ITCnt,2> = ToolDesc
*			InspectionTools<ITCnt,3> = ToolMode
*			InspectionTools<ITCnt,4> = ToolHrs
*			InspectionTools<ITCnt,5> = ToolEsc
* 
		CASE ToolType = 'Transfer'
		    SortPriority    = 4
*			TRCnt += 1
*			TransferTools<TRCnt,1> = ToolID
*			TransferTools<TRCnt,2> = ToolDesc
*			TransferTools<TRCnt,3> = ToolMode
*			TransferTools<TRCnt,4> = ToolHrs
*			TransferTools<TRCnt,5> = ToolEsc
* 
		CASE ToolType = 'Quartz'
		    SortPriority    = 5
*			QZCnt += 1
*			QuartzTools<QZCnt,1> = ToolID
*			QuartzTools<QZCnt,2> = ToolDesc
*			QuartzTools<QZCnt,3> = ToolMode
*			QuartzTools<QZCnt,4> = ToolHrs
*			QuartzTools<QZCnt,5> = ToolEsc
* 
		CASE 1
		    SortPriority    = 6
*			OtherCnt += 1
*			OtherTools<OtherCnt,1> = ToolID
*			OtherTools<OtherCnt,2> = ToolDesc
*			OtherTools<OtherCnt,3> = ''
*			OtherTools<OtherCnt,4> = ToolHrs
*			OtherTools<OtherCnt,5> = ToolEsc
*			
 	END CASE

    DispTools := ToolID : @VM : ToolDesc : @VM : ToolMode : @VM : ToolHrs : @VM : ToolEsc : @VM : SortPriority : @VM: PMStatus : @FM

Next ToolID

DispTools[-1, 1]    = ''
DispTools           = SRP_Array('SortRows', DispTools, 'AR6' : @FM : 'AL1', 'LIST', @FM, @VM)

* CTColCnt = INT(CTCnt/6)
* IF MOD(CTCnt,6) > 0 THEN CTColCnt += 1
* CTPnlCnt = CTColCnt * 6
* CleansTools := STR(@FM:@VM,CTPnlCnt - CTCnt)
* 
* MTColCnt = INT(MTCnt/6)
* IF MOD(MTCnt,6) > 0 THEN MTColCnt += 1
* MTPnlCnt = MTColCnt * 6
* MetrologyTools := STR(@FM:@VM,MTPnlCnt - MTCnt)
* 
* ITColCnt = INT(ITCnt/6)
* IF MOD(ITCnt,6) > 0 THEN ITColCnt += 1
* ITPnlCnt = ITColCnt * 6
* InspectionTools := STR(@FM:@VM,ITPnlCnt - ITCnt)
* 
* TRColCnt = INT(TRCnt/6)
* IF MOD(TRCnt,6) > 0 THEN TRColCnt += 1
* TRPnlCnt = TRColCnt * 6
* TransferTools := STR(@FM:@VM,TRPnlCnt - TRCnt)
* 
* QZColCnt = INT(QZCnt/6)
* IF MOD(TRCnt,6) > 0 THEN QZColCnt += 1
* QZPnlCnt = QZColCnt * 6
* QuartzTools := STR(@FM:@VM,QZPnlCnt - QZCnt)
* 
* OtherColCnt = INT(OtherCnt/6)
* IF MOD(OtherCnt,6) > 0 THEN OtherColCnt += 1
* OtherPnlCnt = OtherColCnt * 6
* OtherTools := STR(@FM:@VM,OtherPnlCnt - OtherCnt)

* DispTools = ''
* IF CleansTools NE ''		THEN DispTools := CleansTools
* IF MetrologyTools NE '' 	THEN DispTools := @FM:MetrologyTools
* IF InspectionTools NE ''	THEN DispTools := @FM:InspectionTools
* IF TransferTools NE ''		THEN DispTools := @FM:TransferTools
* IF QuartzTools NE ''		THEN DispTools := @FM:QuartzTools
* IF OtherTools NE ''			THEN DispTools := @FM:OtherTools

GreenButton    = '.\BMPS\GreenButton.png'
RedButton      = '.\BMPS\RedButton.png'
YellowButton   = '.\BMPS\YellowButton.png'
GrayButton     = '.\BMPS\GrayButton.png'
BlueButton     = '.\BMPS\BlueButton.png'
QuestionButton = '.\BMPS\help_32.bmp'

DefaultBackColor = Get_Property(@WINDOW,'BACKCOLOR')

NumTools = DCount(DispTools, @FM)

FOR I = 1 TO NumTools

	ToolID     = DispTools<I,1>
	ToolDesc   = DispTools<I,2>
	ToolStatus = DispTools<I,3>
	ToolHrs    = DispTools<I,4>
	ToolEsc    = DispTools<I,5>
	PMStatus   = DispTools<I,7>
	
	IF ToolID NE '' THEN
	    
*	    If ( (ToolID EQ 'BIORAD2') or (ToolID EQ 'BIORAD3') or (ToolID EQ 'BIORAD4') or (ToolID EQ 'BIORAD5') ) then
            Begin Case
                Case PMStatus EQ 'OVERDUE'
                    BackColor = RED$
                Case PMStatus EQ 'CURRENTLY_DUE'
                    BackColor = ORANGE$
                Case PMStatus EQ 'DUE_SOON'
                    BackColor = YELLOW$
                Case PMStatus EQ 'NOT_SCHEDULED'
                    BackColor = DefaultBackColor
                Case PMStatus EQ 'NOT_DUE'
                    BackColor = GREEN$
                Case PMStatus EQ 'ERROR'
                    BackColor = BRED$
            End Case
*        end else
*            BackColor = DefaultBackColor
*        end
	    
		Set_Property(@WINDOW:'.TP':I,'TEXT',ToolDesc)
		Set_Property(@WINDOW:'.TP':I,'@TOOLID',ToolID)
		Set_Property(@WINDOW:'.TT':I,'TEXT',ToolHrs)
		Set_Property(@WINDOW:'.TT':I,'FORECOLOR',FONTBLUE$)
		Set_Property(@WINDOW:'.TB':I,'ENABLED',1)
		
		Set_Property(@WINDOW:'.TP':I, 'BACKCOLOR', BackColor)
		Set_Property(@WINDOW:'.TB':I, 'BACKCOLOR', BackColor)
		
		BEGIN CASE
			CASE ToolStatus = 'PROD'
				Set_Property(@WINDOW:'.TB':I,'BITMAP',GreenButton)
				
			CASE ToolStatus[-3,3] = 'MTC' or ToolStatus = 'QUAL_OVERDUE' or ToolStatus EQ 'VER_FAILED'
				Set_Property(@WINDOW:'.TB':I,'BITMAP',RedButton)
			
			CASE ToolStatus = 'LIM' OR ToolStatus = 'QUAL' OR ToolStatus = 'EWORK'  or ToolStatus EQ 'VER'
				Set_Property(@WINDOW:'.TB':I,'BITMAP',YellowButton)
				
			CASE ToolStatus = 'OUT' OR ToolStatus = 'FACILITIES (UnSched)'
				Set_Property(@WINDOW:'.TB':I,'BITMAP',GrayButton)
			CASE ToolStatus = 'RTU'
				Set_Property(@WINDOW:'.TB':I,'BITMAP',BlueButton)	
			CASE 1
				Set_Property(@WINDOW:'.TB':I,'BITMAP',QuestionButton)
			
		END CASE
		
	END ELSE
		Set_Property(@WINDOW:'.TP':I,'TEXT','')
		Set_Property(@WINDOW:'.TT':I,'TEXT','')
		Set_Property(@WINDOW:'.TB':I,'ENABLED',0)
		Set_Property(@WINDOW:'.TB':I,'BITMAP','')
	END
NEXT I

* Set_Property(@WINDOW, 'VISIBLE', 1)

RETURN


* * * * * * *
TBClick:
* * * * * * *

CtrlEntID = Get_Property(@WINDOW,'FOCUS')
CtrlName = FIELD(CtrlEntID,'.',2)

CONVERT 'B' TO 'P' In CtrlEntID								;* Buttons are named 'TB':n
ToolID = Get_Property(CtrlEntID,'@TOOLID')					;* Properties are on panels named 'TP':n

* Check and lock

Open "TOOL" to hTable then
	DialogWin = "DIALOG_TOOL_MODE"
	DialogKey = DialogWin:"*":ToolID
	
	Lock hTable, DialogKey then
		ModeChangeParms = Dialog_Box("DIALOG_TOOL_MODE", @WINDOW, ToolID)
		Unlock hTable, DialogKey
	end else
		Mtext = "Another user is currently updating this tool's status."
		Msg(@Window, "", "ERROR", "", "Update Tool Status" :@FM: Mtext)
		ModeChangeParms = "Cancel"
	end
end

* Cancel returned from dialog box

IF ModeChangeParms = 'Cancel' THEN RETURN

UserID	= ModeChangeParms[1,@FM]
Reason	= ModeChangeParms[COL2()+1,@FM]
NewMode = ModeChangeParms[COL2()+1,@FM]

* Two users trying to update at same time leaves more than one current mode.  Take the first one.

CurrModeDTM = XLATE('TOOL', ToolID, TOOL_CURR_MODE_KEY$, 'X')<1,1>	
CurrModeDTM = FIELD(CurrModeDTM,'*',2)
CurrModeDTM = OCONV(CurrModeDTM,'DT4/^S')

Parms	 = ToolID:@RM
Parms	:= CurrModeDTM:@RM
Parms	:= NewMode:@RM
Parms	:= Reason
	
//obj_Tool_Log('SetMode', Parms)
Tool_Services('ChangeToolMode', ToolID, NewMode, Reason, @User4, 0)
* IF Get_Status(errCode) THEN
* 	Errmsg(errCode)
* 	RETURN
* END
If Error_Services('HasError') then
    Error_Services('DisplayError')
end


GoSub Refresh

RETURN














