Cerca nel blog

venerdì 30 ottobre 2015

SAP PI: Risoluzione al problema "Unable to launch the application"

Risoluzione al problema "Unable to launch the application"

In ambiente di PI, nella schermata principale che viene visualizzata quando si chiama la transazione 
SXMB_IFR,  può capitare che venga visualizzato il seguente messaggio di errore "UNABLE TO LAUNCH THE APPLICATION", quando si cerca di elaborare uno dei tool di PI, ad esempio l'Integration Builder.




PREMESSA: sul client è installato il software Java versione 7 compatibile con SAP NetWeaver Process Integration 7.40 installato sul server.

Risoluzione

METODO 1:
Cancellare tutti i file e cartelle sotto la directory: C:\Users\<username>\AppData\LocalLow\Sun\Java\Deployment\Cache\

Accedere a "Java Control Panel", nel Tab "General", selezionare General tab -> Temporary Internet Files -> Settings -> deselezionare il flag "Keep temporary files on my computer.'"

 
METODO 2:

Lanciare il Java Control Panel, selezionare il Tab (Security), abbassare il livello di Sicurezza a "Medium" e premere il pulsante "Edit Site List..."


Nella popup successiva, premere il pulsante (Add) ed inserire nel campo Location, l'indirizzo Web con cui vi connettete a SAP PI, esempio http://<server>:<port>, in pratica dovete copiare una parte dell'indirizzo che compare nella schermata iniziale di PI quando viene eseguite la transazione SXMB_IFR, e premere il pulsante OK.



Così facendo il problema è risolto.


venerdì 23 ottobre 2015

Programma: Creazione di sessione di Batch Input per la transaz. FB01

Creazione di sessione di Batch Input per la transaz. FB01

Programma custom per la creazione di una sessione di Batch Input per la transazione FB01.
Il programma legge un file .xls(da client) oppure un file .csv(da application server) che ha la seguente struttuta (Una riga di testata con varie rige di posizione):

Testata;Societa;Datadocumento;Dataregistrazione;Tipodocumento;Riferimento;Testotestata;;;;;;;;;;;;;
Posizione;Conto-secondario;Chiave Contabile;Tipo movimento cespite; Importo; Attribuzione; Datavaluta;Testoposizione;Cdc;Ordineinterno;Odv;Posizioneodv;Oda;Posizioneoda;Profitcenter;;;;;
T;SOCIETA;20150624;20150624;AA;;;;;;;;;;;;;;;
P;10000067;70;100;1727433,32;;;G/C  REOPENING 2014 EX 40514;;49999;;;;;;;;;;
P;10000067-1;75;100;1727433,32;;;REOPENING 2015 140514;;49999;;;;;;;;;;
T;SOCIETA;20150624;20150624;AA;;;;;;;;;;;;;;;
P;140550;40;;1793184,07;;;G/C  REOPENING 2014 EX 40550;;49999;;;;;;;;;;
P;10000067-2;75;100;1793184,07;;;REOPENING 2015 140550;;49999;;;;;;;;;;


Il programma effettua i seguenti controlli:
  1. Il numero delle posizioni deve essere compreso tra 2 e 948
  2. Le posizioni possono avere come chiave contabile solo le seguenti (40=dare CoGe; 50= avere CoGe; 70=dare Cespiti; 75= avere Cespite)
  3. Il saldo delle posizioni contabili per ogni singolo documento contabile sia pari a zero, in caso contratio il file di caricamento sarà scartato con un messaggio di errore bloccante.
Superati i controlli, verrà creata un sessione di bacth input  (mediante la submit al programma standard RFBIB100)  che verrà richiamata successivamente nella transazione SM35.


 
************************************************************************
* Programma:      ZF15_AA_FB01                                         *
* Descrizione:    Giroconto Saldi/ Partite Singole Cespite             *

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

REPORT zf15_aa_fb01.

*======================================================================*
*                              INCLUDES                                *
*======================================================================*
************************************************************************
* INCLUDE:        ZF15_AA_FB01_TOP                                     *
* Descrizione:    Giroconto Saldi/ Partite Singole Cespite             *
************************************************************************

TYPES: BEGIN OF ty_bkpf,
         bldat TYPE bldat, "Data doc. nel documento
         blart TYPE blart, "Tipo documento
         bukrs TYPE bukrs,
         budat TYPE budat, "Data di registrazione
         monat TYPE monat, "Mese esercizio
         waers TYPE waers, "Divisa fissa EUR
         xblnr TYPE xblnr1, "Riferimento
         bktxt TYPE bktxt, "Testo testata doc
       END OF ty_bkpf.

TYPES: BEGIN OF ty_file,
         col1     TYPE c LENGTH 1,  " T per testata, P per posizione
         col2     TYPE c LENGTH 20, " T: società, P: conto-secondario
         col3     TYPE c LENGTH 8,  " T: dt doc, P: Chiave contabile
         col4     TYPE c LENGTH 8,  " T: dt reg, P: tipo movimento
         col5     TYPE c LENGTH 20, " T: tipo doc, P: importo
         col6     TYPE c LENGTH 20, " T: riferimento, P: Attribuzione
         col7     TYPE c LENGTH 25, " T: Testo testata, P: Dt valuta
         sgtxt    TYPE sgtxt,
         kostl    TYPE kostl,
         aufnr    TYPE aufnr,
         vbeln    TYPE vbeln_va,
         posnr    TYPE posnr_va,
         ebeln    TYPE ebeln,
         ebelp    TYPE ebelp,
         ko_prctr TYPE prctr,
         row      TYPE i,
       END OF ty_file,

       BEGIN OF ty_header,
         num   TYPE i,
         bukrs TYPE bukrs,
         bldat TYPE bldat,
         budat TYPE budat,
         blart TYPE blart,
         xblnr TYPE xblnr1,
         bktxt TYPE bktxt,
         row   TYPE i,    " serve per evenutali messaggi di errore
       END OF ty_header,

       BEGIN OF ty_item,
         num      TYPE i,
         conto    TYPE c LENGTH 20, " conto secondario
         bschl    TYPE bschl, " chiave contabile
         anbwa    TYPE anbwa, "tipo movimento cespite
         wrbtr    TYPE wrbtr, "importo
         zuonr    TYPE dzuonr, " attribuzione
         valut    TYPE valut, " data valuta
         sgtxt    TYPE sgtxt,
         kostl    TYPE kostl, "Centro di costos
         aufnr    TYPE aufnr, "Ordine
         vbeln    TYPE vbeln_va, " ODV
         posnr    TYPE posnr_va, "Posizione ODV
         ebeln    TYPE ebeln, "ODA
         ebelp    TYPE ebelp, "Posizione ODA
         ko_prctr TYPE prctr, "Profit Center
         row      TYPE i,    " serve per evenutali messaggi di errore
       END OF ty_item.

