REPORT zvdoquo2 LINE-COUNT 100 MESSAGE-ID vn.
*@---------------------------------------------------------------------*
*@ Program ID:    ZVDOQUO2
*@ Program Desc:  Print of Quotation by SAPscript
*@                Original program: RVADOR01
*@        I have modified this program so that a file is generated when
*@        someone has tried to print a quotation.  This file contains
*@        information about the quotation.  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 quotation print out.  Any data that is not on
*@        the quotation print out is handled by a seperate routine by
*@        accessing the appropriate table(s).
*@
*@ Input file    : none
*@ Output file   : id TE - sap output file path, filename is unique to
*@                 that document
*@
*@ Tables Updated: Table zpara - NOWSCO user program parameter table
*@ Programmer: Lee Haire
*@ Position: Basis Consultant
*@ Company: OmniLogic Systems Group Inc.
*@ Client: NOWSCO Well Service Ltd.
*@ Date completed: Nov.2, 1994
*@---------------------------------------------------------------------*
** Change History:
**  Jan 26, 1995  T.Nguyen   Major changes to orginal program
**                           and use it to print out Sales Order also
** C11K900934 May T.Nguyen   Print Base in charge
** C11K900934 950623 RV      Select template based on doc type
**            950804 TN      Change get tax proc. in header_price_print
** LABK900923 Aug PTAM     - output 2nd file with 1st file name in for
**                           SAP MOVER
**            Aug Jlai     - To get the file path name from table
**                           FILENAME
** LABK901070 Aug PTAM     - Suppress BC SURCHG if HM00 exists in
**                           cond type
**                         - for Sales Order if HM00 exist => write
**                           'Fixed price job' to invoice comment
**
** LABK901124 950923 RV - output HM00 Condition type if it's used
**                     - allow for changes to GET_PRICING_PROC(ZVPRINT1)
**                       for MAT_REIMB_AMT
**
**LABK901167 950928 TN - Fix the problem of empty TKOMVD table ( the
**                       the table is retured by SAP function)
**                       For sales order indicate if item is additional
**           951026 TN - Print out phone number of the base that the
**                       salesman belongs to ( Header: Field id 81 )
**           951129 TN - Print out fields required for interfacing with
**                       T.E to SAPBack table.
**LABK901422 951219 GS - Add SD category as key to look up table ZTMPL
**           951228 GS - for online, test transaction, for batch, look
**                       in zpara, and write out SAPBack information
**                       accordingly (create vs. update)
**LABK901551 960220 GS - use sales unit(vbdpa-vrkme) in place of
**                       sales unit of target(vbdpa-zieme),
**                       default template from SAP script file
**LABK901891 960514 GS - Print out customers shipping instructions
**                       from header text and creation date for CECO
**                       Sales Order.  Changes marked by "gs
**LABK901949 960606 GS - Compensate for FOB quotation.  For quotations,
**                       Download an id string that indcates the
**                       following:
**                         Regular Item - 000000000000
**                         FOB Parent   - nnnnnn000000
**                         FOB Child    - xxxxxxnnnnnn
**
**                            where nnnnnn is the current line in doc.
**                                  xxxxxx is the FOB parent line in doc
**                       Also download FOB description and yeild from
**                       the FOB parent item texts if they exist, else
**                       use the regular material description. "gs2
**LABK901946 960529 JLai-To reformat the sales order with used quantity
**                       from quotation
**           960808 GS - Download saleman's comments from header level
**                     - modify FOB filter to exclude type TAQ (blend
**                       components) "gs3
**LABK902227 960922 GSoltys  - allow FOB additives on invoice
**                           - changes commented with "gs4
**LABK902133  961011  JBallard - Added the tables PA0001 and PA0002
**                               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 #
**
**             961017 JBallard - addition of a longer variable (TEXT240)
**                               to store the salespersons comments.
**                             - changes marked with "jb2
**LABK902421   961204 jlai - To add material description to itemtab
**                           for further matching purpose
**labk902483   GSoltys Jan.27\97 Fix ZVPRINT get_pricing_proc call
**             - marked with gs5
**LABK909566   Jan 8, 1997 - Download condition type ZBJS to flat file
**             so that discount % will be suppressed on Access printout
**             for the Quotations
**             Changes marked with jb99.
**----------------------------------------------------------------------
* LABK911324 April 17, 1998
* Modified so that it updates newly created table ZTESAPBACK w/creation
* status. This will eliminate the update process with the the Print
* Processor. Changes marked with gh1
**----------------------------------------------------------------------
* LABK913531 GHorn November 24, 1998
* Cleared textline variable so that it would remain empty if a valid
* selection was not found and prevent an old value being populated.
* Confused?
* Changes marked with gh2
**----------------------------------------------------------------------
** LABK913955 JTsang February 26, 1999
** Convert to SAPScript printing - Quotes/Service Orders/DR-CR Memos/
** Templates.
**----------------------------------------------------------------------
* LABK915116 April 27, 1999 CFieulle  Quotes/Contracts
* Prevent printing when in 'preview' mode. Subroutine 'write_header_
* info_to_file' moved to after 'open_form' where value of preview flag
* is determined.
* Also changes to accommodate layout set printing.
**----------------------------------------------------------------------
* LABK915243 May 11, 1999 CFieulle Templates
* Changes to accommodate layout set printing.
**----------------------------------------------------------------------
* LABK915255 May 13, 1999 CFieulle Service Orders
* Changes to accommodate layout set printing.
**----------------------------------------------------------------------
* LABK915522 June 23, 1999 CFieulle
* Set up filename for backup file.  Also final transport to clone and
* production.
**----------------------------------------------------------------------
* LABK916958 Oct 15, 1999 CFieulle
* Use table Z2UTL instead of: ZUNLN for units,
*                             ZPRLN for personnel on jobs.
* Re: new utilisation data capture.
**----------------------------------------------------------------------
* LABK916974 Oct 15, 1999 CFieulle
* Adjust length of employee number to fit new Fracmaster employee
* numbers which are six digits in length. Layout set ZV_NOWSCO_ORDER
* also adjusted.
**----------------------------------------------------------------------
* LABK916978 Oct 15, 1999 CFieulle
* Allow all of salesperson's text to be printed.
*
**----------------------------------------------------------------------
* LABK917025 Oct 22, 1999 Akassam
* Insert Material number. changes denoted by labk number
**----------------------------------------------------------------------
** 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.
**----------------------------------------------------------------------
** LABK925268  CFieulle 2000-04-10  46B Upgrade.
**                                  Use central address mgmt table ADRC
**                                  instead of SADR for header data on
**                                  all post 46B documents.
**----------------------------------------------------------------------
** 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.
**----------------------------------------------------------------------
** LABK928153  CFieulle 2000-08-21  Print display - list header. Use
**                                  a different zinvoice01 field,
**                                  because it's being used to determine
**                                  when there should be an underline,
**                                  and prevents the first list
**                                  header with net price from being
**                                  printed, because it's being filled
**                                  with another value.
**                                  2. If the quote value is a fixed
**                                  price i.e. HM00 pricing, don't
**                                  print Discount%, Discounted Unit
**                                  Price and Net Price on first page.
**                                  3. Remove file extraction.
**----------------------------------------------------------------------
** LABK929091  Gary Horn  January 5, 2001
**                        Extract CO2 value from conditions
**                        - changes marked with gh3
**----------------------------------------------------------------------
** LABK929345  CFieulle   January 22, 2001
**             Decrease salesperson's comment box by 2 lines.
**             Layoutset ZV_NOWSCO_QUOTE modified. Decrease limit of #
**             of lines in main.
**----------------------------------------------------------------------
** LABK929390  CFieulle   February 06, 2001
**             1.Line items for printing: sum line items of same
**             description and discount %.
**             2.Do not print rejected items (entry in vbap-agru)
**             3.New CO2 value extraction logic. (old one not used)
**             4.Line item print modification: no more boxes for main
**               and total, so line items and total can flow.
**----------------------------------------------------------------------
** LABK929496  CFieulle  February 23, 2001
**             Layout: fob section underline.
**----------------------------------------------------------------------
** LABK929508  CFieulle  March 01, 2001
**             Additional c02 modns.
**----------------------------------------------------------------------
** LABK929587  CFieulle  March 15, 2001
**             Do not include BC surcharge for HM00 documents.
**----------------------------------------------------------------------
** LABK930234  CFieulle  June 19, 2001
**             Quotes:
**             New Condition type Z100 created for 'no-charge' items.
**             Items of category 'AGNN', ZFAP', and 'ZFOB' are auto-
**             matically defaulted to this cond. type and therefore
**             should have no price.
**----------------------------------------------------------------------
** LABK930598  CFieulle August 20, 2001
**             Quotes: Re LABK930234 above - also applies to blend
**             component display.
**----------------------------------------------------------------------
** Sept. 13, 2001 LABK930879 CFieulle
**             Handle multiple zdsc discounts.  If more than one, 'A'
**             (automatic pricing) line should not be used. If more than
**             two, print error message in place of item description.
**             Reason:
**             When a quote is created via TPRSS and the zdsc is later
**             changed manually at the header level, the previous
**             item discount remains resulting in two discounts.  In the
**             case of two discounts, the automatic entry does not take
**             effect. If the user doesn not notice and decides to add
**             another discount (which the system allows), we may end up
**             with multiple zdsc discounts for the same item, in which
**             case we alert them via an error message printed in place
**            of the item description.
**----------------------------------------------------------------------
** LABK930893 CFieulle & BYwang, Sept 28, 2001
**            - Change the display length of the FOB text to 100
**                - add new field in zinvoice01 "fob_mat_desc"
**            - change the SAPForm "zv_nowsco_qoute"
**----------------------------------------------------------------------
** LABK931012 2001/10/12 FLAU
** LABK931071
**            - Incorrect printing of discount unit price due to wrong
**              reference table assignment of zinvoice01-disc_per and
**              zinvoice01-disc_amt.
**----------------------------------------------------------------------
** 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.
**----------------------------------------------------------------------
** LABK931580  CFieulle December 27, 2001
**             Prevent printing of large discount %'s ( * 100).
**             Also make sure underline under each section on FOB pages
**             is always printed.
**----------------------------------------------------------------------
** LABK934576  IJADALOW March 12, 2003
**             Print the Fuel Surcharge and add it to the quote total.
**----------------------------------------------------------------------


TABLES: komk,                          "Communicationarea for conditions
        komp,                          "Communicationarea for conditions
        komv,
        komvd,                         "Communicationarea for conditions
        vbco3,                         "Communicationarea for view
        vbdka,                         "Headerview
        vbdpa,                         "Itemview
        vbdpau,                        "Subitemnumbers
        sadr,                          "Addresses
        tvko,                          "Sales organisation
        tvstt,                         "Plant name
        filename,                      "Path
        t001w,                         "Plants
        vbfa,                          "Document flow table
        adrs,                          "Communicationarea for Address
        kna1,
        konv,                          "conditions table
        usr01,
        tsp03,
        zinvoice01.

TABLES: adrc.  "Addresses (central address admin.)         "LABK925268

INCLUDE rvadtabl.

TABLES: thead.                 "SAPscript: Text Header     "LABK916978

* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*DATA: PATH(60) TYPE C.
DATA: retcode   LIKE sy-subrc.         "Returncode
DATA: repeat(1) TYPE c.
DATA: proposal(10).
DATA: fob_posnr(6) TYPE n.
DATA: fob_hi_qty LIKE vbap-kwmeng.
*data: fob_blend like vbap-arktx,
DATA: fob_blend(45) TYPE c,                                 "LABK929496
      text1 LIKE vbap-arktx.
DATA: fob_cement LIKE vbap-arktx.
DATA: fob_yield TYPE p DECIMALS 5.
DATA: bc_surchg LIKE zinvoice01-net_val.
DATA: quotation_no LIKE vbfa-vbelv.
DATA: total_tax LIKE komv-kwert.
DATA: o_type(1) TYPE c.
DATA: in_type LIKE vbak-vbtyp.      " doc. type of the order entered
DATA: re_type LIKE vbak-vbtyp.      " doc. type of the reference doc.
DATA: name2 LIKE sadr-name2,
      stras LIKE sadr-stras,
      ort01 LIKE sadr-ort01,
      regio LIKE sadr-regio,
      pstlz LIKE sadr-pstlz,
      bill_to_kunnr LIKE vbpa-kunnr.
DATA: total_pst LIKE konv-kwert.

DATA: BEGIN OF tvbdpa OCCURS 0.        "Internal table for items
        INCLUDE STRUCTURE vbdpa.
DATA: END OF tvbdpa.

DATA: BEGIN OF linetab OCCURS 0,
        matnr     LIKE vbap-matnr,
*{   REPLACE        CANK900302                                        1
*\        mat_desc  like zinvoice01-mat_desc,
        mat_desc(100)  TYPE c,
*}   REPLACE
        quantity  LIKE zinvoice01-quantity,
        uom       LIKE zinvoice01-uom,
        disc_per  LIKE zinvoice01-disc_per,
        disc_amt  LIKE zinvoice01-disc_amt,
        net_val   LIKE zinvoice01-net_val,
        sp_addr4  LIKE zinvoice01-sp_addr4,
        sp_city   LIKE zinvoice01-sp_city,
        sp_region LIKE zinvoice01-sp_region,
        sp_pcode  LIKE zinvoice01-sp_pcode,
        posnr     LIKE vbap-posnr,
*       UNDERLINE LIKE ZINVOICE01-UNDERLINE,
        general_flag LIKE zinvoice01-general_flag,          "LABK928153
        hm00_flag(1) TYPE c,
        c02_flag(1)  TYPE c,                                "LABK929390
        mvgr1     LIKE vbap-mvgr1,
        lines     LIKE zinvoice01-lines,
      END OF linetab.
DATA: BEGIN OF fob OCCURS 0,
*{   REPLACE        CANK900302                                        2
*\        mat_desc  like zinvoice01-mat_desc,
        mat_desc(100) TYPE c,
*}   REPLACE
        quantity  LIKE zinvoice01-quantity,
        uom       LIKE zinvoice01-uom,
        disc_per  LIKE zinvoice01-disc_per,
        disc_amt  LIKE zinvoice01-disc_amt,
        net_val   LIKE zinvoice01-net_val,
        sp_addr4  LIKE zinvoice01-sp_addr4,
        sp_city   LIKE zinvoice01-sp_city,
        sp_region LIKE zinvoice01-sp_region,
        sp_pcode  LIKE zinvoice01-sp_pcode,
        posnr     LIKE vbap-posnr,
        uepos     LIKE vbap-uepos,
        yield     TYPE p DECIMALS 5,
        kzwi2     LIKE vbap-kzwi2,
