COMPILE ROUTINE TEST_shipment(Dummy)
ROWDEF(CHARSTR)

DECLARE SUBROUTINE obj_Notes, Set_Status, Msg, obj_Tables, ErrMsg, Send_Info
DECLARE FUNCTION Get_Status, obj_Tables, Msg


$INSERT ORDER_EQU
$INSERT ORDER_DET_EQU
$INSERT COC_EQU
$INSERT WO_LOG_EQU
$INSERT COMPANY_EQU
$INSERT RDS_EQU

debug

FOR ShipNo = 52393 TO 54084

	GOSUB TestCode
	Send_Info(ShipNo)
NEXT ShipNo

GOTO Bail




* * * * * * * *
TestCode:
* * * * * * * *

Result = ''



IF ShipNo	= '' THEN
	ErrorMsg = 'Null parameter "ShipNo" passed to routine. (':Method:')' 
	RETURN
END

OtParms = 'COC':@RM:ShipNo

ShipRec = obj_Tables('ReadOnlyRec',OtParms)		;* Change this back to 'ReadRec' when done with development ******************

IF Get_Status(errCode)	THEN RETURN
IF ShipRec = '' 		THEN RETURN


SendDtm 	= ICONV(OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS'),'DT')
SendUser	= @USER4

WorkOrderNo	= ShipRec<COC_WO$>
WORec		= XLATE('WO_LOG',WorkOrderNo,'','X')

OrderNo			= WORec<WO_LOG_ORDER_NO$>
WOCustNo		= WORec<WO_LOG_CUST_NO$>
OrderItemNos	= WORec<WO_LOG_ORDER_ITEM$>

OrderDetKeys = ''

FOR I = 1 TO COUNT(OrderItemNos,@VM) + (OrderItemNos NE '')
	OrderDetKeys<1,I> = OrderNo:'*':OrderItemNos<1,I>			;* All of Order Item Keys on the Work Order
NEXT I

Captive		= XLATE('COMPANY',WOCustNo,COMPANY_CAPTIVE$,'X')
Consignment = XLATE('COMPANY',WOCustNo,COMPANY_CONSIGNMENT$,'X')

BEGIN CASE
	CASE Captive		; Ordertype = 'CAPTIVE'
	CASE Consignment	; OrderType = 'CAPTIVE'					;* Vision doesn't know about consignment orders yet
	CASE 1				; OrderType = 'MERCHANT'
END CASE


* Build the Order Line Item Detail from RDS records using local methods

OrderItemShippedQtys = ''
OrderItemRejQtys = ''
OrderItemRDSNos = ''

RDSNos = ShipRec<COC_RDS_NO$>
FOR I = 1 TO COUNT(RDSNos,@VM) + (RDSNos NE '')
	RDSNo		= RDSNos<1,I>
	RDSRec		= XLATE('RDS',RDSNo,'','X')
	Rejects		= XLATE('RDS',RDSNo,'TOT_REJ','X')
	WfrsOut		= XLATE('RDS',RDSNo,'WFRS_OUT','X')
	TWProd 		= RDSRec<RDS_TW_PROD$>
	OrderItem	= RDSRec<RDS_ORDER_ITEM$>
	
	IF Captive OR Consignment THEN
		Rejects = 0
	END ELSE
		Rejects += TWProd
	END
	
	LOCATE OrderItem IN OrderItemNos USING @VM SETTING Pos THEN
		OrderItemShippedQtys<1,Pos> = OrderItemShippedQtys<1,Pos> + WfrsOut
		OrderItemRejQtys<1,Pos> = OrderItemRejQtys<1,Pos> + Rejects
		OrderItemRDSNos<1,Pos,-1> = RDSNo
	END
NEXT I

OrderRec	= XLATE('ORDER',OrderNo,'','X')

Company 		= '001'
Warehouse		= 'ME1'


WorkOrderItem	= '0'
CustPONo		= OrderRec<ORDER_PO_NO$>					;* Doctor this up to include the Release No

CustPOLineNo	= OrderRec<ORDER_PO_LINE$>
ShipToCustNo	= OrderRec<ORDER_SHIP_TO_CUST_NO$>
BillToCustNo	= OrderRec<ORDER_BILL_TO_CUST_NO$>


TotalWafersOrdered = SUM(XLATE('ORDER_DET',OrderDetKeys,'WAFER_QTY','X'))

TotalShipWfrQty		= XLATE('COC',ShipNo,'TOT_QTY','X')									;* Total Good Wafers on Shipment
TotalShipRejQty		= XLATE('COC',ShipNo,'TOT_REJ','X')									;* Total Reject Wafers in Shipment
TotalShipProdQty	= XLATE('COC',ShipNo,'TOT_PROD','X')

TotalShipmentWafers = TotalShipWfrQty + TotalShipRejQty + TotalShipProdQty				;* Total wafers in this shipment

OrderItemPartTypes	= XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_VISION_PART_TYPE$,'X')	;* Part Types on this Work Order
PrevShipNos			= XLATE('ORDER_DET',OrderDetKeys,ORDER_DET_SHIP_NO$,'X')			;* All shipments on work order