DATA: gt_file   TYPE STANDARD TABLE OF ty_file,
      gt_header TYPE STANDARD TABLE OF ty_header,
      gt_item   TYPE STANDARD TABLE OF ty_item,
      gs_item   TYPE ty_item.

DATA: gv_dir           TYPE string,
      gv_error         TYPE flag,
      gv_file          LIKE rlgrap-filename,
      gv_nomefile(125) TYPE c,
      gv_pathname      TYPE rstxtlg,
      gv_filename      TYPE rsawbnobjnm,
      gv_path          TYPE c LENGTH 5000,
      gs_bgr00         TYPE bgr00,
      gs_bbkpf         TYPE bbkpf,
      gs_bkpf          TYPE ty_bkpf,
      gs_bbseg         TYPE bbseg,
      gv_error_sess    TYPE flag,
      gv_saknr         TYPE saknr.

**---------- Dichiarazione Costanti ------------**
CONSTANTS: gc_x(1)          TYPE c VALUE 'X',
           gc_max_pos       TYPE i VALUE 948, "990,
           gc_path(8)       TYPE c VALUE '/tmp',
           gc_slash         TYPE c VALUE '/',
           gc_uline         TYPE c VALUE '_',
           gc_e             TYPE c VALUE 'E',
           gc_chiave_40(2)  type c value '40',
           gc_chiave_50(2)  type c value '50',
           gc_chiave_70(2)  type c value '70',
           gc_chiave_75(2)  type c value '75'.

*======================================================================*
*                                DATA ALV                              *
*======================================================================*
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,
      alv_layout  TYPE slis_layout_alv.
     
     
************************************************************************
* INCLUDE:        ZF15_AA_FB01_SEL                                     *
* Descrizione:    Giroconto Saldi/ Partite Singole Cespite             *
************************************************************************

**---------- Dichiarazione Parametri di Selezione ------------**
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.

PARAMETERS: p_coge type SAKNR.

SELECTION-SCREEN: SKIP.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 20(10) text-003 FOR FIELD rb_loc.
PARAMETERS: rb_loc RADIOBUTTON GROUP rd1
            USER-COMMAND moda MODIF ID rd1
            DEFAULT 'X'.
SELECTION-SCREEN COMMENT 40(10) text-004 FOR FIELD rb_ser.
PARAMETERS: rb_ser RADIOBUTTON GROUP rd1
            MODIF ID rd1.
SELECTION-SCREEN END OF LINE.

*SELECTION-SCREEN: SKIP.

PARAMETERS: pfile_se LIKE rlgrap-filename MODIF ID ser DEFAULT '/',
            pfile_lo LIKE rlgrap-filename MODIF ID cli DEFAULT 'C:\'.

SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN: SKIP.

SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-007.
PARAMETERS: p_id LIKE apqi-groupid OBLIGATORY, " DEFAULT 'ZAAFB01'.
            p_user TYPE bgr00-usnam OBLIGATORY.
PARAMETERS: p_mode(1) DEFAULT 'N' NO-DISPLAY.
SELECTION-SCREEN END OF BLOCK b3.
SELECTION-SCREEN END OF BLOCK b1.


************************************************************************
* INCLUDE:        ZF15_AA_FB01_FUN                                     *
* Descrizione:    Giroconto Saldi/ Partite Singole Cespite             *
************************************************************************
*&---------------------------------------------------------------------*
*&      Form  BROWSE_LOCAL_FILE
*&---------------------------------------------------------------------*
* Help di ricerca per file locale
*----------------------------------------------------------------------*
FORM browse_local_file .

  DATA: lv_window_title     TYPE         string,
        lv_initial_dir_path TYPE         string,
        lt_filetab          TYPE         filetable,
        lv_rc               TYPE         i,
        ls_filetable        TYPE LINE OF filetable.

  lv_window_title = text-005.
  lv_initial_dir_path = text-006.
  CLEAR ls_filetable.
  REFRESH lt_filetab.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = lv_window_title
      default_filename        = '*.xls; *.csv'
      file_filter             = '*.xls; *.csv'
      initial_directory       = lv_initial_dir_path
      multiselection          = space
    CHANGING
      file_table              = lt_filetab
      rc                      = lv_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.

  IF sy-subrc = 0.
    READ  TABLE lt_filetab INDEX 1 INTO ls_filetable.
    pfile_lo = ls_filetable-filename.
  ENDIF.

ENDFORM.                    " BROWSE_LOCAL_FILE
*&---------------------------------------------------------------------*
*&      Form  BROWSE_SERVER_FILE
*&---------------------------------------------------------------------*
*  Help di ricerca per file su server
*----------------------------------------------------------------------*
FORM browse_server_file.

  CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
    EXPORTING
      directory  = gv_dir
      filemask   = '*.csv'
    IMPORTING
      serverfile = pfile_se.

ENDFORM.                    " BROWSE_SERVER_FILE

