Thursday, 25 August 2011

Sales order creating using Report program

************************************************************************
* SGL Group - The Carbon Company                                       *
*----------------------------------------------------------------------*
* Type:         Report         Name: ZLSDSALESORDER01                  *
* Application:  SD                                                     *
* Date:         2011-07-26                                             *
* Last Update:  N/A                                                    *
*----------------------------------------------------------------------*
* Author:       POSNIAN                                                *
* Requester:    Shirley Williams                                       *
* Ticket No:                                                           *
*----------------------------------------------------------------------*
* Description:  This Report is used to create sales order  for all the *
*               order information provided through excel sheets using  *
*               BAPI. The old material, customer and WBS Elements have *
*               to lookup in referrence excel sheets for its correspond*
*               -ing material, customer, and WBS Element values. The   *
*               BAPI is also extended to pass the Govt.Contract number *
*               custom field value to sales order.                     *
*----------------------------------------------------------------------*
* Change Log                                                           *
*                                                                      *
* YYYY-MM-DD  Ini  Description / Reason of Change (incl. ticket no.)   *
* ---------- ----- --------------------------------------------------- *
* 2011-07-26  POSNIAN Initial Development-Transport Request# S1DK990088*
*                                                                      *
************************************************************************

report  zlsdsalesorder01 no standard page heading.

* Include for declarations
(include zlsdsalesorder01_top.)
****************************************
*&---------------------------------------------------------------------*
*&  Include           ZLSDSALESORDER01_TOP
*&---------------------------------------------------------------------*

** Declaration for TYPE-POOLS
type-pools: truxs,
            slis.
** Constants declaration
constants: gc_on    type c      value 'X',
           gc_off   type c      value ' ',
           gc_red   type c      value '1',
           gc_green type c      value '3',
           gc_blank type c      value ' ',
           gc_title type string value 'Select Excel file'.

** Types declaration for HEADER Information
types : begin of t_xcls_header,
        order_header          type vbeln,
        order_type            type auart,
        sales_organization    type vkorg,
        distribution_channel  type vtweg,
        division              type spart ,
        sold_to               type kunnr,
        ship_to               type kunnr,
        sales_office          type vkbur,
        sales_group           type vkgrp,
        ref_doc_number        type xblnr_v,
        customer_po           type bstkd,
        customer_po_date      type char8, "bstdk,
        usage_indicator       type abrvw,
        governmen_contr       type zsd_gov_contnum,
        employee_responsible  type kunnr,
        text                  type string,
        end of t_xcls_header.

** Types declaration for ITEM Information
types : begin of t_xcls_item,
        order_item        type vbeln,
        item              type char6, "posnr,
        plant             type werks,
        material          type matnr,
        unit_of_sale      type vrkme,
        wbs_element       type char20, "ps_psp_pnr,
        route             type route,
        customer_po_item  type char6,  "posex,
        customer_material type kdmat,
        price             type netpr,
        end of t_xcls_item.

** Types declaration for SCHEDULE Information
types : begin of t_xcls_shd,
        order_shd     type vbeln,
        item_shd      type char6, "posnr,
        scheduele     type etenr,
        delivery_date type char8, "edatu,
        order_qty     type wmeng,
        end of t_xcls_shd.

** Types declaration for Material master
types: begin of t_matmas,
       matnr_old type matnr,
       matnr_new type matnr,
       end of t_matmas.

** Types declaration for Customer master
types:begin of t_cusmas,
      cust_old type kunnr,
      cust_new type kunnr,
      end of t_cusmas.

** Types declaration for WBS Element
types:begin of t_wbsele,
      wbsele_old type char20,
      wbsele_new type char20,
      end of t_wbsele.

** Types declaration for Messages
types:begin of t_msgs,
      light       type c,
      ord_no      type vbeln,
      item_no     type posnr,
      schd_no     type etenr,
      msgs        type char72,
      new_ord_no  type vbeln,
     end of t_msgs.