*       UNDERLINE LIKE ZINVOICE01-UNDERLINE,
        general_flag LIKE zinvoice01-general_flag,          "LABK928153
      END OF fob.
DATA: BEGIN OF eqp1 OCCURS 12,
*       unit like zunln-zunit,
        unit LIKE z2utl-unit,                               "LABK916958
      END OF eqp1.
DATA: BEGIN OF eqp2 OCCURS 12,
*       unit like zunln-zunit,
        unit LIKE z2utl-unit,                               "LABK916958
      END OF eqp2.
DATA: BEGIN OF emp1 OCCURS 10,
*       number like zprln-pernr,
        number LIKE z2utl-pernr,                            "LABK916958
        name   LIKE p0002-nachn,
*       class  like zprln-matnr,
        class  LIKE z2utl-percls,                           "LABK916958
      END OF emp1.
DATA: BEGIN OF emp2 OCCURS 10,
*       number like zprln-pernr,
        number LIKE z2utl-pernr,                            "LABK916958
        name   LIKE p0002-nachn,
*       class  like zprln-matnr,
        class  LIKE z2utl-percls,                           "LABK916958
      END OF emp2.

* gs2
DATA: BEGIN OF tvbap OCCURS 50.
        INCLUDE STRUCTURE vbdpa.
DATA: END OF tvbap.

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: BEGIN OF tvbdpau OCCURS 5.
        INCLUDE STRUCTURE vbdpau.
DATA: END   OF tvbdpau.
************************************************************************
* my additional data declarations
TABLES: vbak,                          "Sales Document: Header Data
*       tvv1t,                       "Customer Group 1: Description
        t25A2,                       "Customer Group 1: Description "ij1
        vbpa,
        prel,                          " employee names
        pa0001, "Replaces PREL (w/infty = 0001) for empl. names. - jb1
        pa0002, "Replaces PREL (w/infty = 0002) for empl. names. - jb1
        p0001,
        p0002,
*       zprln,                         " employee assignments
*       zunln,                         " unit assignments
        z2utl,                         "Bonus Utilization   "LABK916958
        vbap,
        zpara.
INCLUDE zvprtda1.     "  Global data required for sapscript printing

DATA: book_total TYPE p DECIMALS 2,    "book value - running total
      item_count(6) TYPE n,            "counter for # items
      strng(10) TYPE c,
      text80(80) TYPE c,
      tax_id(2) TYPE n VALUE '32',
      unit_count(6) TYPE n,
      total_net_val_for_item LIKE komvd-kwert,
      hm00_flag(1) TYPE c VALUE ' ',
      c02_flag(1)  TYPE c VALUE ' ',                        "LABK929390
      c02_header_flag(1) TYPE c VALUE ' ',                  "LABK929593
      header_hm00_flag(1) TYPE c VALUE ' ',                 " 950923 RV
      tkomvd_lines(3) TYPE p,
      quote_flag TYPE i VALUE 0,
      emp_count(2) TYPE n,
      itemid(12) TYPE c,                                    "gs2
      fob_parent TYPE i VALUE 0,                            "gs2
      text240(240) TYPE c.                                  "jb2

FIELD-SYMBOLS: .

* the titles for the various sales documents
DATA: title_a(35) VALUE 'Inquiry',
      title_b(35) VALUE 'Cost Estimate',
      title_c(35) VALUE 'Service Order',
      title_d(35) VALUE 'Product Proposal',
      title_g(35) VALUE 'Contract',
      title_h(35) VALUE 'Return Order',
      title_k(35) VALUE 'Credit Memo Request',
      title_l(35) VALUE 'Debit Memo Request'.

DATA: template(40),                                         " 950623 RV
      del_notes(78) TYPE c, len(2) TYPE n.
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.
* ******* data for reformat sales order output ****** "JL9605
DATA:
      BEGIN OF itemtab OCCURS 50,
        item_count(6) TYPE n,
        material_no LIKE vbdpa-matnr,
        arktx LIKE vbdpa-arktx,        "description JL1204
      END OF itemtab,
      quotation LIKE vbap-vgbel.

DATA: g_errmsg(55).                                         "LABK930879
************************************************************************
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

DATA: pr_kappl(01)   TYPE c VALUE 'V'. "Application for pricing

* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*DATA:   PREVIEW(2) TYPE C.                               "LABK915116
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


FORM entry USING return_code us_screen.
  CLEAR retcode.
*  CLEAR PREVIEW.                                      "LABK915116
  PERFORM processing USING us_screen.
*///////////////////////////////////////////////////////////////////////
** For sales order, get template from text field id 0006
*  CLEAR TEMPLATE.
*  IF VBDKA-VBTYP = 'C' OR O_TYPE = 'C'.                " Sales Order
*    IF O_TYPE = 'C'.
*      VBDKA-VBELN = NAST-OBJKY.
*    ENDIF.
*   PERFORM GET_HEADER_TEXT(ZVPRINT1) USING VBDKA-VBELN '0006' TEXTLINE.
*    IF TEXTLINE NE SPACE.
*      TEMPLATE = TEXTLINE.
*    ENDIF.
*  ENDIF.
** For Quotation, get template from text field id 0010
*
*  IF VBDKA-VBTYP = 'B'.
*   PERFORM GET_HEADER_TEXT(ZVPRINT1) USING VBDKA-VBELN '0010' TEXTLINE.
*    IF TEXTLINE NE SPACE.
*      TEMPLATE = TEXTLINE.
*    ENDIF.
*  ENDIF.
*  IF PREVIEW = 'NO'.                                     "LABK915116
**   Get microsoft access template name for this region
*  IF TEMPLATE = ' '.   "If no templates taken from text id 0006, & 0010
*      PERFORM GET_MS_ACCESS_TEMPLATE(ZVPRINT1) USING
*          VBAK-VKORG   VBAK-AUART   VBAK-VBTYP   VBAK-KVGR2   TEMPLATE.
*    ENDIF.
*  ENDIF.                                                 "LABK915116
** write print control data to file
*  PRINT_TABLE = ITCPO.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_CONTROL_DATA(ZVPRINT1) TABLES PRINT_TABLE USING
*              VBDKA-VBELN   SY-UNAME   TEMPLATE      'ZVDOQUO2'.
*    CLOSE DATASET DATAFILE.
*    PERFORM SAP_MOVER_FILE_SETUP(ZVPRINT1) USING DATAFILE.
*  ENDIF.                                                 "LABK915116

*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  IF retcode NE 0.
    return_code = 1.
  ELSE.
    return_code = 0.
  ENDIF.

ENDFORM.

*---------------------------------------------------------------------*
*       FORM PROCESSING                                               *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  PROC_SCREEN                                                   *
*---------------------------------------------------------------------*
FORM processing USING proc_screen.

  PERFORM get_data.
  CHECK retcode = 0.
**//////////////////////////////////////////////////////////////////////
*  PERFORM WRITE_HEADER_INFO_TO_FILE.
**\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  PERFORM form_open USING proc_screen vbdka-land1.
  CHECK retcode = 0.
*///////////////////////////////////////////////////////////////////////
  PERFORM write_header_info_to_file.
*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  PERFORM form_title_print.
  CHECK retcode = 0.
  PERFORM item_print.
  CHECK retcode = 0.
  PERFORM end_print.
  CHECK retcode = 0.
  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 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.

  PERFORM get_pricing_proc(zvprint1) TABLES tkomv tax_table   USING
                   nows_disc_amt   nows_disc_perc
                   price_adj_amt
                   3party_surchg_amt
                   prov_surchg_amt
                   mat_reimb_amt
                   0                   "  0 --> header prices
                   zbjs_amt.                                "gs5

  clear bc_surchg.                                         "LABK934576

* No bc surcharge for HM00 documents                       "LABK929587
  IF header_hm00_flag = ' '.                                "LABK929587
    bc_surchg = prov_surchg_amt.
  ELSE.                                                     "LABK929593
    IF c02_header_flag NE space.                            "LABK929593
      bc_surchg = c02_prov_surchg_amt.                      "LABK929593
    ENDIF.                                                  "LABK929593
  ENDIF.                                                    "LABK929587
*  IF PREVIEW = 'NO'.                                      "LABK915116
*    PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '01' '000001'
*                                                   '24' NOWS_DISC_PERC.
*    PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '01' '000001'
*                                                   '26' NOWS_DISC_AMT.
*    PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '01' '000001'
*                                                   '27' PRICE_ADJ_AMT.
*    PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '01' '000001'
*                                              '28' 3PARTY_SURCHG_AMT.
*  ENDIF.                                                  "LABK915116
*
* added HM00 output IF THERE IS ANY hm00 ON THE INVOICE       RV 950923
  IF header_hm00_flag NE ' '.                               " rv 950923
*    IF PREVIEW = 'NO'.                                      "LABK915116
*      PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '77' 'HM00'.
*    ENDIF.                                                  "LABK915116
  ENDIF.                                                    " rv 950923
*
* only write header surcharge if there is no HM00 in cond type
* after all the item_print processed
*  IF PROV_SURCHG_AMT NE 0 AND HM00_FLAG = ' '.              " 950923 RV
  IF prov_surchg_amt NE 0 AND header_hm00_flag = ' '.       " 950923 RV
*    IF PREVIEW = 'NO'.                                      "LABK915116
*      PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '01' '000001'
*                                                 '30' PROV_SURCHG_AMT.
*      PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001'
*                                       '31' 'BC Surcharge'.
*   ENDIF.                                                   "LABK915116
  ENDIF.

* Write the Mat reimbursement if there is one                " 950923 RV
*                                                            " 950923 RV
  IF mat_reimb_amt NE 0.                                    " 950923 RV
*   IF PREVIEW = 'NO'.                                       "LABK915116
*     PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '01' '000001'  " 950923 RV
*                               '79' MAT_REIMB_AMT.          " 950923 RV
*     PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001'    " 950923 RV
*                             '80' 'Material Reimbursement'. " 950923 RV
*   ENDIF.                                                   "LABK915116
  ENDIF.                                                    " 950923 RV
*
*  IF VBDKA-VBTYP = 'C' AND HM00_FLAG NE ' '.                " 950923 RV
* Sales Order & HM00 => write fixed price job though inv.cmt " 950923 RV
*     PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '74'" 950923 R
*                               'Fixed price job.'.          " 950923 RV
*  ENDIF.                                                    " 950923 RV

  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 PREVIEW = 'NO'.                                     "LABK915116
*      PERFORM WRITE_TO_FILEA(ZVPRINT1) USING '01' '000001'
*                                          TAX_ID    HEADER_TAX-KWERT.
*    ENDIF.                                                 "LABK915116
  ENDLOOP.

*  IF PREVIEW = 'NO'.                                     "LABK915116
**   write Net Total file
*  PERFORM WRITE_TO_FILEA(ZVPRINT1) USING '01' '000001' '32' KOMK-SUPOS.
**   write Final Amount to file
*  PERFORM WRITE_TO_FILEA(ZVPRINT1) USING '01' '000001' '38' KOMK-FKWRT.
*    PERFORM WRITE_TO_FILEA(ZVPRINT1) USING '01' '000001' '29'
*                                             TOTAL_NET_VAL_FOR_ITEM.
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '16'
*                                                DISC_PERC_TEXT.
*  ENDIF.                                                 "LABK915116

* Begin of gh3 -->
* Extract CO2 surcharge
  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_PRINT                                               *
*---------------------------------------------------------------------*
*       Printout of the items                                         *
*---------------------------------------------------------------------*

FORM item_print.
  DATA: material_no LIKE vbdpa-matnr, item_net_val LIKE komvd-kwert,
        net_val_per_unit LIKE komvd-kbetr,
        netvalperunit LIKE komvd-kbetr,
        itemnetval LIKE komvd-kwert,
        l_disc_count(2) TYPE n,                             "LABK930879
        l_man_count(2)  TYPE n.                             "LABK930879

  CLEAR itemtab.                                            "JL9606
  REFRESH itemtab.
  clear: hm00_flag, header_hm00_flag.                       "LABK934576
  LOOP AT tvbdpa.                                           "gs2
    IF NOT tvbdpa-abgru IS INITIAL.                         "LABK929390
      CONTINUE.                                             "LABK929390
    ENDIF.                                                  "LABK929390
    vbdpa = tvbdpa.
    IF vbdka-vbtyp = 'C' OR vbdka-vbtyp = 'B' OR vbdka-vbtyp = 'G'.
      CHECK vbdpa-pstyv NE 'TAQ'.                           "jt1
    ENDIF.
    MOVE-CORRESPONDING tvbdpa TO tvbap.                     "gs2
    APPEND tvbap.                                           "gs2
  ENDLOOP.
  fob_posnr = '999999'.
  fob_blend = ' '.
  fob_cement = ' '.
  CLEAR fob.
  REFRESH fob.
  CLEAR linetab.
  REFRESH linetab.                                          "gs2

  LOOP AT tvbdpa.
    IF NOT tvbdpa-abgru IS INITIAL.                         "LABK929390
      CONTINUE.                                             "LABK929390
    ENDIF.                                                  "LABK929390

    vbdpa = tvbdpa.

    CLEAR c02_flag.                                         "LABK929390

    IF vbdka-vbtyp = 'C'.
      CHECK vbdpa-pstyv NE 'TAQ'.                           "gs4
    ENDIF.
    IF vbdka-vbtyp = 'B' OR vbdka-vbtyp = 'G'.              "gs2
      CHECK vbdpa-pstyv NE 'TAQ'.                           "gs3
    ENDIF.                                                  "gs3
*
*   strip out leading zero's of material no
    WRITE vbdpa-matnr TO material_no NO-ZERO.
    MOVE vbdpa-arktx TO itemtab-arktx.                      "JL961204
    CONDENSE material_no.
    IF vbdpa-posnr_neu NE space.       "Item

*     get_item_prices will populate 2 tables, tkomv and tkomvd. TKOMV
*     contains taxes, nowsco disc, price adj, 3rd party surchage,
*     provincial surchg, and other info of the pricing procedure. TKOMVD
*     most of the time will return: 100 - Gross value, 400 - Net Value
*     for Item, 110 - Nowsco Disc. (also found in TKOMV). The most
*     important piece of info to us is 400 - Net Value for Item. This
*     info is NOT in TKOMVD when the pricing procedure contains either
*     HM00 (Order Value)  or PB00 (Price Gross) or (PB01) Fixed Price,
*     instead 420, 100, and 016 - Fixed Price are returned respectively.
*     These values will be used as substitute for 400. Please use ZTD7A
*     to examine these two tables when exceptions occur.

      PERFORM get_item_prices.

      item_count = item_count + 1.
