'CPL_PREP - new process for Cutting & Parts Lists, Fitters' Lists, Loading Sheets etc

external   fentrybox() messbox() progress() vloadif() vunloadif() chkdate() psa	messboxwait()
external   dpath vloadexcif() chg_dsc() sch scw cpath remove() jobnr fgp bgp
external   mess4() PrintReport() shopmask makeidx() bgs fgs popuplist() dsa
external   increment() cplpath chkstr() addidxrec() strcount() ctgplanpath _GEMS_ShellEx()
external   Background() userid bpopdb() getidxrecs() X_path _SWIP_Crystal() Xreppath $menu

public     ftgdate ptstr job_names[1] prod_code[1] ftr_codes[1] ptval $branch $pdf
public     jobsperftr[1] $itemtype ftrname custname deladdr1
public     deladdr2 deladdr3 deladdr4 offtel hometel ftginstr ftgcomm $repnr
public     cr_status mobile balancedue $carptitle ftrcode $nickname custcode
public     #origsize $rollnr $refnr #out docref #newbal prodcode $rel $reas $plannr

global     CheckFitDate() ChkGdsOut() x ReturnToMenu() #ftrs $ctg ViewCtgPlan()
global     nrjobs nrftrs PrintJobList() gdsinstck apptsconfd
global     anclinstck nrcodes tot_need tot_avail $continue $temp $jobnrs
global     p1 p2 p3 p4 p5 p6 $comment cplrep i #jobnrs #prec $permit
global     PrintJobSheet() FittersLists() PreparationLists() $ftrs
global     PrintJobs() datenumber sr er PrepLists() CheckBalanceHandling() #deposit #dep_reqd
global	 ChkAccCust() y Gds_Reld() ShowDeliveries()


MAIN
local l1 l2 l3 l4 l5 l6 l2a l7 l8
single-step off
  	Background()
  	file unload all
' message "On D: drive at 1150A"	
  	$continue = "Y"

'--------------------------------------suggest day to prepare list for
	CheckFitDate()							'LINE 156
	if x = -1
    		ReturnToMenu()
	end if

'--------------------------------------check recvd or allocated - else print list
	x = ChkGdsOut()                    		'LINE 179 - 1=Not all rec'd; 0=OK
	if x = 1
    		if $menu = "whse"
      		messbox(" Do you want to continue? (y/n) ",1,0,1)
      		if ptstr == "n"
        			ReturnToMenu()
      		end if
    		end if
  	elseif x = 2
    		ReturnToMenu()
  	end if

'----------------------------list of all jobs going out
'   progress(15,10," Please wait .... finding records ",0)
	vloadif(dpath|"goodsout.vws")
  	order change index "todays1.idx"			' list of all records in GOODSOUT with chosen date in "[Date_Out]"
'look at Job_Nrs in GOODSOUT/todays1.idx and remove duplicates
  	data goto record first
  	$jobnrs = [Job_Nr]
  	data goto record next
  	for i = 2 to records                 'create text string of Job Nrs
    		x = chkstr([Job_Nr],$jobnrs)
    		if x = -1
      		$jobnrs = $jobnrs&[Job_Nr]
    		end if
    		data goto record next
  	end for
' message "L66\ $jobnrs is:"&str($jobnrs)
' 	remove("c.txt")
' fopen "c.txt" as 1
' fwrite 1 from $jobnrs
' fclose 1

'create index of CUST_ORD by stepping thro' text string and using ADDIDXRECS
  	x=strcount($jobnrs)                  ' message "x is:"&str(x)
  	#jobnrs = ptval
  	vloadif(dpath|"cust_ord.vws")
  	order change key "[Job_Nr]"
  	remove("jobs2fit.idx")
  	x = makeidx("cust_ord","jobs2fit","0",3)     'message "x is:"&str(x)
  	for i = 1 to #jobnrs
    		jobnr = group($jobnrs,i)
    		data find "[Job_Nr]" equal jobnr options ""
    		x = addidxrec("jobs2fit.idx",precord,7)    'message "x is:"&str(x)
  	end for
  	vunloadif("cust_ord.vws")