LOCATE ShipNo IN PrevShipNos USING @VM SETTING Pos THEN
	PrevShipNos = DELETE(PrevShipNos,1,Pos,0)											;* Remove current shipment from list
END


TotPrevShipQtys = SUM(XLATE('COC',PrevShipNos,'TOT_WAFERS_SHIPPED','X'))				;* All wafers shipped on previous shipments against this Work Order

ResultLine = 0
* Now Load the Line Item specific stuff

IF INDEX(OrderItemPartTypes,'5',1) THEN

	* This Work Order has SRP and/or surcharges on it
	
	
	IF TotalShipmentWafers + TotPRevShipQtys >= TotalWafersOrdered THEN
	
		FOR I = 1 TO COUNT(OrderItemPartTypes,@VM) + (OrderItemPartTypes NE '')
		
			IF OrderItemPartTypes<1,I> = '5' THEN
				
				OrderItemKey = OrderDetKeys<1,I>
				OrderDetRec = XLATE('ORDER_DET',OrderItemKey,'','X')
				
				Canceled = (OrderDetRec<ORDER_DET_UNIT_PRICE$> = 0 AND OrderDetRec<ORDER_DET_ITEM_QTY$> = 0)
				
				IF NOT(Canceled) THEN
					ResultLine += 1
					Result<1,ResultLine> = OrderItemNos<1,I>
					Result<2,ResultLine> = ''									;* Wafer Quantities
					Result<3,ResultLine> = '1'						
					Result<4,ResultLine> = OrderDetRec<ORDER_DET_UNIT_PRICE$>
				END	
			END
		NEXT I
		
	END	;* End of check for last shipment on order
END



FOR I = 1 TO COUNT(OrderItemNos,@VM) + (OrderItemNos NE '')

	IF OrderItemShippedQtys<1,I> NE '' OR OrderItemRejQtys<1,I> NE '' THEN
	
		OrderItemNo = OrderItemNos<1,I>

		OrderDetRec = XLATE('ORDER_DET',OrderNo:'*':OrderItemNo,'','X')
		
		VisionLineNo	= FMT(ABS(OrderDetRec<ORDER_DET_VISION_LINE_NO$>),'R(0)#3')
		EPIPartNo		= OrderDetRec<ORDER_DET_EPI_PN$>
		CustPartNo		= OrderDetRec<ORDER_DET_CUST_PN$>
		
		IF OrderDetRec<ORDER_DET_UNIT_PRICE$> = '' THEN
			UnitPrice = OCONV(0,'MD32')
		END ELSE
			UnitPrice = OCONV(OrderDetRec<ORDER_DET_UNIT_PRICE$>,'MD32')
		END
		
		QuoteNo			= OrderDetRec<ORDER_DET_QUOTE_NO$>
		CustReqDt		= OCONV(OrderDetRec<ORDER_DET_CUST_REQ_DT$>,'[VISION_DT_FORMAT]')
		PromiseDt 		= OCONV(OrderDetRec<ORDER_DET_PROMISE_DT$>,'[VISION_DT_FORMAT]')
		ShipLineShipQty = OrderItemShippedQtys<1,I>
		ShipLineRejQty	= 0
		ItemRDSNos		= OrderItemRDSNos<1,I>
		
		IF ShipLineShipQty NE '' THEN
			ResultLine += 1
			Result<1,ResultLine> = OrderItemNos<1,I>
			Result<2,ResultLine> = OrderItemShippedQtys<1,I>
			Result<3,ResultLine> = ''					
			Result<4,ResultLine> = OrderDetRec<ORDER_DET_UNIT_PRICE$>
		END
		
	END	;* End of check for Shipped or Reject Qtys
	
NEXT I

ShipRec<COC_ORDER_ITEM$>		= Result<1>
ShipRec<COC_ITEM_WFR_QTY$>		= Result<2>
ShipRec<COC_ITEM_MISC_QTY$>		= Result<3>
ShipRec<COC_ITEM_UNIT_PRICE$>	= Result<4>

otParms = FIELDSTORE(otParms,@RM,4,0,ShipRec)
obj_Tables('WriteRec',OtParms)

RETURN


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

RETURN