*     IF PREVIEW = 'NO'.                                     "LABK915116
**         write Item Material No. to file
*          PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                                  '10' MATERIAL_NO.
*     ENDIF.                                                 "LABK915116
*     write Item category to file
      SELECT SINGLE * FROM vbap WHERE vbeln = vbdka-vbeln AND
                                                  posnr = vbdpa-posnr.
*     for quotes, write out ItemID string                           "gs2
      IF vbdka-vbtyp = 'B'.
        fob_parent = 0.
        PERFORM build_itemid USING itemid
                                   vbdpa-posnr
                                   vbdpa-fkrel
                                   vbdpa-uepos
                                   fob_parent.
*       IF PREVIEW = 'NO'.                                   "LABK915116
*              PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                                    '03' ITEMID.
*       ENDIF.                                               "LABK915116
      ENDIF.
*     if we have an fob parent, check to see if there is an fob
*     description to use in place of the regular material description
*     also, if there is a yield, download it
      IF fob_parent EQ 1.
        PERFORM get_item_text(zvprint1) USING vbdka-vbeln vbdpa-posnr
                                              '0053' textline.
        IF textline EQ ' '.
*         IF PREVIEW = 'NO'.                                 "LABK915116
*            PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                                  '13' VBDPA-ARKTX.
*         ENDIF.                                             "LABK915116
        ELSE.
*           use fob description
*          IF PREVIEW = 'NO'.                                "LABK915116
*            PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                                  '13' TEXTLINE.
*          ENDIF.                                            "LABK915116
          fob-mat_desc = textline.
          fob_posnr = vbdpa-posnr.
          fob_hi_qty = vbdpa-kwmeng.
          fob_blend = textline.
          fob-posnr = vbdpa-posnr.
          fob-uepos = vbdpa-uepos.
*         FOB-UNDERLINE = 'F'.
          fob-general_flag = 'F'.                           "LABK928153
          SPLIT textline AT '+' INTO fob_cement text1.
          PERFORM get_item_text(zvprint1) USING vbdka-vbeln vbdpa-posnr
                                                '0054' textline.
*          IF PREVIEW = 'NO'.                                "LABK915116
*            PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                                  '98' TEXTLINE.
*         ENDIF.                                             "LABK915116
          IF textline CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
            " Textline contains a non-numeric character.
            fob-yield = 0.
          ELSE.
            fob-yield = textline.
          ENDIF.
          APPEND fob.
        ENDIF.
        CLEAR textline.                                             "gh2
        PERFORM get_item_text(zvprint1) USING vbdka-vbeln vbdpa-posnr
                                              '0054' textline.
        IF NOT textline IS INITIAL.                                 "gh2
*        IF TEXTLINE NE ' '.
*         IF PREVIEW = 'NO'.                                 "LABK915116
*            PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                                '98' TEXTLINE.
*         ENDIF.                                             "LABK915116
*        ENDIF.                                                     "gs2
        ENDIF.                                                      "gh2
      ELSE.
*     write Item Text (Description) to file as usual
*        IF PREVIEW = 'NO'.                                 "LABK915116
*          PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                                '13' VBDPA-ARKTX.
*        ENDIF.                                              "LABK915116
      ENDIF.
*
**JL9605  IF VBDKA-VBTYP = 'C'.     "  Sales Order
*       check to see if this item is added manually by the user (i.e
*       it is not in the original quotation)
**JL9605    IF VBAP-VGBEL = SPACE AND VBAP-VGPOS = 0 AND QUOTE_FLAG = 1.
*         This item was added in manually by the user
**JL9605  PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT '41'
**JL9605                                                'ADDITIONAL'.
**JL9605  ENDIF.
**JL9605      ENDIF.
*      IF PREVIEW = 'NO'.                                   "LABK915116
**       write Item Category
*        PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT '14'
*                                                       VBAP-PSTYV.
**       write Item Pure Service
*        PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT '05'
*                                                       VBAP-MVGR1.
**       write per TNE quantity
*        PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT '44'
*                                                       VBAP-KDMAT.
**       write Item Quantity to file
*        PERFORM WRITE_TO_FILE(ZVPRINT1)
*                            USING '02' ITEM_COUNT '11' VBDPA-LSMENG.
**      write Item Unit of Measure to file
*
**     use sales unit for quote and sales unit of target for sales order
**      IF VBAK-VBTYP EQ 'B'.   "quote
**         PERFORM WRITE_TO_FILE(ZVPRINT1)
**                              USING '02' ITEM_COUNT '12' VBDPA-VRKME.
**      ELSE.
**         PERFORM WRITE_TO_FILE(ZVPRINT1)
**                              USING '02' ITEM_COUNT '12' VBDPA-ZIEME.
**      ENDIF.
*
**       use sales unit for all - GS
*        PERFORM WRITE_TO_FILE(ZVPRINT1)
*                              USING '02' ITEM_COUNT '12' VBDPA-VRKME.
**       write item net value to file
*        PERFORM WRITE_TO_FILEA(ZVPRINT1)
*                               USING '02' ITEM_COUNT '32' VBDPA-NETWR.
*      ENDIF.                  "if preview = 'NO'         "LABK915116

      total_after_tax = vbdpa-netwr.

*      increase our running total of the book value
*      BOOK_TOTAL = BOOK_TOTAL + KOMVD-KWERT.


      PERFORM get_pricing_proc(zvprint1) 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
                   vbdpa-posnr
                   zbjs_amt.                                "gs5

*     Start insert LABK930879
*     When a quote is created via TPRSS and the zdsc is later changed
*     manually at the header level, the previous item discount remains
*     resulting in two discounts.  In the case of two discounts, the
*     automatic entry does not take effect.  If the user does not
*     notice and decides to add another discount (which the system
*     allows), we may end up with multiple manual zdsc discounts for the
*     same item, in which case we alert them via an error message
*     printed in place of the item description.
      CLEAR: l_disc_count, l_man_count, g_errmsg.
      LOOP AT tkomv WHERE kinak <> 'X'
                      AND kposn = vbdpa-posnr
                      AND stunr = nows_disc.
        ADD 1 TO l_disc_count.
        IF ( tkomv-kherk = 'C' OR tkomv-kherk = 'D' ).
          ADD 1 TO l_man_count. "How many manual discs?
        ENDIF.
      ENDLOOP.
*     If more than one manual discount, print error message
      IF l_man_count > 1.
        CONCATENATE '**ERROR: THIS ITEM HAS MORE THAN ONE'
          'ACTIVE DISCOUNT!**'
          INTO g_errmsg SEPARATED BY ' '.
        zinvoice01-attention = 'X'.
      ELSEIF l_disc_count = 2.
        LOOP AT tkomv WHERE kinak <> 'X'
                        AND kposn = vbdpa-posnr
                        AND stunr  = nows_disc
                        AND kherk <> 'A'.
          nows_disc_perc = tkomv-kbetr.
          nows_disc_perc = ( nows_disc_perc / 10 )  "LABK931580
                              * -1.                 "LABK931580
*          nows_disc_perc = ( nows_disc_perc ) * -1. "LABK931012
        ENDLOOP.
      ENDIF.
*     End insert LABK930879

* 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 = vbdpa-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 = vbdpa-posnr.
          MOVE-CORRESPONDING tkomv TO tkomvd.
          APPEND tkomvd.
        ENDLOOP.
      ENDIF.

      PERFORM get_net_value_for_item(zvprint1) TABLES tkomvd USING
                   nows_disc_amt    nows_disc_perc
                   price_adj_amt    3party_surchg_amt   hm00_flag
                   item_net_val     net_val_per_unit
                   c02_flag.                                "LABK929390

      IF hm00_flag NE space.                                "LABK928153
        zinvoice01-hm00_flag = 'X'.                         "LABK928153
      ENDIF.                                                "LABK928153

*     Get bc surcharge for c02.  For MH00 docs, bc          "LABK929593
*     surcharge is still charged against c02.               "LABK929593
      IF c02_flag NE space.                                 "LABK929593
        c02_header_flag = 'X'.                              "LABK929593
        LOOP AT tkomvd WHERE kposn = vbdpa-posnr            "LABK929593
                        AND ( stunr = prov_surchg OR        "LABK929593
                              stunr = prov_surchg_2 ).      "LABK929593
          CHECK tkomvd-kinak NE 'X'.                        "LABK929593
          ADD tkomvd-kwert TO c02_prov_surchg_amt.          "LABK929593
        ENDLOOP.                                            "LABK929593
      ENDIF.                                                "LABK929593

      netvalperunit = net_val_per_unit.
      itemnetval = item_net_val.
      IF vbdpa-uepos EQ fob_posnr.
        PERFORM construct_fob_items USING netvalperunit itemnetval.
      ELSE.
        tvbdpa-posnr = '000000'.
        IF vbdpa-posnr EQ fob_posnr.
          tvbdpa-arktx = fob_blend.
          tvbdpa-posnr = fob_posnr.
        ENDIF.
        PERFORM construct_line_items USING netvalperunit itemnetval.
        IF tvbdpa-posnr EQ fob_posnr.
          MOVE fob_cement TO tvbdpa-arktx.
        MOVE fob_posnr TO tvbdpa-uepos.  " make sure it is a detail line
          PERFORM construct_fob_items USING netvalperunit itemnetval.
        ENDIF.
      ENDIF.

      IF vbdka-vbtyp = 'C'.            "  Sales Order
*       write Book Value per unit to file
        IF vbap-pstyv = 'ZFOB' OR vbap-pstyv = 'AGNN' OR
          vbap-pstyv = 'ZFOC' OR vbap-pstyv = 'TANN'.
*          IF PREVIEW = 'NO'.                                "LABK915116
*            PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02'   ITEM_COUNT
*                               '39' '              '.
*          ENDIF.                                            "LABK915116
        ELSE.
*          IF PREVIEW = 'NO'.                                "LABK915116
*            PERFORM WRITE_TO_FILEA(ZVPRINT1)
*                          USING '02' ITEM_COUNT '39' NET_VAL_PER_UNIT.
*          ENDIF.                                            "LABK915116
        ENDIF.
      ENDIF.

      IF nows_disc_perc NE 0.
        MOVE 'Disc. %' TO disc_perc_text.
      ENDIF.
*      IF PREVIEW = 'NO'.                                  "LABK915116
*        PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                                '24' NOWS_DISC_PERC.
*        PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                                '26' NOWS_DISC_AMT.
*        PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                                '27' PRICE_ADJ_AMT.
*        PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                                '28' 3PARTY_SURCHG_AMT.
*      ENDIF.                                              "LABK915116
      IF prov_surchg_amt NE 0.
*        IF PREVIEW = 'NO'.                                "LABK915116
*          PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                                '30' PROV_SURCHG_AMT.
*          PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02' ITEM_COUNT
*                                          '31' 'BC Surcharge'.
*        ENDIF.                                            "LABK915116
      ENDIF.
      IF mat_reimb_amt NE 0.                                " 950923 RV
*        IF PREVIEW = 'NO'.                                "LABK915116
*         PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT"950923
*                                         '30' MAT_REIMB_AMT." 950923 RV
*        ENDIF.                                            "LABK915116
      ENDIF.                                                " 950923 RV
      tax_id = 32.
      LOOP AT tax_table.
        ADD 1 TO tax_id.
        ADD tax_table-tax_amount TO total_after_tax.
        ADD tax_table-tax_amount TO total_tax.
*        IF PREVIEW = 'NO'.                                "LABK915116
*          PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                     TAX_ID  TAX_TABLE-TAX_AMOUNT.
*        ENDIF.                                            "LABK915116
      ENDLOOP.
      IF vbap-pstyv = 'ZFOB' OR vbap-pstyv = 'AGNN' OR
         vbap-pstyv = 'ZFOC' OR vbap-pstyv = 'TANN'.
*        IF PREVIEW = 'NO'.                                "LABK915116
*          PERFORM WRITE_TO_FILE(ZVPRINT1) USING '02'   ITEM_COUNT
*                               '29' '              '.
*        ENDIF.                                            "LABK915116
      ELSE.
        ADD item_net_val TO total_net_val_for_item.
*        IF HM00_FLAG NE SPACE.     " HM00 condition         " 950923 RV
*         CLEAR ITEM_NET_VAL.                                " 950923 RV
*       ENDIF.                                               " 950923 RV
*       IF PREVIEW = 'NO'.                                   "LABK915116
*          PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                              '29' ITEM_NET_VAL.
*       ENDIF.                                               "LABK915116
      ENDIF.
*     IF PREVIEW = 'NO'.                                     "LABK915116
*        PERFORM WRITE_TO_FILEA(ZVPRINT1)  USING '02' ITEM_COUNT
*                                              '38' TOTAL_AFTER_TAX.
*     ENDIF.                                                 "LABK915116
      CLEAR total_after_tax.
*
*     If any item has an HM00 condition set the header flag " 950923 RV
      IF hm00_flag NE space.                                " 950923 RV
        header_hm00_flag = 'Y'.                             " 950923 RV
*       IF PREVIEW = 'NO'.                                   "LABK915116
*         PERFORM WRITE_TO_FILE(ZVPRINT1)  USING '02'        " 950923 RV
*                                   ITEM_COUNT '16' 'HM00'.  " 950923 RV
*       ENDIF.                                               "LABK915116
      ENDIF.                                                " 950923 RV

*JB99
*     If the item has a ZBJS condition write to file.
      IF zbjs_flag NE 0 AND hm00_flag EQ space.
*       IF PREVIEW = 'NO'.                                   "LABK915116
*          PERFORM WRITE_TO_FILE(ZVPRINT1)  USING '02'
*                                     ITEM_COUNT '16' 'ZBJS'.
*       ENDIF.                                               "LABK915116
        CLEAR zbjs_flag.
      ENDIF.
*JB99

      MOVE item_count TO itemtab-item_count.                "JL9606
      MOVE material_no   TO itemtab-material_no.            "JL9606
      APPEND itemtab.                                       "JL9606
    ENDIF.
    CLEAR fob_blend.                                        "LABK929496
  ENDLOOP.
* write Total Book Value to file
* PERFORM WRITE_TO_FILEA(ZVPRINT1)
*                              USING  '01' '000001' '25' BOOK_TOTAL.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    IF QUOTE_FLAG = 1.                                        "JL9605
*      PERFORM WRITE_DATA_TO_ACCESS TABLES  ITEMTAB            "JL9605
*                       USING    QUOTATION  ITEM_COUNT.        "JL9605
*    ENDIF.                                                    "JL9605
*  ENDIF.                                                 "LABK915116

