Google Search - Blog...........

SAP ABAP - Quota Update For pa2006.

REPORT zhr_quota_update.


TABLES: pa0000.

TYPES: BEGIN OF ty_pa0000,
pernr TYPE pa0000-pernr,
begda TYPE pa0000-begda,
endda TYPE pa0000-endda,
massn TYPE pa0000-massn,
massg TYPE pa0000-massg,
stat2 TYPE pa0000-stat2,
END OF ty_pa0000.

TYPES: BEGIN OF ty_pa0001,
pernr TYPE pa0001-pernr,
begda TYPE pa0001-begda,
endda TYPE pa0001-endda,
END OF ty_pa0001.

TYPES: BEGIN OF ty_pa0016,
pernr TYPE pa0016-pernr,
begda TYPE pa0016-begda,
endda TYPE pa0016-endda,
zquota TYPE pa0016-zquota,
END OF ty_pa0016.

TYPES: BEGIN OF ty_pa2001,
pernr TYPE pa2001-pernr,
begda TYPE pa2001-begda,
endda TYPE pa2001-endda,
awart TYPE pa2001-awart,
abrtg TYPE pa2001-abrtg,
END OF ty_pa2001.

TYPES: BEGIN OF ty_prop_values.
INCLUDE STRUCTURE pprop.
TYPES: END OF ty_prop_values.

TYPES: BEGIN OF ty_mod_values.
INCLUDE STRUCTURE pskey.
TYPES: END OF ty_mod_values.

TYPES: BEGIN OF ty_t508a.
INCLUDE STRUCTURE t508a.
TYPES:END OF ty_t508a.

TYPES: BEGIN OF ty_t559e,
qtype TYPE t559e-qtype,
rulno TYPE t559e-rulno,
lvnum TYPE t559e-lvnum,
END OF ty_t559e.

TYPES: BEGIN OF ty_pa2006,
pernr TYPE pa2006-pernr,
subty TYPE pa2006-subty,
begda TYPE pa2006-begda,
endda TYPE pa2006-endda,
ktart TYPE pa2006-ktart,
END OF ty_pa2006.

DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.
DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.

DATA: it_prop_values TYPE TABLE OF ty_prop_values WITH HEADER LINE,
it_mod_values TYPE TABLE OF ty_mod_values WITH HEADER LINE,
it_pa0000 TYPE TABLE OF ty_pa0000 WITH HEADER LINE,
it_pa0001 TYPE TABLE OF ty_pa0001 WITH HEADER LINE,
it_pa0016 TYPE TABLE OF ty_pa0016 WITH HEADER LINE,
it_pa2001 TYPE TABLE OF ty_pa2001 WITH HEADER LINE, "DEVK906397
it_pa2006 TYPE TABLE OF ty_pa2006 WITH HEADER LINE, "DEVK906397
it_t559e TYPE TABLE OF ty_t559e WITH HEADER LINE,
it_return TYPE bapireturn.

DATA: i_month TYPE fcmnr,
i_year TYPE gjahr,
i_date TYPE sy-datum,
e_first_day TYPE sy-datum,
e_last_day TYPE sy-datum,
v_date1 TYPE sy-datum,
v_date2 TYPE sy-datum,
v_date3 TYPE sy-datum,
v_date4 TYPE sy-datum,
v_first TYPE sy-datum,
v_last TYPE sy-datum,
v_date5 TYPE vtbbewe-dbervon,
v_date6 TYPE vtbbewe-dbervon,
v_days(10) TYPE c,
v_months(10) TYPE c,
v_years(10) TYPE c,
v_days1 TYPE vtbbewe-atage,
v_months1 TYPE vtbbewe-atage,
v_years1 TYPE vtbbewe-atage,
v_mult TYPE p2006-anzhl,
v_val TYPE char10,
v_total(10) TYPE c,
v_mdate1 TYPE sy-datum,
v_mdate2 TYPE sy-datum,
v_mdate3 TYPE sy-datum.
data: v_mdate6 TYPE sy-datum,
v_mdate7 TYPE sy-datum,
v_mdate8 TYPE sy-datum,
v_mdate9 TYPE sy-datum,
v_ext(15) TYPE c,
v_quota1 TYPE t559e-lvnum,
v_quota2 TYPE t559e-lvnum,
v_quota3 TYPE t559e-lvnum.

