COMPILE FUNCTION Comm_Dialog_WO_Scan_Substrates(Method, Parm1)
#pragma precomp SRP_PreCompiler

/*
	Commuter module for Dialog_WO_Scan window.
	
	08/12/2004 - John C. Henry, J.C. Henry & Co., Inc.
*/

DECLARE SUBROUTINE Set_Property, End_Dialog, Set_Status, ErrMsg, Extract_SI_Keys
DECLARE SUBROUTINE obj_Appwindow, Start_Window, Msg
DECLARE FUNCTION Get_Property, Get_Status, Dialog_Box, Send_Message

$INSERT MSG_EQUATES
$INSERT APPCOLORS

EQU CRLF$	TO \0D0A\

EQU COL$LQP_LOT		TO 1				;* Equates for Lot_Qty... edit table controls used to scan in label data
EQU COL$LQP_QTY		TO 2
EQU COL$LQP_PART	TO 3				;* This is either Cust Part No OR Substrate Part Number depending on which control is visible


EQU CA$ITEM_NO				TO 1		;* Equates for Checking Array passed to Scan routine
EQU CA$LOT_NO				TO 2
EQU CA$LOT_QTY				TO 3
EQU CA$CUST_PART_NO			TO 4
EQU CA$SUB_PART_NO			TO 5
EQU CA$SCANNED_LOT_QTY		TO 6
EQU CA$VERIFY_PART_NO		TO 7

ErrTitle = 'Error in Comm_Dialog_WO_Scan'
ErrorMsg = ''

Result = ''

BEGIN CASE
	CASE Method = 'Create'			; GOSUB Create
	CASE Method = 'OK'				; GOSUB OK
	CASE Method = 'Cancel'			; GOSUB Cancel
	CASE Method = 'LQP'				; GOSUB LQP
	CASE 1
		ErrMsg(ErrTitle:@SVM:'Unknown method ':QUOTE(Method):' passed to routine.')
		
END CASE

RETURN Result


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

OrderNo		= Parm1[1,@FM]
CheckArray	= FIELD(Parm1,@FM,2,999)

Set_Property(@WINDOW:'@ORDER_NO',OrderNo)
Set_Property(@WINDOW,'@CHECK_ARRAY',CheckArray)
obj_AppWindow('Create')

TimeStamp = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
Set_Property(@WINDOW:'.RX_DTM','DEFPROP',TimeStamp)
Set_Property(@WINDOW,'FOCUS',@WINDOW:'.RX_DTM')
Set_Property(@WINDOW:'.RX_DTM','SELECTION',1:@FM:65534)

GOSUB Refresh


* * * * * * *
Close:
* * * * * * *


* * * * * * *
Cancel:
* * * * * * *

End_Dialog(@WINDOW,'Cancel')

RETURN


* * * * * * *
Refresh:
* * * * * * *


ListArray	= Get_Property(CtrlEntId,'ARRAY')
CheckArray = Get_Property(@WINDOW,'@CHECK_ARRAY')

TotalScannedQty = SUM(ListArray<COL$LQP_QTY>)
TotalOrderQty	= SUM(CheckArray<CA$LOT_QTY>)

Set_Property(@WINDOW:'.ORDER_QTY','TEXT',OCONV(TotalOrderQty,'MD0,'))
Set_Property(@WINDOW:'.SCANNED_QTY','TEXT',OCONV(TotalScannedQty,'MD0,'))

IF TotalScannedQty = TotalOrderQty THEN
	Set_Property(@WINDOW:'.SCANNED_QTY','BACKCOLOR',GREEN$)
END ELSE
	Set_Property(@WINDOW:'.SCANNED_QTY','BACKCOLOR',RED$)
END

RETURN


* * * * * * *
LQP:
* * * * * * *

CtrlEntID	= Get_Property(@WINDOW,'FOCUS')
StripANSI	= Get_Property(@WINDOW:'.STRIP_ANSI','CHECK')
SkipSubANSI	= Get_Property(@WINDOW:'.SKIP_SUBSTRATE_ANSI','CHECK')

CheckArray = Get_Property(@WINDOW,'@CHECK_ARRAY')

IF CtrlEntID = @WINDOW:'.LOT_QTY' THEN ColCnt = 2 ELSE ColCnt = 3

PrevSelPos	= Get_Property(CtrlEntId,"PREVSELPOS")
PrevCol 	= PrevSelPos<1>
PrevRow 	= PrevSelPos<2>

