          SUBROUTINE (STK.LEVELS)
** Version# 13.0002[1] - 08/03/2013 - 09:27pm - TSMITH - eclipse
** Copied from BP DICT.CALC.AVAIL.BR Version# 13.0001[1] - 08/02/2013 - 04:22pm - TSMITH - eclipse
** Copied from BP DICT.CALC.AVAIL Version# 13 - 02/15/2012 - 01:31pm - JWITTENB - main
*** Version# 10 - 02/26/2002 - 01:11pm - SHELLYF - develop
*** Subroutine - DICT.CALC.AVAIL
*-------------------------------------------------------------------------*
*** This dictionary subroutine calculates the available quantity of a given
*** product for selected branches.
*-------------------------------------------------------------------------*
*** STK.LEVEL -    Product availability                        (Out)
*-------------------------------------------------------------------------*
*** COMMON - None
*-------------------------------------------------------------------------*

          PN = @ID
          BRS = SEL.BR
          AS.OF.DATE  = ED$

          GET.MIN.RECV.DATE MIN.RECV.DT
          STK.LEVELS  = ''
          IF AS.OF.DATE='' THEN AS.OF.DATE = DATE()

          *** For each brach, get avail qty
          BRN = DCOUNT(SEL.BR,VM)
          FOR BRX = 1 TO BRN
             STK.BR = BRS<1,BRX>

             *** Get plenty date
             LOOKAHEAD.DATE = DATE.NEXT.REC(PN,STK.BR)
             IF AS.OF.DATE >= LOOKAHEAD.DATE THEN
                STK.LEVEL = 9999999
                RETURN
             END

             *** Check kit components
             READV COMP.PNS FROM PRDFILE,PN,53 ELSE COMP.PNS = ''
             IF COMP.PNS THEN
                GOSUB GET.KIT
             END ELSE
                GOSUB GET.ITEM
             END

             STK.LEVELS += STK.LEVEL
          NEXT BRX

          RETURN
*-------------------------------------------------------------------------*
GET.KIT:  *** Kit Components
          KIT.LEVEL = 999999999
          READV COMP.QTYS FROM PRDFILE,PN,52 ELSE RETURN
          CP.CT     = DCOUNT(COMP.PNS,VM)
          FOR CP = 1 TO CP.CT
             PRDD.BR.GET.REC STK.BR,COMP.PNS<1,CP>,PRDD.REC
             IF PRDD.REC THEN
                GOSUB CALC.LEVEL
                STK.LEVEL = INT(STK.LEVEL/COMP.QTYS<1,CP>)
             END ELSE
                STK.LEVEL = 0
             END
             IF KIT.LEVEL > STK.LEVEL THEN KIT.LEVEL = STK.LEVEL
          NEXT CP

          STK.LEVEL = KIT.LEVEL

          RETURN
*-------------------------------------------------------------------------*
GET.ITEM: *** Get the product
          AVAP = AS.OF.DATE:'.':LOOKAHEAD.DATE:'.'
          PRDD.BR.GET.REC STK.BR,PN,PRDD.REC
          IF PRDD.REC<14> = AVAP THEN
             STK.LEVEL = PRDD.REC<15>
          END ELSE
             GOSUB CALC.LEVEL
          END

          RETURN
*-------------------------------------------------------------------------*
CALC.LEVEL: *** Calculate avail qty
          GET.ONHAND PRDD.REC<1>,PRDD.REC<8>,STK.LEVEL
          IDS       = PRDD.REC<2>
          QTYS      = PRDD.REC<3>
          MORE.DATA = YES
          LOOP WHILE MORE.DATA
          REMOVE ID  FROM IDS  SETTING MORE.DATA
          REMOVE QTY FROM QTYS SETTING X
          PTYPE = FIELD(ID,'~',6)
          MODE     = FIELD(ID,'~',3)[1,1]
          ORD.STAT = FIELD(ID,'~',9)
          SHP.DT = FIELD(ID,'~',2)
          IF INDEX(STK.TYPES.AVL$,PTYPE[1,1],1) THEN
             *** Check status
             STAT.OK  = ORD.STAT#'W' AND ORD.STAT#'S' AND ORD.STAT#'D'

             *** Only want to exclude the Plenty Date if the Product is
             *** not a Delete Status Product
             READV PRD.STAT FROM PRDFILE,PN,3 ELSE PRD.STAT = ""
             IF PRD.STAT # "4" THEN
                IF EXC.FUT.SOE$ THEN STAT.OK = NO
             END

             *** Set Ship date
             BEGIN CASE
             CASE MODE = 'S' AND STAT.OK
                SHP.DT = DATE()
             CASE MODE = 'S' AND NOT(STAT.OK) AND ORD.STAT # 'D'
                SHP.DT = SHP.DT
             END CASE

             IF MIN.RECV.DT <= DATE() THEN MIN.RECV.DT = DATE()+1
             IF QTY>0 AND SHP.DT < MIN.RECV.DT THEN SHP.DT = MIN.RECV.DT
             IF QTY>0 AND SHP.DT > AS.OF.DATE THEN QTY = 0
*** If this is before the next recv date then add it in.
             IF SHP.DT < LOOKAHEAD.DATE THEN
                   STK.LEVEL += QTY
             END
          END
          REPEAT

          RETURN
!TSMITH~08/03/13~21:27
