'RANGEREP - print various combinations on req'ns by Product Range for
'           selected periods.


external  dpath sch scw Background() vloadif() remove() PrintReport()
external  fentrybox() chkdate() messbox() progress() vunloadif() vkeybox()
external  messboxwait() makeidx() addidxrec() popuplist() findpopup()

public    ptstr startdate enddate ptval $wiptitle $init_branch

global    Dates() ReqnTypes() x ReturnToMenu() #total BranchReqs() reqtype
global    p1 p2 p3 p4 p5 p6 NrCopies() $ch_branch FileTypeReport()


' STOCK   "wip_1.vw" - "stk1.dfw" - file "wip_sum1" - "wip_1.dfr"
' ANCL    "wip_2.vw" - print "wip_2.dfr"
' BESPOKE   cross-tab "wip_3.dfw" - file "wip_sum3" - print "wip_3.dfr"
' WIP       cross-tab "wip_4.dfw" - file "wip_sum4" - print "wip_4.dfr"
' PT Gds    cross-tab "wip_4.dfw" - file "wip_sum5" - print "wip_5.dfr"
' PT Rqns   cross-tab "wip_5.dfw" - file "wip_sum6" - print "wip_6.dfr"


MAIN
'(str,str,str,int,int,int)
single-step off
  file unload all
  Background()
  remove(dpath|"rnge_sum.*")

  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

  while true
    if left(dpath,1)="L" or left(dpath,1)="N"
      messboxwait(" These reports should be prepared from local backup files ",0,0,1)
      exit while
    end if

    ReqnTypes()
    if x = -1
      exit while
    end if

    Dates()
    if x = -1
      exit while
    end if

    BranchReqs()

    FileTypeReport()

    exit while

  end while

  ReturnToMenu()

END MAIN


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 ReqnTypes()
  while true
    if vkeybox(8,32,"1All 1Bespoke 1Stock","Press {Esc} to exit") = 0
      reqtype=lower(ptstr)
      exit while
    else
      return (-1)
    end if
  end while
END FUNCTION 'ReqnTypes()


FUNCTION Dates()
local ld
' |
  while true
    startdate="01/"|right(date2((days(today)-90)),7)
    x = fentrybox(" Enter Start Date for Product Range Report ",10,"##\/##\/####",startdate)
    if x = 0
      startdate = ptstr
      if chkdate(startdate,1) = -1
        messbox(" Incorrect date - re-enter ",0,0,1)
        continue while
      end if
    elseif x = -1
      return (-1)
    end if
    for ld = 31 to 28 step -1
      enddate = str(ld)|"/"|right(date2((days(today)-30)),7) 'message "enddate is:"&str(enddate)
      if chkdate(enddate,1) = -1
        continue for
      else
        exit for
      end if
    end for
    x = fentrybox(" Enter End Date for Product Range Report ",10,"##\/##\/####",enddate)
    if x = 0
      enddate = ptstr
      if chkdate(enddate,1) = -1
        messbox(" Incorrect date - re-enter ",0,0,1)
        continue while
      end if
      if days(startdate)>=days(enddate)
        messboxwait(" End date is same or earlier than Start ",0,0,1)
        continue while
      end if
      exit while
    elseif x = -1
      return (-1)
    end if
  end while
END FUNCTION 'Dates()


FUNCTION BranchReqs()
'choose branch - ALL - ALL RETAIL - INDIVIDUAL
local s1 s2 s3 s4 s5 s6 s7 $locn
  $locn = "All"
' case base ("R","Raynes")("S","Sheen")("F","Fulham") else "HeadÿOffice"
  s1 = "All"
  s2 = "Fulham"
  s3 = "Putney"
  s4 = "Raynes"
  s5 = "Sheen"
  s6 = "Trade"
  s7 = "Warehouse"
  while true
    x = findpopup(8,60,15,s1&s2&s3&s4&s5&s6&s7,$locn,"Branch",1,0)
    if x = -1
      return (-1)
    end if
    $ch_branch = ptstr
    $init_branch = left(ptstr,1)
' message "$ch_branch is:"&str($ch_branch)
    return (0)
  end while
END FUNCTION 'BranchReqs()


FUNCTION NrCopies()
  while true
    x = fentrybox(" Enter Nr of copies (max=3) ",1,"#","1")
    if x = -1
      continue while
    end if
    ptval = value(ptstr)
    if ptval > 3
      continue while
    else
      return (ptval)
    end if
  end while
END FUNCTION ' NrCopies()


FUNCTION FileTypeReport()
'load req'n file
  progress(15,10," Searching for requisitions ",0)

  vloadif(dpath|"rangerep.vw")

  data query execute "rngerep1.dfq" index "rngerep1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' days(date2([Date_Requisitioned]))>=days(date2(startdate))
' and
' days(date2([Date_Requisitioned]))<=days(date2(enddate))
' and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No req'ns for period chosen ",0,0,1)
    return (-1)
  end if

'select subset of records for ALL/STOCK/BESPOKE
  if reqtype="b" 'search for all bespoke types B J O T W
    data query execute "rngerepB.dfq" index "rngerep2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Item_Type] = "B"
' and NOT(deleted)
' or
' [Item_Type] = "J"
' and NOT(deleted)
' or
' [Item_Type] = "T"
' and NOT(deleted)
' or
' [Item_Type] = "W"
' and NOT(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      messboxwait(" No req'ns for period chosen ",0,0,1)
      return (-1)
    end if
  elseif reqtype="s" 'search for all stock types A C S V
    data query execute "rngerepS.dfq" index "rngerep2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Item_Type] = "A"
' and NOT(deleted)
' or
' [Item_Type] = "C"
' and NOT(deleted)
' or
' [Item_Type] = "S"
' and NOT(deleted)
' or
' [Item_Type] = "V"
' and NOT(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      messboxwait(" No req'ns for period chosen ",0,0,1)
      return (-1)
    end if
  end if

'select subset of records for Branch
' message "$ch_branch is:"&str($ch_branch)
' message "$init_branch is:"&str($init_branch)
  if $init_branch<>"A"
    data query execute "rngerep2.dfq" index "rngerep3.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' left([Reference_Nr],1)=$init_branch
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      messboxwait(" No req'ns for branch and period chosen ",0,0,1)
      return (-1)
    end if
  end if

' remove "N/A" types
  data query execute "rngerep3.dfq" index "rngerep4.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Type]<>"N/A"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No req'ns for branch and period chosen ",0,0,1)
    return (-1)
  end if
  data cross-tab execute "rngerep1.dfw" Smart4 dpath|"rnge_sum"
  order sort now dictionary "rngerep5.idx" fields "[F0002]" descending
  p1 = "rngerep1.dfr"        ' p1 = report definition ("ord_stck.dfr")
  PrintReport(p1,p2,p3,p4,p5,p6)       '
END FUNCTION ' Stk_Bals()


