          PH.ARGS DRPT,SD,ED,BATCH,BRCHS,BR,MIN.GP,MAX.GP,SORT.OPT,ORBY,SELBY,MISC.CHG,PSTAT,SBR,FITEM,ADDL.DATA
** Version# 68.0002[40] - 10/14/2015 - 09:31am - TSMITH - eclipse
*** V68.0002 Change - Custom Coding . - 10/14/2015 - TSMITH - eclipse
** Copied from CBP PRC.PHR.GP.PREVIEW Version# 68.0001 - 06/11/2015 - 03:46pm - TSMITH - eclipse
*** V68.0001 Change - Custom Coding . - 06/11/2015 - TSMITH - eclipse

*-------------------------------------------------------------------------*
*** Program PRC.DVR.GP.PREVIEW
*-------------------------------------------------------------------------*
*** DETAILED INVOICE GP% REPORT OVERVIEW
***  The Detailed Invoice GP% Report lets you create a list of
***  invoices with unusually high or low gross profits, so you
***  can review them and check for errors before actually
***  printing and sending the invoices. Rather than previewing
***  every single invoice, this report allows you to reduce the
***  number of invoices to be previewed to only those most likely
***  to be problems. For example, entries of ten or fifteen
***  points above and below the company average gross profit will
***  reduce the number of invoices to examine significantly. Most
***  errors, in fact, are evidenced by gross profits that are
***  negative or above fifty percent.
*-------------------------------------------------------------------------*
***  PARAMETERS:
***       DRPT     -  COMMON variable (not modified)         (IN)
***       SD       -  Start Date                             (IN)
***       ED       -  End Date                               (IN)
***       BATCH    -  Batch ID of batch of invoices          (IN)
***       BRCHS    -  Branches, Territory, or ALL            (IN)
***       MIN.GP   -  Minimun Gross Profit Percentage        (IN)
***       MAX.GP   -  Maximum Gross Profit Percentage        (IN)
***       SORT.OPT -  Sort by (Invoice#/Time/Customer/       (IN)
***                   Inside Salesperson/Outside
***                   Salesperson/Writer)
***       ORBY     -  Y/N option to display the ordered by   (IN)
***                   from the header of the sales order
***       SELBY    -  Cost type (Generic Cost/Cost of        (IN)
***                   Goods Sold/None)
***       MISC.CHG -  Include invoices for                   (IN)
***                   Miscellaneous Charges (Y/N)
***       PSTAT    -  Select Print Status                    (IN)
***                   P - Detailed Invoice Preview Report
***                   Q - Invoice Preview Queue
***                   B - Batch Print
***                   M - Placed on Manifest
***                   H - Held for Printing
***       SBR      -  Shipping or Pricing Branch             (IN)
***       FITEM    -  File Input Item                        (IN)
***       ADDL.DATA-                                                   [IN]
***                  SET.STAT - Automatically set print status in
***                             this program (Y/N)
***                  CONSIGN  - Include/Exclude Only Consignment Transfers
***                  XFER.OPT - Include/Exclude Only TransFers
***                  IGN.CPYS - Ignore the number of copies when
***                             overriding.
*-------------------------------------------------------------------------*
          * Automatically set Print Status (Y/N)
          SET.STAT = FIELD(ADDL.DATA,'~',1)

          * Include/Exclude/Only Consignment Transfers
          CONSIGN  = FIELD(ADDL.DATA,'~',2)

          * Include/Exclude/Only Transfers
          XFER.OPT  = FIELD(ADDL.DATA,'~',3)

          * Ignore the print copies setting (Y/N)
          IGN.CPYS  = FIELD(ADDL.DATA,'~',4)

          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
          IMG.COUNTER   = 0

          GRAND.SBTOTAL = 0
          GRAND.COST    = 0
          GRAND.ADJ     = 0
          GRAND.GP      = 0
          GRAND.ADJ.GP  = 0

          UT.OPEN.COMMON.FILE 'IMG.XREF',HNDL.NUMBER
          IF HNDL.NUMBER THEN
             IMGFILE = FILES(HNDL.NUMBER)
             IMAGING = YES
          END ELSE
             IMAGING = NO
          END


          QSIGN = -1

          BRS = BR

          IF SBR[1,1] = 'P' THEN SBR.PRICE = 1 ELSE SBR.PRICE = 0
          IF OCONV(BATCH,'MCU') = 'ALL' THEN BATCH = ''
          IF FITEM = '' THEN FILTER.FLAG = NO ELSE FILTER.FLAG = YES

          PSTATS = PSTAT
          IF PSTAT = 'N' THEN PSTAT = ''
          CONVERT VM TO ',' IN PSTATS

          SDT = OCONV(SD,'D4/')
          EDT = OCONV(ED,'D4/')
          COST.BASE = ''
          BEGIN CASE
          CASE SELBY[1,1] = "G"
             COST.BASE<1,1> = 27
             COST.HDR  = 'Cost....'
             COST.DESC = 'Cost'
          CASE SELBY[1,1] = "C"
             COST.BASE<1,1> = 10
             COST.HDR  = 'CogsCost'
             COST.DESC = 'COGS'
          CASE SELBY[1,1] = "B"
             COST.BASE<1,1> = 27
             COST.BASE<1,2> = 10
             COST.HDR = 'Cost....   CogsCost'
             COST.DESC = 'Cost and COGS'
          CASE OTHERWISE
            COST.BASE<1,1>  = 0
            COST.HDR   = '        '
            COST.DESC  = 'None'
          END CASE
          BRK.ON = (SORT.OPT[1,3]='Ins' OR SORT.OPT[1,1]='O' OR SORT.OPT[1,1]='W')

          IF DRPT<33> = '' THEN
             TITLE = 'Detailed Inv GP% for ':SDT:' - ':EDT
          END ELSE
             TITLE = DRPT<33>:'  ':SDT:' - ':EDT
          END
          WRITE 'Selecting...' ON PHSTFILE,PID$
          GOSUB SEL.IDS
          IF IDS = '' THEN
             PRT.STAT = ' ** No Items Found **'
             GOTO FINISH
             END
          WRITE 'Spooling...' ON PHSTFILE,PID$