'--------------------------------------check Orders for Release permission
' 	CheckBalanceHandling()
' 	if x = -1
' 	     ReturnToMenu()
' 	end if

	l1 = "Job’List"
  	l2 = "Preparation’Lists"
  	l2a = "Imminent’Jobs"
  	l3 = "Fitters'’Worksheets"
  	l8 = "Unprocessed’Goodsout"

  	l4 = "Cutting’List"
  	l5 = "Bespoke’Items"
  	l6 = "Ancillaries"
  	l7 = "Fitter's’Lists"

  	while true
    		x = popuplist(11,39,17,l1&l2&l3&l2a&l8,"",1,0)
    		if x = -1
      		exit while
    		end if

    		if ptstr = l1                      'l1 = "Job’List"
      		PrintJobList()							' L307

    		elseif ptstr = l2                  'l2 = "Preparation’Lists"
      		while true
        			x = popuplist(9,31,13,l4&l5&l6&l7,"",1,0)
        			if x = -1
          			exit while
        			end if
        			if ptstr = l4                    'l4 = "Cutting’List"
          			PreparationLists(1,0)

        			elseif ptstr = l5                'l5 = "Bespoke Items"
          			PreparationLists(2,0)

        			elseif ptstr = l6                'l6 = "Ancillaries"
          			PreparationLists(3,0)

        			elseif ptstr = l7              'l7 = "Fitters"
          			PreparationLists(4,0)
        			end if
      		end while

    		elseif ptstr = l3                  'l3 = "Fitters'’Worksheets"
      		PrintJobs()

    		elseif ptstr = l2a                 'l2a = "Imminent’Jobs"
      		execute "imminent.rf3" in-memory

    		elseif ptstr = l8                 'l8 = "Unprocessed’Goodsout"
      		execute "oldgdout.rf3" in-memory

    		end if

  	end while

  	ReturnToMenu()

END MAIN


FUNCTION CheckFitDate()
  	while true
    		ftgdate = days(today) + 1
    		if dayname(date2(ftgdate)) == "Saturday"
      		ftgdate = days(today) + 3
    		end if
    		if dayname(date2(ftgdate)) == "Sunday"
      		ftgdate = days(today) + 2
    		end if
    		fentrybox(" Confirm date for CPL lists ",10,"##\/##\/####",date2(ftgdate))
    		if x = -1
      		return (-1)
    		end if
    		ftgdate = ptstr
    		if chkdate(ftgdate,1) = -1
      		messbox(" Incorrect date - re-enter ",0,0,1)
      		continue while
    		end if
    		exit while
  	end while
END FUNCTION ' CheckFitDate()


FUNCTION ChkGdsOut()
' message "CP L174 ftgdate is:"&str(ftgdate)
	vloadif(dpath|"goodsout.vws")
  	order change key "[Date_Out]"
  	data query execute "cplprep1.dfq" index "todays1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Date_Out]=days(ftgdate)
'   and
'   not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
    		messboxwait(" No goods due for despatch on"&date2(ftgdate),0,0,1)
    		return (2)
  	end if

'check that all records for Stock or Bespoke have been alloc'd/rec'd
  	data query execute "cplprep2.dfq" index "todays2.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [RollNr] = "00000/00"
'   or
' ³ [RollNr] = "BESPOK"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
    		return (0)
	else
    		data query execute "notype_o.dfq" index "unalloc.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Itemtype] <> "O"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
    		if cerror
      		return (0)
     	else
     		messbox(" Not all goods for despatch have been received or allocated ",0,0,1)
     		repaint off
     		order change physical
     		vloadif(dpath|"notrecvd.vw")
     		order change index "unalloc.idx"
     		order sort now dictionary "notrecvd.idx" fields "[Job_Nr]" ascending
' p1 = "notrecvd.dfr"   ' p1 = report definition ("ord_stck.dfr")
' PrintReport(p1,"Not Received",p3,p4,p5,p6)
		    	remove(X_path|"notrcvd1.*")
		    	data query execute "not_del.dfq" Smart4 X_path|"notrcvd1" fields "[Job_Nr|Fitting]"
		     _SWIP_Crystal(Xreppath|"notrcvd2","S",0,1,"")
		     return (1)
    		end if
  	end if
