COMPILE FUNCTION obj_NCR(Method,Parms)
#pragma precomp SRP_PreCompiler

/*
	Methods for the Non Conforming Report (NCR) table
    
	03/29/2006 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Create(RDSNo,Reactor,LossStage,CassNos,SlotNos,PocketNos)					;* Create new NCR Record
    GetProperty(NCRNos,PropName,LossStage,LossBy)	;
    
    Notes:
        12/03/2020 - DJS - Modified RejQty subroutine to count previously NCR'd wafers.
   
*/


DECLARE FUNCTION	Get_Status, Msg, Utility, obj_Tables, Dialog_Box,NextKey, Popup, Get_Property, obj_RDS
DECLARE FUNCTION	obj_WO_Mat, obj_WO_Mat_Log, Database_Services, Datetime, Environment_Services, Logging_Services

DECLARE SUBROUTINE	Set_Status, Msg, obj_Tables, Send_Dyn, Btree.Extract, ErrMsg, Send_Dyn, RList, obj_WO_Log, Send_Event, obj_RDS
DECLARE SUBROUTINE	obj_WO_Mat, obj_WO_Mat_Log, Database_Services, Logging_Services

$INSERT MSG_EQUATES
$INSERT NCR_EQUATES
$INSERT RDS_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT Popup_Equates
$INSERT DICT_EQUATES
$INSERT LOGICAL
$INSERT WO_WFR_EQUATES

EQU TARGET_ACTIVELIST$ TO 5

EQU MPRE$	TO 1			;* Used to return Sorted StageBy from obj_NCR
EQU MPOST$	TO 2
EQU CPRE$	TO 3
EQU CPOST$	TO 4

EQU COL$LOG_FILE		TO 1		;* obj_WO_Mat_Log method parameter equates
EQU COL$LOG_DTM 		TO 2
EQU COL$WO_MAT_ACTION	TO 3
EQU COL$WH_CD			TO 4
EQU COL$LOC_CD			TO 5
EQU COL$WO_NOS			TO 6
EQU COL$CASS_NOS		TO 7
EQU COL$USER_ID			TO 8
EQU COL$TAGS			TO 9
EQU COL$TOOL_ID			TO 10

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\WO_Mat'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' NCR Log.csv'
Headers     = 'Logging DTM' : @FM : 'User' : @FM : 'WOMatKeyID' : @FM : 'Notes'
objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)
LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

ErrTitle = 'Error in Stored Procedure "obj_NCR"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'Create'			; GOSUB Create
	CASE Method = 'CreateWfr'		; GOSUB CreateWfr
	CASE Method = 'StageBy'			; GOSUB StageBy
	CASE Method = 'RejQty'			; GOSUB RejQty
	CASE Method = 'RDSRejQty'		; GOSUB RDSRejQty
	CASE Method = 'TotRejCost'		; GOSUB TotRejCost
	
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to routine.'

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

RETURN Result


* * * * * * *
CreateWfr:
* * * * * * *

	* Create method updated for wafer traceability modifications

	WONo			= Parms[1,@RM]
	WOStep			= Parms[COL2()+1,@RM]
	WOMatCassNo		= Parms[COL2()+1,@RM]
	RDSNo			= Parms[COL2()+1,@RM]
	Reactor			= Parms[COL2()+1,@RM]
	LossStage		= Parms[COL2()+1,@RM]
	WfrIDs			= Parms[COL2()+1,@RM]
	CarrSlotIDs		= Parms[COL2()+1,@RM]
	CassSlotIDs		= Parms[COL2()+1,@RM]
	RPZs			= Parms[COL2()+1,@RM]

	NCRId = NextKey('NCR')

	NCRRec = ''
	NCRRec<NCR_WO_NO$>				= WONo
	NCRrec<NCR_WO_STEP$>			= WOStep
	NCRRec<NCR_WO_MAT_CASS_NO$>		= WOMatCassNo
	NCRRec<NCR_RDS_ID$>				= RDSNo
	NCRRec<NCR_REACTOR$>			= Reactor
	NCRRec<NCR_LOSS_STAGE$> 		= LossStage
	NCRRec<NCR_ENTRY_ID$>			= @USER4
	NCRRec<NCR_ENTRY_DATE$>			= DATE()
	NCRRec<NCR_ENTRY_TIME$>			= TIME()
	NCRRec<NCR_WFR_ID$>				= WfrIDs				;* Wafer IDs of reject material
	NCRRec<NCR_CASS_SLOT_ID$>		= CassSlotIDs			;* Reject from Cassette Slot ID
	NCRRec<NCR_CARR_SLOT_ID$>		= CarrSlotIDs			;* Reject from GaN Carrier
	NCRRec<NCR_SUSC_RPZ$>			= RPZs					;* Reject from Susceptor Pocket
	NCRRec<NCR_STATUS$>				= 'O'		;* Open

	OtParms = 'NCR':@RM:NCRId:@RM:@RM:NCRRec
	obj_Tables('WriteRec',OtParms)
	
	ReactType = Xlate('WO_LOG', WONo, 'REACT_TYPE', 'X')
	If ReactType EQ 'GAN' then
	    // Update wafer trace records so that reject quantities update
	    For each WfrID in WfrIDs using @VM
	        WOWfrRec = Database_Services('ReadDataRow', 'WO_WFR', WfrID)
	        WOWfrRec<WO_WFR_LOC_DTM$, -1>   = Datetime()
            WOWfrRec<WO_WFR_LOC_BY$, -1>    = @User4
            WOWfrRec<WO_WFR_LOC_EVENT$, -1> = 'NCR'
            Database_Services('WriteDataRow', 'WO_WFR', WfrID, WOWfrRec, True$, False$, True$)
	    Next WfrID
	end

	Result = NCRId

RETURN


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

	WONo		    = Parms[1,@RM]
	WOStep		    = Parms[COL2()+1,@RM]
	WOMatCassNo	    = Parms[COL2()+1,@RM]
	RDSNo		    = Parms[COL2()+1,@RM]
	Reactor		    = Parms[COL2()+1,@RM]
	LossStage	    = Parms[COL2()+1,@RM]
	CassNos		    = Parms[COL2()+1,@RM]
	SlotNos		    = Parms[COL2()+1,@RM]
	PocketNos	    = Parms[COL2()+1,@RM]
	Zones		    = Parms[COL2()+1,@RM]
	OutCassNos	    = Parms[COL2()+1,@RM]
	OutSlotNos	    = Parms[COL2()+1,@RM]
	WMRdsNos	    = Parms[COL2()+1,@RM]
	RejWaferIDs	    = Parms[COL2()+1,@RM]
	PrevNCRNos	    = Parms[COL2()+1,@RM]
	MUWONos		    = Parms[COL2()+1,@RM]
	MUWOSteps	    = Parms[COL2()+1,@RM]
	MUCassIDs	    = Parms[COL2()+1,@RM]
	MUSlotNos	    = Parms[COL2()+1,@RM]
    // New Code - djs 2/19/19 -----------
    NCRStatus       = ''
    LossBy          = Parms[COL2()+1,@RM]
    ContainActions  = Parms[COL2()+1,@RM]
    LossComments    = Parms[COL2()+1,@RM]
    ACComments      = Parms[COL2()+1,@RM]
    DeptResp        = Parms[COL2()+1,@RM]
    ACCode          = Parms[COL2()+1,@RM]
    LossCode        = Parms[COL2()+1,@RM]
    NCRStatus       = Parms[COL2()+1,@RM]
    Shift           = Parms[COL2()+1,@RM]
    AuthShipSig     = Parms[COL2()+1,@RM]
    AuthRejSig      = Parms[COL2()+1,@RM]
    SPCDateTime     = Parms[COL2()+1,@RM]
    AuthShipSigDTM  = Parms[COL2()+1,@RM]
    DeptHandled     = Parms[COL2()+1,@RM]
    // ----------------------------------

	IF (WONo = '') THEN
		ErrorMsg = 'Null parameter "WONo" passed to routine. (':Method:')'
	END

	NCRId = NextKey('NCR')

	NCRRec = ''
	NCRRec<NCR_WO_NO$>			    = WONo
	NCRrec<NCR_WO_STEP$>		    = WOStep
	NCRRec<NCR_WO_MAT_CASS_NO$>	    = WOMatCassNo
	NCRRec<NCR_RDS_ID$>			    = RDSNo
	NCRRec<NCR_REACTOR$>		    = Reactor
	NCRRec<NCR_LOSS_STAGE$> 	    = LossStage
	NCRRec<NCR_ENTRY_ID$>		    = @USER4
	NCRRec<NCR_ENTRY_DATE$>		    = DATE()
	NCRRec<NCR_ENTRY_TIME$>		    = TIME()
	If NCRStatus EQ '' then
	    NCRRec<NCR_STATUS$>		    = 'O'		;* Open
	end else
	    NCRRec<NCR_STATUS$>         = NCRStatus
	end
	NCRRec<NCR_CASS_NO$>		    = CassNos
	NCRRec<NCR_SLOT_NO$>		    = SlotNos
	NCRRec<NCR_POCKET_NO$>		    = PocketNos
	NCRRec<NCR_ZONE$>			    = Zones
	NCRRec<NCR_OUT_CASS_NO$>	    = OutCassNos
	NCRRec<NCR_OUT_SLOT_NO$>	    = OutSlotNos
	NCRRec<NCR_WM_RDS_NO$>		    = WMRdsNos
	NCRRec<NCR_REJ_WAFER_ID$>	    = RejWaferIDs
	NCRRec<NCR_PREV_NCR_NO$>	    = PrevNcrNos
	NCRRec<NCR_MU_WO_NO$>		    = MUWONos
	NCRRec<NCR_MU_STEP$>		    = MUWOSteps
	NCRRec<NCR_MU_CASS$>		    = MUCassIDs
	NCRRec<NCR_MU_SLOT$>		    = MUSlotNos
	NCRRec<NCR_TREJ_CNT$>	        = COUNT(CassNos,@VM) + (CassNos NE '')
	// New Code - djs 2/19/19 -----------
    NCRRec<NCR_LOSS_BY$>            = LossBy
    NCRRec<NCR_CONTAIN_ACTIONS$>    = ContainActions
    NCRRec<NCR_LOSS_COMMENTS$>      = LossComments
    NCRRec<NCR_AC_COMMENTS$>        = ACComments
    NCRRec<NCR_DEPT_RESP$>          = DeptResp
    NCRRec<NCR_AC_CODE$>            = ACCode
    NCRRec<NCR_LOSS_CODE$>          = LossCode
    NCRRec<NCR_SHIFT$>              = Shift
    NCRRec<NCR_AUTH_SHIP_SIG$>      = AuthShipSig
    NCRRec<NCR_AUTH_REJ_SIG$>       = AuthRejSig
    NCRRec<NCR_SPC_DTM$>            = SPCDateTime
    NCRRec<NCR_AUTH_SHIP_SIG_DTM$>  = AuthShipSigDTM
    NCRRec<NCR_DEPT$>               = DeptHandled
    // ----------------------------------

	IF RDSNo = '' AND WMRdsNos NE '' THEN
        // EpiPro
		RDSNos		= ''
		ReactorNos	= ''
		RejCnts		= ''
		
		FOR I = 1 TO COUNT(WMRDSNos,@VM) + (WMRdsNos NE '')
			WMRdsNo = WMRdsNos<1,I>
			LOCATE WMRdsNo IN RdsNos USING @VM SETTING Pos THEN
				RejCnts<1,Pos> = RejCnts<1,Pos> + 1
			END ELSE
				RdsNos		= INSERT(RdsNos,1,Pos,0,WMRdsNo)
				ReactorNos	= INSERT(ReactorNos,1,Pos,0,XLATE('RDS',WMRdsNo,RDS_REACTOR$,'X'))
				RejCnts		= INSERT(RejCnts,1,Pos,0,1)
			END
			RDSRec                             = Database_Services('ReadDataRow', 'RDS', WMRdsNo)
			PocketNo                           = PocketNos<0, I>
			RDSRec<RDS_OUT_NCR$, PocketNo>     = NCRId
