CRM and CX Blogs by Members
Find insights on SAP customer relationship management and customer experience products in blog posts from community members. Post your own perspective today!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
1,778

Enabling CRM Transfer Order on REX 3.0

Introduction


Transfer Order functionality is not a standard feature on REX and customization is required in CRM to control the data flow from CRM to REX. The entire functionality of controlling the data flow and data from CRM to REX and vice versa can be custom build and is a part of CRM.

The transfer order data flow custom functionality can be divided into following steps:

  • Transfer order data flow Set-up
  • Transfer order data flow execution.

Transfer order data flow Set-up


Transfer order data flow set-up involves maintenance of custom tables for device settings to control the transaction type for order creation in REX, filtering the transfer order for front end download.

The below are required to be maintained as part of the transfer order data flow Set-up

  • Mapping Transfer Order Fields
  • Maintaining manual discount Table for order pricing
  • Maintaining device setting
  • Assigning download profile to orders
  • Maintaining Transfer order attachment extraction profile
  • Download Custom LOVs and Device Settings

Mapping Transfer Order fields with SUP


Transfer order fields as required in the REX is mapped for MBO generation. The set of fields which are required to map for MBO generation are grouped on the basis whether the fields are

  1. Header
  2. Item
  3. Partner
  4. Notes
  5. Attachments.

List of fields which are considered are explained in the relevant process (Download, Create, and Update).

Maintaining manual discount Table for order pricing


The manual discount condition types in REX are maintained by customization. The discount condition types (Absolute and %) are maintained in CRM and sent to frontend

Maintaining device setting


A custom table ZREXTYPE is created to maintain the device setting. Device setting are to be maintained to identify the sales order transaction type which will be used for the creation of transfer order from REX.

The custom table has following fields in the given order:

  1. S.No

Field Name

Field Type

Width

Description

1

PROCESS_TYPE

Char

4

Business Transaction Type

2

P_DESCRIPTION

Char

40

Description

The sales order type which is maintained in the “transaction type” field is used for the creation of sales order in REX.

Assigning download profile to orders


A custom table ZSO_FILT  is maintained to control the amount of data flow download on REX. Custom table contains various filters which is going to restrict the number of transfer order download on the front end.

The custom table has following fields in the given order:

  1. S.No

Field Name

Field Type

Description

1

DAYS_SELKEY

CHAR

12

CRM Mobile Retail Customizing Profile Name

2

ATT_TYPE_SELKEY

CHAR

12

CRM Mobile Retail Customizing Profile Name

3

ATT_SIZE_SELKEY

CHAR

12

CRM Mobile Retail Customizing Profile Name

4

MAX_NUM_ORD

NUMC

4

No of objects

Combination of various filters can be used (S.No 1 + S.No 4) to control the transfer order flowing to the REX from CRM on basis of numbers and the creation date.

Maintaining Transfer order attachment extraction profile


Restricting the size of attachments and the type of attachments is important for performance reasons. Ideally the size of a single attachment must not exceed 7.5‒10 MB.

The attachment size and attachment types with the transfer orders to be downloaded on REX can be controlled by configuring:

  1. Attachment size profile : Mention the size of the allowed attachment
  2. Attachment Type profile: Mention the attachment type allowed. Example: PDF, doc etc.

The above profiles can be maintained by standard configuration in CRM.

A custom table Z_TO_ATTACHMENTS is required to assign the two attachment profiles for the transfer orders. Only the attachment profiles maintained in the table will be considered for the data download.

The custom table has following fields in the given order:

  1. S.No

Field Name

Field Type

Description

1

Attachment Size

Profile

Char

The profile will control the maximum size of attachments allowed for download to client device. Only those attachments of the sizes that are defined in the attachment size profile are downloaded to the device.

2

Attachment Type Profile

Char

The profile will control the allowed attachment types.

3

Active

Radio Button

Several combinations of filters (S.No 1 + S.No 2) can be maintained. But only one combination can be active at a time. Select the combination of filter to be active.

Download Custom LOVs and Device Settings


A custom BAPI ZGET_CUSTOM_VALUES is used to download:

  1. Custom LOV’s: Custom List of values is downloaded to REX with the use of the custom program.
  2. Device Settings mentioned in custom table ZREXTYPE: Transaction type maintained in the custom table ZREXTYPEare downloaded to REX by using the custom BAPI ZGET_CUSTOM_VALUES.

Transfer order data flow execution


Once transfer order data is setup with custom tables and custom program we can use them for the basic data flow operations. The data flow can happen between REX and CRM for the following operation:

  1. Download of Transfer order to REX
  2. Creation of Transfer order in REX
  3. Updating Transfer order from REX.

Download of Transfer order to REX : Data Flow is from CRM to REX


A custom BAPI is created to enable the data download from CRM to REX in following steps:

Step 1: When the sync is triggered from REX for transfer orders by sales rep, the SUP server communicates with CRM and requests for the transfer orders from CRM.

Step 2: Transfer orders are filtered using the custom table Z_TO_ASSIGN_DOWN_PROF on the basis of maximum no or / and the document date.

Step 3: Attachments with the transfer order are filtered on basis of attachment type and attachment size. The parameters used to filter the attachments are stored in Z_TO_ATTACHMENTS.

Source code


FUNCTION zso_download.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_READ_ITEMS) TYPE  BOOLEAN DEFAULT 'X'
*"     VALUE(IV_READ_PARTNERS) TYPE  BOOLEAN DEFAULT 'X'
*"     VALUE(IV_READ_NOTES) TYPE  BOOLEAN DEFAULT 'X'
*"     VALUE(IV_READ_ATTACHMENTS) TYPE  BOOLEAN DEFAULT 'X'
*"  EXPORTING
*"     VALUE(ET_HEADER) TYPE  ZSD_HEADER_T
*"     VALUE(ET_ITEMS) TYPE  ZSD_ITEM_T
*"     VALUE(ET_PARTNERS) TYPE  ZSD_PARTNER_T
*"     VALUE(ET_NOTES) TYPE  ZSD_NOTE_T
*"     VALUE(ET_ATTACHMENTS) TYPE  /MAC/RST_DOC
*"     VALUE(RETURN) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------

DATA: lv_username   TYPE syuname.
DATA: i_order_guid TYPE crmt_object_guid_tab.
DATA: wa_return     TYPE bapiret2.
data :    ls_dcn_act                TYPE /mac/rsc_dcn_act.
lv_username = sy-uname.

CALL FUNCTION 'ZSO_GETLIST'
EXPORTING
iv_username    = lv_username
IMPORTING
et_order_guids = i_order_guids.
IF i_order_guids IS NOT INITIAL.
SELECT SINGLE * FROM /mac/rsc_dcn_act INTO ls_dcn_act WHERE ACTIVE = 'X'.
if sy-subrc eq 0.
DATA: lo_ref  TYPE REF TO   zcl_so_download.
CREATE OBJECT lo_ref.
lo_ref->so_get_list_aft( EXPORTING i_order_guids = i_order_guids ).
et_header              =  lo_ref->gt_header.
et_items               =  lo_ref->gt_items.
et_partners            =  lo_ref->gt_partners.
et_notes               =  lo_ref->gt_notes.
et_attachments         =  lo_ref->gt_attachments.
return                 =  lo_ref->gt_return.
else.
CALL FUNCTION 'ZSO_GETDETAIL'
EXPORTING
it_order_guids      = i_order_guids
iv_read_items       = iv_read_items
iv_read_partners    = iv_read_partners
iv_read_notes       = iv_read_notes
iv_read_attachments = iv_read_attachments
IMPORTING
et_header           = et_header
et_items            = et_items
et_partners         = et_partners
et_notes            = et_notes
et_attachments      = et_attachments
return              = return.

ENDIF.


ENDIF.
ENDFUNCTION.

FUNCTION zso_getlist.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IV_USERNAME) TYPE  SY-UNAME DEFAULT SY-UNAME
*"  EXPORTING
*"     REFERENCE(ET_ORDER_GUIDS) TYPE  CRMT_OBJECT_GUID_TAB
*"----------------------------------------------------------------------




" get the list of accounts of the user
CALL FUNCTION '/MAC/RSFM_GET_BP_LIST'
EXPORTING
iv_username   = iv_username
IMPORTING
et_account_id = i_account_id.



SORT i_trans_type BY process_type.
DELETE ADJACENT DUPLICATES FROM i_trans_type COMPARING process_type.

" Get the Sales order download criteria
SELECT SINGLE days_selkey
max_num_ord
sold_to
ship_to
active
FROM zso_filt
INTO CORRESPONDING FIELDS OF wa_order_criteria
WHERE active EQ 'X'.

CLEAR lv_max_num_ord.
lv_max_num_ord = wa_order_criteria-max_num_ord.

SELECT SINGLE days_behind
days_ahead
FROM /mac/rsc_days
INTO wa_days
WHERE days_selkey EQ wa_order_criteria-days_selkey.

CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_object_type.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_eq.
wa_selection_param-low       = lc_bus2000115.
APPEND wa_selection_param TO i_selection_param.

IF i_trans_type IS NOT INITIAL.
LOOP AT i_trans_type INTO wa_trans_type.
CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_process_type.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_eq.
wa_selection_param-low       = wa_trans_type-process_type.
APPEND wa_selection_param TO i_selection_param.
ENDLOOP.
ENDIF.

IF wa_order_criteria-sold_to EQ 'X'.
CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_partner_fct.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_eq.
wa_selection_param-low       = '00000001'.
APPEND wa_selection_param TO i_selection_param.
ENDIF.

IF wa_order_criteria-ship_to EQ 'X'.
CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_partner_fct.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_eq.
wa_selection_param-low       = '00000002'.
APPEND wa_selection_param TO i_selection_param.
ENDIF.

"Partner
LOOP AT i_account_id INTO wa_account_id.
CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_bu_partner.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_eq.
wa_selection_param-low       = wa_account_id-partner_id.
APPEND wa_selection_param TO i_selection_param.
ENDLOOP.

***************************CRM Orders************************************
" Setting of start and end date
lv_days_back = wa_days-days_behind.
IF lv_days_back IS INITIAL.
lv_start_date = sy-datum.
ELSE.
lv_start_date = sy-datum - lv_days_back. "Deduct the days back to get the valid from date
ENDIF.

lv_days_ahead = wa_days-days_ahead.
IF lv_days_ahead IS INITIAL.
lv_end_date = sy-datum.
ELSE.
lv_end_date = sy-datum + lv_days_ahead.  "Add the days ahead to get the valid to date
ENDIF.

" Posting date
CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_posting_date.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_bt.
wa_selection_param-low       = lv_start_date.
wa_selection_param-high      = lv_end_date.
APPEND wa_selection_param TO i_selection_param.

CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_siebel_no.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_eq.
wa_selection_param-low       = abap_off. "If blank
APPEND wa_selection_param TO i_selection_param.

"Set the Selection Parameters to Oredr Object Search
CREATE OBJECT lr_1o_search.

CALL METHOD lr_1o_search->set_selection_parameters
EXPORTING
iv_obj_il                 = 'BTQSLSORD'
it_selection_parameters   = i_selection_param
IMPORTING
et_return                 = i_return
EXCEPTIONS
partner_fct_error         = 1
object_type_not_found     = 2
multi_value_not_supported = 3
OTHERS                    = 4.

IF sy-subrc <> 0.
RETURN.
ENDIF.

