antelio
8/7/2019 - 10:56 PM

ZMM006


*&---------------------------------------------------------------------&*
*&             Report ZMM_PO_MALOTE                                    &*
*&                                                                     &*
*----------------------------------------------------------------------&*

REPORT zmm_po_hist NO STANDARD PAGE HEADING MESSAGE-ID 00.

TYPE-POOLS: slis, icon.

TYPES : BEGIN OF ty_po,
  ebeln        TYPE ekpo-ebeln,
  ebelp        TYPE ekpo-ebelp,
  anexo_po     TYPE char4     ,
  bedat        TYPE ekko-bedat,
  mtart        TYPE ekpo-mtart,
  matkl        TYPE ekpo-matkl,
  matnr        TYPE ekpo-matnr,
  txz01        TYPE ekpo-txz01,
  lifnr        TYPE ekko-lifnr,
  name1        TYPE lfa1-name1,
  menge1       TYPE ekpo-menge,
  meins1       TYPE ekpo-meins,
  balqty       TYPE ekpo-menge,
  netpr        TYPE ekpo-netpr,
  peinh        TYPE ekpo-peinh,
  mblnr        TYPE mseg-mblnr,
  dt_migo      TYPE mkpf-bldat,
  dtc_migo     TYPE mkpf-cpudt,
  anexo_migo   TYPE char4     ,
  lblni        TYPE essr-lblni,
  dt_folha     TYPE essr-erdat,
  bldat_folha  TYPE essr-bldat,
  dif_dias     TYPE i         ,
  semaforo     TYPE icon_d    ,
  anexo_folha  TYPE char4     ,
  zeile        TYPE mseg-zeile,
  menge2       TYPE mseg-menge,
  meins2       TYPE mseg-meins,
  werks        TYPE mseg-werks,
  charg        TYPE mseg-charg,
  belnr        TYPE rbkp-belnr,
  anexo_miro   TYPE char4     ,
  bldat        TYPE rbkp-bldat,
  fi_belnr     TYPE bkpf-belnr,
  zterm        TYPE rbkp-zterm,
  zfbdt        TYPE rbkp-zfbdt,
  duedt        TYPE datum     ,
  split(4)     TYPE c         ,
  thick(10)    TYPE c         ,
  width(10)    TYPE c         ,
  length(10)   TYPE c         ,
  grade(10)    TYPE c         ,
  zterm_pedido TYPE ekko-zterm,
  zterm_fi     TYPE bseg-zterm,
  ktokk        TYPE lfa1-ktokk,
  bukrs        TYPE ekko-bukrs,
  mjahr        TYPE mkpf-mjahr,
  dt_doc_migo  TYPE mkpf-bldat,
END OF ty_po.

TYPES : BEGIN OF ty_ekko,
  ebeln TYPE ekko-ebeln,
  lifnr TYPE ekko-lifnr,
  bedat TYPE ekko-bedat,
  zterm TYPE ekko-zterm,
  bukrs TYPE ekko-bukrs,
END OF ty_ekko.

TYPES : BEGIN OF ty_ekpo,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  matnr TYPE ekpo-matnr,
  txz01 TYPE ekpo-txz01,
  menge TYPE ekpo-menge,
  meins TYPE ekpo-meins,
  netpr TYPE ekpo-netpr,
  peinh TYPE ekpo-peinh,
  banfn TYPE ekpo-banfn,
  bnfpo TYPE ekpo-bnfpo,
  mtart TYPE ekpo-mtart,
END OF ty_ekpo.

TYPES : BEGIN OF ty_eban,
  banfn TYPE eban-banfn,
  bnfpo TYPE eban-bnfpo,
  matnr TYPE eban-matnr,
  menge TYPE eban-menge,
  meins TYPE eban-meins,
END OF ty_eban.

TYPES : BEGIN OF ty_ekbe,
  ebeln TYPE ekbe-ebeln,
  ebelp TYPE ekbe-ebelp,
  belnr TYPE ekbe-belnr,
  gjahr TYPE ekbe-gjahr,
  buzei TYPE ekbe-buzei,
  matnr TYPE ekbe-matnr,
END OF ty_ekbe.

TYPES : BEGIN OF ty_mseg,
  mblnr TYPE mseg-mblnr,
  mjahr TYPE mseg-mjahr,
  zeile TYPE mseg-zeile,
  menge TYPE mseg-menge,
  meins TYPE mseg-meins,
  ebeln TYPE mseg-ebeln,
  ebelp TYPE mseg-ebelp,
  matnr TYPE mseg-matnr,
  werks TYPE mseg-werks,
  charg TYPE mseg-charg,
END OF ty_mseg.

TYPES : BEGIN OF ty_rbkp,
  belnr TYPE rbkp-belnr,
  gjahr TYPE rbkp-gjahr,
  bldat TYPE rbkp-bldat,
  stblg TYPE rbkp-stblg,
  zterm TYPE rbkp-zterm,
  zfbdt TYPE rbkp-zfbdt,
  budat TYPE rbkp-budat,
  cpudt TYPE rbkp-cpudt,
END OF ty_rbkp.

TYPES : BEGIN OF ty_rseg,
  belnr TYPE rseg-belnr,
  gjahr TYPE rseg-gjahr,
  ebeln TYPE rseg-ebeln,
  ebelp TYPE rseg-ebelp,
  xblnr TYPE rseg-xblnr,
  lfbnr TYPE rseg-lfbnr,
  lfgja TYPE rseg-lfgja,
  lfpos TYPE rseg-lfpos,
END OF ty_rseg.

TYPES : BEGIN OF ty_lfa1,
  lifnr TYPE lfa1-lifnr,
  ktokk TYPE lfa1-ktokk,
END OF ty_lfa1.