ranges: r_massn for p0000-massn,
r_massg for p0000-massg.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS : s_pernr FOR pa0000-pernr.
PARAMETERS : p_date TYPE sy-datum .
SELECTION-SCREEN END OF BLOCK b1.

INITIALIZATION.
p_date = sy-datum.
START-OF-SELECTION.

SELECT qtype
rulno
lvnum
FROM t559e
INTO TABLE it_t559e
WHERE mopgk = '1'
AND mozko = '01'
AND mobde = '01'.

IF sy-subrc = 0.
SORT it_t559e BY qtype.
ENDIF.

READ TABLE it_t559e WITH KEY qtype = '03'.
IF sy-subrc = 0.
v_quota1 = it_t559e-lvnum.
ENDIF.

READ TABLE it_t559e WITH KEY qtype = '04'.
IF sy-subrc = 0.
v_quota2 = it_t559e-lvnum.
ENDIF.

READ TABLE it_t559e WITH KEY qtype = '24'.
IF sy-subrc = 0.
v_quota3 = it_t559e-lvnum.
ENDIF.

r_massn-low = '01'.
r_massn-sign = 'I'.
r_massn-option = 'EQ'.
APPEND r_massn.

r_massn-low = '28'.
APPEND r_massn.

r_massn-low = '27'.
APPEND r_massn.

r_massg-low = '01'.
r_massg-sign = 'I'.
r_massg-option = 'EQ'.
APPEND r_massg.

r_massg-low = '28'.
APPEND r_massg.

r_massg-low = '27'.
APPEND r_massg.

r_massg-low = '40'.
APPEND r_massg.


SELECT pernr
begda
endda
massn
massg
FROM pa0000
INTO TABLE it_pa0000
WHERE pernr IN s_pernr
AND massn IN r_massn
AND massg IN r_massg
AND stat2 = '3'.

IF sy-subrc = 0.

SORT it_pa0000 BY pernr.

SELECT pernr
begda
endda
FROM pa0001
INTO TABLE it_pa0001
FOR ALL ENTRIES IN it_pa0000
WHERE pernr = it_pa0000-pernr.

IF sy-subrc = 0.
SORT it_pa0001 BY pernr.
DELETE ADJACENT DUPLICATES FROM it_pa0001 COMPARING pernr.
ENDIF.

SELECT pernr
begda
endda
zquota
FROM pa0016
INTO TABLE it_pa0016
FOR ALL ENTRIES IN it_pa0000
WHERE pernr = it_pa0000-pernr
AND endda = '99991231'.

IF sy-subrc = 0 .
SORT it_pa0016 BY pernr.
ENDIF.

SELECT pernr
begda
endda
awart
abrtg
FROM pa2001
INTO TABLE it_pa2001
FOR ALL ENTRIES IN it_pa0000
WHERE pernr = it_pa0000-pernr
AND subty = '0100'
OR subty = '0200'.

IF sy-subrc = 0.
SORT it_pa2001 BY pernr.
ENDIF.

SELECT pernr
subty
begda
endda
ktart
FROM pa2006
INTO TABLE it_pa2006
FOR ALL ENTRIES IN it_pa0016
WHERE pernr = it_pa0016-pernr
AND begda LE it_pa0016-endda
AND endda GE it_pa0016-begda.

IF sy-subrc = 0.
SORT it_pa2006 BY pernr.
ENDIF.
ENDIF.

END-OF-SELECTION.

v_date1+0(4) = sy-datum+0(4).
v_date1+4(4) = '0101'.