END FUNCTION ' ChkGdsOut()


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 FittersLists(pl)
local y
'----------------------------list all fitters going out
'   progress(15,10," Please wait .... finding records ",0)
  	vloadif(dpath|"lststk_G.vw")
  	order change index "todays1.idx"  'look at FtrCode in GOODSOUT/todays1.idx and remove duplicates
  	data goto record first
  	while true
    		if chkstr(mid([FtrCode],2,1),"1 2 3 4 5 6 7 8 9 0") = 0
      		messbox(" Not text’",0,0,1)
      		data goto record next
    		else
      		$ftrs = [FtrCode]
      		exit while
    		end if
    		if record = records
      		return (-1)
    		end if
  	end while
' message "L249/$ftrs is:"&str($ftrs)
  	data goto record next
  	for i = 2 to records                 'create text string of Job Nrs
    		x = chkstr([FtrCode],$ftrs)
    		if x = -1
      		if chkstr(mid([FtrCode],2,1),"1 2 3 4 5 6 7 8 9 0") = 0
        			messbox(" Not text’",0,0,1)
        			data goto record next
      		else
        			$ftrs = $ftrs&[FtrCode]
'         $ftrs = [FtrCode]
'         exit while
      		end if
    		end if
    		data goto record next
  	end for
' message "$ftrs is:"&str($ftrs)

'create index of FTR_LIST by stepping thro' text string and using ADDIDXRECS
  	x=strcount($ftrs)                    'message "x is:"&str(x)
  	#ftrs = ptval                        'message "#ftrs is:"&str(#ftrs)
  	vloadif(dpath|"ftr_list.vws")
  	order change key "[Fitter_Code]"
  	remove("ftrlist.idx")
  	x = makeidx("ftr_list","ftrlist","0",3)     'message "x is:"&str(x)
  	for i = 1 to #ftrs
    		ftrcode = group($ftrs,i)
    		data find "[Fitter_Code]" equal ftrcode options ""
    		x = addidxrec("ftrlist.idx",precord,7)    'message "x is:"&str(x)
    		if x<>0
      		messboxwait(" Fitters index not building correctly [L270] ",0,0,1)
      		return (-1)
    		end if
  	end for
  	order change index "ftrlist.idx"
  	order sort now dictionary "fitters.idx" fields "[Nickname]" ascending
  	sr = @if(records<18,11-int(records/2),3) 'message "sr is:"&str(sr)
  	er = @if(records<18,sr+records+1,20)     'message "er is:"&str(er)
  	while true
    		x = bpopdb("ftr_list",8,"","[Nickname]","L8","[Fitter_Name]","L0","[Fitter_Code]",sr,35,er,45,"",0)
    		if x = -1
		     screen clear box 1 1 sch scw 0 0 no-border
     		repaint off
	     	exit while
    		else
	      	ftrcode = [Fitter_Code]          'message "ftrcode is:"&str(ftrcode)
     	 	$nickname = [Nickname]
      		PrepLists()
      		vloadif(dpath|"ftr_list.vws")
    		end if
  	end while
END FUNCTION ' FittersLists()


' FUNCTION PrintJobList()				'----------------------------list of all jobs going out
' 	progress(15,10," Please wait .... finding jobs ",0)
'   	vloadif(dpath|"jobs2fit.vw")
'   	order change index "jobs2fit.idx"
'   	order sort now dictionary "lstjobs.idx" fields "[CustOrd_Name]" ascending
' '   p1 = "listjobs.dfr"
' '   PrintReport(p1,"Job List",p3,p4,p5,p6)
'
'   	vunloadif("Xjoblst1.vws")
' 	remove(X_path|"X_joblst.*")
' ' 	remove(dpath|"Xjoblst1.*")
'   	vloadif(dpath|"Xjoblst2.vw")
'   	data query execute "not_del.dfq" Smart4 X_path|"X_joblst" fields\
' "[Cutting_Plan;Job_Nr;Fitting_Date;Balance_Due;CustOrd_Name;Delivery_Address_1;Credit_Status;Order_Status;Nickname;FtgDate]"
'   	_SWIP_Crystal(Xreppath|"X_joblst","S",0,1,"")
' END FUNCTION 'PrintJobList()


FUNCTION PrintJobList()				'----------------------------list of all jobs going out
	progress(15,10," Please wait .... finding jobs ",0)
  	vloadif(dpath|"jobs2fit.vw")
  	order change index "jobs2fit.idx"
  	order sort now dictionary "lstjobs.idx" fields "[CustOrd_Name]" ascending
