Function NDW_RDS_Supplement_Events(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   DBW_RDS_Supplement_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert

    History     :   (Date, Initials, Notes)
        05/23/18    djs     Created initial commuter module.
        06/13/24	djm		Modify for new stage-specific supplement system.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler
#Window NDW_RDS_SUPPLEMENT

$insert APP_INSERTS
$insert EVENT_SETUP
$insert WO_MAT_EQUATES
$insert MSG_EQUATES
$Insert SUPPLEMENTS_EQUATES

Declare subroutine  SRP_EditTable_Manager, SRP_Show_Window, Rds_Services, Supplement_Services, RList, SRP_Stopwatch
Declare function    SRP_EditTable_Manager, Database_Services, Material_Services, Rds_Services, RGB, Supplement_Services

CRLF = \0D0A\ 

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
	Transfer Event to OIEvent
	Transfer Param1 to Event
	Transfer Param2 to Param1
	Transfer Param3 to Param2
	*    Transfer Param4 to Param3
	*    Transfer Param5 to Param4
	*    Transfer Param6 to Param5
	*    Transfer Param7 to Param6
	*    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Event WINDOW.CREATE(CreateParam)
	
	RDSColumns        = ''
	ThisWorkOrderNo   = CreateParam
	RDSColumns<0, 1>  = 'ABBREV_OR_CO_NAME'
	RDSColumns<0, 2>  = 'WO'
	RDSColumns<0, 3>  = 'WO_STEP'
	RDSColumns<0, 4>  = 'RUN_ORDER_NUM'
	RDSColumns<0, 5>  = 'SEQ'
	RDSColumns<0, 6>  = 'REACTOR'
	RDSColumns<0, 7>  = 'REACT_IDLE_TIME'
	RDSColumns<0, 8>  = 'HGCV_TEST'
	RDSColumns<0, 9>  = 'DATE_IN'
	RDSColumns<0, 10> = 'TIME_IN'
	RDSColumns<0, 11> = 'DATE_OUT'
	RDSColumns<0, 12> = 'TIME_OUT'
	RDSColumns<0, 13> = 'PART_NUM'
	RDSColumns<0, 14> = 'LOT_NUM'
	RDSColumns<0, 15> = 'SUPPL_INST'
	RDSList           = RDS_Services('GetRDSData', ThisWorkOrderNo, RDSColumns, True$, '')
	RDSRptCtrl        = @Window : '.OLE_RPT_RDS_LIST'
	
	// Turn off AutoPopulate for performance reasons
	Set_Property(@Window, RDSRptCtrl, 'OLE.AutoPopulate', False$)
	Set_Property(@Window:RDSRptCtrl, "OLE.PreviewMode", 1)
	GoSub Setup_OLE_Controls
	SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
	
	// Turn AutoPopulate back on
	Set_Property(@Window, RDSRptCtrl, 'OLE.AutoPopulate', True$)
	
end event


Event PUB_CLOSE.CLICK()
	
	End_Dialog(@Window, '')
	
end event


Event PUB_ADD.CLICK()
	
	OrigSelPos = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos')
	SelList    = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelList')
	If SelList NE '' then
		RDSList   = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')
		OrigList  = RDSList
		CurrSuppl = ''
		
		// Prompt user for supplement information 
		Response     = Dialog_Box('NDW_ADD_SUPPLEMENT', @WINDOW, SelList)
		Save         = Response<1>
		Instructions = Response<2>
		
		If Save EQ True$ then
			Stage = Field(Instructions, ':', 1)
			Text  = Field(Instructions, ':', 2)
			// Insert Instructions into selected rows' Supplement cell
			For each Row in SelList using @FM
				Locate Row in RDSList Using @FM Setting Pos then
					DupeCheck = False$
					CurrSupps = RDSList<Pos, 15>
					Swap CRLF with @FM in CurrSupps
					For each Supp in CurrSupps using @FM
						CurrSuppStage = Field(Supp, ': ', 1)
						DupeCheck = (CurrSuppStage EQ Stage)
					Until DupeCheck
					Next Supp
					
					If DupeCheck EQ False$ then
						If RDSList<Pos,15> = '' then
							RDSList<Pos,15> = Instructions
						end else
							RDSList<Pos,15> = RDSList<Pos,15> : CRLF : Instructions
						end
					end 
				end
			Next Row
			If RDSList NE OrigList then
				Gosub SaveSupps
				Gosub GetExistingSupps
			end
		end
		GoSub RefreshRowColors
	End
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', OrigSelPos)
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'FOCUS', True$)
	Gosub EnableButtons
	
