'ANCL_LST - lists receipts & dispatches of Stock Ancillaries

external vloadif() dpath fentrybox() chkdate() messbox() sch scw cpath
external PrintReport() remove() progress() progtag() fgi bgi Background()
external messboxwait()

public   ptstr monthend0 monthend1 monthend2 $title $itemtype basemonth B_mth
public   prodcode

global   x basicdate Date() briefmonth $init_title m0 m1 m2 y0 y1 $out $file
global   y2 PrepareReport() p1 p2 p3 p4 p5 p6 PrepareData() ReadDataRcvd() i
global   $line prodMRC ReadDataDespd() t1 t2 p $outxls


MAIN
single-step off
  Background()
  file unload all

  x = Date()
  if x = -1
    Background()
    file unload all
    exit main
  end if

  progress(15,10," Removing old files ",0)
  remove("ANCLLIST.*")
  $out    = "ancllist.txt"
  $outxls = "ancl"|left(monthend0,2)|right(monthend0,2)|".txt"
  remove($out)
  remove($outxls)

  progress(15,10," Preparing Ancillary Sales Analysis Report ",1)
  $itemtype = "A"

  PrepareData()

  PrepareReport()   ' message "Finished Stock report"
  fclose 2
  fclose 3
  messboxwait(" Report stored in"&$out|" ",0,0,1)
  tools text-editor "ancllist.txt"
  Keys Alt-P
  Keys Esc
  Background()
  file unload all
END MAIN


FUNCTION Date()
local y
  while true
    monthend0 = right(addmonths(today,-1),5)
    x = fentrybox(" Prepare Stock Ancillaries report for ",5,"*2#\/*2#",monthend0)
    if x = 0
      monthend0 = ptstr
      basemonth = "01/"|ptstr          ' message "basemonth) is:"&str(basemonth)
      x = chkdate(basemonth,1)
      if x = 0
        y = len(date3(basemonth))
        y = y - 8
        briefmonth = monthname(basemonth)|"˙"|str(year(basemonth))
        return (0)
      else
        messbox(" Incorrect date - re-enter ",0,0,1)
      end if
    else
      return (-1)
    end if
  end while
END FUNCTION 'Date()


FUNCTION PrepareData()
  vloadif(dpath|"ancllst1.vw")
  data query execute "carplst6.dfq" index "carplst1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   right(date2([Date_Received]),5) = monthend0
'   and
'   [Item_Type]="A"
'   and
'   not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No requisitions for this period ",0,0,1)
    return (1)
  end if

  vloadif(dpath|"ancllst2.vw")
  data query execute "carplst7.dfq" index "carplst2.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   right(date2([Date_Out]),5) = monthend0
'   and
'   [Itemtype]="A"
'   and
'   not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No requisitions despatched in this period ",0,0,1)
  end if
END FUNCTION ' PrepareData()


FUNCTION ReadDataRcvd()
  vloadif(dpath|"ancllst1.vw")
  order change index "carplst1.idx"
  data query execute "ancllst1.dfq" index "ancllst1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [Product_Code] = prodcode                                          ³
' ³ and                                                                ³
' ³ not (deleted)                                                      ³
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    t1 = 0
    return (0)
  end if
  p = 1
  t1 = filesum([Length_Received])
END FUNCTION ' ReadDataRcvd()


FUNCTION ReadDataDespd()
  vloadif(dpath|"ancllst2.vw")
  order change index "carplst2.idx"
  data query execute "ancllst1.dfq" index "ancllst2.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [Product_Code] = prodcode                                          ³
' ³ and                                                                ³
' ³ not (deleted)                                                      ³
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    t2 = 0
    return (0)
  end if
  p = 1
  t2 = filesum([QuantOut])
END FUNCTION ' ReadDataDespd()


FUNCTION PrepareReport()
  fopen $out as 2
  fopen $outxls as 3
  fwrite 2 from "Ancillaries received & despatched in"&briefmonth
  fwrite 2 from ""
  $line = "˙˙˙˙˙"|left("Product Name"|repeat(" ",40),40)&right("˙˙˙˙˙˙˙˙˙˙˙˙"|"Received",12)&right("˙˙˙˙˙˙˙˙˙˙˙˙"|"Despatched",12)
  fwrite 2 from $line
  $line = "˙˙˙˙˙"|repeat("=",66)
  fwrite 2 from $line
  vloadif(dpath|"products.vws")
  order change key "[Item_Type]"
  data query execute "ancllst2.dfq" index "listbesp.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Item_Type]="A"
'   and not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  order sort now dictionary "ancllst3.idx" fields "[Product_MRC]" ascending
  for i = 1 to records
    p = 0
    progtag(fgi,bgi,prodMRC)
    prodMRC = [Product_MRC]
    prodcode = [Product_Code]
    ReadDataRcvd()
    ReadDataDespd()
    if p = 1
      $line = "˙˙˙˙˙"|left(prodMRC|repeat(" ",40),40)&right("˙˙˙˙˙˙˙˙˙˙˙˙"|fixed(t1,2),12)&right("˙˙˙˙˙˙˙˙˙˙˙˙"|fixed(t2,2),12)
      fwrite 2 from $line
    end if
    $line = prodcode|","|prodMRC|","|fixed(t1,2)|","|fixed(t2,2)
    fwrite 3 from $line
    vloadif(dpath|"products.vws")
    data goto record next
  end for
END FUNCTION ' PrepareReport()

