Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
55 kB
0
Indexable
Never
*&---------------------------------------------------------------------*
*& Report      : zmmBseg                                               *
*& Author      : Garofalo
*& Date        : 03.11.2001  Guida 13/1272011                          *
*& Version     : 0.0                                                   *
*& Transazione : ----                                                  *
*& Logical DB  : ---                                                   *
*& Description : Estrazione movimenti da BSEG per data                 *
*&                                                                     *
*& Notes       :                                                       *
*& Daily Sched.: A richiesta                                           *
*&---------------------------------------------------------------------*
*& Togliere documenti contabili e riportare diviva(waers) e unità di prz
*&---------------------------------------------------------------------*
  REPORT zmmmseg_stampa LINE-SIZE 144 LINE-COUNT 60(4) MESSAGE-ID fb.
*
*&---------------------------------------------------------------------*
*& Dichiarazione Tabelle DataBase                                      *
*&---------------------------------------------------------------------*
  TABLES:  mseg,                        " movimenti materiali
           mkpf,                       " testata documento materiali
           mbew,
           bkpf,                       " testata documento contabile
           bseg,                       " posizioni documento contabile
           afpo,                       " Ordine di produzione
           aufk,                       " testata Ordine di produzione
           ekpo,                       " Ordine d'acquisto
           mara,                       " Anagrafica materiali centrale
           marc,                       " Anagrafica per resp.MRP
           makt,                       " descrizione materiale
           ser03.

*&---------------------------------------------------------------------*
*& Tabelle interne                                                     *
*&---------------------------------------------------------------------*
  DATA: wa_mseg LIKE mseg OCCURS  0 WITH HEADER LINE.
  DATA: wa_mkpf LIKE mkpf OCCURS  0 WITH HEADER LINE.
  DATA: x_mseg LIKE mseg OCCURS  1000 WITH HEADER LINE.
  DATA: BEGIN OF st_mseg OCCURS 100,
          matnr                   LIKE mseg-matnr,
          werks                   LIKE mseg-werks,
          lgort                   LIKE mseg-lgort,
          budat                   LIKE mkpf-budat,
          cputm                   LIKE mkpf-cputm,
          bukrs                   LIKE mseg-bukrs,
          maktx(20)               TYPE c,
          menge                   LIKE mseg-menge,
          meins                   LIKE mseg-meins,
          dmbtr                   LIKE mseg-dmbtr,
          waers                   LIKE mseg-waers,
          sakto                   LIKE bseg-hkont,
          shkzg                   LIKE mseg-shkzg,
          wbs_pre(10)             TYPE c,
          wbs_mag(10)             TYPE c,
          ps_psp_pnr              LIKE mseg-ps_psp_pnr,
          mat_pspnr               LIKE  mseg-mat_pspnr,
          charg                   LIKE mseg-charg,
          bwtar                   LIKE mseg-bwtar,
          bwart(3)                TYPE c,
          mblnr                   LIKE mseg-mblnr,
          mjahr                   LIKE mseg-mjahr,
          zeile                   LIKE mseg-zeile,
          kostl                   LIKE mseg-kostl,
          usnam                   LIKE mkpf-usnam,
          ebeln                   LIKE mseg-ebeln,
          ebelp                   LIKE mseg-ebelp,
          pstyp                   LIKE ekpo-pstyp,
          lifnr                   LIKE mseg-lifnr,
          kunnr                   LIKE mseg-kunnr,
          aufnr                   LIKE mseg-aufnr,
          xblnr                   LIKE mkpf-xblnr,
          blart                   LIKE mkpf-blart,
          blaum                   LIKE mkpf-blaum,
          sobkz                   LIKE mseg-sobkz,
          bklas                   LIKE mbew-bklas,
          sgtxt                   LIKE mseg-sgtxt,
          xauto                   LIKE mseg-xauto,
          lgpbe                   LIKE mard-lgpbe,
          sernr                   LIKE objk-sernr,
          auart                   LIKE aufk-auart,
*  Begin of mod. PC 04.02.2022 - Classificazione materiale
          zzgiurisdizione         TYPE zzgiurisdizione,
          zzclassific_item        TYPE zzclassific_item,
          zzgiurisd_sotto_assieme TYPE zzgiurisd_sotto_assieme,
*  End of mod PC 04.02.2022
        END OF st_mseg.
  DATA: BEGIN OF st_mseg_shadow OCCURS 100,
          matnr       LIKE mseg-matnr,
          werks       LIKE mseg-werks,
          lgort       LIKE mseg-lgort,
          budat       LIKE mkpf-budat,
          cputm       LIKE mkpf-cputm,
          bukrs       LIKE mseg-bukrs,
          maktx(20)   TYPE c,
          menge       LIKE mseg-menge,
          meins       LIKE mseg-meins,
          dmbtr       LIKE mseg-dmbtr,
          waers       LIKE mseg-waers,
          sakto       LIKE bseg-hkont,
          shkzg       LIKE mseg-shkzg,
          wbs_pre(10) TYPE c,
          wbs_mag(10) TYPE c,
          ps_psp_pnr  LIKE mseg-ps_psp_pnr,
          mat_pspnr   LIKE  mseg-mat_pspnr,
          charg       LIKE mseg-charg,
          bwtar       LIKE mseg-bwtar,
          bwart(3)    TYPE c,
          mblnr       LIKE mseg-mblnr,
          mjahr       LIKE mseg-mjahr,
          zeile       LIKE mseg-zeile,
          kostl       LIKE mseg-kostl,
          usnam       LIKE mkpf-usnam,
          ebeln       LIKE mseg-ebeln,
          ebelp       LIKE mseg-ebelp,
          pstyp       LIKE ekpo-pstyp,
          lifnr       LIKE mseg-lifnr,
          kunnr       LIKE mseg-kunnr,
          aufnr       LIKE mseg-aufnr,
          xblnr       LIKE mkpf-xblnr,
          blart       LIKE mkpf-blart,
          blaum       LIKE mkpf-blaum,
          sobkz       LIKE mseg-sobkz,
          bklas       LIKE mbew-bklas,
          sgtxt       LIKE mseg-sgtxt,
          xauto       LIKE mseg-xauto,
          lgpbe       LIKE mard-lgpbe,
          sernr       LIKE objk-sernr,
          auart       LIKE aufk-auart,
        END OF st_mseg_shadow.
  DATA: BEGIN OF kk_mseg,
          matnr LIKE mseg-matnr,
          werks LIKE mseg-werks,
          budat LIKE bkpf-budat,
        END OF kk_mseg.