*-------------------------------------------------------------------------*
*         PH.COST.DESC SELBY,COST.DESC
          HDG = 'Detailed Invoice GP% for ':SDT:' to ':EDT:' - Batch : ':BATCH:' - Min GP% : ':MIN.GP:'% - Max GP% : ':MAX.GP:'%'
          HDG = HDG"L#100":'       Page^####'
          HDG<1,2> = 'Select By : ':COST.DESC:'  -  Sort By : ':SORT.OPT
          IF PSTATS THEN HDG<1,2> := '  -  Print Status : ':PSTATS
          HDG<1,2> := '  -  Set Print : ':YN[SET.STAT+1,1]
          HDG<1,2> := '  -  ':SBR
          HDG<1,3> = 'Consignment Transfers : ':CONSIGN:SPACE(4)
          HDG<1,3> := 'Transfers : ':XFER.OPT:'  -  Branches : '
          HDG3.LEN = LEN(HDG<1,3>)

          IF SELBY[1,1] = 'B' THEN
             WDTH = 143
          END ELSE
             WDTH = 131
          END

          BRANCH.ALLOW = (WDTH - HDG3.LEN)
          IF LEN(BRS) > BRANCH.ALLOW THEN BRS = BRS[1,BRANCH.ALLOW]:'...'
          HDG<1,3> := BRS

          IF CURR.FLG THEN
             HDG<1,4> = 'Report Currency:  ':TGT
             HDG<1,5> = STR('-',WDTH)
          END ELSE
             HDG<1,4> = STR('-',WDTH)
          END

          PRINTER.ON WDTH,TITLE,DOC.ID,HDG,RPT.DFLT=DRPT
          FILTER.PRINT 'S',FITEM

          LAST.BRK = '@@'
          PCT      = 0
          PRINT.CT = 0
          IDN = DCOUNT(IDS,AM)

          FOR IDX=1 TO IDN
          IF BRK.ON AND LAST.BRK#FIELD(SORTBYS<IDX>,'!',1) THEN
             IF LAST.BRK#'@@' AND PCT THEN PRINT CHAR(12); PCT = 0
             LAST.BRK = FIELD(SORTBYS<IDX>,'!',1)
          END
          GOSUB ONE.ID
          NEXT IDX

          *** Begin Test
             IF IMAGING THEN
             IMAGE.STRING  = 'Number of Invoices printed : ':PRINT.CT
             IMAGE.STRING := '   Number of Invoices with Images : '
             IMAGE.STRING := IMG.COUNTER

             PRINT IMAGE.STRING
          END
             PRINT

          GRAND.ADJ = GRAND.ADJ/100
          IF GRAND.SBTOTAL # 0 THEN
             GRAND.GP     = 100*(GRAND.SBTOTAL - GRAND.COST)/GRAND.SBTOTAL
             GRAND.ADJ.GP = 100*(GRAND.SBTOTAL - GRAND.ADJ)/GRAND.SBTOTAL
          END

          PRINT 'Total Sales : '       :GRAND.SBTOTAL:SPACE(5):
          PRINT 'Total Cost : '        :GRAND.COST   :SPACE(5):
          PRINT 'Total GP% : '         :GRAND.GP     "L2#9":' ':
          PRINT 'Total Adjusted GP% : ':GRAND.ADJ.GP "L2#9"


          *PRINT 'Number of Invoices Reviewed : ':IDN

          *** End Test


          PRINTER.OFF DOC.ID

          PRT.STAT = ' is complete.'
FINISH:   UT.PH.CLEANUP
          SEND.MESSAGE 'Phantom',USER.ID,TITLE:PRT.STAT

          STOP
*-------------------------------------------------------------------------*
ONE.ID:   *** Place one order on the report
          GP$    = 0
          HEADED = NO          ;* if headed then order was placed on report
          ID     = IDS<IDX>
          OID    = FIELD(ID,'.',1)
          GEN    = FIELD(ID,'.',2)+0

          SUPP.SORT = TRANS('LEDGER.GPS',OID,2,'FALSE')
          IF SUPP.SORT = "1" THEN
          GOTO ONE.ID.ORIG
          END

          MATREAD LED FROM LEDFILE,OID ELSE MAT LED=''







          * If Job Management Order set up default product.
          IS.JOB     = (LED(83)<1,1>[1,7] = "@JOBNO=")
          IF IS.JOB THEN
             LOCATE LED(33)<1,GEN> IN LED(12)<1> SETTING DGEN THEN
                ST.VEN = LED(5)<1,DGEN>
             END ELSE
                ST.VEN = ''
             END
             DFLT.JM.PN = JOBMGMT.GET.PN(ST.VEN)
          END

          * place all products that have an abnormal GP on the report.  Do
          * not place consignment transfers on the report if the print
          * status is going to be updated
          IF NOT(SET.STAT AND LED(110)<1,1> = 'S') THEN
             LDIDS  = RAISE(LED(48)<1,GEN>)
             LDN    = DCOUNT(LDIDS,VM)
             *                   LDIDS = RAISE(LED(48)<1,GEN>)
 *         LDN   = DCOUNT(LDIDS,VM)
          SRT.LOCS = ''
          SRT.LDIDS= ''
          PRT.LDIDS = LED(48)<1,GEN>
             CONVERT SVM TO AM IN PRT.LDIDS

          LD.CT = DCOUNT(PRT.LDIDS,AM)
          FOR J = 1 TO LD.CT
           LDID  = PRT.LDIDS<J>
                LD.GET LDID
                PN = LD(1)
                IF PN # '' AND (NUM(PN) OR PN = 'C' OR PN[1,2] = "L#") THEN
                   IF PN = 'C' THEN LOC = '*' ELSE
                      LOC = LD(7)<1,GEN,1>
                      LOC = FIELD(LOC,'~',2)
                      IF LOC = '' THEN
                         PRD.LOCATION.GET PRI.LOC,PN,SHIP.BR
                         LOC = PRI.LOC
                      END
                   END
                   IF LOC = '' THEN LOC = 'ZZZ'
                  LOC := J "R%4"
                   LOCATE LOC IN SRT.LOCS BY 'AL' SETTING POS ELSE NULL
                   SRT.LOCS  = INSERT(SRT.LOCS,POS;LOC)
                   SRT.LDIDS = INSERT(SRT.LDIDS,POS;LDID)
                END
             NEXT J
             PRT.LDIDS = SRT.LDIDS

