'DR_Notes - creates & prints Debit Notes

' use Debit Note Progress Sheet as starting reference
' select Supplier
' select invoice (using GDS_RCVD.DB)
' enter/confirm amount for debit note
' print D/N
' update Progress Sheet with generated Debit Note Nr

external  dpath Background() cpath vloadif() bpopdb() sch scw progress() X_path
external  messbox() entryline() fgp bgp messline() mess3() userid remove() Xreppath
external  fentrybox() makeidx() PrintReport() vatrate vunloadif() _SWIP_Crystal()
external  popuplist() messboxwait()

public    ptstr suppcode ptval jobnr invref drnr $total

global    x ReturnToMenu() FindSupplier() y #prodrec suppname ProcessSupplier()
global    suppidx StartTitle() NavRecSuppInv() CreateDebit() reqnref #refnr
global    AddDrNote() EnterDrNote() #drnr varndate drdesc #var #prec m1 m2
global    dritem PrintDrNote() p1 p2 p3 p4 p5 p6 dr_val ProcessDrNote()
global    Returns2Whse() $refnr ordref invref1


MAIN
single-step 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                ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
  p5 = 1                ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1                ' p6 = nr of copies

  Background()
  file unload all
  suppcode = ""

while true
  m1 = "˙˙˙Supplier's˙Invoice"
  m2 = "Returned˙to˙Manufacturer"
  x = popuplist(9,30,13,m1&m2,"Select from",1,0)
  if x = 0
    if ptstr = m1                   ' SUPPLIER'S INVOICE
      while true
        x = FindSupplier()                   ' select supplier (GDS_RCVD)
        if x = -1
          exit while
        end if
        x = ProcessSupplier()                ' show all uncleared orders
      end while

    elseif ptstr=m2			'message "Select from Rtns to WHSE"
      x=Returns2Whse()
      if x = -1
        exit while
      end if
    end if
  elseif x = -1
    exit while
  end if
end while

  ReturnToMenu()

END MAIN


FUNCTION ReturnToMenu()
  Background()
  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.idx" 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 21 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,21,19,58,"",0)
    else
      data goto record record-number #prodrec
      x = bpopdb("supplier",6,"","[Name]","l35","[Supplier_Code]","L6","[Supplier_Code]",8,21,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]
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  return (0)
END FUNCTION 'FindSupplier()


FUNCTION ProcessSupplier()
  vloadif(dpath|"drnotes1.vw")
  progress(15,10," Finding Invoices from"&suppname|" ",0)
  order change key "[Supplier_Code]"
  data query execute "drnotes1.dfq" index "suppinv1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Supplier_Code]=suppcode
' ³ and not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No Invoices to check for"&suppname,0,0,1)
    screen clear box 1 1 sch scw 0 0 no-border
    return (1)
  end if
  vloadif(dpath|"drnotes2.vw")
  order change index "suppinv1.idx"
  data query execute "drnotes2.dfq" index "suppinv2.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Invoice_Nr]<>blank
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No Invoices 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 "[Date_Received]" descending
  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 = {D} or ptval = {d}
      x=ProcessDrNote()
      if x=-1
        exit while
      elseif x = 1
        continue while
      end if

    else
      continue while
    end if
    if x = -1
      continue while
    elseif x = 1
      return (1)
    end if
    exit while
  end while
END FUNCTION ' ProcessSupplier()


FUNCTION StartTitle()
local y1 y2 y3
  repaint on
  repaint
  y1 = format("Invoiced deliveries from"&suppname,"M78")
  y2 = format(" Invoice Nr  Description/Colour                      Del'd     Length    Cost ","L80")
