*---------------------------------------------------------------------*
*               Z _ B J H _ K U N D E _ P A R T N E R                 *
*                                                                     *
*                 Autor: Bernd Haase (BJH Software)                   *
*---------------------------------------------------------------------*
* Programm zum Ermitteln der Partnerrollen und der Konditionssätze zu *
* einer Rechnung. Das Ergebnis wird in einer Tabelle aufbereitet und  *
* kann auf Datei gespeichert werden (nur lokal).                      *
* Hinweis: Das Programm folgt nicht der Kontenfindung und benutzt     *
* auch nicht das kalkulationsschema.                                  *
*---------------------------------------------------------------------*
* Include BJH Konverter zum Tausch von numerischen Felder in Character*
* Felder, welche beim Datentransfer benötigt werden.                  *
*---------------------------------------------------------------------*
* 22.03.2005 BHaa: Programm erstellt                                  *
*---------------------------------------------------------------------*
REPORT  z_bjh_kunde_partner  .

INCLUDE .

TABLES:
 kna1,    "Kundenstamm (allgemeiner Teil)
 knvp,    "Kundenstamm Partnerrollen
 konv,    "Konditionen (Vorgangsdaten)
 t685t,   "Konditionen: Arten: Texte
 tpart,   "Geschäftspartner: Rollen: Texte
 vbpa,    "Vertriebsbeleg: Partner
 vbrk.    "Faktura: Kopfdaten

DATA:
 wa_vbpa LIKE vbpa,
 wa_vbrk LIKE vbrk,
 wa_knvp LIKE knvp,
 wa_konv LIKE konv,

 anzahl(6) TYPE n.

DATA:
 BEGIN OF rechnung,
   vbeln LIKE vbrk-vbeln,
   knumv LIKE vbrk-knumv,
   BEGIN OF kunag,
* nicht kunnr, da diese überschrieben würde
     kunag LIKE vbrk-kunag,
     name1 LIKE kna1-name1,
     pstlz LIKE kna1-pstlz,
     ort01 LIKE kna1-ort01,
     land1 LIKE kna1-land1,
   END OF kunag,
 END OF rechnung.

*****************
* Partnerrollen *
*****************
DATA:
 BEGIN OF wk_vbpa,
   id(4) TYPE c,
   kunnr LIKE vbpa-kunnr,
   BEGIN OF posnr,
     vkorg LIKE knvp-vkorg,
     vtweg LIKE knvp-vtweg,
   END OF posnr,
   spart LIKE knvp-spart,
   parvw LIKE knvp-parvw,
   kunn2 LIKE knvp-kunn2,
   name1 LIKE kna1-name1,
   pstlz LIKE kna1-pstlz,
   ort01 LIKE kna1-ort01,
   land1 LIKE kna1-land1,
   vtext LIKE tpart-vtext,
 END OF wk_vbpa,
 wktab_vbpa LIKE TABLE OF wk_vbpa.

***************
* Konditionen *
***************
DATA:
 BEGIN OF wk_konv,
  knumv LIKE konv-knumv,
  kposn LIKE konv-kposn,
  stunr LIKE konv-stunr,
  zaehk LIKE konv-zaehk,
  kappl LIKE konv-kappl,
  kschl LIKE konv-kschl,
  kdatu(10) TYPE c,
  krech LIKE konv-krech,
  kawrt(12) TYPE c,
  kbetr(12) TYPE c,
  waers LIKE konv-waers,
  kpein(5) TYPE c,
  kmein LIKE konv-kmein,
  kntyp LIKE konv-kntyp,
  vtext LIKE t685t-vtext,
 END OF wk_konv,
 wktab_konv LIKE TABLE OF wk_konv.

****************
* Dateiausgabe *
****************
DATA:
 BEGIN OF wa_vbrk_out,
   vbeln LIKE vbrk-vbeln,
   t01(1) TYPE c,
   vkorg LIKE vbrk-vkorg,
   t02(1) TYPE c,
   vtweg LIKE vbrk-vtweg,
   t03(1) TYPE c,
   spart LIKE vbrk-spart,
   t04(1) TYPE c,
   kunag LIKE vbrk-kunag,
   t05(1) TYPE c,
   kunrg LIKE vbrk-kunrg,
   t06(1) TYPE c,
   land1 LIKE vbrk-land1,
   t07(1) TYPE c,
   fkart LIKE vbrk-fkart,
   t08(1) TYPE c,
   fktyp LIKE vbrk-fktyp,
   t09(1) TYPE c,
   vbtyp LIKE vbrk-vbtyp,
   t10(1) TYPE c,
   kalsm LIKE vbrk-kalsm,
   t11(1) TYPE c,
   vsbed LIKE vbrk-vsbed,
 END OF wa_vbrk_out,

 BEGIN OF wa_out,
   zeile(150) TYPE c,
 END OF wa_out,

 BEGIN OF ds_out,
   satz LIKE wa_out,
 END OF ds_out,

 dstab_out LIKE TABLE OF ds_out.

*---------------------------------------------------------------------*
*                                                                     *
*                 A u s w a h l b i l d s c h i r m                   *
*                                                                     *
*---------------------------------------------------------------------*
* Rechnungsnummer als Einstieg zur Ermittlung der Partnerrollen
SELECTION-SCREEN: BEGIN OF BLOCK a1 WITH FRAME TITLE text-901.
PARAMETERS:
* Rechnungsnummer zur Lieferung
 pm_vbeln LIKE vbrk-vbeln DEFAULT '0090010940'.
SELECTION-SCREEN: END OF BLOCK a1.

* ermittelte Daten lokal speichern
SELECTION-SCREEN: BEGIN OF BLOCK ftp WITH FRAME TITLE text-b01.
PARAMETERS:
* Dateiname
 pmout LIKE rlgrap-filename.
SELECTION-SCREEN: END OF BLOCK ftp.

INITIALIZATION.
 CASE sy-uname.
   WHEN 'RRENAE'.
     MOVE 'd:\haase\sap\daten\vbrk_konv.txt' TO pmout.
   WHEN OTHERS.
     MOVE 'd:\vbrk_konv.txt' TO pmout.
 ENDCASE.

***********************************************************************
*                                                                     *
*                     P R O G R A M M S T A R T                       *
*                                                                     *
***********************************************************************
START-OF-SELECTION.

 CLEAR:   wktab_vbpa, wktab_konv, dstab_out, rechnung.
 REFRESH: wktab_vbpa, wktab_konv, dstab_out.

 MOVE pm_vbeln TO rechnung-vbeln.

* Rechnungsnummer vorhanden
 SELECT SINGLE * FROM vbrk INTO wa_vbrk
 WHERE vbeln = pm_vbeln.
 IF sy-subrc <> 0.
   WRITE: / icon_remove_from_selection,
            'Rechnungsnummer ist dem System nicht bekannt'.
 ELSE.
   MOVE wa_vbrk-vbeln TO rechnung-vbeln.
   MOVE wa_vbrk-knumv TO rechnung-knumv.
   MOVE wa_vbrk-kunag TO rechnung-kunag-kunag.

* Rechnungsdaten für Dateischreibung aufbereiten
   CLEAR: wa_vbrk_out.
   MOVE-CORRESPONDING wa_vbrk TO wa_vbrk_out.
   MOVE wa_vbrk_out TO ds_out-satz.
   APPEND ds_out    TO dstab_out.

   CLEAR ds_out.
   APPEND ds_out TO dstab_out.

* Kunde (Auftraggeber) in der rechnung
   SELECT SINGLE * FROM kna1
   INTO CORRESPONDING FIELDS OF rechnung-kunag
   WHERE kunnr = rechnung-kunag-kunag.
   IF sy-subrc <> 0.
     WRITE: / icon_remove_from_selection,
              'Kundennummer',
              rechnung-kunag-kunag,
              'ist dem System nicht bekannt'.
   ENDIF.
 ENDIF.

* Partnerrollen in den Belegen ausselektieren
 SELECT * FROM vbpa INTO wa_vbpa
 WHERE vbeln = pm_vbeln.
   CLEAR wk_vbpa.

   MOVE 'VBRK' TO wk_vbpa-id.
   MOVE wa_vbpa-vbeln TO wk_vbpa-kunnr.
   MOVE wa_vbpa-posnr TO wk_vbpa-posnr.
   MOVE wa_vbpa-parvw TO wk_vbpa-parvw.
   MOVE wa_vbpa-kunnr TO wk_vbpa-kunn2.

   PERFORM upro-zusatz_info_kunde
           CHANGING wk_vbpa-name1 wk_vbpa-pstlz
                    wk_vbpa-ort01 wk_vbpa-land1.

   PERFORM upro-zusatz_info_rolle CHANGING wk_vbpa-vtext.

   PERFORM upro-datei_vbpa.

   APPEND wk_vbpa TO wktab_vbpa.
 ENDSELECT.

 CLEAR ds_out.
 APPEND ds_out TO dstab_out.