*&---------------------------------------------------------------------*
*&      Form SELECTION_SCREEN_OUTPUT
*&---------------------------------------------------------------------*
*  Modifica selection screen a seconda dei radiobutton
*----------------------------------------------------------------------*
FORM selection_screen_output.
  LOOP AT SCREEN.
    IF NOT rb_ser IS INITIAL.
      CASE screen-group1.
        WHEN 'SER'.
          IF pfile_se IS INITIAL.
            pfile_se = '/'.
          ENDIF.
          screen-active = '1'.
        WHEN 'CLI'.
          screen-active = '0'.
      ENDCASE.
      MODIFY SCREEN.
    ENDIF.

    IF NOT rb_loc IS INITIAL.
      CASE screen-group1.
        WHEN 'SER'.
          screen-active = '0'.
        WHEN 'CLI'.
          IF pfile_lo IS INITIAL.
            pfile_lo = 'C:\'.
          ENDIF.
          screen-active = '1'.
      ENDCASE.
      MODIFY SCREEN.
    ENDIF.

    IF screen-name = 'P_ID'.
      screen-input = '0'.
      MODIFY SCREEN.
    ENDIF.

    IF screen-name = 'P_USER'.
      screen-input = '0'.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.
ENDFORM.                    " SELECTION_SCREEN_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  CLEAR
*&---------------------------------------------------------------------*
*    Clear variabili
*----------------------------------------------------------------------*
FORM clear .
  CLEAR: gv_dir, gv_error, gv_file,
         gv_error, gv_pathname,
         gv_filename, gv_nomefile,
         gv_error_sess, gv_saknr.

ENDFORM.                    " CLEAR
*&---------------------------------------------------------------------*
*&      Form  LOAD_FILE
*&---------------------------------------------------------------------*
*   Load file di input
*----------------------------------------------------------------------*
FORM load_file .
*Se il caricamento del file Excel avviene da PC locale
  IF rb_loc = gc_x.
    IF pfile_lo  IS NOT INITIAL.
      PERFORM read_local_file.
    ELSE.
      gv_error = gc_x.
      "Errore: Inserire il File
      MESSAGE s002(zf15_aa_fb01).
    ENDIF.
  ELSE.
    IF pfile_se IS NOT INITIAL.
      PERFORM read_server_file.
    ELSE.
      gv_error = gc_x.
      "Errore: Inserire il File
      MESSAGE s002(zf15_aa_fb01).
    ENDIF.
  ENDIF.

  IF gv_error NE gc_x.

    CALL FUNCTION 'RSDS_SPLIT_PATH_TO_FILENAME'
      EXPORTING
        i_filepath = gv_file
      IMPORTING
        e_pathname = gv_pathname
        e_filename = gv_filename.

    gv_nomefile = gv_filename.
    TRANSLATE gv_nomefile TO UPPER CASE.
  ENDIF.

ENDFORM.                    " LOAD_FILE
*&---------------------------------------------------------------------*
*&      Form  READ_LOCAL_FILE
*&---------------------------------------------------------------------*
*  Lettura contenuto file excel in locale
*----------------------------------------------------------------------*
FORM read_local_file .

  DATA: lv_exit   TYPE c LENGTH 1,
        lv_string TYPE string,
        ls_file   TYPE ty_file,
        lv_index  TYPE i,
        lt_tabin  LIKE alsmex_tabline  OCCURS 0 WITH HEADER LINE.
  FIELD-SYMBOLS: <fs> TYPE any.
  CLEAR: lv_exit. REFRESH lt_tabin.

  gv_file = pfile_lo.
  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = pfile_lo
      i_begin_col             = 1
      i_begin_row             = 1
      i_end_col               = 15
      i_end_row               = 65000
    TABLES
      intern                  = lt_tabin
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      OTHERS                  = 3.


  IF sy-subrc <> 0.
    gv_error = gc_x.
    "Errore in fase di lettura del file
    MESSAGE s003(zf15_aa_fb01).
  ELSE.
    REFRESH gt_file.
    CLEAR ls_file.
    LOOP AT lt_tabin WHERE row GT 2." 2 rughe con etichette
      MOVE : lt_tabin-col TO lv_index.
      ASSIGN COMPONENT lv_index OF STRUCTURE ls_file TO <fs>.
      MOVE lt_tabin-value TO <fs>.
      MOVE lt_tabin-row TO ls_file-row.
      AT END OF row.
        APPEND ls_file TO gt_file.
        CLEAR ls_file.
      ENDAT.
    ENDLOOP.
  ENDIF.
  REFRESH: lt_tabin.
  FREE: lt_tabin.
ENDFORM.                    " READ_LOCAL_FILE
*&---------------------------------------------------------------------*
*&      Form  READ_SERVER_FILE
*&---------------------------------------------------------------------*
*  lettura file csv su server
*----------------------------------------------------------------------*
FORM read_server_file .

  DATA: lv_string TYPE string,
        ls_file   TYPE ty_file.

  gv_file = pfile_se.

  OPEN DATASET pfile_se FOR INPUT IN TEXT MODE
        ENCODING DEFAULT WITH SMART LINEFEED.
  IF sy-subrc <> 0.
    gv_error = gc_x.
    "Errore in fase di lettura del file
    MESSAGE s003(zf15_aa_fb01).
  ELSE.
    DO.
      CLEAR: lv_string, ls_file.
      READ DATASET pfile_se INTO lv_string.
      IF sy-subrc NE 0.
        EXIT.
      ELSE.
        SPLIT lv_string AT ';'
           INTO ls_file-col1
                ls_file-col2
                ls_file-col3
                ls_file-col4
                ls_file-col5
                ls_file-col6
                ls_file-col7
                ls_file-sgtxt
                ls_file-kostl
                ls_file-aufnr
                ls_file-vbeln
                ls_file-posnr
                ls_file-ebeln
                ls_file-ebelp
                ls_file-ko_prctr.
        APPEND ls_file TO gt_file.
      ENDIF.
    ENDDO.