'   p1 = "listjobs.dfr"
'   PrintReport(p1,"Job List",p3,p4,p5,p6)
  	vunloadif("X_joblst.vws")
	remove(X_path|"X_joblst.*")
  	data query execute "not_del.dfq" Smart4 X_path|"X_joblst" fields\
  	"[Job_Nr;Fitting_Date;Balance_Due;CustOrd_Name;Delivery_Address_1;Credit_Status;Nickname;Order_Status]"
  	_SWIP_Crystal(Xreppath|"X_joblst","S",0,1,"")
END FUNCTION 'PrintJobList()


FUNCTION PreparationLists(list,mark)   ' 1=Stock Cutting List only; 0=enter CPL_ref & print all
local $datafile
	vloadif(dpath|"lststk_G.vw")
  	order change index "todays1.idx"
  	if list = 1
'----------------------------total list for stock carpet, tiles etc
'while true
    		data query execute "ctg_list.dfq" index "list.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [Itemtype] = "C"                                                   ³
' ³ or                                                                 ³
' ³ [Itemtype] = "S"                                                   ³
' ³ or                                                                 ³
' ³ [Itemtype] = "V"                                                   ³
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
    		if cerror
      		messboxwait(" No carpets to cut or stock tiles/vinyl to prepare ",0,0,1)
      		return (1)
    		end if
    		order sort now dictionary "listcarp.idx" fields "[ViewDesc]" ascending
    		$carptitle = "STOCK"
    		remove(X_path|"X_lstcrp.*")
    		data query execute "not_del.dfq" Smart4 X_path|"X_lstcrp" fields \
"[Reference_Nr;Product_Code;Product_MRC;Description_MRC;Width;Lst_Stck;Requsn_Nr;Date_Out;RollNr;QuantOut;Job_Nr;Itemtype;Cost;FtrCode;ViewDesc;Document;CPL_Ref;Locn_S;PhysBal_Stk]"
' "[Reference_Nr;Product_Code;Product_MRC;Description_MRC;Width;Lst_Stck;Requsn_Nr;Date_Out;RollNr;QuantOut;Job_Nr;Itemtype;Cost;FtrCode;ViewDesc;Document;CPL_Ref;Locn_S;PhysicalBalance]"
    		order sort now dictionary "lstcarp.idx" fields "[Job_Nr;RollNr]" ascending
    		vunloadif("X_lstcrp.vws")
    		_SWIP_Crystal(Xreppath|"X_lstcrp","S",0,1,"")
	     file unload all

	elseif list = 2							'total list for Bespoke carpet, tiles etc
		vloadif(dpath|"lststk_G.vw")
	    	order change index "todays1.idx"
    		data query execute "listbesp.dfq" index "list.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Item_Type]="B" or [Item_Type]="J" or [Item_Type]="T" or [Item_Type]="W"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
	    	if cerror
     		messbox(" No Bespoke items to list ",0,0,1)
	    	else
     		order sort now dictionary "listbesp.idx" fields "[ViewDesc]" ascending
      		$carptitle = "BESPOKE"
	    		remove(X_path|"X_lstbsp.*")
    			data query execute "not_del.dfq" Smart4 X_path|"X_lstbsp" fields "[Job_Nr;Product_MRC;Description_MRC;Width;RollNr;QuantOut;Date_Out;Location]"
    			order sort now dictionary "lstbesp.idx" fields "[Job_Nr;RollNr]" ascending
	    		vunloadif("X_lstbsp.vws")
    			_SWIP_Crystal(Xreppath|"X_lstbsp","P",0,1,"")
'p1 = "listbesp.dfr"
'PrintReport(p1,$carptitle&"Carpet list",p3,p4,p5,p6)
	    	end if

	elseif list = 3
'----------------------------total list for Ancillaries
    		vloadif(dpath|"stk_ancl.vws")
    		vloadif(dpath|"lststk_G.vw")
    		order change index "todays1.idx"
    		data query execute "listANCL.dfq" index "list.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Itemtype]="A"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
		if cerror
     		messbox(" No Ancillary items to list ",0,0,1)
      		message "Index error @ L.288"
	    	else
     		$datafile = dpath|"ANCLLIST"     'message "$datafile is:"&str($datafile)
      		remove($datafile|".*")
	      	data cross-tab execute "listancl.dfw" Smart4 $datafile
     	 	vloadif(dpath|"anclist1.vw")
      		order sort now dictionary "listancl" fields "[Product_MRC]" ascending ' CURRENT month

