Showing posts with label Enhancement. Show all posts
Showing posts with label Enhancement. Show all posts

Thursday, April 3, 2025

Billing: Create Custom Outsort Check (Example 2)

Example 2:
FUNCTION ISU_VAL_ZTHOLD1 .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(X_TE327) LIKE  TE327 STRUCTURE  TE327
*"  CHANGING
*"     REFERENCE(XY_OBJ) TYPE  ISU2A_BILLING_DATA
*"     REFERENCE(XY_OUTCNSO) LIKE  ERCHO-OUTCNSO
*"  EXCEPTIONS
*"      GENERAL_FAULT
*"----------------------------------------------------------------------
*  Compare ( Current Bill Amount + Uninvoiced Amount + Account Balance )
*    against Highest and Minimum Amounts
*"----------------------------------------------------------------------
  DATAls_erchz      LIKE erchz,
        lv_nettobtr   LIKE erchz-nettobtr,
        lv_uninvoiced LIKE erchz-nettobtr,
        lv_acc_bal    LIKE erchz-nettobtr,
        lv_outsorted  TYPE c,
        lv_deviation  LIKE ercho-deviation.

  LOOP AT xy_obj-bill-ierchz INTO ls_erchz
          WHERE buchrel 'X'.
    IF ls_erchz-abslkz IS INITIAL.
      lv_nettobtr lv_nettobtr + ls_erchz-nettobtr.
    ENDIF.
  ENDLOOP.

  PERFORM get_uninvoiced_amount USING xy_obj-st-wfkkvkp
                             CHANGING lv_uninvoiced.

  PERFORM get_account_balance USING xy_obj-st-wfkkvkp
                           CHANGING lv_acc_bal.

  lv_nettobtr lv_nettobtr + lv_acc_bal + lv_uninvoiced.

  CALL FUNCTION 'ISU_E25M_BETRW_WAERS_CORRECT'
    EXPORTING
      x_amount          lv_nettobtr
      x_currency        ls_erchz-twaers
    IMPORTING
      y_amount          lv_nettobtr
    EXCEPTIONS
      too_many_decimals 1
      OTHERS            2.
  IF sy-subrc NE 0.
    mac_msg_putx co_msg_error '274' 'AJ' x_te327-validation space
                           space space general_fault.
    IF 2MESSAGE e274(ajWITH spaceENDIF.
  ENDIF.
  break ogoh.
  IF lv_nettobtr > x_te327-value1.
    lv_deviation lv_nettobtr.
    lv_outsorted 'X'.
    mac_msg_putx co_msg_warning '003' 'ZMSG_BI' x_te327-validation xy_obj-bill-erch-belnr
                                            lv_nettobtr x_te327-value1 space.
    IF 2.
      MESSAGE e003(zmsg_biWITH '&' '&' '&' '&'.
*     &1: Bill Doc &2's account balance &3 exceeds threshold $&4
    ENDIF.
  ENDIF.

  IF NOT lv_outsorted IS INITIAL.
    CALL FUNCTION 'ISU_OUTSORT_IERCHO_WRITE'
      EXPORTING
        x_validation x_te327-validation
        x_deviation  lv_deviation
      CHANGING
        xy_iercho    xy_obj-bill-iercho
        xy_outcnso   xy_outcnso.
  ENDIF.
ENDFUNCTION.

Sunday, March 2, 2025

Billing: Create Custom Outsort Check (Example 1)

Example 1:
FUNCTION isu_val_zaccbal1 .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(X_TE327) LIKE  TE327 STRUCTURE  TE327
*"  CHANGING
*"     REFERENCE(XY_OBJ) TYPE  ISU2A_BILLING_DATA
*"     REFERENCE(XY_OUTCNSO) LIKE  ERCHO-OUTCNSO
*"  EXCEPTIONS
*"      GENERAL_FAULT
*"----------------------------------------------------------------------
  DATAls_erchz      LIKE erchz,
        lv_nettobtr   LIKE erchz-nettobtr,
        lv_uninvoiced LIKE erchz-nettobtr,
        lv_acc_bal    LIKE erchz-nettobtr,
        lv_outsorted  TYPE c,
        lv_deviation  LIKE ercho-deviation.

  LOOP AT xy_obj-bill-ierchz INTO ls_erchz
          WHERE buchrel 'X'.
    IF ls_erchz-abslkz IS INITIAL.
      lv_nettobtr lv_nettobtr + ls_erchz-nettobtr.
    ENDIF.
  ENDLOOP.

  PERFORM get_uninvoiced_amount USING xy_obj-st-wfkkvkp
                             CHANGING lv_uninvoiced.

  PERFORM get_account_balance USING xy_obj-st-wfkkvkp
                           CHANGING lv_acc_bal.

  lv_nettobtr lv_nettobtr + lv_acc_bal + lv_uninvoiced.

  CALL FUNCTION 'ISU_E25M_BETRW_WAERS_CORRECT'
    EXPORTING
      x_amount          lv_nettobtr
      x_currency        ls_erchz-twaers
    IMPORTING
      y_amount          lv_nettobtr
    EXCEPTIONS
      too_many_decimals 1
      OTHERS            2.
  IF sy-subrc NE 0.
    mac_msg_putx co_msg_error '274' 'AJ' x_te327-validation space
                           space space general_fault.
    IF 2MESSAGE e274(ajWITH spaceENDIF.
  ENDIF.
  break ogoh.
  IF lv_nettobtr < x_te327-value1 OR
     lv_nettobtr > x_te327-value2.
    lv_deviation lv_nettobtr.
    lv_outsorted 'X'.
    mac_msg_putx co_msg_warning '002' 'ZMSG_BI' x_te327-validation xy_obj-bill-erch-belnr
                                            xy_obj-st-wfkkvkp-vkont lv_outsorted space.
    IF 2.
      MESSAGE e002(zmsg_biWITH '&' '&' '&' '&'.
*   &1: Bill Doc &2 Contract Account &3 is outsorted due to acc bal &4
    ENDIF.
  ENDIF.

  IF NOT lv_outsorted IS INITIAL.
    CALL FUNCTION 'ISU_OUTSORT_IERCHO_WRITE'
      EXPORTING
        x_validation x_te327-validation
        x_deviation  lv_deviation
      CHANGING
        xy_iercho    xy_obj-bill-iercho
        xy_outcnso   xy_outcnso.
  ENDIF.
ENDFUNCTION.

*&---------------------------------------------------------------------*
*&      Form  GET_UNINVOICED_AMOUNT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IS_FKKVKP     Contract Account
*      <--cv_uninv_amt mount="" nbsp="" ninvoiced="" span="">
*----------------------------------------------------------------------*
FORM get_uninvoiced_amount  USING    is_fkkvkp  TYPE  fkkvkp
                            CHANGING cv_uninv  TYPE nettobtr.
  DATA:
    ls_eitr    TYPE eitr,
    lt_eitr    TYPE STANDARD TABLE OF eitr,
    ls_erch    TYPE erch,
    lt_erch    TYPE STANDARD TABLE OF erch,
    ls_dberchz TYPE dberchz,
    lt_dberchz TYPE STANDARD TABLE OF dberchz.

  SELECT INTO TABLE lt_eitr FROM eitr
    WHERE vkont       EQ is_fkkvkp-vkont
      AND simulation  EQ space
      AND tobreleasd  EQ space.
  IF sy-subrc IS INITIAL.
    IF lt_eitr IS NOT INITIAL.
      SELECT INTO TABLE lt_erch FROM erch
        FOR ALL ENTRIES IN lt_eitr
        WHERE belnr     EQ lt_eitr-belnr.
      IF lt_erch IS NOT INITIAL.
        SELECT INTO CORRESPONDING FIELDS OF TABLE lt_dberchz
            FROM dberchz1 AS JOIN dberchz3 AS e
              ON i~belnr    EQ e~belnr
             AND i~belzeile EQ e~belzeile
          FOR ALL ENTRIES IN lt_erch
          WHERE i~belnr   EQ lt_erch-belnr
            AND i~buchrel EQ 'X'.
      ENDIF.
    ENDIF.
  ENDIF.
  LOOP AT lt_dberchz INTO ls_dberchz.
    cv_uninv cv_uninv + ls_dberchz-nettobtr.
  ENDLOOP.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  GET_ACCOUNT_BALANCE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IS_FKKVKP     Contract Account
*      <--cv_acc_bal alance="" ccount="" nbsp="" span="">
*----------------------------------------------------------------------*
FORM get_account_balance  USING    is_fkkvkp   TYPE fkkvkp
                          CHANGING cv_acc_bal  TYPE nettobtr.
  DATA:
    ls_bal TYPE bapiaccbalances,
    lt_bal TYPE STANDARD TABLE OF bapiaccbalances.

  CALL FUNCTION 'BAPI_ACCOUNT_GETBALANCES'
    EXPORTING
      contractaccount  is_fkkvkp-vkont
      partner          is_fkkvkp-gpart
    TABLES
      account_balances lt_bal.
  LOOP AT lt_bal INTO ls_bal.
    CASE ls_bal-text.
      WHEN 'Open'.
        cv_acc_bal cv_acc_bal + ls_bal-withd_val.
      WHEN 'Credit'.
        cv_acc_bal cv_acc_bal ls_bal-withd_val.
    ENDCASE.
  ENDLOOP.
ENDFORM.

    Saturday, March 1, 2025

    Billing: Custom Variant: Example 1: Get Account Balance

    FUNCTION isu_zaccbal.
    *"----------------------------------------------------------------------
    *"*"Local Interface:
    *"  IMPORTING
    *"     REFERENCE(X_RED) TYPE  ISU2A_REDUCED_BILLING_DATA
    *"     REFERENCE(X_OP) TYPE  ISU2A_VARIANT_PARAMETERS
    *"     REFERENCE(X_SS) TYPE  ISU2A_SS
    *"  CHANGING
    *"     REFERENCE(XY_OBJ) TYPE  ISU2A_BILLING_DATA
    *"     REFERENCE(XY_SOBJ) TYPE  ISU2A_DATA_COLLECTOR
    *"  EXCEPTIONS
    *"      GENERAL_FAULT
    *"      REGULAR_ERROR
    *"----------------------------------------------------------------------
    *---------------------------------------------------*
    *                     DATA
    *---------------------------------------------------*
      INCLUDE ievarbasic.

      DATA:
        ls_zone         TYPE isu2a_zone,
        ls_fkkvkp       TYPE fkkvkp,
        lv_bal          TYPE bapicurr_d,
        ls_acc_bal      TYPE bapiaccbalances,
        lt_acc_bal      TYPE STANDARD TABLE OF bapiaccbalances.
    *---------------------------------------------------*
    *                     LOGIC
    *---------------------------------------------------*
    *---------------------------------------------------*
    *           Getting Account Balance
    *---------------------------------------------------*
      MOVE-CORRESPONDING x_ss TO werchz.
      "Initialize Output Operand 1
    mac_init_output co_cont2 x_op-o1.
      SELECT SINGLE INTO ls_fkkvkp FROM fkkvkp WHERE vkont EQ xy_sobj-ever-vkonto.
      IF sy-subrc IS INITIAL.
        CALL FUNCTION 'BAPI_ACCOUNT_GETBALANCES'
          EXPORTING
            contractaccount  xy_sobj-ever-vkonto
            partner          ls_fkkvkp-gpart
          TABLES
            account_balances lt_acc_bal.
        LOOP AT lt_acc_bal INTO ls_acc_bal.
          CASE ls_acc_bal-text.
            WHEN 'Open'.
              lv_bal lv_bal + ls_acc_bal-withd_val.
            WHEN 'Credit'.
              lv_bal lv_bal ls_acc_bal-withd_val.
          ENDCASE.
        ENDLOOP.
      ENDIF.
      MOVE-CORRESPONDING werchz TO out_oper.
      out_oper-qnt_new lv_bal.
      out_oper-qnt_old lv_bal
    "Updating operands into table IOPER, so that operand value is accessible in other schema steps
    mac_output_ioper x_op-o1.  

      werchz-nettobtr lv_bal.
      ls_zone-operab  werchz-ab.
      ls_zone-z       werchz.
      APPEND ls_zone TO izone.
    "Output results to invoice, prequisites, OUT_OPER and IZONE
    mac_invoice_line co_dltyp01

    ENDFUNCTION.

    Wednesday, October 2, 2019

    Billing: Create Custom Outsort Check (Part 2)


    1. Create custom function module starts with ISU_VAL_<Biling Validation Name>  with the following parameters:
      • Importing
        • X_TE327                LIKE TE327
      • Changing 
        • XY_OBJ                 TYPE ISU2A_BILLING_DATA
        • XY_OUTCNSO LIKE ERCHO-OUTCNSO
      • Exceptions
        • GENERAL_FAULT
    2. Validation parameters are set in X_TE327
    3. Call FM 'ISU_OUTSORT_IERCHO_WRITE' if needed to outsort the billing document
    4. Please refer to the following on how to configure outsorting check groups for billing:

    Friday, August 24, 2012

    Event R721 Restrict Contracts during creation of disconnection / reconnection order

    In one of our implementation, we create disconnect order in background by calling FM 'ISU_S_DISCDOC_CHANGE', and we have troubles in deciding which devices to be disconnected and some devices are not even exist in the itab XY_DVALOBJ (BAPI Definition ISU_SERVORD_EDCN, Method SET_ORDERCODE_DISCONNECT) for you to choose.

    SAP use a unique way to find out Contracts that can be disconnected. Basically SAP will check contracts that are listed in the open overdue items against dunning activities (FKKMAKT), and then check against Dunning History (FKKMAZE), if records exist, the contract can be disconnected. Thus, sometimes when you reverse the dunning and then re-run it again, some other devices are available for you to choose again, it's kind of magic. Lol~

    Anyway, this can be resolved by enhancing event R721, you can either refresh itab T_DISC_EVER, so that all contracts are taken into consideration, or, define your own logic to include more contracts to T_DISC_EVER.

    Technical Information:
    Program: SAPLES34
    Include Program: LES34F46
    Form: DCORD_DARK_PBO
    Then,
    FM 'ISU_DISCDOC_ENVIRONMENT_SELECT'
    Then,
    FM 'ISU_GET_EVER_DISC_RELEVANT'
    Then,
    FM 'ISU_DB_GET_POS_DISC_RELEVANT'

    Monday, April 16, 2012

    Loans (Part 4) Event 2900

    Event: 2900 Loan: Calculate Repayment Plan
    Sample FM: FKK_SAMPLE_2900_NEW


    The function module processed at event 2900 calculates a repayment plan for a loan on the basis of the transfer parameters.


    The module creates the repayment plan in table TFKKLOAN and returns it to the calling program.
    Field FAEDN of table FKKLOAN contains the due dates of the individual loan installments. This module calculates the fields INTEREST (interest amount), REPAYMENT (repayment amount), ANNUITY (Annuity = interest amount + repayment amount) for the individual due dates.


    Make sure that the condition INTEREST + REPAYMENT = ANNUITY is filled for all due dates.
    In addition, the total of the amounts in the field REPAYMENT must agree with the total loan (import parameter I_LOANAMOUNT).


    Notes:
    1. Monthly Duration = 30 days
    2. IF interval type = monthly and the interval = 1. The formula is
      • Loan Monthly Payment Amount = [ rate + ( rate / { [ (1 + rate ) ^ months ] - 1 } ) ] * principal
      • rate = annual percentage rate / 1200   
    3. ELSE
      • Get estimate roughly by triangle hypothesis - total interest amount
      • Total Interest Amount = [ ( principal /2 ) * rate * ( duration / 360 ) ] 
      • Payment Amount per Interval = [ Total Interest Amount + Principal Amount ] / Number of Installments

    Monday, February 1, 2010

    Pop Up Error Message

    In most of the cases, we are using keyword Message to display desired message at the message area of status bar. However, in certain scenarios, these message will be overidden by other message at upper level of calling function module.

    For instance, Program A calls Function Module B and it calls Function Module C, C raised an important exception but B either override it with other message or suppress the message. Thus, the user lost the important message from Function Module C.

    One of the solution this kind of scenario would be displaying the message with pop up function module.
    1. Getting the error message after calling function module
    - Perform a select statement from table T100
        IF sy-subrc <> 0.
    *   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
       
        DATA: lv_err_msg TYPE STRING.
     

        SELECT SINGLE text FROM t100 INTO lv_err_msg
         WHERE sprsl EQ sy-langu
           AND arbgb EQ sy-msgid
           AND msgnr EQ sy-msgno.   

        ENDIF.
     
    OR

    - Write the error message into a string data object.

      IF sy-subrc <> 0.
    *   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
            
        DATA: lv_err_msg TYPE STRING.

        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 INTO
              lv_err_msg.

        ENDIF.

    2. Display the error message with pop up function module. Our recomendations in this scenario would be: