'DLY_ROUT - end-of-day routines at Shops; replaces (& includes) DLY_CASH
'30/12/04   UninvoicedOrders() rem'd out unless needed - uninvord.dfr
'           Despat_2() rem'd out - not used - despat_2.dfr
'           ListDespatches() rem'd out - not used - despat_2.dfr

external   vkeybox() sch scw progress() messbox() vloadif() cpath PrintReport()
external   vunloadif() dpath fentrybox() chkdate() remove() areas greeting
external   base findpopup() Background() messboxwait() addidxrec() makeidx()
external   ipath fgp bgp shopmask navrecs() chkstr() strcount() getidxrecs()
external   bpopdb() messline() wraptext() nr6 $drive bge gdsoutpath
external   posncolpopup() colpoplines() fm_busdate() colpopup() $menu userid
external   _shade() #margin_A #margin_B #margin_C #margin_D fgi bgi
external   posnpopup() reqnpath dly_pstgpath progtag() popuplist()
external   X_path _SWIP_Crystal() Xreppath

public     ptstr choice sum_date br_name psa dsa $branch ftgdate ftgd1 #banking
public     invtot custcode abbrv_name ptval jobnr $refnr a_jobnr $shop $footing
public     $dateftr $ftrcode ptary[1] $screen $shop_name ftrname ftrcode
public     ftgperiod $delcode indate monthend fullmonth briefmonth enddate $base $basenames #basenames

global     x ReturnToMenu() i s1 s2 s3 s4 s5 s6 s7 $place leftjob p Job_Locn() ChooseShops2Select()
global     $type $jobnrs #jobnrs FittingDate() $chkdate dl PUL1() PUL2()
global     p2 p3 p4 p5 p6 #int PrintUnrecvdList() ImmtFittingDate() DailyPDQ() p1 Place_Paid() $locn $file
global     Gds_Rcvd() $prec NewIndex() FittedDate() l1 l2 $dfa1 $ftrappts #ftrappts $fitter $day_1 $popstr df clb
global     ShowDeliveries() ReplaceHardSpace() ftrarray[1] ShowOrders() clf  PrintOS_BACS_Bals()
global     Show1stCash() #1stbalance #1strecord Titles_1() $showdel $invoice
global     ShowStatus() ShowAllReqs() S_details #count WriteDetails() invdate
global     jobdesc ftgcomm ftginstr ftgscrn y z slotrec y1 y2 y3 y4
' global     ShowInstructions() $ctgplan ftginit #lenareas #startc #startr
global     ShowInstructions() ftginit #lenareas #startc #startr
global     $instruct EnterInstructions() #upper #rem m1 m5 NavReqns() S_status
global     $delonly ftgplan ShowText() mess5 $ordstat $invdate NavMess_A()
global     custname navmess() bot psmode RecsScroll() orddate fmt
global     SetupDetails() GetCustDetails() ShowDetails() all
global     custaddr1 custaddr2 custcity custpostcode deladdr1 deladdr2 #inv
global     deladdr3 deladdr4 cust_title custcontact offtel hometel $partaddr
global     offax mobile custaddr deladdr k DespatRecs() SelectOver30days()
global     SearchAddress() ShowOrder() #prec CustScreenLine1() CustScreenLine2()
global     ReplaceCR() ShowFittings() varndes ShowReceipts() l #amount $invnr
global     $status SearchInvoice() ReplaceHardSpace2() S_all UndeldReqs()
global     ShowComments() BoxText() y2a y2b y2c y2d #margin #netinv #totcost
global     MarkDespatched() SelectDespatched() ShopImminent() ImmtFileTime() Despat_2()
global     #slotsrem AlterApptSlots() #apptslots ChooseJobs() despatidx $index BACS()
global     UninvoicedOrders() StoreReport() dlyrep ImmtReservation() OrdOvr30() $report_date


MAIN
single-step off
  	Background()
  	file unload all
  	p2 = ""   ' p2 = title at top of choice popup ("LABEL")
  	p3 = 1    ' p3 = printer to be used (1=HPIII_QC; 2=GEN_EPSN etc)
  	p5 = 1    ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  	p4 = 1

' message "L70\ base) is:"&str(base)
	if base="T"
		$place="Trade"
	else
	  	x=Place_Paid()
' message "x) is:"&str(x)
  		if x = -1
    			ReturnToMenu()
	  	end if
  	end if
' $place
  	br_name = $place
  	choice = left(br_name,1)             				'message "choice is:"&str(choice)
  	$branch = choice				

  	x=DailyPDQ()									'L109

  	x=Gds_Rcvd()									'L219
  	if x = -1
'    	ReturnToMenu()
  	end if

'   ListDespatches()

  	x=Despat_2()									'L1877

  	x=ShopImminent()								'L1715

  	x=ImmtReservation()								'L2010
' 													execute "immt_rsv.rf3" in-memory
	x=OrdOvr30()

	x=BACS()

  	ReturnToMenu()

END MAIN


FUNCTION DailyPDQ()     ' show keybox() listing each shop (& ALL)
local br_dir repdate cashrep
  	br_name = $place
  	choice = left(br_name,1)             'message "choice is:"&str(choice)
  	vloadif(dpath|"cshrcvd1.vw")
  	order change physical
  	if areas ! upper(choice)
    		while true
      		x=fentrybox(" Confirm Date for PDQ Summary ",10,"##\/##\/####",today)
      		if x=0
        			sum_date = ptstr
        			if chkdate(sum_date,1) = -1
          			messbox(" Incorrect date - re-enter ",0,0,1)
          			continue while
        			end if
        			exit while
      		elseif x = -1
        			return (-1)
      		end if
    		end while
    		progress(15,10," Please wait ... preparing report ",0)
    		data query execute "totPDQ1.dfq" index "totPDQ2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Banked]=blank and [Branch] = choice
'   and
'   [Method_Of_Payment]="PDQ"
'   and
'   [Abbrv_M] <> "O"
'   and
'   not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    		if cerror
      		messboxwait(" No PDQ payments to summarise ",0,0,1)
      		return (1)
    		end if
    		#banking=filesum([Amount_Received],[Abbrv_M]="C") 'message "#banking is:"&str(#banking)
    		order sort now dictionary "totpdq.idx" fields "[PDQtype]" ascending
    		while true
      		vunloadif("X_cshrcd.vws")
      		remove(X_path|"X_cshrcd.*")
      		data query execute "not_del.dfq" Smart4 X_path|"X_cshrcd" fields "[Customer_Code|Br_KA]"
      		vunloadif("X_cshrcd.vws")
      		_SWIP_Crystal(Xreppath|"tot_pdq1","S",0,1,"") ' draft report
'       p1 = "totPDQd.dfr"                ' draft PDQ printout
'       p6 = 1    ' p6 = nr of copies 'message " nr of copies"
'       PrintReport(p1,p2,p3,p4,p5,p6)
      		vloadif(dpath|"cshrcvd1.vw")
      		messbox(" Has the draft PDQ summary printed correctly? (y/n) ",1,1,1)
      		if ptstr == "y"                      '  store file & exit
        			for i = 1 to records
          			lock-record                        ' update record with inv nr & date
            				[Banked]=today
	          		write-record
     	     		data goto record next
        			end for
        			_SWIP_Crystal(Xreppath|"tot_pdq2","S",0,2,"") ' final report
        			p1 = "totPDQr.dfr"                ' reconciled printout
     	   		repdate = left(sum_date,2)|mid(sum_date,4,2)|right(sum_date,2) 'message "repdate) is:"&str(repdate)
	        		br_dir=case br_name ("Sheen","SHEEN")("Raynes","RAYNES")("Fulham","FULHAM")("Putney","PUTNEY")("HeadÿOffice","HEAD") 'message "br_dir is:"&str(br_dir)
        			cashrep = $drive|"REPORTS\BANKINGS\"|br_dir|"\"|repdate|".PDQ" 'message "cashrep) is:"&str(cashrep)
        			print report execute p1 disk cashrep detail start 1 end 0 copies 1
     	   		exit while
	      	else
        			messbox(" Abandon and print later? (y/n) ",1,1,1)
        			if ptstr == "y"                      '  store file & exit
     	     		return (-1)
	        		else
          			continue while
        			end if
      		end if
    		end while
  else
    messbox(greeting|", you do not have access rights for this report ",0,0,1)
  end if

  screen clear box 1 1 sch scw 0 0 no-border
' if shop selection is not included in "areas" on oldpurch.vws refuse access
' if "areas" allow 2 areas, then only one report to be done at any one time (DG excepted)
' print report listing all cash rec'd for selected shop(s)
END FUNCTION ' Daily_PDQ()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION Place_Paid()
local s1 s2 s3 s4 s5 s6 s7 $locn
  	$locn = case base ("R","Raynes")("S","Sheen")("T","Trade")("P","Putney")("F","Fulham") else "HeadÿOffice"
  	s1 = "Fulham"
  	s2 = "HeadÿOffice"
  	s3 = "Raynes"
  	s4 = "Sheen"
  	s5 = "Trade"
  	s7 = "Putney"
  	while true
    		x = findpopup(8,60,15,s1&s5&s7&s2&s3&s4,$locn,"Place",1,0)
    		if x = -1
      		return (-1)
    		end if
    		$place = ptstr                     'message "$place is:"&str($place)
    		return (0)
  	end while
END FUNCTION 'Place_Paid()


