Compile function NCR_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler
$insert LOGICAL

Declare subroutine Set_Status, Logging_Services, Error_Services
Declare function Environment_Services, Logging_Services, SRP_Datetime, Utility

EQU CRLF$	TO \0D0A\
EQU TAB$	TO CHAR(9)

EQU COL$RDS_NO		TO 1
EQU COL$REACTOR		TO 2
EQU COL$REJ_CNT		TO 3

GoToService

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service SendToSPC(NCRNo, RDSList, ReactorType, PSNo, DeptResp, LossCode, LossDesc, LossStage, LossBy, ACCode, ACDesc)
	Headers = 'Logging DTM' : @FM : 'Machine' : @FM : 'Level' : @FM : 'Log'
	ColumnWidths = 20 : @FM : 12 : @FM : 7 : @FM : 500
	Machine         = Environment_Services('GetServer')
	LogPath = Environment_Services('GetApplicationRootPath') : '\LogFiles\NCR'
	makeDirSuccess = Utility("MAKEDIR", LogPath)
	LogDate = Oconv(Date(), 'D4/')
	LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : '_NcrSendToSpc.csv'
	objLog = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, ' ', Headers, ColumnWidths, False$, False$)
	
	RdsNos = ''
	RdsCount = 0
	ReactorList = ''
	ReactorCount = 0
	RejCntList = ''
	RejCntCount = 0
	
	RDSCnt = DCOUNT(RDSList<1>,@VM)
	If RDSCnt GT 0 then
		FOR I = 1 TO RDSCnt
			If RdsNos NE '' then
				RdsNos := ', '
			end
			RdsNos := RDSList<COL$RDS_NO, I>
			RdsCount += Num(RDSList<COL$RDS_NO, I>)
			If ReactorList NE '' then
				ReactorList := ', '
			end
			ReactorList := RDSList<COL$REACTOR, I>
			ReactorCount += Num(RDSList<COL$REACTOR, I>)
			If RejCntList NE '' then
				RejCntList := ', '
			end
			RejCntList := RDSList<COL$REJ_CNT, I>
			RejCntCount += Num(RDSList<COL$REJ_CNT, I>)
		Next I
	end
	
	LogMessage = 'Attempting to send NCR data to SPC. Data: {'
	LogMessage := 'NCR No: ' : NCRNo
	LogMessage := ', RDS Nos: ' : RdsNos
	LogMessage := ', Reactors: ' : ReactorList
	LogMessage := ', Reject Counts: ' : RejCntList
	LogMessage := ', Reactor Type: ' : ReactorType
	LogMessage := ', PSN: ' : PSNo
	LogMessage := ', Responsible Dept: ' : DeptResp
	LogMessage := ', Loss Code: ' : LossCode
	LogMessage := ', Loss Desc: ' : LossDesc
	LogMessage := ', Loss Stage: ' : LossStage
	LogMessage := ', Loss By: ' : LossBy
	LogMessage := ', AC Code: ' : ACCode
	LogMessage := ', AC Desc: ' : ACDesc
	LogMessage := '}'
	
	LogDate = Oconv(Date(), 'D4/')
	LogData     = ''
	LogData<1>  = Oconv(Date(), 'D4/') : ' ' : Oconv(Time(), 'MTS') ; // Logging DTM
	LogData<2>  = Machine
	LogData<3> = 'INFO'
	LogData<4>  = LogMessage
	Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
	
	ArgsNotNull = NCRNo NE '' and RDSList NE '' and ReactorType NE '' and PSNo NE ''
	ArgsNotNull = ArgsNotNull EQ True$ and DeptResp NE '' and LossCode NE ''
	ArgsNotNull = ArgsNotNull EQ True$ and LossDesc NE '' and LossStage NE ''
	ArgsNotNull = ArgsNotNull EQ True$ and LossBy NE '' and ACCode NE '' and ACDesc NE ''
	
	RdsListCountsAtLeastOne = RdsCount GE 1 and ReactorCount GE 1 and RejCntCount GE 1
	RdsListCountsEqual = RdsCount EQ ReactorCount and ReactorCount EQ RejCntCount
	
	If ArgsNotNull EQ True$ and RdsListCountsAtLeastOne NE False$ and RdsListCountsEqual NE False$ then
		DataLines = ''

		FOR I = 1 TO RDSCnt

			DataLine  = QUOTE(NCRNo):TAB$
			DataLine := QUOTE(RDSList<COL$RDS_NO, I>):TAB$
			
			IF ReactorType = 'P' OR ReactorType = 'EPP' THEN
				DataLine := QUOTE('EP':RDSList<COL$REACTOR, I>):TAB$		;* EpiPRO reactors in SPC have "EP" on the front.
			END ELSE
				DataLine := QUOTE(RDSList<COL$REACTOR, I>):TAB$
			END
			
			DataLine := QUOTE(RDSList<COL$REJ_CNT, I>):TAB$
			DataLine := QUOTE(PSNo):TAB$
			DataLine := QUOTE(DeptResp):TAB$
			DataLine := QUOTE(LossCode):TAB$
			DataLine := QUOTE(LossDesc):TAB$
			DataLine := QUOTE(LossStage):TAB$
			DataLine := QUOTE(LossBy):TAB$
			DataLine := QUOTE(ACCode):TAB$
			DataLine := QUOTE(ACDesc):TAB$
			
			DataLines := DataLine:CRLF$
			
		NEXT I
		
		DateTime = SRP_Datetime("Now")
		Year = SRP_Datetime("Year", DateTime)
		Month = SRP_Datetime("Month", DateTime)
		Day = SRP_Datetime("Day", DateTime)
		
		FileName = NCRNo : '_NCR_Data_' : Year : Month : Day : '.txt'
		Path = Environment_Services('GetSPCDataPath')

		DOSFile = Path : '\' : FileName

		Set_Status(0)

		OSRead DOSRecord FROM DOSFile ELSE
			ErrCode = Status()
			IF ErrCode = 4 THEN
				DOSRecord = ''	;* File doesn't exist
			END ELSE
				* ErrMsg('Unable to read DOS file "SPC_Data.TXT" for update. ':ErrCode:' Please try again.')	;* Remarked out per Chad Becker request - dkk 12/9/14
				RETURN
			END
		END
		 
		DOSRecord := DataLines

		OSWrite DOSRecord ON DOSFile
		
		ErrCode = Status()
		ErrorMessage = ''
		Begin Case
			Case ErrCode EQ 1
				ErrorMessage = 'Bad filename.'
			Case ErrCode EQ 2
				ErrorMessage = 'Cannot write to file. Access denied.'
			Case ErrCode EQ 3
				ErrorMessage = 'Disk or directory full.'
			Case ErrCode EQ 4
				ErrorMessage = 'File does not exist.'
			Case ErrCode EQ 5
				ErrorMessage = 'Unknown error.'
			Case ErrCode EQ 6
				ErrorMessage = 'Attempt to write to a read-only file.'
		End Case
		
		LogDate = Oconv(Date(), 'D4/')
		LogData     = ''
		LogData<1>  = Oconv(Date(), 'D4/') : ' ' : Oconv(Time(), 'MTS') ; // Logging DTM
		LogData<2>  = Machine
		If ErrorMessage NE '' then
			ErrorMessage = 'Error in ' : Service : '. When attempting to write the NCR data to a file the following error occurred. ' : ErrorMessage
			LogData<3> = 'ERROR'
			LogData<4>  = ErrorMessage
			Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
			Error_Services('Add', ErrorMessage)
		end else
			LogData<3> = 'INFO'
			LogData<4>  = 'Sucessfully sent NCR ' : NCRNo : ' data to SPC in file ' : DOSFile : '.'
			Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
		end
	end else
		ErrorMessage = 'Error in ' : Service : '. One or more arguments were null or malformed.'	
		LogDate = Oconv(Date(), 'D4/')
		LogData     = ''
		LogData<1>  = Oconv(Date(), 'D4/') : ' ' : Oconv(Time(), 'MTS') ; // Logging DTM
		LogData<2>  = Machine
		LogData<3> = 'ERROR'
		LogData<4>  = ErrorMessage
		Logging_Services('AppendLog', objLog, LogData, @RM, @FM, False$)
		Error_Services('Add', ErrorMessage)
	end
end service