** Declarations for internal tables, workareas, and variables.
data: it_err_msgs     type table of t_msgs,
      it_xcls_header  type table of t_xcls_header,
      it_xcls_item    type table of t_xcls_item,
      it_xcls_shd     type table of t_xcls_shd,
      gt_cusmstr      type standard table of t_cusmas,
      gt_matmstr      type standard table of t_matmas,
      gt_wbselem      type standard table of t_wbsele,
      wa_err_msgs     type t_msgs,
      it_fieldcat     type slis_t_fieldcat_alv,
      wa_fieldcat     type slis_fieldcat_alv,
      gs_layout       type slis_layout_alv,
      wa_xcls_header  type t_xcls_header,
      wa_xcls_item    type t_xcls_item,
      wa_xcls_shd     type t_xcls_shd.


data :  ex_order_header_in      type bapisdhd1,
        ex_order_header_inx     type bapisdhd1x,
        im_salesdocument        type bapivbeln-vbeln,
        im_return               type table of bapiret2,
        imw_return              type bapiret2,
        ex_order_items_in       type table of bapisditm,
        exw_order_items_in      type bapisditm,
        exw_order_items_inx     type bapisditmx,
        ex_order_items_inx      type table of bapisditmx,
        ex_order_schedules_in   type table of bapischdl,
        exw_order_schedules_in  type bapischdl,
        ex_order_schedules_inx  type table of bapischdlx,
        exw_order_schedules_inx type bapischdlx,
        ex_order_keys           type table of bapisdkey,
        ex_condition_in         type table of bapicond,
        exw_condition_in        type bapicond,
        ex_condition_inx        type table of bapicondx,
        exw_condition_inx       type bapicondx,
        gt_partners             type table of bapiparnr,
        gs_partners             type bapiparnr,
        wa_extensionin          type bapiparex,
        it_extensionin          type table of bapiparex,
        wa_bape_vbak            type bape_vbak,
        wa_bape_vbakx           type bape_vbakx.

data :  gv_salesdocumentin  type  bapivbeln-vbeln,
        gt_return           type table of   bapiret2,
        gs_return           type  bapiret2,
        gt_texts            type table of bapisdtext,
        gs_texts            type bapisdtext,
        gv_file             type string.
*********************************
* Selection-screen
selection-screen: begin of block b1 with frame title text-001.

* Parameters
parameters: p_hdr type string obligatory,
            p_itm type string obligatory,
            p_schd type string obligatory.

selection-screen: end of block b1.
selection-screen: begin of block b2 with frame title text-009.
parameters:  p_cmstr type string obligatory,
             p_mmstr type string obligatory,
             p_welem type string obligatory.

selection-screen: end of block b2.
selection-screen: begin of block b3 with frame title text-010.
parameters : r1 radiobutton group g1 default 'X',
             r2 radiobutton group g1.

selection-screen: end of block b3.

* F4 help for Header sheet
at selection-screen on value-request for p_hdr.
* To provide F4 help for the file
  perform sub_file_f4 changing gv_file.
  p_hdr = gv_file.
  clear gv_file.
* F4 help for Item sheet
at selection-screen on value-request for p_itm.
* To provide F4 help for the file
  perform sub_file_f4 changing gv_file.
  p_itm = gv_file.
  clear gv_file.
* F4 help for Schedule sheet
at selection-screen on value-request for p_schd.
* To provide F4 help for the file
  perform sub_file_f4 changing gv_file.
  p_schd = gv_file.
  clear gv_file.

* F4 help for Customer master sheet
at selection-screen on value-request for p_cmstr.
* To provide F4 help for the file
  perform sub_file_f4 changing gv_file.
  p_cmstr = gv_file.
  clear gv_file.
* F4 help for Material master sheet
at selection-screen on value-request for p_mmstr.
* To provide F4 help for the file
  perform sub_file_f4 changing gv_file.
  p_mmstr = gv_file.
  clear gv_file.
* F4 help for WBS Element sheet
at selection-screen on value-request for p_welem.
* To provide F4 help for the file
  perform sub_file_f4 changing gv_file.
  p_welem = gv_file.
  clear gv_file.

* Start-of-selection
start-of-selection.
* To get the source data
  perform get_source_data.

* Validate the mandatory values for sales order
  perform validate_data.

*  IF it_err_msgs is initial.
  perform create_sales_orders.
* For display results
  perform display_results.
* End-of-selection
end-of-selection.
********************************
  (include zlsdsalesorder01_f01.)
*&---------------------------------------------------------------------*
*&  Include           ZLSDSALESORDER01_F01
*&---------------------------------------------------------------------*
form sub_file_f4 changing f_name type string.
  data:
  l_desktop       type string,
  l_i_files       type filetable,
  l_wa_files      type file_table,
  l_rcode         type int4.


