SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Creates a csv file with salescode information



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SD
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sat Jan 31, 2009 6:53 pm    Post subject: Creates a csv file with salescode information Reply with quote

Code:
*&---------------------------------------------------------------------*
*& Report  ZRL_SALES_ORGANISATION_FILE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
 
report  zrl_sales_organisation_file.
 
tables: tcacs_appl, sscrfields.
 
 
* Selection screen
selection-screen: begin of block b01 with frame title text-b01.
parameters lv_appl type cacsappl.
selection-screen: end of block b01.
 
selection-screen function key 5.
 
types truxs_t_text_data(4096)         type           c occurs 0.   " type for storing the headers
 
constants: gc_app_log_obj             type           bal_s_log-object  value 'Z_SALESORG_OUT',
           gc_app_log_subobj          type           bal_s_log-subobject value 'FILE'.
 
* Data declarations
data: lt_hrp1001                      type table of  hrp1001 with header line,
      lt_result_struc                 type table of  struc with header line,
      lt_zrl_salescode_details        type table of  zrl_salescode_details with header line,
      lt_zrl_salescode_details_csv    type           truxs_t_text_data,
      lt_/ccdev01/a2scode             type table of  /ccdev01/a2scode with header line,
      lt_dfies                        type table of  dfies with header line,
      lt_cacs_ctrtbu                  type table of  cacs_ctrtbu with header line,
      lt_bapibus1006_central_organ    type table of  bapibus1006_central_organ with header line,
      lt_bapibus1006_central_group    type table of  bapibus1006_central_group with header line,
      lt_bapibus1006_central_person   type table of  bapibus1006_central_person with header line.
 
 
 
data: ls_/ccdev01/a2scode             type           /ccdev01/a2scode,
      ls_zrl_salescode_details_csv    like line of   lt_zrl_salescode_details_csv,
      ls_zrl_salescode_details        like line of   lt_zrl_salescode_details.
 
 
 
data: lv_begin_date                   type            cacs_busitime_b,
      lv_end_date                     type            c length 8,
      lv_bp_name                      type            c length 46,
      lv_ds_name_ex                   type            c length 256,
      lv_category                     type            bu_type,
      lv_flag                         type            c,
      gv_loghandle                    type            balloghndl.
 
 
initialization.
  "print text on button
  sscrfields-functxt_05 = 'Log'(005).
  perform init.
 
at selection-screen output.
 
* If the commission application field is not set then get the parameter id and insert it in the commission application field
  if lv_appl is initial.
    get parameter id 'CACSAPPL' field lv_appl.
  endif.
 
at selection-screen.
 
 
* when the system code is ONLI then perform the specified functions otherwise the program has been run already and show the log
  case sy-ucomm.
    when 'ONLI'.
 
      perform init_log.
      perform create_file.
      perform save_log.
 
    when 'FC05'.
      perform show_log.
    when others.
  endcase.
 
  clear sy-ucomm.
 
 
*&---------------------------------------------------------------------*
*&      Form  show_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form show_log.
 
  data:   lv_log_filter        type bal_s_lfil,
          lr_log_handle        type bal_s_logh,
          lv_log_header        type balhdr_t,
          ls_log_header        type balhdr,
          lv_date_from         type balhdr-aldate,
          lv_time_from         type balhdr-altime,
          lv_date_to           type balhdr-aldate,
          lv_time_to           type balhdr-altime.
 
  get parameter id 'ZRL_LOGHANDLE' field gv_loghandle.
 
  lr_log_handle-sign   = 'I'.
  lr_log_handle-option = 'EQ'.
  lr_log_handle-low    = gv_loghandle.
  lr_log_handle-high   = gv_loghandle.
  append lr_log_handle to lv_log_filter-log_handle.
  call function 'BAL_DB_SEARCH'
    exporting
      i_s_log_filter = lv_log_filter
    importing
      e_t_log_header = lv_log_header.
 
  read table lv_log_header index 1 into ls_log_header.
  lv_date_from = ls_log_header-aldate.
  lv_time_from = ls_log_header-altime.
  lv_date_to   = ls_log_header-aldate.
  lv_time_to   = ls_log_header-altime.
 
  call function 'APPL_LOG_DISPLAY'
    exporting
      object                    = gc_app_log_obj
      subobject                 = gc_app_log_subobj
      date_from                 = lv_date_from
      time_from                 = lv_time_from
      date_to                   = lv_date_to
      time_to                   = lv_time_to
      title_selection_screen    = text-048
      title_list_screen         = text-049
      column_selection          = '11112221122   '
      suppress_selection_dialog = 'X'
    exceptions
      no_authority              = 1
      others                    = 2.
 
  if sy-subrc  0.
    if 1 = 2. message e808(z_rl_msg). endif.
    perform add_message using 'Z_RL_MSG' 'E' '808' space space space space.
  endif.
 