REFRESH i_orders[].
CALL METHOD lr_1o_search->get_result_values
EXPORTING
iv_max_hits = '9999999'
IMPORTING
et_results  = i_orders
et_return   = i_return.

APPEND LINES OF i_orders TO i_all_orders.
CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_submitted_date.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_bt.
wa_selection_param-low       = lv_start_date.
wa_selection_param-high      = lv_end_date.
APPEND wa_selection_param TO i_selection_param.

CLEAR wa_selection_param.
wa_selection_param-attr_name = lc_siebel_no.
wa_selection_param-sign      = lc_i.
wa_selection_param-option    = lc_ne.
wa_selection_param-low       = abap_off. "If not blank
APPEND wa_selection_param TO i_selection_param.

"Set the Selection Parameters to Oredr Object Search
CREATE OBJECT lr_1o_search.

CALL METHOD lr_1o_search->set_selection_parameters
EXPORTING
iv_obj_il                 = 'BTQSLSORD'
it_selection_parameters   = i_selection_param
IMPORTING
et_return                 = i_return
EXCEPTIONS
partner_fct_error         = 1
object_type_not_found     = 2
multi_value_not_supported = 3
OTHERS                    = 4.

IF sy-subrc <> 0.
RETURN.
ENDIF.

REFRESH i_orders[].
CALL METHOD lr_1o_search->get_result_values
EXPORTING
iv_max_hits = '9999999'
IMPORTING
et_results  = i_orders
et_return   = i_return.

APPEND LINES OF i_orders TO i_all_orders.

*****************************No. Of Orders Check****************************
" Check if we have enough records for orders
DESCRIBE TABLE i_all_orders LINES lv_result_size.
IF lv_result_size > lv_max_num_ord.
lv_delete_from_index = lv_max_num_ord + 1.
DELETE i_all_orders FROM lv_delete_from_index.
ENDIF.

******************************Pass Order GUIDs***************************
IF i_all_orders IS NOT INITIAL.
LOOP AT i_all_orders INTO wa_orders.
INSERT wa_orders-guid INTO TABLE et_order_guids.
ENDLOOP.
ENDIF.

ENDFUNCTION.

FUNCTION zso_getdetail.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IT_ORDER_GUIDS) TYPE  CRMT_OBJECT_GUID_TAB
*"     VALUE(IV_READ_ITEMS) TYPE  BOOLEAN DEFAULT 'X'
*"     VALUE(IV_READ_PARTNERS) TYPE  BOOLEAN DEFAULT 'X'
*"     VALUE(IV_READ_NOTES) TYPE  BOOLEAN DEFAULT 'X'
*"     VALUE(IV_READ_ATTACHMENTS) TYPE  BOOLEAN DEFAULT 'X'
*"  EXPORTING
*"     VALUE(ET_HEADER) TYPE  ZSD_HEADER_T
*"     VALUE(ET_ITEMS) TYPE  ZSD_ITEM_T
*"     VALUE(ET_PARTNERS) TYPE  ZSD_PARTNER_T
*"     VALUE(ET_NOTES) TYPE  ZSD_NOTE_T
*"     VALUE(ET_ATTACHMENTS) TYPE  /MAC/RST_DOC
*"     VALUE(RETURN) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------

CONSTANTS:
c_header        TYPE crmt_object_name VALUE 'ORDERADM_H',
c_item          TYPE crmt_object_name VALUE 'ORDERADM_I',
c_product       TYPE crmt_object_name VALUE 'PRODUCT_I',
c_sales         TYPE crmt_object_name VALUE 'SALES',
c_pricing       TYPE crmt_object_name VALUE 'PRICING',
c_pricing_item  TYPE crmt_object_name VALUE 'PRICING_I',
c_organisation  TYPE crmt_object_name VALUE 'ORGMAN',
c_customer_h    TYPE crmt_object_name VALUE 'CUSTOMER_H',
c_text          TYPE crmt_object_name VALUE 'TEXTS',
c_schedule      TYPE crmt_object_name VALUE 'SCHEDLIN',
c_schedule_item TYPE crmt_object_name VALUE 'SCHEDLIN_I',
c_partner       TYPE crmt_object_name VALUE 'PARTNER',
c_cumulated_h   TYPE crmt_object_name VALUE 'CUMULAT_H',
c_status        TYPE crmt_object_name VALUE 'STATUS',
c_status_head   TYPE crmt_object_name VALUE 'STATUS_H'.

TYPES: BEGIN OF t_attach,
att_type_selkey TYPE /mac/rsde_profile_name,
att_size_selkey TYPE /mac/rsde_profile_name,
max_num_ord     TYPE zno_of_objects,
END OF t_attach.
DATA:
lv_guid16            TYPE crmt_object_guid,
lv_tabix(5) TYPE n,
lv_attachment_size   TYPE sdok_fsize,
lv_att_size          TYPE sdok_fsize.

DATA:
wa_header_out   TYPE zsd_header,
wa_item_out     TYPE zsd_item,
wa_partners_out TYPE zsd_partner,
wa_notes_out    TYPE zsd_note.

DATA:
i_header        TYPE  crmt_orderadm_h_wrkt,
i_item          TYPE  crmt_orderadm_i_wrkt,
i_product       TYPE  crmt_product_i_wrkt,
i_sales         TYPE  crmt_sales_wrkt,
i_pricing       TYPE  crmt_pricing_wrkt,
i_pricing_item  TYPE  crmt_pricing_i_wrkt,
i_organisation  TYPE  crmt_orgman_wrkt,
i_customer_h    TYPE  crmt_customer_h_wrkt,
i_text          TYPE  crmt_text_wrkt,
i_schedule      TYPE  crmt_schedlin_wrkt,
i_schedule_item TYPE  crmt_schedlin_i_wrkt,
i_partner       TYPE  crmt_partner_external_wrkt,
i_cumulated_h   TYPE  crmt_cumulat_h_wrkt,
i_status        TYPE  crmt_status_wrkt,
i_status_head   TYPE  crmt_status_h_wrkt,
i_lines         TYPE  comt_text_lines_t,
i_lines_temp    TYPE  comt_text_lines_t,
i_itf_text      TYPE  comt_text_lines_t,
i_textstream    TYPE  TABLE OF tdline,
i_order_attach  TYPE  /mac/rst_doc,
i_requested_objects  TYPE  crmt_object_name_tab,
i_mime_type          TYPE /mac/rst_doc_types,
i_header_tab         TYPE crmt_orderadm_h_wrkt_std,
i_return             TYPE bapiret2_t.

DATA:
wa_header        TYPE  crmt_orderadm_h_wrk,
wa_item          TYPE  crmt_orderadm_i_wrk,
wa_product       TYPE  crmt_product_i_wrk,
wa_sales         TYPE  crmt_sales_wrk,
wa_pricing       TYPE  crmt_pricing_wrk,
wa_pricing_item  TYPE  crmt_pricing_i_wrk,
wa_organisation  TYPE  crmt_orgman_wrk,
wa_customer_h    TYPE  crmt_customer_h_wrk,
wa_text          TYPE  crmt_text_wrk,
wa_schedule      TYPE  crmt_schedlin_wrk,
wa_schedule_item TYPE crmt_schedlin_i_wrk,
wa_partner       TYPE  crmt_partner_external_wrk,
wa_cumulated_h   TYPE  crmt_cumulat_h_wrk,
wa_status        TYPE  crmt_status_wrk,
wa_status_head   TYPE  crmt_status_h_wrk,
wa_lines         TYPE  tline,
wa_lines_temp    TYPE  tline,
wa_itf_text      TYPE  tline,
wa_textstream    TYPE  tdline,
wa_order_attach  TYPE  /mac/rss_doc,
wa_requested_objects TYPE crmt_object_name,
wa_attach_criteria   TYPE t_attach,
wa_return            TYPE  bapiret2.

********************************************************************************

IF it_order_guids IS NOT INITIAL.

******* Fill Requested Objects Table for Reading order data

wa_requested_objects = c_header .
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_item.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_product.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_sales.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_pricing.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_pricing_item.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_organisation.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_customer_h .
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_text.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_schedule.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_schedule_item.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_partner.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_cumulated_h.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_status.
INSERT wa_requested_objects INTO TABLE i_requested_objects.
wa_requested_objects = c_status_head.
INSERT wa_requested_objects INTO TABLE i_requested_objects.

******* Get Order Data from CRM_ORDER_READ
CALL FUNCTION 'CRM_ORDER_READ'
EXPORTING
it_header_guid       = it_order_guids
it_requested_objects = i_requested_objects
IMPORTING
et_orderadm_h        = i_header
et_orderadm_i        = i_item
et_product_i         = i_product
et_sales             = i_sales
et_pricing           = i_pricing
et_pricing_i         = i_pricing_item
et_orgman            = i_organisation
et_customer_h        = i_customer_h
et_text              = i_text
et_schedlin          = i_schedule
et_partner           = i_partner
et_cumulat_h         = i_cumulated_h
et_status            = i_status
et_status_h          = i_status_head
et_schedlin_i        = i_schedule_item
EXCEPTIONS
document_not_found   = 1
error_occurred       = 2
document_locked      = 3
no_change_authority  = 4
no_display_authority = 5
no_change_allowed    = 6
OTHERS               = 7.

IF i_header IS INITIAL.
CALL FUNCTION 'BALW_BAPIRETURN_GET2'
EXPORTING
type   = 'E'
cl     = 'ZREX'
number = 013
IMPORTING
return = wa_return.
APPEND wa_return TO return.
ELSE.
" Get the Sales order download criteria
SELECT SINGLE
att_type_selkey
att_size_selkey
max_num_ord
FROM zso_filt
INTO CORRESPONDING FIELDS OF wa_attach_criteria
WHERE active EQ 'X'.

"Attachment Type Field Extracted
SELECT mime_type
FROM /mac/rsc_doctyv
INTO TABLE i_mime_type
WHERE att_type_selkey = wa_attach_criteria-att_type_selkey.

" Get the max. attachment size that should be downloaded to the device
SELECT SINGLE att_size
INTO lv_attachment_size
FROM /mac/rsc_docsze
WHERE att_size_selkey = wa_attach_criteria-att_size_selkey.

" Check if we have enough records for returns
i_header_tab[] = i_header[].

LOOP AT i_header_tab INTO wa_header.
CLEAR wa_header_out.
"****** Fill Header structure
wa_header_out-doc_number = wa_header-object_id.
wa_header_out-doc_guid   = wa_header-guid.
wa_header_out-doc_type   = wa_header-process_type.
wa_header_out-doc_date   = wa_header-posting_date.
wa_header_out-created_by = wa_header-created_by.

READ TABLE i_sales INTO wa_sales WITH KEY ref_guid = wa_header-guid.
IF sy-subrc EQ 0.
"  Convert Timestamp to Date
CONVERT TIME STAMP wa_sales-req_dlv_date TIME ZONE wa_sales-req_timezone
INTO DATE wa_header_out-req_date_h.
wa_header_out-po_number = wa_sales-po_number_sold.

ENDIF.

READ TABLE i_pricing INTO wa_pricing WITH KEY ref_guid = wa_header-guid.
IF sy-subrc EQ 0.
wa_header_out-price_date = wa_pricing-price_date.
wa_header_out-currency   = wa_pricing-currency.
ENDIF.