end event


Event PUB_REMOVE.CLICK()
	
	OrigSelPos = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos')
	SelList    = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelList')
	If SelList NE '' then
		RDSList     = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')
		
		// Prompt user for supplement information 
		Response     = Dialog_Box('NDW_REMOVE_SUPPLEMENTS', @WINDOW, SelList)
		
		Save         = Response
		Gosub GetExistingSupps
		
		GoSub RefreshRowColors
	End
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', OrigSelPos)
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'FOCUS', True$)
	Gosub EnableButtons
	
end event


Event PUB_SELECT_ALL.CLICK()
	
	RDSList = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')
	NumRows = DCount(RDSList, @FM)
	SelPos  = ''
	
	For Index = 1 to NumRows
		If Index LT NumRows then
			SelPos := Index:@FM
		end else
			SelPos := Index
		end
	Next Index
	
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.Focus', True$)
	GoSub EnableButtons
	
end event


Event PUB_CLEAR.CLICK()
	
	// Clear any selected rows
	SelPos = ''
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)
	GoSub EnableButtons
	
end event


Event OLE_SUBCLASS.OnComboClick(CtrlId, Sel, Value)
	
	Send_Event(CtrlId, 'LOSTFOCUS')
	SelPos = ''
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)
	
end event


Event OLE_RPT_RDS_LIST.OnSelChange(CtrlId, Sel, Value)
	
	GoSub EnableButtons
	
