Monday, 26 March 2012

USER COMMAND EMAX

REPORT  ZUSER_COMMAND.

TYPES: BEGIN OF TY_T001,
         BUKRS TYPE BUKRS," COMPANY CODE
         BUTXT TYPE BUTXT," NAME OF THE COMPANY
         ORT01 TYPE ORT01," CITY
         LAND1 TYPE LAND1," COUNTRY KEY
         WAERS TYPE WAERS," CURRENCY KEY
       END OF TY_T001.

TYPES: BEGIN OF TY_KNB1,
         BUKRS TYPE BUKRS," COMPANY CODE
         KUNNR TYPE KUNNR," CUSTOMER NO
         ZTERM TYPE DZTERM," PAYMENT TERMS
       END OF TY_KNB1.

DATA: IT_T001 TYPE STANDARD TABLE OF TY_T001,
      WA_T001 TYPE TY_T001.

DATA: IT_KNB1 TYPE STANDARD TABLE OF TY_KNB1,
      WA_KNB1 TYPE TY_KNB1.

DATA: IT_SELECTED_T001 TYPE STANDARD TABLE OF TY_T001.

DATA: V_BOX(1) TYPE C,
      V_LINES TYPE I,
      V_BUKRS TYPE BUKRS.

SELECT-OPTIONS: S_BUKRS FOR V_BUKRS.

***************************************************
*                START-OF-SELECTION.              *
***************************************************
START-OF-SELECTION.

SELECT BUKRS
       BUTXT
       ORT01
       LAND1
       WAERS
  INTO TABLE IT_T001
  FROM T001 WHERE BUKRS IN S_BUKRS.

***************************************************
*                END-OF-SELECTION.              *
***************************************************
END-OF-SELECTION.

SET PF-STATUS 'ZSTATUS'.

  IF NOT IT_T001 IS INITIAL.

    LOOP AT IT_T001 INTO WA_T001.

      WRITE:/5 V_BOX AS CHECKBOX,
               WA_T001-BUKRS,
               WA_T001-BUTXT,
               WA_T001-ORT01,
               WA_T001-LAND1.
      CLEAR WA_T001.
   ENDLOOP.
  ENDIF.

***************************************************
*                AT USER-COMMAND                  *
***************************************************
AT USER-COMMAND.

CASE SY-UCOMM.

*----1
 WHEN 'SALL'.      " Select All

      V_BOX = 'X'. " Select and display the Records

      LOOP AT IT_T001 INTO WA_T001.

       WRITE:/5 V_BOX AS CHECKBOX,
               WA_T001-BUKRS,
               WA_T001-BUTXT,
               WA_T001-ORT01,
               WA_T001-LAND1.
      CLEAR WA_T001.
   ENDLOOP.

*----2
 WHEN 'DALL'.      " DeSelect All

      V_BOX = SPACE. " Select and display the Records

      LOOP AT IT_T001 INTO WA_T001.

       WRITE:/5 V_BOX AS CHECKBOX,
               WA_T001-BUKRS,
               WA_T001-BUTXT,
               WA_T001-ORT01,
               WA_T001-LAND1.
      CLEAR WA_T001.
   ENDLOOP.

*----3
 WHEN 'DOWNLOAD'.

    REFRESH IT_SELECTED_T001.

      DESCRIBE LIST NUMBER OF LINES V_LINES.

       DO V_LINES TIMES.

        READ LINE SY-INDEX FIELD VALUE V_BOX
                                    WA_T001-BUKRS
                                    WA_T001-BUTXT
                                    WA_T001-ORT01
                                    WA_T001-LAND1
                                    WA_T001-WAERS.
        IF V_BOX = 'X'.
           APPEND WA_T001 TO IT_SELECTED_T001.
        ENDIF.

       ENDDO.

     IF IT_SELECTED_T001 IS INITIAL.
         WRITE:/ 'NO RECORDS SELECTED TO DOWNLOAD'.
      ELSE.

         CALL FUNCTION 'GUI_DOWNLOAD'
           EXPORTING
             FILENAME                        = 'C:\DOWNLOAD.TXT'
             WRITE_FIELD_SEPARATOR           = 'X'
           TABLES
             DATA_TAB                        = IT_SELECTED_T001.

         IF SY-SUBRC <> 0.
            WRITE:/ 'ITAB IS NOT SUCCESSFULLY DOWNLOADED'.
          ELSE.
            WRITE:/ 'ITAB IS SUCCESSFULLY DOWNLOADED TO C:\DOWNLOAD.TXT'.
         ENDIF.

     ENDIF.

*----4

  WHEN 'CUST'.

    REFRESH IT_SELECTED_T001.

      DESCRIBE LIST NUMBER OF LINES V_LINES.

       DO V_LINES TIMES.

        READ LINE SY-INDEX FIELD VALUE V_BOX
                                    WA_T001-BUKRS
                                    WA_T001-BUTXT
                                    WA_T001-ORT01
                                    WA_T001-LAND1
                                    WA_T001-WAERS.
        IF V_BOX = 'X'.
           APPEND WA_T001 TO IT_SELECTED_T001.
        ENDIF.

       ENDDO.

    IF IT_SELECTED_T001 IS INITIAL.

         WRITE:/ 'NO COMPANY CODES ARE SELECTED'.

      ELSE.

         SELECT BUKRS
                KUNNR
                ZTERM INTO TABLE IT_KNB1 FROM KNB1
         FOR ALL ENTRIES IN IT_SELECTED_T001
         WHERE BUKRS = IT_SELECTED_T001-BUKRS.

     IF IT_SELECTED_T001 IS INITIAL.

        WRITE:/ 'NO CUSTOMERS FOUND'.

      ELSE.

        LOOP AT IT_KNB1 INTO WA_KNB1.

          WRITE:/ WA_KNB1-BUKRS,
                  WA_KNB1-KUNNR,
                  WA_KNB1-ZTERM.
        ENDLOOP.
     ENDIF.
    ENDIF.

ENDCASE.