'CSTSALE1 - cost of sales calculated from INVOICED orders.
'	To be run after all invoices put on for previous Month (i.e. run after Suppliers' chqs printed)

external   vkeybox() sch scw progress() messbox() vloadif() base $menu
external   vunloadif() dpath fentrybox() chkdate() greeting cpath
external   PrintReport() popuplist() fgp bgp ChooseBranch() remove()
external   messboxwait() Background()

public     ptstr indate monthend fullmonth briefmonth enddate
public     choice $branch $shop_name startmth

global     x ReturnToMenu() LateCosts() PostDate()
global     p1 p2 p3 p4 p5 p6 ChooseShop() ChooseDate() InvoicedCosts()


MAIN
  single-step off
  Background()

  x = ChooseDate()
  if x = -1
    ReturnToMenu()
  end if

  while true
    file unload all
'     vloadif(dpath|"sal_rep1.vw")
    x = ChooseShop()
    if x = -1
      exit while
    end if
'     x = CurrentCosts()
    x = InvoicedCosts()
    if x = -1
      exit while
    end if
    x = LateCosts()
    if x = -1
      exit while
    end if
'     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(date2(days(today)-30),7)
    x = fentrybox(" Confirm month for report ",7,"*2#\/*4#",monthend)
'     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 "monthend is:"&str(monthend)
' message "enddate is:"&str(enddate)
  startmth = "01/"|right(monthend,8)
' message "startmth is:"&str(startmth)
END FUNCTION 'Date()


FUNCTION ChooseShop()
  $shop_name = ChooseBranch(8,33,1)  'message "$shop_name is:"&str($shop_name)
  if $shop_name = -1
    return (-1)
  end if
  $branch = upper(left($shop_name,1))  '
END FUNCTION ' ChooseShop()


FUNCTION InvoicedCosts()
  remove("cost?.*")
  progress(fgp,bgp," Creating report for"&$shop_name|" ... please wait ",0)
  vloadif(dpath|"cust_ord.vws")
  vloadif(dpath|"cosales1.vw")
  order change key "[Branch]"
  data query execute "dlyrout1.dfq" INDEX "first.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Branch]=$branch
' and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No Sales this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if
  data query execute "cos_rep1.dfq" INDEX "second.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' days([Invoice_Date])>=days(startmth)
' and
' days([Invoice_Date])<=days(enddate)
' and
' [Costed]=blank
' and
' not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No Sales this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if
  order sort now dictionary "sal_rep.idx" fields [Inv_Nr] descending
  data cross-tab execute "cosales1.dfs" data-file "cost1"

' print Summary of Current Costs
  p1 = "cosales1.dfr"
  PrintReport(p1,p2,p3,p4,p5,p6)

'? mark as Costed
  messbox(" Mark as COSTED in data file? (y/n) ",1,1,1)
  if ptstr == "y"
    messbox(" Has Costing Sheet printed correctly? (y/n) ",1,1,1)
    if ptstr == "y"
      PostDate()
    else
      messboxwait(" Cannot post until printed ",0,0,1)
    end if
  end if
  file unload all
'   screen clear box 1 1 sch scw 0 0 no-border
END FUNCTION ' InvoicedCosts()


FUNCTION LateCosts()
' find late costs

  vloadif(dpath|"cosales1.vw")
  order change INDEX "first.idx"

  data query execute "cos_rep3.dfq" INDEX "second.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' days([Invoice_Date])<days(startmth)
' and
' [Costed]=blank
' and
' not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No Late Costs this month at"&$shop_name|" ",0,0,1)
    return (1)
  end if
  order sort now dictionary "sal_rep.idx" fields [Inv_Nr] descending
  data cross-tab execute "cosales1.dfs" data-file "cost1"





'? print Summary of Late Costs
'   p1 = "cosales2.dfr"
'   PrintReport(p1,p2,p3,p4,p5,p6)
'? mark as Costed
'   messbox(" Mark as COSTED in data file? (y/n) ",1,1,1)
'   if ptstr == "y"
'     messbox(" Has Costing Sheet printed correctly? (y/n) ",1,1,1)
'     if ptstr == "y"
'       PostDate()
'     else
'       messboxwait(" Cannot post until printed ",0,0,1)
'     end if
'   end if
'   file unload all
END FUNCTION ' LateCosts()


FUNCTION PostDate()
  vloadif(dpath|"cosales1.vw")
  data query execute "costdate.dfq"  'index "costed.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   replace [Costed] = enddate
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
END FUNCTION ' PostDate()