* Il file *.csv contiene nelle prime 2 righe l'intestazione
* delle colonne, quindi le eliminiamo.
    DELETE gt_file INDEX 2.
    DELETE gt_file INDEX 1.
    CLOSE DATASET pfile_se.
  ENDIF.

ENDFORM.                    " READ_SERVER_FILE
*&---------------------------------------------------------------------*
*&      Form  GET_HEADER_ITEMS
*&---------------------------------------------------------------------*
*  costrisce le tabella di testate e posizioni
*----------------------------------------------------------------------*
FORM get_header_items .
  DATA: ls_file   TYPE ty_file,
        ls_header TYPE ty_header,
        ls_item   TYPE ty_item,
        lv_num    TYPE i.
  CLEAR: lv_num, ls_file.

  CHECK gv_error IS INITIAL.

  LOOP AT gt_file INTO ls_file.
    IF ls_file-col1 = 'T'. " riga testata
      ADD 1 TO lv_num.
      CLEAR ls_header.
      MOVE: lv_num           TO ls_header-num,
            ls_file-col2     TO ls_header-bukrs,
            ls_file-col3     TO ls_header-bldat,
            ls_file-col4     TO ls_header-budat,
            ls_file-col5     TO ls_header-blart,
            ls_file-col6     TO ls_header-xblnr,
            ls_file-col7     TO ls_header-bktxt,
            ls_file-row      TO ls_header-row.
      APPEND ls_header TO gt_header.
    ELSEIF ls_file-col1 = 'P'. " riga posizione
      CLEAR ls_item.
      MOVE: lv_num TO ls_item-num,
            ls_file-col2     TO ls_item-conto,
            ls_file-col3     TO ls_item-bschl,
            ls_file-col4     TO ls_item-anbwa.
      SHIFT ls_file-col5 LEFT DELETING LEADING space.
      REPLACE ',' WITH '.' INTO ls_file-col5.
      CONDENSE ls_file-col5 NO-GAPS.
      ls_item-wrbtr = ls_file-col5.
      MOVE: ls_file-col6     TO ls_item-zuonr,
            ls_file-col7     TO ls_item-valut,
            ls_file-sgtxt    TO ls_item-sgtxt,
            ls_file-kostl    TO ls_item-kostl,
            ls_file-aufnr    TO ls_item-aufnr,
            ls_file-vbeln    TO ls_item-vbeln,
            ls_file-posnr    TO ls_item-posnr,
            ls_file-ebeln    TO ls_item-ebeln,
            ls_file-ebelp    TO ls_item-ebelp,
            ls_file-ko_prctr TO ls_item-ko_prctr,
            ls_file-row      TO ls_item-row.
      APPEND ls_item TO gt_item.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " GET_HEADER_ITEMS
*&---------------------------------------------------------------------*
*&      Form  CHECK_ITEMS
*&---------------------------------------------------------------------*
* Controllo che le testate abbiano ciascuna almeno 2 posizioni.
* Se il nr. di posizioni per una testata supera le 990 posizioni
* controllare l'esistenza nella tabella SKB1 per la società
* del conto coge inserito nei parametri di input
*----------------------------------------------------------------------*
FORM check_header.

  DATA:  gt_item_count TYPE STANDARD TABLE OF ty_item,
         lv_count_pos  TYPE                   i.

  FIELD-SYMBOLS: <fs_h> TYPE ty_header,
                 <fs_i> TYPE ty_item.

*  SORT gt_header BY num.
*  SORT gt_item BY num.
  LOOP AT gt_header ASSIGNING <fs_h>.
    gt_item_count[] = gt_item[].
    DELETE gt_item_count WHERE num <> <fs_h>-num.
    DESCRIBE TABLE gt_item_count LINES lv_count_pos.
    IF lv_count_pos LT 2.
      gv_error = gc_x.
      MESSAGE s010(zf15_aa_fb01) WITH <fs_h>-row.
      EXIT.
    ELSEIF lv_count_pos GT gc_max_pos AND gv_saknr IS INITIAL.
      gv_error = gc_x.
      MESSAGE s012(zf15_aa_fb01).
      EXIT.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " CHECK_HEADER
*&---------------------------------------------------------------------*
*&      Form  CHECK_ITEMS
*&---------------------------------------------------------------------*
* Controlli sulle posizioni:
* 1) chiavi contabili ammesse:
*  -  40 dare CoGe
*  -  50 avere CoGe
*  -  70 dare Cespite
*  -  75 avere Cespite
* 2) saldo documento pari a zero
*----------------------------------------------------------------------*
FORM check_items .

  DATA: ls_item    TYPE ty_item,
        ls_header  TYPE ty_header,
        lv_saldo   TYPE wrbtr,
        lv_num_pos TYPE i.

  FIELD-SYMBOLS: <fs_i> TYPE ty_item.

  CHECK gv_error IS INITIAL.

  " controllo chiavi contabili ammesse
  CLEAR ls_item.
  LOOP AT gt_item INTO ls_item WHERE bschl NE gc_chiave_40
                                 AND bschl NE gc_chiave_50
                                 AND bschl NE gc_chiave_70
                                 AND bschl NE gc_chiave_75.

  ENDLOOP.
  IF ls_item IS NOT INITIAL.
    gv_error = gc_x.
    MESSAGE s007(zf15_aa_fb01) WITH ls_item-bschl ls_item-row.
    EXIT.
  ENDIF.

  " controllo importo nullo
  CLEAR ls_item.
  READ TABLE gt_item INTO ls_item WITH KEY wrbtr = 0.
  IF ls_item IS NOT INITIAL.
    gv_error = gc_x.
    MESSAGE s009(zf15_aa_fb01) WITH ls_item-row.
    EXIT.
  ENDIF.

  LOOP AT gt_item ASSIGNING <fs_i>.
    ls_item = <fs_i>.
    AT NEW num.
      CLEAR lv_saldo.
    ENDAT.
    CASE ls_item-bschl.
      WHEN gc_chiave_40 OR gc_chiave_70.
        lv_saldo = lv_saldo + ls_item-wrbtr.
      WHEN gc_chiave_50 OR gc_chiave_75.
        lv_saldo = lv_saldo - ls_item-wrbtr.
    ENDCASE.

    AT END OF num.
      " controllo saldo diverso da 0.
      IF lv_saldo NE 0.
        READ TABLE gt_header INTO ls_header WITH KEY num = ls_item-num.
        gv_error = gc_x.
        MESSAGE s008(zf15_aa_fb01) WITH ls_header-row.
        EXIT.
      ENDIF.
    ENDAT.
    IF gv_error = gc_x.
      EXIT.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " CHECK_ITEMS