ENDFORM.

*---------------------------------------------------------------------*
*       FORM CONSTRUCT_LINE_ITEMS                                     *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  NET_VAL_UNIT                                                  *
*  -->  ITEM_NET_VAL                                                  *
*---------------------------------------------------------------------*
FORM construct_line_items USING net_val_unit LIKE zinvoice01-disc_amt
                                item_net_val LIKE zinvoice01-book_val.
  DATA: disc_perc LIKE linetab-disc_per.
  CLEAR linetab.

*"LABK931012->
*  disc_perc = nows_disc_perc * 10.
*  net_val_unit = net_val_unit * 10.
  disc_perc = nows_disc_perc.
  net_val_unit = net_val_unit.
*"LABK931012<-

  MOVE tvbdpa-matnr TO linetab-matnr.
  IF NOT fob_blend IS INITIAL.                              "LABK929496
    MOVE fob_blend TO linetab-mat_desc.                     "LABK929496
  ELSE.                                                     "LABK929496
    MOVE tvbdpa-arktx TO linetab-mat_desc.
  ENDIF.                                                    "LABK929496
* Start insert LABK930879
  IF NOT g_errmsg IS INITIAL.
    MOVE g_errmsg TO linetab-mat_desc.
  ENDIF.
* End insert LABK930879
  MOVE vbap-mvgr1 TO linetab-mvgr1.
  MOVE tvbdpa-lsmeng TO linetab-quantity.
  MOVE tvbdpa-vrkme TO linetab-uom.
  MOVE tvbdpa-posnr TO linetab-posnr.
  MOVE hm00_flag TO linetab-hm00_flag.
  IF disc_perc = 1000.                                      "LABK930234
    disc_perc = ' '.                                        "LABK930234
  ENDIF.                                                    "LABK930234
  IF zbjs_flag NE 0.
    MOVE 0 TO linetab-disc_per.  " ZBJS - discount % doesn't apply.
  ELSE.
    MOVE disc_perc TO linetab-disc_per.
  ENDIF.
  IF tkomvd-kschl = 'PB01'.
*   a fixed price item. Enter the net value for consistency.
*   linetab-disc_amt = item_net_val * 10. "LABK931012
    linetab-disc_amt = item_net_val.                        "LABK931012
  ELSE.
*   Calculate the disc unit price because if the item     "LABK930234
*   ZDBP condition is 0, the item net value is            "LABK930234
*   returned for unit net value. At this point it will    "LABK930234
*   be transferred to the disc unit price.                "LABK930234
    linetab-disc_amt =                                      "LABK930234
*"LABK931012->
*    ( item_net_val / linetab-quantity ) * 10             "LABK930234
     ( item_net_val / linetab-quantity ).                   "LABK930234
*"LABK931012<-

*    move net_val_unit to linetab-disc_amt.
  ENDIF.
  MOVE item_net_val TO linetab-net_val.
* MOVE 'H' TO LINETAB-UNDERLINE.
  MOVE 'H' TO linetab-general_flag.                         "LABK928153
  IF vbak-vbtyp = 'K' OR vbak-vbtyp = 'L' OR vbak-vbtyp = 'H'.
    SELECT SINGLE * FROM konv
           WHERE knumv = vbak-knumv
             AND kposn = vbap-posnr
             AND stunr = '502'
             AND zaehk = 07.     " PST related item.
    IF konv-kwert NE 0.
      linetab-lines = '*'.
      total_pst = total_pst + konv-kwert.
    ELSE.
      linetab-lines = ' '.
    ENDIF.
  ENDIF.
* Begin insert LABK929390 -->
  IF NOT c02_flag IS INITIAL.
    linetab-c02_flag = 'X'.
  ENDIF.
* --> End insert LABK929390
  APPEND linetab.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM CONSTRUCT_FOB_ITEMS                                      *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  NET_VAL_UNIT                                                  *
*  -->  ITEM_NET_VAL                                                  *
*---------------------------------------------------------------------*
FORM construct_fob_items USING net_val_unit LIKE komvd-kbetr
                               item_net_val LIKE zinvoice01-book_val.

  DATA: disc_perc LIKE linetab-disc_per.
  CLEAR fob.
*  disc_perc = nows_disc_perc * 10.  "LABK931012
  disc_perc = nows_disc_perc.                               "LABK931012

  MOVE tvbdpa-arktx TO fob-mat_desc.
  SELECT SINGLE * FROM vbap
   WHERE vbeln = vbdka-vbeln
     AND posnr = vbdpa-posnr.
  IF vbap-kdmat = 0.
    vbap-kdmat = 1.
  ENDIF.
*  move vbap-kdmat to fob-quantity.
  fob-quantity = vbap-kwmeng / fob_hi_qty.
  item_net_val = vbap-netpr * vbap-kdmat.
*  net_val_unit = vbap-netpr * 10.
  MOVE tvbdpa-vrkme TO fob-uom.
  IF nows_disc_perc = 100.                                  "LABK930598
    disc_perc = ' '.                                        "LABK930598
  ENDIF.                                                    "LABK930598
  MOVE disc_perc TO fob-disc_per.

*  move item_net_val to fob-net_val.
  disc_perc = nows_disc_perc / 100.
  disc_perc = 1 - disc_perc.
  fob-net_val = vbap-kzwi1 * disc_perc.
  fob-net_val = fob-net_val / fob_hi_qty.

  IF nows_disc_perc = 100.                                  "LABK930598
*   Calculate the disc unit price because if the item       "LABK930598
*   ZDBP condition is 0, the item net value is              "LABK930598
*   returned for unit net value. At this point it will      "LABK930598
*   be transferred to the disc unit price.                  "LABK930598
*"LABK931012->
*    fob-disc_amt = ( fob-net_val / fob-quantity ) * 10. "LABK930598
    fob-disc_amt = ( fob-net_val / fob-quantity ).          "LABK930598
*"LABK931012<-
  ELSE.                                                     "LABK930598
*"LABK931012->
*    if net_val_unit > 100.
*      move net_val_unit to fob-disc_amt.
*    else.
*      fob-disc_amt = net_val_unit * 10.
*    endif.

    IF vbap-meins <> vbap-vrkme.
      fob-disc_amt = ( fob-net_val / fob-quantity ).
    ELSE.
      MOVE net_val_unit TO fob-disc_amt.
    ENDIF.

*"LABK931012<-
  ENDIF.                                                    "LABK930598

  fob-kzwi2 = vbap-kzwi2.
  MOVE tvbdpa-uepos TO fob-uepos.
  MOVE tvbdpa-posnr TO fob-posnr.
* MOVE 'F' TO FOB-UNDERLINE.
  MOVE 'F' TO fob-general_flag.                             "LABK928153
  APPEND fob.
ENDFORM.

* To prepare data for reformatting sales order
*
INCLUDE zvrefsi1.                                           "JL9605


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

FORM sender.

  SELECT SINGLE * FROM tvko  WHERE vkorg = vbdka-vkorg.
  CHECK sy-subrc = 0.
  SELECT SINGLE * FROM sadr WHERE adrnr = tvko-adrnr
                            AND   natio = space.
  vbdka-sland = sadr-land1.
ENDFORM.

*-----------------------------------------------------------------------
*    ACTIVATE_ELEMENTS
*-----------------------------------------------------------------------
FORM activate_elements.
  DATA: typ LIKE vbak-vbtyp.
  DATA: matnr_found TYPE i.

  zinvoice01-mixed = ' '.
  o_type = ' '.
  SELECT SINGLE vbtyp FROM vbak INTO typ WHERE vbeln = vbdka-vbeln.
  IF typ EQ 'C' AND tnapr-fonam = 'ZV_NOWSCO_QUOTE'.
*  There is a preceding document associated with this service order.
*  If the preceding document is a quotation, then this service order
*  will be printed on a quotation form by using the layout set
*  'ZV_NOWSCO_QUOTE'.
    o_type = 'C'.
    nast-kschl = 'ZQ01'.   " output type is quotation.
    typ = 'B'.
    vbdka-vbeln = quotation_no.
    zinvoice01-mixed = '*'.
    SELECT * FROM vbap WHERE vbeln = quotation_no.
      CHECK vbap-pstyv NE 'TAQ'.
      matnr_found = 0.
      LOOP AT linetab.
*      if linetab-matnr eq vbap-matnr.
        IF linetab-mat_desc EQ vbap-arktx.
          matnr_found = 1.
          EXIT.
        ENDIF.
      ENDLOOP.
      IF matnr_found = 0.
*      Add the proposed items to the table.
        CLEAR linetab.
        MOVE vbap-matnr TO linetab-matnr.
        MOVE vbap-arktx TO linetab-mat_desc.
        MOVE 0 TO linetab-quantity.
        MOVE vbap-vrkme TO linetab-uom.
        MOVE vbap-posnr TO linetab-posnr.
*      MOVE 'H' TO LINETAB-UNDERLINE.
        MOVE 'H' TO linetab-general_flag.                   "LABK928153
        APPEND linetab.
      ENDIF.
    ENDSELECT.
  ENDIF.
  CASE typ.
    WHEN 'B' OR 'G'.
      PERFORM print_quotation.
    WHEN 'C'.
      IF vbak-auart = 'ZT' OR vbak-auart = 'ZZT'.
        PERFORM print_template.
      ELSE.
        PERFORM print_service_order.
      ENDIF.
    WHEN 'K' OR 'L' OR 'H'.
      PERFORM print_memo_request.
  ENDCASE.
ENDFORM.

*-----------------------------------------------------------------------
*    print a quotation
*-----------------------------------------------------------------------
FORM print_quotation.
  DATA: item TYPE i, gr2 TYPE i, total_price LIKE linetab-net_val,
        cnt TYPE i, first_loop(1) TYPE n, i(3) TYPE n,
        total_fob_net LIKE linetab-net_val.
  DATA: allowable_lns_left TYPE i,                          "LABK915116
        total_lns TYPE i,                                   "LABK915116
        lns_left TYPE i,                                    "LABK915116
        newpage_flag(1) VALUE 'X'.                          "LABK915116

* Begin insert LABK929390 -->
  DATA: BEGIN OF i_temptab OCCURS 0,
          matnr     LIKE vbap-matnr,
          mat_desc  LIKE zinvoice01-mat_desc,
          quantity  LIKE zinvoice01-quantity,
          uom       LIKE zinvoice01-uom,
          disc_per(20) TYPE c,
          disc_amt(20) TYPE c,
          net_val   LIKE zinvoice01-net_val,
          sp_addr4  LIKE zinvoice01-sp_addr4,
          sp_city   LIKE zinvoice01-sp_city,
          sp_region LIKE zinvoice01-sp_region,
          sp_pcode  LIKE zinvoice01-sp_pcode,
          posnr     LIKE vbap-posnr,
          general_flag LIKE zinvoice01-general_flag,
          hm00_flag(1) TYPE c,
          mvgr1     LIKE vbap-mvgr1,
          c02_flag(1)  TYPE c,
          lines     LIKE zinvoice01-lines,
        END OF i_temptab.

  DATA: BEGIN OF i_additional_tab OCCURS 0.
          INCLUDE STRUCTURE linetab.
  DATA: END OF i_additional_tab.

  DATA: l_addtl_count TYPE i.

  CLEAR:  zinvoice01-sum2, zinvoice01-sum1.
* --> End insert LABK929390

  item = 0.

* Activate header elements
  zinvoice01-order_num = proposal.
* zinvoice01-mixed = ' '.
  LOOP AT linetab.
    IF linetab-posnr NE '000000'.
      cnt = 0.
      LOOP AT fob.
        IF fob-uepos EQ linetab-posnr.
          total_price = total_price + fob-net_val.
          total_fob_net = total_fob_net + fob-kzwi2.
          cnt = cnt + 1.
        ENDIF.
      ENDLOOP.
*      linetab-disc_amt = total_price * 10.  "LABK931012
      linetab-disc_amt = total_price.                       "LABK931012

      IF cnt = 1.
        " no lower level item associated with this line item.
        linetab-net_val = linetab-quantity * total_price.
      ELSE.
        " total net price = total of net values of all components.
        linetab-net_val = total_fob_net.
      ENDIF.
      total_price = 0.
      total_fob_net = 0.
      MODIFY linetab.
    ENDIF.
  ENDLOOP.

  SORT linetab BY matnr quantity ASCENDING.

* Begin insert LABK929390 -->
* Lines items of same material and discount % should not be listed
* separately, but should be summed.
  REFRESH i_temptab.  CLEAR i_temptab.
  LOOP AT linetab.
    MOVE-CORRESPONDING linetab TO i_temptab.
    COLLECT i_temptab.
    CLEAR i_temptab.
  ENDLOOP.
  REFRESH linetab.  CLEAR linetab.
  LOOP AT i_temptab.
    MOVE-CORRESPONDING i_temptab TO linetab.
    APPEND linetab.
    CLEAR linetab.
  ENDLOOP.

* If it's an HM00 and there are c02 lines, list them separately
* under new section - 'Additional items'.
*  if not hm00_flag is initial.
  IF NOT header_hm00_flag IS INITIAL.                       "LABK929508
    LOOP AT linetab.
      IF linetab-c02_flag IS INITIAL.
        ADD linetab-net_val TO zinvoice01-sum1.
      ELSE.
        MOVE-CORRESPONDING linetab TO i_additional_tab.
        ADD i_additional_tab-net_val TO zinvoice01-sum2.
        APPEND i_additional_tab.
        CLEAR i_additional_tab.
      ENDIF.
    ENDLOOP.
    IF sy-subrc = 0.
      DELETE linetab WHERE NOT c02_flag IS initial.
    ENDIF.
  ENDIF.

* --> End insert LABK929390

  LOOP AT linetab.
    MOVE-CORRESPONDING linetab TO zinvoice01.
    MOVE linetab-matnr TO zinvoice01-mat_num.               "labk917025
    IF linetab-disc_amt EQ 0 AND linetab-net_val NE 0.
      " PB01 - fixed price item.
*     zinvoice01-disc_amt = linetab-net_val * 10. "LABK931012
      zinvoice01-disc_amt = linetab-net_val.                "LABK931012
    ENDIF.
