SAP HCM ABAP PRESTAMOS

From SapWiki
Revision as of 20:59, 29 March 2020 by WikiSysop (talk | contribs)
  • cl_hrpa_loan_computations=>compute_estimated_end_of_loan
  • cl_hrpa_loan_computations=>read_payments_for_loan_id
  • cl_hrpa_loan_computations=>compute_repayment_plan

EJEMPLO 01

*&---------------------------------------------------------------------*
*& Report  Y_TEST_LOAN
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  y_test_loan.
TABLES pernr.
INFOTYPES: 0045.
DATA instance    TYPE REF TO cl_hrpa_loan_computations.
DATA gd_pclo_id TYPE pclo_id.
DATA gd_estimated_end_of_loan TYPE d.
DATA  data_ref   TYPE REF TO data.
DATA gd_message_handler TYPE REF TO if_hrpa_message_handler.
data gt_pagos type PCLO_PAYMENTS_TAB.
data gt_P0078_TAB type P0078_TAB.
data: gt_PCLO_REPAY_TAB type PCLO_REPAY_TAB,
     gt_PCLO_LOAN_CHANGES_TAB type PCLO_LOAN_CHANGES_TAB.
* create dummy message handler
 data dummy_handler type ref to cl_hrpa_dummy_message_handler.
 create object dummy_handler.
 message_handler = dummy_handler.
START-OF-SELECTION.
GET pernr.
 PROVIDE * FROM p0045 BETWEEN pn-begda AND pn-endda.
   CALL METHOD cl_hrpa_loan_computations=>get_loan_id_from_p0045
     EXPORTING
       p0045   = p0045
     RECEIVING
       loan_id = gd_pclo_id.
* CALCULO DE FECHA FINAL PRESTAMO
   TRY.
  CALL METHOD cl_hrpa_loan_computations=>compute_estimated_end_of_loan
       EXPORTING
         tclas                 = 'A'
         loan_id               = gd_pclo_id
         message_handler       = dummy_handler
       RECEIVING
         estimated_end_of_loan = gd_estimated_end_of_loan.
   ENDTRY.
* PAGOS DE PRESTAMO
TRY.
CALL METHOD cl_hrpa_loan_computations=>read_payments_for_loan_id
  EXPORTING
    tclas         = 'A'
    loan_id       = gd_pclo_id
*     ZAHLA         = '*'
*     SPRPS         = '*'
*     BEGDA         = LOW_DATE
*     ENDDA         = HIGH_DATE
*     NO_AUTH_CHECK = FALSE
  IMPORTING
    PAYMENTS_TAB  = gt_pagos
*     DATA_EXISTS   =
*     MISSING_AUTH  =
    .
 CATCH CX_HRPA_VIOLATED_ASSERTION .
ENDTRY.


TRY.
CALL METHOD cl_hrpa_loan_computations=>read_p0078_for_loan_id
 EXPORTING
   tclas         = 'A'
   loan_id       = gd_pclo_id
*    ZAHLA         = '*'
*    SPRPS         = '*'
*    BEGDA         = LOW_DATE
*    ENDDA         = HIGH_DATE
*    NO_AUTH_CHECK = FALSE
 IMPORTING
   P0078_TAB     = gt_P0078_TAB
*    DATA_EXISTS   =
*    MISSING_AUTH  =
   .
 CATCH CX_HRPA_VIOLATED_ASSERTION .
ENDTRY.
* GENERAR PLAN DE AMORTIZACION
TRY.
CALL METHOD cl_hrpa_loan_computations=>compute_repayment_plan
 EXPORTING
   tclas           = 'A'
   loan_id         = gd_pclo_id
*    NEW_CURRENCY    =
   message_handler = dummy_handler
 IMPORTING
   REPAYMENT_PLAN  =  gt_PCLO_REPAY_TAB
   LOAN_CHANGES    =  gt_PCLO_LOAN_CHANGES_TAB
*    IS_OK           =
   .
 CATCH CX_HRPA_VIOLATED_ASSERTION .
ENDTRY.
endPROVIDE.
END-OF-SELECTION.

Calculo fecha fin de préstamo

