FUNCTION getdelitime

PARAMETERS pnfrt,pcServiceTyp,pcCategory,pcramp_no,pddeli_dt,pcdeli_time

*!*	PARAMETERS pcjobcard_no,pdjob_dt,pddeli_dt,pcdeli_time,pnfrt

*!*	IF pdjob_dt < DATE()
*!*		DO attention WITH 'Job card date is less then current date' + 'Delivery time for Service will not be calculated automatically'
*!*		RETURN
*!*	ENDIF
*!*	DO attention WITH "Run " + CHR(13) + 'NET time //server name /set' + "command to syncronyze Time of NODE with SERVER when it logs in AUTOSOFT"
*!*	DO attention WITH 'Now check all Ramp on the basis of SERVICE type/category' + CHR(13) + 'Auto select ramp whose Available time & date is minimum and it is not locked'
*!*	IF current time is less THEN Lunch Hrs. check Cur. time + FRT > Lunch start hr.
*!*		IF so, add recess time
*!*	ENDIF
*!*	IF Cur. time + Frt + [Recess time] is greater THEN working hr.
*!*		check if there is Sunday on next day 
*!*			IF 1/2 day OR full day on sunday 
*!*				check it is not festival Holiday on sunday.
*!*			ELSE 
*!*				IF monday is holiday 
*!*					give date of tuseday. Remaining time from Start time.
*!*				ELSE 
*!*					give date on monday. Remaining time from Start time.
*!*				ENDIF 
*!*	ENDIF 

*!*	IF pdjob_dt < DATE()
*!*		DO attention WITH 'Job card date is less then current date' + 'Delivery time for Service will not be calculated automatically'
*!*		RETURN
*!*	ENDIF
PRIVATE lcServeType,lcRampFiltCondition,lcStartTime,lnTotalMinutes

DO case
	CASE pcCategory = 'ENGINE'
		lcServeType = 'ENGINE'
		lcRampFiltCondition = 'rampmASTER.engine'
	CASE pcCategory = 'ACCIDENT'
		lcServeType = 'ACCIDENT'
		lcRampFiltCondition = 'rampmASTER.accident'
	CASE pcServiceTyp = 'FREE'
		lcServeType = 'FREE'
		lcRampFiltCondition = 'rampmASTER.free'
	OTHERWISE 
		lcServeType = 'PAID'
		lcRampFiltCondition = 'rampmASTER.paid'
ENDCASE

