Skip to Content
avatar image
Former Member

regarding bdc of MB1B

HI,

can anyone send me the bdc of mb1b.

Thanks

dinesh

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Oct 24, 2007 at 11:17 AM

    hi,

    REPORT z_sd_stock_transfer NO STANDARD PAGE HEADING

    LINE-COUNT 65

    LINE-SIZE 200

    MESSAGE-ID zz.

    &----


    • S T R U C T U R E D E C L A R A T I O N S *

    &----


    TYPES: BEGIN OF x_data,

    locid(30),

    tr_loc(30),

    docid(18),

    doc_dt(10),

    prdid(20),

    batch(10),

    qty(10),

    uom(3),

    END OF x_data.

    TYPES: BEGIN OF x_out,

    locid(3),

    tr_loc(3),

    docid(18),

    doc_dt(10),

    prdid(20),

    batch(10),

    qty(10),

    mesg(60),

    END OF x_out.

    TYPES: BEGIN OF x_material,

    X_prdid(20), " X Prd id

    matnr TYPE matnr, " Material (SAP)

    END OF x_material.

    TYPES: BEGIN OF x_werks,

    name2(30), " Location

    werks TYPE werks_ext, " Plant

    END OF x_werks.

    &----


    • I N T E R N A L T A B L E D E C L A R A T I O N S *

    &----


    DATA: it_data TYPE STANDARD TABLE OF x_data WITH HEADER LINE.

    DATA: it_out TYPE STANDARD TABLE OF x_out WITH HEADER LINE.

    DATA: it_matnr TYPE STANDARD TABLE OF x_material WITH HEADER LINE. " Material Info

    DATA: it_werks TYPE STANDARD TABLE OF x_werks WITH HEADER LINE." Plant

    DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.

    DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.

    &----


    • V A R I A B L E S D E C L A R A T I O N S *

    &----


    DATA : v_correct TYPE i,

    v_error TYPE i,

    v_total TYPE i,

    l_subrc LIKE sy-subrc.

    DATA: e_group_opened.

    DATA: hdate TYPE sy-datum.

    &----


    • U S E R I N P U T S S C R E E N *

    &----


    SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE text-001.

    PARAMETERS: p_sname(12) DEFAULT 'SALESORDER',

    p_uname(12) DEFAULT sy-uname,

    p_mode DEFAULT 'N',

    p_file TYPE rlgrap-filename OBLIGATORY.

    SELECTION-SCREEN END OF BLOCK blk.

    &----


    • I N I T I A L I Z A T I O N *

    &----


    INITIALIZATION.

    hdate = sy-datum - 1.

    CONCATENATE 'STN' sy-datum INTO p_sname.

    CONDENSE p_sname NO-GAPS.

    &----


    • S E L E C T I O N S C R E E N *

    &----


    AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

    PERFORM get_file.

    AT SELECTION-SCREEN ON p_mode.

    CASE p_mode.

    WHEN 'A' OR 'E' OR 'N'.

    WHEN OTHERS.

    MESSAGE e001(zz) WITH text-e01.

    ENDCASE.

    &----


    • S t a r t o f S e l e c t i o n *

    &----


    START-OF-SELECTION.

    PERFORM get_upload.

    IF NOT it_data[] IS INITIAL.

    PERFORM get_update. " Do Stock Transfer

    ELSE.

    MESSAGE i001(zz) WITH text-001.

    ENDIF.

    &----


    • E n d o f S e l e c t i o n *

    &----


    END-OF-SELECTION.

    IF NOT it_data[] IS INITIAL.

    PERFORM close_group.

    PERFORM get_write.

    ENDIF.

    &----


    *& Form get_file

    &----


    • Get File name

    ----


    FORM get_file .

    CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

    EXPORTING

    program_name = syst-repid

    mask = '*'

    CHANGING

    file_name = p_file

    EXCEPTIONS

    mask_too_long = 1

    OTHERS = 2.

    IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

    ENDIF.

    ENDFORM. " get_file

    &----


    *& Form get_upload

    &----


    • to upload the .CSV file

    ----


    FORM get_upload .

    DATA: l_file TYPE string,

    l_index LIKE sy-tabix.

    CLEAR: it_data, it_data[], it_matnr, it_werks,

    it_out, it_out[], it_matnr[], it_werks[].

    l_file = p_file.

    CALL FUNCTION 'GUI_UPLOAD'

    EXPORTING

    filename = l_file

    filetype = 'ASC'

    has_field_separator = 'X'

    TABLES

    data_tab = it_data

    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.

    IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

    ELSE.

    SORT it_data BY locid tr_loc.

    *- Get material from X material

    SELECT

    X_prdid

    matnr

    FROM zX_master

    INTO TABLE it_matnr FOR ALL ENTRIES IN it_data

    WHERE X_prdid = it_data-prdid.

    IF sy-subrc = 0.

    SORT it_matnr BY X_prdid matnr.

    *- Get the plant from location

    SELECT name2

    werks

    INTO CORRESPONDING FIELDS OF TABLE it_werks

    FROM t001w FOR ALL ENTRIES IN it_data

    WHERE name2 = it_data-locid.

    *- By Transfer location

    SELECT name2

    werks

    APPENDING TABLE it_werks

    FROM t001w FOR ALL ENTRIES IN it_data

    WHERE name2 = it_data-tr_loc.

    IF sy-subrc = 0.

    SORT it_werks BY name2 werks.

    DELETE ADJACENT DUPLICATES FROM it_werks COMPARING ALL FIELDS.

    ENDIF.

    ENDIF.

    LOOP AT it_data.

    l_index = sy-tabix.

    READ TABLE it_matnr WITH KEY X_prdid = it_data-prdid BINARY SEARCH.

    IF sy-subrc = 0.

    it_data-prdid = it_matnr-matnr. " matnr

    ENDIF.

    MODIFY it_data INDEX l_index TRANSPORTING prdid.

    ENDLOOP.

    ENDIF.

    ENDFORM. " get_upload

    **----


    • Close the group

    **----


    FORM close_group.

    IF e_group_opened = 'X'.

    CALL FUNCTION 'BDC_CLOSE_GROUP'.

    IF sy-subrc EQ 0.

    WRITE:/5 'Session Closed Successfully'(008).

    ELSE.

    WRITE:/5 'Unable to Close the session : return code'(009), sy-subrc.

    ENDIF.

    ENDIF.

    ENDFORM. "close_group

    &----


    *& Form get_update

    &----


    • Do Stock Transfer

    ----


    FORM get_update .

    DATA: l_chr(3),

    l_val(2) TYPE n,

    l_index LIKE sy-tabix,

    l_itemno(2) TYPE n,

    l_meinh LIKE marm-meinh,

    l_matnr LIKE mara-matnr,

    l_flg,

    l_str(80).

    DATA: l_in_qty LIKE vbap-zmeng,

    l_meins LIKE mara-meins,

    l_factor LIKE marm-umrez,

    l_qty LIKE vbap-zmeng.

    LOOP AT it_data.

    l_index = sy-tabix.

    AT NEW tr_loc.

    READ TABLE it_data INDEX l_index.

    PERFORM bdc_dynpro USING 'SAPMM07M' '0400'.

    PERFORM bdc_field USING 'MKPF-BLDAT' it_data-doc_dt.

    PERFORM bdc_field USING 'MKPF-BUDAT' it_data-doc_dt.

    PERFORM bdc_field USING 'RM07M-BWARTWA' '301'.

    READ TABLE it_werks WITH KEY name2 = it_data-locid BINARY SEARCH.

    IF sy-subrc = 0.

    PERFORM bdc_field USING 'RM07M-WERKS' it_werks-werks.

    ENDIF.

    PERFORM bdc_field USING 'RM07M-LGORT' '2000'.

    PERFORM bdc_field USING 'XFULL' 'X'.

    PERFORM bdc_field USING 'RM07M-WVERS3' 'X'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '/00'.

    CLEAR l_itemno.

    l_itemno = '01'.

    ENDAT.

    PERFORM bdc_dynpro USING 'SAPMM07M' '0421'.

    *- Get To Plant (From To location)

    CLEAR it_werks.

    READ TABLE it_werks WITH KEY name2 = it_data-tr_loc BINARY SEARCH.

    IF sy-subrc = 0.

    ENDIF.

    **- UOM

    • IF l_chr = '521' OR l_chr = '522' OR l_chr = '511'.

    • l_meinh = 'CAS'.

    • ELSE.

    • l_meinh = 'INN'.

    • ENDIF.

    CLEAR: l_chr, l_meinh.

    l_chr = it_data-prdid+10(3).

    *- About To plant

    IF it_data-tr_loc = '123' OR it_data-tr_loc = 'HOF'.

    IF l_chr = '521' OR l_chr = '522'.

    it_werks-werks = '1002'. " Storage Plant

    ELSE.

    it_werks-werks = '1003'. " Storage Plant

    ENDIF.

    ENDIF.

    IF l_flg IS INITIAL.

    PERFORM bdc_field USING 'MSEGK-UMLGO' '2000'.

    PERFORM bdc_field USING 'MSEGK-UMWRK' it_werks-werks.

    l_flg = 'X'.

    ENDIF.

    CLEAR l_str.

    CONCATENATE 'MSEG-MATNR(' l_itemno ')' INTO l_str.

    CONDENSE l_str NO-GAPS.

    PERFORM bdc_field USING l_str it_data-prdid.

    *- Call FM to get new qty based on SAP UOM

    CLEAR: l_meins, l_qty, l_in_qty, l_matnr.

    l_in_qty = it_data-qty.

    l_matnr = it_data-prdid.

    CALL FUNCTION 'Z_GET_QTY_FROM_UOM'

    EXPORTING

    matnr = l_matnr

    in_meins = it_data-uom

    in_qty = l_in_qty

    IMPORTING

    out_meins = l_meins

    quantity = l_qty.

    IF sy-subrc = 0.

    it_data-qty = l_qty.

    it_data-uom = l_meins.

    ENDIF.

    CLEAR l_str.

    CONCATENATE 'MSEG-ERFMG(' l_itemno ')' INTO l_str.

    CONDENSE l_str NO-GAPS.

    PERFORM bdc_field USING l_str it_data-qty.

    CLEAR l_str.

    CONCATENATE 'MSEG-ERFME(' l_itemno ')' INTO l_str.

    CONDENSE l_str NO-GAPS.

    PERFORM bdc_field USING l_str l_meinh.

    CLEAR l_str.

    CONCATENATE 'MSEG-CHARG(' l_itemno ')' INTO l_str.

    CONDENSE l_str NO-GAPS.

    PERFORM bdc_field USING l_str it_data-batch.

    PERFORM bdc_field USING 'DKACB-FMORE' 'X'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '/00'.

    PERFORM bdc_dynpro USING 'SAPLKACB' '0002'.

    • PERFORM bdc_field USING 'DKACB-FMORE' 'X'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '=ENTE'.

    PERFORM bdc_dynpro USING 'SAPLKACB' '0002'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '=ENTE'.

    l_itemno = l_itemno + 1.

    *- If number of items are greater than 4,go for nxt page

    CLEAR l_val.

    l_val = l_itemno MOD 5.

    IF l_val = '00'.

    PERFORM bdc_dynpro USING 'SAPMM07M' '0421'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '=NLE'.

    l_itemno = '01'.

    PERFORM bdc_dynpro USING 'SAPLKACB' '0002'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '=ENTE'.

    ENDIF.

    *- Populate the output table

    CLEAR it_out.

    it_out-locid = it_data-locid.

    it_out-tr_loc = it_data-tr_loc.

    it_out-docid = it_data-docid.

    it_out-doc_dt = it_data-doc_dt.

    it_out-prdid = it_data-prdid.

    it_out-batch = it_data-batch.

    it_out-qty = it_data-qty.

    APPEND it_out.

    CLEAR it_out.

    AT END OF tr_loc.

    READ TABLE it_data INDEX l_index.

    PERFORM bdc_dynpro USING 'SAPMM07M' '0421'.

    PERFORM bdc_field USING 'DKACB-FMORE' 'X'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '=BU'.

    PERFORM bdc_dynpro USING 'SAPLKACB' '0002'.

    PERFORM bdc_field USING 'BDC_OKCODE'

    '=ENTE'.

    *- Call the transaction

    PERFORM bdc_transaction USING 'MB1B'.

    IMPORT l_subrc FROM MEMORY ID 'L_SUBRC'.

    IF l_subrc = 0.

    v_correct = v_correct + 1.

    ENDIF.

    ENDAT.

    ENDLOOP.

    ENDFORM. " get_update

    &----


    *& Form get_write

    &----


    • text

    ----


    FORM get_write .

    WRITE:/ 'Total Number of Records :'(003), v_total COLOR 1.

    WRITE:/ 'Total Correct Records :'(004), v_correct COLOR 3.

    WRITE:/ 'Total Error Records :'(005), v_error COLOR 6.

    SKIP 3.

    IF NOT it_out[] IS INITIAL.

    WRITE:/5 'Locid'(002),

    10 'Tfr.Locid'(006),

    15 'DocumentID'(007),

    38 'Doc.Date'(014),

    55 'Material'(015),

    70 'Batch'(016),

    85 'Qty'(017),

    100 'Message'(018).

    ENDIF.

    SKIP 1.

    LOOP AT it_out.

    WRITE:/5 it_out-locid,

    10 it_out-tr_loc,

    15 it_out-docid,

    38 it_out-doc_dt,

    55 it_out-prdid,

    70 it_out-batch,

    85 it_out-qty,

    100 it_out-mesg.

    ENDLOOP.

    ENDFORM. " get_write

    **----


    • Start new transaction

    **----


    FORM bdc_transaction USING tcode.

    DATA:

    l_mesg TYPE string.

    DATA: l_subrc LIKE sy-subrc.

    DATA: l_msg_id LIKE t100-arbgb,

    l_msg_no LIKE t100-msgnr,

    l_msg_var1 LIKE balm-msgv1,

    l_msg_var2 LIKE balm-msgv2,

    l_msg_var3 LIKE balm-msgv3,

    l_msg_var4 LIKE balm-msgv4.

    REFRESH messtab.

    CALL TRANSACTION tcode USING bdcdata

    MODE p_mode

    UPDATE 'S'

    MESSAGES INTO messtab.

    l_subrc = sy-subrc.

    EXPORT l_subrc TO MEMORY ID 'L_SUBRC'.

    *- Populate the output table

    • CLEAR it_out.

    *

    • it_out-locid = it_data-locid.

    • it_out-tr_loc = it_data-tr_loc.

    • it_out-docid = it_data-docid.

    • it_out-doc_dt = it_data-doc_dt.

    • it_out-prdid = it_data-prdid.

    • it_out-batch = it_data-batch.

    • it_out-qty = it_data-qty.

    *- Build the message

    LOOP AT messtab.

    IF messtab-msgid = 'VL' AND messtab-msgnr = '311' AND messtab-msgtyp = 'S'.

    EXIT.

    ENDIF.

    ENDLOOP.

    IF sy-subrc = 0.

    l_msg_id = messtab-msgid.

    l_msg_no = messtab-msgnr.

    l_msg_var1 = messtab-msgv1.

    l_msg_var2 = messtab-msgv2.

    l_msg_var3 = messtab-msgv3.

    l_msg_var4 = messtab-msgv4.

    CALL FUNCTION 'MESSAGE_PREPARE'

    EXPORTING

    language = 'E'

    msg_id = l_msg_id

    msg_no = l_msg_no

    msg_var1 = l_msg_var1

    msg_var2 = l_msg_var2

    msg_var3 = l_msg_var3

    msg_var4 = l_msg_var4

    IMPORTING

    msg_text = l_mesg

    EXCEPTIONS

    function_not_completed = 1

    message_not_found = 2

    OTHERS = 3.

    IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

    ELSEIF sy-subrc = 0.

    • l_mesg = messtab-msgv2.

    • CONDENSE l_mesg.

    • CONCATENATE 'Stock Transfered' l_mesg 'created successfully' INTO l_mesg.

    it_out-mesg = l_mesg.

    APPEND it_out.

    CLEAR it_out.

    ENDIF.

    ENDIF.

    IF l_subrc <> 0.

    v_error = v_error + 1.

    IF e_group_opened = ' '.

    CALL FUNCTION 'BDC_OPEN_GROUP'

    EXPORTING

    client = sy-mandt

    group = p_sname

    user = p_uname

    keep = ''

    holddate = hdate.

    IF sy-subrc EQ 0.

    WRITE:/5 'Session'(012), p_sname ,' Created Successfully'(011).

    e_group_opened = 'X'.

    ELSE.

    WRITE:/5 'Unable to Create the session'(013), p_sname ,

    '. return code'(010), sy-subrc.

    ENDIF.

    ENDIF.

    CALL FUNCTION 'BDC_INSERT'

    EXPORTING

    tcode = tcode

    TABLES

    dynprotab = bdcdata.

    ENDIF.

    REFRESH bdcdata.

    CLEAR bdcdata.

    ENDFORM. "bdc_transaction

    **----


    • Start new screen

    **----


    FORM bdc_dynpro USING program dynpro.

    CLEAR bdcdata.

    bdcdata-program = program.

    bdcdata-dynpro = dynpro.

    bdcdata-dynbegin = 'X'.

    APPEND bdcdata.

    ENDFORM. "bdc_dynpro

    **----


    • Insert field

    **----


    FORM bdc_field USING fnam fval.

    CLEAR bdcdata.

    bdcdata-fnam = fnam.

    bdcdata-fval = fval.

    APPEND bdcdata.

    ENDFORM. "bdc_field

    Add comment
    10|10000 characters needed characters exceeded