'order sort now dictionary "listbesp.idx" fields "[ViewDesc]" ascending
'$carptitle = "BESPOKE"
	    		remove(X_path|"X_lstanc.*")
    			data query execute "not_del.dfq" Smart4 X_path|"X_lstanc" fields "[Product_MRC;F0002;PhysicalBalance]"
'     		order sort now dictionary "lstbesp.idx" fields "[Job_Nr;RollNr]" ascending
    			vunloadif("X_lstanc.vws")
	    		_SWIP_Crystal(Xreppath|"X_lstanc","P",0,1,"")

' p1 = "listancl.dfr"                  'message "p1) is:"&str(p1)
' PrintReport(p1,"Ancillary list",p3,p4,p5,p6)     'breakdown by Product_Code
		     vloadif(dpath|"lststk_G.vw")
		end if

	elseif list = 4
    		FittersLists("P")
	end if
END FUNCTION ' PreparationLists()


FUNCTION PrepLists()
	repaint off
  	vloadif(dpath|"ftrslist.vw")
  	order change index "todays1.idx"
  	data query execute "ftrlist1.dfq" index "loading1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [FtrCode] = ftrcode
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
  	end if
  	data query execute "ftrlist2.dfq" index "loading2.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Item_Type]="B" or [Item_Type]="J" or [Item_Type]="T" or [Item_Type]="W" OR [Item_Type]="C" or [Item_Type]="S" or [Item_Type]="V"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	order sort now dictionary "loading3.idx" fields "[Product_Code]" ascending

'   p1 = "ftr_jobF.dfr"
'   PrintReport(p1,"Loading for"&$nickname,p3,p4,p5,p6)
	remove(X_path|"X_ftrjob.*")
    	data query execute "not_del.dfq" Smart4 X_path|"X_ftrjob" fields "[Itemtype;Nickname;Date_Out;Product_MRC;Description_MRC;Job_Nr;QuantOut;Width;RollNr;Product_Code;Lst_Stck]"
	vunloadif("X_ftrjob.vws")
	_SWIP_Crystal(Xreppath|"X_ftrjob","P",0,1,"")

END FUNCTION ' PrepLists()


FUNCTION CheckBalanceHandling()					'
' step through Jobs and print list of Orders which cannot be released

	vloadif(dpath|"jobs2fit.vw")
	order change index "jobs2fit.idx"
  	order sort now dictionary "lstjobs.idx" fields "[CustOrd_Name]" ascending
     for i = 1 to #jobnrs
     	jobnr = group($jobnrs,i)					'
' message "i is:"&str(i)
' message "jobnr is:"&str(jobnr)

		Gds_Reld(jobnr)
		vloadif(dpath|"jobs2fit.vw")
		lock-record
			[Release]=$rel
			[Reason]=$reas
		write-record
' message "$rel is:"&str($rel)
' message "$reas is:"&str($reas)
		data goto record next
	end for

	remove(X_path|"X_gdshld.*")
	vunloadif("X_gdshld.vws")
	data query execute "gds_hld1.dfq" Smart4 X_path|"X_gdshld" fields "[Job_Nr;CustOrd_Name;Delivery_Address_1;Fitting_Date;Release;Reason]"
' [Var_Nr;Date;CustOrd_Name;Amount_Gross;shop;Date_To;Nr_Orders;Avg_Order;Tot_Order_Net;Tot_Order_Gross]"

	_SWIP_Crystal(Xreppath|"X_gdshld","S",0,1,"")
'   _SWIP_Crystal(Xreppath|"$inv_1","EP",0,1,invpath|$nextinvnr|".pdf")

'   p1 = "listjobs.dfr"
'   PrintReport(p1,"Job List",p3,p4,p5,p6)

END FUNCTION 'CheckBalanceHandling()


FUNCTION Gds_Reld(jobnr)
	vloadif(dpath|"gdsreld1.vw")
     order change key "[Job_Nr]"
     data find "[Job_Nr]" equal jobnr options ""
     if cerror                               '   if none - then return
'       x = messbox(" Job Nr not found - confirm as"&jobnr|"? (y/n) - {Esc} to exit ",1,0,0)
'       if x = 0
		vunloadif("gdsreld1.vw")
		$rel = "N"								' FULLY paid	FC-10
		$reas = "FC10"							
	end if
	custcode=[Customer_Code]
	#deposit=[Invoice_Total]-[Balance_Due]
	#dep_reqd=[Invoice_Total]*.45