READ TABLE i_organisation INTO wa_organisation WITH KEY ref_guid = wa_header-guid.
IF sy-subrc EQ 0.
wa_header_out-sales_org  = wa_organisation-sales_org.
wa_header_out-distr_chan = wa_organisation-dis_channel.
wa_header_out-division   = wa_organisation-division.
ENDIF.

READ TABLE i_cumulated_h INTO wa_cumulated_h WITH KEY guid = wa_header-guid.
IF sy-subrc EQ 0.
wa_header_out-gross_value = wa_cumulated_h-gross_value.
wa_header_out-net_value   = wa_cumulated_h-net_value.
wa_header_out-surchg_fee  = wa_cumulated_h-freight.
wa_header_out-tax_amount  = wa_cumulated_h-tax_amount.
*    wa_header_out-total
ENDIF.

READ TABLE i_status INTO wa_status WITH KEY guid           = wa_header-guid
  user_stat_proc = wa_header-process_type.
IF sy-subrc EQ 0.
wa_header_out-order_status = wa_status-status.
ENDIF.

READ TABLE i_status_head INTO wa_status_head WITH KEY guid = wa_header-guid.
IF sy-subrc EQ 0.
wa_header_out-overallrej = wa_status_head-cancelled.
ENDIF.

"****** Fill Item Table
IF iv_read_items EQ 'X'.
LOOP AT i_item INTO wa_item WHERE header = wa_header-guid.
CLEAR wa_item_out.
wa_item_out-doc_number = wa_header-object_id.
wa_item_out-doc_guid   = wa_header-guid.
wa_item_out-itm_number = wa_item-number_int.
wa_item_out-itm_guid   = wa_item-guid.
wa_item_out-material   = wa_item-ordered_prod.
wa_item_out-item_categ = wa_item-itm_type.
wa_item_out-reason_rej = wa_item-subst_reason.

READ TABLE i_schedule_item INTO wa_schedule_item WITH KEY guid = wa_item-guid.
IF sy-subrc EQ 0.
wa_item_out-req_qty = wa_schedule_item-order_qty.
ENDIF.

READ TABLE i_product INTO wa_product WITH KEY guid = wa_item-guid.
IF sy-subrc EQ 0.
wa_item_out-sales_unit = wa_product-process_qty_unit.
wa_item_out-base_unit  = wa_product-base_qty_unit.
ENDIF.
READ TABLE i_pricing_item INTO wa_pricing_item WITH KEY guid = wa_item-guid.
IF sy-subrc EQ 0.
wa_item_out-price_indicator = wa_pricing_item-prc_indicator.
*      wa_item_out-cond_type
*      wa_item_out-hg_lv_item
wa_item_out-gross_value     = wa_pricing_item-gross_value.
wa_item_out-net_value       = wa_pricing_item-net_value.
wa_item_out-discount        = wa_pricing_item-total_discount.
wa_header_out-discount      = wa_header_out-discount + wa_item_out-discount.
*          wa_item_out-discount        = wa_item_out-gross_value - wa_item_out-net_value ."wa_pricing_i.
IF wa_item_out-req_qty NE 0.
wa_item_out-discount_per_uom = wa_item_out-discount / wa_item_out-req_qty.
ENDIF.
wa_item_out-tax_amount      = wa_pricing_item-tax_amount.
wa_item_out-currency        = wa_header_out-currency."wa_pricing_item-currency.
ENDIF.
*      wa_item_out-item_mode
*      wa_item_out-prc_stat_i
*      wa_item_out-deliv_stat
APPEND wa_item_out TO et_items.
ENDLOOP.
ENDIF.

APPEND wa_header_out TO et_header.

"******* Fill partner table
IF iv_read_partners EQ 'X'.
LOOP AT i_partner INTO wa_partner WHERE ref_guid EQ wa_header-guid.
CLEAR wa_partners_out.
wa_partners_out-doc_number = wa_header-object_id.
wa_partners_out-doc_guid   = wa_header-guid.
wa_partners_out-partn_role = wa_partner-partner_fct.
*            wa_partners_out-partn_numb = wa_partner-partner_no.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input         = wa_partner-partner_no
IMPORTING
OUTPUT        = wa_partners_out-partn_numb.

APPEND wa_partners_out TO et_partners.
ENDLOOP.

ENDIF.

"******* Fill Notes Table
IF iv_read_notes EQ 'X'.
LOOP AT i_text INTO wa_text WHERE ref_guid = wa_header-guid.  " AND stxh-tdspras = sy-langu.
CLEAR wa_notes_out.
REFRESH i_lines.

LOOP AT wa_text-lines INTO wa_lines.
lv_tabix  =  sy-tabix.
i_lines_temp = wa_text-lines.
APPEND wa_lines TO i_itf_text.

CALL FUNCTION 'CONVERT_ITF_TO_STREAM_TEXT'
TABLES
itf_text    = i_itf_text
text_stream = i_textstream.

READ TABLE i_textstream INDEX 1 INTO wa_itf_text-tdline.
wa_notes_out-doc_number    = wa_header-object_id.
wa_notes_out-doc_guid      = wa_header-guid.
wa_notes_out-laiso         = wa_text-stxh-tdspras.
wa_notes_out-td_object     = wa_text-stxh-tdobject.
wa_notes_out-td_id         = wa_text-stxh-tdid.
wa_notes_out-notes_seq_no  = lv_tabix.
wa_notes_out-note_tdformat = wa_lines-tdformat.
wa_notes_out-notes_lines   = wa_itf_text-tdline.
APPEND wa_notes_out TO et_notes.

CLEAR: i_itf_text[], wa_itf_text, wa_notes_out.
ENDLOOP.
ENDLOOP.
ENDIF.

"******* Fill Attachments table
IF iv_read_attachments EQ 'X'.
CLEAR lv_guid16.
lv_guid16 = wa_header-guid.
" Get Attachment Details for Sales Order
CALL FUNCTION '/MAC/RSFM_GET_DOC_HDR_DETAIL'
EXPORTING
iv_objkey     = lv_guid16
iv_objtype    = wa_header-object_type
iv_object_id  = wa_header-object_id
IMPORTING
et_attachment = i_order_attach
return        = i_return.

APPEND LINES OF i_return TO return.

*     " Filter attachments according to server customization
LOOP AT i_order_attach INTO wa_order_attach.
IF i_mime_type IS NOT INITIAL.
READ TABLE i_mime_type WITH KEY table_line = wa_order_attach-iv_mimetype TRANSPORTING NO FIELDS.
CHECK sy-subrc EQ 0.
ENDIF.

lv_att_size = lv_attachment_size * 1024 * 1024.
IF wa_order_attach-file_size LE lv_att_size.
INSERT wa_order_attach INTO TABLE et_attachments.
ENDIF.
ENDLOOP.
ENDIF.

ENDLOOP.

ENDIF.

ENDIF.

ENDFUNCTION

Creation of Transfer order in REX: Data Flow is from REX to CRM

A custom BAPI is created to enable the created data in REX to sync with CRM in following steps:

Step 1: Transfer order once saved or confirmed in REX is pushed to SUP server.

Step 2: Once the data is in SUP server a RFC call triggers the custom BAPI (Z BAPI) in CRM and the data is pulled from SUP server and is saved as Transfer order in the CRM back end with respective status.

Step 3: Transfer Order ID is generated and based on status it is further processed.

Source code

FUNCTION zso_create.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IS_HEADER) TYPE  ZSD_HEADER
*"  EXPORTING
*"     VALUE(ES_HEADER) TYPE  ZSD_HEADER
*"  TABLES
*"      ITEMS STRUCTURE  ZSD_ITEM
*"      PARTNERS STRUCTURE  ZSD_PARTNER
*"      CONDITIONS STRUCTURE  ZSD_COND
*"      NOTES STRUCTURE  ZSD_NOTE
*"      RETURN STRUCTURE  BAPIRET2
*"----------------------------------------------------------------------
CONSTANTS : lc_sales_rep TYPE crmt_partner_fct VALUE '00000012',
lc_sold_party TYPE crmt_partner_fct VALUE '00000001',
lc_emp_resp  TYPE crmt_partner_fct VALUE '00000014'.

DATA: wa_in_partner    TYPE zsd_partner,
wa_partners      TYPE zsd_partner,
wa_add_partners  TYPE zsd_partner,
wa_items         TYPE zsd_item,
wa_notes         TYPE zsd_note,
wa_conditions    TYPE zsd_cond,
wa_proc_type     TYPE crmc_proc_type,
wa_crmd_terr TYPE crmd_terr_accrel,
wa_lines         TYPE tline,
wa_notes_temp    TYPE zsd_note,
lt_partner_com     TYPE crmt_partner_comt,
lt_input_fields    TYPE crmt_input_field_tab.


DATA: i_lines         TYPE comt_text_lines_t,
i_territory_ovw TYPE crmt_terrman_territory_ovw_t,
i_crmd_terr TYPE STANDARD TABLE OF crmd_terr_accrel,
i_temp_notes    TYPE TABLE OF zsd_note.

DATA: lv_item_no TYPE crmt_item_no,
lv_partner_numb TYPE crmt_partner_number,
lv_employee_id TYPE crmt_employee,
lv_partner_guid TYPE bu_partner_guid,
lv_guid_temp TYPE crmt_object_guid,
lv_terr_guid TYPE crmt_territory_guid,
lv_path_id TYPE char255,
lv_qty     TYPE crmt_schedlin_quan,
lv_count TYPE i,
lv_terr_id TYPE crmt_territory_path_id,
lv_handle  TYPE crmt_handle,
lv_terr_v TYPE CRMT_OBJECT_GUID.

**********************************************************************
***** Refersh interal tables
**********************************************************************
REFRESH : return,
i_sales,
i_orgman,
i_text,
i_schedlin_i,
i_partner,
i_pridoc_com,
i_status,
i_orderadm_h,
i_customer_h,
i_orderadm_i,
i_product_i,
i_input_fields.

CLEAR:    wa_sales,
wa_orgman,
wa_text,
wa_schedlin_i,
wa_partner,
wa_partners,
wa_pridoc_com,
wa_status,
wa_orderadm_h,
wa_customer_h,
wa_orderadm_i,
wa_product_i,
wa_input_fields.


**********************************************************************
* Create  GUIDs for header
**********************************************************************
PERFORM f_create_guid CHANGING gv_guid.


**********************************************************************
* *Calling subroutine to pass values for pass the PO number and
* requested delivery date
**********************************************************************
PERFORM f_create_sales USING is_header-po_number
is_header-req_date_h
IS_HEADER-overallrej
c_handle1
gv_guid.

**********************************************************************
* *Calling subroutine to pass order status
**********************************************************************
IF is_header-order_status IS NOT INITIAL.
PERFORM f_create_status
USING is_header-doc_type
is_header-order_status
lv_handle
gv_guid.
ENDIF.



" If sales rep is not passed, then pass emp resp as sales rep
READ TABLE partners TRANSPORTING NO FIELDS WITH KEY partn_role = lc_sales_rep.
IF sy-subrc NE 0.
READ TABLE partners INTO wa_partners WITH KEY partn_role = lc_emp_resp.
IF sy-subrc EQ 0.
CLEAR wa_add_partners.
wa_add_partners-partn_role = lc_sales_rep.
wa_add_partners-partn_numb = wa_partners-partn_numb.
INSERT wa_add_partners INTO TABLE partners.
ENDIF.
ENDIF.
CLEAR:wa_partners.

READ TABLE partners INTO wa_partners WITH KEY partn_role = lc_emp_resp.
IF sy-subrc EQ 0.
lv_employee_id = wa_partners-partn_numb.
CALL FUNCTION 'CRM_TERRMAN_TERR_BY_EMP_OBJ'
EXPORTING
iv_employee_id   = lv_employee_id
iv_default_check = 'X'
IMPORTING
et_territory_ovw = i_territory_ovw.
DESCRIBE TABLE i_territory_ovw LINES lv_count.
IF lv_count GT 1.
CLEAR:wa_partners.
READ TABLE partners INTO wa_partners WITH KEY partn_role = lc_sold_party.
IF sy-subrc EQ 0.
lv_partner_numb = wa_partners-partn_numb.
SELECT SINGLE partner_guid
FROM but000
INTO lv_partner_guid
WHERE partner EQ wa_partners-partn_numb.
IF sy-subrc EQ 0.
lv_guid_temp = lv_partner_guid.
SELECT *
FROM crmd_terr_accrel
INTO TABLE i_crmd_terr
WHERE partner_guid EQ lv_partner_guid.
IF sy-subrc EQ 0.
IF sy-dbcnt GT 1.
CLEAR:wa_crmd_terr.
READ TABLE i_crmd_terr
INTO wa_crmd_terr
WITH KEY partner_guid = lv_partner_guid
overridden   = 'X'.
IF sy-subrc EQ 0.
lv_terr_guid = wa_crmd_terr-terr_guid.
ELSE.
CLEAR:wa_crmd_terr.
READ TABLE i_crmd_terr
INTO wa_crmd_terr INDEX 1.
lv_terr_guid    = wa_crmd_terr-terr_guid.
ENDIF.
ELSE.
CLEAR:wa_crmd_terr.
READ TABLE i_crmd_terr
INTO wa_crmd_terr INDEX 1.
lv_terr_guid    = wa_crmd_terr-terr_guid.
ENDIF.
CLEAR lv_terr_v.
SELECT SINGLE guid
FROM CRMM_TERRITORY_V
INTO  lv_terr_v
WHERE terr_guid EQ lv_terr_guid.

SELECT SINGLE path_id
FROM crmm_terrstruct
INTO lv_terr_id
WHERE territory_v_guid EQ lv_terr_v.
IF sy-subrc EQ 0.
READ TABLE partners INTO wa_partners WITH KEY PARTN_ROLE = lc_sales_rep.
lv_partner_numb = wa_partners-partn_numb.
lv_path_id                            = lv_terr_id.
wa_partner_attributes-ref_guid        = gv_guid.
wa_partner_attributes-ref_kind        = 'A'.
wa_partner_attributes-ref_partner_fct = lc_sales_rep.
wa_partner_attributes-ref_partner_no  = lv_partner_numb.
wa_partner_attributes-semantic_key    = 'TERRITORY'.
wa_partner_attributes-fieldname       = 'PATH_ID'.
wa_partner_attributes-value = lv_path_id.
wa_partner_attributes-ref_no_type = 'BP'.
wa_partner_attributes-ref_display_type = 'BP'.
*              wa_partner_attributes-changeable = 'X'.
*              SHIFT wa_partner_attributes-ref_partner_no LEFT DELETING LEADING '0'.
INSERT wa_partner_attributes INTO TABLE i_partner_attributes.

READ TABLE partners INTO wa_partners WITH KEY PARTN_ROLE = lc_emp_resp.
lv_partner_numb = wa_partners-partn_numb.
lv_path_id                            = lv_terr_id.
wa_partner_attributes-ref_guid        = gv_guid.
wa_partner_attributes-ref_kind        = 'A'.
wa_partner_attributes-ref_partner_fct =  lc_emp_resp.
wa_partner_attributes-ref_partner_no  = lv_partner_numb.
wa_partner_attributes-semantic_key    = 'TERRITORY'.
wa_partner_attributes-fieldname       = 'PATH_ID'.
wa_partner_attributes-value = lv_path_id.
wa_partner_attributes-ref_no_type = 'BP'.
wa_partner_attributes-ref_display_type = 'BP'.
*              wa_partner_attributes-changeable = 'X'.
*              SHIFT wa_partner_attributes-ref_partner_no LEFT DELETING LEADING '0'.
INSERT wa_partner_attributes INTO TABLE i_partner_attributes.


* PERFORM f_input_fields  USING lv_handle
* gv_guid
*                                            c_a
* c_partner
*                                            c_part_handle1.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
CLEAR:wa_partners,
wa_crmd_terr,
wa_partner_attributes,
lv_count,
lv_partner_numb,
lv_terr_guid,
lv_path_id,
lv_terr_id,
lv_partner_guid,
lv_employee_id.
REFRESH:i_territory_ovw,
i_crmd_terr.
ENDIF.
CLEAR:wa_partners.
" For newly created contact person, BP GUID is passed in place of DOC_GUID,
LOOP AT partners INTO wa_partners WHERE doc_guid IS NOT INITIAL.
" Get the ID for contact person from the GUID
SELECT SINGLE partner
FROM but000
INTO wa_partners-partn_numb
WHERE partner_guid EQ wa_partners-doc_guid.

" Update partners with partner number of contact person
MODIFY partners FROM wa_partners.

ENDLOOP.

**********************************************************************
* *Calling subroutine to fill values for the table PARTNER.
**********************************************************************
LOOP AT partners INTO wa_in_partner.

PERFORM f_create_partner
USING gv_guid
lv_handle
c_a
c_part_handle1
c_kind_of_entry
c_display_type
c_no_type
wa_in_partner-partn_role
wa_in_partner-partn_numb.
ENDLOOP.

**********************************************************************
* *Calling subroutine to fill values for the table ORGMAN.
**********************************************************************
PERFORM f_create_orgman
USING lv_handle
gv_guid
is_header-sales_org
is_header-distr_chan
is_header-division.

IF items[] IS NOT INITIAL.

" Get item increment procedure for given document type
CALL FUNCTION 'CRM_ORDER_PROC_TYPE_SELECT_CB'
EXPORTING
iv_process_type      = is_header-doc_type
IMPORTING
es_proc_type         = wa_proc_type
EXCEPTIONS
entry_not_found      = 1
text_entry_not_found = 2
OTHERS               = 3.

LOOP AT items INTO wa_items.

CLEAR lv_item_no.
IF wa_items-itm_number IS INITIAL.
lv_item_no = sy-tabix * wa_proc_type-incr_item_proc.
ELSE.
lv_item_no = wa_items-itm_number * wa_proc_type-incr_item_proc.
ENDIF.

PERFORM f_create_guid CHANGING gv_guid1.

**********************************************************************
* *Calling subroutine to fill values for the table CRMD_ORDERADM_I.
**********************************************************************
PERFORM f_create_orderadm_i
USING gv_guid1
lv_handle
gv_guid
wa_items-material
lv_item_no
wa_items-item_mode.

CLEAR i_field_names.

PERFORM f_field_names USING: c_ordered_prod '',
'MODE' ''.

PERFORM f_input_fields USING lv_handle
  gv_guid1
c_b
  c_orderadm_i
''.



**********************************************************************
* *Calling subroutine to fill values for the table SCHEDLIN_I.
**********************************************************************
CLEAR lv_qty.
lv_qty = wa_items-req_qty.

PERFORM f_create_schedlin_i
USING gv_guid1 '' lv_qty.

CLEAR i_field_names.

PERFORM f_field_names
USING: 'LOGICAL KEY' '',
'QUANTITY' ''.

PERFORM f_input_fields
USING ''
gv_guid1
c_b
'SCHEDLIN'
''.

CLEAR i_field_names.

PERFORM f_input_fields
USING lv_handle
gv_guid1
c_b
c_schedlin_i
''.

**********************************************************************
* *Calling subroutine to fill values for the table PRODUCT_I.
**********************************************************************
PERFORM f_create_product_i
USING gv_guid1
lv_handle
wa_items-sales_unit.

CLEAR i_field_names.

PERFORM f_field_names USING: c_process_qty_unit ''.

PERFORM f_input_fields USING lv_handle
  gv_guid1
c_b
  c_product_i
''.

**********************************************************************
* *Calling subroutine to fill values for the table CONDITION_CREATE.
**********************************************************************
IF conditions[] IS NOT INITIAL.
READ TABLE conditions INTO wa_conditions WITH KEY mob_itm_number = wa_items-mob_itm_number.
IF sy-subrc EQ 0.
PERFORM f_create_condition TABLES conditions USING wa_items-mob_itm_number
  c_handle
  gv_guid1
  wa_items-req_qty.

ENDIF.
ENDIF.
PERFORM f_create_customer_i USING lv_handle
  is_header-sales_org
  is_header-distr_chan
  wa_items-material
  gv_guid1
c_a.

ENDLOOP.


ENDIF.

**********************************************************************
* *Calling subroutine to fill values for the table TEXT.
**********************************************************************
IF notes[] IS NOT INITIAL.

SORT notes[] BY doc_guid td_id td_object laiso notes_seq_no.

i_temp_notes = notes[].
DELETE ADJACENT DUPLICATES FROM i_temp_notes COMPARING doc_guid td_id td_object laiso.

LOOP AT i_temp_notes INTO wa_notes_temp.
REFRESH i_lines.
LOOP AT notes INTO wa_notes WHERE td_id     = wa_notes_temp-td_id
AND td_object = wa_notes_temp-td_object
AND laiso     = wa_notes_temp-laiso.
CLEAR wa_lines.
wa_lines-tdformat = wa_notes-note_tdformat.
wa_lines-tdline   = wa_notes-notes_lines.
APPEND wa_lines TO i_lines.
ENDLOOP.
PERFORM f_create_text TABLES i_lines USING c_handle1
  gv_guid
  wa_notes-laiso
  wa_notes-td_object
  wa_notes-td_id
'I'.
ENDLOOP.
ENDIF.
**********************************************************************
*Calling subroutine to create sales order through CRM_ORDER_MAINTAIN.
**********************************************************************
PERFORM f_order_maintain.

**********************************************************************
*Calling subroutine to save the sales order and get return messages
**********************************************************************
PERFORM f_order_save TABLES return USING is_header CHANGING es_header.

ENDFUNCTION.

Updating Transfer order from REX : Data Flow is from REX to CRM

A custom BAPI is created to enable the updated data in REX to sync in CRM in following steps

Step 1: Transfer order once updated in REX is pushed to SUP server.

Step 2: Once the data is in SUP server a RFC call triggers the custom BAPI (Z BAPI) in CRM and the data is pulled from SUP server and updated in the Transfer order in the CRM back end with respective status. Transfer order is updated with the match of transfer order ID.

Source code

FUNCTION zso_update.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IS_HEADER) TYPE  ZSD_HEADER
*"  EXPORTING
*"     VALUE(ES_HEADER) TYPE  ZSD_HEADER
*"  TABLES
*"      ITEMS STRUCTURE  ZSD_ITEM
*"      PARTNERS STRUCTURE  ZSD_PARTNER
*"      CONDITIONS STRUCTURE  ZSD_COND
*"      NOTES STRUCTURE  ZSD_NOTE
*"      RETURN STRUCTURE  BAPIRET2
*"----------------------------------------------------------------------
*We will allow update for following fields:
*- Delete the Product
*- Add the Product
*- Change the Product Quantity
*- Add  manual conditions to existing and new items
*- Insert, update or delete Notes