******************
          LDID.CT = DCOUNT(PRT.LDIDS,AM)
          FOR LD.NO = 1 TO LDID.CT
             LDID   = PRT.LDIDS<LD.NO>
             GOSUB ONE.PN
          NEXT LD.NO
          END

          * If order placed on the report then print the footer
          IF HEADED THEN
             GOSUB FOOTER
          END

          * If the customer wants to Auto Set Print Status then update the
          * print status.
          IF SET.STAT THEN
             * Get values from Bill-to and Ship-to customer records
             READV BT.ACTION FROM CUSFILE,LED(1)<1,GEN>,50 ELSE BT.ACTION=0
             READV ST.ACTION FROM CUSFILE,LED(5)<1,GEN>,50 ELSE ST.ACTION=0
             * Bill-to Values
             BT.PRT = BT.ACTION<1,1>
             BT.FAX = BT.ACTION<1,2>
             * Ship-to Values
             ST.PRT = ST.ACTION<1,1>
             ST.FAX = ST.ACTION<1,2>

             * Get defaults from control record
             READ DFLTS FROM CTRLFILE,'BATCH.DFLT.SETTINGS' ELSE DFLTS = ''
             DFLT.IPRT = DFLTS<1>      ;* # of invoice copies to print
             DFLT.IFAX = DFLTS<2>      ;* # of invoice copies to fax

             * PRINT DATA
             * Figure out between the ship-to customer,
             * the bill-to customer and control file a
             * valid action to generate a invoice.
             BEGIN CASE
             CASE ST.PRT # '' AND NUM(ST.PRT)
                * Use Ship-to level setting
                DUP.CT = ST.PRT
             CASE BT.PRT # '' AND NUM(BT.PRT)
                * Use Bill-to level setting
                DUP.CT = BT.PRT
             CASE DFLT.IPRT # '' AND NUM(DFLT.IPRT)
                * Control record
                DUP.CT = DFLT.IPRT
             CASE OTHERWISE
                DUP.CT = 0
             END CASE

             * FAX DATA
             * Figure out between the ship-to customer,
             * the bill-to customer and control file a
             * valid action to generate a invoice.
             BEGIN CASE
             CASE ST.FAX # '' AND NUM(ST.FAX)
                * Use Ship-to level setting
                FAX.CT = ST.FAX
             CASE BT.FAX # '' AND NUM(BT.FAX)
                * Use Bill-to level setting
                FAX.CT = BT.FAX
             CASE DFLT.IFAX # '' AND NUM(DFLT.IFAX)
                * Control record
                FAX.CT = DFLT.IFAX
             CASE OTHERWISE
                FAX.CT = 0
             END CASE

             * Determine if print status should be updated to 'Hold'
             PRT.ST.CHG = NO
             IF IGN.CPYS OR (DUP.CT >= 0) OR (FAX.CT >= 0) THEN
                * If GP problem put on hold otherwise set to batch print
                IF HEADED THEN
                   PRT.STAT = 'B'
                   PRT.ST.CHG = YES
                END ELSE
                   PRT.STAT = 'B'
                   PRT.ST.CHG = YES
                   CREDIT.CARD.CHK.PRT.ST PRT.STAT,OID,GEN
                END
             END

             IF PRT.ST.CHG THEN
                OE.UPD.PRINT.STAT OID,GEN,PRT.STAT
                OE.ADD.COMMENT OID,GEN,'GP Preview changed Prt Status to : ':PRT.STAT
             END
          END

          RETURN