* Partnerrollen im Kundenstamm auswerten
 SELECT * FROM knvp INTO wa_knvp
 WHERE kunnr = rechnung-kunag-kunag.
   CLEAR wk_vbpa.
   MOVE 'KNVP' TO wk_vbpa-id.
   MOVE wa_knvp-kunnr TO wk_vbpa-kunnr.
   MOVE wa_knvp-vkorg TO wk_vbpa-posnr-vkorg.
   MOVE wa_knvp-vtweg TO wk_vbpa-posnr-vtweg.
   MOVE wa_knvp-spart TO wk_vbpa-spart.
   MOVE wa_knvp-parvw TO wk_vbpa-parvw.
   MOVE wa_knvp-kunn2 TO wk_vbpa-kunn2.

   PERFORM upro-zusatz_info_kunde
           CHANGING wk_vbpa-name1 wk_vbpa-pstlz
                    wk_vbpa-ort01 wk_vbpa-land1.

   PERFORM upro-zusatz_info_rolle CHANGING wk_vbpa-vtext.

   PERFORM upro-datei_vbpa.

   APPEND wk_vbpa TO wktab_vbpa.

 ENDSELECT.

 CLEAR ds_out.
 APPEND ds_out TO dstab_out.

* Konditionen lesen
 SELECT * FROM konv INTO wa_konv
 WHERE knumv = rechnung-knumv.
   MOVE-CORRESPONDING wa_konv TO wk_konv.

   PERFORM upro-wandeln_datum_txt
           USING    wa_konv-kdatu ' ' ' ' '.'
           CHANGING wk_konv-kdatu.

   PERFORM upro-wandeln_num_txt
           USING    wa_konv-kawrt ' '
           CHANGING wk_konv-kawrt.
   PERFORM upro-wandeln_num_txt
           USING    wa_konv-kbetr ' '
           CHANGING wk_konv-kbetr.
   PERFORM upro-wandeln_num_txt
           USING    wa_konv-kpein ' '
           CHANGING wk_konv-kpein.

* Text der Konditionsart dazulesen wegen der besseren Lesbarkeit
   SELECT SINGLE vtext FROM t685t INTO wk_konv-vtext
   WHERE kvewe = 'A'
   AND   kappl = wk_konv-kappl
   AND   kschl = wk_konv-kschl
   AND   spras = sy-langu.
   IF sy-subrc <> 0.
     CLEAR wk_konv-vtext.
   ENDIF.

   APPEND wk_konv TO wktab_konv.

   MOVE wk_konv TO wa_out-zeile.
   MOVE wa_out TO ds_out-satz.
   APPEND ds_out TO dstab_out.
 ENDSELECT.

* Statistik für den Bildschirm
 DESCRIBE TABLE wktab_vbpa LINES anzahl.
 IF anzahl < 1.
   WRITE: / icon_remove_from_selection,
            'Keine Partnerrollen gefunden.'.
 ELSE.
   WRITE: / icon_include_in_selection,
            anzahl, 'Partnerrollen gefunden.'.
 ENDIF.

 DESCRIBE TABLE wktab_konv LINES anzahl.
 IF anzahl < 1.
   WRITE: / icon_remove_from_selection,
            'Keine Konditionen gefunden.'.
 ELSE.
   WRITE: / icon_include_in_selection,
            anzahl, 'Konditionssätze gefunden.'.
 ENDIF.

 PERFORM upro-ws_download.

END-OF-SELECTION.
 EXIT.

***********************************************************************
*
*                         I N C L U D E S
*
***********************************************************************
 INCLUDE zmgl_bjh_konverter.       "Umrechneroutine String=>Wert usw