* Dont print discount perc or disc amount for c02 surcharge "LABK929508
    IF NOT linetab-c02_flag IS INITIAL.                     "LABK929508
      CALL FUNCTION 'WRITE_FORM'                            "LABK929508
             EXPORTING                                      "LABK929508
                  element = 'LINE3'                         "LABK929508
                  window  = 'MAIN'                          "LABK929508
             EXCEPTIONS                                     "LABK929508
                  OTHERS  = 01.                             "LABK929508
    ELSEIF zinvoice01-disc_amt NE 0.                        "LABK929508
*    if zinvoice01-disc_amt ne 0.
      CALL FUNCTION 'WRITE_FORM'
           EXPORTING
                element = 'LINE1'
                window  = 'MAIN'
           EXCEPTIONS
                OTHERS  = 01.
    ELSE.
      CALL FUNCTION 'WRITE_FORM'
           EXPORTING
                element = 'LINE2'
                window  = 'MAIN'
           EXCEPTIONS
                OTHERS  = 01.
    ENDIF.
    total_price = total_price + linetab-net_val.
  ENDLOOP.

* Start delete LABK929390 -->
*  clear zinvoice01-lines.                                   "LABK915116
*  describe table linetab lines zinvoice01-lines.            "LABK915116
*  if zinvoice01-lines >= 30.    "total lines in window      "LABK915116
*    zinvoice01-lines = ( zinvoice01-lines mod 30 ).         "LABK915116
*    if zinvoice01-lines <> 0.                               "LABK915116
*      zinvoice01-underline = ' '.                           "LABK915116
*    endif.                                                  "LABK915116
*  endif.                                                    "LABK915116
*  if zinvoice01-lines <> 0.                                 "LABK915116
**    ADD 10 TO ZINVOICE01-LINES.   "total, comments    "LABK915116
*    add 7 to zinvoice01-lines.                              "LABK929345
*  endif.                                                    "LABK915116
*  if ( zinvoice01-lines >= 30 ).                            "LABK915116
*    zinvoice01-lines = 0.                                   "LABK915116
*  endif.                                                    "LABK915116


*  call function 'WRITE_FORM'                                "LABK915116
*       exporting                                            "LABK915116
*            element = 'WRITE_LINE'                          "LABK915116
*            window  = 'MAIN'                                "LABK915116
*       exceptions                                           "LABK915116
*            others  = 01.                                   "LABK915116
*
* --> End delete LABK929390

* Begin insert LABK929390 -->
* Print any additional items (c02) if any. This happens if it's an HM00.
  DESCRIBE TABLE i_additional_tab LINES l_addtl_count.

  IF l_addtl_count > 0.

* If it's an HM00, print a 'Fixed Price' line as we do with the invoices
* if there are also additional (c02) items.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'FIXED_PRICE_WITH_ADDITIONAL'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.

    LOOP AT i_additional_tab.
      MOVE-CORRESPONDING i_additional_tab TO zinvoice01.
      MOVE i_additional_tab-matnr TO zinvoice01-mat_num.
      IF i_additional_tab-disc_amt EQ 0 AND
         i_additional_tab-net_val NE 0.  " PB01 - fixed price item.
*       zinvoice01-disc_amt = i_additional_tab-net_val * 10. "LABK931012
        zinvoice01-disc_amt = i_additional_tab-net_val.     "LABK931012

      ENDIF.
      AT FIRST.
        CALL FUNCTION 'WRITE_FORM'
             EXPORTING
                  element = 'ADDITIONAL_INTRO'
                  window  = 'MAIN'
             EXCEPTIONS
                  OTHERS  = 01.
      ENDAT.
      CALL FUNCTION 'WRITE_FORM'
           EXPORTING
                element = 'ADDITIONAL_LINE1'
                window  = 'MAIN'
           EXCEPTIONS
                OTHERS  = 01.
      total_price = total_price + i_additional_tab-net_val.
    ENDLOOP.

    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'ADDITIONAL_TOTAL'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.

  ENDIF.
* --> End insert LABK929390

  zinvoice01-underline = 'X'.                               "LABK915116

*{ Start insert LABK934576
  IF fuel_surchg_amt NE 0.
    MOVE '   Fuel Surcharge' TO zinvoice01-hfuelsur_des.
    MOVE fuel_surchg_amt TO zinvoice01-hfuelsur_amt.
  ELSE.
    MOVE ' ' TO zinvoice01-hfuelsur_des.
    MOVE 0 TO zinvoice01-hfuelsur_amt.
  ENDIF.
*} End Insert LABK934576

  IF bc_surchg NE 0.
    MOVE '   BC Surcharge' TO zinvoice01-disc.
    MOVE bc_surchg TO zinvoice01-tt_af_sur.
  ELSE.
    MOVE ' ' TO zinvoice01-disc.
    MOVE 0 TO zinvoice01-tt_af_sur.
  ENDIF.
*{ Start Replace LABK934576
*  zinvoice01-htt_af_sur = total_price + bc_surchg.
  zinvoice01-htt_af_sur = total_price + bc_surchg + fuel_surchg_amt.
*} End Replace LABK934576

*  CALL FUNCTION 'WRITE_FORM' "deleted LABK915116
*       EXPORTING
*            ELEMENT = 'LINE2'
*            WINDOW  = 'TOTAL'
*       EXCEPTIONS
*            OTHERS  = 01.


  CALL FUNCTION 'WRITE_FORM'                                "LABK915116
       EXPORTING                                            "LABK915116
            element = 'TOTAL'                               "LABK915116
            window  = 'MAIN'                                "LABK915116
       EXCEPTIONS                                           "LABK915116
            OTHERS  = 01.                                   "LABK915116


* We're now at the last page before FOB pages               "LABK915116
  zinvoice01-last_page = 'X'.                               "LABK915116
  CLEAR zinvoice01-lines.                                   "LABK915116

  CLEAR: total_lns, lns_left, allowable_lns_left.           "LABK915116

  READ TABLE fob INDEX 1.
  IF sy-subrc EQ 0.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'NEWPAGE'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    total_price = 0.
    first_loop = 0.
    DESCRIBE TABLE fob LINES total_lns.                     "LABK915116
    LOOP AT fob WHERE mat_desc CS '+'.                      "LABK915116
      ADD 3 TO total_lns.                                   "LABK915116
    ENDLOOP.
    LOOP AT fob.
      IF fob-mat_desc NS '+'.                               "LABK915116
        ADD 1 TO zinvoice01-lines.                          "LABK915116
      ENDIF.                                                "LABK915116
      MOVE-CORRESPONDING fob TO zinvoice01.
*{   INSERT         CANK900302                                        1
      MOVE fob-mat_desc TO zinvoice01-fob_mat_desc.
*}   INSERT
      IF fob-uepos EQ '000000'.
        IF first_loop EQ 1.
          PERFORM print_fob_total USING total_price.
*{   INSERT         CANK900302                                        2
          CLEAR zinvoice01-inv_commts.
*}   INSERT
          clear i.                                          "LABK931580
          WHILE i < 225.
            i = i + 1.
            zinvoice01-inv_commts+i = '_'.
          ENDWHILE.
          IF zinvoice01-lines > 26.                         "LABK915116
            CLEAR zinvoice01-lines.                         "LABK915116
            CALL FUNCTION 'WRITE_FORM'                      "LABK915116
                 EXPORTING                                  "LABK915116
                   element = 'WRITE_LINE_FOB_2'             "LABK915116
                   window  = 'MAIN'                         "LABK915116
                 EXCEPTIONS                                 "LABK915116
                   OTHERS  = 01.                            "LABK915116
          ENDIF.                                            "LABK915116
* If there is more than one FOB and the last one on a page  "LABK915116
* would be split between pages, output the whole thing to   "LABK915116
* a next page. Check halfway down the page before the next  "LABK915116
* blend description.                                        "LABK915116
          IF zinvoice01-lines > 13 AND                      "LABK915116
            NOT newpage_flag IS INITIAL.                    "LABK915116
            lns_left = total_lns - zinvoice01-lines.        "LABK915116
            allowable_lns_left = 26 - zinvoice01-lines.     "LABK915116
            IF lns_left > allowable_lns_left.               "LABK915116
              CALL FUNCTION 'WRITE_FORM'                    "LABK915116
                   EXPORTING                                "LABK915116
                     element = 'WRITE_LINE_FOB_2'           "LABK915116
                     window  = 'MAIN'                       "LABK915116
                   EXCEPTIONS                               "LABK915116
                     OTHERS  = 01.                          "LABK915116
              CALL FUNCTION 'WRITE_FORM'                    "LABK915116
                   EXPORTING                                "LABK915116
                      element = 'NEWPAGE'                   "LABK915116
                      window  = 'MAIN'                      "LABK915116
                   EXCEPTIONS                               "LABK915116
                      OTHERS  = 01.                         "LABK915116
              newpage_flag = ' '.                           "LABK915116
              CLEAR: lns_left, allowable_lns_left,          "LABK915116
                     zinvoice01-lines.                      "LABK915116
            ELSE.                                           "LABK915116
              CALL FUNCTION 'WRITE_FORM'                    "LABK915116
                  EXPORTING                                 "LABK915116
                       element = 'UNDERLINE'                "LABK915116
                       window  = 'MAIN'                     "LABK915116
                  EXCEPTIONS                                "LABK915116
                       OTHERS  = 01.                        "LABK915116
            ENDIF.                                          "LABK915116
          ELSE.                                             "LABK915116
            CALL FUNCTION 'WRITE_FORM'
                 EXPORTING
                      element = 'UNDERLINE'
                      window  = 'MAIN'
                 EXCEPTIONS
                      OTHERS  = 01.
          ENDIF.                                            "LABK915116


        ENDIF.
        fob_yield = fob-yield.
        MOVE-CORRESPONDING fob TO zinvoice01.
        ADD 1 TO zinvoice01-lines.                          "LABK915116
        IF zinvoice01-lines > 26.                           "LABK915116
          CLEAR zinvoice01-lines.                           "LABK915116
          CALL FUNCTION 'WRITE_FORM'                        "LABK915116
               EXPORTING                                    "LABK915116
                 element = 'WRITE_LINE_FOB_2'               "LABK915116
                 window  = 'MAIN'                           "LABK915116
               EXCEPTIONS                                   "LABK915116
                 OTHERS  = 01.                              "LABK915116
        ENDIF.                                              "LABK915116
        CALL FUNCTION 'WRITE_FORM'
             EXPORTING
                  element = 'BLEND'
                  window  = 'MAIN'
             EXCEPTIONS
                  OTHERS  = 01.
      ELSE.
        IF zinvoice01-lines > 26.                           "LABK915116
          CLEAR zinvoice01-lines.                           "LABK915116
          CALL FUNCTION 'WRITE_FORM'                        "LABK915116
               EXPORTING                                    "LABK915116
                 element = 'WRITE_LINE_FOB_2'               "LABK915116
                 window  = 'MAIN'                           "LABK915116
               EXCEPTIONS                                   "LABK915116
                 OTHERS  = 01.                              "LABK915116
        ENDIF.                                              "LABK915116

        CALL FUNCTION 'WRITE_FORM'
             EXPORTING
                  element = 'FOBLINE'
                  window  = 'MAIN'
             EXCEPTIONS
                  OTHERS  = 01.
        total_price = total_price + fob-net_val.
        first_loop = 1.
      ENDIF.
    ENDLOOP.
    PERFORM print_fob_total USING total_price.

* Clear last_page here because it (sapscript) seems to      "LABK915116
* start printing the main line items at this point          "LABK915116
    zinvoice01-last_page = ' '.                             "LABK915116

    CALL FUNCTION 'WRITE_FORM'                              "LABK915116
         EXPORTING                                          "LABK915116
              element = 'WRITE_LINE_FOB_2'                  "LABK915116
              window  = 'MAIN'                              "LABK915116
         EXCEPTIONS                                         "LABK915116
              OTHERS  = 01.                                 "LABK915116
  ENDIF.


ENDFORM.

*---------------------------------------------------------------------*
*       FORM print_service_order                                      *
*---------------------------------------------------------------------*
*       This routine prints a service order                           *
*---------------------------------------------------------------------*

FORM print_service_order.
  DATA: total_price LIKE linetab-net_val,
        cnt TYPE i.
  DATA: line_cnt TYPE i.                                    "LABK915255

  zinvoice01-bp_addr1 = ' '.
  zinvoice01-bp_addr2 = ' '.
  zinvoice01-bp_addr3 = ' '.
  zinvoice01-bp_addr4 = ' '.
  IF kna1-name2 EQ ' '.
    MOVE kna1-stras TO zinvoice01-bp_addr1.
    CONCATENATE kna1-ort01 ', ' kna1-regio INTO zinvoice01-bp_addr2.
    MOVE kna1-pstlz TO zinvoice01-bp_addr3.
  ELSE.
    MOVE kna1-name2 TO zinvoice01-bp_addr1.
    MOVE kna1-stras TO zinvoice01-bp_addr2.
    CONCATENATE kna1-ort01 ', ' kna1-regio INTO zinvoice01-bp_addr3.
    MOVE kna1-pstlz TO zinvoice01-bp_addr4.
  ENDIF.
  SORT linetab ASCENDING BY matnr quantity.
  TRANSLATE vbak-bstnk TO UPPER CASE.
  TRANSLATE sadr-ort01 TO UPPER CASE.
  TRANSLATE sadr-name1 TO UPPER CASE.
  IF re_type = 'B'.
    zinvoice01-tax_flag = '*'.
  ELSE.
    zinvoice01-tax_flag = ' '.
  ENDIF.
  TRANSLATE zinvoice01-sp_addr1 TO UPPER CASE.
  TRANSLATE zinvoice01-sp_addr2 TO UPPER CASE.
  CLEAR line_cnt.                                           "LABK915255
  LOOP AT linetab.
    MOVE-CORRESPONDING linetab TO zinvoice01.
    IF zinvoice01-disc_amt = 0.
      zinvoice01-mixed = '*'.
    ELSE.
      zinvoice01-mixed = ' '.
    ENDIF.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'LINE1'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    ADD 1 TO line_cnt.                                      "LABK915255
    IF line_cnt = 28.                                       "LABK915255
      CALL FUNCTION 'WRITE_FORM'                            "LABK915255
           EXPORTING                                        "LABK915255
                element = 'UNDERLINE_ITEMS'                 "LABK915255
                window  = 'MAIN'                            "LABK915255
           EXCEPTIONS                                       "LABK915255
                OTHERS  = 01.                               "LABK915255
      CLEAR line_cnt.                                       "LABK915255
    ENDIF.                                                  "LABK915255
    total_price = total_price + linetab-net_val.
  ENDLOOP.

  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'LINE2'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
  ADD 1 TO line_cnt.                                        "LABK915255
  IF line_cnt = 28.                                         "LABK915255
    CALL FUNCTION 'WRITE_FORM'                              "LABK915255
         EXPORTING                                          "LABK915255
              element = 'UNDERLINE'                         "LABK915255
              window  = 'MAIN'                              "LABK915255
         EXCEPTIONS                                         "LABK915255
              OTHERS  = 01.                                 "LABK915255
    CLEAR line_cnt.                                         "LABK915255
  ENDIF.                                                    "LABK915255

  cnt = 0.
  WHILE cnt < 6.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'LINE3'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    cnt = cnt + 1.
    ADD 1 TO line_cnt.                                      "LABK915255
    IF line_cnt = 28.                                       "LABK915255
      CALL FUNCTION 'WRITE_FORM'                            "LABK915255
           EXPORTING                                        "LABK915255
                element = 'UNDERLINE'                       "LABK915255
                window  = 'MAIN'                            "LABK915255
           EXCEPTIONS                                       "LABK915255
                OTHERS  = 01.                               "LABK915255
      CLEAR line_cnt.                                       "LABK915255
    ENDIF.                                                  "LABK915255
  ENDWHILE.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'UNDERLINE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'ESTIMATE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.

  CLEAR zinvoice01-last_page.
