'MTHLYSAL - monthly invoiced sales report

external   vkeybox() sch scw progress() messbox() vloadif() base $menu
external   vunloadif() dpath fentrybox() chkdate() areas greeting cpath
external   PrintReport() popuplist() fgp bgp statemntpath $shop_name $shop
external   remove() ChooseBranches()
external   X_path _SWIP_Crystal() Xreppath

public     ptstr indate monthend fullmonth briefmonth enddate
public     choice startmth $branch

global     x ReturnToMenu() s1 s2 s3 s4 s5 s6 s7 leftjob sw
global     Report() p1 p2 p3 p4 p5 p6 ChooseDate() $place
' global     StoreReport()


MAIN
  single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  clear $shop_name
  file unload all

  while true
    x = ChooseBranches(8,33,areas)
    if x = -1
      exit while
    end if
    x = ChooseDate()
    if x = -1
      continue while
    end if
    x = Report()
    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 ChooseDate()
local y ld
  while true
'     monthend = right(today,5)
    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()
  progress(fgp,bgp," Creating report for"&$shop_name|" ... please wait ",0)
  vloadif(dpath|"sal_rep3.vw")
  $branch=$shop
  data query execute "dlyrout1.dfq" INDEX "first.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Branch]=$branch
' and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No Sales this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if

  data query execute "sal_rep1.dfq" INDEX "second.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' º   right(date2([Invoice_Date]),7) ! monthend                       º
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No Sales this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if

  if cerror
    messbox(" No Sales this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if
  order sort now dictionary "sal_rep.idx" fields [Inv_Nr] ascending
  screen clear box 1 1 sch scw 0 0 no-border

  	remove(X_path|"X_mth_s.*")
  	data query execute "not_del.dfq" Smart4 X_path|"X_mth_s" fields\
"[Inv_Nr;Job_Nr;Invoice_Date;CustOrd_Name;Delivery_Address_1;Net_Invoice;Invoice_Total;shop;Date_To]"
  	vunloadif("X_mth_s.vws")
'   ClearHardSpaces()
  	sw=_SWIP_Crystal(Xreppath|"mth_sal1","S",0,1,"")
' message "sw is:"&str(sw)
END FUNCTION 'Report()