*&---------------------------------------------------------------------*
*& Comodi                                                              *
*&---------------------------------------------------------------------*
  DATA: lin             TYPE p,
        num-rec(9)      TYPE n,
        num-out(9)      TYPE n,
        num-skip(9)     TYPE n,
        sw_trovato(1)   TYPE c,
        sw_erro(1)      TYPE c VALUE 'N',
        c_pstat         LIKE mara-pstat,
        c_sernp         LIKE marc-sernp,
        c_bwart         LIKE mseg-bwart,
        testata(70)     VALUE 'Lista movimenti contabili di Stock',
        tcode           LIKE sy-tcode,
        totale_valore   LIKE mseg-dmbtr,
        campo_wbs       LIKE mseg-mat_pspnr,
        out_pspnr(24)   TYPE c,
        wbs_mag(24)     TYPE c,
        wbs_pre(24)     TYPE c,
        wk_wbs_pre(10)  TYPE c,
        save_bukrs      LIKE mseg-bukrs,
        save_werks      LIKE mseg-werks,
        save_shkzg      LIKE mseg-shkzg,
        save_buzei      LIKE bseg-buzei,
        virgolapunto(2) VALUE '.,',
        campo_menge(15) TYPE c,
        campo_dmbtr(15) TYPE c,
        key-bschl       LIKE bseg-bschl,
        documseg        LIKE bkpf-awkey.
  DATA: e_prps LIKE  prps.
  DATA: test_grid(60).
  DATA: mydate(10).
  DATA: gv_error.

*&---------------------------------------------------------------------*
*& Dati per il salvataggio in excel                                    *
*&---------------------------------------------------------------------*

  DATA: lv_excel_data TYPE xstring,
        it_raw_data   TYPE solix_tab,
        lv_fname      TYPE string.

*&---------------------------------------------------------------------*
*& Select-Options                                                      *
*&---------------------------------------------------------------------*
  SELECT-OPTIONS: s_bukrs FOR x_mseg-bukrs OBLIGATORY,
                  s_werks FOR x_mseg-werks,
                  s_budat FOR mkpf-budat DEFAULT sy-datum,
                  s_matnr FOR x_mseg-matnr.
  SELECT-OPTIONS: s_bwart FOR x_mseg-bwart.
  SELECT-OPTIONS: s_lgort FOR x_mseg-lgort.
  SELECT-OPTIONS: s_sakto FOR x_mseg-sakto.
  SELECT-OPTIONS: s_bklas FOR st_mseg-bklas.

  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (33) text-003 FOR FIELD p_div.
  SELECTION-SCREEN POSITION 35.
  PARAMETERS: p_div AS CHECKBOX  DEFAULT space.
  SELECTION-SCREEN END OF LINE.

  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (33) text-004 FOR FIELD p_cdc.
  SELECTION-SCREEN POSITION 35.
  PARAMETERS: p_cdc AS CHECKBOX  DEFAULT space.
  SELECTION-SCREEN END OF LINE.

  PARAMETERS: p_wbsmag(10).
  PARAMETERS: p_wbspre(10).

  SELECT-OPTIONS : s_kostl   FOR mseg-kostl,
                   s_dispo   FOR marc-dispo,
                   s_matkl   FOR mara-matkl,
                   s_usnam    FOR mkpf-usnam.
*
  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (30) text-005 FOR FIELD p_movmag.
  SELECTION-SCREEN POSITION 35.
  PARAMETERS : p_movmag RADIOBUTTON GROUP rad1 DEFAULT 'X'.
  SELECTION-SCREEN END OF LINE.

  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (30) text-006 FOR FIELD p_presta.
  SELECTION-SCREEN POSITION 35.
  PARAMETERS : p_presta RADIOBUTTON GROUP rad1.
  SELECTION-SCREEN END OF LINE.

  SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (30) text-007 FOR FIELD p_serie.
  SELECTION-SCREEN POSITION 35.
  PARAMETERS : p_serie AS CHECKBOX.
  SELECTION-SCREEN END OF LINE.
**Start A.De Filippis 01.12.2021 11:28:21
  PARAMETERS: p_storn TYPE c NO-DISPLAY.
**End A.De Filippis 01.12.2021 11:28:21

  INCLUDE zalvrout.

* Selection-Screen per la scelta del salvataggio in locale o su server + scelta del path.
  SELECTION-SCREEN: BEGIN OF BLOCK b6 WITH FRAME TITLE text-008.
  PARAMETERS: rb_loc  RADIOBUTTON GROUP rad2 USER-COMMAND d1, "Salvataggio in locale
              rb_serv RADIOBUTTON GROUP rad2 DEFAULT 'X'.     "Salvataggio sul server
  PARAMETERS: p_pathl TYPE string LOWER CASE,
              p_paths TYPE string LOWER CASE DEFAULT '/usr/sap/MD4'.
  SELECTION-SCREEN: END OF BLOCK b6.

  INITIALIZATION.
    PERFORM zalvrout_initalv.

************************************************************************
*                       AT SELECTION SCREEN                            *
************************************************************************

  AT SELECTION-SCREEN OUTPUT.
    PERFORM f_screen.

  AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_pathl.
    PERFORM f_help_custom CHANGING p_pathl.

  AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_paths.
    PERFORM f_help_custom CHANGING p_paths.

************************************************************************
*                        START OF SELECTION                            *
************************************************************************

  START-OF-SELECTION.

    PERFORM f_check.

**Start A.De Filippis 01.12.2021 11:31:43
    IF p_storn IS INITIAL.
      PERFORM loadfieldcat.
    ENDIF.
*    PERFORM loadfieldcat.
**End A.De Filippis 01.12.2021 11:31:43
    CLEAR: x_mseg.
    REFRESH: x_mseg.
    IF s_matnr-low IS INITIAL.
      IF s_budat-low  IS INITIAL AND
         s_budat-high IS INITIAL.
        MESSAGE i014(fb) WITH 'Specificare date'
                              'registrazione'.
        sw_erro = 'Y'.
      ENDIF.
    ENDIF.
*--------------------- apertura ed elaborazione
    IF sw_erro = 'Y'.
      MESSAGE i014(fb) WITH 'Intervallo date'
                            'eccessivo'.
    ELSE.
*--------------------------------------- estrae mseg per data input
      PERFORM estrai_doc_archiviati.
      IF NOT s_matnr-low IS INITIAL.
        PERFORM estrai_mseg_matnr.
      ELSE.
        PERFORM estrai_mkpf_mseg.
      ENDIF.
*--------------------------------------- niente da elaborare
**Start A.De Filippis 01.12.2021 11:32:48
      REFRESH st_mseg_shadow.
      FREE MEMORY ID 'ZSTORMSHADOW_1'.
      DESCRIBE TABLE x_mseg LINES lin.
      IF p_storn IS NOT INITIAL.
        IF lin > 0. " ci sono dati
          PERFORM verifica_mseg.
          LOOP AT st_mseg.
            MOVE-CORRESPONDING st_mseg TO st_mseg_shadow.
            APPEND st_mseg_shadow.
          ENDLOOP.
          EXPORT tab FROM st_mseg_shadow TO MEMORY ID 'ZSTORMSHADOW_1'.
        ENDIF.
      ELSE.
**End A.De Filippis 01.12.2021 11:32:48
        IF lin > 0. " ci sono dati
          PERFORM verifica_mseg.
          PERFORM griglia_mseg.
        ELSE.
          MESSAGE i014(fb) WITH 'Nessun movimento'
                                'per i parametri indicati'.
        ENDIF.