* Flag for writing notes and signatures box on last page before page
* 'TWO'.
  zinvoice01-last_page = 'X'.


  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'NEWPAGE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
  cnt = 1.
  WHILE cnt < 13.
    READ TABLE eqp1 INDEX cnt.
    IF sy-subrc EQ 0.
      MOVE eqp1-unit TO zinvoice01-dir1.
    ELSE.
      zinvoice01-dir1 = ' '.
    ENDIF.
    READ TABLE eqp2 INDEX cnt.
    IF sy-subrc EQ 0.
      MOVE eqp2-unit TO zinvoice01-dir2.
    ELSE.
      zinvoice01-dir2 = ' '.
    ENDIF.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'SERVICE'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    cnt = cnt + 1.
  ENDWHILE.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'EMPTITLE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
  cnt = 1.
  LOOP AT emp1.
*   concatenate emp1-number+4(4) emp1-name
    CONCATENATE emp1-number+2(6) emp1-name                  "LABK916974
                            INTO zinvoice01-comment1 SEPARATED BY space.
    MOVE emp1-class TO zinvoice01-comment2.
    READ TABLE emp2 INDEX cnt.
    IF sy-subrc EQ 0.
*     concatenate emp2-number+4(4) emp2-name
      CONCATENATE emp2-number+2(6) emp2-name                "LABK916974
                            INTO zinvoice01-comment3 SEPARATED BY space.
      MOVE emp2-class TO zinvoice01-comment4.
    ELSE.
      zinvoice01-comment3 = ' '.
      zinvoice01-comment4 = ' '.
    ENDIF.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'EMPINFO1'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    cnt = cnt + 1.
  ENDLOOP.
  WHILE cnt < 11.
    zinvoice01-comment1 = ' '.
    zinvoice01-comment2 = ' '.
    zinvoice01-comment3 = ' '.
    zinvoice01-comment4 = ' '.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'EMPINFO1'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    cnt = cnt + 1.
  ENDWHILE.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'EMP_UNDERLINE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM print_memo_request                                       *
*---------------------------------------------------------------------*
*       This routine prints a credit/debit memo request               *
*---------------------------------------------------------------------*

FORM print_memo_request.
  DATA: total_price LIKE linetab-net_val, temp LIKE linetab-net_val,
        cnt TYPE i.

  zinvoice01-cust_num = bill_to_kunnr.
  IF name2 EQ ' '.
    MOVE stras TO zinvoice01-bp_addr1.
    CONCATENATE ort01 ', ' regio INTO zinvoice01-bp_addr2.
    MOVE pstlz TO zinvoice01-bp_addr3.
  ELSE.
    MOVE name2 TO zinvoice01-bp_addr1.
    MOVE stras TO zinvoice01-bp_addr2.
    CONCATENATE ort01 ', ' regio INTO zinvoice01-bp_addr3.
    MOVE pstlz TO zinvoice01-bp_addr4.
  ENDIF.
  LOOP AT linetab.
*   make sure the first material of "Frac Sand" will be placed on the
*   top of the list.
    IF linetab-mat_desc(9) = 'Frac Sand'.
      linetab-matnr = '000000000000000000'.
      MODIFY linetab.
      EXIT.
    ENDIF.
  ENDLOOP.
  SORT linetab ASCENDING BY matnr quantity.
  TRANSLATE vbak-bstnk TO UPPER CASE.
  TRANSLATE zinvoice01-sp_addr1 TO UPPER CASE.
  TRANSLATE zinvoice01-sp_addr2 TO UPPER CASE.
  header_hm00_flag = 'Y'.
  zinvoice01-tax_flag = 'Y'.    " suppress disc % and disc unit price
* determine if the discount % and amount shall be suppressed.
  LOOP AT linetab.
    IF linetab-hm00_flag EQ space.
      " non fixed price detected
      header_hm00_flag = ' '.
    ENDIF.
    IF linetab-disc_per > 0.
      zinvoice01-tax_flag = ' '.
    ENDIF.
  ENDLOOP.
  IF header_hm00_flag = 'Y'.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'TEXT1'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
  ENDIF.
  cnt = 0.
  LOOP AT linetab.
    MOVE-CORRESPONDING linetab TO zinvoice01.
    IF header_hm00_flag = 'Y'.
      zinvoice01-mixed = 'Y'.
      zinvoice01-tax_flag = 'Y'.
      MOVE 0 TO zinvoice01-disc_per.
      MOVE 0 TO zinvoice01-disc_amt.
      MOVE 0 TO zinvoice01-net_val.
    ENDIF.
    IF header_hm00_flag NE 'Y' AND zinvoice01-tax_flag EQ 'Y'.
      MOVE 0 TO zinvoice01-disc_per.
      MOVE 0 TO zinvoice01-disc_amt.
    ENDIF.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'LINE1'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    total_price = total_price + linetab-net_val.
    cnt = cnt + 1.
    IF cnt > 26.
*     make sure boundary lines are printed at page break.
      zinvoice01-lines = '*'.
    ELSE.
      zinvoice01-lines = ' '.
    ENDIF.
  ENDLOOP.
  MOVE total_price TO zinvoice01-tt_af_sur.
  IF header_hm00_flag EQ 'Y'.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'FIXED'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
  ENDIF.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'UNDERLINE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
  zinvoice01-mixed = ' '.
  IF bc_surchg NE 0.
    MOVE 'BC Surcharge' TO zinvoice01-disc.
    MOVE bc_surchg TO zinvoice01-net_val.
  ELSE.
    MOVE ' ' TO zinvoice01-disc.
    MOVE 0 TO zinvoice01-net_val.
  ENDIF.
  zinvoice01-hprbk_tot = 0.
  IF total_pst > 0.
    total_tax = total_tax - total_pst.
    zinvoice01-hprbk_tot = total_pst.
  ENDIF.
  IF zinvoice01-hprbk_tot = 0.
    zinvoice01-tax_flag = ' '.
  ELSE.
    zinvoice01-tax_flag = 'Y'.
  ENDIF.
  MOVE total_tax TO zinvoice01-hfinal_amt.
  zinvoice01-htt_af_sur = total_price + total_tax + bc_surchg +
                          total_pst.
  IF cnt > 26.      " more than one page printed.
    cnt = cnt - 26.
  ENDIF.
  IF cnt > 14.
*   if not enough space, generate a new page before printing totals.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'NEWPAGE'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    cnt = 0.
  ENDIF.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'TOTAL1'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
  cnt = 0.
  total_price = 0.
  total_tax = 0.
  total_pst = 0.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM print_template                                           *
*---------------------------------------------------------------------*
*       This routine prints a template                                *
*---------------------------------------------------------------------*

FORM print_template.
  DATA: cnt TYPE i.

  zinvoice01-bp_addr1 = ' '.
  zinvoice01-bp_addr2 = ' '.
  zinvoice01-bp_addr3 = ' '.
  zinvoice01-bp_addr4 = ' '.
  IF kna1-name2 EQ ' '.
    MOVE kna1-stras TO zinvoice01-bp_addr1.
    CONCATENATE kna1-ort01 ', ' kna1-regio INTO zinvoice01-bp_addr2.
    MOVE kna1-pstlz TO zinvoice01-bp_addr3.
  ELSE.
    MOVE kna1-name2 TO zinvoice01-bp_addr1.
    MOVE kna1-stras TO zinvoice01-bp_addr2.
    CONCATENATE kna1-ort01 ', ' kna1-regio INTO zinvoice01-bp_addr3.
    MOVE kna1-pstlz TO zinvoice01-bp_addr4.
  ENDIF.
* sort linetab ascending by matnr quantity.
* translate vbak-bstnk to upper case.
  TRANSLATE sadr-ort01 TO UPPER CASE.
  TRANSLATE sadr-name1 TO UPPER CASE.
  cnt = 0.
  CLEAR zinvoice01-lines.                                   "LABK915243
  LOOP AT linetab.
    MOVE-CORRESPONDING linetab TO zinvoice01.
    MOVE linetab-matnr TO zinvoice01-mat_num.
    MOVE linetab-mvgr1 TO zinvoice01-descline1.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'LINE1'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.
    cnt = cnt + 1.
*    IF CNT > 26. "deleted LABK915243
*      ZINVOICE01-LINES = '*'.
*    ELSE.
*      ZINVOICE01-LINES = ' '.
*    ENDIF.
    IF cnt = 37.              "total lines in window      "LABK915243
      CALL FUNCTION 'WRITE_FORM'                            "LABK915243
           EXPORTING                                        "LABK915243
                element = 'CONTINUED'                       "LABK915243
                window  = 'CONTINUE'                        "LABK915243
           EXCEPTIONS                                       "LABK915243
                OTHERS  = 01.                               "LABK915243
      CLEAR cnt.                                            "LABK915243
    ENDIF.                                                  "LABK915243
  ENDLOOP.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'UNDERLINE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM print_fob_total                                          *
*---------------------------------------------------------------------*
*       This routine prints the FOB total lines for a quotation       *
*---------------------------------------------------------------------*
*

FORM print_fob_total USING total_price LIKE linetab-net_val.

  ADD 1 TO zinvoice01-lines.                                "LABK915116
  IF zinvoice01-lines > 26.                                 "LABK915116
    CLEAR zinvoice01-lines.                                 "LABK915116
    CALL FUNCTION 'WRITE_FORM'                              "LABK915116
        EXPORTING                                           "LABK915116
             element = 'WRITE_LINE_FOB_2'                   "LABK915116
             window  = 'MAIN'                               "LABK915116
        EXCEPTIONS                                          "LABK915116
             OTHERS  = 01.                                  "LABK915116
  ENDIF.                                                    "LABK915116

  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'ULINE'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.

  ADD 1 TO zinvoice01-lines.                                "LABK915116
  IF zinvoice01-lines > 26.                                 "LABK915116
    CLEAR zinvoice01-lines.                                 "LABK915116
    CALL FUNCTION 'WRITE_FORM'                              "LABK915116
        EXPORTING                                           "LABK915116
             element = 'WRITE_LINE_FOB_2'                   "LABK915116
             window  = 'MAIN'                               "LABK915116
        EXCEPTIONS                                          "LABK915116
             OTHERS  = 01.                                  "LABK915116
  ENDIF.                                                    "LABK915116

  zinvoice01-mat_desc = '                     Blend Total FOB:'.
  zinvoice01-quantity = 1.
  zinvoice01-uom = 'TNE'.
  zinvoice01-disc_per = ' '.
  zinvoice01-disc_amt = ' '.
  zinvoice01-net_val = total_price.
*    total_price = 0.
  CALL FUNCTION 'WRITE_FORM'
       EXPORTING
            element = 'FOBTOTAL'
            window  = 'MAIN'
       EXCEPTIONS
            OTHERS  = 01.

  zinvoice01-mat_desc = ' '.
  zinvoice01-quantity = 1.
  zinvoice01-uom = 'M3'.
  zinvoice01-disc_per = ' '.
  zinvoice01-disc_amt = ' '.
  IF fob_yield NE 0.
    ADD 1 TO zinvoice01-lines.                              "LABK915116
    IF zinvoice01-lines > 26.                               "LABK915116
      CLEAR zinvoice01-lines.                               "LABK915116
      CALL FUNCTION 'WRITE_FORM'                            "LABK915116
           EXPORTING                                        "LABK915116
               element = 'WRITE_LINE_FOB_2'                 "LABK915116
               window  = 'MAIN'                             "LABK915116
           EXCEPTIONS                                       "LABK915116
               OTHERS  = 01.                                "LABK915116
    ENDIF.                                                  "LABK915116

    zinvoice01-net_val = total_price / fob_yield.
    total_price = 0.
    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'FOBMCUBE'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.

  ELSE.
    ADD 1 TO zinvoice01-lines.                              "LABK915116
    IF zinvoice01-lines > 26.                               "LABK915116
      CLEAR zinvoice01-lines.                               "LABK915116
      CALL FUNCTION 'WRITE_FORM'                            "LABK915116
           EXPORTING                                        "LABK915116
               element = 'WRITE_LINE_FOB_2'                 "LABK915116
               window  = 'MAIN'                             "LABK915116
           EXCEPTIONS                                       "LABK915116
               OTHERS  = 01.                                "LABK915116
    ENDIF.                                                  "LABK915116

    CALL FUNCTION 'WRITE_FORM'
         EXPORTING
              element = 'FOBMCUBE0'
              window  = 'MAIN'
         EXCEPTIONS
              OTHERS  = 01.

  ENDIF.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM TVBDPAU_CREATE                                           *
*---------------------------------------------------------------------*
*       This routine is creating a table which includes the subitem-  *
*       numbers                                                       *
*---------------------------------------------------------------------*

