report zvadin02 line-count 100 message-id vn.
*@----------------------------------------------------------------------
*@ Program id    : ZVADIN02
*@ Program Desc  : Nowsco Invoice Printing
*@                 1st - Original program: RVADIN01
*@
*@ Transaction Code - none
*@ Input files   : ID - none
*@ Output files  : ID - none
*@
*@ Tables Updated: Table - none
*@
*@
*@ Author      : T. Nguyen
*@ Date        : Jan 28 95
*@ Requested by: Nowsco Well Services
*@----------------------------------------------------------------------
*@ Algorithm: ZVDOINV1 (using print processor) - T. Nguyen
*@        I have modified this program so that a file is generated when
*@        someone has tried to print an invoice.  This file contains
*@        information about the invoice.  Whenever possible, the data
*@        is written to file when the original program is writing the
*@        the same data to the form to be printed.  This insures that
*@        the data written to the file is the same as that which is
*@        printed on the invoice print out.  Any data that is not on
*@        the invoice print out is handled by a seperate routine by
*@        accessing the appropriate table(s).
*@----------------------------------------------------------------------
** MODIFICATION DETAILS:
** Jan 28 95  TNguyen   Major changes to original program
** Feb 01 95  TNguyen   Write 'N/C' to item prices when item category
**                      is ZFOB, AGNN, ZFOC, TANN
** C11K900704 Mar PTAM  Suppress B C surcharge line at item level
**                      Field no should be 31 instead of 01 at header
**
** C11K900736 Apr PTAM  Suppress group item detail lines and
**                      fix price on group item header line
**                      ( Assume group items must be all or non on a
**                      same invoice )
** C11K900784 Apr TNGUYEN  print Date of Service to flat file, id 21
** C11K900750 Apr PTAM  For CR Memo, print Order No., Date of Service &
**                      Job type.
** C11K900934 May TNGUYEN  print Invoice comments, Base in charge
**            Jun TNGUYEN  Get correct document title for G2, L2.
** LABK900750 Jul DVFINTEL Emergency fix to get order number on invoice
**                         printout
** LABK900923 Jul TNGUYEN  Fix get header text problem. Added
**                          subroutine GET_DOC_HEADER_TEXT
**            Aug PTAM     - Draft invoice is replaced by Proforma inv
**                         - Find all delivery notes instead of 1
**           Jlai         - To get path from table FILENAME
** LABK901070 Aug PTAM     - if HM00 exists in cond type
**                           = print N/C for price
**                           = suppress SURCHG
** LABK901124 Sep RVAT     - If there's an HM00 output HM00 for Header
**                         - Also output an HM00 at the item level
**                         - OUtput the SAP Document type F2, F8, etc
**                         - Output the ZREB Condition amount
** LABK901167 950928 TN    - Fix the problem of empty TKOMVD table ( the
**                           the table is retured by SAP function)
** LABK901387 951204 TN    - Get document type for reverse doc. Required
**                           for MS Access table ZTMPL.
** LABK901422 951219 GS    - Add SD category as key to look up table
**                           ZTMPL
**            960103 GS    - Added quick fix to avoid double printing of
**                           01 000001 38 (tax)
**                         - see " gs - quick fix
**  LABK901643 960301 JLai  - To eliminate Too Small Condition
**
**  LABK901707 960315 JLAI  - To get specify template for F8
**
**  LABK901744 960328 GSOLTYS - write out freight to flat file for
**                              use in ceco invoice (cond. type HD00)
**                            - for company code 2600, bypass checking
**                              item total vs. invoice total.
**                            - all changes commented with "gs
**
** LABK901926 960530 GSOLTYS - Undo change to bypass checking item total
**                             vs. invoice total for comp. code 2600.
**                             Instead, whenever we print out '02' '32',
**                             this is the number we use for a running
**                             sum.  This is based on the assumption
**                             that '32' is used ONLY for this purpose.
**                           - changes commented with "gs2
**                           - Use product line from line item as
**                             part of compound key to retreive template
**                             from ztmpl (get_ms_access_template)
**                             As a precaution, if we can't find a
**                             product line, use previous logic and use
**                             a '1'.  Changes outlined with "gs3.
** LABK901693 960531 GSOLTYS - Compensate for provincial surcharge in
**                             'Do Not Mail' validation.  Changes
**                             commented with "gs4
** LABK901946 960606 Jlai    - To pass old quantity from quotation
**                              to invoice
**                   GSoltys - compensate for new condition type ZRYT
**                           - when it exits, blank out the discount %
**                             at the item level
**                           - changes commented with "gs5
**LABK902148 960819 JLlai    - To fix bug for order quantity from quote
**LABK902227 960918 GSoltys  - allow FOB additives on invoice
**                           - changes commented with "gs6
**labk902142 Gsoltys    Utilize 'department' field of header-output as
** October 7th, 1996    drop zone indicator.  Changes commented
**                      "gs7
**
**LABK902133  961011  JBallard - Added the table PA0001 to replace PREL
**                               (used to retrieve employee names).
**                               Commented with "jb1.
**
**                               Special Note: If SAP ever deletes PREL
**                               The tables PA0001, PA0002, PA000#, etc.
**                              can be used, depending on the infotype #
**
**LABK902421       961204 jlai - To add material description to itemtab
**                           for further matching purpose
**                        gsoltys - Prevent double net value(29) output
**                                  - commented with "gs8
**LABK902417 December 6th, 1996 GSoltys
**                 Get well information from sales order instead of from
**                 invoice.
**                 Changes commented with "gs9
**LABK902483 January 8th, 1997 GSoltys
**                 To implememt BJ special price condition - ZBJS
**                 Changes commented with "gs10
**
**LABK902772 January 13, 1997 JBallard
**                 Modified to flag a Performa (F8) or and Invoice (F2)
**                 if any of the following conditions are met:
**
**                   1. Items that are in category TAS or ZAS and have a
**                      pricing condition of ZDSC.
**                   2. Any item with a positive discount and with a
**                      pricing condition of ZDSC.
**
**                 The invoice is flagged with a DO NOT MAIL message
**                 that will be printed off in Access.
**                 Changes commented with "jb2.
**
**           January 23, 1997 JBallard
**                 Same as above but invoice is also flagged if:
**                 1. PB00 and PB01 occur on same item.
**                 2. Double HM00 condition.
**                 3. HM00 and ZDSC cond together.
**                 4. ZBJS with ZDBP, PB00, PB01.
**                 Added subroutine PRINT_INVOICE_HEADER_ERROR - prints
**                 a DO NOT MAIL error message for flagged invoices.
**                 Changes commented with "jb2
**
**          March 26, 1997 JBallard
**                 Tax jurisdiction is now downloaded to the flat file
**                 so that Access can determine whether to print
**                 H.S.T for jurisdictions CHNS1, and CHNF1 (CH*).
**                 Done for companies in Newfoundland and Nova Scotia
**                 where P.S.T. and G.S.T. are combined into H.S.T.
**                 (Harmonized Sales Tax).
**                 Changes commented with jb3.
**
**          April 4, 1997 Jballard
**                Added condition for invoice validations that
**                only look at invoices greater than 71041090. Done so
**                that the validations dont kick in for old invoices.
**                Changes commented with jb4
**
**LABK910355  Feb 18, 1998 LABK910355
**                Add validation to ensure that the Ship To/Tax area
**                on the invoice is the same one as on the sales order.
**                Changes commented with jb5
**----------------------------------------------------------------------
**LABK910778 March 13, 1998
**                Suppress printing of items that do not have a
**                material number.
**                Changes commented with jb6
**----------------------------------------------------------------------
**LABK912012 June 17, 1998
**                Additional chagned regarding Ship To/Tax area,
**                See LABK910355.  Changes marked with gh1
**----------------------------------------------------------------------
**LABK9*     Sept 29, 1998 CFieulle
**                Enable printing by SAPScript as well as print
**                processor. SAPScript uses layout set ZV_NWS_INVOICE01.
**                Two other abap's copied to use SAPscript:
**                zvprint3 (from zvprint2)
**                zvrefsi2 (from zvrefsi1)
**----------------------------------------------------------------------
**LABK913512 Nov 23, 1998  CFieulle
**                Sort items by material number so items of a
**                particular material group (eg. PRODUCT) print
**                together.
**LABK913516      Make SAPScript copy the 'Do Not Use Copy' for now.
**----------------------------------------------------------------------
**LABK913524 Nov 24, 1998 CFieulle
**                Items of same description should be compressed into
**                one line item.
**                Use 'Invoice Form' field of document header text
**                as flag for reprinting of an original document.
**                User will enter the word original in this field.
**----------------------------------------------------------------------
**LABK916213 Aug 23, 1999 CFieulle
**                For situations where there are line items of condition
**                type HM00 with no price (free) (item category TANN,
**                ZFOC or ZTAP) which users want to be included in the
*                 fixed price, because these items have no price,
**                they have been separated into 'Additional Items'.
**                Modify so that these items are included in the
**                fixed price.
**----------------------------------------------------------------------
** LABK918227 Jan 27, 2000 CFieulle
**                Add new condition type Nowsco special discount.
**                Configured for Wascana only, and amount to be printed
**                after BC surcharge and before tax.
**----------------------------------------------------------------------
** LABK918670  CFieulle 2000-03-09 Re user default printer message:
**                                  specify message type 'ZZ', so that
**                                  message VN001 doesn't come up
**                                  when there is a printer error.
**----------------------------------------------------------------------
** Post 46B upgrade**
** LABK926189  CFieulle 2000-07-10 For 46B, jobtype descriptions are
**                                 are now appended with the job type
**                                 number.  We don't want to show the
**                                 number when printing.
**----------------------------------------------------------------------
** LABK926388  CFieulle 2000-07-26 Item text is brought in via
**                                 'RV_BILLING_PRINT_VIEW' from the
**                                 delivery note.  However, the users
**                                 need to change the item descr from
**                                 time to time, in which case, they
**                                 would have to change the del note
**                                 item text.  BUT item text in del's
**                                 can't be changed, so we're making
**                                 sure that the item text we display
**                                 on the invoice comes from the
**                                 corresponding sales order line item.
**----------------------------------------------------------------------
** LABK926703  CFieulle 2000-08-25 Header text retrieval.  Make sure
**                                 that an invoice/order or quote
**                                 number exists to plug into stxl id
**                                 to prevent pulling up a header text
**                                 when none exists for current document
**----------------------------------------------------------------------
** LABK928274  CFieulle 2000-09-26 Disable file extraction.
**----------------------------------------------------------------------
** LABK928636  CFieulle 2000-10-27 Header texts: afe and proposal no.:
**             When a document is copied to another the text may be
**             copied, but only up to a certain number of docs.  So
**             when searching for the text, if there's none, check
**             back to the referenced docs to find the text.
**----------------------------------------------------------------------
** LABK928651  CFieulle 2000-10-31 Re LABK928636 above.  Correct
**             infinite loop.
**----------------------------------------------------------------------
** LABK928790  CFieulle 2000-11-14 Header texts: Re LABK928636 above
**             Do the same for Customer Field Rep.
**----------------------------------------------------------------------
** LABK929091  Gary Horn  January 5, 2001
**             Add logic to look for ZCO2 condition type
**             - changes marked with gh3
**----------------------------------------------------------------------
** LABK929819  CFieulle February 06, 2001
**             Logic to handle C02 surcharge: condition type ZBJS.
**----------------------------------------------------------------------
** LABK930236  CFieulle June 19, 2001
**             New Condition type Z100 created for 'no-charge' items.
**             Items of category 'TANN', ZTAP', and 'ZFOC' are auto-
**             matically defaulted to this cond. type and therefore
**             should have no price.
**----------------------------------------------------------------------
** LABK931107  CFieulle Oct 30, 2001
**             Credit Memos:  For HM00's, instead of printing each
**             line item, change to print only main HM00 line just as
**             for regular invoices.
**----------------------------------------------------------------------
** LABK931123  CFieulle Oct 31, 2001
**             Co 9040: do not exclude first 4 chars of job type desc.
**             for invoices created from Oct 1, 2001
**----------------------------------------------------------------------
** LABK931392  IJADALOW  December 04, 2001.
**             Replacing tables TVV1 and TVV1T, with tables T2502 and
**             T25A2 respectively.  T2502 contains all job types from
**             the past as well as current ones.
**             Requestd by Ophelia Tik.
**             Look for "IJ1" for the changes made.
** LABK931491  IJADALOW  December 14, 2001.
**             Fix output of the Job Description.
**----------------------------------------------------------------------
** LABK934346  Field 'lines' in structure zinvoice01 was changed from
**             2 chars to 4.  Condense so that it can be read correctly
**             in the layoutset, else it affects the alignment of the
**             form.
**----------------------------------------------------------------------
** LABK934226  IJADALOW January 3, 2003
**    &        Print Parent item of FOB's.
** LABK934392  This FOB change is effective for Berlington Cementing
**             Invoices Only
*@----------------------------------------------------------------------
** LABK934576  IJADALOW March 12, 2003
**             Print the Fuel Surcharge
**----------------------------------------------------------------------
** LABK934634  CFieulle March 20, 2003
**             Allow for extra line for fuel surcharge in the 'Total'
**             box of the printout.
**----------------------------------------------------------------------

tables: komk,                          "Communicationarea for conditions
        komp,                          "Communicationarea for conditions
        komvd,                         "Communicationarea for conditions
        vbco3,                         "Communicationarea for view
        vbdkr,                         "Headerview
        vbdpr,                         "Itemview
        sadr,                          "Addresses
        tvko,                          "Sales organisation
        tvstt,                         "Plant name
*        FILENAME,                      "Path
        adrs,                          "Communicationarea for Address
        konv.                                               "gs
include rvadtabl.

* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
data: zryt_flag type i.                                     "gs5
data: net_printed type i.                                   "gs8
data: freight_value like konv-kwert.                        "gs
*DATA: PATH(60) TYPE C.
data: retcode   like sy-subrc.         "Returncode
data: repeat(1) type c.
data: xvbeln like vbrk-vbeln.
data: xposnr like vbrl-posnr.

data: begin of tvbdpr occurs 100.      "Internal table for items
        include structure vbdpr.
data: end of tvbdpr.

data: begin of tkomv occurs 50.
        include structure komv.
data: end of tkomv.

data: begin of tkomvd occurs 50.
        include structure komvd.
data: end of tkomvd.

* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

data: pr_kappl(01)   type c value 'V'. "Application for pricing

* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
********************************************************************
*  Nowsco required additional data
include zvprtda1.
tables: vbrp, *vbrp, komv, vbak,
*** IJ1 Replace Starts
*       vbfa, vbpa, tvv1t, vbrk, bkpf,
        vbfa, vbpa, T25A2, vbrk, bkpf,
*** IJ1 Replace Ends
        prel,                          " Employee names    AClimie
        pa0001,          "Replaces PREL for empl. names  - jb1
        p0001.

data: filenam(8), text80(80) type c, text240(240) type c,
      item_count(6) type n, item_field_id(2) type n,
      item_total_after_disc like komvd-kwert,
      book_total            like komvd-kwert,
      del_notes(78) type c,
      len(2) type n,
      doc_type like vbrk-fkart,
      product_line_flag type i,                             "gs3
      prod_line like vbrp-kvgr2.                            "gs3

data: job_type_no like vbak-kvgr1,     "AClimie
      item_gross_value like komvd-stunr value '100',
      item_fixed_price like komvd-stunr value '016',
      hm00_cond like komvd-stunr value '420',
      hm00_cond_flag(1) type c value 'N',
      hm00_header_cond_flag(1) type c value 'N',
      hm00_exists(1) type c value ' ',
      net_value1 like komvd-stunr value '400',
      net_value2 like komvd-stunr value '500',
      tax_id(2) type n value '32',
      last_print_date like nast-datvr,
      last_print_time like nast-uhrvr,
      opr_zyrt like komvd-stunr value '430',                "gs5
      opr_zbjs like komvd-stunr value '398',                "gs10

      c02_cond_old like komvd-stunr value '398',            "LABK929819
      c02_cond_new like komvd-stunr value '425',            "LABK929819
      c02_new_cond_flag(1) type c,                          "LABK929819

      opr_nocharge like komvd-stunr value '350',            "LABK930236

      zdsc_cnt_flag type i,                                 "jb2
      tas_zas_flag(1) type c,                               "jb2
      bad_item_no1 like vbrp-posnr,                         "jb2
      bad_item_no2 like vbrp-posnr,                         "jb2
      bad_item_no3 like vbrp-posnr,                         "jb2
      bad_item_no4 like vbrp-posnr,                         "jb2
      bad_item_no5 like vbrp-posnr,                         "jb2
      tas_zas_zdsc_error_flag(1) type c,                    "jb2
      zdsc_pos_disc_flag(1) type c,                         "jb2
      dual_cond_err_flag type c,                            "jb2
      pb00_cnt_flag type i,                                 "jb2
      pb01_cnt_flag type i,                                 "jb2
      zdbp_cnt_flag type i,                                 "jb2
      hm00_cnt_flag type i,                                 "jb2
      dum_rc type i,                                        "jb2
      dum_post_amt(16) type c,                              "jb2
      dum_pbook like bsid-dmbtr,                            "jb2
      dum_cond_amt like bsid-dmbtr,                         "jb2
      dum_cond_type(4) type c,                              "jb2
      err_msg1(40) type c,                                  "jb2
      err_msg2 like err_msg1,                               "jb2
      err_msg3 like err_msg1,                               "jb2
      err_msg4 like err_msg1,                               "jb2
      dbl_hm00_err_flag(1) type c,                          "jb2
      pb00_pb01_err_flag(1) type c ,                        "jb2
      zdsc_hm00_err_flag(1) type c,                         "jb2
      nofobtext_err_flag(1) type c,                         "LABK934392
      shipto_diff_flag type c.                              "jb5