' message " #deposit is:"&str(#deposit)
' message " #dep_reqd is:"&str(#dep_reqd)

	if [Credit_Status]="C"
		if[Invoice_Total]<=500
			if [Balance_Due]>.01
' 				vunloadif("gdsreld1.vw")
				$rel = "N"							' insufficient deposit	FC-1
				$reas = "FC01"							
			end if
		end if

' message "jobnr is:"&str(jobnr)
		if [Balance_Due]<.01							
			vunloadif("gdsreld1.vw")
			$rel = "Y"								' FULLY paid	FC-10
			$reas = "FC10"							
		elseif #deposit<#dep_reqd						
			vunloadif("gdsreld1.vw")
			$rel = "N"								' insufficient deposit	FC-1
			$reas = "FC01"							
		elseif #deposit>#dep_reqd						
			if [OrderAcceptanceRecvd]="N"
				vunloadif("gdsreld1.vw")
				$rel = "N"							' FC-2
				$reas = "FC02"							
			elseif [OrderAcceptanceRecvd]="Y"
				if [SameAddress]="Y"
					if days(today)-days([Date_Of_Order])>31
						vunloadif("gdsreld1.vw")
						$rel = "N"					' FC-6
						$reas = "FC06"							
					elseif days(today)-days([Date_Of_Order])<=31
						vunloadif("gdsreld1.vw")
						$rel = "Y"					' FC-3
						$reas = "FC03"							
					else
						vunloadif("gdsreld1.vw")
						$rel = "N"					' No SameAddress Status
						$reas = "FC00"							
					end if
				else
					vunloadif("gdsreld1.vw")
					$rel = "N"						' FC-5
					$reas = "FC05"							
				end if
			else
				vunloadif("gdsreld1.vw")
				$rel = "N"							' No OAR Status
				$reas = "FC11"							
			end if
		end if


	elseif [Credit_Status]="D"
		if [Balance_Due]<.01							' FULLY paid
			vunloadif("gdsreld1.vw")
			$rel = "Y"
			$reas = ""							
		end if

	elseif [Credit_Status]="A"
		vunloadif("gdsreld1.vw")
		y=ChkAccCust()
		return y
	else
		$rel = "N"									' No Credit Status
		$reas = "FC12"							

	end if

END FUNCTION 'Gds_Reld()


FUNCTION ChkAccCust()
' message "custcode is:"&str(custcode)
	vloadif(dpath|"acc_chk1.vw")
     order change key "[Customer_Code]"
     data find "[Customer_Code]" equal custcode options ""
     if cerror                               '   if none - then return
		vunloadif("acc_chk1.vw")
		$rel = "N"								'
		$reas = "FC13"							
	end if
	
	if [OD]="Y"									'
		if days(today)-[Overdue]>7						
' 			message "Inform DG/DC"					' FC-9
			$reas = "FC09"							
		end if
		vunloadif("acc_chk1.vw")
		$rel = "N"								' FC-4
		$reas = "FC04"							

	elseif [OD]="N"
		if [TotalOwing]<=[Credit_Limit]
			vunloadif("acc_chk1.vw")
			$rel = "Y"							' FC-7
			$reas = "FC07"							
		else
			vunloadif("acc_chk1.vw")
			$rel = "Y"							' FC-10
			$reas = "FC10"							
		end if
	end if

END FUNCTION 'ChkAccCust()


FUNCTION PrintJobs()
	vloadif(dpath|"jobs2fit.vw")
  	order change index "jobs2fit.idx"
  	order sort now dictionary "lstjobs.idx" fields "[CustOrd_Name]" ascending
  	sr = @if(records<18,11-int(records/2),3) 'message "sr is:"&str(sr)
  	er = @if(records<18,sr+records+1,20)     'message "er is:"&str(er)
  	while true
    		x = bpopdb("jobs2fit",6,"","[$desc]","L78","[Job_Nr]","L0","[Job_Nr]",sr,2,er,79,"",0)
    		screen shortrestore dsa
    		repaint off
    		if x = -1
      		screen clear box 1 1 sch scw 0 0 no-border
      		repaint off
      		return (-1)
    		else
      		jobnr = [Job_Nr]               'message "jobnr is:"&str(jobnr)

