Saturday, April 20, 2019

bapi for unplanned depreciation abaa

                                                 Bapi for  t-code : ABAA - Unplanned Depreciation

 ABAA :UNPLANNED DEPRECIATION :
There is an unexpected permanent reduction in the worth of the asset that you have to post as unplanned depreciation.
As a rule, the system automatically determines the planned depreciation for the current fiscal year by means of the depreciation keys entered in the master record. If you need to specifically set the amount of depreciation, the system offers a manual unplanned depreciation forecasting option. This means you can manually increase the planned values managed for the asset.The G/L accounts in Financial Accounting are not initially affected by this posting transaction. Asset line items are created, but no FI posting documents. The general ledger accounts are updated and the corresponding FI documents are created by the periodic depreciation posting run. The system then determines the depreciation to be posted up to a specific period, and creates the accompanying posting documents.
Transaction type - 640 : Unplanned depreciation on prior year acquisitions.
During posting system automatically calculates the depreciation in different amounts for different depreciation areas.
After posting you can see the same in AW01N in Planned values section under 'change' column against Unplanned depreciation.
NOTE : BDC OR LSMW doesn't work in transaction : ABAA.
 SO,we looked for bapi option , one bapi that could satisfy the need was :
                        'AMDP_BAPI_CALL_ON_TRANSACTION'.
 so let us design the code :
if you uploading in mass : using excel file then  process follows as:
1> uploading excel file and converting into into an same format in internal table 
2> looping on the table into fieldsymbol and working and then filling the 
structure required in BAPI .
then calling the bapi if retun table has type 'S' then calling bapi  transaction commit otherwise showing error in alv.


