REPORT ZINACTIV NO STANDARD PAGE HEADING.
************************************************************************
* This program displays, locks or deletes the users, whose last login
* date is older then N days or they have never logged in and they were
* created at least N days ago
************************************************************************

* Atcion input
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (20) COMMENT0.
SELECTION-SCREEN POSITION 56.
PARAMETERS: DISP RADIOBUTTON GROUP R1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (20) COMMENT1.
SELECTION-SCREEN POSITION 56.
PARAMETERS: LOCK RADIOBUTTON GROUP R1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (20) COMMENT2.
SELECTION-SCREEN POSITION 56.
PARAMETERS: DELETE RADIOBUTTON GROUP R1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B1.

* User type input
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (35) COMMENT3.
SELECTION-SCREEN POSITION 56.
PARAMETERS: INAC RADIOBUTTON GROUP R2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (35) COMMENT4.
SELECTION-SCREEN POSITION 56.
PARAMETERS: NOLOG RADIOBUTTON GROUP R2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B2.

* Period input
SELECTION-SCREEN BEGIN OF BLOCK B3 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (12) COMMENT5.
SELECTION-SCREEN POSITION 16.
PARAMETERS: DAYS(3) TYPE N OBLIGATORY DEFAULT '60'.
SELECTION-SCREEN COMMENT 21(20) COMMENT6.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B3.

* Initialize the selection screen
INITIALIZATION.
  COMMENT0 = 'DISPLAY USERS WHO'.
  COMMENT1 = 'LOCK USERS WHO'.
  COMMENT2 = 'DELETE USERS WHO'.
  COMMENT3 = 'LAST LOGGED IN'.
  COMMENT4 = 'NEVER LOGGED IN AND WERE CREATED'.
  COMMENT5 = 'AT LEAST'.
  COMMENT6 = 'DAYS AGO'.

START-OF-SELECTION.
* Data declaration
  TABLES: USR02.
  DATA: LAST_DATE TYPE D.
  DATA: BEGIN OF USERS OCCURS 50,
  BNAME LIKE USR02-BNAME,
  TRDAT LIKE USR02-TRDAT,
  ERDAT LIKE USR02-ERDAT,
  END OF USERS.
  data: begin of bdc_tab occurs 100.
          include structure bdcdata.
  data: end of bdc_tab.

* Calculate the date
  LAST_DATE = SY-DATUM.
  LAST_DATE = LAST_DATE - DAYS.

* Find the users that fulfill the criterias
  SELECT * FROM USR02 WHERE USTYP = 'A'.
    IF USR02-TRDAT <= LAST_DATE.
      IF USR02-TRDAT = '00000000' AND NOLOG = 'X'.
        IF USR02-ERDAT <= LAST_DATE.
          MOVE-CORRESPONDING USR02 TO USERS.
          APPEND USERS.
        ENDIF.
      ELSEIF USR02-TRDAT <> '00000000' AND INAC = 'X'.
        MOVE-CORRESPONDING USR02 TO USERS.
        APPEND USERS.
      ENDIF.
    ENDIF.
  ENDSELECT.

* Depending on the action: display, lock or delete
  IF DISP = 'X'.
    WRITE: / '  USER       LAST LOGIN   CREATED'.
    SKIP.
    LOOP AT USERS.
      WRITE: / USERS-BNAME, USERS-TRDAT, USERS-ERDAT.
    ENDLOOP.
  ELSEIF LOCK = 'X'.
    WRITE: / 'LOCKED:'.
    WRITE: / '  USER       LAST LOGIN   CREATED'.
    SKIP.
    LOOP AT USERS.
      WRITE: / USERS-BNAME, USERS-TRDAT, USERS-ERDAT.
      SELECT SINGLE * FROM USR02 WHERE BNAME = USERS-BNAME.
      USR02-UFLAG = '128'.
      MODIFY USR02.
    ENDLOOP.
  ELSE.
    WRITE: / 'DELETED:'.
    WRITE: / '  USER       LAST LOGIN   CREATED'.
    SKIP.
    LOOP AT USERS.
      WRITE: / USERS-BNAME, USERS-TRDAT, USERS-ERDAT.
      PERFORM USER_DELETE.
    ENDLOOP.
  ENDIF.

*---------------------------------------------------------------------*
*       FORM USER_DELETE                                              *
*---------------------------------------------------------------------*
FORM USER_DELETE.

PERFORM BDC_FILL USING 'X'
     'SAPMS01J'
     '0200'.
PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'DELU'.
PERFORM BDC_FILL USING ' '
     'BDC_CURSOR'
     'XU200-XUSER'.
PERFORM BDC_FILL USING ' '
     'XU200-XUSER'
     USERS-BNAME.
PERFORM BDC_FILL USING 'X'
     'SAPLSPO1'
     '0400'.
PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'YES'.
PERFORM BDC_FILL USING 'X'
     'SAPLSPO1'
     '0100'.
PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'YES'.
PERFORM BDC_FILL USING 'X'
     'SAPMS01J'
     '0200'.
PERFORM BDC_FILL USING ' '
     'BDC_OKCODE'
     'BACK'.
PERFORM BDC_FILL USING ' '
     'BDC_CURSOR'
     'XU200-XUSER'.
PERFORM BDC_FILL USING ' '
     'XU200-XUSER'
     USERS-BNAME.

call transaction 'SU01'
using bdc_tab mode 'N'.

ENDFORM.
*---------------------------------------------------------------------*
*       FORM BDC_FILL                                                 *
*---------------------------------------------------------------------*
FORM BDC_FILL USING P1 P2 P3.
  clear bdc_tab.
  if p1 = 'X'.
    bdc_tab-dynbegin = p1.
    bdc_tab-program = p2.
    bdc_tab-dynpro =  p3.
  else.
    bdc_tab-dynbegin = p1.
    bdc_tab-fnam = p2.
    bdc_tab-fval = p3.
  endif.
  append bdc_tab.
ENDFORM.