*-------------------------------------------------------------------------*
FOOTER:   *
          INFLAGS = AM:AM:YES
          OE.ORDER.TOTAL OID:VM:BASE.CUR,GEN,QSIGN,TOTAL,SUB.TOL,FGHT,HNDL,TAX.TOL,FET.TOL,INFLAGS
          TAX.TOL += FET.TOL
          IF CURR.FLG THEN
             CONV.CURR.LED TGT,GEN
          END

          IN.FGHT     = LED(36)<1,GEN,3>*QSIGN
          OUT.FGHT    = LED(36)<1,GEN,4>*QSIGN
          IN.HNDL     = LED(36)<1,GEN,7>*QSIGN
          OUT.HNDL    = LED(36)<1,GEN,8>*QSIGN
          * We want to get the absolute value of the expensed freight
          * because we have seen customers put a negative value in the
          * freight totals screen. For example see DAF861.
          * Expensed freight should always increase the cost.
          EXP.INFRT   = ABS(LED(36)<1,GEN,5>)
          EXP.OUTFRT  = ABS(LED(36)<1,GEN,6>)
          EXP.INHNDL  = ABS(LED(36)<1,GEN,9>)
          EXP.OUTHNDL = ABS(LED(36)<1,GEN,10>)
          EXP.TOTAL   = EXP.INFRT + EXP.OUTFRT + EXP.INHNDL + EXP.OUTHNDL
          GRAND.SBTOTAL += OCONV(SUB.TOL,'MR2')

          * COGS and COMM below incl expensed freight and handling
          COGS.AMT    = -LED(16)<1,GEN>
          COMM.AMT    = -LED(17)<1,GEN>
          BEGIN CASE
          CASE SELBY[1,1] = "C"
             * Remove expensed freight/handling from the cost
             TOL.COST = COGS.AMT - EXP.TOTAL
          CASE SELBY[1,1] = "G"
             * Remove expensed freight/handling from the cost
             TOL.COST = COMM.AMT - EXP.TOTAL
          CASE SELBY[1,1] = "B"
             * Remove expensed freight/handling from the cost
             TOL.COST = COMM.AMT - EXP.TOTAL
          CASE OTHERWISE
             * No costs selected, so zero gross profit calculation
             TOL.COST = SUB.TOL
          END CASE
          GPD1 = SUB.TOL - TOL.COST

          IF SELBY[1,1] = 'B' THEN TOTSP = 83 ELSE TOTSP = 72
          PRINT SPACE(TOTSP+19):"--------------------":
          IF SELBY[1,1] = "B" THEN
             PRINT SPACE(6):'----------'
          END ELSE
             PRINT
          END

          PRINT SPACE(TOTSP):"Invoice Totals -- ":OCONV(SUB.TOL,'MR2') "R2#10":
          PRINT OCONV(GPD1,'MR2')               "R2#10":
          IF SELBY[1,1] = "B" THEN
             GPD2 = SUB.TOL - (COGS.AMT - EXP.TOTAL)
             PRINT SPACE(6):OCONV(GPD2,'MR2')             "R2#10"
          END ELSE
             PRINT
          END
          PRINT
          INV.AMT  = SUB.TOL + FGHT + HNDL + TAX.TOL
          PRINT 'Subt :':OCONV(SUB.TOL,'MR2')             "R2#10":'  ':
          BEGIN CASE
          CASE SELBY[1,1] = 'G'
             PRT.LBL = 'Cost '
          CASE SELBY[1,1] = 'C'
             PRT.LBL = 'COGS '
          CASE SELBY[1,1] = 'B'
             PRT.LBL = 'Cost '
          END CASE
          PRINT PRT.LBL:
          PRINT 'GP$ : ':OCONV(GPD1,'MR2')                "R2#10":'  ':
          PRINT 'Tax :':OCONV(TAX.TOL,'MR2')              "R2#9":'  ':
          PRINT 'Frt :':OCONV(FGHT,'MR2')                 "R2#10":'  ':
          PRINT 'Hndlg : ':OCONV(HNDL,'MR2')              "R2#10":'  ':
          PRINT 'Inv Amt : ':OCONV(INV.AMT,'MR2')   "R2#10":'  ':
          PRINT PRT.LBL:'GP % ':

          IF SUB.TOL=0 THEN GP=0 ELSE
             GP = 100*(GPD1)/SUB.TOL
             IF GP <= -100 OR GP > 100 THEN GP = 0
          END
          PRINT GP      "R0#3"
          IF SELBY[1,1] = "B" THEN
             INV.AMT  = SUB.TOL + FGHT + HNDL + TAX.TOL
             PRINT 'Subt :':OCONV(SUB.TOL,'MR2')          "R2#10":'  ':
             PRINT 'COGS GP$ : ':OCONV(GPD2,'MR2')        "R2#10":'  ':
             PRINT 'Tax :':OCONV(TAX.TOL,'MR2')           "R2#9":'  ':
             PRINT 'Frt :':OCONV(FGHT,'MR2')              "R2#10":'  ':
             PRINT 'Hndlg : ':OCONV(HNDL,'MR2')           "R2#10":'  ':
             PRINT 'Inv Amt : ':OCONV(INV.AMT,'MR2')      "R2#10":'  ':
             PRINT 'COGS GP % ':

             IF SUB.TOL=0 THEN GP=0 ELSE
                GP = 100*(GPD2)/SUB.TOL
                IF GP <= -100 OR GP > 100 THEN GP = 0
             END
             PRINT GP      "R0#3"
          END

             ADJ.COGS = -LED(16)<1,GEN>



             TOL.COGS = ADJ.COGS - EXP.INFRT - EXP.OUTFRT - EXP.INHNDL - EXP.OUTHNDL

             GRAND.COST += (TOL.COGS/100)
               GRAND.ADJ += ADJ.COGS

          PRINT 'Freight and Handling Distribution :'
          PRINT 'Freight In :' :OCONV(IN.FGHT,'MR2')      "R2#8":'  ':
          PRINT 'Freight Out :' :OCONV(OUT.FGHT,'MR2')    "R2#8":'  ':
          PRINT 'Handling In :' :OCONV(IN.HNDL,'MR2')     "R2#8":'  ':
          PRINT 'Handling Out :' :OCONV(OUT.HNDL,'MR2')   "R2#8":'  ':

          BEGIN CASE
          CASE SELBY[1,1] = "C"
             ADJ.GPD = SUB.TOL - COGS.AMT
          CASE SELBY[1,1] = "G"
             ADJ.GPD = SUB.TOL - COMM.AMT
          CASE SELBY[1,1] = "B"
             ADJ.GPD = SUB.TOL - COMM.AMT
          CASE OTHERWISE
             ADJ.GPD = 0
          END CASE

          IF SUB.TOL=0 THEN ADJ.GP=0 ELSE
             ADJ.GP = 100*(ADJ.GPD)/SUB.TOL
             IF ADJ.GP <= -100 OR ADJ.GP > 100 THEN ADJ.GP = 0
          END

          BEGIN CASE
          CASE SELBY[1,1] = 'B';  PRINT SPACE(10):
          CASE SELBY[1,1] = 'N';  PRINT SPACE(15):
          CASE OTHERWISE;         PRINT SPACE(20):
          END CASE

          PRINT PRT.LBL:'Adj GP % ':ADJ.GP       "R0#3":

          IF SELBY[1,1] = 'B' THEN
             ADJ.GPD = SUB.TOL - COGS.AMT
             IF SUB.TOL=0 THEN ADJ.GP=0 ELSE
                ADJ.GP = 100*(ADJ.GPD)/SUB.TOL
                IF ADJ.GP <= -100 OR ADJ.GP > 100 THEN ADJ.GP = 0
             END
             PRINT "  ":'COGS Adj GP % ':ADJ.GP "R0#3"
          END ELSE
             PRINT
          END
          PRINT 'Exp Frt In :' :OCONV(EXP.INFRT,'MR2')     "R2#8":'  ':
          PRINT 'Exp Frt Out :' :OCONV(EXP.OUTFRT,'MR2')   "R2#8":'  ':
          PRINT 'Exp Hndl In :' :OCONV(EXP.INHNDL,'MR2')   "R2#8":'  ':
          PRINT 'Exp Hndl Out :' :OCONV(EXP.OUTHNDL,'MR2') "R2#8":'  '
          PRINT
          PRINT STR('*',WDTH)
          PRINT
          PRINT.CT = PRINT.CT + 1
          RETURN
