FBCJ Transaction SAVE and POST using BAPI

*&---------------------------------------------------------------------*
*& Report  ZFBCJ_UPLOAD
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZFBCJ_UPLOAD.
TABLES SSCRFIELDS.
TYPES BEGIN OF TY_MAKE_EXCEL,
         COLUMN01
(25),
         COLUMN02
(25),
         COLUMN03
(25),
         COLUMN04
(25),
         COLUMN05
(25),
         COLUMN06
(25),
         COLUMN07
(25),
         COLUMN08
(25),
         COLUMN09
(25),
         COLUMN10
(25),
         COLUMN11
(25),
         COLUMN12
(25),
        
END OF TY_MAKE_EXCEL.
TYPES BEGIN OF TY_FINAL,
          TRANSACT_NUMBER 
TYPE BAPI_CJ_ITEMS-TRANSACT_NUMBER,
          P_NET_AMOUNT    
TYPE BAPI_CJ_ITEMS-P_NET_AMOUNT,
          P_RECEIPTS      
TYPE BAPI_CJ_ITEMS-P_RECEIPTS,
          P_PAYMENTS      
TYPE BAPI_CJ_ITEMS-P_PAYMENTS,
          GL_ACCOUNT      
TYPE BAPI_CJ_ITEMS-GL_ACCOUNT,
          TAX_CODE        
TYPE BAPI_CJ_ITEMS-TAX_CODE,
          BP_NAME         
TYPE BAPI_CJ_HEADER-BP_NAME,
          POSITION_TEXT   
TYPE BAPI_CJ_ITEMS-POSITION_TEXT,
          VENDOR_NO       
TYPE BAPI_CJ_ITEMS-VENDOR_NO,
          CUSTOMER        
TYPE BAPI_CJ_ITEMS-CUSTOMER,
          PSTNG_DATE      
TYPE BAPI_CJ_HEADER-PSTNG_DATE,
          DOC_DATE        
TYPE BAPI_CJ_HEADER-DOC_DATE,
          COSTCENTER      
TYPE BAPI_CJ_ITEMS-COSTCENTER,
          PROFIT_CTR      
TYPE BAPI_CJ_ITEMS-PROFIT_CTR,
          FISCAL_YEAR     
TYPE BAPI_CJ_KEY-FISC_YEAR,
          CJ_DOC_NO       
TYPE BAPI_CJ_KEY-POSTING_NUMBER,
        
END OF TY_FINAL.
TYPES BEGIN OF TY_ERROR,
          LNO