DATA: it_po TYPE STANDARD TABLE OF ty_po,
      it_ekko TYPE STANDARD TABLE OF ty_ekko,
      it_ekpo TYPE TABLE OF ekpo,
      it_eban TYPE STANDARD TABLE OF ty_eban,
      it_mseg TYPE STANDARD TABLE OF ty_mseg,
      it_rbkp TYPE STANDARD TABLE OF ty_rbkp,
      it_rseg TYPE STANDARD TABLE OF ty_rseg,
      it_ekbe TYPE STANDARD TABLE OF ekbe,
      it_essr TYPE TABLE OF essr,
      it_ekbe_migo TYPE TABLE OF ekbe,
      it_mkpf TYPE TABLE OF mkpf,
      it_srgbtbrel TYPE TABLE OF srgbtbrel,
      it_lfa1 TYPE TABLE OF ty_lfa1.

DATA: wa_ekbe_migo TYPE ekbe,
      wa_ekbe_migo2 TYPE ekbe,
      wa_srgbtbrel TYPE srgbtbrel,
      wa_ekbe TYPE ekbe,
      wa_mkpf TYPE mkpf,
      wa_essr TYPE essr,
      wa_po TYPE ty_po,
      wa_ekko TYPE ty_ekko,
      wa_ekpo TYPE ekpo,
      wa_eban TYPE ty_eban,
      wa_mseg TYPE ty_mseg,
      wa_rbkp TYPE ty_rbkp,
      wa_rseg TYPE ty_rseg,
      wa_lfa1 TYPE ty_lfa1,
      wa_t052 TYPE t052.

DATA: it_fcat TYPE slis_t_fieldcat_alv,
      it_lshead TYPE slis_t_listheader,
      it_sort TYPE slis_t_sortinfo_alv,
      wa_fcat TYPE slis_fieldcat_alv,
      wa_lshead TYPE slis_listheader,
      wa_layout TYPE slis_layout_alv,
      wa_sort TYPE slis_sortinfo_alv.

DATA: it_cl_data LIKE TABLE OF clobjdat,
      wa_cl_data LIKE clobjdat.

DATA: c_ekgrp TYPE eban-ekgrp,
      c_badat TYPE eban-badat,
      c_bsart TYPE ekko-bsart,
      c_bukrs TYPE ekko-bukrs,
      c_ebeln TYPE ekko-ebeln,
      c_ktokk TYPE lfa1-ktokk,
      c_grmenge TYPE mseg-erfmg,
      c_low(10) TYPE c,
      c_high(10) TYPE c,
      c_date TYPE string.

DATA: lw_object TYPE sibflporb.


DATA: BEGIN OF t_ebeln_ebelp OCCURS 0,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
END OF t_ebeln_ebelp.



DATA: w_t001w TYPE t001w,
      w_t001k TYPE t001k,
      t_bseg TYPE TABLE OF bseg,
      w_bseg TYPE bseg.


SELECTION-SCREEN:BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS:
s_badat FOR c_badat OBLIGATORY,
s_bukrs FOR c_bukrs,
s_ekgrp FOR c_ekgrp,
s_bsart FOR c_bsart,
s_ebeln FOR c_ebeln,
s_ktokk FOR c_ktokk NO INTERVALS.
SELECTION-SCREEN:END OF BLOCK b1.

INITIALIZATION.

  DATA wa_indx TYPE indx.
  IF s_badat[] IS INITIAL.
    IMPORT s_badat[] FROM DATABASE indx(xy) TO wa_indx CLIENT sy-mandt ID 'ZMM006_DATE'.
  ENDIF.

AT SELECTION-SCREEN.

  DATA wa_indx TYPE indx.
  IF s_badat[] IS NOT INITIAL.
    EXPORT s_badat[] TO DATABASE indx(xy) FROM wa_indx CLIENT sy-mandt ID 'ZMM006_DATE'.
  ENDIF.

START-OF-SELECTION.

  PERFORM getdocuments.
  PERFORM getdata.
  PERFORM process_clear_mseg.
  PERFORM setdata.
  PERFORM set_vendor_name.
  PERFORM set_duedt_migo.
  PERFORM fieldcat.
  PERFORM display.

*&---------------------------------------------------------------------*
*&      Form  GETDOCUMENTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM getdocuments .

*  SELECT p~ebeln p~ebelp FROM ekpo AS p
*    INNER JOIN ekko AS k
*      ON k~mandt = p~mandt
*      AND k~ebeln = p~ebeln
*    INNER JOIN lfa1 AS l
*      ON l~lifnr EQ k~lifnr
*    INTO TABLE t_ebeln_ebelp
*    WHERE ( k~aedat IN s_badat
*            OR k~bedat IN s_badat )
*      AND p~ebeln IN s_ebeln
*      AND p~loekz <> 'L'
*      AND k~bukrs IN s_bukrs
*      AND k~ekgrp IN s_ekgrp
*      AND k~bsart IN s_bsart
*    AND l~ktokk IN s_ktokk.
*
*  SELECT p~ebeln p~ebelp
*    FROM ekpo AS p
*    INNER JOIN ekko AS k
*      ON k~mandt = p~mandt
*        AND k~ebeln = p~ebeln
*    INNER JOIN lfa1 AS l
*      ON l~lifnr EQ k~lifnr
*    APPENDING TABLE t_ebeln_ebelp
*    WHERE p~aedat IN s_badat
*      AND p~ebeln IN s_ebeln
*      AND k~bsart IN s_bsart
*      AND k~bukrs IN s_bukrs
*      AND k~ekgrp IN s_ekgrp
*      AND l~ktokk IN s_ktokk
*      AND p~loekz <> 'L'.
*
*  SELECT p~ebeln p~ebelp
*  FROM eket AS t
*  INNER JOIN ekpo AS p
*  ON p~mandt = t~mandt
*  AND p~ebeln = t~ebeln
*  AND p~ebelp = t~ebelp
*  INNER JOIN ekko AS k
*  ON k~mandt = p~mandt
*  AND k~ebeln = p~ebeln
*  INNER JOIN lfa1 AS l
*  ON l~lifnr EQ k~lifnr
*  APPENDING TABLE t_ebeln_ebelp
*  WHERE t~eindt IN s_badat
*  AND p~ebeln IN s_ebeln
*  AND k~bukrs IN s_bukrs
*  AND k~ekgrp IN s_ekgrp
*  AND k~bsart IN s_bsart
*  AND l~ktokk IN s_ktokk
*  AND p~loekz <> 'L'.

  SELECT p~ebeln p~ebelp
    FROM ekbe AS b
    INNER JOIN ekpo AS p
      ON p~mandt = b~mandt
        AND p~ebeln = b~ebeln
        AND p~ebelp = b~ebelp
    INNER JOIN ekko AS k
      ON k~mandt = p~mandt
        AND k~ebeln = p~ebeln
    INNER JOIN lfa1 AS l
      ON l~lifnr EQ k~lifnr
