'AGEDDRS - same as AGED_DRS but no ReturnToMenu

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
  screen clear box 1 1 sch scw 0 0 no-border
  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)
  p5 = 1    ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
'   p5 = 3    ' p5 = choose VIEW/PRINT/FILE;  1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1    ' p6 = nr of copies

  p4 = 1
  vloadif(dpath|"aged_drs.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()
    end if
    screen shortrestore dsa
    vloadif(dpath|"aged_drs.vw")
    SelectDrs()
    Background()
  end while

  ReturnToMenu()

END MAIN


FUNCTION ChooseDate()
local orderdate
  while true
    x = fentrybox(" Enter Base Date for Ageing ",10,"##\/##\/####",today)
    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)
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    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|"aged_drs.vw")
    order change physical
    orderdate=$report_date
    data query execute "mr_drs2.dfq" INDEX "fourth.idx"   ' choose o/s balances
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Invoice_Date]) = 0 and
'   days([Date_Of_Order])<=days(orderdate) and
'   [SalesAnalysis]=$shop
'   and
'   not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      messboxwait(" No uninvoiced orders for"&$shop_name|" ",0,0,1)
    else
      order sort now "uninvd_1" 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;Br_KA;repdate]"
      _SWIP_Crystal(Xreppath|"X_drs5","P",0,1,"")
'       _GEMS_ShellEx("open","uninvoiced.pdf","",1)
    end if
  end if

END FUNCTION ' SelectDrs()

