Auto create and/or approve PN based on new SO line item (ZRPP144)

************************************************************************
* Program     : ZRPP144        REL : ERP2004    Author : Daniel JIang  *
* Created     : 21-11-2011     APP : PP      Type   : Report           *
* Title       : SAM Restructuring – Auto create and/or approve PN based on new SO line item                               *
*----------------------------------------------------------------------*

*------Update list-----------------------------------------------------*
*     21 Nov 2011     Daniel       the first version
* M1  19-JUN-2012     PARK         Copy SO SAM to CSL PN
*
*   the latest update: 2012-06-21 11:00
*--------------------------------------------------------------------- *

REPORT  ZRPP144.

*----------------------------------------------------------------------*.....................
*  Database Table
*----------------------------------------------------------------------*
tables: EKKO,EKPO,CDHDR,CDPOS,VBAK,vbap,KONP,VBKD,EKBE,
        zsd_cdpos,zpp_pn_fty,zcond_map.
*----------------------------------------------------------------------*
*  Internal Table & Variables
*----------------------------------------------------------------------*
databegin of gt_so occurs 0,
  vbeln like vbak-vbeln,
  posnr like zsd_cdpos-posnr,
  udate like zsd_cdpos-udate,
  end of gt_so.

databegin of gt_vbeln occurs 0,
  vbeln like ekpo-j_4kscat,
  end of gt_vbeln.

databegin of gt_pn occurs 0,
  j_4kscat like ekpo-j_4kscat,
  ebeln like ekko-ebeln,
  zzposnr like ekko-zzposnr,
  frgzu like ekko-frgzu,
  reldate like cdhdr-udate,


  end of gt_pn.
data rel_pn like gt_pn occurs 0 with header line.
data lv_tabix type i.

data:   begin of wa_contract,
          lifnr like ekko-lifnr,
          ekgrp like ekko-ekgrp,
          matnr like ekpo-matnr,
          netpr like ekpo-netpr,
          peinh like ekpo-peinh,
          waers like ekko-waers,
          psttr like zmmfgsku-psttr,
          ekorg like ekko-ekorg,
          menge like ekpo-menge,
          fob   type c,
          cmt   type c,
          angnr like ekko-angnr,
          posnr like vbap-posnr,
          bstkd like vbkd-bstkd,
          telf1 like ekko-telf1,
          cmt_wb type c,
          werks like vbak-vkorg,
        end of wa_contract.