endform.                    "show_log
 
*&---------------------------------------------------------------------*
*&      Form  init_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form init_log.
  data: lv_subobj like balsub-subobject,
        lv_dd07v  type dd07v,
        lv_rc     like sy-subrc,
        lv_value  like dd07l-domvalue_l.
 
  "open application log
  call function 'MESSAGES_INITIALIZE_CACS'
    exporting
      i_log_object            = gc_app_log_obj
      i_log_subobject         = gc_app_log_subobj
    importing
      e_log_handle            = gv_loghandle
    exceptions
      log_header_inconsistent = 1
      severe_error            = 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.
 
  set parameter id 'ZRL_LOGHANDLE' field gv_loghandle.
endform.                    " init
 
 
*&---------------------------------------------------------------------*
*&      Form  save_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form save_log.
 
  call function 'CACS_BUFFERLOG_SAVE'
    exporting
      iflg_simulation         = ' '
    exceptions
      log_not_found           = 1
      save_not_allowed        = 2
      numbering_error         = 3
      severe_error            = 4
      msg_not_found           = 5
      log_header_inconsistent = 6
      others                  = 7.
  if sy-subrc  0.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
endform.                    "save_log
*&---------------------------------------------------------------------*
*&      Form  init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form init.
* Clear the local table and local variables
  refresh: lt_hrp1001, lt_/ccdev01/a2scode, lt_result_struc, lt_zrl_salescode_details, lt_zrl_salescode_details_csv,
           lt_bapibus1006_central_person,lt_dfies,lt_cacs_ctrtbu, lt_bapibus1006_central_organ.
 
  clear:   ls_/ccdev01/a2scode, ls_zrl_salescode_details_csv, ls_zrl_salescode_details.
 
  clear:   lv_bp_name, lv_ds_name_ex.
 
 
endform.                    "init
 
*&---------------------------------------------------------------------*
*&      Form  create_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form create_file.
 
* Select all active salescodes and send them to our local table
  call function '/CCDEV01/A2_GET_ACT_SALESCODES'
    exporting
      i_appl    = lv_appl
    tables
      t_a2scode = lt_/ccdev01/a2scode
    exceptions
      others    = 1.
 
  if sy-subrc  0.
    if 1 = 2. message e804(z_rl_msg). endif.
    perform add_message using 'Z_RL_MSG' 'E' '804' 'ZRLAC' space space space.
  endif.
 
 
* Read all organisational relationships for all business partner’s linked to sales codes for all date ranges
  select * from hrp1001 into table lt_hrp1001
     where plvar   eq '01'
      and  sclas   eq 'BP'
      and  relat   eq 'Z40'
      or   relat   eq '008'
      or   relat   eq '291'.
 
 
 
  if sy-subrc  0.
    if 1 = 2. message e805(z_rl_msg). endif.
    perform add_message using 'Z_RL_MSG' 'E' '805' 'ZRLAC' space space space.
  endif.
 
 