CALL FUNCTION 'OIL_MONTH_GET_FIRST_LAST'
EXPORTING
i_month = '01'
i_year = sy-datum+0(4)
i_date = sy-datum
IMPORTING
e_first_day = e_first_day
e_last_day = v_date1
EXCEPTIONS
wrong_date = 1
OTHERS = 2.

v_date2+0(4) = sy-datum+0(4).
v_date2+4(4) = '1212'.

CALL FUNCTION 'OIL_MONTH_GET_FIRST_LAST'
EXPORTING
i_month = '12'
i_year = sy-datum+0(4)
i_date = sy-datum
IMPORTING
e_first_day = v_date2
e_last_day = e_last_day
EXCEPTIONS
wrong_date = 1
OTHERS = 2.



v_mdate1+4(4) = e_first_day+0(4).
v_mdate1+2(2) = e_first_day+4(2).
v_mdate1+0(2) = e_first_day+6(2).

v_mdate2+4(4) = e_last_day+0(4).
v_mdate2+2(2) = e_last_day+4(2).
v_mdate2+0(2) = e_last_day+6(2).

v_mdate7+4(4) = '9999'.
v_mdate7+2(2) = '12'.
v_mdate7+0(2) = '31'.

IF p_date = sy-datum.
v_mdate6+4(4) = sy-datum+0(4).
v_mdate6+2(2) = sy-datum+4(2).
v_mdate6+0(2) = sy-datum+6(2).
ELSE.
v_mdate6+4(4) = p_date+0(4).
v_mdate6+2(2) = p_date+4(2).
v_mdate6+0(2) = p_date+6(2).
ENDIF.



LOOP AT it_pa0001.

READ TABLE it_pa0016 WITH KEY pernr = it_pa0001-pernr.
IF sy-subrc = 0.

v_mdate3+4(4) = it_pa0016-begda+0(4).
v_mdate3+2(2) = it_pa0016-begda+4(2).
v_mdate3+0(2) = it_pa0016-begda+6(2).

v_date3 = it_pa0016-begda.
v_date4 = sy-datum.

CALL FUNCTION 'OIL_MONTH_GET_FIRST_LAST'
EXPORTING
i_month = v_date3+4(2)
i_year = v_date3+0(4)
i_date = v_date3
IMPORTING
e_first_day = v_date3
e_last_day = v_date4
EXCEPTIONS
wrong_date = 1
OTHERS = 2.


v_date5 = it_pa0016-begda.
v_date6 = sy-datum.

CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
EXPORTING
i_date_from = v_date5
i_date_to = v_date6
i_flg_separate = 'X'
IMPORTING
e_days = v_days1
e_months = v_months1
e_years = v_years1.

IF v_date5 = v_date6.
v_days1 = 1.
ENDIF.

v_days1 = v_days1 + v_months1 * 30 + v_years1 * 365.
IF v_years1 GT 5.
v_mult = it_pa0016-zquota / 12.
ENDIF.


LOOP AT it_pa2001 WHERE pernr = it_pa0001-pernr.

v_total = v_total + it_pa2001-abrtg.

ENDLOOP.

v_mdate8+4(4) = it_pa0016-begda+0(4).
v_mdate8+2(2) = it_pa0016-begda+4(2).
v_mdate8+0(2) = it_pa0016-begda+6(2).

v_mdate9+4(4) = it_pa0016-endda+0(4).
v_mdate9+2(2) = it_pa0016-endda+4(2).
v_mdate9+0(2) = it_pa0016-endda+6(2).

LOOP AT it_pa2006 WHERE pernr = it_pa0016-pernr
AND subty = '03'
AND begda GE it_pa0016-begda.

ENDLOOP.
IF sy-subrc ne 0.