FUNCTION Gds_Rcvd()
' message "L220\ $branch) is:"&str($branch)
  	x=messbox(" Print Goods Received Report? (y/n) ",1,1,0)
	if x = -1
      	return (-1)
    	end if
  	if ptstr == "n"
    		return (1)
  	end if
  	p1 = "dly_gds1.dfr"          ' p1 = report definition ("ord_stck.dfr")
  	vloadif(dpath|"dlygoods.vw")
  	progress(15,10," Please wait ... preparing report ",0)
  	remove("goods1.idx")
  	makeidx("gds_rcvd","goods1.idx","0",1)
  	data goto record last
  	while true
    		if days([Date_Received])=days(sum_date)
      		$prec = str(precord)
      		x=addidxrec("goods1.idx",$prec,7)  'message "x is:"&str(x)
      		if x<>0
        			messboxwait(" not added to index ",0,0,1)
      		end if
    		elseif (deleted)
    		elseif days([Date_Received])<days(sum_date)
      		exit while
    		end if
    		data goto record previous
  	end while
  	order change index "goods1.idx"
  	if records=0
    		messboxwait(" No Goods Received to list ",0,0,1)
    		return (0)
  	end if
  	data query execute "dly_gds1.dfq" index "goods2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Branch]=$branch
'   OR
'   [Branch]="X"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		messboxwait(" No Goods Received to list ",0,0,1)
    		return (0)
  	end if
  	data query execute "dly_gds3.dfq" index "goods4.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Branch]=$branch
'   OR
'   [Item_Type]="C"
'   OR
'   [Item_Type]="S"
'   OR
'   [Item_Type]="V"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		messboxwait(" No Goods Received to list ",0,0,1)
    		return (0)
  	end if
  	order sort now dictionary "dlygoods.idx" fields "[Order_Nr]" ascending

  	vunloadif("X_dlygds.vws")
  	remove(X_path|"X_dlygds.*")
  	data query execute "not_del.dfq" Smart4 X_path|"X_dlygds" fields "[Date_Received|sumdate]"
  	_SWIP_Crystal(Xreppath|"dly_gds1","S",0,1,"")
'   p6 = 1    ' p6 = nr of copies
'   PrintReport(p1,p2,p3,p4,p5,p6)
END FUNCTION 'Gds_Rcvd()


FUNCTION UninvoicedOrders()
  progress(15,10," Please wait ... looking for uninvoiced orders ",0)
  vloadif(dpath|"aged_drs.vw")
  if file(ipath|$branch|"_uninv.idx")=1
    order change index ipath|$branch|"_uninv.idx"
  else
    messbox(" No uninvoiced orders ",0,0,1)
    return (0)
  end if
  order sort now "uninvd_2.idx" fields "[Date_Of_Order]" ascending
  screen clear box 1 1 sch scw 0 0 no-border
  PrintReport("uninvord.dfr","Uninvoiced",p3,p4,p5,p6)
END FUNCTION ' UninvoicedOrders()


FUNCTION AlterApptSlots()
local  #newapptslots #newslotsrem
  #newapptslots = #apptslots - #slotsrem ' message "#newapptslots) is:"&str(#newapptslots)
' write to CUST_ORD
  #newslotsrem  = #slotsrem + (#newapptslots-#apptslots) 'message "#newslotsrem is:"&str(#newslotsrem)
  vloadif(dpath|"find_job.vw")
  order change key "[Job_Nr]"
  data find "[Job_Nr]" equal jobnr options "g"   '  find correct JOB
  if cerror
    messbox(" Job Nr NOT found ",0,0,1)
    vloadif(dpath|"despat_2.vw")
    return (-1)
  end if
  lock-record
    [Slots_Rem]  = #newslotsrem
    [Appt_Slots] = #newapptslots
  write-record
  vloadif(dpath|"despat_2.vw")
  return (0)
END FUNCTION ' AlterApptSlots()


' FUNCTION PrintJobList()
'   data query execute "despat_6.dfq" index "despat_3.idx"
' ' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ' ³ [Completed]="Y"
' ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
'   if cerror
'     return (0)
'   else
'     messbox(" Print list of orders just marked for invoicing? (y/n) ",1,1,1)
'     if ptstr == "y"
'       repaint off
'       p1 = "DESPAT_2.dfr"   ' p1 = report definition ("ord_stck.dfr")
'       PrintReport(p1,p2,p3,p4,p5,p6)
'       order change physical                ' release index
'     end if
'   end if
' END FUNCTION ' PrintJobList()


FUNCTION Titles_1()
local y1 y2 y3 y4 y5 y6 y7
  screen clear box 22 5 23 75 0 0 no-border
  y5 = format("Receipts - press any key to continue","M71")
  y6 = format("   Date     Amount  Method      Notes           Ent'd   Time  Posted","L71")
  screen print 9 5 fgp bgp y6
  screen print 22 5 fgp bgp y5
END FUNCTION ' Titles()


FUNCTION GetCustDetails()
  $showdel     = [PDA]
  cust_title   = [Title]
  custaddr1    = [Address_1]
  custaddr2    = [Address_2]
  custcity     = [City/Town]
  custpostcode = [Postcode]
  deladdr1     = [Delivery_Address_1]
  deladdr2     = [Delivery_Address_2]
  deladdr3     = [Del_City]
  deladdr4     = [Del_Postcode]
  custcode     = [Customer_Code]
  custname     = [Customer_Name]
  custcontact  = [Contact_Name]
  offtel       = [Office_Tel]
  hometel      = [Home_Tel]
  offax        = [Office_Fax]
  mobile       = [Mobile/Other_Nr]
  $status = case [Credit_Status] ("A","Account") ("C","Cash only") ("D","Delayed") ("N","No deliveries!!")
END FUNCTION ' GetCustDetails()


FUNCTION ShowCustomer()
local c1 c2 c3 c4 d1 d2 d3 d4 cc1 cr1 cc2 cr2 dc1 dr1 dc2 dr2 pc1 pr1 pc2 pr2 \
      ordets p1 p2 p3 p4 p5 p6 p7 p8 df
  cr1 = 8
  cc1 = 2
  cr2 = cr1+5
  cc2 = cc1+37
  dr1 = cr1
  dc1 = 42
  dr2 = cr2
  dc2 = dc1+37
  pr1 = cr2+1
  pc1 = cc1+14
  pr2 = pr1+8
  pc2 = pc1+50
  if $showdel = "Y"
    df  = clf
  else
    df  = 8
  end if

  screen clear box cr1 cc1 cr2 cc2 clf clb
  c1 = left(custaddr1|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c2 = left(custaddr2|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c3 = left(custcity|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c4 = left(custpostcode|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  screen print cr1 cc1+1 clf clb "ÿCustomer addressÿ(Acc Nr"&custcode|")ÿ"
  screen print cr1+1 cc1+2 clf clb c1
  screen print cr1+2 cc1+2 clf clb c2
  screen print cr1+3 cc1+2 clf clb c3
  screen print cr1+4 cc1+2 clf clb c4
  screen save cr1 cc1 cr2 cc2 custaddr
  screen clear box dr1 dc1 dr2 dc2 df clb
  d1 = left(deladdr1|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d2 = left(deladdr2|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d3 = left(deladdr3|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d4 = left(deladdr4|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  screen print dr1 dc1+1 df clb "ÿDelivery addressÿ"
  screen print dr1+1 dc1+2 df clb d1
  screen print dr1+2 dc1+2 df clb d2
  screen print dr1+3 dc1+2 df clb d3
  screen print dr1+4 dc1+2 df clb d4
  screen save dr1 dc1 dr2 dc2 deladdr
  if $showdel = "N"
    screen print dr2 dc1+1 df clb "ÿNot to be shown on invoiceÿ"
  end if
  screen save dr1 dc1 dr2 dc2 deladdr

  screen clear box pr1 pc1 pr2 pc2 clf clb
  p1 = format("Title:  ÿ   "|left(cust_title|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p2 = format("Name:     ÿ "|left(custname|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p3 = format("Contact:   ÿ"|left(custcontact|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p4 = format("Office tel:ÿ"|left(offtel|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p5 = format("Home tel:  ÿ"|left(hometel|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p6 = format("Fax nr:ÿÿ  ÿ"|left(offax|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p7 = format("Mobile nr: ÿ"|left(mobile|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p8 = " press any key to continue ... "
  screen print pr1 pc1+1 clf clb "ÿACCOUNT STATUS:"&$status|"ÿ"
  screen print pr1+1 pc1+2 clf clb p1
  screen print pr1+2 pc1+2 clf clb p2
  screen print pr1+3 pc1+2 clf clb p3
  screen print pr1+4 pc1+2 clf clb p4
  screen print pr1+5 pc1+2 clf clb p5
  screen print pr1+6 pc1+2 clf clb p6
  screen print pr1+7 pc1+2 clf clb p7
  screen print pr1+8 pc1+19 clf clb p8
  inchar
  screen clear box 7 1 sch 80 0 0 no-border
END FUNCTION ' ShowCustomer()


FUNCTION ShowFittings2()
local $ftgstr $fname d
  $ftgstr = ""
  repaint off
  vloadif(dpath|"ftr_list.vws")
  vloadif(dpath|"appntmnt.vws")
  order change physical
  data query execute "job_reqn.dfq" index "job_reqn.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr and not (deleted)                      ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No appointments for this job ",0,0,1)
    return (1)
  end if
  $dfa1 = ""
  $ftrappts = ""

'--------------------------------------search to remove duplicate days
  if records > 1
    data goto record first
    for i = 1 to records
      $dfa1 = left([DFA],11)
      x = chkstr($dfa1,$ftrappts)
      if x = -1
        $ftrappts = $ftrappts&$dfa1
      end if
      data goto record next
    end for
    x=strcount($ftrappts)                'message "x is:"&str(x)
    #ftrappts = ptval                    '
  else
    $ftrappts = left([DFA],11)
    #ftrappts = 1                   '
  end if
  redimension ftrarray[#ftrappts]
  for i = 1 to #ftrappts
    d = days(left(group($ftrappts,i),5))
    $day_1 = left(date2([Date]),6)|right(date2([Date]),2)'message "$day_1 is:"&str($day_1)
    $fitter = right(group($ftrappts,i),6) 'message "$fitter) is:"&str($fitter)
    $fname = filelookup([FTR_LIST.Fitter_Code],[FTR_LIST.Nickname],$fitter)
    ftrname = ReplaceHardSpace2($fname,len($fname))
    $ftgstr = $ftgstr&$day_1|"ÿ"|left(ftrname|"ÿÿÿÿÿÿÿÿ",8)     ' HARD space
    ftrarray[i] = $fitter
  end for
  while true
    x = posncolpopup(4,5,20,$ftgstr,"",1,0,clf,clb,0,7,0)  'highlight date & press {Enter} to show deliveries
    $dateftr = ptstr
    if x = -1
      repaint off
      exit while
    end if
    screen shortrestore dsa
    $ftrcode = ftrarray[ptval]
    ShowDeliveries()
  end while

  vunloadif("appntmnt.vws")
  vunloadif("delivr_1.vw")
  vunloadif("ftr_list.vws")

END FUNCTION ' ShowFittings2()


FUNCTION ShowOrders()
local $ordstr
  $ordstr = ""
  repaint off
  vloadif(dpath|"variat_n.vws")
  order change physical
  data query execute "job_reqn.dfq" index "job_reqn.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr and not (deleted)                      ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" Job Nr not found ",0,0,1)
    return (1)
  end if
  for i = 1 to records
    #amount=right("ÿÿÿÿÿÿÿÿÿÿ"|currency([Amount_Gross]),10)
    varndes = ReplaceHardSpace2(date2([Date])&format([Reason],"L50")&#amount,70)
    $ordstr = $ordstr&varndes          'message "$popstr is:"&str($popstr)
    data goto record next
  end for
  vunloadif("variat_n.vws")
  while true
    x = colpoplines(10,5,23,$ordstr,"",1,0,clf,clb,14,3)  'highlight date & press {Enter} to show deliveries
    if x = -1
      exit while
    end if
    screen shortrestore dsa
  end while
END FUNCTION ' ShowOrders()


FUNCTION ShowReceipts()
local rcptdes $rcpstr
  $rcpstr = ""
  repaint off
  ptval=0
  vloadif(dpath|"os_rcpts.vw")
  order change key "[Job_Nr]"
  data query execute "vu_cash" index  "#1stcash.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
'   [Job_Nr] = jobnr
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
  if cerror
    messbox(" No cash received for this order ",0,0,1)
    vunloadif("os_rcpts.vw")
    return (0)
  end if
  for i = 1 to records
    #amount=right("ÿÿÿÿÿÿÿÿÿÿÿÿ"|currency([Amount_Received]),12)
    rcptdes = ReplaceHardSpace(date2([Date_Of_Receipt])&format([Method_Of_Payment],"L6")&[Entered_By]&format([Authorisation],"L20")&#amount)
    $rcpstr = $rcpstr&rcptdes          'message "$popstr is:"&str($popstr)
    data goto record next
  end for
  vunloadif("os_rcpts.vw")
  x = colpoplines(10,5,20,$rcpstr,"",1,0,clf,clb,14,3)  'highlight date & press {Enter} to show deliveries
  screen shortrestore dsa
  inchar
END FUNCTION ' ShowReceipts()


FUNCTION WriteDetails()         ' write customer & job details to CUSENT3B
  jobdesc  = ReplaceCR(jobdesc)        'message "jobdesc) is:"&str(jobdesc)
  ftginstr = ReplaceCR(ftginstr)       'message "ftginstr) is:"&str(ftginstr)
  ftgcomm  = ReplaceCR(ftgcomm)        'message "ftgcomm) is:"&str(ftgcomm)
  lock-record
    [Description]     = jobdesc
    [Instructions]    = ftginstr
    [Fitting_Comment] = ftgcomm
    [Last_Update]     = today
    [Updated_By]      = userid
  write-record
END FUNCTION ' WriteDetails()


FUNCTION ShowText()
  x=wraptext(9,14,13,66,15,1,jobdesc,"L",1,0,0)  '   message "x is:"&str(x)
  mess5 = "Job details"|@if(slotrec=0,""," - (timeslots"&str(slotrec)|")")
  screen print  9 16 15 1 mess5

  x = wraptext(14,14,17,66,15,1,ftginstr,"L",1,0,0)
  screen print 14 16 15 1 "Other comments (eg appointment times etc)"

  x = wraptext(18,14,21,66,15,1,ftgcomm,"L",1,0,0)
  screen print 18 16 15 1 "Comments re Fitting"
END FUNCTION ' ShowText()


FUNCTION ShowStatus()
local mess1 r1 r2 c1 c2 cl1 cl2 i
  r1 = 5
  r2 = r1+3
  c1 = 60
  c2 = c1+20
  if $invoice = "Y"
    cl1 = 15
    cl2 = 10
    screen clear box r1 c1 r2 c2 cl1 cl2
    screen print r1+1 c1+5 cl1 cl2 "ÿÿINVOICED"
    screen print r1+2 c1+2 cl1 cl2 $invnr|" - "|date2($invdate)

  else
    mess1 = case $ordstat ("A","Initial only")("U","All details held")\
  ("R","To be authorised")("H","Held for check")("V","Rejected")("P","Authorised")\
  ("L","Being prepared")("D","Despatched") else "Missing"
    if $ordstat = "A"
      cl1 = 14
      cl2 = 0
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
      screen print r1+2 c1+5 cl1 cl2 mess1
    elseif $ordstat = "U"
      cl1 = 14
      cl2 = 0
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
      screen print r1+2 c1+3 cl1 cl2 mess1
    elseif $ordstat = "R"
      cl1 = 10
      cl2 = 0
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
      screen print r1+2 c1+3 cl1 cl2 mess1
    elseif $ordstat = "V"
      for i = 1 to 15
        cl1 = 4
        cl2 = 0
        screen clear box r1 c1 r2 c2 cl1 cl2
        screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
        screen print r1+2 c1+7 cl1 cl2 mess1
        wait .1
        cl1 = 0
        cl2 = 4
        screen clear box r1 c1 r2 c2 cl1 cl2
        screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
        screen print r1+2 c1+7 cl1 cl2 mess1
        wait .1
      end for
    elseif $ordstat = "H"
      for i = 1 to 15
        cl1 = 4
        cl2 = 0
        screen clear box r1 c1 r2 c2 cl1 cl2
        screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
        screen print r1+2 c1+4 cl1 cl2 mess1
        wait .1
        cl1 = 0
        cl2 = 4
        screen clear box r1 c1 r2 c2 cl1 cl2
        screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
        screen print r1+2 c1+4 cl1 cl2 mess1
        wait .1
      end for
    elseif $ordstat = "P"
      cl1 = 10
      cl2 = 0
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
      screen print r1+2 c1+6 cl1 cl2 mess1
    elseif $ordstat = "L"
      cl1 = 4
      cl2 = 0
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
      screen print r1+2 c1+4 cl1 cl2 mess1
    elseif $ordstat = "D"
      cl1 = 10
      cl2 = 0
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+5 cl1 cl2 "ORDER STATUS"
      screen print r1+2 c1+6 cl1 cl2 mess1
    end if
  end if
  screen save r1 c1 r2 c2 S_status
END FUNCTION ' ShowStatus()


FUNCTION ReplaceCR(str1)
local j r m bw l_last #addn l
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = "~"
      r = ""                          ' replace soft space
    end if
    m = m|r
  end for
  return (m)
END FUNCTION ' ReplaceCR()


FUNCTION RecsScroll()
local x bot psmode
  screen save scrheight 1 scrheight scrwidth bot
  smartpeek $_spndmes psmode
  if psmode = 1
    smartpoke $_spndmes 0
  end if
  while TRUE
    NavMess_A()
    x = inchar
    if x = {Down}
      data goto record next
      NavMess_A()

    elseif x = {Up}
      data goto record previous
      NavMess_A()

    elseif x = {PgDn}
      data goto page next
      NavMess_A()

    elseif x = {PgUp}
      data goto page previous
      NavMess_A()

    elseif x = {^End}
      data goto record last
      NavMess_A()

    elseif x = {^Home}
      data goto record first
      NavMess_A()

    elseif x = {Home}
      suspendone
      keys Home,F8
      screen shortrestore bot
      NavMess_A()

    elseif x = {End}
      suspendone
      keys End,F8
      screen shortrestore bot
      NavMess_A()

    else
      exit while
    end if
  end while
  if psmode = 1
    smartpoke $_spndmes 1
  end if
  return (x)
END FUNCTION ' RecsScroll()


FUNCTION NavMess_A()
local ordstat mess1 col1 mess2
' message "[Completed] is:"&str([Completed])

  col1 = 12
  ftgdate = [Fitting_Date]             'message "ftgdate) is:"&date2(ftgdate)
  ordstat = [Order_Status]             ' message "ordstat) is:"&str(ordstat)
  jobnr   = [Job_Nr]                   ' message "jobnr) is:"&str(jobnr)
  mess1   =  format("Job Nr"&[Job_Nr]&"- Ftg Date:"&format(@if([Fitting_Date]=blank,"NONE!",date2([Fitting_Date])),"L8")&"- Status:"&[$case],"M71")
  if [Invoice_Total] < 0
    mess2 =  format("Credit note for:"&currency(abs([Invoice_Total]))&"- balance o/s:"&currency([Balance_Due]),"M71")
  else
    mess2 =  format("Order value:"&currency([Invoice_Total])&"- balance o/s:"&format(currency([Balance_Due]),"R8"),"M71")
  end if
  screen print 19 6 15 col1 mess1
END FUNCTION   'NavMess_A()


FUNCTION ShowDetails()
local c1 c2 c3 c4 d1 d2 d3 d4 cc1 cr1 cc2 cr2 dc1 dr1 dc2 dr2 pc1 pr1 pc2 pr2 \
      ordets p1 p2 p3 p4 p5 p6 p7 p8 clf clb df

  clb = 13
  clf = 10
  cr1 = 8
  cc1 = 2
  cr2 = cr1+5
  cc2 = cc1+37
  dr1 = cr1
  dc1 = 42
  dr2 = cr2
  dc2 = dc1+37
  pr1 = cr2+1
  pc1 = cc1+14
  pr2 = pr1+8
  pc2 = pc1+50
  if $showdel = "Y"
    df  = clf
  else
    df  = 8
  end if
  screen clear box cr1 cc1 cr2 cc2 clf clb
  c1 = left(custaddr1|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c2 = left(custaddr2|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c3 = left(custcity|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c4 = left(custpostcode|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  screen print cr1 cc1+1 clf clb "ÿCustomer's addressÿ"
  screen print cr1+1 cc1+2 clf clb c1
  screen print cr1+2 cc1+2 clf clb c2
  screen print cr1+3 cc1+2 clf clb c3
  screen print cr1+4 cc1+2 clf clb c4
  screen save cr1 cc1 cr2 cc2 custaddr
  screen clear box dr1 dc1 dr2 dc2 df clb
  d1 = left(deladdr1|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d2 = left(deladdr2|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d3 = left(deladdr3|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d4 = left(deladdr4|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  screen print dr1 dc1+1 df clb "ÿDelivery addressÿ"
  screen print dr1+1 dc1+2 df clb d1
  screen print dr1+2 dc1+2 df clb d2
  screen print dr1+3 dc1+2 df clb d3
  screen print dr1+4 dc1+2 df clb d4
  screen save dr1 dc1 dr2 dc2 deladdr
  if $showdel = "N"
    screen print dr2 dc1+1 df clb "ÿNot to be shown on invoiceÿ"
  end if
  screen save dr1 dc1 dr2 dc2 deladdr

  screen clear box pr1 pc1 pr2 pc2 clf clb
  p1 = format("Title:  ÿ   "|left(cust_title|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p2 = format("Name:     ÿ "|left(custname|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p3 = format("Contact:   ÿ"|left(custcontact|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p4 = format("Office tel:ÿ"|left(offtel|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p5 = format("Home tel:  ÿ"|left(hometel|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p6 = format("Fax nr:ÿÿ  ÿ"|left(offax|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p7 = format("Mobile nr: ÿ"|left(mobile|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p8 = " press any key to continue ... "
  screen print pr1 pc1+1 clf clb "ÿACCOUNT STATUS:"&$status|"ÿ"
  screen print pr1+1 pc1+2 clf clb p1
  screen print pr1+2 pc1+2 clf clb p2
  screen print pr1+3 pc1+2 clf clb p3
  screen print pr1+4 pc1+2 clf clb p4
  screen print pr1+5 pc1+2 clf clb p5
  screen print pr1+6 pc1+2 clf clb p6
  screen print pr1+7 pc1+2 clf clb p7
  screen print pr1+8 pc1+19 clf clb p8
  inchar
  repaint off
END FUNCTION ' ShowDetails()


FUNCTION Show1stCash()
local $puar
  ptval=0
  repaint off
  vloadif(dpath|"os_rcpts.vw")
  order change key "[Job_Nr]"
  data query execute "vu_cash" index  "#1stcash.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
'   [Job_Nr] = jobnr
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
  if cerror
    messbox(" No cash received for this order ",0,0,1)
    return (-1)
  end if
  repaint on
  repaint
  screen shortrestore S_details
  Titles_1()
  inchar
  return (-1)
END FUNCTION ' Show1stCash()


FUNCTION SearchAddress()
  vloadif(dpath|"ordstat5.vw")
  data goto record first
  while true
    data find "[Delivery_Address_1]" partial $partaddr options "fi"
    if cerror
      data goto record first
      while true
        data find "[Delivery_Address_2]" partial $partaddr options "fi"
        if cerror
          messboxwait(" `"|$partaddr|"' not found in Customer Order's file ",0,0,1)
          Background()
          return (1)
        else
          #prec = precord
          x = ShowOrder()
          if x = 0
            return (0)     ' correct found & SEEN!
          elseif x = -1
            return (-1)     '
          end if
        end if
      end while

    else
      #prec = precord
      x = ShowOrder()
      if x = 0
        return (0)     ' correct found & SEEN!
      elseif x = -1
        return (-1)     '
      end if
    end if
  end while
END FUNCTION ' SearchAddress()


FUNCTION ShowOrder()
  vloadif(dpath|"ordstat4.vw")
  data goto record record-number #prec
  repaint on
  repaint
  x = messbox(" Is this the order? (y/n) ",1,1,0)
  if x = -1
    return (-1)
  else
    if ptstr == "y"
      jobnr = [Job_Nr]
      return (0)     ' correct found & SEEN!
    else
      repaint off
      progress(15,10," Searching for `"|$partaddr|"' ",0)
      vloadif(dpath|"ordstat5.vw")
      data goto record next
      return (1)
    end if
  end if
END FUNCTION ' ShowOrder()


FUNCTION ShowFittings()
local d
  $popstr = ""
  repaint off
  vloadif(dpath|"ftr_list.vws")
  vloadif(dpath|"appntmnt.vws")
  order change physical
  data query execute "job_reqn.dfq" index "job_reqn.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr and not (deleted)                      ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No appointments for this job ",0,0,1)
    return (1)
  end if
  $dfa1 = ""
  $ftrappts = ""

'--------------------------------------search to remove duplicate days
  if records > 1
    data goto record first
    for i = 1 to records
      $dfa1 = left([DFA],11)
      x = chkstr($dfa1,$ftrappts)
      if x = -1
        $ftrappts = $ftrappts&$dfa1
      end if
      data goto record next
    end for
    x=strcount($ftrappts)                'message "x is:"&str(x)
    #ftrappts = ptval                    '
  else
    $ftrappts = left([DFA],11)
    #ftrappts = 1                   '
  end if
  redimension ftrarray[#ftrappts]
  for i = 1 to #ftrappts
    d = days(left(group($ftrappts,i),5))
    $day_1 = left(date2([Date]),6)|right(date2([Date]),2) 'message "$day_1 is:"&str($day_1)
    $fitter = right(group($ftrappts,i),6) 'message "$fitter) is:"&str($fitter)
'     $fname = filelookup([FTR_LIST.Fitter_Code],[FTR_LIST.Nickname],$fitter)
    ftrname = ReplaceHardSpace(filelookup([FTR_LIST.Fitter_Code],[FTR_LIST.Nickname],$fitter))
    $popstr = $popstr&$day_1|"ÿ"|left(ftrname|"ÿÿÿÿÿÿÿÿ",8)     ' HARD space
    ftrarray[i] = $fitter
  end for

  while true
    x = posncolpopup(5,10,20,$popstr,"",1,0,12,13,0,7,0)  'highlight date & press {Enter} to show deliveries
    $dateftr = ptstr
    if x = -1
      repaint off
      exit while
    end if
    screen shortrestore dsa
    $ftrcode = ftrarray[ptval]
    ShowDeliveries()
  end while
  vloadif(dpath|"appntmnt.vws")
  order change physical
END FUNCTION ' ShowFittings()


FUNCTION ReplaceHardSpace(str1)
local j r m bw l_last #addn
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = " "
      r = "ÿ"                          ' replace hard space
    end if
    m = m|r
  end for
  m = m|repeat("ÿ",#addn)
  return (m)
END FUNCTION ' ReplaceHardSpace()


FUNCTION ReplaceHardSpace2(str1,bw)
local j r m l_last #addn
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = " "
      r = "ÿ"                          ' replace hard space
    end if
    m = m|r
  end for

  if len(m) < bw
    #addn = bw-len(m)
  else
    #addn = mod(len(m),bw)
  end if
  m = m|repeat("ÿ",#addn)
  return (m)
END FUNCTION ' ReplaceHardSpace2()


FUNCTION ShowComments()
  x=wraptext(8,14,12,66,clf,clb,jobdesc,"L",1,0,0)  '   message "x is:"&str(x)
  mess5 = "Job details"|@if(slotrec=0,""," - (timeslots"&str(slotrec)|")")
  screen print  8 16 clf clb mess5
  x = wraptext(13,14,16,66,clf,clb,ftginstr,"L",1,0,0)
  screen print 13 16 clf clb "Other comments (eg appointment times etc)"
  x = wraptext(17,14,20,66,clf,clb,ftgcomm,"L",1,0,0)
  screen print 17 16 clf clb "Comments re Fitting"
  ShowInstructions()
  screen clear box 7 1 sch scw 0 0 no-border
END FUNCTION ' ShowComments()


FUNCTION ShowAllReqs()
local y2 y1 y3 y4
  progress(15,10," Please wait ... finding requisitions ",0)
  repaint off
  vloadif(dpath|"gds_rcvd.vws")
  vloadif(dpath|"supplier.vws")
  vloadif(dpath|"purchord.vws")
  vloadif(dpath|"all_reqn.vw")
  order change key "[JobNr]"
  data query execute "all_reqn.dfq" index "all_req1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    screen shortrestore psa
    x = remove("all_reqn.idx")                ' create temp index for allocation
    x = makeidx("requsn","all_reqn.idx","0",1)
    if x = -1
      message "makeidx() failed"
    end if
    messboxwait(" NO requisitions entered yet ",0,0,1)
    return (1)
  else                               ' order by ListOrder & prodMRC
    order sort now dictionary "all_reqn.idx" fields "[Lst_Stck;Product_MRC]" ascending
    #count = records
    repaint on
    repaint
    screen shortrestore S_all
  end if
  ptval=0
  while true
    ptval = NavReqns()
    if ptval = {Esc}
      repaint off
      vunloadif("gds_rcvd.vws")
      vunloadif("supplier.vws")
      vunloadif("purchord.vws")
      vunloadif("all_reqn.vw")
      screen clear box 7 1 sch scw 0 0 no-border
      return (-1)
    end if
  end while
  repaint off
END FUNCTION ' ShowAllReqs()


FUNCTION ShowInstructions()
local mess2
  if $ordstat <> "L" and $ordstat <> "D"
    fgp = clf
    bgp = clb
    x = messline(" Update Instructions? (y/n) ",1,1,1,21,14,53)
    fgp = fgpleasing
    bgp = bgpleasing
    if x = 0
      if ptstr == "y"
        x = EnterInstructions()          ' return 0 - success
        return (1)
      else                             ' repaint & restore top of screen
        repaint off
        return (1)
      end if
    end if
  else
    messboxwait(" Instructions cannot be altered - already passed/delivered ",0,0,1)
    return (1)
  end if
END FUNCTION ' ShowInstructions()


FUNCTION EnterInstructions()
local l #lenareas #startc #startr
  error off
  while true
    y = format("Press F10 to finish","M53")
    screen print 21 14 15 1 y
    x=wraptext(8,14,12,66,7,1,jobdesc,"L",1,0,0)  '   message "x is:"&str(x)
    mess5 = "Job details"|@if(slotrec=0,""," - (timeslots"&str(slotrec)|")")
    screen print  8 16 7 1 mess5

    x = wraptext(17,14,20,66,7,1,ftgcomm,"L",1,0,0)
    screen print 17 16 7 1 "Comments re Fitting"

    while true
      #lenareas = len(ftginstr)        'message "#lenareas) is:"&str(#lenareas)
      #startc = mod(#lenareas,50)
      #startr = int(#lenareas/50)+1
      screen editor 13 14 16 66 15 1 "Other comments (eg appointment times etc)" VARIABLE ftginstr ftginstr\
      MAX 2 50 START #startr #startc OPTIONS "" 0 0 1 RW_MODE
      smartpeek $_lastkey z
      if z <> {F10}
        messbox(" Must use {F10} to save record!! ",0,0,1)
        continue while
      else
        exit while
      end if
    end while

    x = wraptext(13,14,16,66,7,1,ftginstr,"L",1,0,0)
    screen print 13 16 7 1 "Other comments (eg appointment times etc)"
    while true
      #lenareas = len(ftgcomm)         ' message "#lenareas) is:"&str(#lenareas)
      #startc = mod(#lenareas,50)
      #startr = int(#lenareas/50)+1
      screen editor 17 14 20 66 15 1 "Comments re Fitting" VARIABLE ftgcomm ftgcomm\
      MAX 2 50 START #startr #startc OPTIONS "" 0 0 1 RW_MODE
      smartpeek $_lastkey z
      if z <> {F10}
        messbox(" Must use {F10} to save record!! ",0,0,1)
        continue while
      else
        exit while
      end if
    end while

    messline(" Confirm correct and continue? (y/n) ",1,1,1,22,14,53)
    if ptstr == "y"
      if len(ftginstr) = 0
        messbox(" Must enter instructions ",0,0,1)
        continue while
      else
        vloadif(dpath|"ordstat4.vw")
        order change key "[Job_Nr]"
        data find "[Job_Nr]" equal jobnr options ""
        WriteDetails()
        vunloadif("ordstat4.vw")
        return (0)
      end if
    else
      continue while
    end if
  end while
END FUNCTION ' EnterInstructions()


FUNCTION SearchInvoice()
  vloadif(dpath|"ordstat4.vw")
  order change key "[Inv_Nr]"
  while true
    data find "[Inv_Nr]" equal $invnr options ""
    if cerror
      messboxwait(" Invoice Nr"&$invnr&"not found ",0,0,1)
      return (1)
    else
      jobnr = [Job_Nr]
      return (0)     ' correct found & SEEN!
    end if
  end while
END FUNCTION ' SearchInvoice()


FUNCTION NavReqns()
local x psmode pd pq #percentmargin
'  #m #c
  if $menu == "boss"
    #totcost = filesum([Cost])
    #margin  = (#netinv-#totcost)/#netinv
    #percentmargin = round(#margin*100,2) 'message "#percentmargin is:"&str(#percentmargin)
    y3 = format("ÿ"|chr(24)&chr(25)&"req'ns ("|str(#count)|")    {P}urchase details   {Esc} to exit    (Margin"&fixed(#percentmargin,1)|"%"|")","M78")
    if #margin < #margin_A
      screen print 9 2 12 1 y3
    elseif #margin < #margin_B
      screen print 9 2 14 1 y3
    elseif #margin < #margin_C
      screen print 9 2 10 1 y3
    elseif #margin < #margin_D
      screen print 9 2 14 1 y3
    else
      screen print 9 2 12 1 y3
    end if
  else
    y3 = format("ÿ"|chr(24)&chr(25)&"req'ns ("|str(#count)|")    {P}urchase details   {Esc} to exit ","M78")
    screen print 9 2 15 1 y3
  end if
  screen save scrheight 1 scrheight scrwidth bot
  smartpeek $_spndmes psmode
  if psmode = 1
    smartpoke $_spndmes 0
  end if
  while TRUE
    x = inchar
    if x = {Down}
      data goto record next

    elseif x = {Up}
      data goto record previous

    elseif x = {PgDn}
      data goto page next

    elseif x = {PgUp}
      data goto page previous

    elseif x = {^End}
      data goto record last

    elseif x = {^Home}
      data goto record first

    elseif x = {P} or x = {p}
      NavMess()

    else
      exit while
    end if
  end while
  if psmode = 1
    smartpoke $_spndmes 1
  end if
  return (x)
END FUNCTION   'NavReqns()


FUNCTION BoxText(r1,c1,r2,c2,fg,bg,jst,sprn,sml,pg)
local dr a b $line fmt
  b = dr
  clear psa
  screen save r1 c1 r2 c2 psa
  screen clear box r1 c1 r2 c2 fg bg
  screen print (r1+1) c1+2 fg bg format fmt y2
  screen print (r1+2) c1+2 fg bg format fmt y2a
  screen print (r1+3) c1+2 fg bg format fmt y2c
  screen print (r1+4) c1+2 fg bg format fmt y2b
  screen print (r1+5) c1+2 fg bg format fmt y2d
  screen save r1 c1 r2 c2 dsa
  redimension ptary[1]
  ptval = a
  return (0)
END FUNCTION   'BoxText(r1,c1,r2,c2,fg,bg,ts,jst,sprn,sml,pg)


FUNCTION ShowDeliveries()
local y6
  progress(15,10," Please wait ... finding deliveries for"&jobnr,0)
  fgp = clf
  bgp = clb
  repaint off
  vloadif(dpath|"delivr_1.vw")
  order change key "[Job_Nr]"
  data query execute "job_reqn.dfq" index "sd1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Job_Nr] = jobnr
'   and
'   not(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    screen shortrestore psa
    messboxwait(" NO deliveries booked ",0,0,1)
    fgp = fgpleasing
    bgp = bgpleasing
    return (1)
  end if
  data query execute "showdel1.dfq" index "sd2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Date_Out] = date2(left($dateftr,8))
'   and
'   [Fitter_Code]=$ftrcode
'   and
'   not(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    screen shortrestore psa
    messboxwait(" NO deliveries booked for"&date2($dateftr),0,0,1)
    fgp = fgpleasing
    bgp = bgpleasing
    return (1)
  end if
  screen shortrestore psa
  y6 = format(" Deliveries not yet booked are shown in brackets ","M55")
  screen print 6+records 26 fgp bgp y6
  x = bpopdb("delivr_1",6,"","[ScrollView]","L53","[Quant_OS]","R6","[Job_Nr]",4,26,20,80,"",0)
  screen clear box 6+records 26 6+records 80 0 0 no-border
  fgp = fgpleasing
  bgp = bgpleasing
END FUNCTION ' ShowDeliveries()


FUNCTION NavMess()
local col1 pd pq pr psc psn psr psp pq1 pinv piv pss dld
  if [Item_Type] = "B" or [Item_Type]="W" or [Item_Type]="J" or [Item_Type]="T" or [Item_Type]="O"
    pr = [Date_Allocated]              'message "pr is:"&date2(pr)
    $refnr = [Reference_Nr]            'message "$refnr is:"&str($refnr)
    error off
    pd = filelookup([purchord.Order_Nr],[purchord.Date_Ordered],$refnr) 'message "pd is:"&str(pd)
    if cerror
      messboxwait(" Reference NOT found in PURCHORD.DB ",0,0,1)
      return (1)
    else
      pq  = filelookup([purchord.Order_Nr],[purchord.Delivery_Quoted],$refnr)
      pq1 = @if(pq=null,"N/Q",pq)    'message "pq1 is:"&str(pq1)
      psc = filelookup([purchord.Order_Nr],[purchord.Supplier_Code],$refnr)
      psr = filelookup([purchord.Order_Nr],[purchord.Order_Reference],$refnr)
      pss = filelookup([purchord.Order_Nr],[purchord.Del],$refnr)
      psn = filelookup([supplier.Supplier_Code],[supplier.Name],psc)
      psp = filelookup([supplier.Supplier_Code],[supplier.Telephone],psc)
      dld = case pss ("W","Warehouse")("C","Clapham")("F","Fulham")("R","Raynes")("P","Putney")("S","Sheen")("O","Collected")("I","On Site")
      if value(pr)=0 ' NOT received
        y2 = format("Ordered on"&date2(pd)&"- for delivery:"&pq1,fmt)
        y2a = format(left("From:"&psn&"-"&psp,66),fmt)
        y2c = format(left("Ref:"|psr,66),fmt)   'message "len(y2a) is:"&str(len(y2a))
        y2b = format("",fmt)   'message "y2 is:"&str(y2)
        y2d = format(left("Deliver to:"&dld,66),fmt)   'message "y2d is:"&str(len(y2a))
      else
        y2a = format(left("From:"&psn&"-"&psp,66),fmt)
        y2c = format(left("Ref:"|psr,66),fmt)   'message "len(y2a) is:"&str(len(y2a))
        y2d = format(left("Delivered to:"&dld,66),fmt)   'message "y2d is:"&str(len(y2a))
        pinv= filelookup([gds_rcvd.Order_Nr],[gds_rcvd.Invoice_Nr],$refnr)
        if $menu == "offc" or $menu == "boss"
          piv = filelookup([gds_rcvd.Order_Nr],[gds_rcvd.Invoice_Cost],$refnr)
          y2b = format(left(@if(pinv=blank,"","Invoice amount"&currency(piv)),65),fmt)
        else
          y2b = format("",fmt)   'message "y2 is:"&str(y2)
        end if
        y2  = format(left(@if(pinv=blank,"Order received on"&date2(pr)&"(Invoice not rec'd)","Order received on"&date2(pr)|"ÿ(Inv Nr"&pinv|")"),65),fmt)
      end if
      col1 = 1
    end if
    if BoxText(1,7,7,73,10,5,"L",1,0,0) = 0
      wait 7
      screen shortrestore psa
    end if
  else
    return (1)
  end if
END FUNCTION   'NavMess()


FUNCTION UndeldReqs()
local y6
  progress(15,10," Please wait ... finding deliveries ",0)
  fgp = clf
  bgp = clb
  repaint off
  vloadif(dpath|"delivr_1.vw")
  order change key "[Job_Nr]"
  data query execute "job_reqn.dfq" index "sd1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Job_Nr] = jobnr
'   and
'   not(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    screen shortrestore psa
    messboxwait(" NO undelivered requisitions ",0,0,1)
    fgp = fgpleasing
    bgp = bgpleasing
    return (1)
  end if
  data query execute "undelivd.dfq" index "ud2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Quant_OS]>.01
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    screen shortrestore psa
    messboxwait(" NO undelivered requisitions ",0,0,1)
    fgp = fgpleasing
    bgp = bgpleasing
    return (1)
  end if
  screen shortrestore psa
  y6 = format(" Deliveries not yet booked are shown in brackets ","M55")
  screen print 6+records 26 fgp bgp y6
  x = bpopdb("delivr_1",6,"","[ScrollView]","L53","[Quant_OS]","R6","[Job_Nr]",4,26,20,80,"",0)
  screen clear box 6+records 26 6+records 80 0 0 no-border
  fgp = fgpleasing
  bgp = bgpleasing
END FUNCTION ' UndeldReqs()


FUNCTION SetupDetails()
local $chstr
  jobdesc  = [Description]
  ftginstr = [Instructions]
  ftgcomm  = [Fitting_Comment]
  jobdesc  = @if(len(jobdesc)=0,"Not known",jobdesc)
  ftginstr = @if(len(ftginstr)=0,"Not known",ftginstr)
  ftgcomm  = @if(len(ftgcomm)=0,"Not known",ftgcomm)
  slotrec  = [Appt_Slots]
  $ordstat = [Order_Status]
  $invoice = [Completed]
  $invnr   = [Inv_Nr]
  $invdate = [Invoice_Date]
  #netinv  = [Net_Invoice]
  repaint on
  repaint
  screen save 1 1 6 scw S_all
  repaint off
  vunloadif("ordstat4.vw")
  $chstr = "Customer Fittings Instructions Orders Requisitions Undelivered Receipts"
  while true
    screen shortrestore S_all
    x = colpopup(8,31,20,$chstr,"{Esc} to exit",1,0,clf,clb,0,7)  'highlight date & press {Enter} to show deliveries
    if x = -1
      Background()
      return (-1)
    end if

    if ptstr = "Instructions"
      ShowComments()                ' {I}nstructions
      screen shortrestore S_all

    elseif ptstr = "Receipts"
      #1stbalance = 0
      ShowReceipts()
      screen shortrestore S_all

    elseif ptstr = "Customer"
      repaint off
      vloadif(dpath|"ordstat4.vw")
      order change key "[Job_Nr]"
      data find "[Job_Nr]" equal jobnr options ""
      GetCustDetails()
      vunloadif("ordstat4.vw")
      ShowCustomer()
      screen shortrestore S_all

    elseif ptstr = "Fittings"
      x = ShowFittings2()
      screen shortrestore S_all

    elseif ptstr = "Orders"
      x = ShowOrders()
      inchar
      screen shortrestore S_all

    elseif ptstr = "Requisitions"
      x = ShowAllReqs()

    elseif ptstr = "Undelivered"
      x = UndeldReqs()

    end if
  end while

END FUNCTION ' SetupDetails()


FUNCTION CustScreenLine1()
local y1 y2 y3
  repaint on
  repaint
  ptval=0
  y3 = format(" Today's amendments to Fitting Dates NOT shown","M78")
  y1 = format(" Fitting         Name & Delivery Address","L78")
  y2 = format(" Toggle {C}ompleted/Not {C}ompleted - {O}rder Status - {Esc} ","M78")
  screen print 3 2 fgp bge y3
  screen print 4 2 fgp bgp y1
  screen print 18 2 fgp bgp y2
END FUNCTION ' CustScreenLine1()


FUNCTION CustScreenLine2()
  repaint on
  repaint
  ptval=0
  y1 = format(" Name                                Delivery Address","L71")
  y2 = format(" {Enter} selects - {Esc} exits ","M71")
  screen print 4 6 fgp bgp y1
  screen print 18 6 fgp bgp y2
END FUNCTION ' CustScreenLine2()


FUNCTION DespatRecs()
local x psmode pd pq #percentmargin
  while TRUE
    if [Completed] = "N"
      y2 = format(" Mark as {C}ompleted - {O}rder Status - {Esc} ","M78")
    else
      y2 = format(" {I}ncomplete - change Invoice {D}ate - {O}rder Status - {Esc} ","M78")
    end if
    screen print 18 2 fgp bgp y2
    x = inchar
    if x = {Down}
      data goto record next
    elseif x = {Up}
      data goto record previous
    elseif x = {PgDn}
      data goto page next
    elseif x = {PgUp}
      data goto page previous
    elseif x = {^End}
      data goto record last
    elseif x = {^Home}
      data goto record first
    else
      return (x)
    end if
  end while
END FUNCTION ' DespatRecs()


FUNCTION MarkDespatched(md)
local aa
  if md = "I"
    messbox("Mark"&jobnr&"-"&[CustOrd_Name]&"as incomplete? (y/n) ",1,1,1)
    if ptstr == "y"
      lock-record
        [Completed] = "N"
        [Last_Update]=today
        [DateTB_Inv] = blank
      write-record
      return (0)
    else
      return (1)
    end if

  elseif md = "D"
    invdate = [DateTB_Inv]
    while true
      x = fentrybox(" To be inv'd on"&date2(invdate)|"; change date to: ",9,"##\/##\/####",today)
      if x = 0
        invdate = ptstr
        if chkdate(invdate,1) = -1
          messbox(" Incorrect date - re-enter ",0,0,1)
          continue while
        elseif days(invdate)<days(today)
          messbox(" Must be today or future date ",0,0,1)
          continue while
        end if
        exit while
      elseif x = -1
        return (-1)
      end if
    end while
    lock-record
      [DateTB_Inv] = invdate
      [Completed]  = "Y"
      [Last_Update]= today
      [Comp_Date]  = today
      [Comp_Time]  = time
      [Comp_User]  = userid
      [Updated_By] = userid
    write-record

  elseif md = "C"
    vloadif(dpath|"despat_6.vw")
    order change physical
    data query execute "job_reqn.dfq" index "undeliv.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Job_Nr] = jobnr
'   and
'   not(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      messboxwait(" No uninvoiced orders ",0,0,1)
      return (-1)
    end if
    if filesum([Quant_OS])>.01
      messboxwait(" Items still to be delivered ... cannot invoice ",0,0,1)
      return (-1)
    else
      messboxwait("Check if any goods not yet delivered TO US",0,0,1)
    end if
    vloadif(dpath|"despat_2.vw")
    #apptslots = [Appt_Slots]        'message "#apptslots is:"&str(#apptslots)
    #slotsrem  = [Slots_Rem]         'message "#slotsrem is:"&str(#slotsrem)
    if #slotsrem <> 0
      messbox(" App't slots still to book - remove o/s slots? (y/n) ",1,0,1)
      if ptstr == "y"
        aa = AlterApptSlots()
        if aa = 1
          return (1)
        end if
      else
        return (1)
      end if
    end if

    custcode = [Customer_Code]           'message "custcode) is:"&str(custcode)
    vloadif(dpath|"customer.vws")
    custcity = filelookup([Customer_Code],[City/Town],custcode) 'message "custcity) is:"&str(custcity)
    vloadif(dpath|"despat_2.vw")

    if len(custcity)=0
      messboxwait(" Cannot process - customer's CITY/TOWN not entered ",0,0,1)
      return (1)
    end if

    if [Order_Status]<>"P"
      messboxwait(" Cannot mark as completed - order not passed by Shop ",0,0,1)
      return (1)
    end if

    FittingDate()                      'message "invdate is:"&str(invdate)

    messbox(jobnr&"will be invoiced on"&date2(invdate)|"? (y/n) ",1,1,1)
    if ptstr == "y"
      lock-record
        [DateTB_Inv] = invdate
        [Completed]  = "Y"
        [Last_Update]= today
        [Comp_Date]  = today
        [Comp_Time]  = time
        [Comp_User]  = userid
        [Updated_By] = userid
      write-record
      return (0)
    else
      return (1)
    end if
  end if
END FUNCTION ' MarkDespatched()


FUNCTION FittedDate()
'Mon  =Sat
'Tues =Mon
'Wed  =Tue
'Thurs=Wed
'Fri  =Thu
'Sat  =Fri
'Sun  ; do not run
  if dayname(today)="Monday"
    ftgdate=date2(days(today)-2)
  else
    ftgdate=date2(days(today)-1)
  end if
END FUNCTION ' Ftgdate()


FUNCTION ListDespatches()
  FittedDate()                         'message "ftgdate is"&date2(ftgdate)
  vloadif(dpath|"listjobs.vw")
  progress(15,10," Please wait ... preparing report ",0)
  order change index ipath|"listdesp.idx"
  if records=0
    messboxwait(" No balances owing for fittings on previous working day ",0,0,1)
    return (0)
  end if
  data query execute "dlyrout1.dfq" index "dlyrout1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Branch]=$branch
'   and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No balances owing for fittings 3 days ago ",0,0,1)
    return (0)
  end if
  ftgd1 = date2(days(ftgdate)-1)
  $footing="P = Picked by Branch for invoicing  -  F = Fitter completed ALL work "
  p1 = "dlyrout1.dfr"          ' p1 = report definition ("ord_stck.dfr")
  p6 = 1    ' p6 = nr of copies
  PrintReport(p1,p2,p3,p4,p5,p6)
END FUNCTION ' ListDespatches()


FUNCTION FittingDate()
local  f3
  while true
    if dayname(date2([Fitting_Date])) == "Monday"
      f3 = days([Fitting_Date])+3
    elseif dayname(date2([Fitting_Date])) == "Tuesday"
      f3 = days([Fitting_Date])+3
    elseif dayname(date2([Fitting_Date])) == "Wednesday"
      f3 = days([Fitting_Date])+5
    elseif dayname(date2([Fitting_Date])) == "Thursday"
      f3 = days([Fitting_Date])+5
    elseif dayname(date2([Fitting_Date])) == "Friday"
      f3 = days([Fitting_Date])+5
    elseif dayname(date2([Fitting_Date])) == "Saturday"
      f3 = days([Fitting_Date])+4
    else
      f3 = days([Fitting_Date])+3
    end if
    invdate = date2(f3)
    if days(invdate)<days(today)
      invdate=today
    end if
    return (1)
  end while
END FUNCTION ' FittingDate()


FUNCTION ShopImminent()
  single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  file unload all
  #int=3
  repaint off
  p2 = ""    ' p2 = title at top of choice popup ("LABEL")
  p3 = 1     ' p3 = printer to be used (1=HPIII_QC; 2=GEN_EPSN etc)
  p4 = 1     ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
  p5 = 1     ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1     ' p6 = nr of copies

  ImmtFittingDate()

  PrintUnrecvdList()

  PrintOS_BACS_Bals()

END FUNCTION ' ShopImminent()


FUNCTION PrintUnrecvdList()
'look up date time of shop's fixed index i.e. F_immt.idx
  	if ImmtFileTime()=1 ' no file
    		x=PUL2()
    		if x=1
      		return (1)
    		end if
  	else
     	x=PUL2()
     	if x=1
          	return (1)
        	end if
  	end if

  	while true
    		order sort now dictionary "imm2.idx" fields "[Date_Out]" ascending
    		remove(X_path|"notrcvd1.*")
    		data query execute "not_del.dfq" Smart4 X_path|"notrcvd1" fields "[Job_Nr|Fitting]"
  		if cerror
    			messboxwait(" No O/S Goods for Imminent Jobs ",0,0,1)
    			return (1)
  		end if

    		while true
      		_SWIP_Crystal(Xreppath|"notrcvd1","S",0,1,"Print Now")
      		messbox(" Has`O/S Goods for Imminent Jobs' report printed? (y/n) ",1,1,1)
      		if ptstr == "y"
        			return (1)
      		end if
    		end while
  	end while
END FUNCTION ' PrintUnrecvdList()


FUNCTION PrintOS_BACS_Bals()
' print notrcvd1.rpt type report for Order bals owing for
'use immtftgdate to list Job Nrs to be fitted; report to show jobs with bal OS when deposit paid by BACS
  	progress(15,10," Searching for Jobs to be fitted with OS bals ",0)
  	vloadif(dpath|"bal_bacs.vw")
  	order change key "[Job_Nr]"
  	data query execute "shopimmt.dfq" index "bacs1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   left([Job_Nr],1) = choice
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		messboxwait(" NO outstanding goods for imminent jobs ",0,0,1)
    		return (1)
  	else
    		data query execute "immtjobs.dfq" index "bacs2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'     days([Date_Out]) > days(today)
'     and
'     days([Date_Out]) <= days(ftgperiod)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    		if cerror
      		messboxwait(" NO outstanding goods for imminent jobs ",0,0,1)
      		return (1)
    		else
      		data query execute "balbacs1.dfq" index "bacs3.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Pd_BACS]="Y"
' and
' not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
      		if cerror
        			messboxwait(" NO o/s balances for imm't jobs paid by BACS ",0,0,1)
        			return (1)
      		end if
      		data query execute "balbacs2.dfq" index "bacs4.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Balance_Due]>.1
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
      		if cerror
        			messboxwait(" NO o/s balances for imm't jobs paid by BACS ",0,0,1)
        			return (1)
      		end if
	    		while true
		    		remove(X_path|"balbacs1.*")
    				data query execute "not_del.dfq" Smart4 X_path|"balbacs1" fields "[Job_Nr|Method_Of_Payment]"
		  		if cerror
    					messboxwait(" No O/S Goods for Imminent Jobs ",0,0,1)
		    			return (1)
  				end if
     	 		_SWIP_Crystal(Xreppath|"balbacs1","S",0,1,"Print Now")
      			messbox(" Has`O/S balances for Imm't Jobs paid by BACS' report printed? (y/n) ",1,1,1)
      			if ptstr == "y"
        				return (1)
	      		end if
    			end while
    		end if
  	end if
END FUNCTION ' PrintOS_BACS_Bals()


FUNCTION PUL1() 								' using pre-built index
  vloadif(dpath|"notrecvd.vw")
  order change index $file
  if records=0
    messboxwait(" NO outstanding goods for imminent jobs ",0,0,1)
    return (1)
  end if
END FUNCTION ' PUL1()


FUNCTION PUL2()
  	progress(15,10," Searching for Goods not recv'd for imminent jobs ",0)
  	vloadif(dpath|"notrecvd.vw")
  	order change key "[Job_Nr]"
  	data query execute "shopimmt.dfq" index "int1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   left([Job_Nr],1) = choice
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		messboxwait(" NO outstanding goods for imminent jobs ",0,0,1)
    		return (1)
  	else
    		data query execute "immtjobs.dfq" index "int.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'     days([Date_Out]) > days(today)
'     and
'     days([Date_Out]) <= days(ftgperiod)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    		if cerror
      		messboxwait(" NO outstanding goods for imminent jobs ",0,0,1)
      		return (1)
    		else
      		data query execute "cplprep2.dfq" index "int2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [RollNr] = "00000/00"
'   or
'   [RollNr] = "BESPOK"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
      		if cerror
        			messboxwait(" NO outstanding goods for imminent jobs ",0,0,1)
        			return (1)
      		end if
    		end if
  	end if
END FUNCTION ' PUL2()


FUNCTION ImmtFittingDate()
local  f3
  if dayname(today)="Monday"
    f3=days(today)+#int
  elseif dayname(today)="Tuesday"
    f3=days(today)+#int
  elseif dayname(today)="Wednesday"
    f3=days(today)+#int+2
  elseif dayname(today)="Thursday"
    f3=days(today)+#int+2
  elseif dayname(today)="Friday"
    f3=days(today)+#int+2
  elseif dayname(today)="Saturday"
    f3=days(today)+#int+1
  elseif dayname(today)="Sunday"
    f3=days(today)+#int+1
  end if
  ftgperiod = date2(f3)
' message "ftgperiod is:"&str(ftgperiod)
END FUNCTION ' ImmtFittingDate()


FUNCTION StoreReport(p1,ext)
local dlyrep
  dlyrep =gdsoutpath|choice|left(sum_date,2)|mid(sum_date,4,2)|right(sum_date,2)|"."|ext  '
  print report execute p1 disk dlyrep detail start 1 end 0 copies 1
END FUNCTION ' StoreReport()


FUNCTION ImmtFileTime()
local t
  $file=ipath|$branch|"_immt.idx"      'message "$file) is:"&str($file)
  if file($file)=1
    x=fileinfo($file,f_datetime)
    dl=date2(left(str(x),5))           'message "date of IMMT file is:"&str(dl)
    return(0)
  else
    return(1)
  end if
END FUNCTION ' ImmtFileTime()


FUNCTION Despat_2()
  	fmt = "L64"
  	bot = 7
  	clb = 13
  	clf = 10
  	$invoice = "N"

  	x=ChooseJobs()									'L1884

'   UninvoicedOrders() ' 301204 - rem'd out unless needed

END FUNCTION ' Despat_2()


FUNCTION ChooseJobs()
local s1 s2 s3 s4 s5 s6 $locn
  	$shop=$branch
  	$shop_name=$place
  	progress(15,10," Searching for uninvoiced orders ",0)
	vloadif(dpath|"despat_2.vw")
    	vloadif(dpath|"cust_ord.vws")
    	NewIndex()
    	vunloadif("cust_ord.vws")
    	vloadif(dpath|"despat_2.vw")
	error off
    	order change index ipath|$branch|"_uninv.idx"
	if cerror
		messboxwait(" No UNINVOICED file found - report to Head Office ",0,0,1)
		return (1)
	end if
	order sort now dictionary "lstjobs.idx" fields "[Fitting_Date;Job_Nr]" descending
  	repaint on
  	repaint
  	x=SelectDespatched()
END FUNCTION ' ChooseJobs()


FUNCTION NewIndex()
  progress(fgp,bgp," Uninvoiced orders due for Invoicing ",0)
  progtag(fgi,bgi," Creating list for"&$shop_name)
  data query execute "cofsales.dfq" index "despat_1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [SalesAnalysis]=$branch
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
'     messboxwait(" No uninvoiced orders ",0,0,1)
    return (-1)
  end if
  data query execute "despat_2.dfq" index "despat_0.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Inv_Nr]=blank
'   and
'   not (deleted)
'   and
'   [SalesAnalysis]<>"X"
'   and
'   [SalesAnalysis]<>"P"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
'     messboxwait(" No uninvoiced orders ",0,0,1)
    return (-1)
  end if
  data query execute "despat_4.dfq" index ipath|$branch|"_uninv.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Order_Status]<>"M"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
'     messboxwait(" No uninvoiced orders ",0,0,1)
    return (-1)
  end if
  error on
END FUNCTION ' NewIndex()


FUNCTION SelectDespatched()
  CustScreenLine1()
  while true
    ptval = DespatRecs()
    if ptval = {C} or ptval = {c}
      jobnr = [Job_Nr]
      if [Completed] = "Y"
        continue while
      end if
      repaint off
      #prec = precord
'       MaxFtgDate()
      #slotsrem = [Slots_Rem]
      x = MarkDespatched("C")
      vloadif(dpath|"despat_2.vw")
      if record<records
        data goto record next
      end if
      CustScreenLine1()
      continue while

    elseif ptval = {D} or ptval = {d}
      jobnr = [Job_Nr]
      if [Completed] = "N"
        continue while
      end if
      repaint off
      #prec = precord
'       MaxFtgDate()
      #slotsrem = [Slots_Rem]
      x = MarkDespatched("D")
      vloadif(dpath|"despat_2.vw")
      if record<records
        data goto record next
      end if
      CustScreenLine1()
      continue while

    elseif ptval = {I} or ptval = {i}
      jobnr = [Job_Nr]
      if [Completed] = "N"
        continue while
      end if
      repaint off
      #prec = precord
'       MaxFtgDate()
      #slotsrem = [Slots_Rem]
      x = MarkDespatched("I")
      vloadif(dpath|"despat_2.vw")
      if record<records
        data goto record next
      end if
      CustScreenLine1()
      continue while

    elseif ptval = {O} or ptval = {o}
      jobnr = [Job_Nr]
      #prec = precord
'       MaxFtgDate()
      x = SetupDetails()
      vloadif(dpath|"despat_2.vw")
      CustScreenLine1()
      continue while

    elseif ptval = {Esc}
      Background()
      return (-1)
    end if
  end while
END FUNCTION ' SelectDespatched()


FUNCTION ImmtReservation()
  	single-step off
	Background()
  	file unload all

 	while true
		screen clear box 1 1 sch scw 0 0 no-border
' 	  	x=ChooseShops2Select()			'message "x) is:"&str(x)
'      	if x = -1
' 			ReturnToMenu()
' 	     end if
' 		$base=leftjob						'message "leftjob is:"&str(leftjob)
		$base=$branch						'message "$base is:"&str($base)
		enddate=date2(days(today)+7)				'message "enddate is:"&str(enddate)
		x=remove("immrsv1.idx")			'message "remove is:"&str(x)
	     vloadif(dpath|"appntmnt.vws")
		x=makeidx("appntmnt","immrsv1.idx","0",1)	'message "makeidx is:"&str(x)
	     order change key "[Date]"
		data find "[Date]" equal enddate options ""
		data goto record previous
	 	progress(15,10," Building file of appointments from"&date2(today)&"to"&date2(enddate)&" ",0)
		while days([Date])>days(today)
		     x = addidxrec("immrsv1.idx",precord,7)    'message "x is:"&str(x)
			data goto record previous
		end while
		order change index "immrsv1.idx"		' set number of days selected
' TOTAL LIST OF APPTS FOR NEXT 7 DAYS NOW LISTED
' $delcode=$base|"R"
' message "$delcode) is:"&str($delcode)
' now query to find left([DelAddr&Code],2)=$base|"R"
		data query execute "immtrsv1.dfq" index "immrsv2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'  left([DelAddr&Code],2)=$base|"R"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
		if cerror
			messboxwait(" No Fitters' reservations found for next 7 days (50) ",0,0,1)
			exit while
		end if

'now exclude right([Nickname],3)="EST"
		data query execute "not_est2.dfq" index "immrsv3.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' right([N_name],3)<>"EST"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
		if cerror
			messboxwait(" No Fitters' reservations found for next 7 days (60) ",0,0,1)
			exit while
		end if

    		vunloadif("appntmnt.vws")
     	vloadif(dpath|"imm_rsv1.vw")
		order change index "immrsv3.idx"
		remove(X_path|"X_immrs2.*")
		data query execute "not_del.dfq" Smart4 X_path|"X_immrs2" fields "[Date|Location]"
		if cerror
			messboxwait(" No Fitters' reservations found for next 7 days (70) ",0,0,1)
' 			messbox(" No data found ",0,0,1)
' 				continue while
			exit while
		end if
	
		vunloadif("X_immbkg.vws")
     	vunloadif("imm_rsv1.vw")
		vunloadif("imm_bkg3.vw")
		vunloadif("imm_bkg2.vw")					'   ClearHardSpaces()
		_SWIP_Crystal(Xreppath|"X_immrs1","S",0,1,"")
		exit while

	end while

END FUNCTION ' ImmtReservation()


FUNCTION Job_Locn()
  	s1 = "Warehouse"
  	s2 = "Trade"
  	s3 = "Fulham"
  	s4 = "Raynes"
  	s5 = "Sheen"
  	s7 = "Putney"
  	while true
    		x = popuplist(8,37,15,s3&s7&s4&s5&s2&s1,"Order",1,0)
    		if x = -1
      		return (-1)
    		end if
    		$place = ptstr
    		messbox(" Confirm"&upper($place)&"shop? (y/n) ",1,1,1)
    		if ptstr == "y"
      		leftjob=left($place,1)
      		return (0)
    		else
      		continue while
    		end if
  	end while
END FUNCTION 'Job_Locn()


FUNCTION ChooseShops2Select()
local leftjob currec
	s1 = "Warehouse"
  	s2 = "Trade"
  	s3 = "Fulham"
  	s4 = "Raynes"
  	s5 = "Sheen"
  	s7 = "Putney"
message "L2110\ base) is:"&str(base)
  	if base="O"                         ' choice of Warehouse etc
    		leftjob=Job_Locn()
    		if leftjob = -1
	      	return (-1)
      	end if

  	elseif base="F"
    		while true
      		x = popuplist(8,57,15,s3&S7&s2,"",1,0)
      		if x = -1
	      		return (-1)
      		end if
      		$place = ptstr
      		leftjob=left($place,1)
      		exit while
    		end while

  	elseif base="S"
    		while true
	      	x = popuplist(8,57,15,s5&s4,"",1,0)
      		if x = -1
	      		return (-1)
      		end if
      		$place = ptstr
      		leftjob=left($place,1)
      		exit while
    		end while
	else
		leftjob=Job_Locn()
	end if
END FUNCTION 'ChooseShops2Select()


FUNCTION OrdOvr30()
'ORDOVR30 - orders over 30 days old with a balance o/s. Part of DLY ROUT at shops. The balance of the Order
'		  is due after 30 days whether fitted/invd or not	
single-step off
  	quiet on
  	screen clear box 1 1 sch scw 0 0 no-border
  	repaint off
  	vloadif(dpath|"ovrord30.vw")
	$index=ipath|"ordovr30.idx"				' message "$index is:"&str($index)
	if file($index)=1	' exists message "ordover30 exists"
		order change index ipath|"ordovr30.idx"
	elseif file($index)=0	' does not exists
		data query execute "ordovr_1.dfq" index ipath|"ordovr30.idx"
	end if

	SelectOver30days()

END FUNCTION 'OrdOvr30()


FUNCTION SelectOver30days()
  	Background()
    	progress(15,10," Please wait ... looking for orders over 30 days old ",0)
  	vloadif(dpath|"ovrord30.vw")
 	order change index ipath|"ordovr30.idx"
'  "[SalesAnalysis]"
	data query execute "cofsales.dfq" INDEX "first.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [SalesAnalysis]=$branch
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    	if cerror
     	messboxwait(" No orders over 30 days old at"&br_name|" ",0,0,1)
    	else
     	screen clear box 1 1 sch scw 0 0 no-border
     	vunloadif("X_ovrord.vws")
     	remove(X_path|"X_ovrord.*")
data query execute "not_del.dfq" Smart4 X_path|"X_ovrord" fields\
"[Job_Nr;Title;Contact_Name;CustOrd_Name;Date_Of_Order;Delivery_Address_1;Del_Postcode;Invoice_Total;Balance_Due;Mobile/Other_Nr;Home_Tel;Credit_Status;Email_Addr;Inv_Nr]"
     	_SWIP_Crystal(Xreppath|"X_ordovr","S",0,1,"")
    	end if
END FUNCTION ' SelectOver30days()


FUNCTION BACS()
' lists all BACS/CHAPS/Direct etc recvd at Bank and logged on that day
	lock module br_name
	lock module $branch
'   	choice = left(br_name,1)             				'message "choice is:"&str(choice)
	transfer "BACSDRCT.rf3" in-memory
END FUNCTION ' BACS()