*----------------------------------------------------------------------*
* Internal tables and work areas declaration
*----------------------------------------------------------------------*
DATA: lv_item_no     TYPE crmt_item_no,
lv_flag        TYPE flag,
lv_qty         TYPE crmt_schedlin_quan,
lv_handle      TYPE crmt_handle.

DATA: wa_items         TYPE zsd_item,
wa_notes         TYPE zsd_note,
wa_conditions    TYPE zsd_cond,
wa_notes_temp    TYPE zsd_note,
i_temp_notes     TYPE TABLE OF zsd_note.

DATA: i_lines              TYPE comt_text_lines_t,
wa_lines             TYPE tline,

i_requested_objects  TYPE crmt_object_name_tab,
wa_requested_objects TYPE crmt_object_name,

i_all_schedlin       TYPE crmt_schedlin_wrkt,
wa_all_schedlin      TYPE crmt_schedlin_wrk,

i_order_item         TYPE crmt_orderadm_i_wrkt,
wa_order_item        TYPE crmt_orderadm_i_wrk,

i_header_guid        TYPE crmt_object_guid_tab,
wa_guid_16           TYPE crmt_object_guid.

**********************************************************************
***** Refersh interal tables
**********************************************************************
REFRESH : return,
i_sales,
i_orgman,
i_text,
i_schedlin_i,
i_partner,
i_pridoc_com,
i_status,
i_orderadm_h,
i_customer_h,
i_orderadm_i,
i_input_fields.

CLEAR:    wa_sales,
wa_orgman,
wa_text,
wa_schedlin_i,
wa_partner,
wa_pridoc_com,
wa_status,
wa_orderadm_h,
wa_customer_h,
wa_orderadm_i,
wa_input_fields.

CLEAR lv_flag.

IF is_header-doc_guid IS NOT INITIAL.

gv_header_guid_16          = is_header-doc_guid.
wa_orderadm_h-guid         = gv_header_guid_16.
wa_orderadm_i-header       = gv_header_guid_16.


**********************************************************************
** Update notes
**********************************************************************
IF notes[] IS NOT INITIAL.

SORT notes[] BY doc_guid td_id td_object laiso notes_seq_no.

i_temp_notes = notes[].

DELETE ADJACENT DUPLICATES FROM i_temp_notes COMPARING doc_guid td_id td_object laiso.


LOOP AT i_temp_notes INTO wa_notes_temp.
REFRESH i_lines.
LOOP AT notes INTO wa_notes WHERE td_id     = wa_notes_temp-td_id
AND td_object = wa_notes_temp-td_object
AND laiso     = wa_notes_temp-laiso.
CLEAR wa_lines.
wa_lines-tdformat = wa_notes-note_tdformat.
wa_lines-tdline   = wa_notes-notes_lines.
APPEND wa_lines TO i_lines.
ENDLOOP.

PERFORM f_create_text TABLES i_lines USING lv_handle
  gv_header_guid_16
  wa_notes-laiso
  wa_notes-td_object
  wa_notes-td_id
wa_notes-note_mode.
ENDLOOP.
ENDIF.
**********************************************************************
** Update items
**********************************************************************

LOOP AT items INTO wa_items.

gv_item_guid_16            = wa_items-itm_guid.
wa_orderadm_i-guid         = gv_item_guid_16.

*********************** Update Order Item ****************************
IF wa_items-item_mode EQ c_flag_update.

wa_orderadm_i-mode         = c_update.
" Flage to check to avoid call to CRM_ORDER_RAD in case of multiple items
IF lv_flag NE 'X'.
wa_guid_16 = gv_header_guid_16.
INSERT wa_guid_16 INTO TABLE i_header_guid.

wa_requested_objects = 'SCHEDLIN'.
INSERT wa_requested_objects INTO TABLE i_requested_objects.

CALL FUNCTION 'CRM_ORDER_READ'
EXPORTING
it_header_guid       = i_header_guid
it_requested_objects = i_requested_objects
IMPORTING
et_schedlin          = i_all_schedlin
et_orderadm_i        = i_order_item.

IF sy-subrc EQ 0.
"CRM_ORDER_READ is called, set the flag
lv_flag = 'X'.
ENDIF.
ENDIF.

**** If quantity is to be changed.
IF lv_flag EQ 'X'.

* If the sales order has multiple schedule lines, update first scheduline with requested quantity
LOOP AT i_all_schedlin INTO wa_all_schedlin WHERE item_guid EQ gv_item_guid_16.
IF wa_all_schedlin-schedlin_no EQ '0001'.
IF wa_items-req_qty NE 0.
wa_schedlin-quantity = wa_items-req_qty.
ELSE.
wa_schedlin-quantity = wa_all_schedlin-quantity.
ENDIF.
wa_schedlin-mode      = c_update.
wa_schedlin-guid      = wa_all_schedlin-guid.
wa_schedlin-item_guid = wa_all_schedlin-item_guid.
INSERT wa_schedlin INTO TABLE i_schedlin.
CLEAR wa_schedlin.
ENDIF.
ENDLOOP.
* If the sales order has no schedule lines, insert first scheduline with requested quantity
IF sy-subrc EQ 4.
wa_schedlin-quantity = wa_items-req_qty.
wa_schedlin-mode     = c_insert.
INSERT wa_schedlin INTO TABLE i_schedlin.
CLEAR wa_schedlin.
ENDIF.

wa_schedlin_i-ref_guid   = gv_item_guid_16.
wa_schedlin_i-schedlines = i_schedlin.

CLEAR i_schedlin.
INSERT wa_schedlin_i INTO TABLE i_schedlin_i.
CLEAR wa_schedlin_i.

CLEAR i_field_names.

PERFORM f_field_names
USING:
'LOGICAL KEY' '',
'QUANTITY' '',
'MODE' ''.

PERFORM f_input_fields
USING ''
gv_item_guid_16
c_b
'SCHEDLIN'
''.

PERFORM f_input_fields
USING ''
gv_item_guid_16
c_b
c_schedlin_i
''.

READ TABLE i_order_item INTO wa_order_item WITH KEY guid = gv_item_guid_16.
IF sy-subrc EQ 0.
wa_orderadm_i-guid         = wa_order_item-guid.
wa_orderadm_i-header       = wa_order_item-header.
wa_orderadm_i-product      = wa_order_item-product.
wa_orderadm_i-ordered_prod = wa_order_item-ordered_prod.
wa_orderadm_i-description  = wa_order_item-description.
wa_orderadm_i-number_int   = wa_order_item-number_int.
wa_orderadm_i-itm_type     = wa_order_item-itm_type.
wa_orderadm_i-mode         = c_update."'B'.
INSERT wa_orderadm_i INTO TABLE i_orderadm_i.
ENDIF.
ENDIF.

***********************************************************************
* *Calling subroutine to fill values for the table PRIDOC.
**********************************************************************
IF conditions[] IS NOT INITIAL.
READ TABLE conditions INTO wa_conditions WITH KEY mob_itm_number = wa_items-mob_itm_number.
IF sy-subrc EQ 0.
PERFORM f_create_condition TABLES conditions USING wa_items-mob_itm_number
  lv_handle
  gv_item_guid_16
wa_items-req_qty.
ENDIF.
ENDIF.


*********************** Delete Order Item ****************************
ELSEIF wa_items-item_mode EQ c_flag_delete.

wa_orderadm_i-mode         = c_delete.

INSERT wa_orderadm_i INTO TABLE i_orderadm_i.
CLEAR wa_orderadm_i.

CLEAR i_field_names.

PERFORM f_field_names USING: 'MODE' ''.

PERFORM f_input_fields USING '' gv_item_guid_16 c_b c_orderadm_i ''.

************************ Insert Order Item ***************************
ELSEIF wa_items-item_mode EQ c_flag_insert.

PERFORM f_create_guid CHANGING gv_guid1.

wa_orderadm_i-mode         = c_insert.

PERFORM f_create_orderadm_i
USING gv_guid1
c_handle
gv_header_guid_16
wa_items-material
lv_item_no
*                 wa_items-item_categ
wa_items-item_mode.

CLEAR i_field_names.

PERFORM f_field_names USING: c_ordered_prod '',
'ITM_TYPE' '',
'MODE' ''.

PERFORM f_input_fields USING c_handle gv_guid1 c_b c_orderadm_i ''.

**********************************************************************
* *Calling subroutine to fill values for the table SCHEDLIN_I.
**********************************************************************
CLEAR lv_qty.
lv_qty = wa_items-req_qty.

PERFORM f_create_schedlin_i
USING gv_guid1 gv_mode lv_qty.

CLEAR i_field_names.

PERFORM f_field_names
USING: 'LOGICAL KEY' '',
'QUANTITY' ''.

PERFORM f_input_fields
USING ''
gv_guid1
c_b
'SCHEDLIN'
''.

CLEAR i_field_names.

PERFORM f_input_fields
USING c_handle
gv_guid1
c_b
c_schedlin_i
''.
**********************************************************************
* *Calling subroutine to fill values for the table PRIDOC.
**********************************************************************
IF conditions[] IS NOT INITIAL.
READ TABLE conditions INTO wa_conditions WITH KEY mob_itm_number = wa_items-mob_itm_number.
IF sy-subrc EQ 0.
PERFORM f_create_condition TABLES conditions USING wa_items-mob_itm_number
  lv_handle
  gv_guid1
  wa_items-req_qty.

ENDIF.
ENDIF.
ENDIF.

ENDLOOP.


ENDIF.

**********************************************************************
*Calling subroutine to Update sales order through CRM_ORDER_MAINTAIN.
**********************************************************************
PERFORM f_order_maintain.

**********************************************************************
*Calling subroutine to save the sales order and get return messages
**********************************************************************
PERFORM f_order_save TABLES return USING is_header CHANGING es_header.

ENDFUNCTION.

FUNCTION-POOL zsalesdocuments. "MESSAGE-ID ..

*----------------------------------------------------------------------*
* Constant Declaration
*----------------------------------------------------------------------*
DATA: c_handle(10)      TYPE n VALUE '0000000001',
c_handle1(10) TYPE n VALUE '0000000000',
c_part_handle1(4) TYPE n VALUE '0001',
c_a(1)             TYPE c VALUE 'A',
c_b(1)             TYPE c VALUE 'B',
c_insert(1) TYPE c VALUE 'A',
c_update(1) TYPE c VALUE 'B',
c_delete(1) TYPE c VALUE 'D',
c_flag_update(1)   TYPE c VALUE 'U',
c_flag_delete(1)   TYPE c VALUE 'D',
c_flag_insert(1)   TYPE c VALUE 'I',
c_partner(10) TYPE c VALUE 'PARTNER',
c_ordered_prod(20) TYPE c VALUE 'ORDERED_PROD',
c_mode(4) TYPE c VALUE 'MODE',
c_ptype(20) TYPE c VALUE 'PROCESSTYPE',
c_display_type(2)  TYPE c VALUE 'BP',
c_no_type(2) TYPE c VALUE 'BP',
c_kind_of_entry(1) TYPE c VALUE 'C',
c_disp(20) TYPE c VALUE 'DISPLAY_TYPE',
c_kind(20) TYPE c VALUE 'KIND_OF_ENTRY',
c_notype(10) TYPE c VALUE 'NO_TYPE',
c_partfct(15) TYPE c VALUE 'PARTNER_FCT',
c_partno(10) TYPE c VALUE 'PARTNER_NO',
c_process_qty_unit(16)          TYPE c VALUE 'PROCESS_QTY_UNIT',
c_orderadm_h       TYPE crmt_object_name VALUE 'ORDERADM_H',
c_orderadm_i       TYPE crmt_object_name VALUE 'ORDERADM_I',
c_schedlin_i       TYPE crmt_object_name VALUE 'SCHEDLIN_I',
c_customer_h       TYPE crmt_object_name VALUE 'CUSTOMER_H',
c_customer_i       TYPE crmt_object_name VALUE 'CUSTOMER_I',
c_product_i        TYPE crmt_object_name VALUE 'PRODUCT_I',
c_orgman           TYPE crmt_object_name VALUE 'ORGMAN',
c_status           TYPE crmt_object_name VALUE 'STATUS',
c_sales            TYPE crmt_object_name VALUE 'SALES',
c_texts            TYPE crmt_object_name VALUE 'TEXTS',
c_pridoc           TYPE crmt_object_name VALUE 'PRIDOC'.