PERFORM bdc_dynpro USING 'SAPMP50A' '1000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=INS'.
PERFORM bdc_field USING 'RP50G-PERNR'
it_pa0001-pernr.
PERFORM bdc_field USING 'RP50G-TIMR6'
'X'.
PERFORM bdc_field USING 'RP50G-BEGDA'
v_mdate8.
PERFORM bdc_field USING 'RP50G-ENDDA'
v_mdate9.
PERFORM bdc_field USING 'BDC_CURSOR'
'RP50G-SUBTY'.
PERFORM bdc_field USING 'RP50G-CHOIC'
'2006'.
PERFORM bdc_field USING 'RP50G-SUBTY'
'03'.
PERFORM bdc_dynpro USING 'MP200000' '2250'.
PERFORM bdc_field USING 'BDC_CURSOR'
'P2006-DEEND'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=UPD'.
PERFORM bdc_field USING 'P2006-BEGDA'
v_mdate8.
PERFORM bdc_field USING 'P2006-ENDDA'
v_mdate9.
PERFORM bdc_field USING 'P2006-KTART'
'03'.
v_val = v_quota1.
CONDENSE v_val.
v_mult = v_val+0(5).

PERFORM bdc_field USING 'P2006-ANZHL'
v_mult.
PERFORM bdc_field USING 'P2006-DESTA'
v_mdate8.
PERFORM bdc_field USING 'P2006-DEEND'
v_mdate9.
PERFORM bdc_transaction USING 'PA30'.

ENDIF.
wait up to 5 seconds.

refresh bdcdata.

v_mult = v_mult - v_total.

PERFORM bdc_dynpro USING 'SAPMP50A' '1000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=INS'.
PERFORM bdc_field USING 'RP50G-PERNR'
it_pa0001-pernr.
PERFORM bdc_field USING 'RP50G-TIMR6'
'X'.
IF e_first_day GT it_pa0016-begda.
PERFORM bdc_field USING 'RP50G-BEGDA'
v_mdate1.
ELSE.
PERFORM bdc_field USING 'RP50G-BEGDA'
v_mdate3.
ENDIF.

PERFORM bdc_field USING 'RP50G-ENDDA'
v_mdate2.
PERFORM bdc_field USING 'BDC_CURSOR'
'RP50G-SUBTY'.
PERFORM bdc_field USING 'RP50G-CHOIC'
'2006'.
PERFORM bdc_field USING 'RP50G-SUBTY'
'01'.
PERFORM bdc_dynpro USING 'MP200000' '2250'.
PERFORM bdc_field USING 'BDC_CURSOR'
'P2006-DEEND'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=UPD'.
IF e_first_day GT it_pa0016-begda.
PERFORM bdc_field USING 'P2006-BEGDA'
v_mdate1.
ELSE.
PERFORM bdc_field USING 'P2006-BEGDA'
v_mdate3.
ENDIF.

PERFORM bdc_field USING 'P2006-ENDDA'
v_mdate2.
PERFORM bdc_field USING 'P2006-KTART'
'01'.
v_val = v_mult.
CONDENSE v_val.
v_mult = v_val+0(5).

PERFORM bdc_field USING 'P2006-ANZHL'
v_mult.
IF e_first_day GT it_pa0016-begda.
PERFORM bdc_field USING 'P2006-DESTA'
v_mdate6.
ELSE.
PERFORM bdc_field USING 'P2006-DESTA'
v_mdate6.
ENDIF.
PERFORM bdc_field USING 'P2006-DEEND'
v_mdate7.
PERFORM bdc_transaction USING 'PA30'.

refresh bdcdata.

wait up to 5 seconds.


LOOP AT it_pa2006 WHERE pernr = it_pa0016-pernr
AND subty = '04'
AND begda GE it_pa0016-begda.

ENDLOOP.
IF sy-subrc ne 0.