**Start A.De Filippis 01.12.2021 11:33:13
      ENDIF.
**End A.De Filippis 01.12.2021 11:33:13
    ENDIF.

* Switch-case per il salvataggio del file in locale o upload sul server
    CASE 'X'.
      WHEN rb_loc.
        lv_fname = p_pathl.
        CONCATENATE lv_fname '\Report_' sy-datum sy-uzeit '.xlsx' INTO lv_fname.
        PERFORM converti_in_xlsx CHANGING lv_excel_data.
        it_raw_data = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_excel_data ).
        PERFORM scarica_file_excel USING lv_fname lv_excel_data it_raw_data.
      WHEN rb_serv.
        lv_fname = p_paths.
        CONCATENATE lv_fname '/Report_' sy-datum sy-uzeit '.xlsx' INTO lv_fname.
        PERFORM converti_in_xlsx CHANGING lv_excel_data.
        it_raw_data = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_excel_data ).
        PERFORM carica_file_excel USING lv_fname lv_excel_data it_raw_data.
    ENDCASE.
*---------------------------------------------------------------------*
*       FORM estrai_doc_archiviati                                    *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM estrai_doc_archiviati.
    CLEAR: wa_mkpf, wa_mseg.
    REFRESH: wa_mkpf, wa_mseg.
    CALL FUNCTION 'Z_MM_READ_MOV_FROM_ARCH'
      TABLES
        wmkpf = wa_mkpf
        wmseg = wa_mseg.
    IF sy-subrc >< 0.
      EXIT.
    ENDIF.
    SORT wa_mkpf BY mblnr mjahr.
    LOOP AT wa_mseg
       WHERE  matnr IN s_matnr AND
              werks IN s_werks AND
              bukrs IN s_bukrs AND
              lgort IN s_lgort AND
              kostl IN s_kostl.
      MOVE-CORRESPONDING wa_mseg TO x_mseg.
      x_mseg-xauto = 'E'.
*
      IF p_div > ' '.
        IF x_mseg-sobkz = ' '.
        ELSE.
          CONTINUE.
        ENDIF.
      ENDIF.
      IF p_cdc > ' '.
        IF x_mseg-kostl > ' '.
        ELSE.
          CONTINUE.
        ENDIF.
      ENDIF.
* NEWCARM
      READ TABLE wa_mkpf WITH KEY
       mblnr = wa_mseg-mblnr
       mjahr = wa_mseg-mjahr.
      IF sy-subrc >< 0.
        WRITE: /001 '* ERRORE ACCESSO MKPF archiviato',
         wa_mseg-mblnr, wa_mseg-mjahr.
        CONTINUE.
      ENDIF.
      IF wa_mkpf-budat  IN s_budat  AND
       wa_mkpf-usnam  IN s_usnam.
        MOVE-CORRESPONDING wa_mkpf TO mkpf.
      ELSE.
        CONTINUE.
      ENDIF.
* NEWCARM
      IF x_mseg-parbu >< ' '.
        PERFORM genera_mov.
      ENDIF.
      APPEND x_mseg.
      num-rec = num-rec + 1.
    ENDLOOP.
  ENDFORM.                    "estrai_doc_archiviati
*---------------------------------------------------------------------*
*       FORM estrai_mseg_matnr                                        *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM estrai_mseg_matnr.
    SELECT  * INTO x_mseg  FROM mseg
       WHERE  matnr IN s_matnr AND
              werks IN s_werks AND
              bukrs IN s_bukrs AND
              lgort IN s_lgort AND
              kostl IN s_kostl.

      IF p_div > ' '.
        IF x_mseg-sobkz = ' '.
        ELSE.
          CONTINUE.
        ENDIF.
      ENDIF.
      IF p_cdc > ' '.
        IF x_mseg-kostl > ' '.
        ELSE.
          CONTINUE.
        ENDIF.
      ENDIF.
* NEWCARM
      SELECT SINGLE *  FROM  mkpf
             WHERE mblnr = x_mseg-mblnr AND
                   mjahr = x_mseg-mjahr.
      IF sy-subrc = 0         AND
           mkpf-budat  IN s_budat  AND
           mkpf-usnam  IN s_usnam.
      ELSE.
        CONTINUE.
      ENDIF.
* NEWCARM
      IF x_mseg-parbu >< ' '.
        PERFORM genera_mov.
      ENDIF.
      APPEND x_mseg.
      num-rec = num-rec + 1.
    ENDSELECT.
  ENDFORM.                             " ESTRAI_MSEG
*
*&---------------------------------------------------------------------*
*&      Form  estrai_mkpf
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
  FORM estrai_mkpf_mseg.
*
    PERFORM messaggio_on_line USING 'Estrae MSEG per data'.
*------------------------------ no MIV pregresso e righe non attive
    SELECT *
                          FROM  mkpf
           WHERE budat  IN s_budat  AND
                 usnam  IN s_usnam.
* ------------------- considera solo se cod. valuta = blank. -----------

      PERFORM estrai_mseg.
*
*
    ENDSELECT.
*
  ENDFORM.                             " estrai_partite.
*&---------------------------------------------------------------------*
*&      Form  ESTRAI_MSEG
*&---------------------------------------------------------------------*
  FORM estrai_mseg.
*
    SELECT  * INTO x_mseg  FROM mseg
       WHERE
              mblnr = mkpf-mblnr AND
              mjahr = mkpf-mjahr.
      IF p_div > ' '.
        IF x_mseg-sobkz = ' '.
        ELSE.
          CONTINUE.
        ENDIF.
      ENDIF.
      IF p_cdc > ' '.
        IF x_mseg-kostl > ' '.
        ELSE.
          CONTINUE.
        ENDIF.
      ENDIF.
      IF x_mseg-parbu >< ' '.
        PERFORM genera_mov.
      ENDIF.
      APPEND x_mseg.
      num-rec = num-rec + 1.
    ENDSELECT.
  ENDFORM.                             " ESTRAI_MSEG
*&---------------------------------------------------------------------*
*&      Form  genera_mov per interdivisionali
*&---------------------------------------------------------------------*
  FORM genera_mov.
*
    IF x_mseg-bwart = '301' OR x_mseg-bwart = '302'.
      EXIT.
    ENDIF.
    IF x_mseg-bwart = '303' OR x_mseg-bwart = '304'.
      EXIT.
    ENDIF.
    IF x_mseg-bwart = '305' OR x_mseg-bwart = '306'.
      EXIT.
    ENDIF.
    save_bukrs = x_mseg-bukrs.
    save_werks = x_mseg-werks.
    save_shkzg = x_mseg-shkzg.
*
    x_mseg-bukrs = x_mseg-parbu.
    CONCATENATE x_mseg-bukrs(1) save_werks+1(3) INTO x_mseg-werks.
    APPEND x_mseg.
*
    x_mseg-bwart = 'BMI'.
    IF x_mseg-shkzg = 'H'.
      x_mseg-shkzg = 'S'.
    ELSE.
      x_mseg-shkzg = 'H'.
    ENDIF.