*			RDSRec<RDS_OUT_CASS_NO$, PocketNo> = ''
*			RDSRec<RDS_OUT_SLOT_NO$, PocketNo> = ''
			Database_Services('WriteDataRow', 'RDS', WMRdsNo, RDSRec, True$, False$, True$)
		NEXT I
		
		NCRRec<NCR_RDS_ID$>		= RdsNos
		NCRRec<NCR_REACTOR$>	= ReactorNos
		NCRRec<NCR_TREJ_CNT$>	= RejCnts
	end else
	    // Non-EpiPro
	    WOMatKey = WONo:'*':WOMatCassNo
        WOMatRec = Database_Services('ReadDataRow', 'WO_MAT', WOMatKey)
        For each SlotNo in SlotNos using @VM
            WOMatRec<WO_MAT_SLOT_NCR$, SlotNo>          = NCRId
            WOMatRec<WO_MAT_SLOT_REP_WAFER_ID$, SlotNo> = ''
            WOMatRec<WO_MAT_SLOT_REP_BY$, SlotNo>       = ''       
        Next SlotNo
        Database_Services('WriteDataRow', 'WO_MAT', WOMatKey, WOMatRec, True$, False$, True$)
        LogData    = ''
        LogData<1> = LoggingDTM
        LogData<2> = WOMatKey
        LogData<3> = WOMatRec<WO_MAT_SLOT_NCR$>
        Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
	end

	OtParms = 'NCR':@RM:NCRId:@RM:@RM:NCRRec
	obj_Tables('WriteRec',OtParms)   
	Result                     = NCRId

RETURN


* * * * * * *
StageBy:
* * * * * * *

* Returns passed in NCR NOs grouped by 'StageBy' (StageBy = LOSS_BY:LOSS_STAGE)  OK, they're backwards - deal with it....

NCRNos = Parms[1,@RM]

IF NCRNos = '' THEN RETURN