*&---------------------------------------------------------------------*
*&      Form  PROCESS_FB01_SESSION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM process_fb01_session .
  DATA: ls_apqi     TYPE                   apqi,
        lt_apqi     LIKE STANDARD TABLE OF ls_apqi,
        lv_datatype TYPE                   apqi-datatyp,
        s_apql      TYPE                   apql,
        t_apql      TYPE TABLE OF          apql.

  CONSTANTS: lc_bdc(3) TYPE c VALUE 'BDC'.

  CHECK gv_error IS INITIAL.

  MOVE lc_bdc       TO lv_datatype.
  REFRESH: lt_apqi, t_apql.
  CLEAR: ls_apqi, s_apql.
*  extract session batch input
  CALL FUNCTION 'BDC_OBJECT_SELECT'
    EXPORTING
      name             = p_id
      datatype         = lv_datatype
    TABLES
      apqitab          = lt_apqi
    EXCEPTIONS
      invalid_datatype = 1
      OTHERS           = 2.
  IF sy-subrc IS INITIAL.
    IF lt_apqi[] IS NOT INITIAL.
*      delete existing batch input sessions
      CLEAR ls_apqi.
      LOOP AT lt_apqi INTO ls_apqi.
        CALL FUNCTION 'BDC_OBJECT_DELETE'
          EXPORTING
            queue_id         = ls_apqi-qid
          EXCEPTIONS
            not_found        = 1
            invalid_datatype = 2
            OTHERS           = 3.
      ENDLOOP.
    ENDIF.
  ENDIF.

* delete item session batch input
  CALL FUNCTION 'BDC_PROTOCOL_SELECT'
    EXPORTING
      name         = p_id
    TABLES
      apqltab      = t_apql
    EXCEPTIONS
      invalid_data = 1
      OTHERS       = 2.
  IF t_apql[] IS NOT INITIAL.
    CLEAR s_apql.
    LOOP AT t_apql INTO s_apql.
* delete the item log session batch input
      CALL FUNCTION 'BDC_PROTOCOL_DELETE'
        EXPORTING
          temse_id    = s_apql-temseid
        EXCEPTIONS
          not_found   = 1
          temse_error = 2
          OTHERS      = 3.
      IF sy-subrc IS NOT INITIAL.
* Implement suitable error handling here
      ENDIF.
    ENDLOOP.
  ENDIF.

*Apertura del file in scrittura
  PERFORM f_open_dataset.

*Crea il file da passare al programma standard RFBIBL00
  PERFORM f_creafilebi.

*Chiusura del file in scrittura
  PERFORM f_close_dataset.

*Sessione di BI
  PERFORM f_sessione.

  DELETE DATASET gv_path.


  REFRESH: lt_apqi, t_apql.
  FREE:   lt_apqi, t_apql.
ENDFORM.                    " PROCESS_FB01_SESSION
*&---------------------------------------------------------------------*
*&      Form  F_OPEN_DATASET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_open_dataset .
  CLEAR: gv_path.

  CONCATENATE gc_path gc_slash p_id gc_uline sy-uzeit INTO gv_path.

  TRY.
      OPEN DATASET gv_path FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    CATCH cx_sy_file_open.
  ENDTRY.
*verifico se l'apertura è fallita
  IF sy-subrc IS NOT INITIAL.
    "Errore durante scrittura temporanea nella directory /gestopen
    MESSAGE s002(zf15_aa_fb01) DISPLAY LIKE gc_e.
  ENDIF.
ENDFORM.                    " F_OPEN_DATASET
*&---------------------------------------------------------------------*
*&      Form  F_CREAFILEBI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_creafilebi .
  DATA: lv_monat          TYPE c LENGTH 2,
        lv_num_posiz      TYPE i,
        lv_subtot         TYPE wrbtr,
        lv_chiave_cont(2) TYPE c.

  FIELD-SYMBOLS: <fs_h>       TYPE ty_header,
                 <fs_i>       TYPE ty_item,
                 <fs_i_suppl> TYPE ty_item.

* Creazione struttura BGR00 - unica per ogni file
  PERFORM f_valorizza_bgr00.

  UNASSIGN <fs_h>.
  LOOP AT gt_header ASSIGNING <fs_h>.
    CLEAR:   gs_bkpf.
    lv_monat = <fs_h>-bldat+4(2).
    MOVE: <fs_h>-bukrs TO gs_bkpf-bukrs,
          <fs_h>-bldat TO gs_bkpf-bldat,
          <fs_h>-budat TO gs_bkpf-budat,
          <fs_h>-blart TO gs_bkpf-blart,
          lv_monat     TO gs_bkpf-monat,
          'EUR'        TO gs_bkpf-waers,
          <fs_h>-xblnr TO gs_bkpf-xblnr,
          <fs_h>-bktxt TO gs_bkpf-bktxt.