PERFORM bdc_dynpro USING 'SAPMP50A' '1000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=INS'.
PERFORM bdc_field USING 'RP50G-PERNR'
it_pa0001-pernr.
PERFORM bdc_field USING 'RP50G-TIMR6'
'X'.
PERFORM bdc_field USING 'RP50G-BEGDA'
v_mdate8.
PERFORM bdc_field USING 'RP50G-ENDDA'
v_mdate9.
PERFORM bdc_field USING 'BDC_CURSOR'
'RP50G-SUBTY'.
PERFORM bdc_field USING 'RP50G-CHOIC'
'2006'.
PERFORM bdc_field USING 'RP50G-SUBTY'
'04'.
PERFORM bdc_dynpro USING 'MP200000' '2250'.
PERFORM bdc_field USING 'BDC_CURSOR'
'P2006-DEEND'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=UPD'.
PERFORM bdc_field USING 'P2006-BEGDA'
v_mdate8.
PERFORM bdc_field USING 'P2006-ENDDA'
v_mdate9.
PERFORM bdc_field USING 'P2006-KTART'
'04'.
v_val = v_quota2.
CONDENSE v_val.
v_mult = v_val+0(5).

PERFORM bdc_field USING 'P2006-ANZHL'
v_mult.
PERFORM bdc_field USING 'P2006-DESTA'
v_mdate8.
PERFORM bdc_field USING 'P2006-DEEND'
v_mdate9.
PERFORM bdc_transaction USING 'PA30'.

ENDIF.
wait up to 5 seconds.

refresh bdcdata.

LOOP AT it_pa2006 WHERE pernr = it_pa0016-pernr
AND subty = '24'
AND begda GE it_pa0016-begda.

ENDLOOP.
IF sy-subrc ne 0.
PERFORM bdc_dynpro USING 'SAPMP50A' '1000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=INS'.
PERFORM bdc_field USING 'RP50G-PERNR'
it_pa0001-pernr.
PERFORM bdc_field USING 'RP50G-TIMR6'
'X'.
PERFORM bdc_field USING 'RP50G-BEGDA'
v_mdate8.
PERFORM bdc_field USING 'RP50G-ENDDA'
v_mdate9.
PERFORM bdc_field USING 'BDC_CURSOR'
'RP50G-SUBTY'.
PERFORM bdc_field USING 'RP50G-CHOIC'
'2006'.
PERFORM bdc_field USING 'RP50G-SUBTY'
'24'.
PERFORM bdc_dynpro USING 'MP200000' '2250'.
PERFORM bdc_field USING 'BDC_CURSOR'
'P2006-DEEND'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=UPD'.
PERFORM bdc_field USING 'P2006-BEGDA'
v_mdate8.
PERFORM bdc_field USING 'P2006-ENDDA'
v_mdate9.
PERFORM bdc_field USING 'P2006-KTART'
'24'.
v_val = v_quota3.
CONDENSE v_val.
v_mult = v_val+0(5).

PERFORM bdc_field USING 'P2006-ANZHL'
v_mult.
PERFORM bdc_field USING 'P2006-DESTA'
v_mdate8.
PERFORM bdc_field USING 'P2006-DEEND'
v_mdate9.
PERFORM bdc_transaction USING 'PA30'.
wait up to 5 seconds.
ENDIF.

refresh bdcdata.
ENDIF.
ENDLOOP.
*----------------------------------------------------------------------*
* Start new screen *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "BDC_DYNPRO

*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
IF fval <> space.
CLEAR bdcdata.
IF fnam = 'P2006-ANZHL'.
bdcdata-fnam = fnam.
v_val = fval.
REPLACE ALL OCCURRENCES OF '.' IN v_val WITH ','.
bdcdata-fval = v_val.
ELSE.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
ENDIF.
APPEND bdcdata.
ENDIF.
ENDFORM. "BDC_FIELD
*&---------------------------------------------------------------------*
*& Form BDC_TRANSACTION
*----------------------------------------------------------------------*
FORM bdc_transaction USING value(p_0741).

CALL TRANSACTION p_0741 USING bdcdata
MODE 'A'
UPDATE 'A'
MESSAGES INTO messtab.
ENDFORM. " BDC_TRANSACTION

No comments:

Post a Comment