' 	ShowDeliveries()

 		     PrintJobSheet()

			ViewCtgPlan()

      		vloadif(dpath|"jobs2fit.vw")
    		end if
  	end while
END FUNCTION 'PrintJobs()


FUNCTION ShowDeliveries()
local y6 #thisvisit
	vloadif(dpath|"lststk_G.vw")
  	order change index "todays1.idx"
  	data query execute "jobsheet.dfq" index "job_reqn.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Job_Nr] = jobnr
'   and
'   [Itemtype] <> "O"
'   and
'   not(deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
    		messboxwait(" No goods to despatch for job"&jobnr|" ",0,0,1)
    		return (0)
  	end if
  	order sort execute dictionary "lst_stck.dfs" index "lst_stck"
  	data goto record first
  	$ftrs = [FtrCode]
  	data goto record next
  	for i = 2 to records                 'create text string of Job Nrs
    		x = chkstr([FtrCode],$ftrs)
    		if x = -1
      		$ftrs = $ftrs&[FtrCode]
    		end if
    		data goto record next
  	end for
' message "756\$ftrs is:"&str($ftrs)

  	repaint off
	vloadif(dpath|"delivr_2.vw")
	order change key "[Job_Nr]"
  	data query execute "jobsheet.dfq" index "sd1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' [Job_Nr] = jobnr
' and
' [Itemtype] <> "O"
' and
' not(deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
    		screen shortrestore psa
    		messboxwait(" NO deliveries booked ",0,0,1)
    		return (1)
  	end if
  	data query execute "showdel2.dfq" index "sd2.idx"	'
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Date_Out] = ftgdate
'   and
'   [Fitter_Code]=ftrcode
'   and
'   not(deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
    		screen shortrestore psa
    		messboxwait(" NO deliveries booked for"&ftgdate,0,0,1)
    		return (1)
  	end if

    	remove(X_path|"X_ftrout.*")
    	data query execute "not_del.dfq" Smart4 X_path|"X_ftrout" fields \
"[Job_Nr;Date_Out;Product_MRC;Description_MRC;QuantOut;Cost;Width;Fitter_Name;CustOrd_Name;Delivery_Address_1;Delivery_Address_2;Del_City;Del_Postcode;Balance_Due;Instructions;Fitting_Comment;Customer_Code;Itemtype]"
    	vunloadif("X_ftrout.vws")

    	remove(X_path|"Xftrout1.*")
	vloadif(dpath|"delivr_3.vw")
	order change key "[Customer_Code]"
  	data query execute "statmnt2.dfq" Smart4 X_path|"Xftrout1" fields "[Customer_Code;Customer_Name;Contact_Name;Home_Tel;Office_Tel;Mobile/Other_Nr;Credit_Status;Title]"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' [Customer_Code] = custcode
' and
' not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
    		screen shortrestore psa
    		messboxwait(" NO deliveries booked ",0,0,1)
    		return (1)
  	end if
    	vunloadif("Xftrout1.vws")
     _SWIP_Crystal(Xreppath|"ftroutTJ","S",0,1,"")
END FUNCTION ' ShowDeliveries()


FUNCTION PrintJobSheet()
	custcode  = [Customer_Code]

' find req'ns (not deleted) for jobnr & fitter
	vloadif(dpath|"lststk_G.vw")
  	order change index "todays1.idx"
  	data query execute "jobsheet.dfq" index "job_reqn.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Job_Nr] = jobnr