* Creazione struttura BBKPF
    PERFORM f_valorizza_bbkpf USING gs_bkpf.

    UNASSIGN <fs_i>.
    lv_num_posiz = 1.
    CLEAR: lv_subtot.

    LOOP AT gt_item ASSIGNING <fs_i> WHERE num = <fs_h>-num.
      IF lv_num_posiz GT gc_max_pos.

        lv_num_posiz = 1.

* Se il subtotale è diverso da zero, creo una prima riga
* di contropartita che porti il saldo complessivo del documento
* a zero
        IF lv_subtot NE 0.
          UNASSIGN <fs_i_suppl>.
          ASSIGN gs_item TO <fs_i_suppl>.
          clear: <fs_i_suppl>.

* Se il numero di righe raggiunge il nr. max di righe 990
* determinerà il subtotale parziale delle posizioni elaborare
* creando una riga di contropartita che porti il saldo complessivo
* del documento a zero.
          IF lv_subtot > 0.
            lv_chiave_cont = gc_chiave_50.
          ELSE.
            lv_chiave_cont = gc_chiave_40.
          ENDIF.

* Chiave Contabile
          MOVE lv_chiave_cont TO <fs_i_suppl>-bschl.
*Conto
          MOVE p_coge TO <fs_i_suppl>-conto.
*Importo
          <fs_i_suppl>-wrbtr = abs( lv_subtot ).
* Valorizza record BBSEG per doc. pareggio
          PERFORM f_valorizza_bbseg USING <fs_i_suppl>.
        ENDIF.

* Mi predispongo a creare un nuovo documento contabile
* con i successivi 989 posizioni
* Creazione struttura BBKPF
        PERFORM f_valorizza_bbkpf USING gs_bkpf.

* Se il subtotale è diverso da zero, creo una prima riga
* di contropartita che porti il saldo complessivo del documento
* a zero
        IF lv_subtot NE 0.
          UNASSIGN <fs_i_suppl>.
          ASSIGN gs_item TO <fs_i_suppl>.
          CLEAR: <fs_i_suppl>.

* Se prima ho usato la chiave contabile 50
* adesso uso l'opposta e viceversa.
          IF lv_chiave_cont = gc_chiave_50.
            lv_chiave_cont = gc_chiave_40.
          ELSE.
            lv_chiave_cont = gc_chiave_50.
          ENDIF.

* Chiave Contabile
          MOVE lv_chiave_cont TO <fs_i_suppl>-bschl.
*Conto
          MOVE p_coge TO <fs_i_suppl>-conto.
*Importo
          <fs_i_suppl>-wrbtr = abs( lv_subtot ).
* Chiudo il coudmento contabile precedente
* Valorizza record BBSEG per doc. pareggio
          PERFORM f_valorizza_bbseg USING <fs_i_suppl>.

* Sommo al totale parziale, l'importo della nuova posizione
* che ho in canna
          CASE <fs_i>-bschl.
            WHEN gc_chiave_40 OR gc_chiave_70.
              lv_subtot = lv_subtot + <fs_i>-wrbtr.
            WHEN gc_chiave_50 OR gc_chiave_75.
              lv_subtot = lv_subtot - <fs_i>-wrbtr.
          ENDCASE.

        ENDIF.

      ELSE.

        CASE <fs_i>-bschl.
          WHEN gc_chiave_40 OR gc_chiave_70.
            lv_subtot = lv_subtot + <fs_i>-wrbtr.
          WHEN gc_chiave_50 OR gc_chiave_75.
            lv_subtot = lv_subtot - <fs_i>-wrbtr.
        ENDCASE.

      ENDIF.

* Valorizza record BBSEG per doc. pareggio
      PERFORM f_valorizza_bbseg USING <fs_i>.

      ADD 1 TO lv_num_posiz.

    ENDLOOP.
  ENDLOOP.

ENDFORM.                    " F_CREAFILEBI
*&---------------------------------------------------------------------*
*&      Form  F_VALORIZZA_BGR00
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_valorizza_bgr00 .

  DATA lv_group TYPE string.

  CONSTANTS: lc_0     TYPE c          VALUE '0',
             lc_black TYPE c          VALUE ' ' ,
             lca_sess LIKE d0100-mapn VALUE '1615'.

* Inizializza la struttura per il BI
  CLEAR gs_bgr00.
  PERFORM f_inizializza_struttura USING gs_bgr00.

* Tipo record
  MOVE lc_0 TO gs_bgr00-stype.
* Nome sessione
  MOVE p_id TO gs_bgr00-group. "controllare
* Mandante
  MOVE sy-mandt TO gs_bgr00-mandt.
* User id di lancio del batch
  MOVE p_user TO gs_bgr00-usnam.

* Flag di conservazione sessione
  MOVE gc_x TO gs_bgr00-xkeep.
*Data di avvio
  MOVE lc_black TO gs_bgr00-start.
* Scrittura del tipo record BGR00 nel file
  TRANSFER gs_bgr00 TO gv_path.
ENDFORM.                    " F_VALORIZZA_BGR00
*&---------------------------------------------------------------------*
*&      Form  F_INIZIALIZZA_STRUTTURA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GS_BGR00  text
*----------------------------------------------------------------------*
FORM f_inizializza_struttura USING p_struct.

  FIELD-SYMBOLS <fs_field>.

  DO.
*   Assegna al field symbol un campo della struttura di input
    ASSIGN COMPONENT sy-index OF STRUCTURE p_struct TO <fs_field>.
*   Verifica l'avvenuta assegnazione
    IF sy-subrc IS NOT INITIAL.
      EXIT.
    ENDIF.
*   Se il campo è assegnato, effettua l'inserimento del NODATA
    PERFORM f_riempi_campo USING <fs_field>.
  ENDDO.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  F_RIEMPI_CAMPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_<FS_FIELD>  text
*----------------------------------------------------------------------*
FORM f_riempi_campo USING p_field.

  IF p_field IS INITIAL.
