*** Free Service
*** 1st 30 days from sale dt.
*** 2nd 70 days from sale dt.
*** 3rd 150 days from sale dt.
*** 4th 210 days from sale dt.
*** 5th 270 days from sale dt.
*** 6th 330 days from sale dt.

*** Regular 
*** 60 days after lastservice


do set_menu

private lnformat,lnresp,ldcur_date,lnkms1,lnkms2
store 00000 to lnkms1,lnkms2
ldcur_date = date()
lnformat = 1
lnresp = 1
do alert with '!!! ATTENTION !!!;This option will generate reminder letter;for current date;Do you want to generate reminder or take print out ?',1,'\<Take Print Out;\<Generate Reminder;Exit',lnresp
do case
	case lnresp = 2
		lctmp_wind = sys(2015)
		define wind (lctmp_wind) from 0,0 to 10,70
		acti wind (lctmp_wind)
		move wind (lctmp_wind) cent
		@ 3,3 say 'Current Date : ' get ldcur_date 
		read
		rele wind (lctmp_wind) 
		do genremind
	case lnresp = 1
		private lctmp_wind
		lctmp_wind = sys(2015)
		defi wind (lctmp_wind) from 0,0 to 10,70 pane
		acti wind (lctmp_wind) 
		move wind (lctmp_wind) cent
		@ 2,2 say 'Which Format : ' get lnformat pict '@^ Letter;List' size 1,10
		read
		rele wind (lctmp_wind) 
		if lnformat = 1
			= get_pdt('REMINDER LETTERS')
		else
			= get_pdt('REMINDER LIST')
		endif
		do prinremind
endcase
deac wind wait
do cls_menu


procedure prinremind

sele client
set orde to client
if lnformat = 1
	do wait_msg
*	repo form remi_ltr to (out_file) noco for betw(reminderdt,date1,date2)	
	do prinrepo with 'REMI_LTR','REMI_LTR','betw(reminderdt,date1,date2)','','' 
	deac wind wait
*	do out_repo
else
	do wait_msg
*	repo form remindlst to (out_file) noco for betw(reminderdt,date1,date2)
	do prinrepo with 'REMINDLS','REMINDLS','betw(reminderdt,date1,date2)','','' 
	deac wind wait	
*	do out_repo
endif	

procedure genremind

do wait_msg
clea
sele client
scan for lstservedt > reminderdt and ! empt(reminderdt) and status = 'R'
	repl reminderdt with {  /  /  }
ends
scan for status = 'F' 
	do case
		case serviceseq = '05'
			if sale_dt + 330 < ldcur_date and reminderdt < sale_dt + 330
				repl reminderdt with {  /  /  }
			endif
		case serviceseq = '04'
			if sale_dt + 270 < ldcur_date and reminderdt < sale_dt + 270
				repl reminderdt with {  /  /  }
			endif	
		case serviceseq = '03'
			if sale_dt + 210 < ldcur_date and reminderdt < sale_dt + 210
				repl reminderdt with {  /  /  }
			endif	
		case serviceseq = '02'
			if sale_dt + 150 < ldcur_date and reminderdt < sale_dt + 150
				repl reminderdt with {  /  /  }
			endif	
		case serviceseq = '01'
			if sale_dt + 70  < ldcur_date and reminderdt < sale_dt + 70 
				repl reminderdt with {  /  /  }
			endif	
		case val(serviceseq) = 0
			if sale_dt + 30 < ldcur_date and reminderdt < sale_dt + 30
				repl reminderdt with {  /  /  }
			endif	
	endc
ends
do remindfree 
do regular
deac wind wait

procedure remindfree

parameter pndays,pcserviceseq,pcreminder

do wait_msg
sele client
set orde to client
scan for empt(reminderdt) and status = 'F'
	do case
		case serviceseq = '05'
			if sale_dt + 330 < ldcur_date 
				repl reminderdt with ldcur_date
			endif
		case serviceseq = '04'
			if sale_dt + 270 < ldcur_date and reminderdt < sale_dt + 270
				repl reminderdt with ldcur_date
			endif	
		case serviceseq = '03'
			if sale_dt + 210 < ldcur_date and reminderdt < sale_dt + 210
				repl reminderdt with ldcur_date
			endif	
		case serviceseq = '02'
			if sale_dt + 150 < ldcur_date and reminderdt < sale_dt + 150
				repl reminderdt with ldcur_date
			endif	
		case serviceseq = '01'
			if sale_dt + 70  < ldcur_date and reminderdt < sale_dt + 70 
				repl reminderdt with ldcur_date
			endif	
		case val(serviceseq) = 0
			if sale_dt + 30 < ldcur_date and reminderdt < sale_dt + 30
				repl reminderdt with ldcur_date
			endif	
	endc
