COMPILE FUNCTION obj_Schedule(Method,Parms)

/*
	Methods for SCHEDULE table
    
	12/16/2003 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    AddInvoice(DataStruct)				;* Add Invoice information to Schedule	(from WRITE event of Invoice window)
	RemInvoice							;* Rem Invoice Reference from Schedule	(new to fix problems with garbage)
	AddOrderDetail(WOStepKey,SchedId)	;* Add WO_Step Key to Schedule 
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Send_Dyn

$INSERT MSG_EQUATES
$INSERT SCHEDULE_EQU
$INSERT RDS_EQU

ErrTitle = 'Error in Stored Procedure "obj_Schedule"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'Create'			; GOSUB Create
	CASE Method = 'AddInvoice'		; GOSUB AddInvoice
	CASE Method = 'RemInvoice'		; GOSUB RemInvoice
	CASE Method = 'AddOrderDetail'	; GOSUB AddOrderDetail
	CASE 1

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

RETURN Result


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

OrderNo		= Parms[1,@RM]
WONo		= Parms[COL2()+1,@RM]
LotNo		= Parms[COL2()+1,@RM]
PartNo		= Parms[COL2()+1,@RM]
LotQty		= Parms[COL2()+1,@RM]
CassCnt		= Parms[COL2()+1,@RM]
CassQty		= Parms[COL2()+1,@RM]

IF NOT(ASSIGNED(OrderNo))	THEN ErrorMsg = 'Unassigned Parm "OrderNo" passed to routine. (':Method:')'
IF NOT(ASSIGNED(WONo))		THEN ErrorMsg = 'Unassigned Parm "WONo" passed to routine. (':Method:')'
IF NOT(ASSIGNED(LotNo))		THEN ErrorMsg = 'Unassigned Parm "LotNo" passed to routine. (':Method:')'
IF NOT(ASSIGNED(PartNo))	THEN ErrorMsg = 'Unassigned Parm "PartNo" passed to routine. (':Method:')'
IF NOT(ASSIGNED(LotQty))	THEN ErrorMsg = 'Unassigned Parm "LotQty" passed to routine. (':Method:')'
IF NOT(ASSIGNED(CassCnt))	THEN ErrorMsg = 'Unassigned Parm "CassCnt" passed to routine. (':Method:')'
IF NOT(ASSIGNED(CassQty))	THEN ErrorMsg = 'Unassigned Parm "CassQty" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN
 
IF OrderNo = '' THEN ErrorMsg = 'Null Parameter "OrderNo" passed to routine. (':Method:')'
IF WONo = ''	THEN ErrorMsg = 'Null Parameter "WONo" passed to routine. (':Method:')'
IF LotNo = ''	THEN ErrorMsg = 'Null Parameter "OrderNo" passed to routine. (':Method:')'
IF PartNo = ''	THEN ErrorMsg = 'Null Parameter "PartNo" passed to rtouein. (':Method:')'
IF LotQty = ''	THEN ErrorMsg = 'Null Parameter "LotQty" passed to routine. (':Method:')'
IF CassCnt = ''	THEN ErrorMsg = 'Null Parameter "CassCnt" passed to routine. (':Method:')'
IF CassQty = ''	THEN ErrorMsg = 'Null Parameter "CassQty" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

SchedNo = NextKey('SCHEDULE')

SchedRec = ''
SchedRec<SCHEDULE_ORDER_NO$>	= OrderNo
SchedRec<SCHEDULE_WO$>			= WONo
SchedRec<SCHEDULE_LOT_NUM$>		= LotNo
SchedRec<SCHEDULE_PART_NUM$>	= PartNo
SchedRec<SCHEDULE_LOT_QTY$>		= LotQty
SchedRec<SCHEDULE_BOX_CNT$>		= CassCnt

RDSNos = ''
RunOrdNos = ''
WafersIn = ''

FOR I = 1 TO CassCnt
	RunOrdNos<1,I> = I
	RDSNos<1,I>	= ''
	
	IF CassQty NE '' THEN
		WafersIn<1,I> = CassQty
	END ELSE
		WafersIn<1,I> = ''
	END
	
NEXT I

SchedRec<SCHEDULE_RUN_ORDER_NUM$> = RunOrdNos
SchedRec<SCHEDULE_WAFERS_IN$> = WafersIn
SchedRec<SCHEDULE_RDS_IDS$> = RDSNos


SchedRec<SCHEDULE_ENTRY_ID$>	= @USER4
SchedRec<SCHEDULE_ENTRY_DATE$>	= Date()

obj_Tables('WriteRec','SCHEDULE':@RM:SchedNo:@RM:@RM:SchedRec)

IF Get_Status(errCode) THEN
	Result = ''
END ELSE
	Result = SchedNo
END

RETURN


* * * * * * *
AddInvoice:
* * * * * * *

InvNo		= Parms[1,@RM]
SchedKey	= Parms[COL2()+1,@RM]
SetupCharge	= Parms[COL2()+1,@RM]
SRPCharge	= Parms[COL2()+1,@RM]
ShipQty		= Parms[COL2()+1,@RM]
ShipPrice	= Parms[COL2()+1,@RM]
ShipVia		= Parms[COL2()+1,@RM]
Rejects		= Parms[COL2()+1,@RM]
Unprocessed	= Parms[COL2()+1,@RM]
BoxCnt		= Parms[COL2()+1,@RM]
TrackingNo	= Parms[COL2()+1,@RM]
ShipDt		= Parms[COL2()+1,@RM]

IF InvNo	= ''	THEN ErrorMsg = 'Null parameter InvNo passed to routine (':Method:').'
IF SchedKey = ''	THEN ErrorMsg = 'Null parameter SchedKey passed to routine (':Method:').'
*IF ShipQty	= ''	THEN ErrorMsg = 'Null parameter ShipQty passed to routine (':Method:').'
*IF ShipPrice = ''	THEN ErrorMsg = 'Null parameter ShipPrice passed to routine (':Method:').'
IF ShipDt = ''		THEN ErrorMsg = 'Null parameter ShipDt passed to routine (':Method:').'

IF ErrorMsg NE '' THEN RETURN

thisShipPrice = ICONV(ShipPrice,'MD2')
IF thisShipPrice = '' THEN
	*ErrorMsg = 'Invalid parameter ShipPrice ':QUOTE(ShipPrice):' passed to routine (':Method:').'
END

thisShipDt = ICONV(ShipDt,'D')
IF thisShipDt = '' THEN
	ErrorMsg = 'Invalid parameter ShipDt ':QUOTE(ShipDt):' passed to routine (':Method:').'
END

IF SetupCharge NE '' THEN
	thisSetupCharge = ICONV(SetupCharge,'MD2')
	IF thisSetupCharge = '' THEN
		ErrorMsg = 'Invalid parameter SetupCharge ':QUOTE(SetupCharge):' passed to routine (':Method:').'
	END
END ELSE
	thisSetupCharge = ''
END

IF SRPCharge NE '' THEN
	thisSRPCharge = ICONV(SRPCharge,'MD2')
	IF thisSRPCharge = '' THEN
		ErrorMsg = 'Invalid parameter SRPCharge ':QUOTE(SRPCharge):' passed to routine (':Method:').'
	END
END ELSE
	thisSRPCharge = ''
END

TableVar = ''
OtParms = 'SCHEDULE':@RM:SchedKey:@RM:TableVar 
SchedRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update
IF Get_Status(errCode) THEN Return

LOCATE InvNo IN SchedRec<schedule_invoice_ids$> BY 'AR' USING @VM SETTING Pos THEN

	* Update existing reference
	
	SchedRec<schedule_ship_setup_charge$,Pos>	= thisSetupCharge
	SchedRec<schedule_ship_srp_charge$,Pos>		= thisSRPCharge
	SchedRec<schedule_ship_qty$,Pos>			= ShipQty
	SchedRec<schedule_ship_price$,Pos>			= thisShipPrice
	SchedRec<schedule_ship_via$,Pos>			= ShipVia
	SchedRec<schedule_rejects$,Pos>				= Rejects
	SchedRec<schedule_unprocessed$,Pos>			= Unprocessed
	SchedRec<schedule_box_cnt$,Pos>				= BoxCnt
	SchedRec<schedule_tracking_no$,Pos>			= TrackingNo
	SchedRec<schedule_ship_date$,Pos>			= thisShipDt
	
END ELSE

	* Add new reference
	
	SchedRec = INSERT(SchedRec,schedule_invoice_ids$,Pos,0,InvNo)
	SchedRec = INSERT(SchedRec,schedule_ship_setup_charge$,Pos,0,thisSetupCharge)
	SchedRec = INSERT(SchedRec,schedule_ship_srp_charge$,Pos,0,thisSRPCharge)
	SchedRec = INSERT(SchedRec,schedule_ship_qty$,Pos,0,ShipQty)
	SchedRec = INSERT(SchedRec,schedule_ship_price$,Pos,0,thisShipPrice)
	SchedRec = INSERT(SchedRec,schedule_ship_via$,Pos,0,ShipVia)
	SchedRec = INSERT(SchedRec,schedule_rejects$,Pos,0,Rejects)
	SchedRec = INSERT(SchedRec,schedule_unprocessed$,Pos,0,Rejects)
	SchedRec = INSERT(SchedRec,schedule_box_cnt$,Pos,0,BoxCnt)
	SchedRec = INSERT(SchedRec,schedule_tracking_no$,Pos,0,TrackingNo)
	SchedRec = INSERT(SchedRec,schedule_ship_date$,Pos,0,thisShipDt)
	
END

OtParms = FieldStore(OtParms,@RM,4,1,SchedRec)

obj_Tables('WriteRec',OtParms)		;* Writes and unlocks the record

RETURN



* * * * * * *
RemInvoice:
* * * * * * *

InvNo		= Parms[1,@RM]
SchedKey	= Parms[COL2()+1,@RM]

IF InvNo	= ''	THEN ErrorMsg = 'Null parameter InvNo passed to routine (':Method:').'
IF SchedKey = ''	THEN ErrorMsg = 'Null parameter SchedKey passed to routine (':Method:').'

TableVar = ''
OtParms = 'SCHEDULE':@RM:SchedKey:@RM:TableVar 
SchedRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update
IF Get_Status(errCode) THEN Return

LOCATE InvNo IN SchedRec<schedule_invoice_ids$> BY 'AR' USING @VM SETTING Pos THEN

	* Update existing reference
	SchedRec = Delete(SchedRec,schedule_invoice_ids$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_ship_setup_charge$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_ship_srp_charge$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_ship_qty$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_ship_price$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_ship_via$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_rejects$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_unprocessed$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_box_cnt$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_tracking_no$,Pos,0)
	SchedRec = Delete(SchedRec,schedule_ship_date$,Pos,0)
	
END ELSE
	obj_Tables('UnlockRec',OtParms)
	RETURN		;* Invoice not on this Schedule, nothing to do
END

OtParms = FieldStore(OtParms,@RM,4,1,SchedRec)

obj_Tables('WriteRec',OtParms)		;* Writes and unlocks the record

RETURN


* * * * * * *
AddOrderDetail:
* * * * * * *

WOStepKey	= Parms[1,@RM]
SchedKey	= Parms[COL2()+1,@RM]

IF WOStepKey	= ''	THEN ErrorMsg = 'Null parameter WOStepKey passed to routine (':Method:').'
IF SchedKey		= ''	THEN ErrorMsg = 'Null parameter SchedKeys passed to routine (':Method:').'

IF ErrorMsg NE '' THEN RETURN

TableVar = ''

OtParms = 'SCHEDULE':@RM:SchedKey:@RM:TableVar 
SchedRec = obj_Tables('ReadRec',OtParms)			;* Locks and reads record for update
IF Get_Status(errCode) THEN Return

SchedRec<SCHEDULE_WO_STEP_KEY$> = WOStepKey
RDSKeys = SchedRec<SCHEDULE_RDS_IDS$>

OtParms = FieldStore(OtParms,@RM,4,1,SchedRec)

obj_Tables('WriteRec',OtParms)		;* Writes and unlocks the record

FOR I = 1 TO COUNT(RDSKeys,@VM) + (RDSKeys NE '')
	RDSKey = RDSKeys<1,I>
	CurrWOStepKey = XLATE('RDS',RDSKey,RDS_WO_STEP_KEY$,'X')
	IF CurrWOStepKey NE WOStepKey THEN
		OPEN 'RDS' TO RDSTable THEN
			READ RDSRec FROM RDSTable,RDSKey THEN
				RDSRec<RDS_WO_STEP_KEY$> = WOStepKey
				WRITE RDSRec ON RDSTable,RDSKey THEN
					Send_Dyn('RDS ':RDSKey:' Updated')
				END
			END
		END
	END
NEXT I

RETURN