end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Setup_OLE_Controls:
	
	Qualify     = ''
	Qualify<1>  = 1
	Qualify<4>  = 0
	
	RDSRptCtrl          = @Window : '.OLE_RPT_RDS_LIST'
	RDSPicCtrl          = @Window : '.OLE_PIC_RDS_LIST_DIV'
	
	Send_Message(RDSRptCtrl,  'QUALIFY_EVENT', 'OLE.OnSelChange', Qualify)
	
	ColumnList     = ''
	ColumnList<1>  = 'Customer' : @VM : 'TEXT' : @VM : 150 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Left' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
	ColumnList<2>  = 'WO No' : @VM : 'NUMBER' : @VM : 50 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Center' : @VM : @VM : '#######' : @VM : @VM : 'Segoe UI,9'
	ColumnList<3>  = 'Step' : @VM : 'NUMBER' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 30 : @VM : 'Center' : @VM : @VM : '##': @VM : @VM : 'Segoe UI,9'
	ColumnList<4>  = 'Run' : @VM : 'NUMBER' : @VM : 30 : @VM : @VM : @VM : @VM : @VM : @VM : 30 : @VM : 'Center' : @VM : @VM : '###' : @VM : @VM : 'Segoe UI,9'
	ColumnList<5>  = 'RDS' : @VM : 'NUMBER' : @VM : 50 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Center' : @VM : @VM : '#######' : @VM : @VM : 'Segoe UI,9'
	ColumnList<6>  = 'Rx' : @VM : 'NUMBER' : @VM : 50 : @VM : @VM : @VM : @VM : @VM : @VM : 25 : @VM : 'Center' : @VM : @VM :'##': @VM : @VM : 'Segoe UI,9'
	ColumnList<7>  = 'Idle Hrs' : @VM : 'TIME' : @VM : 75 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Center' : @VM : @VM : 'hh:mm' : @VM : @VM : 'Segoe UI,9'
	ColumnList<8>  = 'HgCv' : @VM : 'BOOL' : @VM : 55 : @VM : @VM : @VM : @VM : @VM : @VM : 40 : @VM : 'Center' : @VM : @VM : 'Yes' : @VM : @VM : 'Segoe UI,9'
	ColumnList<9>  = 'Date In' : @VM : 'DATE' : @VM : 60 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'MM/DD/YY' : @VM : @VM : 'Segoe UI,9'
	ColumnList<10> = 'Time In' : @VM : 'TIME' : @VM : 60 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'hh:mmAA' : @VM : @VM : 'Segoe UI,9'
	ColumnList<11> = 'Date Out' : @VM : 'DATE' : @VM : 60 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'MM/DD/YY': @VM : @VM : 'Segoe UI,9'
	ColumnList<12> = 'Time Out' : @VM : 'TIME' : @VM : 60 : @VM : @VM : @VM : @VM : @VM : @VM : 60 : @VM : 'Center' : @VM : @VM : 'hh:mmAA': @VM : @VM : 'Segoe UI,9'
	ColumnList<13> = 'Part No' : @VM : 'TEXT' : @VM : 50 : @VM : @VM : @VM : @VM : @VM : @VM : 50 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
	ColumnList<14> = 'Lot No' : @VM : 'TEXT' : @VM : 100 : @VM : @VM : @VM : @VM : @VM : @VM : 75 : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
	ColumnList<15> = 'Supplement' : @VM : 'P' : @VM : 300 : @VM : @VM : @VM : @VM : @VM : @VM : 150 : @VM : 'Left' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
	
	Set_Property(RDSRptCtrl, 'OLE.ColumnList', ColumnList)
	Set_Property(RDSRptCtrl, 'OLE.MultiSelect', 2)  ; // 2 = Multiselect Toggle Mode
	
	// Get the design time form size. This will be adjusted based on the number of RDS records.
	FormSize            = Get_Property(@Window, 'SIZE')
	RDSRptCtrlSize      = Get_Property(RDSRptCtrl, 'SIZE')
	NumRDS              = DCount(RDSList, @FM)
	
	// Adjust the form and ReportTable control size based on the number of RDS records in the work order.
	AdditionalRowsAdj   = 0
	AdditionalRowsAdj += (NumRDS - 1) * 20
	
	// Ensure window and controls are sized within client machine's max resolution.
	ProposedNewHeight = FormSize<4> + AdditionalRowsAdj
	SystemWindowSize  = Get_Property('SYSTEM', 'SIZE')
	MaxClientHeight   = SystemWindowSize<4>
	If ProposedNewHeight > MaxClientHeight then
		FormSize<4> = Oconv(MaxClientHeight * '0.9', 'MD0')
	end else
		FormSize<4> = FormSize<4> + AdditionalRowsAdj
	end
	MaxClientWidth    = SystemWindowSize<3>
	If FormSize<3> > MaxClientWidth then
		FormSize<3> = Oconv(MaxClientWidth * '0.9', 'MD0')
	end
	Set_Property(@Window, '@ORIGSIZE', FormSize)      
	// Pull then save original list after formatting for savewarn purposes
	Gosub GetExistingSupps
	Set_Property(RDSRptCtrl, 'OLE.List', RDSList)
	RDSList  = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')
	Set_Property(@Window, '@OrigList', RDSList)
	
	GoSub RefreshRowColors
	
return