field-symbols: , .

data: temp_name(40) type c.
data: zatx_netwr_sum     like vbrp-netwr,
      zatx_net_no_surchg like vbrp-netwr,
      zatx_surchg        like vbrp-netwr,
      tkomvd_lines(3) type p,
      hdr_net_bef_tax_surchg like vbrp-netwr.

data: begin of cancelled_docs occurs 50,   " Store cancelling billing
          vbeln like vbfa-vbeln,       " documents
      end of cancelled_docs.

data: begin of quote occurs 1.
        include structure vbak.
data: end of quote.

data: begin of sales_ord occurs 1.
        include structure vbak.
data: end of sales_ord.

data: begin of del_note_tbl occurs 4,
        vbeln like vbfa-vbeln,
      end of del_note_tbl.

data: begin of t_string,
        char(1) type c,
        rest(39) type c,
      end of t_string.
*{LABK934226
DATA: BEGIN OF tvbap OCCURS 50.
        INCLUDE STRUCTURE vbdpr.
DATA: END OF tvbap.

DATA: fob_parent TYPE i VALUE 0,
      fob_posnr(6) TYPE n,
      fob_blend(45) TYPE c,          "description
      g_cust_num LIKE vbpa-kunnr.

*}LABK934226
* ******* data for reformat sales order output ****** "JL
tables: vbap.
data:
      begin of itemtab occurs 50,
        item_count(6) type n,
        material_no like vbdpr-matnr,
        arktx like vbdpr-arktx,                             "JL1204
        qty_quoted like zinvoice01-quantity,          "LABK934392
      end of itemtab,
      quotation like vbdkr-vbeln,
      quote_flag type i value 0.

*----- Start addition for SAPScript
tables: zinvoice01,
        tsp03,
        usr01.

data: xscreen(1) type c.               "Output on printer or screen

data: begin of item_data occurs 20,
        matnr     like vbrp-matnr,
        mat_num   like zinvoice01-mat_num,
        mat_desc  like zinvoice01-mat_desc,
        quantity  like zinvoice01-quantity,
        uom       like zinvoice01-uom,
        category  like zinvoice01-category,
        tt_af_sur like zinvoice01-tt_af_sur,
        book_val  like zinvoice01-book_val,
        net_val   like zinvoice01-net_val,
        disc_per  like zinvoice01-disc_per,
        disc_amt  like zinvoice01-disc_amt,
        cond_type like zinvoice01-cond_type,
        price_ad  like zinvoice01-price_ad,
        3pty_sur  like zinvoice01-3pty_sur,
        prov_sur  like zinvoice01-prov_sur,
        mat_reim  like zinvoice01-mat_reim,
        fin_amt   like zinvoice01-fin_amt,
        qty_quoted like zinvoice01-quantity,
        bj_cond    like zinvoice01-bj_cond,
        count(6) type n,
        list_pr(15) type c,
        disc(6) type c,
        disc_pr(15) type c,
        price(15) type c,
        quanu(15) type c,
        quanq(15) type c,
        tax_flag(1) type c,
        uepos LIKE vbrp-uepos,       "LABK934226
        posnr LIKE vbrp-posnr,       "LABK934226
      end of item_data.
*{ Start Insert LABK934392
data: item_tmp LIKE item_data OCCURS 20 WITH HEADER LINE,
      rec_index LIKE sy-tabix.
*} LABK934392

data: begin of item_gr1 occurs 20.
        include structure item_data.
data: end of item_gr1.

data: begin of item_gr2 occurs 20.
        include structure item_data.
data: end of item_gr2.

data: begin of item_gr3 occurs 20.
        include structure item_data.
data: end of item_gr3.

data: begin of xitem_data occurs 20,
        matnr     like vbrp-matnr,
        mat_num   like zinvoice01-mat_num,
        mat_desc  like zinvoice01-mat_desc,
        quantity  type p decimals 3,
        uom       like zinvoice01-uom,
        category  like zinvoice01-category,
        tt_af_sur type p decimals 2,
        book_val  type p decimals 2,
        net_val   type p decimals 2,
*        disc_per(5) type c,
        disc_per(7) type c,                                 "LABK930236
        disc_amt  type p decimals 2,
        cond_type like zinvoice01-cond_type,
        price_ad  type p decimals 2,
        3pty_sur  type p decimals 2,
        prov_sur  type p decimals 2,
        mat_reim  type p decimals 2,
        fin_amt   type p decimals 2,
        qty_quoted type p decimals 3,
        bj_cond    like zinvoice01-bj_cond,
*       count(6) type n,
        list_pr(15) type c,
        disc(6) type c,
        disc_pr(15) type c,
        price(15) type c,
        quanu(15) type c,
        quanq(15) type c,
        tax_flag(1) type c,
        uepos LIKE vbrp-uepos,       "LABK934226
        posnr LIKE vbrp-posnr,       "LABK934226
      end of xitem_data.

data: tax1  like komvd-kwert,
      tax2  like komvd-kwert,
      tax3  like komvd-kwert,
      tax4  like komvd-kwert,
      tax5  like komvd-kwert,
      itax1 like komvd-kwert,
      itax2 like komvd-kwert,
      itax3 like komvd-kwert,
      itax4 like komvd-kwert,
      itax5 like komvd-kwert,
      tax_jcd  like vbpa-kunnr,                     "Tax jurisdiction
      sum1hm00 like zinvoice01-net_val,
      sum2hm00 like zinvoice01-net_val,
      sum1norm like zinvoice01-net_val,
      sum2norm like zinvoice01-net_val,
      line1(72) type c,
      line2(72) type c,
      line3(72) type c,
      line4(72) type c.
*     line5(72) type c.

*DATA:   PREVIEW(2) TYPE C.

*----- End addition for SAPScript


********************************************************************


form entry using return_code us_screen.

  clear retcode.
*  CLEAR PREVIEW.
  perform processing using us_screen.
*///////////////////////////////////////////////////////////////////////

** write print control data to file
*  CLEAR PRINT_TABLE.
*  REFRESH PRINT_TABLE.
*  PRINT_TABLE = ITCPO.
*  APPEND PRINT_TABLE.
** For Invoice form, get template from text field id 0011
*  CLEAR TEMP_NAME.
* ???
**ERFORM WRITE_CONTROL_DATA(ZVPRINT1) TABLES PRINT_TABLE USING
**  'jjjj' VBDKR-VBELN VBDKR-VBTYP 'xxxx'.  "???
* The following commented out because the Invoice Form text line in
* the document header text is no longer used to print USA invoices
* so this field can be used to indicate reprint of an original invoice.
* See 'Get_document_title'.
*  IF VBDKR-VBTYP = 'M'    " Invoice Form from invoice save text
*     OR VBDKR-VBTYP = 'U'.             " F2 = M, F8 = U
*    PERFORM GET_HEADER_TEXT(ZVPRINT3) USING
*                                      VBDKR-VBELN '0011' TEXTLINE.
*    IF TEXTLINE NE SPACE.
*      TEMP_NAME = TEXTLINE.
*    ENDIF.
*  ENDIF.
*  IF ( VBDKR-VBTYP = 'M'    " Invoice Form from sales save text
*       OR VBDKR-VBTYP = 'U' )  AND TEMP_NAME = ' '.
*    PERFORM GET_HEADER_TEXT(ZVPRINT3) USING VBDKR-VBELN_VAUF
*            '0011' TEXTLINE.
*    IF TEXTLINE NE SPACE.
*      TEMP_NAME = TEXTLINE.
*    ENDIF.
*  ENDIF.
** Get microsoft access template name for this region
** Product line was hard coded to '1' because there is no product line
** information for invoices or proforma
*
*  IF TEMP_NAME = ' '.
**                                                                  "gs3
*    PRODUCT_LINE_FLAG = 0.
*    SELECT * FROM VBRP WHERE VBELN EQ VBDKR-VBELN.
*      IF VBRP-KVGR2 NE  ' '.
*        PRODUCT_LINE_FLAG = 1.
*        MOVE VBRP-KVGR2 TO PROD_LINE.
*        EXIT.
*      ENDIF.
*    ENDSELECT.
*
*  IF PREVIEW = 'NO'.
*    IF PRODUCT_LINE_FLAG EQ 1.
*      PERFORM GET_MS_ACCESS_TEMPLATE(ZVPRINT3) USING
*              VBDKR-VKORG    DOC_TYPE  VBDKR-VBTYP PROD_LINE TEMP_NAME.
*    ELSE.
*      PERFORM GET_MS_ACCESS_TEMPLATE(ZVPRINT3) USING
*               VBDKR-VKORG    DOC_TYPE  VBDKR-VBTYP    '1'   TEMP_NAME.
*    ENDIF.
*  ENDIF.
*
*  ENDIF.
**                                                                  "gs3
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_CONTROL_DATA(ZVPRINT3) TABLES PRINT_TABLE USING
*            VBDKR-VBELN   SY-UNAME   TEMP_NAME     'ZVADIN02'.
*    CLOSE DATASET DATAFILE.
*    PERFORM SAP_MOVER_FILE_SETUP(ZVPRINT3) USING DATAFILE.
*  ENDIF.
*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  case retcode.
    when 0.
      return_code = 0.
    when 3.
      return_code = 3.
    when others.
      return_code = 1.
  endcase.

endform.

*---------------------------------------------------------------------*
*       FORM PROCESSING                                               *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  PROC_SCREEN                                                   *
*---------------------------------------------------------------------*
form processing using proc_screen.

  perform get_data.

  if vbdkr-uvall ne space or vbdkr-uvals ne space or
     vbdkr-uvprs ne space.
    if proc_screen = space.
      retcode = 3.
      if vbdkr-uvall ne space or vbdkr-uvals ne space.
        syst-msgno = '201'.
        syst-msgid = 'VN'.
        syst-msgty = 'I'.
        perform protocol_update.
      endif.
      if vbdkr-uvprs ne space.
        syst-msgno = '200'.
        syst-msgid = 'VN'.
        syst-msgty = 'I'.
        perform protocol_update.
      endif.
    else.
      if vbdkr-uvall ne space or vbdkr-uvals ne space.
        message i201.
      endif.
      if vbdkr-uvprs ne space.
        message i200.
      endif.
    endif.
  endif.

  check retcode = 0.
  perform form_open using proc_screen vbdkr-land1.
  check retcode = 0.
* Get header info
  perform write_header_info_to_file.
  perform item_print.
  check retcode = 0.
  perform end_print.
  check retcode = 0.
  perform massage_data.
  perform activate_elements.
  perform form_close.
  check retcode = 0.
endform.

***********************************************************************
*       S U B R O U T I N E S                                         *
***********************************************************************

*---------------------------------------------------------------------*
*       FORM END_PRINT                                                *
*---------------------------------------------------------------------*
*                                                                     *
*---------------------------------------------------------------------*

form end_print.
  perform get_header_prices.
  perform header_price_print.
endform.

*---------------------------------------------------------------------*
*       FORM FORM_CLOSE                                               *
*---------------------------------------------------------------------*
*       End of printing the form                                      *
*---------------------------------------------------------------------*

form form_close.

  call function 'CLOSE_FORM'
       exceptions
            others = 1.
  if sy-subrc ne 0.
    retcode = sy-subrc.
    perform protocol_update.
  endif.
  set country space.

endform.

*&---------------------------------------------------------------------*
*&      Form  PRINT_INVOICE_HEADER_ERROR
*&---------------------------------------------------------------------*
*  Created by: John Ballard Jan/97.
*       This subroutine prints an error message in the address
*       fields of the invoice in Access. The error message
*       depends on what lines are passed to it. They vary
*       depending on what condtions have been violated.
*       See FORM HEADER_PRICE_PRINT for existing error
*       messages.
*----------------------------------------------------------------------*
form print_invoice_header_error using line1 line2 line3 line4.

*IF PREVIEW = 'NO'.
*  PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '52' LINE1.
*  PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '53' LINE2.
*  PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '54' LINE3.
*  PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '55' LINE4.
*ENDIF.

endform.                               " PRINT_INVOICE_HEADER_ERROR
*---------------------------------------------------------------------*
*       FORM FORM_OPEN                                                *
*---------------------------------------------------------------------*
*       Start of printing the form                                    *
*---------------------------------------------------------------------*
*  -->  US_SCREEN  Output on screen                                   *
*                  ' ' = Printer                                      *
*                  'X' = Screen                                       *
*  -->  US_COUNTRY County for telecommunication and SET COUNTRY       *
*---------------------------------------------------------------------*

form form_open using us_screen us_country.

* note: ZRVADOPF is a copy of RVADOPFO - I had to add one line of code
* in this program to suppress the printing of the document .... (Lee)
* include zrvadopf.

  if nast-ldest = 'DFLT'.    " Use the user default printer.
    select single * from usr01 where bname = sy-uname.
    select single * from tsp03 where padest = usr01-spld.
    if sy-subrc = 0.
      nast-ldest = usr01-spld.
    else.
*     message e001 with 'Unable to find user default printer.'.
      message e001(zz) with                                 "LABK918670
              'Unable to find user default printer.'.       "LABK918670
    endif.
  endif.


  include rvadopfo.

*  IF ITCPO-TDPREVIEW <> 'X' AND
*     ITCPO-TDNOPRINT <> 'X'.
*    PREVIEW = 'NO'.
*  ENDIF.


endform.

*---------------------------------------------------------------------*
*       FORM GET_DATA                                                 *
*---------------------------------------------------------------------*
*       General provision of data for the form                        *
*---------------------------------------------------------------------*

form get_data.

  call function 'RV_PRICE_PRINT_REFRESH'
       tables
            tkomv = tkomv.
  clear komk.
  clear komp.

  if nast-objky+10(6) ne space.
    vbco3-vbeln = nast-objky+16(10).
  else.
    vbco3-vbeln = nast-objky.
  endif.

  vbco3-mandt = sy-mandt.
  vbco3-spras = nast-spras.
  vbco3-kunde = nast-parnr.
  vbco3-parvw = nast-parvw.

  call function 'RV_BILLING_PRINT_VIEW'
       exporting
            comwa = vbco3
       importing
            kopf  = vbdkr
       tables
            pos   = tvbdpr.

  perform sender.
  perform check_repeat.
endform.

*---------------------------------------------------------------------*
*       FORM GET_ITEM_PRICES                                          *
*---------------------------------------------------------------------*
*       In this routine the price data for the item is fetched from   *
*       the database.                                                 *
*---------------------------------------------------------------------*

form get_item_prices.

  clear: komp,
         tkomv.

  if komk-knumv ne vbdkr-knumv.
    clear komk.
    komk-mandt = sy-mandt.
    komk-kalsm = vbdkr-kalsm.
    komk-kappl = pr_kappl.
    komk-waerk = vbdkr-waerk.
    komk-knumv = vbdkr-knumv.
    komk-vbtyp = vbdkr-vbtyp.
  endif.
  komp-kposn = vbdpr-posnr.

  call function 'RV_PRICE_PRINT_ITEM'
       exporting
            comm_head_i = komk
            comm_item_i = komp
            language    = nast-spras
       importing
            comm_head_e = komk
            comm_item_i = komp
       tables
            tkomv       = tkomv
            tkomvd      = tkomvd.

endform.

*---------------------------------------------------------------------*
*       FORM GET_HEADER_PRICES                                        *
*---------------------------------------------------------------------*
*       In this routine the price data for the header is fetched from *
*       the database.                                                 *
*---------------------------------------------------------------------*

form get_header_prices.

  call function 'RV_PRICE_PRINT_HEAD'
       exporting
            comm_head_i = komk
            language    = nast-spras
       importing
            comm_head_e = komk
       tables
            tkomv       = tkomv
            tkomvd      = tkomvd.

endform.

*---------------------------------------------------------------------*
*       FORM HEADER_PRICE_PRINT                                       *
*---------------------------------------------------------------------*
*       Printout of the header prices                                 *
*---------------------------------------------------------------------*

form header_price_print.

  data: begin of header_tax occurs 5,
          koaid like komvd-koaid,
          kschl like komvd-kschl,
          kwert like komvd-kwert,
        end of header_tax.