* Finding desktop
  call method cl_gui_frontend_services=>get_desktop_directory
    changing
      desktop_directory    = l_desktop
    exceptions
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      others               = 4.
  if sy-subrc <> 0.
    message e001(00) with text-042.
  endif.

* Update View
  call method cl_gui_cfw=>update_view
    exceptions
      cntl_system_error = 1
      cntl_error        = 2
      others            = 3.
* call method for open file
  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      window_title            = gc_title
      default_extension       = '.xls'
      file_filter             = '.xls'
      initial_directory       = l_desktop
    changing
      file_table              = l_i_files
      rc                      = l_rcode
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5.
  if sy-subrc <> 0.
    message e001(00) with text-007.
  endif.

  if l_i_files is not initial.
    read table l_i_files index 1 into l_wa_files.
    if sy-subrc = 0.
      f_name = l_wa_files-filename.
    else.
      exit.
    endif.
  endif.
endform.                    " SUB_FILE_F4
*&---------------------------------------------------------------------*
*&      Form  create_sales_orders
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
* Create sales order with header,item and schedule lines data
form create_sales_orders.

  data: ls_cusmstr    type t_cusmas,
        ls_matmstr    type t_matmas,
        ls_wbselem    type t_wbsele,
        lv_test_mode  type c,
        lv_posex      type posex,
        lv_etenr      type etenr.

* fill the header information of sales order to be created to bapi structure
  loop at it_xcls_header into wa_xcls_header.
    if wa_xcls_header-order_type is not initial.
      ex_order_header_in-doc_type = wa_xcls_header-order_type.
      ex_order_header_inx-doc_type = gc_on.
    endif.

    if wa_xcls_header-sales_organization is not initial.
      ex_order_header_in-sales_org = wa_xcls_header-sales_organization.
      ex_order_header_inx-sales_org = gc_on.
    endif.

    if wa_xcls_header-distribution_channel is not initial.
      ex_order_header_in-distr_chan = wa_xcls_header-distribution_channel.
      ex_order_header_inx-distr_chan = gc_on.
    endif.

    if wa_xcls_header-division is not initial.
      ex_order_header_in-division = wa_xcls_header-division.
      ex_order_header_inx-division = gc_on.
    endif.

    if wa_xcls_header-sales_office is not initial.
      ex_order_header_in-sales_off = wa_xcls_header-sales_office.
      ex_order_header_inx-sales_off = gc_on.
    endif.

    if wa_xcls_header-sales_group is not initial.
      ex_order_header_in-sales_grp = wa_xcls_header-sales_group.
      ex_order_header_inx-sales_grp = gc_on.
    endif.

    if wa_xcls_header-ref_doc_number is not initial.
      ex_order_header_in-ref_doc_l = wa_xcls_header-ref_doc_number.
      ex_order_header_inx-ref_doc_l = gc_on.
    endif.

    if wa_xcls_header-customer_po is not initial.
      ex_order_header_in-purch_no_c = wa_xcls_header-customer_po.
      ex_order_header_inx-purch_no_c = gc_on.
    endif.

    if  wa_xcls_header-customer_po_date is not initial.
      data: lv_po_date type datum.
      lv_po_date = wa_xcls_header-customer_po_date.
      perform date_calc changing  lv_po_date .
      ex_order_header_in-purch_date = lv_po_date .
      ex_order_header_inx-purch_date = gc_on.
    endif.

    if wa_xcls_header-usage_indicator is not initial.
      ex_order_header_in-dlvschduse = wa_xcls_header-usage_indicator.
      ex_order_header_inx-dlvschduse = gc_on.
    endif.

