          SUBROUTINE (ROID,GEN,VIEW.ONLY,AUTO.POP)
** Version# 17.0001[1] - 11/14/2014 - 01:13pm - TSMITH - eclipse

*** Subroutine - RENTAL.DEPOSIT
*-------------------------------------------------------------------------*
*** This program will calculate the deposit required and pop the deposit
*** screen so that the deposit amount can be collected.
*-------------------------------------------------------------------------*
*** ROID      - Rental Id                                             [IN]
*** GEN       - Generation                                            [IN]
*** VIEW.ONLY - Set to True if view only mode                         (IN)
*** AUTO.POP  - Set to Yes if we are trying to automatically pop      (IN)
***             this screen up from the status screen.  It will
***             return out if automatically if there is no deposit
***             required.
*-------------------------------------------------------------------------*
          CHECK.KEY 'CREDIT.AUTH.CODE',AUTH.OK,AUTH.LVL

          READ DFLT.PERC FROM CTRLFILE,'RENTAL.PERCENT' ELSE DFLT.PERC = ''
          QSIGN  = -1
          NEW.OK = YES

          GOSUB CALC.DEP.AMT
          GOSUB INIT
          IF LOCK.ERR THEN PRINT BELL:; RETURN
          IF NOT(LED.EXISTS) THEN GOSUB BUILD.LED ELSE GOSUB SET.GEN
          PGEN = DCOUNT(LED(12),VM)+1
          GOSUB INIT.GEN
          IF AMT.DUE = 0 AND AUTO.POP THEN GOTO FINISH2
          WINDOW ,4,,,9,'RENTAL.DEPOSIT'
          GOSUB DISPLAY
          MENU.LOAD 2,6,12,6,'P'

V.ONLY:   IF VIEW.ONLY THEN
             PRINT @(2,0):BLINK$:'View Only':NORM$
IN.VIEW:     INP A,0,0,0
             GOTO FINISH
          END
*-------------------------------------------------------------------------*
IN.CASH:  AMT = CASH.RECVD
IN.CASH2: INP AMT,17,2,11,'N2'
          IF CHANGED THEN
             CASH.RECVD = AMT
             CHANGE.AMT = 0
             GOSUB DISPLAY
          END
          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.CASH, IN.CASH, IN.CASH, IN.CHNG, IN.CHK