FOR I = 1 TO COUNT(NCRNos,@VM) + (NCRNos NE '')
	NCRNo = NCRNos<1,I>
	NCRRec = XLATE('NCR',NCRNo,'','X')
	StageBy = NCRRec<NCR_LOSS_BY$>:NCRRec<NCR_LOSS_STAGE$>

	BEGIN CASE
		CASE StageBy = 'MPRE' 	;	Result<MPRE$,-1>	= NCRNo			;* These equates are in NCR_EQU
		CASE StageBy = 'MPOST' ;	Result<MPOST$,-1>	= NCRNo
		CASE StageBy = 'CPRE' 	;	Result<CPRE$,-1>	= NCRNo
		CASE StageBy = 'CPOST' ;	Result<CPOST$,-1>	= NCRNo
	END CASE
	
NEXT I

RETURN


* * * * * * *
RejQty:
* * * * * * *

IF NOT(ASSIGNED(NCRNo)) THEN
	NCRNo 	= Parms[1,@RM]
	NCRRec	= Parms[COL2()+1,@RM]
END ELSE
	NCRRec = ''
END

IF NCRNo = '' THEN RETURN

IF NCRRec = '' THEN NCRRec = XLATE('NCR',NCRNo,'','X')

IF NCRRec = '' THEN RETURN

WMRDSNos	= NCRRec<NCR_WM_RDS_NO$>
SlotNos		= NCRRec<NCR_SLOT_NO$>
RDSNos		= NCRRec<NCR_RDS_ID$>
RejWaferIDs	= NCRRec<NCR_REJ_WAFER_ID$>
PrevNCRNos  = NCRRec<NCR_PREV_NCR_NO$>

RejQtys = ''

FOR I = 1 TO COUNT(SlotNos,@VM) + (SlotNos NE '')
			
	IF WMRDSNos<1,I> = '' THEN
	    // Non-EpiPro
	    PrevNCRNo       = PrevNCRNos<0, I>
*	 	IF ( (SlotNos<1,I> > 0) and (PrevNCRNo EQ '') ) THEN RejQtys += 1
	 	IF (SlotNos<1,I> > 0) THEN RejQtys += 1
	END ELSE
	    // EpiPro
		WMRdsNo = WMRDSNos<1,I>
		LOCATE WMRdsNo IN RDSNos BY 'AR' USING @VM SETTING RPos THEN
			 RejQtys<1,RPos> = RejQtys<1,RPos> + 1
		END
	END
NEXT I

IF RejQtys = '' THEN
	IF NCRRec<NCR_TREJ_CNT$> NE '' THEN
		RejQtys = NCRRec<NCR_TREJ_CNT$>
	END
END

* * * * * Added 7/7/2016 JCH * * * *   

* Wafer Traceability Added *

IF RejQtys = '' THEN
	RejQtys = COUNT(NCRRec<NCR_WFR_ID$>,@VM) + (NCRRec<NCR_WFR_ID$> NE '')
END

Result = RejQtys

RETURN


* * * * * * *
RDSRejQty:
* * * * * * *

NCRNos	= Parms[1,@RM]
RDSNo	= Parms[COL2()+1,@RM]

IF NCRNos = '' THEN RETURN
IF RDSNo = '' THEN RETURN


RDSRejQtys = ''
FOR J = 1 TO COUNT(NCRNos,@VM) + (NCRNos NE '')
	NCRNo = NCRNos<1,J>
	GOSUB RejQty

	RejQtys = Result

	LOCATE RDSNo IN NCRRec<NCR_RDS_ID$> USING @VM SETTING Pos THEN
		RDSRejQtys<1,J> = RejQtys<1,POS>
	END
NEXT I
Result = RDSRejQtys

RETURN

	
	
* * * * * * *
TotRejCost:		
* * * * * * *

NCRNo 	= Parms[1,@RM]
NCRRec	= Parms[COL2()+1,@RM]
	
IF NCRNo	= '' THEN RETURN
IF NCRRec	= '' THEN NCRRec = XLATE('NCR',NCRNo,'','X')
IF NCRRec	= '' THEN RETURN

WONo		= NCRRec<NCR_WO_NO$>
WOMatCassNo	= NCRRec<NCR_WO_MAT_CASS_NO$>

IF WOMatCassNo = '' THEN
	CassNos = NCRRec<NCR_CASS_NO$>
	CassCnt = COUNT(CassNos,@VM) + (CassNos NE '')
	FOR I = 1 TO CassCnt
		CassNo = CassNos<1,I>
		IF CassNo NE '' THEN
			Result += XLATE('WO_MAT',WONo:'*':CassNo,'UNIT_PRICE','X')
		END
		
	NEXT I
END ELSE
	UnitCost = XLATE('WO_MAT',WONo:'*':WOMatCassNo,'UNIT_PRICE','X')
	Slots = NCRRec<NCR_SLOT_NO$>
	SlotCnt = COUNT(Slots,@VM) + (Slots NE '')
	FOR I = 1 TO SlotCnt
		Result += UnitCost
	NEXT I
END

RETURN Result



