          PH.ARGS DRPT,BRCHS,BR,SD,ED,DT.TYP,DET,SORT,SEL.CN,SEL.BIDS,SEL.WRI,SEL.PL,SEL.FRMT,FITEM,COST.TYPE,PART.NO
** Version# 45.0002[31] - 02/06/2012 - 10:55am - TSMITH - eclipse
*** V45.0002 Change - Custom Coding CUSTOM - 02/06/2012 - TSMITH - eclipse
** Copied from BP SOE.PHR.OPEN.ORDERS Version# 45 - 07/21/2008 - 03:19pm - KZHU - main

*** Phantom: SOE.PHR.OPEN.ORDERS
*-------------------------------------------------------------------------*
*** This report returns information on open sales orders.
*-------------------------------------------------------------------------*
*** Parameters:
*** All inputs to determine which data to select.
***   BRCHS      -  Branch, territory, all                            (IN)
***   BR         -  Literal entered by user for Branches              (IN)
***   SD         -  Start Date                                        (IN)
***   ED         -  End Date                                          (IN)
***   DT.TYP     -  Date Type: Ship Date, Order Date                  (IN)
***   DET        -  Unused?
***   SORT       -  Sort By: Customer, Order#, Date, Writer           (IN)
***   SEL.CN     -  Select specific Customer (optional)               (IN)
***   SEL.BIDS   -  Bids: Include, Exclude, Only                      (IN)
***   SEL.WRI    -  Select specific Writer (optional)                 (IN)
***   SEL.PL     -  Select Price Line (optional)                      (IN)
***   SEL.FRMT   -  Report Format: Internal, External                 (IN)
***   FITEM      -  Items from additional Select (alt-O)              (IN)
***   COST.TYPE  -  Cost Type: Generic, Goods Sold, None (1,2,'')     (IN)
*-------------------------------------------------------------------------*
*** Common Var:
***   DRPT - Not modified
*-------------------------------------------------------------------------*

*** Setting flags if DRPT<51> is set, detailing what the desired currency
*** display is.
          TGT      = ''
          CURR.FLG = NO
          BASE.CUR = YES
          IF DRPT<51> THEN
             READ BASEC FROM CTRLFILE,"BASE.CURRENCY" ELSE BASEC = ''
             IF DRPT<51> # BASEC THEN
                TGT      = DRPT<51>
                CURR.FLG = YES
                BASE.CUR = NO
             END
          END

          DISP.COST = ''
          LOT.LIST  = ''
          GRND.GP%  = ''
          GRND.GP$  = ''

          BRS = BR

          IF FITEM = '' THEN FILTER.FLAG = NO ELSE FILTER.FLAG = YES

          IF DT.TYP[1,1] = 'S' THEN
             SD.ATTB  = 9
             HDG.WORD = 'Shipdate'
          END ELSE
             SD.ATTB  = 4
             HDG.WORD = 'Ord.Date'
          END

          SDT = OCONV(SD,'D4/')
          EDT = OCONV(ED,'D4/')

          IF SEL.PL = '' AND PART.NO = ''THEN
             BEGIN CASE
             CASE COST.TYPE = 1
                COST.BASE = 17
             CASE COST.TYPE = 2
                COST.BASE = 16
             CASE OTHERWISE
                COST.BASE = ''
             END CASE
          END ELSE
             BEGIN CASE
             CASE COST.TYPE = 1
                COST.BASE = 27
             CASE COST.TYPE = 2
                COST.BASE = 10
             CASE OTHERWISE
                COST.BASE = ''
             END CASE
          END

*** Sets display cost
          BEGIN CASE
          CASE COST.TYPE = 1
             DISP.COST = 'Generic Cost'
          CASE COST.TYPE = 2
             DISP.COST = 'Cost of Goods Sold'
          CASE OTHERWISE
             DISP.COST = 'None'
          END CASE