*-------------------------------------------------------------------------*
IN.CHNG:  AMT = CHANGE.AMT
IN.CHNG2: INP AMT,37,2,11,'N2'
          IF CHANGED THEN
             CHANGE.AMT = AMT
          END
          IF CHANGE.AMT>CASH.RECVD AND (CHANGE.AMT#0 OR CASH.RECVD>0) THEN
             PRINT BELL:; GOTO IN.CHNG
          END
          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.CHNG, IN.CASH, IN.CHNG, IN.CHNG, IN.CHK.NO
*-------------------------------------------------------------------------*
IN.CHK:   AMT = CHECK.RECVD
IN.CHK2:  INP AMT,17,3,11,'N2'
          IF CHANGED THEN
             CHECK.RECVD = AMT
             GOSUB DISPLAY
          END
          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.CHK, IN.CHK, IN.CASH, IN.CHK.NO, IN.CARD
*-------------------------------------------------------------------------*
IN.CHK.NO: *
INCNO:    INP CHECK.NO,37,3,15
          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.CHK.NO, IN.CHK, IN.CHK.NO, IN.CHK.NO,IN.CARD
*-------------------------------------------------------------------------*
IN.CARD:  INP TMP.RECVD,17,4,11,'N2'
          IF CHANGED THEN
CC.CHNG:     IF AUTH.CODE AND AUTH.LVL<3 THEN
                GOSUB DISPLAY
                GOTO IN.CARD
             END
             UT.SEC3 9,HAS.CC,,NO
             CCARD.RECVD = TMP.RECVD

             * If the site does not have the credit card module, then we
             * need to setup some default payment information
             IF NOT(HAS.CC) THEN
                PAY.INFO       = ROID
                PAY.INFO<1,2>  = GEN
                PAY.INFO<1,3>  = PGEN
                PAY.INFO<1,9>  = COLLECT.BY
                PAY.INFO<1,10> = COLLECT.LOCA
                IF COLLECT.TIME = '' THEN COLLECT.TIME = INT(TIME())
                PAY.INFO<1,11> = COLLECT.TIME
                PAY.INFO<1,12> = PAYMENT.DT
                PAY.INFO<1,13> = CHANGE.AMT
                PAY.INFO<1,14> = CHECK.NO
             END ELSE
                GOSUB UPD.GENS
                PAY.INFO = ""
             END

             * Authorize the credit card amount
             SOE.CREDIT.AUTH ROID,PGEN,GEN,CCARD.RECVD,VIEW.ONLY,PAY.INFO

             * Check to see if the credit card was declined
             IF NOT(HAS.CC) THEN
                DECLINED = ""
             END ELSE
                DECLINED = PAY.INFO
             END

             * If the credit card was declined, make sure
             IF NOT(DECLINED) THEN
                CCARD.RECVD = 0
                GOSUB UPD.GENS
                *** Order has been voided back out money received.
                IF DECLINED = 'VOID' THEN
                   AUTH.CODE = "Void Sale"
                END
             END ELSE
                GET.LEDCC.VAL CCARD.NO,3,ROID,PGEN
             END

             GOSUB DISPLAY
          END
          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.CARD, IN.CARD, IN.CHK, IN.CARD, IN.CARD,IN.CARD
*-------------------------------------------------------------------------*
PAY.MISC: WINDOW 40,2,40,5,3
          PRINT @(0,0):'Payment Date : ':OCONV(PAYMENT.DT,'D4/')   "L#10"
          PRINT @(0,1):'Payment Time : ':OCONV(COLLECT.TIME,'MTH') "L#7"
          PRINT @(23,1):TIME.ZONE$ "L#3"
          PRINT @(0,2):'Collected By : ':COLLECT.BY                "L#10"
          PRINT @(0,3):'Collect Loca : ':COLLECT.LOCA              "L#15":
          PRINT @(0,4):'Collect Brch : ':PAYMENT.BR                "L#4":

IN.PDAT:  INP P.DT,15,0,10,'D4/'
          IF CHANGED THEN
             CHK.GL.POST P.DT,OK,'C'
             IF NOT(OK) THEN
                MESS 2,2,BELL:'Payment Date is in a closed Period'
INPDAT2:        INP A
                PRINT @(15,0):OCONV(PAYMENT.DT,'D4/') "L#10"
                GOTO IN.PDAT
             END
             PAYMENT.DT = P.DT
          END
          IF PAYMENT.DT = '' THEN PRINT BELL:; GOTO IN.PDAT
          IF QUIT THEN GOTO EXT.MISC
          ON MOVE+1 GOTO IN.PDAT, IN.PDAT, IN.PDAT, IN.PDAT

IN.PTIM:  INP COLLECT.TIME,15,1,7,'MTH'
          IF COLLECT.TIME='' THEN
             COLLECT.TIME = INT(TIME())
             PRINT @(15,1):OCONV(COLLECT.TIME,'MTH') "L#7"
          END
          IF QUIT THEN GOTO EXT.MISC
          ON MOVE+1 GOTO IN.PTIM, IN.PTIM, IN.PDAT, IN.PTIM

IN.CBY:   INP COLLECT.BY,15,2,8,V_'INITIALS'
          IF COLLECT.BY='' THEN PRINT BELL:; GOTO IN.CBY
          IF QUIT THEN GOTO EXT.MISC
          ON MOVE+1 GOTO IN.CBY, IN.CBY, IN.PTIM, IN.CBY

IN.CLOC:  INP COLLECT.LOCA,15,3,15,V_'S:VERF.LOC.MAINT'
          IF QUIT THEN GOTO EXT.MISC
          ON MOVE+1 GOTO IN.CLOC, IN.CLOC, IN.CBY, IN.CLOC

IN.PBR:   INP.BR 15,4,4,PAYMENT.BR
          IF PAYMENT.BR='' THEN GOTO IN.PBR
          IF QUIT THEN GOTO EXT.MISC
          ON MOVE+1 GOTO IN.PBR, IN.PBR, IN.CLOC, IN.PBR, IN.PBR
          GOTO IN.PBR

EXT.MISC: WINDOW.CLOSE
          RETURN
*-------------------------------------------------------------------------*
CALC.DEP.AMT: *** Calculate the deposit amount required.

          MATBUILD RLD     FROM LD
          DEP.AMT     = 0
          TOT.DEP.AMT = 0
          LDIDS       = RAISE(LED(48)<1,GEN>)
          LD.CNT      = DCOUNT(LDIDS,VM)
          FOR LD.ID = 1 TO LD.CNT
             LDID = LDIDS<1,LD.ID>
             LD.GET LDID
             READV DEP.PERC FROM RENTFILE,LD(32),13 ELSE DEP.PERC = ''
             READV ORIG.VAL  FROM RENTFILE,LD(32),3 ELSE ORIG.VAL   = ''
             IF DEP.PERC = '' THEN
                DEP.PERC = DFLT.PERC
             END
             PRD.DEP.AMT  = OCONV(ORIG.VAL,'MR3')*OCONV(DEP.PERC,'MR2')/100
             TOT.DEP.AMT += ICONV(PRD.DEP.AMT,'MR9')
          NEXT LD.ID
          MATPARSE LD     FROM RLD
          *** get the customers credit limit and figure out if they will
          *** still owe a deposit based off this credit limit.
          GET.AVAIL.CREDIT LED(5)<1,GEN>,CRAVAIL
          *** we have to add back in the deposit amount since it was
          *** included in the credit check and we need to exclude it
          *** to get the correct amount
          CRAVAIL = ICONV(OCONV(CRAVAIL,'MR2') +OCONV(TOT.DEP.AMT,'MR9'),'MR2')
          IF CRAVAIL < 0 THEN CRAVAIL = 0
          DEP.AMT = OCONV(CRAVAIL,'MR2') - OCONV(TOT.DEP.AMT,'MR9')
          IF DEP.AMT < 0 THEN
             DEP.AMT = ICONV(DEP.AMT * -1,'MR9')
          END ELSE
             DEP.AMT = 0
          END
          RETURN
*-------------------------------------------------------------------------*
INIT:     GL.BR    = LED(2)<1,GEN,3>
          LOCK.ERR = NO
          LOG.MV   = ''
          GEN.MV   = ''

          PRC.DATE = DATE()
          RENTAL.CALC.AMT.TOTAL ROID,GEN,PRC.DATE,TOTAL.AMT

*** Save the rental led arrays
          MATBUILD RLED    FROM LED
          MATBUILD ROLED   FROM OLED
          MATBUILD RLD     FROM LD
          MATBUILD ROLD.LD FROM OLD.LD

*** Check if a ledger record needs to be created or not
          READV TST FROM LEDFILE,ROID,1 ELSE TST = ""
          LED.EXISTS = (TST#"")
          OE.LOCK.LED ROID,LOCK.MSG,YES
          IF LOCK.MSG#"" AND LOCK.MSG[1,7]#"You are" THEN LOCK.ERR=YES

          RETURN
*-------------------------------------------------------------------------*
CALC.AMT.DUE: *

          AMT.DUE   = DEP.AMT
          TOT.RECVD = CASH.RECVD + CHECK.RECVD + CCARD.RECVD
          TOT.RECVD = ICONV(OCONV(TOT.RECVD,'MR2'),'MR9')
          AMT.DUE  -= TOT.RECVD
          IF AMT.DUE < 0 THEN AMT.DUE = 0
          RETURN
*-------------------------------------------------------------------------*
INIT.GEN: * Initialize the payment variables

          *** payments come from the ledger record with the
          *** same rental OID.
          READV PMTS FROM LEDFILE,ROID,45 ELSE PMTS = ''
          GN.CNT = DCOUNT(PMTS<1>,VM)
          CASH.RECVD  = 0
          CHECK.RECVD = 0
          CCARD.RECVD = 0
          AMT.RECVD   = 0
          FOR GNN = 1 TO GN.CNT
             PMT = PMTS<1,GNN>
             IF PMT<1,1,1> # '' THEN AMT.RECVD += PMT<1,1,1>
             IF PMT<1,1,2> # '' THEN AMT.RECVD += PMT<1,1,2>
             IF PMT<1,1,3> # '' THEN AMT.RECVD += PMT<1,1,3>
          NEXT GNN

          PERC.AMT.DUE = OCONV(CUSS(22)<1,2,3>,'MR2')
          AMT.DUE   = DEP.AMT
          AMT.RECVD = ICONV(OCONV(AMT.RECVD,'MR2'),'MR9')
          AMT.DUE  -= AMT.RECVD
          IF AMT.DUE < 0 THEN AMT.DUE = 0

          CHECK.NO      = LED(46)<1,PGEN,2>
          FLD.ARGS      = 3
          FLD.ARGS<1,2> = 4
          FLD.ARGS<1,3> = 12
          GET.LEDCC.VAL RTN.DATA,FLD.ARGS,ROID,PGEN
          CCARD.NO      = RTN.DATA<1,1>
          AUTH.CODE     = RTN.DATA<1,2>
          CUR.STAT      = RTN.DATA<1,3>
          PAYMENT.DT    = LED(23)<1,PGEN>
          IF PAYMENT.DT = '' THEN PAYMENT.DT = DATE()
          COLLECT.BY    = LED(27)<1,PGEN,1>
          IF COLLECT.BY = '' THEN COLLECT.BY = USER.ID
          COLLECT.LOCA  = LED(27)<1,PGEN,2>
          IF COLLECT.LOCA = '' THEN COLLECT.LOCA = LOCATION
          COLLECT.TIME  = LED(27)<1,PGEN,3>
          CHANGE.AMT    = 0
          LOCATE 'CASHBOX' IN LED(24)<1,PGEN> SETTING POS THEN
             PAYMENT.BR  = FIELD(LED(25)<1,PGEN>,'~',1)
             PAYMENT.AMT = LED(26)<1,PGEN,POS>
          END ELSE
             GET.TTY PAYMENT.BR
             IF PAYMENT.BR='' THEN PAYMENT.BR = GL.BR
             PAYMENT.AMT = 0
          END

          RETURN
*-------------------------------------------------------------------------*
DISPLAY: *
          GOSUB CALC.AMT.DUE
          PRINT @(17,1):OCONV(AMT.DUE,'MR9')  "R2#11"
          PRINT @(17,2):CASH.RECVD            "R26#11"
          PRINT @(37,2):CHANGE.AMT            "R26#11"
          PRINT @(17,3):CHECK.RECVD           "R26#11"
          PRINT @(37,3):CHECK.NO              "L#16"
          PRINT @(17,4):CCARD.RECVD           "R26#11"
          PRINT @(40,4):AUTH.CODE             "L#13"
          RETURN
*-------------------------------------------------------------------------*
BUILD.LED:   * Default the billing ledger record
          MATPARSE LED FROM RLED
          LED(6)<1,GEN>  = "X"
          LED(9)         = DATE()
          LED(10)        = DATE()
          LED(21)<1,GEN> = ""
          LED(22)<1,GEN> = DATE()
          LED(28)<1,GEN> = ""
*** Default terms from the customer file
          LED(29)<1,GEN> = CUSS(28)
          LED(48)<1,GEN> = ""
          LED(49)        = ""
          LED(50)        = 1

*** Clear the ld attbs
          FOR LD.NUM = 150 TO 200
             LED(LD.NUM) = ""
          NEXT LD.NUM

          COMMENT = "Rental Billing Created."
          OE.ADD.COMMENT ROID,,COMMENT
          GEN.MV = GEN
          LOG.MV = GEN
          RETURN
*-------------------------------------------------------------------------*
SET.GEN:  * Set the gen to attach new payment to
          LOCATE 'I' IN LED(6)<1> SETTING GEN ELSE GEN = 1
          RETURN
*-------------------------------------------------------------------------*
SUBS:     ON OPTION GOTO PAY.MISC
*-------------------------------------------------------------------------*
PAYMENTS: *
  ******* Custom *******************
  *       IF CCARD.RECVD AND LED(46)<1,PGEN,4>='' THEN PRINT BELL:; RETURN
  ***********************************
          GOSUB UPD.GENS
          IF NEW.OK THEN PLIST = 'New' ELSE PLIST = ''
          GEN.CT = DCOUNT(LED(12),VM)
          FOR GN = 1 TO GEN.CT
          IF LED(6)<1,GN,1>='$' THEN
             LOCATE 'CASHBOX' IN LED(24)<1,GN> SETTING POS THEN
                PAY.BR  = FIELD(LED(25)<1,GN>,'~',1)
                PAY.AMT = LED(26)<1,GN,POS>
                END ELSE PAY.AMT = 0; PAY.BR = ''
             P =  ROID"L#8":'.':LED(8)<1,GN>"R%3"
             P := PAY.BR"R#5"
             P := '  ':OCONV(LED(23)<1,GN>,'D2/')"L#8"
             P := PAY.AMT"R26#12"
             P := '  ':LED(27)<1,GN,1>"L#10"
             P := '  ':LED(27)<1,GN,2>"L#10"
             PLIST<1,-1> = P
             END
          NEXT GN
          CHECK.KEY 'SOE.PAYMENT.EDIT',EDIT.OK
          TITLE = 'Payments'
          IF NOT(EDIT.OK) THEN TITLE = TITLE:' - View Only'
          MENU.TABLE P,7,8,1,5,66,,,PLIST,TITLE
          IF P='' OR NOT(EDIT.OK) THEN RETURN
          SV.PGEN = PGEN
          IF P='New' THEN
             PGEN = DCOUNT(LED(12),VM)+1
          END ELSE
             INVN = P[10,3]+0
             LOCATE INVN IN LED(8)<1> SETTING PGEN ELSE
                PGEN = DCOUNT(LED(12),VM)+1
             END
             CHK.GL.POST LED(23)<1,PGEN>,OK,'C'
             IF NOT(VIEW.ONLY) THEN
                IF NOT(OK) THEN
                   MESS 5,5,BELL:'Payment is in a closed Period'
IN$$12:            INPNO A
                   PGEN = SV.PGEN
                   END
                END
             END
          GOSUB INIT.GEN
          GOSUB DISPLAY
          RETURN
*-------------------------------------------------------------------------*
CRS:      *** Show all of the cash receipts and amounts applied to this inv
          SOE.SHOW.PAYMENTS ROID,GEN,QSIGN,1
          RETURN
*-------------------------------------------------------------------------*
FILEIT:   *
          IF NOT(F12) THEN
             GOSUB UPD.GENS
          END
          IF AMT.DUE > 0 THEN
             ANS  = "N"
             PRMT = "You Are About to Override Cash Required! Continue (Y/N) : "
IN$$1:       INP.PROMPT ANS,PRMT,"YN",1
             IF NOT(ANS) THEN
                MOVE = 0; QUIT = 0; F12 = 0
                GOTO IN.CASH
             END
          END
          GOTO FINISH
*-------------------------------------------------------------------------*
UPD.GENS: IF VIEW.ONLY THEN RETURN

     * Save Deposit Data
          GOSUB UPD.PAY.GEN

          IF NOT(UPDT) THEN RETURN

          IF NOT(LED.EXISTS) THEN
             COMMENT = "Rental Billing Created."
             OE.ADD.COMMENT ROID,,COMMENT
          END

     * Add all GEN Updated to Log list
          GN.CT = DCOUNT(GEN.MV,VM)
          FOR J = 1 TO GN.CT
          LGEN = GEN.MV<1,J>
          LOCATE LGEN IN LOG.MV<1> SETTING X ELSE
             LOG.MV<1,-1> = LGEN
          END

     * Calc A/R posting amount for all GENs that changed
          IF LED(6)<1,LGEN>='Y' THEN
             CALC.GL.SUM.AMT ROID,LGEN,LED(2)<1,LGEN,3>,'UBAP'
          END ELSE
             CALC.GL.SUM.AMT ROID,LGEN,LED(2)<1,LGEN,3>,'AR'
          END
          NEXT J

          GENS = GEN.MV
          UPDATE.LEDGER ROID,GEN.MV

          RETURN
*-------------------------------------------------------------------------*
UPD.PAY.GEN: *
          IF CASH.RECVD+CHECK.RECVD+CCARD.RECVD=0 AND LED(6)<1,PGEN>='' THEN
             UPDT = NO
             RETURN
          END
          UPDT = YES
*** Default the new gens information
          IF LED(6)<1,PGEN> = '' THEN
             GET.NEXT.INVN INVN
             LED(1)<1,PGEN>    = LED(1)<1,GEN>
             LED(2)<1,PGEN>    = LED(2)<1,GEN>
             LED(3)<1,PGEN>    = ''
             LED(4)<1,PGEN>    = LED(4)<1,GEN>
             LED(5)<1,PGEN>    = LED(5)<1,GEN>
             LED(6)<1,PGEN>    = '$'
             LED(8)<1,PGEN>    = INVN
             LED(9)<1,PGEN>    = ''
             LED(12)<1,PGEN>   = LED(11)
             LED(11) += 1
             LED(48)<1,PGEN>   = 'PAYMENT'
          END

*** Set the information read from the payment
          LED(27)<1,PGEN,1> = COLLECT.BY
          LED(27)<1,PGEN,2> = COLLECT.LOCA
          IF COLLECT.TIME = '' THEN COLLECT.TIME = INT(TIME())
          LED(27)<1,PGEN,3> = COLLECT.TIME
          LED(23)<1,PGEN>   = PAYMENT.DT

          * Do not update LED(45) with a $0 cash amount
          IF ((CASH.RECVD - CHANGE.AMT) # 0) THEN
             LED(45)<1,PGEN,1> = CASH.RECVD - CHANGE.AMT
          END ELSE
             LED(45)<1,PGEN,1> = ""
          END

          * Do not update LED(45) with a $0 check amount
          IF (CHECK.RECVD # 0) THEN
             LED(45)<1,PGEN,2> = CHECK.RECVD
          END ELSE
             LED(45)<1,PGEN,2> = ""
          END

          * Do not update LED(45) with a $0 credit card amount
          IF (CCARD.RECVD # 0) THEN
             LED(45)<1,PGEN,3> = CCARD.RECVD
          END ELSE
             LED(45)<1,PGEN,3> = ""
          END

          LED(46)<1,PGEN,2> = CHECK.NO
          LED(46)<1,PGEN,3> = CCARD.NO
          LED(46)<1,PGEN,4> = AUTH.CODE
          PAYMENT.AMT = SUM(LED(45)<1,PGEN>)
          LED(24)<1,PGEN> = ''
          LED(25)<1,PGEN> = ''
          LED(26)<1,PGEN> = ''
          LED.GL.AUTOPOST ROID,PGEN,PAYMENT.BR,'CASHBOX', PAYMENT.AMT
          LED.GL.AUTOPOST ROID,PGEN,GL.BR     ,'AR'     ,-PAYMENT.AMT
          LOCATE PGEN IN GEN.MV<1> SETTING POS ELSE
             GEN.MV<1,-1> = PGEN
          END

          COMMENT = "Rental Deposit Taken."
          OE.ADD.COMMENT ROID,,COMMENT
          IF LED(46)<1,PGEN,3> AND LED(46)<1,PGEN,3> # '1ENC' THEN
             * Store credit card data for PGEN in LEDGER.CC file field 20
             * Reset LED(46)<1,PGEN,3> = '1ENC' to indicate conversion
             PUT.LEDCC.VAL OID,PGEN
          END

          RETURN
*-------------------------------------------------------------------------*
FINISH:   WINDOW.CLOSE
FINISH2:  OE.UNLOCK.LED ROID
          MATPARSE LED  FROM RLED
          MATPARSE OLED FROM ROLED
          MATPARSE LD     FROM RLD
          MATPARSE OLD.LD FROM ROLD.LD
          RETURN
*-------------------------------------------------------------------------*
!TSMITH~11/14/14~13:13