CurrPos = Get_Property(CtrlEntId,'SELPOS')
CurrCol = CurrPos<1>
CurrRow = CurrPos<2>

ListData	= Get_Property(CtrlEntId,'LIST')

IF ListData<CurrRow,CurrCol> = '' THEN
	
	ColPointer = CurrCol
	LinePointer = CurrRow
	
	* Find the first non-empty cell
	
	LOOP
		Test = ListData<LinePointer,(ColPointer) >
	UNTIL Test NE '' OR (LinePointer = 0 AND ColPointer = 1)
		ColPointer -= 1
		IF ColPointer = 0 THEN
			ColPointer = ColCnt
			LinePointer -= 1
		END
	REPEAT
	
	* Move one past the non empty cell
	
	BEGIN CASE
		CASE LinePointer = 0 
			* Empty Table
			LinePointer = 1
			CoilPointer = 1
			
		CASE ColPointer = ColCnt
			LinePointer += 1
			ColPointer = 1
			
		CASE 1
			ColPointer += 1
			
	END CASE
	Set_Property(CtrlEntId,"SELPOS",ColPointer:@FM:LinePointer)
END


IF ListData<PrevRow,PrevCol> NE '' THEN
	BEGIN CASE
		CASE PrevCol = COL$LQP_LOT
			IF StripANSI THEN
				RawLot = TRIM(ListData<PrevRow,PrevCol>)
				IF RawLot[1,2] = '1T' THEN
					LotNo = RawLot[3,99]
				END ELSE
					TypeOver = ''
					TypeOver<MTYPE$> = 'B&Rescan'
					TypeOver<MTEXT$> = 'Scanned Code ':QUOTE(RawLot):' is not an ANSI Lot No.'
					Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')
					Set_Property(CtrlEntId,"SELPOS",PrevSelPos)
					Set_Property(CtrlEntID,'INVALUE','',PrevSelPos)
					RETURN
				END
			END ELSE
				LotNo = TRIM(ListData<PrevRow,PrevCol>)
			END
			
			IF CtrlEntID NE @WINDOW:'.LOT_QTY_SUB_PART' THEN
			
				* Don't check Customer Lot Numbers if scanning substrates
				
				LOCATE LotNo IN CheckArray<CA$LOT_NO> USING @VM SETTING Pos ELSE
					* Accept or Rescan message
					TypeOver = ''
					TypeOver<MTYPE$> = 'B&Rescan'
					TypeOver<MTEXT$> = 'Lot No ':QUOTE(LotNo):' not found in':@TM:'Order Item Lot Numbers!'
					Resp = Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')
					
					LotNo = ''											;* Clear entered field for a rescan
					Set_Property(CtrlEntId,"SELPOS",PrevSelPos)			;* Cursor to Lot No for a rescan
				END
			END
			
			Set_Property(CtrlEntID,'INVALUE',LotNo,PrevSelPos)
			
		
		CASE PrevCol = COL$LQP_QTY									
			
			IF StripANSI THEN
				RawLotQty = ListData<PrevRow,PrevCol>
				IF RawLotQty[1,1] = 'Q' THEN
					LotQty = RawLotQty[2,99]
				END ELSE
					TypeOver = ''
					TypeOver<MTYPE$> = 'B&Rescan'
					TypeOver<MTEXT$> = 'Scanned Code ':QUOTE(RawLotQty):' is not an ANSI Quantity'
					Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')
					Set_Property(CtrlEntId,"SELPOS",PrevSelPos)
					Set_Property(CtrlEntID,'INVALUE','',PrevSelPos)
					RETURN
				END
			END ELSE
				LotQty = ListData<PrevRow,PrevCol>
			END
			
			IF NOT(NUM(LotQty)) THEN
				* Accept or Rescan message
				TypeOver = ''
				TypeOver<MTYPE$> = 'B&Rescan'
				TypeOver<MTEXT$> = 'Lot Qty ':QUOTE(LotQty):' is not a number!'
				Resp = Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')

				LotNo = ''											;* Clear entered field for a rescan
				Set_Property(CtrlEntId,"SELPOS",PrevSelPos)			;* Cursor to Lot No for a rescan
			END
			
			Set_Property(CtrlEntID,'INVALUE',LotQty,PrevSelPos)
		
		
		CASE PrevCol = COL$LQP_PART
			
			IF StripANSI AND NOT(SkipSubAnsi) THEN				;***************************************
				RawPartNo = TRIM(ListData<PrevRow,PrevCol>)
				IF RawPartNo[1,1] = 'P' OR RawPartNo[1,1] = 'S' THEN
					PartNo = RawPartNo[2,99]
				END ELSE
					TypeOver = ''
					TypeOver<MTYPE$> = 'B&Rescan'
					TypeOver<MTEXT$> = 'Scanned Code ':QUOTE(RawPartNo):' is not an ANSI Part No'
					Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')
					Set_Property(CtrlEntId,"SELPOS",PrevSelPos)
					Set_Property(CtrlEntID,'INVALUE','',PrevSelPos)
					RETURN
				END
			END ELSE
				PartNo = ListData<PrevRow,PrevCol>
			END
			
			IF CtrlEntID = @WINDOW:'.LOT_QTY_PART' THEN
			
				* Check Customer Part Numbers
				
				LOCATE PartNo IN CheckArray<CA$VERIFY_PART_NO> USING @VM SETTING Pos THEN
				
					PartNo = CheckArray<CA$CUST_PART_NO,Pos>
					
				END ELSE
					* Accept or Rescan message
					TypeOver = ''
					TypeOver<MTYPE$> = 'B&Rescan'
					TypeOver<MTEXT$> = 'Part No ':QUOTE(PartNo):' not found in':@TM:'Order Customer Part Numbers!'
					Resp = Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')

					PartNo = ''											;* Clear entered field for a rescan
					Set_Property(CtrlEntId,"SELPOS",PrevSelPos)			;* Cursor to Lot No for a rescan
				END
					
				
			END ELSE
			
				* Check Substrate Part Numbers
				
				LOCATE PartNo IN CheckArray<CA$SUB_PART_NO> USING @VM SETTING Pos ELSE
					* Accept or Rescan message
					TypeOver = ''
					TypeOver<MTYPE$> = 'B&Rescan'
					TypeOver<MTEXT$> = 'Part No ':QUOTE(PartNo):' not found in':@TM:'Order Substrate Part Numbers!'
					Resp = Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')

					PartNo = ''											;* Clear entered field for a rescan
					Set_Property(CtrlEntId,"SELPOS",PrevSelPos)			;* Cursor to Lot No for a rescan
				END
			END	;* End of check for Customer supplied or Inventory substrate part number
			
			Set_Property(CtrlEntID,'INVALUE',PartNo,PrevSelPos)
		
	END CASE