*** Build our Report Heading...
          *** First line of our heading...
          HDG     = 'Open Sales Order Report for:  ':SDT:' - ':EDT

          DISP.PL = SEL.PL[1,30]
          CONVERT VM TO ',' IN DISP.PL
          IF SEL.PL THEN HDG := ' - Price Line:  ':DISP.PL

          IF DCOUNT(PART.NO,VM) > 1 THEN
             DISP.PART.NO = '*Multi*'
          END ELSE
             DISP.PART.NO = PART.NO
          END

          IF PART.NO THEN HDG := ' - Product:  ':DISP.PART.NO

          IF CURR.FLG THEN
             HDG := ' - Report Currency:  ':TGT
          END

          *** Second line of our heading...
          HDG<1,2> = 'Cost Type:  ':DISP.COST

          *** Fourth line of our heading - data columns...
          HDG<1,4>  = 'Brch.  Order #...  Writer......... Salesman....... Ordered By..... ':HDG.WORD:'  '
          HDG<1,4> := 'Customer Name...................... PO #................ '
          HDG<1,4> := 'Type Mat           Got Reason Lost....'
          HDG<1,4> := 'Sales Amt.... Tax Amt...... Fght Amt..... '
          HDG<1,4> := 'Handling Amt. Status '

          IF SEL.FRMT='Internal' AND COST.BASE # "" THEN
             HDG<1,4> := '  GP$.........  GP%........'
          END

          *** Set our report Width based on our data columns...
          WIDTH = LEN(HDG<1,4>)

          *** Tack the Page Number onto the first line of our heading...
          HDG1.LENGTH = LEN(HDG<1,1>)
          HDG<1,1>   := SPACE(WIDTH - HDG1.LENGTH - 12):'Page: ^#####'

          *** Display the selected Branches on the third line of our hdg...
          BRANCH.ALLOW = (WIDTH - 15)
          IF LEN(BRS)  > BRANCH.ALLOW THEN BRS = BRS[1,BRANCH.ALLOW]:'...'
          HDG<1,3>     = 'Branch(es): ':BRS

          WRITE 'Selecting...' ON PHSTFILE,PID$

          GOSUB SEL.IDS

          WRITE 'Spooling...' ON PHSTFILE,PID$

          IF DRPT<33> = '' THEN
             TITLE = 'Open Sales Order Report'
          END ELSE
             TITLE = DRPT<33>
          END

          PRINTER.ON WIDTH,TITLE,DOC.ID,HDG,,DRPT

          FILTER.PRINT 'S',FITEM
          GOSUB PRT.RPT

          PRINTER.OFF DOC.ID

          UT.PH.CLEANUP

          SEND.MESSAGE 'Phantom',USER.ID,TITLE:' is complete.'

          STOP
