COMPILE FUNCTION SAP_MONITOR(Method,Parms)
/*
	Methods for the SAP_MFS
    
	12/16/2010 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    Write(TableName,ID,NewRec,OrgRec)				;* New or changed records
    Delete(TableName,ID,NewRec,OrgRec)				;* Record delete
   
*/
#pragma precomp SRP_PreCompiler

DECLARE SUBROUTINE obj_SAP, ErrMsg, obj_Post_Log, obj_Tables, Logging_Services, Database_Services, SAP_Services
DECLARE FUNCTION obj_NCR, obj_WM_Out, obj_WO_Mat, Logging_Services, Environment_Services, RetStack, NextKey
DECLARE FUNCTION GaN_Services, Database_Services, Datetime

$INSERT WO_MAT_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT NCR_EQUATES
$INSERT TW_USE_EQUATES
$INSERT RDS_TEST_EQUATES
$INSERT REACT_RUN_EQUATES
$INSERT WO_LOG_EQUATES
$INSERT PROD_VER_EQUATES
$INSERT SAP_MU_TRANS_EQUATES
$INSERT SEMI_VEND_CODE_EQUATES
$INSERT COMPANY_EQUATES

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

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

IF ErrorMsg NE '' THEN
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'Write'			; GOSUB Write
	CASE Method = 'Delete'			; GOSUB Delete
	
	CASE 1
		ErrMsg('Unknown Method ':QUOTE(Method):' passed to routine.')

END CASE

RETURN


* * * * * * *
Write:
* * * * * * * 

TableName	= Parms[1,@RM]
RecKey		= Parms[COL2()+1,@RM]
NewRec		= Parms[COL2()+1,@RM]
OrgRec		= Parms[COL2()+1,@RM]

IF TableName = '' THEN RETURN
IF RecKey = '' THEN RETURN