END 

GOSUB Refresh

RETURN


* * * * * * *
OK:
* * * * * * *

CtrlEntID = @WINDOW:'.FIELD_RADIO'
CheckArray = Get_Property(@WINDOW,'@CHECK_ARRAY')

ControlSelection = Get_Property(CtrlEntID,'VALUE')

Ctrls  = @WINDOW:'.LOT_QTY':@RM				; Props  = 'VISIBLE':@RM
Ctrls := @WINDOW:'.LOT_QTY_PART':@RM		; Props := 'VISIBLE':@RM
Ctrls := @WINDOW:'.LOT_QTY_SUB_PART' 		; Props := 'VISIBLE'

BEGIN CASE
	CASE ControlSelection = 'LQ'		; Ctrl = @WINDOW:'.LOT_QTY'
	CASE ControlSelection = 'LQP'		; Ctrl = @WINDOW:'.LOT_QTY_PART'
	CASE ControlSelection = 'LQSP'		; Ctrl = @WINDOW:'.LOT_QTY_SUB_PART'
END CASE

ListData = Get_Property(Ctrl,'LIST')


TimeStamp = Get_Property(@WINDOW:'.RX_DTM','TEXT')
TimeStamp = ICONV(TimeStamp,'DT')
IF TimeStamp = '' THEN
	ErrMsg('Invalid Received DateTime data entered.  "mm/dd/yy hh:mm:ss" is the basic format.')
	RETURN
END


* TimeStamp = ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')


CheckArray<CA$SCANNED_LOT_QTY> = ''	;* Clear scanned lot quantities column

LineCnt = 1
LOOP
	TestVal = ListData<LineCnt,1>