*   Inizializzazione del campo con un carattere NODATA
    MOVE gc_slash TO p_field.
  ENDIF.

ENDFORM.                    " F_RIEMPI_CAMPO
*&---------------------------------------------------------------------*
*&      Form  F_VALORIZZA_BBKPF
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GS_BKPF  text
*----------------------------------------------------------------------*
FORM f_valorizza_bbkpf USING p_bkpf TYPE ty_bkpf.

  CONSTANTS: lc_1     TYPE c          VALUE '1',
             lc_tcode TYPE tcode      VALUE 'FB01',
             lca_sess TYPE d0100_mapn VALUE '1615'.
  CLEAR gs_bbkpf.

* Inizializza la struttura per il BI
  PERFORM f_inizializza_struttura USING gs_bbkpf.

* Tipo record
  MOVE lc_1 TO gs_bbkpf-stype.

* Codice transazione
  MOVE lc_tcode TO gs_bbkpf-tcode.

* Company code
  MOVE p_bkpf-bukrs TO gs_bbkpf-bukrs.

* Ledger Group
  CLEAR gs_bbkpf-ldgrp.

* Tipo Documento
  MOVE p_bkpf-blart TO gs_bbkpf-blart.

* Data documento
  MOVE p_bkpf-bldat+6(2) TO gs_bbkpf-bldat(2).
  MOVE p_bkpf-bldat+4(2) TO gs_bbkpf-bldat+2(2).
  MOVE p_bkpf-bldat(4)   TO gs_bbkpf-bldat+4(4).

* Data registrazione
  MOVE p_bkpf-budat+6(2) TO gs_bbkpf-budat(2).
  MOVE p_bkpf-budat+4(2) TO gs_bbkpf-budat+2(2).
  MOVE p_bkpf-budat(4)   TO gs_bbkpf-budat+4(4).

* Periodo fiscale
  MOVE p_bkpf-monat TO gs_bbkpf-monat.

* Divisa
  MOVE p_bkpf-waers TO gs_bbkpf-waers.

*  Riferimento
  MOVE p_bkpf-xblnr TO gs_bbkpf-xblnr.

*  Testo Testata Documento
  MOVE p_bkpf-bktxt TO gs_bbkpf-bktxt.

* Scrittura del tipo record BGR00 nel file
  TRANSFER gs_bbkpf TO gv_path.

ENDFORM.                    " F_VALORIZZA_BBKPF

*&---------------------------------------------------------------------*
*&      Form  F_VALORIZZA_BBSEG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GS_BSEG  text
*      -->P_<FS_H>_NUM  text
*----------------------------------------------------------------------*
FORM f_valorizza_bbseg  USING p_fs_item STRUCTURE gs_item.

  DATA: lv_bschl TYPE bseg-bschl.
  DATA lv_cont TYPE i VALUE 1.

  CONSTANTS: lc_h TYPE c VALUE 'H',
             lc_s TYPE c VALUE 'S'.

  CONSTANTS: lc_2     TYPE c VALUE '2',
             lc_dot   TYPE c VALUE '.',
             lc_comma TYPE c VALUE ','.

* Inizializza la struttura per il BI
  CLEAR gs_bbseg.
  PERFORM f_inizializza_struttura USING gs_bbseg.

* Tipo record
  MOVE lc_2 TO gs_bbseg-stype.

* Nome tabella
  MOVE 'BBSEG' TO gs_bbseg-tbnam.

* Chiave Contabile
  MOVE p_fs_item-bschl TO gs_bbseg-newbs.

*Conto
  MOVE p_fs_item-conto TO gs_bbseg-newko.

*Tipo movimento
  MOVE p_fs_item-anbwa TO gs_bbseg-newbw.

*Importo
  MOVE p_fs_item-wrbtr TO gs_bbseg-wrbtr.
  REPLACE lc_dot WITH lc_comma INTO gs_bbseg-wrbtr.

* Assignment only for the second GL account
* and Cost Center or Order for first item
* aufnr field will also contain the objnr extracted from COEP table
*          delete initial zero
  IF NOT p_fs_item-aufnr IS INITIAL AND p_fs_item-aufnr NE ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = p_fs_item-aufnr
      IMPORTING
        output = p_fs_item-aufnr.

    MOVE p_fs_item-aufnr TO gs_bbseg-aufnr.
  ENDIF.

  IF NOT p_fs_item-zuonr IS INITIAL AND p_fs_item-zuonr NE ''.
    MOVE p_fs_item-zuonr TO gs_bbseg-zuonr.
  ENDIF.

* Numero del documento acquisti
  IF NOT p_fs_item-ebeln IS INITIAL AND p_fs_item-ebeln NE ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = p_fs_item-ebeln
      IMPORTING
        output = p_fs_item-ebeln.

    MOVE p_fs_item-ebeln TO gs_bbseg-ebeln.
  ENDIF.

* posizione del documento acquisti
  IF NOT p_fs_item-ebelp IS INITIAL AND p_fs_item-ebelp NE ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = p_fs_item-ebelp
      IMPORTING
        output = p_fs_item-ebelp.

    MOVE p_fs_item-ebelp TO gs_bbseg-ebelp.
  ENDIF.

* Numero del documento vendita
  IF NOT p_fs_item-vbeln IS INITIAL AND p_fs_item-vbeln NE ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = p_fs_item-vbeln
      IMPORTING
        output = p_fs_item-vbeln.

    MOVE p_fs_item-vbeln TO gs_bbseg-vbel2.
  ENDIF.

* posizione del documento vendita
  IF NOT p_fs_item-posnr IS INITIAL AND p_fs_item-posnr NE ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = p_fs_item-posnr
      IMPORTING
        output = p_fs_item-posnr.

    MOVE p_fs_item-posnr TO gs_bbseg-posn2.
  ENDIF.

  IF NOT p_fs_item-sgtxt IS INITIAL AND p_fs_item-sgtxt NE ''.
    MOVE p_fs_item-sgtxt TO gs_bbseg-sgtxt.
  ENDIF.