FORM calcula_fecha_fin using p0045 type p0045 
                    CHANGING p_endda.
  TYPES: BEGIN OF tp_dynfi,
           initf TYPE c,                " initialize if marked
           key   LIKE sy-tabix,
           darbt LIKE p0078-betrg,
           dbtcu LIKE p0078-btrcu,      "currency for repayment plan
           rate  LIKE p0045-anrte,
           ratec LIKE p0045-dbtcu,
           tilbg LIKE p0045-tilbg,
           zahld LIKE p0078-zahld,
           vari  TYPE disvariant,
         END   OF tp_dynfi.

  DATA ps_dynfi TYPE tp_dynfi.
  DATA: lt_p0078 TYPE p0078 OCCURS 0 WITH HEADER LINE.
  DATA lt_p0045 TYPE TABLE OF p0045 WITH HEADER LINE.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_p0078 FROM pa0078 WHERE pernr = p0045-pernr AND
                                                 dlart = p0045-dlart AND
                                                 objid = p0045-objps.

  ps_dynfi-rate = p0045-anrte.
  ps_dynfi-ratec = p0045-dbtcu.
  ps_dynfi-tilbg = p0045-begda.
  ps_dynfi-dbtcu = p0045-dbtcu.

  CALL FUNCTION 'PCLO_BUILD_REPAYMENT_PLAN'
    EXPORTING
      ps_p0045    = p0045
      pf_fday     = p0045-begda
      pf_lday     = p0045-endda    ""'99991231'
      pt_paym     = lt_p0078[]
      pf_rate     = ps_dynfi-rate
      pf_ratecurr = ps_dynfi-ratec                          "GWY534928
      pf_tilbg    = ps_dynfi-tilbg
      pf_curr     = ps_dynfi-dbtcu                    "GWY428607-5
      pf_variant  = ps_dynfi-vari
      pf_nodisp   = '1'
    IMPORTING
*     ps_paper    = gs_paper
      pf_dlend    = p0045-dlend
*     pf_effin    = gf_effin
      pt_plan     = gt_pclo_repay_tab.   "amounts have currency pf_curr

  p_endda = p0045-dlend.

ENDFORM.                    "CALCULa_fecha_fin

Obtener datos de préstamo (copiado de MP004500)

FORM get_loan_results TABLES   pt_savp STRUCTURE gt_savp
                      USING    pf_pernr
                               pf_dlart
                               pf_objps
                               pf_dbtcu
                      CHANGING pfx_opelo
                               pfx_paylo
                               pfx_abrdt
                               pfx_subrc.

  DATA: ltx_rgdir             LIKE  pc261 OCCURS 0 WITH HEADER LINE,
        lt_rgdir              TYPE hrpy_tt_rgdir,           "GWY839032
        lsx_rgdir             LIKE  ltx_rgdir,
        lsx_result            TYPE  pay99_result,
        lsx_v0                LIKE LINE OF lsx_result-inter-v0,
        lsx_rt                LIKE LINE OF lsx_result-inter-rt,
        lfx_relid             LIKE  pcl2-relid,
        lfx_molga             LIKE  t500l-molga,
        lsx_t500l             LIKE  t500l,
        lfx_amount            LIKE  q0045-darbt,
        lfx_line              LIKE  sy-tabix,
        lfx_fday              TYPE  d,
        lfx_lday              TYPE  d,
        lfx_opecu             LIKE  q0045-opecu,
        ls_paper              TYPE  pc2paper,
        lf_payment_value_date TYPE datum,
        ls_loid_33            TYPE  pclo_id.                      "Note 834827

*##--BEGIN--##
  CLEAR: pfx_opelo, pfx_paylo, pfx_abrdt, pfx_subrc.

  CALL FUNCTION 'CU_READ_RGDIR_NEW'
    EXPORTING
      persnr               = pf_pernr
      check_read_authority = space
    IMPORTING
      molga                = lfx_molga
    TABLES
      in_rgdir             = ltx_rgdir
    EXCEPTIONS
      OTHERS               = 1.
  IF sy-subrc <> 0.
    MOVE sy-subrc TO pfx_subrc.
  ELSE.
    MOVE sy-subrc TO pfx_subrc.
    CALL FUNCTION 'CD_READ_LAST_RESULT_IN_RGDIR'
      IMPORTING
        ptx_rgdir_nv = lsx_rgdir
      TABLES
        pt_rgdir     = ltx_rgdir.
    LOOP AT ltx_rgdir.
      APPEND ltx_rgdir TO lt_rgdir.
    ENDLOOP.
    MOVE lsx_rgdir-fpend TO pfx_abrdt.
*   get relid
    SELECT SINGLE * FROM   t500l
                    INTO   lsx_t500l
                    WHERE  molga  = lfx_molga.
    MOVE lsx_t500l-relid TO lfx_relid.

    CALL FUNCTION 'PYXX_READ_PAYROLL_RESULT'
      EXPORTING
        clusterid               = lfx_relid
        employeenumber          = pf_pernr
        sequencenumber          = lsx_rgdir-seqnr
        read_only_international = 'X'
        check_read_authority    = ' '
      CHANGING
        payroll_result          = lsx_result
      EXCEPTIONS
        OTHERS                  = 1.
    IF sy-subrc <> 0.
      MOVE sy-subrc TO pfx_subrc.
    ELSE.
      MOVE sy-subrc TO pfx_subrc.