*      APPENDING TABLE t_ebeln_ebelp
    INTO TABLE t_ebeln_ebelp
    WHERE ( b~budat IN s_badat
            OR b~bldat IN s_badat )
    AND p~ebeln IN s_ebeln
    AND k~bsart IN s_bsart
    AND k~bukrs IN s_bukrs
    AND k~ekgrp IN s_ekgrp
    AND l~ktokk IN s_ktokk
    AND p~loekz <> 'L'.

  SORT t_ebeln_ebelp BY ebeln ebelp.
  DELETE ADJACENT DUPLICATES FROM t_ebeln_ebelp COMPARING ALL FIELDS.

ENDFORM.                    " GETDOCUMENTS
*&---------------------------------------------------------------------*
*&      Form  GETDATA
*&---------------------------------------------------------------------*
FORM getdata.
  CHECK t_ebeln_ebelp[] IS NOT INITIAL.
**--> Seleciona os Itens dos Pedidos de Compras
  SELECT *
  FROM ekpo INTO TABLE it_ekpo
  FOR ALL ENTRIES IN t_ebeln_ebelp
  WHERE ebeln = t_ebeln_ebelp-ebeln
  AND ebelp = t_ebeln_ebelp-ebelp.

  IF it_ekpo[] IS NOT INITIAL.
    SELECT ebeln lifnr bedat zterm bukrs
      FROM ekko INTO TABLE it_ekko
      FOR ALL ENTRIES IN it_ekpo
      WHERE ebeln = it_ekpo-ebeln.
    SELECT *
      FROM ekbe INTO TABLE it_ekbe
      FOR ALL ENTRIES IN it_ekpo
      WHERE ebeln = it_ekpo-ebeln
        AND ebelp = it_ekpo-ebelp.
    SELECT belnr gjahr ebeln ebelp xblnr lfbnr lfgja lfpos
      FROM rseg
      INTO TABLE it_rseg
      FOR ALL ENTRIES IN it_ekpo
      WHERE ebeln = it_ekpo-ebeln
        AND ebelp = it_ekpo-ebelp.
  ENDIF.

  IF it_ekbe[] IS NOT INITIAL.
    DATA: it_ekbe_essr LIKE it_ekbe.

    it_ekbe_essr[] = it_ekbe[].
    DELETE it_ekbe_essr WHERE vgabe <> '9'.
    SELECT *
      FROM essr
      INTO TABLE it_essr
      FOR ALL ENTRIES IN it_ekbe_essr
      WHERE lblni = it_ekbe_essr-belnr.
    SELECT mblnr mjahr zeile menge meins ebeln
           ebelp matnr werks charg
      FROM mseg
      INTO TABLE it_mseg
      FOR ALL ENTRIES IN it_ekbe
      WHERE mblnr = it_ekbe-belnr
        AND mjahr = it_ekbe-gjahr
        AND zeile = it_ekbe-buzei.
    SELECT *
      FROM mkpf
      INTO TABLE it_mkpf
      FOR ALL ENTRIES IN it_mseg
      WHERE mblnr = it_mseg-mblnr
        AND mjahr = it_mseg-mjahr.

    FREE it_ekbe_essr.
  ENDIF.


  IF it_rseg[] IS NOT INITIAL.
    SELECT belnr gjahr budat stblg zterm zfbdt budat cpudt
      FROM rbkp
      INTO TABLE it_rbkp
      FOR ALL ENTRIES IN it_rseg
      WHERE belnr = it_rseg-belnr
        AND gjahr = it_rseg-gjahr.
  ENDIF.

  IF it_ekko[] IS NOT INITIAL.
    SELECT lifnr ktokk
      FROM lfa1 INTO TABLE it_lfa1
      FOR ALL ENTRIES IN it_ekko
      WHERE lifnr EQ it_ekko-lifnr.
    SORT it_lfa1 BY lifnr.
  ENDIF.

**--> Seleciona os Anexos
  SELECT * FROM srgbtbrel
    INTO TABLE it_srgbtbrel.
**--> Move o histórico das MIGOS para tabela auxiliar.
  MOVE it_ekbe[] TO it_ekbe_migo[].
  DELETE it_ekbe_migo[] WHERE bewtp <> 'E'.

**--> Trata estornos da MIRO
  DATA: vl_tabix TYPE sy-tabix.

  DELETE it_rbkp[] WHERE stblg IS NOT INITIAL.
  LOOP AT it_rseg INTO wa_rseg.
    vl_tabix = sy-tabix.
    READ TABLE it_rbkp WITH KEY belnr = wa_rseg-belnr
    gjahr = wa_rseg-gjahr
    TRANSPORTING NO FIELDS.
    IF sy-subrc <> 0.
      CLEAR wa_rseg.
      MODIFY it_rseg FROM wa_rseg INDEX vl_tabix.
    ENDIF.
    CLEAR: wa_rseg.
  ENDLOOP.

  DELETE it_rseg[] WHERE belnr IS INITIAL.