* Get header final amount/ net total to file
*IF PREVIEW = 'NO'.
*  PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '01' '000001' '25' BOOK_TOTAL.
*  PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '01' '000001' '38' KOMK-FKWRT.
*ENDIF.
  move book_total to zinvoice01-hprbk_tot.
  move komk-fkwrt to zinvoice01-hfinal_amt.

* PERFORM WRITE_TO_FILEA(ZVPRINT1) USING '01' '000001' '32' KOMK-SUPOS.
  perform get_pricing_proc(zvprint3) tables tkomv tax_table   using
                   nows_disc_amt   nows_disc_perc
                   price_adj_amt
                   3party_surchg_amt
                   prov_surchg_amt
                   mat_reimb_amt       "950923 RV
                   0                   "  0 --> header prices
                   zbjs_amt            "gs10
                   nows_spec_disc_amt.                      "LABK918227
*
* if there is HM00 in cond type => ignore header surcharge
* added HM00 output IF THERE IS ANY hm00 ON THE INVOICE RV 950923
* IF HM00_COND_FLAG NE ' '.                           " rv 950923
  if hm00_header_cond_flag ne 'N'.                    " rv 950923
    clear: prov_surchg_amt.
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '77' 'HM00'.
*    ENDIF.
    move 'HM00' to zinvoice01-hhead_cont.
  endif.

  hdr_net_bef_tax_surchg = vbrk-netwr. " get header net price       "pt
* HDR_NET_BEF_TAX_SURCHG = KOMK-SUPOS - PROV_SURCHG_AMT.            "pt
*                                      - MAT_REIMB_AMT.   "950923 RV
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '01' '000001' '32'
*                                             HDR_NET_BEF_TAX_SURCHG.
*    PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '01' '000001'
*                                                   '24' NOWS_DISC_PERC.
*    PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '01' '000001'
*                                                   '26' NOWS_DISC_AMT.
*    PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '01' '000001'
*                                                   '27' PRICE_ADJ_AMT.
*    PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '01' '000001'
*                                              '28' 3PARTY_SURCHG_AMT.
*  ENDIF.
  move hdr_net_bef_tax_surchg to zinvoice01-htt_af_sur.
  move nows_disc_perc to zinvoice01-hdisc_per.
  move nows_disc_amt to zinvoice01-hdisc_amt.
  move price_adj_amt to zinvoice01-hprice_adj.
  move 3party_surchg_amt to zinvoice01-h3pty_sur.

  if nows_spec_disc_amt ne 0.                               "LABK918227
    move nows_spec_disc_amt to zinvoice01-hsdisc_amt.       "LABK918227
    move 'Special Discount' to zinvoice01-hsdisc_des.       "LABK918227
  endif.                                                    "LABK918227

*{ start insert LABK934576
  if fuel_surchg_amt ne 0.
    move fuel_surchg_amt to zinvoice01-hfuelsur_amt.
    move '   Fuel Surcharge' to zinvoice01-hfuelsur_des.
  endif.
*} Start Insert LABK934576

*
* only write header surcharge if there is no HM00 in cond type
* after all the item_print processed, surcharge might have been reset
* if HM00 exists.
  if prov_surchg_amt ne 0.
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '01' '000001'
*                                                 '30' PROV_SURCHG_AMT.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                       '31' 'BC Surcharge'.
*    ENDIF.
    move prov_surchg_amt to zinvoice01-htot_sur.
    move 'BC Surcharge' to zinvoice01-hsur_desc.
  endif.

  if mat_reimb_amt ne 0.               "950923 RV
*    IF PREVIEW = 'NO'.
*     PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '01' '000001'   "950923 RV
*                    '79' MAT_REIMB_AMT.                      "950923 RV
*     PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'     "950923 RV
*                    '80' 'Mat. Reimbursement'.               "950923 RV
*    ENDIF.
    move mat_reimb_amt to zinvoice01-hmat_reim.
    move 'Mat. Reimbursement' to zinvoice01-hreim_desc.
  endif.                               "950923 RV

  total_after_disc_amt = book_total +
              nows_disc_amt + price_adj_amt + 3party_surchg_amt
              + zbjs_amt.              "gs10
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '01' '000001'
*                                       '29' TOTAL_AFTER_DISC_AMT.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '16'
*                                               DISC_PERC_TEXT.
*  ENDIF.
  move total_after_disc_amt to zinvoice01-hnet_val.
  move disc_perc_text to zinvoice01-hdisc_desc.

  clear header_tax.
  refresh header_tax.
  loop at tkomvd.
    komvd = tkomvd.
    if komvd-koaid = 'D'.
      move-corresponding komvd to header_tax.
      collect header_tax.
    endif.
  endloop.

  tax_id = 32.
  loop at header_tax.
    add 1 to tax_id.
    check header_tax-kwert ne 0.
    if tax_id ne 38.                   " gs - quick fix
      case tax_id.
        when 33.
          move header_tax-kwert to tax1.
        when 34.
          move header_tax-kwert to tax2.
        when 35.
          move header_tax-kwert to tax3.
        when 36.
          move header_tax-kwert to tax4.
        when 37.
          move header_tax-kwert to tax5.
      endcase.
*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '01' '000001'
*                                         TAX_ID    HEADER_TAX-KWERT.
*      ENDIF.
    endif.                             " gs - quick fix
  endloop.

**********************************************************  start of jb2
* Incorrect Discount Entered (positive value).
  if zdsc_pos_disc_flag = 'X'.
    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'A positive discount has been entered'.
    err_msg3 = 'for an item using the condition ZDSC.'       .
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no1 to textline+20.
    err_msg4 = textline.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    clear textline.
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'A positive discount has been entered' to zinvoice01-bp_addr2.
    move 'for an item using the condition ZDSC.' to zinvoice01-bp_addr3.
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no1 to textline+20.
    move textline to zinvoice01-bp_addr4.
*

* PB00 and PB01 occur together on same item.
  elseif pb00_pb01_err_flag = 'X'.
    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'The conditions PB00 and PB01 are not'.
    err_msg3 = 'allowed together on the same item.'.
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no2 to textline+20.
    err_msg4 = textline.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    clear textline.
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'The conditions PB00 and PB01 are not' to zinvoice01-bp_addr2.
    move 'allowed together on the same item.' to zinvoice01-bp_addr3.
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no2 to textline+20.
    move textline to zinvoice01-bp_addr4.
*

* Double HM00 condition
  elseif dbl_hm00_err_flag = 'X'.
    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'There are multiple HM00 conditions'.
    err_msg3 = 'on this document.'.
    err_msg4 = 'Only one HM00 condition is allowed.'.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'There are multiple HM00 conditions' to zinvoice01-bp_addr2.
    move 'on this document.' to zinvoice01-bp_addr3.
    move 'Only one HM00 condition is allowed.' to zinvoice01-bp_addr4.
*

* HM00 and ZDSC exist on the same item.
  elseif zdsc_hm00_err_flag = 'X'.
    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'Condition HM00 is not allowed with'.
    err_msg3 = 'condition ZDSC.'       .
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no3 to textline+20.
    err_msg4 = textline.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    clear textline.
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'Condition HM00 is not allowed with' to zinvoice01-bp_addr2.
    move 'condition ZDSC.' to zinvoice01-bp_addr3.
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no3 to textline+20.
    move textline to zinvoice01-bp_addr4.
*

* ZBJS with either ZDBP, PB00, PB01
  elseif dual_cond_err_flag = 'X'.

    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'Condition ZBJS is not allowed with'.
    err_msg3 =  'either PB00, PB01, or ZDBP.'       .
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no4 to textline+20.
    err_msg4 = textline.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    clear textline.
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'Condition ZBJS is not allowed with' to zinvoice01-bp_addr2.
    move  'either PB00, PB01, or ZDBP.' to zinvoice01-bp_addr3.
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no4 to textline+20.
    move textline to zinvoice01-bp_addr4.
*

* Item in category TAS or ZAS with condition ZDSC.
  elseif tas_zas_zdsc_error_flag = 'X'.
    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'Condition type ZDSC is not allowed for'.
    err_msg3 = 'items in category TAS or ZAS'       .
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no5 to textline+20.
    err_msg4 = textline.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    clear textline.
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
   move 'Condition type ZDSC is not allowed for' to zinvoice01-bp_addr2.
    move 'items in category TAS or ZAS.' to zinvoice01-bp_addr3.
    move 'Check SAP Item No:  ' to textline.
    move bad_item_no5 to textline+20.
    move textline to zinvoice01-bp_addr4.
*

***********************************************************   end of jb2
* Item Total does not match Invoice Total.
  elseif hdr_net_bef_tax_surchg ne item_total_after_disc.   "gs4

    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'Invoice total is different from Item total'.
    move 'Invoice total: ' to textline.
    move hdr_net_bef_tax_surchg to textline+18.
    err_msg3 = textline.
    move '   Item total: ' to textline.
    move item_total_after_disc to textline+18.
    err_msg4 = textline.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    clear textline.
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'Invoice total is different from Item total'
                                               to zinvoice01-bp_addr2.
    move 'Invoice total: ' to textline.
    move hdr_net_bef_tax_surchg to textline+18.
    move textline to zinvoice01-bp_addr3.
    move '   Item total: ' to textline.
    move item_total_after_disc to textline+18.
    move textline to zinvoice01-bp_addr4.
*

* SHIP-TO on invoice does not match SHIP-TO (TAX AREA) on other docs jb5
  elseif shipto_diff_flag = 'X'.
    err_msg1 = '***********     DO NOT MAIL     ***********'.
    err_msg2 = 'SHIP-TO/TAX AREA partner is different'.
    err_msg3 = 'on preceding documents (SO / Delivery).'.
    err_msg4 = 'Related documents need to be changed.'.
    perform print_invoice_header_error using
                                  err_msg1 err_msg2 err_msg3 err_msg4.
* CF
    move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'SHIP-TO/TAX AREA partner is different' to zinvoice01-bp_addr2.
    move 'on preceding documents (SO / Delivery).'
                                                 to zinvoice01-bp_addr3.
    move 'Related documents need to be changed.'
                                                 to zinvoice01-bp_addr4.
*
*{ LABK934392
  elseif nofobtext_err_flag = 'X'.
        move '***** DO NOT MAIL *****' to zinvoice01-bp_addr1.
    move 'FOB item text was not found' to zinvoice01-bp_addr2.
    move ' '                      to zinvoice01-bp_addr3.
    move ' '                      to zinvoice01-bp_addr4.
*} LABK934392
  else.
*   write bill to address
    perform write_bill_to_party_info(zvprint3)
                                 using vbdkr-vbeln '01' '000001'
                                       zinvoice01-bp_num
                                       zinvoice01-bp_name
                                       zinvoice01-bp_addr1
                                       zinvoice01-bp_addr2
                                       zinvoice01-bp_addr3
                                       zinvoice01-bp_addr4
                                       zinvoice01-bp_pcode
                                       zinvoice01-bp_city
                                       zinvoice01-bp_region
                                       zinvoice01-bp_country.
*                                       PREVIEW.
  endif.

* Begin of gh3 -->
* Get the CO2 Surcharge if there is any
  clear: zinvoice01-co2_surcharge,
         zinvoice01-co2_surcharge_x.

  loop at tkomv where kschl = 'ZCO2'
                  and kinak = space.
    add tkomv-kwert to zinvoice01-co2_surcharge.
  endloop.

  if not zinvoice01-co2_surcharge is initial.
    zinvoice01-co2_surcharge_x = 'X'.
  endif.
* <-- end of gh3
endform.


*---------------------------------------------------------------------*
*       FORM ITEM_PRICE_PRINT                                         *
*---------------------------------------------------------------------*
*       Printout of the item prices                                   *
*---------------------------------------------------------------------*

form item_price_print.

  clear: total_after_disc_amt, hm00_cond_flag.