*----------------------------------------------------------------------*
* Variable Declaration
*----------------------------------------------------------------------*
DATA: lv_log_handle TYPE bapibus20001_control-log_handle,
lv_logical_key    TYPE crmt_logical_key,
lv_krech          TYPE krech,
lv_time_stamp     TYPE timestamp,
lv_tz             TYPE ttzz-tzone,
lv_cus_bpno       TYPE bu_partner,
lv_wsstring       TYPE string,
lv_ws_bpno        TYPE bu_partner,
  gv_mode           TYPE crmt_mode,
gv_header_guid_16 TYPE guid_16,
gv_item_guid_16   TYPE guid_16,
gv_guid           TYPE guid_16,
gv_guid1          TYPE guid_16.

*----------------------------------------------------------------------*
* Internal tables and work areas declaration
*----------------------------------------------------------------------*
DATA: i_orderadm_h TYPE crmt_orderadm_h_comt,
wa_orderadm_h      TYPE crmt_orderadm_h_com,

i_orderadm_i       TYPE crmt_orderadm_i_comt,
wa_orderadm_i      TYPE crmt_orderadm_i_com,

i_partner_attributes  TYPE crmt_partner_attribute_com_tab,
wa_partner_attributes TYPE crmt_partner_attribute_com,

i_product_i        TYPE crmt_product_i_comt,
wa_product_i       TYPE crmt_product_i_com,

i_customer_h       TYPE crmt_customer_h_comt,
i_customer_i       TYPE crmt_customer_i_comt,
wa_customer_h      TYPE crmt_customer_h_com,
wa_customer_i      TYPE crmt_customer_i_com,

i_sales            TYPE crmt_sales_comt,
wa_sales           TYPE crmt_sales_com,

i_partner          TYPE crmt_partner_comt,
wa_partner         TYPE crmt_partner_com,

i_partners         TYPE TABLE OF zsd_partner,
wa_partners        TYPE zsd_partner,

i_orgman           TYPE crmt_orgman_comt,
wa_orgman          TYPE crmt_orgman_com,

it_pricing         TYPE crmt_pricing_comt,
wa_pricing         TYPE crmt_pricing_com,

it_shipping        TYPE crmt_shipping_comt,
wa_shipping        TYPE crmt_shipping_com,

i_product          TYPE crmt_product_i_comt,
wa_product         TYPE crmt_product_i_com,

i_schedlin_i       TYPE crmt_schedlin_i_comt,
wa_schedlin_i      TYPE crmt_schedlin_i_com,

i_schedlin         TYPE crmt_schedlin_extdt,
wa_schedlin        TYPE crmt_schedlin_extd,

i_status           TYPE crmt_status_comt,
wa_status          TYPE crmt_status_com,

i_text             TYPE crmt_text_comt,
wa_text            TYPE crmt_text_com,

wa_input_fields    TYPE crmt_input_field,
i_input_fields     TYPE crmt_input_field_tab,

i_field_names      TYPE crmt_input_field_names_tab,
wa_field_names     TYPE crmt_input_field_names,

i_object_save      TYPE TABLE OF bapibus20001_guid_dis,
wa_object_save     TYPE bapibus20001_guid_dis,

i_saved_objects    TYPE TABLE OF bapibus20001_object_id,
wa_saved_objects   TYPE bapibus20001_object_id,

i_zatab0000y3      TYPE TABLE OF zatab0000y3,
wa_zatab0000y3     TYPE zatab0000y3,
wa_cond_add        TYPE prct_cond_external_input,

wa_pridoc_com      TYPE crmt_pridoc_com,
i_pridoc_com       TYPE crmt_pridoc_comt,

wa_cond            TYPE zsd_cond,
i_cond             TYPE TABLE OF zsd_cond,

i_order_guids      TYPE crmt_object_guid_tab,
wa_order_guids     TYPE crmt_object_guid,
LT_ATTRIBUTES_COM  TYPE CRMT_PARTNER_ATTRIBUTE_COM_TAB.

*&---------------------------------------------------------------------*
*&      Form F_CREATE_GUID
*&---------------------------------------------------------------------*
*       Create GUID
*----------------------------------------------------------------------*
FORM f_create_guid CHANGING lv_guid.
CLEAR lv_guid.
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_16 = lv_guid.
ENDFORM. " F_CREATE_GUID

*&---------------------------------------------------------------------*
*&      Form F_FIELD_NAMES
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM f_field_names USING value(p_field_name) TYPE c
value(p_changable)  TYPE c.
*Passing values in the work area of internal table I_FIELD_NAMES.
wa_field_names-fieldname  = p_field_name.
wa_field_names-changeable = p_changable.

*Inserting values from work area to internal table.
INSERT wa_field_names INTO TABLE i_field_names.
CLEAR wa_field_names.

ENDFORM. " F_FIELD_NAMES

*&---------------------------------------------------------------------*
*&      Form F_INPUT_FIELDS
*&---------------------------------------------------------------------*
*       Prepare input fields
*----------------------------------------------------------------------*
FORM f_input_fields  USING    value(p_handle)
value(p_guid)
value(p_kind)
value(p_objectname)
value(p_logical_key).
*Passing values in the work area of INPUT_FIELDS1.
wa_input_fields-ref_handle  = p_handle.
wa_input_fields-ref_guid    = p_guid.
wa_input_fields-ref_kind    = p_kind.
wa_input_fields-objectname  = p_objectname.
wa_input_fields-logical_key = p_logical_key.

*Passing Field names in the work area of INPUT_FIELDS1.
wa_input_fields-field_names = i_field_names.

*Inserting values from work area to internal table.
INSERT wa_input_fields INTO TABLE i_input_fields.
CLEAR wa_input_fields.

ENDFORM. " F_INPUT_FIELDS

*&---------------------------------------------------------------------*
*&      Form F_CREATE_ORDERADMH
*&---------------------------------------------------------------------*
*       Pass order header data
*----------------------------------------------------------------------*
FORM f_create_orderadmh  USING    value(p_handle)        TYPE n
value(p_guid)          TYPE guid_16
value(p_process_type)  TYPE c
value(p_posting_date)  TYPE d
value(p_zzafld000023)  TYPE c
value(p_mode)          TYPE c.

CLEAR wa_orderadm_h.
*Passing values of the parameters in the work area of internal table I_ORDERADM_H.
wa_orderadm_h-handle       = p_handle.
wa_orderadm_h-guid         = p_guid.
" If order create then only pass process type and posting date.
IF p_mode EQ c_a.
wa_orderadm_h-process_type = p_process_type.
wa_orderadm_h-posting_date = p_posting_date.
ENDIF.

wa_orderadm_h-mode         = p_mode.

*Inserting values from work area to internal table.
INSERT wa_orderadm_h INTO TABLE i_orderadm_h.
CLEAR  wa_orderadm_h.

CLEAR i_field_names.

*Calling Subroutine to fill the field names.
PERFORM f_field_names USING: c_mode '',
c_ptype '',
'POSTING_DATE' ''.

*Calling Subroutine to fill Input fields table.
PERFORM f_input_fields USING p_handle
p_guid
c_a
  c_orderadm_h
''.

ENDFORM. " F_CREATE_ORDERADMH

*&---------------------------------------------------------------------*
*&      Form f_create_customer_h
*&---------------------------------------------------------------------*
*       Pass custom fields to CUSTOMER_H
*----------------------------------------------------------------------*
FORM f_create_customer_h TABLES t_partners USING value(p_ref_handle)    TYPE n
  value(p_guid)          TYPE guid_16
value(p_zzafld00002g)  TYPE c
  value(p_zzafld00002h)  TYPE c
  value(p_zzafld00002k)  TYPE c
value(p_zzafld00002l)  TYPE c
  value(p_zzafld000046)  TYPE c
  value(p_mode)          TYPE c.
i_partners[] = t_partners[].


IF lv_cus_bpno IS NOT INITIAL AND lv_ws_bpno IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input  = lv_cus_bpno
IMPORTING
output = lv_cus_bpno.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input  = lv_ws_bpno
IMPORTING
output = lv_ws_bpno.


ENDIF.
CLEAR:lv_cus_bpno,
lv_ws_bpno.
*Inserting values from work area to internal table.
INSERT wa_customer_h INTO TABLE i_customer_h.
CLEAR  wa_customer_h.

CLEAR i_field_names.

*Calling Subroutine to fill Input fields table.
PERFORM f_input_fields USING p_ref_handle
p_guid
c_a
  c_customer_h
''.

ENDFORM. " F_CREATE_CUSTOMER_H
*&---------------------------------------------------------------------*
*&      Form f_create_partner
*&---------------------------------------------------------------------*
*       Pass partner data
*----------------------------------------------------------------------*
FORM f_create_partner USING value(p_guid) TYPE guid_16
value(p_handle)        TYPE n
value(p_ref_kind)      TYPE c
value(p_part_handle)   TYPE n
value(p_kind_of_entry) TYPE c
value(p_display_type)  TYPE c
value(p_no_type)       TYPE c
value(p_part_fct)      TYPE c
value(p_part_no)       TYPE c.

*Passing values of the parameters in the work area of internal table IT_PARTNER1.
wa_partner-ref_guid = p_guid.
wa_partner-ref_handle = p_handle.
wa_partner-ref_kind = p_ref_kind.
wa_partner-ref_partner_handle = p_part_handle.
wa_partner-kind_of_entry = p_kind_of_entry.
wa_partner-display_type = p_display_type.
wa_partner-no_type = p_no_type.
wa_partner-partner_fct = p_part_fct.
wa_partner-partner_no = p_part_no.

*Inserting values from work area to internal table.
INSERT wa_partner INTO TABLE i_partner.
CLEAR wa_partner.

CLEAR i_field_names.

*Calling Subroutine to fill the field names.
PERFORM f_field_names USING:
c_disp '',
c_kind '',
c_notype '',
c_partfct '',
c_partno ''.

*Calling Subroutine to fill Input fields table.
PERFORM f_input_fields  USING p_handle
p_guid
c_a
c_partner
  c_part_handle1.

ENDFORM. " F_CREATE_PARTNER

*&---------------------------------------------------------------------*
*&      Form f_create_orgman
*&---------------------------------------------------------------------*
*       Pass organizational data
*----------------------------------------------------------------------*
FORM f_create_orgman USING value(p_handle)      TYPE n
value(p_guid)        TYPE guid_16
value(p_sales_org)   TYPE c
value(p_dis_channel) TYPE c
value(p_division)    TYPE c.