* Profit Center
  IF NOT p_fs_item-ko_prctr IS INITIAL AND p_fs_item-ko_prctr NE ''.
    MOVE p_fs_item-ko_prctr TO gs_bbseg-prctr.
  ENDIF.

* Centor di costo
  IF NOT p_fs_item-kostl IS INITIAL AND p_fs_item-kostl NE ''.
    MOVE p_fs_item-kostl TO gs_bbseg-kostl.
  ENDIF.

* Data Valuta
  IF NOT p_fs_item-valut IS INITIAL AND p_fs_item-valut NE ''.
    MOVE p_fs_item-valut+6(2) TO gs_bbseg-valut(2).
    MOVE p_fs_item-valut+4(2) TO gs_bbseg-valut+2(2).
    MOVE p_fs_item-valut(4)   TO gs_bbseg-valut+4(4).
  ENDIF.

  TRANSFER gs_bbseg TO gv_path.

ENDFORM.                    " F_VALORIZZA_BBSEG
*&---------------------------------------------------------------------*
*&      Form  F_CLOSE_DATASET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_close_dataset .

*Chiudo il file in scrittura
  CLOSE DATASET gv_path.

ENDFORM.                    " F_CLOSE_DATASET

*&---------------------------------------------------------------------*
*&      Form  F_SESSIONE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_sessione .
  DATA: lv_callmode  TYPE rfbifunct,
        lv_data      TYPE sy-datum,
        lv_dataplus  TYPE sy-datum,
        lv_rec_group TYPE apq_grpn.

  CONSTANTS: lc_b     TYPE c VALUE 'B',
             lc_black TYPE c VALUE ' '.

  CLEAR: lv_callmode, lv_data, lv_dataplus.
  WAIT UP TO 2 SECONDS.

  MOVE lc_b     TO lv_callmode.
  MOVE sy-datum TO lv_data.
  MOVE sy-datum TO lv_dataplus.

* Crea sessione
  SUBMIT rfbibl00 WITH ds_name  = gv_path
                  WITH callmode = lv_callmode
                  WITH xinf     = gc_x
                  AND RETURN.

  WAIT UP TO 5 SECONDS.

* Si controlla se la sessione è stata creata correttamente,
  PERFORM f_contr_sess CHANGING  gv_error_sess.

ENDFORM.                    " F_SESSIONE
*&---------------------------------------------------------------------*
*&      Form  F_CONTR_SESS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_LV_ERROR  text
*----------------------------------------------------------------------*
FORM f_contr_sess CHANGING p_error.

  CONSTANTS: ca_bdc(3) TYPE c VALUE 'BDC',
             lc_black  TYPE c VALUE ' '.

  DATA: wa_apqi TYPE apqi.

  DATA: t_apqi      LIKE STANDARD TABLE OF wa_apqi,
        lv_datatype TYPE                   apqi-datatyp,
        lv_qstate   TYPE                   apqi-qstate.

  CLEAR: lv_datatype.

  MOVE lc_black     TO lv_qstate.
  MOVE ca_bdc       TO lv_datatype.

  CALL FUNCTION 'BDC_OBJECT_SELECT'
    EXPORTING
      name             = p_id
      datatype         = lv_datatype
      qstate           = lv_qstate
    TABLES
      apqitab          = t_apqi
    EXCEPTIONS
      invalid_datatype = 1
      OTHERS           = 2.
  IF sy-subrc IS NOT INITIAL.
    MOVE gc_x TO p_error.
  ENDIF.
  IF t_apqi[] IS INITIAL.
    MOVE gc_x TO p_error.
  ENDIF.

ENDFORM.                    " F_CONTR_SESS

*&---------------------------------------------------------------------*
*&      Form  CARICAMENTI_INIZIALI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM caricamenti_iniziali .

  SELECT SINGLE saknr FROM skb1 INTO gv_saknr
   WHERE bukrs = 'SOCIETA'
     AND saknr = p_coge.

  IF sy-subrc NE 0.
    CLEAR: gv_saknr.
  ENDIF.

ENDFORM.                    " CARICAMENTI_INIZIALI

*======================================================================*
*     INITIALIZATION
*======================================================================*
INITIALIZATION.
  DATA: lv_data_sist TYPE sy-datum.

  lv_data_sist = sy-datum.
  CONCATENATE 'AB01_' lv_data_sist+2(2)
              lv_data_sist+4(2)
              lv_data_sist+6(2)
              INTO p_id.

  p_user = sy-uname.

*======================================================================*
* AT SELECTION-SCREEN ON VALUE-REQUEST FOR pfile_lo
*======================================================================*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR pfile_lo.
  PERFORM browse_local_file.

*======================================================================*
*  AT SELECTION-SCREEN ON VALUE-REQUEST FOR pfile_se.
*======================================================================*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR pfile_se.
  PERFORM browse_server_file.

*======================================================================*
* AT SELECTION-SCREEN OUTPUT                                           *
*======================================================================*
AT SELECTION-SCREEN OUTPUT.
  PERFORM selection_screen_output.

*======================================================================*
*     START - OF - SELECTION
*======================================================================*
START-OF-SELECTION.
  PERFORM clear.
  PERFORM caricamenti_iniziali.
  PERFORM load_file.
  PERFORM get_header_items.
  PERFORM check_header.
  PERFORM check_items.
  PERFORM process_fb01_session.

*======================================================================*
*     END - OF - SELECTION
*======================================================================*
END-OF-SELECTION.

  IF gv_error NE gc_x.
    IF gv_error_sess NE gc_x.
      MESSAGE text-s01 TYPE 'S'.
    ELSE.
      MESSAGE text-e01 TYPE 'E'.
    ENDIF.
  ENDIF.