* fill the partner information into partner internal table
    if wa_xcls_header-sold_to is not initial.
      gs_partners-partn_role = 'AG'. " sold to party
      read table gt_cusmstr into ls_cusmstr with key cust_old = wa_xcls_header-sold_to.
      if sy-subrc = 0.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  = ls_cusmstr-cust_new
          importing
            output = ls_cusmstr-cust_new.

        gs_partners-partn_numb = ls_cusmstr-cust_new.
        clear: ls_cusmstr.
        append gs_partners to gt_partners.
        clear gs_partners.
      else.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-msgs   = text-011.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

    endif.

    if wa_xcls_header-ship_to is not initial.
      gs_partners-partn_role = 'WE'. "Ship to party
      read table gt_cusmstr into ls_cusmstr with key cust_old = wa_xcls_header-ship_to.
      if sy-subrc = 0.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  = ls_cusmstr-cust_new
          importing
            output = ls_cusmstr-cust_new.
        gs_partners-partn_numb = ls_cusmstr-cust_new.
        clear: ls_cusmstr.
        append gs_partners to gt_partners.
        clear gs_partners.
      else.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-msgs   = text-012.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

    endif.

    if wa_xcls_header-employee_responsible is not initial.
      gs_partners-partn_role = 'ZM'. " Employee Responsible
      call function 'CONVERSION_EXIT_ALPHA_INPUT'
        exporting
          input  = wa_xcls_header-employee_responsible
        importing
          output = wa_xcls_header-employee_responsible.
      gs_partners-partn_numb = wa_xcls_header-employee_responsible.
      append gs_partners to gt_partners.
      clear gs_partners.
    endif.

    if wa_xcls_header-text is not initial.
      gs_texts-doc_number = wa_xcls_header-order_header.
      gs_texts-text_id = 'VBAK'.
      gs_texts-langu = 'E'.
      gs_texts-format_col = '*'.
      gs_texts-text_line = wa_xcls_header-text.
      append gs_texts to gt_texts.
      clear gs_texts.
    endif.

* fill the item information of above header into bapi structure
    loop at it_xcls_item into  wa_xcls_item where order_item = wa_xcls_header-order_header.
      lv_posex = lv_posex + 1.
      if wa_xcls_item-item is not initial .
*        lv_posex = wa_xcls_item-item.
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  = lv_posex "wa_xcls_item-item
          importing
            output = lv_posex. "wa_xcls_item-item.
        exw_order_items_in-itm_number = lv_posex. "wa_xcls_item-item.
        exw_order_items_inx-itm_number = gc_on.
*        clear lv_posex.
      endif.

      if wa_xcls_item-plant is not initial.
        exw_order_items_in-plant = wa_xcls_item-plant.
        exw_order_items_inx-plant = gc_on.
      endif.

      if wa_xcls_item-material is not initial.
        read table gt_matmstr into ls_matmstr with key matnr_old = wa_xcls_item-material.
        if sy-subrc = 0.
          call function 'CONVERSION_EXIT_ALPHA_INPUT'
            exporting
              input  = ls_matmstr-matnr_new
            importing
              output = ls_matmstr-matnr_new.
          exw_order_items_in-material = ls_matmstr-matnr_new.
          clear ls_matmstr.
          exw_order_items_inx-material = gc_on.
        else.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-item_no = wa_xcls_item-item.
          wa_err_msgs-msgs   = text-013.
          wa_err_msgs-light  = gc_red.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.
      endif.

      if wa_xcls_item-unit_of_sale is not initial.
        call function 'CONVERSION_EXIT_CUNIT_INPUT'
          exporting
            input                = wa_xcls_item-unit_of_sale
           language             = sy-langu
         importing
           output               = wa_xcls_item-unit_of_sale
         exceptions
           unit_not_found       = 1
           others               = 2
                  .
        if sy-subrc <> 0.
              message id sy-msgid type sy-msgty number sy-msgno
              with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        endif.

        exw_order_items_in-sales_unit = wa_xcls_item-unit_of_sale.
        exw_order_items_inx-sales_unit = gc_on.
      endif.

      if wa_xcls_item-wbs_element is not initial.
        read table gt_wbselem into ls_wbselem with key wbsele_old = wa_xcls_item-wbs_element+0(6).
        if sy-subrc = 0.
          exw_order_items_in-wbs_elem = ls_wbselem-wbsele_new.
          clear ls_wbselem.
          exw_order_items_inx-wbs_elem = gc_on.
        else.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-item_no = wa_xcls_item-item.
          wa_err_msgs-msgs   = text-014.
          wa_err_msgs-light  = gc_red.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.
      endif.

      if wa_xcls_item-route is not initial.
        exw_order_items_in-route = wa_xcls_item-route.
        exw_order_items_inx-route = gc_on.
      endif.