*
    APPEND x_mseg.
    x_mseg-bwart = 'BMU'.
    x_mseg-shkzg = save_shkzg.
    x_mseg-bukrs = save_bukrs.
    x_mseg-werks = save_werks.
  ENDFORM.                             " GENERA_MOV
*&---------------------------------------------------------------------*
*&      Form  VERIFICA_MSEG
*&---------------------------------------------------------------------*
  FORM verifica_mseg.

    PERFORM messaggio_on_line USING 'Carica ST_MSEG'.
    CLEAR st_mseg.
*------------------------------ legge le righe XMSEG estratte
    LOOP AT x_mseg.
*--------------- verifica selezion
      IF  x_mseg-bukrs IN s_bukrs AND
          x_mseg-werks IN s_werks AND
          x_mseg-bwart IN s_bwart AND
          x_mseg-matnr IN s_matnr AND
          x_mseg-lgort IN s_lgort AND
          x_mseg-kostl IN s_kostl.
      ELSE.
        num-skip = num-skip + 1.
        CONTINUE.
      ENDIF.
* decodifica WBS di magazzino e prelievo
      IF x_mseg-bwart = 'BMU'.
        CLEAR x_mseg-ps_psp_pnr.
      ENDIF.
* ricerca WBS per ordini di produzione
      CLEAR st_mseg-auart.
      IF x_mseg-bwart = '261' OR x_mseg-bwart = '262'.
        SELECT SINGLE * FROM afpo
         WHERE aufnr = x_mseg-aufnr.
        IF sy-subrc = 0.
          x_mseg-ps_psp_pnr = afpo-projn.
        ENDIF.
      ENDIF.
******* fg 04042013   inizio
      IF x_mseg-bwart = '261' OR x_mseg-bwart = '262' OR
         x_mseg-bwart = '101' OR x_mseg-bwart = '102'.
        SELECT SINGLE * FROM aufk
         WHERE aufnr = x_mseg-aufnr.
        IF sy-subrc = 0.
          st_mseg-auart = aufk-auart.
        ENDIF.
      ENDIF.
******* fg 04042013  fine

      campo_wbs = x_mseg-mat_pspnr.
      PERFORM decodifica_wbs.
      wbs_mag = out_pspnr.
      campo_wbs = x_mseg-ps_psp_pnr.
      PERFORM decodifica_wbs.
      wbs_pre = out_pspnr.
      IF p_wbsmag > ' '.
        IF wbs_mag = p_wbsmag.
        ELSE.
          num-skip = num-skip + 1.
          CONTINUE.
        ENDIF.
      ENDIF.
      wk_wbs_pre = wbs_pre.
      IF wbs_pre > ' '.
      ELSE.
        IF x_mseg-sobkz = 'Q' AND wbs_mag > ' '.
          wk_wbs_pre = wbs_mag.
        ENDIF.
      ENDIF.
      IF p_wbspre > ' '.
        IF wk_wbs_pre = p_wbspre.
        ELSE.
          num-skip = num-skip + 1.
          CONTINUE.
        ENDIF.
      ENDIF.
*      IF p_kostl > ' '.
*        IF x_mseg-kostl = p_kostl.
*        ELSE.
*          num-skip = num-skip + 1.
*          CONTINUE.
*        ENDIF.
*      ENDIF.
*----------------------------- selettività sul responsabile
      IF s_dispo-low > ' ' OR s_dispo-high > ' '.
        SELECT SINGLE * FROM marc
               WHERE werks = x_mseg-werks AND
                     matnr = x_mseg-matnr.
        IF sy-subrc >< 0.
          CONTINUE.
        ELSE.
          IF marc-dispo IN s_dispo.
          ELSE.
            CONTINUE.
          ENDIF.
        ENDIF.
      ENDIF.
* Tipo posizione da ODA per verifica MBRL
      IF x_mseg-ebeln > ' ' AND x_mseg-ebelp > ' '.
        SELECT SINGLE pstyp INTO st_mseg-pstyp
           FROM ekpo
               WHERE ebeln = x_mseg-ebeln AND
                     ebelp = x_mseg-ebelp.
        IF sy-subrc = 0 AND x_mseg-bwart = '122' AND st_mseg-pstyp = '3'.
          SELECT SINGLE bwart INTO c_bwart
           FROM mseg WHERE mblnr = x_mseg-mblnr AND
                           mjahr = x_mseg-mjahr AND
                           bwart = '956'.
          IF sy-subrc >< 0.
            st_mseg-pstyp = 'E'.
          ENDIF.
        ENDIF.
      ENDIF.
*----------------------------- selettività sul gruppo merce
      IF s_matkl-low > ' ' OR s_matkl-high > ' '.
        IF x_mseg-matnr > ' '.
          SELECT SINGLE * FROM mara
                 WHERE matnr = x_mseg-matnr.
          IF sy-subrc >< 0.
            CONTINUE.
          ELSE.
            IF mara-matkl IN s_matkl.
            ELSE.
              CONTINUE.
            ENDIF.
          ENDIF.
        ELSE.
          IF x_mseg-ebeln > ' ' AND x_mseg-ebelp > ' '.
            SELECT SINGLE * FROM ekpo
                   WHERE ebeln = x_mseg-ebeln AND
                         ebelp = x_mseg-ebelp.
            IF sy-subrc >< 0.
              CONTINUE.
            ELSE.
              IF ekpo-matkl IN s_matkl.
              ELSE.
                CONTINUE.
              ENDIF.
            ENDIF.
          ELSE.
            CONTINUE.
          ENDIF.
        ENDIF.
      ENDIF.
* ------------------ accesso conto stock
      IF x_mseg-dmbtr > 0.
        IF x_mseg-sakto IS INITIAL OR x_mseg-parbu <> ' '.
          PERFORM accesso_conto.
        ENDIF.
      ENDIF.
*---------------- verifica conto in selection
      IF x_mseg-sakto IN s_sakto.
        num-out = num-out + 1.
      ELSE.
        num-skip = num-skip + 1.
        CONTINUE.
      ENDIF.

*----------- trasferisce campi in output con forzatura wbs ricevente
      IF x_mseg-ps_psp_pnr IS INITIAL.
        IF x_mseg-sobkz = 'Q' AND NOT x_mseg-mat_pspnr IS INITIAL.
          x_mseg-ps_psp_pnr = x_mseg-mat_pspnr.
        ENDIF.
      ENDIF.
      MOVE-CORRESPONDING   x_mseg TO st_mseg.