(5TYPE N,
          MSG
(255),
        
END OF TY_ERROR.
TYPES BEGIN OF TY_POST,
          LNO
(5TYPE N,
          CJ_DOC_NO 
TYPE BAPI_CJ_KEY-POSTING_NUMBER,
          MSG
(255),
        
END OF TY_POST.
DATA IT_MAKE_EXCEL TYPE STANDARD TABLE OF TY_MAKE_EXCEL,
       WA_MAKE_EXCEL 
LIKE LINE OF IT_MAKE_EXCEL,
       IT_GET_EXCEL  
TYPE STANDARD TABLE OF  ALSMEX_TABLINE,
       WA_GET_EXCEL  
LIKE LINE OF IT_GET_EXCEL,
       IT_TCJ_TRANS  
TYPE STANDARD TABLE OF TCJ_TRANSACTIONS,
       WA_TCJ_TRANS  
LIKE LINE OF IT_TCJ_TRANS,
       IT_TCJ_TRANSN 
TYPE STANDARD TABLE OF TCJ_TRANS_NAMES,
       WA_TCJ_TRANSN 
LIKE LINE OF IT_TCJ_TRANSN,
       IT_FINAL      
TYPE STANDARD TABLE OF TY_FINAL,
       WA_FINAL      
LIKE LINE OF IT_FINAL.
DATA FBCJ_HEADER    TYPE BAPI_CJ_HEADER,
       IT_FBCJ_ITEM   
TYPE STANDARD TABLE OF BAPI_CJ_ITEMS,
       WA_FBCJ_ITEM   
LIKE LINE OF IT_FBCJ_ITEM,
       IT_FBCJ_RETURN 
TYPE STANDARD TABLE OF BAPIRET2,
       WA_FBCJ_RETURN 
LIKE LINE OF IT_FBCJ_RETURN.
DATA IT_ERROR TYPE STANDARD TABLE OF TY_ERROR,
       WA_ERROR 
LIKE LINE OF IT_ERROR,
       IT_POST  
TYPE STANDARD TABLE OF TY_POST,
       WA_POST  
LIKE LINE OF IT_POST.
DATA IT_FCAT   TYPE SLIS_T_FIELDCAT_ALV,
       WA_FCAT   
LIKE LINE OF IT_FCAT,
       WA_LAYOUT 
TYPE SLIS_LAYOUT_ALV.
DATA CJ_DOC_NO   TYPE BAPI_CJ_KEY-POSTING_NUMBER,
       FISCAL_YEAR 
TYPE BAPI_CJ_KEY-FISC_YEAR.
DATA LV_NUM(10TYPE N.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.    "Input
PARAMETERS COM_CODE TYPE TCJ_C_JOURNALS-COMP_CODE DEFAULT '9000'.
PARAMETERS CAJO_NUM TYPE TCJ_C_JOURNALS-CAJO_NUMBER.
SELECTION-SCREEN END OF BLOCK B1.
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.  "File Selection
PARAMETER P_FILE TYPE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK B2.
SELECTION-SCREEN BEGIN OF BLOCK B3 WITH FRAME TITLE TEXT-003.  "General Selection
PARAMETERS R1 RADIOBUTTON GROUP RAD1 DEFAULT 'X'.
PARAMETERS R2 RADIOBUTTON GROUP RAD1.
SELECTION-SCREEN END OF BLOCK B3.
SELECTION-SCREENFUNCTION KEY 1.
INITIALIZATION.
  
DATA:FUNCTXT TYPE SMP_DYNTXT.

  
CLEAR FUNCTXT.
  FUNCTXT
-TEXT TEXT-004.  "Download Excel Format
  SSCRFIELDS
-FUNCTXT_01 FUNCTXT.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
  
PERFORM GET_F4_FILE.
AT SELECTION-SCREEN.
  
CASE SSCRFIELDS-UCOMM.
    
WHEN 'FC01'.
      
PERFORM EXCEL_FORMAT.
    
WHEN 'ONLI'.
      
IF COM_CODE IS INITIAL.
        
MESSAGE TEXT-005 TYPE 'E'.  "Please Select Company Code
      
ENDIF.
      
IF CAJO_NUM IS INITIAL.
        
MESSAGE TEXT-006 TYPE 'E'.  "Please Select Journal Number
      
ENDIF.
      
IF P_FILE IS INITIAL.
        
MESSAGE TEXT-007 TYPE 'E'.  "Please Select File Path
      
ENDIF.
  
ENDCASE.
START-OF-SELECTION.
  
PERFORM GET_EXCEL.
  
PERFORM GET_DATA_FROM_EXCEL.
  
PERFORM SAVE_DOCUMENT.
  
IF IT_ERROR[] IS NOT INITIAL.
    
PERFORM BUILD_FIELDCAT_ERROR.
    
PERFORM SHOW_MSG.
  
ELSE.
    
PERFORM POST_DOCUMENT .
    
PERFORM BUILD_FIELDCAT_POST.
    
PERFORM SHOW_MSG.
  
ENDIF.

*&---------------------------------------------------------------------*
*&      Form  GET_F4_FILE
*&---------------------------------------------------------------------*
FORM GET_F4_FILE.
  
CALL FUNCTION 'F4_FILENAME'
    
EXPORTING
      PROGRAM_NAME  
SYST-CPROG
      DYNPRO_NUMBER 
SYST-DYNNR
      FIELD_NAME    
'P_FILE'
    
IMPORTING
      FILE_NAME     
P_FILE.
ENDFORM.                    "GET_F4_FILE

*&---------------------------------------------------------------------*
*&      Form  EXCEL_FORMAT
*&---------------------------------------------------------------------*
FORM EXCEL_FORMAT .
  
REFRESH IT_MAKE_EXCEL.
  
CLEAR WA_MAKE_EXCEL.

  
DATA LV_DEXT TYPE STRING VALUE 'XLS',
         LV_FNAME 
TYPE STRING,
         LV_FDIREC 
TYPE STRING VALUE 'c:\temp\'.

  
DATA LD_FILENAME TYPE STRING,
         LD_PATH 
TYPE STRING,
         LD_FULLPATH 
TYPE STRING,
         LD_RESULT 
TYPE I.

  
IF R1 IS NOT INITIAL.
    LV_FNAME 
TEXT-008.  "CASH PAYMENT
  
ELSE.
    LV_FNAME 
TEXT-009.  "CASH RECEIPTS
  
ENDIF.

  WA_MAKE_EXCEL
-COLUMN01 TEXT-010.  "Business Transaction
  WA_MAKE_EXCEL
-COLUMN02 TEXT-011.  "Amount
  WA_MAKE_EXCEL
-COLUMN03 TEXT-012.  "G/L
  WA_MAKE_EXCEL
-COLUMN04 TEXT-013.  "Receipt Recipient
  WA_MAKE_EXCEL
-COLUMN05 TEXT-014.  "Text
  WA_MAKE_EXCEL
-COLUMN06 TEXT-015.  "Vendor Number
  WA_MAKE_EXCEL
-COLUMN07 TEXT-016.  "Customer Number
  WA_MAKE_EXCEL
-COLUMN08 TEXT-017.  "Posting Date
  WA_MAKE_EXCEL
-COLUMN09 TEXT-018.  "Document Date
  WA_MAKE_EXCEL
-COLUMN10 TEXT-019.  "Cost Center
  WA_MAKE_EXCEL
-COLUMN11 TEXT-020.  "Profit Center
  WA_MAKE_EXCEL
-COLUMN12 TEXT-021.  "Segment
  
APPEND WA_MAKE_EXCEL TO IT_MAKE_EXCEL.

  
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
    
EXPORTING
      DEFAULT_EXTENSION    
LV_DEXT
      DEFAULT_FILE_NAME    
LV_FNAME
      INITIAL_DIRECTORY    
LV_FDIREC
    
CHANGING
      FILENAME             
LD_FILENAME
      PATH                 
LD_PATH
      FULLPATH             
LD_FULLPATH
      USER_ACTION          
LD_RESULT
    
EXCEPTIONS
      CNTL_ERROR           
1
      ERROR_NO_GUI         
2
      NOT_SUPPORTED_BY_GUI 
3
      
OTHERS               4.
  
IF SY-SUBRC <> 0.
    
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  
ENDIF.

  
CALL FUNCTION 'GUI_DOWNLOAD'
    
EXPORTING
      FILENAME              
LD_FULLPATH
      FILETYPE              
'ASC'
      WRITE_FIELD_SEPARATOR 
'X'
    
TABLES
      DATA_TAB              
IT_MAKE_EXCEL[].     "need to declare and populate
ENDFORM.                    " EXCEL_FORMAT

*&---------------------------------------------------------------------*
*&      Form  GET_EXCEL
*&---------------------------------------------------------------------*
FORM GET_EXCEL .

  
REFRESH IT_GET_EXCEL[].

  
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    
EXPORTING
      FILENAME                
P_FILE
      I_BEGIN_COL             
'1'
      I_BEGIN_ROW             
'2'
      I_END_COL               
'12'
      I_END_ROW               
'999'
    
TABLES
      INTERN                  
IT_GET_EXCEL
    
EXCEPTIONS
      INCONSISTENT_PARAMETERS 
1
      UPLOAD_OLE              
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.
  
ENDIF.

  
IF COM_CODE IS NOT INITIAL.
    
SELECT FROM TCJ_TRANSACTIONS INTO TABLE IT_TCJ_TRANS  WHERE COMP_CODE COM_CODE.
    
SELECT FROM TCJ_TRANS_NAMES  INTO TABLE IT_TCJ_TRANSN WHERE COMP_CODE COM_CODE.

  
ENDIF.
ENDFORM.                    " GET_EXCEL
*&---------------------------------------------------------------------*
*&      Form  GET_DATA_FROM_EXCEL
*&---------------------------------------------------------------------*
FORM GET_DATA_FROM_EXCEL .

  
LOOP AT IT_GET_EXCEL INTO WA_GET_EXCEL.
    
CASE WA_GET_EXCEL-COL.
      
WHEN '0001'.
        
READ TABLE IT_TCJ_TRANSN INTO WA_TCJ_TRANSN WITH KEY TRANSACT_NAME WA_GET_EXCEL-VALUE.
        
IF SY-SUBRC 0.
          WA_FINAL
-TRANSACT_NUMBER WA_TCJ_TRANSN-TRANSACT_NUMBER.
          
READ TABLE IT_TCJ_TRANS INTO WA_TCJ_TRANS WITH KEY TRANSACT_NUMBER WA_TCJ_TRANSN-TRANSACT_NUMBER.
          
IF SY-SUBRC 0.
            WA_FINAL
-TRANSACT_NUMBER WA_TCJ_TRANS-TRANSACT_NUMBER.
            WA_FINAL
-TAX_CODE        WA_TCJ_TRANS-TAX_CODE.
            LV_NUM 
WA_TCJ_TRANS-GL_ACCOUNT.
            WA_FINAL
-GL_ACCOUNT      LV_NUM.
          
ENDIF.
        
ENDIF.
      
WHEN '0002'.
        
IF R2 'X'.
          WA_FINAL
-P_RECEIPTS  WA_GET_EXCEL-VALUE.
        
ELSEIF R1 'X'.
          WA_FINAL
-P_PAYMENTS  WA_GET_EXCEL-VALUE.
        
ENDIF.
        WA_FINAL
-P_NET_AMOUNT WA_GET_EXCEL-VALUE.
      
WHEN '0003'.
        LV_NUM 
WA_GET_EXCEL-VALUE.
        WA_FINAL
-GL_ACCOUNT LV_NUM.
      
WHEN '0004'.
        WA_FINAL
-BP_NAME WA_GET_EXCEL-VALUE.
      
WHEN '0005'.
        WA_FINAL
-POSITION_TEXT WA_GET_EXCEL-VALUE.
      
WHEN '0006'.
        LV_NUM 
WA_GET_EXCEL-VALUE.
        WA_FINAL
-VENDOR_NO LV_NUM.
      
WHEN '0007'.
        LV_NUM 
WA_GET_EXCEL-VALUE.
        WA_FINAL
-CUSTOMER LV_NUM.
      
WHEN '0008'.
        
CONCATENATE WA_GET_EXCEL-VALUE+6(4WA_GET_EXCEL-VALUE+3(2)  WA_GET_EXCEL-VALUE+0(2)  INTO WA_FINAL-PSTNG_DATE.
      
WHEN '0009'.
        
CONCATENATE WA_GET_EXCEL-VALUE+6(4WA_GET_EXCEL-VALUE+3(2)  WA_GET_EXCEL-VALUE+0(2)  INTO WA_FINAL-DOC_DATE.
      
WHEN '0010'.
        WA_FINAL
-COSTCENTER WA_GET_EXCEL-VALUE.
      
WHEN '0011'.
        LV_NUM 
WA_GET_EXCEL-VALUE.
        WA_FINAL
-PROFIT_CTR LV_NUM.
      
WHEN '0012'.
    
ENDCASE.

    
AT END OF ROW.
      
APPEND WA_FINAL TO IT_FINAL.
      
CLEAR WA_FINAL.
    
ENDAT.
    
CLEAR LV_NUMWA_GET_EXCELWA_TCJ_TRANSWA_TCJ_TRANSN.
  
ENDLOOP.
ENDFORM.                    " GET_DATA_FROM_EXCEL

*&---------------------------------------------------------------------*
*&      Form  SAVE_DOCUMENT
*&---------------------------------------------------------------------*
FORM SAVE_DOCUMENT .

  
DATA LV_ERFLAG,
         LV_EXL_LNO
,
         LV_LOOP 
TYPE SY-TABIX.

  
DATA I_DATUM  LIKE  CSKS-DATBI,
         I_KOKRS  
LIKE  CSKS-KOKRS,
         I_KOSTL  
LIKE  CSKS-KOSTL,
         I_PRCTR  
LIKE  CEPC-PRCTR.

  
TYPES BEGIN OF TY_KNA1,
            KUNNR 
TYPE KNA1-KUNNR,
          
END OF TY_KNA1.

  
TYPES BEGIN OF TY_LFA1,
            LIFNR 
TYPE LFA1-LIFNR,
          
END OF TY_LFA1.

  
DATA IT_KNA1 TYPE STANDARD TABLE OF TY_KNA1,
         WA_KNA1 
LIKE LINE OF IT_KNA1,
         IT_LFA1 
TYPE STANDARD TABLE OF TY_LFA1,
         WA_LFA1 
LIKE LINE OF IT_LFA1.

  
IF IT_FINAL IS NOT INITIAL.
    
SELECT LIFNR FROM LFA1
               
INTO TABLE IT_LFA1
               
FOR ALL ENTRIES IN IT_FINAL
               
WHERE LIFNR IT_FINAL-VENDOR_NO.

    
SELECT KUNNR FROM KNA1
                 
INTO TABLE IT_KNA1
                 
FOR ALL ENTRIES IN IT_FINAL
                 
WHERE KUNNR IT_FINAL-CUSTOMER.

  
ENDIF.

  
LOOP AT IT_FINAL INTO WA_FINAL.
    LV_LOOP 
LV_LOOP + 1.
    LV_EXL_LNO 
SY-TABIX + 1.
*** Validation Begin ***
    
CLEAR I_DATUMI_KOKRSI_KOSTLI_PRCTRLV_ERFLAG.
    
IF R1 'X'.
      
IF WA_FINAL-COSTCENTER <> ''.
        I_DATUM 
WA_FINAL-PSTNG_DATE.
        I_KOKRS 
COM_CODE.
        I_KOSTL 
WA_FINAL-COSTCENTER.

        
CALL FUNCTION 'RK_KOSTL_CHECK' " Validating Cost Center
          
EXPORTING
            DATUM                      
I_DATUM
            KOKRS                      
I_KOKRS
            KOSTL                      
I_KOSTL
         
EXCEPTIONS
           INPUT_ERROR                
1
           KOSTL_NOT_COMPLETE         
2
           KOSTL_NOT_FOUND            
3
           NO_GSBER                   
4
           NO_PRIMARY_POSTING         
5
           NO_REVENUES_POSTING        
6
           NO_SECUNDARY_POSTING       
7
           TEXT_NOT_FOUND             
8
           WRONG_BUKRS                
9
           WRONG_GSBER                
10
           WRONG_KATYP                
11
           CCA_NOT_ACTIVE             
12
           
OTHERS                     13
                  
.
        
IF SY-SUBRC <> 0.
          
IF SY-SUBRC 03.
            LV_ERFLAG 
'X'.
            WA_ERROR
-LNO LV_EXL_LNO.
            WA_ERROR
-MSG TEXT-022"'Invalid Cost Center, Kindly check it'.
            
APPEND WA_ERROR TO IT_ERROR.
            
CLEAR WA_ERROR.
          
ENDIF.
        
ENDIF.
      
ENDIF.
    
ELSEIF R2 'X'.
      
IF WA_FINAL-PROFIT_CTR <> ''.
        I_PRCTR 
WA_FINAL-PROFIT_CTR.

        
CALL FUNCTION 'KE_PROFIT_CENTER_CHECK' " Validating Profit Center
          
EXPORTING
           PRCTR                       
I_PRCTR

          
EXCEPTIONS
           NOT_FOUND                   
1
           
OTHERS                      2
                  
.
        
IF SY-SUBRC <> 0.
          
IF SY-SUBRC 01.
            LV_ERFLAG 
'X'.
            WA_ERROR
-LNO LV_EXL_LNO.
            WA_ERROR
-MSG TEXT-023"'Invalid Profit Center, Kindly check it'.
            
APPEND WA_ERROR TO IT_ERROR.
            
CLEAR WA_ERROR.
          
ENDIF.
        
ENDIF.
      
ENDIF.
    
ENDIF.

    
IF WA_FINAL-VENDOR_NO <> ''.
      
READ TABLE IT_LFA1 INTO WA_LFA1 WITH KEY LIFNR WA_FINAL-VENDOR_NO.
      
IF SY-SUBRC <> 0.
        LV_ERFLAG 
'X'.
        WA_ERROR
-LNO LV_EXL_LNO.
        WA_ERROR
-MSG TEXT-024"'Invalid Vendor Number, Kindly Check it'.
        
APPEND WA_ERROR TO IT_ERROR.
        
CLEAR WA_ERRORWA_LFA1.
      
ENDIF.
    
ELSEIF WA_FINAL-CUSTOMER <> ''.
      
READ TABLE IT_KNA1 INTO WA_KNA1 WITH KEY KUNNR WA_FINAL-CUSTOMER.
      
IF SY-SUBRC <> 0.
        LV_ERFLAG 
'X'.
        WA_ERROR
-LNO LV_EXL_LNO.
        WA_ERROR
-MSG TEXT-025"'Invalid Customer Number, Kindly Check it'.
        
APPEND WA_ERROR TO IT_ERROR.
        
CLEAR WA_ERRORWA_KNA1.
      
ENDIF.
    
ENDIF.

    
IF WA_FINAL-GL_ACCOUNT '' AND WA_FINAL-VENDOR_NO '' AND WA_FINAL-CUSTOMER ''.
      LV_ERFLAG 
'X'.
      WA_ERROR
-LNO LV_EXL_LNO.
      WA_ERROR
-MSG TEXT-026"'Please Maintain any one GL Account Number or Vendor Number or Customer Number'.
      
APPEND WA_ERROR TO IT_ERROR.
      
CLEAR WA_ERROR.
    
ENDIF.
*** Validation Begin ***



    FBCJ_HEADER
-COMP_CODE   COM_CODE.
    FBCJ_HEADER
-CAJO_NUMBER CAJO_NUM.
    FBCJ_HEADER
-CURRENCY    TEXT-027"'INR'.
    
IF WA_FINAL-DOC_DATE IS NOT INITIAL.
      FBCJ_HEADER
-DOC_DATE    WA_FINAL-DOC_DATE.
    
ELSE.
      FBCJ_HEADER
-DOC_DATE    WA_FINAL-PSTNG_DATE.
    
ENDIF.
    FBCJ_HEADER
-PSTNG_DATE  WA_FINAL-PSTNG_DATE.
    FBCJ_HEADER
-BP_NAME     WA_FINAL-BP_NAME.

    
IF WA_FINAL-TRANSACT_NUMBER IS NOT INITIAL.
      WA_FBCJ_ITEM
-TRANSACT_NUMBER WA_FINAL-TRANSACT_NUMBER.
      
CONDENSE WA_FBCJ_ITEM-TRANSACT_NUMBER NO-GAPS.
    
ELSE.
      LV_ERFLAG 
'X'.
      WA_ERROR
-LNO LV_EXL_LNO.
      
READ TABLE IT_GET_EXCEL INTO WA_GET_EXCEL WITH KEY ROW LV_LOOP COL '0001'.
      
IF SY-SUBRC 0.
        
CONCATENATE TEXT-028 TEXT-029 WA_GET_EXCEL-VALUE TEXT-029 TEXT-030 INTO WA_ERROR-MSG SEPARATED BY ' '"TEXT-028'Business transcation' TEXT-029'"'  TEXT-030'is not found in excel in system'
      
ENDIF.
      
APPEND WA_ERROR TO IT_ERROR.
      
CLEAR WA_ERROR.
    
ENDIF.

    
IF LV_ERFLAG 'X'.
      
CLEAR WA_ERRORWA_FINALFBCJ_HEADERWA_FBCJ_ITEM.
      
CONTINUE.
    
ENDIF.

    WA_FBCJ_ITEM
-P_RECEIPTS      WA_FINAL-P_RECEIPTS.
    WA_FBCJ_ITEM
-P_PAYMENTS      WA_FINAL-P_PAYMENTS.
    WA_FBCJ_ITEM
-P_NET_AMOUNT    WA_FINAL-P_NET_AMOUNT.
    WA_FBCJ_ITEM
-TAX_CODE        WA_FINAL-TAX_CODE.
    WA_FBCJ_ITEM
-GL_ACCOUNT      WA_FINAL-GL_ACCOUNT.
    WA_FBCJ_ITEM
-POSITION_TEXT   WA_FINAL-POSITION_TEXT.
    WA_FBCJ_ITEM
-VENDOR_NO       WA_FINAL-VENDOR_NO.
    WA_FBCJ_ITEM
-CUSTOMER        WA_FINAL-CUSTOMER.
    WA_FBCJ_ITEM
-COSTCENTER      WA_FINAL-COSTCENTER.
    WA_FBCJ_ITEM
-PROFIT_CTR      WA_FINAL-PROFIT_CTR.
    
APPEND WA_FBCJ_ITEM TO IT_FBCJ_ITEM.

    
CALL FUNCTION 'BAPI_CASHJOURNALDOC_CREATE'
      
EXPORTING
        
HEADER              FBCJ_HEADER
      
IMPORTING
        FISCAL_YEAR         
FISCAL_YEAR
        CASH_JOURNAL_DOC_NO 
CJ_DOC_NO
      
TABLES
        ITEMS               
IT_FBCJ_ITEM
        
RETURN              IT_FBCJ_RETURN.

    
LOOP AT IT_FBCJ_RETURN INTO WA_FBCJ_RETURN WHERE TYPE 'E'.
      LV_ERFLAG 
'X'.
      
CALL FUNCTION 'FORMAT_MESSAGE'
        
EXPORTING
          
ID        WA_FBCJ_RETURN-ID
          LANG      
SY-LANGU
          
NO        WA_FBCJ_RETURN-NUMBER
          V1        
WA_FBCJ_RETURN-MESSAGE_V1
          V2        
WA_FBCJ_RETURN-MESSAGE_V2
          V3        
WA_FBCJ_RETURN-MESSAGE_V3
          V4        
WA_FBCJ_RETURN-MESSAGE_V4
        
IMPORTING
          MSG       
WA_ERROR-MSG
        
EXCEPTIONS
          NOT_FOUND 
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.
      WA_ERROR
-LNO LV_EXL_LNO.
      
APPEND WA_ERROR TO IT_ERROR.

      
CLEAR WA_FBCJ_RETURNWA_ERROR.
    
ENDLOOP.

    
READ TABLE IT_FBCJ_RETURN INTO WA_FBCJ_RETURN WITH KEY TYPE 'E'.
    
IF SY-SUBRC <> AND LV_ERFLAG <> 'X'.
      WA_FINAL
-FISCAL_YEAR FISCAL_YEAR.
      WA_FINAL
-CJ_DOC_NO   CJ_DOC_NO.
      
MODIFY IT_FINAL FROM WA_FINAL INDEX LV_LOOP.
    
ENDIF.
    
CLEAR WA_FBCJ_ITEMWA_FINAL.
    
REFRESH IT_FBCJ_ITEM.
  
ENDLOOP.
ENDFORM.                    " SAVE_DOCUMENT
*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCAT_ERROR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM BUILD_FIELDCAT_ERROR .
  
REFRESH IT_FCAT[].
  
CLEAR WA_FCAT.
  WA_FCAT
-FIELDNAME 'LNO'.
  WA_FCAT
-SELTEXT_L TEXT-031.  "Excel Line No.
  WA_FCAT
-EMPHASIZE 'C610'.
  
APPEND WA_FCAT TO IT_FCAT.

  
CLEAR WA_FCAT.
  WA_FCAT
-FIELDNAME 'MSG'.
  WA_FCAT
-SELTEXT_L TEXT-032.  "Error Message
  
APPEND WA_FCAT TO IT_FCAT.

  WA_LAYOUT
-COLWIDTH_OPTIMIZE 'X'.
ENDFORM.                    " BUILD_FIELDCAT_ERROR
*&---------------------------------------------------------------------*
*&      Form  SHOW_MSG
*&---------------------------------------------------------------------*
FORM SHOW_MSG .

  
IF IT_ERROR IS NOT INITIAL.
    
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      
EXPORTING
        I_CALLBACK_PROGRAM 
SY-REPID
        IS_LAYOUT          
WA_LAYOUT
        IT_FIELDCAT        
IT_FCAT
      
TABLES
        T_OUTTAB           
IT_ERROR
      
EXCEPTIONS
        PROGRAM_ERROR      
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.
  
ENDIF.

  
IF IT_POST IS NOT INITIAL .
    
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      
EXPORTING
        I_CALLBACK_PROGRAM 
SY-REPID
        IS_LAYOUT          
WA_LAYOUT
        IT_FIELDCAT        
IT_FCAT
      
TABLES
        T_OUTTAB           
IT_POST
      
EXCEPTIONS
        PROGRAM_ERROR      
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.
  
ENDIF.
ENDFORM.                    " SHOW_MSG
*&---------------------------------------------------------------------*
*&      Form  POST_DOCUMENT
*&---------------------------------------------------------------------*
FORM POST_DOCUMENT .

  
DATA LV_EXL_LNO(5)        TYPE N.

  
DATA I_COMP_CODE          LIKE TCJ_C_JOURNALS-COMP_CODE,
         I_CAJO_NUMBER        
LIKE TCJ_C_JOURNALS-CAJO_NUMBER,
         I_TYP                
TYPE CJTRANSTYP,
         I_DISPLAY_PERIOD_LO  
LIKE  SY-DATUM,
         I_DISPLAY_PERIOD_HI  
LIKE  SY-DATUM.

  
DATA IT_FBCJ_POST        TYPE STANDARD TABLE OF ISCJ_POSTINGS,
         WA_FBCJ_POST        
LIKE LINE OF IT_FBCJ_POST,
         ITCJ_WTAX_ITEMS     
TYPE STANDARD TABLE OF TCJ_WTAX_ITEMS,
         ITCJ_SPLIT_POSTINGS 
TYPE STANDARD TABLE OF ISCJ_POSTINGS,
         ITCJ_CPD            
TYPE STANDARD TABLE OF TCJ_CPD.

  
DATA E_ERROR_NUMBER LIKE  TCJ_DOCUMENTS-POSTING_NUMBER.

  
DATA P_BEG_BALANCE      TYPE  CJAMOUNT,
         P_TOTAL_RECEIPTS   
TYPE  CJAMOUNT,
         P_TOTAL_PAYMENTS   
TYPE  CJAMOUNT,
         P_TOTAL_CHECKS     
TYPE  CJAMOUNT,
         P_RUN_BALANCE      
TYPE  CJAMOUNT,
         P_RUN_CASH_BALANCE 
TYPE  CJAMOUNT,
         P_NUMB_OF_REC      
TYPE  I,
         P_NUMB_OF_PAYM     
TYPE  I,
         P_NUMB_OF_CHECKS   
TYPE  I.

  I_COMP_CODE 
COM_CODE.
  I_CAJO_NUMBER 
CAJO_NUM.
  
IF R1 'X'.
    I_TYP 
'E'."Payment
  
ELSEIF R2 'X'.
    I_TYP 
'R'"reciept
  
ENDIF.

  
LOOP AT IT_FINAL INTO WA_FINAL.
    
REFRESH IT_FBCJ_POST[].
    LV_EXL_LNO 
SY-TABIX + 1.
    WA_FBCJ_POST
-CAJO_NUMBER CAJO_NUM.
    WA_FBCJ_POST
-COMP_CODE COM_CODE.
    WA_FBCJ_POST
-FISC_YEAR WA_FINAL-FISCAL_YEAR.
    WA_FBCJ_POST
-POSTING_NUMBER WA_FINAL-CJ_DOC_NO.
    WA_FBCJ_POST
-H_RECEIPTS WA_FINAL-P_RECEIPTS.
    WA_FBCJ_POST
-H_PAYMENTS WA_FINAL-P_PAYMENTS.
    
IF R1 'X'.
      WA_FBCJ_POST
-H_NET_AMOUNT WA_FINAL-P_PAYMENTS."PAYMENT
    
ELSEIF R2 'X'.
      WA_FBCJ_POST
-H_NET_AMOUNT WA_FINAL-P_RECEIPTS"RECIEPT
    
ENDIF.
    WA_FBCJ_POST
-BP_NAME =  WA_FINAL-BP_NAME.
    
IF WA_FINAL-DOC_DATE IS NOT INITIAL.
      WA_FBCJ_POST
-DOCUMENT_DATE WA_FINAL-DOC_DATE.
    
ELSE.
      WA_FBCJ_POST
-DOCUMENT_DATE WA_FINAL-PSTNG_DATE.
    
ENDIF.
    WA_FBCJ_POST
-POSTING_DATE WA_FINAL-PSTNG_DATE.
    WA_FBCJ_POST
-DOCUMENT_STATUS 'S'.
    WA_FBCJ_POST
-TAX_CODE WA_FINAL-TAX_CODE.
    WA_FBCJ_POST
-POSITION_TEXT WA_FINAL-POSITION_TEXT.
    WA_FBCJ_POST
-VENDOR_NO WA_FINAL-VENDOR_NO.
    WA_FBCJ_POST
-CUSTOMER WA_FINAL-CUSTOMER.
*    WA_FBCJ_POST-GSBER = WA_FINAL-BUS_AREA.
    WA_FBCJ_POST
-KOSTL WA_FINAL-COSTCENTER.
    WA_FBCJ_POST
-PRCTR WA_FINAL-PROFIT_CTR.
*    WA_FBCJ_POST-BUPLA = WA_FINAL-BUPLA.
*    WA_FBCJ_POST-SECCO = WA_FINAL-SECCO.
    WA_FBCJ_POST
-P_RECEIPTS WA_FINAL-P_RECEIPTS.
    WA_FBCJ_POST
-P_PAYMENTS WA_FINAL-P_PAYMENTS.
    
IF R1 'X'.
      WA_FBCJ_POST
-P_NET_AMOUNT WA_FINAL-P_PAYMENTS."PAYMENT
    
ELSEIF R2 'X'.
      WA_FBCJ_POST
-P_NET_AMOUNT WA_FINAL-P_RECEIPTS"RECIEPT
    
ENDIF.
    WA_FBCJ_POST
-GL_ACCOUNT WA_FINAL-GL_ACCOUNT.

    
READ TABLE IT_TCJ_TRANSN INTO WA_TCJ_TRANSN WITH KEY TRANSACT_NUMBER WA_FINAL-TRANSACT_NUMBER.
    
IF SY-SUBRC 0.
      WA_FBCJ_POST
-TRANSACT_NAME WA_TCJ_TRANSN-TRANSACT_NAME.
    
ENDIF.

    
APPEND WA_FBCJ_POST TO IT_FBCJ_POST.
    
CLEAR WA_FBCJ_POST.

    
CALL FUNCTION 'FCJ_POST_ALL'
      
EXPORTING
        I_COMP_CODE         
I_COMP_CODE
        I_CAJO_NUMBER       
I_CAJO_NUMBER
        I_CURRENCY          
''
        I_TYP               
I_TYP
        I_DISPLAY_PERIOD_LO 
I_DISPLAY_PERIOD_LO
        I_DISPLAY_PERIOD_HI 
I_DISPLAY_PERIOD_HI
      
IMPORTING
        E_ERROR_NUMBER      
E_ERROR_NUMBER
      
TABLES
        ITCJ_POSTINGS       
IT_FBCJ_POST
        ITCJ_WTAX_ITEMS     
ITCJ_WTAX_ITEMS
        ITCJ_SPLIT_POSTINGS 
ITCJ_SPLIT_POSTINGS
        ITCJ_CPD            
ITCJ_CPD
      
CHANGING
        P_BEG_BALANCE       
P_BEG_BALANCE
        P_TOTAL_RECEIPTS    
P_TOTAL_RECEIPTS
        P_TOTAL_PAYMENTS    
P_TOTAL_PAYMENTS
        P_TOTAL_CHECKS      
P_TOTAL_CHECKS
        P_RUN_BALANCE       
P_RUN_BALANCE
        P_RUN_CASH_BALANCE  
P_RUN_CASH_BALANCE
        P_NUMB_OF_REC       
P_NUMB_OF_REC
        P_NUMB_OF_PAYM      
P_NUMB_OF_PAYM
        P_NUMB_OF_CHECKS    
P_NUMB_OF_CHECKS.

    
READ TABLE IT_FBCJ_POST INTO WA_FBCJ_POST INDEX 1.
    
IF WA_FBCJ_POST-DOCUMENT_STATUS  <> 'P'.
      
CALL FUNCTION 'FCJ_DELETE_DOCUMENT'
        
EXPORTING
          I_COMP_CODE      
I_COMP_CODE
          I_CAJO_NUMBER    
I_CAJO_NUMBER
          I_POSTING_NUMBER 
WA_FINAL-CJ_DOC_NO
        
EXCEPTIONS
          NO_DOCUMENT      
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.

      
EXIT.
    
ELSE.
      WA_POST
-LNO LV_EXL_LNO.
      WA_POST
-CJ_DOC_NO WA_FINAL-CJ_DOC_NO.
      WA_POST
-MSG TEXT-033.  "Posted Successfully
      
APPEND WA_POST TO IT_POST.
    
ENDIF.

    
CLEAR WA_FINALWA_POST.

  
ENDLOOP.

ENDFORM.                    " POST_DOCUMENT

*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCAT_POST
*&---------------------------------------------------------------------*
FORM BUILD_FIELDCAT_POST .

  
REFRESH IT_FCAT.
  
CLEAR WA_FCAT.
  WA_FCAT
-FIELDNAME 'LNO'.
  WA_FCAT
-SELTEXT_L TEXT-034.  "Excel Line No.
  WA_FCAT
-EMPHASIZE 'C510'.
  
APPEND WA_FCAT TO IT_FCAT.

  
CLEAR WA_FCAT.
  WA_FCAT
-FIELDNAME 'CJ_DOC_NO'.
  WA_FCAT
-SELTEXT_L TEXT-035.  "Posting Number
  
APPEND WA_FCAT TO IT_FCAT.

  
CLEAR WA_FCAT.
  WA_FCAT
-FIELDNAME 'MSG'.
  WA_FCAT
-SELTEXT_L TEXT-036.  "Message
  
APPEND WA_FCAT TO IT_FCAT.

  WA_LAYOUT
-COLWIDTH_OPTIMIZE 'X'.
ENDFORM.                    " BUILD_FIELDCAT_POST


Share this

Related Posts

2 comments

comments
16 August 2024 at 18:29 delete

Hii vimal raj can you please share the upload excel template
Thanks in advance

Reply
avatar