ENDFORM.                    " GETDATA
*&---------------------------------------------------------------------*
*&      Form  select_tvarv
*&---------------------------------------------------------------------*
FORM select_tvarv
  USING pv_name TYPE rvari_vnam
  CHANGING ct_table TYPE STANDARD TABLE.

  TYPES:
  BEGIN OF ts_tvarvc_entry,
    sign   TYPE c LENGTH 01,
    option TYPE c LENGTH 02,
    low    TYPE tvarv_val  ,
    high   TYPE tvarv_val  ,
  END   OF ts_tvarvc_entry,
  tt_tvarvc_entry TYPE STANDARD TABLE OF ts_tvarvc_entry
  WITH DEFAULT KEY.
  DATA: lt_tvarvc TYPE tt_tvarvc_entry.

* Declaração dos ponteiros locais
  FIELD-SYMBOLS:
  <fs_tvarvc> LIKE LINE OF lt_tvarvc,
  <fs_table>  TYPE ANY.

  SELECT sign opti AS option low high
    FROM tvarvc
    INTO TABLE lt_tvarvc
    WHERE name EQ pv_name.
  LOOP AT lt_tvarvc ASSIGNING <fs_tvarvc>.
    INSERT INITIAL LINE INTO TABLE ct_table ASSIGNING <fs_table>.
    MOVE-CORRESPONDING <fs_tvarvc> TO <fs_table>.
  ENDLOOP.
ENDFORM.                    "select_tvarv
*&---------------------------------------------------------------------*
*&      Form  SETDATA
*&---------------------------------------------------------------------*
FORM setdata.

  DATA: w_t052 TYPE t052,
        w_bkpf TYPE bkpf,
        v_awkey TYPE bkpf-awkey.

* Declaração dos ranges locais
  DATA: lt_entry_sheet_green  TYPE RANGE OF i,
        lt_entry_sheet_yellow TYPE RANGE OF i,
        lt_entry_sheet_red    TYPE RANGE OF i.

**--> Monta dados do ALV.
  DATA: vl_instid TYPE srgbtbrel-instid_a.

  CLEAR wa_po.

**--> Parametrizações de Semáforo
  PERFORM select_tvarv:
  USING 'ENTRY_SHEET_GREEN'  CHANGING lt_entry_sheet_green,
  USING 'ENTRY_SHEET_YELLOW' CHANGING lt_entry_sheet_yellow,
  USING 'ENTRY_SHEET_RED'    CHANGING lt_entry_sheet_red   .

  DELETE it_ekbe[] WHERE bewtp = 'Q'.

  SORT it_ekbe_migo BY ebeln ebelp lfbnr bwart.

***--> Itens do Histórico de Pedido por Folha de Serviço.
  LOOP AT it_ekbe INTO wa_ekbe.
    READ TABLE it_ekpo INTO wa_ekpo
    WITH KEY ebeln = wa_ekbe-ebeln
    ebelp = wa_ekbe-ebelp.
    CHECK sy-subrc IS INITIAL.
***---> Move dados do Pedido de Compras.
    MOVE-CORRESPONDING wa_ekpo TO wa_po.
    MOVE:
    wa_ekpo-menge TO wa_po-menge1,
    wa_ekpo-meins TO wa_po-meins1.

***--> Quebra o relatório em Folha de Serviços.
    READ TABLE it_essr INTO wa_essr
    WITH KEY lblni = wa_ekbe-lfbnr.

    IF sy-subrc = 0.
*--> Descarta as Folhas de Serviço Estornadas.
      CHECK wa_essr-loekz <> 'X'.

***--> Não quebra o relatório por MIGO.
      CHECK wa_ekbe-bewtp = 'D'.

**--> Move os dados da Folha de Serviços.
      MOVE:
      wa_essr-lblni TO wa_po-lblni,
      wa_essr-erdat TO wa_po-dt_folha,
      wa_essr-bldat TO wa_po-bldat_folha.
      IF wa_essr-kzabn IS NOT INITIAL.
        wa_po-semaforo = icon_green_light.
      ELSE.
        IF wa_po-dt_folha GT '00010101'.
          wa_po-dif_dias = sy-datum - wa_po-dt_folha.
          IF wa_po-dif_dias   IN lt_entry_sheet_green .
            wa_po-semaforo = icon_green_light.
          ENDIF.
          IF wa_po-dif_dias   IN lt_entry_sheet_yellow.
            wa_po-semaforo = icon_yellow_light.
          ENDIF.
          IF wa_po-dif_dias   IN lt_entry_sheet_red   .
            wa_po-semaforo = icon_red_light.
          ENDIF.
        ENDIF.
      ENDIF.
**--> Move anexo Anexo Folha de Serviços.
      READ TABLE it_srgbtbrel INTO wa_srgbtbrel
      WITH KEY instid_a = wa_essr-lblni.
      IF sy-subrc = 0.
        MOVE icon_attachment TO wa_po-anexo_folha.
      ENDIF.
    ENDIF.

*** Move anexo do Pedido de Compras.
    READ TABLE it_srgbtbrel INTO wa_srgbtbrel
    WITH KEY instid_a = wa_po-ebeln.
    IF sy-subrc = 0.
      MOVE icon_attachment TO wa_po-anexo_po.
    ENDIF.

**--> Encontra respectiva MIGO da Folha.
    CLEAR wa_mseg.
    IF wa_ekbe-lfbnr IS NOT INITIAL.
      READ TABLE it_ekbe_migo INTO wa_ekbe_migo
      WITH KEY lfbnr = wa_ekbe-lfbnr
      bewtp = 'E'.
    ELSE.
      READ TABLE it_ekbe_migo INTO wa_ekbe_migo
      WITH KEY belnr = wa_ekbe-ebeln
      gjahr = wa_ekbe-gjahr
      buzei = wa_ekbe-buzei.

    ENDIF.

    IF sy-subrc = 0.
      LOOP AT it_mseg INTO wa_mseg
      WHERE mblnr = wa_ekbe_migo-belnr.
        c_grmenge = c_grmenge + wa_mseg-menge.
      ENDLOOP.
    ENDIF.