*---------------------------------------------------------------------*
*                  Z U S A T Z _ I N F O _ K U N D E                  *
*---------------------------------------------------------------------*
* Kundenstammsatz anlegen und Zusatzdaten damit holen.                *
*---------------------------------------------------------------------*
FORM upro-zusatz_info_kunde
    CHANGING p_name1
             p_pstlz
             p_ort01
             p_land1.

 SELECT SINGLE name1 pstlz ort01 land1 FROM kna1
 INTO (p_name1, p_pstlz, p_ort01, p_land1)
 WHERE kunnr = wk_vbpa-kunn2.
 IF sy-subrc <> 0.
   CLEAR: p_name1, p_pstlz, p_ort01, p_land1.
 ENDIF.

ENDFORM.

*---------------------------------------------------------------------*
*                  Z U S A T Z _ I N F O _ R O L L E                  *
*---------------------------------------------------------------------*
* Text zur Partnerrolle einlesen zur besseren Lesbarkeit              *
*---------------------------------------------------------------------*
FORM upro-zusatz_info_rolle
    CHANGING p_vtext.

 SELECT SINGLE vtext FROM tpart INTO p_vtext
 WHERE parvw = wk_vbpa-parvw
 AND   spras = sy-langu.
 IF sy-subrc <> 0.
   CLEAR: p_vtext.
 ENDIF.

ENDFORM.

*---------------------------------------------------------------------*
*                         D A T E I _ V B P A                         *
*---------------------------------------------------------------------*
* Partnerrollen. Aufbereitung für den Dateiausdruck, mit Trennzeichen *
* zur besseren Optik.                                                 *
*---------------------------------------------------------------------*
FORM upro-datei_vbpa.

 DATA:
   BEGIN OF p_out,
     id(4) TYPE c,
     t01(1) TYPE c,
     kunnr LIKE vbpa-kunnr,
     t02(1) TYPE c,
     posnr LIKE vbrp-posnr,
     t03(1) TYPE c,
     spart LIKE knvp-spart,
     t04(1) TYPE c,
     parvw LIKE knvp-parvw,
     t05(1) TYPE c,
     kunn2 LIKE knvp-kunn2,
     t06(1) TYPE c,
     name1 LIKE kna1-name1,
     t07(1) TYPE c,
     pstlz LIKE kna1-pstlz,
     t08(1) TYPE c,
     ort01 LIKE kna1-ort01,
     t09(1) TYPE c,
     land1 LIKE kna1-land1,
     t10(1) TYPE c,
     vtext LIKE tpart-vtext,
   END OF p_out.

 MOVE-CORRESPONDING wk_vbpa TO p_out.
 MOVE p_out    TO ds_out-satz.
 APPEND ds_out TO dstab_out.

ENDFORM.

*---------------------------------------------------------------------*
*                         W S _ D O W N L O A D                       *
*---------------------------------------------------------------------*
* intern aufbereitete tabelle mit allen Informationen lokal auf Platte*
* speichern.                                                          *
*---------------------------------------------------------------------*
FORM upro-ws_download.

 CALL FUNCTION 'WS_DOWNLOAD'
      EXPORTING
*           BIN_FILESIZE   = ' '
*           CODEPAGE       = ' '
           filename       = pmout
           filetype       = 'ASC'
*           MODE           = ' '
*           WK1_N_FORMAT   = ' '
*           WK1_N_SIZE     = ' '
*           WK1_T_FORMAT   = ' '
*           WK1_T_SIZE     = ' '
*           COL_SELECT     = ' '
*           COL_SELECTMASK = ' '
*           NO_AUTH_CHECK  = ' '
*      IMPORTING
*           FILELENGTH     =
      TABLES
           data_tab       = dstab_out
*           FIELDNAMES     =
      EXCEPTIONS
           file_open_error         = 1
           file_write_error        = 2
           invalid_filesize        = 3
           invalid_type            = 4
           no_batch                = 5
           unknown_error           = 6
           invalid_table_width     = 7
           gui_refuse_filetransfer = 8
           customer_error          = 9
           OTHERS                  = 10.

 IF sy-subrc <> 0.
   WRITE: / icon_remove_from_selection,
            'Daten konnten nicht geschrieben werden.'.
 ELSE.
   WRITE: / icon_include_in_selection,
            'Daten erfolgreich gespeichert.'.
 ENDIF.


ENDFORM.

***** Ende *****
* Copyright BJH Software, Datei überarbeitet am: 26.5.2005