FORM tvbdpau_create.

  LOOP AT tvbdpa.
    IF tvbdpa-uepos IS INITIAL.
      IF tvbdpau-uposv > 0.
        APPEND tvbdpau.
        CLEAR tvbdpau.
      ENDIF.
      tvbdpau-posnr = tvbdpa-posnr.
    ELSE.
      IF tvbdpau-uposv IS INITIAL OR
         tvbdpau-uposv > tvbdpa-posnr.
        tvbdpau-uposv = tvbdpa-posnr.
      ENDIF.
      IF tvbdpau-uposb < tvbdpa-posnr AND
         tvbdpau-uposv < tvbdpa-posnr.
        tvbdpau-uposb = tvbdpa-posnr.
      ENDIF.
    ENDIF.
  ENDLOOP.
  IF tvbdpau-uposv > 0.
    APPEND tvbdpau.
  ENDIF.
  SORT tvbdpau.

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.
  re_type = ' '.
  in_type = ' '.
  SELECT SINGLE vbtyp INTO in_type FROM vbak WHERE vbeln = nast-objky.
  SELECT SINGLE vgbel INTO quotation_no
                      FROM vbap
                     WHERE vbeln = nast-objky.
  SELECT SINGLE vbtyp INTO re_type FROM vbak WHERE vbeln = quotation_no.
  SELECT SINGLE vbelv INTO quotation_no
                      FROM vbfa
                     WHERE vbelv = quotation_no.
  IF sy-subrc EQ 0 AND in_type = 'C' AND re_type = 'B'.
*  there is a preceding document associated with this service order.
*  usually, the preceding document is a quotation, therefore the
*  layout set 'ZV_NOWSCO_QUOTE' is used to replace the default one.
    tnapr-fonam = 'ZV_NOWSCO_QUOTE'.
*   nast-objky = quotation_no.
*   nast-kschl = 'ZQ01'.
  ENDIF.

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

  CALL FUNCTION 'RV_DOCUMENT_PRINT_VIEW'
       EXPORTING
            comwa = vbco3
       IMPORTING
            kopf  = vbdka
       TABLES
            pos   = tvbdpa.

  PERFORM sender.
  PERFORM tvbdpau_create.

ENDFORM.

*---------------------------------------------------------------------*
*       FORM PROTOCOL_UPDATE                                          *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
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 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 vbdka-knumv.
    CLEAR komk.
    komk-mandt = sy-mandt.
    komk-kalsm = vbdka-kalsm.
    komk-kappl = pr_kappl.
    komk-waerk = vbdka-waerk.
    komk-knumv = vbdka-knumv.
    komk-vbtyp = vbdka-vbtyp.
  ENDIF.
  komp-kposn = vbdpa-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 FORM_CLOSE                                               *
*---------------------------------------------------------------------*
*       End of printing the form                                      *
*---------------------------------------------------------------------*

FORM form_close.

  CALL FUNCTION 'CLOSE_FORM'           "...Ende Formulardruck
       EXCEPTIONS OTHERS = 1.
  IF sy-subrc NE 0.
    retcode = 1.
  ENDIF.
  SET COUNTRY space.

ENDFORM.

*---------------------------------------------------------------------*
*       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.

  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.

* LABK912523 - revert the zrvadopf back to rvadopfo, which open the
*              layout set.
  INCLUDE rvadopfo.

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

ENDFORM.

*---------------------------------------------------------------------*
*       FORM FORM_TITLE_PRINT                                         *
*---------------------------------------------------------------------*
*       Printing of the form title depending of the field VBTYP       *
*---------------------------------------------------------------------*

FORM form_title_print.

  DATA: title(35).             "document type name we will write to file

  CASE vbdka-vbtyp.
    WHEN 'A'.
*     assign name of document type to our variable
      title =  title_a.                                     " 950623 RV
    WHEN 'B'.                                               " 950623 RV
      title =  title_b.                                     " 950623 RV
    WHEN 'C'.                                               " 950623 RV
      title =  title_c.                                     " 950623 RV
    WHEN 'D'.                                               " 950623 RV
      title =  title_d.                                     " 950623 RV
    WHEN 'G'.                                               " 950623 RV
      title =  title_g.                                     " 950623 RV
    WHEN 'H'.                                               " 950623 RV
      title =  title_h.                                     " 950623 RV
    WHEN 'K'.                                               " 950623 RV
      title =  title_k.                                     " 950623 RV
    WHEN 'L'.                                               " 950623 RV
      title =  title_l.                                     " 950623 RV
    WHEN OTHERS.                                            " 950623 RV
      title =  title_c.                                     " 950623 RV
  ENDCASE.
*  IF PREVIEW = 'NO'.                                     "LABK915116
**   write Document Type to file
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '01' TITLE.
**   write currency of first item to header
*  PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '15' VBDKA-WAERK.
*  ENDIF.                                                 "LABK915116

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.
*
* Note: The Location that TE wants is actually the first line in the
*       name field of the 'ZB' partner as seen in the partner details
*       screen.  The Well Name that TE is looking for is actually the
*       the location field of the 'ZB' partner as seen in the partner
*       details screen.
*
************************************************************************
FORM write_header_info_to_file.

  DATA: job_type_no(3), personnel_name(30),
        del_date(10).
  DATA: length TYPE i,
*        FILENAME(12) TYPE C,
*        OUT_NAME(12) TYPE C,
        cnt TYPE i.

* determine how many chars to take off the beginning of the doc. number
  length = strlen( vbdka-vbeln ) - 8.
* assign to temporary field-symbol
  ASSIGN vbdka-vbeln+length(8) TO .
** assign field-symbol to filename
*  FILENAME = .
** add '.' for the extension
*  MOVE '.' TO FILENAME+8.
*  IF PREVIEW = 'NO'.                                        "LABK915116
**   to get path information
**   perform get_path_name(zvprint1) using path 'TE'  changing retcode.
*    PERFORM GET_PATH_NAME(ZVPRINT1) USING PATH 'SDBCK'      "LABK915522
*                                         CHANGING RETCODE.  "LABK915522
*
*    IF RETCODE NE 0.
*      SUBMIT ZCALLERR WITH RET_CODE = RETCODE
*                      WITH MAIN_PGM = SY-REPID.
*      EXIT.
*    ENDIF.
*    PERFORM OPEN_FILE(ZVPRINT1) USING FILENAME OUT_NAME PATH.
*  ENDIF.           "if preview = 'NO'                    "LABK915116
*   'S:\'.
* BOOK_TOTAL = 0.
  item_count = 0.

  IF vbdka-vbtyp = 'G'.                "  Contract
*    IF PREVIEW = 'NO'.                                     "LABK915116
*      PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001'
*                                          '02' VBDKA-VBELN.
*    ENDIF.                                                 "LABK915116
  ENDIF.

  IF vbdka-vbtyp = 'H'.          "  Return Order              950623 RV
*    IF PREVIEW = 'NO'.                                     "LABK915116
*     PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001'    " 950623 RV
*                                          '04' VBDKA-VBELN. " 950623 RV
*    ENDIF.                                                 "LABK915116
  ENDIF.                                                    " 950623 RV

  IF vbdka-vbtyp = 'B'.          " Quotation
*   write Quotation Number to file
*    IF PREVIEW = 'NO'.                                     "LABK915116
*      PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001'
*                                            '02' VBDKA-VBELN.
*    ENDIF.                                                 "LABK915116
  ENDIF.
  CLEAR quotation.                                          "JL9605
  IF vbdka-vbtyp = 'C'.                "  Sales Order
*    IF PREVIEW = 'NO'.                                     "LABK915116
*      PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001'
*                                            '04' VBDKA-VBELN.
*    ENDIF.                                                 "LABK915116
*   write Quotation Number to file
    SELECT * FROM vbap WHERE vbeln = vbdka-vbeln.
      IF vbap-vgbel NE space.
        SELECT SINGLE * FROM vbak WHERE vbeln = vbap-vgbel.
        IF sy-subrc = 0 AND vbak-auart = 'AG'.    " Quotation type
*         IF PREVIEW = 'NO'.                                 "LABK915116
*            PERFORM WRITE_TO_FILE(ZVPRINT1) USING
*                                       '01' '000001' '02' VBAK-VBELN.
*         ENDIF.                                             "LABK915116
          quote_flag = 1.
          quotation = vbap-vgbel.
          EXIT.
        ENDIF.
      ENDIF.
    ENDSELECT.
    IF quote_flag = 0.
*     IF PREVIEW = 'NO'.                                     "LABK915116
*        PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '02' '  '.
*     ENDIF.                                                 "LABK915116
    ENDIF.
*   Get all delivery notes for the sales order
    PERFORM get_del_notes TABLES del_note_tbl
                            USING vbdka-vbeln   del_notes.
*    IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '05' DEL_NOTES.
*    ENDIF.                                                 "LABK915116

*   write units to be used to file.
    unit_count = 0.
    CLEAR eqp1.
    REFRESH eqp1.
    CLEAR eqp2.
    REFRESH eqp2.
*   select * from zunln where vbelv = vbdka-vbeln.
    SELECT * FROM z2utl WHERE vbeln = vbdka-vbeln           "LABK916958
                          AND unit NE ' '.                  "LABK916958
      unit_count = unit_count + 1.
*     IF PREVIEW = 'NO'.                                     "LABK915116
*        PERFORM WRITE_TO_FILE(ZVPRINT1)
**                         using '04' unit_count '01' zunln-zunit.
*                     USING '04' UNIT_COUNT '01' Z2UTL-UNIT. "LABK915116
*     ENDIF.                                                 "LABK915116
      IF unit_count < 13.
*       move zunln-zunit to eqp1-unit.
        MOVE z2utl-unit TO eqp1-unit.                       "LABK916958
        APPEND eqp1.
      ELSE.
*       move zunln-zunit to eqp2-unit.
        MOVE z2utl-unit TO eqp2-unit.                       "LABK916958
        APPEND eqp2.
      ENDIF.
    ENDSELECT.
    IF unit_count = 0.                 "  Write dummy record
*     IF PREVIEW = 'NO'.                                     "LABK915116
*        PERFORM WRITE_TO_FILE(ZVPRINT1) USING '04' '000001' '01' '   '.
*     ENDIF.                                                 "LABK915116
    ENDIF.
*   write personnel was used in the job to file
    unit_count = 0.
    CLEAR emp1.
    CLEAR emp2.
    REFRESH emp1.
    REFRESH emp2.
    cnt = 1.
*   select * from zprln where vbelv = vbdka-vbeln.
    SELECT * FROM z2utl WHERE vbeln = vbdka-vbeln.          "LABK916958
      CLEAR: p0002, personnel_name.

* This select statement modified with PA0002. - 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 = '0002' AND
*                           PERNR = ZPRLN-PERNR.
*        MOVE PREL+3 TO P0002.
*     clear emp.
*     select * from pa0002 where pernr = zprln-pernr. " jb1
      SELECT * FROM pa0002 WHERE pernr = z2utl-pernr.       "LABK916958
        MOVE-CORRESPONDING pa0002 TO p0002.
        MOVE p0002-vorna(1) TO personnel_name.
        MOVE '.' TO personnel_name+1(1).
        MOVE p0002-nachn TO personnel_name+3(27).
        ADD 1 TO unit_count.
* Find the person's Class                                    "LABK916958
        IF z2utl-percls IS INITIAL.                         "LABK916958
          SELECT SINGLE stell INTO (pa0001-stell)           "LABK916958
            FROM pa0001 WHERE pernr = z2utl-pernr           "LABK916958
                         AND endda >= z2utl-effdate         "LABK916958
                         AND begda <= z2utl-effdate.        "LABK916958
          CONCATENATE 'P0' pa0001-stell+6(2)                "LABK916958
            INTO z2utl-percls.                              "LABK916958
        ENDIF.                                              "LABK916958
*       IF PREVIEW = 'NO'.                                   "LABK915116
*          PERFORM WRITE_TO_FILE(ZVPRINT1) USING '03' UNIT_COUNT '01'
*                                                       PERSONNEL_NAME.
*         write Employee class to file
*         perform write_to_file(zvprint1) using '03' unit_count '02'
*                                                      zprln-matnr.
*         PERFORM WRITE_TO_FILE(ZVPRINT1) USING '03'         "LABK915116
*             UNIT_COUNT '02' Z2UTL-PERCLS.                  "LABK915116
*         write Employee number to file
*         perform write_to_file(zvprint1) using '03' unit_count '03'
*                                                      zprln-pernr.
*         PERFORM WRITE_TO_FILE(ZVPRINT1) USING '03'         "LABK915116
*             UNIT_COUNT '03' Z2UTL-PERNR.                   "LABK915116
*       ENDIF.                                               "LABK915116
*     store employee info
        IF cnt < 11.
          MOVE personnel_name TO emp1-name.
*       move zprln-matnr to emp1-class.
*       move zprln-pernr to emp1-number.
         MOVE z2utl-percls TO emp1-class.                   "LABK915116
         MOVE z2utl-pernr TO emp1-number.                   "LABK915116
          APPEND emp1.
        ELSE.
          MOVE personnel_name TO emp2-name.
*       move zprln-matnr to emp2-class.
*       move zprln-pernr to emp2-number.
         MOVE z2utl-percls TO emp2-class.                   "LABK915116
         MOVE z2utl-pernr TO emp2-number.                   "LABK915116
          APPEND emp2.
        ENDIF.
        cnt = cnt + 1.

        EXIT.
      ENDSELECT.
    ENDSELECT.
    IF unit_count = 0.                 "  Write dummy record
*     IF PREVIEW = 'NO'.                                     "LABK915116
*        PERFORM WRITE_TO_FILE(ZVPRINT1) USING '03' '000001' '01' '   '.
*     ENDIF.                                                 "LABK915116
    ENDIF.
  ENDIF.

* call procedure to write Proposal No. to file
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0004' textline.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '03' TEXTLINE.
*  ENDIF.                                                 "LABK915116
  proposal = textline.    " to be used later by write_to_sapback_table