IF FLOCK('RAMPMASTER')
	lcTmpCurTime = DATETIME()
	lcTime = LEFT(TIME(),5)
	lcStartTime = lcTime
	SELECT * ;
		FROM rampmASTER ;
		WHERE rampmASTER.lockupto < DATE() ;
			AND &lcRampFiltCondition ;
		ORDER BY max_date,max_time,ramp_no ASC  ;
		INTO CURSOR query
	IF EMPTY(RECCOUNT('query'))
		DO err_msg WITH 'NO RAMP IS AVAILABLE' + CHR(13) + 'DELIVERY TIME WILL NOT BE CALCULATED AUTOMATICALLY'
	ENDIF 	
	GO TOP IN QUERY
	lnUptoLunchMinute = (VAL(LEFT(query.Lunch_frm,2)) - VAL(LEFT(QUERY.WORKINGFrm,2)))*60
	lnUptoLunchMinute = lnUptoLunchMinute + (VAL(righ(query.Lunch_frm,2)) - VAL(righ(QUERY.WORKINGFrm,2)))
	ldStartDt = MAX(QUERY.MAX_DATE,DATE())

	IF QUERY.MAX_DATE <= DATE() AND QUERY.MAX_TIME <= lctime
		IF lctime > QUERY.WORKINGTO
			UNLOCK IN rampmaster
			DO err_msg WITH 'THis will go to Next Day as time of this RAMP is OVER for today'
			RETURN 
		ENDIF 
			lnminutes = MINUTE(lcTmpCurTime)
			lnhrs = HOUR(lcTmpCurTime)
			lnendhrs = lnhrs + int((lnminutes + pnFrt)/60)
			lnEndMinutes = MOD(lnminutes + pnFrt,60)
			lcEndTime = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
	*!*			MESSAGEBOX('Current time : ' + lcTime + CHR(13) + 'End Time : ' + lcEndTime)
	ELSE 
		lcStartTime = query.MAX_TIME
		lnminutes = VAL(RIGHT(query.MAX_TIME,2))
		lnhrs = VAL(left(query.MAX_TIME,2))
		lnendhrs = lnhrs + int((lnminutes + pnFrt)/60)
		lnEndMinutes = MOD(lnminutes + pnFrt,60)
		lcEndTime = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
	*!*		MESSAGEBOX('Current time : ' + lcTime + CHR(13) + 'End Time : ' + lcEndTime)
	ENDIF 
	lnTotalMinutes = pnFrt
	*!*	IF lcTime <= LUNCH_FRM AND lcEndTime > LUNCH_FRM
	IF lcTime <= LUNCH_FRM AND lcEndTime > LUNCH_FRM AND lcStartTime <> LUNCH_to 
		lnTotalMinutes = pnFrt + QUERY.LUNCH_MIN
	*!*		MESSAGEBOX('LUCH CASE')
		lnendhrs = lnhrs + int((lnminutes + pnFrt + QUERY.LUNCH_MIN)/60)
		lnEndMinutes = MOD(lnminutes + pnFrt + QUERY.LUNCH_MIN,60)
		lcEndTime = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
	*!*		MESSAGEBOX('Current time : ' + lcTime + CHR(13) + 'End Time : ' + lcEndTime)
	ENDIF
	IF lcEndTime >= QUERY.WORKINGTO
	*!*		MESSAGEBOX('c/f to NEXT DAY')
		IF DOW(DATE() + 1) = 1	&& SUNDAY
			DO CASE 
				CASE query.sundaytime = 'NO'
					lndaymin = (VAL(LEFT(QUERY.WORKINGTO,2)) - VAL(LEFT(lcStartTime,2)))*60    && Hrs Converted In Min
					lndaymin = lndaymin + (VAL(righ(QUERY.WORKINGTO,2)) - VAL(righ(lcStartTime,2)))
	*!*					MESSAGEBOX('Minutes used in Same Day = ' + STR(lndaymin) + CHR(13) + 'out of ' + STR(lnTotalMinutes) + ' minutes')
					pddeli_dt = ldStartDt + 2 
					lnendhrs = VAL(LEFT(query.workingFrm,2)) ;
					           + int( (VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin)/60)
					lnEndMinutes = MOD(VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin,60)
					pcdeli_time = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
				CASE query.sundaytime = 'YES'
					lndaymin = (VAL(LEFT(QUERY.WORKINGTO,2)) - VAL(LEFT(lcStartTime,2)))*60    && Hrs Converted In Min
					lndaymin = lndaymin + (VAL(righ(QUERY.WORKINGTO,2)) - VAL(righ(lcStartTime,2)))
					lntotRemainminForNextDay = lnTotalMinutes - lndaymin
					IF lntotRemainminForNextDay > lnUptoLunchMinute
						lnendhrs = VAL(LEFT(query.Lunch_to,2)) ;
						           + int( (VAL(RIGHT(query.Lunch_to,2)) + lntotRemainminForNextDay - lnUptoLunchMinute)/60)
						lnEndMinutes = MOD(VAL(RIGHT(query.Lunch_to,2)) + lntotRemainminForNextDay - lnUptoLunchMinute,60)
						pcdeli_time = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
					ELSE 
						lnendhrs = VAL(LEFT(query.workingFrm,2)) ;
						           + int( (VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin)/60)
						lnEndMinutes = MOD(VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin,60)
						pcdeli_time = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
					ENDIF 
	*!*					MESSAGEBOX('Minutes used in Same Day = ' + STR(lndaymin) + CHR(13) + 'out of ' + STR(lnTotalMinutes) + ' minutes')
					pddeli_dt = ldStartDt + 1 

				CASE query.sundaytime = 'UPTO LUNCH'
					lndaymin = (VAL(LEFT(QUERY.WORKINGTO,2)) - VAL(LEFT(lcStartTime,2)))*60    && Hrs Converted In Min
					lndaymin = lndaymin + (VAL(righ(QUERY.WORKINGTO,2)) - VAL(righ(lcStartTime,2)))
					lntotRemainminForNextDay = lnTotalMinutes - lndaymin
					lnSunDayMin = (VAL(LEFT(query.Lunch_frm,2)) - VAL(LEFT(QUERY.WORKINGFrm,2)))*60
					lnSunDayMin = lnSunDayMin + (VAL(righ(query.Lunch_frm,2)) - VAL(righ(QUERY.WORKINGFrm,2)))
	*!*					MESSAGEBOX('Minutes used in Same Day = ' + STR(lndaymin) + CHR(13) + 'out of ' + STR(lnTotalMinutes) + ' minutes' + 'Sunday min : ' + STR(lnSundaymin))
					IF lnSunDayMin >= lntotRemainminForNextDay
						pddeli_dt = ldStartDt + 1 
						lnendhrs = VAL(LEFT(query.workingFrm,2)) ;
						           + int( (VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin)/60)
						lnEndMinutes = MOD(VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin,60)
						pcdeli_time = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
					ELSE 
						lntotRemainminForNextDay = lntotRemainminForNextDay - lnSunDayMin
	*!*						MESSAGEBOX('Minutes Remain for Monday is : ' + STR(lntotRemainminForNextDay))
						pddeli_dt = ldStartDt + 2 
						lnendhrs = VAL(LEFT(query.workingFrm,2)) ;
						           + int( (VAL(RIGHT(query.workingFrm,2)) + lntotRemainminForNextDay)/60)
						lnEndMinutes = MOD(VAL(RIGHT(query.workingFrm,2)) + lntotRemainminForNextDay,60)
						pcdeli_time = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
					ENDIF 			
			ENDCASE 
		ELSE 
			lndaymin = (VAL(LEFT(QUERY.WORKINGTO,2)) - VAL(LEFT(lcStartTime,2)))*60    && Hrs Converted In Min
			lndaymin = lndaymin + (VAL(righ(QUERY.WORKINGTO,2)) - VAL(righ(lcStartTime,2)))
	*!*			MESSAGEBOX('Minutes used in Same Day = ' + STR(lndaymin) + CHR(13) + 'out of ' + STR(lnTotalMinutes) + ' minutes')
			pddeli_dt = ldStartDt + 1 
			lnendhrs = VAL(LEFT(query.workingFrm,2)) ;
			           + int( (VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin)/60)
			lnEndMinutes = MOD(VAL(RIGHT(query.workingFrm,2)) + lnTotalMinutes - lndaymin,60)
			pcdeli_time = PADL(LTRIM(STR(lnEndHrs)),2,'0') + ':' + PADL(LTRIM(STR(lnEndMinutes)),2,'0')
		ENDIF 
	ELSE 
		pcdeli_time = lcEndTime
		pddeli_dt = ldStartDt
	ENDIF
	*!*	MESSAGEBOX('Deli Time : ' + pcdeli_time + CHR(13) + 'Date : ' + DTOC(pddeli_dt))
	SELECT RAMPMASTER
	LOCATE FOR RAMP_NO = QUERY.RAMP_NO
	IF FOUND()
		REPLACE max_date WITH pddeli_dt ;
			max_time WITH pcdeli_time
	ENDIF 
	UNLOCK IN rampmaster 
	pcramp_no = QUERY.RAMP_NO
	INSERT INTO ramphistory ;
		(ramp_no,jobcard_no,start_dt,start_hrs,end_dt,end_time) ;
	VALUES ;
		(pcramp_no,m.jobcard_no,ldStartDt,lcStartTime,pddeli_dt,pcdeli_time)
	RETURN 
ENDIF