* accesso MBEW per bklas
      CLEAR st_mseg-bklas.
      IF x_mseg-matnr > ' '.
        SELECT SINGLE pstat INTO c_pstat
         FROM mara WHERE matnr = x_mseg-matnr.
        IF c_pstat CS 'B'.
          SELECT SINGLE bklas INTO st_mseg-bklas
           FROM mbew WHERE matnr = x_mseg-matnr AND
                           bwkey = x_mseg-werks AND
                           bwtar = x_mseg-bwtar.
          IF sy-subrc >< 0.
            WRITE: /001 '* ERRORE ACCESSO MSEG',
             x_mseg-matnr, x_mseg-werks,
             x_mseg-bwtar.
          ENDIF.
        ENDIF.
      ENDIF.
      IF st_mseg-bklas IN s_bklas.
      ELSE.
        CONTINUE.
      ENDIF.
*
      IF x_mseg-xauto = 'E'.
        READ TABLE wa_mkpf WITH KEY
         mblnr = x_mseg-mblnr
         mjahr = x_mseg-mjahr.
        IF sy-subrc >< 0.
          WRITE: /001 '* ERROR1 ACCESSO MKPF archiviato',
           x_mseg-mblnr, x_mseg-mjahr.
          CONTINUE.
        ELSE.
          MOVE-CORRESPONDING wa_mkpf TO mkpf.
        ENDIF.
      ELSE.
        SELECT  SINGLE * FROM mkpf WHERE mblnr = x_mseg-mblnr AND
                 mjahr = x_mseg-mjahr.
        IF sy-subrc NE 0.
          WRITE: /001 '* ERRORE ACCESSO MKPF',documseg.
        ENDIF.
      ENDIF.
* considera / escludi Prestazioni
      IF mkpf-tcode2(4) = 'ML81' OR
         mkpf-tcode2(4) = 'ML85'.
        IF p_presta > ' '.
        ELSE.
          CONTINUE.
        ENDIF.
      ELSE.
        IF p_presta > ' '.
          CONTINUE.
        ENDIF.
      ENDIF.
* considera / escludi Prestazioni
      IF st_mseg-bwtar = st_mseg-charg.
        CLEAR st_mseg-charg.
      ENDIF.
      st_mseg-budat = mkpf-budat.
      st_mseg-cputm = mkpf-cputm.
      st_mseg-usnam = mkpf-usnam.
      st_mseg-xblnr = mkpf-xblnr.
      st_mseg-blart = mkpf-blart.
      st_mseg-blaum = mkpf-blaum.
*
      st_mseg-zeile = x_mseg-zeile.
*
      IF x_mseg-ebeln IS INITIAL.
        st_mseg-meins = x_mseg-meins.
        st_mseg-menge = x_mseg-menge.
      ELSE.
        st_mseg-meins = x_mseg-erfme.
        st_mseg-menge = x_mseg-erfmg.
      ENDIF.
      st_mseg-dmbtr = x_mseg-dmbtr.
      IF x_mseg-shkzg = 'H'.
*        st_mseg-segno-menge = '-'.
*        st_mseg-segno-dmbtr = '-'.
*       IF p_greed > ' ' .
        COMPUTE st_mseg-menge = st_mseg-menge * -1.
        COMPUTE st_mseg-dmbtr = st_mseg-dmbtr * -1.
*        ENDIF.
      ELSE.
*        IF p_greed > ' ' .
        COMPUTE st_mseg-menge = st_mseg-menge * 1.
        COMPUTE st_mseg-dmbtr = st_mseg-dmbtr * 1.
*        ENDIF.
*        st_mseg-segno-menge = ' '.
*        st_mseg-segno-dmbtr = ' '.
      ENDIF.
*
      st_mseg-wbs_mag = wbs_mag.
      st_mseg-wbs_pre = wbs_pre.
*
      SELECT SINGLE * FROM makt
     WHERE
       matnr = x_mseg-matnr AND
       spras  = 'I'.
      IF  sy-subrc NE 0 .
        st_mseg-maktx = ' '.
      ELSE.
        st_mseg-maktx = makt-maktx.
      ENDIF.
*  coordinata
      IF p_movmag > ' '.
        CLEAR st_mseg-lgpbe.
        SELECT SINGLE lgpbe INTO st_mseg-lgpbe
        FROM mard WHERE matnr = x_mseg-matnr AND
                        werks = x_mseg-werks AND
                        lgort = x_mseg-lgort.
      ENDIF.

*  Begin of mod. PC 04.02.2022 - aggiunta lettura classificazione materiale
      PERFORM get_classification_matnr.
*  End of mod PC 04.02.2022

      num-out = num-out + 1.
      APPEND st_mseg.
      IF NOT p_serie IS INITIAL.
        PERFORM accesso_serie.
      ENDIF.
    ENDLOOP.
*
  ENDFORM.                             " VERIFICA_MSEG
*&---------------------------------------------------------------------*
*&      Form  accesso_serie
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
  FORM accesso_serie.
    SELECT SINGLE sernp INTO c_sernp
      FROM  marc
      WHERE matnr = st_mseg-matnr AND
            werks = st_mseg-werks.
    IF c_sernp = 'ZPP2'.
      CLEAR st_mseg-menge.
      CLEAR st_mseg-dmbtr.
      SELECT  * FROM ser03 UP TO 1 ROWS
                WHERE mblnr EQ st_mseg-mblnr AND
                      mjahr EQ st_mseg-mjahr AND
                      zeile EQ st_mseg-zeile AND
                      bwart EQ st_mseg-bwart AND
                      shkzg EQ st_mseg-shkzg.
*
*--------------------estrazione Serial Number
        SELECT  sernr INTO st_mseg-sernr
          FROM objk
                   WHERE obknr EQ ser03-obknr.
          IF sy-subrc = 0.
            APPEND st_mseg.
          ENDIF.
        ENDSELECT.
      ENDSELECT.
      CLEAR st_mseg-sernr.
    ENDIF.
  ENDFORM.                    "accesso_serie
*&---------------------------------------------------------------------*
*&      Form  accesso_conto
*&---------------------------------------------------------------------*
  FORM accesso_conto.
    IF x_mseg-parbu >< ' '.
      IF x_mseg-bwart = '201' OR x_mseg-bwart = '202' OR
         x_mseg-bwart = '221' OR x_mseg-bwart = '222' OR
         x_mseg-bwart = '261' OR x_mseg-bwart = '262' OR
         x_mseg-bwart = '922'.
        EXIT.
      ENDIF.
    ENDIF.
*
    sw_trovato = 'N'.
    CONCATENATE x_mseg-mblnr x_mseg-mjahr INTO documseg.
*------------------------------ legge testata doc. contabile
    SELECT  * FROM bkpf WHERE awtyp = 'MKPF' AND
              awkey = documseg AND bukrs = x_mseg-bukrs.
      sw_trovato = 'Y'.
      IF x_mseg-bwart = '101' OR
         x_mseg-bwart = '102' OR
         x_mseg-bwart = '122'.
      ELSE.
        EXIT.
      ENDIF.
    ENDSELECT.
    IF sw_trovato NE 'Y'.
      WRITE: /001 '* ERRORE ACCESSO BKPF',documseg,x_mseg-bwart,
                              x_mseg-bukrs.
      EXIT.
    ENDIF.
    sw_trovato = 'N'.
    IF x_mseg-bwart = 'BMU'.
      IF x_mseg-shkzg = 'H'.
        key-bschl = '99'.
      ELSE.
        key-bschl = '89'.
      ENDIF.
    ELSE.
      IF x_mseg-bwart = 'BMI'.
        IF x_mseg-shkzg = 'H'.
          key-bschl = '91'.
        ELSE.
          key-bschl = '81'.
        ENDIF.
      ELSE.
        IF x_mseg-shkzg = 'S'.
          key-bschl = '89'.
        ELSE.
          key-bschl = '99'.
        ENDIF.
      ENDIF.
    ENDIF.
    SELECT   * FROM bseg WHERE
               bukrs = bkpf-bukrs AND
               belnr = bkpf-belnr AND
               gjahr = bkpf-gjahr AND
               bschl = key-bschl AND