'   y3 = format(" "|chr(24)&chr(25)&"to scroll - create {D}ebit Note - {J}ob req'ns - {P}rice - {Esc} restarts ","M80")
  y3 = format(" "|chr(24)&chr(25)&"to scroll - create {D}ebit Note - {Esc} exits ","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
  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
    elseif x = {Home}
      suspendone
      keys Home,F8
    elseif x = {End}
      suspendone
      keys End,F8
    else
      exit while
    end if
  end while
  if psmode = 1
    smartpoke $_spndmes 1
  end if
  return (x)
END FUNCTION ' NavRecSuppInv()


FUNCTION CreateDebit()
local scrn
  scrn=apinfo(ap_filex)         'message "Screen is:"&str(x)
  x = EnterDrNote()
  vloadif(dpath|scrn)
  return (x)
END FUNCTION ' CreateDebit()


FUNCTION AddDrNote(drnr,#var,drdesc,dritem)
  vloadif(dpath|"dn_items.vws")
  data enter lock
    [DN_ref]   = drnr
    [DN_Items] = dritem
'     [SuppCode] = suppcode
    [Reason]   = drdesc
    [Amount]   = #var
  write-record
  vloadif(dpath|"dr_notes.vws")      'check if already used
  dr_val = [AmountClaimed]+#var
  lock-record
    [AmountClaimed] = dr_val
  write-record
END FUNCTION 'AddDrNote()


FUNCTION EnterDrNote()
local $mask a1 a2 a3 a4 m4 $hdr
  #refnr = 0
  vloadif(dpath|"dr_notes.vws")      'check if already used
  data goto record last
  #drnr = value(filemax([DN_ref]))   'message "#drnr is:"&str(#drnr)
  if cerror
    #drnr = 1
  else
    #drnr = #drnr+1
  end if
  drnr = right("00000"|str(#drnr),5) 'message "drnr is:"&str(drnr)
  if cerror
    #drnr = 1
  end if
  while true
    x = fentrybox(" Date of Debit Note ",10,"##\/##\/####",today)
    if x = -1
      continue while
    elseif x = 0
      exit while
    end if
  end while
  varndate = ptstr
  data enter lock                    'create record in DR_NOTES.VWS
    [DN_ref]        = drnr
    [Dated]         = varndate
    [Job_Nr]        = jobnr
    [Reqn_Ref]      = reqnref
    [SuppCode]      = suppcode
    [AmountClaimed] = 0
    [Contact]       = ""
    [InvNr]         = invref
  write-record
  #prec = precord
  while true
    #refnr=#refnr+1
    dritem = drnr|"-"|str(right("00"|str(#refnr),2)) 'message "dritem is:"&str(dritem)
    while true
      x = fentrybox(" Reason for debit note (supplier reads this!) - {Esc} exits ",50,"","")
      if x = -1
        messbox(" Finished with all items for this Debit Note? (y/n) ",1,1,1)
        if ptstr == "y"
          return (1)
        end if
      elseif x = 0
        if ptstr = ""
          continue while
        end if
        drdesc = ptstr
        exit while
      end if
    end while
    while true
      x = fentrybox(" Net amount (exc VAT) claimed for this item ",8,"*8{[1234567890.\-]}","")
      if x = -1
        x = messbox(" Confirm Description Line only - no value (y/n) ",1,0,1)
        if ptstr == "y"
          #var=blank
          exit while
        else
          continue while
        end if
'         return (-1)
      elseif x = 0
        if ptstr = ""
          continue while
        end if
        #var = value(ptstr)
        exit while
      end if
      x = messbox(" Confirm Debit Item value of"&currency(#var)|"? (y/n) ",1,0,1)
      if ptstr == "y"
        exit while
      else
        continue while
      end if
    end while
    x = AddDrNote(drnr,#var,drdesc,dritem)
  end while
END FUNCTION ' EnterDrNote()


FUNCTION PrintDrNote()
local $index $file
  $index = "onlyone.idx"
  $file = "dr_notes"
  remove($index)
  x = makeidx($file,$index,str(#prec),3)    'message "x is:"&str(x)
  vloadif(dpath|"drnotes3.vw")      'check if already used
  order change index $index
  $total="Debit Note claimed"&currency(dr_val*(1+(vatrate/100)))&"("|currency(dr_val)&"plus VAT @"&str(vatrate)|"%)"
'   $total="Debit Note claimed"&currency(dr_val*(1+(vatrate/100)))&"("|currency(dr_val)&"plus VAT @ 20%)"
  p3 = 1                               'message "Printing to LJ3"
'   remove(X_path|"X_drnote.*")
'   data query execute "not_del.dfq" Smart4 X_path|"X_drnote" fields "[DN_Items|Contact]"
'   vunloadif("X_drnote.vws")
'   ClearHardSpaces()
'   _SWIP_Crystal(Xreppath|"dr_note1","P",0,1,"")
'   _SWIP_Crystal(Xreppath|"dr_note2","P",0,1,"")
'
   p1 = "dr_note2.dfr"   ' p1 = report definition ("ord_stck.dfr")
   PrintReport(p1,p2,p3,p4,p5,p6)
   p1 = "dr_note3.dfr"   ' p1 = report definition ("ord_stck.dfr")
   PrintReport(p1,p2,p3,p4,p5,p6)
   vunloadif("drnotes3.vw")
END FUNCTION ' PrintDrNote()


FUNCTION ProcessDrNote()
  messbox(" Create Debit Note for invoice"&str([Invoice_Nr])|"? (y/n) ",1,1,1)
  if ptstr == "y"
    repaint off
    jobnr   = left([Order_Nr],6)
    reqnref = [Order_Nr]
    invref  = [Invoice_Nr]
    x=CreateDebit()
    if x = 1
      PrintDrNote()
    else
      return (-1)
    end if
    return (1)
  end if
END FUNCTION ' ProcessDrNote()


FUNCTION Returns2Whse()
'show all unprocessed (i.e. Return2Manufr note NOT printed for $reason="RM")
  while true
    vloadif(dpath|"retn2mfr.vw")
    order change physical
    data query execute "rtn2mfr1.dfq" index "rtn2mfr1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [DN_sheet]=blank
'   and
'   [RetnCode]="RM"
'   and
'   [Invoice_Nr]<>blank
'   and
'   not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
    if cerror
      messboxwait(" No further Debit notes to process from Returns ",0,0,1)
      vunloadif("retn2mfr.vw")
      return (-1)
    else
      x = bpopdb("retn2mfr",5,"","[Vdesc]","L70","[Supplier_Code]","L0","[ReferenceNr]",5,5,21,75,"",0)
      if x = -1
        repaint off
        vunloadif("retn2mfr.vw")
        return(1)
      else
        repaint off
        invref  = [Invoice_Nr]
        ordref  = [ReferenceNr]
        vloadif(dpath|"drnotes2.vw")
        order change key "[Order_Nr]"
        data find "[Order_Nr]" equal ordref options ""
        invref1  = [Invoice_Nr]		' message "invref1 is:"&str(invref1)
        x=ProcessDrNote()
        if x=-1
          exit while
        else
          vloadif(dpath|"retn2mfr.vw")
          lock-record
            [DrNote]=drnr
            [DN_sheet]=today
          write-record
        end if
      end if
    end if
  end while
END FUNCTION ' Returns2Whse()