*-------------------------------------------------------------------------*
ONE.PN:   *LDID = LDIDS<1,LDX>
          IF NUM(LDID) THEN
             LD.GET LDID
             OE.GET.LI.OVRD.FLAG 1,GEN,GEN,OVRD.FLG,LDID,1
             PRC.OVRD.TYP  = OVRD.FLG
             OE.GET.LI.OVRD.FLAG 2,GEN,GEN,OVRD.FLG,LDID,1
             COGS.OVRD.TYP = OVRD.FLG
             OE.GET.LI.OVRD.FLAG 3,GEN,GEN,OVRD.FLG,LDID,1
             COST.OVRD.TYP = OVRD.FLG
             IF CURR.FLG THEN
                CONV.CURR.LD TGT,GEN
             END


             PN  = LD(1)

             * If Job Mangement Order and Line Item get the default
             * Job Management Product.
             IF IS.JOB THEN
                IF LD(1)[1,2] = "L#" THEN
                   IF LD(70) # "" AND NUM(LD(70)) THEN
                      PN = LD(70)
                   END ELSE
                      PN = DFLT.JM.PN
                   END
                END
             END

             IF NUM(PN) THEN
                IF LD(33)<1,GEN> = "MISC" AND NOT(MISC.CHG) THEN NULL ELSE
                   MATREAD PRD FROM PRDFILE,PN ELSE RETURN
                   IF PRD(3) = 6 THEN RETURN;* skip status Comment

                   QTY = -(SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>))
                   NP  = LD(8)<1,GEN>+0
                   IF NOT(COST.BASE<1,1>) THEN
                      CST1    = NP
                   END ELSE
                      CST1 = LD(COST.BASE<1,1>)<1,GEN>+0
                      IF SELBY[1,1] = "B" THEN
                         CST2 = LD(COST.BASE<1,2>)<1,GEN>+0
                      END ELSE
                         CST2 = ''
                      END
                   END
                   IQP = LD(9)<1,GEN>
                   IF NP=0 THEN
                      GP = 0;GP1 = 0;GP2 = 0
                   END ELSE
                      GP1 = 100*(NP-CST1)/NP
                      IF SELBY[1,1] = "B" THEN
                         GP2 = 100*(NP-CST2)/NP
                      END ELSE
                         GP2 = ''
                      END
                   END
                   IF MIN.GP=100 AND MAX.GP=100 THEN
                      IF SELBY[1,1] = "B" THEN
                         IF CST1+0=0 OR CST2+0=0 THEN GOSUB PR.PN
                      END ELSE
                         IF CST1+0=0 THEN GOSUB PR.PN
                      END
                   END ELSE
                      * When displaying both Cost and COGS, if either of
                      * the GP values are in the range then both will still
                      * print even if one of them is not.
                      IF SELBY[1,1] = 'B' THEN
                         IF (GP1 =< MIN.GP OR GP1 >= MAX.GP) OR (GP2 =<MIN.GP OR GP2 >= MAX.GP) THEN
                            GOSUB PR.PN
                         END
                      END ELSE
                         IF GP1 =< MIN.GP OR GP1 >= MAX.GP THEN
                            GOSUB PR.PN
                         END
                      END
                   END
                END
             END
          END
          RETURN
*-------------------------------------------------------------------------*
PR.PN:    IF NOT(HEADED) THEN GOSUB HEADER
          ALT.DESC = ''
          OE.DESC.GET DESC,ALT.DESC,'SOE'
          DLN = DCOUNT(DESC,VM)

          FOR DLX=1 TO DLN
             IF DLX=1 THEN
                GET.ALL.PRD BR,PN
                PRICE.PER.GET PER.QTY,PER.UM
                TYP = FIELD(LD(7)<1,GEN,1>,'~',1)
                IQ.TO.ALPHA PLNE(3),PRD(7),LD(23),QTY,Q1,U1,Q2,U2,QS.ALPHA
                PRINT " ":TYP"L#2":TRIM(QS.ALPHA)  "R#13":'  ':
                PRINT DESC<1,DLX>             "L#35":
                PRINT '  ':PER.UM             "L#2":
                IF LD(9)<1,GEN>#'' THEN
                   PRINT OCONV(IQP*PER.QTY,'MR9')      "R3#10":
                END ELSE PRINT ''                      "L#10":
                PRINT OCONV(NP*PER.QTY, 'MR9')         "R3#11":PRC.OVRD.TYP "L#1":
                EXT = ICONV(OCONV(NP*QTY,'MR9'),'MR2')
                IF COST.BASE<1,1> THEN
                   BEGIN CASE
                   CASE SELBY[1,1] = "B"
                      PRINT OCONV(CST1*PER.QTY,'MR9')  "R3#10":COST.OVRD.TYP "L#1":
                      PRINT OCONV(CST2*PER.QTY,'MR9')  "R3#10":COGS.OVRD.TYP "L#1":
                   CASE SELBY[1,1] = "G"
                      PRINT OCONV(CST1*PER.QTY,'MR9')  "R3#10":COST.OVRD.TYP "L#1":
                   CASE SELBY[1,1] = "C"
                      PRINT OCONV(CST1*PER.QTY,'MR9')  "R3#10":COGS.OVRD.TYP "L#1":
                   END CASE
                END ELSE
                   PRINT ''                            "R#11":
                END
                PRINT OCONV(EXT,'MR2')                 "R2#10":
                GPD = NP - CST1
                GPS$ = OCONV((GPD*QTY),'MR9')
                PRINT OCONV((GPD*QTY),'MR9')           "R2#10":
                IF NP=0 THEN GP=0 ELSE
                   GP = 100*(GPD)/NP
                END
                PRINT '  ':GP"R0#4":
                IF SELBY[1,1] = "B" THEN
                   GPD = NP - CST2
                   GPS$ = OCONV((GPD*QTY),'MR9')
                   PRINT OCONV((GPD*QTY),'MR9')        "R2#10":
                   IF NP=0 THEN GP=0 ELSE
                      GP = 100*(GPD)/NP
                   END
                   PRINT '  ':GP"R0#4"
                END ELSE
                   PRINT
                END
             END ELSE
                PRINT SPACE(18):DESC<1,DLX>            "L#35"
             END
          NEXT DLX
          GP$ += GPS$
          RETURN
*-------------------------------------------------------------------------*
HEADER:   HEADED = YES
          PCT   += 1
          IF SBR.PRICE THEN BR=LED(2)<1,GEN,1> ELSE BR=LED(2)<1,GEN,2>
          GET.CUS BR,LED(1)<1,GEN>,LED(5)<1,GEN>,QSIGN
          BT.CSZ = CUS(3):', ':CUS(4):' ':CUS(5)
          INVN = LED(8)<1,GEN>
          ORN  = OID:'.':INVN"R%3"
          PRINT 'Invoice # ':ORN                 "L#20":
           INSLMN = OCONV(LED(34)<1,GEN>,'TINITIALS;X;3;3')
          OTSLMN = OCONV(LED(72)<1,GEN>,'TINITIALS;X;3;3')
          PRINT 'Inside Sales: ':INSLMN           "L#23":' ':
          PRINT 'Outside Sales: ':OTSLMN          "L#23":'  '
          *READV PRINT.STAT FROM LEDLFILE,OID,9 ELSE PRINT.STAT = ''
          *PRINT 'Print Status : ':PRINT.STAT<1,GEN> "L#10"


          PRINT 'Bill to : ':CUS(1)              "L#40":
          PRINT 'Ship to : ':CUSS(1)             "L#40"
          PRINT '          ':CUS(2)<1,1>         "L#40":
          PRINT '          ':LED(78)<1,GEN,1>    "L#40"
          PRINT '          ':BT.CSZ              "L#40":
          PRINT '          ':TRIM(LED(78)<1,GEN,3>):" ":LED(75)<1,GEN>
          PRINT 'Shipdate: ':OCONV(LED(9)<1,GEN>,'D2/')"L#20":
          PRINT 'Stk Br # ':LED(2)<1,GEN,2>"L#16":
          IF NOT(BRK.ON) THEN
             WRITER = OCONV(LED(73)<1,GEN>,'TINITIALS;X;3;3')
             PRINT 'Writer: ':WRITER"L#20":
             PRINT 'Tax Jurisdiction: ':LED(79)<1,GEN,1>
          END ELSE
             PRINT SORT.OPT:': ':LAST.BRK"L#20":
          END
         * IF ORBY THEN
         *    OR.BY   = ''
         *    CRTL.ID = 'OE.VAL.CUS.CONTACT'
         *    READ VAL.NAME FROM CTRLFILE,CRTL.ID ELSE VAL.NAME = ''
         *    IF VAL.NAME THEN
         *       UT.OPEN.FILE 'CONTACT',CONFILE,ERR.MSG,YES
         *       IF NOT(ERR.MSG) THEN
         *          CON.ID         = LED(68)<1,GEN>
         *          CONTACT.ID     = CON.ID
         *          IF CON.ID[1,1] = '#' THEN
         *             CONTACT.ID  = TRIM(CON.ID[2,99])
         *          END
         *          READ CREC FROM CONFILE,CONTACT.ID ELSE CREC = ''
         *          IF CREC  # '' THEN
         *             OR.BY = TRIM(CREC<3>):', ':TRIM(CREC<1>):' '
         *             OR.BY:= TRIM(CREC<2>)
         *          END
         *       END
         *    END
         *    IF NOT(OR.BY) THEN OR.BY = LED(68)<1,GEN>
         *    PRINT 'Ordered By: ':OR.BY"L#30"
         * END ELSE
         *    PRINT
         * END
          PRINT 'Batch # : ':LED(28)<1,GEN>"L#20":
          PRINT 'PO # ':LED(13)<1,GEN>"L#20":
           IF LED(69)<1,GEN,1> THEN
             FGT.ALL = 'YES'
          END ELSE
             FGT.ALL = 'NO'
          END
          PRINT 'Freight Allowed: ':FGT.ALL"L#15":' ':
          PRINT 'Price Class: ':LED(37)<1,GEN,1>"L#7":
           READV LEDL17 FROM LEDLFILE,OID,17  ELSE LEDL17   = ''
          MNFST.ID = LEDL17<1,GEN>
          PRINT 'Manifest: ':MNFST.ID"L#8"
          PRINT 'Ship Via: ':LED(70)<1,GEN>"L#16"


          *PRINT 'Terms : ':LED(29)<1,GEN>"L#10"


          INT.NOTES = LED(80)<1,GEN>
          SHP.INS = LED(74)<1,GEN>
          IF INT.NOTES#'' THEN
             PRINT 'Internal Notes : ':INT.NOTES<1,1,1> "L#40"
             CT = DCOUNT(INT.NOTES,SVM)
             FOR J = 2 TO CT
             PRINT SPACE(17):INT.NOTES<1,1,J> "L#40"
             NEXT J
          END
          PRINT

          BEGIN CASE
          CASE SELBY[1,1] = "G"
             PRINT SPACE(106):'Cost  Cost'
          CASE SELBY[1,1] = "C"
             PRINT SPACE(106):'COGS  COGS'
          CASE SELBY[1,1] = "B"
             PRINT SPACE(116):'Cost  Cost      COGS  COGS'
          END CASE
          IF SELBY[1,1] = 'B' THEN
             PRINT 'Typ. Quantity...  Product Description................ Per  RegPrice   NetPrice   ':COST.HDR:'   ExtPrice      GP $  GP %      GP $  GP %'
          END ELSE
             PRINT 'Typ. Quantity...  Product Description................ Per  RegPrice   NetPrice   ':COST.HDR:'   ExtPrice      GP $  GP %'
          END

          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:  SORTBYS = ''
          IDS     = ''

          BEGIN CASE
          CASE PSTAT OR SD = '' OR ED = ''
             PQ.ACTIVE = YES
             CMD = 'SELECT PRINT.QUEUE '
             IF XFER.OPT # 'Only' THEN CMD := 'WITH STATUS "I" '
             IF XFER.OPT = 'Include' THEN CMD := ' OR'
             IF XFER.OPT # 'Exclude' THEN
                CMD := ' WITH (STATUS "S" AND @ID = "T]")'
             END

             EXECUTE CMD CAPTURING MSG

             LOOP
                READNEXT ID ELSE EXIT
                GOSUB ADD.ID
             REPEAT
          CASE OTHERWISE
             PQ.ACTIVE = NO
             BEGIN CASE
             CASE XFER.OPT = 'Only'
                TYPES = 5
             CASE XFER.OPT = 'Include'
                TYPES = 1:VM:5
             CASE XFER.OPT = 'Exclude'
                TYPES = 1
             END CASE
             TYP.CNT = DCOUNT(TYPES,VM)

             FOR TYP = 1 TO TYP.CNT
                FOR DT = SD TO ED
                   JIDS = JLI.READ.EMU(TYPES<1,TYP>:'*':DT)
                   MORE.DATA = (JIDS#'')
                   LOOP WHILE MORE.DATA
                      REMOVE ID FROM JIDS SETTING MORE.DATA
                      GOSUB ADD.ID
                   REPEAT
                NEXT DT
             NEXT TYP
          END CASE

          RETURN
*-------------------------------------------------------------------------*
ADD.ID:   *
          OID = FIELD(ID,'.',1)
          IDX = FIELD(ID,'.',2)+0
          MATREAD LED FROM LEDFILE,OID                    ELSE GOTO SKIPIT

          * Include/Exclude/Only Consignment Transfers
          IF CONSIGN = 'Exclude' AND LED(110)<1,1> = 'S' THEN GOTO SKIPIT
          IF CONSIGN = 'Only' AND LED(110)<1,1> # 'S' THEN GOTO SKIPIT


          IF PQ.ACTIVE THEN
             LOCATE IDX IN LED(12)<1>  SETTING GEN        ELSE GOTO SKIPIT
          END ELSE
             LOCATE IDX IN LED(8)<1>  SETTING GEN         ELSE GOTO SKIPIT
          END
          INVN = LED(8)<1,GEN>+0
          IF SBR.PRICE THEN BR=LED(2)<1,GEN,1> ELSE BR=LED(2)<1,GEN,2>
          LOCATE BR IN BRCHS<1> SETTING X                 ELSE GOTO SKIPIT
          IF BATCH#'' AND BATCH#LED(28)<1,GEN>            THEN GOTO SKIPIT
          IF SD#'' AND LED(9)<1,GEN> < SD                 THEN GOTO SKIPIT
          IF ED#'' AND LED(9)<1,GEN> > ED                 THEN GOTO SKIPIT
          IF FILTER.FLAG THEN
             FILTER.SELECT SKIP.FLAG,OID,GEN,FITEM
             IF SKIP.FLAG THEN GOTO SKIPIT
          END
          IF PSTAT THEN
             READV PST FROM LEDLFILE,OID,9 ELSE PST = ''
             LOCATE PST<1,GEN> IN PSTAT<1> SETTING XX ELSE GOTO SKIPIT
          END
          LID = OID:'.':GEN"R%3"
          CN = LED(1)<1,GEN>
          READV CSORT FROM CUSFILE,CN,8 ELSE CSORT = ''
          BEGIN CASE
          CASE SORT.OPT[1,1] = 'T'                     ; * Sortby time
             READV LOG FROM LEDLFILE,OID,10 ELSE LOG = ''
             SORTBY = LOG<1,1,2>"R%5":'!':LOG<1,1,3>"R%5":'!':ID
          CASE SORT.OPT[1,1] = 'C'        ; * Sortby customer, then OID/GEN
             SORTBY = CSORT:'!':CN:'!':LID:'!':ID
          CASE SORT.OPT[1,1] = 'W'                       ; * Sortby Writer
             SORTBY = LED(73)<1,GEN>:'!':CSORT:'!':CN:'!':ID
          CASE SORT.OPT[1,1] = 'O'          ; * Sortby outside salesperson
             SORTBY = LED(72)<1,GEN>:'!':CSORT:'!':CN:'!':ID
          CASE SORT.OPT[1,3] = 'Ins'         ; * Sortby inside salesperson
             SORTBY = LED(34)<1,GEN>:'!':CSORT:'!':CN:'!':ID
          CASE OTHERWISE                              ; * Sortby Invoice #
             SORTBY = OID:'.':INVN
          END CASE
          LOCATE SORTBY IN SORTBYS BY 'AL' SETTING POS ELSE NULL
          SORTBYS = INSERT(SORTBYS,POS;SORTBY)
          IDS     = INSERT(IDS,POS;LID)

SKIPIT:   RETURN
*-------------------------------------------------------------------------*
ONE.ID.ORIG:   *** Place one order on the report
          GP$    = 0
          HEADED = NO          ;* if headed then order was placed on report
          ID     = IDS<IDX>
          OID    = FIELD(ID,'.',1)
          GEN    = FIELD(ID,'.',2)+0


          MATREAD LED FROM LEDFILE,OID ELSE MAT LED=''





          * If Job Management Order set up default product.
          IS.JOB     = (LED(83)<1,1>[1,7] = "@JOBNO=")
          IF IS.JOB THEN
             LOCATE LED(33)<1,GEN> IN LED(12)<1> SETTING DGEN THEN
                ST.VEN = LED(5)<1,DGEN>
             END ELSE
                ST.VEN = ''
             END
             DFLT.JM.PN = JOBMGMT.GET.PN(ST.VEN)
          END

          * place all products that have an abnormal GP on the report.  Do
          * not place consignment transfers on the report if the print
          * status is going to be updated
          IF NOT(SET.STAT AND LED(110)<1,1> = 'S') THEN
             LDIDS  = RAISE(LED(48)<1,GEN>)
             LDN    = DCOUNT(LDIDS,VM)

                          FOR LDX = 1 TO LDN
                GOSUB ONE.PN.ORIG
             NEXT LDX
          END



          * If order placed on the report then print the footer
          IF HEADED THEN
             GOSUB FOOTER
          END

          * If the customer wants to Auto Set Print Status then update the
          * print status.
          IF SET.STAT THEN
             * Get values from Bill-to and Ship-to customer records
             READV BT.ACTION FROM CUSFILE,LED(1)<1,GEN>,50 ELSE BT.ACTION=0
             READV ST.ACTION FROM CUSFILE,LED(5)<1,GEN>,50 ELSE ST.ACTION=0
             * Bill-to Values
             BT.PRT = BT.ACTION<1,1>
             BT.FAX = BT.ACTION<1,2>
             * Ship-to Values
             ST.PRT = ST.ACTION<1,1>
             ST.FAX = ST.ACTION<1,2>

             * Get defaults from control record
             READ DFLTS FROM CTRLFILE,'BATCH.DFLT.SETTINGS' ELSE DFLTS = ''
             DFLT.IPRT = DFLTS<1>      ;* # of invoice copies to print
             DFLT.IFAX = DFLTS<2>      ;* # of invoice copies to fax

             * PRINT DATA
             * Figure out between the ship-to customer,
             * the bill-to customer and control file a
             * valid action to generate a invoice.
             BEGIN CASE
             CASE ST.PRT # '' AND NUM(ST.PRT)
                * Use Ship-to level setting
                DUP.CT = ST.PRT
             CASE BT.PRT # '' AND NUM(BT.PRT)
                * Use Bill-to level setting
                DUP.CT = BT.PRT
             CASE DFLT.IPRT # '' AND NUM(DFLT.IPRT)
                * Control record
                DUP.CT = DFLT.IPRT
             CASE OTHERWISE
                DUP.CT = 0
             END CASE

             * FAX DATA
             * Figure out between the ship-to customer,
             * the bill-to customer and control file a
             * valid action to generate a invoice.
             BEGIN CASE
             CASE ST.FAX # '' AND NUM(ST.FAX)
                * Use Ship-to level setting
                FAX.CT = ST.FAX
             CASE BT.FAX # '' AND NUM(BT.FAX)
                * Use Bill-to level setting
                FAX.CT = BT.FAX
             CASE DFLT.IFAX # '' AND NUM(DFLT.IFAX)
                * Control record
                FAX.CT = DFLT.IFAX
             CASE OTHERWISE
                FAX.CT = 0
             END CASE

             * Determine if print status should be updated to 'Hold'
             PRT.ST.CHG = NO
             IF IGN.CPYS OR (DUP.CT >= 0) OR (FAX.CT >= 0) THEN
                * If GP problem put on hold otherwise set to batch print
                IF HEADED THEN
                   PRT.STAT = 'B'
                   PRT.ST.CHG = YES
                END ELSE
                   PRT.STAT = 'B'
                   PRT.ST.CHG = YES
                   CREDIT.CARD.CHK.PRT.ST PRT.STAT,OID,GEN
                END
             END

             IF PRT.ST.CHG THEN
                OE.UPD.PRINT.STAT OID,GEN,PRT.STAT
                OE.ADD.COMMENT OID,GEN,'GP Preview changed Prt Status to : ':PRT.STAT
             END
          END

          RETURN
*-------------------------------------------------------------------------*
ONE.PN.ORIG:   LDID = LDIDS<1,LDX>
          IF NUM(LDID) THEN
             LD.GET LDID
             OE.GET.LI.OVRD.FLAG 1,GEN,GEN,OVRD.FLG,LDID,1
             PRC.OVRD.TYP  = OVRD.FLG
             OE.GET.LI.OVRD.FLAG 2,GEN,GEN,OVRD.FLG,LDID,1
             COGS.OVRD.TYP = OVRD.FLG
             OE.GET.LI.OVRD.FLAG 3,GEN,GEN,OVRD.FLG,LDID,1
             COST.OVRD.TYP = OVRD.FLG
             IF CURR.FLG THEN
                CONV.CURR.LD TGT,GEN
             END


             PN  = LD(1)

             * If Job Mangement Order and Line Item get the default
             * Job Management Product.
             IF IS.JOB THEN
                IF LD(1)[1,2] = "L#" THEN
                   IF LD(70) # "" AND NUM(LD(70)) THEN
                      PN = LD(70)
                   END ELSE
                      PN = DFLT.JM.PN
                   END
                END
             END

             IF NUM(PN) THEN
                IF LD(33)<1,GEN> = "MISC" AND NOT(MISC.CHG) THEN NULL ELSE
                   MATREAD PRD FROM PRDFILE,PN ELSE RETURN
                   IF PRD(3) = 6 THEN RETURN;* skip status Comment

                   QTY = -(SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>))
                   NP  = LD(8)<1,GEN>+0
                   IF NOT(COST.BASE<1,1>) THEN
                      CST1    = NP
                   END ELSE
                      CST1 = LD(COST.BASE<1,1>)<1,GEN>+0
                      IF SELBY[1,1] = "B" THEN
                         CST2 = LD(COST.BASE<1,2>)<1,GEN>+0
                      END ELSE
                         CST2 = ''
                      END
                   END
                   IQP = LD(9)<1,GEN>
                   IF NP=0 THEN
                      GP = 0;GP1 = 0;GP2 = 0
                   END ELSE
                      GP1 = 100*(NP-CST1)/NP
                      IF SELBY[1,1] = "B" THEN
                         GP2 = 100*(NP-CST2)/NP
                      END ELSE
                         GP2 = ''
                      END
                   END
                   IF MIN.GP=100 AND MAX.GP=100 THEN
                      IF SELBY[1,1] = "B" THEN
                         IF CST1+0=0 OR CST2+0=0 THEN GOSUB PR.PN
                      END ELSE
                         IF CST1+0=0 THEN GOSUB PR.PN
                      END
                   END ELSE
                      * When displaying both Cost and COGS, if either of
                      * the GP values are in the range then both will still
                      * print even if one of them is not.
                      IF SELBY[1,1] = 'B' THEN
                         IF (GP1 =< MIN.GP OR GP1 >= MAX.GP) OR (GP2 =<MIN.GP OR GP2 >= MAX.GP) THEN
                            GOSUB PR.PN
                         END
                      END ELSE
                         IF GP1 =< MIN.GP OR GP1 >= MAX.GP THEN
                            GOSUB PR.PN
                         END
                      END
                   END
                END
             END
          END
          RETURN
*-------------------------------------------------------------------------*
!TSMITH~10/14/15~09:31
