'AUTHOR_O - authorizes Customer's orders after `signing-off' by shops.
'           checks Gross Profit etc - OFFICE use

' 09/02/95 - exception messages not written for Putney orders

external   messbox() vloadif() dpath sch scw bpopdb() jobnr fgp bgp
external   progress() popuplist() entryline() messline() city delidxrec()
external   cpath #margin_A #margin_B #margin_C #margin_D exception()
external   userid getidxrecs() messboxwait()

public     ptstr psa ptval $dayftr

global     x UnAuthorized() y1 y2 ReturnToMenu() y deladdr custaddr custpost
global     deladdr1 custpostcode deladdr2 deladdr3 deladdr4 custname i
global     hometel offtel offax mobile custaddr1 custaddr2 custcity custcontact cust_title
global     j CheckCustomer() MarginCheck() bline $update CheckDeliveries()
global     ReviewAddr() DeliveryAddr() CustomerAddr() S_details ShowAllReqs()
global     ShowDetails() CheckOrder() CopyAddresses() WriteDetails()
global     ReferShop() #margin #netinv #totcost #percentmargin mess1 cat
global     r1 #count WriteHOLD() WritePASS() $fail #recnr Navrecs()
global     UpdApptRecs() $newstat


MAIN
single-step off
'   quiet off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  $fail = ""
  $update = "N"
  r1 = 7
  while true
    x = UnAuthorized()
    if x = -1
      exit while
    end if
  end while

  ReturnToMenu()

END MAIN


FUNCTION CheckOrder()
' x=apinfo(ap_filep)         'message "Screen is:"&str(x)
  error off
  #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 14 3 12 0 "Gross Margin"
    screen print 15 6 12 0 fixed(#percentmargin,1)|"%"
  elseif #margin < #margin_B
    screen print 14 3 14 0 "Gross Margin"
    screen print 15 6 14 0 fixed(#percentmargin,1)|"%"
  elseif #margin < #margin_C
    screen print 14 3 10 0 "Gross Margin"
    screen print 15 6 10 0 fixed(#percentmargin,1)|"%"
  elseif #margin < #margin_D
    screen print 14 3 14 0 "Gross Margin"
    screen print 15 6 14 0 fixed(#percentmargin,1)|"%"
  else
    screen print 14 3 12 0 "Gross Margin"
    screen print 15 6 12 0 fixed(#percentmargin,1)|"%"
  end if

  CopyAddresses()

  x = ReviewAddr()
  if x = -1
    return (-1)
  elseif x = 0
    return (0)
  elseif x = 1
    return (1)
  end if
END FUNCTION ' CheckOrder()


FUNCTION ReviewAddr()
  while true
    ShowDetails()
    y1 = format("Change {C}ustomer/{D}elivery addresses or {S}undry details ","M70")
    screen print 21 5 15 1 y1
    y2 = format("{F10} to check requisitions - {Esc} to abandon","M70")
    screen print 22 5 15 1 y2

    while true
      x = inchar                       ' message "x is:"&str(x)
      if x = 99                        ' c - change Customer
        x = CustomerAddr()
        if x <> -1
          $update = "Y"
        end if
        exit while

      elseif x = 100                   ' d - change DELIVERY
        x = DeliveryAddr()
        if x <> -1
          $update = "Y"
        end if
        exit while

      elseif x = 115                ' s - change SUNDRY
        x = CheckCustomer()
        if x <> -1
          $update = "Y"
        end if
        exit while

      elseif x = 324                ' F10 - check reqns
        if $update = "Y"
          messbox(" Customer details changed - write to file? (y/n) ",1,1,1)
          if ptstr == "y"
            WriteDetails()
            $update = "N"
          else
            messbox(" Then why bother to change them? - write to file? (y/n) ",1,0,1)
            if ptstr == "y"
              WriteDetails()
              $update = "N"
            else
              messbox(" You'll be found out - don't say I didn't warn you ",0,1,1)
            end if
          end if
        end if

        screen save 1 1 sch-5 42 S_details
        if tablecount([Product_MRC]) = 0
          $fail = " NO requisitions for job"
          x = ReferShop()
          if x = 1               ' no more unauth's
            return (-1)
          end if
          order change index "un_auth.idx"
          return (0)
        end if

        if tablecount([Product_MRC]) > 18
          repaint off
          ShowAllReqs()
        end if

        repaint on
        repaint
        screen shortrestore S_details
        screen print 1 43 14 1 " Product               Length    Cost "
        x = messline(" Confirm that ALL requisitions are costed correctly? (y/n) ",1,1,0,22,5,70)
        if ptstr == "y"                ' all reqns costed correctly
          if records > 0
            if tablemin([Cost]) = 0      ' check for Zero costs
              $fail = " One or more items not costed"
              x = ReferShop()
              if x = 1               ' no more unauth's
                return (-1)
              elseif x = 0
                order change index "un_auth.idx"
                return (0)
              end if
            end if

          else
            return (-1)
          end if

          x = MarginCheck()
          if x = 1               ' no more unauth's
            return (-1)
          elseif x = 0           ' MORE unauth's
            return (0)
          end if

        else                           ' reqns not costed refer to shop
          $fail = " Rejected"
          x = ReferShop()
          if x = 1               ' no more unauth's
            return (-1)
          elseif x = 0           ' MORE unauth's
            order change index "un_auth.idx"
            return (0)
          end if
          order change index "un_auth.idx"
          return (0)
        end if

      elseif x = 763                ' Esc - abort
        return (-1)
      end if
    end while
  end while
END FUNCTION 'ReviewAddr()


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 ","M70")
  screen print 22 5 15 1 y2
  repaint on
  repaint
  screen shortrestore S_details

  ptval=0
  while true
    ptval = navrecs()
    if ptval = {F10}
      exit while
    end if
  end while
  vloadif(dpath|"authoriz.vw")
END FUNCTION ' ShowAllReqs()


FUNCTION WriteHOLD()      ' High or Low GP - refer to DG
  vloadif(dpath|"authoriz.vw")
  repaint off
  $newstat = "H"
  lock-record
    [Order_Status] = $newstat
  write-record
  #recnr = record
  UpdApptRecs()
  vloadif(dpath|"authoriz.vw")
  order change physical
  x = delidxrec("un_auth.idx",#recnr,1)
  x = getidxrecs("un_auth.idx",2)
  if ptval = 0
    messbox(" No more to authorise ",0,1,1)
    return (1)
  end if
  return (0)
END FUNCTION  ' WriteHOLD()


FUNCTION WritePASS()
  repaint off
  vloadif(dpath|"authoriz.vw")
  $newstat = "P"
  lock-record
    [Order_Status] = $newstat
  write-record
  #recnr = record
  UpdApptRecs()
  vloadif(dpath|"authoriz.vw")
  order change physical
  x = delidxrec("un_auth.idx",#recnr,1)
  x = getidxrecs("un_auth.idx",2)
  if ptval = 0
    messbox(" No more to authorise ",0,1,1)
    return (1)
  end if
  return (0)
END FUNCTION  'WritePASS()


FUNCTION MarginCheck()                 ' 1=failed; 0=passed
local $br
' message "#netinv is:"&str(#netinv)
' message "#totcost is:"&str(#totcost)
  $br = [Branch]
  if #margin < #margin_A
    repaint off
    messbox(" This Order has NOT been passed - referred to DAVIDG ",0,0,1)
    cat = "MARGIN"
    mess1 = jobnr&"- BAD      Gross Margin"&right("ÿÿÿÿÿÿ"|fixed(#percentmargin,1),6)|"%"&"- Net invoice"&right("ÿÿÿÿÿÿÿÿ"|currency(#netinv),8)&"- total costs"&right("ÿÿÿÿÿÿÿÿ"|currency(#totcost),8) 'L287
    if $br <> "P"
      x = exception(userid,today,time24,cat,mess1)
    end if
    x = WriteHOLD()
    if x = 1               ' no more unauth's
      return (1)
    elseif x = 0           ' MORE unauth's
      order change index "un_auth.idx"
      return (0)
    end if

  elseif #margin < #margin_B           ' 25-30%
    repaint off
    messbox(" This Order has been passed but reported ",0,0,1)
    cat = "MARGIN"
    mess1 = jobnr&"- LOW      Gross Margin"&right("ÿÿÿÿÿÿ"|fixed(#percentmargin,1),6)|"%"&"- Net invoice"&right("ÿÿÿÿÿÿÿÿ"|currency(#netinv),8)&"- total costs"&right("ÿÿÿÿÿÿÿÿ"|currency(#totcost),8) 'L287
    if $br <> "P"
      x = exception(userid,today,time24,cat,mess1)
    end if
'     x = exception(userid,today,time24,cat,mess1)
    x = WritePASS()
    if x = 1               ' no more unauth's
      return (1)
    elseif x = 0           ' MORE unauth's
      order change index "un_auth.idx"
      return (0)
    end if

  elseif #margin < #margin_C           ' 30-40%
    repaint off
    messbox(" This Order has been passed ",0,0,1)
    x = WritePASS()
    if x = 1               ' no more unauth's
      return (1)
    elseif x = 0           ' MORE unauth's
      order change index "un_auth.idx"
      return (0)
    end if

  elseif #margin < #margin_D           ' 40-50%
    repaint off
    messbox(" This Order has been passed but reported ",0,0,1)
    cat = "MARGIN"
    mess1 = jobnr&"- HIGH     Gross Margin"&right("ÿÿÿÿÿÿ"|fixed(#percentmargin,1),6)|"%"&"- Net invoice"&right("ÿÿÿÿÿÿÿÿ"|currency(#netinv),8)&"- total costs"&right("ÿÿÿÿÿÿÿÿ"|currency(#totcost),8) 'L287
    if $br <> "P"
      x = exception(userid,today,time24,cat,mess1)
    end if
'     x = exception(userid,today,time24,cat,mess1)
    x = WritePASS()
    if x = 1               ' no more unauth's
      return (1)
    elseif x = 0           ' MORE unauth's
      order change index "un_auth.idx"
      return (0)
    end if

  else
    repaint off
    messbox(" This Order has NOT been passed - referred to DAVIDG ",0,0,1)
    cat = "MARGIN"
    mess1 = jobnr&"- CRITICAL Gross Margin"&right("ÿÿÿÿÿÿ"|fixed(#percentmargin,1),6)|"%"&"- Net invoice"&right("ÿÿÿÿÿÿÿÿ"|currency(#netinv),8)&"- total costs"&right("ÿÿÿÿÿÿÿÿ"|currency(#totcost),8) 'L287
    if $br <> "P"
      x = exception(userid,today,time24,cat,mess1)
    end if
'     x = exception(userid,today,time24,cat,mess1)
    x = WriteHOLD()
    if x = 1               ' no more unauth's
      return (1)
    elseif x = 0           ' MORE unauth's
      order change index "un_auth.idx"
      return (0)
    end if

  end if
END FUNCTION ' MarginCheck()


FUNCTION CheckCustomer()
local   $nophone
' update Title
  while true
    y1 = format(" Select Title ","M70")
    screen print 21 5 15 1 y1
    y = popuplist(11,5,18,"Mr Mrs Mrÿ&ÿMrs Miss Ms Other LtdÿCo","",1,0)
    if ptstr = "Other"
      while true
        x = entryline(" Enter title ",20,"","",21,5,70)
        if x = 0
          cust_title = ptstr
          exit while
        end if
      end while
    elseif ptstr = "LtdÿCo"
      cust_title = ""
      exit while
    else
      cust_title = ptstr
      x = entryline(" Enter Forename or Initial ",35,"","",21,5,70)
      cust_title = cust_title&proper(ptstr)
    end if
    exit while
  end while

' update Contact Name
  while true
'   custcontact = trim(cust_title)&trim(custname)
    y1 = format(" ","M70")
    screen print 21 5 15 1 y1
    x = entryline(" Contact name (if different to customer name) ",45,"",custcontact,21,5,70)
    if x = 0
      custcontact = ptstr
      exit while
    end if
  end while

  $nophone = "N"
  while true
    while true
      if hometel = "N/P"
        hometel = "0"
      end if
      x = entryline(" Home 'phone number (eg 0-171-498-1453) - {Esc} if none ",15,"*15{[1234567890\-]}",hometel,21,5,70)
      if x = 0
        if ptstr = "0"
          hometel = "N/P"
        else
          hometel = ptstr
          $nophone = "Y"
          exit while
        end if
      elseif x = -1
        hometel = "N/P"
        exit while
      end if
    end while

    while true
      if offtel = "N/P"
        offtel = "0"
      end if
      x = entryline(" Office 'phone number (eg 0-171-498-1453) - {Esc} if none ",15,"*15{[1234567890\-]}",offtel,21,5,70)
      if x = 0
        if ptstr = "0"
          offtel = "N/P"
        else
          offtel = ptstr
          $nophone = "Y"
          exit while
        end if
      elseif x = -1
        offtel = "N/P"
        exit while
      end if
    end while

    while true
      if mobile = "N/P"
        mobile = "0"
      end if
      x = entryline(" Mobile 'phone number (eg 0860-291565) - {Esc} if none ",15,"*15{[1234567890\-]}",mobile,21,5,70)
      if x = 0
        if ptstr = "0"
          mobile = "N/P"
        else
          mobile = ptstr
          $nophone = "Y"
          exit while
        end if
      elseif x = -1
        mobile = "N/P"
        exit while
      end if
    end while

    if $nophone = "N"
      x = messline(" You cannot be serious! Everyone has a 'phone. Re-enter? (y/n) ",1,1,1,21,5,70)
      if ptstr=="y"
        continue while
      else
        exit while
      end if
    else
      exit while
    end if
  end while

  while true
    if offax = "N/P"
      offax = "0"
    end if
    x = entryline(" Fax number (eg 0-171-498-1455) - {Esc} if none ",15,"*15{[1234567890\-]}",offax,21,5,70)
    if x = 0
      offax = @if(ptstr=="0","N/P",ptstr)
      exit while
    elseif x = -1
      offax = "N/P"
      exit while
    end if
  end while
END FUNCTION 'CheckCustomer()


FUNCTION CustomerAddr()
  while true
    x = entryline(" Customer's Address - Line 1 ",35,"",custaddr1,21,5,70)
    if x = 0
      custaddr1 = proper(ptstr)
      exit while
    end if
  end while

  while true
    x = entryline(" Customer's Address - Line 2 ",35,"",custaddr2,21,5,70)
    if x = 0
      custaddr2 = proper(ptstr)
      exit while
    end if
  end while

' message "custcity is:"&str(custcity)
  while true
    x = entryline(" Customer's Address - Town/City ",20,city,custcity,21,5,70)
    if x = 0
      custcity = ptstr
      exit while
    end if
  end while

  while true
    x = entryline(" Customer's Postcode ",8,"AU*7{[A-Za-z1234567890\ ]U}",custpostcode,21,5,70)
    if x = 0
      custpostcode = ptstr
      exit while
    else
'       message "FENTRY Error @ L1324"
    end if
  end while
END FUNCTION 'CustomerAddr()


FUNCTION DeliveryAddr()   'message "enter deladdr using deladdr"
  while true
    x = entryline(" Enter Delivery Address - Line 1 ",35,"",deladdr1,21,5,70)
    if x = 0
      deladdr1 = proper(ptstr)
      exit while
    end if
  end while

  while true
    x = entryline(" Enter Delivery Address - Line 2 ",35,"",deladdr2,21,5,70)
    if x = 0
      deladdr2 = proper(ptstr)
      exit while
    end if
  end while

  while true
    x = entryline(" Enter Town/City ",20,city,deladdr3,21,5,70)
    if x = 0
      deladdr3 = ptstr
      exit while
    end if
  end while

  while true
' message "deladdr4 is:"&str(deladdr4)
    x = entryline(" Delivery Postcode ",8,"AU*7{[A-Za-z1234567890\ ]U}",deladdr4,21,5,70)
    if x = 0
      deladdr4 = ptstr
      exit while
    else
'       message "FENTRY Error @ L1324"
    end if
  end while
END FUNCTION ' DeliveryAddr()


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 = 4
  cc1 = 2
  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(custaddr1|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c2 = left(custaddr2|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c3 = left(custcity|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  c4 = left(custpostcode|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  screen print cr1 cc1+1 15 1 "ÿCustomer's 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

  screen clear box dr1 dc1 dr2 dc2 15 1
  d1 = left(deladdr1|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d2 = left(deladdr2|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d3 = left(deladdr3|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  d4 = left(deladdr4|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35)
  screen print dr1 dc1+1 15 1 "ÿDelivery addressÿ"
  screen print dr1+1 dc1+2 15 1 d1
  screen print dr1+2 dc1+2 15 1 d2
  screen print dr1+3 dc1+2 15 1 d3
  screen print dr1+4 dc1+2 15 1 d4
  screen save dr1 dc1 dr2 dc2 deladdr

  screen clear box pr1 pc1 pr2 pc2 15 1
  p1 = format("Title:  ÿ   "|left(cust_title|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p2 = format("Name:     ÿ "|left(custname|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p3 = format("Contact:   ÿ"|left(custcontact|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p4 = format("Office tel:ÿ"|left(offtel|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p5 = format("Home tel:  ÿ"|left(hometel|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p6 = format("Fax nr:ÿÿ  ÿ"|left(offax|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  p7 = format("Mobile nr: ÿ"|left(mobile|"ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ",35),"L47")
  screen print pr1 pc1+1 15 1 "ÿSundry detailsÿ"
  screen print pr1+1 pc1+2 15 1 p1
  screen print pr1+2 pc1+2 15 1 p2
  screen print pr1+3 pc1+2 15 1 p3
  screen print pr1+4 pc1+2 15 1 p4
  screen print pr1+5 pc1+2 15 1 p5
  screen print pr1+6 pc1+2 15 1 p6
  screen print pr1+7 pc1+2 15 1 p7
  screen save pr1 pc1 pr2 pc2 ordets
END FUNCTION 'ShowDetails()


FUNCTION CopyAddresses()
  custaddr1    = [Address_1]
  custaddr2    = [Address_2]
  custcity     = [City/Town]
  custpostcode = [Postcode]
  custcontact  = [Contact_Name]
  cust_title   = [Cust_Title]
  custname     = [Customer_Name]
  hometel      = [Home_Tel]
  offtel       = [Office_Tel]
  mobile       = [Mobile/Other_Nr]
  offax        = [Office_Fax]
  deladdr1     = [Delivery_Address_1]
  deladdr2     = [Delivery_Address_2]
  deladdr3     = [Del_City]
  deladdr4     = [Del_Postcode]
END FUNCTION 'CopyAddresses()


FUNCTION UnAuthorized()
while true
  progress(fgp,bgp," Searching for unauthorized Orders ",0)
  vloadif(dpath|"authoriz.vw")
  order change key "[Order_Status]"
  data query execute "unauth_O.dfq" index "un_auth.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [Order_Status] = "R"                                               ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    screen clear box 1 1 sch scw 0 0 no-border
    messbox(" No Jobs to authorize ",0,0,1)
    return (-1)
  end if
  screen shortrestore psa

  while true
    data goto record first
    #count = records
'     screen shortrestore psa
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    y1 = format(" Choose Job to Authorize & press {Enter} ","M80")
    screen print r1 1 15 1 y1
    bline = min(r1+#count+3,20)
    screen print bline 1 15 1 (format(" {Esc} exits ","M80"))
    x = bpopdb("authoriz",6,"","[Title]","L78","[Job_Nr]","L6","[Job_Nr]",8,1,19,80,"",0)
    if x = 0
      screen clear box 1 1 sch scw 0 0 no-border
      jobnr = ptstr
      x = CheckOrder()                 ' message "Leaving CheckOrder - x is:"&str(x)
      if x = 0
      elseif x = 1
        screen clear box 1 1 sch scw 0 0 no-border
        return (-1)
      elseif x = -1
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        return (-1)
      end if
    elseif x = -1
      screen clear box 1 1 sch scw 0 0 no-border
      return (-1)
    end if
  end while
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  exit while
end while
END FUNCTION ' UnAuthorized()


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 WriteDetails()         ' write customer & job details to CUSENT3B
  lock-record
    [Cust_Title]         = cust_title
'     [Customer_Name]      = custname   ' CANNOT change this anyway
    [Contact_Name]       = custcontact
    [Office_Tel]         = offtel
    [Home_Tel]           = hometel
    [Office_Fax]         = offax
    [Mobile/Other_Nr]    = mobile
    [Address_1]          = custaddr1
    [Address_2]          = custaddr2
    [City/Town]          = custcity
    [Postcode]           = custpostcode
    [Delivery_Address_1] = deladdr1
    [Delivery_Address_2] = deladdr2
    [Del_City]           = deladdr3
    [Del_Postcode]       = deladdr4
    [Last_Update]        = today
    [Updated_By]         = userid
  write-record
END FUNCTION ' WriteDetails()


FUNCTION UpdApptRecs()
local #apptnr
  repaint off
  vloadif(dpath|"bookappt.vw")
  order change key "[Job_Nr]"
  data query execute "job_reqn.dfq" index "upd_recs.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror                            ' if none - then return
    return (0)                         ' no appts made yet
  end if

  for i = 1 to records
    $dayftr = left([DFA],11)
    #apptnr = [Appointment_Order]
    lock-record
      [Entered_By] = userid
      [Date_Altered] = today
      [Time] = now
      [Status] = $newstat
    write-record

    repaint off
    vloadif(dpath|"apptdate.vws")         ' message "jobnr is:"&str(jobnr)
    order change key "[DayFitter]"
    data find "[DayFitter]" equal $dayftr options ""
    if cerror
      return (0)
    end if
    lock-record
      dbput("[B"|str(#apptnr)|"]",$newstat)
    write-record
    vloadif(dpath|"bookappt.vw")
    data goto record next
  end for
END FUNCTION 'UpdApptRecs()


FUNCTION ReferShop()
' change messbox to confirm send back to shop & show Job Nr
  messbox($fail&"- refer Order back to Shop? (y/n) ",1,0,1)
  if ptstr == "n"
    return (1)
  else
    messbox(" Checking for booked deliveries ",0,0,1)
    x = CheckDeliveries()
    if x = 1
      return (1)
    end if
    vloadif(dpath|"authoriz.vw")
    repaint off
    $newstat = "V"
    lock-record
      [Order_Status] = $newstat
    write-record
    #recnr = record
    UpdApptRecs()
    vloadif(dpath|"authoriz.vw")
    order change physical
    x = delidxrec("un_auth.idx",#recnr,1)
    x = getidxrecs("un_auth.idx",2)
    if ptval = 0
      messbox(" No more to authorise ",0,1,1)
      return (1)
    end if
    return (0)
  end if
END FUNCTION ' ReferShop()


FUNCTION CheckDeliveries()
  vloadif(dpath|"goodsout.vws")
  order change key "[Job_Nr]"
  data query execute "job_reqn.dfq" index "chkdelvs.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Job_Nr] = jobnr
'   and
'   not(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    return (0)
  else
    messboxwait(" Deliveries already booked out ",0,0,1)
    return (1)
  end if
END FUNCTION 'CheckDeliveries()

