'CHK_COST - views req'ns & costs

external   messbox() vloadif() dpath sch scw bpopdb() jobnr fgp bgp
external   progress() delidxrec() userid fentrybox() shopmask
external   cpath #margin_A #margin_B #margin_C #margin_D wraptext()

public     ptstr psa ptval $dayftr

global     $newstat i ChkCosts() NoReqns() costline $line1
'  FindNr() z SetupDetails(0)
global     x y1 y2 ReturnToMenu() y deladdr c1 w $ordstat #count custline
global     deladdr1 deladdr2 deladdr3 deladdr4 custname navrecs()
global     custaddr r2 r3 bline cust_title j jobdesc ftgcomm ftginstr
global     S_details ShowAllReqs() h1 h2 dr1 dr2 dr3
global     CheckOrder()
global     #margin #netinv #totcost #percentmargin


MAIN
single-step off
'   quiet off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  while true
    x = ChkCosts()
    if x = -1
      exit while
    end if
  end while

  ReturnToMenu()

END MAIN


FUNCTION ChkCosts()
  while true
'     vloadif(dpath|"authorzA.vw")
    x = fentrybox(" Enter Order Nr or {Esc} to exit ",6,shopmask,"")
    if x = -1
      return (-1)
    elseif x = 0
      if len(ptstr)=5
        jobnr=left(ptstr,1)|"0"|right(ptstr,4)
      else
        jobnr = ptstr
      end if
    end if

    vloadif(dpath|"authorzB.vw")
    order change key "[Job_Nr]"
    data find "[Job_Nr]" equal jobnr options ""
    if cerror                               '   if none - then return
      messbox(" Job Nr not found ",0,0,1)
      continue while
    end if

    x = CheckOrder()
    if x = 0
      #count = records
      if #count = 0
        screen clear box 1 1 sch scw 0 0 no-border
        return (-1)
      end if
    elseif x = -1
      screen clear box 1 1 sch scw 0 0 no-border
      repaint off
      continue while
    end if
  end while
END FUNCTION ' ChkCosts()


FUNCTION CheckOrder()
  cust_title = [Cust_Title]
  custname   = [Customer_Name]
  #netinv    = [Net_Invoice]             'message "#netinv is:"&str(#netinv)
  #totcost   = tablesum([Cost])          'message "#totcost is:"&str(#totcost)
  #margin    = (#netinv-#totcost)/#netinv
  #percentmargin = round(#margin*100,2)'message "#percentmargin is:"&str(#percentmargin)

  custline = format(jobnr&"-"&cust_title&custname&"(invoiced at"&currency(#netinv)&"plus VAT)","L80")
'   custline = format(jobnr&"-"&cust_title&custname,"L80")
  screen print 1 1 14 1 custline
  costline = format(" Total costs are"&currency(#totcost),"L40")
  screen print 4 1 14 1 costline
  $line1 = format(" Press any key to continue ","M80")
  screen print 22 1 15 1 $line1

  if #margin < #margin_A
    screen print 2 11 12 0 "Gross Margin "|fixed(#percentmargin,1)|"%"
  elseif #margin < #margin_B
    screen print 2 11 14 0 "Gross Margin "|fixed(#percentmargin,1)|"%"
  elseif #margin < #margin_C
    screen print 2 11 10 0 "Gross Margin "|fixed(#percentmargin,1)|"%"
  elseif #margin < #margin_D
    screen print 2 11 14 0 "Gross Margin "|fixed(#percentmargin,1)|"%"
  else
    screen print 2 11 12 0 "Gross Margin "|fixed(#percentmargin,1)|"%"
  end if

  dr1 = 8
  c1 = 1
  h1 = 5
  h2 = 4
  dr2 = dr1+h1
  dr3 = dr2+h2
  w  = 37
  jobdesc  = [Description]
  jobdesc  = @if(len(jobdesc)=0,"Not known",jobdesc)
  x=wraptext(dr1,c1,dr1+h1,c1+w,15,1,jobdesc,"L",1,0,0)  '   message "x is:"&str(x)
  screen print  dr1 c1+2 15 1 "Job details"
  screen save 1 1 21 42 S_details
  while true
    if tablecount([Product_MRC]) = 0
      NoReqns()
      inchar
      exit while
    end if
    if tablecount([Product_MRC]) > 18
      repaint off
      ShowAllReqs()
    end if
    repaint on
    repaint
    screen shortrestore S_details
    screen print 1 1 14 1 custline
    inchar
    exit while
  end while

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"authorzA.vw")
  return (-1)
END FUNCTION ' CheckOrder()


FUNCTION NoReqns()
local rr1 rr2 rc1 rc2 cr1 cc1 c2
  rr1 = 3
  rc1 = 42
  rr2 = rr1+3
  rc2 = rc1+37
  screen clear box rr1 rc1 rr2 rc2 15 12
  c1 = format("No requisitions entered ","M35")
  c2 = format("for this job","M35")
  screen print rr1+1 rc1+2 15 12 c1
  screen print rr1+2 rc1+2 15 12 c2
END FUNCTION ' NoReqns()


FUNCTION ShowAllReqs()
  vloadif(dpath|"ord_rejc.vw")
  order change key "[Job_Nr]"
  data query execute "job_reqn.dfq" index "job_reqn.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No requisitions found ",0,0,1)
    return (-1)
  end if
  y2 = format(" Scroll to check all requisitions - {F10} to continue ","M80")
  screen print 22 1 15 1 y2
  repaint on
  repaint
  screen print 1 1 14 1 custline
  screen shortrestore S_details

  ptval=0
  while true
    ptval = navrecs()
    if ptval = {F10}
      exit while
    end if
  end while
  vloadif(dpath|"authorzB.vw")
END FUNCTION ' ShowAllReqs()


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 Navrecs()
local x bot psmode
  screen save scrheight 1 scrheight scrwidth bot
  smartpeek $_spndmes psmode
  if psmode = 1
       smartpoke $_spndmes 0
  end if
  while TRUE
    x = inchar
    if x = {Down}
      data goto record next
    elseif x = {Up}
      data goto record previous
    elseif x = {PgDn}
      data goto page next
    elseif x = {PgUp}
      data goto page previous
    elseif x = {^End}
      data goto record last
    elseif x = {^Home}
      data goto record first
    else
      exit while
    end if
  end while
  if psmode = 1
    smartpoke $_spndmes 1
  end if
  return (x)
END FUNCTION   'Navrecs()


FUNCTION SetupDetails()
'   if j = 0
  progress(fgp,bgp," Checking for existing order ",0)
  data find "[Job_Nr]" equal jobnr options "gw"
  if cerror                               '   if none - then return
    return (-1)
  else
    while true
      ftginstr = [Instructions]
'       ftgcomm  = [Fitting_Comment]
'       jobdesc  = [Description]
      jobdesc  = @if(len(jobdesc)=0,"Not known",jobdesc)
'       ftginstr = @if(len(ftginstr)=0,"Not known",ftginstr)
'       ftgcomm  = @if(len(ftgcomm)=0,"Not known",ftgcomm)
'       slotrec  = [Appt_Slots]
      repaint on
      repaint
      $ordstat = [Order_Status]
      screen save 1 1 8 scw S_details
      repaint off
      x = ShowAllReqs()
      if x = -1
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        return (0)                 ' = exit
      end if
    end while
  end if
END FUNCTION ' SetupDetails()


