'SUPP_INV - routine to check Supplier's invoice against GDS_RCVD. Also marks
'           GDS_RCVD with Due Date of Payment (DDP)

external   entryline() vloadif() dpath bpopdb() sch scw progress() messbox()
external   fgp bgp cpath navrecs() reqnpopup() messline() exception() bge
external   userid remove() messboxwait()  makeidx() addidxrec() delidxrec()
external   getidxrecs() vunloadif() $menu sipath chkdate() colpopup()
external   chkstr() strcount() fm_busdate() colpoplines() wraptext()
external   check_2000() fentrybox() tone() vatrate

public     ptstr suppcode ptval psa dsa jobnr #invdate $rollnr

global     #SMLC #SMLR #priceC #priceR #prodrec PrintSummary() c r z1 z2 z3 z4 z5 z6 z7 z8 z9 z0
global     x y FindSupplier() suppname y1 y2 y3 y4 ReturnToMenu() S_hdr S_all
global     ordernr ProcessInvoice() ShowCompleted() #invcost $paymth Titles()
global     #ordcost #margin #M_l #M_m #M_h cat mess prodsupp StartTitle() k
global     $suppinv #invitems #L_l #L_u #totalinv #invtotal i r_date #R_m
global     $action #rem $comment_P #item z ViewReqns() suppidx $recstr
global     PopComm() NavRecSuppInv() r1 r2 c1 c2 cl1 cl2 ExceptlCost()
global     #tot_diff ProcessLineItem() m1 m2 m3 m4 m5 ns #recs #nritems[1]
global     $rec $itemtype prodcode TypeSummary() #631001 #631002 #631003
global     #631004 #631005 #631006 #631007 #631008 #631009 #631010 $fitter
global     ShowSummary() slotrec $ordstat $invoice  $invnr $invdate #1stbalance
global     $dfa1 $ftrappts #ftrappts ftrarray[1] ReplaceHardSpace2() $ftrcode
global     #amount varndes $ordstr rcptdes ReplaceHardSpace() custaddr mess5
global     $showdel cust_title custaddr1 custaddr2 custcity custpostcode
global     deladdr1 deladdr2 deladdr3 deladdr4 custcode custname custcontact
global     offtel hometel offax mobile $status deladdr clf clb ShowPrice()
global     sun_ACC SuppInvEnter() origview #inv_net $sunper1 $sunper2 $sunper3
global     Sun_Period() #inv_gross acctgperiod acc_period #vat


MAIN
  single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  r1 = 1
  r2 = r1+2
  cl1 = 15
  clb = 13
  clf = 10

  repaint off
  #L_l = 100
  #L_u = 500

' EXCEPTN levels
  #M_l = .5     ' invoice < 100 margin is 50p
  #M_m = 1      ' invoice > 100 & < 500 margin is 100p
  #M_h = 2      ' invoice > 500 margin is 2

' REJECT if MORE than 10% out
  #R_m = .1

  suppcode = ""
  while true
    x = FindSupplier()                   ' select supplier (GDS_RCVD)
    if x = 1
      continue while
    elseif x = -1
      ReturnToMenu()
    end if
    x = ShowCompleted()                  ' show all uncleared orders
    if x = -1
      continue while
    end if
  end while
END MAIN


FUNCTION ShowCompleted()
local suppinvidx
  progress(15,10," Finding Orders from"&suppname|" ",0)
  ns = 0
  $action = "N"
  suppinvidx=sipath|"SI"|suppcode|".idx"  'message "suppinvidx is:"&str(suppinvidx)
  if file(suppinvidx)=0
    messboxwait(" Supplier index file not found - inform JDC ",0,0,1)
    vunloadif("suppinv1.vw")
    vloadif(dpath|"suppinv0.vw")
    order change key "[Supplier_Code]"
    data query execute "goods_in.dfq" index "suppinv1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŋ
'    [Supplier_Code]=suppcode
' ĀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŲ
    if cerror
      messbox(" No Goods Received to check for"&suppname,0,0,1)
      screen clear box 1 1 sch scw 0 0 no-border
      return (1)
    end if
    vunloadif("suppinv0.vw")
    vloadif(dpath|"suppinv1.vw")
    order change index "suppinv1.idx"
  else
    vloadif(dpath|"suppinv1.vw")
    order change index suppinvidx
  end if

  error off
  data query execute "supp_inv.dfq" index "suppinv2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŋ
'   [Supplier_Code] = suppcode and len([Invoice_Nr])=0 and not(deleted)
' ĀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŲ
  if cerror
    messbox(" No Goods Received to check for"&suppname,0,0,1)
    screen clear box 1 1 sch scw 0 0 no-border
    return (1)
  end if
  suppidx = "supp_inv.idx"
  order sort now dictionary suppidx fields "[Order_Nr]" ascending

  while true
    $suppinv = ""
    #invitems = 0
    #rem = 0
    while true
      StartTitle()
      ptval = NavRecSuppInv()
      if ptval = {Esc}
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        return (-1)

      elseif ptval = {P} or ptval = {p}
        ShowPrice()
        continue while

      elseif ptval = {J} or ptval = {j}  ' view job req'ns
        jobnr = left([Order_Nr],6)
        ViewReqns()
        continue while

      elseif ptval = {S} or ptval = {s}
        #totalinv = 0

        while true
          while true
            x = entryline(" Enter Supplier's Invoice Nr ",10,"*10XU","",22,1,80)
            if x = -1
              screen clear box 22 1 22 80 0 0 no-border
              ns = 1
              exit while
            end if
            if len(ptstr)=0
              continue while
            end if
            $suppinv = upper(ptstr)
            exit while
          end while
          if ns = 1
            ns = 0
            continue while
          end if
          ns = 0
          while true			
            x = entryline(" Enter date of invoice ",10,"##\/##\/####",today,22,1,80)
            if x = 0
              #invdate = ptstr
              if chkdate(#invdate,1) < 0
                tone("error")
                messbox(#invdate&"is a bad date! Enter again ",0,0,1)
              else
                exit while
              end if
            end if
          end while

          while true
            x = entryline(" GROSS amount of Supplier's invoice (inc. of VAT) ",8,"","",22,1,80)
            if x = -1
              screen clear box 22 1 22 80 0 0 no-border
              ns = 1
              exit while
            end if
            if len(ptstr)=0
              continue while
            end if
            #inv_gross = value(ptstr)       'message "#inv_gross is:"&str(#inv_gross)
message "#inv_gross) is:"&str(#inv_gross)
repaint on
repaint
single-step on
            exit while
          end while
          if ns = 1
            ns = 0
            continue while
          end if
          ns = 0
        end while

        while true
          acctgperiod = right(date2(days(today)-30+240),7)
          x = fentrybox(" Enter SUN accounting period ",7,"*2#\/*4#",acctgperiod)
          if x = 0
            acc_period=right(ptstr,4)|"0"|left(ptstr,2) '
message "acc_period is:"&str(acc_period)
            exit while
          else
      '       return (-1)
          end if
        end while

        while true
          x = entryline(" Enter Nr of items on Invoice to check ",4,"","",22,1,80)
          if x = -1
            screen clear box 22 1 22 80 0 0 no-border
            continue while
          end if
          if len(ptstr)=0
            screen clear box 22 1 22 80 0 0 no-border
            continue while
          end if
          screen clear box 22 1 22 80 0 0 no-border
          #invitems = value(ptstr)
          #rem = #invitems
          redimension #nritems[#invitems]
          #item = 0
          exit while
        end while

        x = messline(str(#invitems)&"items on Invoice"&str($suppinv)&"totalling"&currency(#inv_net)&"- correct? (y/n) ",1,1,1,22,1,80)
        if ptstr == "n"
          screen clear box 22 1 22 80 0 0 no-border
          continue while
        end if
      else
        continue while
      end if
    end while

      x = ProcessInvoice()
      if x = -1
        continue while
      elseif x = 1
        return (1)
      end if

      exit while
    end while
'   end while
END FUNCTION 'ShowCompleted()


FUNCTION ProcessInvoice()
local $delrecs
  x = remove("inv_list.idx")
  if x = -1
  ' message "remove() failed"
  end if

  x = makeidx("gds_rcvd","inv_list.idx","0",1)
  if x = -1
  ' message "makeidx() failed"
  end if

  screen clear box 22 1 22 scw 0 0 no-border
  y3 = format(" ¯ = Checked - {C}heck/uncheck - {J}ob req's - {F10} finishes - {Esc} restarts","M80")
  screen print 22 1 fgp bgp y3
  ptval=0
  while true
    ptval = navrecs()

    if ptval = {C} or ptval = {c}
      #ordcost = [Total_Cost]
      x = ProcessLineItem()            'message "#totalinv is:"&str(#totalinv)
      if x = 1
        screen clear box 22 1 22 scw 0 0 no-border
        y3 = format(" ¯ = Checked - {C}heck/uncheck - {J}ob req's - {F10} finishes - {Esc} restarts","M80")
        screen print 22 1 fgp bgp y3
'         if #rem=0 and round(#invtotal-#totalinv,2)=0
        if #rem=0 and round(#inv_net-#totalinv,2)=0
          y4 = format("ALL checked - total agrees with invoice ","M80")
          screen print 22 1 fgp bge y4
        else
          y4 = format("Items to be checked:"&str(#rem)&" - total of items so far"&currency(#totalinv),"M80")
          screen print 22 1 fgp bgp y4
        end if
        continue while
      end if
      $action = "Y"
      if x = 0
        Titles()
        screen clear box 22 1 22 scw 0 0 no-border
        y3 = format(" ¯ = Checked - {C}heck/uncheck - {J}ob req's - {F10} finishes - {Esc} restarts","M80")
        screen print 22 1 fgp bgp y3
        if #rem=0 and round(#inv_net-#totalinv,2)=0
          y4 = format("ALL checked - total agrees with invoice ","M80")
          screen print 22 1 fgp bge y4
        else
          y4 = format("Items to be checked:"&str(#rem)&" - total of items so far"&currency(#totalinv),"M80")
          screen print 22 1 fgp bgp y4
        end if
        data goto record next
        continue while

      elseif x = 2                     ' invoice rejected
        repaint off
        data goto record first
        for i = 1 to records     'message "Clear check mark from all supp_inv.idx items"
          if [Check] = "¯"
            lock-record
              [Check] = ""
              [Invoice_Cost] = 0
              [Invoice_Nr]  = ""
              [Check] = ""
            write-record
          end if
          data goto record next
        end for
        clear #totalinv
        clear #invtotal
        screen clear box 1 1 sch scw 0 0 no-border
        data goto record first
        return (-1)

      elseif x = -1
        Titles()
        continue while
      end if

    elseif ptval = {F10}
      if #totalinv <> #inv_net
        messboxwait(" Total of line items entered does not agree with invoice total! Re-enter ",0,0,1)
        continue while
      end if

      #vat=fixed(#invtotal-#inv_net,2)
message "#vat) is:"&str(#vat)
      x = messbox(" VAT is"&currency(#inv_gross)|"? (y/n) ",1,1,1)
      if ptstr == "n"
        repaint on
        repaint
        Titles()
        clear #631001 #631002 #631003 #631004 #631005 #631006 #631007 #631008 #631009 #631010
        continue while
      end if

      repaint off                      ' create INV_LIST
      data goto record first
      $recstr = ""
      for i = 1 to records
        if [Check] = "¯"
          x = addidxrec("inv_list.idx",precord,7)   ' add to temp index
          $recstr = $recstr&str(record)
        end if
        data goto record next
      end for

'       m2 = upper(mid(addmonths(date1(r_date),0),3,4))|"˙"|right(addmonths(date1(r_date),0),2)
'       m3 = upper(mid(addmonths(date1(r_date),1),3,4))|"˙"|right(addmonths(date1(r_date),1),2)
'       m4 = upper(mid(addmonths(date1(r_date),2),3,4))|"˙"|right(addmonths(date1(r_date),2),2)
'       m5 = format("Select Due Month for Payment","M80")
'       screen print 22 1 15 1 m5
'       screen clear box 22 1 22 80 0 0 no-border
'       x = reqnpopup(19,58,23,m2&m3&m4,"",1,0,1,15,1)  'message "x is:"&str(x)
'       if x = 0
'         $paymth = ptstr
'       end if
' $paymth=check_2000($paymth)

      x = messline(" Invoice nr"&$suppinv&"for"&currency(#inv_gross)|"? (y/n) ",1,1,1,22,1,80)
      if ptstr == "n"
        repaint on
        repaint
        Titles()
        clear #631001 #631002 #631003 #631004 #631005 #631006 #631007 #631008 #631009 #631010
        continue while
      end if
      vloadif(dpath|"itemtype.vw")
      vloadif(dpath|"suppinv1.vw")
      order change index "inv_list.idx"
      data goto record first
      #recs = records
      data goto record first
      $delrecs = ""
      clear #631001 #631002 #631003 #631004 #631005 #631006 #631007 #631008 #631009 #631010
      for i = #recs to 1 step -1
        prodcode = [Product_Code]
        $itemtype = filelookup([itemtype.Product_Code],[itemtype.Item_Type],prodcode)
        lock-record
          [DueDate]     = $paymth
          [Last_Update] = today
          [Updated_By]  = userid
          [Check]       = ""
        write-record
        $rec = val(group($recstr,i))   'message "$rec is:"&str($rec)
        x = delidxrec(suppidx,$rec,2)  'message "delidx is:"&str(x)
        TypeSummary()
        data goto record next
      end for
      ShowSummary()

      #invtotal=#inv_net*(1+(vatrate/100))

      while true
        x = entryline(" Confirm Gross Amount of Supplier's invoice (INC VAT) ",8,"",fixed(#invtotal,2),22,1,80)
        if x = -1
          screen clear box 22 1 22 80 0 0 no-border
          ns = 1
          exit while
        end if
        if len(ptstr)=0
          continue while
        end if
        #invtotal = value(ptstr)       'message "#invtotal) is:"&str(#invtotal)
        exit while
      end while

      SuppInvEnter()

      x = getidxrecs(suppidx,2)
      order change index suppidx
      if x = 0
        if ptval = 0
          messbox(" No more to process for this Supplier ",0,0,1)
          screen clear box 1 1 sch scw 0 0 no-border
          repaint off
          return (1)
        else
          return (0)               ' temp
        end if
      end if

    elseif ptval = {Esc}
      repaint off
      data goto record first
      for i = 1 to records     'message "Clear check mark from all supp_inv.idx items"
       if [Check] = "¯"
         lock-record
             [Check] = ""
             [Invoice_Cost] = 0
             [Invoice_Nr]  = ""
             [Check] = ""
           write-record
         end if
         data goto record next
      end for
      clear #totalinv
      clear #invtotal
      screen clear box 1 1 sch scw 0 0 no-border
      data goto record first
      return (-1)
    end if
  end while
END FUNCTION ' ProcessInvoice()



FUNCTION ProcessLineItem()
local m1 m2 m3 m4 m5 m6 r1 r2 c1 c2 cl1 cl2 lmsg c3 c4
  r1 = 3
  r2 = r1+2
  cl1 = 15
  cl2 = 12
' message "chk for rollnr"
  if [Check] = "¯"
    $rollnr	=[RollNr]
' message "$rollnr is:"&str($rollnr)
    #invcost = [Invoice_Cost]
    lock-record
      [Invoice_Cost] = 0
      [Invoice_Nr]  = ""
      [Check] = ""
    write-record

' check o/s total of inv's to be
    #rem = #rem + 1                    'message "#invitems is:"&str(#invitems)
' show total of orders checked
    #totalinv = #totalinv - #invcost   'message "#totalinv!!! is:"&str(#totalinv)

  else
    if #rem = 0
      messbox(" No more line items to check ",0,0,1)
      return (1)
    end if

    while true
      ordernr  = [Order_Nr]
      #ordcost = [Total_Cost]              ' message "#ordcost is:"&str(#ordcost)
      $comment_P = [Comments]
      case
        when (#ordcost<=#L_l)
          #margin = #M_l
        when ((#ordcost>#L_l) and (#ordcost<=#L_u))
          #margin = #M_m
        otherwise
          #margin = #M_h
      end case                           ' message "#margin is:"&str(#margin)
      while true
' message "#invitems is:"&str(#invitems)
        if #invitems = 1
          x = entryline(" Enter invoiced cost (s.b."&currency(#ordcost)|")",8,"",#inv_net,22,1,80)
        else
          x = entryline(" Enter invoiced cost (s.b."&currency(#ordcost)|")",8,"","",22,1,80)
        end if
        if x = -1
          screen print 22 1 fgp bgp y3
          return (-1)
        end if
        if ptstr = ""
          continue while
        end if
        #invcost = value(ptstr)            'message "#invcost is:"&str(#invcost)
        exit while
      end while

      if #R_m*#invcost < abs(#ordcost-#invcost)     ' reject
        if $menu == "BOSS"
          messbox(" Ordered @"&currency(#ordcost)&"- invoiced at"&currency(#invcost)&"- accept anyway? (y/n) ",1,0,1)
          if ptstr=="n"
            return (2)
          end if
        else
          if userid = "MARTNA"
            messbox(" Ordered @"&currency(#ordcost)&"- invoiced at"&currency(#invcost)&"- accept anyway? (y/n) ",1,0,1)
            if ptstr=="n"
              return (2)
            end if
          else
            messboxwait(" Price difference TOO great!!  Refer invoice for further investigation ",0,0,1)
            return (2)
          end if
        end if
      end if

      if #invcost < (#ordcost-#margin)   'message "inv < ord"
        messline(" Price difference - re-enter invoice cost of"&currency(#invcost)|"? (y/n)",1,0,1,22,1,80)
        if ptstr == "n"
          prodSUPP = [ProdSupp]
          ExceptlCost()
          vloadif(dpath|"suppinv1.vw")
          repaint on
          repaint
        else
          continue while
        end if
      elseif #invcost > (#ordcost+#margin) 'message "inv > ord"
        messline(" Price difference - re-enter invoice cost of"&currency(#invcost)|"? (y/n)",1,0,1,22,1,80)
        if ptstr == "n"
          repaint off
          prodSUPP = [ProdSupp]  ' message "prodSUPP is:"&str(prodSUPP)
          ExceptlCost()
          vloadif(dpath|"suppinv1.vw")
          repaint on
          repaint
        else
          continue while
        end if
      end if
      lock-record
        [Check] = "¯"
        [Invoice_Cost] = #invcost
        [Invoice_Nr]  = $suppinv
      write-record
      #rem = #rem - 1                    'message "#invitems is:"&str(#invitems)
      #totalinv = #totalinv + #invcost   'message "#totalinv!!! is:"&str(#totalinv)
      r_date = [Date_Rcvd]
      repaint off
      if left(ordernr,1)<>"X"
        vloadif(dpath|"requsn.vws")
        order change key "[Reference_Nr]"  'message "ordernr is:"&str(ordernr)
        data find "[Reference_Nr]" equal ordernr options ""
        if cerror                               '   if none - then return
          x = messbox(" Order Nr not found ",0,0,1)
        else
          if [Cost]<>[Cost_OS]
            messboxwait("N.B. DCOOPR - alter costs in GOODSOUT file ",0,0,1)
          end if
' message "#invcost is:"&str(#invcost)
          lock-record
            dbput("[Cost]",#invcost)
            dbput("[Cost_OS]",#invcost)
          write-record
        end if
        vloadif(dpath|"suppinv1.vw")
      end if
      return (0)
    end while
  end if
END FUNCTION ' ProcessLineItem()


FUNCTION ExceptlCost()
  repaint off
  cat = "PUR_INVC"
  mess = left(suppname,15)&left(prodsupp,15)&"diff. on invoice (is"&currency(#invcost)&"s.b."&currency(#ordcost)|")"
  x = exception(userid,today,time24,cat,mess)
END FUNCTION 'ExceptlCost()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  error off
  window close
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION FindSupplier()
  vloadif(dpath|"supplier.vws")
  order change physical
  order sort now dictionary "suppname" fields "[Name]" ascending
  repaint off
  while true
    y = format(" Choose Supplier and press {Enter} ","M38")
    screen print 7 22 15 1 y
    screen print 20 22 15 1 (format(" {Enter} views orders - {Esc} exits ","M38"))
    if len(suppcode)=0
      x = bpopdb("supplier",6,"","[Name]","l35","[Supplier_Code]","L6","[Supplier_Code]",8,22,19,58,"",0)
    else
'       x = bpopdb("supplier",6,"fe"&suppcode,"[Name]","l35","[Supplier_Code]","L6","[Supplier_Code]",8,22,19,58,"",0)
      data goto record record-number #prodrec
      x = bpopdb("supplier",6,"","[Name]","l35","[Supplier_Code]","L6","[Supplier_Code]",8,22,19,58,"",0)
    end if
    if x = 0
      exit while
    elseif x = -1
      screen clear box 1 1 sch scw 0 0 no-border
      return (-1)
    end if
  end while
  #prodrec = record
  suppcode = ptstr
  suppname = [Name]
  sun_ACC  = [AccCode]
  if len(sun_ACC)=0
    messboxwait(" SUN Account code missing on SUPPLIER.DB - cannot process ",0,0,1)
    return (1)
  end if
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
'   progress(15,10," Finding Orders from"&suppname|" ",0)
  return (0)
END FUNCTION 'FindSupplier()


FUNCTION Titles()
  repaint on
  repaint
  y1 = format("Completed deliveries from"&suppname,"M78")
  y2 = format("   Order Nr  Description/Colour                      Del'd     Length    Cost ","L80")
  y3 = format(" ¯ = Checked - {C}heck/uncheck - {F10} finishes - {Esc} restarts","M80")
  screen clear box 3 1 5 80 fgp bgp
  screen print 4 2 fgp bgp y1
  screen print 6 1 fgp bgp y2
  screen print 22 1 fgp bgp y3
END FUNCTION ' Titles()


FUNCTION StartTitle()
  repaint on
  repaint
  y1 = format("Completed deliveries from"&suppname,"M78")
  y2 = format("   Order Nr  Description/Colour                      Del'd     Length    Cost ","L80")
'   y3 = format(" "|chr(24)&chr(25)&"to scroll   -   {S}tart   -   {Esc} restarts ","M80")
  y3 = format(" "|chr(24)&chr(25)&"to scroll - {S}tart invoice - {J}ob req'ns - {P}rice - {Esc} restarts ","M80")
  screen clear box 3 1 5 80 fgp bgp
  screen print 4 2 fgp bgp y1
  screen print 6 1 fgp bgp y2
  screen print 22 1 fgp bgp y3
END FUNCTION ' StartTitle()


FUNCTION NavRecSuppInv()
local x bot psmode
  screen save scrheight 1 scrheight scrwidth bot
  smartpeek $_spndmes psmode
  if psmode = 1
    smartpoke $_spndmes 0
  end if
  PopComm()
  while TRUE
    x = inchar
    if x = {Down}
      screen shortrestore S_hdr
      data goto record next
      PopComm()
    elseif x = {Up}
      screen shortrestore S_hdr
      data goto record previous
      PopComm()
    elseif x = {PgDn}
      screen shortrestore S_hdr
      data goto page next
      PopComm()
    elseif x = {PgUp}
      screen shortrestore S_hdr
      data goto page previous
      PopComm()
    elseif x = {^End}
      screen shortrestore S_hdr
      data goto record last
      PopComm()
    elseif x = {^Home}
      screen shortrestore S_hdr
      data goto record first
      PopComm()
    elseif x = {Home}
      screen shortrestore S_hdr
      suspendone
      keys Home,F8
      PopComm()
      screen shortrestore bot
    elseif x = {End}
      screen shortrestore S_hdr
      suspendone
      keys End,F8
      PopComm()
      screen shortrestore bot
    else
      exit while
    end if
  end while
  if psmode = 1
    smartpoke $_spndmes 1
  end if
  return (x)
END FUNCTION ' NavRecSuppInv()


FUNCTION PopComm()
  error off
  if [Status] = "A"                    ' ACCEPTED into stock
    cl2 = 10
    if len([Comments]) > 0
      y1 = " "|[Comments]
      c1 = 40-(len(y1)/2)-1
      c2 = c1+(len(y1))+2
      screen save r1 c1 r2 c2 S_hdr
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+1 cl1 cl2 y1
    end if

  else                                 ' NOT accepted
    cl2 = 12
    if len([Comments]) > 0
      y1 = " REJECTED -"&[Comments]
      c1 = 40-(len(y1)/2)
      c2 = c1+(len(y1))+2
      screen save r1 c1 r2 c2 S_hdr
      screen clear box r1 c1 r2 c2 cl1 cl2
      screen print r1+1 c1+1 cl1 cl2 y1
    end if
  end if
END FUNCTION ' PopComm()


FUNCTION ViewReqns()
local left_scn y5
  repaint off
  screen save 1 1 sch 25 left_scn    ' save left 30 columns
  vloadif(dpath|"shw_reqs.vw")
  order change key "[Job_Nr]"
  data find "[Job_Nr]" equal jobnr options ""
  repaint on
  repaint
  screen shortrestore left_scn
  y5 = format(" Press any key to continue ... ","M55")
  screen print 22 26 fgp bgp y5
  inchar
  screen clear box 22 1 22 80 0 0 no-border
  repaint off
  vunloadif("shw_reqs.vw")
  vloadif(dpath|"suppinv1.vw")
END FUNCTION ' ViewReqns()


FUNCTION TypeSummary()
  case ($itemtype)
    when "C"
      #631001 = #631001 + [Invoice_Cost]
    when "A"
      #631002 = #631002 + [Invoice_Cost]
    when "S"
      #631003 = #631003 + [Invoice_Cost]
    when "V"
      #631004 = #631004 + [Invoice_Cost]
    when "B"
      #631005 = #631005 + [Invoice_Cost]
    when "J"
      #631006 = #631006 + [Invoice_Cost]
    when "T"
      #631007 = #631007 + [Invoice_Cost]
    when "W"
      #631008 = #631008 + [Invoice_Cost]
    when "F"
      #631009 = #631009 + [Invoice_Cost]
    when "O"
      #631010 = #631010 + [Invoice_Cost]
  end case
END FUNCTION ' TypeSummary()


FUNCTION ShowSummary()
  r=9
  c=30
message "check for rollnr"
  z1=" 631001 -"|format(currency(#631001),"R10")
  z2=" 631002 -"|format(currency(#631002),"R10")
  z3=" 631003 -"|format(currency(#631003),"R10")
  z4=" 631004 -"|format(currency(#631004),"R10")
  z5=" 631005 -"|format(currency(#631005),"R10")
  z6=" 631006 -"|format(currency(#631006),"R10")
  z7=" 631007 -"|format(currency(#631007),"R10")
  z8=" 631008 -"|format(currency(#631008),"R10")
  z9=" 631009 -"|format(currency(#631009),"R10")
  z0=" 631010 -"|format(currency(#631010),"R10")
  while true
    PrintSummary()
    inchar
    PrintSummary()
'     x = messline(" Invoice nr"&$suppinv&"for"&currency(#invtotal)&"to be paid in"&$paymth|"? (y/n) ",1,1,1,22,1,80)
    messline(" Have you written down the Account Nrs? (y/n) ",1,1,1,22,1,80)
    if ptstr == "y"
      exit while
    else
      screen clear box 22 1 22 80 15 1 no-border
      continue while
    end if
  end while
END FUNCTION ' ShowSummary()


FUNCTION PrintSummary()
  screen save r c r+11 c+21 psa
  screen clear box r c r+11 c+21 14 13
  screen print r c+1 14 13 "˙Post to"&SUN_ACC|" "
  screen print r+1 c+1 14 13 z1
  screen print r+2 c+1 14 13 z2
  screen print r+3 c+1 14 13 z3
  screen print r+4 c+1 14 13 z4
  screen print r+5 c+1 14 13 z5
  screen print r+6 c+1 14 13 z6
  screen print r+7 c+1 14 13 z7
  screen print r+8 c+1 14 13 z8
  screen print r+9 c+1 14 13 z9
  screen print r+10 c+1 14 13 z0
  screen print r+11 c+1 14 13 "Any key to continue"
END FUNCTION ' PrintSummary()


FUNCTION ReplaceHardSpace(str1)
local j r m bw l_last #addn
'   bw = 35                              ' boxwidth
  bw = 43                              ' boxwidth
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = " "
      r = "˙"                          ' replace hard space
    end if
    m = m|r
  end for

  if len(m) < bw
    #addn = bw-len(m)
  else
    #addn = mod(len(m),bw)
  end if
  m = m|repeat("˙",#addn)
  return (m)
END FUNCTION ' ReplaceHardSpace()


FUNCTION ReplaceHardSpace2(str1,bw)
local j r m l_last #addn
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = " "
      r = "˙"                          ' replace hard space
    end if
    m = m|r
  end for

  if len(m) < bw
    #addn = bw-len(m)
  else
    #addn = mod(len(m),bw)
  end if
  m = m|repeat("˙",#addn)
  return (m)
END FUNCTION ' ReplaceHardSpace2()


FUNCTION ShowPrice()
' local #rcm #rcy #rrm #rry #price a1 a2 $price #rcmp #rcyp #rrmp #rryp
'show REQUSN price & value of line item in a popup

  origview=apinfo(ap_filex)
  repaint off
  prodcode = [Product_Code]
  vloadif(dpath|"showprce.vw")
  order change key "[Product_Code]"
  data find "[Product_Code]" equal prodcode options ""
  if cerror                               '   if none - then return
    messboxwait(" No price shown for this item ",0,0,1)
    return (1)
  end if
  #SMLC = [SM_List_Cuts]
  #SMLR = [SM_List_Rolls]
  if #SMLC = 0 and #SMLR = 0
    y1 = format(" No prices listed for this item - press any key ","M80")
    screen print 22 1 14 1 y1
  else
    y1 = format(" Cut/Roll prices:"&currency(#SMLC)|"/"|currency(#SMLR)&"- press any key ","M80")
    screen print 22 1 14 1 y1
'? show box with price & ext'n
  end if
  inchar
  vunloadif("showprce.vw")
  vloadif(dpath|origview)
  return (0)
END FUNCTION ' ShowPrice()


FUNCTION SuppInvEnter()
' message "enter values in SUPPINV"
' message "SUN_acc) is:"&str(SUN_acc)
' message "suppcode) is:"&str(suppcode)
' message "#invdate) is:"&str(#invdate)
' message "#invtotal) is:"&str(#invtotal)
' message "$suppinv) is:"&str($suppinv)

  origview=apinfo(ap_filex)
  vloadif(dpath|"suppinv.vws")
  Sun_Period()
  data enter lock
    [SUN_code]	=SUN_acc
    [SMW_code]	=suppcode
    [Inv_Date]	=#invdate
    [Inv_Nr]  	=$suppinv
    [Net_Value]	=#inv_net
    [SUN_Period]=$sunper3
    [RollNr]	=$rollnr
    [Gross_Value]=#invtotal
    [VAT_AMOUNT]=fixed(#invtotal-#inv_net,2)
    [ACC631001]	=#631001
    [ACC631002]	=#631002
    [ACC631003]	=#631003
    [ACC631004]	=#631004
    [ACC631005]	=#631005
    [ACC631006]	=#631006
    [ACC631007]	=#631007
    [ACC631008]	=#631008
    [ACC631009]	=#631009
    [ACC631010]	=#631010
  write-record
  vloadif(dpath|origview)
END FUNCTION ' SuppInvEnter()


FUNCTION Sun_Period()
message "$rollnr is:"&str($rollnr)
$sunper1=mid($rollnr,3,2)
message "$sunper1 is:"&str($sunper1)
$sunper2=mid($rollnr,5,1)
message "$sunper2 is:"&str($sunper2)

END FUNCTION ' Sun_Period()

' message "#631001 is:"&str(#631001)
' message "#631002 is:"&str(#631002)
' message "#631003 is:"&str(#631003)
' message "#631004 is:"&str(#631004)
' message "#631005 is:"&str(#631005)
' message "#631006 is:"&str(#631006)
' message "#631007 is:"&str(#631007)
' message "#631008 is:"&str(#631008)
' message "#631009 is:"&str(#631009)
' message "#631010 is:"&str(#631010)