*                 bwkey = X_MSEG-werks and
               bwtar = x_mseg-bwtar AND
               matnr = x_mseg-matnr.
      IF x_mseg-dmbtr = bseg-dmbtr AND
         x_mseg-ebeln = bseg-ebeln AND
         x_mseg-ebelp = bseg-ebelp.
        IF sw_trovato = 'Y'.
          WRITE: /001 '* DOPPIO BSEG',documseg,x_mseg-bwart,
                                  x_mseg-bukrs,key-bschl,bkpf-belnr,
                                  bkpf-gjahr,x_mseg-matnr.
        ELSE.
          x_mseg-sakto = bseg-hkont.
          save_buzei = bseg-buzei.
          sw_trovato = 'Y'.
        ENDIF.
      ENDIF.
    ENDSELECT.
    IF sw_trovato <> 'Y'.
      WRITE: /001 '* ERRORE ACCESSO BSEG',documseg,x_mseg-bwart,
                              x_mseg-bukrs,key-bschl,bkpf-belnr,
                              bkpf-gjahr,x_mseg-matnr.
      EXIT.
    ENDIF.
    IF  x_mseg-parbu <> ' '.
      PERFORM accesso_bseg.
    ELSE.
      IF x_mseg-bwart = '301' OR x_mseg-bwart = '302' OR
         x_mseg-bwart = '303' OR x_mseg-bwart = '304'.
        IF x_mseg-bukrs(1) <> x_mseg-umwrk(1).
          PERFORM accesso_bseg.
        ENDIF.
      ENDIF.
    ENDIF.
*  WRITE: /001 '* OK ACCESSO BSEG',X_MSEG-mblnr,BSEG-hkont,X_MSEG-matnr.
  ENDFORM.                    "accesso_conto
*&---------------------------------------------------------------------*
*&      Form  accesso_bseg
*&---------------------------------------------------------------------*
  FORM accesso_bseg.
    IF key-bschl > '90'.
      key-bschl = '40'.
    ELSE.
      key-bschl = '50'.
    ENDIF.
    SELECT  SINGLE * FROM bseg WHERE
             bukrs = bkpf-bukrs AND
             belnr = bkpf-belnr AND
             gjahr = bkpf-gjahr AND
             bschl = key-bschl.
    IF sy-subrc NE 0.
      WRITE: /001 '* ERRORE calcolo buzei',documseg,x_mseg-bwart,
                              x_mseg-bukrs,key-bschl,bkpf-belnr,
                              bkpf-gjahr,save_buzei.
      save_buzei = save_buzei + 1.
    ELSE.
      save_buzei = save_buzei + bseg-buzei - 1.
    ENDIF.
    SELECT SINGLE  * FROM bseg WHERE
               bukrs = bkpf-bukrs AND
               belnr = bkpf-belnr AND
               gjahr = bkpf-gjahr AND
               buzei = save_buzei.
    IF sy-subrc NE 0.
      WRITE: /001 '* ERRORE RECUPERO BSEG',documseg,x_mseg-bwart,
                              x_mseg-bukrs,key-bschl,bkpf-belnr,
                              bkpf-gjahr,save_buzei.
      EXIT.
    ENDIF.
    x_mseg-sakto = bseg-hkont.
    IF bseg-bschl = '40' OR bseg-bschl = '50'.
*  WRITE: /001 '* OK ACCESSO BSEG',X_MSEG-mblnr,BSEG-hkont,X_MSEG-matnr.
    ELSE.
      WRITE: /001 '* errore ACCESSO BSEG',x_mseg-mblnr,bseg-hkont,
                     x_mseg-matnr.
      x_mseg-sakto = 'XXXXXXXX'.
      x_mseg-werks = x_mseg-parbu.
    ENDIF.
  ENDFORM.                    "accesso_bseg

*&---------------------------------------------------------------------*
*&      Form  decodifica_wbs
*&---------------------------------------------------------------------*
  FORM decodifica_wbs.
    out_pspnr = ' '.
    IF campo_wbs = 0.
      EXIT.
    ENDIF.

    CALL FUNCTION 'CJPN_GET_WBS_ELEMENT'
      EXPORTING
        i_pspnr     = campo_wbs
      IMPORTING
        e_prps      = e_prps
      EXCEPTIONS
        input_error = 1
        not_found   = 2
        OTHERS      = 3.

    IF sy-subrc <> 0.
      MESSAGE s000 WITH 'Commessa inesistente'
                                  campo_wbs.
      EXIT.
    ELSE.
      out_pspnr = e_prps-posid(12).
    ENDIF.
  ENDFORM.                    "decodifica_wbs
*&---------------------------------------------------------------------*
*& LINE-selection                                                      *
*&---------------------------------------------------------------------*
*
*  AT LINE-SELECTION.
*    PERFORM schedula_mb03.
*---------------------------------------------------------------------*
*       FORM schedula_mb03                                            *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM schedula_mb03.
*
    SET PARAMETER ID 'MBN' FIELD st_mseg-mblnr.
    SET PARAMETER ID 'MJA' FIELD st_mseg-mjahr.
    tcode = 'MB03'.
    CALL TRANSACTION tcode AND SKIP FIRST SCREEN.
  ENDFORM.                    "schedula_mb03
*---------------------------------------------------------------------*
*       FORM schedula_co03                                            *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM schedula_co03.
    SET PARAMETER ID 'ANR' FIELD st_mseg-aufnr.
    tcode = 'CO03'.
    CALL TRANSACTION tcode AND SKIP FIRST SCREEN.
  ENDFORM.                    "schedula_co03
*---------------------------------------------------------------------*
*       FORM schedula_me23n                                           *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM schedula_me23n.
    SET PARAMETER ID 'BES' FIELD st_mseg-ebeln.
    tcode = 'ME23N'.
    CALL TRANSACTION tcode AND SKIP FIRST SCREEN.
  ENDFORM.                    "schedula_me23n
*---------------------------------------------------------------------*
*       FORM schedula_vl03nn                                          *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM schedula_vl03n.
    SET PARAMETER ID 'VL' FIELD st_mseg-xblnr.
    tcode = 'VL03N'.
    CALL TRANSACTION tcode AND SKIP FIRST SCREEN.
  ENDFORM.                    "schedula_vl03n
