'CR_NOTES - creates Credit Note in CUST_ORD file

external   messbox() fentrybox() chkdate() dpath vloadif() progress()
external   vunloadif() sch scw cpath shopmask fgp bgp userid scr
external   exception() vatrate messboxwait()

public     ptstr origjobnr custcode credref

global     $mess cat ProcessCredit() AddVarn()
global     cred_date
global     CheckNr() custname orderdate
global     invtot rcvd
global     credtot
global     x ReturnToMenu()
global     lastbal
global     balancedue
global     origrecnr $sales $type #vat abbrv_name

MAIN
single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  quiet on

'   vloadif(dpath|"cust_ord.vws")
  vloadif(dpath|"ordstat4.vw")
  #vat = vatrate

  while true

     x = CheckNr()                              ' check whether Job Nr exists in
     if x = -1
       exit while
     end if

  end while

  ReturnToMenu()

END MAIN


FUNCTION CheckNr()                     ' checks for JobNr - if not found
local    response
  custcode = ""
  credref = ""
  origjobnr = ""
  custname = ""
  invtot = 0
  orderdate = today
  while true
    screen clear box 1 1 sch scw 0 0 no-border
    while true
      x = fentrybox(" Enter Order Nr that Credit Note is for - {Esc} exits ",6,shopmask,"")
      if x = -1
        return (-1)
      elseif x = 0
        exit while
      end if
    end while
    origjobnr = ptstr
    order change key "[Job_Nr]"
    progress(fgp,bgp," Checking for original order ",0)
    data find "[Job_Nr]" equal origjobnr options ""
    if cerror                               '   if none - then return
      messbox("ÿJob Nr not yet entered - cannot process Credit Note ",0,1,1)
      return (1)
    else
' is this correct
      repaint on
      repaint
      messbox(" Is this the original Order Nr? (y/n) ",1,0,1)
      if ptstr == "n"
        return (1)
      end if
      repaint off
      custcode = [Customer_Code]
      custname = [CustOrd_Name]
      origrecnr = precord
    end if

    while true
      x = fentrybox(" Enter Credit Note Reference Nr ",6,shopmask,"")
      if x = 0
        exit while
      end if
    end while
    credref = ptstr

    data find "[Job_Nr]" equal credref options ""
    if cerror                               '   if none - then return
      messbox("ÿEnter details for Credit Note ",0,1,1)
    else
      messbox("ÿCredit already entered ",0,0,1)
      ReturnToMenu()
    end if

'     screen clear box 1 1 sch scw 0 0 no-border
    while true
      x = fentrybox(" Enter Date of Credit Note ",10,"##\/##\/####",today)
      if x = 0
        cred_date = ptstr
        if chkdate(cred_date,1) = -1
	  messbox(" Incorrect date - re-enter ",0,0,1)
	  continue while
	end if
        exit while
      end if
    end while

'     screen clear box 1 1 sch scw 0 0 no-border
    while true
      x = fentrybox(" Amount of Credit Note - (do NOT enter minus figures) ",8,"*8{[-1234567890.]}",format(invtot,"2r,"))
      if x = 0
        exit while
      end if
    end while
    credtot = -value(ptstr)

    messbox(" Credit Note to "|custname|" for "|currency(-credtot)|" Correct?(y/n)",1,0,1)
    if ptstr == "y"
      exit while
    end if
  end while

  progress(fgp,bgp,"ÿEntering recordsÿ",0)

' ' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
' ' º  Create new "cashrcvd" record for credit note amount               º
' ' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
'   vloadif(dpath|"cashrcvd.vws")
'   data enter lock
'     [Date_Of_Receipt]   = cred_date
'     [Job_Nr]            = origjobnr
'     [Method_Of_Payment] = "CREDIT"
'     [Abbrv_M]           = "O"
'     [SalAnal]           = left(credref,1)
'     [Branch]            = left(credref,1)
'     [Receipt_Nr]        = "NONE"
'     [Amount_Received]   = credtot
'     [Entered_By]        = userid
'     [Time]              = now
'   write-record

' ' transact Cash/Credit Note
'    vloadif(dpath|"cust_ord.vws")
'    lastbal = value([Balance_Due])
'    balancedue = lastbal + credtot
'    lock-record
'      [Balance_Due] = balancedue
'    write-record

  ProcessCredit()

END FUNCTION


FUNCTION ProcessCredit()
  $type  = case left(credref,1)("C","S")("R","S")("S","S")("F","S")("P","F")("W","H")("T","H")
  $sales = case left(credref,1)("C","X")("R","R")("S","S")("F","F")("P","X")("W","W")("T","T")("Y","Y")
  vloadif(dpath|"cust_ord.vws")
  abbrv_name = proper(left(custname,7))   'message "$sales is:"&str($sales)
  if len(custname) = 0
    messboxwait(" Customer's name has been omitted - pls contact David @ HO ",0,0,1)
  end if
  if len(abbrv_name) = 0
    messboxwait(" Customer's abbrv'd name has been omitted - pls contact David @ HO ",0,0,1)
  end if
  data enter lock
    [Job_Nr]        = credref
    [SalesAnalysis] = $sales
    [Branch]        = left(credref,1)
    [Type_Branch]   = $type
    [CustOrd_Name]  = custname
    [Abbrv_Name]    = abbrv_name
    [Date_Of_Order] = cred_date
    [Invoice_Total] = 0
    [Net_Invoice]   = 0
    [Balance_Due]   = 0
    [Customer_Code] = custcode
    [Updated_By]    = userid
    [Last_Update]   = today
    [Order_Status]  = "H"              ' use for order processing only
    [PDA]           = "Y"
    [Origin]        = "E"
    [Completed]     = "N"
  write-record
'   recnr = precord
'   $status = "A"
  AddVarn(credref|"-00",credtot,"Credit Note","Credit","CREDIT",cred_date)
  $mess = "Credit Note raised for"&custname&"("|credref|")"&currency(credtot)
  cat = "SUNDRY"
  x = exception(userid,today,time24,cat,$mess)
  vunloadif(dpath|"unread1.vw")
END FUNCTION 'ProcessCredit()


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 AddVarn(varnr,#gross,$reas,$ref,$notif,$varndate)
local balancedue lastbal newtotal oldtotal newnet oldnet #prec x
  vloadif(dpath|"addvarn3.vw")
  data enter lock
    [Var_Nr]        = varnr
    [VarnJobNr]     = left(varnr,6)
    [Amount_Gross]  = #gross
    [Reason]        = $reas
    [Customers_Ref] = $ref
    [Notif_Method]  = $notif
    [Date]          = $varndate
    [Entered_By]    = userid
  write-record

  lastbal = round([Balance_Due],2)
  balancedue = lastbal + #gross
  oldtotal = round([Invoice_Total],2)
  newtotal = oldtotal + #gross
  oldnet   = round([Net_Invoice],2)
  newnet   = oldnet + round(#gross*100/(100+vatrate),2)
  lock-record
    [Balance_Due]   = balancedue
    [Invoice_Total] = newtotal
    [Net_Invoice]   = newnet
'     [Order_Status]  = "U"
  write-record
END FUNCTION ' AddVarn()