data g_rel_flag type c.
data g_ebeln like ekko-ebeln.
DATA:   BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE.
DATA:   MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
DATA: L_SUBRC LIKE SY-SUBRC.
data: cn_rfc(12type c.

*M1
data: so_zzttlsam like vbak-zzttlsam,
      so_zzttlsam2 like vbak-zzttlsam2,
      so_zzttlsam3 like vbak-zzttlsam3,
      so_zzttlsam1a like vbak-zzttlsam1a,
      so_waerk      like vbak-waerk, "M1
      l_vakey       like konh-vakey,
      zzposnr       like ekko-zzposnr,
      v_mvgr4       like vbap-mvgr4,
      wa_knumh      like konh-knumh,
      so_zc23       like konv-kbetr,
      so_zc24       like konv-kbetr,
      so_zc25       like konv-kbetr,
      so_zc26       like konv-kbetr,
      so_zc52       like konv-kbetr,
      so_zc53       like konv-kbetr,
      wa_new_pn     like ekko-ebeln,
      wa_knumv      like vbak-knumv.
      "P_KNUMH       LIKE KONP-KNUMH,
      "P_KSCHL       LIKE KONP-KSCHL.

databegin of it_vbeln occurs 0,
        j_4kscat like ekpo-j_4kscat,
        bukrs_vf like vbak-bukrs_vf,
        zzposnr  like ekko-zzposnr,
        ebeln    like ekko-ebeln,
        lifnr    like eord-lifnr,
      end of it_vbeln.

data: it_konp like konp occurs 0 with header line.

data: wa_kopos like konp-kopos.
*M1 END

*----------------------------------------------------------------------
*  Parameter & Select-Options
*----------------------------------------------------------------------
selection-screen begin of block 1 with frame title text-010.
    select-options: s_vbeln for vbak-vbeln.
    select-options: s_udate for vbak-audat no-extension default sy-datum obligatory .
    select-options: s_date for vbak-audat no-display.
selection-screen end of block 1.

*----------------------------------------------------------------------*
*  Initialize                                                          *
*----------------------------------------------------------------------*
initialization.

*----------------------------------------------------------------------*
*  AT SELECTION-SCREEN                                                 *
*----------------------------------------------------------------------*
at selection-screen.

*----------------------------------------------------------------------*
*  Start of Processing                                                 *
*----------------------------------------------------------------------*
start-of-selection.

perform get_so.
perform get_pn.
perform create_pn.


end-of-selection.
*&---------------------------------------------------------------------*
*&      Form  GET_SO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_SO .
if s_udate-low is not initial.
  if s_udate-high is initial.
    s_date-high = s_udate-low.
  else.
    s_date-high = s_udate-high.
  endif.
  s_date-low = s_udate-low - 3.
  s_date-option = 'BT'.
  s_date-sign = 'I'.
  append s_date.
endif.
if s_udate-low is initial and s_udate-high is not initial.
  s_date-low = s_udate-high - 3.
  s_date-high = s_udate-high.
  s_date-option = 'BT'.
  s_date-sign = 'I'.
  append s_date.
endif.

select a~vbeln a~posnr a~udate into corresponding fields of table gt_so
  from zsd_cdpos as a
  inner join vbap as b on a~vbeln = b~vbeln and a~posnr = b~posnr
  where a~vbeln in s_vbeln
    and a~udate in s_date
    and a~fname = 'BMENG'
    and a~chngind = 'I'
    and b~werks like '3%'
    and b~abgru eq space.
sort gt_so by vbeln posnr udate.
delete adjacent duplicates from gt_so.
loop at gt_so.
  gt_vbeln-vbeln = gt_so-vbeln.
  collect gt_vbeln.
endloop.
sort gt_vbeln.
delete adjacent duplicates from gt_vbeln.

ENDFORM.                    " GET_SO
*&---------------------------------------------------------------------*
*&      Form  GET_RELDATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_RELDATE .
  data: wa_cdpos like cdpos.
  clear wa_cdpos.
  if gt_pn-frgzu = 'X'.
     select * from cdpos
                       where OBJECTCLAS = 'EINKBELEG'
                         and objectid   = gt_pn-ebeln
                         and fname      = 'FRGZU'.
        if cdpos-changenr > wa_cdpos-changenr.
           wa_cdpos = cdpos.
        endif.
     endselect.
     if sy-subrc = '0'.
        select single * from cdhdr
                       where OBJECTCLAS = wa_cdpos-objectclas
                         and objectid   = wa_cdpos-objectid
                         and changenr   = wa_cdpos-changenr.
        gt_pn-reldate = cdhdr-udate.
     endif.
  endif.
ENDFORM.                    " GET_RELDATE
*&---------------------------------------------------------------------*
*&      Form  GET_PN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_PN .

data tmp_pn like gt_pn occurs 0 with header line.

check gt_vbeln[] is not initial.
select a~ebeln a~zzposnr a~frgzu b~j_4kscat   into corresponding fields of table gt_pn
  from ekko as a
  inner join ekpo as b on a~ebeln = b~ebeln
  inner join vbap as c on c~vbeln = b~j_4kscat and a~zzposnr = c~posnr
  for all entries in gt_vbeln
  where b~j_4kscat = gt_vbeln-vbeln
    and a~bstyp = 'K'
    and b~loekz = space
    and c~werks like '3%'
    and c~abgru = space.

*loop at gt_pn.
*  lv_tabix = sy-tabix.
*  perform get_reldate.
*  modify gt_pn index lv_tabix.
*endloop.

" check the first pn if is released
sort gt_pn by j_4kscat ebeln.
tmp_pn[] = gt_pn[].
loop at gt_pn.
  at new j_4kscat.
   clear tmp_pn.
   read table tmp_pn with key j_4kscat = gt_pn-j_4kscat
                                 frgzu = 'X'.
   if sy-subrc = 0.
       collect tmp_pn into rel_pn.
   endif.
  endat.
endloop.
ENDFORM.                    " GET_PN
*&---------------------------------------------------------------------*
*&      Form  CREATE_PN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CREATE_PN .
tables eord.
data wa_lifnr like eord-lifnr.
data l_frgzu like ekko-frgzu.

loop at gt_so.
  clear wa_lifnr.
  select single lifnr into wa_lifnr from eord where matnr = gt_so-vbeln.
  if wa_lifnr is initial.
    continue.
  endif.

  clear: g_rel_flag, rel_pn.
  read table rel_pn with key j_4kscat = gt_so-vbeln.
*  check sy-subrc = 0 .

  if rel_pn-frgzu = 'X'.
    g_rel_flag = 'X'.
  endif.

  clear: g_ebeln, l_frgzu.
  select single a~ebeln a~frgzu into (g_ebeln, l_frgzu )
    from ekko as a
    inner join ekpo as b on a~ebeln = b~ebeln
    inner join vbap as c on c~vbeln = b~j_4kscat and a~zzposnr = c~posnr
    where b~j_4kscat = gt_so-vbeln
      and a~bstyp = 'K'
      and b~loekz = space
      and c~abgru = space
      and c~posnr = gt_so-posnr.
  if sy-subrc = 0"if the new item pn exist
    "" if the new item pn is not released and the old pn is released
*    if l_frgzu <> 'X' and rel_pn-frgzu = 'X'.
**      perform copy_cost.
*      perform release_pn using g_ebeln.
*    endif.
  else"if new item pn not exist
      perform auto_create_pn using rel_pn-ebeln.

      if rel_pn-frgzu = 'X'.
        perform release_pn using g_ebeln.
      endif.
  endif.
endloop.
ENDFORM.                    " CREATE_PN
*&---------------------------------------------------------------------*
*&      Form  AUTO_CREATE_PN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM AUTO_CREATE_PN using p_ebeln .
clear: wa_contract-lifnr,
       wa_contract-ekorg,
       wa_contract-matnr,
       wa_contract-waers,
       wa_contract-posnr.

* Purch Grp :  WA_CONTRACT-EKGRP
  wa_contract-ekgrp ='311'.
* Material : WA_CONTRACT-MATNR

  wa_contract-matnr = gt_so-vbeln.

select single vkorg into wa_contract-werks from vbak
  where vbeln = gt_so-vbeln.

* Vendor : WA_CONTRACT-LIFNR
  select single lifnr into wa_contract-lifnr
         from eord
         where matnr = wa_contract-matnr
         and   werks = wa_contract-werks.

  if sy-subrc <> '0'.
     message e058(zpp01) with 'No Source List. Please check it.'.
  endif.

* Purchasing Organization
 select single ekorg from eord into wa_contract-ekorg
               where lifnr = wa_contract-lifnr
                 and matnr = wa_contract-matnr
                 and werks = wa_contract-werks.

* Cust._PO_NO. : WA_CONTRACT-BSTKD
*  wa_contract-bstkd = wa_zfgsku-bstkd.

* Currency : WA_CONTRACT-WAERS
*  if wa_contract-werks = '3011'.
*     select single waers into wa_contract-waers from lfm1
*            where lifnr = wa_contract-lifnr
*            and   ekorg = '301A'.
*  else.
*     select single waers into wa_contract-waers from lfm1
*            where lifnr = wa_contract-lifnr
*            and   ekorg = '312A'.
*  endif.
if p_ebeln is not initial.
  select single waers into wa_contract-waers  from ekko
    where ebeln = p_ebeln.
else.
  loop at gt_pn where j_4kscat = gt_so-vbeln.
    select single waers into wa_contract-waers  from ekko
      where ebeln = gt_pn-ebeln.
    exit.
  endloop.
endif.
* Delivery Date : WA_CONTRACT-PSTTR
*  wa_contract-psttr = wa_zfgsku-edatu.

* SO Item No. : WA_CONTRACT-POSNR
  wa_contract-posnr = gt_so-posnr.

* FOB : WA_CONTRACT-FOB/ CMT : WA_CONTRACT-CMT
  clear: wa_contract-fob, wa_contract-cmt.
  clear zpp_pn_fty.
  select single * from zpp_pn_fty where lifnr = wa_contract-lifnr.
  if zpp_pn_fty-type = 'CMT'.
     wa_contract-cmt ='X'.
  else.
     wa_contract-fob ='X'.
  endif.

  perform bdc_me31 using p_ebeln.

*M1
PERFORM get_wa_knumh.

*  select single knumv into wa_knumv from vbak
*    where vbeln = p_ebeln.

  " p_ebeln wa_new_pn

  select b~j_4kscat c~bukrs_vf a~zzposnr a~ebeln d~lifnr appending table it_vbeln
        from ekko as a
        inner join ekpo as b on a~ebeln = b~ebeln
        inner join vbak as c on b~j_4kscat = c~vbeln
        inner join eord as d on c~vbeln = d~matnr
        where b~j_4kscat = gt_so-vbeln
          and a~loekz = space
          and a~bstyp = 'K'.

*  concatenate it_vbeln-ebeln '%' into l_vakey.
*  select single knumh into wa_knumh from konh
*           where vakey like l_vakey.

  select * into table it_konp from konp
    where knumh = wa_knumh "wa_new_pn
      and loevm_ko = space.

   loop at it_konp.
     wa_kopos = it_konp-kopos.
   endloop.

  select single a~zzposnr INTO zzposnr
  from ekko as a
  inner join ekpo as b on a~ebeln = b~ebeln
  inner join vbak as c on b~j_4kscat = c~vbeln
  inner join eord as d on c~vbeln = d~matnr
  where b~j_4kscat = gt_so-vbeln
    and a~loekz = space
    and a~bstyp = 'K'.
  "endselect.

  "select single knumv into wa_knumv from vbak where vbeln = gt_so-vbeln.


  select single zzttlsam zzttlsam2 zzttlsam3 zzttlsam1a "knumh
    into (so_zzttlsam,so_zzttlsam2,so_zzttlsam3,so_zzttlsam1a)
    from vbak where vbeln = gt_so-vbeln.

  select single mvgr4 into v_mvgr4 from vbap where vbeln = gt_so-vbeln and posnr = zzposnr.
  if v_mvgr4 = 'ZH'.
    PERFORM UPDATE_COND_VALUE USING wa_knumh 'ZKNI' so_zzttlsam.
*    update konp set kbetr = so_zzttlsam   where knumh = wa_new_pn and kschl = 'ZKNI' and loevm_ko = space.
  else.
    PERFORM UPDATE_COND_VALUE USING wa_knumh 'ZAKN' so_zzttlsam.
*    update konp set kbetr = so_zzttlsam   where knumh = wa_new_pn and kschl = 'ZAKN' and loevm_ko = space.
  endif.

   PERFORM UPDATE_COND_VALUE USING wa_knumh 'ZLIN' so_zzttlsam2.
   PERFORM UPDATE_COND_VALUE USING wa_knumh 'ZSEW' so_zzttlsam3.
   PERFORM UPDATE_COND_VALUE USING wa_knumh 'ZKTS' so_zzttlsam1a.

*M1 END

ENDFORM.                    " AUTO_CREATE_PN

*M1
FORM update_cond_value USING    p_line
                                p_cond
                                p_kbetr.
data: wa_value like konp-kbetr,
      wa_value2 like konp-kpein.

   read table it_konp with key knumh = wa_knumh
                               kschl = p_cond.
   if sy-subrc eq 0.
     konp = it_konp.
     konp-kbetr = p_kbetr.
     "konp-kschl = p_cond.
     "konp-kpein = wa_value2.
     update konp.
   else.
     read table it_konp with key knumh = wa_knumh
                                 kschl = 'J3AP'.
     konp = it_konp.
     konp-kschl = p_cond.
     wa_kopos = wa_kopos + 1.
     konp-kopos = wa_kopos.
     konp-kbetr = p_kbetr.
     konp-zaehk_ind = wa_kopos.
     "konp-kpein = wa_value2.
     insert konp.
   endif.
   commit work ."and wait.
ENDFORM.

FORM get_wa_knumh.
  concatenate wa_new_pn '%' into l_vakey.
  select single knumh into wa_knumh from konh
           where vakey like l_vakey.
ENDFORM.    "get_wa_knumh
*M1 END

*&---------------------------------------------------------------------*
*&      Form  RELEASE_PN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM RELEASE_PN using p_ebeln .
     submit zrpp028 with s_ebeln = p_ebeln
                     and return exporting list to memory.
     wait up to 1 seconds.
ENDFORM.                    " RELEASE_PN
*&---------------------------------------------------------------------*
*&      Form  BDC_ME31
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM BDC_ME31 using p_ebeln.
CONSTANTS: lv_pk_vendor like lfa1-lifnr value '0000605079'.
data: wa_date(10)  type c,
      wa_menge(15type c,
      wa_netpr(10type c,
      wa_peinh(5type c.

data: wa_asize(22)    type c value 'J_3ASZDI-J_3ASIZED(??)',
      wa_genge(18)    type c value 'J_3ASZDI-MENGE(??)',
      wa_kscat(21)    type c value 'J_3ASZDI-J_4KSCAT(??)',
      counter(2)    type n.

data: wa_aufnr like aufk-aufnr.
data: wa_ktokk like lfa1-ktokk.
data: wa_vbak  like vbak.
data: wa_konv  like konv occurs 0 with header line,
      it_konv  like konv occurs 0 with header line.
data: wa_bsart like ekko-bsart,
      wa_bukrs like ekko-bukrs.

data: wa_werks like zmm0001-werks,
      wa_txt(10type c.

data: wa_j3ap  like konv-kbetr,
      wa_kbetr(9type c,
      wa_kpein(5type c,
      wa_meins(3type c.

data: wa_type(3type c,
      wa_net(12type c.

data: wa_kursk like VBKD-KURSK.

databegin of it_pn occurs 0,
        ebeln like ekko-ebeln,
        reldt like cdhdr-udate,
        rel   type c,
      end of it_pn..

data: lv_tabix like sy-tabix.

data: it_konp like konp occurs 0 with header line.

data: wa_knumh  like konh-knumh,
      l_vakey   like konh-vakey,
      lv_cond_flag type c.

data: lv_ftyterm like vbak-zzftyterm.

clear: lv_ftyterm.

if wa_contract-lifnr eq '0000006160' or wa_contract-lifnr eq '0000003080'.
    select single zzftyterm into lv_ftyterm
        from vbak
        where vbeln = gt_so-vbeln
          and augru = space.
    if wa_contract-fob eq 'X'.
        if lv_ftyterm ne 'FOB'.
            message e999(zmrr) with 'PN is FOB but SO Factory term not equal to FOB'.
        endif.
    elseif wa_contract-cmt eq 'X'.
        if lv_ftyterm ne 'CMT'.
            message e999(zmrr) with 'PN is CMT but SO Factory term not equal to CMT'.
        endif.
    elseif wa_contract-cmt_wb eq 'X'.
        message e999(zmrr) with 'PN is CMT without BOM'.
        exit.
    endif.
endif.

     select single ktokk into wa_ktokk
                   from lfa1 where lifnr = wa_contract-lifnr.
     if wa_contract-lifnr = lv_pk_vendor.
       wa_ktokk = 'ZV01'.
     endif.

     if wa_ktokk = 'ZV01'.
       wa_txt = wa_contract-lifnr.
       wa_txt+9(1) = '1'.

       shift wa_txt left deleting leading '0'.
       if wa_contract-lifnr = lv_pk_vendor.
         wa_werks = '3861'.
       else.
         wa_werks = wa_txt.
       endif.
     endif.

*     IO for factory 2
      data: wa_zzfty2 like vbak-zzfty2.
      data: wa_aufnr_fty2 like wa_aufnr.

      select single zzfty2 into wa_zzfty2
        from vbak
        where vbeln = gt_so-vbeln and
              kunnr = '0000100016' and
              vkorg like '5%'.

      if sy-subrc = 0.
        if wa_zzfty2 is initial.
          message e999(zmrr) with 'Factory 2 cannot be empty'.
          exit.
        endif.

        wa_zzfty2+3(1) = '1'.
        select single ordt into wa_aufnr_fty2 from zpp_fty_cctr
                    where   fty = wa_zzfty2
                      and crtio = 'X'.

         if sy-subrc eq 0.
           concatenate wa_contract-matnr '/' wa_aufnr_fty2 into wa_aufnr_fty2.
         endif.
      endif.


*     IO for factory 2 for vendor 6160
      data: wa_zzfty6 like vbak-zzfty2.
      data: wa_aufnr_fty6 like wa_aufnr.
      if wa_contract-lifnr eq '0000006160' or wa_contract-lifnr eq '0000003080'."M11 Add'or wa_contract-lifnr eq '0000003080''.
          select single zzfty2 into wa_zzfty6
            from vbak
            where vbeln = gt_so-vbeln.
          if sy-subrc = 0.
            if wa_zzfty6 is initial.
              message e999(zmrr) with 'Factory 2 cannot be empty'.
              exit.
            endif.
            wa_zzfty6+3(1) = '1'.
            select single ordt into wa_aufnr_fty6 from zpp_fty_cctr
                        where   fty = wa_zzfty6
                          and crtio = 'X'.

             if sy-subrc eq 0.
               concatenate wa_contract-matnr '/' wa_aufnr_fty6 into wa_aufnr_fty6.
             endif.
          endif.
      endif.

     select single * into wa_vbak from vbak
                                       where vbeln = wa_contract-matnr.
*// check if there is released PN
if g_rel_flag = 'X'.
  concatenate p_ebeln '%' into l_vakey.
  clear wa_knumh.
  select single knumh into wa_knumh from konh
           where vakey like l_vakey.

  refresh it_konp.

  select * into table it_konp from konp
                 where knumh = wa_knumh
                   and LOEVM_KO = space.
  loop at it_konp.
    it_konv-kschl = it_konp-kschl.
    if it_konp-konwa ne wa_contract-waers.
         CALL FUNCTION 'Z_EX_RATE'
           EXPORTING
             FCURR         = it_konp-konwa
             ZTCURR        = wa_contract-waers
           IMPORTING
             EX_RATE       = wa_kursk.
      it_konv-kbetr = it_konp-kbetr * wa_kursk.
    else.
      it_konv-kbetr = it_konp-kbetr.
    endif.

    if it_konp-kschl = 'J3AP'.
      wa_j3ap = it_konv-kbetr.
      wa_peinh = it_konp-kpein.
    endif.
    it_konv-kpein = it_konp-kpein.
    it_konv-kmein = it_konp-kmein.
    append it_konv.
  endloop.
else.
*// retrieve SO condition value for creating PN
      refresh: wa_konv, it_konv.

      select * into table wa_konv from konv where knumv = wa_vbak-knumv
                                        and kposn = wa_contract-posnr
                                        and kschl like 'ZC%'.

*// end of [H03]
      if wa_contract-fob = 'X'.
         wa_type = 'FOB'.
      else.
         wa_type = 'CMT'.
      endif.

      sort wa_konv by kschl descending.
      refresh it_konv.


*// Mapping SO condition to PN condition
      clear: wa_J3ap,lv_cond_flag.
      loop at wa_konv.

        clear lv_cond_flag.
        select * from zcond_map where so_cond = wa_konv-kschl
                                         and type    = wa_type.

*          : do not map washing cost (ZC07 -> ZW01) for all CSL
            if wa_contract-WERKS = '3011' or wa_contract-werks = '3121'.
               if wa_konv-kschl = 'ZC07'.
                  continue.
               endif.
            endif.

            if sy-subrc eq 0.
              if wa_konv-waers ne wa_contract-waers.
                 CALL FUNCTION 'Z_EX_RATE'
                   EXPORTING
                     FCURR         = wa_konv-waers
                     ZTCURR        = wa_contract-waers
                   IMPORTING
                     EX_RATE       = wa_kursk
                           .
                 if lv_cond_flag ne 'X'.
                     wa_konv-kbetr = wa_konv-kbetr * wa_kursk.
                 endif.
                 lv_cond_flag = 'X'.
               endif.

              if zcond_map-pn_cond = 'J3AP'.
                wa_j3ap = wa_j3ap + wa_konv-kbetr.
                wa_peinh = wa_konv-kpein.
              endif.
*              else.
                read table it_konv with key kschl = zcond_map-pn_cond.
                if sy-subrc eq 0.
                  it_konv-kbetr = it_konv-kbetr + wa_konv-kbetr.
                  modify it_konv index sy-tabix.
                else.
                  move-corresponding wa_konv to it_konv.
                  it_konv-kschl = zcond_map-pn_cond.
*                : check for company code starting with '6' and
*                            copy condition for ZC11 to PN
                  if it_konv-kschl = 'ZQ01'.
                     select single * from vbak where vbeln = wa_contract-matnr.
                     if vbak-BUKRS_VF(1) = '6'.
                        append it_konv.
                     endif.
                  else.
                    append it_konv.
                  endif.
                endif.
            endif.
        endselect.
      endloop.
endif.

wa_net = wa_j3ap.

delete it_konv where kbetr = 0.
*concatenate sy-datum+6(2) '.' sy-datum+4(2) '.'
*            sy-datum(4) into wa_date.
wa_Date = '31.12.9999'.
wa_menge = '1'.

    perform bdc_dynpro      using 'SAPMM06E' '0200'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'RM06E-WERKS'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '/00'.
    perform bdc_field       using 'EKKO-LIFNR'
                                  wa_contract-lifnr.
***Begin 002: BDJV amendment
    if wa_contract-matnr(1eq 'B'.
        wa_bukrs = '2010'.
    else.
        concatenate wa_contract-matnr(1'010' into wa_bukrs.
    endif.
***End 002
*// determine doc type for PN
    IF wa_contract-fob = 'X'.
       select single bsart into wa_bsart
                    from zpp_doc_type
                    where bukrs = wa_bukrs
                      and type  = 'FOB CONTRACT'.
    else.
       select single bsart into wa_bsart
                    from zpp_doc_type
                    where bukrs = wa_bukrs
                      and type  = 'CMT CONTRACT'.
    ENDIF.

    perform bdc_field       using 'RM06E-EVART'
                                  wa_bsart.

    perform bdc_field       using 'EKKO-EKORG'
                                  wa_contract-ekorg.
    perform bdc_field       using 'EKKO-EKGRP'
                                  wa_contract-ekgrp.
    perform bdc_field       using 'RM06E-WERKS'
                                  wa_contract-werks.
    perform bdc_field       using 'RM06E-LGORT'
                                  ''.
    if wa_contract-fob = 'X' or wa_contract-cmt_wb = 'X'.
      perform bdc_field       using 'RM06E-EPSTP'
                                    ' '.
    else.
      perform bdc_field       using 'RM06E-EPSTP'
                                    'L'.
    endif.

data: wa_ebeln like ekpo-ebeln.

    select single a~ebeln into wa_ebeln from ekpo as a
                           inner join ekko as b
                           on a~ebeln = b~ebeln
                           where not bsart in ('ZPM''ZPF')
                             and b~bstyp = 'F'
                             and j_4kscat = wa_contract-matnr
                             and mtart = 'ZSFG'
                             and a~loekz = space.
    if sy-subrc eq 0.
       wa_vbak-zzopa = 'Y'.
    else.
       if not wa_vbak-zzopa is initial.
          wa_vbak-zzopa = 'Y'.
       endif.
    endif.

    perform bdc_dynpro      using 'SAPMM06E' '0201'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'EKKO-KDATE'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '/00'.
    perform bdc_field       using 'EKKO-KDATE'
                                  wa_date.
    perform bdc_field       using 'EKKO-ANGNR'
                                  wa_contract-angnr.
    perform bdc_field       using 'EKKO-WAERS'
                                  wa_contract-waers.
    perform bdc_field       using 'EKKO-IHREZ'
                                  wa_vbak-zzttlsam.
    perform bdc_field       using 'EKKO-UNSEZ'
                                  wa_vbak-zzttlsam2.
    perform bdc_field       using 'EKKO-VERKF'
                                  wa_vbak-zzopa.
    perform bdc_field       using 'EKKO-TELF1'
*                                  wa_contract-posnr.
                                  wa_contract-telf1.
    perform bdc_field       using 'EKKO_CI-ZZPOSNR'
                                  wa_contract-posnr.


    perform bdc_dynpro      using 'SAPMM06E' '0220'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'RM06E-EVRTP(01)'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=DETA'.
    perform bdc_field       using 'RM06E-TCSELFLAG(01)'
                                  'X'.
    perform bdc_field       using 'EKPO-EMATN(01)'
                                  wa_contract-matnr.
    perform bdc_field       using 'EKPO-KTMNG(01)'
                                  wa_menge.

    if wa_contract-lifnr = '0000005180' or wa_contract-lifnr = '0000005120'

       or  wa_contract-lifnr = '0000005230'     .

      perform bdc_field       using 'EKPO-NETPR(01)'
                                    '0'.
    else.
      perform bdc_field       using 'EKPO-NETPR(01)'
                                    wa_net.
    endif.
    perform bdc_field       using 'EKPO-PEINH(01)'
                                  wa_peinh.

    data: wa_bstkd1(20type c.
    wa_bstkd1 = wa_contract-bstkd(20).

*    if wa_contract-lifnr = '0000005180'.
*      perform bdc_dynpro      using 'SAPMM06E' '0211'.
*      perform bdc_field       using 'BDC_OKCODE'
*                                   '=KO'.
*      perform bdc_field       using 'EKPO-BEDNR'
*                                    wa_vbak-kunnr.
*      perform bdc_field       using 'EKPO-IDNLF'
*                                    wa_vbak-bname.
*
*      perform bdc_field       using 'EKPO-REPOS'
*                                    ''.
*      perform bdc_field       using 'EKPO-WEBRE'
*                                    space.
*      perform bdc_field       using 'EKPO-NETPR'
**                                    wa_net.
*                                    '0.0'.
*      perform bdc_field       using 'EKPO-LABNR'
**                                    wa_contract-bstkd.
*                                    wa_bstkd1.
*    else.
      perform bdc_dynpro      using 'SAPMM06E' '0211'.
      perform bdc_field       using 'BDC_OKCODE'
                                    '=KO'.
      perform bdc_field       using 'EKPO-BEDNR'
                                    wa_vbak-kunnr.
      perform bdc_field       using 'EKPO-IDNLF'
                                    wa_vbak-bname.
      perform bdc_field       using 'EKPO-LABNR'
*                                  wa_contract-bstkd.
                                    wa_bstkd1.
*    endif.


*    if wa_contract-lifnr ne '0000005180' and wa_contract-lifnr ne '0000005190'.
    if wa_contract-lifnr ne '0000005180' and wa_contract-lifnr ne '0000005120'
*M10 add
       or  wa_contract-lifnr = '0000005230'     .
*M10 End

*    clear wa_J3ap.
    loop at it_konv where kschl ne 'J3AP'.
*      wa_j3ap = wa_j3ap + it_konv-kbetr.
      wa_kbetr = it_konv-kbetr.
      wa_kpein = it_konv-kpein.
      shift wa_kbetr left deleting leading space.
      shift wa_kpein left deleting leading space.

      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
        EXPORTING
          INPUT                = it_konv-kmein
          LANGUAGE             = 'E'
        IMPORTING
          OUTPUT               = wa_meins
        EXCEPTIONS
          UNIT_NOT_FOUND       = 1
          OTHERS               = 2
                .

      perform bdc_dynpro      using 'SAPMV13A' '0201'.
      perform bdc_field       using 'BDC_OKCODE'
                                    '/00'.
      perform bdc_field       using 'KONP-KSCHL(02)'
                                    it_konv-kschl.
      perform bdc_field       using 'KONP-KBETR(02)'
                                    wa_kbetr.
*      perform bdc_field       using 'KONP-KONWA(02)'
*                                    it_konv-waers.
      perform bdc_field       using 'KONP-KPEIN(02)'
                                    wa_kpein.
      perform bdc_field       using 'KONP-KMEIN(02)'
                                    wa_meins.

      perform bdc_dynpro      using 'SAPMV13A' '0201'.
      perform bdc_field       using 'BDC_CURSOR'
                                    'KONP-KSCHL(02)'.
      perform bdc_field       using 'BDC_OKCODE'
                                    '=EINF'.
      perform bdc_field       using 'RV130-SELKZ(02)'
                                    'X'.
    endloop.

    clear: wa_kbetr,wa_kpein,wa_meins.
    read table it_konv with key kschl = 'J3AP'.
    if sy-subrc eq 0.
        wa_kbetr = it_konv-kbetr.
        wa_kpein = it_konv-kpein.
        CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
          EXPORTING
            INPUT                = it_konv-kmein
            LANGUAGE             = 'E'
          IMPORTING
            OUTPUT               = wa_meins
          EXCEPTIONS
            UNIT_NOT_FOUND       = 1
            OTHERS               = 2
                  .
    endif.

    perform bdc_dynpro      using 'SAPMV13A' '0201'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=BACK'.
    perform bdc_field       using 'KONP-KBETR(01)'
                                  wa_kbetr.
    perform bdc_field       using 'KONP-KONWA(01)'
                                  wa_contract-waers.
    perform bdc_field       using 'KONP-KPEIN(01)'
                                  wa_kpein.
    perform bdc_field       using 'KONP-KMEIN(01)'
                                  wa_meins.
    else.
        perform bdc_dynpro      using 'SAPMV13A' '0201'.
        perform bdc_field       using 'BDC_OKCODE'
                                      '=BACK'.
        perform bdc_field       using 'KONP-KPEIN(01)'
                                      wa_peinh.
    endif.



perform bdc_dynpro      using 'SAPMM06E' '0220'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'EKPO-EMATN(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=J3AK'.
*perform bdc_field       using 'RM06E-EBELP'
*                              record-EBELP_002.
perform bdc_dynpro      using 'SAPLJ3AD' '8000'.
perform bdc_field       using 'BDC_OKCODE'
                              '=SZOV'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'J_3ADIMM-LGORT(01)'.
perform bdc_dynpro      using 'SAPLJ3AD' '8000'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BAC1'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'J_3ASZDI-J_4KSCAT(01)'.
*perform bdc_field       using 'J_3ASZDI-J_3AETENR'
*                              record-J_3AETENR_003.
perform bdc_field       using 'J_3ASZDI-J_4KSCAT(01)'
                              wa_contract-matnr.    "record-J_4KSCAT_01_004.

    perform bdc_dynpro      using 'SAPMM06E' '0220'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'RM06E-EBELP'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=BU'.

    perform bdc_dynpro      using 'SAPLSPO1' '0300'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=YES'.

*   23/04/07 : change to rfc call
    databegin of it_pnlist occurs 0,
            ebeln like ekko-ebeln,
          end of it_pnlist.

    data: wa_last_pn like ekko-ebeln.

    if wa_contract-WERKS = '3011' or wa_contract-WERKS = '3121'.
       clear: it_pnlist[],wa_last_pn, wa_new_pn.
       select a~ebeln  into corresponding fields of table it_pnlist
              from ekko as a inner join ekpo as b
              on a~ebeln = b~ebeln
              where b~matnr = wa_contract-matnr
*              and   a~zzposnr = wa_contract-posnr
              and   b~loekz = space
              and   a~bsart in ('ZCM''ZCF').

       sort it_pnlist by ebeln descending.
       read table it_pnlist index 1.
       if sy-subrc = '0'.
          wa_last_pn = it_pnlist-ebeln.
       endif.

       perform rfc_transaction using 'ME31K'.
    else.
       perform bdc_transaction using 'ME31K'.
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
          WAIT       = 'X'.
    endif.
*   modify 2008.11.18 by Leo.
    clear BDCDATA.
    refresh BDCDATA.
*   end .
    wait up to 1 seconds.

    if wa_contract-WERKS = '3011' or wa_contract-WERKS = '3121'.
       clear: it_pnlist[], wa_new_pn.
       select a~ebeln  into corresponding fields of table it_pnlist
              from ekko as a inner join ekpo as b
              on a~ebeln = b~ebeln
              where b~matnr = wa_contract-matnr
*              and   a~zzposnr = wa_contract-posnr
              and   b~loekz = space
              and   a~bsart in ('ZCM''ZCF').

       sort it_pnlist by ebeln descending.
       read table it_pnlist index 1.
       if sy-subrc = '0'.
          wa_new_pn = it_pnlist-ebeln.
       endif.

       if wa_new_pn > wa_last_pn.
           select * from ekko where ebeln = wa_new_pn.
             ekko-zzposnr = wa_contract-posnr.
             update ekko.
           endselect.
       endif.
    endif.

ENDFORM.                    " BDC_ME31

*----------------------------------------------------------------------*
*        Start new transaction according to parameters                 *
*----------------------------------------------------------------------*
FORM BDC_TRANSACTION USING TCODE.
  DATA: L_MSTRING(480).
  DATA: WA_PARAMS TYPE CTU_PARAMS.

    REFRESH MESSTAB.
    WA_PARAMS-DISMODE = 'N'.
    WA_PARAMS-UPDMODE = 'L'.
    WA_PARAMS-DEFSIZE = 'X'.
    CALL TRANSACTION TCODE USING BDCDATA OPTIONS FROM WA_PARAMS
                   MESSAGES INTO MESSTAB.
    if sy-subrc = 0.
      read table messtab with key msgtyp = 'S' .
      if sy-subrc = 0 and messtab-msgv2 <> space.
        g_ebeln = messtab-msgv2.
      endif.
    endif.

    L_SUBRC = SY-SUBRC.
    REFRESH BDCDATA.
ENDFORM.

*----------------------------------------------------------------------*
*        Start new screen                                              *
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
    CLEAR BDCDATA.
    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    APPEND BDCDATA.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  RFC_TRANSACTION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_2237   text
*----------------------------------------------------------------------*
FORM RFC_TRANSACTION  USING    VALUE(P_TCODE).
*
    refresh messtab.

    CONCATENATE SY-SYSID 'CLNT' SY-MANDT INTO CN_RFC.

    CALL FUNCTION 'ZRFC_BDC'
      destination cn_rfc
      EXPORTING
        TCODE          = P_TCODE
        LV_MODE        = 'N'
     TABLES
       BDC_DATA       =  BDCDATA
       TA_MESS        =  MESSTAB   .

    if sy-subrc = 0.
      read table messtab with key msgtyp = 'S' .
      if sy-subrc = 0 and messtab-msgv2 <> space.
        g_ebeln = messtab-msgv2.
      endif.
    endif.
ENDFORM.                    " RFC_TRANSACTION
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章