'AGED_DRS - Full list or individual for each shop

external   dpath sch scw vloadif() vunloadif() messbox() base progress()
external   cpath vkeybox() PrintReport() navrecs() progtag() fgi bgi
external   fentrybox() chkdate() popuplist() ChooseBranch() $menu
external   messboxwait() ChooseShop() remove() Background()
external   X_path _SWIP_Crystal() Xreppath _GEMS_ShellEx()

public     ptstr $shop_init value1 $REPORT_DATE $SHOP_NAME $MONTH_TXT $response
public     $branch ptval jobnr dsa $shop orderdate

global     per9 per8 per7 per0 per1 $stat i ftgdate x ReturnToMenu()
global     ChooseMths() $check SelectDrs()
global     p1 p2 p3 p4 p5 p6 CheckComplete() ChooseDate()


MAIN
single-step off
  	quiet on
    	Background()
  	vloadif(dpath|"ageddr1.vw")
  	order change key "[Job_Nr]"

  	ChooseDate()

  	while true
    		$shop_name = ChooseBranch(8,33,1)
    		$shop_init = upper(left($shop_name,1))
    		$shop      = $shop_name
    		if $shop_name = -1
      		ReturnToMenu()
'          return (-1)
    		end if
    		screen shortrestore dsa
    		vloadif(dpath|"ageddr1.vw")
    		SelectDrs()
    		Background()
     end while

  	ReturnToMenu()

END MAIN


FUNCTION ChooseDate()
' local orderdate
  	while true
' message "today is:"&str(today)
    		x = fentrybox(" Enter Base Date for Ageing ",10,"##\/##\/####",today)
' message "x) is:"&str(x)
    		if x = 0
      		orderdate = ptstr
      		if chkdate(orderdate,1) = -1
        			messbox(" Incorrect date - re-enter ",0,0,1)
        			continue while
      		end if
      		exit while
    		end if
  	end while
  	$report_date = date3(orderdate)
END FUNCTION ' ChooseDate()


FUNCTION ChooseMths()
  $MONTH_TXT = " showing ALL debts on "
  value1 = 0
  return (0)
END FUNCTION


FUNCTION CheckComplete()
local tblk1 tblk2 tmax
  error off
  tblk1 = not(iserr(tablelookup([Expect_Fitting_Date],days([Expect_Fitting_Date])>0)))
  if tblk1 = 0
    $stat = "N"
    ftgdate = blank
    return (0)
  end if
  error off
  tblk2 = iserr(tablelookup([Expect_Fitting_Date],[Expect_Fitting_Date]=blank))
  if tblk2 = 0
    ftgdate = tablemax([Expect_Fitting_Date]) ' message "tmax is:"&date2(tmax)
    $stat = "I"                     ' message "Some deliveries"
  else
    ftgdate = tablemax([Expect_Fitting_Date]) ' message "tmax is:"&date2(tmax)
    if days(tmax) < days(today)
      $stat = "D"                    ' message "ALL delivered!"
    else
      $stat = "I"                    ' message "Some deliveries - all booked out!"
    end if
  end if
END FUNCTION 'CheckComplete()


FUNCTION ReturnToMenu()
  Background()
  file unload all
' message "$menu is:"&str($menu)
'   if $menu = "shop"
  transfer cpath|"pm_menu.psl" in-memory
'   end if
END FUNCTION ' ReturnToMenu()


FUNCTION SelectDrs()
  	$branch = $shop_name
  	while true
    		ChooseMths()                         'message "$shop is:"&str($shop)
		Background()
    		progress(15,10," Please wait ... looking for debtors ",0)
    		order change key "[SalesAnalysis]"
    		data query execute "shops1.dfq" INDEX "first.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [SalesAnalysis]=$shop_init
'   and
'   not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    		if cerror
      		messboxwait(" No outstanding debtors at"&$shop_name|" ",0,0,1)
      		exit while
    		end if
    		order change index "first.idx"
    		data query execute "aged_drs.dfq" INDEX "second.idx"   ' choose o/s balances
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   (days(today) - days([Invoice_Date]) >= value1
'   and
'   days([Invoice_Date]) > 0
'   and
'   [Balance_Due] > 0.009     ' 1p or more
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    		if cerror
      		messboxwait(" No outstanding debtors at"&$shop_name|" ",0,0,1)
      		exit while
    		end if
    		if $shop_init = "P"
      		data query execute "ageddrsP.dfq" INDEX "third.idx"   ' choose o/s balances
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Invoice_Date])>days("31/08/2001")
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
      		if cerror
        			messboxwait(" No outstanding debtors at"&$shop_name|" ",0,0,1)
        			exit while
      		end if
    		end if
    		order sort now "aged_drs" fields "[Invoice_Date]" ascending
    		vunloadif("X_agddrs.vws")
    		remove(X_path|"X_agddrs.*")
    		data query execute "not_del.dfq" Smart4 X_path|"X_agddrs" fields "[Customer_Code|repdate;CommHeld]"
    		_SWIP_Crystal(Xreppath|"ageddrs1","P",0,1,"")
    		screen clear box 1 1 sch scw 0 0 no-border
'     PrintReport("ageddrs1.dfr","Aged Debtors",p3,p4,p5,p6)
    		exit while
  	end while

  	Background()

  	messbox(" Print uninvoiced orders? (y/n) ",1,1,1)
  	if ptstr == "y"
    		progress(15,10," Please wait ... looking for uninvoiced orders ",0)
		vloadif(dpath|"ageddr1.vw")
		order change physical
  		data query execute "despat_4.dfq" index "uninv.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Order_Status]<>"M"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  		if cerror
      		messboxwait(" No uninvoiced orders for"&$shop_name|" ",0,0,1)
    			return (-1)
  		end if
    		data query execute "mr_drs8.dfq" INDEX "second.idx"   ' choose o/s balances
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' days([Invoice_Date]) = 0 and
' days([Date_Of_Order])<=days(orderdate) and
' [SalesAnalysis]=left($shop,1)
' and
' not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    		if cerror
      		messboxwait(" No uninvoiced orders for"&$shop_name|" ",0,0,1)
    		else
      		order sort now "ageddrs5" fields "[Date_Of_Order]" ascending
      		screen clear box 1 1 sch scw 0 0 no-border
      		vunloadif("X_mrdrs5.vws")
      		remove(X_path|"X_mrdrs5.*")
'       remove("Uninvoiced.pdf")
      		data query execute "not_del.dfq" Smart4 X_path|"X_mrdrs5" fields "[Job_Nr;Date_Of_Order;Customer_Name;Invoice_Total;Bal_OS;TotalCosts;Br_KA;repdate]"
      		_SWIP_Crystal(Xreppath|"X_drs6","P",0,1,"")
'       _SWIP_Crystal(Xreppath|"X_drs5","EP",0,1,"uninvoiced.pdf")
'       _GEMS_ShellEx("open","uninvoiced.pdf","",1)
    		end if
  	end if
END FUNCTION ' SelectDrs()


