'CHKORDER - check costs/req'ns of selected order

external   messbox() vloadif() dpath sch scw bpopdb() fgp bgp
external   progress() delidxrec() fentrybox() shopmask reqnpath
external   cpath #margin_A #margin_B #margin_C #margin_D wraptext()

public     ptstr psa ptval #jobrec jobnr

global     x y1 y2 y3 ReturnToMenu() y deladdr c1 w CheckJobNr()
global     deladdr1 deladdr2 deladdr3 deladdr4 custname $instruct
global     custaddr r2 r3 bline current1
global     cust_title
global     j jobdesc ftgcomm ftginstr
global     S_details ShowAllReqs() ShowInstructions() h1 h2 dr1 dr2 dr3
global     ShowDetails() CheckOrder() ShowAddress()
global     #margin #netinv #totcost #percentmargin
global     r1 #count $fail #recnr Navrecs() custline NoReqns()


MAIN
single-step off
'   quiet off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  current1 = reqnpath|jobnr|".idx"

  $fail = ""
  r1 = 7
  while true
    x = CheckJobNr()
    if x = -1
      exit while
    end if
  end while
  ReturnToMenu()
END MAIN


FUNCTION CheckJobNr()                   ' finds Job & updates Cust_Ord
local l1 c3 c2 c1 ques $reqstr
  $instruct = ""
  smartpoke $_ins 0
  while true
  repaint off
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ Find Job Nr                                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  x = fentrybox(" Enter Order Nr or {Esc} to exit ",6,shopmask,"")
  if x = 0
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    if len(ptstr)=5
      jobnr=left(ptstr,1)|"0"|right(ptstr,4)
    else
      jobnr = ptstr
    end if
    vloadif(dpath|"authorzB.vw")
    order change key "[Job_Nr]"
    data find "[Job_Nr]" equal jobnr options ""
    if cerror
      messbox(" Job Nr"&jobnr&"not found ",0,0,1)
      screen clear box 1 1 sch scw 0 0 no-border
      return (0)
    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
      vloadif(dpath|"authorzA.vw")
      continue while
    end if
  elseif x = -1
    screen clear box 1 1 sch scw 0 0 no-border
    return (-1)
  end if
end while
END FUNCTION ' CheckJobNr()


FUNCTION ShowInstructions()
  dr1 = 8
  c1 = 1
  h1 = 5
  h2 = 4
  dr2 = dr1+h1
  dr3 = dr2+h2
  w  = 37
  jobdesc  = [Description]
  ftginstr = [Instructions]
  ftgcomm  = [Fitting_Comment]
  jobdesc  = @if(len(jobdesc)=0,"Not known",jobdesc)
  ftginstr = @if(len(ftginstr)=0,"Not known",ftginstr)
  ftgcomm  = @if(len(ftgcomm)=0,"Not known",ftgcomm)

  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"

  x = wraptext(dr2,c1,dr2+h2,c1+w,15,1,ftginstr,"L",1,0,0)
  screen print dr2 c1+2 15 1 "Other notes (eg appointment times)"

  x = wraptext(dr3,c1,dr3+h2,c1+w,15,1,ftgcomm,"L",1,0,0)
  screen print dr3 c1+2 15 1 "Comments re Fitting"

END FUNCTION ' ShowInstructions()


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 CheckOrder()
  cust_title = [Cust_Title]
  custname   = [Customer_Name]
  custline   = format(jobnr&"-"&cust_title&custname,"L80")
  screen print 1 1 14 1 custline

  #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)

  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

  ShowAddress()

  ShowDetails()

  ShowInstructions()

  screen save 1 1 21 42 S_details

  while true
    if tablecount([Product_MRC]) = 0
      NoReqns()
      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
    exit while
  end while

  while true
'     y2 = format(" Net Invoice"&currency(#netinv),"M40")|format("Total costs"&currency(#totcost),"M40")
    y2 = format(" Net Invoice"&currency(#netinv)&"   -   Costs"&currency(#totcost)&"   -   Margin"&currency(#netinv-#totcost),"M80")
    screen print 22 1 14 12 y2
    y3 = format("{Esc} to exit","M80")
    screen print 23 1 15 1 y3
    x = inchar                    'message "x is:"&str(x)
    if x = 763                ' Esc - abort
      screen clear box 1 1 sch scw 0 0 no-border
      repaint off
'       vloadif(dpath|"authorzA.vw")
'       order change index "un_auth.idx"
      return (-1)
    end if
  end while
END FUNCTION ' CheckOrder()


FUNCTION ShowAllReqs()
  vloadif(dpath|"ord_rejc.vw")
  if file(current1) = 1
    order change index current1
    if records = 0
      messbox(" No requisitions found ",0,0,1)
      return (-1)
    end if
  else
    order change key "[Job_Nr]"
    data query execute "job_reqn.dfq" index current1
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      messbox(" No requisitions found ",0,0,1)
      return (-1)
    end if
  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 ShowDetails()
local c1 c2 c3 c4 d1 d2 d3 d4 cc1 cr1 cc2 cr2 dc1 dr1 dc2 dr2 pc1 pr1 pc2 pr2 \
      ordets p1 p2 p3 p4 p5 p6 p7

  cr1 = 3
  cc1 = 1
  cr2 = cr1+5
  cc2 = cc1+37
  dr1 = cr1
  dc1 = 42
  dr2 = cr2
  dc2 = dc1+37
  pr1 = cr2+2
  pc1 = cc1+14
  pr2 = pr1+8
  pc2 = pc1+50

  screen clear box cr1 cc1 cr2 cc2 15 1
  c1 = left(deladdr1|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c2 = left(deladdr2|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c3 = left(deladdr3|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c4 = left(deladdr4|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  screen print cr1 cc1+1 15 1 "ÿDelivery addressÿ"
  screen print cr1+1 cc1+2 15 1 c1
  screen print cr1+2 cc1+2 15 1 c2
  screen print cr1+3 cc1+2 15 1 c3
  screen print cr1+4 cc1+2 15 1 c4
  screen save cr1 cc1 cr2 cc2 custaddr
END FUNCTION 'ShowDetails()


FUNCTION ShowAddress()
  cust_title   = [Cust_Title]
  custname     = [Customer_Name]
  deladdr1     = [Delivery_Address_1]
  deladdr2     = [Delivery_Address_2]
  deladdr3     = [Del_City]
  deladdr4     = [Del_Postcode]
END FUNCTION 'CopyCustomer()


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()
