antelio
11/28/2018 - 9:20 PM

PO Release Strategy SMOD - ZXM06U22

*&---------------------------------------------------------------------*
*&  Include           ZXM06U22
*&---------------------------------------------------------------------*

*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(I_CEKKO) LIKE  CEKKO STRUCTURE  CEKKO
*"     VALUE(IT_BEKPO) TYPE  MMPUR_BEKPO OPTIONAL
*"     VALUE(IT_BEKET) TYPE  MMPUR_BEKET OPTIONAL
*"     VALUE(IT_EKKNU) TYPE  MMPUR_EKKNU OPTIONAL
*"  EXPORTING
*"     VALUE(E_CEKKO) LIKE  CEKKO STRUCTURE  CEKKO


TYPES: BEGIN OF ty_po,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  zekkn TYPE ekkn-zekkn,
  ebanf TYPE ekpo-banfn,
  bnfpo TYPE ekpo-bnfpo,
  zebkn TYPE ebkn-zebkn,
  depto TYPE cekko-zzdepto,
  matkl TYPE cekko-matkl,
  anln1 TYPE anla-anln1,
  anln2 TYPE anla-anln2,
  kostl TYPE ekkn-kostl,
  netwr TYPE ekkn-netwr,
END OF ty_po.

DATA: t_po   TYPE STANDARD TABLE OF ty_po,
      w_eban TYPE eban,
      w_ebkn TYPE ebkn,
      t_ebkn LIKE TABLE OF w_ebkn,
      w_ekpo   LIKE LINE OF it_bekpo,
      w_po   LIKE LINE OF t_po,
      w_ekkn LIKE LINE OF it_ekknu,
      w_ccusto TYPE ztmm_comp_ccusto.

BREAK-POINT ID zm06e0004.
LOG-POINT ID  zm06e0004 FIELDS i_cekko it_bekpo it_ekknu.

e_cekko = i_cekko.
*SORT it_ekknu BY kostl ASCENDING.

* Totaliza os itens por centro de custo
LOOP AT it_bekpo INTO w_ekpo.
  CLEAR w_po.
  w_po-ebeln = w_ekpo-ebeln.
  w_po-ebelp = w_ekpo-ebelp.
  w_po-matkl = w_ekpo-matkl.
  IF w_ekpo-banfn IS NOT INITIAL.
    SELECT SINGLE * FROM eban INTO w_eban
      WHERE banfn = w_ekpo-banfn
        AND bnfpo = w_ekpo-bnfpo.
    SELECT * FROM ebkn INTO TABLE t_ebkn
      WHERE banfn = w_ekpo-banfn
        AND bnfpo = w_ekpo-bnfpo.
    IF sy-subrc IS INITIAL.
      LOOP AT t_ebkn INTO w_ebkn.
        IF w_eban-kostl is not initial.
          IF w_ebkn-vproz = 0.
            w_po-ebanf = w_ebkn-banfn.
            w_po-bnfpo = w_ebkn-bnfpo.
            w_po-zebkn = w_ebkn-zebkn.
            w_po-kostl = w_ekkn-kostl.
            w_po-netwr = w_eban-netwr.
            SELECT SINGLE depto
            INTO w_po-depto
            FROM ztmm_comp_ccusto
            WHERE kostl = w_po-kostl.
            APPEND w_po TO t_po.
          ELSE.
            "TODO
            w_po-ebanf = w_ebkn-banfn.
            w_po-bnfpo = w_ebkn-bnfpo.
            w_po-zebkn = w_ebkn-zebkn.
            w_po-kostl = w_ekkn-kostl.
            w_po-netwr = w_eban-netwr.
            SELECT SINGLE depto
            INTO w_po-depto
            FROM ztmm_comp_ccusto
            WHERE kostl = w_po-kostl.
            APPEND w_po TO t_po.
          ENDIF.
        ELSE.
          w_po-ebanf = w_ebkn-banfn.
          w_po-bnfpo = w_ebkn-bnfpo.
          w_po-zebkn = w_ebkn-zebkn.
          w_po-netwr = w_eban-netwr.

        ENDIF.
      ENDLOOP.
    ELSE. "NO BANFN
      w_po
    ENDIF.

  ELSE.
    CASE w_ekpo-knttp.
      WHEN ' '.
        CONCATENATE 'X' i_cekko-bukrs INTO w_po-depto.
        w_po-netwr = w_ekpo-netpr.
        APPEND w_po TO t_po.
      WHEN 'K' or 'Z'.
        LOOP AT it_ekknu INTO w_ekkn
        WHERE ebelp = w_ekpo-ebelp.
          w_po-zekkn = w_ekkn-zekkn.
          w_po-kostl = w_ekkn-kostl.
          SELECT SINGLE depto
          INTO w_po-depto
          FROM ztmm_comp_ccusto
          WHERE kostl = w_po-kostl.
          w_po-netwr = w_ekkn-netwr.
          APPEND w_po TO t_po.
        ENDLOOP.
      WHEN 'A'.
        LOOP AT it_ekknu INTO w_ekkn
        WHERE ebelp = w_ekpo-ebelp.
          w_po-zekkn = w_ekkn-zekkn.
          w_po-anln1 = w_ekkn-anln1.
          w_po-anln2 = w_ekkn-anln2.
          SELECT SINGLE kostl
          FROM anlz
          INTO w_po-kostl
          WHERE anln1 = w_po-anln1
          AND anln2 = w_po-anln2
          AND bdatu >= sy-datum
          AND adatu <= sy-datum.
          SELECT SINGLE depto
          INTO w_po-depto
          FROM ztmm_comp_ccusto
          WHERE kostl = w_po-kostl.
          IF w_ekkn-netwr IS INITIAL.
            "TODO
          ELSE.
            w_po-netwr = w_ekkn-netwr.
          ENDIF.
          APPEND w_po TO t_po.
        ENDLOOP.
      WHEN OTHERS.
        LOOP AT it_ekknu INTO w_ekkn
        WHERE ebelp = w_ekpo-ebelp.
          w_po-zekkn = w_ekkn-zekkn.
          w_po-kostl = w_ekkn-kostl.
