Partner Function Update

mail@pastecode.io avatar
unknown
abap
a year ago
3.4 kB
7
Indexable
Never
DATA: ls_data TYPE cvis_ei_extern,
      lt_data TYPE cvis_ei_extern_t.

DATA: BEGIN OF pf,
        kunnr TYPE kunnr,
        vkorg TYPE vkorg,
        vtweg TYPE vtweg,
        spart TYPE spart,
        parvw TYPE parvw,
        gpanr TYPE gpanr,
      END OF pf,
      pf_tab LIKE STANDARD TABLE OF pf.

" PF structure contains the details of the new partner function to be added
CLEAR: ls_data.

" Get the partner number and guid for the customer code using cvi_cust_link table
ls_data-partner-header-object_task = gc_object_task_update.
ls_data-partner-header-object = gc_object_bp.
ls_data-partner-header-object_instance-bpartner = lv_partner.
ls_data-partner-header-object_instance-bpartnerguid = lv_partner_guid.

ls_data-customer-header-object_instance-kunnr = pf-kunnr.
ls_data-customer-header-object_task = gc_object_task_update.

APPEND INITIAL LINE TO ls_data-customer-sales_data-sales ASSIGNING FIELD-SYMBOL(<sale>).
IF <sale> IS ASSIGNED.
  " task
  <sale>-task = gc_object_task_update.
  " key fields
  <sale>-data_key-vkorg = pf-vkorg.
  <sale>-data_key-vtweg = pf-vtweg.
  <sale>-data_key-spart = pf-spart.

  " This code is required to replace existing partner function assignment with new partner/customer
  SELECT * 
    FROM knvp 
    INTO TABLE @DATA(partners)
    WHERE kunnr = @pf-kunnr 
      AND vkorg = @pf-vkorg 
      AND vtweg = @pf-vtweg 
      AND spart = @pf-spart 
      AND parvw = @pf-parvw.

  LOOP AT partners INTO DATA(ls_partner).
    APPEND INITIAL LINE TO <sale>-functions-functions ASSIGNING FIELD-SYMBOL(<function>).
    IF <function> IS ASSIGNED.
      <function>-task = gc_object_task_delete.
      <function>-data_key-parvw = ls_partner-parvw.
      <function>-data_key-parza = ls_partner-parza.
      <function>-data-partner  = ls_partner-pernr.
      <function>-datax-partner = abap_true.
    ENDIF.
    CLEAR ls_partner.
    UNASSIGN <function>.
  ENDLOOP.

  " Add the new partner function
  UNASSIGN <function>.
  APPEND INITIAL LINE TO <sale>-functions-functions ASSIGNING <function>.
  <function>-task = gc_object_task_insert.
  <function>-data_key-parvw = pf-parvw. " value of role in excel line
  <function>-data-partner = pf-gpanr.   " partner in excel line
  <function>-datax-partner = abap_true.
ENDIF.


cl_md_bp_maintain=>validate_single(
  EXPORTING
    i_data        = ls_data
  IMPORTING
    et_return_map = DATA(lt_return_validation) ).

"Suppress the mandatory fields checks from validate in update mode
IF ls_data-partner-header-object_task = gc_object_task_update.
  DELETE lt_return_validation WHERE id = 'R11' AND number = '401'.  
ENDIF.
DELETE lt_return_validation WHERE type EQ 'S' OR type EQ 'W' OR type EQ 'I'.

IF lt_return_validation IS INITIAL.
  REFRESH lt_data.
  APPEND ls_data TO lt_data.
  cl_md_bp_maintain=>maintain(
    EXPORTING
      i_data    = lt_data
    IMPORTING
      e_return  = DATA(lt_return) ).

  READ TABLE lt_return INTO DATA(ls_return) INDEX 1.  " since we send 1 bp at a time
  IF sy-subrc = 0.
    DELETE ls_return-object_msg WHERE type EQ 'S' OR type EQ 'W' OR type EQ 'I'.
  ENDIF.

  IF ls_return-object_msg IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true.
    IF sy-subrc = 0.
      output_line_pf-icon = icon_green_light.
      output_line_pf-msg = 'Data updated successfully'.
    ENDIF.
  ENDIF.
ENDIF.