**--> Encontra respectiva MIGO da Folha.
    CLEAR wa_mseg.
    IF wa_ekbe-lfbnr IS NOT INITIAL.
      READ TABLE it_ekbe_migo
      INTO wa_ekbe_migo
      WITH KEY lfbnr = wa_ekbe-lfbnr
      bewtp = 'E'.
      IF sy-subrc IS INITIAL.
        READ TABLE it_mseg
        INTO wa_mseg
        WITH KEY mblnr = wa_ekbe_migo-belnr
        mjahr = wa_ekbe_migo-gjahr
        zeile = wa_ekbe_migo-buzei.
        IF sy-subrc IS NOT INITIAL.
          CLEAR wa_mseg.
        ENDIF.
      ENDIF.

    ELSE.
      READ TABLE it_mseg
      INTO wa_mseg
      WITH KEY mblnr = wa_ekbe-belnr
      mjahr = wa_ekbe-gjahr
      zeile = wa_ekbe-buzei.
      IF sy-subrc IS NOT INITIAL.
        CLEAR wa_mseg.
      ENDIF.

    ENDIF.
    IF sy-subrc IS INITIAL.
      MOVE:
      wa_mseg-mblnr TO wa_po-mblnr,
      wa_mseg-mjahr TO wa_po-mjahr,
      wa_ekbe-menge TO wa_po-menge2,
      wa_mseg-meins TO wa_po-meins2,
      wa_mseg-werks TO wa_po-werks,
      wa_mseg-charg TO wa_po-charg.

      CONCATENATE wa_mseg-mblnr wa_mseg-mjahr INTO vl_instid.
      READ TABLE it_srgbtbrel INTO wa_srgbtbrel
      WITH KEY instid_a = vl_instid.
      IF sy-subrc = 0.
        MOVE icon_attachment TO wa_po-anexo_migo.
      ENDIF.
      CLEAR: vl_instid.

      READ TABLE it_mkpf INTO wa_mkpf
      WITH KEY mblnr = wa_mseg-mblnr
      mjahr = wa_mseg-mjahr.
      IF sy-subrc = 0.
        wa_po-dt_migo = wa_mkpf-budat.
        wa_po-dt_doc_migo = wa_mkpf-bldat.
        wa_po-dtc_migo = wa_mkpf-cpudt.
      ENDIF.
    ENDIF.

    wa_po-balqty = wa_eban-menge - c_grmenge.

    CLEAR : wa_rseg, wa_rbkp.
**--> Move dados da MIRO

    CLEAR wa_mseg.
    IF wa_ekbe-lfbnr IS NOT INITIAL.
      READ TABLE it_ekbe_migo
      INTO wa_ekbe_migo
      WITH KEY lfbnr = wa_ekbe-lfbnr
      bewtp = 'E'.
      IF sy-subrc IS INITIAL.
        READ TABLE it_mseg
        INTO wa_mseg
        WITH KEY mblnr = wa_ekbe_migo-belnr
        mjahr = wa_ekbe_migo-gjahr
        zeile = wa_ekbe_migo-buzei.
        IF sy-subrc IS NOT INITIAL.
          CLEAR wa_mseg.
        ENDIF.
      ENDIF.

    ELSE.
      READ TABLE it_mseg
      INTO wa_mseg
      WITH KEY mblnr = wa_ekbe-belnr
      mjahr = wa_ekbe-gjahr
      zeile = wa_ekbe-buzei.
      IF sy-subrc IS NOT INITIAL.
        CLEAR wa_mseg.
      ENDIF.
    ENDIF.

    IF wa_ekbe-lfbnr IS NOT INITIAL.
      READ TABLE it_rseg
        INTO wa_rseg
        WITH KEY lfbnr = wa_ekbe-lfbnr
                 lfgja = wa_ekbe-lfgja
                 lfpos = wa_ekbe-lfpos.
    ELSE.
      READ TABLE it_rseg
        INTO wa_rseg
        WITH KEY ebeln = wa_mseg-ebeln
                 ebelp = wa_mseg-ebelp.
    ENDIF.
    IF sy-subrc = 0.
      READ TABLE it_rbkp INTO wa_rbkp
        WITH KEY belnr = wa_rseg-belnr
        gjahr = wa_rseg-gjahr.
      IF sy-subrc EQ 0.
        IF wa_rbkp-stblg IS INITIAL.
          MOVE:
            wa_rbkp-belnr TO wa_po-belnr,
            wa_rbkp-zterm TO wa_po-zterm,
            wa_rbkp-zfbdt TO wa_po-zfbdt,
            wa_rbkp-bldat TO wa_po-bldat.

          CALL FUNCTION 'FI_FIND_PAYMENT_CONDITIONS'
            EXPORTING
              i_zterm = wa_rbkp-zterm
              i_bldat = wa_rbkp-bldat
              i_budat = wa_rbkp-budat
              i_cpudt = wa_rbkp-cpudt
              i_zfbdt = wa_rbkp-zfbdt
            IMPORTING
              e_t052  = w_t052.

          CONCATENATE wa_rbkp-belnr wa_rbkp-bldat(4)
            INTO v_awkey .

          SELECT SINGLE *
            FROM bkpf
            INTO w_bkpf
            WHERE awkey =  v_awkey.
          IF sy-subrc IS INITIAL.
            wa_po-fi_belnr = w_bkpf-belnr.
          ENDIF.

          IF w_t052-xsplt = 'X'.
            wa_po-split = 'PARC'.
          ENDIF.

          SELECT SINGLE * FROM t001w INTO w_t001w WHERE werks = wa_po-werks.
          SELECT SINGLE * FROM t001k INTO w_t001k WHERE bwkey = w_t001w-bwkey.

          CALL FUNCTION 'FI_DOCUMENT_READ'
            EXPORTING
              i_bukrs     = w_t001k-bukrs
              i_belnr     = wa_po-fi_belnr
              i_gjahr     = wa_rbkp-bldat(4)
            TABLES
              t_bseg      = t_bseg
            EXCEPTIONS
              wrong_input = 1
              not_found   = 2
              OTHERS      = 3.
          IF sy-subrc IS INITIAL.
            DELETE t_bseg WHERE koart <> 'K'.
            SORT t_bseg BY fdtag.
            READ TABLE t_bseg INDEX 1 INTO w_bseg.
            wa_po-duedt    = w_bseg-fdtag.
            wa_po-zterm_fi = w_bseg-zterm.
          ENDIF.