*&---------------------------------------------------------------------*
*&      Form  MESSAGGIO_ON_LINE
*&---------------------------------------------------------------------*
*& Display a video dello stato dell'elaborazione                       *
*&---------------------------------------------------------------------*
  FORM messaggio_on_line USING testo.
*
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        text = testo.
*
  ENDFORM.                               " MESSAGGIO_ON_LINE
*---------------------------------------------------------------------*
*       FORM loadfieldcat                                             *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM loadfieldcat.
    WRITE sy-datum TO mydate DD/MM/YYYY .

    CONCATENATE  'Lista Movimenti gestionali - Data Elaborazione: '
                 mydate
                      INTO test_grid RESPECTING BLANKS.
    PERFORM zalvrout_buildheader USING test_grid.
    PERFORM zalvrout_addfieldcat USING
      '' 'XAUTO' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'MATNR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      'maktx' '' '' 'Descrizione mat.' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'WERKS' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'LGORT' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'BWTAR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'BKLAS' 'MBEW' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'BWART' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'SOBKZ' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'MENGE' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'MEINS' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'DMBTR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'LGPBE' 'MARD' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'WAERS' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'PS_PSP_PNR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'MAT_PSPNR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'BUDAT' 'BKPF' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'CPUTM' 'MKPF' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'BUKRS' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'SAKTO' 'MSEG' '' 'A' '' '' ''.
*    PERFORM zalvrout_addfieldcat USING
*      '' 'PSPHI' 'PRPS' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'KOSTL' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'MBLNR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
  '' 'ZEILE' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'MJAHR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'USNAM' 'MKPF' '' 'A' '' '' ''.

*nuove
    PERFORM zalvrout_addfieldcat USING
      '' 'EBELN' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'EBELP' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'PSTYP' 'EKPO' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'LIFNR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'KUNNR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'AUFNR' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      'AUART' '' '' 'Tipo OdP' 'A' '' '' ''.

    PERFORM zalvrout_addfieldcat USING
      '' 'CHARG' 'MSEG' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'XBLNR' 'MKPF' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'BLART' 'MKPF' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'SERNR' 'EQUI' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'BLAUM' 'MKPF' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
      '' 'SHKZG' 'MKPF' '' 'A' '' '' ''.
* shkzg
    PERFORM zalvrout_addfieldcat USING
      '' 'SGTXT' 'MSEG' '' 'A' '' '' ''.

*  Begin of mod. PC 04.02.2022 - Classificazione materiale
    PERFORM zalvrout_addfieldcat USING
    '' 'ZZGIURISDIZIONE' 'ZSDRDS' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
    '' 'ZZCLASSIFIC_ITEM' 'ZSDRDS' '' 'A' '' '' ''.
    PERFORM zalvrout_addfieldcat USING
  '' 'ZZGIURISD_SOTTO_ASSIEME' 'ZSDRDS' '' 'A' '' '' ''.
*  End of mod PC 04.02.2022

  ENDFORM.                    "loadfieldcat
*---------------------------------------------------------------------*
*       FORM griglia_bsis                                             *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
  FORM griglia_mseg.
    SORT st_mseg BY budat cputm.
    PERFORM messaggio_on_line USING 'Prepara griglia'.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_background_id         = 'ALV_BACKGROUND'            "QBJ50
        i_callback_program      = g_repid
        i_callback_user_command = 'ALV_CALLBACK_USER_COMMAND'
*       I_STRUCTURE_NAME        = 'CYDETALL'
        it_fieldcat             = g_fieldcat
        is_layout               = gs_layout
        i_save                  = g_save
        is_variant              = g_variant
        it_events               = gt_events[]
        i_grid_title            = testata
        it_special_groups       = gt_sp_group[]
        it_sort                 = gt_sort[]
      IMPORTING
        e_exit_caused_by_caller = g_exit_caused_by_caller
        es_exit_caused_by_user  = gs_exit_caused_by_user
      TABLES
        t_outtab                = st_mseg
      EXCEPTIONS
        program_error           = 1
        OTHERS                  = 2.
    IF sy-subrc = 0.
      IF g_exit_caused_by_caller = 'X'.
*"  Forced Exit by calling program
*"  <do_something>.
      ELSE.
*"  User left list via F3, F12 or F15
        IF gs_exit_caused_by_user-back = 'X'.       "F3
*"    <do_something>.
        ELSE.
          IF gs_exit_caused_by_user-exit = 'X'.     "F15
*"      <do_something>.
          ELSE.
            IF gs_exit_caused_by_user-cancel = 'X'. "F12
*"        <do_something>.
            ELSE.
*"        should not occur!
*"        <do_Abnormal_End>.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
    ELSE.
*"Fatal error callin ALV
* MESSAGE AXXX(XY) WITH ...
    ENDIF.
*"Form routines
    "


  ENDFORM.                    " PREPARA_GRIGLIA
*---------------------------------------------------------------------*
*       FORM ALV_CALLBACK_USER_COMMAND                                *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  r_ucomm                                                       *
*  -->  ls_selfield                                                   *
*---------------------------------------------------------------------*
  FORM alv_callback_user_command
   USING r_ucomm TYPE sy-ucomm   ls_selfield TYPE slis_selfield.
    READ TABLE st_mseg INDEX ls_selfield-tabindex.
    IF sy-subrc = 0.
      IF ls_selfield-sel_tab_field = '1-EBELN'.
        PERFORM schedula_me23n.
      ELSE.
        IF ls_selfield-sel_tab_field = '1-AUFNR'.
          PERFORM schedula_co03.
        ELSE.
          IF ls_selfield-sel_tab_field = '1-XBLNR'.
            PERFORM schedula_vl03n.
          ELSE.
            PERFORM schedula_mb03.
          ENDIF.
        ENDIF.
      ENDIF.
    ELSE.
      PERFORM schedula_mb03.
    ENDIF.
*    ENDIF.
  ENDFORM.                    "alv_callback_user_command
*&---------------------------------------------------------------------*
*&      Form  GET_CLASSIFICATION_MATNR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_ST_MSEG  text
*----------------------------------------------------------------------*
  FORM get_classification_matnr.