'   and
'   [Itemtype] <> "O"
'   and
'   not(deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	if cerror
    		messboxwait(" No goods to despatch for job"&jobnr|" ",0,0,1)
    		return (0)
  	end if
  	order sort execute dictionary "lst_stck.dfs" index "lst_stck"
  	data goto record first
  	$ftrs = [FtrCode]
  	data goto record next
  	for i = 2 to records                 'create text string of Job Nrs
    		x = chkstr([FtrCode],$ftrs)
    		if x = -1
      		$ftrs = $ftrs&[FtrCode]
    		end if
    		data goto record next
  	end for
	x=strcount($ftrs)                    'message "x is:"&str(x)
  	#ftrs = ptval                        'message "#ftrs is:"&str(#ftrs)
	for i = 1 to #ftrs
    		ftrcode = group($ftrs,i)           '
    		vloadif(dpath|"ftr_list.vws")
    		ftrname = filelookup([Fitter_Code],[Nickname],ftrcode)
    		vloadif(dpath|"lststk_G.vw")
    		data query execute "ftrsheet.dfq" index "ftrsheet.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [FtrCode] = ftrcode
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
' sort by LstOrder
		order sort execute dictionary "lst_stck" index "lststck1"
     	remove(X_path|"X_ftrout.*")
     	data query execute "not_del.dfq" Smart4 X_path|"X_ftrout" fields \
 "[Job_Nr;Date_Out;Product_MRC;Description_MRC;QuantOut;Cost;Width;Fitter_Name;CustOrd_Name;Delivery_Address_1;Delivery_Address_2;Del_City;Del_Postcode;Balance_Due;Instructions;Fitting_Comment;Customer_Code;Itemtype]"
     	vunloadif("X_ftrout.vws")
     	remove(X_path|"Xftrout1.*")

	 	vloadif(dpath|"delivr_3.vw")
	 	order change key "[Customer_Code]"
	   	data query execute "statmnt2.dfq" Smart4 X_path|"Xftrout1" fields "[Customer_Code;Customer_Name;Contact_Name;Home_Tel;Office_Tel;Mobile/Other_Nr;Credit_Status;Title]"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' [Customer_Code] = custcode
' and
' not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
	   	if cerror
	    		screen shortrestore psa
     		messboxwait(" NO deliveries booked ",0,0,1)
     		return (1)
	   	end if
     	vunloadif("Xftrout1.vws")
    		vloadif(dpath|"lststk_G.vw")
    		order change index "lst_stck.idx"
 		if [PartJob]<>"Y"
		     _SWIP_Crystal(Xreppath|"ftroutTJ","P",0,1,"")
		else
		     _SWIP_Crystal(Xreppath|"ftroutPJ","P",0,1,"")
		end if

' 	     _SWIP_Crystal(Xreppath|"X_ftrout","S",0,1,"")
  	end for
  	vunloadif("ftr_list.vws")
' 	vloadif(dpath|"jobs2fit.vw")
  	return (0)
END FUNCTION 'PrintJobSheet()


FUNCTION ViewCtgPlan()
local $new $br $pdfstr
	single-step off
	Background()
  	repaint off
	x=remove("C:\SW2K_NEW\programs\ctgplan1.txt")			'message "remove x) is:"&str(x)
  	error off
' message "jobnr is:"&str(jobnr)
	$br=left(jobnr,1)
     $branch=case $br ("S","Sheen")("W","Warehouse")("P","Putney")("F","Fulham")("R","Raynes") ' message "$branch) is:"&str($branch)
'    	tools os "dir V:\Fulham\F23299*.pdf /O:N /B > C:\SW2K_NEW\programs\ctgplan.txt"
' message "$branch) is:"&str($branch)

	x="V:\"|$branch|"\"|jobnr|"*.pdf"					'
' message "filename is:"&str(x)
    	tools os "dir V:\"|$branch|"\"|jobnr|"*.pdf /O:N /B > C:\SW2K_NEW\programs\ctgplan1.txt"
' message "chk text file"
	if len("ctgplan1.txt")<1
		messboxwait(" No ctg plan found for"&jobnr,0,0,1)
		return (0)
	else
		messboxwait(" Showing Cutting Plans for"&jobnr,0,0,1)
	end if
     fopen "C:\SW2K_NEW\programs\ctgplan1.txt" as 1
	$new="Y"
   	while eof(1) = 0
      	fread 1 into $pdfstr						'message "$pdfstr) is:"&str($pdfstr)
		if len($pdfstr)=0
			if $new="Y"
	 			messboxwait(" No ctg plan found for"&jobnr,0,0,1)
			end if
		    	fclose 1
			return (0)
		end if
  		$pdf="V:\"|$branch|"\"|$pdfstr					'message "$pdf is:"&str($pdf)
		if file($pdf) = TRUE
			_GEMS_ShellEx("open",$pdf,"",1)
			$new="N"
		else
			messboxwait("Cutting Plan `"|$pdf|"' not found - report to Office ",0,0,1)
			Background()
			exit while
		end if
    	end while
    	fclose 1
END FUNCTION ' ViewCtgPlan()

