compile subroutine order_fields_valid(ConvType, DataIo, Branch, ReturnedValue)
begin condition
pre:
post:
end condition
declare function msg, get_property, set_property
declare subroutine extract_si_keys

$insert logical
$insert quote_equ
$insert msg_equates
$insert prod_spec_equ
$insert wo_log_equ

equ valid$         to 0    ;* successful
equ invalid_msg$   to 1    ;* bad data       -   print error message window
equ invalid_conv$  to 2    ;* bad conversion -          "         "
equ invalid_nomsg$ to 3    ;* bad but do not print the error message window
equ CrLf$          to char(13):char(10)

ReturnedValue = DataIo       
status() = Valid$
* WO, LOT_NUM, PO
begin case
    case Branch = 'WO'
        TextOut = 'Work Order number '
    case Branch = 'LOT_NUM'
        TextOut = 'Lot number '
    case Branch = 'PO'
        TextOut = 'Purchase Order number '
    case Branch = 'PART_NUM'
        TextOut = 'Part number.'
end case
PartsToIgnore = 'HOT LOT CHARGE~LOT CHARGE~SMALL LOT CHARGE~MATRIX LOT CHARGE~'
PartsToIgnore:= 'SET UP CHARGE~ENGINEERING LOT CHARGE~VOLUME ADJUSTMENT~'
PartsToIgnore:= 'SRP CHARGE~TENCOR/SURFSCAN CHARGE~SRP EPI CHARGE~'
PartsToIgnore:= 'DUTY CHARGES~FREIGHT CHARGES~RECIPE CHARGE'
LotsToIgnore = 'TEST & STRIPE~TEST WAFERS'
convert '~' to @fm in PartsToIgnore
convert '~' to @fm in LotsToIgnore
begin case
   case ConvType = 'ICONV'
      if Branch = 'PO' and DataIo = 'PROCUREMENT CARD' then
          ReturnedValue = DataIo
      end else
  	      CustNo = get_property( @window:'.CUST_NO', 'TEXT' )
			CurRow = get_property( @window:'.ORDER_DETAIL', 'SELPOS' )<2>
			OrderInfo = get_property( @window:'.ORDER_DETAIL', 'ARRAY' )
	      if Branch = 'PART_NUM' then
			     * NEW PN CHECK
              locate DataIo in PartsToIgnore using @fm setting PPos else
			        PSNId = OrderInfo<2,CurRow>
			        ValidPartNums = xlate( 'PROD_SPEC', PSNId, prod_spec_part_nums$, 'X' )
			        locate DataIo in ValidPartNums using @vm setting Ppos else
			           * not found, tell the entry person that the will need to 
			           * go and enter the part number into the PSN if it is valid
			           * cause maybe they made a mistake
			           MsgInfo = ''
			           MsgInfo<mtext$> = 'Part Number ':quote(DataIo):' was not found in PSN# ':PSNId:'.  If this is a valid part number, you will need to enter it into the PSN prior to entry here.'
			           MsgInfo<micon$> = 'H'
			           Void = msg( '', MsgInfo )
			           status() = invalid_nomsg$
			        end
		        end
	      end else
	        if Branch = 'LOT_NUM' and DataIo[1,2] = 'OK' then
	           * USING THE OKMETIC NUMBER FOR THE LOT NUMBER DUE TO WAFER INVENTORY
	           return 0
	        end
	        if Branch = 'LOT_NUM' then
	           locate DataIo in LotsToIgnore using @fm setting PPos then
	              return 0
	           end
	        end
		     extract_si_keys( 'ORDER', Branch, DataIo, Keys )
		     OrderNo = get_property( @window:'.ORDER_NO', 'TEXT' )
		     locate OrderNo in Keys using @vm setting Fpos then
		         keys = delete( Keys, 1, Fpos, 0 )
		     end
		     if Keys then
		         swap @vm with ', ' in Keys
		         MsgInfo = ''
		         MsgInfo<mtext$> = TextOut:' ':Quote(DataIo):' has been used in order number ':CrLf$:Keys
		         MsgInfo<micon$> = 'H'
		         Void = msg( '', MsgInfo )
		         if Branch = 'LOT_NUM' then
		            MsgInfo = ''
		            MsgInfo<mtext$> = 'You may duplicate this lot number for special cases.':CrLf$:'Please make sure that this is what you want to do,':CrLf$:'prior to saving this order.'
		            MsgInfo<micon$> = '!'
		            Void = msg( '', MsgInfo )
		         end else
		            status() = invalid_nomsg$
		         end
		         return 0
		     end else
		         ReturnedValue = DataIo
		     end
		     if Branch = 'WO' then
		        * VALIDATE THAT THE THIS WORK ORDER NUMBER'S PSN MATCH THE PSN ON THIS LINE
		        PSNId = OrderInfo<2,CurRow>
		        WORecPSNId = xlate( 'WO_LOG', DataIo, wo_log_prod_spec_id$, 'X' )
		        if ( WORecPSNId = PSNId ) else
		           * ERROR
		           MsgInfo = ''
		           MsgInfo<mtext$> = 'PSN ':PSNId:' does not belong to work order ':DataIo
		           MsgInfo<micon$> = 'H'
		           Void = msg( '', MsgInfo )
		           status() = invalid_nomsg$   
		        end       				  		           
		     end else
		        if Branch = 'SUB_PART_NUM' then
		           * VALIDATE THAT THE THIS WORK ORDER NUMBER'S PSN MATCH THE PSN ON THIS LINE
     		        PSNId = OrderInfo<2,CurRow>
     		        PSNSubPartNums = xlate( 'PROD_SPEC', PSNId, prod_spec_sub_part_nums$, 'X' )
     		        locate DataIo in PSNSubPartNums using @vm setting FPos else
			           * ERROR
			           MsgInfo = ''
			           MsgInfo<mtext$> = 'Substrate Part Number ':quote(DataIo):' was not found in PSN# ':PSNId:'.  If this is a valid substrate part number, you will need to enter it into the PSN prior to entry here.'
			           MsgInfo<micon$> = 'H'
			           Void = msg( '', MsgInfo )
			           status() = invalid_nomsg$
     		        end
		        end
		     end		        
		  end
	  end
   case ConvType = 'OCONV'
      * no output
      ReturnedValue = DataIo
   case otherwise$
      ReturnedValue = ''
      status() = invalid_conv$
end case
return