* find well name and location(stored in name1 and location fields of
* ZB partner.
  PERFORM get_well_data(zvprint1) USING vbdka-vbeln '01' '000001'.
*                                     PREVIEW.              "LABK915116
  IF vbpa-adrnr NE ' '.
    IF NOT sadr IS INITIAL.                                 "LABK925268
      zinvoice01-sp_addr1 = sadr-ort01.
      zinvoice01-sp_addr2 = sadr-name1.
    ELSEIF NOT adrc IS INITIAL.                             "LABK925268
      zinvoice01-sp_addr1 = adrc-city1.                     "LABK925268
      zinvoice01-sp_addr2 = adrc-name1.                     "LABK925268
    ENDIF.                                                  "LABK925268
  ELSE.
    zinvoice01-sp_addr1 = kna1-ort01.
    zinvoice01-sp_addr2 = kna1-name1.
  ENDIF.
  PERFORM get_well_info USING vbdka-vbeln '01' '000001'.

* write Job Type (description) to file
  SELECT SINGLE * FROM vbak WHERE vbeln = vbdka-vbeln.
  job_type_no = vbak-kvgr1.
* SELECT SINGLE * FROM tvv1t WHERE spras = sy-langu AND
  SELECT SINGLE * FROM t25A2 WHERE spras = sy-langu AND   "IJ1
                                   kvgr1 = job_type_no.
*  IF PREVIEW = 'NO'.                                     "LABK915116
** PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '07' TVV1T-BEZEI.
*   PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '07'
*                                         TVV1T-BEZEI+4(16). "LABK926189
**   write purchase order number to file
*   PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '18' VBAK-BSTNK.
*
**   write Quotation Date to file
*  PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '20' VBDKA-AUDAT.
*
**   write Date of Service to file
*   PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '21' VBAK-VDATU.
*
**   write Product Line to file
*   PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '88' VBAK-KVGR2.
*
**  write Creation Date to file                                      "gs
*   PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '95' VBAK-ERDAT.
*  ENDIF.                                                 "LABK915116

* write sales representative to file
  CLEAR p0001.
  SELECT SINGLE * FROM vbpa WHERE
           parvw = 'VE' AND
           posnr = '000000' AND
           vbeln = vbdka-vbeln.

* This select statement modified with PA0001 - 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'.                                     "LABK915116
*  PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '08' P0001-ENAME.
*  ENDIF.                                                 "LABK915116

* Write base's phone number of salesman
  sadr-telf1 = ''.                     " Defaults to head office.
  adrc-tel_number = ''.                                     "LABK925268
  SELECT SINGLE * FROM t001w WHERE werks = p0001-werks.
  SELECT SINGLE * FROM sadr WHERE adrnr = t001w-adrnr AND natio = ' '.
  IF sy-subrc = 0.                                          "LABK925268
*    IF PREVIEW = 'NO'.                                      "LABK915116
*      PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '81'
*                                            SADR-TELF1.
*    ENDIF.                                                  "LABK915116
  ELSE.                                                     "LABK925268
    SELECT SINGLE * FROM adrc WHERE addrnumber = t001w-adrnr"LABK925268
                                AND date_from LE sy-datum   "LABK925268
                                AND nation      = ' '.      "LABK925268
    IF sy-subrc = 0.                                        "LABK925268
*      IF PREVIEW = 'NO'.                                    "LABK925268
*        PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' "LABK925268
*                                  '81'    ADRC-TEL_NUMBER.  "LABK925268
*      ENDIF.                                                "LABK925268
    ENDIF.                                                  "LABK925268
  ENDIF.                                                    "LABK925268

* write bill to party information to file
  PERFORM write_bill_to_party_info(zvprint1)
                             USING vbdka-vbeln '01' '000001'.
*                             PREVIEW.                       "LABK915116
  MOVE vbpa-kunnr TO bill_to_kunnr.
  IF vbpa-adrnr NE ' '.
    IF NOT sadr IS INITIAL.                                 "LABK925268
      MOVE sadr-name2 TO name2.
      MOVE sadr-stras TO stras.
      MOVE sadr-ort01 TO ort01.
      MOVE sadr-regio TO regio.
      MOVE sadr-pstlz TO pstlz.
    ELSEIF NOT adrc IS INITIAL.                             "LABK925268
      MOVE adrc-name2      TO name2.                        "LABK925268
      MOVE adrc-street     TO stras.                        "LABK925268
      MOVE adrc-city1      TO ort01.                        "LABK925268
      MOVE adrc-region     TO regio.                        "LABK925268
      MOVE adrc-post_code1 TO pstlz.                        "LABK925268
    ENDIF.                                                  "LABK925268
  ELSE.
    MOVE kna1-name2 TO name2.
    MOVE kna1-stras TO stras.
    MOVE kna1-ort01 TO ort01.
    MOVE kna1-regio TO regio.
    MOVE kna1-pstlz TO pstlz.
  ENDIF.

* write ship to party information to file
  PERFORM write_ship_to_party_info(zvprint1)
                             USING vbdka-vbeln '01' '000001'.
*                                   PREVIEW.              "LABK915116

*  write shipping instructions                                       "gs
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0017' textline.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '84' TEXTLINE.
*  ENDIF.                                                 "LABK915116
*  write salesman's comments                                        "gs3
*                                 changed TEXTLINE to TEXT240       "jb2
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0002' text240.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '74' TEXT240.
*  ENDIF.                                                 "LABK915116
* salesperson comments
*  zinvoice01-comment1 = text240(70).
*  zinvoice01-comment2 = text240+70(70).
*  zinvoice01-comment3 = text240+140(70).
*  zinvoice01-comment4 = text240+210(30).
  zinvoice01-inv_commts = text240.
  MOVE vbdka-vbeln TO thead-tdname.                         "LABK916978

*  write AFE no to file
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0003' textline.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '70' TEXTLINE.
*  ENDIF.                                                 "LABK915116
  zinvoice01-afe_num = textline.
*  write sales customer contact
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0001' textline.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '71'
*                                               TEXTLINE.
*  ENDIF.                                                 "LABK915116
*  write field customer contact
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0031' textline.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '72'
*                                               TEXTLINE.
*  ENDIF.                                                 "LABK915116
  zinvoice01-descline1 = textline.
*  write so ticket no to file
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0015' textline.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '73' TEXTLINE.
*  ENDIF.                                                 "LABK915116
  zinvoice01-descline2 = textline.
*  write Base in Charge
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0032' text80.
  SELECT SINGLE * FROM tvstt WHERE spras = sy-langu AND
                                   vstel = text80(4).
  IF sy-subrc = 0.
    text80 = tvstt-vtext.
    zinvoice01-descline3 = text80.
  ENDIF.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '75' TEXT80.
*  ENDIF.                                                 "LABK915116
*  write proposal path name
  PERFORM get_header_text(zvprint1) USING vbdka-vbeln '0005' textline.
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_FILE(ZVPRINT1) USING '01' '000001' '17'
*                                                          TEXTLINE.
*  ENDIF.                                                 "LABK915116
*  write some extra fields to the SAPBack table for T.E
*  IF PREVIEW = 'NO'.                                     "LABK915116
*    PERFORM WRITE_TO_SAPBACK_TABLE USING PROPOSAL.
*  ENDIF.                                                 "LABK915116
ENDFORM.

*---------------------------------------------------------------------*
*       FORM GET_WELL_INFO                                            *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM get_well_info USING well_location rec_id seq_id.
  SELECT SINGLE * FROM vbpa WHERE
           parvw = 'ZB' AND
           posnr = '000000' AND
           vbeln = well_location.

  IF vbpa-adrnr NE ' '.
    CLEAR: sadr, adrc.                                      "LABK925268
* well locatn, name, formation changed in ZB partner, write them to file
    SELECT SINGLE * FROM sadr WHERE
             adrnr = vbpa-adrnr AND
             natio = ' '.

    IF sy-subrc = 0.                                        "LABK925268
      PERFORM replace_hard_corded_texts USING
               sadr-name1   sadr-ort01   sadr-ort02.
      zinvoice01-bp_addr1 = sadr-ort01.
      SPLIT sadr-name1 AT ':' INTO zinvoice01-bp_addr3
                                   zinvoice01-bp_addr2.
    ELSE.                                                   "LABK925268
      SELECT SINGLE * FROM adrc WHERE                       "LABK925268
               addrnumber = vbpa-adrnr AND                  "LABK925268
               date_from LE sy-datum   AND                  "LABK925268
               nation     = ' '.                            "LABK925268
      IF sy-subrc = 0.                                      "LABK925268
        PERFORM replace_hard_corded_texts USING             "LABK925268
                 adrc-name1   adrc-city1   adrc-city2.      "LABK925268
        zinvoice01-bp_addr1 = adrc-city1.                   "LABK925268
        SPLIT adrc-name1 AT ':' INTO zinvoice01-bp_addr3    "LABK925268
                                     zinvoice01-bp_addr2.   "LABK925268
      ENDIF.                                                "LABK925268
    ENDIF.                                                  "LABK925268
  ELSE.
* well locatn, name, formation not changed, write them to file
    SELECT SINGLE * FROM kna1 WHERE
             kunnr = vbpa-kunnr.

    PERFORM replace_hard_corded_texts USING
             kna1-name1   kna1-ort01   kna1-ort02.
    zinvoice01-bp_addr1 = kna1-ort01.
    SPLIT kna1-name1 AT ':' INTO zinvoice01-bp_addr3
                                 zinvoice01-bp_addr2.
  ENDIF.
  IF zinvoice01-bp_addr2 EQ ' '.
    zinvoice01-bp_addr2 = zinvoice01-bp_addr3.
  ELSE.
    CONCATENATE ':' zinvoice01-bp_addr2 INTO zinvoice01-bp_addr2.
  ENDIF.
  CONDENSE zinvoice01-bp_addr2.
ENDFORM.

*---------------------------------------------------------------------*
*       FORM REPLACE_HARD_CORDED_TEXTS                                *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  LOCATION                                                      *
*  -->  WELLNAME                                                      *
*  -->  FORMATION                                                     *
*---------------------------------------------------------------------*
FORM replace_hard_corded_texts USING location wellname formation.   "gs4
  DATA: templocation(35) TYPE c.
  MOVE location TO templocation.
  IF templocation+0(5) = 'Free:' OR templocation+0(5) = 'FREE:'.
    SHIFT location LEFT BY 5 PLACES.
  ENDIF.

  REPLACE 'Location' WITH '' INTO location.
  REPLACE 'Well Name' WITH '' INTO wellname.
  REPLACE 'Formation' WITH '' INTO formation.
  REPLACE 'LOCATION' WITH '' INTO location.
  REPLACE 'WELL NAME' WITH '' INTO wellname.
  REPLACE 'FORMATION' WITH '' INTO formation.


ENDFORM.

*---------------------------------------------------------------------*
*       FORM WRITE_TO_SAPBACK_TABLE                                   *
*---------------------------------------------------------------------*
*       Look for all delivery notes related to the billing document   *
*
* December 29th, 1995
* gs - for online quotes, test transaction code
*      to determine update vs. create
*    - for tprss quotes, use zpara table to receive action parameters
*---------------------------------------------------------------------*
FORM write_to_sapback_table USING prop.
*  DATA: STR(80).                       " Message for T.E
*  DATA: QUOTE_NUM(10).
*
*  CHECK VBDKA-VBTYP = 'B'.             " Quotation
** write Proposal Number to file
*  PERFORM WRITE_TO_FILE(ZVPRINT1) USING '27' '000001' '01' PROP.
** write Quotation Number to file
*  PERFORM WRITE_TO_FILE(ZVPRINT1) USING '27' '000001' '02' VBDKA-VBELN.
** Construct informed message for T.E (sapback)
** A) for on-line, test transaction to determine update/create
*  IF SY-TCODE = 'VA21'.                " create
*    WRITE 'CREATE 0' TO STR.
*  ENDIF.
*  IF SY-TCODE = 'VA22'.                " update
*    WRITE 'UPDATE 0' TO STR.
*  ENDIF.
** B) for trpss update/create (background), we must use different method
**    because it always uses va22
**    use ZPARA to receive update 0 or create 0 action from TPRSS
*  MOVE VBDKA-VBELN TO QUOTE_NUM.
*  SHIFT QUOTE_NUM BY 2 PLACES LEFT.
*  SELECT SINGLE * FROM ZPARA WHERE
*      RECTYPE = 'TPRSS' AND PGMNAME = QUOTE_NUM.
** If we cannot find it, we have to assume that we are online and that
** method A) will prevail
*  IF SY-SUBRC EQ 0.
*    WRITE ZPARA-PARAMETER1 TO STR.
*  ENDIF.
**
*  WRITE 'Quotation' TO STR+9.
*  WRITE VBDKA-VBELN TO STR+19.
*  WRITE '<==>' TO STR+30.
*  WRITE PROP TO STR+35.
*  WRITE SY-DATUM TO STR+50.
*  WRITE SY-UZEIT TO STR+62.
*
*  CONDENSE STR.
** perfrom housekeeping on zpara
*  DELETE FROM ZPARA WHERE
*     RECTYPE = 'TPRSS' AND PGMNAME = QUOTE_NUM.
** write message to file
*  PERFORM WRITE_TO_FILE(ZVPRINT1) USING '27' '000001' '03' STR.
*
** Update SAPBACK table ZTESAPBACK - gh1
*  PERFORM SAPBACK_UPDATE(ZVTEQUO2) USING PROP VBDKA-VBELN STR.

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.

  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.
    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.
    CALL FUNCTION 'STRING_CONCATENATE'
         EXPORTING
              string1 = del_notes_str
              string2 = '@'
         IMPORTING
              string  = del_notes_str.
    CALL FUNCTION 'STRING_CONCATENATE'
         EXPORTING
              string1 = del_notes_str
              string2 = t_string
         IMPORTING
              string  = del_notes_str.
  ENDLOOP.
  CLEAR sy-subrc.
  WHILE sy-subrc = 0.
    REPLACE '@' WITH ' ' INTO del_notes_str.
  ENDWHILE.
ENDFORM.
*
*-----------------------------------------------------------------------
*  BUILD_ITEMID
*    - construct itemid string to show parent type, and parent-child
*      relationship and sequence if applicable (FOB children)
*-----------------------------------------------------------------------
FORM build_itemid
                  USING itemid
                  currentline
                  billing
                  currenthigher
                  fob_parent.
*
  DATA: parentline LIKE vbdpa-uepos,
        ida(6) TYPE c,
        idb(6) TYPE c.
*
  IF currenthigher NE '000000'.
*  we have an FOB child
    MOVE currenthigher TO ida.         "parent line number
    MOVE currentline TO idb.           "current child line number
  ELSE.
*  we must have one of three types of parents
    IF billing EQ ' '.
*     regular parent item with no explosion
      MOVE '000000' TO ida.
      MOVE '000000' TO idb.
    ELSE.
*     we must have one of two types of parents that explodes
*     check all items for a parent equal to the currentline and has rel.
*     billing eq ' '  (FOB Parent)
      LOOP AT tvbap.
        IF tvbap-uepos EQ currentline AND tvbap-fkrel EQ ' '.
          fob_parent = 1.
          EXIT.
        ENDIF.
      ENDLOOP.
      IF fob_parent EQ 1.
*        FOB explosion parent
        MOVE currentline TO ida.
      ELSE.
*        Regular explosion parent
        MOVE '000000' TO ida.
      ENDIF.
      MOVE '000000' TO idb.
    ENDIF.
  ENDIF.
* concatenate id's
  MOVE ida TO itemid.
  WRITE idb TO itemid+6.
ENDFORM.
*





Make your own free website on Tripod.com