BEGIN CASE

	CASE TableName = 'TW_USE'
		
		MetNo = RecKey[1,'*']
		
		IF NewRec<TW_USE_TW_USE_CODE$> = '99' THEN
			RETURN		;* No wafers used for code 99
		END
		
		NewQty = NewRec<TW_USE_TW_QTY$>	
		OrgQty = OrgRec<TW_USE_TW_QTY$>
			
		IF OrgQty = '' THEN
			TransQty = NewQty
		END ELSE
			TransQty = NewQty - OrgQty
		END
		
		IF TransQty = 0  THEN RETURN
		
		TWType = NewRec<TW_USE_TW_TYPE$>
		
		IF  NewRec<TW_USE_TW_USE_CODE$> = '11' THEN
			RDSNo		= XLATE('RDS_TEST',MetNo,RDS_TEST_RDS_NO$,'X')
			WONo		= XLATE('REACT_RUN',RDSNo,REACT_RUN_WO_NO$,'X')
			ProdOrdNo	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')
			
			IF ProdOrdNo = '' THEN RETURN
			
			CustTWPartNo = TWType
			
						
		END
		
		IF TWType = 'PROD' THEN
			RDSNo		= XLATE('RDS_TEST',MetNo,RDS_TEST_RDS_NO$,'X')
			WONo		= XLATE('REACT_RUN',RDSNo,REACT_RUN_WO_NO$,'X')
			ProdOrdNo	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')
		
			IF ProdOrdNo = '' THEN RETURN
		END
	
	CASE TableName = 'NCR'
		
		NewQty = SUM(obj_NCR('RejQty',RecKey:@RM:NewRec))
		
		IF OrgRec = '' THEN
			TransQty = NewQty
		END ELSE
			OrgQty = SUM(obj_NCR('RejQty',RecKey:@RM:OrgRec))
			TransQty = NewQty - OrgQty
		END
		
		IF TransQty = 0 THEN RETURN
		
		WONo		= NewRec<NCR_WO_NO$>
		ProdOrdNo	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')

		IF ProdOrdNo = '' THEN RETURN

 	CASE TableName = 'WO_MAT'
 	
		WONo	      = RecKey[1,'*']
		CassNo	      = RecKey[COL2()+1,'*']
        WOMatKey      = WONo:'*':CassNo
		ProdOrdNo     = XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')
		IF ProdOrdNo  = '' THEN RETURN
		
		
		* * * * * * * *  New Material Hold Transaction   * * * * * * * * *
		NewMatHoldFlag = NewRec<WO_MAT_HOLD$>
		OrgMatHoldFlag = OrgRec<WO_MAT_HOLD$>
		
		SAPBatchNo	= NewRec<WO_MAT_SAP_BATCH_NO$>
		MUBoxFlag	= XLATE('WO_MAT',RecKey,'MU_BOX','X')
		
		if MUBoxFlag = 1 then
			SLoc = '0400'
		end else
			Sloc = '0500'
		end
		
		IF OrgRec NE '' THEN
			IF OrgMatHoldFlag EQ '' THEN OrgMatHoldFlag = 0
			IF NewMatHoldFlag EQ '' THEN NewMatHoldFlag = 0
			
			IF (NewMatHoldFlag NE OrgMatHoldFlag) THEN
			    
			    IF SAPBatchNo NE '' THEN				
					obj_SAP('AddTransaction','WO_MAT_HOLD':@RM:SAPBatchNo:@RM:Sloc:@RM:NewMatHoldFlag)
				END
			END
		END
		
		* * * * * * * *  End of Material Hold Transaction   * * * * * * * * *
		
	CASE TableName = 'WM_OUT'
	
		NewMUFlag		= NewRec<WM_OUT_MAKEUP_BOX$> AND NewRec<WM_OUT_SUP_VER_SIG$>
		OrgMUFlag		= OrgRec<WM_OUT_MAKEUP_BOX$> AND OrgRec<WM_OUT_SUP_VER_SIG$>
		
		IF OrgMUFlag = '' THEN OrgMUFlag = 0
		IF NewMUFlag = '' THEN NewMUFlag = 0
		
		IF (NewMUFlag NE OrgMUFlag) THEN
		
			WONo			= RecKey[1,'*']	
			CassNo			= FIELD(RecKey,'*',3)
			
			ProdOrdNo		= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')
			IF ProdOrdNo 	= '' THEN RETURN

			NewWfrCnt = obj_WM_Out('WfrsOut',RecKey:@RM:NewRec)
			OrgWfrCnt = obj_WM_Out('WfrsOut',RecKey:@RM:OrgRec)
			
			IF NewMUFlag = 1 AND OrgMUFlag = 0 THEN
				TransQty = NewWfrCnt
			END
			IF NewMUFlag = 0 and OrgMUFlag = 1 THEN
				TransQty = (0 - OrgWfrCnt)
			END
			
		END
		  
 	CASE TableName = 'WO_LOG'
 	
 	
		WONo        = RecKey
 	    ProdOrdNo   = ''
		NewHoldFlag = NewRec<WO_LOG_WO_HOLD$>
		OrgHoldFlag = OrgRec<WO_LOG_WO_HOLD$>
		
		