UNTIL TestVal = ''
	LotNo	= TRIM(ListData<LineCnt,1>)
	Qty		= TRIM(ListData<LineCnt,2>)
	
	IF ControlSelection = 'LQ' THEN
		LOCATE LotNo IN CheckArray<CA$LOT_NO> USING @VM SETTING Pos THEN
			OrderItemNo = CheckArray<CA$ITEM_NO,Pos>
			CustPartNo	= CheckArray<CA$CUST_PART_NO,Pos>
			SubPartNo	= CheckArray<CA$SUB_PART_NO,Pos>
			CheckArray<CA$SCANNED_LOT_QTY,Pos> = CheckArray<CA$SCANNED_LOT_QTY,Pos> + Qty
		END ELSE
			* Accept or Rescan message
			TypeOver = ''
			TypeOver<MTYPE$> = 'B&Rescan'
			TypeOver<MTEXT$> = 'Lot No ':QUOTE(LotNo):' not found in':@TM:'Order Item Lot Numbers!'
			Resp = Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')
			
			LotNo = ''														;* Clear entered field for a rescan
			Set_Property(CtrlEntId,"SELPOS",COL$LOT_NO:@FM:LineCnt)			;* Cursor to Lot No for a rescan
			RETURN
		END
		

	END
	
	IF ControlSelection = 'LQP' THEN
		CustPartNo	= TRIM(ListData<LineCnt,3>)
		LOCATE LotNo IN CheckArray<CA$LOT_NO> USING @VM SETTING Pos THEN
			OrderItemNo = CheckArray<CA$ITEM_NO,Pos>
			SubPartNo	= CheckArray<CA$SUB_PART_NO,Pos>
			CheckArray<CA$SCANNED_LOT_QTY,Pos> = CheckArray<CA$SCANNED_LOT_QTY,Pos> + Qty
		END ELSE
			* Accept or Rescan message
			TypeOver = ''
			TypeOver<MTYPE$> = 'B&Rescan'
			TypeOver<MTEXT$> = 'Lot No ':QUOTE(LotNo):' not found in':@TM:'Order Item Lot Numbers!'
			Resp = Msg(@WINDOW,TypeOver,'LABEL_DATA_ERROR')
			
			LotNo = ''														;* Clear entered field for a rescan
			Set_Property(CtrlEntId,"SELPOS",COL$LOT_NO:@FM:LineCnt)			;* Cursor to Lot No for a rescan
			RETURN
		END
	END
	
	IF ControlSelection = 'LQSP' THEN
		
		SubPartNo	= TRIM(ListData<LineCnt,3>)
		OrderItemNo = CheckArray<CA$ITEM_NO,1>		;* Changed 2/8/2010 from ' = 1 ' JCH
		CustPartNo = CheckArray<CA$CUST_PART_NO,1>
		
	END

	Result<1,LineCnt> = LineCnt
	Result<2,LineCnt> = LotNo							;* Lot No
	Result<3,LineCnt> = Qty
	Result<4,LineCnt> = CustPartNo	;* PartNo
	Result<5,LineCnt> = SubPartNo	;* Substrate Part No
	Result<6,LineCnt> = OCONV(TimeStamp,'DT')
	Result<7,LineCnt> = @USER4
	Result<8,LineCnt> = OrderItemNo
	LineCnt += 1
REPEAT

IF ControlSelection NE 'LQSP' THEN

	* Orders with Epi supplied substrates don't have any lot quantities to check against.
	
	ProblemQtyLots = ''
	FOR I = 1 TO COUNT(CheckArray<CA$LOT_NO>,@VM) + (CheckArray<CA$LOT_NO> NE '')
		IF (CheckArray<CA$SCANNED_LOT_QTY,I> NE '') AND (CheckArray<CA$LOT_QTY,I> NE CheckArray<CA$SCANNED_LOT_QTY,I>) THEN
			* Lot quantity mismatch
			ProblemQtyLots<-1> = CheckArray<CA$LOT_NO,I>
		END
	NEXT I

	IF ProblemQtyLots NE '' THEN
		LineCnt = 1
		LOOP
			TestVal = ListData<LineCnt,1>
		UNTIL TestVal = ''
			LotNo	= TRIM(ListData<LineCnt,1>)
			LOCATE LotNo IN ProblemQtyLots USING @FM SETTING Pos THEN
				stat = Send_Message(Ctrl,'COLOR_BY_POS',0,LineCnt,RED$)		;* Flag all the lines with lot numbers having qty problems
			END
			LineCnt += 1
		REPEAT
		
		ErrMsg('Lot Quantity(s) Scanned do not match Order Item Lot Quantity(s)!||Review and rescan highlighted items.')
		RETURN
	END
END ELSE
	* 
END

End_Dialog(@WINDOW,Result)

RETURN