** the below code commented as per mail Aug 10th 2011
*      IF wa_xcls_item-customer_po_item IS NOT INITIAL.
*        lv_posex = wa_xcls_item-item .
        call function 'CONVERSION_EXIT_ALPHA_INPUT'
          exporting
            input  = lv_posex "wa_xcls_item-customer_po_item
          importing
            output = lv_posex.  "wa_xcls_item-customer_po_item.
        exw_order_items_in-po_itm_no = lv_posex ."wa_xcls_item-customer_po_item.
        exw_order_items_inx-po_itm_no = gc_on.
*        clear lv_posex.
*      ENDIF.

      if wa_xcls_item-customer_material is not initial.
        exw_order_items_in-cust_mat35 = wa_xcls_item-customer_material.
        exw_order_items_inx-cust_mat35 = gc_on.
      endif.

      if wa_xcls_item-price is not initial.

        exw_condition_in-itm_number = lv_posex. "wa_xcls_item-item .
        exw_condition_in-cond_type  = 'ZHPR'.
        exw_condition_in-cond_value = wa_xcls_item-price.
        exw_condition_in-currency = 'USD'.

        append exw_condition_in to ex_condition_in.
        clear exw_condition_in.
        exw_condition_inx-itm_number = lv_posex. "wa_xcls_item-item .
        exw_condition_inx-cond_type  = 'ZHPR'.
        exw_condition_inx-cond_value = 'X'.
        exw_condition_inx-currency = 'X'.

        append  exw_condition_inx to ex_condition_inx.
        clear exw_condition_inx.

      endif.

      append exw_order_items_in to ex_order_items_in.
      clear exw_order_items_in.
      append exw_order_items_inx to ex_order_items_inx.
      clear exw_order_items_inx.

* fill the schedule line information of above items into bapi structure
      loop at it_xcls_shd into wa_xcls_shd where order_shd = wa_xcls_header-order_header
                                                 and item_shd = wa_xcls_item-item.

        if wa_xcls_item-item is not initial.
          call function 'CONVERSION_EXIT_ALPHA_INPUT'
            exporting
              input  = lv_posex "wa_xcls_item-item
            importing
              output = lv_posex . "wa_xcls_item-item.
          exw_order_schedules_in-itm_number = lv_posex ."wa_xcls_item-item.
          exw_order_schedules_inx-itm_number  = gc_on.
        endif.
        lv_etenr = lv_etenr + 1.
        if wa_xcls_shd-scheduele is not initial.
          call function 'CONVERSION_EXIT_ALPHA_INPUT'
            exporting
              input  = lv_etenr            "wa_xcls_shd-scheduele
            importing
              output = lv_etenr .          "wa_xcls_shd-scheduele.

          exw_order_schedules_in-sched_line  = lv_etenr.   "wa_xcls_shd-scheduele.
          exw_order_schedules_inx-sched_line  = gc_on.
        endif.

        if wa_xcls_shd-delivery_date is not initial.
          data: lv_del_date type datum.
          lv_del_date = wa_xcls_shd-delivery_date.
          perform date_calc changing  lv_del_date .
          exw_order_schedules_in-req_date =   lv_del_date .
          exw_order_schedules_inx-req_date =   gc_on.
        endif.

        if wa_xcls_shd-order_qty is not initial.
          exw_order_schedules_in-req_qty =   wa_xcls_shd-order_qty.
          exw_order_schedules_inx-req_qty =   gc_on.
        endif.
        append exw_order_schedules_in to ex_order_schedules_in.
        clear exw_order_schedules_in.
        append exw_order_schedules_inx to ex_order_schedules_inx.
        clear exw_order_schedules_inx.
      endloop.
      clear lv_etenr.
    endloop.
    clear lv_posex.
* Test mode execution
    if r1 = gc_on.
      lv_test_mode = gc_on.
    else.

* Production mode execution
      lv_test_mode = gc_off.
    endif.

* BAPI Extension passing Government contract field  to BAPI
* Processing the header extension
    clear wa_bape_vbak.
    if wa_xcls_header-governmen_contr is not initial.
      wa_bape_vbak-zz_gov_contract = wa_xcls_header-governmen_contr.
      wa_extensionin-structure = 'BAPE_VBAK'.
      wa_extensionin-valuepart1 = wa_bape_vbak.
      append wa_extensionin to it_extensionin.
      clear wa_extensionin.
      wa_bape_vbakx-zz_gov_contract = gc_on.
      wa_extensionin-structure = 'BAPE_VBAKX'.
      wa_extensionin-valuepart1 = wa_bape_vbakx.
      append wa_extensionin to it_extensionin.
      clear wa_extensionin.
    endif.

* call BAPI for create sales order
    call function 'BAPI_SALESORDER_CREATEFROMDAT2'
      exporting
        salesdocumentin      = im_salesdocument
        order_header_in      = ex_order_header_in
        order_header_inx     = ex_order_header_inx
        testrun              = lv_test_mode
      importing
        salesdocument        = gv_salesdocumentin
      tables
        return               = gt_return
        order_items_in       = ex_order_items_in
        order_items_inx      = ex_order_items_inx
        order_partners       = gt_partners
        order_schedules_in   = ex_order_schedules_in
        order_schedules_inx  = ex_order_schedules_inx
        order_conditions_in  = ex_condition_in
        order_conditions_inx = ex_condition_inx
        order_text           = gt_texts
        extensionin          = it_extensionin.

* If any error information is found, then donot commit just display error information
    read table gt_return into gs_return with key type = 'E'.
    if sy-subrc <> 0.
      clear gs_return.
      read table gt_return into gs_return with key type = 'A'.
      if sy-subrc <> 0.

*If no error information is found and production mode radio button is selected then
* call the bapi_transaction_commit function module to commit the created sales order.
        if r2 = gc_on.
          call function 'BAPI_TRANSACTION_COMMIT'.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-msgs   = text-015.
          wa_err_msgs-light  = gc_green.
          wa_err_msgs-new_ord_no = gv_salesdocumentin.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        elseif r1 = gc_on.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-msgs   = text-015.
          wa_err_msgs-light  = gc_green.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.
      endif.
    else.
      clear gs_return.
      loop at gt_return into gs_return where  ( type = 'E'
                                             or type = 'A' )
                                              and number ne '219'.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-msgs   = gs_return-message.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endloop.

    endif.
* clear work area
    clear:  im_salesdocument,
            ex_order_header_in,
            ex_order_header_inx,
            gv_salesdocumentin,
            gt_return,
            ex_order_items_in,
            ex_order_items_inx,
            gt_partners,
            ex_order_schedules_in,
            ex_order_schedules_inx,
            lv_test_mode,
            wa_xcls_header.
  endloop.
  sort it_err_msgs by ord_no.
endform.                    "create_sales_orders

*&---------------------------------------------------------------------*
*&      Form  GET_SOURCE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form get_source_data .

* read customer header information from spread sheet
  perform read_mapping_file using p_hdr  changing it_xcls_header.

* read customer item information from spread sheet
  perform read_mapping_file using p_itm  changing it_xcls_item.

* read customer schedule information from spread sheet
  perform read_mapping_file using p_schd  changing it_xcls_shd.

* read customer master information from spread sheet
  perform read_mapping_file using p_cmstr  changing gt_cusmstr.

*read material master infromation from spread sheet
  perform read_mapping_file using p_mmstr  changing gt_matmstr.

* read wbs element information  from spread sheet
  perform read_mapping_file using p_welem  changing gt_wbselem.

endform.                    "GET_SOURCE_DATA

*&---------------------------------------------------------------------*
*&      Form  read_mapping_file
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_CUSMAS   text
*      -->PT_CUSMAS  text
*----------------------------------------------------------------------*
form read_mapping_file  using    p_cusmas type string
                        changing pt_cusmas type standard table.

  data : lv_filename type rlgrap-filename,
         it_raw type truxs_t_text_data.
  lv_filename = p_cusmas.

* read the material master, customer maters, and WBS element information.
  call function 'TEXT_CONVERT_XLS_TO_SAP'
    exporting
      i_line_header        = gc_on
      i_tab_raw_data       = it_raw
      i_filename           = lv_filename
    tables
      i_tab_converted_data = pt_cusmas[]
    exceptions
      conversion_failed    = 1
      others               = 2.

  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.                    "read_mapping_file

*&---------------------------------------------------------------------*
*&      Form  date_calc
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_DATE     text
*----------------------------------------------------------------------*
form date_calc changing p_date type datum.

  data: lv_len type i,
        lv_dd(2) type n,
        lv_mm(2) type n,
        lv_yyyy(4) type n.
* perform the date calculation, as the inputted date is in different format.
  lv_len = strlen( p_date ).
  case lv_len.
    when '6'.
      lv_mm = p_date+0(1).
      lv_dd = p_date+1(1).
      lv_yyyy = p_date+2(4).
      concatenate lv_yyyy lv_mm lv_dd into p_date in character mode.
    when '7'.
      lv_mm = p_date+0(2).
      if lv_mm gt 12.
        lv_mm = p_date+0(1).
        lv_dd = p_date+1(2).
        lv_yyyy = p_date+3(4).
      else.
        lv_mm = p_date+0(2).
        lv_dd = p_date+2(1).
        lv_yyyy = p_date+3(4).
      endif.
      concatenate lv_yyyy lv_mm lv_dd into p_date in character mode.
    when '8'.
      lv_mm = p_date+0(2).
      lv_dd = p_date+2(2).
      lv_yyyy = p_date+4(4).
      concatenate lv_yyyy lv_mm lv_dd into p_date in character mode.

  endcase.


endform.                    "date_calc
*&---------------------------------------------------------------------*
*&      Form  VALIDATE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form validate_data .

  loop at it_xcls_header into wa_xcls_header.
* fill the Order type information into header internal table
    if wa_xcls_header-order_type is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-016.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.

* fill the Distribution channel information into header internal table
    if wa_xcls_header-distribution_channel is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-017.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.
* fill the Division information into header internal table
    if wa_xcls_header-division is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-018.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.
* fill the Reference doc number information into header internal table
    if wa_xcls_header-ref_doc_number is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-019.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.
* fill the Usage indicator information into header internal table
    if wa_xcls_header-usage_indicator is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-020.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.

* fill the partner information into partner internal table
    if wa_xcls_header-sold_to is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-021.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.
* fill the partner information into partner internal table
    if wa_xcls_header-ship_to is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-022.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.
* fill the Employee responsible information into header internal table
    if wa_xcls_header-employee_responsible is initial.
      wa_err_msgs-ord_no = wa_xcls_header-order_header.
      wa_err_msgs-item_no = gc_blank.
      wa_err_msgs-schd_no = gc_blank.
      wa_err_msgs-msgs = text-023.
      wa_err_msgs-light  = gc_red.
      append wa_err_msgs to it_err_msgs.
      clear wa_err_msgs.
    endif.


* fill the item information of above header into bapi structure
    loop at it_xcls_item into  wa_xcls_item where order_item = wa_xcls_header-order_header.
      if wa_xcls_item-order_item is initial.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-item_no = gc_blank.
        wa_err_msgs-schd_no = gc_blank.
        wa_err_msgs-msgs = text-024.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.
      if wa_xcls_item-item is initial .
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-item_no = gc_blank.
        wa_err_msgs-schd_no = gc_blank.
        wa_err_msgs-msgs = text-025.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

      if wa_xcls_item-plant is initial.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-item_no = wa_xcls_item-item .
        wa_err_msgs-schd_no = gc_blank.
        wa_err_msgs-msgs = text-026.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

      if wa_xcls_item-material is initial.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-item_no = wa_xcls_item-item .
        wa_err_msgs-schd_no = gc_blank.
        wa_err_msgs-msgs = text-027.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

      if wa_xcls_item-unit_of_sale is initial.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-item_no = wa_xcls_item-item .
        wa_err_msgs-schd_no = gc_blank.
        wa_err_msgs-msgs = text-028.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

      if wa_xcls_item-route is initial.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-item_no = wa_xcls_item-item .
        wa_err_msgs-schd_no = gc_blank.
        wa_err_msgs-msgs = text-029.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

      if wa_xcls_item-price is initial.
        wa_err_msgs-ord_no = wa_xcls_header-order_header.
        wa_err_msgs-item_no = wa_xcls_item-item .
        wa_err_msgs-schd_no = gc_blank.
        wa_err_msgs-msgs = text-030.
        wa_err_msgs-light  = gc_red.
        append wa_err_msgs to it_err_msgs.
        clear wa_err_msgs.
      endif.

* fill the schedule line information of above items into bapi structure
      loop at it_xcls_shd into wa_xcls_shd where order_shd = wa_xcls_header-order_header
                                                 and item_shd = wa_xcls_item-item.

        if wa_xcls_shd-order_shd is initial.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-item_no = wa_xcls_item-item .
          wa_err_msgs-schd_no = wa_xcls_shd-scheduele.
          wa_err_msgs-msgs = text-031.
          wa_err_msgs-light  = gc_red.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.

        if wa_xcls_item-item is initial.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-item_no = wa_xcls_item-item .
          wa_err_msgs-schd_no = wa_xcls_shd-scheduele.
          wa_err_msgs-msgs = text-032.
          wa_err_msgs-light  = gc_red.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.

        if wa_xcls_shd-scheduele is initial.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-item_no = wa_xcls_item-item .
          wa_err_msgs-schd_no = wa_xcls_shd-scheduele.
          wa_err_msgs-msgs = text-033.
          wa_err_msgs-light  = gc_red.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.

        if wa_xcls_shd-delivery_date is initial.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-item_no = wa_xcls_item-item .
          wa_err_msgs-schd_no = wa_xcls_shd-scheduele.
          wa_err_msgs-msgs = text-034.
          wa_err_msgs-light  = gc_red.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.

        if wa_xcls_shd-order_qty is initial.
          wa_err_msgs-ord_no = wa_xcls_header-order_header.
          wa_err_msgs-item_no = wa_xcls_item-item .
          wa_err_msgs-schd_no = wa_xcls_shd-scheduele.
          wa_err_msgs-msgs = text-035.
          wa_err_msgs-light  = gc_red.
          append wa_err_msgs to it_err_msgs.
          clear wa_err_msgs.
        endif.
      endloop.
    endloop.
  endloop.
endform.                    " VALIDATE_DATA
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ERRORS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form display_results .

  wa_fieldcat-row_pos   = '1'.
  wa_fieldcat-col_pos   = '1'.
  wa_fieldcat-fieldname = 'ORD_NO'.
  wa_fieldcat-tabname   = 'IT_ERR_MSGS'.
  wa_fieldcat-seltext_l = text-036.
  append wa_fieldcat to it_fieldcat.
  clear wa_fieldcat.

  wa_fieldcat-row_pos   = '1'.
  wa_fieldcat-col_pos   = '2'.
  wa_fieldcat-fieldname = 'ITEM_NO'.
  wa_fieldcat-tabname   = 'IT_ERR_MSGS'.
  wa_fieldcat-seltext_l = text-037.
  append wa_fieldcat to it_fieldcat.
  clear wa_fieldcat.

  wa_fieldcat-row_pos   = '1'.
  wa_fieldcat-col_pos   = '4'.
  wa_fieldcat-fieldname = 'MSGS'.
  wa_fieldcat-tabname   = 'IT_ERR_MSGS'.
  wa_fieldcat-seltext_l = text-038.
  append wa_fieldcat to it_fieldcat.
  clear wa_fieldcat.

 if r1 = gc_off.

  wa_fieldcat-row_pos   = '1'.
  wa_fieldcat-col_pos   = '5'.
  wa_fieldcat-fieldname = 'NEW_ORD_NO'.
  wa_fieldcat-tabname   = 'IT_ERR_MSGS'.
  wa_fieldcat-seltext_l = text-039.
  append wa_fieldcat to it_fieldcat.
  clear wa_fieldcat.

 endif.

  wa_fieldcat-row_pos   = '1'.
  wa_fieldcat-col_pos   = '6'.
  wa_fieldcat-fieldname = 'LIGHT'.
  wa_fieldcat-tabname   = 'IT_ERR_MSGS'.
  wa_fieldcat-seltext_l = text-040.
  append wa_fieldcat to it_fieldcat.
  clear wa_fieldcat.

  gs_layout-colwidth_optimize = gc_on.
  gs_layout-lights_tabname = 'IT_ERR_MSGS'.
  gs_layout-lights_fieldname = 'LIGHT'.

  call function 'REUSE_ALV_GRID_DISPLAY'
   exporting
     i_callback_program                = sy-repid
     i_grid_title                      = text-041
     is_layout                         = gs_layout
     it_fieldcat                       = it_fieldcat
    tables
      t_outtab                          = it_err_msgs
     exceptions
       program_error                     = 1
       others                            = 2
            .
  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.                    " DISPLAY_ERRORS
**************************
excell Sheet for test cases
https://docs.google.com/?tab=mo&authuser=0&pli=1#home

No comments:

Post a Comment