* T.N      Sep 28, 1995
* TKOMV  - Price Determination Communications condition record
* TKOMVD - Price Determination Communications cond. record for printing
* Once again, we are at SAP's mercy as it decides what it will return
* in these tables. This time for item w/ zero amount and HM00 cond, it
* decides to return NOTHING for TKOMVD (What a piece of %#!%^&). The
* following piece code is designed to get around that problem.

  clear tkomvd_lines.

  loop at tkomvd.
    if tkomvd-kposn = vbdpr-posnr.
      tkomvd_lines = 1.                "  Table is not EMPTY
      exit.
    endif.
  endloop.

  if tkomvd_lines = 0.                 "  Table is EMPTY
    clear tkomvd.
    refresh tkomvd.
*   We'll copy information from TKOMV to TKOMVD for this item
    loop at tkomv.
      check tkomv-kposn = vbdpr-posnr.
      move-corresponding tkomv to tkomvd.
      append tkomvd.
    endloop.
  endif.

  clear c02_new_cond_flag.                                   "LABK929819

  loop at tkomvd.
*{ Start Insert LABK934576
    IF tkomvd-stunr = fuel_surcharge.
      CONTINUE.
    ENDIF.
*} End Insert LABK934576
    clear item_data-cond_type.
    komvd = tkomvd.
*   Obtain various values
    if komvd-stunr = opr_zyrt.                                     "gs10
*     handles zryt condition type                                   "gs5
      total_after_disc_amt = komvd-kwert.                           "gs5
      add komvd-kwert to book_total.                                "gs5
*      IF PREVIEW = 'NO'.                                            "cf
*       PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '02'   ITEM_COUNT    "gs5
*                            '25' KOMVD-KWERT.                      "gs5
*      ENDIF.
      move komvd-kwert to item_data-book_val.
      zryt_flag = 1.                                                "gs5
    elseif komvd-stunr = item_gross_value or    "  Mutually exclusive
      komvd-stunr = item_fixed_price
*   Begin insert LABK929819 -->
*   Make sure the c02 line (with new stunr = 425)
*   is evaluated so we can get a net value just like
*   other regular lines. 398 is evaluated as a zbjs item.
      or komvd-stunr = c02_cond_new
      or komvd-stunr = c02_cond_old.
        item_data-cond_type = 'c02'.                        "LABK929500
        if komvd-stunr = c02_cond_new or
           komvd-stunr = c02_cond_old.
          c02_new_cond_flag = 'X'.
        endif.
        if komvd-stunr <> c02_cond_old.
*   -->   End insert LABK929819
          total_after_disc_amt = komvd-kwert.
          add komvd-kwert to book_total.
*        IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FI  LEA(ZVPRINT3) USING '02'   ITEM_COUNT
*                             '25' KOMVD-KWERT.
*        ENDIF.
        move komvd-kwert to item_data-book_val.
      endif.                                                "LABK929819
*   Start insert LABK930236
*   'No-charge' items cancel out gross value
*    elseif komvd-stunr = opr_nocharge.
*      add komvd-kwert to book_total.
*      add komvd-kwert to total_after_disc_amt.
*      add komvd-kwert to item_data-book_val.
*   End insert LABK930236
    elseif komvd-stunr = hm00_cond.
      hm00_cond_flag = 'Y'.
      hm00_header_cond_flag = 'Y'.
      add komvd-kwert to book_total.
      total_after_disc_amt = komvd-kwert.
*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '02'   ITEM_COUNT
*                                                '25' KOMVD-KWERT.
*      ENDIF.
      move komvd-kwert to item_data-book_val.
*     Get HM00 condition type RV 950923
*
*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT
*                                                 '16' 'HM00'.
*      ENDIF.
      move 'HM00' to item_data-cond_type.
*   Begin insert LABK929819 -->
*   If ZBJS condition comes before HM00 (older invoices before
*   the c02 config changes would have this.  In which case, if they're
*   HM00, we want all items to be included in fixed price with no
*   separating out of the c02's into additional items. So for the
*   c02, don't process any other condition types.
      if not c02_new_cond_flag is initial.
        exit.
      endif.
*   --> End insert LABK929819

    endif.
  endloop.

endform.

*---------------------------------------------------------------------*
*       FORM ITEM_PRINT                                               *
*---------------------------------------------------------------------*
*       Printout of the items                                         *
*---------------------------------------------------------------------*

form item_print.
  data: material_no like vbdpr-matnr,
        temp_disc type f.

  clear itemtab.                                            "JL9606
  refresh itemtab.                                          "JL9606

* Clear Header level flags.
  clear: zdsc_pos_disc_flag,                                "jb2
         pb00_pb01_err_flag,                                "jb2
         dbl_hm00_err_flag,                                 "jb2
         zdsc_hm00_err_flag,                                "jb2
         dual_cond_err_flag,                                "jb2
         tas_zas_zdsc_error_flag,                           "jb2
         nofobtext_err_flag,                                "LABK934392
         bad_item_no1,                                      "jb2
         bad_item_no2,                                      "jb2
         bad_item_no3,                                      "jb2
         bad_item_no4,                                      "jb2
         bad_item_no5.                                      "jb2

  item_total_after_disc = 0.

*{LABK934226
* Get the Customer ID Number for this invoice
  SELECT SINGLE kunnr FROM vbpa
         INTO g_cust_num
         WHERE vbeln = vbdkr-vbeln
           AND posnr = '000000'
           AND parvw = 'AG'.

  TRANSLATE nast-dsnam TO UPPER CASE.

  IF vbdkr-vbtyp = 'M'.           "for invoices only
    LOOP AT tvbdpr.
      CHECK tvbdpr-pstyv NE 'TAQ'.
      MOVE-CORRESPONDING tvbdpr TO tvbap.
      APPEND tvbap.
    ENDLOOP.
    fob_posnr = '999999'.
    fob_blend = ' '.
  ENDIF.
*}LABK934226

  loop at tvbdpr.
    clear: itax1,itax2, itax3, itax4, itax5, item_data.

    vbdpr = tvbdpr.

*   Make sure a material number exists. If not, do not download to file.
    check not vbdpr-matnr is initial.  "jb6

    select single * from vbrp where
                  vbeln = vbdkr-vbeln and
                  posnr = vbdpr-posnr.

* Clear item level flags.
    clear:   tas_zas_flag,                                  "jb2
             zdsc_cnt_flag,                                 "jb2
             pb00_cnt_flag,                                 "jb2
             pb01_cnt_flag,                                 "jb2
             zdbp_cnt_flag,                                 "jb2
             hm00_cnt_flag.                                 "jb2
*   set zryt flag to zero (assume it does not exist)                "gs5
    zryt_flag = 0.                                          "gs5
*   set net value printed flag to zero so that is only prints one   "gs8
    net_printed = 0.                                        "gs8
*   set zbjs flag to zero (assume it does not exist)               "gs10
    zbjs_flag = 0.                     "gs10

* Get the item text from sales order item.                 "LABK926388
    select single * from vbap                              "LABK926388
      where vbeln = vbdpr-vbeln_vauf                       "LABK926388
        and posnr = vbdpr-posnr_vauf.                      "LABK926388
    vbdpr-arktx = vbap-arktx.                              "LABK926388

*   strip out leading zero's of material no
    write vbdpr-matnr to material_no no-zero.
    condense material_no.
    move vbdpr-arktx to itemtab-arktx.                      "JL1204
    perform get_item_prices.

    if hm00_exists = ' '.              " only loop through this once
      loop at tkomvd. " determine if HM00 cond type exists
        komvd = tkomvd.
      endloop.
    endif.

    if vbrp-pstyv ne 'TAQ'.                                 "gs6
* suppress group item lines, higher-level item NE 0.
      item_count = item_count + 1.
*   Get Material no/ material description
*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT
*                                                '10' MATERIAL_NO.
*        PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT
*                                                '13' VBDPR-ARKTX.
*      ENDIF.
      move material_no to item_data-mat_num.
      move vbdpr-matnr to item_data-matnr.
      move vbdpr-arktx to item_data-mat_desc.

*   Get Item Quantity (invoiced qty)
*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT
*                                                '11' VBDPR-FKIMG.
*      ENDIF.
      move vbdpr-fkimg to item_data-quantity.

*   Get Item Unit of Measure
*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT
*                                                '12' VBDPR-VRKME.
*      ENDIF.
      move vbdpr-vrkme to item_data-uom.

*   Get Item category
*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT '14'
*                                                          VBRP-PSTYV.
*      ENDIF.
      move vbrp-pstyv to item_data-category.

      if vbrp-pstyv = 'ZATX'.
        clear: zatx_netwr_sum, zatx_surchg, zatx_net_no_surchg,
               komvd-kwert, nows_disc_perc, nows_disc_amt.

        select * from vbrp into *vbrp where vbeln = vbrp-vbeln and
                 uepos = vbrp-aupos.
          add *vbrp-netwr to zatx_netwr_sum.
          add *vbrp-kzwi1 to komvd-kwert.
*   acc. surcharge of the detail line, can use this to acc other amts.
          perform get_pricing_proc(zvprint3)
                   tables tkomv tax_table   using
                   nows_disc_amt    nows_disc_perc
                   price_adj_amt
                   3party_surchg_amt
                   prov_surchg_amt
                   mat_reimb_amt       "950923 RV
                  *vbrp-posnr
                   zbjs_amt            "gs10
                   nows_spec_disc_amt.                      "LABK918227
          add prov_surchg_amt to zatx_surchg.
          add mat_reimb_amt   to zatx_surchg.               "950923 RV
        endselect.

        add komvd-kwert to book_total.
        total_after_tax = zatx_netwr_sum.
        zatx_net_no_surchg = zatx_netwr_sum - zatx_surchg.
*      ADD ZATX_NET_NO_SURCHG TO ITEM_TOTAL_AFTER_DISC.             "gs2
        add zatx_netwr_sum to item_total_after_disc.        "gs2
        nows_disc_amt = ( komvd-kwert - zatx_net_no_surchg ) * -1.
        temp_disc = ( 1 - ( zatx_net_no_surchg / komvd-kwert ) ) * 100.

*      Why * by 100? There seems to be a problem of converting between
*      float type and nows_disc_perc type (it would be 0.xx without
*      * by 100). I don't know why that is the case!
        nows_disc_perc = temp_disc * 100.
*        IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '02' ITEM_COUNT
*                                                 '32' ZATX_NETWR_SUM.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '02' ITEM_COUNT
*                                                 '25' KOMVD-KWERT.
*        ENDIF.
        move zatx_netwr_sum to item_data-tt_af_sur.
        move komvd-kwert to item_data-book_val.

       if hm00_exists ne 'Y'. " Only write if there is no HM00 cond type
          if net_printed = 0.                                     "gs8
*            IF PREVIEW = 'NO'.
*              PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '02'   ITEM_COUNT
*                                    '29' ZATX_NET_NO_SURCHG.
*            ENDIF.
            move zatx_net_no_surchg to item_data-net_val.
            net_printed = 1.                                       "gs8
          endif.                                                   "gs8
        else.
          if net_printed = 0.                                      "gs8
*            IF PREVIEW = 'NO'.
*              PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02'   ITEM_COUNT
*                                 '29' '           '.
*            ENDIF.
            move '           ' to item_data-net_val.
            net_printed = 1.                                       "gs8
          endif.                                                   "gs8
        endif.

*      if zryt condition type exist for this item,                  "gs5
*      blank out the discount percentage                            "gs5
       if zryt_flag eq 0 and zbjs_flag eq 0.                       "gs10
*         IF PREVIEW = 'NO'.
*           PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                                  '24' NOWS_DISC_PERC.
*         ENDIF.
         move nows_disc_perc to item_data-disc_per.
       else .                                                      "gs5
*         IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT  "gs5
*                                                  '24' ' '.        "gs5
*         ENDIF.
         move ' ' to item_data-disc_per.
         if zbjs_flag ne 0 and hm00_cond_flag ne 'Y'.              "gs10
*            IF PREVIEW = 'NO'.
*            PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT "gs10
*                                                   '16' 'ZBJS'.   "gs10
*            ENDIF.
            move 'ZBJS' to item_data-bj_cond.
          endif.                                                   "gs10
        endif.                                                      "gs5
*        IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                                '26' NOWS_DISC_AMT.
*        ENDIF.
        move nows_disc_amt to item_data-disc_amt.
        if nows_disc_perc ne 0.
          disc_perc_text = 'Disc. %'.
        endif.
      else.
*   write Item Net Value 2 to file
        add vbdpr-netwr to item_total_after_disc.                   "gs2
*        IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '02' ITEM_COUNT
*                                               '32' VBDPR-NETWR.
*        ENDIF.
        move vbdpr-netwr to item_data-tt_af_sur.
        total_after_tax = vbdpr-netwr.
      endif.

      perform item_price_print.

*{LABK934226
* Identify the FOB parent and the items belonging to the same FOB
      IF vbdkr-vbtyp = 'M'               "Invoices Only
        AND g_cust_num = '0001116370'    "Berlington Only
        AND job_type_no >= 100           "Cementing Jobs only
        AND job_type_no <= 199
        AND nast-dsnam <> 'OLD'.         "New Items Only

        fob_parent = 0.
        PERFORM determine_parent USING vbdpr-posnr
                                         vbdpr-uepos
                                         fob_parent.
*     if we have an fob parent, check to see if there is an fob
*     description to use in place of the regular material description
        IF fob_parent EQ 1.
          CLEAR textline.
*         PERFORM get_item_text USING vbdkr-vbeln vbdpr-posnr
*         Get item text from the Sales Order, NOT THE QUOTE
          PERFORM get_item_text USING vbdkr-vbeln_vauf vbdpr-posnr_vauf
                                      '0053' textline.
          IF NOT textline IS INITIAL.
            fob_blend = textline.
          ELSE.                                            "LABK934392
            fob_blend = 'X'.                               "LABK934392
            nofobtext_err_flag = 'X'.                      "LABK934392
          ENDIF.
          fob_posnr = vbdpr-posnr.
        ENDIF.

        IF vbdpr-uepos EQ fob_posnr.
*         This is an FOB detail line
          MOVE tvbdpr-uepos TO item_data-uepos.
          MOVE tvbdpr-posnr TO item_data-posnr.
          MOVE fob_blend    TO item_data-mat_desc.
        ELSE.
*         This might be an FOB parent and/or FOB detail line
          tvbdpr-posnr = '000000'.
          IF vbdpr-posnr EQ fob_posnr.
            tvbdpr-arktx = fob_blend.
            tvbdpr-posnr = fob_posnr.
          ENDIF.
*         Make sure it is a detail line
          IF tvbdpr-posnr EQ fob_posnr.
            MOVE fob_posnr TO tvbdpr-uepos.
            MOVE fob_blend TO item_data-mat_desc.
          ENDIF.
          MOVE tvbdpr-uepos TO item_data-uepos.
          MOVE tvbdpr-posnr TO item_data-posnr.
        ENDIF.
      ENDIF.
*}LABK934226

*   Get item taxes
      perform get_pricing_proc(zvprint3) tables tkomv tax_table   using
                     nows_disc_amt    nows_disc_perc
                     price_adj_amt
                     3party_surchg_amt
                     prov_surchg_amt
                     mat_reimb_amt
                     vbdpr-posnr
                     zbjs_amt            "gs10
                     nows_spec_disc_amt.                     "LABK918227

      if hm00_cond_flag = 'Y'.
        clear: nows_disc_amt, nows_disc_perc, price_adj_amt,
               3party_surchg_amt, prov_surchg_amt, mat_reimb_amt,
                   zbjs_amt,            "gs10
                   nows_spec_disc_amt.                       "LABK918227
      endif.

      if nows_disc_perc ne 0.
        disc_perc_text = 'Disc. %'.
      endif.

      if vbrp-pstyv ne 'ZATX'.
*
*      if zryt condition type exist for this item,                  "gs5
*      blank out the discount percentage                            "gs5
       if zryt_flag eq 0 and zbjs_flag eq 0.                       "gs10
*         IF PREVIEW = 'NO'.
*           PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                                  '24' NOWS_DISC_PERC.
*         ENDIF.
         move nows_disc_perc to item_data-disc_per.
        else.                                                       "gs5
*         IF PREVIEW = 'NO'.
*         PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT   "gs5
*                                                 '24' ' '.         "gs5
*         ENDIF.
         move ' ' to item_data-disc_per.
         if zbjs_flag ne 0 and hm00_cond_flag ne 'Y'.              "gs10
*           IF PREVIEW = 'NO'.
*           PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02' ITEM_COUNT  "gs10
*                                                  '16' 'ZBJS'.    "gs10
*           ENDIF.
           move 'ZBJS' to item_data-bj_cond.
          endif.                                                   "gs10
        endif.                                                     "gs5
*        IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                                  '26' NOWS_DISC_AMT.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                                  '27' PRICE_ADJ_AMT.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                                '28' 3PARTY_SURCHG_AMT.
*        ENDIF.
        move nows_disc_amt to item_data-disc_amt.
        move price_adj_amt to item_data-price_ad.
        move 3party_surchg_amt to item_data-3pty_sur.
      endif.
      if vbrp-pstyv = 'ZFOB' or vbrp-pstyv = 'AGNN' or
         vbrp-pstyv = 'ZFOC' or vbrp-pstyv = 'TANN'.
        if net_printed = 0.                                        "gs8
*          IF PREVIEW = 'NO'.
*            PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02'   ITEM_COUNT
*                                     '29' '           '.
*          ENDIF.
          move '        ' to item_data-net_val.
          net_printed = 1.                                         "gs8
        endif.                                                     "gs8
      endif.

      if vbrp-pstyv ne 'ZATX'.
        if c02_new_cond_flag = ' '.                        "LABK929500
          total_after_disc_amt = total_after_disc_amt +
                  nows_disc_amt + price_adj_amt + 3party_surchg_amt
                + zbjs_amt.              "gs10
        else.                                               "LABK929500
          total_after_disc_amt = total_after_disc_amt +     "LABK929500
          nows_disc_amt + price_adj_amt + 3party_surchg_amt."LABK929500
        endif.                                              "LABK929500
       if hm00_exists ne 'Y'. " Only write if there is no HM00 cond type
          if net_printed = 0.                               "gs8
*            IF PREVIEW = 'NO'.
*              PERFORM WRITE_TO_FILEA(ZVPRINT3) USING '02'   ITEM_COUNT
*                                             '29' TOTAL_AFTER_DISC_AMT.
*            ENDIF.
            move total_after_disc_amt to item_data-net_val.
            net_printed = 1.                                "gs8
          endif.                                            "gs8
        else.
          if net_printed = 0.                               "gs8
*            IF PREVIEW = 'NO'.
*              PERFORM WRITE_TO_FILE(ZVPRINT3) USING '02'   ITEM_COUNT
*                                  '29' '           '.
*            ENDIF.
            move '          ' to item_data-net_val.
            net_printed = 1.                                "gs8
          endif.                                            "gs8
        endif.
*     ADD TOTAL_AFTER_DISC_AMT TO ITEM_TOTAL_AFTER_DISC.            "gs2
      endif.

*      if vbrp-pstyv = 'ZTAP' or vbrp-pstyv = 'TANN' or     "LABK930236
*         vbrp-pstyv = 'ZFOC'.                              "LABK930236
*        item_data-book_val = item_data-net_val = 0.        "LABK930236
*      endif.                                               "LABK930236

***************************************************************start jb2
* This section is used to determine if the invoice violates any
* pricing conditions. GET_PRICING_INFO is used to fill the
* PRICING_INFO table with the pricing condition values
* (TYPE, VALUE, COUNTER).
* This section should be used to add any other pricing condition
* violations that need to be flagged with DO NOT MAIL (using the
* subroutine PRINT_INVOICE_HEADER_ERROR   .
* NOTE: for each new condition that is added, 2 new flag variables
*       need to be created. One as a counter, one as an indicator to
*       flag with DO NOT MAIL.
*       A new BAD_ITEM_NO# variable needs to be created
*       where # is the next number in the sequence of violations.
*       e.g. as of Feb 3, 1997 the number of violation conditions
*       is up to 5.
*       New error messages for DO NOT MAIL need to be created in
*       the subroutine HEADER_PRICE_PRINT.


      perform get_pricing_info(zvprint3) using vbrp-vbeln vbrp-posnr
              dum_rc dum_post_amt dum_pbook dum_cond_amt dum_cond_type .


      loop at pricing_info.
        case pricing_info-condition_type.
          when 'PB00'.
            if pricing_info-condition_amount gt 0.
              add 1 to pb00_cnt_flag.
            endif.
          when 'PB01'.
            if pricing_info-condition_amount gt 0.
              add 1 to pb01_cnt_flag.
            endif.
          when 'ZDBP'.
            if pricing_info-condition_amount gt 0.
              add 1 to zdbp_cnt_flag.
            endif.
          when 'HM00'.
            if pricing_info-condition_amount gt 0.
              add pricing_info-condition_counter to hm00_cnt_flag.
            endif.
          when 'ZDSC'.
            if pricing_info-condition_amount lt 0.
              add 1 to zdsc_cnt_flag.
            elseif pricing_info-condition_amount gt 0. "Positive disc.
              zdsc_pos_disc_flag = 'X'.
              write vbdpr-posnr to bad_item_no1 no-zero.
              condense bad_item_no1 .
            endif.
        endcase.
      endloop.

*     Only perform checks if document is a Pro Forma or an Invoice  jb2
      if ( vbdkr-vbtyp = 'M' or vbdkr-vbtyp = 'U' )
*     and the document # is greater than last one created April 4/97 jb4
          and vbdkr-vbeln gt '0071041090'.

*       Check if item is in category TAS or ZAS                     jb2
        if vbrp-pstyv = 'TAS' or  vbrp-pstyv = 'ZAS'.
          tas_zas_flag = 'X'.
          write vbdpr-posnr to bad_item_no5 no-zero.
          condense bad_item_no5 .
        endif.

*       Check if PB00 and PB01 are together on same item.          jb2
        if pb00_cnt_flag gt 0 and pb01_cnt_flag gt 0.
          write vbdpr-posnr to bad_item_no2 no-zero.
          condense bad_item_no2 .
          pb00_pb01_err_flag = 'X'.
        endif.

*       Check if there is a double HM00 condition.    jb2
        if hm00_cnt_flag gt 1.
          dbl_hm00_err_flag = 'X'.
        endif.

*       Check for HM00 and ZDSC on the same item.        jb2
        if hm00_cnt_flag gt 0 and zdsc_cnt_flag gt 0.
          write vbdpr-posnr to bad_item_no3 no-zero.
          condense bad_item_no3 .
          zdsc_hm00_err_flag = 'X'.
        endif.

*       Check for ZDSC_CNT_FLAG and TAS_ZAS_FLAG.                jb2
        if zdsc_cnt_flag gt 0 and tas_zas_flag = 'X'.
          tas_zas_zdsc_error_flag = 'X'.
        endif.

*       Check if item is in category ZBJS along with PB00, PB01, ZDBP
        if zbjs_flag gt 0
           and ( pb00_cnt_flag gt 0 or pb01_cnt_flag gt 0
                                        or zdbp_cnt_flag gt 0 ).
          write vbdpr-posnr to bad_item_no4 no-zero.
          condense bad_item_no4 .
          dual_cond_err_flag = 'X'.
        endif.
      endif.

**************************************************************** end jb2

      if prov_surchg_amt ne 0.
*        IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                               '30' PROV_SURCHG_AMT.
*        ENDIF.
        move prov_surchg_amt to item_data-prov_sur.
      endif.

      if mat_reimb_amt ne 0.           "950923 RV
*        IF PREVIEW = 'NO'.
*     PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT "950923 RV
*                                         '40' MAT_REIMB_AMT. "950923 RV
*        ENDIF.
        move mat_reimb_amt to item_data-mat_reim.
      endif.                           "950923 RV

      tax_id = 32.
      loop at tax_table.
        add 1 to tax_id.
        add tax_table-tax_amount to total_after_tax.
*        IF PREVIEW = 'NO'.
*          PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                       TAX_ID  TAX_TABLE-TAX_AMOUNT.
*        ENDIF.
        case tax_id.
          when 33.
            move tax_table-tax_amount to itax1.
          when 34.
            move tax_table-tax_amount to itax2.
          when 35.
            move tax_table-tax_amount to itax3.
          when 36.
            move tax_table-tax_amount to itax4.
          when 37.
            move tax_table-tax_amount to itax5.
        endcase.
      endloop.

*      IF PREVIEW = 'NO'.
*        PERFORM WRITE_TO_FILEA(ZVPRINT3)  USING '02' ITEM_COUNT
*                                              '38' TOTAL_AFTER_TAX.
*      ENDIF.
      MOVE item_data-mat_desc to itemtab-arktx.             "LABK934392
      move total_after_tax to item_data-fin_amt.
      move item_count to itemtab-item_count.                "JL9608
      move material_no   to itemtab-material_no.            "JL9608
                                       "JL1204 arktx added
      append itemtab.                                       "JL9608
    endif.

* Used to put a '*' in front of items that have no T related tax
    if itax2 ne 0.
      move '*' to item_data-tax_flag.
    else.
      move ' ' to item_data-tax_flag.
    endif.

* Save item data to internal table
    move item_count to item_data-count.
   if item_data-category ne 'ZON'.
     check item_data-mat_desc ne ' '.
      append item_data.
   endif.

  endloop.
*{ Start Insert LABK934392
  SORT item_data BY mat_num uepos descending posnr descending.
*} End Insert LABK934392
  if quote_flag = 1.                                        "JL9605
    if vbdkr-vbtyp <> 'O'.    "Don't need quote info for Credit Memos
*{Start insert LABK934392
      IF vbdkr-vbtyp = 'M'                "Invoices Only
         AND g_cust_num = '0001116370'    "Berlington Only
         AND job_type_no >= 100           "Cementing Jobs only
         AND job_type_no <= 199
         AND nast-dsnam <> 'OLD'.         "New Items Only

        perform get_quantity_quoted tables  itemtab item_data
                       using    quotation  item_count.
      ELSE.
*} End insert LABK934392
        perform write_data_to_access tables  itemtab item_data  "JL
                       using    quotation  item_count.         "JL9605
*                                PREVIEW.
      ENDIF.
    endif.
  endif.                                                    "JL9605

*{ Start Insert LABK934392            ------------------------
* Collect the FOB items together with the parent
  data: l_fobparent LIKE item_gr1-posnr,
        fob_inhand.

  IF vbdkr-vbtyp = 'M'                "Invoices Only
     AND g_cust_num = '0001116370'    "Berlington Only
     AND job_type_no >= 100           "Cementing Jobs only
     AND job_type_no <= 199
     AND nast-dsnam <> 'OLD'.         "New Items Only

    REFRESH item_tmp.
    SORT item_data BY uepos posnr.

* The list Unit Price for the FOB = total gross price/parent Qty used
* The Disc Unit Price for the FOB = total net price / parent Qty used
    LOOP AT item_data.
      IF NOT item_data-uepos IS INITIAL.
        IF item_data-posnr = item_data-uepos.
          l_fobparent = item_data-uepos.
          IF fob_inhand = 'X'.
            APPEND item_tmp.
            CLEAR fob_inhand.
          ENDIF.
          item_tmp = item_data.
          fob_inhand = 'X'.
        ELSEIF l_fobparent = item_data-uepos.
          fob_inhand = 'X'.
          ADD: item_data-tt_af_sur TO item_tmp-tt_af_sur,
               item_data-net_val   TO item_tmp-net_val,
               item_data-disc_amt  TO item_tmp-disc_amt,
               item_data-price_ad  TO item_tmp-price_ad,
               item_data-3pty_sur  TO item_tmp-3pty_sur,
               item_data-prov_sur  TO item_tmp-prov_sur,
               item_data-mat_reim  TO item_tmp-mat_reim,
               item_data-fin_amt   TO item_tmp-fin_amt.
        ENDIF.
      ELSE.
        APPEND item_data TO item_tmp.
        CLEAR fob_inhand.
      ENDIF.
    ENDLOOP.
    IF fob_inhand = 'X'.
      APPEND item_tmp.
    ENDIF.
    REFRESH item_data.
    item_data[] = item_tmp[].
    SORT item_data BY matnr.
  ENDIF.
*} End Insert LABK934392 -----------------------------

endform.
* To prepare data for reformatting sales order
*
include zvrefsi2.                                           "JL9605


*---------------------------------------------------------------------*
*       FORM SENDER                                                   *
*---------------------------------------------------------------------*
*       This routine determines the address of the sender (Table VKO) *
*---------------------------------------------------------------------*

form sender.

  select single * from tvko  where vkorg = vbdkr-vkorg.
  check sy-subrc = 0.
  select single * from sadr where adrnr = tvko-adrnr
                            and   natio = space.
  vbdkr-sland = sadr-land1.

endform.


************************************************************************
*      FORM WRITE_HEADER_INFO_TO_FILE
************************************************************************
* - This routine writes any header data to the file that could not be
*   written in the code already given.
*
************************************************************************
form write_header_info_to_file.

*  DATA: LENGTH TYPE I, OUT_FNAME(12), TITLE(35).
  data: length type i, title(35).
***JL        QUOTE_FLAG TYPE I VALUE 0.

  data: l_ref_vbeln like vbdkr-vbeln.                      "LABK928636

  clear quote_flag.                                        "JL
  item_count = 0.
  length = strlen( vbdkr-vbeln ) - 8.
  assign vbdkr-vbeln+length(8) to .
*  FILENAM = .
** to get path from FILENAME
**check 'department' field for drop zone indicator                  "gs7
*  IF PREVIEW = 'NO'.
*
*  PERFORM GET_PATH_NAME(ZVPRINT3) USING                 "gs7
*          PATH 'INVBCK' CHANGING RETCODE.                "gs7
*  IF RETCODE NE 0.
*    SUBMIT ZCALLERR WITH RET_CODE = RETCODE
*                    WITH MAIN_PGM = SY-REPID.
*    EXIT.
*  ENDIF.
*  PERFORM OPEN_FILE(ZVPRINT3) USING FILENAM OUT_FNAME
*                                       PATH.
*
*  ENDIF.          "if preview = 'no'

* Get document title
* These titles can be found in layout set RVINVOICE01.
  case vbdkr-vbtyp.
    when 'M'.
      title = 'Invoice'.
      perform get_document_title using title doc_type.
    when 'N'.
      perform get_document_title using title  doc_type.
      title = 'Reverse Document'.
    when 'O'.
      title = 'Credit Memo'.
      perform get_document_title using title  doc_type.
    when 'P'.
      title = 'Debit Memo'.
      perform get_document_title using title   doc_type.
    when 'S'.
      perform get_document_title using title  doc_type.
      title = 'Reverse Credit Memo'.
    when 'U'.
      perform get_document_title using title   doc_type.
      title = 'Pro Forma Invoice'.     " No Draft/Copy prefix required
    when others.
      title = 'Invoice'.
  endcase.

*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '01' TITLE.
*  ENDIF.

* concatenate title '(Do Not Use)' into title separated by ' '.
  move title to zinvoice01-title.

* Get Sales Order
  clear: sales_ord, quote.
  refresh: sales_ord, quote.
  perform get_inv_related_docs(zvprint3) tables sales_ord quote
                              using vbdkr-vbeln   doc_type.
  read table sales_ord index 1.  " Only use first sales ord for now
  move sales_ord-vbeln to vbdkr-vbeln_vauf.

* Get Invoice number / Invoice date
*  IF PREVIEW = 'NO'.
*  PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '06' VBDKR-VBELN.
*  PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '23' VBDKR-FKDAT.
*  ENDIF.
  move vbdkr-vbeln to zinvoice01-inv_num.
  move vbdkr-fkdat to zinvoice01-inv_date.

* Get Reference no/date, Delivery no/date, Order no/date
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                               '18' SALES_ORD-BSTNK.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                                '19' SALES_ORD-BSTDK.
*  ENDIF.
  move sales_ord-bstnk to zinvoice01-po_num.
  move sales_ord-bstdk to zinvoice01-so_date.

* Get all delivery notes
  perform get_del_notes tables del_note_tbl
                            using vbdkr-vbeln_vauf   del_notes.
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '05' DEL_NOTES.
*  ENDIF.
  concatenate 'Delivery Note: ' del_notes(36) into zinvoice01-delnotes.
  move del_notes+36  to zinvoice01-delnotes2.

*
* WRite out SAp Document type, F8, F2, S1, etc   RV 950923
*
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '78' DOC_TYPE.
*  ENDIF.
  move doc_type to zinvoice01-doc_type.
*
  if doc_type cs 'L2' or doc_type cs 'G2'.
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                                 '22' SALES_ORD-VDATU.
*    ENDIF.
    move sales_ord-vdatu to zinvoice01-del_date.
  else.
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                                   '22' VBDKR-LFDAT.
*    ENDIF.
    move vbdkr-lfdat to zinvoice01-del_date.
  endif.
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                               '04' VBDKR-VBELN_VAUF.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                               '09' VBDKR-AUDAT_VAUF.
*  ENDIF.
  move vbdkr-vbeln_vauf to zinvoice01-order_num.
  move vbdkr-audat_vauf to zinvoice01-order_date.

* Get invoice currency code
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                                 '15' VBDKR-WAERK.
*  ENDIF.
  move vbdkr-waerk to zinvoice01-curr_code.

* Get invoiced address*******ship to party
  perform write_ship_to_party_info_2(zvprint3)
                                 using vbdkr-vbeln '01' '000001'
                                       zinvoice01-dir1
                                       zinvoice01-dir2
                                       zinvoice01-cust_num
                                       zinvoice01-cust_name
                                       zinvoice01-sp_addr1
                                       zinvoice01-sp_addr2
                                       zinvoice01-sp_pcode
                                       zinvoice01-sp_city
                                       zinvoice01-sp_country
                                       zinvoice01-sp_region
                                       zinvoice01-sp_addr3
                                       zinvoice01-sp_addr4
                                       zinvoice01-dir3
                                       zinvoice01-acct_group
                                       zinvoice01-tax_exempt.
*                                       PREVIEW.

* find well name and location stored in name1 and location fields of
* ZB partner
* perform get_well_data(zvprint3) using vbdkr-vbeln '01' '000001'.  "gs9
* get location data using so number instead of invoice number       "gs9
  perform get_well_data(zvprint3) using                             "gs9
                                    vbdkr-vbeln_vauf '01' '000001'
                                        zinvoice01-bp_county
                                        zinvoice01-well_name
                                        zinvoice01-formation
                                        zinvoice01-zb_street
                                        zinvoice01-zb_country
                                        zinvoice01-zb_pcode
                                        zinvoice01-zb_region.
*                                        PREVIEW.

* Get Date of Service
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                               '21' SALES_ORD-VDATU.
*  ENDIF.
  move sales_ord-vdatu to zinvoice01-req_del_dt.

* Get job type description
  job_type_no = sales_ord-kvgr1.
*** IJ1 Replace Starts
*  select single * from tvv1t where spras = sy-langu and
*                                   kvgr1 = job_type_no.
  select single * from T25A2 where spras = sy-langu and
                                   kvgr1 = job_type_no.
*** IJ1 Replace Ends
*  IF PREVIEW = 'NO'.
** PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '07' TVV1T-BEZEI.
*   PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '07' "LABK926189
*                                         TVV1T-BEZEI+4(16). "LABK926189
*  ENDIF.
* MOVE TVV1T-BEZEI TO ZINVOICE01-JOB_TYPE.
  if vbdkr-vkorg = '9040' and                               "LABK931123
     vbdkr-fkdat >= '20011001'.                             "LABK931123
*** IJ1 Replace Starts
*    move tvv1t-bezei to zinvoice01-job_type.                "LABK931123
*  else.                                                     "LABK931123
*    move tvv1t-bezei+4(16) to zinvoice01-job_type.          "LABK926189
    move t25A2-bezek to zinvoice01-job_type.                "LABK931123
  else.                                                     "LABK931123
*    move t25A2-bezek+4(16) to zinvoice01-job_type.          "LABK926189
    move t25A2-bezek to zinvoice01-job_type.          "LABK931491
*** IJ1 Replace Ends
  endif.                                                    "LABK931123

* Write Tax Jurisdiction to file    jb3.

  select single * from vbpa where
                   parvw = 'WE' and
                   posnr = '000000' and
                   vbeln = vbdkr-vbeln.
*  IF PREVIEW = 'NO'.
*   PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '39' VBPA-KUNNR.
*  ENDIF.
  move vbpa-kunnr to tax_jcd.

* When printing invoices, check to make sure the SHIP-TO/TAX AREA
* is the same across all preceding documents. jb5
  if ( vbdkr-vbtyp = 'M' or vbdkr-vbtyp = 'U' ).                    "gh1
    perform check_for_diff_shipto.
  endif.

* write salesman name to file
  clear p0001.
  select single * from vbpa where
                   parvw = 'VE' and
                   posnr = '000000' and
                   vbeln = vbdkr-vbeln.

* This select statement modified with PA0001. LABKK902133 - jb1
* The move-corresponding is probably not necessary, but is kept in
* until it can be determined for sure that it is not req'd.

*  SELECT * FROM PREL WHERE INFTY = '0001' AND
*                           PERNR = VBPA-PERNR.
*    MOVE PREL+3 TO P0001.

  select * from pa0001 where pernr = vbpa-pernr.
    move-corresponding pa0001 to p0001.
  endselect.
*  IF PREVIEW = 'NO'.
*  PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '08' P0001-ENAME.
*  ENDIF.
  move p0001-ename to zinvoice01-salesman.

* Get quotation number
  clear quotation.                                          "JL9606
  read table quote     index 1.        " Only use first quote for now
* if sy-subrc = 0 and quote-auart = 'AG'.      "  Quotation type    "gh2
  if sy-subrc = 0 and ( quote-auart = 'AG' or quote-auart = 'ZZQT' ).
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001'
*                                            '02'    QUOTE-VBELN.
*    ENDIF.
    move quote-vbeln to zinvoice01-quote_num.
    quote_flag = 1.
    quotation = quote-vbeln.                                "JL9606
  endif.
  if quote_flag = 0.
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '02' '  '.
*    ENDIF.
    move '  ' to zinvoice01-quote_num.
  endif.

*  Get proposal number
* Check whether this doc's order was copied from another   "LABK928636
* and if so, retrace until we get to the original order    "LABK928636
* and get the text for this order.                         "LABK928636
  clear: l_ref_vbeln, textline.                            "LABK928636
  perform trace_ref_doc_text using '0004' changing         "LABK928636
                                   l_ref_vbeln.            "LABK928636
  if not l_ref_vbeln is initial.                           "LABK928636
    perform get_header_text(zvprint3) using l_ref_vbeln    "LABK928636
                                      '0004' textline.     "LABK928636
  else.                                                    "LABK928636
    perform get_doc_header_text using vbdkr-vbeln
        vbdkr-vbeln_vauf   quote-vbeln  '0004' textline.
  endif.                                                   "LABK928636

*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '03'
*                                               TEXTLINE.
*  ENDIF.
  move textline to zinvoice01-prop_num.

*  Get SO ticket no
  perform get_doc_header_text using
        vbdkr-vbeln     vbdkr-vbeln_vauf   quote-vbeln  '0015' textline.
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '73'
*                                               TEXTLINE.
*  ENDIF.
  move textline to zinvoice01-so_ticket.

*  Get AFE no
* Check whether this doc's order was copied from another   "LABK928636
* and if so, retrace until we get to the original order    "LABK928636
* and get the text for this order.                         "LABK928636
  clear: l_ref_vbeln, textline.                            "LABK928636
  perform trace_ref_doc_text using '0003' changing         "LABK928636
                                   l_ref_vbeln.            "LABK928636
  if not l_ref_vbeln is initial.                           "LABK928636
    perform get_header_text(zvprint3) using l_ref_vbeln    "LABK928636
                                      '0003' textline.     "LABK928636
  else.                                                    "LABK928636
    perform get_doc_header_text using  vbdkr-vbeln
         vbdkr-vbeln_vauf   quote-vbeln  '0003' textline.
  endif.                                                   "LABK928636

*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '70' TEXTLINE.
*  ENDIF.
  move textline to zinvoice01-afe_num.

*  Get sales customer contact
  perform get_doc_header_text using
        vbdkr-vbeln     vbdkr-vbeln_vauf   quote-vbeln  '0001' textline.
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '71' TEXTLINE.
*  ENDIF.
  move textline to zinvoice01-sales_cont.

*  Get field customer contact
* Check whether this doc's order was copied from another   "LABK928790
* and if so, retrace until we get to the original order    "LABK928790
* and get the text for this order.                         "LABK928790
  clear: l_ref_vbeln, textline.                            "LABK928790
  perform trace_ref_doc_text using '0031' changing         "LABK928790
                                   l_ref_vbeln.            "LABK928790
  if not l_ref_vbeln is initial.                           "LABK928790
    perform get_header_text(zvprint3) using l_ref_vbeln    "LABK928790
                                      '0031' textline.     "LABK928790
  else.                                                    "LABK928790
    perform get_doc_header_text using
        vbdkr-vbeln     vbdkr-vbeln_vauf   quote-vbeln  '0031' textline.
  endif.                                                   "LABK928790
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '72' TEXTLINE.
*  ENDIF.
  move textline to zinvoice01-ops_cont.

*  Get invoice comment
  perform get_header_text(zvprint3) using vbdkr-vbeln '0033' text240.
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '74'   TEXT240.
*  ENDIF.
  perform get_header_text_2(zvprint3) using vbdkr-vbeln '0033' line1
                                      line2 line3 line4.
  if vbdkr-vbtyp = 'U' and zinvoice01-delnotes <> ' '.     "pro forma
    if zinvoice01-delnotes2 <> ' '.
      move: zinvoice01-delnotes  to zinvoice01-comment1,
            zinvoice01-delnotes2 to zinvoice01-comment2,
            line1                to zinvoice01-comment3,
            line2                to zinvoice01-comment4.
    else.
      move: zinvoice01-delnotes  to zinvoice01-comment1,
            line1                to zinvoice01-comment2,
            line2                to zinvoice01-comment3,
            line3                to zinvoice01-comment4.
    endif.
  else.
    move: line1 to zinvoice01-comment1,
          line2 to zinvoice01-comment2,
          line3 to zinvoice01-comment3,
          line4 to zinvoice01-comment4.
  endif.

*  Get Base in Charge
  perform get_doc_header_text using
        vbdkr-vbeln     vbdkr-vbeln_vauf   quote-vbeln  '0032' text80.
  select single * from tvstt where spras = sy-langu and
                                   vstel = text80(4).
  if sy-subrc = 0.
    text80 = tvstt-vtext.
  endif.
*  IF PREVIEW = 'NO'.
*    PERFORM WRITE_TO_FILE(ZVPRINT3) USING '01' '000001' '75' TEXT80.
*  ENDIF.
  move text80 to zinvoice01-base_chrge.

* Get Freight
* if no freight condition exists, 0.00 written                       "gs
  clear freight_value.                                      "gs
  select * from vbrk where                                  "gs
    vbeln = vbdkr-vbeln.                                    "gs
    select * from konv where                                "gs
      knumv = vbrk-knumv      "doc. cond. number                   "gs
      and kschl = 'HD00'.     "condition type                      "gs
      freight_value = freight_value + konv-kwert.                  "gs
    endselect.                                                     "gs
    exit.                                                          "gs
  endselect.                                                       "gs
  if freight_value eq 0.    "write out line but with a blank       "gs
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING                        "gs
*                          '01' '000001' '94' ' '.                  "gs
*    ENDIF.
    move ' ' to zinvoice01-freight.
  else.                                                            "gs
*    IF PREVIEW = 'NO'.
*      PERFORM WRITE_TO_FILE(ZVPRINT3) USING                        "gs
*                          '01' '000001' '94' FREIGHT_VALUE.        "gs
*    ENDIF.
    move freight_value to zinvoice01-freight.
  endif.                                                           "gs
*
endform.


*---------------------------------------------------------------------*
*       FORM GET_DOC_HEADER_TEXT                                      *
*---------------------------------------------------------------------*
*  Get the header text for this document                              *
*---------------------------------------------------------------------*
form get_doc_header_text using invoice_hdr sales_hdr quote_hdr
                                                        txt_id   txt.

  perform get_header_text(zvprint3) using invoice_hdr txt_id txt.
  if txt = space.
    if not sales_hdr is initial.                           "LABK926703
      perform get_header_text(zvprint3) using sales_hdr txt_id txt.
    endif.                                                 "LABK926703
    if txt = space.
      if not quote_hdr is initial.                         "LABK926703
        perform get_header_text(zvprint3) using quote_hdr txt_id txt.
      endif.                                               "LABK926703
    endif.
  endif.

endform.

*---------------------------------------------------------------------*
*       FORM GET_DOCUMENT_TITLE                                       *
*---------------------------------------------------------------------*
*  Get the correct title for invoices, credit memos, debit memos      *
*---------------------------------------------------------------------*
form get_document_title using doc_title   document_type.

  tables: stxl, stxh.

  data: temp_str(15) type c.

  data: begin of stxl_id occurs 0,
          tdobject like stxl-tdobject,
          tdname   like stxl-tdname,
          tdid     like stxl-tdid,
          spras    like stxl-tdspras,
        end of stxl_id.

  data: begin of lines occurs 0.
          include structure tline.
  data: end of lines.

  data: textname(18) type c.

  clear textname.

  select * from vbfa where vbelv = vbdkr-vbeln_vauf and vbtyp_n = 'N'.
    move vbfa-vbeln to cancelled_docs-vbeln.
    collect cancelled_docs.            " Save it to an internal table.
  endselect.

  select single * from vbrk where vbeln = vbdkr-vbeln.
  document_type = vbrk-fkart.
  if vbrk-rfbsk = 'C'.                 "  Accounting doc was created.
    select single * from bkpf where bukrs = vbrk-bukrs and
                                    belnr = vbrk-belnr and
                                    gjahr = vbrk-gjahr.
*   Compare acct doc created date/time against last print date/time
    if ( last_print_date > bkpf-cpudt ) or
       ( last_print_date = bkpf-cpudt and
             last_print_time > bkpf-cputm ).

* Check for repeat of original print (indicated in the header text
* of the document in the Invoice Comment), or else just a copy
      move: 'VBBK'      to stxl_id-tdobject,
            vbdkr-vbeln to stxl_id-tdname,
            '0011'     to stxl_id-tdid,
            sy-langu   to stxl_id-spras.

      import tline to lines from database stxl(tx) id stxl_id.
      if sy-subrc = 0.
        loop at lines.
          move lines-tdline to textname.
          exit.
        endloop.
        translate textname to upper case.
        delete from database stxl(tx) id stxl_id.
      endif.
      if textname ns 'ORIGINAL'.
        temp_str = 'Copy'.
      endif.
    endif.
  endif.

* Cancelled document?
  loop at cancelled_docs.
    select single * from vbrk where vbeln = cancelled_docs-vbeln.
    if sy-subrc = 0.
      if vbrk-sfakn = vbdkr-vbeln.
        move 'Cancelled' to temp_str.
        exit.
      endif.
    endif.
  endloop.

  concatenate temp_str doc_title into doc_title separated by ' '.

endform.

*---------------------------------------------------------------------*
*       FORM CHECK_REPEAT                                             *
*---------------------------------------------------------------------*
*       A text is printed, if it is a repeat print for the document.  *
*---------------------------------------------------------------------*

form check_repeat.

  clear repeat.
  select * into *nast from nast where kappl = nast-kappl
                                and   objky = nast-objky
                                and   kschl = nast-kschl
                                and   spras = nast-spras
                                and   parnr = nast-parnr
                                and   parvw = nast-parvw
                                and   nacha between '1' and '4'.
    check *nast-vstat = '1'.
    move *nast-datvr to last_print_date.
    move *nast-uhrvr to last_print_time.
    repeat = 'X'.
*   EXIT.
  endselect.
endform.

*---------------------------------------------------------------------*
*       FORM GET_DEL_NOTES.                                           *
*---------------------------------------------------------------------*
*       Look for all delivery notes related to the billing document   *
*---------------------------------------------------------------------*

form get_del_notes tables deliv_intab structure del_note_tbl
                          using sales_order    del_notes_str.
  data: len1 type i.

  clear: deliv_intab, del_notes_str.
  refresh: deliv_intab.

  select * from vbfa where vbelv = sales_order
                     and vbtyp_n = 'J'.         " just del notes
    move-corresponding vbfa to deliv_intab.
    collect deliv_intab.
  endselect.

  loop at deliv_intab.
    clear: sy-subrc.
*   only delivery notes related to the billing document
    select * from vbfa where vbelv = deliv_intab-vbeln
                         and vbeln = vbdkr-vbeln
                         and vbtyp_v = 'J'.
      exit.
    endselect.
    if sy-subrc = 0.
      t_string = deliv_intab-vbeln.
      len = strlen( t_string ).
      do len times.
        if t_string-char = '0' or t_string = ' '.
          shift t_string left.
        else.
          exit.
        endif.
      enddo.
* ************************************************************
*  To eliminate Raising Too Small Exception
      len1 = strlen( del_notes_str ).
      if len1 > 69.
        exit.
      endif.
* **************************************************************

      concatenate del_notes_str t_string into del_notes_str
                                    separated by ' '.
    endif.
  endloop.

endform.


*&---------------------------------------------------------------------*
*&      Form  CHECK_FOR_DIFF_SHIPTO  jb5
*&---------------------------------------------------------------------*
*       this routine checks to make sure the ship-to / tax area
*       is the same on both the invoice and sales order.
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form check_for_diff_shipto.

* Check Sales Order
  select single * from vbpa where
                   parvw = 'WE' and
                   posnr = '000000' and
                   vbeln = vbdkr-vbeln_vauf.

  if sy-subrc = 0.
    if vbpa-kunnr ne vbdkr-kunwe.
      shipto_diff_flag = 'X'.
    else.
* Check Delivery Note
      select single * from vbpa where
                       parvw = 'WE' and
                       posnr = '000000' and
                       vbeln = vbdkr-vbeln_vl.
      if sy-subrc = 0.
        if vbpa-kunnr ne vbdkr-kunwe.
          shipto_diff_flag = 'X'.
        endif.
      endif.
    endif.
  endif.

endform.                               " CHECK_FOR_DIFF_SHIPTO
*&---------------------------------------------------------------------*
*&      Form  MASSAGE_DATA
*&---------------------------------------------------------------------*
form massage_data.

  data: new_val(15)   type c,
        additional(1) type c,
        regular(1)    type c,
        lines         type i.


*  Convert appropriate header data to upper case.
  translate zinvoice01-afe_num to upper case.
  translate zinvoice01-po_num to upper case.
  translate zinvoice01-so_ticket to upper case.
  translate zinvoice01-well_name to upper case.
  translate zinvoice01-bp_county to upper case.

*  Service description window formatting
  if zinvoice01-well_name cs 'Name'.
    move ' ' to zinvoice01-well_name.
  endif.
  if zinvoice01-bp_country cs 'Location'.
    move ' ' to zinvoice01-bp_country.
  endif.

*  Prepare total box data
  perform convert_no_to_str2(zvprint3)  using
                                   zinvoice01-hfinal_amt 2 'Y' new_val.
* move new_val to zinvoice01-totalline5.
* move new_val to zinvoice01-totalline6.                    "LABK918227
  move new_val to zinvoice01-totalline7.                    "LABK934576

* Taxes
  if tax2 ne 0 or tax5 ne 0.

    if tax_jcd(3) = 'CAQ'.
*     move '  Q.S.T.' to zinvoice01-descline3.
*     move '  Q.S.T.' to zinvoice01-descline4.              "LABK918227
      move '  Q.S.T.' to zinvoice01-descline5.              "LABK934576
    else.
*     move '* Provincial Tax' to zinvoice01-descline3.
*     move '* Provincial Tax' to zinvoice01-descline4.      "LABK918227
      move '* Provincial Tax' to zinvoice01-descline5.      "LABK934576
    endif.
*   move '  G.S.T.' to zinvoice01-descline4.
*   move '  G.S.T.' to zinvoice01-descline5.                "LABK918227
    move '  G.S.T.' to zinvoice01-descline6.                "LABK934576
    perform convert_no_to_str(zvprint3)  using
                                  tax1 2 'Y' new_val.
*   move new_val to zinvoice01-totalline4.
*   move new_val to zinvoice01-totalline5.                  "LABK918227
    move new_val to zinvoice01-totalline6.                  "LABK934576
    if tax2 ne 0.
      perform convert_no_to_str(zvprint3)  using
                                    tax2 2 'Y' new_val.
*     move new_val to zinvoice01-totalline3.
*     move new_val to zinvoice01-totalline4.                "LABK918227
      move new_val to zinvoice01-totalline5.                "LABK934576
    else.
      perform convert_no_to_str(zvprint3)  using
                                    tax5 2 'Y' new_val.
*     move new_val to zinvoice01-totalline3.
*     move new_val to zinvoice01-totalline4.                "LABK918227
      move new_val to zinvoice01-totalline5.                "LABK934576
    endif.
  else.
    perform convert_no_to_str(zvprint3)  using
                                  tax1 2 'Y' new_val.
*   move new_val to zinvoice01-totalline3.
*   move new_val to zinvoice01-totalline4.                  "LABK918227
    move new_val to zinvoice01-totalline5.                  "LABK934576
    if tax_jcd(2) = 'CH'.
*     move '  H.S.T.' to zinvoice01-descline3.
*     move '  H.S.T.' to zinvoice01-descline4.              "LABK918227
      move '  H.S.T.' to zinvoice01-descline5.              "LABK934576
    else.
*     move '  G.S.T.' to zinvoice01-descline3.
*     move '  G.S.T.' to zinvoice01-descline4.              "LABK918227
      move '  G.S.T.' to zinvoice01-descline5.              "LABK934576
    endif.
*   move ' ' to zinvoice01-descline4.
*   move ' ' to zinvoice01-totalline4.
*   move ' ' to zinvoice01-descline5.                       "LABK918227
*   move ' ' to zinvoice01-totalline5.                      "LABK918227
    move ' ' to zinvoice01-descline6.                       "LABK934576
    move ' ' to zinvoice01-totalline6.                      "LABK934576
  endif.

* Surcharge
  if zinvoice01-hsur_desc ne ' '.
    perform convert_no_to_str(zvprint3)  using
                                  zinvoice01-htot_sur 2 'Y' new_val.
    move new_val to zinvoice01-totalline1.
    move zinvoice01-hsur_desc to zinvoice01-descline1.
  endif.
  perform convert_no_to_str(zvprint3)  using
                                zinvoice01-htt_af_sur 2 'Y' new_val.
* move new_val to zinvoice01-totalline2.
* move new_val to zinvoice01-totalline3.                    "LABK918227
  move new_val to zinvoice01-totalline4.                    "LABK934576

* Nowsco special discount                                   "LABK918227
  if nows_spec_disc_amt ne 0.                               "LABK918227
    move zinvoice01-hsdisc_des to zinvoice01-descline2.     "LABK918227
    perform convert_no_to_str(zvprint3)  using              "LABK918227
           zinvoice01-hsdisc_amt 2 'Y' new_val.             "LABK918227
    move new_val to zinvoice01-totalline2.                  "LABK918227
  endif.                                                    "LABK918227

* Nowsco special discount                                   "LABK934576
  if fuel_surchg_amt ne 0.                                  "LABK934576
    move zinvoice01-hfuelsur_des to zinvoice01-descline3.   "LABK934576
    perform convert_no_to_str(zvprint3)  using              "LABK934576
           zinvoice01-hfuelsur_amt 2 'Y' new_val.           "LABK934576
    move new_val to zinvoice01-totalline3.                  "LABK934576
  endif.                                                    "LABK934576


* Get unique records
loop at item_data.
  move-corresponding item_data to xitem_data.
  collect xitem_data.
  clear xitem_data.
endloop.

refresh item_data.
loop at xitem_data.
  move-corresponding xitem_data to item_data.
  append item_data.
  clear item_data.
endloop.

* Prepare item data
  clear: additional, regular.
  loop at item_data.

    if item_data-quantity ne 0.
*     List Unit Price division
      if item_data-bj_cond = 'ZBJS'
        or item_data-cond_type = 'c02'.                  "LABK929500
        item_data-book_val = item_data-net_val * 1000
                           / item_data-quantity.
        item_data-disc_amt = ' '.
      else.
        item_data-book_val = item_data-book_val * 1000
                             / item_data-quantity.
*     Discount Unit Price division
        item_data-disc_amt = item_data-net_val * 1000
                             / item_data-quantity.
      endif.
    endif.

*   List Unit Price
    if item_data-mat_num eq '300001' or
      item_data-mat_num eq '000000000000300001'.
*        If material is 300001, use net val / quantity instead of
*        book val / quantity
      if item_data-net_val ne 0.
*       List Unit Price division
        item_data-book_val = item_data-net_val * 1000
                             / item_data-quantity.
      endif.
    endif.
    if item_data-book_val ne 0.
      perform convert_no_to_str(zvprint3)  using
                                    item_data-book_val 2 'Y' new_val.

      move new_val to item_data-list_pr.
    else.
      move 'N/C' to item_data-list_pr.
    endif.

*     Discount Percentage
    perform convert_no_to_str(zvprint3)  using
                                     item_data-disc_per 2 'Y' new_val.
    write new_val+9(6) to item_data-disc.
    if item_data-disc eq '00.00'
      or item_data-cond_type = 'c02'.                      "LABK929500
      move ' ' to item_data-disc.
    endif.

*     Discount Unit Price
    if zinvoice01-hdisc_desc ne ' '.
      if item_data-net_val ne 0.
        perform convert_no_to_str(zvprint3)  using
                                   item_data-disc_amt 2 'Y' new_val.
        move new_val to item_data-disc_pr.
      else.
        move 'N/C' to item_data-disc_pr.
      endif.
    else.
      move ' ' to item_data-disc_pr.
    endif.

*     Price (net value)
    perform convert_no_to_str(zvprint3)  using
                                     item_data-net_val 2 'Y' new_val.
    if new_val = ' '.
      move '0.00' to new_val.
    endif.
    move new_val to item_data-price.

*     Quantity Used
    perform convert_no_to_str(zvprint3)  using
                                     item_data-quantity 3 'Y' new_val.
    if new_val = ' '.
      move '0.000' to new_val.
    endif.
    move new_val to item_data-quanu.
    condense item_data-quanu.

*     Quantity Quoted
    perform convert_no_to_str(zvprint3)  using
                                     item_data-qty_quoted 3 'Y' new_val.
    move new_val to item_data-quanq.
    condense item_data-quanq.

*   Test to see if we have regular AND additional item, or just reg.
*   if item_data-qty_quoted ne 0 or item_data-quanq ne '0.000'.
    if item_data-qty_quoted ne 0 or item_data-quanq ne ' '.
      move 'X' to additional.
    else.
      move 'X' to regular.
    endif.

    modify item_data.
  endloop.

* Set mixed flag if we have both additional AND regular items
  if additional eq 'X' and regular eq 'X'.
    move 'X' to zinvoice01-mixed.
  else.
    move ' ' to zinvoice01-mixed.
  endif.

* For hm00 and mixed (fixed\additional) invoices, separate into
* fixed and additional groups and total each groups price
  clear: item_gr1, item_gr2, sum1hm00, sum2hm00, sum1norm, sum2norm.
  refresh: item_gr1, item_gr2.

*{Start Insert LABK934392
  IF vbdkr-vbtyp = 'M'                "Invoices Only
     AND g_cust_num = '0001116370'    "Berlington Only
     AND job_type_no >= 100           "Cementing Jobs only
     AND job_type_no <= 199
     AND nast-dsnam <> 'OLD'.         "New Items Only

    refresh item_tmp.
    item_tmp[] = item_data[].
    refresh item_data. clear item_data.

    LOOP at item_tmp.
*     For FOB's only, clear unit price before discount
      IF item_tmp-posnr = item_tmp-uepos AND NOT
         item_tmp-posnr IS INITIAL.
        IF item_tmp-disc_pr IS INITIAL OR
           item_tmp-disc_pr = 'N/C'.
          item_tmp-disc_amt = item_tmp-book_val.
          item_tmp-disc_pr = item_tmp-list_pr.
        ENDIF.
        CLEAR: item_tmp-list_pr,
               item_tmp-disc_per, item_tmp-disc.

        MODIFY item_tmp.

*     Combine the items with the same item number and item description
*     and discounted unit price
        READ table item_data with key matnr = item_tmp-matnr
                                 mat_desc = item_tmp-mat_desc
                                 disc_amt = item_tmp-disc_amt.
        IF sy-subrc = 0.
          rec_index = sy-tabix.
          ADD item_tmp-quantity to item_data-quantity.
          ADD item_tmp-qty_quoted to  item_data-qty_quoted.
          ADD item_tmp-net_val  to item_data-net_val.

*       Price (net value)
          perform convert_no_to_str(zvprint3)  using
                                       item_data-net_val 2 'Y' new_val.
          if new_val = ' '.
            move '0.00' to new_val.
          endif.
          move new_val to item_data-price.

*         Quantity Quoted
          perform convert_no_to_str(zvprint3)  using
                                     item_data-qty_quoted 3 'Y' new_val.
          move new_val to item_data-quanq.
          condense item_data-quanq.

*         Quantity Used
          perform convert_no_to_str(zvprint3)  using
                                     item_data-quantity 3 'Y' new_val.
          if new_val = ' '.
            move '0.000' to new_val.
          endif.
          move new_val to item_data-quanu.
          condense item_data-quanu.

          MODIFY item_data INDEX rec_index.
        Else.
          APPEND item_tmp TO item_data.
        ENDIF.
      ELSE.
        APPEND item_tmp TO item_data.
      ENDIF.
    ENDLOOP.

  ENDIF.
*}End Insert LABK934392

  loop at item_data.
*    If we have a HM00, sort into two groups - fixed and additional
*   if zinvoice01-hhead_cont eq 'HM00'.
    if ( zinvoice01-hhead_cont eq 'HM00' and zinvoice01-mixed = ' ' ).
      if item_data-cond_type eq 'HM00'.
        move-corresponding item_data to item_gr1.       "Fixed
        append item_gr1.
        add item_data-net_val to sum1hm00.
      else.
* Want 'free of charge' items included in fixed price section
        if item_data-category = 'ZTAP' or                  "LABK916213
           item_data-category = 'ZFOC' or                  "LABK916213
           item_data-category = 'TANN'.                    "LABK916213
          move-corresponding item_data to item_gr1.        "LABK916213
          append item_gr1.                                 "LABK916213
          add item_data-net_val to sum1hm00.               "LABK916213
        elseif item_data-quantity <> 0.                    "LABK916213
* If all from quotes, and additional item not included in fixed price
* (i.e. not included in billing doc), if not used, don't print.
*       if item_data-quantity <> 0.
          move-corresponding item_data to item_gr2.        "Additional
          append item_gr2.
          add item_data-net_val to sum2hm00.
        endif.
      endif.
*   elseif zinvoice01-mixed eq 'X'.
    elseif ( zinvoice01-hhead_cont <> 'HM00'
            and zinvoice01-mixed eq 'X' ).
      if item_data-qty_quoted ne 0 or item_data-quanq ne ' '.
        move-corresponding item_data to item_gr1.
        append item_gr1.
        add item_data-net_val to sum2norm.
      else.
        move-corresponding item_data to item_gr2.
        append item_gr2.
        add item_data-net_val to sum1norm.
      endif.
***NEW**
    elseif ( zinvoice01-hhead_cont = 'HM00'
            and zinvoice01-mixed = 'X' ).
      if ( item_data-cond_type eq 'HM00' )
           and ( item_data-qty_quoted ne 0 or item_data-quanq ne ' ' ).
        move-corresponding item_data to item_gr1.       "Fixed
        append item_gr1.
        add item_data-net_val to sum1hm00.
      elseif ( item_data-cond_type = 'HM00' )
           and ( item_data-qty_quoted = 0 or item_data-quanq = ' ' ).
        move-corresponding item_data to item_gr2.       "Addl with fx pr
        append item_gr2.
        add item_data-net_val to sum1hm00.
      else.
        move-corresponding item_data to item_gr3.       "In addn to fx
        append item_gr3.                                "pr ie regular
        add item_data-net_val to sum2hm00.
      endif.
***END NEW**
    endif.
  endloop.

  sort item_gr1 by matnr ascending tax_flag descending.
  sort item_gr2 by matnr ascending tax_flag descending.
  sort item_gr3 by matnr ascending tax_flag descending.
  sort item_data by matnr ascending tax_flag descending.

** Frac Sand
** At lease one existing line containing frac sand must be
** first in all groups
  clear lines.
  describe table item_gr1 lines lines.
  if lines > 0.
    read table item_gr1 with key mat_desc(9) = 'Frac Sand'.
    if sy-subrc = 0.
      perform move_frac_first tables item_gr1.
    endif.
  endif.

  clear lines.
  describe table item_gr2 lines lines.
  if lines > 0.
    read table item_gr2 with key mat_desc(9) = 'Frac Sand'.
    if sy-subrc = 0.
      perform move_frac_first tables item_gr2.
    endif.
  endif.

  clear lines.
  describe table item_gr3 lines lines.
  if lines > 0.
    read table item_gr3 with key mat_desc(9) = 'Frac Sand'.
    if sy-subrc = 0.
      perform move_frac_first tables item_gr3.
    endif.
  endif.

  clear lines.
  describe table item_data lines lines.
  if lines > 0.
    read table item_data with key mat_desc(9) = 'Frac Sand'.
    if sy-subrc = 0.
      perform move_frac_first tables item_data.
    endif.
  endif.
  clear lines.
** end frac sand

* No qty quoted for Debit Memos
* if ( vbdkr-vbtyp = 'O' ) or ( vbdkr-vbtyp = 'P' ).
  if vbdkr-vbtyp = 'P'.
    item_gr1-quanq = ' '.
    modify item_gr1 transporting quanq where quanq ne ' '.
    item_gr2-quanq = ' '.
    modify item_gr2 transporting quanq where quanq ne ' '.
    item_gr3-quanq = ' '.
    modify item_gr3 transporting quanq where quanq ne ' '.
    item_data-quanq = ' '.
    modify item_data transporting quanq where quanq ne ' '.
  endif.

* Convert sums into strings
  perform convert_no_to_str(zvprint3)  using
                                       sum1hm00 2 'Y' new_val.
  move new_val to zinvoice01-sum1hm00.
  perform convert_no_to_str(zvprint3)  using
                                       sum2hm00 2 'Y' new_val.
  move new_val to zinvoice01-sum2hm00.
  perform convert_no_to_str(zvprint3)  using
                                       sum1norm 2 'Y' new_val.
  move new_val to zinvoice01-sum1norm.
  perform convert_no_to_str(zvprint3)  using
                                       sum2norm 2 'Y' new_val.
  move new_val to zinvoice01-sum2norm.


endform.                    " MASSAGE_DATA


*&---------------------------------------------------------------------*
*&      Form  MOVE_FRAC_FIRST
*&---------------------------------------------------------------------*
form move_frac_first tables ftab structure item_data.

data: begin of frac_line occurs 0.
        include structure item_gr1.
data: end of frac_line.

data: unused    type i,
      used_indx type i,
      old_count type i,
      new_count type i,
      tot_lines type i.

  clear: unused, used_indx, old_count, new_count, tot_lines.

  describe table ftab lines tot_lines.

  loop at ftab where quanu = '0.000'.       "Unused
    add 1 to unused.
  endloop.

  read table ftab with key mat_desc(9) = 'Frac Sand'
                               quanu = '0.000'.
  if sy-subrc = 0.
    if unused <> 0.
      old_count = 1.
      while old_count <= unused.
        read table ftab index old_count.
        if ftab-mat_desc(9) = 'Frac Sand'.
          add 1 to new_count.
          move ftab to frac_line.
          delete ftab index old_count.
          insert frac_line into ftab index new_count.
          exit.
        endif.
        add 1 to old_count.
      endwhile.
    endif.
    clear: old_count, new_count.
  endif.

  used_indx = unused + 1.

  loop at ftab where mat_desc(9) = 'Frac Sand'
                     and quanu <> '0.000'.
    exit.
  endloop.
  if sy-subrc = 0.
    old_count = used_indx.               "first line of used
    new_count = used_indx.
    while old_count <= tot_lines.
      read table ftab index old_count.
      if ftab-mat_desc(9) = 'Frac Sand'.
        move ftab to frac_line.
        delete ftab index old_count.
        insert frac_line into ftab index new_count.
*       add 1 to new_count.
        exit.
      endif.
      add 1 to old_count.
    endwhile.
    clear: old_count, new_count, tot_lines.
  endif.


endform.                    "MOVE_FRAC_FIRST


*&---------------------------------------------------------------------*
*&      Form  ACTIVATE_ELEMENTS
*&---------------------------------------------------------------------*
form activate_elements.

  data: item type i, gr2 type i, gr3 type i.
  item = 0.

  clear zinvoice01-lines.

* Start delete LABK931107
*  if vbdkr-vbtyp = 'O'.             "Credit Memos
*    loop at item_data.
*      add 1 to zinvoice01-lines.
*    endloop.
*  else.                             "all other document types
* End delete LABK931107

* This section gets the number of lines in the 'main' portion
* of the printout and adds the number of lines reserved for the
* 'total' portion of the printout.

    if ( zinvoice01-hhead_cont eq 'HM00'
        and zinvoice01-mixed eq ' ' ).
      add 2 to zinvoice01-lines.
      loop at item_gr1.
        add 1 to zinvoice01-lines.
      endloop.
      add 1 to zinvoice01-lines.
      loop at item_gr2.
        add 1 to gr2.
        add 1 to zinvoice01-lines.
      endloop.
      if gr2 gt 0.
       add 1 to zinvoice01-lines.
      endif.
    elseif ( zinvoice01-hhead_cont <> 'HM00'
             and zinvoice01-mixed eq 'X' ).
      loop at item_gr1.
        add 1 to zinvoice01-lines.
      endloop.
      loop at item_gr2.
        add 1 to zinvoice01-lines.
      endloop.
      add 1 to zinvoice01-lines.
    elseif ( zinvoice01-hhead_cont = 'HM00'
             and zinvoice01-mixed eq 'X' ).
      add 1 to zinvoice01-lines.
      loop at item_gr1.
        add 1 to zinvoice01-lines.
      endloop.
      loop at item_gr2.
*        AT FIRST.
*          ADD 2 TO ZINVOICE01-LINES.
*        ENDAT.
        add 1 to zinvoice01-lines.
      endloop.
      add 1 to zinvoice01-lines.
      loop at item_gr3.
        add 1 to gr3.
        at first.
          add 1 to zinvoice01-lines.
        endat.
        add 1 to zinvoice01-lines.
      endloop.
      if gr3 gt 0.
        add 1 to zinvoice01-lines.
      endif.
    else.
      loop at item_data.
        add 1 to zinvoice01-lines.
      endloop.
    endif.
    clear: gr2, gr3.
* Start delete LABK931107
*  endif.                "vbdkr-vbtyp = 'O'
* End delete LABK931107


* Footer for co. code 9040 higher than for others so lines in main
* window must be less
  if vbdkr-vkorg = '9040'.
    if zinvoice01-lines >= 30.              "total lines in main window
      zinvoice01-lines = ( zinvoice01-lines mod 30 ).
    endif.
  else.
    if zinvoice01-lines >= 33.              "total lines in main window
      zinvoice01-lines = ( zinvoice01-lines mod 33 ).
    endif.
  endif.

* {Start replace LABK934634
*  case vbdkr-vbtyp.
*    when 'O' or 'P'.                       "credit or debit memo
*      if zinvoice01-lines <> 0.            "exactly 32 lines
*        add 9 to zinvoice01-lines.         "total box + 'credit'
*      endif.
*    when 'M'.                               "invoice
*      if zinvoice01-lines <> 0.             "exactly 32 lines
*        if vbdkr-vkorg = '9040'.
*          add 11 to zinvoice01-lines.       "2 for tot box + pmt instr
*        else.
*          add 9 to zinvoice01-lines.        "total box
*        endif.
*      endif.
*    when others.
*      if zinvoice01-lines <> 0.             "exactly 32 lines
*        add 14 to zinvoice01-lines.         "total box + Nowsco Rep
*      endif.
*  endcase.

* 9 lines were reserved for the total portion of the printout.
* A new line for fuel surcharge has been added so we need to adjust.
* Add 1.
  case vbdkr-vbtyp.
    when 'O' or 'P'.                         "credit or debit memo
      if zinvoice01-lines <> 0.              "exactly 32 lines
        add 10 to zinvoice01-lines.          "total box + 'credit'
      endif.
    when 'M'.                                 "invoice
      if zinvoice01-lines <> 0.               "exactly 32 lines
        if vbdkr-vkorg = '9040'.
          add 12 to zinvoice01-lines.         "2 for tot box + pmt instr
        else.
          add 10 to zinvoice01-lines.          "total box
        endif.
      endif.
    when others.
      if zinvoice01-lines <> 0.               "exactly 32 lines
        add 15 to zinvoice01-lines.           "total box + Nowsco Rep
      endif.
  endcase.
* End replace LABK934634}

  if vbdkr-vkorg = '9040'.
    if ( zinvoice01-lines >= 30 ).
      zinvoice01-lines = 0.
      zinvoice01-underline = ' '.
    endif.
  else.
    if ( zinvoice01-lines >= 33 ).
      zinvoice01-lines = 0.
      zinvoice01-underline = ' '.
    endif.
  endif.

* {Start insert LABK934346
  condense zinvoice01-lines no-gaps.
*  End insert LABK934346}

* Activate header elements
* Start delete LABK931107
*  if vbdkr-vbtyp = 'O'.              "Credit Memo
*    item = 0.
*    loop at item_data.
*      move-corresponding item_data to zinvoice01.
*      if item eq 0.
*        perform write_form using 'LINE1' 'MAIN'.
*      else.
*        perform write_form using 'LINEN' 'MAIN'.
*      endif.
*      add 1 to item.
*    endloop.
*  else.
* End delete LABK931107

*  If we have an hm00
    if ( zinvoice01-hhead_cont eq 'HM00'
        and zinvoice01-mixed eq ' ' ).
*     Show header line
      perform write_form using 'LINE1HM00' 'MAIN'.

*     Loop through items included in fixed price
      loop at item_gr1.
        move-corresponding item_gr1 to zinvoice01.
        perform write_form using 'LINE2HM00' 'MAIN'.
      endloop.

*     Display fixed price
      perform write_form using 'LINE3HM00' 'MAIN'.

*     Loop through additional items
      gr2 = 0.
      loop at item_gr2.
        move-corresponding item_gr2 to zinvoice01.
        add 1 to gr2.
        at first.
          perform write_form using 'LINE4HM00' 'MAIN'.
        endat.
*        PERFORM WRITE_FORM USING 'LINE2HM00' 'MAIN'.
        perform write_form using 'LINE2HM00_ADD' 'MAIN'.
      endloop.

*     Display additional item total
      if gr2 gt 0.
        perform write_form using 'LINE5HM00' 'MAIN'.
      endif.

*  else, check to see if we have an invoice with fixed and additional
*  items (TPRSS)
* elseif zinvoice01-mixed eq 'X'.
    elseif ( zinvoice01-hhead_cont <> 'HM00'
             and zinvoice01-mixed eq 'X' ).
      item = 0.
      loop at item_gr1.
        move-corresponding item_gr1 to zinvoice01.
        if item eq 0.
          perform write_form using 'LINE1' 'MAIN'.
        else.
          perform write_form using 'LINEN' 'MAIN'.
        endif.
        add 1 to item.
      endloop.

      loop at item_gr2.
        move-corresponding item_gr2 to zinvoice01.
        add 1 to gr2.
        at first.
          perform write_form using 'ADDHEAD' 'MAIN'.
        endat.
        perform write_form using 'LINEN' 'MAIN'.
      endloop.

    elseif ( zinvoice01-hhead_cont = 'HM00'
             and zinvoice01-mixed eq 'X' ).
*     Show header line
      perform write_form using 'LINE1HM00' 'MAIN'.

*     Loop through items included in fixed price from quote
      loop at item_gr1.
        move-corresponding item_gr1 to zinvoice01.
*       perform write_form using 'LINE6HM00' 'MAIN'.
* Users no longer want qty quoted for fixed price items
        perform write_form using 'LINE2HM00' 'MAIN'.
      endloop.

*     Loop through additional items with fixed price
      loop at item_gr2.
        move-corresponding item_gr2 to zinvoice01.
        add 1 to gr2.
***Don't show 'Addtl Items' header or header line anymore
*       at first.
*         perform write_form using 'ADDHEAD' 'MAIN'.
*     Show header line anymore
*         perform write_form using 'LINE1HM00' 'MAIN'.
*       endat.
        perform write_form using 'LINE2HM00' 'MAIN'.
      endloop.

*     Display fixed price
      perform write_form using 'LINE3HM00' 'MAIN'.

*     Loop through additional items i.e. in addition to fixed price
      gr3 = 0.
      loop at item_gr3.
        move-corresponding item_gr3 to zinvoice01.
        add 1 to gr3.
        at first.
          perform write_form using 'LINE4HM00' 'MAIN'.
        endat.
*       perform write_form using 'LINE2HM00' 'MAIN'.
        perform write_form using 'LINE1' 'MAIN'.
      endloop.

*     Display additional item total
      if gr3 gt 0.
        perform write_form using 'LINE5HM00' 'MAIN'.
      endif.

*  Regular invoice
    else.
      item = 0.
      loop at item_data.
        move-corresponding item_data to zinvoice01.
        if item eq 0.
          perform write_form using 'LINE1' 'MAIN'.
        else.
          perform write_form using 'LINEN' 'MAIN'.
        endif.
       add 1 to item.
      endloop.
    endif.
* Start delete LABK931107
*  endif.           "vbdkr-vbtyp = 'O'
* End delete LABK931107

  perform write_form using 'UNDERLINE' 'MAIN'.
  zinvoice01-underline = 'X'.
  perform write_form using 'TOTAL' 'MAIN'.

endform.                    " ACTIVATE_ELEMENTS


*&---------------------------------------------------------------------*
*&      Form  PROTOCOL_UPDATE
*&---------------------------------------------------------------------*
*       The messages are collected for the processing protocol.       *
*---------------------------------------------------------------------*
form protocol_update.

  check xscreen = space.
  call function 'NAST_PROTOCOL_UPDATE'
       exporting
            msg_arbgb = syst-msgid
            msg_nr    = syst-msgno
            msg_ty    = syst-msgty
            msg_v1    = syst-msgv1
            msg_v2    = syst-msgv2
            msg_v3    = syst-msgv3
            msg_v4    = syst-msgv4
       exceptions
            others    = 1.

endform.                    " PROTOCOL_UPDATE

*&---------------------------------------------------------------------*
*&      Form  WRITE_FORM
*&---------------------------------------------------------------------*
form write_form using element window.

call function 'WRITE_FORM'
     exporting
          element       = element
          window        = window
     exceptions
          element       = 1
          window        = 6
          others        = 7.

endform.                    " WRITE_FORM

* >>Start Insert LABK928636 >>
*&---------------------------------------------------------------------*
*&      Form  TRACE_REF_DOC_TEXT
*&---------------------------------------------------------------------*
form trace_ref_doc_text using value(p_id) changing p_ref_vbeln.

  data: l_vbeln like vbdkr-vbeln,
        l_tdid  like stxh-tdid.

  l_vbeln = vbdkr-vbeln.
  l_tdid  = p_id.

  clear p_ref_vbeln.

* Get the sales order that this was created from and get any
* possible sales orders that this order was copied from.

* Set sy-subrc.
  sy-subrc = 0.

  while sy-subrc = 0.
    select single * from stxh
      where tdobject = 'VBBK'
        and tdname   = l_vbeln
        and tdid     = l_tdid
        and tdspras  = sy-langu.
    if sy-subrc = 0.
      if not stxh-tdref     is initial and
         not stxh-tdrefname is initial.
* There is a ref document
        p_ref_vbeln = stxh-tdrefname(10).
* Check that the referenced doc is not the same as the one
* we got before. Not 100% sure that the original record would
* have no ref flag.
        if p_ref_vbeln = l_vbeln.
          exit.
        else.
          l_vbeln = stxh-tdrefname(10).
        endif.
      else.
* No ref document
        exit.
      endif.
    endif.
  endwhile.

endform.                    " TRACE_REF_DOC_TEXT
* >>End Insert LABK928636 >>
*{ Start insert LABK934226
*-----------------------------------------------------------------------
*  Determine Parent
*    Find if the item is a parent item, i.e. An exploding FOB
*-----------------------------------------------------------------------
FORM determine_parent USING p_posnr    "child line number
                        p_uepos        "parent line number
                        fob_parent.

  IF p_uepos = '000000'.
*     check all items for a parent equal to the current posnr
    LOOP AT tvbap.
      IF tvbap-uepos EQ p_posnr.
        fob_parent = 1.
        EXIT.
      ENDIF.
    ENDLOOP.
  ENDIF.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM print_item_text                                          *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM get_item_text USING p_vbeln p_posnr p_id p_line.

  DATA: lines LIKE tline OCCURS 1 WITH HEADER LINE,
         l_name(70) TYPE c.

  CONCATENATE p_vbeln p_posnr INTO l_name.

  CALL FUNCTION 'READ_TEXT'
       EXPORTING
            client          = sy-mandt
            id              = p_id
            language        = 'E'
            name            = l_name
            object          = 'VBBP'
       TABLES
            lines           = lines
       EXCEPTIONS
            id              = 01
            language        = 02
            name            = 03
            not_found       = 04
            object          = 05
            reference_check = 06.
  READ TABLE lines INDEX 1.
  MOVE lines-tdline TO p_line.
  REFRESH lines.

ENDFORM.                                           "get_item_text

*-----------------------------------------------------------------------
*                   Form GET_quoted_quantity
* This is a modified copy of the form "WRITE_DATA_TO_ACCESS" in the
* include program "ZVREFSI2"
*-----------------------------------------------------------------------
*FORM WRITE_DATA_TO_ACCESS TABLES SALES_ITEMTAB STRUCTURE ITEMTAB
FORM get_quantity_quoted TABLES SALES_ITEMTAB STRUCTURE ITEMTAB
                                 ITEM_DATA STRUCTURE ITEM_DATA
                         USING    QUOTATION   LAST_ITEM.

  DATA:  BEGIN OF QUOTE_ITEMTAB OCCURS 50,
             ITEM_COUNT(6) TYPE N,
             MATNR LIKE VBAP-MATNR,     " Material Number
             ARKTX LIKE VBAP-ARKTX,     "short text
             KWMENG LIKE VBAP-KWMENG,   "Quantity
             VRKME   LIKE VBAP-VRKME,   "Unit
             PSTYV LIKE VBAP-PSTYV,     "Item category
             posnr like vbap-posnr,
             uepos like vbap-uepos,
         END OF QUOTE_ITEMTAB,
         MATCH_SW(1) TYPE C,
         ITEM_COUNT(6) TYPE N.

  DATA: wa_quote_item like line of quote_itemtab.
  DATA: quote_desc like quote_itemtab-arktx,
*        temp_desc like quote_itemtab-arktx,
        temp_posnr like vbap-posnr,
        temp_uepos like vbap-uepos.
  DATA: BEGIN OF tvbdpa OCCURS 0.        "Internal table for items
          INCLUDE STRUCTURE vbdpa.
  DATA: END OF tvbdpa.

  ITEM_COUNT = LAST_ITEM.

  CLEAR: QUOTE_ITEMTAB, wa_quote_item.
  REFRESH QUOTE_ITEMTAB.

  SELECT SINGLE * FROM VBAK WHERE
    VBELN = QUOTATION.
  IF VBAK-AUART NE 'AG'.
    WRITE: / 'ZVREFSI2 error, no quotation...'.
    EXIT.            "exit from the routine
  ENDIF.

* Only populate quote with ernam = 'SAPPRT' or 'TPRSS'.    "LABK926688
  IF ( VBAK-ERNAM NE 'SAPPRT'
      and vbak-ernam(4) ne 'TPRS' ).                        "LABK930749
    EXIT.
  ENDIF.

  fob_posnr = '999999'.
  fob_blend = ' '.
  REFRESH tvbap.
  SELECT * FROM vbap
     INTO corresponding fields of TABLE tvbap
    WHERE VBELN = QUOTATION
      AND pstyv NE 'TAQ'
      AND abgru = ' '.

  SELECT * FROM VBAP WHERE
      VBELN = QUOTATION AND PSTYV NE 'TAQ'.
    MOVE-CORRESPONDING VBAP TO wa_quote_item.
    PERFORM TRUNC_LEAD_ZERO USING wa_quote_item-MATNR.

    fob_parent = 0.
    PERFORM determine_parent USING vbap-posnr
                                 vbap-uepos
                                 fob_parent.
*  if we have an fob parent, check to see if there is an fob
*  description to use in place of the regular material description
    IF fob_parent EQ 1.
      CLEAR textline.
* Get item text from THE QUOTE
      PERFORM get_item_text USING vbap-vbeln vbap-posnr
                                '0053' textline.
      fob_blend = wa_quote_item-ARKTX.
      IF NOT textline IS INITIAL.
        fob_blend = textline.
      ENDIF.
      fob_posnr = vbap-posnr.
    ENDIF.

      temp_posnr = vbap-posnr.
      temp_uepos = vbap-uepos.
      IF vbap-uepos EQ fob_posnr.
*       This is an FOB detail line
        MOVE vbap-uepos TO wa_quote_item-uepos.
        MOVE vbap-posnr TO wa_quote_item-posnr.
        MOVE fob_blend    TO wa_quote_item-arktx.
      ELSE.
*       This might be an FOB parent and/or FOB detail line
        temp_posnr = '000000'.
        IF vbap-posnr EQ fob_posnr.
          temp_posnr = fob_posnr.
        ENDIF.
*       Make sure it is a detail line
        IF temp_posnr EQ fob_posnr.
          MOVE fob_posnr TO temp_uepos.
          MOVE fob_blend TO wa_quote_item-arktx.
        ENDIF.
        MOVE temp_uepos TO wa_quote_item-uepos.
        MOVE temp_posnr TO wa_quote_item-posnr.
      ENDIF.

    read table quote_itemtab with key matnr = wa_quote_item-matnr
                                arktx = wa_quote_item-arktx.
    if sy-subrc = 0.
       add wa_quote_item-kwmeng to quote_itemtab-kwmeng.
       modify quote_itemtab index sy-tabix.
    else.
      append wa_quote_item to quote_itemtab.
    endif.
  ENDSELECT.

  LOOP AT QUOTE_ITEMTAB.
    quote_desc = quote_itemtab-arktx.
    CONDENSE quote_desc NO-GAPS.
    TRANSLATE quote_desc TO UPPER CASE.
    CLEAR MATCH_SW.

    LOOP AT SALES_ITEMTAB.
      CONDENSE sales_itemtab-arktx NO-GAPS.
      TRANSLATE sales_itemtab-arktx TO UPPER CASE.

      IF SALES_ITEMTAB-MATERIAL_NO = QUOTE_ITEMTAB-MATNR
         AND SALES_ITEMTAB-ARKTX = quote_desc.
        LOOP AT ITEM_DATA WHERE
                          COUNT EQ SALES_ITEMTAB-ITEM_COUNT.
          MATCH_SW = 'X'.
          MOVE QUOTE_ITEMTAB-KWMENG TO ITEM_DATA-QTY_QUOTED.
          MODIFY ITEM_DATA.

          EXIT.
        ENDLOOP.
        EXIT.
      ENDIF.
    ENDLOOP.

    IF MATCH_SW NE 'X'.
      ITEM_COUNT = ITEM_COUNT + 1.
      CLEAR ITEM_DATA.
      MOVE QUOTE_ITEMTAB-MATNR TO
           ITEM_DATA-MAT_NUM.
      MOVE QUOTE_ITEMTAB-ARKTX TO
           ITEM_DATA-MAT_DESC.
      MOVE QUOTE_ITEMTAB-KWMENG TO
           ITEM_DATA-QTY_QUOTED.
      MOVE QUOTE_ITEMTAB-VRKME TO
           ITEM_DATA-UOM.
      MOVE QUOTE_ITEMTAB-PSTYV TO
           ITEM_DATA-CATEGORY.
      MOVE QUOTE_ITEMTAB-uepos TO
           ITEM_DATA-uepos.
      MOVE QUOTE_ITEMTAB-posnr TO
           ITEM_DATA-posnr.
      APPEND ITEM_DATA.
    ENDIF.
  ENDLOOP.

ENDFORM.                             "get_quantity_quoted
*} End insert LABK934392












Make your own free website on Tripod.com