*          SELECT SINGLE depto
*          INTO w_po-depto
*          FROM ztmm_comp_ccusto
*          WHERE kostl = w_po-kostl.
          APPEND w_po TO t_po.
        ENDLOOP.
    ENDCASE.
  ENDIF.
ENDLOOP.

LOG-POINT ID  zm06e0004 FIELDS t_po.

DATA: t_sum  TYPE TABLE OF ty_po,
      w_sum  LIKE LINE OF t_sum.

LOOP AT t_po INTO w_po.
  CLEAR w_sum.
  w_sum-depto = w_po-depto.
  w_sum-netwr = w_po-netwr.
  COLLECT w_sum INTO t_sum.
ENDLOOP.

LOG-POINT ID  zm06e0004 FIELDS t_sum.
SORT t_sum BY netwr DESCENDING.
READ TABLE t_sum INDEX 1 INTO w_sum..

e_cekko-zzdepto = w_sum-depto.

REFRESH t_sum.

LOOP AT t_po INTO w_po.
  CLEAR w_sum.
  w_sum-matkl = w_po-matkl.
  w_sum-netwr = w_po-netwr.
  COLLECT w_sum INTO t_sum.
ENDLOOP.

LOG-POINT ID  zm06e0004 FIELDS t_sum.
SORT t_sum BY netwr DESCENDING.
READ TABLE t_sum INDEX 1 INTO w_sum..

e_cekko-matkl = w_sum-matkl.


*  IF w_ekko-knttp =  ' '.
*
*    LOOP AT it_ekknu INTO w_ekknu.
*
*      MOVE-CORRESPONDING w_ekknu TO w_ekkn.
*
*      CLEAR w_ekpo.
*      READ TABLE it_bekpo INTO w_ekpo
*        WITH KEY ebelp = w_ekknu-ebelp.
*      IF w_ekkn-netwr IS INITIAL.
*        w_ekkn-netwr = w_ekpo-netpr.
*      ENDIF.
*
*      COLLECT w_ekkn INTO t_ekkn.
*    ENDLOOP.
*  ENDLOOP.
* Ordena com o item de maior valor
*SORT t_ekkn BY netwr DESCENDING.

*READ TABLE t_ekkn INTO w_ekkn INDEX 1.
*IF sy-subrc IS INITIAL.
*
*  READ TABLE it_ekknu INTO w_ekknu
*    WITH KEY kostl = w_ekkn-kostl.
*  IF sy-subrc IS INITIAL.
*    READ TABLE it_bekpo INTO w_ekpo
*      WITH KEY ebelp = w_ekknu-ebelp.
*    IF sy-subrc IS INITIAL.
*      e_cekko-matkl = w_ekpo-matkl.
*    ENDIF.
*  ENDIF.
*
** Seleciona Tabela de Cadastro de Centro de Custo x Departamentos
*  SELECT SINGLE * FROM ztmm_comp_ccusto
*    INTO w_ccusto
*    WHERE kostl = w_ekkn-kostl.
*
*  IF sy-subrc IS INITIAL.
*    e_cekko-zzdepto = w_ccusto-depto.
*  ENDIF.
**
*endif.
*endif.