*		IF OrgRec NE '' THEN
*			IF OrgHoldFlag = '' THEN OrgHoldFlag = 0
*			
*			ProdOrdNo = NewRec<WO_LOG_PROD_ORD_NO$>
*			IF ProdOrdNo = '' THEN RETURN
*				
*			IF NewHoldFlag NE OrgHoldFlag THEN
*				obj_SAP('AddTransaction','WO_HOLD':@RM:ProdOrdNo:@RM:NewHoldFlag)
*			END
*		END
		
		IF OrgRec<WO_LOG_WO_STOP_DTM$> = '' THEN
		
			* Added check so open Work Orders send Promise Ship Dt changes to SAP - DKK & JCH 7/9/2015
		
			NewPromiseShipDt = NewRec<WO_LOG_PROMISE_SHIP_DT$>
			OrgPromiseShipDt = OrgRec<WO_LOG_PROMISE_SHIP_DT$>
			
			IF NewPromiseShipDt NE OrgPromiseShipDt THEN
				IF NewPromiseShipDt NE '' AND OrgPromiseShipDt NE '' THEN
					obj_SAP('AddTransaction','PROMISE_DT':@RM:ProdOrdNo:@RM:OrgPromiseShipDt:@RM:NewPromiseShipDt)
				END
			END
			
		END
		
		NewStartDTM = NewRec<WO_LOG_WO_START_DTM$>
		OrgStartDTM = OrgRec<WO_LOG_WO_START_DTM$>
		
		NewStartDt = NewStartDTM[1,'.']
		OrgStartDt = OrgStartDTM[1,'.']
		
		IF NewStartDt NE '' AND OrgStartDt EQ '' AND ProdOrdNo NE '' THEN
			obj_SAP('AddTransaction','WO_START':@RM:ProdOrdNo:@RM:'1')
		END
		
		NewStopDTM = NewRec<WO_LOG_WO_STOP_DTM$>
		OrgStopDTM = OrgRec<WO_LOG_WO_STOP_DTM$>
		
		NewStopDt = NewStopDTM[1,'.']
		OrgStopDt = OrgStopDTM[1,'.']
		
		// These transactions have been deprecated. 2/12/20 - djs
*		IF NewStopDt NE '' AND OrgStopDt = '' THEN
*			obj_SAP('AddTransaction','WO_STOP':@RM:ProdOrdNo:@RM:'1')
*		END
	
	CASE 1
	
END CASE

RETURN


* * * * * * *
Delete:
* * * * * * *

TableName	= Parms[1,@RM]
RecKey		= Parms[COL2()+1,@RM]
NewRec		= Parms[COL2()+1,@RM]
OrgRec		= Parms[COL2()+1,@RM]

IF TableName = '' THEN RETURN
IF RecKey = '' THEN RETURN

BEGIN CASE

	CASE TableName = 'TW_USE'
	
		MetNo = RecKey[1,'*']
	
		IF OrgRec <TW_USE_TW_USE_CODE$> = '99' THEN
			RETURN		;* No wafers used for code 99
		END
		
		OrgQty = OrgRec<TW_USE_TW_QTY$>
			
		TransQty = (0 - OrgQty)	
						
		IF TransQty = 0  THEN RETURN
		
		TWType = OrgRec<TW_USE_TW_TYPE$>
		
		IF OrgRec<TW_USE_TW_USE_CODE$> = '11' THEN
			RDSNo		= XLATE('RDS_TEST',MetNo,RDS_TEST_RDS_NO$,'X')
			WONo		= XLATE('REACT_RUN',RDSNo,REACT_RUN_WO_NO$,'X')
			ProdOrdNo	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')
			
			IF ProdOrdNo = '' THEN RETURN
			
			CustTWPartNo = OrgRec<TW_USE_TW_TYPE$>
			
		END
		
		IF TWType = 'PROD' THEN
			RDSNo		= XLATE('RDS_TEST',MetNo,RDS_TEST_RDS_NO$,'X')
			WONo		= XLATE('REACT_RUN',RDSNo,REACT_RUN_WO_NO$,'X')
			ProdOrdNo	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')
		
			IF ProdOrdNo = '' THEN RETURN
		
		END
	
	CASE TableName = 'NCR'
		
		OrgQty = SUM(obj_NCR('RejQty',RecKey:@RM:OrgRec))
		TransQty = 0 - OrgQty
		
		IF TransQty = 0 THEN RETURN
	
		WONo		= OrgRec<NCR_WO_NO$>
		ProdOrdNo	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')

		IF ProdOrdNo = '' THEN RETURN
		
	CASE TableName = 'WO_LOG'
		NULL
	
END CASE

RETURN