*  Lettura classificazioni materiale
    DATA: lt_allocvaluesnum  LIKE TABLE OF bapi1003_alloc_values_num,
          lt_allocvalueschar LIKE TABLE OF bapi1003_alloc_values_char,
          lt_allocvaluescurr LIKE TABLE OF bapi1003_alloc_values_curr,
          lt_return          LIKE TABLE OF bapiret2,
          lv_objectkey       LIKE  bapi1003_key-object,
          wa_allocvalueschar LIKE LINE OF lt_allocvalueschar.

    CONSTANTS: c_norm_exp      TYPE atnam VALUE 'NORM_EXP',
               c_nr_exp_usa    TYPE atnam VALUE 'NR_EXP_USA',
               c_cont_usa_ctrl TYPE atnam VALUE 'CONT_USA_CTRL'.

    CLEAR: st_mseg-zzgiurisdizione ,st_mseg-zzclassific_item ,st_mseg-zzgiurisd_sotto_assieme.

    CHECK st_mseg-matnr IS NOT INITIAL.
    CLEAR: lv_objectkey.
    REFRESH: lt_allocvalueschar[],lt_allocvaluesnum[],
             lt_allocvaluescurr[],lt_return[].

    lv_objectkey = st_mseg-matnr.
    CALL FUNCTION 'BAPI_OBJCL_GETDETAIL'
      EXPORTING
        objectkey       = lv_objectkey
        objecttable     = 'MARA'
        classnum        = 'CLS_MAT'
        classtype       = '001'
        keydate         = sy-datum
        language        = sy-langu
      TABLES
        allocvaluesnum  = lt_allocvaluesnum
        allocvalueschar = lt_allocvalueschar
        allocvaluescurr = lt_allocvaluescurr
        return          = lt_return.

    LOOP AT lt_allocvalueschar INTO wa_allocvalueschar WHERE charact EQ c_norm_exp OR
                                                             charact EQ c_nr_exp_usa OR
                                                             charact EQ c_cont_usa_ctrl.

      CASE wa_allocvalueschar-charact.
        WHEN c_norm_exp.
          st_mseg-zzgiurisdizione = wa_allocvalueschar-value_char.
        WHEN c_nr_exp_usa.
          st_mseg-zzclassific_item = wa_allocvalueschar-value_char.
        WHEN c_cont_usa_ctrl.
          st_mseg-zzgiurisd_sotto_assieme = wa_allocvalueschar-value_char.
      ENDCASE.

      CLEAR wa_allocvalueschar.
    ENDLOOP.

    IF st_mseg-zzgiurisdizione IS INITIAL.
      st_mseg-zzgiurisdizione = 'NO'.
    ENDIF.
    IF st_mseg-zzclassific_item IS INITIAL.
      st_mseg-zzclassific_item = 'NO'.
    ENDIF.
    IF st_mseg-zzgiurisd_sotto_assieme IS INITIAL.
      st_mseg-zzgiurisd_sotto_assieme = 'NO'.
    ENDIF.

  ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  F_SCREEN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
  FORM f_screen .
    IF rb_serv IS NOT INITIAL.
      LOOP AT SCREEN.
        IF screen-name CS 'P_PATHL'.
          screen-input     = 0.
          screen-output    = 0.
          screen-invisible = 1.
          MODIFY SCREEN.
        ENDIF.
      ENDLOOP.
    ELSEIF rb_loc IS NOT INITIAL.
      LOOP AT SCREEN.
        IF screen-name CS 'P_PATHS'.
          screen-input     = 0.
          screen-output    = 0.
          screen-invisible = 1.
          MODIFY SCREEN.
        ENDIF.
      ENDLOOP.
    ENDIF.
  ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  F_HELP_CUSTOM
*&---------------------------------------------------------------------*
*       Seleziona il path in base al salvataggio locale o su server
*----------------------------------------------------------------------*
*      <--P_P_PATHL  text
*----------------------------------------------------------------------*
  FORM f_help_custom  CHANGING cv_file TYPE string.

    IF rb_loc IS NOT INITIAL.
      CALL METHOD cl_gui_frontend_services=>directory_browse
        CHANGING
          selected_folder      = cv_file
        EXCEPTIONS
          cntl_error           = 1
          error_no_gui         = 2
          not_supported_by_gui = 3
          OTHERS               = 4.
    ELSE.
      CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
        EXPORTING
          directory        = ' '
          filemask         = 'X'
        IMPORTING
          serverfile       = cv_file
        EXCEPTIONS
          canceled_by_user = 1
          OTHERS           = 2.
    ENDIF.

  ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  F_CHECK
*&---------------------------------------------------------------------*
*       Controlla se i campi dei path sono inizializzati
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
  FORM f_check .
    IF rb_loc IS NOT INITIAL.
      IF p_pathl IS INITIAL.
        gv_error = 'X'.
        MESSAGE s398(00) WITH text-e01 DISPLAY LIKE 'E'.
        EXIT.
      ENDIF.
    ELSEIF rb_serv IS NOT INITIAL.
      IF p_paths IS INITIAL.
        gv_error = 'X'.
        MESSAGE s398(00) WITH text-e01 DISPLAY LIKE 'E'.
        EXIT.
      ENDIF.
    ENDIF.
  ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  CONVERTI_IN_XLSX
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_PATHL  text
*----------------------------------------------------------------------*
  FORM converti_in_xlsx  USING    lv_excel_data.
    TRY.
        DATA: lo_excel        TYPE REF TO zcl_excel,
              lo_xl_converter TYPE REF TO zcl_excel_converter.

        lo_xl_converter = NEW zcl_excel_converter( ).

        lo_xl_converter->convert( EXPORTING
                                    it_table = st_mseg[]
                                  CHANGING
                                    co_excel = lo_excel ).
        DATA(lo_excel_ws) = lo_excel->get_active_worksheet( ).
        lo_excel_ws->freeze_panes( ip_num_rows = 1 ).

        DATA(lo_excel_writer) = CAST zif_excel_writer( NEW zcl_excel_writer_2007( ) ).
        lv_excel_data = lo_excel_writer->write_file( lo_excel ).

        DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_excel_data ).

*        PERFORM scarica_file_excel USING lv_fname lv_excel_data it_raw_data.

      CATCH cx_root INTO DATA(e_txt).
        WRITE: / e_txt->get_text( ).
    ENDTRY.
  ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SCARICA_FILE_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LV_FNAME  text
*      -->P_LV_EXCEL_DATA  text
*      -->P_IT_RAW_DATA  text
*----------------------------------------------------------------------*
  FORM scarica_file_excel  USING    p_lv_fname  TYPE string
                                    p_lv_excel_data TYPE xstring
                                    p_it_raw_data TYPE solix_tab.

    cl_gui_frontend_services=>gui_download( EXPORTING
                                              filename = p_lv_fname
                                              filetype = 'BIN'
                                              bin_filesize = xstrlen( p_lv_excel_data )
                                             CHANGING
                                               data_tab = p_it_raw_data ).

  ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  CARICA_SUL_SERVER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_PATHS  text
*----------------------------------------------------------------------*
  FORM carica_file_excel  USING   lv_fname TYPE string lv_excel_data TYPE xstring it_raw_data TYPE solix_tab.
    DATA: lv_fullpath TYPE string,
          lv_msg      TYPE string.

    lv_fullpath = lv_fname.

    "OPEN DATASET filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    OPEN DATASET lv_fullpath FOR OUTPUT IN BINARY MODE MESSAGE lv_msg.
    IF sy-subrc EQ 0.
      LOOP AT it_raw_data ASSIGNING FIELD-SYMBOL(<fs>).
        TRANSFER <fs> TO lv_fullpath.
      ENDLOOP.
      CLOSE DATASET lv_fullpath.
    ELSE.
      WRITE: / 'Error: ', lv_msg.
    ENDIF.
  ENDFORM.
Leave a Comment