SAP MM BAPI MATERIAL

From SapWiki
Revision as of 22:08, 4 April 2020 by WikiSysop (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
*----------------------------------------------------------------------*
***INCLUDE 
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  SEARCH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SEARCH .
 data lt_stpo type TABLE OF stpo.
 data ls_stpo like LINE OF lt_stpo.
 data ld_f.
 data ld_l type i.

 clear: mast,
        knmt,
        kna1-name1,
        makt,
        gd_maktx,
        t001w.

 select single * from kna1 where KUNNR = kna1-kunnr.

 select SINGLE * from t001w where werks = marc-werks.

 loop at gt_comp into gs_comp where matnr is NOT INITIAL.
   SELECT    * APPENDING TABLE lt_stpo FROM  STPO
          WHERE  IDNRK  = gs_comp-MATNR
          AND    MENGE  = gs_comp-menge.
   if sy-subrc <> 0.
* debe encontrarse para todos los comp
     ld_f = co_yes.
     exit.
   endif.

 ENDLOOP.

 case ld_f.
   when co_yes.

   when co_no.
     sort lt_stpo by STLNR.
     delete ADJACENT DUPLICATES FROM lt_stpo COMPARING STLNR.

     ld_l = lines( lt_stpo ).
     if ld_l = 1.
       read TABLE lt_stpo into ls_stpo index 1.
       select single * from mast where stlnr = ls_stpo-stlnr
                           and werks  = marc-werks.
       if sy-subrc = 0.
         select SINGLE * from makt where matnr = mast-matnr
                                     and spras = sy-langu.

         select single * from KNMT where matnr = mast-matnr
                                       and kunnr = kna1-kunnr.
         if sy-subrc = 0.
           select SINGLE maktx into gd_maktx from makt where matnr = knmt-matnr
                         and spras = sy-langu.
         endif.
       endif.

     endif.

 ENDCASE.

ENDFORM.                    " SEARCH
*&---------------------------------------------------------------------*
*&      Form  UPDATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM UPDATE .

 select single * from kna1 where KUNNR = kna1-kunnr.
 select SINGLE * from t001w where werks = marc-werks.

 loop at gt_comp into gs_comp where matnr is NOT INITIAL.
   gd_tabix = sy-tabix.
   select SINGLE maktx into gs_comp-maktx from makt
                       where matnr = gs_comp-matnr
                          and spras = sy-langu.
   if sy-subrc = 0.
     modify gt_comp from gs_comp INDEX gd_tabix.
   endif.

 ENDLOOP.

ENDFORM.                    " UPDATE

*&---------------------------------------------------------------------*
*&      Form  f_crear_material
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM f_crear_material .

 PERFORM fill_data.

 CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
 EXPORTING
   headdata             = wa_headdata
   clientdata             = wa_clientdata_new
   clientdatax          = wa_clientdatax
   SALESDATA            = wa_salesdata_new
   SALESDATAx           = wa_salesdatax
 IMPORTING
   RETURN               = wa_return
 TABLES
   materialdescription  = gt_bapi_makt
   taxclassifications   = gt_mlan          "Tax Data
   unitsofmeasure       = gt_bapi_marm     "Units of measure
   unitsofmeasurex      = gt_bapi_marmx    "Checkbox for Units of measure
   returnmessages       = gt_returnmessages
*      materiallongtext     = it_bapi_mltx
   .

*  if p_test <> 'X'.
 IF wa_return-type <> 'E'.
   CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*      EXPORTING
*        wait   = abap_on
     IMPORTING
       return = wa_return_commit.
 ENDIF.
* endif.

ENDFORM.                    " crear_material

*&---------------------------------------------------------------------*
*&      Form  get_matnr
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->O_MATNR    text
*----------------------------------------------------------------------*
form get_matnr CHANGING o_matnr.
 data: gv_bapireturn1 type bapireturn1,
       gt_bapimatnr like table of BAPIMATINR,
       gs_bapimatnr LIKE LINE OF  gt_bapimatnr.

 CALL FUNCTION 'BAPI_MATERIAL_GETINTNUMBER'
   EXPORTING
     material_type    = '1'
     industry_sector  = 'HAWA'
     required_numbers = 1
   IMPORTING
     return           = gv_bapireturn1
   TABLES
     material_number  = gt_bapimatnr.
 IF gv_bapireturn1-type EQ 'S'.
   READ TABLE gt_bapimatnr INTO gs_bapimatnr INDEX 1.
   IF sy-subrc EQ 0.
     o_matnr = gs_bapimatnr-material.
   ENDIF.
 ENDIF.
ENDFORM.                    "get_matnr
*&---------------------------------------------------------------------*
*&      Form  fill_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form fill_data.
 data gs_bapi_makt like LINE OF gt_bapi_makt.
 data gs_bapi_marm like LINE OF gt_bapi_marm.
 data gs_bapi_marmx like LINE OF gt_bapi_marmx.
 data gs_mlan like LINE OF gt_mlan.

 clear: wa_headdata,
        wa_clientdata_new,
        wa_clientdatax,
        wa_salesdata_new,
        wa_salesdatax,
        gt_bapi_makt[],
        gt_bapi_marm[],
        gt_bapi_marmx[],
        gt_mlan[].

 perform get_matnr CHANGING  wa_headdata-material .
 wa_headdata-ind_sector         =  '1'.
 wa_headdata-matl_type          =  'HAWA'.

 wa_headdata-basic_view         =  co_yes.
 wa_headdata-sales_view         =  co_yes.
 wa_headdata-purchase_view      =  co_yes.
 wa_headdata-mrp_view            =  co_yes.
 wa_headdata-forecast_view      =  co_yes.
*  wa_headdata-work_sched_view    =  abap_on.
*  wa_headdata-prt_view           =  abap_on.
 wa_headdata-storage_view       =  co_yes.    "!
**  wa_headdata-warehouse_view     =  abap_on.
*   wa_headdata-quality_view       =  abap_on.
 wa_headdata-account_view       =  co_yes.
*  wa_headdata-cost_view          =  abap_on.
**  wa_headdata-material_external  =  wa_clientdata-material_external.
**  wa_headdata-material_guid      =  wa_clientdata-material_guid.
**  wa_headdata-material_version   =  wa_clientdata-material_version.

 wa_clientdata_new-MATL_GROUP  = '001'.
 wa_clientdata_new-OLD_MAT_NO  = 'MDF'.
 wa_clientdata_new-BASE_UOM    = 'UN'.
 wa_clientdata_new-NET_WEIGHT  = '1,000'.
 wa_clientdata_new-UNIT_OF_WT  = 'KG'.
 wa_clientdata_new-DIVISION    = '01'.
 wa_clientdata_new-PROD_HIER   = '0001300003'.
 wa_clientdata_new-BATCH_MGMT  = 'X'.
 wa_clientdata_new-PVALIDFROM   = SY-DATUM.
 wa_clientdata_new-ITEM_CAT     = 'NORM'.

 wa_clientdatax-MATL_GROUP  = co_yes.
 wa_clientdatax-OLD_MAT_NO  = co_yes.
 wa_clientdatax-BASE_UOM    = co_yes.
 wa_clientdatax-NET_WEIGHT  = co_yes.
 wa_clientdatax-UNIT_OF_WT  = co_yes.
 wa_clientdatax-DIVISION    = co_yes.
 wa_clientdatax-PROD_HIER   = co_yes .
 wa_clientdatax-BATCH_MGMT  = co_yes .
 wa_clientdatax-PVALIDFROM   = co_yes.
 wa_clientdatax-ITEM_CAT     = co_yes.

 wa_salesdata_new-SALES_ORG   = '01'.
 wa_salesdata_new-DISTR_CHAN  = 'Z2'.
 wa_salesdata_new-MATL_STATS  = '1'.
 wa_salesdata_new-ITEM_CAT    = 'ZMDF'  .
 wa_salesdata_new-ACCT_ASSGT  = '01'.

 wa_salesdatax-SALES_ORG   = '01'.
 wa_salesdatax-DISTR_CHAN  = 'Z2'.
 wa_salesdatax-MATL_STATS  = co_yes.
 wa_salesdatax-ITEM_CAT    = co_yes  .
 wa_salesdatax-ACCT_ASSGT  = co_yes.


 gs_bapi_makt-LANGU   = 'ES'.
 gs_bapi_makt-MATL_DESC  = 'Campo9'.
 append gs_bapi_makt to gt_bapi_makt.

 gs_bapi_marm-ALT_UNIT  = 'UN'.
 gs_bapi_marm-GROSS_WT  = '1,000'.
 append gs_bapi_marm to gt_bapi_marm.

 gs_bapi_marmx-ALT_UNIT  = 'UN'.
 gs_bapi_marmx-GROSS_WT  = co_yes.
 append gs_bapi_marmx to gt_bapi_marmx.


 gs_mlan-DEPCOUNTRY  = 'CL'.
 gs_mlan-TAX_TYPE_1  = 'ZIVA'.
 gs_mlan-TAXCLASS_1  = '1'.
 append gs_mlan to gt_mlan.

ENDFORM.                    "fill_data