*** Move anexo da MIRO
          CONCATENATE wa_rbkp-belnr wa_rbkp-bldat(4) INTO vl_instid.
          READ TABLE it_srgbtbrel INTO wa_srgbtbrel
            WITH KEY instid_a = vl_instid.

          IF sy-subrc = 0.
            MOVE icon_attachment TO wa_po-anexo_miro.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDIF.

    CLEAR vl_instid.

**--> Seleciona os dados de cabeçalho do Pedido de Compras
    CLEAR wa_ekko.

    READ TABLE it_ekko INTO wa_ekko WITH KEY ebeln = wa_ekbe-ebeln.
    IF sy-subrc EQ 0.
      MOVE: wa_ekko-lifnr TO wa_po-lifnr,
      wa_ekko-bedat TO wa_po-bedat,
      wa_ekko-zterm TO wa_po-zterm_pedido,
      wa_ekko-bukrs TO wa_po-bukrs.

      CLEAR wa_lfa1.
      READ TABLE it_lfa1 INTO wa_lfa1 WITH KEY lifnr = wa_ekko-lifnr BINARY SEARCH.
      IF sy-subrc IS INITIAL.
        MOVE wa_lfa1-ktokk TO wa_po-ktokk.
      ENDIF.
    ENDIF.

    IF   wa_po-lblni        IS NOT INITIAL
    AND wa_po-mblnr        IS INITIAL
    AND wa_po-zterm_pedido IS NOT INITIAL.

      CLEAR wa_t052.
      CALL FUNCTION 'FI_TERMS_OF_PAYMENT_CHECK'
        EXPORTING
          i_zterm          = wa_po-zterm_pedido
        IMPORTING
          e_t052           = wa_t052
        EXCEPTIONS
          terms_of_payment = 1
          conditions       = 2
          OTHERS           = 3.
      IF sy-subrc IS INITIAL.
        wa_po-duedt = wa_po-bldat_folha + wa_t052-ztag1.
      ENDIF.
    ENDIF.

    APPEND wa_po TO it_po.
    CLEAR: wa_po, wa_ekpo, wa_mseg.

  ENDLOOP.

ENDFORM.                    " SETDATA
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fieldcat .

  DATA: i TYPE i.

  REFRESH it_fcat.

  i = 1.     PERFORM buildfields USING i 'EBELN'    'IT_PO' 'Pedido' '' 10.
  i = i + 1. PERFORM buildfields USING i 'EBELP'    'IT_PO' 'Item' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ANEXO_PO' 'IT_PO' 'Anexo-PO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'BEDAT'    'IT_PO' 'Data do Pedido' '' 0.
  i = i + 1. PERFORM buildfields USING i 'LBLNI'    'IT_PO' 'Folha de Serviços' '' 0.
  i = i + 1. PERFORM buildfields USING i 'DT_FOLHA' 'IT_PO' 'Data Cria. de Serviços' '' 0.
  i = i + 1. PERFORM buildfields USING i 'BLDAT_FOLHA' 'IT_PO' 'DataDoc Folha' '' 0.
  i = i + 1. PERFORM buildfields USING i 'DIF_DIAS' 'IT_PO' 'Diferença de Dias'      '' 0.
  i = i + 1. PERFORM buildfields USING i 'SEMAFORO' 'IT_PO' 'Status'                 '' 0.
  i = i + 1. PERFORM buildfields USING i 'MTART'    'IT_PO' 'Tipo do Material' '' 0.
  i = i + 1. PERFORM buildfields USING i 'MATKL'    'IT_PO' 'Grupo de Mercadorias' '' 0.
  i = i + 1. PERFORM buildfields USING i 'MATNR'    'IT_PO' 'Número do Material' '' 0.
  i = i + 1. PERFORM buildfields USING i 'TXZ01'    'IT_PO' 'Descrição do Material' '' 0.
  i = i + 1. PERFORM buildfields USING i 'LIFNR'    'IT_PO' 'Número do Fornecedor' '' 0.
  i = i + 1. PERFORM buildfields USING i 'NAME1'    'IT_PO' 'Fornecedor' '' 0.
  i = i + 1. PERFORM buildfields USING i 'MENGE1'   'IT_PO' 'Quantidade do Pedido' '' 0.
  i = i + 1. PERFORM buildfields USING i 'MEINS'    'IT_PO' 'Unidade do Pedido' '' 0.
  i = i + 1. PERFORM buildfields USING i 'NETPR'    'IT_PO' 'Preço' '' 0.
  i = i + 1. PERFORM buildfields USING i 'PEINH'    'IT_PO' 'Unidade de Preço' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ANEXO_FOLHA' 'IT_PO' 'Anexo-Folha' '' 0.
  i = i + 1. PERFORM buildfields USING i 'MBLNR'    'IT_PO' 'MIGO' ''  0.
  i = i + 1. PERFORM buildfields USING i 'DT_MIGO'  'IT_PO' 'DtL MIGO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'DTC_MIGO'  'IT_PO' 'DtC MIGO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ANEXO_MIGO' 'IT_PO' 'Anexo-MIGO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'MENGE2'     'IT_PO' 'Quantidade MIGO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'MEINS2'     'IT_PO' 'Unidade MIGO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'BELNR'      'IT_PO' 'MIRO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ANEXO_MIRO' 'IT_PO' 'Anexo-MIRO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'BLDAT' 'IT_PO' 'DtL MIRO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'FI_BELNR' 'IT_PO' 'FI Doc' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ZTERM' 'IT_PO' 'C.Pag.MIRO' '' 0.
  i = i + 1. PERFORM buildfields USING i 'SPLIT' 'IT_PO' 'Parcel.' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ZFBDT' 'IT_PO' 'Dt.Base' '' 0.
  i = i + 1. PERFORM buildfields USING i 'DUEDT' 'IT_PO' 'Dt.Pago' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ZTERM_PEDIDO' 'IT_PO' 'C.Pag.Pedido' '' 0.
  i = i + 1. PERFORM buildfields USING i 'ZTERM_FI' 'IT_PO' 'C.Pag.Contábil' '' 0.
  i = i + 1. PERFORM buildfields USING i 'KTOKK' 'IT_PO' 'Grp.Contas Fornecedor' '' 0.
  i = i + 1. PERFORM buildfields USING i 'BUKRS' 'IT_PO' 'Empresa' '' 0.

