          SUBROUTINE (CN,NO.OE,COD,PRT.MSG,NO.SHIP,AUTHNAME,MESSAGE,INCL.OID,INCL.GEN,CLIM.WRN,CUSS23)
** Version# 44.0003[6] - 04/13/2016 - 03:36pm - TSMITH - eclipse
*** V44.0003 Change - Custom Coding . - 04/13/2016 - TSMITH - eclipse

*** Subroutine - SOE.CREDIT.CHECK
*-------------------------------------------------------------------------*
*** Uses the flags from the Credit Control Parameters screen and uses
*** these to do a credit check.
*-------------------------------------------------------------------------*
*** CN       - customer number                                        [IN]
*** NO.OE    - Indicates the current user is not allowed to enter     (OUT)
***            order entry for the current customer (CN), unless
***            they have authorization.
*** COD      - Yes for COD                                            (OUT)
*** PRT.MSG  - Set to YES when "See Credit Manager" gets printed.     (OUT)
*** NO.SHIP  - If YES, this indicates that the current order          (OUT)
***            cannot be shipped (without auth) due to a credit
***            limit problem.
*** AUTHNAME - NOT USED
*** MESSAGE  - Warning message for various problems.                  (OUT)
*** INCL.OID - Indicates if the current order should be included      (IN)
***            in the Credit Check.
*** INCL.GEN - Indicates which GEN of the current order should be     (IN)
***            checked.
*** CLIM.WRN - This is the "Credit Warning Message" that should be    (OUT)
***            displayed on the screen in Order Entry.
*** CUSS23   - Customer credit params (for Solar)                     (OUT)
*-------------------------------------------------------------------------*
*** Common:
*** CUSS must be current for this routine.
*** LED must be current if INCL.OID or INCL.GEN are not empty
***
*** NOTE: Since this subroutine can be called from outside OE where LED is
***       not set, all code accessing LED should first check INCL.OID or
***       INCL.GEN
*-------------------------------------------------------------------------*
          NO.OE    = NO
          COD      = NO
          PRT.MSG  = NO
          NO.SHIP  = NO
          MESSAGE  = ''
          CLIM.WRN = ''

          *** Master Job Bids need no Credit Check
          IF INCL.OID AND (LED(128)<1,1> # "") THEN RETURN
          IF INCL.OID AND (LED(129)<1,2> # "") THEN RETURN

          IF INCL.GEN THEN
*            READV CUSS23 FROM TERMSFILE,LED(29)<1,INCL.GEN>,28 ELSE CUSS23 = ''
             TMP = LED(23)<1,INCL.GEN>
             IF TMP+0 = 0 THEN TMP = LED(9)<1,INCL.GEN>
             TERMS.VALUE CUSS23,LED(29)<1,INCL.GEN>,28,TMP,ERRS
             IF CUSS23 THEN CUSS(23) = CUSS23
          END

          ORD.AMT=0; PAY.AMT=0; DISC.AMT=0
          IF INCL.OID # '' AND INCL.GEN # '' THEN
             OE.GET.QSIGN QSIGN,INCL.OID,INCL.GEN
             GOSUB GET.ORD.AMT
          END

* If Credit Control disabled, Return
          IF CUSS(23)<1,10> THEN IF SUM(CUSS(23))=1 THEN RETURN

          IF INCL.OID#'' THEN
             GOSUB GET.PAY.AMT
             GOSUB GET.DISC.AMT
          END

          IF INCL.OID # '' AND PAY.AMT >= (ORD.AMT + DISC.AMT) AND ORD.AMT # 0 THEN ALL.CASH = YES ELSE ALL.CASH = NO

          GET.AVAIL.CREDIT CN,CRAVAIL,PASTDUE,OTHERDUE,CRLIM,INCL.OID,INCL.GEN

          IF CUSS(22)<1,2,1> AND PASTDUE>CUSS(22)<1,1,2>+0 THEN
             OVER = YES
          END ELSE
             IF CRAVAIL < 0 THEN OVER=YES ELSE OVER=NO
          END
          * If they have authorized personnel set in credit params, check
          * that the ordered by is valid and they are under their max
          * CUSS(27) is "Auth personnel only"
          IF NOT(OVER) AND CUSS(27) AND INCL.OID AND INCL.GEN AND LED(68)<1,INCL.GEN> THEN
             * There can be excess space stored in CUSS(29), remove it
             * Also match case in case it was manually changed in header
             ORDEREDBY = TRIM(OCONV(LED(68)<1,INCL.GEN>,'MCU'))
             AUTHPERSONLIST = TRIMS(OCONV(CUSS(29)<1>,'MCU'))
             LOCATE ORDEREDBY IN AUTHPERSONLIST<1> SETTING POS THEN
                AUTHPERSONMAX = CUSS(30)<1,POS>
                IF AUTHPERSONMAX # '' THEN
                   IF ORD.AMT > AUTHPERSONMAX THEN OVER = YES
                END
             END ELSE
                OVER = YES
             END
          END

          IF INCL.OID#'' AND INCL.GEN#'' THEN
             OE.GET.QSIGN QSIGN,INCL.OID,INCL.GEN
             OE.ORDER.TOTAL.BY.TYPE INCL.OID,QSIGN,,,,,ORD.TOTAL
             CR.OVRD = LED(126)<1,1> + 0
             * If override is less than amt of gen - can't use it
             IF CR.OVRD THEN
                IF CR.OVRD < ORD.TOTAL THEN CR.OVRD = 0
             END
          END ELSE
             CR.OVRD = 0
             ORD.TOTAL = 0
          END

          IF OVER AND CR.OVRD THEN OVER = NO

          CUSLIMIT = CUSS(22)<1,1,3>+0
          IF CUSLIMIT>0 AND INCL.GEN THEN
             GET.CUS.JLIMIT CN,JLIMIT
          END ELSE
             JLIMIT   = 0
             CUSLIMIT = 0
          END
          IF CUSS(23)<1,1> OR (CUSS(23)<1,2> AND OVER)   THEN GOSUB COD
          IF CUSS(23)<1,3> OR (CUSS(23)<1,4> AND OVER)   THEN GOSUB MSG
          IF CUSS(23)<1,5>                               THEN NO.OE = YES
          * if credit override amt covers gen, then credit is authorized
          IF NOT(CR.OVRD) THEN
             IF NOT(ALL.CASH) THEN
                IF CUSS(23)<1,6> AND OVER OR CUSS(23)<1,7> THEN NO.OE = YES
                IF CUSS(23)<1,8> AND OVER OR CUSS(23)<1,9> THEN GOSUB NO.SHIP
             END ELSE
                IF CUSS(23)<1,7>                         THEN NO.OE = YES
                IF CUSS(23)<1,9>                         THEN GOSUB NO.SHIP
             END
             IF CUSLIMIT < JLIMIT                        THEN GOSUB JLIM

             GOSUB CHK.CWARN
          END

        ** Code to notify users of inactive customers.
        LIMIT = CUSS(22)<1,1,1>
        STAT = LED(6)
        IF LIMIT=100 AND STAT="B" THEN
        MESSAGE = 'Inactive Cust - $1 Limit'
        END





          ** Return current value of CUSS(23) back to client
          CUSS23 = CUSS(23)

          RETURN
*-------------------------------------------------------------------------*
COD:      COD = YES
          GOSUB GET.PMT
          MESSAGE = PMT:"COD orders Only!"
          RETURN
*-------------------------------------------------------------------------*
MSG:      PRT.MSG = YES
          PUSH.JOB.STACK "CREDIT.MGR"
          MESSAGE = "See Credit Manager"
          RETURN
*-------------------------------------------------------------------------*
JLIM:     NO.SHIP = YES
          MESSAGE = "Job Limit Exceeded"
          RETURN
*-------------------------------------------------------------------------*
NO.SHIP:  NO.SHIP = YES
          IF INCL.GEN AND LED(8)<1,INCL.GEN> THEN RETURN
          IF (INCL.OID[1,1] = "R") THEN
             MESSAGE = 'Rental Agreement on Hold'
          END ELSE
             MESSAGE = 'Shipping ticket on Hold'
          END
          RETURN
*-------------------------------------------------------------------------*
GET.PMT:  BEGIN CASE
          CASE CUSS(31)<1,2>; PMT = 'Cash or Check '
          CASE CUSS(31)<1,1>; PMT = 'Cash or Company Check '
          CASE OTHERWISE;     PMT = 'Cash '
          END CASE
          RETURN
*-------------------------------------------------------------------------*
GET.ORD.AMT: *
          GN.CT = DCOUNT(LED(12),VM)
          FOR GN = 1 TO GN.CT
             STAT = LED(6)<1,GN>
             IF STAT # 'X' AND STAT # '$' AND STAT # 'Y' AND LED(48)<1,GN>#'' THEN
                OE.ORDER.TOTAL INCL.OID,GN,QSIGN,AMT
                ORD.AMT += AMT
             END
          NEXT GN

          RETURN
*-------------------------------------------------------------------------*
GET.PAY.AMT: *
          GN.CT = DCOUNT(LED(12),VM)
          FOR GN = 1 TO GN.CT
             STAT = LED(6)<1,GN>
             IF STAT = '$' AND LED(48)<1,GN> = 'PAYMENT' THEN
                LOCATE 'CASHBOX' IN LED(24)<1,GN> SETTING POS THEN
                   PAY.AMT += LED(26)<1,GN,POS>
                END ELSE
                   GIFT.NO = LED(46)<1,GN,20>
                   IF GIFT.NO THEN
                      GOSUB GET.GIFT.PAY
                      PAY.AMT += GIFT.PAY
                   END
                END
             END
          NEXT GN

          RETURN
*-------------------------------------------------------------------------*
GET.GIFT.PAY:*** Get gift payment amount

          GIFT.PAY = 0
          UT.OPEN.FILE 'GIFT.CERTIFICATE',GIFTCERTFILE,ERR.MSG
          IF ERR.MSG THEN RETURN

          READV GIFT.GL FROM GIFTCERTFILE,GIFT.NO,10 ELSE RETURN

          LOCATE GIFT.GL IN LED(25)<1,GN> SETTING POS THEN
             GIFT.PAY = LED(26)<1,GN,POS>
          END

          RETURN
*-------------------------------------------------------------------------*
GET.DISC.AMT: *
          GN.CT = DCOUNT(LED(12),VM)
          FOR GN = 1 TO GN.CT
             STAT = LED(6)<1,GN>
             IF STAT # 'X' AND STAT # '$' AND STAT # 'Y' AND LED(48)<1,GN>#'' THEN
                SOE.CALC.DISC INCL.OID,GN,CASH.DISC
                DISC.AMT += CASH.DISC
             END
          NEXT GN

          RETURN
*-------------------------------------------------------------------------*
CHK.CWARN: *** Check the credit limit warning message.
          * No Warnings neccessary if customer is currently COD only.
          IF NOT(OVER) AND NOT(COD) THEN
             *** Check the past due percentage
             IF PAST.DUE.PCT$+0 > 0 AND CUSS(22)<1,2,1> THEN
                PDP  = (1-OCONV(PAST.DUE.PCT$,'MR4'))
                CDP  = OCONV(PAST.DUE.PCT$,'MR2')
                CAMT = (CUSS(22)<1,1,2>+0)*PDP
                *** Pastdue amt > certain pct of past due limit
                IF PASTDUE >= CAMT THEN
                   CLIM.WRN     = 'is Within ':CDP "R2#5":'% of'
                   CLIM.WRN<-1> = 'Past Due Limit (':OCONV(CUSS(22)<1,1,2>,'MR2'):')'
                END
             END

             *** Check the credit limit percentage
             IF CLIMIT.PCT$+0 > 0 AND CLIM.WRN = '' THEN
                CDP  = OCONV(CLIMIT.PCT$,'MR2')
                PDP  = CDP/100
                CAMT = CRLIM*PDP

                *** Avail credit is less than certain percent of total clim
                IF CRAVAIL <= CAMT THEN
                   CLIM.WRN     = 'is Within ':CDP "R2#5":'% of'
                   CLIM.WRN<-1> = 'Credit Limit (':OCONV(CRLIM,'MR2'):')'
                END
             END
          END
          RETURN
!TSMITH~04/13/16~15:36
