CAL - ISPS automate V01 GR (ZRPP106B)

***********************************************************************
* Program     : ZRPP106B         REL : 4.6C   Author : Steven
* Created     : 2010.11.23       APP : ZPP
* Title       : Background Job for V01 GR
* Description : CAL - ISPS automate V01 GR
*
*M1 Steven  2011.03.16 Remove M1
*M2 Daniel  2011.04.19 If the GR failed, then do reverse FG GR. (fix bug of original program)
*M3 Daniel  2011.06.07 Grid in PO schedule instead of PO item when create PO.(fix bug of original program)
*M4 Daniel  2011.07.25 allow background run, the txt doc should not be changed  after performing
*                      if folder have no file, it shouldn't happen error.
*M5 Daniel  2011.09.08 if check_ZPP_FGGR_SO_CAL fail ,display all unsuitable material
*M6 Daniel  2012.01.12  fix bug , use BAPI instead of "update"
* the last update 2012.01.12   10:00
*----------------------------------------------------------------------

REPORT  ZRPP106B.

TABLES: T100,ekko,ekpo,ZPP_FGGR_SO_CAL,j_3abdsi,resb,zpp_fggr_batch,
         mslb, mchb, rm06e, J_3APGEN.

DATAbegin of itab_file_record occurs 0,
        BUDAT(10type c"like rm06e-bedat,
        EBELN(10)  type c,
        MENGE(13)  type c,
        J_3ASIZ(8type c,  "M2 modify the length
        LIFNR(10)  type c,
       " LGORT(4)   type c,
        vbeln like vbak-vbeln,
        FG        type c,
        filepath(75type c,
        filename(60type c,
"        ebeln_cal like mseg-ebeln,
        ebelp like mseg-ebelp,
        matnr like mseg-matnr,
        index type i,
"M4 add
        j_3asize like ZJ3ADICT-J_3ASIZE,
        O_J3AKORD1  LIKE J_3APGEN-J_3AKORD1, "COLOR  001
        O_J3AKORD2  LIKE J_3APGEN-J_3AKORD2, " STREAM  2,4
        O_J3AKORD3  LIKE J_3APGEN-J_3AKORD3, "SIZE   M,L
"M4 END
      end of itab_file_record.

data: itab_file_record_cal like itab_file_record occurs 0 with header line.

databegin of it_mchb occurs 0,
       lgort like mchb-lgort,
       matnr like mchb-matnr,
       j_3asize like mchb-j_3asize,
       clabs like mchb-clabs,
      end of it_mchb.

databegin of file_itab occurs 0,
         text_content type c length 100"m4 modify
       end of file_itab.

data: file_itab_cache like file_itab occurs 0 with header line.

databegin of file_itab_count occurs 0,
        count type i,
      end of file_itab_count.

DATA: temp_file_record like itab_file_record occurs 0 with header line.
data: lv_filename TYPE STRING.


DATA: BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE.
DATA: MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
DATA: L_SUBRC LIKE SY-SUBRC.
TYPES: KCDE_DIRECTORY LIKE RLGRAP-FILENAME OCCURS 0.
DATA: L_DIRECTORY TYPE KCDE_DIRECTORY WITH HEADER LINE.

data: classtype(4type c,
      IN_FILE(255TYPE C,
      OUT_FILE(255TYPE C.

data: move_flag type c.
data: mess(60type c.
data:err_message(200type c.

*   begin of file information retrival

DATABEGIN OF FILE_LIST OCCURS 100,
        DIRNAME(75TYPE C" name of directory. (possibly truncated.)
        NAME(75)    TYPE C" name of entry. (possibly truncated.)
        TYPE(30)    TYPE C,            " type of entry.
        LEN(8)      TYPE P,            " length in bytes.
        OWNER(8)    TYPE C,            " owner of the entry.
        MTIME(6)    TYPE P, " last modification date, seconds since 1970
        MODE(9)     TYPE C" like "rwx-r-x--x": protection mode.
        USEABLE(1)  TYPE C,
        SUBRC(4)    TYPE C,
        ERRNO(3)    TYPE C,
        ERRMSG(40)  TYPE C,
        MOD_DATE    TYPE D,
        MOD_TIME(8TYPE C,            " hh:mm:ss
        SEEN(1)     TYPE C,
        CHANGED(1)  TYPE C,
      END OF FILE_LIST.

DATABEGIN OF FILE_NAME OCCURS 100,
        NAME(75)    TYPE C,
      END OF FILE_NAME.


DATABEGIN OF FILE,
        DIRNAME(75TYPE C" name of directory. (possibly truncated.)
        NAME(75)    TYPE C" name of entry. (possibly truncated.)
        TYPE(30)    TYPE C,            " type of entry.
        LEN(8)      TYPE P,            " length in bytes.
        OWNER(8)    TYPE C,            " owner of the entry.
        MTIME(6)    TYPE P, " last modification date, seconds since 1970
        MODE(9)     TYPE C" like "rwx-r-x--x": protection mode.
        USEABLE(1)  TYPE C,
        SUBRC(4)    TYPE C,
        ERRNO(3)    TYPE C,
        ERRMSG(40)  TYPE C,
        MOD_DATE    TYPE D,
        MOD_TIME(8TYPE C,            " hh:mm:ss
        SEEN(1)     TYPE C,
        CHANGED(1)  TYPE C,
      END OF FILE.

data:begin of it_po occurs 0,
        ebeln like ekko-ebeln,
     end of it_po.

databegin of it_doc occurs 0,
         docno(50type c,
      end of it_doc.

databegin of it_j_3abdsi occurs 0,
        matnr like j_3abdsi-matnr,
        j_3asize like j_3abdsi-j_3asize,
        erfmg like j_3abdsi-erfmg,
        meins like j_3abdsi-erfmg, "M2 add
        rsnum like j_3abdsi-rsnum, "M3 add
      end of it_j_3abdsi.
data: it_suc like zpp_fggr_log_cal occurs 0 with header line.
data: it_err like zpp_fggr_log_cal occurs 0 with header line.
data: wa_bom like zpp_2nd_bom occurs 0 with header line.

data: lv_ebelp(5type c.
data: pass_fg_check type c.
DATA: WA_MATNR LIKE EKPO-MATNR.
data: file_path  like  rlgrap-filename.
data: lv_tabix like sy-tabix.
data: file_index type i.
data: file_count type i.
data: file_flag type c.
data: lv_index type i.
data: lv_fromName(145type c,
      lv_toName(145type c.
data: bflag type c.
data: d_menge(13type c.
data: b_bool type c.

*M2 add
data: sc_stock like mslb occurs 0 with header line.
data old_J3ABDSI like J_3ABDSI occurs 0 with header line.
data old_resb like resb occurs 0 with header line.
*M2 end

*M3 add
data g_ttl(7type p. "m3 the total po qty.
data gc_ttl(13type c"M3 add
data  old_mchb like mchb occurs 0 with header line"M3 add
data old_file like itab_file_record occurs 0 with header line.
data zme2o_succ type c.
data migo_succ type c.
data check_stock_error type c.
*M3 end
*M4 add
DATA: A_GENERIC_NAME(75TYPE C.
DATA: ERRCNT(2TYPE P VALUE 0.
data move_succ type c.
data move_err type c.
data create_po_succ type c.
*M4 end
*----------------------------------------------------------------------
*  Parameter & Select-Options
*----------------------------------------------------------------------
selection-screen begin of block 1 with frame title text-001.
    select-options: p_lifnr for ekko-lifnr .
    parameter: p_werks like ekpo-werks.
    parameter: p_lgort like ekpo-lgort.
    parameter: s_lgort like ekpo-lgort.
selection-screen end of block 1.

selection-screen begin of block 2 with frame title text-002.
    selection-screen skip.
    parameter: i_path  like cffile-filename default '\\10.2.1.35\PP\CEN_ISPS_CAL'."'\\10.2.1.35\pp\cen_isps'."
selection-screen end of block 2.

*----------------------------------------------------------------------*
*  AT SELECTION-SCREEN                                                 *
*----------------------------------------------------------------------*
at selection-screen on value-request for i_path.
  call function 'WS_FILENAME_GET'
       exporting
            def_path = 'C:\'
            mask     = ',*.*,*.*.'
            mode     = 'O'
       importing
            filename = i_path
       exceptions
            selection_cancel = 1
            selection_error = 2
            others = 3.

at selection-screen.

start-of-selection.
clear:it_po[].
perform GET_FILENAME.
file_index = 0.
describe table file_name lines file_count.
loop at file_name.
  clear:it_doc[].
  file_flag = 'X'.
  perform GET_DATA_FROM_TXT using file_name-name
                                  file_count.
endloop.
append lines of itab_file_record to old_file."m3 add
sort itab_file_record by index.
delete itab_file_record where vbeln is initial.
*M4 add
if itab_file_record[] is initial.
  message i058(zpp01) with 'Read file fail or no data in the file ' . "M4 add
  stop.
endif.
*M4 end
do file_count times.
  clear: it_po, it_po[]. "m4 add
  clear: zme2o_succ,migo_succ, check_stock_error, create_po_succ."M3 add
  clear:it_doc[],bflag,b_bool.
  clear: move_succ,move_err.
  lv_index = lv_index + 1.
  perform get_ttl_poQty. "M3 add
  perform add_ebelp using lv_index.
  perform create_po using lv_index.   "m4 remove
*  perform sort_scheduleLine_by_j3asize using lv_index. "m4 add
*  perform create_bapi_po using lv_index. "M4 add
  describe table it_doc.
*  if sy-tfill = 1. "m4 remove
  if create_po_succ = 'X'.  "M4 add
     perform get_poRequire. "M2 add
     perform check_ZPP_FGGR_SO_CAL using lv_index.
     if bflag eq '' and check_stock_error ne 'X'"M3 add check_stock_error ne 'X'
*       if b_bool eq 'X'. "m3 remove
        perform get_mslb. " M2 add , get the advance SC stock data  before zme2o
        perform bdc_zme2o using lv_index.
*       endif.
     else.
       continue.
     endif.
  else.
     continue.
  endif.
  describe table it_doc.
*  if sy-tfill = 2 or b_bool eq '' . "M3 remove
  if  zme2o_succ eq 'X'"M3 add zme20_succ eq 'X'
*     perform bdc_migo using lv_index.
     perform bapi_migo using lv_index.
  else.
    continue.
  endif.

  describe table it_doc.
  if migo_succ eq 'X'"M3 modify
    clear:file_itab_cache[].
    clear: lv_fromName, lv_toName. "M3 add
*M3 add change 'itab_file_record' to 'old_file'
    loop at old_file where index = lv_index.
*      old_file-budat+2(1) = ''.
*      old_file-budat+5(1) = ''.
      CONDENSE old_file-budat NO-GAPS.
      concatenate old_file-budat ';'
                  old_file-ebeln ';'
                  old_file-menge ';'
                  old_file-J_3ASIZ ';'
                  old_file-LIFNR ';'
                  old_file-vbeln into file_itab_cache-text_content.
       if old_file-fg eq 'X'.
         concatenate file_itab_cache-text_content ';' old_file-fg into file_itab_cache-text_content.
       endif.
      append file_itab_cache.
    concatenate old_file-filepath '\' old_file-filename into lv_fromName.
    concatenate old_file-filepath '\COMPLETE\' old_file-filename into lv_toName.
    endloop.
    check file_itab_cache[] is not initial.  " M3 add
    move_succ = 'X'clear move_err.
    perform move_file tables file_itab_cache
                      using lv_fromName
                             move_succ.
    delete itab_file_record where index = lv_index.
    delete old_file where index = lv_index. "m3 add
    sort itab_file_record by index.
  endif.
enddo.
perform insert_batch_result_table.
end-of-selection.
*message i058(zpp01) with 'ISPS automate V01 GR process completed.'.
*----------------------------------------------------------------------*
* FORM GET_FILENAME                                                    *
*----------------------------------------------------------------------*
FORM GET_FILENAME.
  file_path = i_path.
  clear:file_name[].

*  CALL FUNCTION 'KCD_FRONT_END_DIRECTORY_READ'
*     EXPORTING
*          I_PATH              = file_path
**     IMPORTING
**          E_PURE_PATH         = L_PURE_PATH
*     TABLES
*          E_DIRECTORY         = L_DIRECTORY
*     EXCEPTIONS
*          DOWNLOAD            = 1
*          UPLOAD              = 2
*          EXECUTE             = 3
*          DIRECTORY_NOT_EXIST = 4
*          DIRECTORY           = 5
*          OTHERS              = 6.
*
*  FILE_NAME[] = L_DIRECTORY[].
*  describe table file_name.
*  if sy-tfill eq 0.
*    stop.
**     message e000(zt) with 'You must enter a upload filename!'.
*  endif.

*M4 add
MOVE '*' TO A_GENERIC_NAME.
translate file_path to lower case.
 CALL 'C_DIR_READ_FINISH'             " just to be sure
    ID 'ERRNO'  FIELD FILE_LIST-ERRNO
    ID 'ERRMSG' FIELD FILE_LIST-ERRMSG.

CALL 'C_DIR_READ_START' ID 'DIR'    FIELD FILE_PATH
                        ID 'FILE'   FIELD A_GENERIC_NAME
                        ID 'ERRNO'  FIELD FILE-ERRNO
                        ID 'ERRMSG' FIELD FILE-ERRMSG.
IF SY-SUBRC <> 0.
*  MESSAGE E204(S1) WITH FILE_LIST-ERRMSG FILE-ERRMSG.
  exit.
ENDIF.

DO.
  CLEAR FILE.
  CALL 'C_DIR_READ_NEXT'
    ID 'TYPE'   FIELD FILE-TYPE
    ID 'NAME'   FIELD FILE-NAME
    ID 'LEN'    FIELD FILE-LEN
    ID 'OWNER'  FIELD FILE-OWNER
    ID 'MTIME'  FIELD FILE-MTIME
    ID 'MODE'   FIELD FILE-MODE
    ID 'ERRNO'  FIELD FILE-ERRNO
    ID 'ERRMSG' FIELD FILE-ERRMSG.
  FILE-DIRNAME = FILE_PATH.
  MOVE SY-SUBRC TO FILE-SUBRC.
  CASE SY-SUBRC.
    WHEN 0.
    WHEN 1.
      EXIT.
    WHEN OTHERS.                     " SY-SUBRC >= 2
      ADD 1 TO ERRCNT.
      IF ERRCNT > 10.
        EXIT.
      ENDIF.
      IF SY-SUBRC = 5.
        MOVE'???' TO FILE-TYPE,
              '???' TO FILE-OWNER,
              '???' TO FILE-MODE.
      ENDIF.
  ENDCASE.
  PERFORM P6_TO_DATE_TIME_TZ(RSTR0400) USING FILE-MTIME
                                             FILE-MOD_TIME
                                             FILE-MOD_DATE.

*  Does the filename contains the requested pattern?
*  IF FILE-NAME(4) = classtype.            "*******************
    MOVE-corresponding FILE TO FILE_NAME.
    APPEND FILE_NAME.
*  ENDIF.                               "*******************
ENDDO.

CALL 'C_DIR_READ_FINISH'
    ID 'ERRNO'  FIELD FILE_LIST-ERRNO
    ID 'ERRMSG' FIELD FILE_LIST-ERRMSG.
IF SY-SUBRC <> 0.
  WRITE: / 'C_DIR_READ_FINISH''SUBRC', SY-SUBRC.
ENDIF.
*
*


*M4 end

  loop at FILE_NAME.
    lv_tabix = sy-tabix.
    SHIFT FILE_NAME-NAME UP TO '.'.
    if FILE_NAME-NAME <> '.txt'.
      delete FILE_NAME index lv_tabix.
    endif.
  ENDLOOP.

ENDFORM.
*----------------------------------------------------------------------*
* FORM GET_DATA_FROM_TXT                                                    *
*----------------------------------------------------------------------*
FORM GET_DATA_FROM_TXT using p_name
                             p_count.
      datacount type i.
      clear:file_itab[].
        concatenate file_path '\' p_name into lv_filename.
*M4 remove
*        CALL FUNCTION 'GUI_UPLOAD'
*          EXPORTING
*            FILENAME                      = lv_filename
*            FILETYPE                      = 'ASC'
*            HAS_FIELD_SEPARATOR           = 'X'
*          "  REPLACEMENT                   = ';'
*          TABLES
*            DATA_TAB                      = file_itab
*          EXCEPTIONS
*            FILE_OPEN_ERROR               = 1
*            FILE_READ_ERROR               = 2
*            NO_BATCH                      = 3
*            GUI_REFUSE_FILETRANSFER       = 4
*            INVALID_TYPE                  = 5
*            NO_AUTHORITY                  = 6
*            UNKNOWN_ERROR                 = 7
*            BAD_DATA_FORMAT               = 8
*            HEADER_NOT_ALLOWED            = 9
*            SEPARATOR_NOT_ALLOWED         = 10
*            HEADER_TOO_LONG               = 11
*            UNKNOWN_DP_ERROR              = 12
*            ACCESS_DENIED                 = 13
*            DP_OUT_OF_MEMORY              = 14
*            DISK_FULL                     = 15
*            DP_TIMEOUT                    = 16
*            OTHERS                        = 17.


*        describe table file_itab.
*        if sy-subrc = 0.
*          loop at file_itab.
*              split file_itab-text_content at ';' into
*                  temp_file_record-budat
*                  temp_file_record-ebeln
*                  temp_file_record-menge
*                  temp_file_record-J_3ASIZ
*                  temp_file_record-LIFNR "temp_file_record-LGORT
*                  temp_file_record-vbeln
*                  temp_file_record-fg .
*
*                if sy-subrc = 4.
*                    raise invalid_file_format.
*                else.
*
**                    concatenate temp_file_record-budat+0(2) '.' temp_file_record-budat+2(2) '.'
**                    temp_file_record-budat+4(4) into temp_file_record-budat.
*
**                    concatenate temp_file_record-budat+4(4) temp_file_record-budat+2(2)
**                       temp_file_record-budat+0(2) into temp_file_record-budat.
*
*
*                    count = strlen( temp_file_record-LIFNR ).
**M1
**                    if count = 4.
**                       concatenate '000000' temp_file_record-LIFNR into temp_file_record-LIFNR.
**                    endif.
**M1 (END)
*                    move-corresponding temp_file_record to itab_file_record.
*                    itab_file_record-filepath = file_path.
*                    itab_file_record-filename = p_name.
*
*                    if temp_file_record-fg ne 'X'.
*                       if file_flag eq 'X'.
*                          file_index = file_index + 1.
*                       endif.
*                       itab_file_record-index = file_index.
*                    else.
*                       itab_file_record-index = p_count.
*                    endif.
*                    append  itab_file_record.
*              endif.
*              clear:file_flag.
*           endloop.
*M4 end

"M4 add
        open dataset lv_filename for input in text mode encoding default.
        check sy-subrc = 0.
        do.
          read dataset lv_filename into file_itab.
          if sy-subrc ne 0.
              exit.
           endif.

           split file_itab-text_content at ';' into
              temp_file_record-budat
              temp_file_record-ebeln
              temp_file_record-menge
              temp_file_record-J_3ASIZ
              temp_file_record-LIFNR "temp_file_record-LGORT
              temp_file_record-vbeln
              temp_file_record-fg .
              if sy-subrc = 4.
                 raise invalid_file_format.
              else.
                  count = strlen( temp_file_record-LIFNR ).
                  move-corresponding temp_file_record to itab_file_record.
                  itab_file_record-filepath = file_path.
                  itab_file_record-filename = p_name.
                  if temp_file_record-fg ne 'X'.
                     if file_flag eq 'X'.
                        file_index = file_index + 1.
                     endif.
                     itab_file_record-index = file_index.
                  else.
                     itab_file_record-index = p_count.
                  endif.
                  append  itab_file_record.
              endif.
              clear:file_flag.
            enddo.

            close dataset lv_filename.
         if itab_file_record[] is initial.
*           message  e058(zpp01) with 'Read file fail or no data in the file ' . "M4 add
           exit.
         endif.
*M4 end
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  create_po1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*remark: In the beginning this BDC exist BUG, sometimes the schdule line item will be overwrittn  or lost.
* so you have to sort it by j_3asize at first.  remark on 2011.08.06
FORM create_po using p_index.
*// create FG PO by bdc
data:  lv_ekorg like ekko-ekorg.
data:  lp_umson(15type c value 'RM06E-UMSON(??)',
       lp_ematn(14type c value 'EKPO-EMATN(??)',
       lp_menge(14type c value 'EKPO-MENGE(??)',
       lp_bstpo(15type c value 'RM06E-BSTPO(??)',
       lp_tcs(19type c value 'RM06E-TCSELFLAG(??)'.
data: lv_tcs_cal(19type c.
data: lv_item(2type n.
*M3 add
data: wa_asize(22)    type c value 'J_3ASZDI-J_3ASIZED(??)',
      wa_menge(18)    type c value 'J_3ASZDI-MENGE(??)',
      wa_kscat(21)    type c value 'J_3ASZDI-J_4KSCAT(??)',
      counter(2)    type n.
data l_index type i.
data wa_page type i.
data wa_page_flag type i.
data wa_etenr type i.
data wa_etenr_inside type i.
data etenr1(4type c.
data etenr2(4type c.
*M3 end
clear:it_po[].
    concatenate p_werks+0(3'A' into lv_ekorg.
sort itab_file_record by index j_3asiz. "M3 add
    read table itab_file_record index 1.

*M3 add
data wa_bedat like RM06E-BEDAT.
wa_bedat = itab_file_record-budat.
concatenate itab_file_record-budat+4(4) itab_file_record-budat+2(2)
   itab_file_record-budat+0(2into wa_bedat.

perform sort_scheduleLine_by_j3asize using p_index. "m4 add

perform bdc_dynpro      using 'SAPMM06E' '0100'.
perform bdc_field       using 'BDC_CURSOR'
                              'RM06E-LGORT'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'EKKO-LIFNR'
                               itab_file_record-lifnr.  "'6090'.
perform bdc_field       using 'RM06E-BSART'
                              'ZPM'.
perform bdc_field       using 'RM06E-BEDAT'
                              itab_file_record-budat . "'07072011'.    "'07.06.2011'.
perform bdc_field       using 'EKKO-EKORG'
                              lv_ekorg.  "'601A'.
perform bdc_field       using 'EKKO-EKGRP'
                              '611'.
perform bdc_field       using 'RM06E-EPSTP'
                              'L'.
perform bdc_field       using 'RM06E-LPEIN'
                              'T'.
perform bdc_field       using 'RM06E-WERKS'
                              p_werks . "'6011'.
perform bdc_field       using 'RM06E-LGORT'
                              p_lgort.  "'9998'.



perform bdc_dynpro      using 'SAPMM06E' '0120'.
perform bdc_field       using 'BDC_CURSOR'
                              'EKPO-WERKS(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'EKPO-EMATN(01)'
                              itab_file_record-matnr . "'6Y1107004-V01'.
perform bdc_field       using 'EKPO-MENGE(01)'
                              gc_ttl.
perform bdc_field       using 'RM06E-EEIND(01)'
                              itab_file_record-budat..
perform bdc_field       using 'RM06E-UMSON(01)'
                              'X'.
perform bdc_dynpro      using 'SAPMM06E' '0120'.
perform bdc_field       using 'BDC_CURSOR'
                              'RM06E-BSTPO(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=J3AE'.
perform bdc_field       using 'RM06E-EBELP'
                              '10'.
perform bdc_field       using 'RM06E-TCSELFLAG(01)'
                              'X'.

counter = 1.
loop at itab_file_record where index = p_index.
  l_index = l_index + 1.
  wa_asize+19(2) = counter.
  wa_menge+15(2) = counter.
  wa_kscat+18(2) = counter.
  if l_index < 8.
    perform bdc_dynpro      using 'SAPLJ3AD' '8000'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '/00'.
    perform bdc_field       using 'BDC_CURSOR'
                                  wa_kscat. "'J_3ASZDI-J_4KSCAT(01)'.
    perform bdc_field       using 'J_3ASZDI-J_3AETENR'
                                  '1'.
    perform bdc_field       using wa_asize  "'J_3ASZDI-J_3ASIZED(01)'
                                  itab_file_record-j_3asiz.
    perform bdc_field       using wa_menge  "'J_3ASZDI-MENGE(01)'
                                  itab_file_record-menge.
    perform bdc_field       using wa_kscat  "'J_3ASZDI-J_4KSCAT(01)'
                                  itab_file_record-vbeln.
  endif.

  wa_page_flag = ( l_index - 8 ) mod 6 .
  if l_index > 7 and wa_page_flag = 0.
    wa_page = ( l_index - 8 ) div 6 . " if wa_page = 0 ,it's the second page , then one add one.
    wa_etenr =  wa_page * 6 + 1.
    wa_etenr_inside = wa_page * 6 + 7.
    etenr1 = wa_etenr.
    etenr2 = wa_etenr_inside.
    condense: etenr1,etenr2.
    perform bdc_dynpro      using 'SAPLJ3AD' '8000'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=P+'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'J_3ASZDI-J_4KSCAT(07)'.
    perform bdc_field       using 'J_3ASZDI-J_3AETENR'
                                  etenr1 . "wa_etenr ."  if wa_page = 1 , then '1'.

    perform bdc_field       using 'BDC_OKCODE'
                                  '=P+'.

    counter = 2.
    wa_asize+19(2) = counter.
    wa_menge+15(2) = counter.
    wa_kscat+18(2) = counter.
  endif.

  if l_index > 7.

    perform bdc_dynpro      using 'SAPLJ3AD' '8000'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '/00'.
    perform bdc_field       using 'BDC_CURSOR'
                                  wa_kscat. "'J_3ASZDI-J_4KSCAT(01)'.
    perform bdc_field       using 'J_3ASZDI-J_3AETENR'
                                  etenr2 ."wa_etenr_inside .  "  7,13,19,25
    perform bdc_field       using wa_asize  "'J_3ASZDI-J_3ASIZED(01)'
                                  itab_file_record-j_3asiz.
    perform bdc_field       using wa_menge  "'J_3ASZDI-MENGE(01)'
                                  itab_file_record-menge.
    perform bdc_field       using wa_kscat  "'J_3ASZDI-J_4KSCAT(01)'
                                  itab_file_record-vbeln.
  endif.

  if l_index < 8.
    wa_etenr_inside = 1.
    etenr2 = 1.
    condense etenr2.
  endif.

  counter =  counter + 1.

endloop.

perform bdc_dynpro      using 'SAPLJ3AD' '8000'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BAC1'.
*must remove  these code ,otherwise it occur error
*the last item
*perform bdc_field       using 'BDC_CURSOR'
*                              wa_kscat.  "  'J_3ASZDI-J_4KSCAT(03)'.
*perform bdc_field       using 'J_3ASZDI-J_3AETENR'
*                              wa_etenr_inside .
* end

perform bdc_dynpro      using 'SAPMM06E' '0120'.
perform bdc_field       using 'BDC_CURSOR'
                              'EKPO-MENGE(01)'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BU'.
perform bdc_field       using 'RM06E-EBELP'
                              '10'.
perform bdc_dynpro      using 'SAPLSPO1' '0300'.
perform bdc_field       using 'BDC_OKCODE'
                              '=YES'.



perform bdc_transaction using 'ME21'.

*M3 end

*M3 remove
*perform bdc_dynpro      using 'SAPMM06E' '0100'.
*perform bdc_field       using 'BDC_CURSOR'
*                              'RM06E-LPEIN'.
*perform bdc_field       using 'BDC_OKCODE'
*                              '/00'.
*perform bdc_field       using 'EKKO-LIFNR'
*                              itab_file_record-lifnr."'6090'.
*perform bdc_field       using 'RM06E-BSART'
*                              'ZPM'.
*perform bdc_field       using 'RM06E-BEDAT'
*                              itab_file_record-budat."sy-datum."'26.11.2010'.
*perform bdc_field       using 'EKKO-EKORG'
*                              lv_ekorg."'601A'.
*perform bdc_field       using 'EKKO-EKGRP'
*                              '611'.
*perform bdc_field       using 'RM06E-EPSTP'
*                              'L'.
*perform bdc_field       using 'RM06E-LPEIN'
*                              ''.
*perform bdc_field       using 'RM06E-WERKS'
*                              p_werks."'6011'.
*perform bdc_field       using 'RM06E-LGORT'
*                               p_lgort."'9998'.
*clear:d_menge.
*loop at itab_file_record where index = p_index.
*    lv_tabix = sy-tabix.
*    lv_item = lv_tabix.
*    d_menge = d_menge + itab_file_record-menge.
*    lp_umson+12(2) = lv_item.
*    lp_ematn+11(2) = lv_item.
*    lp_menge+11(2) = lv_item.
*    lp_bstpo+12(2) = lv_item.
*    lp_tcs+16(2) = lv_item.
*    perform bdc_dynpro      using 'SAPMM06E' '0120'.
*    perform bdc_field       using 'BDC_CURSOR'
*                                  lp_ematn."'RM06E-UMSON(01)'.
*    perform bdc_field       using 'BDC_OKCODE'
*                                  '/00'.
*    if lv_tabix > 1.
*       perform bdc_field       using 'RM06E-EBELP'
*                                     '10'.
*       perform bdc_field       using lv_tcs_cal"'RM06E-TCSELFLAG(01)'
*                                     ''.
*    endif.
*    perform bdc_field       using lp_ematn"'EKPO-EMATN(01)'
*                                  itab_file_record-matnr."'6P1012013-V01'.
*    perform bdc_field       using lp_menge"EKPO-MENGE(01)'
*                                  itab_file_record-menge."'             1000'.
*    perform bdc_field       using lp_umson"'RM06E-UMSON(01)'
*                                  'X'.
*
*
*    perform bdc_dynpro      using 'SAPMM06E' '0120'.
*
*    perform bdc_field       using 'BDC_CURSOR'
*                                  lp_bstpo."'RM06E-BSTPO(01)'.
*
*    perform bdc_field       using 'BDC_OKCODE'
*                                  '=J3AE'.
*    perform bdc_field       using 'RM06E-EBELP'
*                                  '10'.
*    perform bdc_field       using lp_tcs"'RM06E-TCSELFLAG(01)'
*                                  'X'.
*
*    perform bdc_dynpro      using 'SAPLJ3AD' '8000'.
*    perform bdc_field       using 'BDC_OKCODE'
*                                  '=BAC1'.
*    perform bdc_field       using 'BDC_CURSOR'
*                                  'J_3ASZDI-J_3ASIZED(01)'.
*    perform bdc_field       using 'J_3ASZDI-J_3AETENR'
*                                  '1'.
*    perform bdc_field       using 'J_3ASZDI-J_3ASIZED(01)'
*                                  itab_file_record-j_3asiz."'REDM'.
*
*    lv_tcs_cal = lp_tcs.
*endloop.
*    perform bdc_dynpro      using 'SAPMM06E' '0120'.
*    perform bdc_field       using 'BDC_CURSOR'
*                                  'RM06E-EBELP'.
*    perform bdc_field       using 'BDC_OKCODE'
*                                  '=BU'.
*    perform bdc_field       using 'RM06E-EBELP'
*                                  '10'.
*
*    perform bdc_dynpro      using 'SAPLSPO1' '0300'.
*    perform bdc_field       using 'BDC_OKCODE'
*                                  '=YES'.
*    perform bdc_transaction using 'ME21'.
*M3 remove


    if l_subrc eq 0.
       create_po_succ = 'X'.
       describe table messtab.
       read table messtab index sy-tfill.
       it_po-ebeln = messtab-msgv2.

       concatenate 'PO: ' it_po-ebeln 'is created' into it_doc-docno
                   separated by space.
       append it_doc.
       append it_po.
*       perform insert_batch_result_log using itab_file_record-vbeln
*                                             itab_file_record-ebeln
*                                             it_po-ebeln
*                                             it_doc-docno
*                                             itab_file_record-budat
*                                             itab_file_record-fg
*                                             itab_file_record-matnr
*                                             g_ttl
*                                             'X'.
    else.
       read table itab_file_record index 1.
       read table messtab index 1.
       concatenate 'Create Purchase Order is Err (' messtab-msgv2 ')' into err_message.
       perform insert_batch_result_log using itab_file_record-vbeln
                                     itab_file_record-ebeln
                                     it_po-ebeln "M2 add
                                     err_message
                                     itab_file_record-budat
                                     itab_file_record-fg
                                     itab_file_record-matnr
                                     g_ttl
                                     ''.
       exit.
*       message e058(zpp01) with err_message.
    endif.

*    wait up to 1 seconds.
ENDFORM.                    " create_po

*&---------------------------------------------------------------------*
*&      Form  bdc_zme2o
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM bdc_zme2o using p_index.
data:lv_count type i,
     lv_item(5type c value '08/03'.
clear: messtab. refresh messtab. "m3 add
data wa_budat(10type c"M3 add
concatenate sy-datum+6(2'.' sy-datum+4(2'.' sy-datum(4into wa_budat.
perform bdc_dynpro      using 'ZRM06ELLB' '1000'.
perform bdc_field       using 'BDC_CURSOR'
                              'S_BUDAT'.
perform bdc_field       using 'BDC_OKCODE'
                              '=ONLI'.
perform bdc_field       using 'S_LIFNR-LOW'
                              ''.
perform bdc_field       using 'P_CONV'
                              'SY'.
perform bdc_field       using 'P_SCREQ'
                              'X'.
perform bdc_field       using 'P_TPSCR'
                              'X'.
perform bdc_field       using 'P_TPSCP'
                              'X'.
perform bdc_field       using 'P_BATGRP'
                              'X'.

read table it_po index 1.
    perform bdc_field       using 'S_EBELN'
                                  it_po-ebeln.
    perform bdc_field       using 'S_LGORT'
                                  s_lgort.
read table itab_file_record index 1.
    perform bdc_field       using 'S_FR'
                                  itab_file_record-fg.
    perform bdc_field       using 'S_BUDAT'
                                 itab_file_record-budat.

perform bdc_dynpro      using 'SAPMSSY0' '0120'.
perform bdc_field       using 'BDC_OKCODE'
                              '=EXPA'.
perform bdc_dynpro      using 'SAPMSSY0' '0120'.
*perform bdc_field       using 'BDC_CURSOR'
*                              '10/05'.
perform bdc_field       using 'BDC_OKCODE'
                              '=MALL'.

perform bdc_dynpro      using 'SAPMSSY0' '0120'.
perform bdc_field       using 'BDC_OKCODE'
                              '=WABU'.
perform bdc_transaction using 'ZME2O'.

   describe table messtab.
   read table it_po index 1.
   read table messtab index sy-tfill.
*   it_po-ebeln = messtab-msgv2.  "M3 remove
if l_subrc eq 0 and it_po-ebeln ne ''.

   zme2o_succ = 'X'"M3 add
   concatenate it_po-ebeln 'is posted' into it_doc-docno
               separated by space.
   append it_doc.
*   perform insert_batch_result_log using itab_file_record-vbeln
*                                     itab_file_record-ebeln
*                                     it_po-ebeln
*                                     it_doc-docno
*                                     itab_file_record-budat
*                                     itab_file_record-matnr
*                                     d_menge
*                                     'X'.
else.
       clear zme2o_succ . "m3 add
       read table itab_file_record index 1.
       read table MESSTAB with key MSGTYP = 'E'.
*       concatenate 'ZME2O Error (' messtab-msgv1 messtab-msgv2
*                     messtab-msgv3 messtab-msgv4 ')'  into err_message.

       move 'ZME2O Error' to err_message.

*       concatenate 'ZME2O Error (' itab_file_record-ebeln ')' into err_message.
*       perform reverse_scstock."M2 add
       perform insert_batch_result_log using itab_file_record-vbeln
                                       itab_file_record-ebeln
                                       it_po-ebeln "M2 add
                                       err_message
                                       itab_file_record-budat
                                       itab_file_record-fg
                                       itab_file_record-matnr
                                       g_ttl
                                       ''.
       exit.
*       message e058(zpp01) with err_message.
endif.
ENDFORM.                    " bdc_zme2o

*&---------------------------------------------------------------------*
*&      Form  bdc_migo
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*Pay attention to this migo BDC, the grid value may be wrong,  remark by daniel 2011.08.07
FORM bdc_migo using P_index.
data: lv_take(18type c value 'GOITEM-TAKE_IT(??)'.
data: lv_take_cal(18type c.
data: lv_num(2type n.
data: lv_date(10type c.
data wa_date(10type c.
clear: messtab. refresh messtab. "m3 add

concatenate sy-datum+6(2'.' sy-datum+4(2'.' sy-datum+0(4into lv_date.
*m4 add
concatenate itab_file_record-budat+0(2'.' itab_file_record-budat+2(2'.'
   itab_file_record-budat+4(4into wa_date.
*m4 end
*-------------------

    perform bdc_dynpro      using 'SAPLMIGO' '0001'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=MIGO_OK_HEADER_OPEN'.

    perform bdc_dynpro      using 'SAPLMIGO' '0001'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=MIGO_OK_DETAIL_OPEN'.

    perform bdc_dynpro      using 'SAPLMIGO' '0001'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=MIGO_OK_ACTION'.
    perform bdc_field       using 'GODYNPRO-ACTION'
                                  'A01'.

    perform bdc_dynpro      using 'SAPLMIGO' '0001'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=MIGO_OK_REFDOC'.
    perform bdc_field       using 'GODYNPRO-REFDOC'
                                  'R01'.

    perform bdc_dynpro      using 'SAPLMIGO' '0001'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=OK_GO'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'GODYNPRO-PO_NUMBER'.
    perform bdc_field       using 'GODEFAULT_TV-BWART'
                                  '101'.

    perform bdc_dynpro      using 'SAPLMIGO' '1300'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=OK_TIP_GO'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'G_TIP_DONT_SHOW_AGAIN'.

    perform bdc_transaction using 'MIGO'.

    read table it_po index 1.
    perform bdc_dynpro      using 'SAPLMIGO' '0001'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=OK_GO'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'GODYNPRO-PO_NUMBER'.
    perform bdc_field       using 'GODYNPRO-PO_NUMBER'
                                  it_po-ebeln."'6190000569'.
    perform bdc_field       using 'GODYNPRO-PO_WERKS'
                                  p_werks."'18.12.2010'.
    perform bdc_field       using 'GOHEAD-BLDAT'
                                  lv_date."'18.12.2010'.
    read table itab_file_record index 1.
    perform bdc_field       using 'GOHEAD-BUDAT'
                                  wa_date .  " itab_file_record-budat."'18.12.2010'.
    perform bdc_field       using 'GOHEAD-LFSNR'
                                  itab_file_record-EBELN.
    perform bdc_field       using 'GOHEAD-WEVER'
                                  '3'.

    loop at itab_file_record where index = P_index.

      perform bdc_dynpro      using 'SAPLMIGO' '0001'.
      perform bdc_field       using 'BDC_OKCODE'
                                    '=MIGO_OK_NEXT_IT'.
      perform bdc_field       using 'BDC_CURSOR'
                                    'GODYNPRO-DETAIL_TAKE'.
      perform bdc_field       using 'GODYNPRO-DETAIL_TAKE'
                                    'X'.
    endloop.

    perform bdc_dynpro      using 'SAPLMIGO' '0001'.
    perform bdc_field       using 'BDC_OKCODE'
                                  '=OK_POST'.
    perform bdc_field       using 'BDC_CURSOR'
                                  'GODYNPRO-DETAIL_TAKE'.
    perform bdc_field       using 'GODYNPRO-DETAIL_TAKE'
                                  'X'.

    perform bdc_transaction using 'MIGO'.

    read table messtab with key msgtyp = 'S'
                                msgid  = 'MIGO'
                                msgnr  = '012'.
    if sy-subrc eq 0.
       migo_succ = 'X'"M3 add
       concatenate 'GR NO. ' messtab-msgv1 ' is created' into it_doc-docno
                   separated by space.
       append it_doc.
       perform insert_batch_result_log using itab_file_record-vbeln
                                     itab_file_record-ebeln
                                     it_po-ebeln
                                     it_doc-docno
                                     itab_file_record-budat
                                     itab_file_record-fg
                                     itab_file_record-matnr
                                     g_ttl
                                     'X'.
*M6 remark
*       perform consume_all_prodMat.  "M3 add
*       perform consume_all_scMat. "M3 add
*M6 end
    else.
       clear migo_succ . "m3 add
       read table itab_file_record index 1.
       move 'Goods Receipt Error'  to err_message.
       perform reverse_scstock."M2 add
       perform insert_batch_result_log using itab_file_record-vbeln
                                     itab_file_record-ebeln
                                     it_po-ebeln "M2 add
                                     err_message
                                     itab_file_record-budat
                                     itab_file_record-fg
                                     itab_file_record-matnr
                                     g_ttl
                                     ''.
       exit.
       "message e058(zpp01) with err_message.
    endif.
ENDFORM.                    " bdc_migo




*----------------------------------------------------------------------*
*        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.
*  IF FVAL <> NODATA.
    CLEAR BDCDATA.
    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    APPEND BDCDATA.
*  ENDIF.
ENDFORM.

*----------------------------------------------------------------------*
*        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'".'S'. "L --> local
    WA_PARAMS-DEFSIZE = 'X'.
    CALL TRANSACTION TCODE USING BDCDATA OPTIONS FROM WA_PARAMS
                   MESSAGES INTO MESSTAB.
    L_SUBRC = SY-SUBRC.
      read table MESSTAB with key MSGTYP = 'E'.
      if sy-subrc eq 0.
           SELECT SINGLE * FROM T100 WHERE SPRSL = MESSTAB-MSGSPRA
                                      AND   ARBGB = MESSTAB-MSGID
                                      AND   MSGNR = MESSTAB-MSGNR.
            IF SY-SUBRC = 0.
              L_MSTRING = T100-TEXT.
              IF L_MSTRING CS '&1'.
                REPLACE '&1' WITH MESSTAB-MSGV1 INTO L_MSTRING.
                REPLACE '&2' WITH MESSTAB-MSGV2 INTO L_MSTRING.
                REPLACE '&3' WITH MESSTAB-MSGV3 INTO L_MSTRING.
                REPLACE '&4' WITH MESSTAB-MSGV4 INTO L_MSTRING.
              ELSE.
                REPLACE '&' WITH MESSTAB-MSGV1 INTO L_MSTRING.
                REPLACE '&' WITH MESSTAB-MSGV2 INTO L_MSTRING.
                REPLACE '&' WITH MESSTAB-MSGV3 INTO L_MSTRING.
                REPLACE '&' WITH MESSTAB-MSGV4 INTO L_MSTRING.
              ENDIF.
              CONDENSE L_MSTRING.
.
*              message e058(zpp01) with l_mstring(250).

            ENDIF.
      endif.
  REFRESH BDCDATA.
ENDFORM.

FORM check_ZPP_FGGR_SO_CAL using p_index.
*M5 add
  databegin of lt_diff_mat occurs 0,
      matnr like mara-matnr,
    end of lt_diff_mat.
*M5 end
  data: lv_clabs like mchb-clabs.
  clear:it_j_3abdsi,lv_clabs,it_mchb[].
  read table it_po index 1.
  read table itab_file_record index 1.

    select distinct b~lgort a~matnr a~j_3asize b~clabs into it_mchb from  mchb as b
                inner join j_3abdsi as a on a~matnr = b~matnr and a~j_3asize = b~j_3asize"lv_clabs
                where b~j_4kscat = itab_file_record-vbeln
                  and a~aufnr = it_po-ebeln
                  and b~lgort = s_lgort
                  and b~clabs > 0.
       collect  it_mchb.
    endselect.
*"M3 add
    clear old_mchb[].
    select * into table old_mchb from mchb
        where werks = p_werks
          and lgort = s_lgort
          and j_4kscat = itab_file_record-vbeln.
*M3 end
    loop at itab_file_record where index = p_index.
       if bflag = 'X'.
          exit.
       endif.
    endloop.   "M3 add
    read table itab_file_record with key index = p_index. "M3 add
    clear:it_j_3abdsi[],lv_clabs. "M3 add
       select matnr j_3asize erfmg rsnum   "M3 add rsnum
         into corresponding fields of table it_j_3abdsi
         from j_3abdsi
        where aufnr = it_po-ebeln
          and posnr = itab_file_record-ebelp.
*           and j_3asize = itab_file_record-j_3asiz.
       describe table it_j_3abdsi.
       if sy-tfill ne 0.
         loop at  it_j_3abdsi.
            clear: it_mchb.
            read table it_mchb with key matnr = it_j_3abdsi-matnr
                                        j_3asize = it_j_3abdsi-j_3asize
                                        lgort = s_lgort.
            lv_tabix = sy-tabix.
            if sy-subrc = 0.
              b_bool = 'X'.
              if it_mchb-clabs > 0.   "if it_mchb-clabs ne 0. "M3 modify
                if itab_file_record-fg ne 'X'.
*                   perform bom_to_order."M2 add
                   if it_j_3abdsi-erfmg > it_mchb-clabs.
                          select single * from ZPP_FGGR_SO_CAL where vbeln = itab_file_record-vbeln
                                               and matnr = it_j_3abdsi-matnr
                                               and werks = p_werks.
                      if sy-subrc eq 0.
                         perform update_data using it_mchb-clabs.
                         it_mchb-clabs = 0.
                         modify it_mchb index lv_tabix.
                      else.
                          check_stock_error = 'X'"m3 add
*M5 add
                          lt_diff_mat-matnr = it_j_3abdsi-matnr.
                          collect lt_diff_mat.
*M5 end
*M5 remark
*                          concatenate 'Deficit of Mat: ' it_j_3abdsi-matnr into  err_message.
*                          perform insert_batch_result_log using itab_file_record-vbeln
*                                      itab_file_record-ebeln
*                                      it_po-ebeln "M2 add
*                                      err_message "M3 modify
*                                      itab_file_record-budat
*                                      itab_file_record-fg
*                                      itab_file_record-matnr
*                                      g_ttl   "d_menge M3 modify
*                                      ''.
*                          bflag = 'X'.
*                          exit.
*M5 end
                          "message e058(zpp01) with c.
                      endif.
                   else"7470 stock >= po req. qty
                        it_mchb-clabs = it_mchb-clabs - it_j_3abdsi-erfmg.
                        modify it_mchb index lv_tabix.
                   endif.
                else.  " if final GR
                  "M3 ADD
                    if it_j_3abdsi-erfmg > it_mchb-clabs.
                         perform update_data using it_mchb-clabs.
                         it_mchb-clabs = 0.
                         modify it_mchb index lv_tabix.
                   else.
                        it_mchb-clabs = it_mchb-clabs - it_j_3abdsi-erfmg.
                        modify it_mchb index lv_tabix.
                   endif.
                  "M3 end
*M3 remove
*                    perform update_data using it_mchb-clabs.
*                    it_mchb-clabs = 0.
*                    modify it_mchb index lv_tabix.
*m3 remove
                endif.
              else.  " if 7470stock qty <= 0 then....
                "M3 add
                  if itab_file_record-fg ne 'X'.
                      select single * from ZPP_FGGR_SO_CAL where vbeln = itab_file_record-vbeln
                                          and matnr = it_j_3abdsi-matnr
                                          and werks = p_werks.
                      if sy-subrc  = 0.
                         perform delete_item_data.
                      else.
                          check_stock_error = 'X'"m3 add
*M5 add
                          lt_diff_mat-matnr = it_j_3abdsi-matnr.
                          collect lt_diff_mat.
*M5 end

*                          concatenate 'Deficit of Mat: ' it_j_3abdsi-matnr into  err_message. "M4 add
*                          perform insert_batch_result_log using itab_file_record-vbeln
*                                          itab_file_record-ebeln
*                                          it_po-ebeln "M2 add
*                                          err_message
*                                          itab_file_record-budat
*                                          itab_file_record-fg
*                                          itab_file_record-matnr
*                                          g_ttl   "d_menge M3 modify
*                                          ''.
*                          bflag = 'X'.
*                          exit.
                      endif.
                   else.
                      perform delete_item_data.
                   endif.
                "M3 end
*                perform delete_item_data. "M3 remove
              endif.
            else.  " 7070 stock not exists the material

                        select single * from ZPP_FGGR_SO_CAL where vbeln = itab_file_record-vbeln
                                                    and matnr = it_j_3abdsi-matnr
                                                    and werks = p_werks.
                        if sy-subrc eq 0 or itab_file_record-fg eq 'X'.
                          perform delete_item_data.
                        else.
                            "perform delete_po using it_po-ebeln.
                           check_stock_error = 'X'"m3 add
*M5 add
                          lt_diff_mat-matnr = it_j_3abdsi-matnr.
                          collect lt_diff_mat.
*M5 end
*                            concatenate 'Deficit of Mat: ' it_j_3abdsi-matnr into  err_message. "M4 add
*                            perform insert_batch_result_log using itab_file_record-vbeln
*                                              itab_file_record-ebeln
*                                              it_po-ebeln "M2 add
*                                              err_message
*                                              itab_file_record-budat
*                                              itab_file_record-fg
*                                              itab_file_record-matnr
*                                              g_ttl   "d_menge M3 modify
*                                              ''.
*                            bflag = 'X'.
*                            exit.
                            "message e058(zpp01) with 'Error in Process GR'.
                        endif.

            endif.
         endloop.
       endif.
*M5 add
    if  check_stock_error = 'X'.
      sort lt_diff_mat .
      delete adjacent duplicates from lt_diff_mat.
      loop at lt_diff_mat.
        if sy-tabix = 1.
          concatenate 'Deficit of Mat: ' lt_diff_mat-matnr into  err_message.
        else.
          concatenate err_message ',' lt_diff_mat-matnr into err_message.
        endif.
      endloop.

      perform insert_batch_result_log using itab_file_record-vbeln
                        itab_file_record-ebeln
                        it_po-ebeln "M2 add
                        err_message
                        itab_file_record-budat
                        itab_file_record-fg
                        itab_file_record-matnr
                        g_ttl   "d_menge M3 modify
                        ''.
      bflag = 'X'.
      exit.
    endif.
*M5 end

*    endloop. "m3 remove
ENDFORM.                    " get_grid

*&---------------------------------------------------------------------*
*&      Form  insert_batch_result_log
*&---------------------------------------------------------------------*
FORM insert_batch_result_log using  p_vbeln
                                    p_ebeln
                                    p_po
                                    p_msg
                                    p_budat
                                    p_zfinal
                                    p_matnr
                                    p_menge
                                    p_type.
   data: i_len type i.
   data: d_Str(10type c.
   condense p_budat NO-GAPS.
   i_len = strlen( p_budat ).
   if i_len = 8.
     concatenate p_budat+4(4)  p_budat+2(2)
                    p_budat+0(2into d_Str.
   else.
     concatenate p_budat+6(4)  p_budat+3(2)
                    p_budat+0(2into d_Str.
   endif.
   if p_type eq 'X'.
      it_suc-vbeln = p_vbeln.
      it_suc-ebeln = p_ebeln.
      it_suc-fg_po = p_po.

     " lt_fggr_batch-mblnr = itab_file_record-manter.
      "lt_fggr_batch-zz_flag = p_flag.
      it_suc-zz_msg = p_msg.
      it_suc-budat =  d_Str.
      it_suc-zfinal = p_zfinal.
      it_suc-matheader = p_matnr.
*      lt_fggr_batch-matnr = lt_matnr-matnr.
*      lt_fggr_batch-J_3ASIZ = itab_file_record-J_3ASIZ .
      it_suc-menge = p_menge.
*      lt_fggr_batch-meins = itab_file_record-MENGE.

      it_suc-uname = sy-uname.
      it_suc-udate = sy-datum.
      it_suc-utime = sy-uzeit.
      append it_suc.
   else.
      it_err-vbeln = p_vbeln.
      it_err-ebeln = p_ebeln.
      it_err-fg_po = p_po.
      it_err-zz_msg = p_msg.
      it_err-budat = d_Str.
      it_err-zfinal = p_zfinal.
      it_err-matheader = p_matnr.
      it_err-menge = p_menge.
      it_err-uname = sy-uname.
      it_err-udate = sy-datum.
      it_err-utime = sy-uzeit.
      append it_err.
      clear:file_itab_cache[].
*M3 modify , change 'itab_file_record' to 'old_file '
      loop at old_file where index = lv_index.
        old_file-budat+2(1) = ''.
        old_file-budat+5(1) = ''.
        CONDENSE old_file-budat NO-GAPS.
        concatenate old_file-budat ';'
                    old_file-ebeln ';'
                    old_file-menge ';'
                    old_file-J_3ASIZ ';'
                    old_file-LIFNR ';'
                    old_file-vbeln into file_itab_cache-text_content.
        if old_file-fg eq 'X'.
           concatenate file_itab_cache-text_content ';' old_file-fg into file_itab_cache-text_content.
        endif.
        append file_itab_cache.
        concatenate old_file-filepath '\' old_file-filename into lv_fromName.
        concatenate old_file-filepath '\NONCOMPLETE\' old_file-filename into lv_toName.
      endloop.
      move_err = 'X'clear move_succ.
      perform move_file tables file_itab_cache
                        using lv_fromName
                              move_err.

      sort itab_file_record by index.
      delete it_suc where ebeln = p_ebeln.
      if create_po_succ = 'X'"m4 add on 2011.08.06
        perform reverse_poRequir using p_po . "M2 add
        perform reverse_po using p_po. "M2 add
      endif.
      delete itab_file_record where index = lv_index.
      delete old_file where index = lv_index. "M3 add

   endif.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  insert_batch_result_log
*&---------------------------------------------------------------------*
FORM insert_batch_result_table.
    describe table it_suc.
    if sy-tfill ne 0.
      insert ZPP_FGGR_LOG_CAL from table it_suc.
    endif.
    describe table it_err.
    if sy-tfill ne 0.
      insert ZPP_FGGR_LOG_CAL from table it_err.
    endif.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  update_data
*&---------------------------------------------------------------------*
FORM update_data using p_value.
    clear: j_3abdsi , resb."M3 add
    select single * from j_3abdsi
                  where matnr = it_j_3abdsi-matnr
                    and aufnr = it_po-ebeln
                    and posnr = itab_file_record-ebelp
                    and j_3asize = it_j_3abdsi-j_3asize
                    and rsnum = it_j_3abdsi-rsnum.  "M3 add
    if sy-subrc = 0.
       j_3abdsi-erfmg = p_value.
       j_3abdsi-nomng = p_value.
       j_3abdsi-menge = p_value.
       j_3abdsi-aedat = sy-datum.
       j_3abdsi-utime = sy-uzeit.
       j_3abdsi-uname = sy-uname.
       update j_3abdsi.
       select single * from resb where rsnum = j_3abdsi-rsnum
                                   and matnr = it_j_3abdsi-matnr
                                   and j_3asize = it_j_3abdsi-j_3asize.
         if sy-subrc = 0.
            resb-erfmg = p_value.
            resb-nomng = p_value.
            resb-bdmng = p_value.
            update resb.
         endif.
    endif.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  delete_item_data
*&---------------------------------------------------------------------*
FORM delete_item_data.
data: lv_rsnum like j_3abdsi-rsnum.
   select single rsnum into lv_rsnum from j_3abdsi
                  where matnr = it_j_3abdsi-matnr
                    and aufnr = it_po-ebeln
                    and posnr = itab_file_record-ebelp
                    and j_3asize = it_j_3abdsi-j_3asize
                    and rsnum = it_j_3abdsi-rsnum.  "M3 add..
   if sy-subrc eq 0.
       delete from j_3abdsi where matnr = it_j_3abdsi-matnr
                              and aufnr = it_po-ebeln
                              and posnr = itab_file_record-ebelp
                              and j_3asize = it_j_3abdsi-j_3asize
                              and rsnum = it_j_3abdsi-rsnum.  "M3 add.

       delete from resb where     rsnum = lv_rsnum
                         and matnr = it_j_3abdsi-matnr
                         and j_3asize = it_j_3abdsi-j_3asize
                         and ebeln = it_po-ebeln.
   endif.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  DELETE_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*FORM DELETE_PO using p_po.
*   delete from j_3abdsi where aufnr = p_po.
*   delete from resb where ebeln = p_po.
*   delete from ekko where ebeln = p_po.
*   delete from ekpo where ebeln = p_po.
*ENDFORM.                    " DELETE_PO
*&---------------------------------------------------------------------*
*&      Form  ADD_EBELP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LV_INDEX  text
*----------------------------------------------------------------------*
FORM ADD_EBELP  USING    P_LV_INDEX.
    clear:lv_ebelp.
    lv_ebelp = lv_ebelp + 10"M3 add
    loop at itab_file_record where index = P_LV_INDEX.
      lv_tabix = sy-tabix.
*      lv_ebelp = lv_ebelp + 10. "M3 remark
      TRANSLATE itab_file_record-vbeln to upper case.
      concatenate itab_file_record-vbeln '-V01' into itab_file_record-matnr.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' "料號前面加0”
           EXPORTING
             input = lv_ebelp
           IMPORTING
             output = lv_ebelp.
      itab_file_record-ebelp = lv_ebelp.
      modify  itab_file_record index lv_tabix.
    endloop.
ENDFORM.                    " ADD_EBELP

*&---------------------------------------------------------------------*
*&      Form  MOVE_FILE
*&---------------------------------------------------------------------*
FORM MOVE_FILE tables p_table
               using p_delete_file
                     p_flag.
*M4 add
  data: IN_FILE(255TYPE C,
        OUT_FILE(255TYPE C.

  DATA: CMND LIKE SXPGCOLIST-NAME,
        PARM LIKE SXPGCOLIST-PARAMETERS .
  DATABEGIN OF OUT_DATA OCCURS 50.
          INCLUDE STRUCTURE BTCXPM.
  DATAEND   OF OUT_DATA .
  DATA: RTN_CH.

*< 1 >* Call LHA Command

* create system command
  CMND =   'ZMOVE'.  "<-- system command name. Refer "SM69"
********************************************************
* Configuration                                        *
* operating system command : "cmd"                     *
* parameters for operating system command : "/c move"  *
********************************************************

* MOVE need parameter "<source file> _ <target file>"



  move p_delete_file TO OUT_FILE.
  if move_succ = 'X'.
    concatenate i_path '\COMPLETE\' into IN_FILE.
  endif.
  if move_err = 'X'.
    concatenate i_path '\NONCOMPLETE\' into IN_FILE.
  endif.

  CONCATENATE OUT_FILE IN_FILE INTO PARM SEPARATED BY SPACE.




*call system command
  CLEAR RTN_CH.
  CALL FUNCTION 'SXPG_CALL_SYSTEM'
       EXPORTING
            COMMANDNAME                = CMND
            ADDITIONAL_PARAMETERS      = PARM
*       IMPORTING
*            STATUS                     = RTN_CH
       TABLES
            EXEC_PROTOCOL              = OUT_DATA
       EXCEPTIONS
            NO_PERMISSION              = 01
            COMMAND_NOT_FOUND          = 02
            PARAMETERS_TOO_LONG        = 03
            SECURITY_RISK              = 04
            WRONG_CHECK_CALL_INTERFACE = 05
            PROGRAM_START_ERROR        = 06
            PROGRAM_TERMINATION_ERROR  = 07
            X_ERROR                    = 08
            PARAMETER_EXPECTED         = 09
            TOO_MANY_PARAMETERS        = 10
            ILLEGAL_COMMAND            = 11.


*M4 end.


*   data: sFileName_c TYPE STRING,
*         sFileName_d TYPE RLGRAP-FILENAME.
*   sFileName_c = p_create_file.
*   sFileName_d = p_delete_file.
*   CALL FUNCTION 'GUI_DOWNLOAD'
*        EXPORTING
*          filename =  sFileName_c
*          filetype = 'DAT'
*          codepage = '4310'
*          replacement = '#'
*          write_field_separator = 'X'
*          write_bom = 'X'
*        TABLES
*          data_tab =  p_table.
*
*   CALL FUNCTION 'GUI_DELETE_FILE'
*        EXPORTING
*          FILE_NAME = sFileName_d
*        EXCEPTIONS
*          FAILED    = 1
*          OTHERS    = 2.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  GET_MSLB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*M2 add
FORM GET_MSLB .
  data wa_matnr like mslb-matnr.
  clear sc_stock[].
  read table itab_file_record  index 1.
  wa_matnr = itab_file_record-vbeln.
  concatenate wa_matnr '%' into wa_matnr.
  select * into table sc_stock
    from mslb
    where matnr like wa_matnr
      and werks eq   p_werks
*      and sobkz eq 'O'
      and j_4kscat eq itab_file_record-vbeln.
ENDFORM.                    " GET_MSLB
*&---------------------------------------------------------------------*
*&      Form  REVERSE_SCSTOCK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*M2 add
FORM REVERSE_SCSTOCK .
*M6 add
data wa_mblnr like mseg-mblnr.
data wa_mjahr like mseg-mjahr.
data wa_budat like mkpf-budat.
data: ret like BAPIRET2 occurs 0 with header line.

select single mblnr mjahr into (wa_mblnr,
       wa_mjahr) from mseg
       where ebeln = it_po-ebeln
         and bwart = '541'.
if sy-subrc = 0.
    select single budat into wa_budat from mkpf
      where mblnr = wa_mblnr
        and mjahr = wa_mjahr.

    CALL FUNCTION 'BAPI_GOODSMVT_CANCEL'
      EXPORTING
        MATERIALDOCUMENT          = wa_mblnr
        MATDOCUMENTYEAR           = wa_mjahr
        GOODSMVT_PSTNG_DATE       = wa_budat
      TABLES
        RETURN                    = ret.

    read table ret with key type = 'E'.
    if sy-subrc eq 0.
*       message w999(zmrr) with ret-message.
    else.
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*       destination cn_rfc
        EXPORTING
          WAIT       = 'X'.
    endif.

    wait up to 1 seconds.

*    concatenate ta_grid-mblnr 'is reversed' into it_doc-docno
*                separated by space.
*    append it_doc.

endif.



*M6 end


*M6 remark
*  data wa_matnr like mslb-matnr.
*  field-symbols <f1> type mslb.
*  wa_matnr = itab_file_record-vbeln.
*  concatenate wa_matnr '%' into wa_matnr.
*  if sc_stock[] is not initial.
*    loop at sc_stock assigning <f1>.
*      select single * from mslb
*        where matnr = <f1>-matnr
*          and werks = <f1>-werks
*          and charg = <f1>-charg
*          and sobkz = <f1>-sobkz
*          and lifnr = <f1>-lifnr.
*      if sy-subrc = 0.
*        update mslb from <f1>.
*      else.
*        insert mslb from <f1>.
*      endif.
*    endloop.
*  else.
*    delete from mslb where matnr like wa_matnr
**                       and sobkz eq 'O'
*                       and werks eq p_werks
*                       and j_4kscat eq itab_file_record-vbeln.
*  endif.
**M3 add
*  if old_mchb[] is not initial.
*    modify  mchb from TABLE old_mchb.
*  else.
*    delete mchb from table old_mchb.
*  endif.
**M3 end
*M6 end
ENDFORM.                    " REVERSE_SCSTOCK
*&---------------------------------------------------------------------*
*&      Form  REVERSE_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_PO  text
*----------------------------------------------------------------------*
*M2 add
FORM REVERSE_PO  USING    P_PO.
data: ret         like BAPIRET2 occurs 0 with header line.
data: poitem      like BAPIMEPOITEM occurs 0 with header line.
data: POITEMX     like BAPIMEPOITEMX occurs 0 with header line.
data  poheader    like bapimepoheader occurs 0 with header line.
data  poheaderx    like bapimepoheader occurs 0 with header line.
data: cn_rfc(10type c.

     check p_po is not initial.

*     wait up to 1 seconds.

     poheader-po_number = p_po.
     append poheader.

     poheaderx-po_number =  'X'.
     append poheaderx.

*     POITEM-PO_ITEM = EKPO-EBELP.
*     POITEM-DELETE_IND = 'L'.
*     APPEND POITEM.
*
*     POITEMX-PO_ITEM = EKPO-EBELP.
*     POITEMX-PO_ITEMX = 'X'.
*     POITEMX-DELETE_IND = 'X'.
*     APPEND POITEMX.

    loop at itab_file_record where index = lv_index.
      POITEM-PO_ITEM = itab_file_record-EBELP.
      POITEM-DELETE_IND = 'L'.
      APPEND POITEM.
      clear  POITEM.

       POITEMX-PO_ITEM = itab_file_record-EBELP.
       POITEMX-PO_ITEMX = 'X'.
       POITEMX-DELETE_IND = 'X'.
       APPEND POITEMX.
       CLEAR  POITEMX.
    endloop.


    CALL FUNCTION 'BAPI_PO_CHANGE'
    destination cn_rfc
      EXPORTING
        PURCHASEORDER                = P_PO

*        POHEADER      = POHEADER
*        POHEADERX     = POHEADERX
      TABLES
        RETURN                       = RET
        POITEM                       = POITEM
        POITEMX                      = POITEMX.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    destination cn_rfc
      EXPORTING
        WAIT       = 'X'.

    wait up to 1 seconds.

    concatenate p_po 'is deleted' into it_doc-docno
                separated by space.
    append it_doc.
ENDFORM.                    " REVERSE_PO
*&---------------------------------------------------------------------*
*&      Form  GET_POREQUIRE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*M2 add
FORM GET_POREQUIRE .
  clear: old_j3abdsi[], old_resb[].
  data wa_matnr like mslb-matnr.
  read table itab_file_record index 1.
  wa_matnr = itab_file_record-vbeln.
  concatenate wa_matnr '%' into wa_matnr.
  select * into table old_j3abdsi
    from j_3abdsi
    where aufnr eq it_po-ebeln
      and matnr like wa_matnr
      and werks eq p_werks.

  select * into table old_resb
    from resb
    where ebeln eq it_po-ebeln
      and matnr like wa_matnr
      and werks eq p_werks.
ENDFORM.                    " GET_POREQUIRE
*&---------------------------------------------------------------------*
*&      Form  REVERSE_POREQUIR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_PO  text
*----------------------------------------------------------------------*
*M2 add
FORM REVERSE_POREQUIR  USING    P_P_PO.
  check old_j3abdsi[] is not initial.
  modify j_3abdsi from table old_j3abdsi.
  modify resb from table old_resb.
  clear: old_j3abdsi[], old_resb[].
ENDFORM.                    " REVERSE_POREQUIR
*&---------------------------------------------------------------------*
*&      Form  BOM_TO_ORDER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM BOM_TO_ORDER .
*  data order_unit like mara-bstme.
*  data bom_unit like mara-meins.
*  data:wa_umrez like marm-umrez,
*       wa_umren like marm-umren.
*  data: wa_input type p,
*        wa_output type p.
*
*  select single bstme meins into (order_unit, bom_unit)
*    from mara
*    where matnr eq it_j_3abdsi-matnr.
*  check order_unit is not initial .
*  if order_unit ne bom_unit.
*    CALL FUNCTION 'MATERIAL_UNIT_CONVERSION'
*    EXPORTING
*      INPUT                        = wa_input
*      MATNR                        = it_j_3abdsi-matnr
*      MEINH                        = order_unit
*    IMPORTING
*      OUTPUT                       = wa_output
*      UMREN                        = wa_umren
*      UMREZ                        = wa_umrez
*    EXCEPTIONS
*      CONVERSION_NOT_FOUND = 1
*      INPUT_INVALID        = 2
*      MATERIAL_NOT_FOUND   = 3
*      MEINH_NOT_FOUND      = 4
*      MEINS_MISSING        = 5
*      NO_MEINH             = 6
*      OUTPUT_INVALID       = 7
*      OVERFLOW             = 8
*      OTHERS               = 9.
*
*    it_j_3abdsi-erfmg = it_j_3abdsi-erfmg * WA_UMREZ / WA_UMREN.
*
*  else.
*      wa_umren = 1.
*      wa_umrez = 1.
*  endif.

ENDFORM.                    " BOM_TO_ORDER
*&---------------------------------------------------------------------*
*&      Form  GET_TTL_POQTY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*M3
FORM GET_TTL_POQTY .
clear g_ttl.
loop at itab_file_record where index = lv_index.
  g_ttl = g_ttl + itab_file_record-menge.
endloop.
gc_ttl = g_ttl.
ENDFORM.                    " GET_TTL_POQTY
*&---------------------------------------------------------------------*
*&      Form  CONSUME_ALL_PRODMAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
*M3
FORM CONSUME_ALL_PRODMAT .
data: wa_mchb like line of it_mchb.
check itab_file_record-fg eq 'X'.
loop at it_mchb into wa_mchb.
  select single * from mchb
    where matnr = wa_mchb-matnr
      and werks = p_werks
      and lgort = wa_mchb-lgort
      and j_3asize = wa_mchb-j_3asize.
  if sy-subrc = 0.
    mchb-clabs = 0.
    modify mchb .
  endif.
endloop.
ENDFORM.                    " CONSUME_ALL_PRODMAT
*&---------------------------------------------------------------------*
*&      Form  CONSUME_ALL_SCMAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CONSUME_ALL_SCMAT .
  data wa_matnr like mslb-matnr.
  wa_matnr = itab_file_record-vbeln.
  if itab_file_record-fg eq 'X'.
    concatenate wa_matnr '%' into wa_matnr.
    select single * from mslb
      where matnr like wa_matnr
        and werks = p_werks
        and j_4kscat eq itab_file_record-vbeln.
    if sy-subrc = 0.
      delete from mslb where matnr like wa_matnr
*                         and sobkz eq 'O'
                         and werks eq p_werks
                         and j_4kscat eq itab_file_record-vbeln.
    endif.
  endif.

ENDFORM.                    " CONSUME_ALL_SCMAT
*&---------------------------------------------------------------------*
*&      Form  CREATE_BAPI_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LV_INDEX  text
*----------------------------------------------------------------------*
FORM CREATE_BAPI_PO  USING    P_INDEX.
data: wa_po_header like BAPIEKKOC,
      wa_po_no     like ekko-ebeln,
      wa_item_no   like ekpo-ebelp,
      wa_po_item_add like BAPIEKPOA    occurs 0 with header line,
      wa_po_header_add like BAPIEKKOA.

data: wa_po_items like /AFS/BAPIPOITEM   occurs 0 with header line,
      wa_po_item_sch like /AFS/BAPISCHED occurs 0 with header line
,
      wa_po_ass like BAPIEKKN occurs 0 with header line,
      wa_return    like BAPIRETURN,
      wa_return1   like BAPIRETURN occurs 0 with header line.
data: l_seq like sy-tabix.
data: lv_ekorg(4type c.  "like ekko-ekorg.
data wa_bedat(8TYPE C ." like RM06E-BEDAT.
data wa_count(4type n.


wa_bedat = itab_file_record-budat.
concatenate p_werks+0(3'A' into lv_ekorg.

clear: wa_po_header.
refresh: wa_po_items, wa_return1, wa_po_item_sch.

wa_count = 1.

    loop at itab_file_record where index = p_index.
        l_seq = sy-tabix.
        if l_seq = 1.
          concatenate  itab_file_record-budat+4(4)  itab_file_record-budat+2(2)
                       itab_file_record-budat+0(2)  into wa_bedat.
          wa_po_header-vendor    = itab_file_record-lifnr.
          shift wa_po_header-vendor right deleting trailing space.
          overlay wa_po_header-vendor with '0000000000' only space.
          wa_po_header-doc_cat  = 'F'.
          wa_po_header-doc_type = 'ZPM'.
          wa_po_header-co_code  = '6010'.
          wa_po_header-doc_date = wa_bedat.
          wa_po_header-purch_org = lv_ekorg. "601A
          wa_po_header-pur_group = '611'.
*          wa_po_header_add-CREATED_BY = sy-uname.


          wa_po_items-po_item = '00010'.
          wa_po_items-pur_mat = itab_file_record-matnr.
          wa_po_items-DISP_QUAN = g_ttl.
          wa_po_items-DELIV_DATE = wa_bedat.
          wa_po_items-plant   = p_werks.
          wa_po_items-PO_PRICE = 'X'.
          wa_po_items-stock_cat = itab_file_record-vbeln.
          wa_po_items-STORE_LOC = p_lgort.
*          wa_po_items-afs_flag = 'X'.
          append wa_po_items.
        endif.



        wa_po_item_sch-po_item = '00010'.
        wa_po_item_sch-serial_no = wa_count.
        wa_po_item_sch-quantity = itab_file_record-menge.
        wa_po_item_sch-deliv_date = wa_bedat.
        wa_po_item_sch-SCHE_LINE_NO = wa_count.
        wa_po_item_sch-ordered_qty = g_ttl.
*        wa_po_item_sch-req_cat = itab_file_record-vbeln.
        wa_po_item_sch-stock_cat = itab_file_record-vbeln.
*        if itab_file_record-j_3asize is not initial.
*          wa_po_item_sch-sch_size = itab_file_record-j_3asize.
*        else.
          wa_po_item_sch-sch_size = itab_file_record-j_3asiz.
*        endif.
*        wa_po_item_sch-CREATE_IND = 'X'.
        append wa_po_item_sch.
        wa_count = wa_count + 1.

    endloop.

    clear wa_return.
    CALL FUNCTION '/AFS/BAPI_PO_CREATE'
      EXPORTING
        PO_HEADER                        = wa_po_header
        PO_HEADER_ADD_DATA               = wa_po_header_add
*        HEADER_ADD_DATA_RELEVANT         = 'X'
*        ITEM_ADD_DATA_RELEVANT           = 'X'
      IMPORTING
        PURCHASEORDER                    = wa_po_no
        RETURN                           = wa_return
      TABLES
        RETURN1                          = wa_return1
        PO_ITEMS                         = wa_po_items
        PO_ITEM_ADD_DATA                 = wa_po_item_add
        PO_ITEM_SCHEDULES                = wa_po_item_sch.

    read table wa_return1 with key type = 'E'.
    if sy-subrc ne 0.
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          WAIT       = 'X'.
       wait up to 1 seconds.


       it_po-ebeln = wa_return-MESSAGE_V2 .
       concatenate it_po-ebeln 'is created' into it_doc-docno
                   separated by space.
       append it_doc.
       append it_po.

       create_po_succ = 'X'.
       read table itab_file_record index 1.
*       perform insert_batch_result_log using itab_file_record-vbeln
*                                             itab_file_record-ebeln
*                                             wa_return-MESSAGE_V2     "PO number
*                                             wa_return-message   "CMT Finish Goods PO created under the number 6190001236
*                                             itab_file_record-budat
*                                             itab_file_record-fg
*                                             itab_file_record-matnr
*                                             g_ttl
*                                             'X'.


    else.
       concatenate 'Create Purchase Order is Err (' wa_return1-message ')' into err_message.
       read table itab_file_record index 1.
       perform insert_batch_result_log using itab_file_record-vbeln
                                     itab_file_record-ebeln
                                     it_po-ebeln "M2 add
                                     err_message
                                     itab_file_record-budat
                                     itab_file_record-fg
                                     itab_file_record-matnr
                                     g_ttl
                                     ''.
    endif.


ENDFORM.                    " CREATE_BAPI_PO
*&---------------------------------------------------------------------*
*&      Form  SORT_SCHEDULELINE_BY_J3ASIZE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_INDEX  text
*----------------------------------------------------------------------*
FORM SORT_SCHEDULELINE_BY_J3ASIZE  USING    P_INDEX.
data l_tabix type i.
data: wa_lentry(20type c,
      wa_grid_value like J_3ABDBS-J_3ASIZE,
      wa_mat  like mara-matnr,
      wa_aentx8  type J_3AENTX8,
      wa_inseam(40type c,
      wa_size type j_3asize.

loop at itab_file_record where index = P_INDEX.
  l_tabix = sy-tabix.
*  CALL FUNCTION 'Z_CONVERSION_VALUE_FROM'
*    EXPORTING
*      MATNR             = itab_file_record-MATNR
*      GRID              = itab_file_record-J_3ASIZ
*    IMPORTING
*      J_3ASIZE  = itab_file_record-J_3asize.
*
      CLEAR J_3APGEN.

      CALL FUNCTION 'J_3A_CONVERT_SIZE'
        EXPORTING
             I_MATNR             = itab_file_record-MATNR
             I_J3ASIZE           = itab_file_record-J_3ASIZ
             I_INPUT_CONVERSION  = 'X'
        IMPORTING
             O_J3AKORD1          = J_3APGEN-J_3AKORD1
             O_J3AKORD2          = J_3APGEN-J_3AKORD2
             O_J3AKORD3          = J_3APGEN-J_3AKORD3
             O_J3ASIZE           = J_3APGEN-J_3AKORDX
        EXCEPTIONS
             ID_UNKNOWN          = 1
             NO_VALUES_AVAILABLE = 2
             NO_GRID_DETERMINED  = 3
             NO_CONV_MAINTAINED  = 4
             SIZE_NOT_VALID      = 5
             OTHERS              = 6.

      itab_file_record-j_3asize = J_3APGEN-J_3AKORDX .
      itab_file_record-O_J3AKORD1  = J_3APGEN-J_3AKORD1. "COLOR  001
      itab_file_record-O_J3AKORD2  = J_3APGEN-J_3AKORD2. " STREAM  2,4
      itab_file_record-O_J3AKORD3  = J_3APGEN-J_3AKORD3. "SIZE   M,L

      shift itab_file_record-O_J3AKORD2 right deleting trailing ''.
      overlay itab_file_record-O_J3AKORD2 with '00000000'.

      shift itab_file_record-O_J3AKORD3 right deleting trailing ''.
      overlay itab_file_record-O_J3AKORD3 with '00000000'.


  modify  itab_file_record index l_tabix .
endloop.
sort itab_file_record by index O_J3AKORD1 O_J3AKORD2 O_J3AKORD3 .
ENDFORM.                    " SORT_SCHEDULELINE_BY_J3ASIZE
*&---------------------------------------------------------------------*
*&      Form  BAPI_MIGO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LV_INDEX  text
*----------------------------------------------------------------------*
FORM BAPI_MIGO  USING    P_INDEX.
  data:it_gdmvt_head  LIKE  BAPI2017_GM_HEAD_01,
       it_afs_item    LIKE  /AFS/BAPI2017_GM_ITEM_CREATE
                           occurs 0 with header line,
       it_g_ret like    BAPIRET2 OCCURS 0 WITH HEADER LINE,
       lv_mdoc  like BAPI2017_GM_HEAD_RET-MAT_DOC,
       lv_year  like BAPI2017_GM_HEAD_RET-DOC_YEAR.
  data:it_gdmvt_code  LIKE  BAPI2017_GM_CODE  value '01'.
  data:it_return      LIKE  BAPIRET2 occurs 0 with header line.
  data:lv_tabix like sy-tabix,
       wa_mblnr like mseg-mblnr,
       wa_menge like mseg-menge.
data:begin of it_mseg occurs 0,
     mblnr like mseg-mblnr,
     mjahr like mseg-mjahr,
     zeile like mseg-zeile,
     menge like mseg-menge,
    end of it_mseg.
       refresh: it_afs_item .

data wa_return    like BAPIRETURN.

data wa_budat(8type c.
data wa_count(4type n.

wa_count = 1.
read table it_po index 1.
loop at itab_file_record where index = p_index.
   if sy-tabix = 1.
     concatenate  itab_file_record-budat+4(4)  itab_file_record-budat+2(2)
         itab_file_record-budat+0(2)  into wa_budat.
    "header
     it_gdmvt_head-pstng_date = wa_budat. "  posting date
     it_gdmvt_head-doc_date  = sy-datum.   "docmunt date
     it_gdmvt_head-ref_doc_no = itab_file_record-ebeln. "Delivery note
    endif.

     "Item
      it_afs_item-material  =  itab_file_record-matnr .  " Material Code
      it_afs_item-plant      = p_werks .     "Plant
      it_afs_item-stge_loc   = p_lgort.   "Storage Location
      if itab_file_record-j_3asize is not initial.
        it_afs_item-grid_value = itab_file_record-j_3asize ."Grid Value  should '00226 L'  not 00226L
      else.
        it_afs_item-grid_value = itab_file_record-j_3asiz ."Grid Value
      endif.
      it_afs_item-stock_cat  = itab_file_record-vbeln . "Stock Category
      it_afs_item-po_number  = it_po-ebeln . "Purchase order
      it_afs_item-po_item    = '00010' .    "Po item

      it_afs_item-sched_line_sku = wa_count . "Po sche line
*      it_afs_item-vendrbatch = goodsmvt_data-vendr_batch ." Vendor Batch
      it_afs_item-move_type  = '101' .  "Movement type
*      it_afs_item-val_type    = goodsmvt_data-val_type .  "Valuation type
*      it_afs_item-batch       = goodsmvt_data-rfid_batch .  "RFID Batch
      it_afs_item-entry_qnt = itab_file_record-menge.     " Quantity
      it_afs_item-mvt_ind    = 'B'.
      append it_afs_item.

      wa_count = wa_count + 1.
endloop.

       CALL FUNCTION '/AFS/BAPI_GOODSMVT_CREATE'
          EXPORTING
            GOODSMVT_HEADER             = it_gdmvt_head
            GOODSMVT_CODE               = it_gdmvt_code
          IMPORTING
            MATERIALDOCUMENT            = lv_mdoc
            MATDOCUMENTYEAR             = lv_year

          TABLES
            AFS_GOODSMVT_ITEM           = it_afs_item
            RETURN                      = it_return.

        read table it_return with key type = 'E'.
            if sy-subrc eq 0.
               clear migo_succ . "m3 add
               read table itab_file_record index 1.
               concatenate 'Goods Receipt Error ( ' it_return-message ')' into err_message.
               perform reverse_scstock."M2 add
               perform insert_batch_result_log using itab_file_record-vbeln
                                             itab_file_record-ebeln
                                             it_po-ebeln "M2 add
                                             err_message
                                             itab_file_record-budat
                                             itab_file_record-fg
                                             itab_file_record-matnr
                                             g_ttl
                                             ''.
            else.
                CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                   EXPORTING
                     WAIT = 'X'.
                wait up to 1 seconds.
                 migo_succ = 'X'"M3 add
                 concatenate 'GR NO. ' lv_mdoc ' is created' into it_doc-docno
                             separated by space.
                 append it_doc.
                 perform insert_batch_result_log using itab_file_record-vbeln
                                               itab_file_record-ebeln
                                               it_po-ebeln
                                               it_doc-docno
                                               itab_file_record-budat
                                               itab_file_record-fg
                                               itab_file_record-matnr
                                               g_ttl
                                               'X'.
*M6 remark
*                 perform consume_all_prodMat.  "M3 add
*                 perform consume_all_scMat. "M3 add
*M6 end
            endif.
ENDFORM.                    " BAPI_MIGO
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章