COMPILE ROUTINE FIX_BATCHES(Dummy)
ROWDEF(CHARSTR)

DECLARE SUBROUTINE Send_Dyn,ErrMsg, Set_Status, Send_Info, obj_Tables, obj_SAP, Btree.Extract
DECLARE FUNCTION obj_WO_Mat, obj_SAP, 

$INSERT WO_LOG_EQUATES
$INSERT WO_MAT_EQUATES
$INSERT WM_OUT_EQUATES
$INSERT PROD_VER_EQUATES

EQU CRLF$ TO \0D0A\
EQU TAB$ TO \09\

EQU COL$SAP_PLANT			TO 1
EQU COL$SAP_MAT_TYPE		TO 2
EQU COL$EPI_PART_NO			TO 3
EQU COL$SAP_UOM				TO 4
EQU COL$SAP_STORAGE_LOC		TO 5
EQU COL$SAP_STORAGE_TYPE	TO 6
EQU COL$SAP_WH_NO			TO 7
EQU COL$SAP_BIN				TO 8
EQU COL$WO_NO				TO 9
EQU COL$CASS_NO				TO 10
EQU COL$CASS_ID_SAP			TO 11
EQU COL$LOT_NO				TO 12
EQU COL$CURR_WFR_QTY		TO 13
EQU COL$EXPIRY_DT			TO 14
EQU COL$SUB_SUPP_CD			TO 15
EQU COL$CUST_PART_REV		TO 16


OPEN 'RDS' TO RDSTable ELSE
	ErrMsg('Unable to open "RDS" table for import')
	GOTO Bail
END

OPEN 'WO_LOG' TO WOLogTable ELSE
	ErrMsg('Unable to open "WO_LOG" table for import')
	GOTO Bail
END

OPEN 'DICT.WO_MAT' TO DictWOMat ELSE
	GOTO Bail
END


OPEN 'WO_MAT' TO WOMatTable ELSE
	GOTO Bail
END

OSREAD WIPUpload FROM 'C:\SAPInventorySent.txt' ELSE GOTO Bail

SWAP \0D0A\ WITH @FM IN WIPUpload
CONVERT \09\ TO @VM  IN WIPUpload

WIPUpload[-1,1] = ''

LineCount = COUNT(WIPUpload,@FM) + (WIPUpload NE '')


TransLog = ''
SAPChangeLog = 'WOMatKey':TAB$:'Prod Ord No':TAB$:'Org Batch No':TAB$:'Batch No in OpenInsight':CRLF$

FOR I = 2 TO LineCount
	LineIn = WIPUpload<I>
	
	SAPStorageLoc 	= LineIn<1,COL$SAP_STORAGE_LOC>
	SAPBin			= LineIn<1,COL$SAP_BIN>
	WONo			= LineIn<1,COL$WO_NO>
	CassNo			= LineIn<1,COL$CASS_NO>
	CassID			= LineIn<1,COL$CASS_ID_SAP>
	LotNo			= LineIn<1,COL$LOT_NO>
	
	WOMatKey = WONo:'*':CassNo
	
	IF WONo NE '' AND CassNo NE '' THEN
	
		READ WOMatRec FROM WOMatTable,WOMatKey THEN
			WriteFlag = 0
		
			CurrBatchNo	= WOMatRec<WO_MAT_SAP_BATCH_NO$>
			ProdOrdNo	= XLATE('WO_LOG',WONo,WO_LOG_PROD_ORD_NO$,'X')
			WOMatRec<WO_MAT_SAP_BATCH_NO$> = LotNo
			WriteFlag = 1
			
			IF ProdOrdNo NE '' THEN
				IF CurrBatchNo NE LotNo THEN
					SAPChangeLog := WOMatKey:TAB$:ProdOrdNo:TAB$:CurrBatchNo:TAB$:LotNo:CRLF$
					WOMatRec<WO_MAT_SAP_BATCH_NO$> = LotNo
					WriteFlag = 1
				END
			END ELSE
				
			
				IF CurrBatchNo = '' THEN
					WOMatRec<WO_MAT_SAP_BATCH_NO$> = LotNo
					WriteFlag = 1
					TransLog<I> = I:' ':WOMatKey:' -> Batch Added: ':LotNo
					Send_Dyn(WOMatKey:' -> Batch Added: ':LotNo)
				END ELSE
					IF CurrBatchNo NE LotNo THEN
						WOMatRec<WO_MAT_SAP_BATCH_NO$> = LotNo
						WriteFlag = 1
						TransLog<I> = I:' ':WOMatKey:' Batch Changed: ':CurrBatchNo:' -> ':LotNo
						Send_Dyn(WOMatKey:' Batch Changed: ':CurrBatchNo:' -> ':LotNo)
					
					END
				
				END
			END
			
			IF WriteFlag = 1 THEN
				WRITE WOMatRec ON WOMatTable,WOMatKey ELSE NULL
			END
			
		
		END	;* End of read
	END

NEXT I

SWAP @FM WITH CRLF$ IN TransLog
SWAP @FM WITH CRLF$ IN SAPChangeLog

OSWRITE TransLog TO 'C:\TransLog.txt' 
OSWRITE SAPChangeLog TO 'C:\SAPChangeLog.txt'


DEBUG



* * * * * * *
Bail:
* * * * * * *


END