GetExistingSupps:
	
	NumRDS        = DCount(RDSList, @FM)
	SuppsWithLots = Supplement_Services('GetLotsWithSupplements', 'RDS')
	For I = 1 to NumRDS
		RDSSupps  = ''
		ThisRDS   = RDSList<I, 5>
		If Count(SuppsWithLots, ThisRDS) NE 0 then
			SuppCheck = Supplement_Services('GetSupplementsForLot', 'RDS', ThisRDS)
			If SuppCheck NE '' AND SuppCheck NE False$ then
				SuppCount = DCount(SuppCheck, @FM)
				For J = 1 to SuppCount
					CurrSuppStage = Xlate('SUPPLEMENTS', SuppCheck<J>, SUPPLEMENTS_STAGE$, 'X', '')
					CurrSuppText  = Xlate('SUPPLEMENTS', SuppCheck<J>, SUPPLEMENTS_SUPPL_TEXT$, 'X', '')
					If RDSSupps EQ '' then
						RDSSupps = CurrSuppStage: ': ' : CurrSuppText
					End else
						RDSSupps := CRLF : CurrSuppStage: ': ' : CurrSuppText
					end
				Next J
			end
			If RDSSupps NE '' then RDSList<I,15> = RDSSupps
		end else
			RDSList<I,15> = ''
		end
	Next I
	
return


RefreshRowColors:
	
	RDSRptCtrl  = @Window : '.OLE_RPT_RDS_LIST'
	NumRDS      = DCount(RDSList, @FM)
	
	// Fill report table with RDS data
	Set_Property(RDSRptCtrl, 'OLE.List', RDSList)
	
	// Color every other cell to increase row distinguishability
	Colors = ''
	If NumRDS GT 1 then
		For RowNo = 1 To NumRDS
			Begin Case
				Case RDSList<RowNo, 15> NE ''
					Colors<RowNo> = RGB(255, 255, 255)
					
				Case Mod(RowNo, 2) EQ 1
					Colors<RowNo> = RGB(231, 243, 254)
					
				Case Mod(RowNo, 2) EQ 0
					Colors<RowNo> = RGB(196, 222, 247)
			End Case
		Next RowNo
		Set_Property(RDSRptCtrl, 'OLE.RecordColors', Colors)
	end
	
	// Clear any selected rows
	SelPos = ''
	Set_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.SelPos', SelPos)
	
return


SaveSupps:

	Instructions  = ''
	RDSNotifyList = ''
	ChangeCount   = Dcount(OrigSelPos, @FM)
	For I = 1 to ChangeCount
		CurrRowNum = OrigSelPos<I>
		CurrRow    = RDSList<CurrRowNum>
		LotSupps   = CurrRow<1,15>
		Swap CRLF with @FM in LotSupps
		SuppCount  = Dcount(LotSupps, @FM)
		For J = 1 to SuppCount
			ThisLot           = CurrRow<1,5>
			RDSNotifyList<-1> = ThisLot
			ThisStage         = Field(LotSupps<J>, ':', 1)
			ThisSupp          = Field(LotSupps<J>, ': ', 2)
			ThisSupp          = TrimF(ThisSupp)
			ret               = Supplement_Services('CreateSupplement', 'RDS', ThisLot, ThisStage, ThisSupp, @User4)
			Instructions      = ThisSupp
		Next J
	Next I
	
	If RDSNotifyList NE '' then Supplement_Services('SendNotifications', RDSNotifyList, 'Created', Instructions, @User4)	
	
return


EnableButtons:
	
	RDSList = Get_Property(@Window : '.OLE_RPT_RDS_LIST', 'OLE.List')
	SelPos  = Get_Property(@Window:".OLE_RPT_RDS_LIST", "OLE.SelPos")
	If SelPos NE '' then
		Set_Property(@Window : '.PUB_ADD', 'ENABLED', True$)
		LastSel = SelPos<1>
		If RDSList<LastSel, 15> NE '' then
			Set_Property(@Window : '.PUB_REMOVE', 'ENABLED', True$)
		end else
			Set_Property(@Window : '.PUB_REMOVE', 'ENABLED', False$)
		end
	End else
		Set_Property(@Window : '.PUB_ADD', 'ENABLED', False$)
		Set_Property(@Window : '.PUB_REMOVE', 'ENABLED', False$)
	end
	
return