*     get loan result .....
      LOOP AT lsx_result-inter-v0 INTO lsx_v0 WHERE v0typ = 'L'.
        IF pf_dlart = lsx_v0-vinfo+0(4) AND
           pf_objps = lsx_v0-vinfo+4(2).
          LOOP AT lsx_result-inter-rt INTO lsx_rt
                                      WHERE v0typ = 'L'
                                      AND v0znr = lsx_v0-v0znr
                                      AND   lgart = '/LLB'.

            MOVE: lsx_result-inter-versc-waers TO lfx_opecu,
                  lsx_rt-betrg                 TO pfx_opelo.

*            PERFORM convert_to_local_currency
*                      USING    pfx_opelo
*                               lfx_opecu
*                               pf_dbtcu
*                               pfx_abrdt
*                               'W' "called from PBO
*                               SPACE   "use BAPI for conversion
*                               pf_pernr
*                               lfx_molga
*                               lt_rgdir
*                      CHANGING pfx_opelo.
            EXIT.
          ENDLOOP.
          IF sy-subrc <> 0.            "no open loan
            MOVE '9' TO pfx_subrc.
            EXIT. "the loop                                 "GWY458200
          ELSE.                                             "GWY458200
            MOVE '0' TO pfx_subrc.     "found open loan     "GWY458200
            EXIT. "the loop                                 "GWY458200
          ENDIF.
        ELSE.                 "this loan is not yet processed in payroll
          MOVE '7' TO pfx_subrc.
        ENDIF.
      ENDLOOP.

*     get the correct payment for loans paid back already:
*       in a productive system one can assume that a loan with
*       q0045-begdaf < pfx_abrdt will be eather be open or paid back
      CHECK p0045-begda < pfx_abrdt.    "#EC PORTABLE        "GWY458200
*     calculate ammount beeing payed so far
      PERFORM clear_ltrgdir TABLES ltx_rgdir.
      DESCRIBE TABLE ltx_rgdir LINES lfx_line.
      SORT ltx_rgdir ASCENDING BY seqnr.
      READ TABLE ltx_rgdir INDEX 1.
      lfx_fday = ltx_rgdir-fpbeg.
      READ TABLE ltx_rgdir INDEX lfx_line.
      lfx_lday = ltx_rgdir-fpend.

      SORT pt_savp ASCENDING BY zahld.
      LOOP AT pt_savp WHERE pydir = 'O'
                      AND   sprps = space.
        IF pt_savp-zahld >= lfx_fday AND
           pt_savp-zahld <= lfx_lday.

          LOOP AT ltx_rgdir WHERE fpbeg <= pt_savp-zahld
                              AND fpend >= pt_savp-zahld.
            EXIT.
          ENDLOOP.

          IF sy-subrc NE 0.                                 "JLA1532930
            CONTINUE.
          ENDIF.

          ls_paper-pabrj = ltx_rgdir-fpper(4).
          ls_paper-pabrp = ltx_rgdir-fpper+4(2).

          CALL FUNCTION 'PCLO_GET_VALUE_DATE_PAYMENT'       "GWY839032
            EXPORTING
              pf_zahla              = pt_savp-zahla
              pf_payment_begda      = pt_savp-zahld
              pf_payment_endda      = pt_savp-endda
              pf_molga              = lfx_molga
              pf_abkrs              = ltx_rgdir-abkrs
*             PF_ABKRS_BEGDA        = '19000101'
*             PF_ABKRS_ENDDA        = '99991231'
              pf_paper              = ls_paper
            IMPORTING
              pfx_value_date        = lf_payment_value_date
            EXCEPTIONS
              error                 = 1
              payment_not_in_period = 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.

*          PERFORM convert_to_local_currency
*                    USING    pt_savp-betrg
*                             pt_savp-btrcu
*                             pf_dbtcu
*                             lf_payment_value_date
*                             'W' "called from PBO
*                             SPACE   "use BAPI for conversion
*                             pf_pernr
*                             lfx_molga
*                             lt_rgdir
*                    CHANGING pt_savp-betrg.
          ADD pt_savp-betrg TO lfx_amount.
        ENDIF.
      ENDLOOP.
      MOVE lfx_amount TO pfx_paylo.
    ENDIF.
  ENDIF.
ENDFORM.                               " GET_LOAN_RESULTS