Monday, April 6, 2020

Automation of FD32 in SAP SD

  Automation of FD32 in SAP SD : Credit Limit for Customer based on 90 days Sales


As per our Company's Director demand , they want to give Credit Limit to there customer based on the sales , as there was no controlling over the credit limit given by sap user who had access of FD32.
So we the basic idea was to give a z-report which user can see and update the credit limit for customer's at one go , based on the average sale's of 90 days, and giving them credit limit for 1 month or 45 Days .
So I used BAPI : CREDIT_LIMIT_CHANGE BAPI , for those customer whose credit limit is defined in Table in knkk , it will be updated or else it will be inserted , with average sales value ,
but the main condition was that for particular customer whose credit limit is made 0 or 1 , it should not be updated and for those customer whose sale is 0 in last 3 month Then there Credit limit should be updated to  0.
So , We decided to show the proposed limit and average sales , and current credit limit .

The program went well , although at first instance , it disrupted the whole Company's Sales Force , but they could were forced to provide a valid reason for increasing the Credit Limit.


Input Screen :




















When you Execute, this report , you will O/P as such :

There's a button on the Top  Menu , Update Credit Limit :

2nd way Code :
Directly upload from Excel file and there credit limit will be set.
Code is :

Main Code :
*&---------------------------------------------------------------------*
*& Report  ZSD_CREDIT_LIMIT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsd_credit_limit.
INCLUDE zsd_credit_limit_top.
refresh it_tab it_message .
AT SELECTION-SCREEN ON VALUE-REQUEST FOR filename.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name 'filename'
    IMPORTING
      file_name  filename.

START-OF-SELECTION.


  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*     I_FIELD_SEPERATOR    =
      i_line_header        'X'
      i_tab_raw_data       it_raw
      i_filename           filename
    TABLES
      i_tab_converted_data it_tab
    EXCEPTIONS
      conversion_failed    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.

  IF it_tab[] IS NOT INITIAL.
    LOOP AT it_tab INTO wa_tab.
      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  wa_tab-kunnr
        IMPORTING
          output wa_tab-kunnr.

      wa_output-klimk wa_tab-klimk.
      wa_output-kunnr wa_tab-kunnr.
      SELECT SINGLE FROM knkk INTO  CORRESPONDING FIELDS OF  i_knkk
       WHERE kunnr wa_output-kunnr.
      SELECT SINGLE FROM knka INTO CORRESPONDING FIELDS OF i_knka
        WHERE kunnr wa_output-kunnr.
*-----------------For Log Report : We are updating CDPOS , CDHDR -----------------*

      DATAlv_objectclass       TYPE cdobjectcl,
            lv_objectid          TYPE cdobjectv,
            lv_changenr          TYPE cdchangenr,
            ls_workarea_knkk_new TYPE knkk,
            ls_workarea_knkk_old TYPE knkk.

      ls_workarea_knkk_old i_knkk.
      MOVE-CORRESPONDING ls_workarea_knkk_old TO ls_workarea_knkk_new.
      ls_workarea_knkk_new-klimk wa_output-klimk.
      lv_objectclass 'KLIM'.
      lv_objectid wa_output-kunnr.

      CALL FUNCTION 'CHANGEDOCUMENT_OPEN'
        EXPORTING
          objectclass      lv_objectclass
          objectid         lv_objectid
        EXCEPTIONS
          sequence_invalid 1
          OTHERS           2.

      IF sy-subrc 0.
        CALL FUNCTION 'CHANGEDOCUMENT_SINGLE_CASE'
          EXPORTING
            change_indicator       'U'
            tablename              'KNKK'
            workarea_new           ls_workarea_knkk_new
            workarea_old           ls_workarea_knkk_old
          EXCEPTIONS
            nametab_error          1
            open_missing           2
            position_insert_failed 3
            OTHERS                 4.
        IF sy-subrc 0.

          CALL FUNCTION 'CHANGEDOCUMENT_CLOSE'
            EXPORTING
              date_of_change          sy-datum
              objectclass             lv_objectclass
              objectid                lv_objectid
              tcode                   'ZFD32A'
              time_of_change          sy-uzeit
              username                sy-uname
              object_change_indicator 'U'
            EXCEPTIONS
              header_insert_failed    1
              no_position_inserted    2
              object_invalid          3
              open_missing            4
              position_insert_failed  5
              OTHERS                  6.
          CLEAR ls_workarea_knkk_newls_workarea_knkk_old,lv_objectid.
        ENDIF.
      ENDIF.
*--------------------------------EOC : 12/09/2019 -------------------------------------------*


      IF i_knkk IS NOT INITIAL .  " check to ensure that if has data then only it updates value
        i_knkk-klimk wa_output-klimk.
        i_knkk-aedat sy-datum"SY-DATUM WHEN CHANGED
        i_knkk-aenam sy-uname"SY-NAME OF USER WHO CHANGED IT .
        upd_knkk 'U'"update
      ELSE.
        upd_knkk 'I'.  "insert
      ENDIF.
      IF i_knka IS NOT INITIAL .
        upd_knka 'U'.
      ELSE.
        upd_knka 'I'.
      ENDIF.


*checking whether i_knkk and i_knka is initial to update or insert
      IF i_knkk IS  INITIAL.
        i_knkk-mandt sy-mandt.
        i_knkk-kunnr wa_output-kunnr.
        i_knkk-knkli wa_output-kunnr.
        i_knkk-kkber =  '1000'.
        i_knkk-klimk wa_output-klimk.
        i_knkk-ernam sy-uname"ADDED ON 30/08/2019
        i_knkk-erdat sy-datum"added on 30/08/2019
      ENDIF.
      IF i_knka IS INITIAL .
        i_knka-mandt sy-mandt.
        i_knka-kunnr wa_output-kunnr"FOR TESTING CLOSED
        i_knka-waers 'INR'.   "FOR TESTING CLOSED
      ENDIF " TO FILL ONLY IF DATA IS NOT AVAILABLE
*
*  Call FM to update Customer Credit Limit

      CALL FUNCTION 'CREDITLIMIT_CHANGE'
        EXPORTING
          i_knka   i_knka " ->DONE TESING PURPOSE
          i_knkk   i_knkk
          upd_knka upd_knka
          upd_knkk upd_knkk
*         XNEUA    = ' '
*         XREFL    = ' '
          yknka    yknka
          yknkk    yknkk.
      COMMIT WORK AND WAIT .
      MODIFY IT_TAB FROM WA_TAB TRANSPORTING KUNNR.
      CLEAR WA_TAB.
    ENDLOOP.
    IF sy-subrc IS INITIAL.

    ENDIF.
  ENDIF.

  WAIT UP TO SECONDS.

  SELECT    KNA1~kunnr,
            KNA1~NAME1,
            KNKK~klimk,
            KNKK~knkli,
            KNKK~ctlpc
            FROM knkk
            INNER JOIN KNA1 ON KNA1~KUNNR KNKK~KUNNR
            INTO TABLE @DATA(it_knkk1)
            FOR ALL ENTRIES IN @it_tab
            WHERE KNKK~kunnr @it_tab-kunnr.



  SORT it_tab BY kunnr.
  SORT it_knkk1 BY kunnr.
  LOOP AT  it_tab INTO wa_tab .
    wa_message-kunnr wa_tab-kunnr.
    READ TABLE it_knkk1 INTO DATA(wa_knkkWITH  KEY kunnr wa_tab-kunnr
                                                            BINARY SEARCH.
    IF sy-subrc IS INITIAL.
      wa_message-klimk  wa_knkk-klimk.
      wa_message-NAME1  wa_knkk-NAME1.
      CLEAR wa_knkk.
    ENDIF.
    APPEND wa_message TO it_message.
    CLEAR wa_message,wa_tab.
  ENDLOOP.




*Display Data
** Declaration for ALV Grid **
  DATA gr_table  TYPE REF TO cl_salv_table,
         g_column  TYPE REF TO cl_salv_column,
         g_columns TYPE REF TO cl_salv_columns_table.
** Declarations for ALV Functions
  DATA gr_functions TYPE REF TO cl_salv_functions_list.
  DATA gr_display TYPE REF TO cl_salv_display_settings,
         gr_layout  TYPE REF TO cl_salv_layout,
         key        TYPE salv_s_layout_key,
         lv_title   TYPE lvc_title.

  CLEAR gr_table.
  TRY.
      CALL METHOD cl_salv_table=>factory
        EXPORTING
          list_display if_salv_c_bool_sap=>false
        IMPORTING
          r_salv_table gr_table
        CHANGING
          t_table      it_message.
    CATCH cx_salv_msg .
  ENDTRY.
  IF gr_table IS INITIAL.
    MESSAGE text-008  TYPE 'I' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
** Get functions details
  gr_functions gr_table->get_functions).