* Get the salescode bp name and last name
  loop at lt_/ccdev01/a2scode.
 
 
*Get the sales code number
    ls_zrl_salescode_details-sales_code        = lt_/ccdev01/a2scode-sales_code.
 
*Get salescode type
    ls_zrl_salescode_details-sales_code_type   = lt_/ccdev01/a2scode-sales_type.
 
*Get salescode business partner number
    ls_zrl_salescode_details-sales_code_bp     = lt_/ccdev01/a2scode-partner.
 
 
 
*  Loop at the local relationships table and get dates for the stipulated relationships
    loop at lt_hrp1001
     where sobid eq lt_/ccdev01/a2scode-partner.
 
 
* Set the begin and end dates to those in the local relationships table
      ls_zrl_salescode_details-rel_begin_date = lt_hrp1001-begda.
      lv_begin_date                           = ls_zrl_salescode_details-rel_begin_date.
      ls_zrl_salescode_details-rel_end_date   = lt_hrp1001-endda.
 
 
* Get the salescode business partner's name using their BP number
      call function 'BUPA_CENTRAL_GET_DETAIL'
        exporting
          iv_partner     = ls_zrl_salescode_details-sales_code_bp
        importing
          es_data_person = lt_bapibus1006_central_person
          es_data_organ  = lt_bapibus1006_central_organ
          es_data_group  = lt_bapibus1006_central_group
          ev_category    = lv_category.
 
* Check whether the BP is a person
      if lv_category = 1.
 
*      if lt_bapibus1006_central_person is not initial.
 
*    If the BP is a person then place this name in the central person local table
        if lt_bapibus1006_central_person-firstname is not initial.
 
          concatenate     lv_bp_name
                          lt_bapibus1006_central_person-firstname
            into          lv_bp_name
            separated by  space.
 
        endif.
 
        if lt_bapibus1006_central_person-lastname is not initial.
 
          concatenate   lv_bp_name
                        lt_bapibus1006_central_person-lastname
          into          lv_bp_name
          separated by  space.
 
        endif.
* If the BP is an organisation
      elseif lv_category = 2.
*   If the BP is a company then place this name in the central organisation local table
        lv_bp_name = lt_bapibus1006_central_organ-name1.
      endif.
 
      if lv_category = 3.
        lv_bp_name = lt_bapibus1006_central_group-namegroup1.
      endif.
 
* Get the name of the BP and put it in the salescode structure
      ls_zrl_salescode_details-bp_name = lv_bp_name.
 
 
      clear : lv_bp_name, lv_category.  " Clear these variables so that they can be used again
 
      if  lt_hrp1001-relat = 'Z40' or  lt_hrp1001-relat = '008'. "Get all org attributes for external
        call function 'Z_RL_GET_ORG_ATTRIBUTES'
        exporting
          i_appl                      = lv_appl
          i_si_scode_bp               = ls_zrl_salescode_details-sales_code_bp
          i_ctrtbu_id                 = lt_/ccdev01/a2scode-ctrtbu_id
          i_busitime                  = lv_begin_date
*          i_techtime                  =
          i_det_necessary             = 1
          i_org_det_necessary         = 1
          i_ccode_det_necessary       = 1
          i_cons_det_necessary        = 1
          i_suppress_info_messages    = 'X'
       importing
         e_company_code              = ls_zrl_salescode_details-company_code
         e_line_of_business          = ls_zrl_salescode_details-sales_lob
         e_region                    = ls_zrl_salescode_details-region
*     E_GROUP                     =
         e_consultant_bp             = ls_zrl_salescode_details-consultant_bp
       exceptions
         error                          = 1
         others                         = 2.
 
        if sy-subrc  0.
          if 1 = 2. message e056(z_rl_msg). endif.
          perform add_message using  'Z_RL_MSG'
                                     'E'
                                     '056'
                                     ls_zrl_salescode_details-sales_code_bp
                                     space
                                     space
                                     space.
        endif.
 
      elseif lt_hrp1001-relat = '291' " "Get all org attributes for employee
        and lt_hrp1001-otype = 'S'
        or  lt_hrp1001-otype = 'O'.
 
        call function 'Z_RL_GET_ORG_ATTRIBUTES'
         exporting
           i_appl                      = lv_appl
           i_si_scode_bp               = ls_zrl_salescode_details-sales_code_bp
           i_ctrtbu_id                 = lt_/ccdev01/a2scode-ctrtbu_id
           i_busitime                  = lv_begin_date
*          i_techtime                  =
           i_det_necessary             = 1
           i_org_det_necessary         = 1
           i_ccode_det_necessary       = 1
           i_cons_det_necessary        = 0
           i_suppress_info_messages    = 'X'
        importing
          e_company_code              = ls_zrl_salescode_details-company_code
          e_line_of_business          = ls_zrl_salescode_details-sales_lob
          e_region                    = ls_zrl_salescode_details-region
*     E_GROUP                     =
          e_consultant_bp             = ls_zrl_salescode_details-consultant_bp
        exceptions
          error                          = 1
          others                         = 2.
 
        if sy-subrc  0.
          if 1 = 2. message e056(z_rl_msg). endif.
          perform add_message using  'Z_RL_MSG'
                                     'E'
                                     '056'
                                     ls_zrl_salescode_details-sales_code_bp
                                     space
                                     space
                                     space.
        endif.
 
      endif.
 
 
 
      clear: lt_bapibus1006_central_person, lv_begin_date.  " clear lv_begin-date and the central person table so that we can use them again
 
 
*     Get the name of the consultant
      call function 'BUPA_CENTRAL_GET_DETAIL'
        exporting
          iv_partner     = ls_zrl_salescode_details-consultant_bp
        importing
          es_data_person = lt_bapibus1006_central_person
          ev_category    = lv_category.
 
      if lv_category = 1.
 
        if lt_bapibus1006_central_person-firstname is not initial.
 
          concatenate     lv_bp_name
                          lt_bapibus1006_central_person-firstname
            into          lv_bp_name
            separated by  space.
 
        endif.
 
        if lt_bapibus1006_central_person-lastname is not initial.
 
          concatenate   lv_bp_name
                        lt_bapibus1006_central_person-lastname
          into          lv_bp_name
          separated by  space.
 
        endif.
 
      endif.
 
* Get first and last name of the consultant BP and assign them to our salescode structure
      ls_zrl_salescode_details-consultant_name = lv_bp_name.
 
      clear : lv_bp_name, lv_category.  " Clear these variables so that they can be used again
 
 
*   Append the output structure to a local table
      append ls_zrl_salescode_details to lt_zrl_salescode_details.
    endloop.
 
  endloop.
 
  "---------------------------------------------------------------------
  " Create TXT File
  "---------------------------------------------------------------------
 
 
  "Get the locical base file name
  call function 'FILE_GET_NAME'
    exporting
      logical_filename = 'ZRL_SALES_ORGANISATION'
      parameter_1      = sy-datum
    importing
      file_name        = lv_ds_name_ex
    exceptions
      others           = 1.
 
  if sy-subrc  0.
    if 1 = 2. message e806(z_rl_msg). endif.
    perform add_message using 'Z_RL_MSG' 'E' '806' 'ZRL_SALES_ORGANISATION' space space space.
  endif.
 
*Open the dataset
  open dataset lv_ds_name_ex for output in text mode encoding default.
 
  if sy-subrc  0.   " Error check
    if 1 = 2. message e807(z_rl_msg). endif.
    perform add_message using 'Z_RL_MSG' 'E' '807' space space space space.
  endif.
 
 
  clear: lt_dfies.
 
  " Get field descriptions from salescode to org structure table
  call function 'DDIF_FIELDINFO_GET'
    exporting
      tabname   = 'zrl_salescode_details'
    tables
      dfies_tab = lt_dfies.
 
*  write column headers to the file
  loop at lt_dfies.
 
    case lt_dfies-fieldname.
      when 'CONSULTANT_BP'.
        lt_dfies-scrtext_m = 'Consultant BP'.
      when 'REL_BEGIN_DATE'.
        lt_dfies-scrtext_m = 'Rel Begin Date'.
      when 'REL_END_DATE'.
        lt_dfies-scrtext_m = 'Rel End Date'.
    endcase.
 
    transfer lt_dfies-scrtext_m  to lv_ds_name_ex no end of line.
    transfer ';' to lv_ds_name_ex no end of line.
    clear: lt_dfies-scrtext_m.
  endloop.
 
  transfer '' to lv_ds_name_ex.
 
  close  dataset lv_ds_name_ex.
 
 
 
  "Convert output to csv format
  call function 'SAP_CONVERT_TO_CSV_FORMAT'
       exporting
         i_field_seperator          = ';'
        i_line_header              = 'X'
*       I_FILENAME                 =
*       I_APPL_KEEP                = ' '
    tables
      i_tab_sap_data             = lt_zrl_salescode_details
   changing
     i_tab_converted_data        = lt_zrl_salescode_details_csv.
 
  .
  if sy-subrc  0.
    message 'Conversion to CSV format failed.' type 'E'.
  endif.
 
 
 
* Open the dataset
  open dataset lv_ds_name_ex for appending in text mode encoding default.
 
  if sy-subrc  0.   " Error check
    if 1 = 2. message e807(z_rl_msg). endif.
    perform add_message using 'Z_RL_MSG' 'E' '807' space space space space.
  endif.
 
  loop at lt_zrl_salescode_details_csv into ls_zrl_salescode_details_csv.
    transfer ls_zrl_salescode_details_csv to lv_ds_name_ex.
  endloop.
 
*Write records to the file
  if sy-subrc  0.
    message 'Writing records to file failed.' type 'E'.
  endif.
 
  "close the dataset
  close dataset lv_ds_name_ex.
 
*  write: /1 'File written: ', lv_ds_name_ex.
 
endform.                    "create_file
 
*&---------------------------------------------------------------------*
*&      Form  add_message
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ARBGB    text
*      -->P_MSGTY    text
*      -->P_TXTNR    text
*      -->P_MSGV1    text
*      -->P_MSGV2    text
*      -->P_MSGV3    text
*      -->P_MSGV4    text
*----------------------------------------------------------------------*
form add_message using p_arbgb
                       p_msgty
                       p_txtnr
                       p_msgv1
                       p_msgv2
                       p_msgv3
                       p_msgv4.
 
  data: lv_arbgb like smesg-arbgb,
        lv_msgty like smesg-msgty,
        lv_msgv1 like smesg-msgv1,
        lv_msgv2 like smesg-msgv2,
        lv_msgv3 like smesg-msgv3,
        lv_msgv4 like smesg-msgv4.
 
  move: p_arbgb to lv_arbgb,
        p_msgty to lv_msgty,
        p_msgv1 to lv_msgv1,
        p_msgv2 to lv_msgv2,
        p_msgv3 to lv_msgv3,
        p_msgv4 to lv_msgv4.
 
 
  call function 'MESSAGE_STORE_WITH_DISPLAY'
    exporting
      arbgb                 = lv_arbgb
      msgty                 = lv_msgty
      txtnr                 = p_txtnr
      msgv1                 = lv_msgv1
      msgv2                 = lv_msgv2
      msgv3                 = lv_msgv3
      msgv4                 = lv_msgv4
      xdialog               = ''
    exceptions
      log_not_found         = 1
      msg_inconsistent      = 2
      log_is_full           = 3
      msg_not_stored        = 4
      msg_store_not_allowed = 5
      severe_error          = 6
      others                = 7.
  if sy-subrc  0.
    message a803(cacsib).
  endif.
 
endform.                    " add_message
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> SD All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.