'SAL_REP  - sales reports; new version to use VARIAT_N file

external   dpath sch scw vloadif() vunloadif() chkdate() lpath messbox()
external   cpath fentrybox() vkeybox() progress() fgp bgp PrintReport()
external   popuplist() ChooseBranch()

public     ptstr indate monthend fullmonth briefmonth enddate
PUBLIC     $shop_init $REPORT_DATE $SHOP_NAME $MONTH_TXT $response

GLOBAL     Date() x Report() ReturnToMenu() p1 p2 p3 p4 p5 p6


MAIN
single-step off
  QUIET ON
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off

'(str,str,str,int,int,int)
'   p1 =                  ' p1 = report definition ("ord_stck.dfr")
  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
    vloadif(dpath|"sal_rep1.vw")
    $shop_name = ChooseBranch(8,33,1)
    $shop_init = upper(left($shop_name,1))  'message "$shop_name) is:"&str($shop_name)
    if $shop_name = -1
      exit while
    end if
' message "$shop_init is:"&str($shop_init)
' repaint on
' repaint
' single-step on
    vloadif(dpath|"sal_rep1.vw")

    x = Date()
    if x = -1
      continue while
    end if

    x = Report()
    if x = -1
      exit while
    end if

  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 Date()
local y ld
  while true
    monthend = right(today,7)
    x = fentrybox(" Confirm month for"&$shop_name|"'s report ",7,"*2#\/*4#",monthend)
    if x = 0
      monthend = ptstr
      fullmonth = "01/"|ptstr
      x = chkdate(fullmonth,1)
      if x = 0
        y = len(date3(fullmonth))
        y = y - 8
        briefmonth = monthname(fullmonth)|" '"|right(fullmonth,2)
        exit while
      else
        messbox(" Incorrect date - re-enter ",0,0,1)
      end if
    else
      return (-1)
    end if
    briefmonth = left(date3(fullmonth),3)
  end while
  for ld = 31 to 28 step -1
    enddate = str(ld)|"/"|monthend         'message "last_m_e is:"&str(last_m_e)
    if chkdate(enddate,1) = -1
      continue for
    else
      exit for
    end if
  end for
' message "enddate is:"&date3(enddate)
END FUNCTION 'Date()


FUNCTION Report()
'   IF $shop_init !! "A"                        ' find only those shops chosen above
message "$shop_init is:"&str($shop_init)
'   vloadif(dpath|"sal_rep.vw")
  order change key "[Var_Nr]"
  progress(fgp,bgp," Creating report for"&$shop_name|" ... please wait ",0)
  data query execute "shops.dfq" INDEX "first.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³  left[Var_Nr,1]=$shop_init                                          ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No Sales this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if
  data query execute "sal_rep.dfq" INDEX "second.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' º   right(date2([Date]),5) ! monthend                       º
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No Sales this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if
  order sort execute "sal_rep.dfs" INDEX "sal_rep"
  screen clear box 1 1 sch scw 0 0 no-border
  p1 = "sal_rep1.dfr"
  PrintReport(p1,p2,p3,p4,p5,p6)
END FUNCTION 'Report()