** Activate All Buttons in Tool Bar
  gr_functions->set_allif_salv_c_bool_sap=>true ).
* Layout Save Option
  gr_layout gr_table->get_layout).
  key-report sy-repid.
  gr_layout->set_keykey ).
*pass the folling values to the SET_SAVE_RESTRICTION method.
*RESTRICT_NONE
*RESTRICT_USER_DEPENDANT
*RESTRICT_USER_INDEPENDANT
  gr_layout->set_save_restrictionif_salv_c_layout=>restrict_none ).
******* Global Display Settings  *******
  CLEAR gr_display.
*  MOVE 'Sales Order Details' TO lv_title.
  gr_display gr_table->get_display_settings).               " Global Display settings"
  gr_display->set_striped_patternif_salv_c_bool_sap=>true ).  "Activate Strip Pattern"
*  GR_DISPLAY->SET_LIST_HEADER( LV_TITLE ).

  TRY.
      g_columns gr_table->get_columns).
      g_column g_columns->get_column'KUNNR' ).
      g_column->set_short_text'Customer' ).
      g_column->set_medium_text'Customer Code' ).
      g_column->set_long_text'Customer Code' ).

    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
  ENDTRY.


  TRY.
      g_columns gr_table->get_columns).
      g_column g_columns->get_column'NAME1' ).
      g_column->set_short_text'Name' ).
      g_column->set_medium_text'Customer Name' ).
      g_column->set_long_text'Customer Name' ).

    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
  ENDTRY.

  TRY.
      g_columns gr_table->get_columns).
      g_column g_columns->get_column'KLIMK' ).
      g_column->set_short_text'Cr.Limit' ).
      g_column->set_medium_text'Cr.Limit' ).
      g_column->set_long_text'Credit Limit' ).

    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
  ENDTRY.


  CALL METHOD gr_table->display.

---------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
Now paste this code in TOP  Include :

*&---------------------------------------------------------------------*
*&  Include           ZSD_CREDIT_LIMIT_TOP
*&---------------------------------------------------------------------*
TABLES vbrpmaravbrk.
TYPE-POOLSslis.
TYPES BEGIN OF TY_MESSAGE,
        KUNNR TYPE KUNNR,  "kunnr
        NAME1 TYPE NAME1,
        KLIMK TYPE KLIMK"credit limit
        END OF TY_MESSAGE.
DATA IT_MESSAGE TYPE TABLE OF TY_MESSAGE"Dt : 10.1.2020
       WA_MESSAGE TYPE          TY_MESSAGE"dt : 10.1.2020

DATA lr_alv     TYPE REF TO cl_salv_table,
       gr_columns TYPE REF TO cl_salv_columns_table,
       l_column   TYPE REF TO cl_salv_column.

DATA it_raw     TYPE truxs_t_text_data.
TYPES BEGIN OF ty_main,
          KUNNR     TYPE KUNNR,
          KLIMK     TYPE NETWR,
        END OF ty_main.



      DATAi_knka   TYPE knka,
            i_knkk   TYPE knkk,
            upd_knka TYPE cdpos-chngind,
            upd_knkk TYPE cdpos-chngind,
            yknka    TYPE knka,
            yknkk    TYPE knkk.
DATA it_main TYPE TABLE OF TY_MAIN,
       it_tab  TYPE TABLE OF ty_main,
       wa_main TYPE          ty_main,
       wa_tab  TYPE           ty_main.
DATA IT_OUTPUT TYPE TABLE OF TY_MAIN,
       WA_OUTPUT TYPE          TY_MAIN.
SELECTION-SCREEN:BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS filename TYPE  rlgrap-filename .
SELECTION-SCREENEND OF BLOCK b1.

-----------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------