*-------------------------------------------------------------------------*
SEL.IDS:  *
          EX  = 'SELECT ORDER.QUEUE WITH @ID "S]" AND WITH &INDEX&.STATUS '
          EX := '# "Y~]"'
          BEGIN CASE
          CASE SEL.BIDS[1,1]='E'
             EX := ' AND WITH &INDEX&.STATUS # "B~]"'
          CASE SEL.BIDS[1,1]='O'
             EX := ' AND WITH &INDEX&.STATUS = "B~]"'
          END CASE
          QSIGN = -1
          EXECUTE EX CAPTURING MSG

          IDS      = ''
          SORTBYS  = ''
          LOOP
          READNEXT ID ELSE EXIT
          OID = FIELD(ID,'.',1)
          GID = FIELD(ID,'.',3)
          MATREAD LED FROM LEDFILE,OID                  ELSE GOTO SKIPID
          LOCATE GID IN LED(12)<1> SETTING GEN          ELSE GOTO SKIPID

          LOCATE LED(2)<1,GEN,1> IN BRCHS<1> SETTING X  ELSE GOTO SKIPID
          IF SEL.CN#'' THEN
             IF SEL.CN#LED(1)<1,GEN> AND SEL.CN#LED(5)<1,GEN> THEN GOTO SKIPID
          END

          IF (SEL.WRI#'') AND (SEL.WRI NE LED(73)<1,GEN>) THEN GOTO SKIPID

          IF FILTER.FLAG THEN
             FILTER.SELECT SKIP.FLAG,OID,GEN,FITEM
             IF SKIP.FLAG THEN GOTO SKIPID
          END

          IF SEL.PL OR PART.NO THEN
             ADD.IT  = NO
             LDIDS   = LED(48)<1,GEN>
             LDID.CT = DCOUNT(LDIDS,SVM)
             FOR J = 1 TO LDID.CT
                LD.GET LDIDS<1,1,J>
                IF PART.NO THEN
                   LOCATE LD(1) IN PART.NO<1> SETTING POS THEN
                      ADD.IT = YES
                      EXIT
                   END
                   *IF LD(1) = PART.NO THEN ADD.IT = YES; EXIT
                END ELSE
                   READV PRD.PL FROM PRDFILE,LD(1),9 ELSE PRD.PL = ''
                   LOCATE PRD.PL IN SEL.PL<1> SETTING POS THEN
                      ADD.IT = YES
                      EXIT
                   END
                  * IF PRD.PL=SEL.PL THEN ADD.IT = YES; EXIT
                END
             NEXT J
             IF NOT(ADD.IT) THEN GOTO SKIPID
          END

          IF SD#'' THEN
             IF LED(SD.ATTB)<1,GEN> < SD THEN GOTO SKIPID
          END

          IF ED#'' THEN
             IF LED(SD.ATTB)<1,GEN> > ED THEN GOTO SKIPID
          END

          ORN = OID:'.':GEN"R%3"
          BEGIN CASE
          CASE SORT[1,1] = "O"
             SORTBY = OID:'.':GEN
             LOCATE SORTBY IN SORTBYS<1> BY 'AL' SETTING POS ELSE NULL
             SORTBYS = INSERT(SORTBYS,1,POS;SORTBY)
             IDS     = INSERT(IDS,1,POS;OID:'.':GEN)
          CASE OTHERWISE
             READV SORTBY FROM CUSFILE,LED(5)<1,GEN>,8 ELSE SORTBY = ''
             SORTBY = SORTBY:'*':LED(5)<1,GEN>
             BEGIN CASE
             CASE SORT[1,1] = "D"
                SORTBY = LED(SD.ATTB)<1,GEN>"R%5":SORTBY:ORN
             CASE SORT[1,1] = "C"
                SORTBY = SORTBY:ORN:LED(SD.ATTB)<1,GEN>
             CASE SORT[1,1] = "W"
                SORTBY = LED(73)<1,GEN>:SORTBY:ORN
             END CASE
             LOCATE SORTBY IN SORTBYS<1> BY 'AL' SETTING POS ELSE NULL
             SORTBYS = INSERT(SORTBYS,1,POS;SORTBY)
             IDS     = INSERT(IDS,1,POS;OID:'.':GEN)
          END CASE

SKIPID:   REPEAT

          RETURN
*-------------------------------------------------------------------------*
PRT.RPT:  GSUB.TOL = 0
          GTAX.TOL = 0
          GFGHT    = 0
          GHNDL    = 0
          CSUB.TOL = 0
          CTAX.TOL = 0
          CFGHT    = 0
          CHNDL    = 0
          ORD.CT   = 0
          GP$.TOL  = 0
          LAST.SORT = '@@@'
          LOT.BILL  = NO
          PGP% = ''
          MORE.DATA = YES
          LOOP WHILE MORE.DATA
          REMOVE ID FROM IDS SETTING MORE.DATA
          OID = FIELD(ID,'.',1)
          GEN = FIELD(ID,'.',2)
          MATREAD LED FROM LEDFILE,OID             ELSE GOTO SKIPIT
***  Currency Change.  Used to inform any subroutines that may be
***  called that pay attention to desired currency.
          IF CURR.FLG THEN
             CHG.LED.CURR TGT,GEN
          END
          BT.CN = LED(2)<1,GEN>
          ST.CN = LED(5)<1,GEN>
          IF SORT[1,1]='C' AND ST.CN#LAST.SORT          THEN GOSUB CSUB.PRT
          IF SORT[1,1]='W' AND LED(73)<1,GEN>#LAST.SORT THEN GOSUB CSUB.PRT
          GET.CUS LED(2)<1,GEN,1>,BT.CN,ST.CN,QSIGN
          NAME = CUSS(1)
          READV W.NAME FROM INIFILE,LED(73)<1,GEN>,3 ELSE
             W.NAME = LED(73)<1,GEN>
          END
*** See if this is an open lot billing item; if so, go calc amounts
          LDIDS   = RAISE(LED(48)<1,GEN>)
          LDID.CT = DCOUNT(LDIDS,VM)
          IF LDID.CT # 1                            THEN GOTO PAST.LOT
          LDID = LDIDS
          LD.GET LDID
          PN = LD(1)
          QS.TST = SUM(LD(5)<1,GEN>)+SUM(LD(6)<1,GEN>)
          IF QS.TST = 0                             THEN GOTO PAST.LOT
          IF NOT(NUM(PN))                           THEN GOTO PAST.LOT
          READV STAT FROM PRDFILE,PN,3              ELSE GOTO PAST.LOT
          PRD.STAT = STAT<1,1>
          IF PRD.STAT = 9 THEN
             LOT.BILL = YES
             LOT.ID = OID:'~':LDID "R%4"
             LOCATE LOT.ID IN LOT.LIST<1> BY 'AL' SETTING LOT.POS THEN
                SUB.TOL  = 0; PSUB.TOL = '*LOT*'
                GP$      = 0; PGP$     = '*LOT*'
                PGP%     = '*LOT*'
                TAX.TOL  = 0; PTAX.TOL = '*LOT*'
                FGHT     = 0: PFGHT    = '*LOT*'
                HNDL     = 0; PHNDL    = '*LOT*'
             END ELSE
                LOT.LIST = INSERT(LOT.LIST,1,LOT.POS;LOT.ID)
                GOSUB DO.LOT.TOTAL
             END
          END

PAST.LOT: *
          BEGIN CASE
          CASE SEL.PL OR PART.NO
             GOSUB DO.PL.TOTAL
          CASE LOT.BILL
             NULL
          CASE OTHERWISE
             OE.ORDER.TOTAL OID:VM:BASE.CUR,GEN,QSIGN,TOTAL,SUB.TOL,FGHT,HNDL,TAX.TOL,FET.TOL
             TAX.TOL += FET.TOL
          END CASE

          SALESMAN=TRANS('INITIALS',LED(72)<1,GEN>,3,'X')
          WRITER=TRANS('INITIALS',LED(73)<1,GEN>,3,'X')
          ORDERED=LED(68)<1,GEN>
          ORDER_TYPE=TRANS('LEDGER.QUOTE',OID,1,'X')
          MATERIAL_TYPE=TRANS('LEDGER.QUOTE',OID,2,'X')
          GOT=TRANS('LEDGER.QUOTE',OID,3,'X')
          WHY_WE_LOST=TRANS('LEDGER.QUOTE',OID,4,'X')



          PRINT LED(2)<1,GEN,1>                        "R#4" :'   ':
          PRINT OID                                    "L#10":'  ':
          *** Writer...
          PRINT WRITER                         "L#15":' ':
          PRINT SALESMAN                       "L#15":' ':
          PRINT ORDERED                        "L#15":' ':

          *** Ship Date...
          PRINT OCONV(LED(SD.ATTB)<1,GEN>,'D2/')       "L#8" :'  ':
          *** Customer Name...
          PRINT NAME                                   "L#35":' ':
          *** PO#...
          PRINT LED(13)<1,GEN>                         "L#20":' ':
          PRINT ORDER_TYPE                             "L#4":' ':
          PRINT MATERIAL_TYPE                          "L#13":' ':
          PRINT GOT                                    "L#3":' ':
          PRINT WHY_WE_LOST                            "L#13":' ':




          BEGIN CASE
          CASE SEL.PL OR PART.NO
             PRINT SUB.TOL                             "R26#14":
             PRINT SPACE(43):
          CASE LOT.BILL
             PRINT PSUB.TOL                            "R26#14":
             PRINT PTAX.TOL                            "R#14":
             PRINT PFGHT                               "R#14":
             PRINT PHNDL                               "R#14":' ':
          CASE OTHERWISE
             PRINT SUB.TOL                             "R26#14":
             PRINT TAX.TOL                             "R26#14":
             PRINT FGHT                                "R26#14":
             PRINT HNDL                                "R26#14":' ':
          END CASE






          IF SEL.FRMT = 'Internal' AND COST.BASE # "" THEN
             IF COST.BASE # '' THEN
                BEGIN CASE
                CASE SEL.PL OR PART.NO
                CASE OTHERWISE
                   TOL.COGS = -LED(COST.BASE)<1,GEN>
                END CASE
             END ELSE
                TOL.COGS = ''
             END
             IF NOT(LOT.BILL) THEN
                GP$ = SUB.TOL - TOL.COGS
             END
             IF SUB.TOL+0 # 0 THEN DIV = SUB.TOL ELSE DIV = 1
             IF GP$+0 # 0 THEN GP% = 100*GP$/DIV ELSE GP% = 0
             IF LOT.BILL AND PGP% # '*LOT*' THEN PGP% = GP%

             *** Order Status...
             PRINT LED(6)<1,GEN>                       "L#6":

             IF LOT.BILL THEN
                PRINT PGP$                             "R26#15":
                PRINT PGP%                             "R0#13"
             END ELSE
                PRINT GP$                              "R26#15":
                PRINT GP%                              "R0#13"
             END

             GP$.TOL  += GP$
             GRND.GP$ += GP$
             GRND.GP% += GP%
          END ELSE
             PRINT LED(6)<1,GEN>                       "L#1"
          END

          GSUB.TOL += SUB.TOL
          GTAX.TOL += TAX.TOL
          GFGHT    += FGHT
          GHNDL    += HNDL
          CSUB.TOL += SUB.TOL
          CTAX.TOL += TAX.TOL
          CFGHT    += FGHT
          CHNDL    += HNDL
          ORD.CT   += 1
          LOT.BILL = NO
          PGP% = ''

SKIPIT:   REPEAT


          IF (SORT[1,1] = 'W') OR (SORT[1,1] = 'C') THEN GOSUB CSUB.PRT


*** This section of code prints the Grand Total

          IF COST.BASE # "" THEN
             PRINT SPACE(95):STR('=',14):' ':STR('=',13):' ':STR('=',13):' ':STR('=',13):SPACE(10):STR('=',12):"  ":STR('=',11)
          END ELSE
             PRINT SPACE(95):STR('=',14):' ':STR('=',13):' ':STR('=',13):' ':STR('=',13)
          END

          PRINT SPACE(77):'Grand Totals ---- ':

          PRINT GSUB.TOL                               "R26#14":

          IF NOT(SEL.PL) AND NOT(PART.NO) THEN
             PRINT GTAX.TOL                            "R26#14":
             PRINT GFGHT                               "R26#14":
             PRINT GHNDL                               "R26#14":
          END ELSE
             PRINT SPACE(42):
          END
          IF SEL.FRMT = 'Internal' AND COST.BASE # "" THEN
             PRINT SPACE(8):
             PRINT GRND.GP$                         "R26#14":

             IF GSUB.TOL+0 = 0 THEN DIVR = 1 ELSE DIVR = GSUB.TOL

             GRND.GP% = 100*(GRND.GP$/DIVR)

             PRINT GRND.GP%                         "R0#13"
          END ELSE
             PRINT
          END
          RETURN

*-------------------------------------------------------------------------*
DO.PL.TOTAL: *** Calculate the order total based on the price lines select
          TOTAL    = 0
          SUB.TOL  = 0
          FGHT     = ''
          HNDL     = ''
          TAX.TOL  = ''
          CB.EXT   = ''
          TOL.COGS = ''
          LDIDS    = RAISE(LED(48)<1,GEN>)
          LDID.CT  = DCOUNT(LDIDS,VM)

          FOR J = 1 TO LDID.CT
             LDID = LDIDS<1,J>
             LD.GET LDID
             READV PRD.PL FROM PRDFILE,LD(1),9 ELSE PRD.PL = ''

             BEGIN CASE
             CASE SEL.PL
                LOCATE PRD.PL IN SEL.PL<1> SETTING PL.POS ELSE CONTINUE
             CASE PART.NO
                LOCATE LD(1) IN PART.NO<1> SETTING PN.POS ELSE CONTINUE
             END CASE

             * lot item totals is already calculated at this point
             * in DO.LOT.TOTAL
             IF LOT.BILL THEN
                IF PSUB.TOL # "*LOT*" THEN SUB.TOL = PSUB.TOL
             END ELSE
                LN.QS   = (SUM(LD(5)<1,GEN>)+SUM(LD(6)<1,GEN>))*QSIGN
                LN.EXT  = ICONV(OCONV(LN.QS*LD(8)<1,GEN>,'MR9'),'MR2')
                IF COST.BASE # "" THEN
                   CB.EXT  = ICONV(OCONV(LN.QS*LD(COST.BASE)<1,GEN>,'MR9'),'MR2')
                END ELSE
                   CB.EXT = ""
                END
                TOL.COGS +=  CB.EXT
                SUB.TOL += LN.EXT
             END
          NEXT J

          TOTAL = SUB.TOL

          RETURN
*-------------------------------------------------------------------------*
DO.LOT.TOTAL: *
          SHP.DT = LED(9)<1,GEN>
          GN.CNT = DCOUNT(LED(12),VM)
          IF CURR.FLG THEN
             CONV.CURR.LD TGT,GEN
          END
          LOT.PRC = LD(67)<1,1>+0
          LOT.COG = LD(67)<1,2>+0
          FOR GN = 1 TO GN.CNT
             QS.TST = SUM(LD(5)<1,GN>)+SUM(LD(6)<1,GN>)
             IF LED(8)<1,GN> AND LED(6)<1,GN> # 'X' AND QS.TST+0#0 THEN
                INVN.AMT = LD(8)<1,GN>
                COGS.AMT = LD(10)<1,GN>
                LOT.PRC -= INVN.AMT
                LOT.COG -= COGS.AMT
             END
          NEXT GN
          LOT.PRC = ICONV(OCONV(LOT.PRC,'MR9'),'MR2')
          LOT.COG = ICONV(OCONV(LOT.COG,'MR9'),'MR2')
          SUB.TOL  = LOT.PRC
          GP$      = LOT.PRC - LOT.COG
          PSUB.TOL = LOT.PRC
          PGP$     = LOT.PRC - LOT.COG
          IF LOT.PRC+0=0 THEN PGP% = 0 ELSE
             PGP% = 100 * (LOT.PRC - LOT.COG)/LOT.PRC
          END
          TAX.TOL = 0; PTAX.TOL = '*LOT*'
          FGHT    = 0; PFGHT    = '*LOT*'
          HNDL    = 0; PHNDL    = '*LOT*'

          RETURN
*-------------------------------------------------------------------------*
CSUB.PRT: IF LAST.SORT#'@@@' THEN
             IF SORT[1,1] = 'C' THEN
                T.NAME = NAME
             END ELSE
                T.NAME = W.NAME
             END
             IF ORD.CT>1 THEN
                PRINT SPACE(96):'-------------':SPACE(52):'------------':SPACE(2):'-----------'
                NAME.LEN = LEN(T.NAME)
                IF NAME.LEN < 31 THEN
                   LEN.REMANDER = 31 - NAME.LEN
                END ELSE
                   LEN.REMANDER = 0
                   T.NAME = LEFT(T.NAME,30)
                END
                PRINT SPACE(40):'Subtotal for ':T.NAME:SPACE(LEN.REMANDER):' -------- ':
                PRINT CSUB.TOL                               "R26#15":
                IF NOT(SEL.PL) AND NOT(PART.NO) THEN
                   PRINT CTAX.TOL                            "R26#14":
                   PRINT CFGHT                               "R26#14":
                   IF SEL.FRMT = 'Internal' THEN
                      PRINT CHNDL                            "R26#14":
                      PRINT SPACE(8):
                      PRINT GP$.TOL                          "R26#14":
                      IF CSUB.TOL+0 = 0 THEN DIV = 1 ELSE DIV = CSUB.TOL
                      GP%.TOL = 100*(GP$.TOL/DIV)
                      PRINT GP%.TOL                          "R0#13"
                   END ELSE
                     PRINT CHNDL                            "R26#14"
                   END
                END ELSE
                   PRINT SPACE(50):GP$.TOL                  "R26#14":
                   IF CSUB.TOL+0 = 0 THEN DIV = 1 ELSE DIV = CSUB.TOL
                   GP%.TOL = 100*(GP$.TOL/DIV)
                   PRINT GP%.TOL                            "R0#13"
                END
             END
             PRINT
          END
          CSUB.TOL = 0
          CTAX.TOL = 0
          CFGHT    = 0
          CHNDL    = 0
          ORD.CT   = 0
          GP$.TOL  = 0
          GP%.TOL  = 0
          IF SORT[1,1] = "C" THEN LAST.SORT = ST.CN ELSE LAST.SORT = LED(73)<1,GEN>
          RETURN
!TSMITH~02/06/12~10:55