wa_orgman-ref_handle = p_handle.
wa_orgman-ref_guid   = p_guid.
wa_orgman-ref_kind   = 'A'.

wa_orgman-division       = p_division.
wa_orgman-dis_channel    = p_dis_channel.
wa_orgman-sales_org      = p_sales_org.
wa_orgman-sales_org_resp = p_sales_org.

wa_orgman-division_ori = 'C'.
wa_orgman-dis_channel_ori = 'C'.
wa_orgman-sales_org_ori = 'C'.
wa_orgman-sales_orgr_ori = 'C'.

wa_orgman-mode = 'A'.

INSERT wa_orgman INTO TABLE i_orgman.
CLEAR wa_orgman.

CLEAR i_field_names.

PERFORM f_field_names USING: 'DIVISION' '',
'DIVISION_ORI' '',
'DIS_CHANNEL' '',
'DIS_CHANNEL_ORI' '',
'NO_DETERMINATION' '',
'SALES_ORG' '',
'SALES_ORG_ORI' '',
'SALES_ORG_RESP' '',
'SALES_ORGR_ORI' ''.

PERFORM f_input_fields USING p_handle
p_guid
'A'
c_orgman
''.

ENDFORM. "f_create_orgman

*&---------------------------------------------------------------------*
*&      Form f_create_orderadm_i
*&---------------------------------------------------------------------*
*       Pass material to ordered product
*----------------------------------------------------------------------*
FORM f_create_orderadm_i USING value(p_item_guid)    TYPE guid_16
value(p_handle)       TYPE n
value(p_header_guid)  TYPE guid_16
value(p_ordered_prod) TYPE c
value(p_item_no)      TYPE n
* value(p_item_categ)   TYPE c
value(p_item_mode)    TYPE c.

wa_orderadm_i-guid         = p_item_guid.
wa_orderadm_i-handle       = p_handle.
wa_orderadm_i-header       = p_header_guid.
wa_orderadm_i-ordered_prod = p_ordered_prod.
wa_orderadm_i-number_int   = p_item_no.
wa_orderadm_i-itm_language = sy-langu."'EN'.
* wa_orderadm_i-itm_type     = p_item_categ.
wa_orderadm_i-mode         = p_item_mode.

INSERT wa_orderadm_i INTO TABLE i_orderadm_i.
CLEAR wa_orderadm_i.

ENDFORM. "f_create_orderadm_i

*&---------------------------------------------------------------------*
*&      Form F_CREATE_STATUS
*&---------------------------------------------------------------------*
*       Pass order status
*----------------------------------------------------------------------*
FORM f_create_status  USING value(p_doc_type) TYPE c
value(p_order_status) TYPE c
value(p_handle)       TYPE n
value(p_guid)         TYPE guid_16.

*Passing values of the parameters in the work area of internal table I_STATUS.
wa_status-ref_handle = p_handle.
wa_status-ref_guid = p_guid.
wa_status-ref_kind = c_a.
wa_status-status = p_order_status.

CLEAR wa_status-user_stat_proc.
SELECT SINGLE user_stat_proc
FROM crmc_proc_type
INTO wa_status-user_stat_proc
WHERE process_type EQ p_doc_type.

wa_status-activate = 'X'.

CLEAR lv_logical_key.
CONCATENATE wa_status-status wa_status-user_stat_proc INTO lv_logical_key.
*Inserting values from work area to internal table.
INSERT wa_status INTO TABLE i_status.
CLEAR  wa_status.

CLEAR i_field_names.

*Calling Subroutine to fill the field names.
PERFORM f_field_names USING: 'ACTIVATE' ''.

*Calling Subroutine to fill Input fields table.
PERFORM f_input_fields USING p_handle
p_guid
c_a
c_status
lv_logical_key.

ENDFORM. " F_CREATE_STATUS

*&---------------------------------------------------------------------*
*&      Form f_create_schedlin_i
*&---------------------------------------------------------------------*
*       Pass quantity into schedule line
*----------------------------------------------------------------------*
FORM f_create_schedlin_i USING value(p_guid) TYPE guid_16
value(p_mode) TYPE c
value(p_qty) TYPE p.

wa_schedlin-quantity = p_qty.
wa_schedlin-mode     = p_mode.
INSERT wa_schedlin INTO TABLE i_schedlin.
CLEAR wa_schedlin.

wa_schedlin_i-ref_guid   = p_guid.
wa_schedlin_i-schedlines = i_schedlin.

CLEAR i_schedlin.
INSERT wa_schedlin_i INTO TABLE i_schedlin_i.
CLEAR wa_schedlin_i.

ENDFORM. "f_create_schedlin_i

*&---------------------------------------------------------------------*
*&      Form f_create_orderadm_i
*&---------------------------------------------------------------------*
*       Pass material to ordered product
*----------------------------------------------------------------------*
FORM f_create_product_i USING value(p_item_guid)    TYPE guid_16
value(p_handle) TYPE n
value(p_sales_unit)   TYPE c.

wa_product_i-ref_guid = p_item_guid.
wa_product_i-ref_handle = p_handle.
wa_product_i-process_qty_unit = p_sales_unit.

INSERT wa_product_i INTO TABLE i_product_i.
CLEAR wa_product_i.

ENDFORM. "f_create_orderadm_i
*&---------------------------------------------------------------------*
*&      Form f_create_text
*&---------------------------------------------------------------------*
*       Pass header notes
*----------------------------------------------------------------------*
FORM f_create_text TABLES t_line USING value(p_handle)    TYPE n
value(p_guid) TYPE guid_16
value(p_lang)      TYPE c
value(p_form_name) TYPE c
value(p_text_id)   TYPE c
value(p_mode)      TYPE c.

wa_text-ref_handle = p_handle.
wa_text-ref_guid   = p_guid.
wa_text-ref_kind   = 'A'.
wa_text-mode       = p_mode.

wa_text-tdspras    = p_lang.
wa_text-tdform     = 'SYSTEM'.
wa_text-tdid       = p_text_id.
wa_text-lines[]    = t_line[].

INSERT wa_text INTO TABLE i_text.
CLEAR wa_text.

CLEAR i_field_names.

PERFORM f_field_names USING: 'LINES' '',
'MODE' ''.

lv_logical_key = p_guid.
CONCATENATE p_form_name lv_logical_key INTO lv_logical_key.

PERFORM f_input_fields USING p_handle
p_guid
'A'
c_texts
  lv_logical_key.

ENDFORM. "f_create_text

*&---------------------------------------------------------------------*
*&      Form f_create_condition
*&---------------------------------------------------------------------*
*       Pass manual discounts as Conditions at item level
*----------------------------------------------------------------------*
FORM f_create_condition TABLES t_cond USING value(p_mob_itm_no)    TYPE n
  value(p_handle)        TYPE n
  value(p_guid)          TYPE guid_16
  value(p_quantity)      TYPE i.

i_cond[] = t_cond[].
CLEAR: wa_pridoc_com.

wa_pridoc_com-ref_handle   = p_handle.
wa_pridoc_com-ref_guid     = p_guid.
wa_pridoc_com-ref_kind     = 'A'.
wa_pridoc_com-pricing_type = 'B'.

LOOP AT i_cond INTO wa_cond WHERE mob_itm_number EQ p_mob_itm_no.
CLEAR wa_cond_add.
wa_cond_add-kschl = wa_cond-disc_cond_type.

" Get the calculation type for disc_cond_type of item.
SELECT SINGLE krech
INTO lv_krech
FROM prcc_cond_ct
WHERE kappl EQ 'CRM'
AND kschl EQ wa_cond_add-kschl.

IF lv_krech EQ 'A'.      "If calc type is Percentage
wa_cond_add-kbetr = wa_cond-rate.
ELSEIF lv_krech EQ 'B'.  " If calc type is Fixed Amount
wa_cond_add-kbetr = wa_cond-rate * p_quantity.
wa_cond_add-waers = wa_cond-currency.
ENDIF.

INSERT wa_cond_add INTO TABLE wa_pridoc_com-cond_add.

ENDLOOP.

INSERT wa_pridoc_com INTO TABLE i_pridoc_com.

CLEAR i_field_names.

PERFORM f_field_names USING: 'KSCHL' '',
'WAERS' '',
'KBETR' '',
'KWERT' ''.

PERFORM f_input_fields USING p_handle
p_guid
'B'
c_pridoc
''.

ENDFORM.                    "f_create_condition

*&---------------------------------------------------------------------*
*&      Form F_CREATE_SALES
*&---------------------------------------------------------------------*
*       Pass Requested delivery date
*----------------------------------------------------------------------*
FORM f_create_sales USING value(p_po_number)     TYPE c
value(p_req_date)      TYPE d
VALUE(p_overallrej)    type c
value(p_handle) TYPE n
value(p_guid)          TYPE guid_16.

CONVERT DATE p_req_date INTO TIME STAMP lv_time_stamp TIME ZONE lv_tz.
wa_sales-ref_guid       = p_guid.
wa_sales-ref_kind       = 'B'.
wa_sales-po_number_sold = p_po_number.
wa_sales-req_dlv_date   = lv_time_stamp.
IF p_OVERALLREJ eq 'C'.
wa_sales-rejection = '10'.
ENDIF.
INSERT wa_sales INTO TABLE i_sales.
CLEAR i_field_names.
PERFORM f_field_names USING: 'PO_NUMBER_SOLD' '',
'REQ_DLV_DATE' '',
'REJECTION' ''.
PERFORM f_input_fields USING p_handle
p_guid
'B'
c_sales
''.

ENDFORM. " F_CREATE_SALES

*&---------------------------------------------------------------------*
*&      Form f_order_maintain
*&---------------------------------------------------------------------*
*       Call CRM_ORDER_MAINTAIN
*----------------------------------------------------------------------*
FORM f_order_maintain .

*calling function module to create standard order.

CALL FUNCTION 'CRM_ORDER_MAINTAIN'
EXPORTING
it_sales              = i_sales
it_orgman             = i_orgman
it_product_i          = i_product_i
it_customer_h         = i_customer_h
it_customer_i         = i_customer_i
it_text               = i_text
it_schedlin_i         = i_schedlin_i
it_partner            = i_partner
it_pridoc             = i_pridoc_com
it_status             = i_status
CHANGING
ct_orderadm_h         = i_orderadm_h
ct_orderadm_i         = i_orderadm_i
ct_input_fields       = i_input_fields
ct_partner_attributes = i_partner_attributes
*      CT_PARTNER_ATTRIBUTES = LT_ATTRIBUTES_COM
EXCEPTIONS
error_occurred        = 1
document_locked       = 2
no_change_allowed     = 3
no_authority          = 4
OTHERS                = 5.

*If function module is called successfully.
IF sy-subrc EQ 0.
*---- get created/updated orders
REFRESH i_order_guids.
LOOP AT i_orderadm_h INTO wa_orderadm_h WHERE NOT guid IS INITIAL.
INSERT wa_orderadm_h-guid INTO TABLE i_order_guids.
ENDLOOP.
ENDIF.

ENDFORM. "f_order_maintain

*&---------------------------------------------------------------------*
*&      Form f_order_save
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM f_order_save TABLES return STRUCTURE bapiret2
USING    value(p_is_header) TYPE zsd_header
CHANGING value(p_es_header) TYPE zsd_header.

" Passing value of GUID to work area of internal table I_OBJECT_SAVE.
REFRESH: i_object_save,
i_saved_objects.

