'COSTSALE - cost of sales

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()
external   X_path _SWIP_Crystal() Xreppath

public     ptstr indate monthend fullmonth briefmonth enddate
public     choice $branch $shop_name startmth

global     x ReturnToMenu() CurrentCosts() LateCosts() PostDate()
global     p1 p2 p3 p4 p5 p6 ChooseShop() ChooseDate()


MAIN
  single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  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
  p5 = 1    ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1    ' p6 = nr of copies
while true
  x = ChooseDate()
  if x = -1
'     ReturnToMenu()
	exit while
  end if

  while true
    file unload all
'     vloadif(dpath|"sal_rep1.vw")
    x = ChooseShop()
    if x = -1
      exit while
    end if
    x = CurrentCosts()
    if x = 1
      exit while
    end if
'     x = LateCosts()
'     if x = 1
'       exit while
'     end if
'     exit while
  end 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 CurrentCosts()
  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_rep0.dfq" INDEX "first_0.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' mid([Reference_Nr],2,1)<>"R"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  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" Smart4 "cost1"
'   p1 = "cosales1.dfr"
  vunloadif("X_COS1.vws")
  remove(X_path|"X_COS1.*")
  data query execute "not_del.dfq" Smart4 X_path|"X_COS1" fields "[cosales1|Other]"
  _SWIP_Crystal(Xreppath|"X_COS1","P",0,1,"")

'? 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()
' message "L149 - costs NOT posted"
    else
      messboxwait(" Cannot post until printed ",0,0,1)
    end if
  end if
  file unload all
END FUNCTION ' CurrentCosts()


FUNCTION LateCosts()
' find late costs
message "startmth is:"&str(startmth)
' repaint on
' repaint
' single-step on
  vloadif(dpath|"cosales1.vw")
  order change INDEX "first_0.idx"
  data query execute "cos_rep2.dfq" INDEX "second_0.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' days([Invoice_Date])>=days("01/01/1996")
' and
' 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 "salrep_0.idx" fields [Inv_Nr] descending

  data cross-tab execute "cosales1.dfs" Smart4 "cost2"

'? print Summary of Late Costs
'   p1 = "cosales2.dfr"
'   PrintReport(p1,p2,p3,p4,p5,p6)

  vunloadif("X_COS1.vws")
  remove(X_path|"X_COS1.*")
  data query execute "not_del.dfq" Smart4 X_path|"X_COS1" fields "[cosales1|Other]"
  _SWIP_Crystal(Xreppath|"X_COS2","P",0,1,"")

'? 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"
message "L149 - costs NOT posted"
'       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()


'   remove(X_path|"X_mth_o.*")
'   vunloadif("X_inv_a.vws")
'   ClearHardSpaces()
'   data query execute "not_del.dfq" Smart4 X_path|"X_mth_o" fields\
'   "[Var_Nr;Date;CustOrd_Name;Amount_Gross;shop;Date_To;Nr_Orders;Avg_Order;Tot_Order_Net;Tot_Order_Gross]"
'   _SWIP_Crystal(Xreppath|"$inv_1","P",0,1,"")
'   _SWIP_Crystal(Xreppath|"$inv_1","EP",0,1,invpath|$nextinvnr|".pdf")