ENDFORM.                    " FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  BUILDFIELDS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0449   text
*      -->P_0450   text
*      -->P_0451   text
*      -->P_0452   text
*----------------------------------------------------------------------*
FORM buildfields  USING    value(p_col_pos) LIKE sy-cucol
      value(p_fldname) TYPE slis_fieldname
      value(p_tabname) TYPE slis_tabname
      value(p_reptext) LIKE dd03p-reptext
      value(p_do_sum) TYPE char1
      value(p_len) TYPE i.

  CLEAR wa_fcat.
  wa_fcat-col_pos = p_col_pos.
  wa_fcat-fieldname = p_fldname.
  wa_fcat-tabname = p_tabname.
  wa_fcat-reptext_ddic = p_reptext.
  wa_fcat-do_sum = p_do_sum.
  wa_fcat-just = 'C'.
  IF p_len IS NOT INITIAL.
    wa_fcat-intlen = p_len.
  ENDIF.
  APPEND wa_fcat TO it_fcat.

ENDFORM.                    " BUILDFIELDS
*&---------------------------------------------------------------------*
*&      Form  DISPLAY
*&---------------------------------------------------------------------*
FORM display.

  CLEAR wa_layout.
  wa_layout-zebra = 'X'.
  wa_layout-colwidth_optimize = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_bypassing_buffer       = 'X'
      i_callback_program       = sy-cprog
      i_callback_user_command  = 'USER_COMMAND'
      i_callback_top_of_page   = 'TOP_OF_PAGE'
      i_background_id          = 'ALV_BACKGROUND'
      is_layout                = wa_layout
      it_fieldcat              = it_fcat
      i_callback_pf_status_set = 'ZSTATUS'
      i_save                   = 'A'
    TABLES
      t_outtab                 = it_po[]
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    " DISPLAY

*&---------------------------------------------------------------------*
*&      Form  zstatus
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->EXTAB      text
*----------------------------------------------------------------------*
FORM zstatus USING extab TYPE slis_t_extab.                 "#EC CALLED
  SET PF-STATUS 'ZSTATUS'.
  REFRESH extab[].
ENDFORM.                    " zstatus

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top_of_page.                                           "#EC *

  REFRESH it_lshead.
  CLEAR wa_lshead.

  WRITE: s_badat-low TO c_low DD/MM/YYYY,s_badat-high TO c_high DD/MM/YYYY.
  IF s_badat-high IS INITIAL.
    CONCATENATE 'De' c_low INTO c_date SEPARATED BY space.
  ELSE.
    CONCATENATE 'Datado de' c_low 'e' c_high INTO c_date SEPARATED BY space.
  ENDIF.

  wa_lshead-typ = 'H'.
  wa_lshead-info = 'Rastreabilidade de Pedidos'.
  APPEND wa_lshead TO it_lshead.

  wa_lshead-typ = 'S'.
  wa_lshead-info = c_date.
  APPEND wa_lshead TO it_lshead.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = it_lshead.

ENDFORM.                    "top_of_page
*&---------------------------------------------------------------------*
*&      Form  BUILD_SORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_1072   text
*      -->P_1073   text
*      -->P_1074   text
*----------------------------------------------------------------------*
FORM build_sort  USING    value(p_fldname) TYPE slis_fieldname
      value(p_spos) LIKE alvdynp-sortpos
      value(p_up) LIKE alvdynp-sortup.

  wa_sort-fieldname = p_fldname.
  wa_sort-spos = p_spos.
  wa_sort-up = p_up.
  APPEND wa_sort TO it_sort.
  CLEAR wa_sort.

