'SALREP_B - BOSS ONLY report for Sales showing % margin

external   dpath sch scw vloadif() vunloadif() chkdate() lpath messbox()
external   cpath fentrybox() vkeybox() progress() fgp bgp PrintReport()
external   ChooseBranch() Background() shopstring shoplist
external   X_path _SWIP_Crystal() Xreppath remove()

public     ptstr indate monthend fullmonth briefmonth #fs_totinv #fs_totcost
PUBLIC     $SHOP, $REPORT_DATE, $SHOP_NAME, $MONTH_TXT, $response $shop_init

global     Date() x Report() p1 p2 p3 p4 p5 p6 i enddate


MAIN
single-step off
  QUIET ON
  file unload all
  Background()
'   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

  Date()

  while true
    x=messbox(" Prepare ALL individual reports for shops? (y/n) ",1,1,0)
    if x = -1
      exit while
    end if
    if ptstr == "y"
' message "shopstring is:"&str(shopstring)
' message "shoplist is:"&str(shoplist)
      for i = 1 to ((len(shoplist)+1)/2)
        $shop_name = group(shopstring,i)
        $shop      = group(shoplist,i) '
        $shop_init = group(shoplist,i) '
' message "Printing report for:"&str($shop_name)
' message "$shop) is:"&str($shop)
' message "$shopinit is:"&str($shop_init)
        Report()
      end for
    else
      $shop_name = ChooseBranch(8,33,1)
      if $shop_name = -1
        file unload all
        Background()
        exit main
      end if
      $shop = upper(left($shop_name,1))  '
' message "$shop_name is:"&str($shop_name)
      $shop_init=$shop
' message "$shop_init is:"&str($shop_init)
      Report()
    end if
  end while

  file unload all
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off

END MAIN


FUNCTION Report()
local #margin
  vloadif(dpath|"salrep_B.vw")
  order change physical
  progress(fgp,bgp," Creating report for"&$shop_name|" ... please wait ",0)
  data query execute "shops.dfq" INDEX "first.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
'   left([Job_Nr],1)=$shop_init
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
  if cerror
    messbox(" No Sales for"&$shop_name|" ",0,0,1)
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    vunloadif("sal_rep.vw")
    return (1)
  end if
' message "monthend is:"&str(monthend)
'   order change index "first.idx"
  data query execute "salrep_B.dfq" INDEX "second.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
' right(date2([Invoice_Date]),7)="09/2006"
' º   right(date2([Invoice_Date]),7) ! monthend                        º
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
  if cerror
    messbox(" No Sales this month at"&$shop_name|" ",0,0,1)
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    vunloadif("sal_rep.vw")
    return (1)
  end if
  order sort now dictionary "sal_rep.idx" fields "[Job_Nr]" ascending
'   order sort execute "salrep_B" INDEX "sal_rep"
  #fs_totinv  = filesum([Net_Invoice])
  #fs_totcost = filesum([TotalCost])

  vunloadif("Xsalrepb.vws")
  remove(X_path|"Xsalrepb.*")
'   ClearHardSpaces()
'   data query execute "not_del.dfq" Smart4 X_path|"Xsalrepb" fields\

  data query execute "not_del.dfq" Smart4 X_path|"Xsalrepb" fields "[Job_Nr;Inv_Nr;Invoice_Date;CustOrd_Name;Net_Invoice;TotalCost;Invoice_Total]"
  _SWIP_Crystal(Xreppath|"salrep_b","P",0,1,"")
'   _SWIP_Crystal(Xreppath|"$inv_1","EP",0,1,invpath|$nextinvnr|".pdf")

PrintReport("salrep_B.dfr","",p3,p4,p5,p6)
'   if ptstr == "p"
'     progress(fgp,bgp," Printing report for All shops ... please wait ",0)
'   elseif ptstr == "v"
'     progress(fgp,bgp," Printing report for All shops ... please wait ",0)
'   elseif ptstr == "f"
'     x = upper(left($shop_name,3))|"_"|left(monthend,2)|right(monthend,2)|".TXT"
'     progress(fgp,bgp," Printing report for All shops ... please wait ",0)
'   end if
END FUNCTION 'Report()


' FUNCTION Date()
' local y
'   while true
' '     monthend = right(to_date,5)
'     monthend = right(today,5)
'     fullmonth = "01/"|monthend
'     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
'     briefmonth = left(date3(fullmonth),3)
'   end while
' END FUNCTION 'Date()

FUNCTION Date()
local y ld
  while true
    monthend = right(today,7)
    x = fentrybox(" Confirm month for reports ",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()

' order sort now dictionary "a" fields "[Job_Nr]" ascending