CLEAR lv_log_handle.

LOOP AT i_order_guids INTO wa_order_guids.
CLEAR wa_object_save.
wa_object_save-guid = wa_order_guids.
wa_object_save-object_type = 'BUS2000115'.
INSERT wa_object_save INTO TABLE i_object_save.
ENDLOOP.

*Calling Function module to save standard order.
CALL FUNCTION 'BAPI_BUSPROCESSND_SAVE'
IMPORTING
log_handle      = lv_log_handle
TABLES
objects_to_save = i_object_save
saved_objects   = i_saved_objects
return          = return.

*---- get log
PERFORM get_appl_log TABLES return
USING lv_log_handle '3' i_order_guids.
CLEAR p_es_header.
p_es_header = p_is_header.

READ TABLE i_saved_objects INTO wa_saved_objects INDEX 1.
IF sy-subrc EQ 0.
p_es_header-doc_number  = wa_saved_objects-object_id.
p_es_header-doc_guid    = wa_saved_objects-guid.
ENDIF.

IF p_es_header-doc_number IS NOT INITIAL.
" Convert error messages to warnings as Order is saved
CALL FUNCTION '/MAC/RSFM_CRM_MSG_ERR_TO_WRNIN'
TABLES
return = return.
" If Order is created then Commit
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
RETURN.
ENDIF.

SORT return BY type id number message.
DELETE ADJACENT DUPLICATES FROM return COMPARING type id number message.

ENDFORM. "f_order_save


*&---------------------------------------------------------------------*
*&      Form get_appl_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_appl_log
TABLES   ct_return      STRUCTURE bapiret2
USING    iv_loghandle   TYPE balloghndl
iv_level       TYPE crmt_msglevel
it_order_guids TYPE crmt_object_guid_tab.

DATA: ls_return     LIKE LINE OF ct_return,
ls_order_guid LIKE LINE OF it_order_guids,
*       // Messages handles
lt_log_handle TYPE bal_t_logh,
ls_log_filter TYPE bal_s_lfil,
ls_extnumber  LIKE LINE OF ls_log_filter-extnumber,
lt_msg_handle TYPE bal_t_msgh,
ls_msg_handle LIKE LINE OF lt_msg_handle,
lv_msgnumber  TYPE crmt_msgnr_at_register,
lv_lognumber  TYPE balognr,
*       // Messages
ls_bal_message TYPE bal_s_msg,
ls_crm_message TYPE crmt_msg_info,
*       // Message level
ls_msg_filter TYPE bal_s_mfil,
lr_level      TYPE bal_s_dlev.

*---- get frame log
IF NOT iv_loghandle IS INITIAL.
CALL FUNCTION 'CRM_MESSAGES_GET_LOG_INFO'
EXPORTING
iv_log_handle        = iv_loghandle
IMPORTING
ev_msgnr_at_register = lv_msgnumber
EXCEPTIONS
OTHERS               = 0.
APPEND iv_loghandle TO lt_log_handle.
CALL FUNCTION 'BAL_GLB_SEARCH_MSG'
EXPORTING
i_t_log_handle = lt_log_handle
IMPORTING
e_t_msg_handle = lt_msg_handle
EXCEPTIONS
msg_not_found  = 0
OTHERS         = 0.
LOOP AT lt_msg_handle INTO ls_msg_handle
WHERE msgnumber GT lv_msgnumber.
CALL FUNCTION 'BAL_LOG_HDR_READ'
EXPORTING
i_log_handle  = ls_msg_handle-log_handle
IMPORTING
e_lognumber   = lv_lognumber
EXCEPTIONS
log_not_found = 1
OTHERS        = 2.
IF sy-subrc NE 0.
CLEAR lv_lognumber.
ENDIF.
CALL FUNCTION 'CRM_MESSAGES_GET_MSG_INFO'
EXPORTING
is_msg_handle           = ls_msg_handle
IMPORTING
es_info                 = ls_crm_message
es_msg                  = ls_bal_message
EXCEPTIONS
not_found               = 1
wrong_context_structure = 2
OTHERS = 3.
IF sy-subrc EQ 0.
PERFORM set_bapireturn2 TABLES ct_return
USING ls_bal_message-msgty ls_bal_message-msgid
ls_bal_message-msgno
ls_bal_message-msgv1 ls_bal_message-msgv2
ls_bal_message-msgv3 ls_bal_message-msgv4
lv_lognumber ls_crm_message-msgnumber
ls_crm_message-object_name space
ls_crm_message-fieldname.
ENDIF.
ENDLOOP.
CLEAR lt_msg_handle.
ENDIF.
* // Filter for the messages, according to user level
CHECK NOT iv_level IS INITIAL.
*---- get application log
ls_extnumber-sign   = 'I'.
ls_extnumber-option = 'EQ'.
LOOP AT it_order_guids INTO ls_order_guid.
ls_extnumber-low    = ls_order_guid.
APPEND ls_extnumber TO ls_log_filter-extnumber.
ENDLOOP.
* // Check it
CALL FUNCTION 'CRM_ORDER_VALUE_RANGE_CHECK'
EXPORTING
iv_value                 = iv_level
iv_domain_name           = 'CRM_MSGLEVEL'
EXCEPTIONS
value_not_in_value_range = 1
OTHERS                   = 2.
IF sy-subrc NE 0.
PERFORM set_bapireturn2 TABLES ct_return
USING syst-msgty syst-msgid syst-msgno
syst-msgv1 syst-msgv2
syst-msgv3 syst-msgv4
space 0 space space space.
EXIT.
ENDIF.

lr_level-sign   = 'I'.
lr_level-option = 'LE'.
lr_level-low    = iv_level.
APPEND lr_level TO ls_msg_filter-detlevel.

CALL FUNCTION 'BAL_GLB_SEARCH_MSG'
EXPORTING
i_s_log_filter = ls_log_filter
i_s_msg_filter = ls_msg_filter
IMPORTING
e_t_msg_handle = lt_msg_handle
EXCEPTIONS
msg_not_found  = 0
OTHERS         = 0.
LOOP AT lt_msg_handle INTO ls_msg_handle.
CALL FUNCTION 'BAL_LOG_HDR_READ'
EXPORTING
i_log_handle  = ls_msg_handle-log_handle
IMPORTING
e_lognumber   = lv_lognumber
EXCEPTIONS
log_not_found = 1
OTHERS        = 2.
IF sy-subrc NE 0.
CLEAR lv_lognumber.
ENDIF.
CALL FUNCTION 'CRM_MESSAGES_GET_MSG_INFO'
EXPORTING
is_msg_handle           = ls_msg_handle
IMPORTING
es_info                 = ls_crm_message
es_msg                  = ls_bal_message
EXCEPTIONS
not_found               = 1
wrong_context_structure = 2
OTHERS = 3.
IF sy-subrc EQ 0.
PERFORM set_bapireturn2 TABLES ct_return
USING ls_bal_message-msgty ls_bal_message-msgid
ls_bal_message-msgno
ls_bal_message-msgv1 ls_bal_message-msgv2
ls_bal_message-msgv3 ls_bal_message-msgv4
lv_lognumber  ls_crm_message-msgnumber
ls_crm_message-object_name space
ls_crm_message-fieldname.
ENDIF.
ENDLOOP.

ENDFORM.                               " GET_APPL_LOG


*&---------------------------------------------------------------------*
*&      Form set_bapireturn2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM set_bapireturn2 TABLES ct_return STRUCTURE bapiret2
USING value(iv_msgty) TYPE bapireturn-type
value(iv_msgid) TYPE syst-msgid
value(iv_msgno) "// No typed parameter!
value(iv_msgv1)
value(iv_msgv2)
value(iv_msgv3)
value(iv_msgv4)
value(iv_logid) LIKE bapireturn-log_no
value(iv_logno) TYPE bapireturn-log_msg_no
value(iv_param)
value(iv_row)   TYPE bapiret2-row
value(iv_field).
STATICS: BEGIN OF ls_msgvx,
msgno TYPE syst-msgno,
msgv1 TYPE msgv1,
msgv2 TYPE msgv2,
msgv3 TYPE msgv3,
msgv4 TYPE msgv4,
END OF ls_msgvx,
lv_param TYPE bapiret2-parameter,
lv_field TYPE bapiret2-field.

CLEAR ct_return.
ls_msgvx-msgno = iv_msgno.
ls_msgvx-msgv1 = iv_msgv1. ls_msgvx-msgv2 = iv_msgv2.  "#EC DOM_EQUAL
ls_msgvx-msgv3 = iv_msgv3. ls_msgvx-msgv4 = iv_msgv4.
lv_param = iv_param.
lv_field = iv_field.
CALL FUNCTION 'BALW_BAPIRETURN_GET2'
EXPORTING
type       = iv_msgty
cl         = iv_msgid
number     = ls_msgvx-msgno
par1       = ls_msgvx-msgv1                        "#EC DOM_EQUAL
par2       = ls_msgvx-msgv2                        "#EC DOM_EQUAL
par3       = ls_msgvx-msgv3                        "#EC DOM_EQUAL
par4       = ls_msgvx-msgv4                        "#EC DOM_EQUAL
log_no     = iv_logid
log_msg_no = iv_logno
parameter  = lv_param
row        = iv_row
field      = lv_field
IMPORTING
return     = ct_return.
APPEND ct_return.

ENDFORM. " SET_BAPIRETURN2
*&---------------------------------------------------------------------*
*&      Form F_CREATE_CUSTOMER_I
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LV_HANDLE  text
*      -->P_GV_GUID  text
*----------------------------------------------------------------------*
FORM f_create_customer_i  USING   value(p_ref_handle)    TYPE n
value(p_sorg) TYPE c
value(p_dist_ch) TYPE c
value(p_prod) TYPE c
value(p_guid)          TYPE guid_16
value(p_mode)          TYPE c.
DATA : l_product_guid LIKE comm_product-product_guid,

l_ws_prod_code  TYPE  matnr_ku,
l_matnr TYPE matnr,
l_prod TYPE comm_product-product_id.
CLEAR:wa_customer_i,
l_prod.
wa_customer_i-ref_handle   = '0000000001'.
" p_ref_handle.
wa_customer_i-ref_guid     = p_guid.
wa_customer_i-mode         = p_mode.

* LOOP AT i_orderadm_i INTO wa_orderadm_i .
CALL FUNCTION 'CONVERSION_EXIT_PRID1_INPUT'
EXPORTING
input             = p_prod
IMPORTING
output            = l_prod
EXCEPTIONS
lenght_error      = 1
customizing_error = 2
OTHERS            = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CLEAR:l_product_guid.
SELECT SINGLE product_guid INTO l_product_guid FROM  comm_product
WHERE  product_id  = l_prod.
.
CLEAR:wa_partners,
lv_ws_bpno,
l_matnr.

CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input        = p_prod
IMPORTING
output       = l_matnr
EXCEPTIONS
length_error = 1
OTHERS       = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CLEAR:l_ws_prod_code .

INSERT wa_customer_i INTO TABLE i_customer_i.
* ENDLOOP.



*Calling Subroutine to fill Input fields table.
PERFORM f_input_fields USING p_ref_handle
p_guid
c_b
  c_customer_i
''.


ENDFORM. " F_CREATE_CUSTOMER_I

********Above documents was co authored by my fellow colleague Karthi Dhayalan. *********

2 Comments
Labels in this area
Top kudoed authors