ends


procedure regular

do wait_msg
sele client
set orde to client
scan for (lstservedt + 60 < ldcur_date) and empt(reminderdt) and status = 'R'
	repl reminderdt with ldcur_date
ends


procedure remind

parameter pcreminder,days

if ! reminder_loop
	retu
endif	
do wait_msg
sele job_mast
set orde to client_no desc
sele client
set orde to lstservedt
loca for empt(iif(pcreminder = '1',reminder1,iif(pcreminder = '2',reminder2,reminder3))) and empt(job_status) and lstservedt + days <= sys_date and ! (VAL(serviceseq) = 0 and status = 'F') and status <> 'A'
*loca for empt(iif(pcreminder = '1',reminder1,iif(pcreminder = '2',reminder2,reminder3))) and empt(job_status) and betw(lstservedt + days,date1,date2) and ! (VAL(serviceseq) = 0 and status = 'F') and status <> 'A'
if ! found()
	retu
endif	
set rela to client_no into job_mast addi
reminder_no = 'reminder' + pcreminder
reminder_loop = .T.
SCAN for empt(iif(pcreminder = '1',reminder1,iif(pcreminder = '2',reminder2,reminder3))) and empt(job_status) and lstservedt + days <= sys_date and ! (VAL(serviceseq) = 0 and status = 'F')  and status <> 'A' while reminder_loop 
*SCAN for empt(iif(pcreminder = '1',reminder1,iif(pcreminder = '2',reminder2,reminder3))) and empt(job_status) and betw(lstservedt + days,date1,date2) and ! (VAL(serviceseq) = 0 and status = 'F')  and status <> 'A' while reminder_loop 
	do prin_ltr
	deac wind wait	
ENDS
set rela off into job_mast
sele job_mast
set orde to client_no asce

procedure prin_ltr

if lnformat = 2
	repl client.reminderdt with ldcur_date
	inse into reminder ;
		(reminderdt,client_no,reminderno,status,lstservedt) ;
	values ;
		(ldcur_date,client.client_no,'1',client.status,client.lstservedt)
	retu
endif
do case
	case inli(status,'P','R')
		do regularpara
	case status = 'F'
		do cli_due with serviceseq,lnkms1,lnkms2 in reminder
		do freepara
endc

if printstatus()
	if rloc('CLIENT')
		do case
			case pcreminder = '1'
				repl client.REMINDER1 with 'Y'
			case pcreminder = '2'
				repl client.REMINDER2 with 'Y'
			case pcreminder = '3'
				repl client.REMINDER3 with 'Y'
		endc
		unlo in client
	endif
	m.client_no = client.client_no
	m.reminderdt = sys_date
	m.reminderno = pcreminder
	m.status = client.status
	m.lstservedt = client.lstservedt
	inse into reminder from memv
else
	private resp
	resp = 1
	do alert with '!!! ATTENTION !!!;Printer is found OFF LINE;What Shoud I Do ?',1,'\!Stop Printing;\<Reprint Last One;\<Continue',resp
	do case
		case resp = 1
			reminder_loop = .F.
		case resp = 3
			if rloc('CLIENT')
				do case
					case pcreminder = '1'
						repl client.REMINDER1 with 'Y'
					case pcreminder = '2'
						repl client.REMINDER2 with 'Y'
					case pcreminder = '3'
						repl client.REMINDER3 with 'Y'
				endc
				unlo in client
			endif
		m.client_no = client.client_no
		m.reminderdt = sys_date
		m.reminderno = pcreminder
		m.status = client.status
		m.lstservedt = client.lstservedt
		inse into reminder from memv
	endc
endif

procedure regularpara

if status = 'P'
if val(serviceseq) + 1 = tot_serve
endif
else
endif

procedure freepara

repl lastremind with pcserviceseq ;
	reminderdt with ldcur_date

procedure cli_due

parameter pcserviceseq,pnkms1,pnkms2

private lcmodel_no,lccuralia
lcmodel_no = ' ' 
lccuralia = alia()
if profseek(client.modelcolno,'MODELCOL','MODELCOLNO')
	lcmodel_no = modelcol.model_no
endif
private lcseq
lcseq =  val(pcserviceseq) + 1
lcseq = padl(allt(str(lcseq)),2,'0')
sele freeserv
loca for modelNo = lcmodel_no and serve_seq = lcseq
if found()
	pnkms1 = frm_kms
	pnkms2 = to_kms
else
	store 0 to pnkms1,pnkms2
endif
sele (lccuralia)