*----------------top (data declaration in program )-----------------------------------------------------*
*&---------------------------------------------------------------------*
*&  Include           ZFI_ASSET_UNPLANNED_TOP
*&---------------------------------------------------------------------*
TYPESfs_struct(4096TYPE OCCURS .


TYPESBEGIN OF ty_data1,
         bukrs TYPE  bukrs,    "comp_code
         anln1 TYPE  anln1,    "Main Asset Number
         bwasl TYPE  bwasl,    "Asset Transaction Type
         bldat TYPE  bldat,    "Document Date in Document
         budat TYPE  budat,    "Posting Date in the Document
         bzdat TYPE  bzdat,    "Asset Value Date
         sgtxt TYPE  sgtxt,    "Item Text
         anbtr TYPE  anbtr,    "Amount posted
         gjahr TYPE  gjahr,    "Fiscal Year
       END OF ty_data1.

DATA :lv_code           LIKE sy-ucomm VALUE 'SAVE'.
DATAls_raifp2         TYPE raifp2,
      lt_raifp2         TYPE TABLE OF raifp2,
      ls_raifp3         TYPE raifp3,
      lt_raifp3         TYPE TABLE OF raifp3,
      lt_return         TYPE TABLE OF bapiret2,
      ls_raifp4         TYPE            raifp4,
      ls_raifp1         TYPE            raifp1,
      it_fld            TYPE  slis_t_fieldcat_alv,
      wa_fld            TYPE  slis_fieldcat_alv,
      w_struct          TYPE           fs_struct,
      g_repid           LIKE            sy-repid,
      wa_return         TYPE            bapiret2,
      it_return1        TYPE TABLE OF   bapiret2,
      wa_return1        TYPE            bapiret2,
      gt_writedown_data TYPE   TABLE OF   ty_data1,
      gs_writedown_data TYPE              ty_data1.




SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS:p_file  LIKE rlgrap-filename OBLIGATORY,
           p_gjahr TYPE gjahr OBLIGATORY DEFAULT '2017'.
SELECTION-SCREEN END OF BLOCK b1.
 *---------------------------------------------------------------*
 *---------                 -Main Program  ----------------------*
 *---------------------------------------------------------------*
REPORT zfi_asset_unplanned_dep.
INCLUDE ZFI_ASSET_UNPLANNED_TOP"data declarations
INCLUDE ZFI_ASSET_UNPLANNED_F01"bapi & query


 INITIALIZATION .
 g_repid sy-repid.

 AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
   perform f_request_pfile.

  start-of-SELECTION.
  perform f_get_file_ver1.
  end-of-SELECTION.
 if gt_writedown_data[] is NOT INITIAL " if there is data in file
 perform asset_writedown.
   IF IT_RETURN1[] IS NOT INITIAL.
      PERFORM F_FIELDCAT.  "to display if any log
      PERFORM F_DISPLAY_LOG" to display log
   ENDIF.
  ELSE.
    MESSAGE 'NO DATA TO PROCESS' TYPE 'E'.
    ENDIF. 
 
 *---------------------------------------------------------------------*
 *---------- QUERY AND PERFORM CODE ------------------------------------*
 *----------------------------------------------------------------------*
&---------------------------------------------------------------------*
*&  Include           ZFI_ASSET_UNPLANNED_F01
*&---------------------------------------------------------------------*

FORM f_fieldcat .
  wa_fld-col_pos 1.
  wa_fld-tabname 'IT_FINAL1'.
  wa_fld-fieldname 'TYPE'.
  wa_fld-seltext_l 'TYPE'.
  APPEND wa_fld TO it_fld.
  CLEAR wa_fld.

  wa_fld-col_pos 2.
  wa_fld-tabname 'IT_FINAL1'.
  wa_fld-fieldname 'ID'.
  wa_fld-seltext_l 'ID'.
  APPEND wa_fld TO it_fld.
  CLEAR wa_fld.

  wa_fld-col_pos 3.
  wa_fld-tabname 'IT_FINAL1'.
  wa_fld-fieldname 'NUMBER'.
  wa_fld-seltext_l 'NUMBER'.
  APPEND wa_fld TO it_fld.
  CLEAR wa_fld.

  wa_fld-col_pos 4.
  wa_fld-tabname 'IT_FINAL1'.
  wa_fld-fieldname 'ROW'.
  wa_fld-seltext_l 'ROW'.
  APPEND wa_fld TO it_fld.
  CLEAR wa_fld.

  wa_fld-col_pos 5.
  wa_fld-tabname 'IT_FINAL1'.
  wa_fld-fieldname 'FIELD'.
  wa_fld-seltext_l 'FIELD'.
  APPEND wa_fld TO it_fld.
  CLEAR wa_fld.

  wa_fld-col_pos 6.
  wa_fld-tabname 'IT_FINAL1'.
  wa_fld-fieldname 'MESSAGE'.
  wa_fld-seltext_l 'MESSAGE'.
  APPEND wa_fld TO it_fld.
  CLEAR wa_fld.
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  F_REQUEST_PFILE
*&---------------------------------------------------------------------*
*       F4 HELP FUNCTIONALITY TO GET THE FILENAME
*----------------------------------------------------------------------*
*  -->  P_FILE       FILE NAME
*  <--
*----------------------------------------------------------------------*
FORM f_request_pfile .
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  syst-cprog
      dynpro_number syst-dynnr
      field_name    'X'
    IMPORTING
      file_name     p_file.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  F_GET_FILE_VER1
*&---------------------------------------------------------------------*
*       TEXT CONVERSION TO EXCEL FILE
*----------------------------------------------------------------------*
*  -->  P_FILE                        FILE NAME INTAKE
*  <--  GT_WRITEDOWN_DATA            I-TABLE TO HOLD EXCEL O-P DATA
*----------------------------------------------------------------------*
FORM f_get_file_ver1 .
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_field_seperator    'X'
      i_line_header        '1'
      i_tab_raw_data       w_struct
      i_filename           p_file
    TABLES
      i_tab_converted_data gt_writedown_data.
  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.
*&---------------------------------------------------------------------*
*&      Form  ASSET_WRITEDOWN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM asset_writedown .
*-----------unplanned depreciation ------------------------------------*
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
  FIELD-SYMBOLS<fs_writedown> TYPE ty_data1.
  REFRESH lt_raifp2[] lt_raifp3[] ,lt_return[].

  SORT gt_writedown_data[] BY bwasl budat bldat bzdat bukrs anln1.
*----------------------------------------------------------------------*
* looping on gt_writedown_data to execute in bapi
*----------------------------------------------------------------------*
  LOOP AT gt_writedown_data ASSIGNING <fs_writedown>.

    CLEAR ls_raifp1 ,ls_raifp2 ls_raifp3"clearing always before filling
    ls_raifp1-bwasl <fs_writedown>-bwasl.   "assettrasfer type "650 or 640 for unplanned depreciation
    ls_raifp1-budat <fs_writedown>-budat.   "posting date
    ls_raifp1-bldat <fs_writedown>-bldat.   "Doc Date
    ls_raifp1-bzdat <fs_writedown>-bzdat.   "value date
    ls_raifp1-waers 'INR'.                  "currency
    ls_raifp2-bukrs  <fs_writedown>-bukrs.  "comp_code
    ls_raifp2-anln1  <fs_writedown>-anln1.  "asset main no (12 Character must)
    ls_raifp2-gjahr  <fs_writedown>-gjahr.  "fiscal year
    ls_raifp2-anbtr  <fs_writedown>-anbtr.  "Amount Posted
    ls_raifp2-sgtxt  =  <fs_writedown>-sgtxt.  "item txt
    APPEND ls_raifp2 TO lt_raifp2.
    CLEAR ls_raifp2.

    ls_raifp3-xbanl 'X'.
    APPEND ls_raifp3 TO lt_raifp3.
    CLEAR ls_raifp3.

    CALL FUNCTION 'AMDP_BAPI_CALL_ON_TRANSACTION'
      EXPORTING
        i_raifp1 ls_raifp1
        i_awtyp  'AMBU'
*       E_TXNOK  =
      TABLES
        t_raifp2 lt_raifp2
        t_raifp3 lt_raifp3
        t_return lt_return
      CHANGING
        c_fcode  lv_code.
    "To handle return table we are copying into another ret table
    APPEND LINES OF lt_return TO it_return1.
    "To commit only when the return type is 'S' "
    READ TABLE lt_return INTO wa_return WITH KEY type 'S'.
    IF sy-subrc IS INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait ' '.


    ENDIF.
    REFRESH  lt_return[].
  ENDLOOP.
ENDFORM.

FORM f_display_log .

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      it_fieldcat   it_fld
      i_default     ''
      i_save        'X'
    TABLES
      t_outtab      it_return1
    EXCEPTIONS
      program_error 1
      OTHERS        2.
  IF sy-subrc <> 0.
  ENDIF.

ENDFORM.