ENDFORM.                    " BUILD_SORT
*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->UCOMM      text
*      -->SELFIELD   text
*----------------------------------------------------------------------*
FORM user_command USING ucomm    LIKE sy-ucomm
      selfield TYPE slis_selfield.

  DATA: gd_repid LIKE sy-repid,
        ref_grid TYPE REF TO cl_gui_alv_grid,
        vl_subrc TYPE sy-subrc.

  IF ref_grid IS INITIAL.
    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
      IMPORTING
        e_grid = ref_grid.
  ENDIF.

  IF NOT ref_grid IS INITIAL.
    CALL METHOD ref_grid->check_changed_data .
  ENDIF.

  CASE ucomm.
    WHEN '&IC1'.
      CASE selfield-sel_tab_field.
        WHEN 'IT_PO-EBELN'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          SET PARAMETER ID : 'BES' FIELD wa_po-ebeln.
          CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.
        WHEN 'IT_PO-LBLNI'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          SET PARAMETER ID : 'LBL' FIELD wa_po-lblni.
          CALL TRANSACTION 'ML81N' AND SKIP FIRST SCREEN.
        WHEN 'IT_PO-MBLNR'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          CALL FUNCTION 'MIGO_DIALOG'
            EXPORTING
              i_action            = 'A04'
              i_refdoc            = 'R02'
              i_notree            = 'X'
              i_skip_first_screen = 'X'
              i_deadend           = 'X'
              i_okcode            = 'OK_GO'
              i_mblnr             = wa_po-mblnr
            EXCEPTIONS
              illegal_combination = 1
              OTHERS              = 2.
          IF sy-subrc <> 0.
          ENDIF.
        WHEN 'IT_PO-BELNR'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          SET PARAMETER ID :
          'RBN' FIELD wa_po-belnr,
          'GJR' FIELD wa_po-bldat(8).
          CALL TRANSACTION 'MIR4' AND SKIP FIRST SCREEN.
        WHEN 'IT_PO-ANEXO_MIGO'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          CONCATENATE wa_po-mblnr wa_po-dt_migo(4) INTO lw_object-instid.
          lw_object-typeid = 'BUS2017'.
          lw_object-catid = 'BO'.
          CALL FUNCTION 'GOS_ATTACHMENT_LIST_POPUP'
            EXPORTING
              is_object = lw_object.
        WHEN 'IT_PO-ANEXO_MIRO'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          CLEAR: lw_object.
          CONCATENATE wa_po-belnr wa_po-bldat(4) INTO lw_object-instid.
          lw_object-typeid = 'BUS2081'.
          lw_object-catid = 'BO'.
          CALL FUNCTION 'GOS_ATTACHMENT_LIST_POPUP'
            EXPORTING
              is_object = lw_object.
        WHEN 'IT_PO-ANEXO_FOLHA'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          CLEAR: lw_object.
          MOVE wa_po-lblni TO lw_object-instid.
          lw_object-typeid = 'BUS2091'.
          lw_object-catid = 'BO'.
          CALL FUNCTION 'GOS_ATTACHMENT_LIST_POPUP'
            EXPORTING
              is_object = lw_object.
        WHEN 'IT_PO-ANEXO_PO'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          CLEAR: lw_object.
          MOVE wa_po-ebeln TO lw_object-instid.
          lw_object-typeid = 'BUS2012'.
          lw_object-catid = 'BO'.
          CALL FUNCTION 'GOS_ATTACHMENT_LIST_POPUP'
            EXPORTING
              is_object = lw_object.
        WHEN 'IT_PO-FI_BELNR'.
          READ TABLE it_po INTO wa_po INDEX selfield-tabindex.
          SET PARAMETER ID 'BLN' FIELD wa_po-fi_belnr.
          SELECT SINGLE * FROM t001w INTO w_t001w WHERE werks = wa_po-werks.
          SELECT SINGLE * FROM t001k INTO w_t001k WHERE bwkey = w_t001w-bwkey.
          SET PARAMETER ID 'BUK' FIELD w_t001k-bukrs.
          SET PARAMETER ID 'GJR' FIELD wa_po-bldat(8).
          CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
      ENDCASE.
  ENDCASE.
ENDFORM.                    "user_command
*&---------------------------------------------------------------------*
*&      Form  CLEAR_MSEG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM process_clear_mseg .
  DATA: v_mseg TYPE mseg.
  LOOP AT it_mseg INTO wa_mseg.
    SELECT SINGLE * FROM mseg
      INTO v_mseg
      WHERE sjahr = wa_mseg-mjahr
      AND smbln = wa_mseg-mblnr
      AND smblp = wa_mseg-zeile.
    IF sy-subrc IS INITIAL.
      DELETE it_mseg
        WHERE mblnr = wa_mseg-mblnr
          AND mjahr = wa_mseg-mjahr
          AND zeile = wa_mseg-zeile.
      DELETE it_mseg
        WHERE mblnr = v_mseg-mblnr
          AND mjahr = v_mseg-mjahr
          AND zeile = v_mseg-zeile.
      DELETE it_ekbe
        WHERE belnr = wa_mseg-mblnr
          AND gjahr = wa_mseg-mjahr
          AND buzei = wa_mseg-zeile.
      DELETE it_ekbe
        WHERE belnr = v_mseg-mblnr
          AND gjahr = v_mseg-mjahr
          AND buzei = v_mseg-zeile.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " CLEAR_MSEG
*&---------------------------------------------------------------------*
*&      Form  SET_VENDOR_NAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_vendor_name .
  FIELD-SYMBOLS: <po> LIKE LINE OF it_po .
  LOOP AT it_po ASSIGNING <po>.
    CHECK <po>-lifnr IS NOT INITIAL.
    SELECT SINGLE name1 FROM lfa1
    INTO <po>-name1 WHERE lifnr = <po>-lifnr.
    IF sy-subrc IS NOT INITIAL.
      CLEAR <po>-name1.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " SET_VENDOR_NAME
*&---------------------------------------------------------------------*
*&      Form  SET_ZFBDT_MIGO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_duedt_migo .
  FIELD-SYMBOLS: <po> LIKE LINE OF it_po .
  LOOP AT it_po ASSIGNING <po>.
    CHECK <po>-duedt IS INITIAL.
    CHECK <po>-mblnr IS NOT INITIAL.
    CHECK <po>-zterm_pedido IS NOT INITIAL.
    CLEAR wa_t052.
    CALL FUNCTION 'FI_TERMS_OF_PAYMENT_CHECK'
      EXPORTING
        i_zterm          = <po>-zterm_pedido
      IMPORTING
        e_t052           = wa_t052
      EXCEPTIONS
        terms_of_payment = 1
        conditions       = 2
        OTHERS           = 3.

    <po>-duedt = <po>-dt_doc_migo + wa_t052-ztag1.
  ENDLOOP.
ENDFORM.                    " SET_ZFBDT_MIGO