Skip to end of metadata
Go to start of metadata

List of all pages

Pages at first level

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*&---------------------------------------------------------------------*
*& Report ZAXXX_MASTERDATA_CREATE
*&
************************************************************************
* PROGRAM--INITIAL CREATION
************************************************************************
* PROGRAM--Modification*MOD-1771404
* Program name : ZAXXX_MASTERDATA_CREATE'
* Reference program (copied from) : na.
* Development specification ID :
* Author : Aveek Ghose
* Description :
*
* This report is run in the foreground as also in the batch allowing
* user to maintain the master data in the SIGN system. The master data
* include Product, Location, Location-Product and Transportation lanes
************************************************************************
REPORT zaxxx_masterdata_create MESSAGE-ID zfpv.
* include zaxxpr_maintain_masterdt_top.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_MAINTAIN_MASTERDT_TOP
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_MAINTAIN_MASTERDATA_TOP
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--Modification*MOD-1771404
* Program name : ZAXXX_MASTERDATA_CREATE'
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2006
* Author : Aveek Ghose
* Description :
*
* This report is run in the foreground as also in the batch allowing
* user to maintain the master data in the SIGN system. The master data
* include Product, Location, Location-Product and Transportation lanes
************************************************************************
************************************************************************
* PROGRAM--INITIAL CREATION
* This is the Top include that contains the global data declarations
* and the selection screen of the main program
* ZAXXPR_MAINTAIN_MASTERDATA
************************************************************************
*********************TYPES DECLARATION****************************
************************************************************************
TYPES:
BEGIN OF s_source,
  prvar TYPE char18,
  vartx TYPE char30,
  pcode TYPE matnr,
  maktx TYPE maktx,
  markt TYPE char4,
  martx TYPE char20,
  pdctr TYPE char2,
  pctxt TYPE char20,
END OF s_source,
t_source TYPE STANDARD TABLE OF s_source,
BEGIN OF s_markt,
  markt TYPE char4,
  martx TYPE char20,
END OF s_markt,
t_markt TYPE STANDARD TABLE OF s_markt,
BEGIN OF s_pdctr,
  pdctr TYPE char2,
  pctxt TYPE char20,
END OF s_pdctr,
t_pdctr TYPE STANDARD TABLE OF s_pdctr,
BEGIN OF s_produ,
  pcode TYPE matnr,
  vartx TYPE maktx,
END OF s_produ,
t_produ TYPE STANDARD TABLE OF s_produ,
BEGIN OF s_location,
  locno TYPE /sapapo/locno,
  loctp TYPE /sapapo/c_loctype,
  locct TYPE char1,
END OF s_location,
t_location TYPE STANDARD TABLE OF s_location,
BEGIN OF s_product,
  matnr TYPE /sapapo/matnr,
  matct TYPE char1,
END OF s_product,
t_product TYPE STANDARD TABLE OF s_product,
BEGIN OF s_locprd,
  locno TYPE /sapapo/locno,
  matnr TYPE /sapapo/matnr,
  loctp TYPE /sapapo/c_loctype,
  prctp TYPE /sapapo/beskz,
END OF s_locprd,
t_locprd TYPE STANDARD TABLE OF s_locprd,
BEGIN OF s_tlane,
  locfr TYPE /sapapo/locno,
  locto TYPE /sapapo/locno,
  matnr TYPE /sapapo/matnr,
  ltpfr TYPE /sapapo/c_loctype,
  ltpto TYPE /sapapo/c_loctype,
  lidfr TYPE /sapapo/locid,
  lidto TYPE /sapapo/locid,
  matid TYPE /sapapo/matid,
  datfr TYPE timestamp,
  datto TYPE timestamp,
  crtyp TYPE flag,
END OF s_tlane,
t_tlane TYPE STANDARD TABLE OF s_tlane,
BEGIN OF s_loc_extloc,
  locno TYPE /sapapo/locno,
  extloc TYPE /sapapo/loc_extlocno,
END OF s_loc_extloc,
t_loc_extloc TYPE STANDARD TABLE OF s_loc_extloc,
BEGIN OF s_prd_extprd,
  matnr TYPE /sapapo/matnr,
  extmat TYPE /sapapo/ext_matnr,
END OF s_prd_extprd,
t_prd_extprd TYPE STANDARD TABLE OF s_prd_extprd,
t_log_table TYPE STANDARD TABLE OF bal_s_msg,
t_log_location TYPE STANDARD TABLE OF bal_s_msg,
t_log_product TYPE STANDARD TABLE OF bal_s_msg,
t_log_locprd TYPE STANDARD TABLE OF bal_s_msg,
t_log_tlane TYPE STANDARD TABLE OF bal_s_msg,
t_masterdata TYPE STANDARD TABLE OF zap_sign,
t_zdu1010 TYPE STANDARD TABLE OF zdu1010, "Market table
* t_zdu1000 TYPE STANDARD TABLE OF zdu1000, "Source table
t_zapwhse TYPE STANDARD TABLE OF zap_whse,"Whse table
t_zdmpcg1 TYPE STANDARD TABLE OF zdmpcg1, "Core prd tab
t_zdm0705 TYPE STANDARD TABLE OF zdm0705, "Variant tab
t_zap0702 TYPE STANDARD TABLE OF zap0702,
t_return TYPE STANDARD TABLE OF bapiret2,
* Tables for Location BAPI
t_location_head TYPE STANDARD TABLE OF bapi10002loc2,
t_location_head_x TYPE STANDARD TABLE OF bapi10002loc2x,
t_location_text TYPE STANDARD TABLE OF bapi10002loctext2,
t_location_text_x TYPE STANDARD TABLE OF bapi10002loctext2x,
t_location_address TYPE STANDARD TABLE OF bapi10002locaddr2,
t_location_address_x TYPE STANDARD TABLE OF bapi10002locaddr2x,
t_location_model TYPE STANDARD TABLE OF bapi10002locmodel2,
* Tables for Product BAPI
t_product_head TYPE STANDARD TABLE OF bapi10001prd2,
t_product_head_x TYPE STANDARD TABLE OF bapi10001prd2x,
t_product_text TYPE STANDARD TABLE OF bapi10001prdtext2,
t_product_text_x TYPE STANDARD TABLE OF bapi10001prdtext2x,
* Tables for Location-Product BAPI
t_product_location TYPE STANDARD TABLE OF bapi10001locprd2,
t_product_location_x TYPE STANDARD TABLE OF bapi10001locprd2x,
t_product_location_version TYPE STANDARD TABLE OF bapi10001locprdvers2,
t_product_location_version_x TYPE STANDARD TABLE OF bapi10001locprdvers2x,
t_product_model TYPE STANDARD TABLE OF bapi10001locprdmodel2,
* Tables for Transportation lane BAPI
t_transport_lane TYPE STANDARD TABLE OF bapi11201trlane,
t_transport_lanex TYPE STANDARD TABLE OF bapi11201trlanex,
t_prod_procurement TYPE STANDARD TABLE OF bapi11201sources,
t_prod_procurementx TYPE STANDARD TABLE OF bapi11201sourcesx,
t_means_of_transport TYPE STANDARD TABLE OF bapi11201meansoftransp,
t_means_of_transportx TYPE STANDARD TABLE OF bapi11201meansoftranspx,
* BDC table
t_bdcdata TYPE STANDARD TABLE OF bdcdata,
t_messages TYPE STANDARD TABLE OF bdcmsgcoll.
************************************************************************
*****************CONSTANTS DECLARATION****************************
************************************************************************
CONSTANTS:
gc_s TYPE ddbool_d VALUE 'S',
gc_e TYPE bapi10002commctrl VALUE 'E',
gc_core TYPE char1 VALUE 'C',
gc_var TYPE char1 VALUE 'V',
gc_x TYPE char1 VALUE 'X',
gc_markt TYPE char1 VALUE 'M',
gc_pdctr TYPE char1 VALUE 'F',
gc_whse TYPE char1 VALUE 'W',
gc_utc TYPE timezone VALUE 'UTC', "UTC time zome
gc_uom_th TYPE /sapapo/meins VALUE 'TH', "Def Base uom
gc_en TYPE laiso VALUE 'EN', "Def ISO langu
gc_000 TYPE /sapapo/c_modelid VALUE '000', "SCM model
* Location types
gc_ltp_1001 TYPE /sapapo/c_loctype VALUE '1001', "For Prd centre
gc_ltp_1002 TYPE /sapapo/c_loctype VALUE '1002', "For Mkt/whouse
* Procurement types
gc_ptp_x TYPE /sapapo/beskz VALUE 'X', "For Prd centre
gc_ptp_f TYPE /sapapo/beskz VALUE 'F', "For Mkt/whouse
** Variant status
gc_stat_p TYPE char1 VALUE 'P',
gc_stat_a TYPE char1 VALUE 'A',
* Timestamps
gc_startti TYPE syuzeit VALUE '000000',
gc_endti TYPE syuzeit VALUE '235959',
* Default means of transport
gc_ttype_0001 TYPE /sapapo/tr_traty VALUE '0001',
* Business system grp
gc_bsgapo TYPE /sapapo/loc_logqs VALUE 'BSGAPO',
* Message class name
gc_msgid TYPE symsgid VALUE 'ZAXXP_SIGN_FPV',
* Message types
gc_warn TYPE symsgty VALUE 'W',
gc_err TYPE symsgty VALUE 'E',
gc_succ TYPE symsgty VALUE 'S',
* Log object/subobjects
gc_logobject TYPE balobj_d VALUE 'ZAP_MDATA',
gc_logsubobjl TYPE balsubobj VALUE
'ZAP_LOCATION',
gc_logsubobjp TYPE balsubobj VALUE
'ZAP_PRODUCT',
gc_logsubobjlp TYPE balsubobj VALUE
'ZAP_LOCPRD',
gc_logsubobjtl TYPE balsubobj VALUE
'ZAP_TLANE',
* Message nos
gc_msgno_021 TYPE symsgno VALUE '021',
gc_msgno_023 TYPE symsgno VALUE '023',
gc_msgno_024 TYPE symsgno VALUE '024',
gc_msgno_025 TYPE symsgno VALUE '025',
gc_msgno_026 TYPE symsgno VALUE '026',
gc_msgno_027 TYPE symsgno VALUE '027',
gc_msgno_028 TYPE symsgno VALUE '028',
gc_msgno_029 TYPE symsgno VALUE '029',
gc_msgno_030 TYPE symsgno VALUE '030',
gc_msgno_031 TYPE symsgno VALUE '031',
gc_msgno_032 TYPE symsgno VALUE '032',
gc_msgno_033 TYPE symsgno VALUE '033',
gc_msgno_034 TYPE symsgno VALUE '034',
gc_msgno_035 TYPE symsgno VALUE '035',
gc_msgno_037 TYPE symsgno VALUE '037',
gc_msgno_038 TYPE symsgno VALUE '038',
gc_msgno_039 TYPE symsgno VALUE '039',
gc_msgno_040 TYPE symsgno VALUE '040',
gc_msgno_041 TYPE symsgno VALUE '041',
gc_msgno_042 TYPE symsgno VALUE '042',
gc_msgno_043 TYPE symsgno VALUE '043',
gc_msgno_044 TYPE symsgno VALUE '044',
gc_msgno_050 TYPE symsgno VALUE '050',
gc_msgno_051 TYPE symsgno VALUE '051',
gc_msgno_052 TYPE symsgno VALUE '052',
gc_msgno_053 TYPE symsgno VALUE '053',
gc_msgno_054 TYPE symsgno VALUE '054',
gc_msgno_055 TYPE symsgno VALUE '055',
gc_msgno_061 TYPE symsgno VALUE '061',
gc_msgno_075 TYPE symsgno VALUE '075',
gc_msgno_076 TYPE symsgno VALUE '076',
gc_msgno_078 TYPE symsgno VALUE '078',
gc_msgno_096 TYPE symsgno VALUE '096',
* Screen fields
gc_p_parloc TYPE dynfnam VALUE 'P_PARLOC',
gc_dynfld_prdl TYPE dynfnam VALUE 'S_PRD-LOW',
gc_dynfld_prdh TYPE dynfnam VALUE 'S_PRD-HIGH',
gc_dynfld_mktl TYPE dynfnam VALUE 'S_MKT-LOW',
gc_dynfld_mkth TYPE dynfnam VALUE 'S_MKT-HIGH',
gc_dynfld_pctl TYPE dynfnam VALUE 'S_PCT-LOW',
gc_dynfld_pcth TYPE dynfnam VALUE 'S_PCT-HIGH',
* Hierarchy update parameters
gc_lochier TYPE /sapapo/relid VALUE 'RELH_LOKATION',
gc_prdhier TYPE /sapapo/relid VALUE 'RELH_PRODUKT',
gc_lvlidp TYPE /sapapo/rellevid VALUE '101',
gc_lvlidc TYPE /sapapo/rellevid VALUE '102',
* Call Transaction parameters
gc_dismode_n TYPE ctu_mode VALUE 'N',
gc_update_s TYPE ctu_update VALUE 'S'.
************************************************************************
*********************DATA DECLARATION*****************************
************************************************************************
DATA:
gv_logsys TYPE logsys,
gv_prd TYPE matnr,
gv_mkt TYPE char4,
gv_pct TYPE char2.
DATA:
gt_masterdata TYPE t_masterdata,
gt_source TYPE t_source,
gt_produ TYPE t_produ,
gt_pdctr TYPE t_pdctr,
gt_markt TYPE t_markt,
gt_zdmpcg1 TYPE t_zdmpcg1,
gt_zdm0705 TYPE t_zdm0705,
gt_zap0702 TYPE t_zap0702,
gt_zdu1010 TYPE t_zdu1010,
* gt_zdu1000 TYPE t_zdu1000,
gt_zapwhse TYPE t_zapwhse,
gt_log_handle TYPE bal_t_logh,
gt_log_location TYPE t_log_location,
gt_log_product TYPE t_log_product,
gt_log_locprd TYPE t_log_locprd,
gt_log_tlane TYPE t_log_tlane,
gt_bdcdata TYPE t_bdcdata.
************************************************************************
****************SELECTION SCREEN DEFINITION***********************
************************************************************************
* Block to enter the sourcing information details
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS:
s_prd FOR gv_prd, " Core Product/variant
s_mkt FOR gv_mkt, " Market
s_pct FOR gv_pct. " Production centre
SELECTION-SCREEN: END OF BLOCK b1.
* Block to enter the master data to be maintained
SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
* Create location
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (30) text-003 FOR FIELD p_crloc.
PARAMETERS: p_crloc AS CHECKBOX USER-COMMAND ucom DEFAULT 'X'.
SELECTION-SCREEN: END OF LINE.
* Option to maintain Parent node for hierarchy
SELECTION-SCREEN: COMMENT 5(20) text-004.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 10(20) text-005 FOR FIELD p_parloc.
PARAMETERS: p_parloc TYPE /sapapo/locno OBLIGATORY
DEFAULT 'NETWORK'.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: SKIP.
* Create Product.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (30) text-006 FOR FIELD p_crprd.
PARAMETERS: p_crprd AS CHECKBOX USER-COMMAND ucom DEFAULT 'X'.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: SKIP.
* Create Location-Product
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (30) text-007 FOR FIELD p_crlp.
PARAMETERS: p_crlp AS CHECKBOX USER-COMMAND ucom DEFAULT 'X'.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: SKIP.
* Create Transportation Lane
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT (30) text-008 FOR FIELD p_crtl.
PARAMETERS: p_crtl AS CHECKBOX USER-COMMAND ucom DEFAULT 'X'.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: END OF BLOCK b2.
***********************************************************************
***********************INITIALIZATION****************************
***********************************************************************
INITIALIZATION.
  REFRESH gt_masterdata.
  PERFORM f_adj_screen_flds.
  PERFORM f_get_f4_masterdata CHANGING gt_source
                                        gt_produ
                                        gt_markt
                                        gt_pdctr.
************************************************************************
****************SELECTION SCREEN EVENTS***************************
************************************************************************
AT SELECTION-SCREEN ON s_prd.
* Validates product against the SIGN view
  PERFORM f_validate_product.

AT SELECTION-SCREEN ON s_mkt.
* Validate market against the SIGN view
  PERFORM f_validate_market.

AT SELECTION-SCREEN ON s_pct.
* Validate factory against the SIGN view
  PERFORM f_validate_prodcentre.
* Custom search help for product.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-low.
  PERFORM f_f4_for_product USING gc_dynfld_prdl
  gt_produ.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-high.
  PERFORM f_f4_for_product USING gc_dynfld_prdh
  gt_produ.
* Custom search help for market
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-low.
  PERFORM f_f4_for_market USING gc_dynfld_mktl
  gt_markt.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-high.
  PERFORM f_f4_for_market USING gc_dynfld_mkth
  gt_markt.
* Custom search help for Production centre
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_pct-low.
  PERFORM f_f4_for_prodcentre USING gc_dynfld_pctl
  gt_pdctr.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_pct-high.
  PERFORM f_f4_for_prodcentre USING gc_dynfld_pcth
  gt_pdctr.

AT SELECTION-SCREEN OUTPUT.
  PERFORM f_adj_screen_flds.
************************************************************************
*********************START OF SELECTION***************************
************************************************************************
START-OF-SELECTION.
* Check that atleast one of the 4 master data creation options is ticked
  IF p_crloc IS INITIAL AND p_crprd IS INITIAL AND
  p_crlp IS INITIAL AND p_crtl IS INITIAL.
* Atleast select one of the 4 options for master data creation.
    MESSAGE i008(zfpv).
    LEAVE LIST-PROCESSING.
  ENDIF.
  CLEAR gv_logsys.
* Get the name of the logical system
  PERFORM f_get_logical_system CHANGING gv_logsys.
* REFRESH: gt_masterdata.
  REFRESH: gt_log_location, gt_log_product, gt_log_locprd, gt_log_tlane.
  REFRESH: gt_log_handle.
* Retrieve the master data from teh SIGN portfolio meeting the user
* selection
  PERFORM f_retrieve_masterdata CHANGING gt_masterdata.
* Get the master data details of the product and variant
* MASTER DATA CREATION/MAINTENANCE
  IF p_crloc IS NOT INITIAL..
    REFRESH: gt_zdu1010,
* gt_zdu1000,
             gt_zapwhse.
* Get the master data details for the market, source and warehouse
    PERFORM f_get_location_details USING gt_masterdata
                                   CHANGING gt_zdu1010
* gt_zdu1000
                                            gt_zapwhse.
* Create the master data for Location.
    PERFORM f_create_location USING gv_logsys
                                    gt_masterdata
                                    gt_zdu1010
* gt_zdu1000
gt_zapwhse.
* Update the location log in the data base
    PERFORM f_update_log_in_db  USING gc_logobject
                                      gc_logsubobjl
                                      gt_log_location
                                CHANGING gt_log_handle.
  ENDIF.
  IF p_crprd IS NOT INITIAL.
* Get the master data details of the product and variant
    REFRESH: gt_zdmpcg1, gt_zdm0705.
    PERFORM f_get_product_details USING gt_masterdata
                                  CHANGING  gt_zdmpcg1
                                            gt_zdm0705
                                            gt_zap0702.
* Create the master data for Product
    PERFORM f_create_product USING  gv_logsys
                                    gt_masterdata
                                    gt_zdmpcg1
                                    gt_zdm0705
                                    gt_zap0702.
* Update the product log in the data base
    PERFORM f_update_log_in_db USING  gc_logobject
                                      gc_logsubobjp
                                      gt_log_product
                               CHANGING gt_log_handle.
  ENDIF.
  IF p_crlp IS NOT INITIAL.
* Create the master data for the Location-Product
    PERFORM f_create_locprd USING gv_logsys
  gt_masterdata.
* Perform the Live cache consistency check program
    PERFORM f_check_lc_consistency.
* Update the location-product log in the data base
    PERFORM f_update_log_in_db USING  gc_logobject
                                      gc_logsubobjlp
                                      gt_log_locprd
                               CHANGING gt_log_handle.
  ENDIF.
  IF p_crtl IS NOT INITIAL.
* Create the master data for transportation lanes
    PERFORM f_create_tlane USING gv_logsys
  gt_masterdata.
* Update the tlane log in the data base
    PERFORM f_update_log_in_db USING  gc_logobject
                                      gc_logsubobjtl
                                      gt_log_tlane
                               CHANGING gt_log_handle.
  ENDIF.
* Saves the log in the data base
  PERFORM f_save_log_in_db USING gt_log_handle.
* Displays the log from the data base
  PERFORM f_display_log USING gt_log_handle.
  INCLUDE zaxxpr_maintain_masterdata_f02.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_MAINTAIN_MASTERDATA_F02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_MAINTAIN_MASTERDATA_F01
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--Modification*MOD-1771404
* Program name : ZAXXX_MASTERDATA_CREATE
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2006
* Initial Change request number : PMI_SIGN_0002
* Author : Aveek Ghose
* Description :
*
* This report is run in the foreground as also in the batch allowing
* user to maintain the master data in the SIGN system. The master data
* include Product, Location, Location-Product and Transportation lanes
************************************************************************
************************************************************************
* PROGRAM--INITIAL CREATION
* Description :
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_MAINTAIN_MASTERDATA
*---------------------------------------------------------------------*
************************************************************************
*&---------------------------------------------------------------------*
*& Form f_adj_screen_flds
*&---------------------------------------------------------------------*
* Adjusts the screen fields based on user input
*----------------------------------------------------------------------*
FORM f_adj_screen_flds .
  LOOP AT SCREEN.
    CASE screen-name.
      WHEN gc_p_parloc.
        IF p_crloc IS INITIAL.
          screen-input = 0.
          CLEAR p_parloc.
          MODIFY SCREEN.
        ELSE.
          screen-input = 0.
          p_parloc = 'NETWORK'.
          MODIFY SCREEN.
        ENDIF.
      WHEN  gc_dynfld_mktl OR
            gc_dynfld_mkth OR
            gc_dynfld_pctl OR
            gc_dynfld_pcth.
        IF p_crloc IS INITIAL AND
           p_crlp IS INITIAL AND
           p_crtl IS INITIAL.
          screen-input = 0.
          REFRESH: s_mkt, s_pct.
          MODIFY SCREEN.
        ELSE.
          screen-input = 1.
          MODIFY SCREEN.
        ENDIF.
      WHEN gc_dynfld_prdl OR gc_dynfld_prdh.
        IF p_crprd IS INITIAL AND
           p_crlp IS INITIAL AND
           p_crtl IS INITIAL.
          screen-input = 0.
          REFRESH s_prd.
          MODIFY SCREEN.
        ELSE.
          screen-input = 1.
          MODIFY SCREEN.
        ENDIF.
    ENDCASE.
  ENDLOOP.
ENDFORM. " f_adj_screen_flds
*&---------------------------------------------------------------------*
*& Form f_validate_product
*&---------------------------------------------------------------------*
* Validates the product
*----------------------------------------------------------------------*
FORM f_validate_product .
  DATA:
  lv_prd TYPE char18.                                       "#EC NEEDED

  IF s_prd[] IS INITIAL.
    EXIT.
  ENDIF.

  SELECT prvar UP TO 1 ROWS
    INTO lv_prd
    FROM zap_sign
    WHERE pcode IN s_prd OR
          prvar IN s_prd.
  ENDSELECT.
  IF sy-subrc <> 0.
* Product(s) not part of valid sourcing relation(s)
    MESSAGE e004(zfpv).
  ENDIF.
ENDFORM. " f_validate_product
*&---------------------------------------------------------------------*
*& Form f_validate_market
*&---------------------------------------------------------------------*
* Validates the market
*----------------------------------------------------------------------*
FORM f_validate_market .
  DATA:
  lv_mkt TYPE char4.                                        "#EC NEEDED

  IF s_mkt[] IS INITIAL.
    EXIT.
  ENDIF.

  SELECT markt UP TO 1 ROWS
    INTO lv_mkt
    FROM zap_sign
    WHERE markt IN s_mkt.
  ENDSELECT.
  IF sy-subrc <> 0.
* Market(s) not part of valid sourcing relation(s)
    MESSAGE e002(zfpv).
  ENDIF.
ENDFORM. " f_validate_market
*&---------------------------------------------------------------------*
*& Form f_validate_prodcentre
*&---------------------------------------------------------------------*
* Validates the Production centre
*----------------------------------------------------------------------*
FORM f_validate_prodcentre .
  DATA:
  lv_pct TYPE char2.                                        "#EC NEEDED

  IF s_pct[] IS INITIAL.
    EXIT.
  ENDIF.

  SELECT pdctr UP TO 1 ROWS
    INTO lv_pct
    FROM zap_sign
    WHERE pdctr IN s_pct.
  ENDSELECT.
  IF sy-subrc <> 0.
* Production centre(s) not part of valid sourcing relation(s)
    MESSAGE e003(zfpv).
  ENDIF.
ENDFORM. " f_validate_prodcentre
*&---------------------------------------------------------------------*
*& Form f_get_f4_masterdata
*&---------------------------------------------------------------------*
* Gets the master data for the custome search helps
*----------------------------------------------------------------------*
FORM f_get_f4_masterdata CHANGING et_source TYPE t_source
                                  et_produ TYPE t_produ
                                  et_markt TYPE t_markt
                                  et_pdctr TYPE t_pdctr.
  DATA:
  ls_source TYPE s_source,
  ls_produ TYPE s_produ,
  ls_markt TYPE s_markt,
  ls_pdctr TYPE s_pdctr.

  IF et_source IS INITIAL.
    SELECT  prvar vartx
            pcode maktx
            markt martx
            pdctr pctxt
      INTO TABLE et_source
      FROM zap_sign.
    IF sy-subrc <> 0.
* no sourcing info is maintained in the sign portfolio
      MESSAGE i005(zfpv).
      LEAVE LIST-PROCESSING.
    ENDIF.
* Populate the tables for the custom search helps
    LOOP AT et_source INTO ls_source.
      CLEAR: ls_produ, ls_markt, ls_pdctr.
      ls_produ-pcode = ls_source-prvar.
      ls_produ-vartx = ls_source-maktx.
      APPEND ls_produ TO et_produ.
      ls_produ-pcode = ls_source-pcode.
      ls_produ-vartx = ls_source-vartx.
      APPEND ls_produ TO et_produ.
      ls_markt-markt = ls_source-markt.
      ls_markt-martx = ls_source-martx.
      APPEND ls_markt TO et_markt.
      ls_pdctr-pdctr = ls_source-pdctr.
      ls_pdctr-pctxt = ls_source-pctxt.
      APPEND ls_pdctr TO et_pdctr.
    ENDLOOP.
    SORT: et_produ, et_markt, et_pdctr.
    DELETE ADJACENT DUPLICATES FROM et_produ
    COMPARING ALL FIELDS.
    DELETE ADJACENT DUPLICATES FROM et_markt
    COMPARING ALL FIELDS.
    DELETE ADJACENT DUPLICATES FROM et_pdctr
    COMPARING ALL FIELDS.
  ENDIF.
ENDFORM. " f_get_f4_masterdata
*&---------------------------------------------------------------------*
*& Form f_f4_for_product
*&---------------------------------------------------------------------*
* Search help for product
*----------------------------------------------------------------------*
* -->IV_DYNFLD_PRD Scr fld for prod
* -->IT_PRODU Product table
*----------------------------------------------------------------------*
FORM f_f4_for_product USING iv_dynfld_prd TYPE dynfnam
                            it_produ TYPE t_produ.
  CONSTANTS:
  lc_pcode TYPE fieldname VALUE 'PCODE'.
  PERFORM f_launch_f4_window USING  lc_pcode
                                    iv_dynfld_prd
                                    text-009
                                    it_produ.
ENDFORM. " f_f4_for_product
*&---------------------------------------------------------------------*
*& Form f_launch_f4_window
*&---------------------------------------------------------------------*
* Launches the F4 window
*----------------------------------------------------------------------*
* -->IV_RETFLD Return field
* -->IV_DYNFLD Screen field
* -->IV_WINTITLE Window title
* -->IT_VALUETAB Value table
*----------------------------------------------------------------------*
FORM f_launch_f4_window USING iv_retfld TYPE fieldname
                              iv_dynfld TYPE dynfnam
                              iv_wintitle TYPE c
                              it_valuetab TYPE STANDARD TABLE.
  DATA:
  lv_dynprog TYPE syrepid,
  lv_dynpnr TYPE sydynnr.
  lv_dynprog = sy-repid.
  lv_dynpnr = sy-dynnr.

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = iv_retfld
      dynpprog        = lv_dynprog
      dynpnr          = lv_dynpnr
      dynprofield     = iv_dynfld
      window_title    = iv_wintitle
      value_org       = gc_s
    TABLES
      value_tab       = it_valuetab
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.
ENDFORM. " f_launch_f4_window
*&---------------------------------------------------------------------*
*& Form f_f4_for_market
*&---------------------------------------------------------------------*
* Search help for market
*----------------------------------------------------------------------*
* -->IV_DYNFLD_MKT Scr fld for market
* -->IT_MARKT Market table
*----------------------------------------------------------------------*
FORM f_f4_for_market USING iv_dynfld_mkt TYPE dynfnam
it_markt TYPE t_markt.
  CONSTANTS:
  lc_markt TYPE fieldname VALUE 'MARKT'.
  PERFORM f_launch_f4_window USING  lc_markt
                                    iv_dynfld_mkt
                                    text-010
                                    it_markt.
ENDFORM. " f_f4_for_market
*&---------------------------------------------------------------------*
*& Form f_f4_for_prodcentre
*&---------------------------------------------------------------------*
* Search help for production centre
*----------------------------------------------------------------------*
* -->IV_DYNFLD_PCT Scr field for prod centre
* -->IT_PDCTR Prioduction centre table
*----------------------------------------------------------------------*
FORM f_f4_for_prodcentre USING iv_dynfld_pct TYPE dynfnam
                               it_pdctr TYPE t_pdctr.
  CONSTANTS:
  lc_pdctr TYPE fieldname VALUE 'PDCTR'.

  PERFORM f_launch_f4_window USING  lc_pdctr
                                    iv_dynfld_pct
                                    text-011
                                    it_pdctr.
ENDFORM. " f_f4_for_prodcentre
*&---------------------------------------------------------------------*
*& Form f_retrieve_masterdata
*&---------------------------------------------------------------------*
* Gets the master data from the SIGN portfolio meeting the
* user selection
*----------------------------------------------------------------------*
* <--ET_MASTERDATA Master data table
*----------------------------------------------------------------------*
FORM f_retrieve_masterdata CHANGING et_masterdata TYPE t_masterdata.
  SELECT * INTO TABLE et_masterdata
    FROM zap_sign
    WHERE ( prvar IN s_prd OR
            pcode IN s_prd ) AND
          markt IN s_mkt AND
          pdctr IN s_pct AND
          delfl = space.
  IF sy-subrc <> 0.
* No sourcing info is maintained for the user selection
    MESSAGE i018(zfpv).
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM. " f_retrieve_masterdata
*&---------------------------------------------------------------------*
*& Form f_create_location
*&---------------------------------------------------------------------*
* Creates Location
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_MASTERDATA Table containing master data to be created
* -->ZDU1010 Market table
* -->ZDU1000 Source table
* -->ZAPWHSE Warehouse table
*----------------------------------------------------------------------*
FORM f_create_location USING  iv_logsys TYPE logsys
                              it_masterdata TYPE t_masterdata
                              it_zdu1010 TYPE t_zdu1010
*                               it_zdu1000 TYPE t_zdu1000
                              it_zapwhse TYPE t_zapwhse.
  DATA:
  lv_next TYPE flag.
  DATA:
  lt_location TYPE t_location,
  lt_loc_extloc TYPE t_loc_extloc.

* Collect all possible locations from the sourcing informations.
  PERFORM f_collect_locations USING it_masterdata
                              CHANGING lt_location
                                       lt_loc_extloc.
* Filter locations based on the entries in /SAPAPO/LOC to obtain the
* locations that need to be created
  PERFORM f_filter_location CHANGING lt_location
                                     lv_next.
  IF lv_next IS NOT INITIAL.
* Location(s) entered for creation already there in database
    MESSAGE i019(zfpv).
* Proceed to the creation of the next master data object
    EXIT.
  ENDIF.
* Create the locations in the database thru BAPI interface
  PERFORM f_insert_location_in_db USING iv_logsys
                                        lt_loc_extloc
                                        it_zdu1010
*                                       it_zdu1000
                                        it_zapwhse
                                  CHANGING lt_location.
* Update the location hierarchy for the locations successfully
* created
  PERFORM f_update_location_hierarchy USING lt_location.
ENDFORM. " f_create_location
*&---------------------------------------------------------------------*
*& Form f_collect_locations
*&---------------------------------------------------------------------*
* Collects all possible locations from the srcing info
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data fronm the view table
* <--ET_LOCATION Location table
* <--ET_LOC_EXTLOC Location-ext location mapping table
*----------------------------------------------------------------------*
FORM f_collect_locations USING it_masterdata TYPE t_masterdata
                         CHANGING et_location TYPE t_location
                                  et_loc_extloc TYPE t_loc_extloc.
  DATA:
  ls_location TYPE s_location,
  ls_loc_extloc TYPE s_loc_extloc,
  ls_masterdata TYPE zap_sign.

  LOOP AT it_masterdata INTO ls_masterdata.
    CLEAR ls_location.
    ls_location-locno = ls_masterdata-markt.
    ls_location-loctp = gc_ltp_1002.
    ls_location-locct = gc_markt.
    APPEND ls_location TO et_location.
    ls_location-locno = ls_masterdata-pdctr.
    ls_location-loctp = gc_ltp_1001.
    ls_location-locct = gc_pdctr.
    APPEND ls_location TO et_location.
    IF ls_masterdata-whse1 IS NOT INITIAL.
      ls_location-locno = ls_masterdata-whse1.
      ls_location-loctp = gc_ltp_1002.
      ls_location-locct = gc_whse.
      APPEND ls_location TO et_location.
    ENDIF.
    IF ls_masterdata-whse2 IS NOT INITIAL.
      ls_location-locno = ls_masterdata-whse2.
      ls_location-loctp = gc_ltp_1002.
      ls_location-locct = gc_whse.
      APPEND ls_location TO et_location.
    ENDIF.
  ENDLOOP.
  SORT et_location BY locno.
  DELETE ADJACENT DUPLICATES FROM et_location
  COMPARING locno.

* Get the external location details
  LOOP AT et_location INTO ls_location.
    CLEAR ls_loc_extloc.
    ls_loc_extloc-locno = ls_location-locno.
    PERFORM f_get_extloc_from_loc USING ls_location-locno
                                  CHANGING ls_loc_extloc-extloc.
    APPEND ls_loc_extloc TO et_loc_extloc.
  ENDLOOP.
  SORT et_loc_extloc BY locno.
ENDFORM. " f_collect_locations
*&---------------------------------------------------------------------*
*& Form f_filter_location
*&---------------------------------------------------------------------*
* Filters location based on /SAPAPO/LOC
*----------------------------------------------------------------------*
* <--ET_LOCATION Filtered locations
*----------------------------------------------------------------------*
FORM f_filter_location CHANGING et_location TYPE t_location
                                ev_next TYPE flag.
  DATA:
  lt_location_cr TYPE t_location,
  ls_location TYPE s_location.
  FIELD-SYMBOLS:
  <lfs_location> TYPE s_location.

* Find all the locations from the view table that are already created
  SELECT locno
    INTO CORRESPONDING FIELDS OF TABLE lt_location_cr
    FROM /sapapo/loc
    FOR ALL ENTRIES IN et_location
    WHERE locno = et_location-locno.
  LOOP AT lt_location_cr INTO ls_location.
    READ TABLE et_location ASSIGNING <lfs_location>
      WITH KEY locno = ls_location-locno
      BINARY SEARCH.
    IF sy-subrc = 0.
      CLEAR <lfs_location>-locno.
      IF ev_next IS NOT INITIAL.
        CONTINUE.
      ENDIF.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_warn
                                        gc_msgno_024
                                        ls_location-locno
                                        space space space space
                                 CHANGING gt_log_location.
    ENDIF.
  ENDLOOP.
  DELETE et_location WHERE locno IS INITIAL.
  IF et_location IS INITIAL.
    ev_next = gc_x.
  ENDIF.
ENDFORM. " f_filter_location
*&---------------------------------------------------------------------*
*& Form f_get_location_details
*&---------------------------------------------------------------------*
* Get the location details
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data table
* <--ET_ZDU1010 Market table
* <--ET_ZDU1000 Prod centre table
* <--ET_ZAPWHSE Warehouse table
*----------------------------------------------------------------------*
FORM f_get_location_details USING it_masterdata TYPE t_masterdata
                            CHANGING et_zdu1010 TYPE t_zdu1010
*                                    et_zdu1000 TYPE t_zdu1000
                                     et_zapwhse TYPE t_zapwhse.
* Get the market details.
  SELECT * INTO TABLE et_zdu1010
    FROM zdu1010
    FOR ALL ENTRIES IN it_masterdata
    WHERE markt = it_masterdata-markt.
  IF sy-subrc = 0.
    SORT et_zdu1010 BY markt.
    DELETE ADJACENT DUPLICATES FROM et_zdu1010
    COMPARING ALL FIELDS.
  ENDIF.
** Get the production centre details
* SELECT * INTO TABLE et_zdu1000
* FROM zdu1000
* FOR ALL ENTRIES IN it_masterdata
* WHERE pdctr = it_masterdata-pdctr.
*
* IF sy-subrc = 0.
* SORT et_zdu1000 BY pdctr.
* DELETE ADJACENT DUPLICATES FROM et_zdu1000
* COMPARING ALL FIELDS.
* ENDIF.
* Get the warehouse details
  SELECT * INTO TABLE et_zapwhse
    FROM zap_whse
    FOR ALL ENTRIES IN it_masterdata
    WHERE whse = it_masterdata-whse1.
  SELECT * APPENDING TABLE et_zapwhse
    FROM zap_whse
    FOR ALL ENTRIES IN it_masterdata
    WHERE whse = it_masterdata-whse2.
  IF sy-subrc = 0.
    SORT et_zapwhse BY whse.
    DELETE ADJACENT DUPLICATES FROM et_zapwhse
    COMPARING ALL FIELDS.
  ENDIF.
ENDFORM. " f_get_location_details
*&---------------------------------------------------------------------*
*& Form f_insert_location_in_db
*&---------------------------------------------------------------------*
* Inserts location into database
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_LOC_EXTLOC Location-Ext location mapping table
* -->IT_ZDU1010 Market table
* -->IT_ZDU1000 Source table
* -->IT_ZAPWHSE Ware house table
* <--ET_LOCATION Location table
*----------------------------------------------------------------------*
FORM f_insert_location_in_db USING  iv_logsys TYPE logsys
                                    it_loc_extloc TYPE t_loc_extloc
                                    it_zdu1010 TYPE t_zdu1010
*                                     it_zdu1000 TYPE t_zdu1000
                                    it_zapwhse TYPE t_zapwhse
                             CHANGING et_location TYPE t_location.
  DATA:
  lt_return TYPE t_return,
  lt_location_head TYPE t_location_head,
  lt_location_head_x TYPE t_location_head_x,
  lt_location_text TYPE t_location_text,
  lt_location_text_x TYPE t_location_text_x,
  lt_location_address TYPE t_location_address,
  lt_location_address_x TYPE t_location_address_x,
  lt_location_model TYPE t_location_model.
  DATA:
  ls_return TYPE bapiret2.
  FIELD-SYMBOLS:
  <lfs_location> TYPE s_location.

  LOOP AT et_location ASSIGNING <lfs_location>.
    REFRESH: lt_return, lt_location_head, lt_location_head_x,
    lt_location_text, lt_location_text_x,
    lt_location_address, lt_location_address_x,
    lt_location_model.
    PERFORM f_populate_locbapi_table USING <lfs_location>
                                            it_loc_extloc
                                            it_zdu1010
*                                           it_zdu1000
                                            it_zapwhse
                                     CHANGING lt_location_head
                                              lt_location_head_x
                                              lt_location_text
                                              lt_location_text_x
                                              lt_location_address
                                              lt_location_address_x
                                              lt_location_model.
* Call teh BAPi interface to create the locations
    PERFORM f_call_location_bapi USING  iv_logsys
                                        lt_location_head
                                        lt_location_head_x
                                        lt_location_text
                                        lt_location_text_x
                                        lt_location_address
                                        lt_location_address_x
                                        lt_location_model
                                 CHANGING lt_return.
* If the location is not created successfully, mark the location
* for deletion from the table so that is not considered for
* updation of location hierarchy
    IF lt_return IS NOT INITIAL.
      READ TABLE lt_return INTO ls_return
      WITH KEY type = gc_e.
      IF sy-subrc = 0.
        CLEAR <lfs_location>-locno.
        PERFORM f_update_log_table USING  ls_return-id
                                          ls_return-type
                                          ls_return-number
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message
                                   CHANGING gt_log_location.
      ELSE.
* Location created successfully
        PERFORM f_update_log_table USING  gc_msgid
                                          gc_succ
                                          gc_msgno_025
                                          <lfs_location>-locno
                                          space space space space
                                   CHANGING gt_log_location.
      ENDIF.
    ELSE.
* Location created successfully
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_succ
                                        gc_msgno_025
                                        <lfs_location>-locno
                                        space space space space
                                 CHANGING gt_log_location.
    ENDIF.
  ENDLOOP.
  DELETE et_location WHERE locno IS INITIAL.
ENDFORM. " f_insert_location_in_db
*&---------------------------------------------------------------------*
*& Form f_populate_locBAPI_table
*&---------------------------------------------------------------------*
* Populates the BAPI structure
*----------------------------------------------------------------------*
* -->IS_LOCATION Location structure
* -->IT_LOC_EXTLOC Table for loc-extloc mapping
* -->IT_ZDU1010 Market table
* -->IT_ZDU1000 Source table
* -->IT_ZAPWHSE Warehouse table
* <--ET_LOCATION_HEAD Location head
* <--ET_LOCATION_HEAD_X Location head x
* <--ET_LOCATION_TEXT Location text
* <--ET_LOCATION_TEXT_X Location text x
* <--ET_LOCATION_ADDRESS Location address
* <--ET_LOCATION_ADDRESS_X Location address x
* <--ET_LOCATION_MODEL Location model
*----------------------------------------------------------------------*
FORM f_populate_locbapi_table USING is_location TYPE s_location
                                    it_loc_extloc TYPE t_loc_extloc
                                    it_zdu1010 TYPE t_zdu1010
*                                   it_zdu1000 TYPE t_zdu1000
                                    it_zapwhse TYPE t_zapwhse
                              CHANGING et_location_head TYPE t_location_head
                                        et_location_head_x TYPE t_location_head_x
                                        et_location_text TYPE t_location_text
                                        et_location_text_x TYPE t_location_text_x
                                        et_location_address TYPE t_location_address
                                        et_location_address_x TYPE t_location_address_x
                                        et_location_model TYPE t_location_model.
  DATA:
  lv_cntry TYPE land1,
  lv_isocnt TYPE intca,
  lv_locdesc TYPE /sapapo/c_loc_descr40.
  DATA:
  ls_zdu1010 TYPE zdu1010,
* ls_zdu1000 TYPE zdu1000,
  ls_zapwhse TYPE zap_whse,
  ls_loc_extloc TYPE s_loc_extloc,
  ls_location_head TYPE bapi10002loc2,
  ls_location_head_x TYPE bapi10002loc2x,
  ls_location_text TYPE bapi10002loctext2,
  ls_location_text_x TYPE bapi10002loctext2x,
  ls_location_address TYPE bapi10002locaddr2,
  ls_location_address_x TYPE bapi10002locaddr2x,
  ls_location_model TYPE bapi10002locmodel2.
* Get the country and description of the location
* The comparision between is_location-locno and the
* corresponding field in the internal tables is properly
* tested though they are of different types
  CASE is_location-locct.
    WHEN gc_markt.
      READ TABLE it_zdu1010 INTO ls_zdu1010
      WITH KEY markt = is_location-locno
      BINARY SEARCH.
      IF sy-subrc = 0.
*-->MOD-001 Right now SMDL doesnt use std ISO cntry code
* lv_cntry = ls_zdu1010-terry.
* lv_cntry = '00'.
        SELECT SINGLE cntry INTO lv_isocnt
        FROM zdu1011 WHERE terry = ls_zdu1010-terry.
        IF sy-subrc = 0 AND lv_isocnt IS NOT INITIAL.
          lv_cntry = lv_isocnt.
        ELSE.
          lv_cntry = '00'.
        ENDIF.
*<--MOD-001 Right now SMDL doesnt use std ISO cntry code
        lv_locdesc = ls_zdu1010-stext.
      ENDIF.
    WHEN gc_pdctr.
* READ TABLE it_zdu1000 INTO ls_zdu1000
* WITH KEY pdctr = is_location-locno
* BINARY SEARCH.
* IF sy-subrc = 0.
**-->MOD-001 Right now SMDL doesnt use std ISO cntry code
** lv_cntry = ls_zdu1000-terry.
** lv_cntry = '00'.
* SELECT SINGLE cntry INTO lv_isocnt
* FROM zdu1011 WHERE terry = ls_zdu1000-terry.
* IF sy-subrc = 0 AND lv_isocnt IS NOT INITIAL.
* lv_cntry = lv_isocnt.
* ELSE.
* lv_cntry = '00'.
* ENDIF.
**<--MOD-001 Right now SMDL doesnt use std ISO cntry code
* lv_locdesc = ls_zdu1000-stext.
* ENDIF.WHEN gc_whse.
      READ TABLE it_zapwhse INTO ls_zapwhse
        WITH KEY whse = is_location-locno
        BINARY SEARCH.
      IF sy-subrc = 0.
        lv_cntry = ls_zapwhse-cntry.
        lv_locdesc = ls_zapwhse-whdesc.
      ENDIF.
  ENDCASE.
* Populating the structures.
  READ TABLE it_loc_extloc INTO ls_loc_extloc
    WITH KEY locno = is_location-locno
    BINARY SEARCH.
  IF sy-subrc = 0.
* Populating the external location indicator
    ls_location_head-location = ls_loc_extloc-extloc.
    ls_location_text-location = ls_loc_extloc-extloc.
    ls_location_address-location = ls_loc_extloc-extloc.
    ls_location_model-location = ls_loc_extloc-extloc.
    ls_location_head_x-location = ls_loc_extloc-extloc.
    ls_location_text_x-location = ls_loc_extloc-extloc.
    ls_location_address_x-location = ls_loc_extloc-extloc.
  ENDIF.
* Populating the location type
  ls_location_head-loctype = is_location-loctp.
  ls_location_text-loctype = is_location-loctp.
  ls_location_address-loctype = is_location-loctp.
  ls_location_model-loctype = is_location-loctp.
  ls_location_head_x-loctype = is_location-loctp.
  ls_location_text_x-loctype = is_location-loctp.
  ls_location_address_x-loctype = is_location-loctp.
* Populating the timezone
  ls_location_head-time_zone = gc_utc.
  ls_location_head_x-time_zone = gc_x.
* Populating the attribute03 field
  ls_location_head-attribute_5 = is_location-locct.
  ls_location_head_x-attribute_5 = gc_x.
* Populating the iso language
  ls_location_text-langu_iso = gc_en.
  ls_location_address-langu_iso = gc_en.
  ls_location_text_x-langu_iso = gc_en.
  ls_location_address_x-langu_iso = gc_x.
* Populating the location description
  ls_location_text-loc_descr = lv_locdesc.
  ls_location_text_x-loc_descr = gc_x.
* Populating the country key
  ls_location_address-country = lv_cntry.
  ls_location_address_x-country = gc_x.
* Populating the location model
  ls_location_model-model = gc_000.
* Appending the structures to the respective BAPI tables
  APPEND: ls_location_head TO et_location_head,
  ls_location_head_x TO et_location_head_x,
  ls_location_text TO et_location_text,
  ls_location_text_x TO et_location_text_x,
  ls_location_address TO et_location_address,
  ls_location_address_x TO et_location_address_x,
  ls_location_model TO et_location_model.
ENDFORM. " f_populate_locBAPI_table
*&---------------------------------------------------------------------*
*& Form f_get_logical_system
*&---------------------------------------------------------------------*
* Gets the name of the logical system
*----------------------------------------------------------------------*
* <--EV_LOGSYS Logical system
*----------------------------------------------------------------------*
FORM f_get_logical_system CHANGING ev_logsys TYPE logsys.
  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system             = ev_logsys
    EXCEPTIONS
      own_logical_system_not_defined = 1
      OTHERS                         = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
ENDFORM. " f_get_logical_system
*&---------------------------------------------------------------------*
*& Form f_call_location_bapi
*&---------------------------------------------------------------------*
* Location BAPI call
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_LOCATION_HEAD Location head
* -->IT_LOCATION_HEAD_X Location head x
* -->IT_LOCATION_TEXT Location text
* -->IT_LOCATION_TEXT_X Location text x
* -->IT_LOCATION_ADDRESS Location address
* -->IT_LOCATION_ADDRESS_X Location address x
* -->IT_LOCATION_MODEL Location model
* <--ET_RETURN Return table
*----------------------------------------------------------------------*
FORM f_call_location_bapi USING iv_logsys TYPE logsys
                                it_location_head TYPE t_location_head
                                it_location_head_x TYPE t_location_head_x
                                it_location_text TYPE t_location_text
                                it_location_text_x TYPE t_location_text_x
                                it_location_address TYPE t_location_address
                                it_location_address_x TYPE t_location_address_x
                                it_location_model TYPE t_location_model
                          CHANGING et_return TYPE t_return.
  DATA:
  lv_err TYPE flag.

  CALL FUNCTION 'BAPI_LOCSRVAPS_SAVEMULTI2'
    EXPORTING
      logical_system     = iv_logsys
      commit_control     = space
    TABLES
      location_head      = it_location_head
      location_head_x    = it_location_head_x
      location_text      = it_location_text
      location_text_x    = it_location_text_x
      location_address   = it_location_address
      location_address_x = it_location_address_x
      location_model     = it_location_model
      return             = et_return.
  LOOP AT et_return TRANSPORTING NO FIELDS
                    WHERE type = gc_err.
    lv_err = gc_x.
    EXIT.
  ENDLOOP.
  IF lv_err IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = gc_x.
  ENDIF.
ENDFORM. " f_call_location_bapi
*&---------------------------------------------------------------------*
*& Form f_get_product_details
*&---------------------------------------------------------------------*
* Gets the product details
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data table
* <--ET_ZDMPCG1 Core product table
* <--ET_ZDM0705 Variant table
*----------------------------------------------------------------------*
FORM f_get_product_details  USING it_masterdata TYPE t_masterdata
                            CHANGING et_zdmpcg1 TYPE t_zdmpcg1
                            et_zdm0705 TYPE t_zdm0705
                            et_zap0702 TYPE t_zap0702.
* Get the core product details
  SELECT * INTO TABLE et_zdmpcg1
    FROM zdmpcg1
    FOR ALL ENTRIES IN it_masterdata
    WHERE prdcd = it_masterdata-pcode.
  IF sy-subrc = 0.
    SORT et_zdmpcg1 BY prdcd.
    DELETE ADJACENT DUPLICATES FROM et_zdmpcg1
    COMPARING ALL FIELDS.
  ENDIF.
* Get the variant details
  SELECT * INTO TABLE et_zdm0705
    FROM zdm0705
    FOR ALL ENTRIES IN it_masterdata
    WHERE prvar = it_masterdata-prvar.
  IF sy-subrc = 0.
    SORT et_zdm0705 BY prvar.
    DELETE ADJACENT DUPLICATES FROM et_zdm0705
      COMPARING ALL FIELDS.
  ENDIF.
* Get the variant mat grp
  SELECT * INTO TABLE et_zap0702
    FROM zap0702
    FOR ALL ENTRIES IN et_zdmpcg1
    WHERE matkl_parpr = et_zdmpcg1-matkl.
  IF sy-subrc = 0.
    SORT et_zap0702 BY matkl_parpr.
    DELETE ADJACENT DUPLICATES FROM et_zap0702
      COMPARING ALL FIELDS.
  ENDIF.
ENDFORM. " f_get_product_details
*&---------------------------------------------------------------------*
*& Form f_create_product
*&---------------------------------------------------------------------*
* Creates the master data for Product
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_MASTERDATA Master data table
* -->IT_ZDMPCG1 Core product table
* -->IT_ZDM0705 Variant table
*----------------------------------------------------------------------*
FORM f_create_product USING iv_logsys TYPE logsys
                            it_masterdata TYPE t_masterdata
                            it_zdmpcg1 TYPE t_zdmpcg1
                            it_zdm0705 TYPE t_zdm0705
                            it_zap0702 TYPE t_zap0702.
  DATA:
  lv_next TYPE flag.
  DATA:
  lt_product TYPE t_product,
  lt_prd_extprd TYPE t_prd_extprd.

* Get all possible products that need to be created
  PERFORM f_collect_products  USING it_masterdata
                              CHANGING lt_product
                                       lt_prd_extprd.
* Filter products based on the entries in /SAPAPO/MATKEY to obtain the
* products that need to be created
  PERFORM f_filter_product CHANGING lt_product
                                    lv_next.
  IF lv_next IS NOT INITIAL.
* Product(s) entered for creation already there in database
    MESSAGE i020(zfpv).
    EXIT.
  ENDIF.
* Create the products in the database thru BAPI interface
  PERFORM f_insert_product_in_db USING  iv_logsys
                                        lt_prd_extprd
                                        it_zdmpcg1
                                        it_zdm0705
                                        it_zap0702
                                 CHANGING lt_product.
* Update the product hierarchy for the products successfully
* created
  PERFORM f_update_product_hierarchy USING lt_product.
ENDFORM. " f_create_product
*&---------------------------------------------------------------------*
*& Form f_collect_products
*&---------------------------------------------------------------------*
* Collects all products for creation
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data table
* <--ET_PRODUCT Product table
* <--ET_PRD_EXTPRD Prod-ext prod mapping
*----------------------------------------------------------------------*
FORM f_collect_products USING it_masterdata TYPE t_masterdata
                        CHANGING et_product TYPE t_product
                                 et_prd_extprd TYPE t_prd_extprd.
  DATA:
  ls_masterdata TYPE zap_sign,
  ls_prd_extprd TYPE s_prd_extprd,
  ls_product TYPE s_product.

  LOOP AT it_masterdata INTO ls_masterdata
    WHERE status = gc_stat_p OR
          status = gc_stat_a.
    CLEAR ls_product.
    ls_product-matnr = ls_masterdata-pcode.
    ls_product-matct = gc_core.
    APPEND ls_product TO et_product.
    ls_product-matnr = ls_masterdata-prvar.
    ls_product-matct = gc_var.
    APPEND ls_product TO et_product.
  ENDLOOP.
  SORT et_product BY matnr.
  DELETE ADJACENT DUPLICATES FROM et_product
    COMPARING matnr.
* Get the external product details
  LOOP AT et_product INTO ls_product.
    CLEAR ls_prd_extprd.
    ls_prd_extprd-matnr = ls_product-matnr.
    PERFORM f_get_extmat_from_mat USING ls_product-matnr
                                  CHANGING ls_prd_extprd-extmat.
    APPEND ls_prd_extprd TO et_prd_extprd.
  ENDLOOP.
  SORT et_prd_extprd BY matnr.
ENDFORM. " f_collect_products
*&---------------------------------------------------------------------*
*& Form f_filter_product
*&---------------------------------------------------------------------*
* Filters the products that need to be created
*----------------------------------------------------------------------*
* <--ET_PRODUCT Product table
*----------------------------------------------------------------------*
FORM f_filter_product CHANGING et_product TYPE t_product
                               ev_next TYPE flag.
  DATA:
  lt_product_cr TYPE t_product,
  ls_product TYPE s_product.
  FIELD-SYMBOLS:
  <lfs_product> TYPE s_product.

* Find all the locations from the view table that are already created
  SELECT matnr
    INTO CORRESPONDING FIELDS OF TABLE lt_product_cr
    FROM /sapapo/matkey
    FOR ALL ENTRIES IN et_product
    WHERE matnr = et_product-matnr.
  LOOP AT lt_product_cr INTO ls_product.
    READ TABLE et_product ASSIGNING <lfs_product>
      WITH KEY matnr = ls_product-matnr
      BINARY SEARCH.
    IF sy-subrc = 0.
      CLEAR <lfs_product>-matnr.
      IF ev_next IS NOT INITIAL.
        CONTINUE.
      ENDIF.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_warn
                                        gc_msgno_026
                                        ls_product-matnr
                                        space space space space
                                 CHANGING gt_log_product.
    ENDIF.
  ENDLOOP.
  DELETE et_product WHERE matnr IS INITIAL.
  IF et_product IS INITIAL.
    ev_next = gc_x.
  ENDIF.
ENDFORM. " f_filter_product
*&---------------------------------------------------------------------*
*& Form insert_product_in_db
*&---------------------------------------------------------------------*
* Inserts the new prods in the dbase thru BAPI interface
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_PRD_EXTPRD Prod-ext prod mapping table
* -->IT_ZDMPCG1 Core product table
* -->IT_ZDM0705 Variant table
* <--ET_PRODUCT Product table
*----------------------------------------------------------------------*
FORM f_insert_product_in_db USING iv_logsys TYPE logsys
                                  it_prd_extprd TYPE t_prd_extprd
                                  it_zdmpcg1 TYPE t_zdmpcg1
                                  it_zdm0705 TYPE t_zdm0705
                                  it_zap0702 TYPE t_zap0702
                            CHANGING et_product TYPE t_product.
  DATA:
  lv_errvar TYPE flag, " SR 2,754,131
  lt_return TYPE t_return,
  lt_product_head TYPE t_product_head,
  lt_product_head_x TYPE t_product_head_x,
  lt_product_text TYPE t_product_text,
  lt_product_text_x TYPE t_product_text_x.
  DATA:
  ls_return TYPE bapiret2.
  FIELD-SYMBOLS:
  <lfs_product> TYPE s_product.
  LOOP AT et_product ASSIGNING <lfs_product>.
    CLEAR lv_errvar. " SR 2,754,131
    REFRESH: lt_return, lt_product_head, lt_product_head_x,
             lt_product_text, lt_product_text_x.
    PERFORM f_populate_prdbapi_table USING <lfs_product>
                                            it_prd_extprd
                                            it_zdmpcg1
                                            it_zdm0705
                                            it_zap0702
                                     CHANGING lv_errvar " SR 2,754,131
                                              lt_product_head
                                              lt_product_head_x
                                              lt_product_text
                                              lt_product_text_x.
*  -->SR 2,754,131
    IF lv_errvar IS NOT INITIAL.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_096
                                        <lfs_product>-matnr
                                        space space space space
                                 CHANGING gt_log_product.
      CLEAR <lfs_product>-matnr.
      CONTINUE.
    ENDIF.
*<--SR 2,754,131
* Call the BAPI interface to create the products
    PERFORM f_call_product_bapi USING iv_logsys
                                      lt_product_head
                                      lt_product_head_x
                                      lt_product_text
                                      lt_product_text_x
                                CHANGING lt_return.
* If the product is not created successfully, mark the product
* for deletion from the table so that is not considered for
* updation of product hierarchy
    IF lt_return IS NOT INITIAL.
      CLEAR <lfs_product>-matnr.
      READ TABLE lt_return INTO ls_return
      WITH KEY type = gc_e.
      IF sy-subrc = 0.
        PERFORM f_update_log_table USING  ls_return-id
                                          ls_return-type
                                          ls_return-number
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message
                                   CHANGING gt_log_product.
      ENDIF.
    ELSE.
* Product created successfully
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_succ
                                        gc_msgno_027
                                        <lfs_product>-matnr
                                        space space space space
                                 CHANGING gt_log_product.
    ENDIF.
  ENDLOOP.
  DELETE et_product WHERE matnr IS INITIAL.
ENDFORM. " insert_product_in_db
*&---------------------------------------------------------------------*
*& Form f_populate_prdbapi_table
*&---------------------------------------------------------------------*
* Populates the Product BAPI tables
*----------------------------------------------------------------------*
* -->IS_PRODUCT Product structure
* -->IT_PRD_EXTPRD Prod-ext prod mapping
* -->IT_ZDMPCG1 Core product table
* -->IT_ZDM0705 Variant table
* <--ET_PRODUCT_HEAD Product head
* <--ET_PRODUCT_HEAD_X Product head x
* <--ET_PRODUCT_TEXT Product text
* <--ET_PRODUCT_TEXT_X Product text x
*----------------------------------------------------------------------*
FORM f_populate_prdbapi_table USING is_product TYPE s_product
                                    it_prd_extprd TYPE t_prd_extprd
                                    it_zdmpcg1 TYPE t_zdmpcg1
                                    it_zdm0705 TYPE t_zdm0705
                                    it_zap0702 TYPE t_zap0702
                              CHANGING  ev_errvar TYPE flag " SR 2,754,131
                                        et_product_head TYPE t_product_head
                                        et_product_head_x TYPE t_product_head_x
                                        et_product_text TYPE t_product_text
                                        et_product_text_x TYPE t_product_text_x.
  DATA:
  lv_matkl TYPE /sapapo/matkl,
  lv_maktx TYPE /sapapo/maktx.
  DATA:
  ls_zdmpcg1 TYPE zdmpcg1,
  ls_zdm0705 TYPE zdm0705,
  ls_prd_extprd TYPE s_prd_extprd.
  DATA:
  ls_product_head TYPE bapi10001prd2,
  ls_product_head_x TYPE bapi10001prd2x,
  ls_product_text TYPE bapi10001prdtext2,
  ls_product_text_x TYPE bapi10001prdtext2x.
  CASE is_product-matct.
    WHEN gc_core.
      READ TABLE it_zdmpcg1 INTO ls_zdmpcg1
        WITH KEY prdcd = is_product-matnr
        BINARY SEARCH.
      IF sy-subrc = 0.
        lv_matkl = ls_zdmpcg1-matkl.
        lv_maktx = ls_zdmpcg1-maktx.
      ENDIF.
    WHEN gc_var.
* READ TABLE it_zdm0705 INTO ls_zdm0705
* WITH KEY prvar = is_product-matnr
* BINARY SEARCH.
* IF sy-subrc = 0.
* lv_maktx = ls_zdm0705-vartx.
* ENDIF.
      PERFORM f_get_variant_matkl_maktx USING is_product-matnr
                                              it_zdmpcg1
                                              it_zap0702
                                        CHANGING lv_matkl
                                                 lv_maktx.
*-->SR 2,754,131
* If material grp is not maintained for a variant the masterdata
* should not be created.
      IF lv_matkl IS INITIAL.
        ev_errvar = gc_x.
        EXIT.
      ENDIF.
*<--SR 2,754,131
  ENDCASE.
  CLEAR: ls_product_head, ls_product_head_x,
  ls_product_text, ls_product_text_x.
* Populating the structures
  READ TABLE it_prd_extprd INTO ls_prd_extprd
    WITH KEY matnr = is_product-matnr
    BINARY SEARCH.
  IF sy-subrc = 0.
* Populating the external product indicator
    ls_product_head-product = ls_prd_extprd-extmat.
    ls_product_text-product = ls_prd_extprd-extmat.
    ls_product_head_x-product = ls_prd_extprd-extmat.
    ls_product_text_x-product = ls_prd_extprd-extmat.
  ENDIF.
* Populating the material group
  ls_product_head-material_group = lv_matkl.
  ls_product_head_x-material_group = gc_x.
* Populating the unit of measure
  ls_product_head-base_uom = gc_uom_th.
  ls_product_head_x-base_uom = gc_x.
* Populating the product description
  ls_product_text-prd_descr = lv_maktx.
  ls_product_text_x-prd_descr = gc_x.
* Populating the ISO language
  ls_product_text-langu_iso = gc_en.
  ls_product_text_x-langu_iso = gc_en.
* Appending the structures to the respective BAPI tables
  APPEND: ls_product_head TO et_product_head,
  ls_product_head_x TO et_product_head_x,
  ls_product_text TO et_product_text,
  ls_product_text_x TO et_product_text_x.
ENDFORM. " f_populate_prdbapi_table
*&---------------------------------------------------------------------*
*& Form f_call_product_bapi
*&---------------------------------------------------------------------*
* Calls the product BAPI to create the product
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_PRODUCT_HEAD Product head
* -->IT_PRODUCT_HEAD_X Product head x
* -->IT_PRODUCT_TEXT Product text
* -->IT_PRODUCT_TEXT_X Product text x
* <--ET_RETURN BAPI return table
*----------------------------------------------------------------------*
FORM f_call_product_bapi USING  iv_logsys TYPE logsys
                                it_product_head TYPE t_product_head
                                it_product_head_x TYPE t_product_head_x
                                it_product_text TYPE t_product_text
                                it_product_text_x TYPE t_product_text_x
                         CHANGING et_return TYPE t_return.
  DATA:
  lv_err TYPE flag.

  CALL FUNCTION 'BAPI_PRDSRVAPS_SAVEMULTI2'
    EXPORTING
      logical_system = iv_logsys
      commit_control = space
    TABLES
      product_head   = it_product_head
      product_head_x = it_product_head_x
      product_text   = it_product_text
      product_text_x = it_product_text_x
      return         = et_return.
  LOOP AT et_return TRANSPORTING NO FIELDS
                    WHERE type = gc_err.
    lv_err = gc_x.
    EXIT.
  ENDLOOP.
  IF lv_err IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = gc_x.
  ENDIF.
ENDFORM. " f_call_product_bapi
*&---------------------------------------------------------------------*
*& Form f_create_locprd
*&---------------------------------------------------------------------*
* Creates master data for location-product
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_MASTERDATA Master data table
*----------------------------------------------------------------------*
FORM f_create_locprd USING iv_logsys TYPE logsys
                           it_masterdata TYPE t_masterdata.
  DATA:
  lv_dummy TYPE flag VALUE 'X',
  lv_next TYPE flag.
  DATA:
  lt_locprd TYPE t_locprd,
  lt_location TYPE t_location,
  lt_product TYPE t_product,
  lt_prd_extprd TYPE t_prd_extprd,
  lt_loc_extloc TYPE t_loc_extloc.
* Get all possible products
  PERFORM f_collect_products USING it_masterdata
                             CHANGING lt_product
                                      lt_prd_extprd.
* Identify the products which are not there in the data base
  PERFORM f_filter_product CHANGING lt_product
                                    lv_dummy.
* Collect all possible locations
  PERFORM f_collect_locations USING it_masterdata
                              CHANGING lt_location
                                       lt_loc_extloc.
* Identify the locations which are not there in the data base
  PERFORM f_filter_location CHANGING lt_location
                                     lv_dummy.
* Collect possible location-products that need to be created
  PERFORM f_collect_locprd USING  it_masterdata
                                  lt_location
                                  lt_product
                           CHANGING lt_locprd.
  IF lt_locprd IS INITIAL.
    EXIT.
  ENDIF.
* Filter the matlocs to be created
  PERFORM f_filter_locprd CHANGING lt_locprd
                                   lv_next.
  IF lv_next IS NOT INITIAL.
* The Location-product(s) entered for creation already in database
    MESSAGE i022(zfpv).
    EXIT.
  ENDIF.
* Insert the location-products in the data base thru BAPI interface
  PERFORM f_insert_locprd_in_db USING iv_logsys
                                      lt_loc_extloc
                                      lt_prd_extprd
                                CHANGING lt_locprd.
* Update the location-product hierarchy for the location-product
* successfully created
  PERFORM f_update_locprd_hierarchy.
ENDFORM. " f_create_locprd
*&---------------------------------------------------------------------*
*& Form f_collect_locprd
*&---------------------------------------------------------------------*
* Collects all possible location-products
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data table
* -->IT_LOCATION Location table
* -->IT_PRODUCT Product table
* <--ET_LOCPRD Location-product table
*----------------------------------------------------------------------*
FORM f_collect_locprd USING it_masterdata TYPE t_masterdata
                            it_location TYPE t_location
                            it_product TYPE t_product
                      CHANGING et_locprd TYPE t_locprd.
  DATA:
  ls_masterdata TYPE zap_sign.

  LOOP AT it_masterdata INTO ls_masterdata
                        WHERE status = gc_stat_p OR
                              status = gc_stat_a.
* Check if core product is there in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
      WITH KEY matnr = ls_masterdata-pcode
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Core Product already created
      PERFORM f_find_locprd_comb USING  ls_masterdata
                                        gc_core
                                        it_location
                                 CHANGING et_locprd.
    ELSE.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_037
                                        ls_masterdata-pdctr
                                        ls_masterdata-pcode
                                        ls_masterdata-markt
                                        ls_masterdata-pcode
                                        space
                                 CHANGING gt_log_locprd.
    ENDIF. "Entry involving Core product
* Check if product variant is there in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
      WITH KEY matnr = ls_masterdata-prvar
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Product variant already created
      PERFORM f_find_locprd_comb USING  ls_masterdata
                                        gc_var
                                        it_location
                                 CHANGING et_locprd.
    ELSE.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_038
                                        ls_masterdata-pdctr
                                        ls_masterdata-prvar
                                        ls_masterdata-markt
                                        ls_masterdata-prvar
                                        space
                                 CHANGING gt_log_locprd.
    ENDIF. "Entry involving Core product
  ENDLOOP.
  IF et_locprd IS INITIAL.
* The location(s)/product(s) not available in the db for mapping
    MESSAGE i021(zfpv).
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_err
                                      gc_msgno_021
                                      space
                                      space
                                      space
                                      space
                                      space
                               CHANGING gt_log_locprd.
    EXIT.
  ENDIF.
  SORT et_locprd BY locno matnr.
  DELETE ADJACENT DUPLICATES FROM et_locprd
    COMPARING locno matnr.
ENDFORM. " f_collect_locprd
*&---------------------------------------------------------------------*
*& Form f_find_locprd_comb
*&---------------------------------------------------------------------*
* Evaluatres entries for location-products from the sourcing
*----------------------------------------------------------------------*
* -->IS_MASTERDATA Master data table
* -->IV_PRDTP Product type
* <--ET_LOCPRD Location-product table
*----------------------------------------------------------------------*
FORM f_find_locprd_comb USING is_masterdata TYPE zap_sign
                              iv_prdtp TYPE c
                              it_location TYPE t_location
                        CHANGING et_locprd TYPE t_locprd.
  DATA:
  lv_matnr TYPE /sapapo/matnr,
  ls_locprd TYPE s_locprd.

  CASE iv_prdtp.
    WHEN gc_core.
      lv_matnr = is_masterdata-pcode.
    WHEN gc_var.
      lv_matnr = is_masterdata-prvar.
  ENDCASE.
* Check if Source created in the data base
  READ TABLE it_location TRANSPORTING NO FIELDS
    WITH KEY locno = is_masterdata-pdctr
    BINARY SEARCH.
  IF sy-subrc <> 0.
    ls_locprd-locno = is_masterdata-pdctr.
    ls_locprd-matnr = lv_matnr.
    ls_locprd-loctp = gc_ltp_1001.
    ls_locprd-prctp = gc_ptp_x.
    APPEND ls_locprd TO et_locprd.
  ELSE.
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_err
                                      gc_msgno_039
                                      is_masterdata-pdctr
                                      is_masterdata-pcode
                                      is_masterdata-pdctr
                                      space
                                      space
                               CHANGING gt_log_locprd.
  ENDIF.
* Check if Market created in the data base
  READ TABLE it_location TRANSPORTING NO FIELDS
    WITH KEY locno = is_masterdata-markt
    BINARY SEARCH.
  IF sy-subrc <> 0.
    ls_locprd-locno = is_masterdata-markt.
    ls_locprd-matnr = lv_matnr.
    ls_locprd-loctp = gc_ltp_1002.
    ls_locprd-prctp = gc_ptp_f.
    APPEND ls_locprd TO et_locprd.
  ELSE.
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_err
                                      gc_msgno_040
                                      is_masterdata-markt
                                      is_masterdata-pcode
                                      is_masterdata-markt
                                      space
                                      space
                               CHANGING gt_log_locprd.
  ENDIF.
  IF is_masterdata-whse1 IS NOT INITIAL.
    READ TABLE it_location TRANSPORTING NO FIELDS
      WITH KEY locno = is_masterdata-whse1
      BINARY SEARCH.
    IF sy-subrc <> 0.
      ls_locprd-locno = is_masterdata-whse1.
      ls_locprd-matnr = lv_matnr.
      ls_locprd-loctp = gc_ltp_1002.
      ls_locprd-prctp = gc_ptp_f.
      APPEND ls_locprd TO et_locprd.
    ELSE.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_041
                                        is_masterdata-whse1
                                        is_masterdata-pcode
                                        is_masterdata-whse1
                                        space
                                        space
                                 CHANGING gt_log_locprd.
    ENDIF.
  ENDIF.
  IF is_masterdata-whse2 IS NOT INITIAL.
    READ TABLE it_location TRANSPORTING NO FIELDS
      WITH KEY locno = is_masterdata-whse2
      BINARY SEARCH.
    IF sy-subrc <> 0.
      ls_locprd-locno = is_masterdata-whse2.
      ls_locprd-matnr = lv_matnr.
      ls_locprd-loctp = gc_ltp_1002.
      ls_locprd-prctp = gc_ptp_f.
      APPEND ls_locprd TO et_locprd.
    ELSE.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_042
                                        is_masterdata-whse2
                                        is_masterdata-pcode
                                        is_masterdata-whse2
                                        space
                                        space
                                 CHANGING gt_log_locprd.
    ENDIF.
  ENDIF.
ENDFORM. " f_find_locprd_comb
*&---------------------------------------------------------------------*
*& Form f_filter_locprd
*&---------------------------------------------------------------------*
* Filters the location-products to be created
*----------------------------------------------------------------------*
* <--ET_LOCPRD Location-product table
*----------------------------------------------------------------------*
FORM f_filter_locprd CHANGING et_locprd TYPE t_locprd
                              ev_next TYPE flag.
  DATA:
  lt_locprd_cr TYPE t_locprd,
  ls_locprd TYPE s_locprd.
  FIELD-SYMBOLS:
  <lfs_locprd> TYPE s_locprd.
  SELECT locno matnr
    INTO CORRESPONDING FIELDS OF TABLE lt_locprd_cr
    FROM /sapapo/v_matloc
    FOR ALL ENTRIES IN et_locprd
    WHERE locno = et_locprd-locno AND
          matnr = et_locprd-matnr.
  LOOP AT lt_locprd_cr INTO ls_locprd.
    READ TABLE et_locprd ASSIGNING <lfs_locprd>
      WITH KEY  locno = ls_locprd-locno
                matnr = ls_locprd-matnr.
    IF sy-subrc = 0.
      CLEAR: <lfs_locprd>-locno, <lfs_locprd>-matnr.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_warn
                                        gc_msgno_043
                                        ls_locprd-locno
                                        ls_locprd-matnr
                                        space
                                        space
                                        space
                                 CHANGING gt_log_locprd.
    ENDIF.
  ENDLOOP.
  DELETE et_locprd WHERE locno IS INITIAL AND
                         matnr IS INITIAL.
  IF et_locprd IS INITIAL.
    ev_next = gc_x.
  ENDIF.
ENDFORM. " f_filter_locprd
*&---------------------------------------------------------------------*
*& Form f_insert_locprd_in_db
*&---------------------------------------------------------------------*
* Inserts location-prod in the database thru the BAPI interface
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_LOC_EXTLOC Location-ext loc mapping table
* -->IT_PRD_EXTPRD Prod-ext prod mapping table
* <--ET_LOCPRD Location-prod table
*----------------------------------------------------------------------*
FORM f_insert_locprd_in_db USING  iv_logsys TYPE logsys
                                  it_loc_extloc TYPE t_loc_extloc
                                  it_prd_extprd TYPE t_prd_extprd
                           CHANGING et_locprd TYPE t_locprd.
  DATA:
  lt_return TYPE t_return,
  lt_product_location TYPE t_product_location,
  lt_product_location_x TYPE t_product_location_x,
  lt_product_location_version TYPE t_product_location_version,
  lt_product_location_version_x TYPE t_product_location_version_x,
  lt_product_model TYPE t_product_model.
  DATA:
  ls_return TYPE bapiret2.
  FIELD-SYMBOLS:
  <lfs_locprd> TYPE s_locprd.

  LOOP AT et_locprd ASSIGNING <lfs_locprd>.
    REFRESH:  lt_return, lt_product_location, lt_product_location_x,
              lt_product_location_version, lt_product_location_version_x,
              lt_product_model.
    PERFORM f_populate_locprdbapi_table USING
                                          <lfs_locprd>
                                          it_loc_extloc
                                          it_prd_extprd
                                        CHANGING
                                          lt_product_location
                                          lt_product_location_x
                                          lt_product_location_version
                                          lt_product_location_version_x
                                          lt_product_model.
* Call the BAPI to create the location-products
    PERFORM f_call_locprd_bapi USING  iv_logsys
                                      lt_product_location
                                      lt_product_location_x
                                      lt_product_location_version
                                      lt_product_location_version_x
                                      lt_product_model
                               CHANGING lt_return.
* If the location-product is not created successfully, mark it
* for deletion from the table so that is not considered for
* updation of location-product hierarchy
    IF lt_return IS NOT INITIAL.
      CLEAR: <lfs_locprd>-locno, <lfs_locprd>-matnr.
      READ TABLE lt_return INTO ls_return
        WITH KEY type = gc_e.
      IF sy-subrc = 0.
        PERFORM f_update_log_table USING  ls_return-id
                                          ls_return-type
                                          ls_return-number
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message
                                   CHANGING gt_log_locprd.
      ENDIF.
    ELSE.
* Location-Product created successfully
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_succ
                                        gc_msgno_044
                                        <lfs_locprd>-locno
                                        <lfs_locprd>-matnr
                                        space space space
                                 CHANGING gt_log_locprd.
    ENDIF.
* Process the spool with the BAPI return (to be coded)
  ENDLOOP.
  DELETE et_locprd WHERE  locno IS INITIAL AND
                          matnr IS INITIAL.
ENDFORM. " f_insert_locprd_in_db
*&---------------------------------------------------------------------*
*& Form f_populate_locprdbapi_table
*&---------------------------------------------------------------------*
* Populates the location-product BAPI tables
*----------------------------------------------------------------------*
* -->IS_LOCPRD Location-prod structure
* -->IT_LOC_EXTLOC Location-ext loc mapping table
* -->IT_PRD_EXTPRD Prod-ext prod mapping table
* <--ET_PRODUCT_LOCATION Product location
* <--ET_PRODUCT_LOCATION_X Product location x
* <--ET_PRODUCT_LOCATION_VERSION Prod loc version
* <--ET_PRODUCT_LOCATION_VERSION_X Prod loc version x
* <--ET_PRODUCT_MODEL Product model
*----------------------------------------------------------------------*
FORM f_populate_locprdbapi_table USING  is_locprd TYPE s_locprd
                                        it_loc_extloc TYPE t_loc_extloc
                                        it_prd_extprd TYPE t_prd_extprd
CHANGING et_product_location TYPE t_product_location
                                  et_product_location_x TYPE t_product_location_x
                                  et_product_location_version TYPE t_product_location_version
                                  et_product_location_version_x TYPE t_product_location_version_x
                                  et_product_model TYPE t_product_model.
  DATA:
  ls_loc_extloc TYPE s_loc_extloc,
  ls_prd_extprd TYPE s_prd_extprd,
  ls_product_location TYPE bapi10001locprd2,
  ls_product_location_x TYPE bapi10001locprd2x,
  ls_product_location_version TYPE bapi10001locprdvers2,
  ls_product_location_version_x TYPE bapi10001locprdvers2x,
  ls_product_model TYPE bapi10001locprdmodel2.

  READ TABLE it_loc_extloc INTO ls_loc_extloc
    WITH KEY locno = is_locprd-locno
    BINARY SEARCH.
  IF sy-subrc = 0.
* Populating the external location
    ls_product_location-location = ls_loc_extloc-extloc.
    ls_product_location_version-location = ls_loc_extloc-extloc.
    ls_product_model-location = ls_loc_extloc-extloc.
    ls_product_location_x-location = ls_loc_extloc-extloc.
    ls_product_location_version_x-location = ls_loc_extloc-extloc.
  ENDIF.
  READ TABLE it_prd_extprd INTO ls_prd_extprd
    WITH KEY matnr = is_locprd-matnr
    BINARY SEARCH.
  IF sy-subrc = 0.
* Populating the external product
    ls_product_location-product = ls_prd_extprd-extmat.
    ls_product_location_version-product = ls_prd_extprd-extmat.
    ls_product_model-product = ls_prd_extprd-extmat.
    ls_product_location_x-product = ls_prd_extprd-extmat.
    ls_product_location_version_x-product = ls_prd_extprd-extmat.
  ENDIF.
* Populating the location type
  ls_product_location-loctype = is_locprd-loctp.
  ls_product_location_version-loctype = is_locprd-loctp.
  ls_product_model-loctype = is_locprd-loctp.
  ls_product_location_x-loctype = is_locprd-loctp.
  ls_product_location_version_x-loctype = is_locprd-loctp.
* Populating the Procurement type
  ls_product_location_version-procurement_type = is_locprd-prctp.
  ls_product_location_version_x-procurement_type = gc_x.
* Populating the model
  ls_product_model-model = gc_000.
* Appending the structures to the tables
  APPEND: ls_product_location TO et_product_location,
  ls_product_location_x TO et_product_location_x,
  ls_product_location_version TO et_product_location_version,
  ls_product_location_version_x TO
  et_product_location_version_x,
  ls_product_model TO et_product_model.
ENDFORM. " f_populate_locprdbapi_table
*&---------------------------------------------------------------------*
*& Form f_call_locprd_bapi
*&---------------------------------------------------------------------*
* Creates the location-product
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_PRODUCT_LOCATION Prod location
* -->IT_PRODUCT_LOCATION_X Prod location x
* -->IT_PRODUCT_LOCATION_VERSION Prod location version
* -->IT_PRODUCT_LOCATION_VERSION_X Prod location version x
* -->IT_PRODUCT_MODEL Prod model
* <--ET_RETURN BAPI return table
*----------------------------------------------------------------------*
FORM f_call_locprd_bapi USING iv_logsys TYPE logsys
                              it_product_location TYPE t_product_location
                              it_product_location_x TYPE t_product_location_x
                              it_product_location_version TYPE t_product_location_version
                              it_product_location_version_x TYPE t_product_location_version_x
                              it_product_model TYPE t_product_model
                        CHANGING et_return TYPE t_return.
  DATA:
  lv_err TYPE flag.

  CALL FUNCTION 'BAPI_PRDSRVAPS_SAVEMULTI2'
    EXPORTING
      logical_system             = iv_logsys
      commit_control             = space
    TABLES
      product_location           = it_product_location
      product_location_x         = it_product_location_x
      product_location_version   = it_product_location_version
      product_location_version_x = it_product_location_version_x
      product_model              = it_product_model
      return                     = et_return.

  LOOP AT et_return TRANSPORTING NO FIELDS
  WHERE type = gc_err.
    lv_err = gc_x.
    EXIT.
  ENDLOOP.
  IF lv_err IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = gc_x.
  ENDIF.
ENDFORM. " f_call_locprd_bapi
*&---------------------------------------------------------------------*
*& Form f_create_tlane
*&---------------------------------------------------------------------*
* Creates master data for transportation lane
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_MASTERDATA Master data table
*----------------------------------------------------------------------*
FORM f_create_tlane USING iv_logsys TYPE logsys
                          it_masterdata TYPE t_masterdata.
  DATA:
  lv_dummy TYPE flag VALUE 'X'.
  DATA:
  lt_tlane TYPE t_tlane,
  lt_tlane_new TYPE t_tlane,
  lt_tlane_dat TYPE t_tlane,
  lt_location TYPE t_location,
  lt_product TYPE t_product,
  lt_prd_extprd TYPE t_prd_extprd,
  lt_loc_extloc TYPE t_loc_extloc.

* Get all possible products
  PERFORM f_collect_products USING it_masterdata
                             CHANGING lt_product
  lt_prd_extprd.
* Identify the products which are not there in the data base
  PERFORM f_filter_product CHANGING lt_product
                                    lv_dummy.
* Collect all possible locations
  PERFORM f_collect_locations USING it_masterdata
                              CHANGING lt_location
                                       lt_loc_extloc.
* Identify the locations which are not there in the data base
  PERFORM f_filter_location CHANGING lt_location
                                     lv_dummy.
* Collect all possible transportation lanes
  PERFORM f_collect_tlanes USING  it_masterdata
                                  lt_location
                                  lt_product
                           CHANGING lt_tlane.
  IF lt_tlane IS INITIAL.
    EXIT.
  ENDIF.
* The Tlanes can be categorised into 3 categories:
* 1> To be created fresh
* 2> Exists, but new product need to be assigned
* 3> Only validity dates need to be changed
  PERFORM f_categorise_tlanes USING lt_tlane
                              CHANGING lt_tlane_new
                                       lt_tlane_dat.
* Insert the tlanes into the database thru the BAPI interface
  PERFORM f_insert_tlane_in_db USING  iv_logsys
                                      lt_tlane_new
                                      lt_tlane_dat
                                      lt_loc_extloc
                                      lt_prd_extprd.
ENDFORM. " f_create_tlane
*&---------------------------------------------------------------------*
*& Form f_collect_tlanes
*&---------------------------------------------------------------------*
* Collects all possible transportation lanes
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data table
* -->IT_LOCATION Location table* -->IT_PRODUCT Product table
* <--ET_TLANE Transportation lane table
*----------------------------------------------------------------------*
FORM f_collect_tlanes USING it_masterdata TYPE t_masterdata
                            it_location TYPE t_location
                            it_product TYPE t_product
                      CHANGING et_tlane TYPE t_tlane.
  DATA:
  lv_dtfrmin TYPE timestamp,
  lv_dttomax TYPE timestamp,
  lv_tabix TYPE sytabix,
  lv_locfr TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno,
  lv_matnr TYPE /sapapo/matnr.
  DATA:
  ls_masterdata TYPE zap_sign.
  FIELD-SYMBOLS:
  <lfs_tlanetmp> TYPE s_tlane,
  <lfs_tlane> TYPE s_tlane.

  LOOP AT it_masterdata INTO ls_masterdata
                        WHERE status = gc_stat_p OR
                              status = gc_stat_a.
* Check if the core product exists in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
      WITH KEY matnr = ls_masterdata-pcode
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Core product exists, find the tlanes involving it
      PERFORM f_find_tlane_comb USING ls_masterdata
                                      gc_core
                                      it_location
                                CHANGING et_tlane.
    ELSE.
* Product doesnt exist in database, update log
      lv_locfr = ls_masterdata-pdctr.
      lv_locto = ls_masterdata-markt.
      lv_matnr = ls_masterdata-pcode.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_028
                                        lv_locfr lv_locto lv_matnr
                                        space space
                                 CHANGING gt_log_tlane.
    ENDIF.
* Check if the variant exists in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
      WITH KEY matnr = ls_masterdata-prvar
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Variant exists, find the tlanes involving it
      PERFORM f_find_tlane_comb USING ls_masterdata
                                      gc_var
                                      it_location
                                CHANGING et_tlane.
    ELSE.
* Product doesnt exist in database, update log
      lv_locfr = ls_masterdata-pdctr.
      lv_locto = ls_masterdata-markt.
      lv_matnr = ls_masterdata-prvar.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_029
                                        lv_locfr lv_locto lv_matnr
                                        space space
                                 CHANGING gt_log_tlane.
    ENDIF.
  ENDLOOP.
  IF et_tlane IS INITIAL.
* The location(s)/product(s) not available in the db for tlane creation
    MESSAGE i023(zfpv).
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_err
                                      gc_msgno_023
                                      space space space
                                      space space
                               CHANGING gt_log_tlane.
    EXIT.
  ENDIF.
  SORT et_tlane BY locfr locto matnr.
  READ TABLE et_tlane ASSIGNING <lfs_tlane> INDEX 1.
  lv_dtfrmin = <lfs_tlane>-datfr.
  lv_dttomax = <lfs_tlane>-datto.

  LOOP AT et_tlane ASSIGNING <lfs_tlane>.
    IF <lfs_tlane>-datfr < lv_dtfrmin.
      lv_dtfrmin = <lfs_tlane>-datfr.
    ENDIF.
    IF <lfs_tlane>-datto > lv_dttomax.
      lv_dttomax = <lfs_tlane>-datto.
    ENDIF.
    AT END OF matnr.
      <lfs_tlane>-datfr = lv_dtfrmin.
      <lfs_tlane>-datto = lv_dttomax.
      lv_tabix = sy-tabix + 1.
      READ TABLE et_tlane ASSIGNING <lfs_tlanetmp> INDEX lv_tabix.
      IF sy-subrc = 0.
        lv_dtfrmin = <lfs_tlanetmp>-datfr.
        lv_dttomax = <lfs_tlanetmp>-datto.
      ENDIF.
    ENDAT.
  ENDLOOP.
  SORT et_tlane BY locfr locto matnr datfr ASCENDING datto DESCENDING.
  DELETE ADJACENT DUPLICATES FROM et_tlane
  COMPARING locfr locto matnr.
* Get the internal guids for material and locations
  LOOP AT et_tlane ASSIGNING <lfs_tlane>.
    PERFORM f_get_locid_from_locno  USING <lfs_tlane>-locfr
                                    CHANGING <lfs_tlane>-lidfr.
    PERFORM f_get_locid_from_locno  USING <lfs_tlane>-locto
                                    CHANGING <lfs_tlane>-lidto.
    PERFORM f_get_matid_from_matnr  USING <lfs_tlane>-matnr
                                    CHANGING <lfs_tlane>-matid.
  ENDLOOP.
ENDFORM. " f_collect_tlanes
*&---------------------------------------------------------------------*
*& Form f_find_tlane_comb
*&---------------------------------------------------------------------*
* Finds tlanes involving a product
*----------------------------------------------------------------------*
* -->IS_MASTERDATA Masterdata table
* -->IV_PRDTP Prioduct type
* -->IT_LOCATION Location table
* <--ET_TLANE Tlane table
*----------------------------------------------------------------------*
FORM f_find_tlane_comb USING  is_masterdata TYPE zap_sign
                              iv_prdtp TYPE c
                              it_location TYPE t_location
                       CHANGING et_tlane TYPE t_tlane.
  DATA:
  lv_matnr TYPE /sapapo/matnr,
  lv_locfr TYPE /sapapo/locno,
  lv_locwh TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno.

  CASE iv_prdtp.
    WHEN gc_core.
      lv_matnr = is_masterdata-pcode.
    WHEN gc_var.
      lv_matnr = is_masterdata-prvar.
  ENDCASE.
* First see if the Source and Market exist in the database.
* Check the presence of warehouses in the sourcing. We assume that
* Warehouse2 cannot be present in absence of Warehouse1.
  READ TABLE it_location TRANSPORTING NO FIELDS
    WITH KEY locno = is_masterdata-markt
    BINARY SEARCH.
  IF sy-subrc <> 0.
* Market exists in the data base
    READ TABLE it_location TRANSPORTING NO FIELDS
      WITH KEY locno = is_masterdata-pdctr
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Source exists in the data base
      IF is_masterdata-whse1 IS INITIAL.
* No warehouse in the route, create a lane between Source and market
        lv_locfr = is_masterdata-pdctr.
        lv_locto = is_masterdata-markt.
        PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                    gc_ltp_1001 gc_ltp_1002
                                    is_masterdata
                              CHANGING et_tlane.
      ELSE.
* Warehouse1 exists in the route
        READ TABLE it_location TRANSPORTING NO FIELDS
          WITH KEY locno = is_masterdata-whse1
          BINARY SEARCH.
        IF sy-subrc <> 0.
* Warehouse1 exists in the data base
          IF is_masterdata-whse2 IS INITIAL.
* No Warehouse2 exists, create a lane between source and Warehouse1 and
* one between Warehouse1 to Market
            lv_locfr = is_masterdata-pdctr.
            lv_locto = is_masterdata-whse1.
            PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                        gc_ltp_1001 gc_ltp_1002
                                        is_masterdata
                                  CHANGING et_tlane.
            lv_locfr = is_masterdata-whse1.
            lv_locto = is_masterdata-markt.
            PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                        gc_ltp_1002 gc_ltp_1002
                                        is_masterdata
                                  CHANGING et_tlane.
          ELSE.
* Warehouse2 exists in the route
            READ TABLE it_location TRANSPORTING NO FIELDS
              WITH KEY locno = is_masterdata-whse2
              BINARY SEARCH.
            IF sy-subrc <> 0.
* Warehouse2 exists in the database, create 3 lanes: Source to
* Warehouse1, Warehouse1 to Warehouse2, Warehouse2 to Market
              lv_locfr = is_masterdata-pdctr.
              lv_locto = is_masterdata-whse1.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                          gc_ltp_1001 gc_ltp_1002
                                          is_masterdata
                                    CHANGING et_tlane.
              lv_locfr = is_masterdata-whse1.
              lv_locto = is_masterdata-whse2.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                          gc_ltp_1002 gc_ltp_1002
                                          is_masterdata
                                    CHANGING et_tlane.
              lv_locfr = is_masterdata-whse2.
              lv_locto = is_masterdata-markt.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                          gc_ltp_1002 gc_ltp_1002
                                          is_masterdata
                                    CHANGING et_tlane.
            ELSE.
* Warehouse2 is yet to be created in the database, update log
              lv_locfr = is_masterdata-pdctr.
              lv_locwh = is_masterdata-whse1.
              lv_locto = is_masterdata-whse2.
              PERFORM f_update_log_table USING  gc_msgid
                                                gc_err
                                                gc_msgno_033
                                                lv_locfr lv_locwh
                                                lv_locto lv_matnr
                                                space
                                         CHANGING gt_log_tlane.
            ENDIF.
          ENDIF.
        ELSE.
* Warehouse1 is yet to be created in the database, update log
          lv_locfr = is_masterdata-pdctr.
          lv_locto = is_masterdata-whse1.
          PERFORM f_update_log_table USING  gc_msgid
                                            gc_err
                                            gc_msgno_032
                                            lv_locfr lv_locto
                                            lv_matnr space
                                            space
                                     CHANGING gt_log_tlane.
        ENDIF.
      ENDIF.
    ELSE.
* Source is yet to be created in the database, update log
      lv_locfr = is_masterdata-pdctr.
      lv_locto = is_masterdata-markt.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_031
                                        lv_locfr lv_locto
                                        lv_matnr space
                                        space
                                 CHANGING gt_log_tlane.
    ENDIF.
  ELSE.
* Market is yet to be created in the database, update log
    lv_locfr = is_masterdata-pdctr.
    lv_locto = is_masterdata-markt.
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_err
                                      gc_msgno_030
                                      lv_locfr lv_locto
                                      lv_matnr space
                                      space
                               CHANGING gt_log_tlane.
  ENDIF.
ENDFORM. " f_find_tlane_comb
*&---------------------------------------------------------------------*
*& Form f_get_tstamps
*&---------------------------------------------------------------------*
* Gets the timestamps for the Tlane validity dates
*----------------------------------------------------------------------*
* -->IS_MASTERDATA Master data table
* <--EV_DATFR Valid from tstamp
* <--EV_DATTO Valid to tstamp
*----------------------------------------------------------------------*
FORM f_get_tstamps USING is_masterdata TYPE zap_sign
                   CHANGING ev_datfr TYPE timestamp
                            ev_datto TYPE timestamp.
  CONVERT DATE is_masterdata-datab TIME gc_startti
    INTO TIME STAMP ev_datfr TIME ZONE gc_utc.
  CONVERT DATE is_masterdata-datbi TIME gc_endti
    INTO TIME STAMP ev_datto TIME ZONE gc_utc.
ENDFORM. " f_get_tstamps
*&---------------------------------------------------------------------*
*& Form f_build_tlane
*&---------------------------------------------------------------------*
* Builds a Tlane
*----------------------------------------------------------------------*
* -->IV_LOCFR Location from
* -->IV_LOCTO Location to
* -->IV_MATNR Product
* -->IV_LTPFR Loctype from
* -->IV_LTPTO Loctype to
* -->IS_MASTERDATA Master data
* <--ET_TLANE Tlane table
*----------------------------------------------------------------------*
FORM f_build_tlane USING  iv_locfr TYPE /sapapo/locno
                          iv_locto TYPE /sapapo/locno
                          iv_matnr TYPE /sapapo/matnr
                          iv_ltpfr TYPE /sapapo/c_loctype
                          iv_ltpto TYPE /sapapo/c_loctype
                          is_masterdata TYPE zap_sign
                   CHANGING et_tlane TYPE t_tlane.
  DATA:
  ls_tlane TYPE s_tlane.
  ls_tlane-locfr = iv_locfr.
  ls_tlane-locto = iv_locto.
  ls_tlane-matnr = iv_matnr.
  ls_tlane-ltpfr = iv_ltpfr.
  ls_tlane-ltpto = iv_ltpto.
  PERFORM f_get_tstamps USING is_masterdata
    CHANGING ls_tlane-datfr ls_tlane-datto.
  APPEND ls_tlane TO et_tlane.
ENDFORM. " f_build_tlane
*&---------------------------------------------------------------------*
*& Form f_categorise_tlanes
*&---------------------------------------------------------------------*
* Categorises Transp lanes
*----------------------------------------------------------------------*
* -->IT_TLANE Tlane table
* <--ET_TLANE_NEW Tlane table with new entries
* <--ET_TLANE_DAT Tlane table with date changes
*----------------------------------------------------------------------*
FORM f_categorise_tlanes USING it_tlane TYPE t_tlane
                         CHANGING et_tlane_new TYPE t_tlane
                                  et_tlane_dat TYPE t_tlane.
  DATA:
  lt_tlane_cr TYPE STANDARD TABLE OF /sapapo/trprod,
  ls_tlane_cr TYPE /sapapo/trprod,                          "#EC NEEDED
  ls_tlane_tmp TYPE s_tlane,
  ls_tlane TYPE s_tlane.
* The table /SAPAPO/TRPROD the key field is /SAPAPO/TRPID.
* But over here we are retrieving data from it with respect to
* the source and destination which anyway is unique for a given
* product involved in the lane.
  SELECT locfr locto matid
    INTO CORRESPONDING FIELDS OF TABLE lt_tlane_cr
    FROM /sapapo/trprod
    FOR ALL ENTRIES IN it_tlane
    WHERE locfr = it_tlane-lidfr AND
          locto = it_tlane-lidto.
  IF sy-subrc = 0.
    SORT lt_tlane_cr BY locfr locto matid.
  ENDIF.
  LOOP AT it_tlane INTO ls_tlane.
    READ TABLE lt_tlane_cr INTO ls_tlane_cr
      WITH KEY  locfr = ls_tlane-lidfr
                locto = ls_tlane-lidto
                matid = ls_tlane-matid
      BINARY SEARCH.
    IF sy-subrc = 0.
      APPEND ls_tlane TO et_tlane_dat.
    ELSE.
* Check whether a product needs to be assigned to an existing
* Tlane
      READ TABLE lt_tlane_cr INTO ls_tlane_cr
        WITH KEY locfr = ls_tlane-lidfr
                 locto = ls_tlane-lidto
        BINARY SEARCH.
      IF sy-subrc = 0.
* Mark the record to denote assignment of prod to existing
* Tlane
        ls_tlane-crtyp = gc_x.
      ENDIF.
      IF ls_tlane-locfr = ls_tlane_tmp-locfr AND
      ls_tlane-locto = ls_tlane_tmp-locto.
        ls_tlane-crtyp = gc_x.
      ENDIF.
      APPEND ls_tlane TO et_tlane_new.
    ENDIF.
    ls_tlane_tmp = ls_tlane.
    CLEAR ls_tlane.
  ENDLOOP.
ENDFORM. " f_categorise_tlanes
*&---------------------------------------------------------------------*
*& Form f_get_locid_from_locno
*&---------------------------------------------------------------------*
* Gets the guid for locations
*----------------------------------------------------------------------*
* -->IV_LOCNO Location no
* <--EV_LOCID Location id
*----------------------------------------------------------------------*
FORM f_get_locid_from_locno USING iv_locno TYPE /sapapo/locno
                            CHANGING ev_locid TYPE /sapapo/locid.
  CALL FUNCTION '/SAPAPO/DM_LOCNO_GET_LOCID'
    EXPORTING
      iv_locno           = iv_locno
    IMPORTING
      ev_locid           = ev_locid
    EXCEPTIONS
      location_not_found = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_get_locid_from_locno
*&---------------------------------------------------------------------*
*& Form f_get_matid_from_matnr
*&---------------------------------------------------------------------*
* Gets the guid for material
*----------------------------------------------------------------------*
* -->IV_MATNR Material no
* <--EV_MATID Material id
*----------------------------------------------------------------------*
FORM f_get_matid_from_matnr USING iv_matnr TYPE /sapapo/matnr
                            CHANGING ev_matid TYPE /sapapo/matid.
  CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
    EXPORTING
      iv_matnr        = iv_matnr
    IMPORTING
      ev_matid        = ev_matid
    EXCEPTIONS
      matid_not_found = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_get_matid_from_matnr
*&---------------------------------------------------------------------*
*& Form f_insert_tlane_in_db
*&---------------------------------------------------------------------*
* Inserts transportation lanes in the data base
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_TLANE_NEW Tlanes to be created/updated thru BAPI
* -->IT_TLANE_DAT Tlanes whose validity dates need to be changed
* -->IT_LOC_EXTLOC Loc-ext loc mapping table
* -->IT_PRD_EXTPRD Prd-ext prd mapping table
*----------------------------------------------------------------------*
FORM f_insert_tlane_in_db USING iv_logsys TYPE logsys
                                it_tlane_new TYPE t_tlane
                                it_tlane_dat TYPE t_tlane
                                it_loc_extloc TYPE t_loc_extloc
                                it_prd_extprd TYPE t_prd_extprd.
  DATA:
  lt_return TYPE t_return,
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_transport_lane TYPE t_transport_lane,
  lt_transport_lanex TYPE t_transport_lanex,
  lt_prod_procurement TYPE t_prod_procurement,
  lt_prod_procurementx TYPE t_prod_procurementx,
  lt_means_of_transport TYPE t_means_of_transport,
  lt_means_of_transportx TYPE t_means_of_transportx.
  DATA:
  ls_tlane TYPE s_tlane,
  ls_return TYPE bapiret2.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.

* Creating new tlanes/assigning prods to existing tlanes
  LOOP AT it_tlane_new INTO ls_tlane.
    IF ls_tlane-datfr > ls_tlane-datto.
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_078
                                        ls_tlane-locfr
                                        ls_tlane-locto
                                        ls_tlane-matnr space
                                        space
                                 CHANGING gt_log_tlane.
      CONTINUE.
    ENDIF.
    REFRESH:  lt_return, lt_transport_lane, lt_transport_lanex,
              lt_prod_procurement, lt_prod_procurementx,
              lt_means_of_transport, lt_means_of_transportx.
    PERFORM f_populate_tlane_bapi USING ls_tlane
                                        it_loc_extloc
                                        it_prd_extprd
                                  CHANGING  lt_transport_lane
                                            lt_transport_lanex
                                            lt_prod_procurement
                                            lt_prod_procurementx
                                            lt_means_of_transport
                                            lt_means_of_transportx.
* Call the BAPI to create/update transportation lanes
    PERFORM f_call_tlane_bapi USING iv_logsys
                                    lt_transport_lane
                                    lt_transport_lanex
                                    lt_prod_procurement
                                    lt_prod_procurementx
                                    lt_means_of_transport
                                    lt_means_of_transportx
                              CHANGING lt_return.
    IF lt_return IS NOT INITIAL.
      READ TABLE lt_return INTO ls_return
        WITH KEY type = gc_e.
      IF sy-subrc = 0.
        PERFORM f_update_log_table USING  ls_return-id
                                          ls_return-type
                                          ls_return-number
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message_v1
                                          ls_return-message
                                   CHANGING gt_log_tlane.
      ELSE.
* Tlane created successfully
        IF ls_tlane-crtyp IS INITIAL.
          PERFORM f_update_log_table USING  gc_msgid
                                            gc_succ
                                            gc_msgno_034
                                            ls_tlane-locfr ls_tlane-locto
                                            ls_tlane-matnr space
                                            space
                                     CHANGING gt_log_tlane.
        ELSE.
          PERFORM f_update_log_table USING  gc_msgid
                                            gc_succ
                                            gc_msgno_061
                                            ls_tlane-matnr
                                            ls_tlane-locfr ls_tlane-locto
                                            space space
                                     CHANGING gt_log_tlane.
        ENDIF.
      ENDIF.
    ELSE.
* Tlane created successfully
      IF ls_tlane-crtyp IS INITIAL.
        PERFORM f_update_log_table USING  gc_msgid
                                          gc_succ
                                          gc_msgno_034
                                          ls_tlane-locfr ls_tlane-locto
                                          ls_tlane-matnr space
                                          space
                                   CHANGING gt_log_tlane.
      ELSE.
        PERFORM f_update_log_table USING  gc_msgid
                                          gc_succ
                                          gc_msgno_061
                                          ls_tlane-matnr
                                          ls_tlane-locfr ls_tlane-locto
                                          space space
                                   CHANGING gt_log_tlane.
      ENDIF.
    ENDIF.
  ENDLOOP.
* Updating the validation dates of existing tlanes.
  IF it_tlane_dat IS INITIAL.
    EXIT.
  ENDIF.
  SELECT * INTO TABLE lt_trprod
    FROM /sapapo/trprod
    FOR ALL ENTRIES IN it_tlane_dat
    WHERE locfr = it_tlane_dat-lidfr AND
          locto = it_tlane_dat-lidto AND
          matid = it_tlane_dat-matid.
  IF sy-subrc = 0.
    LOOP AT lt_trprod ASSIGNING <lfs_trprod>.
      READ TABLE it_tlane_dat INTO ls_tlane
        WITH KEY  lidfr = <lfs_trprod>-locfr
                  lidto = <lfs_trprod>-locto
                  matid = <lfs_trprod>-matid.
      IF sy-subrc = 0.
        <lfs_trprod>-valfr = ls_tlane-datfr.
        <lfs_trprod>-valto = ls_tlane-datto.
      ENDIF.
    ENDLOOP.
  ENDIF.
  MODIFY /sapapo/trprod FROM TABLE lt_trprod.
  IF sy-subrc = 0.
    COMMIT WORK.
    LOOP AT it_tlane_dat INTO ls_tlane.
* Tlane created successfully
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_succ
                                        gc_msgno_035
                                        ls_tlane-locfr ls_tlane-locto
                                        ls_tlane-matnr space
                                        space
                                 CHANGING gt_log_tlane.
    ENDLOOP.
  ELSE.
    ROLLBACK WORK.
  ENDIF.
ENDFORM. " f_insert_tlane_in_db
*&---------------------------------------------------------------------*
*& Form f_populate_tlane_bapi
*&---------------------------------------------------------------------*
* Populates the BAPI tables for tlanes
*----------------------------------------------------------------------*
* -->IS_TLANE Tlane structure
* -->IT_LOC_EXTLOC Loc-ext loc mapping table
* -->IT_PRD_EXTPRD Prd-ext prd mapping table
* <--ET_TRANSPORT_LANE Tlane table
* <--ET_TRANSPORT_LANEX Tlane table x
* <--ET_PROD_PROCUREMENT Prod procurement table
* <--ET_PROD_PROCUREMENTX Prod procurement table x
* <--ET_MEANS_OF_TRANSPORT Means of transport table
* <--ET_MEANS_OF_TRANSPORTX Means of transport table x
*----------------------------------------------------------------------*
FORM f_populate_tlane_bapi USING  is_tlane TYPE s_tlane
                                  it_loc_extloc TYPE t_loc_extloc
                                  it_prd_extprd TYPE t_prd_extprd
                           CHANGING et_transport_lane TYPE t_transport_lane
                                    et_transport_lanex TYPE t_transport_lanex
                                    et_prod_procurement TYPE t_prod_procurement
                                    et_prod_procurementx TYPE t_prod_procurementx
                                    et_means_of_transport TYPE t_means_of_transport
                                    et_means_of_transportx TYPE t_means_of_transportx.
  CONSTANTS:
  lc_stdt TYPE sydatum VALUE '20010101',
  lc_endt TYPE sydatum VALUE '29991231'.
  DATA:
  ls_loc_extloc TYPE s_loc_extloc,
  ls_prd_extprd TYPE s_prd_extprd,
  ls_transport_lane TYPE bapi11201trlane,
  ls_transport_lanex TYPE bapi11201trlanex,
  ls_prod_procurement TYPE bapi11201sources,
  ls_prod_procurementx TYPE bapi11201sourcesx,
  ls_means_of_transport TYPE bapi11201meansoftransp,
  ls_means_of_transportx TYPE bapi11201meansoftranspx.

  READ TABLE it_loc_extloc INTO ls_loc_extloc
    WITH KEY locno = is_tlane-locfr
    BINARY SEARCH.
  IF sy-subrc = 0.
* Populating the from location
    ls_transport_lane-location_from = ls_loc_extloc-extloc.
    ls_transport_lanex-location_from = ls_loc_extloc-extloc.
    ls_prod_procurement-location_from = ls_loc_extloc-extloc.
    ls_prod_procurementx-location_from = ls_loc_extloc-extloc.
    IF is_tlane-crtyp IS INITIAL.
      ls_means_of_transport-location_from = ls_loc_extloc-extloc.
      ls_means_of_transportx-location_from = ls_loc_extloc-extloc.
    ENDIF.
  ENDIF.
  READ TABLE it_loc_extloc INTO ls_loc_extloc
    WITH KEY locno = is_tlane-locto
    BINARY SEARCH.
  IF sy-subrc = 0.
* Populating the from location
    ls_transport_lane-location_to = ls_loc_extloc-extloc.
    ls_transport_lanex-location_to = ls_loc_extloc-extloc.
    ls_prod_procurement-location_to = ls_loc_extloc-extloc.
    ls_prod_procurementx-location_to = ls_loc_extloc-extloc.
    IF is_tlane-crtyp IS INITIAL.
      ls_means_of_transport-location_to = ls_loc_extloc-extloc.
      ls_means_of_transportx-location_to = ls_loc_extloc-extloc.
    ENDIF.
  ENDIF.
  READ TABLE it_prd_extprd INTO ls_prd_extprd
    WITH KEY matnr = is_tlane-matnr
    BINARY SEARCH.
  IF sy-subrc = 0.
* Populating the product
    ls_prod_procurement-product = ls_prd_extprd-extmat.
    ls_prod_procurementx-product = ls_prd_extprd-extmat.
  ENDIF.
* Populating the location type from
  ls_transport_lane-loctype_loc_from = is_tlane-ltpfr.
  ls_transport_lanex-loctype_loc_from = is_tlane-ltpfr.
  ls_prod_procurement-loctype_loc_from = is_tlane-ltpfr.
  ls_prod_procurementx-loctype_loc_from = is_tlane-ltpfr.
  IF is_tlane-crtyp IS INITIAL.
    ls_means_of_transport-loctype_loc_from = is_tlane-ltpfr.
    ls_means_of_transportx-loctype_loc_from = is_tlane-ltpfr.
  ENDIF.
* Populating the location type to
  ls_transport_lane-loctype_loc_to = is_tlane-ltpto.
  ls_transport_lanex-loctype_loc_to = is_tlane-ltpto.
  ls_prod_procurement-loctype_loc_to = is_tlane-ltpto.
  ls_prod_procurementx-loctype_loc_to = is_tlane-ltpto.
  IF is_tlane-crtyp IS INITIAL.
    ls_means_of_transport-loctype_loc_to = is_tlane-ltpto.
    ls_means_of_transportx-loctype_loc_to = is_tlane-ltpto.
  ENDIF.
* Populating the validity dates for tlanes
  ls_prod_procurement-valfr = is_tlane-datfr.
  ls_prod_procurementx-valfr = is_tlane-datfr.
  ls_prod_procurement-valto = is_tlane-datto.
  ls_prod_procurementx-valto = is_tlane-datto.
  IF is_tlane-crtyp IS INITIAL.
    CONVERT DATE lc_stdt TIME gc_startti
      INTO TIME STAMP ls_means_of_transport-valfr TIME ZONE gc_utc.
    CONVERT DATE lc_endt TIME gc_endti
      INTO TIME STAMP ls_means_of_transport-valto TIME ZONE gc_utc.
* ls_means_of_transport-valfr = is_tlane-datfr.
    ls_means_of_transportx-valfr = ls_means_of_transport-valfr.
* ls_means_of_transport-valto = is_tlane-datto.
    ls_means_of_transportx-valto = ls_means_of_transport-valto.
  ENDIF.
* Populating the means of transport and other flags
  IF is_tlane-crtyp IS INITIAL.
    ls_means_of_transport-ttype = gc_ttype_0001.
    ls_means_of_transportx-ttype = gc_ttype_0001.
    ls_means_of_transport-valid_all_prds_flg = gc_x.
    ls_means_of_transportx-valid_all_prds_flg = gc_x.
    ls_means_of_transport-aggt_tpl_flg = gc_x.
    ls_means_of_transportx-aggt_tpl_flg = gc_x.
    ls_means_of_transport-dtlt_tpl_flg = gc_x.
    ls_means_of_transportx-dtlt_tpl_flg = gc_x.
  ENDIF.
* Appending the structures to the respective tables
  APPEND: ls_transport_lane TO et_transport_lane,
  ls_transport_lanex TO et_transport_lanex,
  ls_prod_procurement TO et_prod_procurement,
  ls_prod_procurementx TO et_prod_procurementx.
  IF is_tlane-crtyp IS INITIAL.
    APPEND: ls_means_of_transport TO et_means_of_transport,
    ls_means_of_transportx TO et_means_of_transportx.
  ENDIF.
ENDFORM. " f_populate_tlane_bapi
*&---------------------------------------------------------------------*
*& Form f_call_tlane_bapi
*&---------------------------------------------------------------------*
* Calls BAPI to create/update tlanes
*----------------------------------------------------------------------*
* -->IV_LOGSYS Logical system
* -->IT_TRANSPORT_LANE Tlane table
* -->IT_TRANSPORT_LANEX Tlane table x
* -->IT_PROD_PROCUREMENT Prod procurement table
* -->IT_PROD_PROCUREMENTX Prod procurement table x
* -->IT_MEANS_OF_TRANSPORT Means of transport table
* -->IT_MEANS_OF_TRANSPORTX Means of transport table x
* <--ET_RETURN BAPI Return table
*----------------------------------------------------------------------*
FORM f_call_tlane_bapi USING  iv_logsys TYPE logsys
                              it_transport_lane TYPE t_transport_lane
                              it_transport_lanex TYPE t_transport_lanex
                              it_prod_procurement TYPE t_prod_procurement
                              it_prod_procurementx TYPE t_prod_procurementx
                              it_means_of_transport TYPE t_means_of_transport
                              it_means_of_transportx TYPE t_means_of_transportx
                       CHANGING et_return TYPE t_return.
  DATA:
  lv_err TYPE flag.

  CALL FUNCTION 'BAPI_TRLSRVAPS_SAVEMULTI'
    EXPORTING
      logical_system        = iv_logsys
      business_system_group = gc_bsgapo
      model                 = gc_000
      commit_control        = space
    TABLES
      transport_lane        = it_transport_lane
      transport_lanex       = it_transport_lanex
      prod_procurement      = it_prod_procurement
      prod_procurementx     = it_prod_procurementx
      means_of_transport    = it_means_of_transport
      means_of_transportx   = it_means_of_transportx
      return                = et_return.
  LOOP AT et_return TRANSPORTING NO FIELDS
                    WHERE type = gc_err.
    lv_err = gc_x.
    EXIT.
  ENDLOOP.
  IF lv_err IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = gc_x.
  ENDIF.
ENDFORM. " f_call_tlane_bapi
*&---------------------------------------------------------------------*
*& Form f_get_extloc_from_loc
*&---------------------------------------------------------------------*
* Get ext locno from locno
*----------------------------------------------------------------------*
* -->IV_LOCNO Locno
* <--EV_EXTLOCNO Ext locno
*----------------------------------------------------------------------*
FORM f_get_extloc_from_loc USING iv_locno TYPE /sapapo/locno
                           CHANGING ev_extlocno TYPE /sapapo/loc_extlocno.
  CALL FUNCTION '/SAPAPO/DM_LOC_GET_EXTLOC'
    EXPORTING
      iv_locno       = iv_locno
    IMPORTING
      ev_ext_locno   = ev_extlocno
    EXCEPTIONS
      not_qualified  = 1
      no_location    = 2
      not_unique     = 3
      logsys_initial = 4
      OTHERS         = 5.
* If Location doesnt exist in the data base (to be created)
* The external indicator should be the same as the number
  IF sy-subrc <> 0.
    ev_extlocno = iv_locno.
  ENDIF.
ENDFORM. " f_get_extloc_from_loc
*&---------------------------------------------------------------------*
*& Form f_get_extmat_from_mat
*&---------------------------------------------------------------------*
* Gets external prod no from prod no
*----------------------------------------------------------------------*
* -->IV_MATNR Matnr
* <--EV_EXT_MATNR Ext matnr
*----------------------------------------------------------------------*
FORM f_get_extmat_from_mat USING iv_matnr TYPE /sapapo/matnr
                           CHANGING ev_ext_matnr TYPE /sapapo/ext_matnr.
  CALL FUNCTION '/SAPAPO/DM_MAT_GET_EXTMAT'
    EXPORTING
      iv_matnr      = iv_matnr
    CHANGING
      iv_ext_matnr  = ev_ext_matnr
    EXCEPTIONS
      not_qualified = 1
      no_material   = 2
      no_location   = 3
      not_unique    = 4
      OTHERS        = 5.
  IF sy-subrc <> 0.
    ev_ext_matnr = iv_matnr.
  ENDIF.
ENDFORM. " f_get_extmat_from_mat
*&---------------------------------------------------------------------*
*& Form f_update_log_table
*&---------------------------------------------------------------------*
* Update logs
*----------------------------------------------------------------------*
* -->IV_MSGID Message class
* -->IV_MSGTY Message type
* -->IV_MSGNO Message no
* -->IV_MSGV1 Variable1
* -->IV_MSGV2 Variable2
* -->IV_MSGV3 Variable3
* -->IV_MSGV4 Variable4
* -->IV_MSG Message
* <--ET_LOG_TABLE Log table
*----------------------------------------------------------------------*
FORM f_update_log_table USING iv_msgid TYPE symsgid
                              iv_msgty TYPE symsgty
                              iv_msgno TYPE symsgno
                              iv_msgv1 TYPE any
                              iv_msgv2 TYPE any
                              iv_msgv3 TYPE any
                              iv_msgv4 TYPE any
                              iv_msg TYPE bapi_msg
                        CHANGING et_log_table TYPE t_log_table.
  DATA:
  ls_log_table TYPE bal_s_msg.
  ls_log_table-msgty = iv_msgty.
  ls_log_table-msgid = iv_msgid.
  ls_log_table-msgno = iv_msgno.
  ls_log_table-msgv1 = iv_msgv1.
  ls_log_table-msgv2 = iv_msgv2.
  ls_log_table-msgv3 = iv_msgv3.
  ls_log_table-msgv4 = iv_msgv4.
  ls_log_table-context-value = iv_msg.
  APPEND ls_log_table TO et_log_table.
ENDFORM. " f_update_log_table
*&---------------------------------------------------------------------*
*& Form f_update_log_in_db
*&---------------------------------------------------------------------*
* Updates the log in the data base
*----------------------------------------------------------------------*
* -->IV_LOGOBJECT Log object
* -->IV_LOGSUBOBJ Log sub object
* -->IT_LOG_TABLE Log message table
* <--ET_LOG_HANDLE Log handle table
*----------------------------------------------------------------------*
FORM f_update_log_in_db USING iv_logobject TYPE balobj_d
                              iv_logsubobj TYPE balsubobj
                              it_log_table TYPE t_log_table
                        CHANGING et_log_handle TYPE bal_t_logh.
  DATA:
  lv_extnumber TYPE balnrext,
  ls_log TYPE bal_s_log,
  ls_log_handle TYPE balloghndl,
  ls_log_msg TYPE bal_s_msg.
* Fill the log header
  CONCATENATE sy-datum sy-uzeit INTO lv_extnumber.
  ls_log-extnumber = lv_extnumber.
  ls_log-object = iv_logobject.
  ls_log-alprog = sy-repid.
  ls_log-subobject = iv_logsubobj.
* Create the log
  CALL FUNCTION 'BAL_LOG_CREATE'
    EXPORTING
      i_s_log                 = ls_log
    IMPORTING
      e_log_handle            = ls_log_handle
    EXCEPTIONS
      log_header_inconsistent = 1
      OTHERS                  = 2.
  IF sy-subrc <> 0.
* Log could not be created
    MESSAGE i036(zfpv) WITH iv_logobject iv_logsubobj.
    LEAVE LIST-PROCESSING.
  ENDIF.
* Add message to the log created
  LOOP AT it_log_table INTO ls_log_msg.
    CALL FUNCTION 'BAL_LOG_MSG_ADD'
      EXPORTING
        i_log_handle     = ls_log_handle
        i_s_msg          = ls_log_msg
      EXCEPTIONS
        log_not_found    = 1
        msg_inconsistent = 2
        log_is_full      = 3
        OTHERS           = 4.
    IF sy-subrc <> 0.
* Do nothing
    ENDIF.
  ENDLOOP.
  INSERT ls_log_handle INTO TABLE et_log_handle.
ENDFORM. " f_update_log_in_db
*&---------------------------------------------------------------------*
*& Form f_save_log_in_db
*&---------------------------------------------------------------------*
* Saves the create log in the data base
*----------------------------------------------------------------------*
* -->IT_LOG_HANDLE Log handle table
*----------------------------------------------------------------------*
FORM f_save_log_in_db USING it_log_handle TYPE bal_t_logh.
  CALL FUNCTION 'BAL_DB_SAVE'
    EXPORTING
      i_client         = sy-mandt
      i_save_all       = space
      i_t_log_handle   = it_log_handle
    EXCEPTIONS
      log_not_found    = 1
      save_not_allowed = 2
      numbering_error  = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_save_log_in_db
*&---------------------------------------------------------------------*
*& Form f_display_log
*&---------------------------------------------------------------------*
* Displays the log from the data base
*----------------------------------------------------------------------*
* -->IT_LOG_HANDLE Log handle table
*----------------------------------------------------------------------*
FORM f_display_log USING it_log_handle TYPE bal_t_logh.
  CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
    EXPORTING
      i_t_log_handle       = it_log_handle
    EXCEPTIONS
      profile_inconsistent = 1
      internal_error       = 2
      no_data_available    = 3
      no_authority         = 4
      OTHERS               = 5.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_display_log
*&---------------------------------------------------------------------*
*& Form f_update_location_hierarchy
*&---------------------------------------------------------------------*
* Updates location hierarchy
*----------------------------------------------------------------------*
* -->IT_LOCATION Location table
*----------------------------------------------------------------------*
FORM f_update_location_hierarchy USING it_location TYPE t_location.
  DATA:
  ls_location TYPE s_location,
  ls_reldhloc TYPE /sapapo/reldhloc.
  ls_reldhloc-mandt = sy-mandt.
  ls_reldhloc-relid = gc_lochier.
  ls_reldhloc-clevid = gc_lvlidc.
* Get the locid for 'PMI NETWORK'
  PERFORM f_get_locid_from_locno USING p_parloc
                                 CHANGING ls_reldhloc-pnodeid.
  LOOP AT it_location INTO ls_location.
    PERFORM f_get_locid_from_locno USING ls_location-locno
                                   CHANGING ls_reldhloc-cnodeid.
    MODIFY /sapapo/reldhloc FROM ls_reldhloc.
    IF sy-subrc <> 0.
      ROLLBACK WORK.
* Location hieararchy update failed
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_050
                                        ls_location-locno
                                        p_parloc
                                        space space space
                                 CHANGING gt_log_location.
    ELSE.
      COMMIT WORK AND WAIT.
* Location hieararchy update is successfull
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_succ
                                        gc_msgno_051
                                        ls_location-locno
                                        p_parloc
                                        space space space
                                 CHANGING gt_log_location.
    ENDIF.
  ENDLOOP.
ENDFORM. " f_update_location_hierarchy
*&---------------------------------------------------------------------*
*& Form f_fill_bdctab
*&---------------------------------------------------------------------*
* Fills BDC table
*----------------------------------------------------------------------*
* -->IV_FLD1 Field1
* -->IV_FLD2 Field2
* -->IV_FLD3 Field3
*----------------------------------------------------------------------*
FORM f_fill_bdctab USING  iv_fld1 TYPE any
                          iv_fld2 TYPE any
                          iv_fld3 TYPE flag.
  DATA:
  ls_bdcdata TYPE bdcdata.
  CLEAR ls_bdcdata.
  IF iv_fld3 IS NOT INITIAL.
    ls_bdcdata-program = iv_fld1.
    ls_bdcdata-dynpro = iv_fld2.
    ls_bdcdata-dynbegin = iv_fld3.
  ELSE.
    ls_bdcdata-fnam = iv_fld1.
    ls_bdcdata-fval = iv_fld2.
  ENDIF.
  APPEND ls_bdcdata TO gt_bdcdata.
ENDFORM. " f_fill_bdctab
*&---------------------------------------------------------------------*
*& Form f_update_product_hierarchy
*&---------------------------------------------------------------------*
* Updates product hierarchy
*----------------------------------------------------------------------*
* -->IT_PRODUCT Product table
*----------------------------------------------------------------------*
FORM f_update_product_hierarchy USING it_product TYPE t_product.
  DATA:
  lv_parent TYPE /sapapo/matnr,
  lv_child TYPE /sapapo/matnr,
  lv_parentid TYPE /sapapo/matid,
  lv_childid TYPE /sapapo/matid.
  DATA:
  ls_reldhmat TYPE /sapapo/reldhmat,
  ls_product TYPE s_product.
  LOOP AT it_product INTO ls_product.
    CLEAR ls_reldhmat.
    ls_reldhmat-mandt = sy-mandt.
    ls_reldhmat-relid = 'RELH_PRODUKT'.
    CLEAR: lv_parentid, lv_childid, lv_parent, lv_child.
    PERFORM f_get_prod_parnt_child USING ls_product
                                   CHANGING lv_parentid
                                            lv_childid
                                            lv_parent
                                            lv_child.
    IF lv_parentid IS INITIAL.
      ls_reldhmat-clevid = gc_lvlidp.
      ls_reldhmat-cnodeid = lv_childid.
    ELSE.
      ls_reldhmat-clevid = gc_lvlidc.
      ls_reldhmat-pnodeid = lv_parentid.
      ls_reldhmat-cnodeid = lv_childid.
    ENDIF.
    MODIFY /sapapo/reldhmat FROM ls_reldhmat.
* Update log for Products
    IF sy-subrc <> 0.
      ROLLBACK WORK.
* Product hieararchy update failed
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_err
                                        gc_msgno_052
                                        lv_child
                                        lv_parent
                                        space space space
                                 CHANGING gt_log_product.
    ELSE.
      COMMIT WORK AND WAIT.
* Product hieararchy update is successfull
      PERFORM f_update_log_table USING  gc_msgid
                                        gc_succ
                                        gc_msgno_053
                                        lv_child
                                        lv_parent
                                        space space space
                                 CHANGING gt_log_product.
    ENDIF.
  ENDLOOP.
ENDFORM. " f_update_product_hierarchy
*&---------------------------------------------------------------------*
*& Form f_update_locprd_hierarchy
*&---------------------------------------------------------------------*
* Updates Location-Product hierarchy
*----------------------------------------------------------------------*
FORM f_update_locprd_hierarchy .
  CONSTANTS:
  lc_locprdrelh TYPE /sapapo/relid VALUE 'RELH_LOKATIONSPRODUKT'.

  CALL FUNCTION '/SAPAPO/RELH_REORGANIZE'
    EXPORTING
      iv_hierarchy             = lc_locprdrelh
    EXCEPTIONS
      not_qualified            = 1
      relid_not_found          = 2
      no_hierarchy_type        = 3
      link_table_not_specified = 4
      OTHERS                   = 5.
  IF sy-subrc <> 0.
* Location-Product hieararchy update failed
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_err
                                      gc_msgno_055
                                      space space
                                      space space space
                               CHANGING gt_log_locprd.
  ELSE.
* Location-Product hieararchy update is successfull
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_succ
                                      gc_msgno_054
                                      space space
                                      space space space
                               CHANGING gt_log_locprd.
  ENDIF.
ENDFORM. " f_update_locprd_hierarchy
*&---------------------------------------------------------------------*
*& Form f_get_variant_matkl_maktx
*&---------------------------------------------------------------------*
* Gets matkl for prod variant
*----------------------------------------------------------------------*
* -->IV_MATNR Matnr
* -->IT_ZDM0705 Variant table
* -->IT_ZAP0702 Matkl table
* <--EV_MATKL Matkl
* <--EV_MAKTX Maktx
*----------------------------------------------------------------------*
FORM f_get_variant_matkl_maktx USING  iv_matnr TYPE /sapapo/matnr
                                      it_zdmpcg1 TYPE t_zdmpcg1
                                      it_zap0702 TYPE t_zap0702
                               CHANGING ev_matkl TYPE /sapapo/matkl
                                        ev_maktx TYPE /sapapo/maktx.
  CONSTANTS:
  lc_per TYPE char1 VALUE '.'.
  DATA:
* lv_prdcd TYPE zdprdcd,
  lv_ext TYPE char2.
  DATA:
  ls_zdmpcg1 TYPE zdmpcg1,
  ls_zap0702 TYPE zap0702.
* SPLIT iv_matnr AT lc_per INTO lv_prdcd lv_ext.
* READ TABLE it_zdmpcg1 INTO ls_zdmpcg1
* WITH KEY prdcd = lv_prdcd
* BINARY SEARCH.
*
* IF sy-subrc = 0.
* ev_maktx = ls_zdmpcg1-maktx.
* READ TABLE it_zap0702 INTO ls_zap0702
* WITH KEY matkl_parpr = ls_zdmpcg1-matkl
* BINARY SEARCH.
* IF sy-subrc = 0.
* ev_matkl = ls_zap0702-matkl_prvar.
* ENDIF.
* ENDIF.
ENDFORM. " f_get_variant_matkl_maktx
*&---------------------------------------------------------------------*
*& Form f_check_lc_consistency
*&---------------------------------------------------------------------*
* Checks the Live cache consistency
*----------------------------------------------------------------------*
FORM f_check_lc_consistency .
  DATA:
  lt_messages TYPE t_messages.
  REFRESH gt_bdcdata.

  PERFORM f_populate_bdcdata_lc.
  PERFORM f_call_transaction_lccons CHANGING lt_messages.
  READ TABLE lt_messages TRANSPORTING NO FIELDS
                         WITH KEY msgtyp = gc_err.
  IF sy-subrc = 0.
* Live Cache consistency check for Location-Products failed
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_err
                                      gc_msgno_075
                                      space space
                                      space space space
                               CHANGING gt_log_locprd.
  ELSE.
* Live Cache consistency check for Location-Products performed
    PERFORM f_update_log_table USING  gc_msgid
                                      gc_succ
                                      gc_msgno_076
                                      space space
                                      space space space
                               CHANGING gt_log_locprd.
  ENDIF.
ENDFORM. " f_check_lc_consistency
*&---------------------------------------------------------------------*
*& Form f_populate_bdcdata_lc
*&---------------------------------------------------------------------*
* Populates the BDC data
*----------------------------------------------------------------------*
FORM f_populate_bdcdata_lc .
  PERFORM f_fill_bdctab
    USING:  '/SAPAPO/TS_LCM_CONS_CHECK'
            1000 'X',
            'BDC_OKCODE' '=ONLI' space,
            'PAREA' 'PMI_SNP' space,
            'RVRSIO-LOW' '000' space,
            'SHOW' 'X' space,
            'REPAIR' 'X' space,
            'P_LOCK' 'X' space,
            'P_ANCHOR' 'X' space,
            'SNPMAST' 'X' space,
            'SAPMSSY0' '0120' 'X',
            'BDC_OKCODE' '=%EX' space,
            '/SAPAPO/TS_LCM_CONS_CHECK'
            '1000' 'X',
            'BDC_OKCODE' '/EENDE' space.
ENDFORM. " f_populate_bdcdata_lc
*&---------------------------------------------------------------------*
*& Form f_call_transaction_lccons
*&---------------------------------------------------------------------*
* Calls the transaction for consistency check
*----------------------------------------------------------------------*
* --> ET_MESSAGES Messages tab
*----------------------------------------------------------------------*
FORM f_call_transaction_lccons CHANGING et_messages TYPE t_messages.
  DATA:
  ls_options TYPE ctu_params.
  ls_options-dismode = gc_dismode_n.
  ls_options-updmode = gc_update_s.
* ls_options-defsize = 'X'.
  ls_options-racommit = 'X'.
  CALL TRANSACTION '/SAPAPO/TSCONS' USING gt_bdcdata
    OPTIONS FROM ls_options
    MESSAGES INTO et_messages.
ENDFORM. " f_call_transaction_lccons
*&---------------------------------------------------------------------*
*& Form f_get_prod_parnt_child
*&---------------------------------------------------------------------*
* Gets the parent and the child reln
*----------------------------------------------------------------------*
* -->IS_PRODUCT Product structure
* <--EV_PARENT Parent node
* <--EV_CHILD Child node
*----------------------------------------------------------------------*
FORM f_get_prod_parnt_child USING is_product TYPE s_product
                            CHANGING  ev_parentid TYPE /sapapo/matid
                                      ev_childid TYPE /sapapo/matid
                                      ev_parent TYPE /sapapo/matnr
                                      ev_child TYPE /sapapo/matnr.
  CONSTANTS:
  lc_per TYPE c VALUE '.'.
  DATA:
  lv_ext TYPE char2.                                        "#EC NEEDED
* A product variant should be assigned under its core product
  ev_child = is_product-matnr.
  PERFORM f_get_matid_from_matnr USING is_product-matnr
                                 CHANGING ev_childid.
  SPLIT ev_child AT lc_per INTO ev_parent lv_ext.
  IF ev_parent <> ev_child.
* If the product is a core product assign it under the
* node SNP_PROD_HIER
    PERFORM f_get_matid_from_matnr USING ev_parent
                                   CHANGING ev_parentid.
  ELSE.
    ev_parent = 'SNP_PROD_HIER'.
  ENDIF.
ENDFORM. " f_get_prod_parnt_child
*Text elements
*----------------------------------------------------------
* 003 Create Location
* 005 Parent Node for Hierarchy
* 006 Create Product
* 007 Create Location Product
* 008 Transport aion Lane
*Selection texts
*----------------------------------------------------------
* P_CRLOC Location
* P_CRLP Location Product
* P_CRPRD Product
* P_CRTL Transportation Lane
* P_PARLOC Prod Location
* S_MKT Market
* S_PCT Production Center
* S_PRD Product
*Messages
*----------------------------------------------------------
*
* Message class: ZFPV
*002 Market(s) not part of valid sourcing relation(s)
*003 Production centre(s) not part of valid sourcing relation(s)
*004 Product(s) not part of valid sourcing relation(s)
*005 no sourcing info is maintained in the sign portfolio
*008 Atleast select one of the 4 options for master data creation.
*018 No sourcing info is maintained for the user selection
*019 Location(s) entered for creation already there in database
*020 Product(s) entered for creation already there in database
*021 The location(s)/product(s) not available in the db for mapping
*022 The Location-product(s) entered for creation already in database
*023 The location(s)/product(s) not available in the db for tlane creation
*036 Log could not be created & &.>
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zapo_create_stock.

TYPES: BEGIN OF ty_data,
        version LIKE /sapapo/apo01-vrsioid,
        product LIKE /sapapo/msnp_matnr-matnr,
        location LIKE /sapapo/loc-locno,
        loc_type(15) TYPE c,
        ini_stock(5) TYPE c,
        cat TYPE /sapapo/atpcat,
       END OF ty_data,
       BEGIN OF ty_raw,
          data(100) TYPE c,
       END OF ty_raw.

DATA: lt_raw TYPE STANDARD TABLE OF ty_raw,
      ls_raw TYPE ty_raw,
      lt_data TYPE STANDARD TABLE OF ty_data,
      ls_data TYPE ty_data.

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

INCLUDE: /sapapo/constants.
INCLUDE: /sapapo/mmdrpfde.

*PARAMETERS: version LIKE /sapapo/apo01-vrsioid
*                    MEMORY ID /sapapo/vrsioid OBLIGATORY,
*            p_matnr  LIKE /sapapo/msnp_matnr-matnr OBLIGATORY.
*
*
*PARAMETERS: p_locno LIKE /sapapo/loc-locno OBLIGATORY.
*PARAMETERS: p_stock TYPE i DEFAULT 50 OBLIGATORY,
*            p_atpcat TYPE  /sapapo/atpcat DEFAULT 'CC' OBLIGATORY.

DATA:
  p_sttype TYPE /sapapo/stocktype VALUE gc_om_stock_unrestricted,
  p_locid  TYPE /sapapo/locid,
  p_matid  TYPE /sapapo/matid,
  gv_pegid TYPE /sapapo/pegid,
  gv_charg TYPE /sapapo/charg,
  gv_stock TYPE /sapapo/stockqty,
  gs_stock TYPE /sapapo/stock_struc,
  gt_stock TYPE TABLE OF /sapapo/stock_struc.

DATA:
  gv_dyn_loc(20),
  gv_dyn_mat(20),
  gv_dyn_ver(20),
  gv_dynnr LIKE sy-dynnr,
  gv_ok TYPE c.

*   own logsys
DATA lv_source_system TYPE logsys.

SELECTION-SCREEN BEGIN OF BLOCK block_up WITH FRAME TITLE text-upl.
PARAMETERS:
            p_upldfl TYPE string,
            p_delr   TYPE char1 DEFAULT 'X'.           "delimiter
SELECTION-SCREEN END OF BLOCK block_up.

*-----------------------------------------------------------------
* Event: At selection screen
*-----------------------------------------------------------------

AT SELECTION-SCREEN.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_upldfl.
  PERFORM f4_file_lookup USING p_upldfl.

START-OF-SELECTION.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = p_upldfl
      filetype                = 'DAT'
    TABLES
      data_tab                = lt_data
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.

*  LOOP AT lt_raw INTO ls_raw .
*    SPLIT ls_raw AT ',' INTO ls_data-version ls_data-product ls_data-location
*                             ls_data-loc_type ls_data-ini_stock ls_data-cat.
*    APPEND ls_data TO lt_data.
*  ENDLOOP.

INITIALIZATION.

* check liveCache availability

  CALL FUNCTION '/SAPAPO/MSNP_BAS_CHECK_LC'
    IMPORTING
      ev_ok = gv_ok.
  IF gv_ok = gc_false.
    LEAVE PROGRAM.
  ENDIF.

  IF gv_dynnr IS INITIAL.
    gv_dynnr = sy-dynnr.
  ENDIF.

*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_locno.
*
*  gv_dyn_ver = 'VERSION'.
*  gv_dyn_loc = 'P_LOCNO'.
*  PERFORM f4help_locno USING    gv_dynnr gv_dyn_ver gv_dyn_loc
*                       CHANGING version p_locno.
*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_matnr.
*
*  gv_dyn_ver = 'VERSION'.
*  gv_dyn_loc = 'P_LOCNO'.
*  gv_dyn_mat = 'P_MATNR'.
*  PERFORM f4help_matnr USING  gv_dynnr gv_dyn_ver gv_dyn_mat gv_dyn_loc
*                       CHANGING version p_matnr.

END-OF-SELECTION.
  CLEAR: ls_data.
  LOOP AT lt_data INTO ls_data.

    IF ls_data-version = gc_active_version.
      WRITE: / text-001.
      EXIT.
    ENDIF.

* locid from locno

    CALL FUNCTION '/SAPAPO/DM_LOCNO_GET_LOCID'
      EXPORTING
        iv_locno           = ls_data-location "p_locno
      IMPORTING
        ev_locid           = p_locid
      EXCEPTIONS
        location_not_found = 1
        OTHERS             = 2.
    IF sy-subrc <> 0.
      MESSAGE e025(/sapapo/snp).
    ENDIF.

* matid from matnr

    CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
      EXPORTING
        iv_matnr        = ls_data-product "p_matnr
      IMPORTING
        ev_matid        = p_matid
      EXCEPTIONS
        matid_not_found = 1
        OTHERS          = 2.
    IF sy-subrc <> 0.
      MESSAGE e037(/sapapo/snp).
    ENDIF.

* own logical system

    CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
      IMPORTING
        own_logical_system             = lv_source_system
      EXCEPTIONS
        own_logical_system_not_defined = 1
        OTHERS                         = 2.

    IF sy-subrc IS INITIAL.
    ENDIF.

    gv_stock = ls_data-ini_stock. "p_stock.

    gs_stock-matid     = p_matid.
    gs_stock-locid     = p_locid.
    gs_stock-stock     = gv_stock.
    gs_stock-stocktype = p_sttype.
    gs_stock-atpcat    = ls_data-cat. "p_atpcat.
    gs_stock-updcounter = gc_stkupdcnt_off.
    APPEND gs_stock TO gt_stock.

    CALL FUNCTION '/SAPAPO/DM_STOCK_PUT'
      EXPORTING
        iv_method             = 'C'
        iv_simid              = ls_data-version "version
        iv_logsys             = lv_source_system
      TABLES
        t_stock               = gt_stock
      EXCEPTIONS
        no_pegid              = 1
        error_om_stock_change = 2
        OTHERS                = 4.
    IF sy-subrc <> 0.
      CASE sy-subrc.
        WHEN '1'.
          WRITE: text-002. " 'Error reading pegid'
        WHEN OTHERS.
          WRITE: text-003. " 'Error changing stock'
      ENDCASE.
    ELSE.

* post changes in anchor table

      CALL FUNCTION '/SAPAPO/DM_STOCK_COMMIT_LC'
        EXPORTING
          iv_simid = ls_data-version. "version.

* set simversion for //RRP_COMMIT

      CALL FUNCTION '/SAPAPO/RRP_GEN_PARAMS_SET'
        EXPORTING
          iv_simversion = ls_data-version. "version.

      CALL FUNCTION '/SAPAPO/RRP_COMMIT'.

* JKCP0199

      COMMIT WORK.

      WRITE:/ text-004 . " 'LC orders successfully created'
      WRITE:/ text-005.  " '***************************************'
      WRITE:/ ls_data-version, space, ls_data-product, space, ls_data-location.
      WRITE:/ text-005.
      WRITE:/ text-006.  " 'This program does NOT USE DATA FROM ...'
      WRITE:/ text-007.  " '***************************************'

    ENDIF.

    CALL FUNCTION '/SAPAPO/OM_MESSAGES_SHOW'.
    CLEAR: ls_data.
  ENDLOOP.
  INCLUDE /sapapo/rmsnpdrpff1.

*&---------------------------------------------------------------------*
*&      Form  F4_FILE_LOOKUP
*&---------------------------------------------------------------------*
FORM f4_file_lookup USING p_filename TYPE string.

* F4 help to select the file path

  CALL FUNCTION '/SAPDMC/LSM_F4_FRONTEND_FILE'
    EXPORTING
      pathname         = p_filename
    CHANGING
      pathfile         = p_filename
    EXCEPTIONS
      canceled_by_user = 1
      system_error     = 2
      OTHERS           = 3.
  IF sy-subrc <> 0.
*    write: 'Cancelled'.
  ENDIF.

*  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
*    CHANGING
*      file_name = p_filename.

ENDFORM. " F4_FILE_LOOKUP

*Text elements
*----------------------------------------------------------
* 001 Stock changes cannot be made in the active version
* 002 Error reading Peg ID
* 003 Error occurred when changing stock
* 004 liveCache orders created successfully
* 005 ***************************************************************************
* 006 * No data will be used from Demand Planning or the R3 system. *
* 007 ***************************************************************************
* 008 Key Fig.
* UPL Upload Options

*Selection texts
*----------------------------------------------------------
* P_DELR         Delimiter
* P_UPLDFL         File path and name

*Messages
*----------------------------------------------------------
*
* Message class: /SAPAPO/SDP_DEP
*200   Planning book &1 does not exist
*
* Message class: /SAPAPO/SNP
*025   Location &1 does not exist
*037   Product &1 does not exist

This report is run in the foreground to remove the product from the transportation lane once the products are obsoete. After this the  program will also delete the products from the corresponding markets warehouses and production center locations.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*&************************************************************************
* PROGRAM--INITIAL CREATION
* This report is run in the foreground to remove the product from the
* transportation lane once the products are obsoete. After this the
* program will also delete the products from the corresponding markets
* warehouses and production center locations.
************************************************************************
************************************************************************
* PROGRAM--Modification*MOD-1771404
* Program name : ZAXXPR_DELETE_MASTER_DATA
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2008
* Author : Aveek Ghose.
* Description :
*
* This report is run in the foreground to remove the product from the
* transportation lane once the products are obsoete. After this the
* program will also delete the products from the corresponding markets
* warehouses and production center locations.
************************************************************************
************************************************************************
* MOD-1887120
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 10.03.2008
* Author : Aveek Ghose
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA.
************************************************************************
REPORT zaxxpr_delete_master_data MESSAGE-ID zaxxp_sign_fpv.
* Include for global data and constants declaration
* INCLUDE zaxxpr_delete_master_data_top.
* include ZAXXPR_DEL_MASTER_DATA_TOP.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DEL_MASTER_DATA_TOP
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_TOP
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--INITIAL CREATION
* Description XXXXXXXXXX :
*
* This is the Top include that contains the global data declarations
* and the selection screen of the main program
* ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
* PROGRAM--Modification: MOD-1771404
* Program name : ZAXXPR_DELETE_MASTER_DATA_TOP
* Reference program (copied from) : na.
* Development specification ID :
* Initial Change request number : PMI_SIGN_0002
* Author : Aveek Ghose..
* Description :
*
* This is the Top include that contains the global data declarations
* and the selection screen of the main program
* ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
**********************TYPES DECLARATION***************************
************************************************************************
TYPE-POOLS: slis...
* Types dclaration for view table data.
TYPES : BEGIN OF s_view_data,
          pcode TYPE matnr, " Product
          maktx TYPE char40, " Prod Desc
          markt TYPE char4, " Market
          martx TYPE char20, " Market Desc
          pdctr TYPE char2, " Product Center
          pctxt TYPE char20, " Description
          whse1 TYPE char3, " Warehouse1
          whse2 TYPE char3, " Warehouse2
          prvar TYPE char18, " Product Variant
          vartx TYPE char30, " Variant Description
          status TYPE char1, " Status
        END OF s_view_data,
        t_view_data TYPE STANDARD TABLE OF s_view_data.
*MOD-1771404*****
TYPES : BEGIN OF l_view_data,
          pcode TYPE matnr, " Product
          markt TYPE char4, " Market
          pdctr TYPE char2, " Product Center
          whse1 TYPE char3, " Warehouse1
          whse2 TYPE char3, " Warehouse2
          prvar TYPE char18, " Product Variant
          status TYPE char1, " Status
        END OF l_view_data,
        lv_view_data TYPE STANDARD TABLE OF s_view_data.
*MOD-1771404****
* Types declaration for F4 help Product data
TYPES : BEGIN OF s_f4_data_matnr,
          pcode TYPE matnr, " Product
          maktx TYPE maktx, " Description
        END OF s_f4_data_matnr,
        t_f4_data_matnr TYPE STANDARD TABLE OF s_f4_data_matnr.
* Types declaration for F4 help Location data
TYPES : BEGIN OF s_f4_data_loc,
          locno TYPE /sapapo/locno, " Location
          desc TYPE /sapapo/loc_descr40, " Description
          pcode TYPE /sapapo/matnr, " Product
          prvar TYPE /sapapo/matnr, " Product Variant
*          MOD-012
          pdctr TYPE char2, "Production Center
*          MOD-012
          vartx TYPE char30, " Variant Description
          maktx TYPE char40, " Prod Desc
          status TYPE char1, " Status
          loctyp TYPE /sapapo/c_loctype, " Location Type
          whse1 TYPE char3,
          whse2 TYPE char3,
          lv_exist TYPE i, " Variable
        END OF s_f4_data_loc,
        t_f4_data_loc TYPE STANDARD TABLE OF s_f4_data_loc.
* Types declaration for Warehouse description
TYPES : BEGIN OF s_whse,
          whse TYPE char3, " Location
          whdesc TYPE char30, " Description
        END OF s_whse,
        t_whse TYPE STANDARD TABLE OF s_whse.
* Types declaration for F4 help Location data
TYPES : BEGIN OF s_f4_loc,
          locno TYPE /sapapo/locno, " Location
          desc TYPE /sapapo/loc_descr40, " Description
        END OF s_f4_loc,
        t_f4_loc TYPE STANDARD TABLE OF s_f4_loc.
* Types declaration for SCREEN TABLE used in submit statement
TYPES : t_rsparams TYPE STANDARD TABLE OF rsparams.
* Types declaration for Transportation Lane data
TYPES : BEGIN OF s_tlane_data,
          trpid TYPE /sapapo/tr_trpid, " Trip Id
          locto TYPE /sapapo/locno, " Location to
          matid TYPE matnr, " Material Id
          locfr TYPE /sapapo/locno, " Location from
        END OF s_tlane_data,
        t_tlane_data TYPE STANDARD TABLE OF s_tlane_data.
* Types Declaration for Range Table
TYPES : BEGIN OF s_range_matid,
          sign TYPE char1, " Sign
          option TYPE char2, " Option
          low TYPE /sapapo/matid, " Matid low
          high TYPE /sapapo/matid, " Matid high
        END OF s_range_matid,
        t_matid_range TYPE STANDARD TABLE OF s_range_matid.
* Types Declaration for Range Table
TYPES : BEGIN OF s_range_locid,
          sign TYPE char1, " Sign
          option TYPE char2, " Option
          low TYPE /sapapo/locid, " Locid low
          high TYPE /sapapo/locid, " locid high
        END OF s_range_locid,
        t_locid_range TYPE STANDARD TABLE OF s_range_locid.
* Types Declaration for ALV Display
TYPES : BEGIN OF s_display,
          prod TYPE /sapapo/matnr, " Product
          p_des TYPE char40, " Product Desc
          loc TYPE /sapapo/locno, " Location
          l_des TYPE /sapapo/loc_descr40, " Location Desc
          status TYPE char15, " Deletion status
          light TYPE iconname, " Light Indicator
        END OF s_display,
        t_display TYPE STANDARD TABLE OF s_display.
TYPES : BEGIN OF s_displayprd,
          prod TYPE /sapapo/matnr, " Product
          p_des TYPE char40, " Product Desc
          status TYPE char15, " Deletion status
          light TYPE iconname, " Light Indicator
        END OF s_displayprd,
        t_displayprd TYPE STANDARD TABLE OF s_displayprd.
* Types Declaration for Product select options
TYPES : BEGIN OF s_range_product,
          sign TYPE char1, " Sign
          option TYPE char2, " Option
          low TYPE matnr, " Product low
          high TYPE matnr, " Product high
        END OF s_range_product.
* Types Declaration for Location select option
TYPES : BEGIN OF s_range_location,
          sign TYPE char1, " Sign
          option TYPE char2, " Option
          low TYPE /sapapo/locno, " Location low
          high TYPE /sapapo/locno, " Location high
        END OF s_range_location.
* Types Declaration for Matids for the Products
TYPES : BEGIN OF s_matid,
          matid TYPE /sapapo/matid, " Matid
          matnr TYPE matnr, " Product
        END OF s_matid,
        t_matid TYPE STANDARD TABLE OF s_matid.
* Types Declaration for Locid and Locations
TYPES : BEGIN OF s_locid,
          locid TYPE /sapapo/locid, " Matid
          locno TYPE /sapapo/locno, " Product
        END OF s_locid,
        t_locid TYPE STANDARD TABLE OF s_locid.
TYPES : BEGIN OF s_tlaneupd,
          locfr TYPE /sapapo/locno,
          locto TYPE /sapapo/locno,
          matnr TYPE matnr,
          matid TYPE /sapapo/matid,
          msg TYPE /fre/bif_messagetext,
        END OF s_tlaneupd,
        t_tlaneupd TYPE STANDARD TABLE OF s_tlaneupd.
TYPES : BEGIN OF s_matloc,
          matnr TYPE matnr,
          locno TYPE /sapapo/locno,
        END OF s_matloc,
        t_matloc TYPE STANDARD TABLE OF s_matloc.
*MOD-1771404
TYPES : t_masterdata TYPE STANDARD TABLE OF zap_scmpf,
        s_tship TYPE zap_tshpt,
        t_tship TYPE STANDARD TABLE OF s_tship,
        t_log_table TYPE STANDARD TABLE OF bal_s_msg,
        t_log_location TYPE STANDARD TABLE OF bal_s_msg,
        t_log_product TYPE STANDARD TABLE OF bal_s_msg,
        t_log_locprd TYPE STANDARD TABLE OF bal_s_msg,
        t_log_tlane TYPE STANDARD TABLE OF bal_s_msg.
TYPES : BEGIN OF s_location,
          locno TYPE /sapapo/locno,
          loctp TYPE /sapapo/c_loctype,
          locct TYPE char1,
        END OF s_location,
        t_location TYPE STANDARD TABLE OF s_location,
BEGIN OF s_product,
  matnr TYPE /sapapo/matnr,
  matct TYPE char1,
END OF s_product,
t_product TYPE STANDARD TABLE OF s_product,
BEGIN OF s_tlane,
  locfr TYPE /sapapo/locno,
  locto TYPE /sapapo/locno,
  matnr TYPE /sapapo/matnr,
  ltpfr TYPE /sapapo/c_loctype,
  ltpto TYPE /sapapo/c_loctype,
  lidfr TYPE /sapapo/locid,
  lidto TYPE /sapapo/locid,
  matid TYPE /sapapo/matid,
  datfr TYPE timestamp,
  datto TYPE timestamp,
  crtyp TYPE flag,
  lv_exist TYPE i,
END OF s_tlane,
t_tlane TYPE STANDARD TABLE OF s_tlane,
BEGIN OF s_loc_extloc,
  locno TYPE /sapapo/locno,
  extloc TYPE /sapapo/loc_extlocno,
END OF s_loc_extloc,
t_loc_extloc TYPE STANDARD TABLE OF s_loc_extloc,
BEGIN OF s_prd_extprd,
  matnr TYPE /sapapo/matnr,
  extmat TYPE /sapapo/ext_matnr,
END OF s_prd_extprd,
t_prd_extprd TYPE STANDARD TABLE OF s_prd_extprd,
BEGIN OF s_locprd,
  locno TYPE /sapapo/locno,
  matnr TYPE /sapapo/matnr,
  loctp TYPE /sapapo/c_loctype,
  prctp TYPE /sapapo/beskz,
END OF s_locprd,
t_locprd TYPE STANDARD TABLE OF s_locprd.
DATA: t_sign TYPE STANDARD TABLE OF zap_sign.
DATA: t_sign1 TYPE STANDARD TABLE OF zap_sign.
DATA: it_zdu1002 TYPE STANDARD TABLE OF zdu1002.
DATA: it_zdm0705 TYPE STANDARD TABLE OF zdm0705.
*MOD-1771404
************************************************************************
******************CONSTANTS DECLARATION***************************
************************************************************************
CONSTANTS:
* Selection Screen field names
gc_prd_low TYPE dynfnam VALUE 'S_PRD-LOW',
gc_prd_high TYPE dynfnam VALUE 'S_PRD-HIGH',
gc_mkt_low TYPE dynfnam VALUE 'S_MKT-LOW',
gc_mkt_high TYPE dynfnam VALUE 'S_MKT-HIGH',
* Transaction Name and Program Names
gc_bp2 TYPE baltcode VALUE '/SAPAPO/BP2',
gc_slg1 TYPE baltcode VALUE 'SLG1',
gc_mode_e TYPE ctu_mode VALUE 'E',
gc_mode_n TYPE ctu_mode VALUE 'N',
gc_foreground TYPE char1 VALUE 'A',
gc_rscsel TYPE dynfnam VALUE 'RSCSEL-SLOW_I',
gc_l_brac TYPE char1 VALUE '(',
gc_r_brac TYPE char1 VALUE ')',
* Selection Screen Elements for /SAPAPO/RLCDELETE
gc_radio_btn TYPE rsscr_kind VALUE 'R',
gc_parameter TYPE rsscr_kind VALUE 'P',
gc_sel_optn TYPE rsscr_kind VALUE 'S',
gc_sign_i TYPE tvarv_sign VALUE 'I',
gc_option_eq TYPE tvarv_opti VALUE 'EQ',
gc_manual_sel TYPE rsscr_name VALUE 'SHUF_PA1',
gc_product TYPE rsscr_name VALUE 'SEL_PROD',
gc_location TYPE rsscr_name VALUE 'SEL_LOC',
gc_plan_vrsn TYPE rsscr_name VALUE 'P_VERSIO',
gc_fixed_ord TYPE rsscr_name VALUE 'P_FIXED',
gc_category TYPE rsscr_name VALUE 'RB_CAT',
gc_key_fig TYPE rsscr_name VALUE 'RB_KEYF',
gc_only_apo TYPE rsscr_name VALUE 'P_APO',
gc_test_run TYPE rsscr_name VALUE 'P_TEST',
gc_pvrsn_000 TYPE tvarv_val VALUE '000',
* Selection screen fields for /SAPAPO/DELETE_PP_ORDER
gc_pl_vrsn TYPE rsscr_name VALUE 'LV_SIMID',
gc_prod TYPE rsscr_name VALUE 'LT_MATAB',
gc_loc TYPE rsscr_name VALUE 'LT_LOTAB',
gc_plan_prod TYPE rsscr_name VALUE 'LV_EXT',
gc_contract TYPE rsscr_name VALUE 'LV_SUBCO',
gc_display_ord TYPE rsscr_name VALUE 'LV_LIST',
gc_delete_ord TYPE rsscr_name VALUE 'LV_DEL',
gc_send_delete TYPE rsscr_name VALUE 'LV_EVENT',
* ALV grid display fields
gc_pos_1 TYPE lvc_colpos VALUE '0001',
gc_pos_2 TYPE lvc_colpos VALUE '0002',
gc_pos_3 TYPE lvc_colpos VALUE '0003',
gc_pos_4 TYPE lvc_colpos VALUE '0004',
gc_pos_5 TYPE lvc_colpos VALUE '0005',
gc_pos_6 TYPE lvc_colpos VALUE '0006',
gc_prodct TYPE lvc_fname VALUE 'PROD',
gc_status TYPE lvc_fname VALUE 'STATUS',
gc_light TYPE lvc_fname VALUE 'LIGHT',
gc_prod_des TYPE lvc_fname VALUE 'P_DES',
gc_loctn TYPE lvc_fname VALUE 'LOC',
gc_loc_des TYPE lvc_fname VALUE 'L_DES',
gc_tabname TYPE lvc_tname VALUE 'LT_DISPLAY',
gc_left TYPE lvc_just VALUE 'L',
gc_center TYPE lvc_just VALUE 'C',
gc_len_prod TYPE lvc_outlen VALUE '18',
gc_len_p_des TYPE lvc_outlen VALUE '40',
gc_len_loc TYPE lvc_outlen VALUE '20',
gc_len_l_des TYPE lvc_outlen VALUE '30',
gc_len_stat TYPE lvc_outlen VALUE '20',
gc_len_lt TYPE lvc_outlen VALUE '5',
gc_set_pf TYPE slis_formname VALUE 'F_SET_PF',
gc_show_log TYPE slis_formname VALUE 'F_SHOW_LOG',
gc_lt_top TYPE slis_formname VALUE 'F_TOP_TL',
gc_tlupd_top TYPE slis_formname VALUE 'F_TOP_TLUPD',
gc_prd_top TYPE slis_formname VALUE 'F_TOP_PRD',
gc_td_log TYPE gui_code VALUE 'TD_LOG',
gc_tlane TYPE gui_code VALUE 'TLANE',
gs_type_s TYPE char1 VALUE 'S',
gs_type_h TYPE char1 VALUE 'H',
gs_type_a TYPE char1 VALUE 'A',
gc_save TYPE char1 VALUE 'A',
gc_red TYPE iconname VALUE ' (5C)',
gc_green TYPE iconname VALUE ' (5B)',
* ALV display for Tlane
gc_prod_tl TYPE lvc_fname VALUE 'MATID',
gc_locto TYPE lvc_fname VALUE 'LOCTO',
gc_locfr TYPE lvc_fname VALUE 'LOCFR',
gc_matnr TYPE lvc_fname VALUE 'MATNR',
gc_msg TYPE lvc_fname VALUE 'MSG',
gc_tl_tab TYPE lvc_tname VALUE 'GT_TLANE',
gc_tl_tabu TYPE lvc_tname VALUE 'IT_TLANEUPD',
* Others
gc_s TYPE ddbool_d VALUE 'S',
gc_inactive TYPE char1 VALUE 'I',
gc_set TYPE char1 VALUE 'X',
gc_set_val TYPE tvarv_val VALUE 'X',
gc_int_commit TYPE bapi10001commctrl VALUE 'E',
gc_bs_group TYPE /sapapo/logqs VALUE 'BSGAPO',
gc_error TYPE bapi_mtype VALUE 'E',
gc_abend TYPE bapi_mtype VALUE 'A',
gc_dot TYPE char1 VALUE '.',
gc_colon TYPE char1 VALUE ':',
gc_space TYPE char4 VALUE ' ',
gc_star TYPE char1 VALUE '*',
*MOD-1771404
gc_core TYPE char1 VALUE 'C',
gc_var TYPE char1 VALUE 'V',
* Message class name
gc_msgid TYPE symsgid VALUE 'ZAXXP_SIGN_FPV',
* Message types
gc_warn TYPE symsgty VALUE 'W',
gc_err TYPE symsgty VALUE 'E',
gc_succ TYPE symsgty VALUE 'S',
* Message nos
gc_msgno_021 TYPE symsgno VALUE '021',
gc_msgno_023 TYPE symsgno VALUE '023',
gc_msgno_024 TYPE symsgno VALUE '024',
gc_msgno_025 TYPE symsgno VALUE '025',
gc_msgno_026 TYPE symsgno VALUE '026',
gc_msgno_027 TYPE symsgno VALUE '027',
gc_msgno_028 TYPE symsgno VALUE '028',
gc_msgno_029 TYPE symsgno VALUE '029',
gc_msgno_030 TYPE symsgno VALUE '030',
gc_msgno_031 TYPE symsgno VALUE '031',
gc_msgno_032 TYPE symsgno VALUE '032',
gc_msgno_033 TYPE symsgno VALUE '033',
gc_msgno_034 TYPE symsgno VALUE '034',
gc_msgno_035 TYPE symsgno VALUE '035',
gc_msgno_037 TYPE symsgno VALUE '037',
gc_msgno_038 TYPE symsgno VALUE '038',
gc_msgno_039 TYPE symsgno VALUE '039',
gc_msgno_040 TYPE symsgno VALUE '040',
gc_msgno_041 TYPE symsgno VALUE '041',
gc_msgno_042 TYPE symsgno VALUE '042',
gc_msgno_043 TYPE symsgno VALUE '043',
gc_msgno_044 TYPE symsgno VALUE '044',
gc_msgno_050 TYPE symsgno VALUE '050',
gc_msgno_051 TYPE symsgno VALUE '051',
gc_msgno_052 TYPE symsgno VALUE '052',
gc_msgno_053 TYPE symsgno VALUE '053',
gc_msgno_054 TYPE symsgno VALUE '054',
gc_msgno_055 TYPE symsgno VALUE '055',
gc_msgno_061 TYPE symsgno VALUE '061',
gc_msgno_075 TYPE symsgno VALUE '075',
gc_msgno_076 TYPE symsgno VALUE '076',
gc_msgno_078 TYPE symsgno VALUE '078',
gc_msgno_087 TYPE symsgno VALUE '087',
gc_msgno_096 TYPE symsgno VALUE '096',
gc_msgno_98 TYPE symsgno VALUE '098',
gc_msgno_100 TYPE symsgno VALUE '100',
* Location types
gc_ltp_1001 TYPE /sapapo/c_loctype VALUE '1001', "For Prd centre
gc_ltp_1002 TYPE /sapapo/c_loctype VALUE '1002', "For Mkt/whouse
* Procurement types
gc_ptp_x TYPE /sapapo/beskz VALUE 'X', "For Prd centre
gc_ptp_f TYPE /sapapo/beskz VALUE 'F', "For Mkt/whouse
* Variant status
gc_stat_p TYPE char1 VALUE 'P',
gc_stat_a TYPE char1 VALUE 'A',
* Timestamps
gc_startti TYPE syuzeit VALUE '000000',
gc_endti TYPE syuzeit VALUE '235959',
* Default means of transport
gc_ttype_0001 TYPE /sapapo/tr_traty VALUE '0001',
* Business system grp
gc_bsgapo TYPE /sapapo/loc_logqs VALUE 'BSGAPO',
gc_utc TYPE timezone VALUE 'UTC'. "UTC time zome
*MOD-1771404
************************************************************************
**********************DATA DECLARATION****************************
************************************************************************
DATA: gv_flgstart TYPE flag,
      gv_prd TYPE matnr,
      gv_mkt TYPE /sapapo/locno,
      gv_date_before TYPE sy-datum,
      gv_time_before TYPE sy-uzeit,
      gv_flg_error TYPE char1,
      gv_msg_error TYPE char1. " Flag for Call Tr fail
* Internal table declarations
DATA: gt_f4_data_matnr TYPE STANDARD TABLE OF s_f4_data_matnr,
      gt_f4_data_loc TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_matloc TYPE t_matloc,
      gt_prod_data TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_product TYPE STANDARD TABLE OF bapi10001key,
*      MOD-1771404
      gt_new_product TYPE STANDARD TABLE OF s_product,
      gt_location TYPE STANDARD TABLE OF s_location,
      gt_new_tlane TYPE STANDARD TABLE OF s_tlane,
*      MOD-1771404
      gt_core_prod TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_prod_var TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_loc_data TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_tlane TYPE STANDARD TABLE OF s_tlane_data,
      gt_matid_all TYPE STANDARD TABLE OF s_matid,
      gt_view_data TYPE STANDARD TABLE OF s_view_data,
      gt_bdc_tab TYPE STANDARD TABLE OF bdcdata,
      gt_variant_table TYPE STANDARD TABLE OF rsparams,
      gt_fcat TYPE STANDARD TABLE OF slis_fieldcat_alv,
      gt_top_page TYPE slis_t_listheader,
      gt_masterdata TYPE t_masterdata,
      gt_tship TYPE t_tship,
      gt_whse TYPE t_whse,
*      MOD-1771404
      gt_log_location TYPE t_log_location,
      gt_log_product TYPE t_log_product,
      gt_log_locprd TYPE t_log_locprd,
      gt_log_tlane TYPE t_log_tlane.
DATA: lt_tlane TYPE t_tlane,
      lt_tlane_new TYPE t_tlane,
      lt_tlane_dat TYPE t_tlane,
      lt_location TYPE t_location,
      lt_product TYPE t_product,
      lt_prd_extprd TYPE t_prd_extprd,
      lt_loc_extloc TYPE t_loc_extloc,
      lt_locprd TYPE t_locprd.
DATA: l_wa_zdm0705 TYPE zdm0705.
DATA: l_wa_zdu1002 TYPE zdu1002.
DATA: lv_prvar TYPE char40.
*MOD-1771404
************************************************************************
*****************SELECTION SCREEN DEFINITION**********************
************************************************************************
* Block to enter the product and location data
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
* Option to delete master data
*************************************************************
*MOD-001
*************************************************************
* SELECTION-SCREEN: BEGIN OF LINE,
* COMMENT 5(27) text-033 FOR FIELD p_mdel.
* PARAMETERS: p_mdel RADIOBUTTON GROUP grp1 USER-COMMAND ucom
* DEFAULT 'X'.
*
PARAMETERS: p_mdel RADIOBUTTON GROUP grp1.
*
* PARAMETERS p_mdel AS CHECKBOX DEFAULT 'X'.
*
* SELECTION-SCREEN END OF LINE.
* SELECTION-SCREEN SKIP.
* selection-screen: begin of line,
* comment (79) text-043.
* selection-screen end of line.
* selection-screen: begin of line,
* comment (79) text-044.
* selection-screen end of line.
* SELECTION-SCREEN SKIP.
**************************************************************
*MOD-001
*************************************************************
PARAMETERS: p_log TYPE logsys OBLIGATORY MEMORY ID mid.
SELECT-OPTIONS:
s_prd FOR gv_prd OBLIGATORY, " Product variant
s_mkt FOR gv_mkt OBLIGATORY. " Market
SELECTION-SCREEN: SKIP.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN POSITION 33.
PARAMETERS: p_test AS CHECKBOX DEFAULT 'X'.                 "#EC *
* No Selection Text Maintained as we are displaying comment for it
SELECTION-SCREEN COMMENT 1(16) text-002 FOR FIELD p_test.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN PUSHBUTTON 5(10) text-042 USER-COMMAND wuf.
*MOD-1771404
* Option to update transportation lanes
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-032 FOR FIELD p_tlan.
PARAMETERS: p_tlan RADIOBUTTON GROUP grp1.
SELECTION-SCREEN END OF LINE.
* Sourcing for tlane
SELECTION-SCREEN SKIP.
PARAMETERS: p_src TYPE /sapapo/locno OBLIGATORY,
p_des TYPE /sapapo/locno OBLIGATORY,
p_prd TYPE matnr OBLIGATORY.
* Radio button for validity date change
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-028 FOR FIELD p_tdat.
PARAMETERS: p_tdat RADIOBUTTON GROUP grp USER-COMMAND ucom
MODIF ID mod DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
* Fields for dates
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 10(15) text-030 FOR FIELD p_frd.
PARAMETERS p_frd TYPE sydatum.
SELECTION-SCREEN: COMMENT 40(10) text-031 FOR FIELD p_tod.
PARAMETERS p_tod TYPE sydatum.
SELECTION-SCREEN: END OF LINE.
* Radio button for product removal
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-029 FOR FIELD p_tprd.
PARAMETERS: p_tprd RADIOBUTTON GROUP grp.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b1.
*MOD-1771404
************************************************************************
**********************INITIALIZATION******************************
************************************************************************
INITIALIZATION.
  CLEAR gv_flgstart.
* Gets Product and location data for populatiing the search helps
  PERFORM f_get_f4_data CHANGING gt_f4_data_matnr
  gt_f4_data_loc
  gt_view_data.
* Adjusts screen fields
  PERFORM f_adj_screen_flds.
***********************************************************************
****************SELECTION SCREEN EVENTS**************************
***********************************************************************
AT SELECTION-SCREEN ON s_prd.
* Validates product against the SIGN view
  PERFORM f_validate_product.

AT SELECTION-SCREEN ON s_mkt.
* Validate market against the SIGN view
  PERFORM f_validate_market.
* Custom search help for Product-Low
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-low.
  PERFORM f_f4_for_prodcentre USING gc_prd_low
                                    gt_f4_data_matnr.
* Custom search help for Product-High
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-high.
  PERFORM f_f4_for_prodcentre USING gc_prd_high
                                    gt_f4_data_matnr.
* Custom search help for Location-Low
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-low.
  PERFORM f_f4_for_market USING gc_mkt_low
                                gt_f4_data_loc.
* Custom search help for Location-High
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-high.
  PERFORM f_f4_for_market USING gc_mkt_high
                                gt_f4_data_loc.
* Adjusting the screen fields
AT SELECTION-SCREEN OUTPUT.
  CLEAR gv_flgstart.
*mod-099
  PERFORM f_clear_tables.
*mod-099
  PERFORM f_adj_screen_flds.

AT SELECTION-SCREEN.
  IF sy-ucomm = 'WUF'.
    REFRESH gt_matloc.
    PERFORM f_check_product_status USING gt_f4_data_loc
    CHANGING  gt_prod_data
              gt_core_prod
              gt_prod_var
              gt_loc_data
              gt_matloc.
* MOD-1887120 *
    SORT gt_core_prod BY locno pcode.
    DELETE ADJACENT DUPLICATES FROM gt_core_prod
    COMPARING locno
              pcode.
    SORT gt_prod_var BY locno pcode prvar.
    DELETE ADJACENT DUPLICATES FROM gt_prod_var
    COMPARING locno
              pcode
              prvar.
* MOD-1887120 *
* Call the WUF transaction
    PERFORM launch_wuf USING  gt_core_prod
                              gt_prod_var.
  ENDIF.
************************************************************************
**********************START OF SELECTION**************************
************************************************************************
START-OF-SELECTION.
  gv_flgstart = 'X'.
  IF p_mdel IS NOT INITIAL.
* Filter all the Products selected for deletion with status inactive
    REFRESH gt_matloc.
* wait up to 5 seconds.
    PERFORM f_check_product_status USING gt_f4_data_loc
    CHANGING  gt_prod_data
              gt_core_prod
              gt_prod_var
              gt_loc_data
              gt_matloc.
* Set the Date and Time before deleting the Data.
    gv_date_before = sy-datum.
    gv_time_before = sy-uzeit.
    IF gt_core_prod IS INITIAL.
      MESSAGE i085.
    ENDIF.
    IF gt_prod_var IS INITIAL.
      MESSAGE i086.
      LEAVE LIST-PROCESSING.
    ENDIF.
    IF p_test IS INITIAL.
* Delete the Transactional data for the Product and Location Combination
* Using the call transaction /SAPAPO/BP2
*MOD-1887120
      SORT gt_core_prod BY locno pcode.
      DELETE ADJACENT DUPLICATES FROM gt_core_prod
      COMPARING locno
                pcode.
      SORT gt_prod_var BY locno pcode prvar.
      DELETE ADJACENT DUPLICATES FROM gt_prod_var
      COMPARING locno
                pcode
                prvar.
      SORT gt_loc_data BY locno.
      DELETE ADJACENT DUPLICATES FROM gt_loc_data
      COMPARING locno.
*MOD-1887120
      PERFORM f_delete_transactional_data USING gt_core_prod
                                                gt_prod_var
                                                gt_loc_data
                                          CHANGING  gt_prod_data
                                                    gt_bdc_tab.
* Delete the Transactional data created manually by submitting to the
* SAP standard program /SAPAPO/RLCDEL via selection screen variant.
      PERFORM f_delete_manual_created_data USING  gt_core_prod
                                                  gt_prod_var
                                                  gt_loc_data
                                           CHANGING gt_variant_table.
* Delete the PPDS orders for selection screen Product and Location
* Done by submitting to SAP Standard program /SAPAPO/DELETE_PP_ORDER
* Via screen table.
      PERFORM f_delete_ppds_orders USING  gt_core_prod
                                          gt_prod_var
                                          gt_loc_data
                                   CHANGING gt_variant_table.
    ENDIF.
* Delete Products from the Transportation Lanes for the Products and
* locations entered at the selection screen.
    PERFORM f_delete_product_from_tlane USING gt_core_prod
                                              gt_prod_var
                                        CHANGING  gt_loc_data
                                                  gt_matid_all
                                                  gt_tlane.
* Run the Transportation Lane consistency check program
* PERFORM f_run_consistency_check.
* Set the Deletion flag for the Products selected for deletion.
    IF p_test IS INITIAL.
      PERFORM f_set_deletion_flag USING gt_core_prod
                                        gt_prod_var
                                        gt_loc_data
                                        gt_matloc.
    ENDIF.
* Display the ALV Report.
    PERFORM f_display_alv USING gt_prod_data
                                gt_matloc
                          CHANGING  gt_fcat
                                    gt_matid_all.
  ENDIF.
* If the user chooses to update the transportation lane
**MOD-1771404
  IF p_tlan IS NOT INITIAL.
*MOD-1771404
* Retrieve the master data from teh SIGN portfolio meeting the user
* selection
    PERFORM f_retrieve_masterdata CHANGING  gt_masterdata
                                            gt_tship
                                            gt_whse.
* Collect all possible transportation lanes
    PERFORM f_collect_tlanes USING  gt_masterdata
                                    gt_tship
                                    gt_location
                                    gt_new_product
                             CHANGING gt_new_tlane.
    IF gt_new_tlane IS INITIAL.
      EXIT.
    ENDIF.
* The Tlanes can be categorised into 3 categories:
* 1> To be created fresh
* 2> Exists, but new product need to be assigned
* 3> Only validity dates need to be changed
    PERFORM f_categorise_tlanes USING gt_new_tlane
                                CHANGING  lt_tlane_new
                                          lt_tlane_dat.
*MOD-1771404 changed by Aveek
    PERFORM f_update_tlane.
*MOD-1771404 changed by Aveek
* PERFORM f_update_tlane using gt_new_tlane.
*MOD-1771404
  ENDIF.
** Include for subroutine declaration
  INCLUDE zaxxpr_delete_master_data_f02.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F01
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--INITIAL CREATION
*
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA...
************************************************************************
************************************************************************
* PROGRAM--Modification MOD-1771404
************************************************************************
*
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2006
* Author : Aveek Ghose..
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
* MOD-1887120
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 10.03.2006
* Author : Aveek Ghose
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA
************************************************************************
*&---------------------------------------------------------------------*
*& Form f_validate_product
*&---------------------------------------------------------------------*
* Validates the product
*----------------------------------------------------------------------*
FORM f_validate_product.
* Local Date Declaration
  DATA: lv_prd TYPE matnr,
        lv_matnr TYPE /sapapo/matnr.
* Field symbols:
  FIELD-SYMBOLS:
  <lfs_prd> TYPE s_range_product.
  IF p_mdel IS INITIAL.
    EXIT.
  ENDIF.
* Both Product and Location can not be a range at the selection screen
  IF  s_prd-low IS NOT INITIAL AND
      s_prd-high IS NOT INITIAL AND
      s_mkt-low IS NOT INITIAL AND
      s_mkt-high IS NOT INITIAL.
* Error Message: Both Product and Location can not be multiple
    MESSAGE e056.
  ENDIF.
* Check if both Product and location dont have multiple entries
  READ TABLE s_prd INDEX 2 TRANSPORTING NO FIELDS.
  IF sy-subrc = 0.
    READ TABLE s_mkt INDEX 2 TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
* Error Message: Both Product and Location can not be multiple
      MESSAGE e056.
    ENDIF.
  ENDIF.
* Check if user has entered * in the select option for location.
  READ TABLE s_prd ASSIGNING <lfs_prd> WITH KEY low = gc_star.
  IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
    MESSAGE e059.
  ELSE.
    READ TABLE s_prd ASSIGNING <lfs_prd> WITH KEY high = gc_star.
    IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
      MESSAGE e059.
    ENDIF.
  ENDIF.
  SELECT pcode UP TO 1 ROWS
    INTO lv_prd
    FROM zap_sign
    WHERE pcode IN s_prd.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_prd IS INITIAL.
* Product does not exist in APO
    MESSAGE e016.
  ENDIF.
ENDFORM. " f_validate_product
*&---------------------------------------------------------------------*
*& Form f_validate_market
*&---------------------------------------------------------------------*
* Validates the market
*----------------------------------------------------------------------*
FORM f_validate_market .
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3. " Warehouse2
* Field Symbols:
  FIELD-SYMBOLS:
  <lfs_mkt> TYPE s_range_location.
  IF p_mdel IS INITIAL.
    EXIT.
  ENDIF.
* Both Product and Location can not be multiple at the selection screen
  IF  s_prd-low IS NOT INITIAL AND
      s_prd-high IS NOT INITIAL AND
      s_mkt-low IS NOT INITIAL AND
      s_mkt-high IS NOT INITIAL.
* Error Message: Both Product and Location can not be multiple
    MESSAGE e056.
  ENDIF.
* Check if both Product and location dont have multiple entries
  READ TABLE s_prd INDEX 2 TRANSPORTING NO FIELDS.
  IF sy-subrc = 0.
    READ TABLE s_mkt INDEX 2 TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
* Error Message: Both Product and Location can not be multiple
      MESSAGE e056.
    ENDIF.
  ENDIF.
* Check if user has entered * in the select option for location.
  READ TABLE s_mkt ASSIGNING <lfs_mkt> WITH KEY low = gc_star.
  IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
    MESSAGE e060.
  ELSE.
    READ TABLE s_mkt ASSIGNING <lfs_mkt> WITH KEY high = gc_star.
    IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
      MESSAGE e060.
    ENDIF.
  ENDIF.
* Check if the Location is a market
  SELECT markt UP TO 1 ROWS
    INTO lv_mkt
    FROM zap_sign
    WHERE markt IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Production center
  SELECT pdctr UP TO 1 ROWS
    INTO lv_pdctr
    FROM zap_sign
    WHERE pdctr IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_pdctr IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Warehouse 1
  SELECT whse1 UP TO 1 ROWS
    INTO lv_whse1
    FROM zap_sign
    WHERE whse1 IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_whse1 IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Warehouse 2
  SELECT whse2 UP TO 1 ROWS
    INTO lv_whse2
    FROM zap_sign
    WHERE whse2 IN s_mkt.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_whse2 IS NOT INITIAL.
* Location does not exist in APO
    MESSAGE e017.
  ENDIF.
ENDFORM. " f_validate_market
*&---------------------------------------------------------------------*
*& Form F_GET_F4_DATA
*&---------------------------------------------------------------------*
* Get all the data required for custom F4 helps
*----------------------------------------------------------------------*
* <--ct_f4_data_matnr Itab with products
* <--ct_f4_data_loc Itab with F4 data for location
* <--ct_view_data Itab wiht data selected from view table
*----------------------------------------------------------------------*
FORM f_get_f4_data CHANGING ct_f4_data_matnr TYPE t_f4_data_matnr
                            ct_f4_data_loc TYPE t_f4_data_loc
                            ct_view_data TYPE t_view_data.
* Local Data Declaration
  DATA: ls_f4_data_matnr TYPE s_f4_data_matnr,
        ls_f4_data_loc TYPE s_f4_data_loc,
        lts_warehouse TYPE SORTED TABLE OF s_whse
                      WITH NON-UNIQUE KEY whse,
        lt_whse TYPE STANDARD TABLE OF s_whse,
        ls_whse TYPE s_whse.
* Local Field-symbols
  FIELD-SYMBOLS: <lfs_view> TYPE s_view_data,
                 <lfs_whse> TYPE s_whse.
  IF ( ct_f4_data_matnr IS INITIAL AND
       ct_f4_data_loc IS INITIAL ).
    SELECT  pcode "Material Number
            maktx "Product Desc
            markt "Market
            pdctr "Production center
            pctxt "Prod center desc
            whse1 "Warehouse 1
            whse2 "Warehouse 2
            prvar "Prod Variant
            vartx "Variant Description
      INTO TABLE ct_view_data
      FROM zap_sign.
    IF sy-subrc <> 0.
* no sourcing info is maintained in the sign portfolio
      MESSAGE i005.
      LEAVE LIST-PROCESSING.
    ENDIF.
    LOOP AT ct_view_data ASSIGNING <lfs_view>.
      ls_whse-whse = <lfs_view>-whse1.
      APPEND ls_whse TO lt_whse.
      ls_whse-whse = <lfs_view>-whse2.
      APPEND ls_whse TO lt_whse.
    ENDLOOP.
    SORT lt_whse BY whse.
    DELETE ADJACENT DUPLICATES FROM lt_whse COMPARING whse.
    IF NOT lt_whse IS INITIAL.
* Get the Warehouse Descriptions
      SELECT  whse "Warehouse Name
              whdesc "Warehouse Description
        FROM zap_whse
        INTO TABLE lts_warehouse
        FOR ALL ENTRIES IN lt_whse
        WHERE whse = lt_whse-whse.
    ENDIF.
* Populate the Internal Table with Product,Product Variant
* and Location. The location can be the Market,Production center,
* Warehouse 1,Warehouse 2. All the four locations will be clubbed
* under the field Location in the internal table
    LOOP AT ct_view_data ASSIGNING <lfs_view>.
      ls_f4_data_matnr-pcode = <lfs_view>-pcode.
      ls_f4_data_matnr-maktx = <lfs_view>-maktx.
      APPEND ls_f4_data_matnr TO ct_f4_data_matnr.
      CLEAR ls_f4_data_matnr.
      ls_f4_data_loc-pcode = <lfs_view>-pcode.
      ls_f4_data_loc-desc = <lfs_view>-martx.
      ls_f4_data_loc-locno = <lfs_view>-markt.
      ls_f4_data_loc-prvar = <lfs_view>-prvar.
      ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
      ls_f4_data_loc-vartx = <lfs_view>-vartx.
      ls_f4_data_loc-maktx = <lfs_view>-maktx.
      ls_f4_data_loc-status = <lfs_view>-status.
      APPEND ls_f4_data_loc TO ct_f4_data_loc.
      CLEAR ls_f4_data_loc.
      ls_f4_data_loc-pcode = <lfs_view>-pcode.
      ls_f4_data_loc-desc = <lfs_view>-pctxt.
      ls_f4_data_loc-locno = <lfs_view>-pdctr.
      ls_f4_data_loc-prvar = <lfs_view>-prvar.
      ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
      ls_f4_data_loc-vartx = <lfs_view>-vartx.
      ls_f4_data_loc-maktx = <lfs_view>-maktx.
      ls_f4_data_loc-status = <lfs_view>-status.
      APPEND ls_f4_data_loc TO ct_f4_data_loc.
      CLEAR ls_f4_data_loc.
* Read the description for Warehouse 1
      READ TABLE lts_warehouse ASSIGNING <lfs_whse>
                               WITH KEY whse = <lfs_view>-whse1.
      IF sy-subrc = 0.
        ls_f4_data_loc-pcode = <lfs_view>-pcode.
        ls_f4_data_loc-desc = <lfs_whse>-whdesc.
        ls_f4_data_loc-locno = <lfs_view>-whse1.
        ls_f4_data_loc-prvar = <lfs_view>-prvar.
        ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
        ls_f4_data_loc-vartx = <lfs_view>-vartx.
        ls_f4_data_loc-maktx = <lfs_view>-maktx.
        ls_f4_data_loc-status = <lfs_view>-status.
        APPEND ls_f4_data_loc TO ct_f4_data_loc.
        CLEAR ls_f4_data_loc.
      ENDIF.
* Read the description for Warehouse 2
      READ TABLE lts_warehouse ASSIGNING <lfs_whse>
                               WITH KEY whse = <lfs_view>-whse2.
      IF sy-subrc = 0.
        ls_f4_data_loc-pcode = <lfs_view>-pcode.
        ls_f4_data_loc-desc = <lfs_whse>-whdesc.
        ls_f4_data_loc-locno = <lfs_view>-whse2.
        ls_f4_data_loc-prvar = <lfs_view>-prvar.
        ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
        ls_f4_data_loc-vartx = <lfs_view>-vartx.
        ls_f4_data_loc-maktx = <lfs_view>-maktx.
        ls_f4_data_loc-status = <lfs_view>-status.
        APPEND ls_f4_data_loc TO ct_f4_data_loc.
        CLEAR ls_f4_data_loc.
      ENDIF.
    ENDLOOP.
    SORT ct_f4_data_matnr BY pcode.
    DELETE ADJACENT DUPLICATES FROM ct_f4_data_matnr COMPARING pcode.
  ENDIF.
ENDFORM. " F_GET_F4_DATA
*&---------------------------------------------------------------------*
*& Form f_f4_for_prodcentre
*&---------------------------------------------------------------------*
* Display F4 Help for Product
*----------------------------------------------------------------------*
* -->IV_PRD Field Name for Product
* -->IT_F4_DATA Itab with F4 data
*----------------------------------------------------------------------*
FORM f_f4_for_prodcentre USING iv_prd TYPE dynfnam
                               it_f4_data_matnr TYPE t_f4_data_matnr.
* Local Constants Declaration
  CONSTANTS: lc_pcode TYPE fieldname VALUE 'PCODE'.
  PERFORM f_launch_f4_window USING  lc_pcode
                                    iv_prd
                                    text-016
                                    it_f4_data_matnr.
ENDFORM. " f4_for_prodcentre
*&---------------------------------------------------------------------*
*& Form f_f4_for_market
*&---------------------------------------------------------------------*
* Display F4 Help for locations
*----------------------------------------------------------------------*
* -->IV_LOC Field Name for Location
* -->IT_F4_DATA Itab with the F4 Data
*----------------------------------------------------------------------*
FORM f_f4_for_market USING  iv_loc TYPE dynfnam
                            it_f4_data_loc TYPE t_f4_data_loc.
* Local Data Declaratino
  DATA: lt_loc_for_f4 TYPE STANDARD TABLE OF s_f4_loc.
* Local Constants Declaration
  CONSTANTS:
    lc_pdctr TYPE fieldname VALUE 'LOCNO'.
    lt_loc_for_f4 = it_f4_data_loc .
  SORT lt_loc_for_f4 BY locno.
  DELETE ADJACENT DUPLICATES FROM lt_loc_for_f4 COMPARING locno.
  DELETE lt_loc_for_f4 WHERE locno IS INITIAL.
  PERFORM f_launch_f4_window USING  lc_pdctr
                                    iv_loc
                                    text-017
                                    lt_loc_for_f4.
ENDFORM. " f_f4_for_market
*&---------------------------------------------------------------------*
*& Form f_launch_f4_window
*&---------------------------------------------------------------------*
* Displays the Custom F4 Help for the field name passed
*----------------------------------------------------------------------*
* -->IV_RETFLD Return field
* -->IV_DYNFLD Screen field
* -->IV_WINTITLE Window title
* -->IT_VALUETAB Value table
*----------------------------------------------------------------------*
FORM f_launch_f4_window USING iv_retfld TYPE fieldname
                              iv_dynfld TYPE dynfnam
                              iv_wintitle TYPE c
                              it_valuetab TYPE STANDARD TABLE.
  DATA:
    lv_dynprog TYPE syrepid,
    lv_dynpnr TYPE sydynnr.
    lv_dynprog = sy-repid.
    lv_dynpnr = sy-dynnr.

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = iv_retfld
      dynpprog        = lv_dynprog
      dynpnr          = lv_dynpnr
      dynprofield     = iv_dynfld
      window_title    = iv_wintitle
      value_org       = gc_s
    TABLES
      value_tab       = it_valuetab
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.
  IF sy-subrc <> 0.
* Error while displaying search help
    MESSAGE i058.
    EXIT.
  ENDIF.
ENDFORM. " f_launch_f4_window
*&---------------------------------------------------------------------*
*& Form f_check_product_status
*&---------------------------------------------------------------------*
* Get all the Products with status Inactive selected for deletion
*----------------------------------------------------------------------*
* -->IT_F4_LOC_DATA Itab wiht the products to be deleted
* <--CT_PROD_DATA Itab with the Product and Locs to be deleted
* <--CT_CORE_PROD Itab with the core products to be deleted
* <--CT_PROD_VAR Itab with the product varients to be deleted
* <--CT_LOC_DATA Itab with the locations to be deleted
*----------------------------------------------------------------------*
FORM f_check_product_status USING it_f4_data_loc TYPE t_f4_data_loc
                            CHANGING  ct_prod_data TYPE t_f4_data_loc
                                      ct_core_prod TYPE t_f4_data_loc
                                      ct_prod_var TYPE t_f4_data_loc
                                      ct_loc_data TYPE t_f4_data_loc
                                      ct_matloc TYPE t_matloc.
*MOD-1771404
** Local Date Declaration
* DATA: lv_mkt TYPE char4, " Market
* lv_pdctr TYPE char2, " Production center
* lv_whse1 TYPE char3, " Warehouse1
* lv_whse2 TYPE char3. " Warehouse2
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3. " Warehouse2
  DATA: ls_sign TYPE zap_sign.
  DATA: lv_pdctr1 TYPE char2,
        lv_ltext1 TYPE char40.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid,
        lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  DATA: lv_matnr TYPE /sapapo/matnr,
        lt_matloc TYPE STANDARD TABLE OF /sapapo/v_matloc,
        lv_prod_data TYPE s_f4_data_loc,
        lv_tabix TYPE sy-tabix,
        lv_line TYPE i,
        lv_count TYPE i,
        lv_count1 TYPE i.
*MOD-011
  RANGES: lv_mkt1 FOR s_mkt.
  DATA: int TYPE i,
        rspar TYPE TABLE OF rsparams,
        wa_rspar LIKE LINE OF rspar,
        lv_indicator TYPE i.
  DATA: lv_indicator1 TYPE i VALUE 0.
  DATA: l_wa_prod TYPE s_f4_data_loc.
  DATA: it_itab_prod TYPE STANDARD TABLE OF s_f4_data_loc.
*MOD-011
  FIELD-SYMBOLS:
    <lfs_core_prod> TYPE s_f4_data_loc,
    <lfs_prod_data> TYPE s_f4_data_loc,
    <lfs_tsign> TYPE zap_sign.
* <lfs_zdm0705> TYPE ZDM0705.
  FIELD-SYMBOLS:
    <lfs_view> TYPE s_view_data,
*  MOD-1771404
    <lfs_loc_data> TYPE s_f4_data_loc.

*MOD-1771404
  ct_prod_data = it_f4_data_loc.
***MOD-1771404
* Check if the Location is a market
  SELECT *
    INTO TABLE t_sign
    FROM zap_sign
    WHERE pcode IN s_prd AND
          ( markt IN s_mkt OR
            pdctr IN s_mkt OR
            whse1 IN s_mkt OR
            whse2 IN s_mkt ).
  IF sy-subrc <> 0.
    MESSAGE i004 WITH s_prd.
    LEAVE LIST-PROCESSING.
  ENDIF.
* DELETE t_sign WHERE status <> gc_inactive.
*MOD-1771404
  DELETE: ct_prod_data WHERE status <> gc_inactive,
          ct_prod_data WHERE NOT ( pcode IN s_prd ).
**MOD-1771404: This was commneted out for WHH.
* ct_prod_data WHERE NOT ( locno IN s_mkt ).
**MOD-1771404: This was commneted out for WHH.
***MOD-1771404: This was commneted out for WHH.
  IF NOT ct_prod_data[] IS INITIAL.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY  pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
* whse2 = <lfs_prod_data>-locno BINARY SEARCH.*
        whse2 = s_mkt-low BINARY SEARCH.
* whse1 = <lfs_prod_data>-locno BINARY SEARCH.
        IF sy-subrc <> 0.
*MOD-1771404
*MOD-1887120
          READ TABLE t_sign ASSIGNING <lfs_tsign>
            WITH KEY  pcode = <lfs_prod_data>-pcode
                      prvar = <lfs_prod_data>-prvar
*                     whse1 = <lfs_prod_data>-locno BINARY SEARCH.
                      whse1 = s_mkt-low BINARY SEARCH.
          IF sy-subrc <> 0.
*MOD-1771404
            READ TABLE t_sign ASSIGNING <lfs_tsign>
              WITH KEY  pcode = <lfs_prod_data>-pcode
                        prvar = <lfs_prod_data>-prvar
                        markt = s_mkt-low BINARY SEARCH.
*MOD-1887120
* markt = <lfs_prod_data>-locno binary search.
*
*MOD-1887120
            IF sy-subrc <> 0.
              READ TABLE t_sign ASSIGNING <lfs_tsign>
                WITH KEY  pcode = <lfs_prod_data>-pcode
                          prvar = <lfs_prod_data>-prvar
                          pdctr = s_mkt-low BINARY SEARCH.
* pdctr IN s_mkt BINARY SEARCH.
* pdctr = <lfs_prod_data>-locno BINARY SEARCH.
              IF sy-subrc = 0.
                IF  s_mkt-low = <lfs_prod_data>-locno AND
                    s_mkt-low = <lfs_tsign>-pdctr.
                  <lfs_prod_data>-lv_exist = 0.
                  CONTINUE.
*MOD-1887120
* ELSE.
* <lfs_prod_data>-lv_exist = 1.
* Continue.
*MOD-1887120
                ENDIF.
*MOD-1887120
                IF s_mkt-low = <lfs_prod_data>-locno.
                  <lfs_prod_data>-lv_exist = 0.
                  CONTINUE.
*MOD-1887120
* ELSE.
* <lfs_prod_data>-lv_exist = 1.
* Continue.
*MOD-1887120
                ENDIF.
*MOD-1887120
              ELSE.
                <lfs_prod_data>-lv_exist = 1.
                CONTINUE.
              ENDIF.
            ELSE.
              READ TABLE t_sign ASSIGNING <lfs_tsign>
                WITH KEY  pcode = <lfs_prod_data>-pcode
                          prvar = <lfs_prod_data>-prvar
* markt = <lfs_prod_data>-locno BINARY SEARCH.
              markt = s_mkt-low BINARY SEARCH.
* pdctr = <lfs_prod_data>-locno BINARY SEARCH.
              IF sy-subrc = 0.
                <lfs_prod_data>-lv_exist = 0.
                CONTINUE.
              ELSE.
                <lfs_prod_data>-lv_exist = 1.
                CONTINUE.
              ENDIF.
            ENDIF.
          ELSE.
            <lfs_prod_data>-lv_exist = 0.
            <lfs_prod_data>-whse1 = <lfs_tsign>-whse1.
            EXIT.
          ENDIF.
        ELSE.
          <lfs_prod_data>-lv_exist = 0.
*MOD-1771404
          <lfs_prod_data>-whse1 = <lfs_tsign>-whse1.
          <lfs_prod_data>-whse2 = <lfs_tsign>-whse2.
*MOD-1771404
          EXIT.
        ENDIF.
      ENDLOOP.
    ENDLOOP.
  ENDIF.
*MOD-1887120
* DELETE: ct_prod_data WHERE lv_exist eq 1.
* DELETE ADJACENT DUPLICATES FROM CT_PROD_DATA COMPARING
* LOCNO
* PCODE
* PRVAR
* WHSE1
* WHSE2.
*
*MOD-1887120
*MOD-1887120
* To Handle Multiple production centres
  IF NOT ct_prod_data[] IS INITIAL.
    SELECT *
      INTO TABLE t_sign
      FROM zap_sign
      WHERE pcode IN s_prd AND
            pdctr IN s_mkt.
    IF sy-subrc <> 0.
* MESSAGE I004 with s_prd.
    ELSE.
* DELETE t_sign WHERE status <> gc_inactive.
      lv_tabix = 0.
      IF NOT t_sign IS INITIAL.
        LOOP AT t_sign ASSIGNING <lfs_tsign>.
          lv_tabix = lv_tabix + 1.
          LOOP AT s_mkt.
* Get the Warehouse Descriptions
* SELECT pdctr ltext up to 1 rows
* INTO (LV_PDCTR1, LV_LTEXT1)
* FROM ZDU1000
* WHERE PDCTR = S_MKT-LOW.
* ENDSELECT.
*MOD-1887120
            IF sy-subrc = 0.
*MOD-1887120
              l_wa_prod-locno = <lfs_tsign>-pdctr.
              l_wa_prod-desc = lv_ltext1.
              l_wa_prod-pcode = <lfs_tsign>-pcode.
              l_wa_prod-prvar = <lfs_tsign>-prvar.
              l_wa_prod-vartx = <lfs_tsign>-vartx.
              l_wa_prod-pdctr = s_mkt-low.
              l_wa_prod-maktx = <lfs_tsign>-maktx.
              l_wa_prod-whse1 = <lfs_tsign>-whse1.
              l_wa_prod-whse2 = <lfs_tsign>-whse2.
* l_wa_prod-status = <lfs_tsign>-status.
* l_wa_prod-loctyp = <lfs_tsign>-loctyp.
              APPEND l_wa_prod TO it_itab_prod.
              APPEND LINES OF it_itab_prod TO ct_prod_data.
*MOD-1887120
            ENDIF.
*MOD-1887120
          ENDLOOP.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.
*MOD-099
* DELETE: ct_prod_data WHERE lv_exist eq 1.
*MOD-099
  SORT ct_prod_data BY  locno
                        pcode
                        prvar
                        pdctr
                        status
                        whse1
                        whse2.
  DELETE ADJACENT DUPLICATES FROM ct_prod_data
    COMPARING locno
              pcode
              prvar
              pdctr
              status
              whse1
              whse2.
*MOD-1887120
  IF ct_prod_data IS INITIAL.
* No Inactive products are present in the view table.
    MESSAGE i063.
    LEAVE LIST-PROCESSING.
  ENDIF.
  SELECT matnr locno INTO TABLE ct_matloc
    FROM /sapapo/v_matloc
    FOR ALL ENTRIES IN ct_prod_data
    WHERE ( matnr = ct_prod_data-pcode OR
            matnr = ct_prod_data-prvar ) AND
            locno = ct_prod_data-locno.
*MOD-1887120
* LOCNO IN S_MKT.
*MOD-1887120
  IF sy-subrc <> 0.
    MESSAGE i066.
    IF gv_flgstart IS NOT INITIAL.
      LEAVE LIST-PROCESSING.
    ELSE.
      LEAVE TO SCREEN 1000.
    ENDIF.
  ENDIF.
* Delete the entries that are not in data base.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE lt_matloc TRANSPORTING NO FIELDS
* WITH KEY matnr = <lfs_prod_data>-prvar
* locno = <lfs_prod_data>-locno.
* IF sy-subrc <> 0.
* CLEAR <lfs_prod_data>-prvar.
* ENDIF.
* ENDLOOP.
* DELETE ct_prod_data WHERE prvar IS INITIAL.
  ct_core_prod = ct_prod_data.
  ct_prod_var = ct_prod_data.
  ct_loc_data = ct_prod_data.
  SORT ct_core_prod BY pcode.
*MOD-1887120
* DELETE ADJACENT DUPLICATES FROM ct_core_prod COMPARING pcode.
*MOD-1887120
* DELETE ADJACENT DUPLICATES FROM CT_CORE_PROD COMPARING
* locno
* pcode
* prvar
* whse1
* whse2.
  SORT ct_prod_data BY  locno
                        pcode
                        prvar
                        pdctr
                        status
                        whse1
                        whse2.
  DELETE ADJACENT DUPLICATES FROM ct_prod_data
    COMPARING locno
              pcode
              prvar
              pdctr
              status
              whse1
              whse2.
*MOD-1887120
*MOD-1887120
  DELETE ct_core_prod WHERE pcode IS INITIAL.
  LOOP AT ct_core_prod ASSIGNING <lfs_core_prod>.
    LOOP AT gt_view_data TRANSPORTING NO FIELDS
      WHERE pcode = <lfs_core_prod>-pcode AND
            status <> 'I'.
      CLEAR <lfs_core_prod>-pcode.
      EXIT.
    ENDLOOP.
  ENDLOOP.
  DELETE ct_core_prod WHERE pcode IS INITIAL.
**MOD-1887120
* LV_TABIX = 0.
* IF not S_MKT IS INITIAL.
* LOOP AT S_MKT.
* LV_TABIX = LV_TABIX + 1.
* IF LV_TABIX > 1.
* delete: ct_core_prod WHERE NOT ( locno IN s_mkt ).
* ENDIF.
* ENDLOOP.
* ENDIF.
**MOD-1887120
*MOD-1771404
* SORT ct_prod_var BY prvar.
* DELETE ADJACENT DUPLICATES FROM ct_prod_var COMPARING prvar.
*MOD-1771404
  LOOP AT ct_prod_var ASSIGNING <lfs_core_prod>.
    SELECT SINGLE matnr INTO lv_matnr
      FROM /sapapo/matkey
      WHERE matnr = <lfs_core_prod>-prvar.
    IF sy-subrc <> 0.
      CLEAR <lfs_core_prod>-prvar.
    ENDIF.
  ENDLOOP.
  DELETE ct_prod_var WHERE prvar IS INITIAL.
*MOD-1771404
* SORT ct_loc_data BY locno.
* DELETE ADJACENT DUPLICATES FROM ct_loc_data COMPARING locno.
*MOD-1771404
  SORT ct_loc_data BY locno pcode prvar.
  DELETE ADJACENT DUPLICATES FROM ct_loc_data
    COMPARING locno
              pcode
              prvar.
*MOD-1771404
  DELETE ct_loc_data WHERE locno IS INITIAL.
*MOD-1771404
*MOD-1887120 for multiple locations
  lv_indicator = 0.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE CT_MATLOC TRANSPORTING NO FIELDS
* WITH KEY MATNR = <lfs_core_prod>-PCODE
* locno = <lfs_core_prod>-locno binary search.
* if sy-subrc <> 0.
    CLEAR: lv_whse1.
    SELECT whse1 UP TO 1 ROWS
      INTO lv_whse1
      FROM zap_sign
      WHERE whse1 = <lfs_prod_data>-locno OR
            whse2 = <lfs_prod_data>-locno AND
            pcode IN s_prd.
    ENDSELECT.
    IF sy-subrc = 0.
      PERFORM get_desc_loc USING <lfs_prod_data>
                           CHANGING lv_ware_desc.
      IF sy-subrc = 0.
        <lfs_prod_data>-desc = lv_ware_desc.
        lv_indicator = 1.
      ENDIF.
    ELSE.
      <lfs_prod_data>-lv_exist = 1.
    ENDIF.
* ENDIF.
  ENDLOOP.
* DELETE CT_PROD_DATA WHERE LV_EXIST = 1.
*MOD-1887120
**MOD-1887120 for multiple locations
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE pdctr = <lfs_prod_data>-locno and
* pcode in s_prd.
* ENDSELECT.
* IF SY-SUBRC = 0.
* lv_indicator = 1.
* else.
* <lfs_prod_data>-LV_EXIST = 1.
* ENDIF.
* ENDLOOP.
**MOD-1887120
**MOD-1887120 for multiple locations
* LOOP AT ct_core_prod ASSIGNING <lfs_core_prod>.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE pdctr = <lfs_core_prod>-locno and
* pcode in s_prd.
* ENDSELECT.
* IF SY-SUBRC = 0.
* lv_indicator = 1.
* else.
* <lfs_core_prod>-LV_EXIST = 1.
* ENDIF.
* ENDLOOP.
**MOD-1887120
*MOD-1771404: 1 warehouse case
  IF lv_indicator NE 1.
    CLEAR: lv_whse1.
    SELECT whse1 UP TO 1 ROWS
      INTO lv_whse1
      FROM zap_sign
      WHERE whse1 IN s_mkt OR
            whse2 IN s_mkt AND
            pcode IN s_prd.
    ENDSELECT.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* LOOP AT S_MKT.
      READ TABLE t_sign ASSIGNING <lfs_tsign>
        WITH KEY  pcode = <lfs_prod_data>-pcode
                  prvar = <lfs_prod_data>-prvar
                  whse1 = s_mkt-low BINARY SEARCH.
**MOD-1887120
* whse1 = lv_whse1.
**MOD-1887120
      IF sy-subrc = 0.
        <lfs_prod_data>-locno = s_mkt-low.
*MOD-1887120
* <lfs_prod_data>-whse1 = s_mkt-low.
        <lfs_prod_data>-whse1 = lv_whse1.
*MOD-1887120
* PERFORM GET_DESC_LOC USING lv_prod_data
        PERFORM get_desc_loc  USING <lfs_prod_data>
                              CHANGING lv_ware_desc.
        IF sy-subrc = 0.
          <lfs_prod_data>-desc = lv_ware_desc.
        ENDIF.
      ENDIF.
    ENDLOOP.
*MOD-1887120
  ENDIF.
*MOD-1887120
**MOD-1887120 Multiple Location Case.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE whse1 IN s_mkt or
* whse2 in s_mkt and
* pcode in s_prd.
* ENDSELECT.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* LOOP AT S_MKT.
* READ TABLE t_sign ASSIGNING <lfs_tsign>
* WITH KEY pcode = <lfs_prod_data>-pcode
* prvar = <lfs_prod_data>-prvar
* markt = s_mkt-low BINARY SEARCH.
* if sy-subrc = 0.
* <lfs_prod_data>-locno = s_mkt-low.
* <lfs_prod_data>-whse1 = lv_whse1.
* PERFORM GET_DESC_LOC USING lv_prod_data
* changing lv_ware_desc.
* if sy-subrc = 0.
* <lfs_prod_data>-desc = lv_ware_desc.
* endif.
* endif.
* ENDLOOP.
* ENDLOOP.
**MOD-1887120
  REFRESH: ct_loc_data, ct_prod_var.
  CLEAR: ct_loc_data, ct_prod_var.

  ct_loc_data = ct_prod_data.
  ct_prod_var = ct_prod_data.
  SELECT matnr locno INTO TABLE ct_matloc
    FROM /sapapo/v_matloc
    FOR ALL ENTRIES IN ct_prod_data
    WHERE ( matnr = ct_prod_data-pcode OR
            matnr = ct_prod_data-prvar ) AND
*MOD-1887120
          locno IN s_mkt.
* locno = ct_prod_data-locno.
*MOD-1887120
  IF sy-subrc <> 0.
    MESSAGE i066.
    IF gv_flgstart IS NOT INITIAL.
      LEAVE LIST-PROCESSING.
    ELSE.
      LEAVE TO SCREEN 1000.
    ENDIF.
  ENDIF.
*MOD-1771404
*MOD-1771404: 2 warehouse case.
*MOD-1771404
* Check if the Location is a Warehouse 1
*MOD-1887120
  IF lv_indicator NE 1.
*MOD-1887120
    CLEAR: lv_whse1, lv_whse2.
    SELECT whse1 UP TO 1 ROWS
      INTO lv_whse1
      FROM zap_sign
      WHERE whse1 IN s_mkt OR
            whse2 IN s_mkt AND
            pcode IN s_prd.
    ENDSELECT.
* IF sy-subrc = 0 OR
* lv_whse1 IS NOT INITIAL.
* EXIT.
* ENDIF.
* Check if the Location is a Warehouse 2
    SELECT whse2 UP TO 1 ROWS
      INTO lv_whse2
      FROM zap_sign
      WHERE whse1 IN s_mkt OR
            whse2 IN s_mkt AND
            pcode IN s_prd.
    ENDSELECT.
* IF sy-subrc <> 0 OR
* lv_whse2 IS INITIAL.
** Location does not exist in APO
* MESSAGE e017.
* ENDIF.
*MOD-1887120
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
**MOD-011
* LOOP AT S_MKT.
**MOD-011
* READ TABLE t_sign ASSIGNING <lfs_tsign>
* WITH KEY pcode = <lfs_prod_data>-pcode
* prvar = <lfs_prod_data>-prvar
* whse2 = lv_whse2 BINARY SEARCH.
* if sy-subrc = 0.
* <lfs_prod_data>-locno = s_mkt-low.
* <lfs_prod_data>-whse1 = lv_whse1.
* ENDIF.
**MOD-011
* ENDLOOP.
**MOD-011
* ENDLOOP.
**MOD-1771404
*MOD-1887120
*MOD-1887120
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY  pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
* WHSE2 = lv_whse2 binary search.
        whse2 = s_mkt-low BINARY SEARCH.
        IF sy-subrc = 0.
          <lfs_prod_data>-locno = s_mkt-low.
          <lfs_prod_data>-whse1 = lv_whse1.
        ENDIF.
*MOD-1887120
      ENDLOOP.
*MOD-1887120
    ENDLOOP.
*MOD-1887120
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY  pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
                    whse2 = s_mkt-low BINARY SEARCH.
        IF sy-subrc = 0.
          <lfs_prod_data>-locno = s_mkt-low.
*MOD-1771404
          <lfs_prod_data>-whse1 = <lfs_prod_data>-whse1.
          <lfs_prod_data>-whse2 = s_mkt-low.
*MOD-1771404
          PERFORM get_desc_whse2 USING lv_prod_data
            CHANGING lv_ware_desc.
          IF sy-subrc = 0.
            <lfs_prod_data>-desc = lv_ware_desc.
          ENDIF.
        ENDIF.
*MOD-1887120
      ENDLOOP.
*MOD-1887120
    ENDLOOP.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY  pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
                    whse2 = s_mkt-low BINARY SEARCH.
* whse2 = lv_whse2 BINARY SEARCH.
        IF sy-subrc = 0.
          <lfs_prod_data>-locno = s_mkt-low.
*MOD-1771404
          <lfs_prod_data>-whse1 = <lfs_prod_data>-whse1.
          <lfs_prod_data>-whse2 = s_mkt-low.
* <lfs_prod_data>-whse2 = lv_whse2.
*MOD-1887120
*MOD-1771404
          PERFORM get_desc_whse2  USING lv_prod_data
                                  CHANGING lv_ware_desc.
          IF sy-subrc = 0.
            <lfs_prod_data>-desc = lv_ware_desc.
          ENDIF.
        ENDIF.
*MOD-1887120
      ENDLOOP.
*MOD-1887120
    ENDLOOP.
*MOD-1887120
  ELSE.
    DELETE: ct_prod_data WHERE NOT ( locno IN s_mkt ).
  ENDIF.
*MOD-1887120
  REFRESH: ct_loc_data, ct_prod_var.
  CLEAR: ct_loc_data, ct_prod_var.

  ct_loc_data = ct_prod_data.
  ct_prod_var = ct_prod_data.
*MOD-1887120
**MOD-1887120
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
    <lfs_prod_data>-lv_exist = 0.
  ENDLOOP.
  FIELD-SYMBOLS:<lfs_matloc> TYPE s_matloc.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
    READ TABLE ct_matloc ASSIGNING <lfs_matloc>
      WITH KEY  matnr = <lfs_prod_data>-pcode
                locno = <lfs_prod_data>-locno.
    IF sy-subrc NE 0.
      READ TABLE ct_matloc ASSIGNING <lfs_matloc>
        WITH KEY  matnr = <lfs_prod_data>-prvar
                  locno = <lfs_prod_data>-locno.
      IF sy-subrc NE 0.
        <lfs_prod_data>-lv_exist = 1.
      ENDIF.
    ENDIF.
  ENDLOOP.
  DELETE ct_prod_data WHERE lv_exist = 1.

  REFRESH: ct_loc_data, ct_prod_var, ct_core_prod.
  CLEAR: ct_loc_data, ct_prod_var, ct_core_prod.

  ct_core_prod = ct_prod_data.
  ct_prod_var = ct_prod_data.
  ct_loc_data = ct_prod_data.
*MOD-1887120
**MOD-1887120 Changes made after release of transport on 28.03
  DATA: l_wa_zdm0705 TYPE zdm0705.
  DATA: l_wa_zdu1002 TYPE zdu1002.
  DATA: lv_prvar TYPE char18.
  REFRESH: it_zdm0705.
  REFRESH: it_zdu1002.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
    SELECT * APPENDING TABLE it_zdm0705
      FROM zdm0705
      WHERE parpr = <lfs_prod_data>-pcode.
  ENDLOOP.
  SELECT * INTO TABLE it_zdu1002
    FROM zdu1002
    FOR ALL ENTRIES IN it_zdm0705
    WHERE prvar = it_zdm0705-prvar.
  SORT it_zdu1002 BY markt prvar.
  DATA: lv_product TYPE char18.
  CLEAR: l_wa_zdu1002.
  CLEAR: l_wa_zdm0705.
  SORT it_zdm0705 BY prvar parpr.
  DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
  LOOP AT it_zdm0705 INTO l_wa_zdm0705.
    READ TABLE it_zdu1002 INTO l_wa_zdu1002
      WITH KEY prvar = l_wa_zdm0705-prvar BINARY SEARCH.
    IF sy-subrc = 0.
      lv_count = lv_count + 1.
      IF l_wa_zdu1002-status NE 'I'.
        lv_indicator1 = 1.
      ENDIF.
    ENDIF.
  ENDLOOP.
**MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* LOOP AT IT_ZDM0705 into l_wa_zdm0705
* WHERE prvar = l_wa_zdu1002-prvar.
** READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
** prvar = l_wa_zdu1002-prvar binary search.
** where dmoap NE 'Z'.
** IF L_WA_ZDU1002-PRVAR NE L_WA_ZDM0705-PRVAR.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
*
** else.
** l_wa_zdm0705-dmoap = 'Z'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
** exit.
* ENDLOOP.
* endif.
**MOD-1887120 Changes made after release of transport on 28.03
**MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* READ TABLE IT_ZDM0705 ASSIGNING <LFS_ZDM0705>
* WITH KEY PRVAR = L_WA_ZDU1002-PRVAR BINARY SEARCH.
** LOOP AT IT_ZDM0705 into l_wa_zdm0705
** WHERE prvar = l_wa_zdu1002-prvar.
* IF SY-SUBRC = 0.
* <lfs_zdm0705>-dmoap = 'X'.
** l_wa_zdm0705-dmoap = 'X'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* ENDIF.
* ENDLOOP.
** ENDLOOP.
* endif.
*MOD-1887120 Changes made after release of transport on 28.03
* LOOP AT it_zdu1002 into l_wa_zdu1002.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* prvar = l_wa_zdu1002-prvar binary search.
* if sy-subrc ne 0.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* endif.
* ENDLOOP.
*mod-099
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
** READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
** parpr = <lfs_prod_data>-pcode binary
*search.
** if sy-subrc ne 0.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* prvar = <lfs_prod_data>-prvar binary search.
* if sy-subrc ne 0.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* endif.
** endif.
* ENDLOOP.
**mod-099
*MOD-1887120 Changes made after release of transport on 28.03
  IF lv_indicator1 = 1.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* parpr = <lfs_prod_data>-pcode
* dmoap = 'X'.
      IF sy-subrc = 0.
        DELETE ct_core_prod WHERE pcode = <lfs_prod_data>-pcode.
* Indicates that the core product should not be included in
* transportation lane and /sapapo/bp2 transaction.
        READ TABLE ct_matloc ASSIGNING <lfs_matloc>
          WITH KEY  matnr = <lfs_prod_data>-pcode
                    locno = <lfs_prod_data>-locno.
        IF sy-subrc NE 0.
          READ TABLE ct_matloc ASSIGNING <lfs_matloc>
            WITH KEY  matnr = <lfs_prod_data>-prvar
                      locno = <lfs_prod_data>-locno.
          IF sy-subrc NE 0.
            <lfs_prod_data>-lv_exist = 1.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.
  DELETE ct_prod_data WHERE lv_exist = 1.
*mod-099 changed on 04042006
*MOD-099 changed on 04042006
  REFRESH: ct_loc_data, ct_prod_var. " ct_core_prod.
  CLEAR: ct_loc_data, ct_prod_var. " ct_core_prod.
*MOD-1887120
* ct_core_prod = ct_prod_data.
*MOD-1887120
  ct_prod_var = ct_prod_data.
  ct_loc_data = ct_prod_data.
  lv_indicator1 = 0.
*MOD-1887120 Changes made after release of transport on 28.03
*MOD-1771404
* SELECT matnr locno INTO TABLE ct_matloc
* FROM /sapapo/v_matloc
* FOR ALL ENTRIES IN ct_prod_data
* WHERE ( matnr = ct_prod_data-pcode OR
* matnr = ct_prod_data-prvar ) AND
**
* locno = ct_prod_data-locno.
**
* IF sy-subrc <> 0.
* MESSAGE i066.
* IF gv_flgstart IS NOT INITIAL.
* LEAVE LIST-PROCESSING.
* ELSE.
* LEAVE TO SCREEN 1000.
* ENDIF.
* ENDIF.
*MOD-1771404
ENDFORM. " f_check_product_status
*&---------------------------------------------------------------------*
*& Form f_delete_transactional_data
*&---------------------------------------------------------------------*
* Delete the transactional data for the Product and Location
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_PROD_DATA Itab wiht Product and Locations selected
* <--CT_BDC_DATA BDC TABLE
*----------------------------------------------------------------------*
FORM f_delete_transactional_data USING  it_core_prod TYPE t_f4_data_loc
                                        it_prod_var TYPE t_f4_data_loc
                                        it_loc_data TYPE t_f4_data_loc
                                CHANGING ct_prod_data TYPE t_f4_data_loc
                                         ct_bdc_data TYPE STANDARD TABLE.
                                                            "#EC *
* CT_BDC_DATA is populated in the Perform populate_bdc_tab
* Local Data Declaration
  DATA: lv_ctu_params TYPE ctu_params,
        lv_fname TYPE dynfnam,
        lv_count(2) TYPE n,
        lt_msg_tab TYPE STANDARD TABLE OF bdcmsgcoll.
  DATA: lv_prev_pcode TYPE matnr.
  DATA: percentage_text TYPE c LENGTH 5.
  DATA: lv_tabix(20).
  DATA: lv_line TYPE i.
  DATA: lv_line1 TYPE i.
  DATA: lv_line2 TYPE i.
  DATA: lv_pcode TYPE /sapapo/matnr.
  DATA: s_product TYPE RANGE OF bapi10001apxprodlockey-product,
        s_product_wa LIKE LINE OF s_product.
* Field-symbols
  FIELD-SYMBOLS:  <lfs_msg_ret> TYPE bdcmsgcoll,
                  <lfs_data> TYPE s_f4_data_loc,
                  <lfs_data1> TYPE s_f4_data_loc.
  REFRESH ct_bdc_data.
*MOD-1887120
  SORT it_core_prod BY pcode.
  DELETE ADJACENT DUPLICATES FROM it_core_prod
    COMPARING pcode.
  DESCRIBE TABLE it_core_prod LINES lv_line.
  SORT it_prod_var BY pcode prvar.
  DELETE ADJACENT DUPLICATES FROM it_prod_var
    COMPARING pcode prvar.
  DESCRIBE TABLE it_prod_var LINES lv_line1.
  lv_line2 = lv_line + lv_line1.
  IF lv_line2 <= 7.
*Version 0002
*MOD-1887120: Code that is fixed till TESTAVEEK27.
*Populate the general selection criteria on first screen
    PERFORM f_populate_bdc_tab
      USING:  'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
              ' ' 'BDC_OKCODE' '=DUMMY',
              ' ' 'PLVERS' '000',
              ' ' 'MD_SYS' p_log,
              ' ' 'EXTRB' 'X',
              ' ' 'MO_DEL' 'X',
              ' ' 'SIM' space,
              ' ' 'P_LOGDEL' '5'.
*Populate the select options for Product
    PERFORM f_populate_bdc_tab
      USING:  'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
              ' ' 'BDC_OKCODE' '=%011'.
*Populate the select options screen for product
    PERFORM f_populate_bdc_tab
      USING:  'X' 'SAPLALDB' '3000',
*             ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
              ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_core_prod ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab
        USING:  ' ' lv_fname <lfs_data>-pcode.
      CLEAR lv_fname.
    ENDLOOP.
    LOOP AT it_prod_var ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab
        USING:  ' ' lv_fname <lfs_data>-prvar.
      CLEAR lv_fname.
    ENDLOOP.
*Populate the select options for the Locations
    PERFORM f_populate_bdc_tab
      USING:  'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
              ' ' 'BDC_OKCODE' '=%012'.
*Populate the select options screen for product
    PERFORM f_populate_bdc_tab
      USING:  'X' 'SAPLALDB' '3000',
*             ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
              ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_loc_data ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab
        USING:  ' ' lv_fname <lfs_data>-locno.
      CLEAR lv_fname.
    ENDLOOP.
*Populate the objects to be deleted on first screen
    PERFORM f_populate_bdc_tab
      USING:  'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
              ' ' 'BDC_OKCODE' '=ONLI',
              ' ' 'PLVERS' '000',
              ' ' 'MD_SYS' p_log,
              ' ' 'EXTRB' 'X',
              ' ' 'MO_DEL' 'X',
              ' ' 'MO_STAT' 'X',
              ' ' 'PO_DEL' 'X',
              ' ' 'SL_DEL' 'X',
              ' ' 'ST_DEL' 'X',
              ' ' 'PIR_DEL' 'X',
              ' ' 'SIM' space,
              ' ' ' ' ' '.
    PERFORM f_populate_bdc_tab
      USING:  'X' 'SAPLSPO1' '0500',
              ' ' 'BDC_OKCODE' '=OPT1'.
    CLEAR gv_msg_error.
    lv_ctu_params-nobinpt = space.
    lv_ctu_params-dismode = gc_mode_e.
    lv_ctu_params-updmode = 'S'.
    lv_ctu_params-racommit = 'X'.
* Call Transaction /SAPAPO/BP2 for deleting the Transactional data
    CALL TRANSACTION gc_bp2
      USING ct_bdc_data
      OPTIONS FROM lv_ctu_params
      MESSAGES INTO lt_msg_tab.
*MOD-1887120: Code that is fixed till TESTAVEEK27.
*Version 0002
  ELSE.
**Version 0005
*CTS D7AK900899 New Version code fixed for testaveek28 case.
*MOD-1887120: Code that is changed after the release of transport.
* Populate the general selection criteria on first screen
    PERFORM f_populate_bdc_tab
      USING:  'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
*             ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
              ' ' 'BDC_OKCODE' '=%011',
*             ' ' 'BDC_OKCODE' '=DUMMY',
              ' ' 'PLVERS' '000',
              ' ' 'MD_SYS' p_log,
              ' ' 'EXTRB' 'X',
              ' ' 'MO_DEL' 'X',
              ' ' 'SIM' space,
              ' ' 'P_LOGDEL' '5'.
    SORT it_core_prod BY pcode.
    DELETE ADJACENT DUPLICATES FROM it_core_prod
      COMPARING pcode.
    DESCRIBE TABLE it_core_prod LINES lv_line.
    CLEAR lv_count.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
*** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-pcode,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
* ENDLOOP.
*Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
    SORT it_prod_var BY pcode prvar.
    DELETE ADJACENT DUPLICATES FROM it_prod_var
      COMPARING pcode prvar.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000'.
*MOD-1887120:
    IF NOT it_core_prod[] IS INITIAL.
*MOD-1887120:
      LOOP AT it_core_prod ASSIGNING <lfs_data>.
        lv_pcode = <lfs_data>-pcode.
*lv_count = lv_count + 1.
        lv_count = 1.
        CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_line > 1.
        PERFORM f_populate_bdc_tab
          USING:  'X' 'SAPLALDB' '3000',
                  ' ' lv_fname <lfs_data>-pcode,
                  ' ' 'BDC_OKCODE' '=LINS'.
        CLEAR lv_fname.
        DESCRIBE TABLE it_prod_var LINES lv_line.
* sort <lfs_data> by locno pcode prvar.
        LOOP AT it_prod_var ASSIGNING <lfs_data1> WHERE pcode = lv_pcode.
          lv_count = 1.
          CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* if lv_line > 1.
          PERFORM f_populate_bdc_tab
            USING:  'X' 'SAPLALDB' '3000',
                    ' ' lv_fname <lfs_data1>-prvar,
                    ' ' 'BDC_OKCODE' '=LINS'.
* else.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
* endif.
          CLEAR lv_fname.
        ENDLOOP.
*MOD-1887120: Changed after 04.04.2006
        LOOP AT it_prod_var ASSIGNING <lfs_data1>.
          lv_count = 1.
          CONCATENATE gc_rscsel
                      gc_l_brac
                      lv_count
                      gc_r_brac
                 INTO lv_fname.
          PERFORM f_populate_bdc_tab
            USING:  'X' 'SAPLALDB' '3000',
                    ' ' lv_fname <lfs_data1>-prvar,
                    ' ' 'BDC_OKCODE' '=LINS'.
          CLEAR lv_fname.
        ENDLOOP.
      ENDLOOP.
*MOD-1887120:
*MOD-1887120:
    ELSE.
*MOD-1887120:
      DESCRIBE TABLE it_prod_var LINES lv_line.
* sort <lfs_data> by locno pcode prvar.
      LOOP AT it_prod_var ASSIGNING <lfs_data1>. "where pcode=lv_pcode.
* lv_count = lv_count + 1.
        lv_count = 1.
        CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* if lv_line > 1.
        PERFORM f_populate_bdc_tab
          USING:  'X' 'SAPLALDB' '3000',
                  ' ' lv_fname <lfs_data1>-prvar,
                  ' ' 'BDC_OKCODE' '=LINS'.
* else.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
* endif.
        CLEAR lv_fname.
      ENDLOOP.
*MOD-1887120:
    ENDIF.
*MOD-1887120:
* Populate the select options screen for product
    PERFORM f_populate_bdc_tab
      USING:
*             'X' 'SAPLALDB' '3000',
*             ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
              ' ' 'BDC_OKCODE' '=ACPT'.
* Populate the select options for the Locations
    PERFORM f_populate_bdc_tab
      USING:  'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
*             ' ' 'BDC_CURSOR' 'EXT_PRD-LOW',
              ' ' 'BDC_OKCODE' '=%012',
              ' ' 'PLVERS' '000',
              ' ' 'MD_SYS' p_log,
              ' ' 'EXTRB' 'X',
              ' ' 'MO_DEL' 'X',
              ' ' 'SIM' space,
              ' ' 'P_LOGDEL' '5'.
* Populate the select options screen for product
    PERFORM f_populate_bdc_tab
      USING:  'X' 'SAPLALDB' '3000',
*             ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
              ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_loc_data ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
* lv_count = 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab
        USING:  ' ' lv_fname <lfs_data>-locno.
* ' ' 'BDC_OKCODE' '=LINS'.
      CLEAR lv_fname.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-locno,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
    ENDLOOP.
* CLEAR lv_count.
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-locno,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
* ENDLOOP.
*
* Populate the objects to be deleted on first screen
    PERFORM f_populate_bdc_tab
      USING:  'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
              ' ' 'BDC_OKCODE' '=DUMMY',
              ' ' 'PLVERS' '000',
              ' ' 'MD_SYS' p_log,
              ' ' 'EXTRB' 'X',
              ' ' 'MO_DEL' 'X',
              ' ' 'MO_STAT' 'X',
              ' ' 'PO_DEL' 'X',
              ' ' 'SL_DEL' 'X',
              ' ' 'ST_DEL' 'X',
              ' ' 'PIR_DEL' 'X',
              ' ' 'SIM' space,
              ' ' ' ' ' '.
    PERFORM f_populate_bdc_tab
      USING:  'X' 'SAPLSPO1' '0500',
              ' ' 'BDC_OKCODE' '=OPT1'.
    CLEAR gv_msg_error.
    lv_ctu_params-nobinpt = space.
* lv_ctu_params-dismode = gc_mode_e.
    lv_ctu_params-dismode = 'A'.
    lv_ctu_params-updmode = 'S'.
    lv_ctu_params-racommit = 'X'.
* Call Transaction /SAPAPO/BP2 for deleting the Transactional data
    CALL TRANSACTION gc_bp2
      USING ct_bdc_data
      OPTIONS FROM lv_ctu_params
      MESSAGES INTO lt_msg_tab.
***MOD-1887120: Code that is changed after the release of transport.
*CTS D7AK900899 New Version
*Code fixed for testaveek28 case.
**Version 0005
  ENDIF.
*MOD-1887120
**Version 0002
**MOD-1887120: Code that is fixed till TESTAVEEK27.
**Populate the general selection criteria on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
**Populate the select options for Product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%011'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-pcode.
* CLEAR lv_fname.
*
* ENDLOOP.
*
* LOOP AT it_prod_var ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar.
* CLEAR lv_fname.
* ENDLOOP.
*
**Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%012'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
*
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
*
* CLEAR lv_fname.
*
* ENDLOOP.
*
**Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=ONLI',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
*
**MOD-1887120: Code that is fixed till TESTAVEEK27.
**Version 0002
***Version 0005
**CTS D7AK900899 New Version code fixed for testaveek28 case.
**MOD-1887120: Code that is changed after the release of transport.
** Populate the general selection criteria on first screen
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
* ' ' 'BDC_OKCODE' '=%011',
** ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
* sort it_core_prod by pcode.
* delete adjacent duplicates from it_core_prod
* comparing pcode.
* describe table it_core_prod lines lv_line.
*
* CLEAR lv_count.
** LOOP AT it_core_prod ASSIGNING <lfs_data>.
** lv_count = 1.
** CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
**** if lv_line > 1.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-pcode,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
** ENDLOOP.
*
**Populate the select options screen for product
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
*** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
** ' ' 'BDC_OKCODE' '=ACPT'.
*
* sort it_prod_var by pcode prvar.
* delete adjacent duplicates from it_prod_var
* comparing pcode prvar.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000'.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_pcode = <lfs_data>-pcode.
**lv_count = lv_count + 1.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
*** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-pcode,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
*
* describe table it_prod_var lines lv_line.
*
** sort <lfs_data> by locno pcode prvar.
* LOOP AT it_prod_var ASSIGNING <lfs_data1> where pcode = lv_pcode.
** lv_count = lv_count + 1.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data1>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
** else.
** PERFORM f_populate_bdc_tab USING:
** ' ' lv_fname <lfs_data>-prvar,
** ' ' 'BDC_OKCODE' '=LINS'.
** endif.
* CLEAR lv_fname.
* ENDLOOP.
* ENDLOOP.
*
** Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
*
** Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PRD-LOW',
* ' ' 'BDC_OKCODE' '=%012',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
*
** Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
*
* CLEAR lv_count.
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
** lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
** ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-locno,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
* ENDLOOP.
*
*
** CLEAR lv_count.
** LOOP AT it_loc_data ASSIGNING <lfs_data>.
** lv_count = 1.
** CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-locno,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
** ENDLOOP.
*
**
** Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
****MOD-1887120: Code that is changed after the release of transport.
**CTS D7AK900899 New Version
**Code fixed for testaveek28 case.
***Version 0005
**Version 0006 latest version.
**Code for looping at it_prod_var and doing the call transaction for
**each record
**CTS D7AK900899 New Version
*
* sort it_prod_var by pcode prvar.
* delete adjacent duplicates from it_prod_var
* comparing pcode prvar.
*
* clear: lv_prev_pcode.
*
* LOOP AT it_prod_var ASSIGNING <lfs_data>.
*
** Populate the general selection criteria on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
* ' ' 'BDC_OKCODE' '=%011',
** ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_prev_pcode ne <lfs_data>-pcode.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-pcode.
* CLEAR lv_fname.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar.
* CLEAR lv_fname.
* clear: lv_count.
*
*
**Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%012'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
* CLEAR lv_fname.
* clear: lv_count.
*
** Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
* CLEAR gv_msg_error.
* lv_ctu_params-nobinpt = space.
** lv_ctu_params-dismode = gc_mode_e.
* lv_ctu_params-dismode = 'A'.
* lv_ctu_params-updmode = 'S'.
* lv_ctu_params-racommit = 'X'.
*
** Call Transaction /SAPAPO/BP2 for deleting the Transactional data
* CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS
* FROM lv_ctu_params
* MESSAGES INTO lt_msg_tab.
*
* clear: ct_bdc_data.
*
* ENDLOOP.
***
******MOD-1887120: Code that is changed after the release of transport.
****CTS D7AK900899 New Version
****Version 0006
**MOD-1887120:
* CLEAR gv_msg_error.
* lv_ctu_params-nobinpt = space.
** lv_ctu_params-dismode = gc_mode_e.
* lv_ctu_params-dismode = 'A'.
* lv_ctu_params-updmode = 'S'.
* lv_ctu_params-racommit = 'X'.
*
** Call Transaction /SAPAPO/BP2 for deleting the Transactional data
* CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS FROM lv_ctu_params
* MESSAGES INTO lt_msg_tab.
**MOD-1887120:
** commit work and wait.
*MOD-1771404
  DO 10 TIMES.
    WAIT UP TO '0.1' SECONDS.
    percentage_text(3) = sy-index.
    percentage_text+3 = '%'.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        percentage = sy-index
        text       = percentage_text.
  ENDDO.
*MOD-1771404
  LOOP AT lt_msg_tab ASSIGNING <lfs_msg_ret>
    WHERE msgtyp = gc_error OR
          msgtyp = gc_abend.
    gv_msg_error = gc_set.
    EXIT.
  ENDLOOP.
ENDFORM. " f_delete_transactional_data
*&---------------------------------------------------------------------*
*& Form F_POPULATE_BDC_TAB
*&---------------------------------------------------------------------*
* Pupulates BDC Table with the parameters passed to the perform
*----------------------------------------------------------------------*
* -->IV_FLG Flag for New Screem
* -->IV_VAR1 Program Name / Field Name
* -->IV_VAR2 Screen Number / Field Value
*----------------------------------------------------------------------*
FORM f_populate_bdc_tab USING iv_flg TYPE any
                              iv_var1 TYPE any
                              iv_var2 TYPE any.
* Local Data Declaration
  DATA: ls_bdc_data TYPE bdcdata.
* Populate the BDC Table with the variables passed in it.
  CLEAR ls_bdc_data.
  IF iv_flg = gc_set. " = 'X'
    ls_bdc_data-program = iv_var1.
    ls_bdc_data-dynpro = iv_var2.
    ls_bdc_data-dynbegin = gc_set.
  ELSE.
    ls_bdc_data-fnam = iv_var1.
    ls_bdc_data-fval = iv_var2.
  ENDIF.
  APPEND ls_bdc_data TO gt_bdc_tab.
ENDFORM. " F_POPULATE_BDC_TAB
*&---------------------------------------------------------------------*
*& Form f_delete_manual_created_data
*&---------------------------------------------------------------------*
* Delete the Manually entered transactional data for prod and loc
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_VARIANT_TABLE Screen Varient Table.
*----------------------------------------------------------------------*
FORM f_delete_manual_created_data USING it_core_prod TYPE t_f4_data_loc
                                        it_prod_var TYPE t_f4_data_loc
                                        it_loc_data TYPE t_f4_data_loc
                                  CHANGING ct_variant_table TYPE t_rsparams.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc.
* Populate the selection screen table
  PERFORM f_populate_screen_table USING:
* Field Name Type Sign Option Low Value
* Populate the Additional selection options
* Select the Manual selection button
  gc_manual_sel gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Populate the Planning Version
  gc_plan_vrsn gc_parameter gc_sign_i gc_option_eq gc_pvrsn_000 space.
* Populate the Core Products to be deleted
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_product gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-pcode space.
  ENDLOOP.
* Populate the Product Variants to be deleted
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_product gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-prvar space.
  ENDLOOP.
* Populate the Locations to be deleted.
  LOOP AT it_loc_data ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_location gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-locno space.
  ENDLOOP.
* Populate the Fixed Orders Checkbox
  PERFORM f_populate_screen_table USING:
  gc_fixed_ord gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Populate the Selection Objects
* Unselect the Key Figure Radio Button
  gc_key_fig gc_radio_btn gc_sign_i gc_option_eq space space,
* By Category Radio Button
  gc_category gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Populate the Settings
* Select Delete Transactional data only in APO Radio Button
  gc_only_apo gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Test Run check box.
  gc_test_run gc_parameter gc_sign_i gc_option_eq space space.
* Submit to SAP Standard report /SAPAPO/RLCDELETE
* For deleting manually entered data for the product and location
  SUBMIT /sapapo/rlcdelete
    WITH SELECTION-TABLE ct_variant_table
    AND RETURN.
ENDFORM. " f_delete_manual_created_data
*&---------------------------------------------------------------------*
*& Form F_POPULATE_SCREEN_TABLE
*&---------------------------------------------------------------------*
* Populate the screen table with the parameters passed
*----------------------------------------------------------------------*
* -->IV_NAME Screen field Name
* -->IV_TYPE Screen field type
* -->IV_SIGN Sign I / E
* -->IV_OPTION Option BT/EQ
* -->IV_LOW Lower value
* -->IV_HIGH Higher value
*----------------------------------------------------------------------*
FORM f_populate_screen_table USING  iv_name TYPE rsscr_name
                                    iv_type TYPE rsscr_kind
                                    iv_sign TYPE tvarv_sign
                                    iv_option TYPE tvarv_opti
                                    iv_low TYPE any
                                    iv_high TYPE tvarv_val.
* Local Data Declaration
  DATA: ls_variant TYPE rsparams.
* Populate the Screen Table
  ls_variant-selname = iv_name.
  ls_variant-kind = iv_type.
  ls_variant-sign = iv_sign.
  ls_variant-option = iv_option.
  ls_variant-low = iv_low.
  ls_variant-high = iv_high.
  APPEND ls_variant TO gt_variant_table.
  CLEAR ls_variant.
ENDFORM. " F_POPULATE_SCREEN_TABLE
*&---------------------------------------------------------------------*
*& Form f_delete_ppds_orders
*&---------------------------------------------------------------------*
* Delete the PPDS orders
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_VARIANT_TABLE Screen Varient Table.
*----------------------------------------------------------------------*
FORM f_delete_ppds_orders USING it_core_prod TYPE t_f4_data_loc
                                it_prod_var TYPE t_f4_data_loc
                                it_loc_data TYPE t_f4_data_loc
                          CHANGING ct_variant_table TYPE t_rsparams.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc.
  REFRESH ct_variant_table.
* Populate the selection screen variant
  PERFORM f_populate_screen_table USING:
* Field Name Type Sign Option Low Value
* Populate the Location Product details
  gc_pl_vrsn gc_parameter gc_sign_i gc_option_eq gc_pvrsn_000 space.
* Populate the Core Products to be deleted
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_prod gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-pcode space.
  ENDLOOP.
* Populate the Product Variants to be deleted
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_prod gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-prvar space.
  ENDLOOP.
* Populate the Locations to be deleted.
  LOOP AT it_loc_data ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_loc gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-locno space.
  ENDLOOP.
* Populate Product planned externally checkbox
  PERFORM f_populate_screen_table USING:
  gc_plan_prod gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Populate Subcontracting Planned Orders
  gc_contract gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Display order check box
  gc_display_ord gc_parameter gc_sign_i gc_option_eq space space,
* Check the Delete Order check box
  gc_delete_ord gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Send Deleted orders checkbox
  gc_send_delete gc_parameter gc_sign_i gc_option_eq space space.
* Submit standard program /SAPAPO/DELETE_PP_ORDER
* For deleting PPDS orders created for the product and location
  SUBMIT /sapapo/delete_pp_order
    WITH SELECTION-TABLE ct_variant_table
    AND RETURN.
ENDFORM. " f_delete_ppds_orders
*&---------------------------------------------------------------------*
*& Form f_delete_product_from_tlane
*&---------------------------------------------------------------------*
* Delete Products from the Transportation Lane
*&---------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* <--CT_LOC_DATA Itab with the locations to be deleted
* <--CT_MATID_ALL Itab with Matid for all the Products
* <--CT_TLANE Itab with transportation lane data
*----------------------------------------------------------------------*
FORM f_delete_product_from_tlane USING  it_core_prod TYPE t_f4_data_loc
                                        it_prod_var TYPE t_f4_data_loc
                                 CHANGING  ct_loc_data TYPE t_f4_data_loc
                                           ct_matid_all TYPE t_matid
                                           ct_tlane TYPE t_tlane_data.
  CONSTANTS:
  lc_stime TYPE t VALUE '000000',
  lc_etime TYPE t VALUE '235959',
  lc_utc TYPE timezone VALUE 'UTC'.
  DATA:
  lv_dummy TYPE /sapapo/c_loctype,
  lv_valfr TYPE timestamp,
  lv_valto TYPE timestamp,
  lv_matid TYPE /sapapo/matid,
  lv_srcid TYPE /sapapo/locid,
  lv_desid TYPE /sapapo/locid.
  DATA:
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_matid TYPE t_matid,
  ls_matid TYPE s_matid,
  lt_tlaneupd TYPE t_tlaneupd,
  ls_tlaneupd TYPE s_tlaneupd.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.
* Local Data Declaration
  DATA: lv_matid_low TYPE /sapapo/matid,
        lv_locid_low TYPE /sapapo/locid,
        lv_loctyp TYPE /sapapo/c_loctype,
        lr_matid TYPE RANGE OF /sapapo/matid,
        ls_trprod TYPE s_tlane_data,
        lr_locid TYPE RANGE OF /sapapo/locid,
        ls_locid TYPE s_locid,
        ls_loc_new TYPE /sapapo/loc,
        lt_locid_all TYPE STANDARD TABLE OF s_locid,
        lv_locid TYPE /sapapo/locid,
        lv_locno TYPE /sapapo/locno.
* Field-symbols
  FIELD-SYMBOLS:  <lfs_data> TYPE s_f4_data_loc,
                  <lfs_locid> TYPE s_locid,
                  <lfs_matid> TYPE s_matid.
*MOD-1771404
  FIELD-SYMBOLS:
* <lfs_zdm0705> TYPE zdm0705,
  <lfs_prod_data> TYPE s_f4_data_loc,
  <lfs_matloc> TYPE s_matloc.
*MOD-1771404
  TYPES : BEGIN OF s_view_data,
            pcode TYPE matnr, " Product
            maktx TYPE char40, " Prod Desc
            markt TYPE char4, " Market
            martx TYPE char20, " Market Desc
            pdctr TYPE char2, " Product Center
            pctxt TYPE char20, " Description
            whse1 TYPE char3, " Warehouse1
            whse2 TYPE char3, " Warehouse2
            prvar TYPE char18, " Product Variant
            vartx TYPE char30, " Variant Description
            status TYPE char1, " Status
          END OF s_view_data,
          lv_view_data TYPE STANDARD TABLE OF s_view_data.
* For Core Products
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
* Get the MATID for the Core Products
    PERFORM f_get_matid_for_prod  USING <lfs_data>-pcode
                                  CHANGING lv_matid_low.
    ls_matid-matid = lv_matid_low.
    ls_matid-matnr = <lfs_data>-pcode.
    APPEND ls_matid TO ct_matid_all.
    CLEAR ls_matid.
* Populate the Range Table with the Core Products
    PERFORM f_populate_range_table_matid  USING lv_matid_low
                                          CHANGING lr_matid.
    CLEAR lv_matid_low.
  ENDLOOP.
* For Product Variants
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
* Get the MATID for the Core Products
    PERFORM f_get_matid_for_prod USING <lfs_data>-prvar
    CHANGING lv_matid_low.
    ls_matid-matnr = <lfs_data>-prvar.
    ls_matid-matid = lv_matid_low.
    APPEND ls_matid TO ct_matid_all.
    CLEAR ls_matid.
* Populate the Range Table with the Core Products
    PERFORM f_populate_range_table_matid  USING lv_matid_low
                                          CHANGING lr_matid.
    CLEAR lv_matid_low.
  ENDLOOP.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lr_matid
    COMPARING sign option low high.
*MOD-1771404
* For Locations
  LOOP AT ct_loc_data ASSIGNING <lfs_data>.
* Get the Locid for the Location
    PERFORM f_get_locid_for_loc USING <lfs_data>-locno
                                CHANGING lv_locid_low
    lv_loctyp.
    <lfs_data>-loctyp = lv_loctyp.
    ls_locid-locno = <lfs_data>-locno.
    ls_locid-locid = lv_locid_low.
    APPEND ls_locid TO lt_locid_all.
    CLEAR ls_locid.
* Populate the Range Table with the Location
    PERFORM f_populate_range_table_locid  USING lv_locid_low
                                          CHANGING lr_locid.
    CLEAR: lv_locid_low, lv_loctyp.
  ENDLOOP.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lr_locid
    COMPARING sign option low high.
*MOD-1771404
*MOD-1771404
** Get all the transportation lanes for the above Product and Locations
* SELECT *
* FROM /sapapo/trprod
* INTO TABLE lt_trprod
* WHERE matid IN lr_matid AND
* ( locfr IN lr_locid OR
* locto IN lr_locid ).
* IF sy-subrc <> 0.
** No Transportation lane selected for deletion
* MESSAGE i047.
** EXIT.
* ENDIF.
*MOD-1771404
* Get all the transportation lanes for the above Product and Locations
  SELECT *
    FROM /sapapo/trprod
    INTO TABLE lt_trprod
    WHERE matid IN lr_matid AND
          ( locfr IN lr_locid OR
            locto IN lr_locid ).
  IF sy-subrc <> 0.
* No Transportation lane selected for deletion
    MESSAGE i047.
* EXIT.
  ENDIF.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lt_locid_all
    COMPARING locid locno.
*MOD-1771404
  LOOP AT lt_trprod ASSIGNING <lfs_trprod>.
    READ TABLE lt_locid_all ASSIGNING <lfs_locid>
                            WITH KEY locid = <lfs_trprod>-locto.
    IF sy-subrc = 0.
      ls_trprod-locto = <lfs_locid>-locno.
    ENDIF.
    CLEAR ls_loc_new.
    CALL FUNCTION '/SAPAPO/CMDS_LOC_SINGLE'
      EXPORTING
        iv_locid          = <lfs_trprod>-locfr
      IMPORTING
        es_loc            = ls_loc_new
      EXCEPTIONS
        empty_locid       = 1
        no_location_found = 2
        OTHERS            = 3.
    IF sy-subrc = 0.
      ls_trprod-locfr = ls_loc_new-locno.
    ENDIF.
*MOD-1771404
    CLEAR: lv_locid, lv_locno.
    SELECT SINGLE locid locno
      FROM /sapapo/loc
      INTO (lv_locid, lv_locno)
      WHERE locid = <lfs_trprod>-locto.
    IF sy-subrc = 0.
      IF ls_trprod-locto IS INITIAL.
        ls_trprod-locto = lv_locno.
      ENDIF.
    ENDIF.
*MOD-1771404
    READ TABLE ct_matid_all ASSIGNING <lfs_matid> WITH KEY
    matid = <lfs_trprod>-matid.
    IF sy-subrc = 0.
      ls_trprod-matid = <lfs_matid>-matnr.
    ENDIF.
    APPEND ls_trprod TO ct_tlane.
    CLEAR ls_trprod.
  ENDLOOP.
**MOD-1887120 Changes made after release of transport on 28.03
  DATA: l_wa_zdm0705 TYPE zdm0705.
  DATA: l_wa_zdu1002 TYPE zdu1002.
  DATA: lv_prvar TYPE char18.
  DATA: lv_indicator1 TYPE i.
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
**MOD-1887120
  DATA: lv_product TYPE char18.
* clear: l_wa_zdu1002.
* clear: l_wa_zdm0705.
* SORT it_zdm0705 BY prvar parpr.
** DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* LOOP AT IT_ZDM0705 INTO L_WA_ZDM0705.
* READ TABLE IT_ZDU1002 INTO L_WA_ZDU1002
* WITH KEY PRVAR = L_WA_ZDM0705-PRVAR BINARY SEARCH.
* IF SY-SUBRC = 0.
* IF L_WA_ZDU1002-STATUS NE 'I'.
* lv_indicator1 = 1.
* ENDIF.
* ENDIF.
* ENDLOOP.
***MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* READ TABLE IT_ZDM0705 ASSIGNING <LFS_ZDM0705>
* WITH KEY PRVAR = L_WA_ZDU1002-PRVAR BINARY SEARCH.
** LOOP AT IT_ZDM0705 into l_wa_zdm0705
** WHERE prvar = l_wa_zdu1002-prvar.
* IF SY-SUBRC = 0.
* <lfs_zdm0705>-dmoap = 'X'.
** l_wa_zdm0705-dmoap = 'X'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* ENDIF.
* ENDLOOP.
** ENDLOOP.
* endif.
*
**MOD-1887120 Changes made after release of transport on 28.03
  IF lv_indicator1 = 1.
    LOOP AT it_prod_var ASSIGNING <lfs_prod_data>.
      READ TABLE it_zdm0705 INTO l_wa_zdm0705
        WITH KEY  parpr = <lfs_prod_data>-pcode
                  dmoap = 'X'.
      IF sy-subrc = 0.
        DELETE it_core_prod WHERE pcode = <lfs_prod_data>-pcode.
* Indicates that the core product should not be included in
* transportation lane and /sapapo/bp2 transaction.
        READ TABLE gt_matloc ASSIGNING <lfs_matloc>
          WITH KEY  matnr = <lfs_prod_data>-pcode
                    locno = <lfs_prod_data>-locno.
        IF sy-subrc NE 0.
          READ TABLE gt_matloc ASSIGNING <lfs_matloc>
            WITH KEY  matnr = <lfs_prod_data>-prvar
                      locno = <lfs_prod_data>-locno.
          IF sy-subrc NE 0.
            <lfs_prod_data>-lv_exist = 1.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.
  DELETE it_prod_var WHERE lv_exist = 1.
*MOD-1887120 Changes made after release of transport on 28.03
  LOOP AT ct_tlane INTO ls_trprod.
    READ TABLE it_zdm0705 INTO l_wa_zdm0705
      WITH KEY  prvar = ls_trprod-matid
                dmoap = 'X'.
    IF sy-subrc = 0.
      ls_trprod-matid = 'X'.
      MODIFY ct_tlane FROM ls_trprod TRANSPORTING matid.
      COMMIT WORK AND WAIT.
    ELSE.
      READ TABLE it_zdm0705 INTO l_wa_zdm0705
        WITH KEY  parpr = ls_trprod-matid
                  dmoap = 'X'.
      IF sy-subrc = 0.
        ls_trprod-matid = 'X'.
        MODIFY ct_tlane FROM ls_trprod TRANSPORTING matid.
        COMMIT WORK AND WAIT.
      ENDIF.
    ENDIF.
  ENDLOOP.
  DELETE ct_tlane WHERE matid = 'X'.
**MOD-1887120 Changes made after release of transport on 28.03
  IF p_test IS NOT INITIAL.
    EXIT.
  ENDIF.
*MOD-1771404
* Delete SAP Table /SAPAPO/TRPROD
  DELETE /sapapo/trprod FROM TABLE lt_trprod.
  IF sy-subrc = 0.
    COMMIT WORK.
  ELSE.
    ROLLBACK WORK.
  ENDIF.
*MOD-1771404
ENDFORM. " f_delete_product_from_tlane
*&---------------------------------------------------------------------*
*& Form f_get_matid_for_prod
*&---------------------------------------------------------------------*
* Get the MATID for the material passed
*----------------------------------------------------------------------*
* -->IV_PRD Product (Material Number)
* <--CV_MATID MATID for the above material no
*----------------------------------------------------------------------*
FORM f_get_matid_for_prod USING iv_prd TYPE any
                          CHANGING cv_matid TYPE /sapapo/matid.
* Local Data Declaration
  DATA: lv_matnr TYPE /sapapo/matnr.
  CHECK NOT iv_prd IS INITIAL.
  CLEAR lv_matnr.
  lv_matnr = iv_prd.
  CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
    EXPORTING
      iv_matnr        = lv_matnr
    IMPORTING
      ev_matid        = cv_matid
    EXCEPTIONS
      matid_not_found = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    CLEAR cv_matid.
  ENDIF.
ENDFORM. " f_get_matid_for_prod
*&---------------------------------------------------------------------*
*& Form f_get_locid_for_loc
*&---------------------------------------------------------------------*
* Get the LOCID for the Location passed
*----------------------------------------------------------------------*
* -->IV_MKT Location Number
* <--CV_LOCID LOCID for the above Location
* <--CV_LOCTYP Location Type
*----------------------------------------------------------------------*
FORM f_get_locid_for_loc  USING iv_mkt TYPE /sapapo/locno
                          CHANGING  cv_locid_low TYPE /sapapo/locid
                                    cv_loctyp TYPE /sapapo/c_loctype.
* Local Data Declaration
  DATA: ls_loc TYPE /sapapo/loc.
  CHECK NOT iv_mkt IS INITIAL.
  CLEAR ls_loc.
  CALL FUNCTION '/SAPAPO/LOC_LOCNO_GET_LOCID'
    EXPORTING
      iv_locno           = iv_mkt
    IMPORTING
*     ev_locid           = cv_locid_low
      es_location        = ls_loc
    EXCEPTIONS
      location_not_found = 1
      not_qualified      = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    CLEAR cv_locid_low.
  ELSE.
    cv_loctyp = ls_loc-loctype.
    cv_locid_low = ls_loc-locid.
  ENDIF.
ENDFORM. " f_get_locid_for_loc
*&---------------------------------------------------------------------*
*& Form f_populate_range_table_matid
*&---------------------------------------------------------------------*
* Populate the Range thable wiht the values passed
*----------------------------------------------------------------------*
* -->IV_VALUE_LOW Lower Value of the Range
* <--ES_RANGE Structure of type Range
* <--CT_RANGE_TAB Table of type Range
*----------------------------------------------------------------------*
FORM f_populate_range_table_matid USING iv_value_low TYPE any
                                  CHANGING ct_range_tab TYPE t_matid_range.
* Local Data Declaration
  DATA: ls_locid TYPE s_range_locid.
  ls_locid-sign = gc_sign_i.
  ls_locid-option = gc_option_eq.
  ls_locid-low = iv_value_low.
  APPEND ls_locid TO ct_range_tab.
  CLEAR ls_locid.
ENDFORM. " f_populate_range_table_matid
*&---------------------------------------------------------------------*
*& Form f_set_deletion_flag
*&---------------------------------------------------------------------*
* Set the deletion flag for the Products to be deleted
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the location details
*----------------------------------------------------------------------*
FORM f_set_deletion_flag USING  it_core_prod TYPE t_f4_data_loc
                                it_prod_var TYPE t_f4_data_loc
                                it_loc_data TYPE t_f4_data_loc
                                ct_matloc TYPE t_matloc.
* Local Data Declaration
  DATA: lv_own_sys TYPE bapiapologsys,
        lv_matnr TYPE /sapapo/matnr,
        lt_product TYPE STANDARD TABLE OF bapi10001key,
        ls_product TYPE bapi10001key,
        ls_prod_key TYPE bapi_prodloc_key,
        lt_prod_key TYPE STANDARD TABLE OF bapi_prodloc_key,
        lt_matloc TYPE t_matloc,
        lt_return TYPE STANDARD TABLE OF bapiret2.
*       Field Symbols
        FIELD-SYMBOLS: <lfs_mat> TYPE s_f4_data_loc,
        <lfs_product> TYPE bapi10001key,
        <lfs_loc> TYPE s_f4_data_loc,
        <lfs_return> TYPE bapiret2.
*MOD-1771404
*MOD-1887120
  SORT it_prod_var BY locno
                      pcode
                      prvar.
  DELETE ADJACENT DUPLICATES FROM it_prod_var
    COMPARING locno
              pcode
              prvar.
  SORT it_loc_data BY locno
                      pcode
                      prvar
                      loctyp
                      whse1.
  DELETE ADJACENT DUPLICATES FROM it_loc_data
    COMPARING locno
              pcode
              prvar
              loctyp
              whse1.
*MOD-1771404
* Populate the Core Product
  LOOP AT it_core_prod ASSIGNING <lfs_mat>.
    LOOP AT it_loc_data ASSIGNING <lfs_loc>.
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
        WITH KEY  matnr = <lfs_mat>-pcode
                  locno = <lfs_loc>-locno.
      IF sy-subrc = 0.
        ls_prod_key-loctype = <lfs_loc>-loctyp.
        ls_prod_key-product = <lfs_mat>-pcode.
        ls_prod_key-location = <lfs_loc>-locno.
        APPEND ls_prod_key TO lt_prod_key.
        CLEAR ls_prod_key.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
*MOD-1887120
  SORT lt_prod_key BY product location loctype.
  DELETE ADJACENT DUPLICATES FROM lt_prod_key
    COMPARING product
              location
              loctype.
*MOD-1887120
* Populate the Product Variant
  LOOP AT it_prod_var ASSIGNING <lfs_mat>.
    LOOP AT it_loc_data ASSIGNING <lfs_loc>.
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
        WITH KEY  matnr = <lfs_mat>-prvar
                  locno = <lfs_loc>-locno.
      IF sy-subrc = 0.
        ls_prod_key-loctype = <lfs_loc>-loctyp.
        ls_prod_key-product = <lfs_mat>-prvar.
        ls_prod_key-location = <lfs_loc>-locno.
        APPEND ls_prod_key TO lt_prod_key.
        CLEAR ls_prod_key.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
*MOD-1771404
*MOD-1887120
  SORT lt_prod_key BY product location loctype.
*MOD-1887120
  DELETE ADJACENT DUPLICATES FROM lt_prod_key
    COMPARING product
              location
              loctype.
*MOD-1771404
* Get the Own Logical system name
  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system             = lv_own_sys
    EXCEPTIONS
      own_logical_system_not_defined = 1
      OTHERS                         = 2.
  IF sy-subrc <> 0.
    CLEAR lv_own_sys.
  ENDIF.
* This BAPI will set the deletion flag for the materials selected
* for deletion in the SAP table /SAPAPO/MATLOC
  CALL FUNCTION 'BAPI_PRDSRVAPS_REMOVEITEMS'
    EXPORTING
      logical_system        = lv_own_sys
      business_system_group = gc_bs_group
      commit_control        = space
    TABLES
      product_location_keys = lt_prod_key
      return                = lt_return.
  CLEAR gv_flg_error.
  LOOP AT lt_return ASSIGNING <lfs_return>
    WHERE type = gc_error OR
          type = gc_abend .
    gv_flg_error = gc_set.
    EXIT.
  ENDLOOP.
  IF gv_flg_error = gc_set.
* Product deletion failed for all the selected products
    MESSAGE i048.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
* Delete the Products using the SAP report /SAPAPO/DELETE_PRODUCTS
    SUBMIT /sapapo/delete_products AND RETURN.
    SORT lt_prod_key BY product.
    DELETE ADJACENT DUPLICATES FROM lt_prod_key
      COMPARING product.
* Look for products which dont have a location
    SELECT matnr locno
      INTO TABLE lt_matloc
      FROM /sapapo/v_matloc
      FOR ALL ENTRIES IN gt_prod_data
      WHERE matnr = gt_prod_data-pcode OR
            matnr = gt_prod_data-prvar.
    SORT lt_matloc BY matnr.
    DELETE ADJACENT DUPLICATES FROM lt_matloc
    COMPARING matnr.
    LOOP AT gt_prod_data ASSIGNING <lfs_mat>.
      READ TABLE lt_matloc TRANSPORTING NO FIELDS
        WITH KEY matnr = <lfs_mat>-pcode
        BINARY SEARCH.
      IF sy-subrc <> 0.
        ls_product-product = <lfs_mat>-pcode.
        APPEND ls_product TO lt_product.
      ENDIF.
      READ TABLE lt_matloc TRANSPORTING NO FIELDS
        WITH KEY matnr = <lfs_mat>-prvar
        BINARY SEARCH.
      IF sy-subrc <> 0.
        ls_product-product = <lfs_mat>-prvar.
        APPEND ls_product TO lt_product.
      ENDIF.
    ENDLOOP.
    SORT lt_product.
    DELETE ADJACENT DUPLICATES FROM lt_product COMPARING ALL FIELDS.
* In case of copre products check if they have any active variants
* before deleting them
    LOOP AT lt_product ASSIGNING <lfs_product>.
      SELECT SINGLE matnr INTO lv_matnr
        FROM /sapapo/matkey
        WHERE matnr = <lfs_product>-product.
      IF sy-subrc <> 0.
        CLEAR <lfs_product>-product.
        CONTINUE.
      ENDIF.
      FIND gc_dot IN <lfs_product>-product.
      IF sy-subrc <> 0.
        LOOP AT gt_view_data TRANSPORTING NO FIELDS
          WHERE pcode = <lfs_product>-product AND
                status <> 'I'.
          CLEAR <lfs_product>-product.
          EXIT.
        ENDLOOP.
      ENDIF.
    ENDLOOP.
    DELETE lt_product WHERE product IS INITIAL.
    REFRESH gt_product.
    gt_product = lt_product.
    IF lt_product IS NOT INITIAL.
* Delete the products which dont have any locations
      CALL FUNCTION 'BAPI_PRDSRVAPS_DELMULTI'
        EXPORTING
          logical_system        = lv_own_sys
          business_system_group = gc_bs_group
          commit_control        = space
        TABLES
          product_keys          = lt_product
          return                = lt_return.
      CLEAR gv_flg_error.
      LOOP AT lt_return ASSIGNING <lfs_return>
        WHERE type = gc_error OR
              type = gc_abend .
        gv_flg_error = gc_set.
        EXIT.
      ENDLOOP.
      IF gv_flg_error = gc_set.
* Product deletion failed for all the selected products
        MESSAGE i048.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
* Delete the Products using the SAP report /SAPAPO/DELETE_PRODUCTS
        SUBMIT /sapapo/delete_products AND RETURN.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM. " f_set_deletion_flag
*&---------------------------------------------------------------------*
*& Form f_display_alv
*&---------------------------------------------------------------------*
* Display ALV report with selected Product and Locations
*----------------------------------------------------------------------*
* -->IT_PROD_DATA Itab with the Product and Location combs
* <--CT_FCAT Itab for Field Catelogue
* <--CT_MATID_ALL Itab with Matid for all the Products
*----------------------------------------------------------------------*
FORM f_display_alv  USING it_prod_data TYPE t_f4_data_loc
                          ct_matloc TYPE t_matloc
                    CHANGING  ct_fcat TYPE slis_t_fieldcat_alv
                              ct_matid_all TYPE t_matid.
*Local constants:
  CONSTANTS:
  lc_tabprd TYPE tabname VALUE 'LT_DISPLAYPRD'.
* Local Data Declaration
  DATA: ls_display TYPE s_display,
        ls_product TYPE bapi10001key,
        ls_layout TYPE slis_layout_alv,
        ls_displayprd TYPE s_displayprd,
        lt_displayprd TYPE t_displayprd,
        lv_flg_del TYPE char1,
        lv_flg_prd TYPE flag,
        lv_repid TYPE syrepid,
        lt_events TYPE STANDARD TABLE OF slis_alv_event,
        lt_eventsprd TYPE STANDARD TABLE OF slis_alv_event,
        lt_display TYPE STANDARD TABLE OF s_display,
        lt_prod_1 TYPE STANDARD TABLE OF s_f4_data_loc,
        lt_prod_2 TYPE STANDARD TABLE OF s_f4_data_loc,
        lt_matloc TYPE STANDARD TABLE OF /sapapo/v_matloc,
        lt_matlocdel TYPE STANDARD TABLE OF /sapapo/matloc,
        ls_matloc TYPE /sapapo/matloc,
        ls_matid TYPE s_matid.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid,
        lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3, " Warehouse2
        l_wa_matloc TYPE s_matloc.
  DATA: l_wa_display TYPE s_display.
        lv_repid = sy-repid.
* Field-Symbols
  FIELD-SYMBOLS:  <lfs_prod> TYPE s_f4_data_loc,
                  <lfs_prod_data> TYPE s_f4_data_loc,
                  <lfs_display> TYPE s_display,
                  <lfs_displayprd> TYPE s_displayprd,
                  <lfs_matloc> TYPE /sapapo/v_matloc,
                  <lfs_matid> TYPE s_matid.
**MOD-1887120
* LOOP AT IT_PROD_DATA ASSIGNING <lfs_prod_data>.
* READ TABLE ct_matloc into l_wa_matloc
* WITH KEY matnr = <lfs_prod_data>-prvar
* locno = <lfs_prod_data>-locno.
* IF sy-subrc = 0.
* CLEAR: ls_display, ls_displayprd.
* ls_display-prod = <lfs_prod_data>-prvar.
* ls_display-p_des = <lfs_prod_data>-vartx.
**MOD-1771404
** ls_display-loc = <lfs_prod>-locno.
** ls_display-l_des = <lfs_prod>-desc.
**MOD-1771404
* ls_display-loc = l_wa_matloc-LOCNO.
* ls_display-l_des = lv_ware_desc.
* APPEND ls_display TO lt_display.
* <lfs_prod_data>-locno = l_wa_matloc-LOCNO.
* ENDIF.
* READ TABLE gt_product TRANSPORTING NO FIELDS
* WITH KEY product = <lfs_prod_data>-prvar.
* IF sy-subrc = 0.
* ls_displayprd-prod = <lfs_prod_data>-prvar.
* ls_displayprd-p_des = <lfs_prod_data>-vartx.
* APPEND ls_displayprd TO lt_displayprd.
* ENDIF.
* ENDLOOP.
**MOD-1887120
  lt_prod_1 = it_prod_data.
  lt_prod_2 = it_prod_data.
  CLEAR lv_flg_del.
*MOD-1887120
* SORT lt_prod_1 BY prvar locno.
  SORT lt_prod_1 BY locno pcode prvar pdctr status whse1 whse2.
* DELETE ADJACENT DUPLICATES FROM lt_prod_1 COMPARING prvar locno.
  DELETE ADJACENT DUPLICATES FROM lt_prod_1
    COMPARING locno
              pcode
              prvar
              pdctr
              status
              whse1
              whse2.
*MOD-1887120
*MOD-1887120
* SORT lt_prod_2 BY pcode locno.
  SORT lt_prod_2 BY locno pcode prvar pdctr status whse1 whse2.
* DELETE ADJACENT DUPLICATES FROM lt_prod_2 COMPARING pcode locno.
  DELETE ADJACENT DUPLICATES FROM lt_prod_2
    COMPARING locno
              pcode
              prvar
              pdctr
              status
              whse1
              whse2.
*MOD-1887120
**MOD-1771404
* PERFORM F_GET_LOC_DESC USING IT_PROD_DATA
* CHANGING lv_ware_desc.
**MOD-1771404
  LOOP AT lt_prod_1 ASSIGNING <lfs_prod>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
        WITH KEY  matnr = <lfs_prod>-prvar
                  locno = s_mkt-low.
*MOD-1887120
* locno = <lfs_prod>-locno.
*MOD-1887120
      IF sy-subrc = 0.
        CLEAR:  ls_display, ls_displayprd.
                ls_display-prod = <lfs_prod>-prvar.
                ls_display-p_des = <lfs_prod>-vartx.
*MOD-1771404
* ls_display-loc = <lfs_prod>-locno.
* ls_display-l_des = <lfs_prod>-desc.
*MOD-1771404
*MOD-1887120
        ls_display-loc = s_mkt-low.
*MOD-1887120
*MOD-1887120
* ls_display-loc = <lfs_prod>-locno.
*MOD-1887120
**MOD-1887120
        PERFORM f_get_loc_desc CHANGING lv_ware_desc.
**MOD-1887120
        ls_display-l_des = lv_ware_desc.
        APPEND ls_display TO lt_display.
      ENDIF.
      READ TABLE gt_product TRANSPORTING NO FIELDS
        WITH KEY product = <lfs_prod>-prvar.
      IF sy-subrc = 0.
        ls_displayprd-prod = <lfs_prod>-prvar.
        ls_displayprd-p_des = <lfs_prod>-vartx.
        APPEND ls_displayprd TO lt_displayprd.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
  LOOP AT lt_prod_2 ASSIGNING <lfs_prod>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
        WITH KEY  matnr = <lfs_prod>-pcode
                  locno = s_mkt-low.
*MOD-1887120
* locno = <lfs_prod>-locno.
*MOD-1887120
      IF sy-subrc = 0.
        CLEAR: ls_display, lv_flg_prd.
        LOOP AT gt_view_data TRANSPORTING NO FIELDS
          WHERE pcode = <lfs_prod>-pcode AND
                status <> 'I'.
          lv_flg_prd = 'X'.
        ENDLOOP.
        IF lv_flg_prd IS INITIAL.
          ls_display-prod = <lfs_prod>-pcode.
          ls_display-p_des = <lfs_prod>-maktx.
* ls_display-loc = <lfs_prod>-locno.
* ls_display-l_des = <lfs_prod>-desc.
*MOD-1887120
          ls_display-loc = s_mkt-low.
*MOD-1887120
* ls_display-loc = <lfs_prod>-locno.
*MOD-1887120
**MOD-1887120
* PERFORM F_GET_LOC_DESC USING lt_prod_2
          PERFORM f_get_loc_desc CHANGING lv_ware_desc.
**MOD-1887120
          ls_display-l_des = lv_ware_desc.
          APPEND ls_display TO lt_display.
          READ TABLE gt_product TRANSPORTING NO FIELDS
            WITH KEY product = <lfs_prod>-pcode.
          IF sy-subrc = 0.
            ls_displayprd-prod = <lfs_prod>-pcode.
            ls_displayprd-p_des = <lfs_prod>-maktx.
            APPEND ls_displayprd TO lt_displayprd.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
  LOOP AT gt_product INTO ls_product.
    ls_displayprd-prod = ls_product.
    FIND gc_dot IN ls_product.
    IF sy-subrc = 0.
      READ TABLE gt_prod_data ASSIGNING <lfs_prod>
        WITH KEY prvar = ls_product-product.
      IF sy-subrc = 0.
        ls_displayprd-p_des = <lfs_prod>-vartx.
      ENDIF.
    ELSE.
      READ TABLE it_prod_data ASSIGNING <lfs_prod>
        WITH KEY pcode = ls_product-product.
      IF sy-subrc = 0.
        ls_displayprd-p_des = <lfs_prod>-maktx.
      ENDIF.
    ENDIF.
  ENDLOOP.
  SORT lt_display BY prod loc.
*MOD-1887120
  DELETE ADJACENT DUPLICATES FROM lt_display
    COMPARING prod loc.
*MOD-1887120
**MOD-1887120
*LOOP AT lt_display into l_wa_display.
* READ TABLE CT_MATLOC INTO L_WA_MATLOC
* WITH KEY matnr = l_wa_display-prod
* locno = l_wa_display-loc.
* IF sy-subrc ne 0.
* l_wa_display-status = 'X'.
* modify lt_display from l_wa_display transporting status.
* ENDIF.
*ENDLOOP.
*DELETE lt_display WHERE status = 'X'.
**MOD-1887120
  SORT lt_displayprd BY prod.
  DELETE ADJACENT DUPLICATES FROM lt_displayprd
    COMPARING prod.
  IF lt_display IS INITIAL.
    EXIT.
  ENDIF.
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
    EXPORTING
      i_callback_program       = lv_repid
      i_callback_user_command  = gc_show_log
      i_callback_pf_status_set = gc_set_pf.
* Populate the top of page event.
  PERFORM f_get_events CHANGING lt_events.
  REFRESH gt_fcat.
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_3 gc_prodct gc_tabname text-003 gc_len_prod gc_left,
  gc_pos_4 gc_prod_des gc_tabname text-004 gc_len_p_des gc_left,
  gc_pos_5 gc_loctn gc_tabname text-005 gc_len_loc gc_left,
  gc_pos_6 gc_loc_des gc_tabname text-006 gc_len_l_des gc_left.
* If not test mode then display status of deletion
  IF p_test <> gc_set.
    PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
    gc_pos_2 gc_status gc_tabname text-019 gc_len_stat gc_left,
    gc_pos_1 gc_light gc_tabname space gc_len_lt gc_center.
    SELECT *
      FROM /sapapo/v_matloc
      INTO TABLE lt_matloc
      FOR ALL ENTRIES IN lt_display
      WHERE matnr = lt_display-prod AND
            locno = lt_display-loc.
*Check if the product is deleted.
    LOOP AT lt_display ASSIGNING <lfs_display>.
      READ TABLE lt_matloc ASSIGNING <lfs_matloc>
        WITH KEY  matnr = <lfs_display>-prod
                  locno = <lfs_display>-loc.
      IF sy-subrc = 0.
        <lfs_display>-status = text-020.
        <lfs_display>-light = gc_red.
        CLEAR <lfs_matloc>-lvorm.
        MOVE-CORRESPONDING <lfs_matloc> TO ls_matloc.
        APPEND ls_matloc TO lt_matlocdel.
        lv_flg_del = gc_set.
      ELSE.
        <lfs_display>-status = text-021.
        <lfs_display>-light = gc_green.
      ENDIF.
    ENDLOOP.
    IF lv_flg_del = gc_set. " Deletion failed for atleast one prod
* Deletion failed for some products.
      MODIFY /sapapo/matloc FROM TABLE lt_matlocdel.
      MESSAGE s064.
    ELSE.
* Deletion successful for all the products
      MESSAGE s065.
    ENDIF.
  ENDIF. " For IF p_test <> gc_set.
* Display the ALV List Display
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
    EXPORTING
      is_layout                  = ls_layout
      it_fieldcat                = gt_fcat
      i_tabname                  = gc_tabname
      it_events                  = lt_events
    TABLES
      t_outtab                   = lt_display
    EXCEPTIONS
      program_error              = 1
      maximum_of_appends_reached = 2
      OTHERS                     = 3.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
  IF p_test IS INITIAL.
    PERFORM f_get_events_prd CHANGING lt_eventsprd.
    REFRESH gt_fcat.
* Populate the Field Catelog
    PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
    gc_pos_3 gc_prodct lc_tabprd text-003 gc_len_prod gc_left,
    gc_pos_4 gc_prod_des lc_tabprd text-004 gc_len_p_des gc_left.
    IF p_test <> gc_set.
      PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
      gc_pos_2 gc_status lc_tabprd text-019 gc_len_stat gc_left,
      gc_pos_1 gc_light lc_tabprd space gc_len_lt gc_center.
* Check the Products from the /SAPAPO/MATKEY to check for deletion
      SELECT matid matnr INTO TABLE ct_matid_all
        FROM /sapapo/matkey
        FOR ALL ENTRIES IN lt_displayprd
        WHERE matnr = lt_displayprd-prod.
      LOOP AT lt_displayprd ASSIGNING <lfs_displayprd>.
        READ TABLE ct_matid_all INTO ls_matid
          WITH KEY matnr = <lfs_displayprd>-prod.
        IF sy-subrc = 0.
          <lfs_displayprd>-status = text-020.
          <lfs_displayprd>-light = gc_red.
          lv_flg_del = gc_set.
        ELSE.
          <lfs_displayprd>-status = text-021.
          <lfs_displayprd>-light = gc_green.
        ENDIF.
      ENDLOOP.
      IF lv_flg_del = gc_set. " Deletion failed for atleast one prod
* Deletion failed for some products.
        MESSAGE s064.
      ELSE.
* Deletion successful for all the products
        MESSAGE s065.
      ENDIF.
    ENDIF.
* Display the ALV List Display
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
      EXPORTING
        is_layout                  = ls_layout
        it_fieldcat                = gt_fcat
        i_tabname                  = lc_tabprd
        it_events                  = lt_eventsprd
      TABLES
        t_outtab                   = lt_displayprd
      EXCEPTIONS
        program_error              = 1
        maximum_of_appends_reached = 2
        OTHERS                     = 3.
    IF sy-subrc <> 0.
* Error in displaying end report
      MESSAGE e062.
      LEAVE LIST-PROCESSING.
    ENDIF.
  ENDIF.
* Displays the ALV list
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'
    EXCEPTIONS
      program_error = 1
      OTHERS        = 2.
ENDFORM. " f_display_alv
*&---------------------------------------------------------------------*
*& Form f_get_events
*&---------------------------------------------------------------------*
* Get the TOP OF PAGE event
*----------------------------------------------------------------------*
* <--CT_EVENTS Itab with the events to be triggred
*----------------------------------------------------------------------*
FORM f_get_events CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
  WITH KEY name = slis_ev_top_of_page
  BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = slis_ev_top_of_page.
  ENDIF.
ENDFORM. " f_get_events
*&---------------------------------------------------------------------*
*& Form vpopulate_fieldcat
*&---------------------------------------------------------------------*
* Populate the fieldcatelog for ALV report
*----------------------------------------------------------------------*
* -->IV_CPOS Column Position
* -->IV_FNAM Field Name
* -->IV_TABNAM Internal Table Name
* -->IV_TITLE Column Title
* -->IV_OPT_LEN Output Length
* -->IV_JUSTIFY Justify left / right
*----------------------------------------------------------------------*
FORM f_populate_fieldcat USING  iv_cpos TYPE lvc_colpos
                                iv_fnam TYPE lvc_fname
                                iv_tabnam TYPE lvc_tname
                                iv_title TYPE reptext
                                iv_opt_len TYPE lvc_outlen
                                iv_justify TYPE lvc_just.
* Local Data Declaration
  DATA: ls_fcat TYPE slis_fieldcat_alv.
  CLEAR ls_fcat.
  ls_fcat-col_pos = iv_cpos.
  ls_fcat-fieldname = iv_fnam.
  ls_fcat-tabname = iv_tabnam.
  ls_fcat-reptext_ddic = iv_title.
  ls_fcat-outputlen = iv_opt_len.
  ls_fcat-just = iv_justify.
  IF iv_fnam = gc_light.
    ls_fcat-icon = gc_set.
  ENDIF.
  APPEND ls_fcat TO gt_fcat.
ENDFORM. " f_populate_fieldcat
*&---------------------------------------------------------------------*
*& Form F_SET_PF
*&---------------------------------------------------------------------*
* Set the PF Status for the display and log screen
*----------------------------------------------------------------------*
* -->CT_EXTAB
*----------------------------------------------------------------------*
FORM f_set_pf USING ct_extab TYPE slis_t_extab.             "#EC CALLED
* Dynamic perform required to set custom PF status for ALV report
                                                            "#EC *
* CT_EXTAB is required as per the standard exit perform of ALVgrid fm
* Local Data Declation.
  DATA: ls_extab TYPE slis_extab,
        lt_extab TYPE STANDARD TABLE OF slis_extab.
* If the Test Run check box is selected then disable the Log Icons
  IF p_test = gc_set.
    ls_extab-fcode = gc_td_log.
    APPEND ls_extab TO lt_extab.
    CLEAR ls_extab.
    SET PF-STATUS 'PF_DISPLAY_LOG' EXCLUDING lt_extab.
  ENDIF.
  IF gt_tlane IS INITIAL.
    ls_extab-fcode = gc_tlane.
    APPEND ls_extab TO lt_extab.
    CLEAR ls_extab.
  ENDIF.
  SET PF-STATUS 'PF_DISPLAY_LOG' EXCLUDING lt_extab.
ENDFORM. " F_SET_PF
*&---------------------------------------------------------------------*
*& Form f_show_log
*&---------------------------------------------------------------------*
* Display Log report using
*----------------------------------------------------------------------*
* --> iv_ucomm OK Code for the user command
* --> is_selfield Selected field information
*----------------------------------------------------------------------*
FORM f_show_log USING iv_ucomm TYPE sy-ucomm
                      is_selfield TYPE slis_selfield.        "#EC CALLED
* Dynamic perform for displaying log based on user action
                                                            "#EC *
* IS_SELFIELD is passed as per the standard exit perform for ALVgrid
  IF iv_ucomm = gc_td_log.
* Local Data Delaration.
    DATA: lt_msg_dtl TYPE STANDARD TABLE OF bdcmsgcoll,
          lv_mode TYPE char1,
          lv_error TYPE char1,
          lv_date TYPE char10,
          lv_time TYPE char8,
          lv_date_to TYPE char10,
          lv_time_to TYPE char8.
* Field-Symbols
    FIELD-SYMBOLS: <lfs_msg_dtl> TYPE bdcmsgcoll.
    REFRESH gt_bdc_tab.
    CLEAR: lv_date, lv_time.
    CONCATENATE gv_date_before+6(2) gv_date_before+4(2)
    gv_date_before+0(4) INTO lv_date SEPARATED BY gc_dot.
    CONCATENATE gv_time_before+0(2) gv_time_before+2(2)
    gv_time_before+4(2) INTO lv_time SEPARATED BY gc_colon.
    CONCATENATE sy-datum+6(2) sy-datum+4(2)
    sy-datum+0(4) INTO lv_date_to SEPARATED BY gc_dot.
    CONCATENATE sy-uzeit+0(2) sy-uzeit+2(2)
    sy-uzeit+4(2) INTO lv_time_to SEPARATED BY gc_colon.
* Populate the BDC Table for SLG1 Transaction
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLSLG3' '0100',
    ' ' 'BDC_CURSOR' 'BALHDR-ALPROG',
* ' ' 'BDC_OKCODE' '=SELE',
    ' ' 'BALHDR-OBJECT' '*',
    ' ' 'BALHDR-SUBOBJECT' '*',
    ' ' 'BALHDR-EXTNUMBER' '*',
    ' ' 'BALHDR-ALDATE' lv_date,
    ' ' 'BALHDR-ALTIME' lv_time,
    ' ' '*BALHDR-ALDATE' lv_date_to,
    ' ' '*BALHDR-ALTIME' lv_time_to,
    ' ' 'BALHDR-ALUSER' sy-uname,
    ' ' 'BALHDR-ALTCODE' '*',
    ' ' 'BALHDR-ALPROG' '*',
    ' ' 'MODE_ALL' 'X',
    ' ' 'PROBCLASS_4' 'X'.
    CLEAR lv_error.
    lv_mode = gc_foreground.
* Call Transaction SLG1 for displaying the LOG for the action.
    CALL TRANSACTION gc_slg1
      USING gt_bdc_tab
      MODE lv_mode
      MESSAGES INTO lt_msg_dtl.
    LOOP AT lt_msg_dtl ASSIGNING <lfs_msg_dtl>
      WHERE msgtyp = gc_error OR
            msgtyp = gc_abend.
      lv_error = gc_set.
      EXIT.
    ENDLOOP.
    IF lv_error = gc_set.
* Error while displaying Log.
      MESSAGE i049.
    ENDIF.
  ELSEIF iv_ucomm = gc_tlane.
* Display Tlane deletion data.
    PERFORM f_display_tlane USING gt_tlane.
  ENDIF.
ENDFORM. " f_show_log
*&---------------------------------------------------------------------*
*& Form top_of_page
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for ALV report
*----------------------------------------------------------------------*
FORM top_of_page.                                           "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
        lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_h gc_space text-015,
  gs_type_s text-007 sy-uname,
  gs_type_s text-008 lv_date,
  gs_type_s text-009 lv_time,
  gs_type_s gc_space gc_space.
* If Test Run then select
  IF p_test = gc_set. " IF test run
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-013,
    gs_type_a space text-014.
  ELSEIF gv_msg_error = gc_set. "IF error in call trns
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-018.
  ELSEIF gv_flg_error = gc_set. " IF error in deletion
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-012.
  ELSEIF gv_flg_error <> gc_set. " IF no error
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-011.
  ENDIF.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " top_of_page
*&---------------------------------------------------------------------*
*& Form f_populate_top_page
*&---------------------------------------------------------------------*
* Populate the Header for ALV top of page with parameters passed
*----------------------------------------------------------------------*
* -->IV_TYPE Line Type
* -->IV_KEY Key
* -->IV_INFO Info to be printed
*----------------------------------------------------------------------*
FORM f_populate_top_page USING  iv_type TYPE char1
                                iv_key TYPE any
                                iv_info TYPE any.
* Local Data Declaration
  DATA: ls_top_page TYPE slis_listheader.
  CLEAR ls_top_page.
  ls_top_page-typ = iv_type.
  ls_top_page-key = iv_key.
  ls_top_page-info = iv_info.
  APPEND ls_top_page TO gt_top_page.
ENDFORM. " f_populate_top_page
*&---------------------------------------------------------------------*
*& Form f_populate_range_table_LOCID
*&---------------------------------------------------------------------*
* Populate the Range thable wiht the values passed
*----------------------------------------------------------------------*
* -->IV_VALUE_LOW Lower Value of the Range
* <--CT_RANGE_TAB Table of type Range
*----------------------------------------------------------------------*
FORM f_populate_range_table_locid USING iv_value_low TYPE any
                                  CHANGING ct_range_tab TYPE t_locid_range.
* Local Data Declaration
  DATA: ls_locid TYPE s_range_locid.
  ls_locid-sign = gc_sign_i.
  ls_locid-option = gc_option_eq.
  ls_locid-low = iv_value_low.
  APPEND ls_locid TO ct_range_tab.
  CLEAR ls_locid.
ENDFORM. " f_populate_range_table_LOCID
*&---------------------------------------------------------------------*
*& Form f_display_tlane
*&---------------------------------------------------------------------*
* Display Transportation lane deletion data
*----------------------------------------------------------------------*
* -->CT_TLANE Itab with details of Tlane deleted
*----------------------------------------------------------------------*
FORM f_display_tlane USING ct_tlane TYPE t_tlane_data.
* Local Data Declaration
  DATA: lt_tl_events TYPE STANDARD TABLE OF slis_alv_event.
* Populate the top of page event.
  PERFORM f_get_events_tl CHANGING lt_tl_events.
  REFRESH gt_fcat.
*MOD-1887120
  SORT ct_tlane BY locfr locto matid.
*MOD-1887120
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_1 gc_locfr gc_tl_tab text-023 gc_len_l_des gc_left,
  gc_pos_2 gc_locto gc_tl_tab text-024 gc_len_l_des gc_left,
  gc_pos_3 gc_prod_tl gc_tl_tab text-022 gc_len_l_des gc_left.
* Display the ALV Grid Display
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = gt_fcat
      i_save             = gc_save
      it_events          = lt_tl_events
    TABLES
      t_outtab           = ct_tlane
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM. " f_display_tlane
*&---------------------------------------------------------------------*
*& Form f_get_events_tl
*&---------------------------------------------------------------------*
* Get event top of page for Tlane report
*----------------------------------------------------------------------*
* <--CT_EVENTS Itab for top of page
*----------------------------------------------------------------------*
FORM f_get_events_tl CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
    WITH KEY name = slis_ev_top_of_page
    BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = gc_lt_top.
  ENDIF.
ENDFORM. " f_get_events_tl
*&---------------------------------------------------------------------*
*& Form F_TOP_TL
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for TLANE report
*----------------------------------------------------------------------*
FORM f_top_tl.                                              "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
        lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_h gc_space text-025,
  gs_type_s text-007 sy-uname,
  gs_type_s text-008 lv_date,
  gs_type_s text-009 lv_time,
  gs_type_s gc_space gc_space.
  IF p_test IS INITIAL.
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-026.
  ELSE.
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-041.
  ENDIF.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " F_TOP_TL
*&---------------------------------------------------------------------*
*& Form f_adj_screen_flds
*&---------------------------------------------------------------------*
* Adjusts the screen fields
*----------------------------------------------------------------------*
FORM f_adj_screen_flds .
*MOD-1771404 by Aveek
* IF p_mdel IS INITIAL.
* LOOP AT SCREEN.
* IF screen-name = 'P_LOG' OR
* screen-name = 'S_PRD-LOW' OR
* screen-name = 'S_PRD-HIGH' OR
* screen-name = 'S_MKT-LOW' OR
* screen-name = 'S_MKT-HIGH' OR
* screen-name = 'P_TEST'.
* screen-input = 0.
* REFRESH: s_prd, s_mkt.
* MODIFY SCREEN.
* ENDIF.
* ENDLOOP.
* ENDIF.
* IF p_tlan IS INITIAL.
* LOOP AT SCREEN.
* IF screen-name = 'P_SRC' OR
* screen-name = 'P_DES' OR
* screen-name = 'P_PRD' OR
* screen-name = 'P_TDAT' OR
* screen-name = 'P_TPRD' OR
* screen-name = 'P_FRD' OR
* screen-name = 'P_TOD'.
* screen-input = 0.
* CLEAR: p_src, p_des, p_prd.
* MODIFY SCREEN.
* ENDIF.
* ENDLOOP.
* ENDIF.
*
* IF p_tdat IS INITIAL.
* LOOP AT SCREEN.
* IF screen-name = 'P_FRD' OR
* screen-name = 'P_TOD'.
* screen-input = 0.
* CLEAR: p_frd, p_tod.
* MODIFY SCREEN.
* ENDIF.
* ENDLOOP.
* ENDIF.
***MOD-1771404 by Aveek
ENDFORM. " f_adj_screen_flds
*&---------------------------------------------------------------------*
*& Form f_update_tlane
*&---------------------------------------------------------------------*
* Updates transportation lane
*----------------------------------------------------------------------*
**MOD-1771404 by Aveek
FORM f_update_tlane .
  CONSTANTS:
  lc_stime TYPE t VALUE '000000',
  lc_etime TYPE t VALUE '235959',
  lc_utc TYPE timezone VALUE 'UTC'.
  DATA:
  lv_dummy TYPE /sapapo/c_loctype,
  lv_valfr TYPE timestamp,
  lv_valto TYPE timestamp,
  lv_matid TYPE /sapapo/matid,
  lv_srcid TYPE /sapapo/locid,
  lv_desid TYPE /sapapo/locid.
  DATA:
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_matid TYPE t_matid,
  ls_matid TYPE s_matid,
  lt_tlaneupd TYPE t_tlaneupd,
  ls_tlaneupd TYPE s_tlaneupd.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.
  PERFORM f_get_locid_for_loc
        USING p_src
        CHANGING lv_srcid
  lv_dummy.
  PERFORM f_get_locid_for_loc
        USING p_des
        CHANGING lv_desid
  lv_dummy.
  PERFORM f_get_matid_for_prod
        USING p_prd
        CHANGING lv_matid.
  ls_matid-matid = lv_matid.
  ls_matid-matnr = p_prd.
  APPEND ls_matid TO lt_matid.
  SELECT b~matid a~prvar
    APPENDING TABLE lt_matid
    FROM zap_sign AS a JOIN
        /sapapo/matkey AS b
      ON a~prvar = b~matnr
    WHERE pcode = p_prd AND
          ( pdctr = p_src OR
            whse1 = p_src OR
            whse2 = p_src ) AND
          ( whse1 = p_des OR
            whse2 = p_des OR
            markt = p_des ).
**MOD-1771404 Changed code
* SELECT b~matid a~prvar
* APPENDING TABLE lt_matid
* FROM zm_zap_sign AS a JOIN
* /sapapo/matkey AS b
* ON a~prvar = b~matnr
* WHERE pcode = p_prd AND
* ( markt = p_src or
* pdctr = p_src OR
* whse1 = p_src OR
* whse2 = p_src ) OR
* ( pdctr = p_des OR
* whse1 = p_des OR
* whse2 = p_des OR
* markt = p_des ).
*MOD-1771404
  IF sy-subrc <> 0.
    MESSAGE i072 WITH p_prd p_src p_des.
    LEAVE LIST-PROCESSING.
  ENDIF.
  SELECT * INTO TABLE lt_trprod
    FROM /sapapo/trprod
    FOR ALL ENTRIES IN lt_matid
    WHERE locfr = lv_srcid AND
          locto = lv_desid AND
          matid = lt_matid-matid.
*MOD-1771404
* SELECT * INTO TABLE lt_trprod
* FROM /sapapo/trprod
* FOR ALL ENTRIES IN lt_matid
* WHERE ( locfr = lv_srcid OR
* locfr = lv_desid ) AND
* matid = lt_matid-matid OR
* ( locto = lv_srcid OR
* locto = lv_desid ) AND
* matid = lt_matid-matid.
*MOD-1771404
  IF sy-subrc <> 0.
    MESSAGE i067 WITH p_src p_des p_prd.
    LEAVE LIST-PROCESSING.
  ENDIF.
* Change of validity dates
  IF p_tdat IS NOT INITIAL.
    IF p_frd IS INITIAL OR p_tod IS INITIAL.
      MESSAGE i070.
      LEAVE LIST-PROCESSING.
    ELSE.
      IF p_frd > p_tod.
        MESSAGE i071.
        LEAVE LIST-PROCESSING.
      ENDIF.
    ENDIF.
    CONVERT: DATE p_frd TIME lc_stime
    INTO TIME STAMP lv_valfr TIME ZONE lc_utc,
    DATE p_tod TIME lc_etime
    INTO TIME STAMP lv_valto TIME ZONE lc_utc.
    LOOP AT lt_trprod ASSIGNING <lfs_trprod>.
      <lfs_trprod>-valfr = lv_valfr.
      <lfs_trprod>-valto = lv_valto.
    ENDLOOP.
*MOD-1771404
    MODIFY /sapapo/trprod FROM TABLE lt_trprod.
*MOD-1771404
    IF sy-subrc = 0.
      COMMIT WORK AND WAIT.
      MESSAGE s068 WITH p_src p_des p_prd.
    ENDIF.
  ENDIF.
* Removal of product assignment
  IF p_tprd IS NOT INITIAL.
*MOD-1771404
    DELETE /sapapo/trprod FROM TABLE lt_trprod.
*MOD-1771404
    IF sy-subrc = 0.
      COMMIT WORK AND WAIT.
      MESSAGE s069 WITH p_prd p_src p_des.
    ENDIF.
  ENDIF.
* Display the lanes updated in an ALV
  CLEAR ls_tlaneupd.
  ls_tlaneupd-locfr = p_src.
  ls_tlaneupd-locto = p_des.
  IF p_tdat IS NOT INITIAL.
    ls_tlaneupd-msg = text-034.
  ELSE.
    ls_tlaneupd-msg = text-035.
  ENDIF.
  LOOP AT lt_matid INTO ls_matid.
    ls_tlaneupd-matnr = ls_matid-matnr.
    APPEND ls_tlaneupd TO lt_tlaneupd.
  ENDLOOP.
*MOD-1771404
* LOOP AT lt_trprod INTO ls_trprod.
* CALL FUNCTION '/SAPAPO/DM_MATID_GET_MATERIAL'
* EXPORTING
* iv_matid = ls_trprod-matid
* iv_langu = sy-langu
* IMPORTING
* ev_matnr = lv_matnr.
*
* ls_tlaneupd-matnr = lv_matnr.
* ls_tlaneupd-locfr
* ls_tlaneupd-locto
* ls_tlaneupd-matnr
* ls_tlaneupd-matid
*
*
* APPEND ls_tlaneupd TO lt_tlaneupd.
* ENDLOOP.
*
* ls_tlaneupd-matnr = ls_matid-matnr.
* APPEND ls_tlaneupd TO lt_tlaneupd.
* ENDLOOP.
*MOD-1771404
  PERFORM display_alv_tlaneupd USING lt_tlaneupd.
ENDFORM. " f_update_tlane
***MOD-1771404 by Aveek
*&---------------------------------------------------------------------*
*& Form display_alv_tlaneupd
*&---------------------------------------------------------------------*
* Displays the ALV for updated Tlane
*----------------------------------------------------------------------*
* -->IT_TLANEUPD Tlane table
*----------------------------------------------------------------------*
FORM display_alv_tlaneupd USING it_tlaneupd TYPE t_tlaneupd.
* Local Data Declaration
  DATA: lt_tl_events TYPE STANDARD TABLE OF slis_alv_event.
* Populate the top of page event.
  PERFORM f_get_events_tlupd CHANGING lt_tl_events.
  REFRESH gt_fcat.
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_1 gc_locfr gc_tl_tabu text-023 gc_len_l_des gc_left,
  gc_pos_2 gc_locto gc_tl_tabu text-024 gc_len_l_des gc_left,
  gc_pos_3 gc_matnr gc_tl_tabu text-022 gc_len_l_des gc_left,
  gc_pos_4 gc_msg gc_tl_tabu text-036 gc_len_l_des gc_left.
* Display the ALV Grid Display
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = gt_fcat
      i_save             = gc_save
      it_events          = lt_tl_events
    TABLES
      t_outtab           = it_tlaneupd
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM. " display_alv_tlaneupd
*&---------------------------------------------------------------------*
*& Form f_get_events_tlupd
*&---------------------------------------------------------------------*
* Gets the events tab
*----------------------------------------------------------------------*
* <--CT_EVENTS Event table
*----------------------------------------------------------------------*
FORM f_get_events_tlupd CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
    WITH KEY name = slis_ev_top_of_page
    BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = gc_tlupd_top.
  ENDIF.
ENDFORM. " f_get_events_tlupd
*&---------------------------------------------------------------------*
*& Form F_TOP_TLUPD
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for TLANE report
*----------------------------------------------------------------------*
FORM f_top_tlupd.                                           "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
  lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
    SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
    SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_h gc_space text-037,
  gs_type_s text-007 sy-uname,
  gs_type_s text-008 lv_date,
  gs_type_s text-009 lv_time,
  gs_type_s gc_space gc_space,
  gs_type_a text-010 text-038.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " F_TOP_TLUPD
*&---------------------------------------------------------------------*
*& Form f_get_events_prd
*&---------------------------------------------------------------------*
* Gets the events for product
*----------------------------------------------------------------------*
* <--CT_EVENTS Events table
*----------------------------------------------------------------------*
FORM f_get_events_prd CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
    WITH KEY name = slis_ev_top_of_page
    BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = gc_prd_top.
  ENDIF.
ENDFORM. " f_get_events_prd
*&---------------------------------------------------------------------*
*& Form F_TOP_PRD
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for TLANE report
*----------------------------------------------------------------------*
FORM f_top_prd.                                             "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
        lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_s gc_space gc_space.
  IF p_test IS NOT INITIAL.
    PERFORM f_populate_top_page USING
    gs_type_a text-010 text-039.
  ELSE.
    PERFORM f_populate_top_page USING
    gs_type_a text-010 text-040.
  ENDIF.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " F_TOP_TLUPD
*&---------------------------------------------------------------------*
*& Form launch_WUF
*&---------------------------------------------------------------------*
* Launches the WUF
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Core product
* -->IT_PROD_VAR Product variant
*----------------------------------------------------------------------*
FORM launch_wuf USING it_core_prod TYPE t_f4_data_loc
                      it_prod_var TYPE t_f4_data_loc.
  CONSTANTS:
  lc_rscsel TYPE fieldname VALUE 'RSCSEL-SLOW_I'.
  DATA: lv_ctu_params TYPE ctu_params,
        lv_fname TYPE dynfnam,
        lv_count(2) TYPE n,
        lt_msg_tab TYPE STANDARD TABLE OF bdcmsgcoll.
* Field-symbols
  FIELD-SYMBOLS:  <lfs_msg_ret> TYPE bdcmsgcoll,
                  <lfs_data> TYPE s_f4_data_loc.
  PERFORM f_populate_bdc_tab USING:
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=OBSEL',
  space 'WUF_OBJ_NAME_STRUC-NAME' 'Location Product',
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=MORE__1',
  space 'WUF_OPT_FIELDS-DISPLAY_DEPS' 'X',
  space 'BDC_CURSOR' 'WUF_SEL_FIELDS-LOW__1',
  space 'WUF_SEL_FIELDS-FIELDNAMETXT__1' 'Product',
  'X' 'SAPLALDB' '3000',
  space 'BDC_OKCODE' '=ACPT'.
  CLEAR lv_count.
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    lv_count = lv_count + 1.
    CONCATENATE lc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
    PERFORM f_populate_bdc_tab USING:
    ' ' lv_fname <lfs_data>-pcode.
    CLEAR lv_fname.
  ENDLOOP.
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    lv_count = lv_count + 1.
    CONCATENATE lc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
    PERFORM f_populate_bdc_tab USING:
    ' ' lv_fname <lfs_data>-prvar.
    CLEAR lv_fname.
  ENDLOOP.
  PERFORM f_populate_bdc_tab USING:
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=EXEC_DIR',
  'X' 'SAPMSSY0' '0120',
  space 'BDC_OKCODE' '=&F15',
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=EXIT'.
  lv_ctu_params-nobinpt = space.
  lv_ctu_params-dismode = 'E'.
  lv_ctu_params-updmode = 'S'.
  lv_ctu_params-racommit = 'X'.
  CALL TRANSACTION 'WUF'
    USING gt_bdc_tab
    OPTIONS FROM lv_ctu_params
    MESSAGES INTO lt_msg_tab.
ENDFORM. " launch_WUF
*&---------------------------------------------------------------------*
*& Form f_retrieve_masterdata
*&---------------------------------------------------------------------*
* Gets the master data from the SIGN portfolio meeting the
* user selection
*----------------------------------------------------------------------*
* <--ET_MASTERDATA Master data table
*----------------------------------------------------------------------*
FORM f_retrieve_masterdata CHANGING et_masterdata TYPE t_masterdata
                                    et_tship TYPE t_tship
                                    et_whse TYPE t_whse.
  DATA:
  lt_masterdata TYPE t_masterdata,
  lt_tship TYPE t_tship,
  ls_tship TYPE s_tship,
  ls_whse TYPE s_whse.
  SELECT * INTO TABLE et_masterdata
    FROM zap_scmpf
    WHERE ( pcode = p_prd ) AND
          ( markt = p_src OR
            pdctr = p_src ) OR
          ( pdctr = p_des OR
            markt = p_des ) AND
          delfl = space.
  IF sy-subrc <> 0.
* No sourcing info is maintained for the user selection
    MESSAGE i018.
    LEAVE LIST-PROCESSING.
  ENDIF.
* Obtaining all the possible transhipments in the sourcing
  lt_masterdata = et_masterdata.
  DELETE lt_masterdata WHERE whind IS INITIAL.
  IF lt_masterdata IS INITIAL.
    EXIT.
  ENDIF.
** MOD-1771404
  SELECT * INTO TABLE et_tship
    FROM zap_tshpt
    FOR ALL ENTRIES IN lt_masterdata
    WHERE markt = lt_masterdata-markt AND
          ( pcode = lt_masterdata-pcode OR
            pcode = lt_masterdata-prvar ). " AND
* pcode = lt_masterdata-pcode AND
* pdctr = lt_masterdata-pdctr.
** MOD-1771404
  IF sy-subrc = 0.
    SORT et_tship BY markt pcode pdctr altscn sloc dloc.
    DELETE ADJACENT DUPLICATES FROM et_tship
      COMPARING markt pcode pdctr altscn sloc dloc.
    lt_tship = et_tship.
    SORT lt_tship BY sloc dloc.
    DELETE ADJACENT DUPLICATES FROM lt_tship
      COMPARING sloc dloc.
    LOOP AT lt_tship INTO ls_tship.
      CLEAR ls_whse.
      ls_whse-whse = ls_tship-sloc.
      APPEND ls_whse TO et_whse.
      IF ls_tship-dloc IS INITIAL.
        CONTINUE.
      ENDIF.
      CLEAR ls_whse.
      ls_whse-whse = ls_tship-dloc.
      APPEND ls_whse TO et_whse.
    ENDLOOP.
    SORT et_whse BY whse.
    DELETE ADJACENT DUPLICATES FROM et_whse
      COMPARING whse.
  ENDIF.
ENDFORM. " f_retrieve_masterdata
*&---------------------------------------------------------------------*
*& Form f_collect_tlanes
*&---------------------------------------------------------------------*
* Collects all possible transportation lanes
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data table
* -->IT_LOCATION Location table
* -->IT_PRODUCT Product table
* <--ET_TLANE Transportation lane table
*----------------------------------------------------------------------*
FORM f_collect_tlanes USING it_masterdata TYPE t_masterdata
                            it_tship TYPE t_tship
                            it_location TYPE t_location
                            it_product TYPE t_product
                      CHANGING et_tlane TYPE t_tlane.
  CONSTANTS:
  lc_stime TYPE t VALUE '000000',
  lc_etime TYPE t VALUE '235959',
  lc_utc TYPE timezone VALUE 'UTC'.
  DATA:
  lv_dummy TYPE /sapapo/c_loctype,
  lv_valfr TYPE timestamp,
  lv_valto TYPE timestamp,
  lv_matid TYPE /sapapo/matid,
  lv_srcid TYPE /sapapo/locid,
  lv_desid TYPE /sapapo/locid.
  DATA:
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_matid TYPE t_matid,
  ls_matid TYPE s_matid,
  lt_tlaneupd TYPE t_tlaneupd,
  ls_tlaneupd TYPE s_tlaneupd.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.
  DATA:
  ls_tlane TYPE s_tlane.
  DATA: lv_exist TYPE i.
  DATA:
  lv_dtfrmin TYPE timestamp,
  lv_dttomax TYPE timestamp,
  lv_tabix TYPE sytabix,
  lv_locfr TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno,
  lv_matnr TYPE /sapapo/matnr.
  DATA:
  ls_masterdata TYPE zap_scmpf.
  FIELD-SYMBOLS:
  <lfs_tlanetmp> TYPE s_tlane,
  <lfs_tlane> TYPE s_tlane.
  LOOP AT it_masterdata INTO ls_masterdata
  WHERE status = 'P' OR
  status = 'A'.
* Check if the core product exists in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
      WITH KEY matnr = ls_masterdata-pcode
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Core product exists, find the tlanes involving it
      PERFORM f_find_tlane_comb
        USING ls_masterdata
              it_tship
              gc_core
              it_location
        CHANGING et_tlane.
    ELSE.
*MOD-1771404
* Product doesnt exist in database, update log
      lv_locfr = ls_masterdata-pdctr.
      lv_locto = ls_masterdata-markt.
      lv_matnr = ls_masterdata-pcode.
      PERFORM f_update_log_table
        USING gc_msgid
              gc_err
              gc_msgno_028
              lv_locfr lv_locto lv_matnr
              space space
        CHANGING gt_log_tlane.
*MOD-1771404
    ENDIF.
* Check if the variant exists in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
      WITH KEY matnr = ls_masterdata-prvar
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Variant exists, find the tlanes involving it
      PERFORM f_find_tlane_comb
        USING ls_masterdata
              it_tship
              gc_var
              it_location
        CHANGING et_tlane.
    ELSE.
*MOD-1771404
* Product doesnt exist in database, update log
      lv_locfr = ls_masterdata-pdctr.
      lv_locto = ls_masterdata-markt.
      lv_matnr = ls_masterdata-prvar.
      PERFORM f_update_log_table
        USING gc_msgid
              gc_err
              gc_msgno_029
              lv_locfr lv_locto lv_matnr
              space space
        CHANGING gt_log_tlane.
    ENDIF.
*MOD-1771404
  ENDLOOP.
*MOD-1771404
  IF et_tlane IS INITIAL.
* The location(s)/product(s) not available in the db for tlane creation
    MESSAGE i023.
    PERFORM f_update_log_table
      USING gc_msgid
            gc_err
            gc_msgno_023
            space space space
            space space
      CHANGING gt_log_tlane.
    EXIT.
  ENDIF.
*MOD-1771404
  SORT et_tlane BY locfr locto matnr datfr datto.
  DELETE ADJACENT DUPLICATES FROM et_tlane
    COMPARING locfr locto matnr datfr datto.
  READ TABLE et_tlane ASSIGNING <lfs_tlane> INDEX 1.
  lv_dtfrmin = <lfs_tlane>-datfr.
  lv_dttomax = <lfs_tlane>-datto.
  LOOP AT et_tlane ASSIGNING <lfs_tlane>.
    IF <lfs_tlane>-datfr < lv_dtfrmin.
      lv_dtfrmin = <lfs_tlane>-datfr.
    ENDIF.
    IF <lfs_tlane>-datto > lv_dttomax.
      lv_dttomax = <lfs_tlane>-datto.
    ENDIF.
    AT END OF matnr.
      <lfs_tlane>-datfr = lv_dtfrmin.
      <lfs_tlane>-datto = lv_dttomax.
      lv_tabix = sy-tabix + 1.
      READ TABLE et_tlane ASSIGNING <lfs_tlanetmp> INDEX lv_tabix.
      IF sy-subrc = 0.
        lv_dtfrmin = <lfs_tlanetmp>-datfr.
        lv_dttomax = <lfs_tlanetmp>-datto.
      ENDIF.
    ENDAT.
  ENDLOOP.
  SORT et_tlane BY locfr locto matnr datfr ASCENDING datto DESCENDING.
  DELETE ADJACENT DUPLICATES FROM et_tlane
  COMPARING locfr locto matnr.
** Get the internal guids for material and locations
  LOOP AT et_tlane ASSIGNING <lfs_tlane>.
    PERFORM f_get_locid_from_locno USING <lfs_tlane>-locfr
      CHANGING <lfs_tlane>-lidfr.
    PERFORM f_get_locid_from_locno USING <lfs_tlane>-locto
      CHANGING <lfs_tlane>-lidto.
    PERFORM f_get_matid_from_matnr USING <lfs_tlane>-matnr
      CHANGING <lfs_tlane>-matid.
  ENDLOOP.
  PERFORM f_get_locid_for_loc USING p_src
    CHANGING lv_srcid
  lv_dummy.
  PERFORM f_get_locid_for_loc USING p_des
    CHANGING lv_desid
  lv_dummy.
  PERFORM f_get_matid_for_prod USING p_prd
  CHANGING lv_matid.
  ls_matid-matid = lv_matid.
  ls_matid-matnr = p_prd.
  APPEND ls_matid TO lt_matid.
  LOOP AT et_tlane INTO ls_tlane.
    READ TABLE lt_matid TRANSPORTING NO FIELDS
      WITH KEY matnr = ls_tlane-matnr
      BINARY SEARCH.
    IF sy-subrc <> 0.
      lv_exist = 1.
      MODIFY et_tlane FROM ls_tlane TRANSPORTING lv_exist.
    ENDIF.
  ENDLOOP.
  DELETE et_tlane WHERE lv_exist = 1.
ENDFORM. " f_collect_tlanes
*&---------------------------------------------------------------------*
*& Form f_find_tlane_comb
*&---------------------------------------------------------------------*
* Finds tlanes involving a product
*----------------------------------------------------------------------*
* -->IS_MASTERDATA Masterdata table
* -->IV_PRDTP Prioduct type
* -->IT_LOCATION Location table
* <--ET_TLANE Tlane table
*----------------------------------------------------------------------*
FORM f_find_tlane_comb USING  is_masterdata TYPE zap_scmpf
                              it_tship TYPE t_tship
                              iv_prdtp TYPE c
                              it_location TYPE t_location
                       CHANGING et_tlane TYPE t_tlane.
  DATA:
  lv_matnr TYPE /sapapo/matnr,
  ls_tship TYPE s_tship,
  ls_tship_tmp TYPE s_tship,
  lv_locfr TYPE /sapapo/locno,
  lv_locwh TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno.
  CASE iv_prdtp.
    WHEN gc_core.
      lv_matnr = is_masterdata-pcode.
    WHEN gc_var.
      lv_matnr = is_masterdata-prvar.
  ENDCASE.
* First see if the Source and Market exist in the database.
* Check the presence of transhipments in the sourcing. We assume that
* the destination can only be blank for the last entry of a given
* sourcing alternative
  READ TABLE it_location TRANSPORTING NO FIELDS
    WITH KEY locno = is_masterdata-markt
    BINARY SEARCH.
  IF sy-subrc <> 0.
* Market exists in the data base
    READ TABLE it_location TRANSPORTING NO FIELDS
      WITH KEY locno = is_masterdata-pdctr
      BINARY SEARCH.
    IF sy-subrc <> 0.
* Source exists in the data base
      IF is_masterdata-whind IS INITIAL.
        lv_locfr = is_masterdata-pdctr.
        lv_locto = is_masterdata-markt.
        PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                      gc_ltp_1001 gc_ltp_1002
                                      is_masterdata-datab
                                      is_masterdata-datbi
                              CHANGING et_tlane.
      ELSE.
        LOOP AT it_tship INTO ls_tship
          WHERE markt = is_masterdata-markt AND
                pcode = is_masterdata-pcode AND
*               pcode = is_masterdata-prvar AND
                pdctr = is_masterdata-pdctr.
          ls_tship_tmp = ls_tship.
* For every alternative sourcing
          AT NEW altscn.
* Define lane between the source and the first transhipment
            READ TABLE it_location TRANSPORTING NO FIELDS
              WITH KEY locno = ls_tship-sloc
              BINARY SEARCH.
            IF sy-subrc <> 0.
              lv_locfr = is_masterdata-pdctr.
              lv_locto = ls_tship_tmp-sloc.
              PERFORM f_build_tlane USING lv_locfr
                                          lv_locto
                                          lv_matnr
                                          gc_ltp_1001
                                          gc_ltp_1002
                                          ls_tship_tmp-datab
                                          ls_tship_tmp-datbi
                                    CHANGING et_tlane.
            ELSE.
              lv_locfr = is_masterdata-pdctr.
              lv_locto = is_masterdata-markt.
              lv_locwh = ls_tship-sloc.
              PERFORM f_update_log_table USING  gc_msgid
                                                gc_err
                                                gc_msgno_087
                                                lv_locfr lv_locto
                                                lv_locwh space
                                                space
                                         CHANGING gt_log_tlane.
            ENDIF.
          ENDAT.
* Define lanes between the successive transhipments in the srcing
          IF ls_tship-dloc IS NOT INITIAL.
            READ TABLE it_location TRANSPORTING NO FIELDS
              WITH KEY locno = ls_tship-dloc
              BINARY SEARCH.
            IF sy-subrc <> 0.
              lv_locfr = ls_tship_tmp-sloc.
              lv_locto = ls_tship_tmp-dloc.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                          gc_ltp_1002 gc_ltp_1002
                                          ls_tship_tmp-datab
                                          ls_tship_tmp-datbi
                                    CHANGING et_tlane.
            ELSE.
              lv_locfr = is_masterdata-pdctr.
              lv_locto = is_masterdata-markt.
              lv_locwh = ls_tship-dloc.
              PERFORM f_update_log_table USING gc_msgid
                                                gc_err
                                                gc_msgno_087
                                                lv_locfr lv_locto
                                                lv_locwh space
                                                space
                                          CHANGING gt_log_tlane.
            ENDIF.
          ENDIF.
          AT END OF altscn.
* Define lane between the final transhipment and the market
            IF ls_tship_tmp-dloc IS NOT INITIAL.
              lv_locfr = ls_tship_tmp-dloc.
              lv_locto = is_masterdata-markt.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                          gc_ltp_1002 gc_ltp_1002
                                          ls_tship_tmp-datab
                                          ls_tship_tmp-datbi
                                    CHANGING et_tlane.
            ELSE.
              lv_locfr = ls_tship_tmp-sloc.
              lv_locto = is_masterdata-markt.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
                                          gc_ltp_1002 gc_ltp_1002
                                          ls_tship_tmp-datab
                                          ls_tship_tmp-datbi
                                    CHANGING et_tlane.
            ENDIF.
          ENDAT.
          CLEAR ls_tship_tmp.
        ENDLOOP.
      ENDIF.
    ELSE.
* Source is yet to be created in the database, update log
      lv_locfr = is_masterdata-pdctr.
      lv_locto = is_masterdata-markt.
      PERFORM f_update_log_table USING gc_msgid
                                        gc_err
                                        gc_msgno_031
                                        lv_locfr lv_locto
                                        lv_matnr space
                                        space
                                CHANGING gt_log_tlane.
    ENDIF.
  ELSE.
* Market is yet to be created in the database, update log
    lv_locfr = is_masterdata-pdctr.
    lv_locto = is_masterdata-markt.
    PERFORM f_update_log_table USING gc_msgid
                                      gc_err
                                      gc_msgno_030
                                      lv_locfr lv_locto
                                      lv_matnr space
                                      space
                                CHANGING gt_log_tlane.
  ENDIF.
ENDFORM. " f_find_tlane_comb
*&---------------------------------------------------------------------*
*& Form f_build_tlane
*&---------------------------------------------------------------------*
* Builds a Tlane
*----------------------------------------------------------------------*
* -->IV_LOCFR Location from
* -->IV_LOCTO Location to
* -->IV_MATNR Product
* -->IV_LTPFR Loctype from
* -->IV_LTPTO Loctype to
* -->IS_MASTERDATA Master data
* <--ET_TLANE Tlane table
*----------------------------------------------------------------------*
FORM f_build_tlane USING iv_locfr TYPE /sapapo/locno
                          iv_locto TYPE /sapapo/locno
                          iv_matnr TYPE /sapapo/matnr
                          iv_ltpfr TYPE /sapapo/c_loctype
                          iv_ltpto TYPE /sapapo/c_loctype
                          iv_datfr TYPE dats
                          iv_datto TYPE dats
                   CHANGING et_tlane TYPE t_tlane.
  DATA:
  ls_tlane TYPE s_tlane.
  ls_tlane-locfr = iv_locfr.
  ls_tlane-locto = iv_locto.
  ls_tlane-matnr = iv_matnr.
  ls_tlane-ltpfr = iv_ltpfr.
  ls_tlane-ltpto = iv_ltpto.
  PERFORM f_get_tstamps USING iv_datfr iv_datto
    CHANGING ls_tlane-datfr ls_tlane-datto.
  APPEND ls_tlane TO et_tlane.
ENDFORM. " f_build_tlane
*&---------------------------------------------------------------------*
*& Form f_get_tstamps
*&---------------------------------------------------------------------*
* Gets the timestamps for the Tlane validity dates
*----------------------------------------------------------------------*
* -->IS_MASTERDATA Master data table
* <--EV_DATFR Valid from tstamp
* <--EV_DATTO Valid to tstamp
*----------------------------------------------------------------------*
FORM f_get_tstamps USING  iv_datfr TYPE d
                          iv_datto TYPE d
                  CHANGING  ev_datfr TYPE timestamp
                            ev_datto TYPE timestamp.
  CONVERT DATE iv_datfr TIME gc_startti
    INTO TIME STAMP ev_datfr TIME ZONE gc_utc.
  CONVERT DATE iv_datto TIME gc_endti
    INTO TIME STAMP ev_datto TIME ZONE gc_utc.
ENDFORM. " f_get_tstamps
*&---------------------------------------------------------------------*
*& Form f_update_log_table
*&---------------------------------------------------------------------*
* Update logs
*----------------------------------------------------------------------*
* -->IV_MSGID Message class
* -->IV_MSGTY Message type
* -->IV_MSGNO Message no
* -->IV_MSGV1 Variable1
* -->IV_MSGV2 Variable2
* -->IV_MSGV3 Variable3
* -->IV_MSGV4 Variable4
* -->IV_MSG Message
* <--ET_LOG_TABLE Log table
*----------------------------------------------------------------------*
FORM f_update_log_table USING iv_msgid TYPE symsgid
                              iv_msgty TYPE symsgty
                              iv_msgno TYPE symsgno
                              iv_msgv1 TYPE any
                              iv_msgv2 TYPE any
                              iv_msgv3 TYPE any
                              iv_msgv4 TYPE any
                              iv_msg TYPE bapi_msg
                        CHANGING et_log_table TYPE t_log_table.
  DATA:
  ls_log_table TYPE bal_s_msg.
  DATA: lv_datto TYPE timestamp.
  ls_log_table-msgty = iv_msgty.
  ls_log_table-msgid = iv_msgid.
  ls_log_table-msgno = iv_msgno.
  ls_log_table-msgv1 = iv_msgv1.
  ls_log_table-msgv2 = iv_msgv2.
  ls_log_table-msgv3 = iv_msgv3.
  ls_log_table-msgv4 = iv_msgv4.
  ls_log_table-context-value = iv_msg.
  APPEND ls_log_table TO et_log_table.
ENDFORM. " f_update_log_table
*&---------------------------------------------------------------------*
*& Form f_get_locid_from_locno
*&---------------------------------------------------------------------*
* Gets the guid for locations
*----------------------------------------------------------------------*
* -->IV_LOCNO Location no
* <--EV_LOCID Location id
*----------------------------------------------------------------------*
FORM f_get_locid_from_locno USING iv_locno TYPE /sapapo/locno
                            CHANGING ev_locid TYPE /sapapo/locid.
  CALL FUNCTION '/SAPAPO/DM_LOCNO_GET_LOCID'
    EXPORTING
      iv_locno           = iv_locno
    IMPORTING
      ev_locid           = ev_locid
    EXCEPTIONS
      location_not_found = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_get_locid_from_locno
**&---------------------------------------------------------------------
**
**& Form f_get_locid_from_locno
**&---------------------------------------------------------------------
**
** Gets the guid for locations
**----------------------------------------------------------------------
**
** -->IV_LOCNO Location no
** <--EV_LOCID Location id
**----------------------------------------------------------------------
*MOD-1771404
*FORM f_get_locid_from_locno USING iv_locno TYPE /sapapo/locno
* CHANGING ev_locid TYPE /sapapo/locid.
*
* CALL FUNCTION '/SAPAPO/DM_LOCNO_GET_LOCID'
* EXPORTING
* iv_locno = iv_locno
* IMPORTING
* ev_locid = ev_locid
* EXCEPTIONS
* location_not_found = 1
* OTHERS = 2.
*
* IF sy-subrc <> 0.
** Do nothing
* ENDIF.
*
*
*ENDFORM. " f_get_locid_from_locno
*MOD-1771404
*&---------------------------------------------------------------------*
*& Form f_get_matid_from_matnr
*&---------------------------------------------------------------------*
* Gets the guid for material
*----------------------------------------------------------------------*
* -->IV_MATNR Material no
* <--EV_MATID Material id
*----------------------------------------------------------------------*
FORM f_get_matid_from_matnr USING iv_matnr TYPE /sapapo/matnr
                            CHANGING ev_matid TYPE /sapapo/matid.
  CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
    EXPORTING
      iv_matnr        = iv_matnr
    IMPORTING
      ev_matid        = ev_matid
    EXCEPTIONS
      matid_not_found = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_get_matid_from_matnr
*&---------------------------------------------------------------------*
*& Form f_categorise_tlanes
*&---------------------------------------------------------------------*
* Categorises Transp lanes
*----------------------------------------------------------------------*
* -->IT_TLANE Tlane table
* <--ET_TLANE_NEW Tlane table with new entries
* <--ET_TLANE_DAT Tlane table with date changes
*----------------------------------------------------------------------*
FORM f_categorise_tlanes USING it_tlane TYPE t_tlane
                         CHANGING et_tlane_new TYPE t_tlane
                                  et_tlane_dat TYPE t_tlane.
  DATA:
  lt_tlane_cr TYPE STANDARD TABLE OF /sapapo/trprod,
  ls_tlane_cr TYPE /sapapo/trprod,                          "#EC NEEDED
  ls_tlane_tmp TYPE s_tlane,
  ls_tlane TYPE s_tlane,
  ls_tlane_new TYPE s_tlane.
  DATA: lv_exist TYPE i.
  DATA: lv_exist1 TYPE i.
  DATA:
  lv_dtfrmin TYPE timestamp,
  lv_dttomax TYPE timestamp,
  lv_tabix TYPE sytabix,
  lv_locfr TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno,
  lv_matnr TYPE /sapapo/matnr.
  DATA:
  ls_masterdata TYPE zap_scmpf.
  FIELD-SYMBOLS:
  <lfs_tlanetmp> TYPE s_tlane,
  <lfs_tlane> TYPE s_tlane.
*MOD-1771404
  SORT it_tlane BY locfr locto matnr datfr datto.
  DELETE ADJACENT DUPLICATES FROM it_tlane
  COMPARING locfr locto matnr datfr datto.
  READ TABLE it_tlane ASSIGNING <lfs_tlane> INDEX 1.
  lv_dtfrmin = <lfs_tlane>-datfr.
  lv_dttomax = <lfs_tlane>-datto.
  LOOP AT it_tlane ASSIGNING <lfs_tlane>.
    IF <lfs_tlane>-datfr < lv_dtfrmin.
      lv_dtfrmin = <lfs_tlane>-datfr.
    ENDIF.
    IF <lfs_tlane>-datto > lv_dttomax.
      lv_dttomax = <lfs_tlane>-datto.
    ENDIF.
    AT END OF matnr.
      <lfs_tlane>-datfr = lv_dtfrmin.
      <lfs_tlane>-datto = lv_dttomax.
      lv_tabix = sy-tabix + 1.
      READ TABLE it_tlane ASSIGNING <lfs_tlanetmp> INDEX lv_tabix.
      IF sy-subrc = 0.
        lv_dtfrmin = <lfs_tlanetmp>-datfr.
        lv_dttomax = <lfs_tlanetmp>-datto.
      ENDIF.
    ENDAT.
  ENDLOOP.
  SORT it_tlane BY locfr locto matnr datfr ASCENDING datto DESCENDING.
  DELETE ADJACENT DUPLICATES FROM it_tlane
  COMPARING locfr locto matnr.
*MOD-1771404
* The table /SAPAPO/TRPROD the key field is /SAPAPO/TRPID.
* But over here we are retrieving data from it with respect to
* the source and destination which anyway is unique for a given
* product involved in the lane.
  SELECT locfr locto matid valfr valto
   INTO CORRESPONDING FIELDS OF TABLE lt_tlane_cr
   FROM /sapapo/trprod
   FOR ALL ENTRIES IN it_tlane
   WHERE locfr = it_tlane-lidfr AND
         locto = it_tlane-lidto.
* valfr = it_tlane-datfr and
* valto = it_tlane-datto.
*MOD-1771404
  IF sy-subrc = 0.
    SORT lt_tlane_cr BY locfr locto matid valfr valto.
  ENDIF.
  LOOP AT it_tlane INTO ls_tlane.
    READ TABLE lt_tlane_cr INTO ls_tlane_cr
      WITH KEY locfr = ls_tlane-lidfr
                locto = ls_tlane-lidto
                matid = ls_tlane-matid.
*MOD-1771404
* BINARY SEARCH.
*MOD-1771404
    IF sy-subrc = 0.
      APPEND ls_tlane TO et_tlane_dat.
    ELSE.
* Check whether a product needs to be assigned to an existing
* Tlane
      READ TABLE lt_tlane_cr INTO ls_tlane_cr
      WITH KEY locfr = ls_tlane-lidfr
      locto = ls_tlane-lidto
      BINARY SEARCH.
      IF sy-subrc = 0.
* Mark the record to denote assignment of prod to existing
* Tlane
        ls_tlane-crtyp = 'X'.
      ENDIF.
      IF ls_tlane-locfr = ls_tlane_tmp-locfr AND
      ls_tlane-locto = ls_tlane_tmp-locto.
        ls_tlane-crtyp = 'X'.
      ENDIF.
      APPEND ls_tlane TO et_tlane_new.
    ENDIF.
    ls_tlane_tmp = ls_tlane.
    CLEAR ls_tlane.
  ENDLOOP.
*MOD-1771404
  SORT et_tlane_new BY locfr locto matnr.
  SORT et_tlane_dat BY locfr locto matnr.
  IF NOT et_tlane_new[] IS INITIAL.
    DELETE ADJACENT DUPLICATES FROM et_tlane_new
    COMPARING locfr locto matnr.
  ENDIF.
  IF NOT et_tlane_dat[] IS INITIAL.
    DELETE ADJACENT DUPLICATES FROM et_tlane_dat
    COMPARING locfr locto matnr.
  ENDIF.
*MOD-1771404
ENDFORM. " f_categorise_tlanes
*&---------------------------------------------------------------------*
*& Form F_GET_LOC_DESC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_IT_PROD_DATA text
* <--P_LV_WARE_DESC text
*----------------------------------------------------------------------*
FORM f_get_loc_desc CHANGING lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  FIELD-SYMBOLS:
  <lfs_prod_data> TYPE s_f4_data_loc.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid.
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3. " Warehouse2
* LOOP AT it_prod_data ASSIGNING <lfs_prod_data>.
* Check if the Location is a market
  SELECT markt UP TO 1 ROWS
    INTO lv_mkt
    FROM zap_sign
*   where markt = <lfs_prod_data>-locno.
    WHERE markt = s_mkt-low.
  ENDSELECT.
  IF  sy-subrc = 0 OR
      lv_mkt IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
      INTO lv_locid
      FROM /sapapo/loc
      WHERE locno = lv_mkt.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
        INTO lv_ware_desc
        FROM /sapapo/loct
        WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = s_mkt-low.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
* endif.
*MOD-1887120
        EXIT.
      ENDIF.
    ENDIF.
  ENDIF.
* Check if the Location is a Production center
  SELECT pdctr UP TO 1 ROWS
    INTO lv_pdctr
    FROM zap_sign
*   WHERE pdctr IN s_mkt.
    WHERE pdctr = s_mkt-low.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_pdctr IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
      INTO lv_locid
      FROM /sapapo/loc
      WHERE locno = lv_pdctr.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
        INTO lv_ware_desc
        FROM /sapapo/loct
        WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = s_mkt-low.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
*MOD-1887120
        EXIT.
      ENDIF.
    ENDIF.
  ENDIF.
* Check if the Location is a Warehouse 1
  SELECT whse1 UP TO 1 ROWS
    INTO lv_whse1
    FROM zap_sign
*   WHERE whse1 IN s_mkt.
    WHERE whse1 = s_mkt-low.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_whse1 IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
      INTO lv_locid
      FROM /sapapo/loc
      WHERE locno = lv_whse1.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
        INTO lv_ware_desc
        FROM /sapapo/loct
        WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = lv_whse1.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
*MOD-1887120
        EXIT.
      ENDIF.
    ENDIF.
  ENDIF.
* Check if the Location is a Warehouse 2
  SELECT whse2 UP TO 1 ROWS
    INTO lv_whse2
    FROM zap_sign
*   WHERE whse2 IN s_mkt.
    WHERE whse2 = s_mkt-low.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_whse2 IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
      INTO lv_locid
      FROM /sapapo/loc
      WHERE locno = lv_whse2.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
        INTO lv_ware_desc
        FROM /sapapo/loct
        WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = LV_WHSE2.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
*MOD-1887120
        EXIT.
      ENDIF.
* Location does not exist in APO
      MESSAGE e017.
    ENDIF.
  ENDIF.
* ENDLOOP.
ENDFORM. " F_GET_LOC_DESC
*&---------------------------------------------------------------------*
*& Form GET_DESC_LOC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_<LFS_PROD_DATA> text
* <--P_LV_WARE_DESC text
*----------------------------------------------------------------------*
FORM get_desc_loc USING p_lv_prod_data TYPE s_f4_data_loc
                  CHANGING p_lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  FIELD-SYMBOLS:
  <lfs_prod_data> TYPE s_f4_data_loc.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid.
  DATA: lv_ware_desc TYPE /sapapo/loc_descr40.
** Local Date Declaration
* DATA: lv_mkt TYPE zdmarkt, " Market
* lv_pdctr TYPE zdpdctr, " Production center
* lv_whse1 TYPE zawhse1, " Warehouse1
* lv_whse2 TYPE zawhse2. " Warehouse2
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3. " Warehouse2
* Check if the Location is a market
  SELECT whse1 UP TO 1 ROWS
    INTO lv_whse1
    FROM zap_sign
*   WHERE whse1 eq s_mkt-low.
    WHERE whse1 = p_lv_prod_data-locno.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
      INTO lv_locid
      FROM /sapapo/loc
      WHERE locno = lv_whse1.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
        INTO p_lv_ware_desc
        FROM /sapapo/loct
        WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
        lv_ware_desc = p_lv_ware_desc.
      ENDIF.
      EXIT.
    ENDIF.
  ENDIF.
ENDFORM. " GET_DESC_LOC
*&---------------------------------------------------------------------*
*& Form GET_DESC_WHSE2
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LV_PROD_DATA text
* <--P_LV_WARE_DESC text
*----------------------------------------------------------------------*
FORM get_desc_whse2 USING p_lv_prod_data TYPE s_f4_data_loc
                    CHANGING p_lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  FIELD-SYMBOLS:
  <lfs_prod_data> TYPE s_f4_data_loc.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid.
  DATA: lv_ware_desc TYPE /sapapo/loc_descr40.
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3. " Warehouse2
*MOD-1771404
* Check if the Location is a market
* SELECT whse2 UP TO 1 ROWS
* INTO lv_whse2
* FROM zm_zap_sign
* WHERE whse1 IN s_mkt.
* ENDSELECT.
*MOD-1771404
  SELECT whse2 UP TO 1 ROWS
    INTO lv_whse2
    FROM zap_sign
    WHERE whse1 IN s_mkt OR
          whse2 IN s_mkt AND
          pcode IN s_prd.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
      INTO lv_locid
      FROM /sapapo/loc
      WHERE locno = lv_whse2.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
        INTO p_lv_ware_desc
        FROM /sapapo/loct
        WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
        lv_ware_desc = p_lv_ware_desc.
      ENDIF.
      EXIT.
    ENDIF.
  ENDIF.
ENDFORM. " GET_DESC_WHSE2
*&---------------------------------------------------------------------*
*& Form f_clear_tables
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_clear_tables.
  REFRESH: it_zdm0705, it_zdu1002, gt_prod_data,
            gt_core_prod, gt_loc_data.
  CLEAR: it_zdm0705, it_zdu1002, gt_prod_data,
          gt_core_prod, gt_loc_data.
ENDFORM. " f_clear_tables
*Text elements
*----------------------------------------------------------
* 002 Test Delete
* 028 Validity Date Change
* 029 Product Removal
* 030 From date
* 031 To date
* 032 Transportation Lane
* 042 WUF
*Selection texts
*----------------------------------------------------------
* P_DES Destination
* P_FRD From Date
* P_LOG Logical system
* P_MDEL Model
* P_PRD Production
* P_SRC Source
* P_TDAT To Date
* P_TEST Test
* P_TLAN Transportation Lane
* P_TOD To Date
* P_TPRD Product Removal
* S_MKT Market
* S_PRD Product
*----------------------------------------------------------*
*Messages
*----------------------------------------------------------*
*Message class: ZAXXP_SIGN_FPV
*005 no sourcing info is maintained in the sign portfolio
*016 Product does not exist in APO
*017 Location does not exist in APO
*056 Both Product and Location can not be multiple
*059 Product value as * not allowed at the selection screen
*060 Product value as * not allowed at the selection screen
*085 Core Product is Initial.
*086 Product Variant is Initial>

Description

This program reads a forecast file from PPC and loads forecast quantities into APO Demand Planning LiveCache

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zapofcast MESSAGE-ID zfpv LINE-SIZE 132.
************************************************************************
* PROGRAM....... ZAPOFCAST
* TITLE......... APO Forecast Interface
* AUTHOR........ Aveek Ghose
* DATE WRITTEN.. 15/09/2006
* R/3 RELEASE... 4.6C
* =====================================================================
** COPIED FROM... N/A
* DESCRIPTION... This program reads a forecast file from PPC and loads
* forecast quantities into APO Demand Planning LiveCache
* - Key Figure 'Customer Forecast Units' (Z_CUSFU).
* Include change request PD7K900203 in initial transport.
* =====================================================================
** PROGRAM TYPE.. Inbound Interface
* DEV. CLASS.... ZPLN
* LOGICAL DB.... N/A
* =====================================================================
** SCREENS....... N/A
* GUI TITLE..... N/A
* GUI STATUS.... N/A
* TRANSACTIONS.. N/A
* USER EXITS.... N/A
* =====================================================================*
* CHANGE HISTORY *
*Date By Correction Number & Brief Description
*Release
*---------------------------------------------------------------
* Tables / Structures
*----------------------------------------------------------------------*
*MOD-011
*TABLES: zpln_ppc_loc,
* zpln_ppc_mat.
*MOD-011
* Selection table for BAPI function modules
DATA: t_selection TYPE STANDARD TABLE OF bapi10030pbselection,
      g_selection_wa LIKE LINE OF t_selection,
*       Key Figure table for BAPI function moduleDATA:
      t_key_figure_selection TYPE STANDARD TABLE OF bapi10030keyfigure,
      g_key_figure_selection_wa LIKE LINE OF t_key_figure_selection.
* Group By table for BAPI function module
DATA: t_group_by TYPE STANDARD TABLE OF bapi10030pbgroupby,
      g_group_by_wa LIKE LINE OF t_group_by,
*       Time Series table output from BAPI_PBSRVAPS_GETDETAILDATA:
      t_time_series TYPE STANDARD TABLE OF bapi10030pbtimeserieso,
      g_time_series_wa LIKE LINE OF t_time_series,
*       Time Series table input to BAPI_PBSRVAPS_CHANGEKEYFIGVALDATA:
      t_time_series_chg TYPE STANDARD TABLE OF bapi10030pbtimeseries,
      g_time_series_chg_wa LIKE LINE OF t_time_series_chg,
*       Time Bucket data input to BAPI_PBSRVAPS_CHANGEKEYFIGVALDATA:
      t_time_series_item TYPE STANDARD TABLE OF bapi10030pbtimeseriesitem,
      g_time_series_item_wa LIKE LINE OF t_time_series_item,
*       Characteristic Combination table for BAPI_PBSRVAPS_CHANGEKEYFIGVALDATA
      t_char_comb TYPE STANDARD TABLE OF bapi10030pbcharacteristics,
      g_char_comb_wa LIKE LINE OF t_char_comb.
* Return table from BAPI function modules
DATA: t_return TYPE STANDARD TABLE OF bapiret2,
      g_return_wa LIKE LINE OF t_return.
TYPES :
BEGIN OF ty_forecast_data,
  source_code(2) TYPE c, "Source Code
  lang_group(21) TYPE c, "Language Group
*   pres_code(6) type c, "Presentation Code
  pres_code(7) TYPE c, "Presentation Code
  pres_name(40) TYPE c, "Presentation Name
  site(4) TYPE c, " PPC Location
  indication(3) TYPE c, "Indication
  date TYPE datum, "Date
  gi_date TYPE datum, "Date
  ord_number(8) TYPE c, "Order Number
  ord_line(6) TYPE c, "Order Line Number
  units(20) TYPE c, "Units
END OF ty_forecast_data.
TYPES :
BEGIN OF ty_forecast_data1,
  source_code(2) TYPE c, "Source Code
  lang_group(21) TYPE c, "Language Group
  pres_code(7) TYPE c, "Presentation Code
  r3_material(40) TYPE c, " R/3 Material Code
  pres_name(40) TYPE c, "Presentation Name
  site(4) TYPE c, " PPC Location
  r3_loc(10) TYPE c, " R/3 Location
  indication(3) TYPE c, "Indication
  date TYPE datum, "Date
  gi_date TYPE datum, "Date
  ord_number(8) TYPE c, "Order Number
  ord_line(6) TYPE c, "Order Line Number
  units(20) TYPE c, "Units
  forecast_cv(20) TYPE c, " Forecast Quantity (converted)
  calweek LIKE scal-week, " Calendar Week
  calweek_end LIKE scal-week, " Calendar Week
  calmonth(6) TYPE c, " Calendar Month
  calweekend_date TYPE scal-date, " Calendar Week End date
  calyear(6) TYPE c, " Calendar Year
  calweek_diff(4) TYPE c, "Week Diff
  startweek LIKE scal-week,
  endweek LIKE scal-week,
  start_day LIKE scal-date,
  end_day LIKE scal-date,
  calyear_start(4) TYPE c, " Year Start
  calyear_end(4) TYPE c, " Year End
  delete(1) TYPE c, "Delete Flag
END OF ty_forecast_data1.
TYPES :
BEGIN OF ty_forecast_data2,
  source_code(2) TYPE c, "Source Code
  lang_group(21) TYPE c, "Language Group
  pres_code(7) TYPE c, "Presentation Code
  r3_material(40) TYPE c, " R/3 Material Code
  pres_name(40) TYPE c, "Presentation Name
  site(4) TYPE c, " PPC Location
  r3_loc(10) TYPE c, " R/3 Location
  indication(3) TYPE c, "Indication
  calweek LIKE scal-week, " Calendar Week
  calweek_end TYPE scal-week, " Calendar Week
  endweek LIKE scal-week,
  forecast_cv(20) TYPE c, " Forecast Quantity (converted)
  date TYPE datum, "Date
  gi_date TYPE datum, "Date
  ord_number(8) TYPE c, "Order Number
  ord_line(6) TYPE c, "Order Line Number
  units(20) TYPE c, "Units
  calyear_start(4) TYPE c, " Year Start
  calyear_end(4) TYPE c, " Year End
  calmonth(6) TYPE c, " Calendar Month
  calyear(6) TYPE c, " Calendar Year
  startweek LIKE scal-week,
  start_day LIKE scal-date,
  end_day LIKE scal-date,
  delete(1) TYPE c, "Delete Flag
END OF ty_forecast_data2.
TYPES :
BEGIN OF ty_forecast_data4,
  r3_material(40) TYPE c, " R/3 Material Code
  site(4) TYPE c, " PPC Location
  r3_loc(10) TYPE c, " R/3 Location
  indication(3) TYPE c, "Indication
  calweek LIKE scal-week, " Calendar Week
  source_code(2) TYPE c, "Source Code
  lang_group(21) TYPE c, "Language Group
  pres_code(7) TYPE c, "Presentation Code
  pres_name(40) TYPE c, "Presentation Name
  calweek_end TYPE scal-week, " Calendar Week
  endweek LIKE scal-week,
  forecast_cv(20) TYPE c, " Forecast Quantity (converted)
  date TYPE datum, "Date
  gi_date TYPE datum, "Date
  ord_number(8) TYPE c, "Order Number
  ord_line(6) TYPE c, "Order Line Number
  units(20) TYPE c, "Units
  calyear_start(4) TYPE c, " Year Start
  calyear_end(4) TYPE c, " Year End
  calmonth(6) TYPE c, " Calendar Month
  calyear(6) TYPE c, " Calendar Year
  startweek LIKE scal-week,
  start_day LIKE scal-date,
  end_day LIKE scal-date,
  delete(1) TYPE c, "Delete Flag
END OF ty_forecast_data4.
DATA: t_forecast_data2 TYPE STANDARD TABLE OF ty_forecast_data2
        INITIAL SIZE 0 WITH HEADER LINE.
DATA: t_forecast_data3 TYPE STANDARD TABLE OF ty_forecast_data2
        INITIAL SIZE 0 WITH HEADER LINE.
DATA: t_forecast_data4 TYPE STANDARD TABLE OF ty_forecast_data4
        INITIAL SIZE 0 WITH HEADER LINE.
DATA: t_forecast_data TYPE STANDARD TABLE OF ty_forecast_data
        INITIAL SIZE 0 WITH HEADER LINE,
      t_forecast_data1 TYPE STANDARD TABLE OF ty_forecast_data1
        INITIAL SIZE 0 WITH HEADER LINE,
      g_forecast_data_wa LIKE LINE OF t_forecast_data1,
      g_forecast_data_wa2 LIKE LINE OF t_forecast_data2,
      g_forecast_data_wa3 LIKE LINE OF t_forecast_data3,
      l_forecast_data TYPE STANDARD TABLE OF ty_forecast_data1,
      l_forecast_data_wa LIKE LINE OF t_forecast_data1,
      l_forecast_data2 TYPE STANDARD TABLE OF ty_forecast_data2,
      l_forecast_data_wa2 LIKE LINE OF t_forecast_data2.
* Smaller forecast table to hold data for specific product/plant
* combination]
*MOD-006
*DATA: t_forecast_sub TYPE STANDARD TABLE OF ty_forecast_data1,
* g_forecast_sub_wa LIKE LINE OF t_forecast_sub.
*MOD-006
DATA: t_forecast_sub TYPE STANDARD TABLE OF ty_forecast_data2,
      t_forecast_sub1 TYPE STANDARD TABLE OF ty_forecast_data2,
      g_forecast_sub_wa LIKE LINE OF t_forecast_sub.
* Error table
TYPES :
BEGIN OF ty_error_data.
        INCLUDE TYPE ty_forecast_data1.
TYPES : error_type(1) TYPE c,
END OF ty_error_data.
DATA: t_error_data TYPE STANDARD TABLE OF ty_error_data,
      g_error_data_wa LIKE LINE OF t_error_data.
* Structure to hold input file data
TYPES :
BEGIN OF g_input_data1,
  line(255) TYPE c,
END OF g_input_data1.
DATA: g_input_data TYPE STANDARD TABLE OF g_input_data1
        INITIAL SIZE 0 WITH HEADER LINE.
DATA: ilines TYPE STANDARD TABLE OF string WITH HEADER LINE.
*----------------------------------------------------------------------*
* Global Variables
*----------------------------------------------------------------------*
DATA: g_date_from TYPE /sapapo/cdps_eval_date_from, " From Date
      g_date_to TYPE /sapapo/cdps_eval_date_to. " To Date
DATA: g_file TYPE string, " Filename
      g_material TYPE /sapapo/matnr, " Material
      g_cnt_input_recs TYPE i, " Number of file records
      g_cnt_changes TYPE i, " Number of changes attempted
      g_cnt_error_recs TYPE i, " Number of records with errors
      g_flg_error. " Error
*data: i_file type ref to cl_gui_frontend_services.
* Internal tables *
DATA: BEGIN OF i_file OCCURS 0,
        field(3000) TYPE c,
      END OF i_file.
*DATA: i_file TYPE REF TO CL_WD_FILE_UPLOAD.
*----------------------------------------------------------------------*
* Objects
*----------------------------------------------------------------------*
*----------------------------------------------------------------------*
* Constants
*----------------------------------------------------------------------*
CONSTANTS: c_planningbook TYPE /sapapo/pb_mview " Planning Book
* VALUE 'Z_PPC_FCST_DP',
VALUE '9ASNP94',
c_period_type TYPE /sapapo/cdps_eval_type " Period Type
VALUE 'A'.
CONSTANTS:
  c_version(22) TYPE c VALUE '000', " Version
  c_log_type TYPE c VALUE 'L', " Logical Filetype
  c_logqs TYPE /sapapo/logqs VALUE 'PSGA', " Business Sys Grp
  c_ppc_mat_error(1) TYPE c VALUE '1',
  " PPC Material not found in custom mapping table
  c_ppc_loc_error(1) TYPE c VALUE '2',
  " PPC Location not found in custom mapping table
  c_r3_mat_error(1) TYPE c VALUE '3',
  " R/3 to APO material mapping error
  c_r3_loc_error(1) TYPE c VALUE '4',
  " R/3 to APO location mapping error
  c_pbook_error(1) TYPE c VALUE '5',
  " Error getting planning book information
  c_date_error(1) TYPE c VALUE '6',
  " Invalid date error
  c_change_error(1) TYPE c VALUE '7',
  " Error changing key figure values
  c_per_ind TYPE /sapapo/perkz VALUE 'P', " Period Indicator
  c_fisc_var TYPE /sapapo/periv VALUE 'JJ', " Fiscal year variant
  c_separator TYPE c VALUE ',', " Field separator
  c_no_hdr_material(1) TYPE c VALUE '1',
*   C_LOCTYPE TYPE /SAPAPO/C_LOCTYPE VALUE '1002'.
  c_loctype TYPE /sapapo/c_loctype VALUE '1001'.
" No header material in file record
*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
* Files to process
SELECTION-SCREEN BEGIN OF BLOCK file WITH FRAME TITLE text-001.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF BLOCK infile WITH FRAME TITLE text-002.
PARAMETERS: p_login LIKE filename-fileintern OBLIGATORY.
" DEFAULT 'Z_DDS_43525_GPSG_EMEA_DEMAND'.
PARAMETERS: p_phyin TYPE string.
PARAMETERS: p_horz(4) TYPE c DEFAULT '60'.
PARAMETERS: p_dist(4) TYPE c DEFAULT '0200'.
PARAMETERS: p_logsys TYPE edipparnum DEFAULT 'PD1120DEV'.
SELECTION-SCREEN END OF BLOCK infile.
SELECTION-SCREEN END OF BLOCK file.
*&---------------------------------------------------------------------*
* At Selection Screen
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
  PERFORM sub_get_physical_file USING p_login p_phyin.
*&---------------------------------------------------------------------*
* At Selection Screen Output
*&---------------------------------------------------------------------*
* Make the physical file name display only
AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    IF screen-name = 'P_PHYIN'.
      screen-input = 0.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.
*&---------------------------------------------------------------------*
* INITIALIZATION.
*&---------------------------------------------------------------------*
INITIALIZATION.
  CLEAR : g_return_wa,
          g_selection_wa,
          g_key_figure_selection_wa,
          g_group_by_wa,
          g_time_series_wa,
          g_time_series_chg_wa,
          g_time_series_item_wa,
          g_char_comb_wa,
          g_forecast_data_wa,
          g_forecast_sub_wa,
          g_error_data_wa,
          g_input_data,
          g_date_from,
          g_date_to,
          g_cnt_input_recs,
          g_cnt_changes,
          g_cnt_error_recs,
          g_flg_error.
  REFRESH : t_return,
            t_selection,
            t_key_figure_selection,
            t_group_by,
            t_time_series,
            t_time_series_chg,
            t_time_series_item,
            t_char_comb,
            t_forecast_data,
            t_forecast_sub,
            t_error_data.

  PERFORM sub_get_physical_file USING p_login p_phyin.
*&---------------------------------------------------------------------*
* Start-Of-Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* Read input file into internal table
  PERFORM sub_read_file.
  PERFORM validate_locmap.
  IF NOT t_forecast_data[] IS INITIAL.
* Map data to format for processing
    PERFORM sub_map_data.
    IF NOT t_forecast_data2[] IS INITIAL.
** Process valid data records
      PERFORM sub_process_data.
    ENDIF.
  ENDIF.
* Display program results
  PERFORM sub_display_results.
**&---------------------------------------------------------------------
**
*& Form sub_display_results
*&---------------------------------------------------------------------*
* Write results to spool
*----------------------------------------------------------------------*
FORM sub_display_results.
*** Program Results
  SKIP.
  WRITE: / text-025.
*** File locations
* Input File
  SKIP.
  WRITE: / text-026,
        15 p_phyin.
*** Record Counts
  SKIP.
* Number of records in input file
  WRITE: / text-027,
        48 g_cnt_input_recs.
* Number of records with errors
  WRITE: / text-028,
        48 g_cnt_error_recs.
* Number of records successfully updated in planning book
  WRITE: / text-029,
        48 g_cnt_changes.
*MOD-010
  IF NOT t_error_data[] IS INITIAL.
* Error Details:
    SKIP.
    WRITE: / text-024.
    SKIP.
    WRITE: / text-006,
          13 text-007,
          29 text-008,
          41 text-009,
          61 text-010.
    WRITE: / text-011 UNDER text-006,
              text-012 UNDER text-007,
              text-013 UNDER text-008,
              text-014 UNDER text-009,
              text-015 UNDER text-010.
*MOD-010
    SORT t_error_data
      BY  pres_code
          site
          date
          forecast_cv.
    DELETE ADJACENT DUPLICATES FROM t_error_data
      COMPARING pres_code
                site
                date
                forecast_cv.
*MOD-010
    LOOP AT t_error_data INTO g_error_data_wa.
      WRITE: / g_error_data_wa-pres_code UNDER text-006,
                g_error_data_wa-site UNDER text-007,
                g_error_data_wa-date UNDER text-008,
            48 g_error_data_wa-forecast_cv LEFT-JUSTIFIED.
      CASE g_error_data_wa-error_type.
        WHEN c_ppc_mat_error.
          WRITE text-031 UNDER text-010.
        WHEN c_r3_mat_error.
          WRITE text-019 UNDER text-010.
        WHEN c_r3_loc_error.
          WRITE text-020 UNDER text-010.
        WHEN c_pbook_error.
          WRITE text-021 UNDER text-010.
        WHEN c_date_error.
          WRITE text-022 UNDER text-010.
        WHEN c_change_error.
          WRITE text-023 UNDER text-010.
      ENDCASE.
      CLEAR : g_error_data_wa.
    ENDLOOP.
    IF sy-batch IS INITIAL.
      MESSAGE i000 WITH text-030.
    ELSE.
* MESSAGE e000 WITH text-030. "TPR1336-
      MESSAGE i000 WITH text-030.                           "TPR1336+
    ENDIF.
  ENDIF.
ENDFORM. " sub_display_results
*&---------------------------------------------------------------------*
*& Form sub_map_data
*&---------------------------------------------------------------------*
* Map PPC codes to SAP codes and convert input values
*----------------------------------------------------------------------*
FORM sub_map_data.
  DATA: l_forecast(6) TYPE c,
        l_uom TYPE i.
  DATA: l_sold_num(10) TYPE n, " Sold To Quantity (numerical char.)
        l_sold(7) TYPE p DECIMALS 3, " Sold To Quantity (Packed, 3 dec.)
        l_calweek LIKE scal-week, " Calendar Week
        l_calweek_end LIKE scal-week, " Calendar Week
        l_startcalweek LIKE scal-week, " Calendar Week
        l_calweek1(2) TYPE c, " Calendar Week
        l_calweek_end1(2) TYPE c, " Calendar Week
        l_calweekend_date LIKE scal-week, " Calendar Week
        l_calmonth(6) TYPE c, " Calendar Month
        l_calyear_start(4) TYPE c, " Calendar Year
        l_calyear_end(4) TYPE c, " Calendar Year
        l_calweek_diff TYPE scal-week, " Diff in week
        l_fiscper(7) TYPE c. " Fiscal Period
  DATA: l_temp_wa LIKE g_forecast_data_wa2,
        l_hold_wa LIKE g_forecast_data_wa2.
  DATA: lt_forecast_data2 TYPE STANDARD TABLE OF ty_forecast_data2
          INITIAL SIZE 0 WITH HEADER LINE.
  DATA: lt_forecast_data3 TYPE STANDARD TABLE OF ty_forecast_data2
          INITIAL SIZE 0 WITH HEADER LINE.
  DATA: lt_forecast_data4 TYPE STANDARD TABLE OF ty_forecast_data2
          INITIAL SIZE 0 WITH HEADER LINE.
  DATA: l_wa_forecast_data TYPE ty_forecast_data2.
  DATA: l_wa_forecast_data1 TYPE ty_forecast_data1.
  DATA: l_wa_forecast_data2 TYPE ty_forecast_data2.
*MOD-008
* Local variables
  DATA: l_curr_date TYPE sydatum, " Program Run Date
        l_record_date TYPE sydatum, " Formatted Record Date
        l_curr_year(2) TYPE c, " Program Year (without century)
        l_curr_year_num TYPE i, " Integer value of Program Year
        l_century(2) TYPE c, " Program Century (without year)
        l_century_num TYPE i, " Integer value of Century
        l_input_year_num TYPE i. " Integer value of Record Year
* Record date - format MM/DD/YY
  DATA: BEGIN OF l_input_date,
          month(2) TYPE c,
          filler1(1) TYPE c,
          day(2) TYPE c,
          filler2(1) TYPE c,
          year(2) TYPE c,
        END OF l_input_date.
*MOD-008
  FIELD-SYMBOLS:
    <l_wa_forecast> TYPE ty_forecast_data2.

  DATA: lv_forecast_cv TYPE f.
  CLEAR : lv_forecast_cv.
  DATA: l_first_day LIKE scal-date,
        l_last_day LIKE scal-date.
  DATA: l_first_day1 LIKE scal-date,
        l_last_day1 LIKE scal-date.
  DATA: l_start_week LIKE scal-week,
        l_end_week LIKE scal-week.
  DATA: l_start_week1 LIKE scal-week,
        l_end_week1 LIKE scal-week.
  DATA: lv_sap_code(50) TYPE c.
  DATA: return TYPE bapiret2.
  DATA: l_date_tabix LIKE sy-tabix.
  DATA: l_tabix TYPE sy-tabix.
  DATA: l_start_year(4) TYPE c,
        l_end_year(4) TYPE c,
        l_calyear_diff(4) TYPE c,
        l_calyear_data_start(4) TYPE c.
  LOOP AT t_forecast_data.
    MOVE-CORRESPONDING t_forecast_data TO t_forecast_data1.
    APPEND t_forecast_data1.
    CLEAR : t_forecast_data1.
  ENDLOOP.
***************************************************************
*MOD-004
***************************************************************
* Loop through internal table
  LOOP AT t_forecast_data1 INTO g_forecast_data_wa.
* Map Material code to SAP product and conversion factor using
* cross-reference table; if not found, delete record and store
* error.
    CLEAR : lv_sap_code.
* CALL FUNCTION 'Z_XREF_LOOKUP' DESTINATION P_LOGSYS
* EXPORTING
* TRANSID = 'ZELCUSMATLOOKUP'
* INVALUE = g_forecast_data_wa-pres_code
* IMPORTING
* OUTVALUE = lv_sap_code
* RETURN = RETURN.
*************************************************************
*MOD-004
*************************************************************
* IF SY-SUBRC = 0.
* IF NOT LV_SAP_CODE IS INITIAL. "RETURN IS INITIAL.
** Convert SAP Material Code to APO Material Number; if not found,
* delete record and store error
* SELECT SINGLE matnr FROM /sapapo/matmap
* INTO g_forecast_data_wa-r3_material
* WHERE ext_matnr = lv_sap_code
* AND logqs = c_logqs.
* IF sy-subrc <> 0.
* MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
* g_error_data_wa-error_type = c_r3_mat_error.
* APPEND g_error_data_wa TO t_error_data.
* CLEAR g_error_data_wa.
* DELETE t_forecast_data1.
* CLEAR g_forecast_data_wa.
* ADD 1 TO g_cnt_error_recs.
* CONTINUE.
* ENDIF.
*MOD-011
* l_uom = 1.
*MOD-011
* ELSE.
* Store material in error table; delete current record of data
* table; continue
* MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
* g_error_data_wa-error_type = c_ppc_mat_error.
* APPEND g_error_data_wa TO t_error_data.
* CLEAR g_error_data_wa.
* DELETE t_forecast_data1.
* CLEAR g_forecast_data_wa.
* ADD 1 TO g_cnt_error_recs.
* CONTINUE.
* ENDIF.
*************************************************************
*MOD-004
*************************************************************
** Convert Forecast Quantity using conversion factor
    l_forecast = g_forecast_data_wa-units.
    g_forecast_data_wa-forecast_cv = g_forecast_data_wa-units.
*MOD-009
*MOD-006
    CLEAR : l_calweek,
            l_calweek_end,
            l_calweekend_date,
            l_calweek_diff,
            l_fiscper,
            l_calmonth,
            l_calyear_start,
            l_calyear_end,
            l_start_week,
            l_first_day,
            l_end_week,
            l_last_day.
*MOD-006
*MOD-006
*Convert Monday Date to Calendar Week and Fiscal Period
*MOD-010
    PERFORM sub_format_dates
      USING g_forecast_data_wa-gi_date
            l_calweek
            l_calweek_end
            l_calweekend_date
            l_calweek_diff
            l_fiscper
            l_calmonth
            l_calyear_start
            l_calyear_end
            l_start_week
            l_first_day
            l_end_week
            l_last_day.

    g_forecast_data_wa-calweek = l_calweek.
    g_forecast_data_wa-calweek_end = l_calweek_end.
    g_forecast_data_wa-calmonth = l_calmonth.
    g_forecast_data_wa-calyear_start = l_calyear_start.
    g_forecast_data_wa-calyear_end = l_calyear_end.
    g_forecast_data_wa-startweek = l_start_week.
    g_forecast_data_wa-endweek = l_end_week.
    g_forecast_data_wa-start_day = l_first_day.
    g_forecast_data_wa-end_day = l_last_day.
*MOD-010
* Modify table record
    MODIFY t_forecast_data1 FROM g_forecast_data_wa.
    CLEAR g_forecast_data_wa.
  ENDLOOP.
***************************************************************
*MOD-004
***************************************************************
****************************************************************
**MOD-004
****************************************************************
*MOD-010
  LOOP AT t_forecast_data1 INTO l_wa_forecast_data1.
    l_wa_forecast_data2-source_code = l_wa_forecast_data1-source_code.
    l_wa_forecast_data2-lang_group = l_wa_forecast_data1-lang_group.
    l_wa_forecast_data2-pres_code = l_wa_forecast_data1-pres_code.
    l_wa_forecast_data2-r3_material = l_wa_forecast_data1-r3_material.
    l_wa_forecast_data2-pres_name = l_wa_forecast_data1-pres_name.
    l_wa_forecast_data2-site = l_wa_forecast_data1-site.
    l_wa_forecast_data2-r3_loc = l_wa_forecast_data1-r3_loc.
    l_wa_forecast_data2-indication = l_wa_forecast_data1-indication.
    l_wa_forecast_data2-date = l_wa_forecast_data1-date.
    l_wa_forecast_data2-gi_date = l_wa_forecast_data1-gi_date.
    l_wa_forecast_data2-ord_number = l_wa_forecast_data1-ord_number.
    l_wa_forecast_data2-ord_line = l_wa_forecast_data1-ord_line.
    l_wa_forecast_data2-units = l_wa_forecast_data1-units.
    l_wa_forecast_data2-forecast_cv = l_wa_forecast_data1-forecast_cv.
    l_wa_forecast_data2-calweek = l_wa_forecast_data1-calweek.
    l_wa_forecast_data2-calweek_end = l_wa_forecast_data1-calweek_end.
    l_wa_forecast_data2-startweek = l_wa_forecast_data1-startweek.
    l_wa_forecast_data2-endweek = l_wa_forecast_data1-endweek.
    l_wa_forecast_data2-start_day = l_wa_forecast_data1-start_day.
    l_wa_forecast_data2-end_day = l_wa_forecast_data1-end_day.
    l_wa_forecast_data2-calyear_start =
    l_wa_forecast_data1-calyear_start.
    l_wa_forecast_data2-calyear_end = l_wa_forecast_data1-calyear_end.
    l_wa_forecast_data2-delete = l_wa_forecast_data1-delete.
    APPEND l_wa_forecast_data2 TO t_forecast_data2.
    CLEAR : l_wa_forecast_data1.
    CLEAR : t_forecast_data2.
  ENDLOOP.
*MOD-010
  SORT t_forecast_data2
    BY  source_code
        lang_group
        pres_code
        r3_material
        pres_name
        site
        r3_loc
        indication
        calweek
        endweek
        calmonth
        calyear_start
        calyear_end
        calweek_end
        startweek
        start_day
        end_day
        delete.
*****************************************************************
**MOD-010
*****************************************************************
  CLEAR : lv_forecast_cv.
* collecting the product Variant.
*MOD-007
  lt_forecast_data2[] = t_forecast_data2[].
* Aggregation for JOR and NOR.
  DELETE lt_forecast_data2
    WHERE indication <> 'JOR'
      AND indication <> 'NOR'.
  IF NOT lt_forecast_data2[] IS INITIAL.
    LOOP AT lt_forecast_data2 ASSIGNING <l_wa_forecast>.
      lv_forecast_cv = lv_forecast_cv + <l_wa_forecast>-forecast_cv.
      <l_wa_forecast>-delete = 'X'.
      AT END OF calweek.
        <l_wa_forecast>-delete = ' '.
        <l_wa_forecast>-forecast_cv = lv_forecast_cv.
        CLEAR lv_forecast_cv.
      ENDAT.
    ENDLOOP.
  ENDIF.
  DELETE lt_forecast_data2 WHERE delete = 'X'.
* Aggregation where Indication ne JOR and NOR.
  lt_forecast_data3[] = t_forecast_data2[].
  LOOP AT lt_forecast_data3.
    IF lt_forecast_data3-indication = 'JOR'
    OR lt_forecast_data3-indication = 'NOR'.
      DELETE lt_forecast_data3.
    ELSE.
*MOD-009
*if lt_forecast_data3-calweek > lt_forecast_data3-calweek_end.
*MOD-009
***MOD-011
* if lt_forecast_data3-calweek > lt_forecast_data3-endweek.
* delete lt_forecast_data3.
* else.
* append lt_forecast_data3 to lt_forecast_data4.
* endif.
***MOD-011
      APPEND lt_forecast_data3 TO lt_forecast_data4.
**MOD-011
*MOD-010
* endif.
*MOD-010
    ENDIF.
  ENDLOOP.
* Aggregation beyond the horizon for all indication.
  IF NOT lt_forecast_data3[] IS INITIAL.
    LOOP AT lt_forecast_data3 ASSIGNING <l_wa_forecast>.
      lv_forecast_cv = lv_forecast_cv + <l_wa_forecast>-forecast_cv.
      <l_wa_forecast>-delete = 'X'.
      AT END OF calweek.
        <l_wa_forecast>-delete = ' '.
        <l_wa_forecast>-forecast_cv = lv_forecast_cv.
        CLEAR lv_forecast_cv.
      ENDAT.
    ENDLOOP.
  ENDIF.
  DELETE lt_forecast_data3 WHERE delete = 'X'.
  APPEND LINES OF lt_forecast_data3 TO lt_forecast_data2.
*MOD-007
*MOD-007
  REFRESH : t_forecast_data2.
  APPEND LINES OF lt_forecast_data2 TO t_forecast_data2.
  SORT t_forecast_data2
    BY  source_code
        lang_group
        pres_code
        r3_material
        pres_name
        site
        r3_loc
        indication
        calweek
        endweek
        calweek_end
        startweek
        start_day
        end_day
        calyear_start
        calyear_end
        delete.

  lt_forecast_data2[] = t_forecast_data2[].
  READ TABLE lt_forecast_data2 INDEX 1.
  l_start_year = lt_forecast_data2-calyear_start.
  DESCRIBE TABLE lt_forecast_data2 LINES l_tabix.
  READ TABLE lt_forecast_data2 INDEX l_tabix.
  l_end_year = lt_forecast_data2-calyear_end.
  LOOP AT lt_forecast_data2.
    IF sy-tabix = 1.
      l_calyear_data_start = ( lt_forecast_data2-calyear_start + 2 ).
    ENDIF.
    EXIT.
  ENDLOOP.
*MOD-009
* delete lt_forecast_data2 where
* calyear_start > l_calyear_data_start. "#EC PORTABLE
*MOD-009
* delete lt_forecast_data2 where gi_date < sy-datum.
*MOD-009
  REFRESH : t_forecast_data2.
  APPEND LINES OF lt_forecast_data2 TO t_forecast_data2.
*MOD-009
*****************************************************************
**MOD-010
*****************************************************************
ENDFORM. " sub_map_data
**&---------------------------------------------------------------------
**
**& Form sub_process_data
**&---------------------------------------------------------------------
**
** Loop through data records, separate into product/location
** sub-groups, and process
**----------------------------------------------------------------------
**
FORM sub_process_data.
  DATA: l_temp_wa LIKE g_forecast_data_wa2,
        l_hold_wa LIKE g_forecast_data_wa2.
  CLEAR l_hold_wa.
**MOD-010
  SORT t_forecast_data2
    BY  r3_material
        r3_loc
        date
        calweek ASCENDING.
**MOD-010
*** Loop through internal table, collecting sub-table for each
*** material/plant
  LOOP AT t_forecast_data2 INTO g_forecast_data_wa2.
    l_temp_wa = g_forecast_data_wa2.
* Initial Loop Pass -
* Save hold variables during first loop pass
    AT FIRST.
      l_hold_wa = l_temp_wa.
    ENDAT.
* Normal Loop Processing -
* If control break occurs:
* (1) process existing sub-table for
* material plant combination;
* (2) clear sub-table;
* (3) set new hold values;
* (4) append current work area to sub-table.
*
* Otherwise, append current work area to sub-table
    IF ( l_hold_wa-r3_material <> g_forecast_data_wa2-r3_material ) OR
    ( l_hold_wa-r3_loc <> g_forecast_data_wa2-r3_loc ) OR
    ( l_hold_wa-calweek <> g_forecast_data_wa2-calweek ).
* Process sub-table; clear sub-table
* Set new hold values; append sub-table.
      l_hold_wa = l_temp_wa.
      APPEND g_forecast_data_wa2 TO t_forecast_sub.
      CLEAR g_forecast_data_wa2.
*MOD-009
      PERFORM sub_call_bapi.
      REFRESH t_forecast_sub.
*MOD-009
    ELSE.
* Append sub-table
      APPEND g_forecast_data_wa2 TO t_forecast_sub.
      CLEAR g_forecast_data_wa2.
    ENDIF.
* Final Loop Pass -
* Process existing sub-table
    AT LAST.
      PERFORM sub_call_bapi.
    ENDAT.
  ENDLOOP.
ENDFORM. " sub_process_data
**&---------------------------------------------------------------------
**
*& Form sub_read_file
*&---------------------------------------------------------------------*
* Read interface file
*----------------------------------------------------------------------*
FORM sub_read_file.
* Get Physical Filename
  IF p_phyin IS INITIAL.
    PERFORM sub_get_physical_file USING p_login p_phyin.
  ENDIF.
* Open a file object based on the selection screen information.
  g_file = p_login.

** Read file into data record and append to internal table
  PERFORM sub_read_data.

ENDFORM. " sub_read_file
*&---------------------------------------------------------------------*
*& Form sub_get_physical_file
*&---------------------------------------------------------------------*
* Determine Physical filename from Logical Filename
*----------------------------------------------------------------------*
* -->P_LFILE Logical Filename
* -->P_PFILE Physical Filename
*----------------------------------------------------------------------*
FORM sub_get_physical_file USING p_lfile
                                  p_pfile.
  CHECK NOT p_lfile IS INITIAL.
  CALL FUNCTION 'FILE_GET_NAME'
    EXPORTING
      client           = sy-mandt
      logical_filename = p_lfile
      operating_system = sy-opsys
    IMPORTING
      file_name        = p_pfile
    EXCEPTIONS
      file_not_found   = 1
      OTHERS           = 2.
  IF sy-subrc NE 0.
    p_pfile = '' .
    MESSAGE e002 WITH p_lfile.
  ENDIF.
ENDFORM. " sub_get_physical_file
*&---------------------------------------------------------------------*
*& Form sub_read_data
*&---------------------------------------------------------------------*
* Read data from input file
*----------------------------------------------------------------------*
FORM sub_read_data.
  DATA: delimeter(2) TYPE c VALUE ';'.
  DATA: l_wa_input_data TYPE g_input_data1.
  OPEN DATASET p_phyin FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  " message v_msg.
  IF sy-subrc = 0.
    REFRESH : g_input_data.
    DO.
      READ DATASET p_phyin INTO g_input_data.
      IF sy-subrc NE 0.
        IF g_cnt_input_recs = 0.
          MESSAGE s000 WITH 'Input file is empty'.
          g_flg_error = 'Y'.
          STOP.
        ENDIF.
        EXIT.
      ELSE.
        APPEND g_input_data.
        ADD 1 TO g_cnt_input_recs.
        CLEAR : g_input_data,
        g_forecast_data_wa.
      ENDIF.
    ENDDO.
  ELSE.
    MESSAGE e000 WITH 'File cannot be opened'.
  ENDIF.
  CLOSE DATASET p_phyin.
  LOOP AT g_input_data.
    t_forecast_data-source_code = g_input_data-line+0(4).
    SEARCH t_forecast_data-source_code FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-source_code+sy-fdpos = space.
    ENDIF.
    t_forecast_data-lang_group = g_input_data-line+6(24).
    CONDENSE t_forecast_data-lang_group.
    SEARCH t_forecast_data-lang_group FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-lang_group+sy-fdpos = space.
    ENDIF.
    t_forecast_data-pres_code = g_input_data-line+30(9).
    CONDENSE t_forecast_data-pres_code.
    SEARCH t_forecast_data-pres_code FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-pres_code+sy-fdpos = space.
    ENDIF.
    t_forecast_data-pres_name = g_input_data-line+39(38).
    CONDENSE t_forecast_data-pres_name.
    SEARCH t_forecast_data-pres_name FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-pres_name+sy-fdpos = space.
    ENDIF.
    t_forecast_data-site = p_dist.
    t_forecast_data-indication = g_input_data-line+77(6).
    CONDENSE t_forecast_data-indication.
    SEARCH t_forecast_data-indication FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-indication+sy-fdpos = space.
    ENDIF.
    t_forecast_data-date = g_input_data-line+83(11).
    CONDENSE t_forecast_data-date.
    SEARCH t_forecast_data-date FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-date+sy-fdpos = space.
    ENDIF.
    t_forecast_data-gi_date = g_input_data-line+94(12).
    CONDENSE t_forecast_data-gi_date.
    SEARCH t_forecast_data-gi_date FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-gi_date+sy-fdpos = space.
    ENDIF.
    t_forecast_data-ord_number = g_input_data-line+106(10).
    CONDENSE t_forecast_data-ord_number.
    SEARCH t_forecast_data-ord_number FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-ord_number+sy-fdpos = space.
    ENDIF.
    t_forecast_data-ord_line = g_input_data-line+116(8).
    CONDENSE t_forecast_data-ord_line.
    SEARCH t_forecast_data-ord_line FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-ord_line+sy-fdpos = space.
    ENDIF.
    t_forecast_data-units = g_input_data-line+124(28).
    CONDENSE t_forecast_data-units.
    SEARCH t_forecast_data-units FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-units+sy-fdpos = space.
    ENDIF.
    APPEND t_forecast_data.
    CLEAR t_forecast_data.
    CLEAR : g_input_data,
    g_forecast_data_wa.
  ENDLOOP.
*MOD-002
ENDFORM. " sub_read_data
*&---------------------------------------------------------------------*
*& Form sub_call_bapi
*&---------------------------------------------------------------------*
* Read values of sub-table and update the Customer Forecast
* key figure in the correct time buckets
*----------------------------------------------------------------------*
FORM sub_call_bapi.
  DATA: l_date TYPE sydatum,
        l_start_date TYPE sydatum,
        l_end_date TYPE sydatum,
        l_period_start TYPE bapi10030pbperiodstart,
        l_period_end TYPE bapi10030pbperiodend,
        l_matnr TYPE /sapapo/matnr,
        l_locno TYPE /sapapo/locno,
        l_lines TYPE i.
*MOD-011
  DATA:
    l_calweek LIKE scal-week, " Calendar Week
    l_calweek_end LIKE scal-week, " Calendar Week
    l_startcalweek LIKE scal-week, " Calendar Week
    l_calweek1(2) TYPE c, " Calendar Week
    l_calweek_end1(2) TYPE c, " Calendar Week
    l_calweekend_date LIKE scal-week, " Calendar Week
    l_calmonth(6) TYPE c, " Calendar Month
    l_calyear_start(4) TYPE c, " Calendar Year
    l_calyear_end(4) TYPE c, " Calendar Year
    l_calweek_diff TYPE scal-week, " Diff in week
    p_calweek LIKE scal-week, " Calendar Week
    p_calweek_end LIKE scal-week, " Calendar Week
    p_startcalweek LIKE scal-week, " Calendar Week
    p_calweek1(2) TYPE c, " Calendar Week
    p_calweek_end1(2) TYPE c, " Calendar Week
    p_calweekend_date LIKE scal-week, " Calendar Week
    p_calmonth(6) TYPE c, " Calendar Month
    p_calyear_start(4) TYPE c, " Calendar Year
    p_calyear_end(4) TYPE c, " Calendar Year
    p_calweek_diff TYPE scal-week, " Diff in week
    p_calweek_start TYPE scal-week, " Diff in week
    p_start_week_day TYPE scdatum, " Date
    l_record_date TYPE sy-datum, " Date
    lv_year_start TYPE cyear, " Year
    lv_week_start TYPE scal-week, " Week
    lv_week_end TYPE scal-week. " Week
*MOD-011
  DATA: l_time_series_id TYPE /sapapo/mxrow.
*MOD-011
  DATA: t_periotab TYPE /sapapo/periotab OCCURS 0 WITH HEADER LINE.
  DATA: g_periotab_wa TYPE /sapapo/periotab.
*MOD-011
  REFRESH : t_selection,
            t_group_by,
            t_key_figure_selection,
            t_time_series,
            t_time_series_chg,
            t_time_series_item,
            t_return.

  READ TABLE t_forecast_sub INDEX 1 INTO g_forecast_sub_wa.
******************************************
*MOD-005
******************************************
* l_matnr = g_forecast_sub_wa-r3_material.
* l_locno = g_forecast_sub_wa-r3_loc.
******************************************
*MOD-005
******************************************
  l_matnr = g_forecast_sub_wa-pres_code.
  l_locno = g_forecast_sub_wa-site.
**********************************************************
** Fill parameters/tables for BAPI_PBSRVAPS_GETDETAIL **
**********************************************************
**MOD-006
*** Date Parameters
  g_date_from = g_forecast_sub_wa-date.
  g_date_to = g_forecast_sub_wa-gi_date.
**MOD-006
*MOD-006
* Selection table
  g_selection_wa-characteristic_name = '9AVERSION'. " Version
  g_selection_wa-char_val_sign = 'I'.
  g_selection_wa-char_val_option = 'EQ'.
  g_selection_wa-char_val_low = c_version. " Default '000'
  APPEND g_selection_wa TO t_selection.
  CLEAR g_selection_wa.
* g_selection_wa-characteristic_name = 'ZMATNR'. " APO-DP Product
  g_selection_wa-characteristic_name = '9AMATNR'. " APO-DP Product
  g_selection_wa-char_val_sign = 'I'.
  g_selection_wa-char_val_option = 'EQ'.
  g_selection_wa-char_val_low = l_matnr.
  APPEND g_selection_wa TO t_selection.
  CLEAR g_selection_wa.
  g_selection_wa-characteristic_name = '9ALOCNO'. " APO-DP Product
  g_selection_wa-char_val_sign = 'I'.
  g_selection_wa-char_val_option = 'EQ'.
  g_selection_wa-char_val_low = l_locno.
  APPEND g_selection_wa TO t_selection.
  CLEAR g_selection_wa.
**MOD-006
* g_selection_wa-characteristic_name = 'ZDCENTER'. " Location
* g_selection_wa-char_val_sign = 'I'.
* g_selection_wa-char_val_option = 'EQ'.
* g_selection_wa-char_val_low = P_DIST.
* APPEND g_selection_wa TO t_selection.
* CLEAR g_selection_wa.
**MOD-006
* Group By Table
*** Begin of change for TPR0817
* g_group_by_wa-characteristic_name = 'ZMATNR'.
  g_group_by_wa-characteristic_name = '9AMATNR'.
*** End of change for TPR0817
  APPEND g_group_by_wa TO t_group_by.
  CLEAR g_group_by_wa.
* Key Figure Table - Customer Forecast in Units
* g_key_figure_selection_wa-key_figure = 'Z_CUSFU'.
  g_key_figure_selection_wa-key_figure = '9AATDZU'.
  APPEND g_key_figure_selection_wa TO t_key_figure_selection.
  CLEAR g_key_figure_selection_wa.
  REFRESH t_return.
*MOD-006
* Call FM to get time series number
  CALL FUNCTION 'BAPI_PBSRVAPS_GETDETAIL'
    EXPORTING
      planningbook         = c_planningbook
*     SELECTION_ID         =
      period_type          = c_period_type
      date_from            = g_date_from
      date_to              = g_date_to
    TABLES
      selection            = t_selection
      group_by             = t_group_by
      key_figure_selection = t_key_figure_selection
      time_series          = t_time_series
      return               = t_return.
*MOD-006
  IF NOT t_return[] IS INITIAL.
    LOOP AT t_forecast_sub INTO g_forecast_sub_wa.
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_pbook_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
    ENDLOOP.
    EXIT.
  ENDIF.
****************************************************************
** Fill parameters/tables for BAPI_PBSRVAPS_CHANGEKEYFIGVAL **
****************************************************************
* Time Series table (containing time series ID)
  READ TABLE t_time_series INDEX 1 INTO g_time_series_wa.
  MOVE-CORRESPONDING g_time_series_wa TO g_time_series_chg_wa.
  APPEND g_time_series_chg_wa TO t_time_series_chg.
  l_time_series_id = g_time_series_wa-time_series_id.
  CLEAR g_time_series_chg_wa.
*MOD-009
  t_forecast_sub1[] = t_forecast_sub[].
* Time Series Item table (contains all values to be loaded)
  LOOP AT t_forecast_sub INTO g_forecast_sub_wa.
* Time Series ID
    g_time_series_item_wa-time_series_id = l_time_series_id.
* Check date validity
    CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
      EXPORTING
        date                      = g_forecast_sub_wa-gi_date
      EXCEPTIONS
        plausibility_check_failed = 1
        OTHERS                    = 2.
    IF sy-subrc <> 0.
*MOD-010
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_date_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
      CONTINUE.
    ENDIF.
* Get Date Range for given date
    CALL FUNCTION '/SAPAPO/MC_DATE_GET_FIRST_LAST'
      EXPORTING
        i_date      = g_forecast_sub_wa-gi_date
        i_perkz     = 'W'
*       I_FABKL     = ' '
*       I_PERIV     = ' '
      IMPORTING
        i_firstdate = l_start_date
        i_lastdate  = l_end_date.
**********
*MOD-011
**********
** Starting Timestamp
* CONVERT DATE l_start_date INTO TIME STAMP l_period_start
* TIME ZONE 'UTC '.
* g_time_series_item_wa-period_begin = l_period_start.
*
** Ending Timestamp
* CONVERT DATE l_end_date TIME '235959' INTO TIME STAMP l_period_end
* TIME ZONE 'UTC '.
* g_time_series_item_wa-period_end = l_period_end.
*MOD-011
*********
*MOD-011
*********
* Data for forecast run date.
* l_record_date = sy-datum.
* CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
* EXPORTING
* date = l_record_date
* IMPORTING
* week = l_calweek
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
* g_flg_error = 'X'.
* EXIT.
* ELSE.
* p_calweek(2) = l_calweek+4(2).
* p_calweek+2(4) = l_calweek(4).
* ENDIF.
*
    l_record_date = sy-datum.
    CALL FUNCTION 'DATE_GET_WEEK'
      EXPORTING
        date         = l_record_date
      IMPORTING
        week         = l_calweek
      EXCEPTIONS
        date_invalid = 1
        OTHERS       = 2.
    IF sy-subrc <> 0.
      g_flg_error = 'X'.
      EXIT.
    ELSE.
      p_calweek(2) = l_calweek+4(2).
      p_calweek+2(4) = l_calweek(4).
    ENDIF.
    lv_year_start = p_calweek+2(4).
    lv_week_start = p_calweek+0(2).
    lv_week_end = lv_week_start + 1.
    CONCATENATE lv_year_start lv_week_end+4(2) INTO p_calweek_start.
    CALL FUNCTION 'WEEK_GET_FIRST_DAY'
      EXPORTING
        week         = p_calweek_start
      IMPORTING
        date         = p_start_week_day
      EXCEPTIONS
        week_invalid = 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.
    CALL FUNCTION '/SAPAPO/SDP_TB_GET_PERIOTAB'
      EXPORTING
*       I_TBID              = '104 WEEKS'
        i_tbid              = '9ASNP'
        i_direction         = '+'
        i_startdate         = p_start_week_day
        i_offset_in_days    = '0'
      TABLES
        e_periotab          = t_periotab
      EXCEPTIONS
        tb_not_exists       = 1
        no_tb_defined       = 2
        wrong_tb_definition = 3
        OTHERS              = 4.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    LOOP AT t_periotab INTO g_periotab_wa.
** Starting Timestamp
      CONVERT DATE g_periotab_wa-ertag
      INTO TIME STAMP l_period_start
      TIME ZONE 'UTC '.
      g_time_series_item_wa-period_begin = l_period_start.
* Ending Timestamp
      CONVERT DATE g_periotab_wa-bistag
      TIME '235959' INTO TIME STAMP l_period_end
      TIME ZONE 'UTC '.
      g_time_series_item_wa-period_end = l_period_end.
* Forecast Value
      g_time_series_item_wa-time_series_value = 0.
      g_time_series_item_wa-time_series_id = l_time_series_id.
      APPEND g_time_series_item_wa TO t_time_series_item.
      CLEAR g_time_series_item_wa.
    ENDLOOP.
*MOD-011
  ENDLOOP.
  REFRESH : t_return,
  t_char_comb.
  SORT t_time_series_item BY time_series_id
  period_begin
  period_end.
  DELETE ADJACENT DUPLICATES FROM t_time_series_item
  COMPARING
  time_series_id
  period_begin
  period_end.
*********
**MOD-011
*********
*Delete the first and the last record from the time series table.
  DATA: l_tabix TYPE sy-tabix.
  DESCRIBE TABLE t_time_series_item LINES l_tabix.
  IF l_tabix NE 0.
    DELETE t_time_series_item INDEX l_tabix.
    DELETE t_time_series_item INDEX 1.
  ENDIF.
*********
**MOD-011
*********
*MOD-011
  IF g_material NE g_forecast_sub_wa-r3_material.
*MOD-011
* Refresh the Planning Book
    CALL FUNCTION 'BAPI_PBSRVAPS_CHANGEKEYFIGVAL'
      EXPORTING
        planningbook                = c_planningbook
        commit_control              = 'E'
      TABLES
        selection                   = t_selection
        time_series                 = t_time_series_chg
        time_series_item            = t_time_series_item
        characteristics_combination = t_char_comb
        return                      = t_return.
* If error in return table, store message in error table and attempt
* rollback; otherwise commit
    IF NOT t_return[] IS INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    ENDIF.
*MOD-011
    g_material = g_forecast_sub_wa-r3_material.
  ENDIF.
*MOD-011
*MOD-009
  REFRESH : t_time_series_item.
**************
***MOD-011
**************
* Time Series Item table (contains all values to be loaded)
  LOOP AT t_forecast_sub1 INTO g_forecast_sub_wa.
* Time Series ID
    g_time_series_item_wa-time_series_id = l_time_series_id.
* Check date validity
    CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
      EXPORTING
        date                      = g_forecast_sub_wa-gi_date
      EXCEPTIONS
        plausibility_check_failed = 1
        OTHERS                    = 2.
    IF sy-subrc <> 0.
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_date_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub1.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
      CONTINUE.
    ENDIF.
* Get Date Range for given date
    CALL FUNCTION '/SAPAPO/MC_DATE_GET_FIRST_LAST'
      EXPORTING
        i_date      = g_forecast_sub_wa-gi_date
        i_perkz     = 'W'
*       I_FABKL     = ' '
*       I_PERIV     = ' '
      IMPORTING
        i_firstdate = l_start_date
        i_lastdate  = l_end_date.
** Starting Timestamp
*
* CONVERT DATE l_start_date INTO TIME STAMP l_period_start
*
* TIME ZONE 'UTC'.
*
* g_time_series_item_wa-period_begin = l_period_start.
*
** Ending Timestamp
*
* CONVERT DATE l_end_date TIME '235959' INTO TIME STAMP l_period_end
*
* TIME ZONE 'UTC'.
*
* g_time_series_item_wa-period_end = l_period_end.
*
* Forecast Value
    g_time_series_item_wa-time_series_value = g_forecast_sub_wa-forecast_cv.
    APPEND g_time_series_item_wa TO t_time_series_item.
    CLEAR g_time_series_item_wa.
  ENDLOOP.
* Call FM BAPI_PBSRVAPS_CHANGEKEYFIGVAL to change Z_CUSFU for all
* time buckets
  REFRESH : t_return,
            t_char_comb.
*MOD-006
  SORT t_time_series_item
    BY  time_series_id
        period_begin
        period_end.
  DELETE ADJACENT DUPLICATES FROM t_time_series_item
  COMPARING time_series_id
            period_begin
            period_end.
*MOD-006
  CALL FUNCTION 'BAPI_PBSRVAPS_CHANGEKEYFIGVAL'
    EXPORTING
      planningbook                = c_planningbook
      commit_control              = 'E'
    TABLES
      selection                   = t_selection
      time_series                 = t_time_series_chg
      time_series_item            = t_time_series_item
      characteristics_combination = t_char_comb
      return                      = t_return.
* If error in return table, store message in error table and attempt
* rollback; otherwise commit
  IF NOT t_return[] IS INITIAL.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
* IMPORTING
* RETURN =
    .
    LOOP AT t_forecast_sub1 INTO g_forecast_sub_wa.
*MOD-010
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_change_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub1.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
      CONTINUE.
    ENDLOOP.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' .
    DESCRIBE TABLE t_forecast_sub1 LINES l_lines.
    g_cnt_changes = g_cnt_changes + l_lines.
  ENDIF.
************
*MOD-011
************
ENDFORM. " sub_call_bapi
*&---------------------------------------------------------------------*
*& Form sub_format_dates
*&---------------------------------------------------------------------*
FORM sub_format_dates USING p_date
                            p_calweek
                            p_calweek_end
                            p_weekend_date
                            p_week_diff
                            p_fiscper
                            p_calmonth
                            p_calyear_start
                            p_calyear_end
                            p_start_week
                            p_start_week_day
                            p_end_week
                            p_end_week_day.
* Local variables
  DATA: l_curr_date TYPE sydatum, " Program Run Date
        l_record_date TYPE sydatum, " Formatted Record Date
        l_curr_year(2) TYPE c, " Program Year (without century)
        l_curr_year_num TYPE i, " Integer value of Program Year
        l_century(2) TYPE c, " Program Century (without year)
        l_century_num TYPE i, " Integer value of Century
        l_input_year_num TYPE i. " Integer value of Record Year
  DATA: con_period_week TYPE /sapapo/perkz VALUE 'W'.
* Record date - format MM/DD/YY
  DATA: BEGIN OF l_input_date,
          month(2) TYPE c,
          filler1(1) TYPE c,
          day(2) TYPE c,
          filler2(1) TYPE c,
          year(2) TYPE c,
        END OF l_input_date.
  DATA: l_calweek TYPE kweek,
        l_fiscper TYPE /sapapo/perid.
*MOD-006
  DATA: l_calmonth(6) TYPE c,
        l_calyear(6) TYPE c,
        l_month(2) TYPE c,
        l_year(4) TYPE c,
        l_perid TYPE /sapapo/perid.
  DATA: p_calweek_start LIKE scal-week.
  DATA: f_date TYPE d,
        l_fromdate LIKE scal-date,
        l_todate LIKE scal-date.
  DATA: lv_year_start(4) TYPE c,
        lv_week_start(2) TYPE c,
        lv_week_end(2) TYPE c.
  DATA: lv_calweek_start TYPE scal-week.
  DATA: lv_start_week_day TYPE scal-date.
  DATA: lv_end_week_day TYPE scal-date.
  DATA: lv_length TYPE i.
  CLEAR : l_perid.
**MOD-006
* Data for forecast run date.
  l_record_date = sy-datum.
* Use /SAPAPO/DATE_GET_WEEK, /SAPAPO/PERIOD_GET_FOR_DATE (P/JJ)... but
* must convert to DATS format first
* CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
* EXPORTING
* date = l_record_date
* IMPORTING
* week = l_calweek
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
* g_flg_error = 'X'.
* EXIT.
* ELSE.
* p_calweek(2) = l_calweek+4(2).
* p_calweek+2(4) = l_calweek(4).
* ENDIF.
  CALL FUNCTION 'DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek(2) = l_calweek+4(2).
    p_calweek+2(4) = l_calweek(4).
  ENDIF.
  lv_year_start = p_calweek+2(4).
  lv_week_start = p_calweek+0(2).
  lv_week_end = lv_week_start + 1.
  CONCATENATE lv_year_start lv_week_end INTO p_calweek_start.
  CALL FUNCTION 'WEEK_GET_FIRST_DAY'
    EXPORTING
      week         = p_calweek_start
    IMPORTING
      date         = p_start_week_day
    EXCEPTIONS
      week_invalid = 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.
  p_end_week_day = p_start_week_day + p_horz.
  l_record_date = p_end_week_day.
  CLEAR : l_calweek.
* CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
* EXPORTING
* date = l_record_date
* IMPORTING
* week = l_calweek
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
* g_flg_error = 'X'.
* EXIT.
* ELSE.
* p_calweek_end = l_calweek.
* p_calyear_end = l_calweek+2(4).
* ENDIF.
  CALL FUNCTION 'DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek_end = l_calweek.
    p_calyear_end = l_calweek+2(4).
  ENDIF.
  CLEAR : p_end_week_day.
  CALL FUNCTION 'WEEK_GET_FIRST_DAY'
    EXPORTING
      week         = p_calweek_end
    IMPORTING
      date         = p_end_week_day
    EXCEPTIONS
      week_invalid = 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.
  CLEAR : lv_week_start, lv_week_end.
*MOD-006
  lv_week_start = p_calweek_start+4(2).
  lv_year_start = p_calweek_start+0(4).
  CONCATENATE lv_week_start lv_year_start INTO p_start_week.
  p_calyear_start = lv_year_start.
  lv_week_start = p_calweek_end+4(2).
  lv_year_start = p_calweek_end+0(4).
  CONCATENATE lv_week_start lv_year_start INTO p_end_week.
*MOD-006
  p_week_diff = p_calweek_end+0(2) - p_calweek+0(2).
  CONDENSE p_week_diff.
  p_week_diff = ceil( p_week_diff ).
*MOD-006
*Data for availability date.
  l_record_date = p_date.
* Use /SAPAPO/DATE_GET_WEEK, /SAPAPO/PERIOD_GET_FOR_DATE (P/JJ)... but
* must convert to DATS format first
* CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
* EXPORTING
* date = l_record_date
* IMPORTING
* week = l_calweek
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
* g_flg_error = 'X'.
* EXIT.
* ELSE.
* p_calweek(2) = l_calweek+4(2).
* p_calweek+2(4) = l_calweek(4).
* ENDIF.
  CALL FUNCTION 'DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek(2) = l_calweek+4(2).
    p_calweek+2(4) = l_calweek(4).
  ENDIF.
  lv_year_start = p_calweek+2(4).
  lv_week_start = p_calweek+0(2).
  lv_week_end = lv_week_start + 1.
  lv_length = strlen( lv_week_end ).
  IF lv_length < 2.
    CONCATENATE '0' lv_week_end INTO lv_week_end.
  ENDIF.
  CONSTANTS: c_52(2) TYPE c VALUE '52'.
  CONCATENATE lv_year_start lv_week_end INTO lv_calweek_start.
  IF lv_calweek_start+4(2) > c_52.
    lv_calweek_start+4(2) = c_52.
  ENDIF.
  CALL FUNCTION 'WEEK_GET_FIRST_DAY'
    EXPORTING
      week         = lv_calweek_start
    IMPORTING
      date         = lv_start_week_day
    EXCEPTIONS
      week_invalid = 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.
  lv_end_week_day = lv_start_week_day + p_horz.
  l_record_date = lv_end_week_day.
  CLEAR : l_calweek.
* CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
* EXPORTING
* date = l_record_date
* IMPORTING
* week = l_calweek
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
* g_flg_error = 'X'.
* EXIT.
* ELSE.
* p_calweek_end = l_calweek.
* ENDIF.
  CALL FUNCTION 'DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek_end = l_calweek.
  ENDIF.
  lv_year_start = p_calweek_end+0(4).
  lv_week_start = p_calweek_end+4(2).
  CONCATENATE lv_week_start lv_year_start INTO p_calweek_end.
  p_calyear_end = lv_year_start.
*MOD-008
  CLEAR : l_perid.
  l_record_date = p_date.
  CALL FUNCTION '/SAPAPO/PERIOD_GET_FOR_DATE'
    EXPORTING
      i_date      = l_record_date
      i_perkz     = 'M'
      i_periv     = 'K0'
    IMPORTING
      e_perid     = l_perid
    EXCEPTIONS
      wrong_perkz = 1
      wrong_date  = 2
      wrong_periv = 3
      OTHERS      = 4.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calmonth(2) = l_perid+4(2).
    p_calmonth+2(4) = l_perid(4).
    p_calyear_start = l_perid+0(4).
  ENDIF.
*MOD-008
ENDFORM. " sub_format_dates
*&---------------------------------------------------------------------*
*& Form validate_locmap
*&---------------------------------------------------------------------*
FORM validate_locmap .
*MOD-011
** Convert SAP Location Code to APO Location
  IF NOT t_forecast_data[] IS INITIAL.
    READ TABLE t_forecast_data INTO g_forecast_data_wa INDEX 1.
    SELECT SINGLE locno FROM /sapapo/locmap
    INTO g_forecast_data_wa-r3_loc
    WHERE ext_locno = p_dist
    AND loctype = c_loctype
    AND logqs = c_logqs.
    IF sy-subrc <> 0.
      MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_r3_loc_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      CLEAR g_forecast_data_wa.
      ADD 1 TO g_cnt_error_recs.
    ENDIF.
  ENDIF.
*MOD-011
ENDFORM. " validate_locmap
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zapoddprogram NO STANDARD PAGE HEADING.
TYPE-POOLS: slis.
TYPES :
BEGIN OF ty_excel_field,
  excel_index TYPE sy-index,
  field_name TYPE string,
  field_value TYPE string,
END OF ty_excel_field.
DATA: is_excel_field TYPE ty_excel_field.
DATA: it_excel_field TYPE TABLE OF ty_excel_field.
*Internal tables to hold table informationDATA: IT_TABLES TYPE TABLE OF DD02V.
DATA: is_tables TYPE dd02v.
DATA: it_tabl_inf_tmp TYPE dd02v.
DATA: it_tabl_technics TYPE TABLE OF dd09l.
DATA: is_tabl_technics TYPE dd09l.
DATA: is_tabl_technics_tmp TYPE dd09l.
DATA: it_tabl_fields TYPE dd03p OCCURS 0.
DATA: it_tabl_fields_tmp TYPE dd03p OCCURS 0.
DATA: is_tabl_fields TYPE dd03p.
*Internal tables to hold domain informationDATA: IT_DOMAINS TYPE TABLE OF DD01V.
DATA: is_domains TYPE dd01v.
DATA: is_domains_tmp TYPE dd01v.
DATA: it_doma_values TYPE dd07v OCCURS 0.
DATA: it_doma_values_tmp TYPE dd07v OCCURS 0.
DATA: is_doma_values TYPE dd07v.
*Internal tables to hold data element information
DATA: it_dataelements TYPE TABLE OF dd04v.
DATA: is_dataelements TYPE dd04v.
DATA: is_dataelements_tmp TYPE dd04v.
*Internal table to hold messages
TYPES :
BEGIN OF ty_messages,
  mandt TYPE sy-mandt,
  msgid TYPE sy-msgid,
  msgty TYPE sy-msgty,
  msgno TYPE sy-msgno,
  msgv1 TYPE sy-msgv1,
  msgv2 TYPE sy-msgv2,
  msgv3 TYPE sy-msgv3,
  msgv4 TYPE sy-msgv4,
  text TYPE t100-text,
END OF ty_messages.
DATA: is_messages TYPE ty_messages.
DATA: it_messages TYPE TABLE OF ty_messages.
*Variables to hold message information
DATA: v_msgid TYPE sy-msgid,
      v_msgty TYPE sy-msgty,
      v_msgno TYPE sy-msgno,
      v_msgv1 TYPE sy-msgv1,
      v_msgv2 TYPE sy-msgv2,
      v_msgv3 TYPE sy-msgv3,
      v_msgv4 TYPE sy-msgv4,
      v_text TYPE t100-text.
*Variables
DATA: line(100) TYPE c.
DATA: v_table_name_check TYPE tbatg-tabname.
DATA: v_ext TYPE ddrefstruc-flag.
DATA: v_create(1) TYPE c.
*Field symbols
FIELD-SYMBOLS:<fs_value> TYPE any.
*Constants
CONSTANTS:
c_x(1) VALUE 'X',
c_space(1) VALUE '',
c_yes(1) VALUE 'Y'.
DATA: v_exist(1) TYPE c.
*---------------------------------------------------------------------*
*End of Data declarations for table maintenance generator
*---------------------------------------------------------------------*
DATA: v_current_cell_index TYPE sy-index.
DATA: v_merged_cells TYPE sy-index.
DATA: v_an_integer_string TYPE string.
DATA: v_an_int TYPE i.
*EXCEL XML file name
DATA: v_rlgrap_excel_xml_file TYPE string.
*Main iXML factory
DATA: g_ixml TYPE REF TO if_ixml.
*XML data bytes size
DATA: v_xml_code_bytes TYPE i.
*XML data
DATA: v_xml_data(500) OCCURS 0.
*Stream factory.
DATA: g_streamfactory TYPE REF TO if_ixml_stream_factory.
*XML istream
DATA: g_istream TYPE REF TO if_ixml_istream.
*XML parser
DATA: g_parser TYPE REF TO if_ixml_parser.
*XML DOM object
DATA: g_obj_dom_xml TYPE REF TO if_ixml_document.
*EXCEL root element
DATA: g_obj_excel_workbook TYPE REF TO if_ixml_element.
*Excel XML workbook name
DATA: g_excel_workbook_name TYPE string.
*To hold the all the Worksheet under the Excel's Workbook element
DATA: g_obj_worksheet_collections TYPE REF TO if_ixml_node_collection.
*To hold the iterator for the Worksheet elements collection
DATA: g_obj_worksheet_itr TYPE REF TO if_ixml_node_iterator.
*To hold a Worksheet node.
DATA: v_obj_a_worksheet_node TYPE REF TO if_ixml_node.
*To hold a Worksheet element.
DATA: v_obj_a_worksheet_element TYPE REF TO if_ixml_element.
*To hold the Worksheet name
DATA: v_worksheet_ss_name TYPE string.
*To hold a Worksheet/Table element.
DATA: v_obj_a_table_element TYPE REF TO if_ixml_element.
*To hold the all the Row elements under the Table element
DATA: v_obj_row_collections TYPE REF TO if_ixml_node_collection.
*To hold the iterator for the Row elements collection
DATA: v_obj_row_itr TYPE REF TO if_ixml_node_iterator.
*To hold a Row node.
DATA: v_obj_a_row_node TYPE REF TO if_ixml_node.
*To hold a Row element.
DATA: v_obj_a_row_element TYPE REF TO if_ixml_element.
*To hold the all the Cell elements under the Row element
DATA: v_obj_cell_collections TYPE REF TO if_ixml_node_collection.
*To hold the iterator for the Cell elements collection
DATA: v_obj_cell_itr TYPE REF TO if_ixml_node_iterator.
*To hold a Cell node.
DATA: v_obj_a_cell_node TYPE REF TO if_ixml_node.
*To hold a Cell element.
DATA: v_obj_a_cell_element TYPE REF TO if_ixml_element.
*To hold a Data element.
DATA: v_obj_a_data_element TYPE REF TO if_ixml_element.
* To hold text nodes
DATA: v_obj_text_lst TYPE REF TO if_ixml_node_list.
DATA: v_obj_text_itr TYPE REF TO if_ixml_node_iterator.
* To hold a text value
DATA: v_txt_val TYPE string.
*To hold a text node.
DATA: v_obj_a_txt_node TYPE REF TO if_ixml_node.
*Type casting
DATA: g_cast TYPE REF TO if_ixml_unknown.
*To hold the node type
DATA: v_txt_type TYPE i.
DATA: v_subrc TYPE i.
***********SELECTION-SCREEN****************************
*Input file name
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETER: p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK b1.
*Browse for input file
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM f001_file_value_request USING p_file.
***********AT SELECTION-SCREEN*************************
AT SELECTION-SCREEN.
*Validate files
  PERFORM f_file_check.
**********START-OF-SELECTION*************************
START-OF-SELECTION.
*Read data from file
  PERFORM f004_get_file_data.
*Create domains
  PERFORM f007_create_domains.
*Create data elements
  PERFORM f008_create_dataelements.
*Create tables
  PERFORM f009_create_tables.
**********END-OF-SELECTION*************************
END-OF-SELECTION.
  PERFORM f011_display_messages.
  PERFORM f_free_memory.
*---------------------------------------------------------------------*
*FORM F001_FILE_FOR_VALUE_REQUEST
*---------------------------------------------------------------------*
FORM f001_file_value_request USING file TYPE rlgrap-filename.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      mask      = ''
      static    = 'X'
    CHANGING
      file_name = file.
ENDFORM.                    "F001_FILE_VALUE_REQUEST
*----------------------------------------------------------------------*
*FORM F004_GET_FILE_DATA
*----------------------------------------------------------------------*
*Before proceeding further, the developers must understand completely
*the structure of an XML file and a few terms associated with the XML.
*XML Version :
*Workbook :
*Worksheet :
*Table :
*Column :
*Row :
*Cell :
*Data :
*----------------------------------------------------------------------*
*STEP 1: Read the XML file data into V_XML_CODE
* The output in V_XML_CODE will be the complete XML raw data
* which is not understandable manually.
*----------------------------------------------------------------------*
FORM f004_get_file_data.
*Convert to right case
  v_rlgrap_excel_xml_file = p_file.
*Upload the excel file
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename   = v_rlgrap_excel_xml_file
      filetype   = 'BIN'
    IMPORTING
      filelength = v_xml_code_bytes
    CHANGING
      data_tab   = v_xml_data.
  IF sy-subrc <> 0.
* Cannot open the Presentation Server file.
    WRITE: / 'Cannot open/read the Presentation Server file',
    v_rlgrap_excel_xml_file.
    EXIT.
  ENDIF.
*---------------------------------------------------------------------*
*STEP 2:Initialize the EXCEL DOM object and get
* the root element.
*---------------------------------------------------------------------*
*CL_IXML is the standard class provided by SAP to handle
*any XML applications.
* Load ixml library
  CLASS cl_ixml DEFINITION LOAD.
*Create the main factory for the iXML library. G_IXML is exactly
*similar to the class CL_IXML.
*Note that CREATE is a static method, hence we use '=>'
  g_ixml = cl_ixml=>create( ).
* Create a main stream factory.
  CALL METHOD g_ixml->create_stream_factory
    RECEIVING
      rval = g_streamfactory.
* Create a stream for the itab containg XML raw content.
  CALL METHOD g_streamfactory->create_istream_itable
    EXPORTING
      size  = v_xml_code_bytes
      table = v_xml_data[]
    RECEIVING
      rval  = g_istream.
* Create a new DOM object.
  CALL METHOD g_ixml->create_document
    RECEIVING
      rval = g_obj_dom_xml.
* Create a parser.
  CALL METHOD g_ixml->create_parser
    EXPORTING
      stream_factory = g_streamfactory
      istream        = g_istream
      document       = g_obj_dom_xml
    RECEIVING
      rval           = g_parser.
* Parse the XML source.
  CALL METHOD g_parser->parse
    RECEIVING
      rval = v_subrc.
  IF v_subrc <> 0.
* Parsing failed for XML file, Make sure valid XML is found.
    WRITE: / 'Parsing failed for XML file, Make sure valid ' &
  'XML is found.'.
    EXIT.
  ENDIF.
* close the opened stream and thereby the opened xml file.
  CALL METHOD g_istream->close.
* Get the root element of the Excel XML.
  CALL METHOD g_obj_dom_xml->get_root_element
    RECEIVING
      rval = g_obj_excel_workbook.
  IF g_obj_excel_workbook IS INITIAL.
* The DOM object for the Excel_XML does not seems to be initialized.
    WRITE: / 'The DOM object for the Excel_XML does not seems' &
  'to be initialized.'.
    EXIT.
  ENDIF.
* validate for Excel
* Get the root element name
  CALL METHOD g_obj_excel_workbook->get_name
    RECEIVING
      rval = g_excel_workbook_name.
  CONDENSE g_excel_workbook_name.
* Check if the name is Workbook
  IF g_excel_workbook_name <> 'Workbook'.
    WRITE: / 'The EXCEL XML root element name should be ' &
    'Workbook and not', g_excel_workbook_name.
    EXIT.
  ENDIF.
*---------------------------------------------------------------------*
*STEP 3:Get worksheets
*---------------------------------------------------------------------*
* Get all the Worksheet elements
  CALL METHOD g_obj_excel_workbook->get_elements_by_tag_name
    EXPORTING
      name      = 'Worksheet'
*     NAMESPACE = ''
      depth     = 1
    RECEIVING
      rval      = g_obj_worksheet_collections.
* Make sure the collection is valid
  IF g_obj_worksheet_collections IS INITIAL.
    WRITE: / 'The Excel workbook does not contain any Worksheets.'.
    EXIT.
  ENDIF.
* create an iterator for Worksheet elements
  CALL METHOD g_obj_worksheet_collections->create_iterator
    RECEIVING
      rval = g_obj_worksheet_itr.
* Make sure the iterator is valid.
  IF g_obj_worksheet_itr IS INITIAL.
    WRITE: / 'The Excel workbook does not contain any Worksheets.'.
    EXIT.
  ENDIF.
* step 3.1 get the first worksheet
  CALL METHOD g_obj_worksheet_itr->get_next
    RECEIVING
      rval = v_obj_a_worksheet_node.
* Validate of not null before proceeding.
  IF v_obj_a_worksheet_node IS INITIAL.
    WRITE: / 'The Excel workbook does not contain any Worksheets.'.
    EXIT.
  ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
  CLEAR g_cast.
  CALL METHOD v_obj_a_worksheet_node->query_interface
    EXPORTING
      iid  = 130 "IXML_IID_ELEMENT
    RECEIVING
      rval = g_cast.
  v_obj_a_worksheet_element ?= g_cast.
* Make sure the casting was success
  IF v_obj_a_worksheet_element IS INITIAL.
    WRITE: / 'The Excel workbook does not contain any Worksheets.'.
    EXIT.
  ENDIF.
* Get the value for the attribute ss:Name
  CALL METHOD v_obj_a_worksheet_element->get_attribute
    EXPORTING
      name      = 'Name'
      namespace = 'ss'
    RECEIVING
      rval      = v_worksheet_ss_name.
  CONDENSE v_worksheet_ss_name.
  TRANSLATE v_worksheet_ss_name TO UPPER CASE.
* step 3.3 Loop (1) get each worksheet one by one,
* Note: it's assumed that the name of the worksheet is
* same as the table name.
* lets loop through all the Worksheet elements
  DO."Worksheets
* DATA_COUNT = 0.
    IF sy-index = 1. " Special case for the first
      IF v_worksheet_ss_name = '#QUERY'.
* get the next element of the iterator.
        CALL METHOD g_obj_worksheet_itr->get_next
          RECEIVING
            rval = v_obj_a_worksheet_node.
      ENDIF.
    ELSE.
* get the next element of the iterator.
      CALL METHOD g_obj_worksheet_itr->get_next
        RECEIVING
          rval = v_obj_a_worksheet_node.
    ENDIF.
* Validate of not null before proceeding.
    IF v_obj_a_worksheet_node IS INITIAL.
      EXIT.
    ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
    CLEAR g_cast.
    CALL METHOD v_obj_a_worksheet_node->query_interface
      EXPORTING
        iid  = 130 "IXML_IID_ELEMENT
      RECEIVING
        rval = g_cast.
    v_obj_a_worksheet_element ?= g_cast.
* Make sure the casting was success
    IF v_obj_a_worksheet_element IS INITIAL.
      CONTINUE.
    ENDIF.
* Get the value for the attribute ss:Name
    CALL METHOD v_obj_a_worksheet_element->get_attribute
      EXPORTING
        name      = 'Name'
        namespace = 'ss'
      RECEIVING
        rval      = v_worksheet_ss_name.
    CONDENSE v_worksheet_ss_name.
* The worksheet name is empty, so just dont consider
* that worksheet
    IF v_worksheet_ss_name = ''.
      CONTINUE.
    ENDIF.
*---------------------------------------------------------------------*
*STEP 4:Get the first row of the table element.
*---------------------------------------------------------------------*
* clear all cached records of the itab
* IT_EXCEL_FIELD for every new worksheet
    CLEAR: is_excel_field, it_excel_field[].
* Get the Table element, under the Worksheet.
    CALL METHOD v_obj_a_worksheet_element->find_from_name
      EXPORTING
        name      = 'Table'
        namespace = ''
        depth     = 1
      RECEIVING
        rval      = v_obj_a_table_element.
    IF v_obj_a_table_element IS INITIAL.
      CONTINUE.
    ENDIF.
* Get all the Row elements
    CALL METHOD v_obj_a_table_element->get_elements_by_tag_name
      EXPORTING
        name      = 'Row'
*       NAMESPACE = ''
        depth     = 1
      RECEIVING
        rval      = v_obj_row_collections.
* Make sure the collection is valid
    IF v_obj_row_collections IS INITIAL.
      CONTINUE.
    ENDIF.
* create an iterator for Row elements
    CALL METHOD v_obj_row_collections->create_iterator
      RECEIVING
        rval = v_obj_row_itr.
* Make sure the iterator is valid.
    IF v_obj_row_itr IS INITIAL.
      CONTINUE.
    ENDIF.
* Get the first Row element.
    CALL METHOD v_obj_row_itr->get_next
      RECEIVING
        rval = v_obj_a_row_node.
* Make sure the Row element is valid
    IF v_obj_a_row_node IS INITIAL.
      CONTINUE.
    ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
    CLEAR g_cast.
    CALL METHOD v_obj_a_row_node->query_interface
      EXPORTING
        iid  = 130 "IXML_IID_ELEMENT
      RECEIVING
        rval = g_cast.
    v_obj_a_row_element ?= g_cast.
* Make sure the casting was success
    IF v_obj_a_row_element IS INITIAL.
      CONTINUE.
    ENDIF.
*---------------------------------------------------------------------*
*STEP 5:Get the column names of the current table these are
* the fields defined under the table and cache them
* with together with the index.
* Also take into consideration the fields missing
* and merging of cells.
*---------------------------------------------------------------------*
* Get all the Cell elements
    CALL METHOD v_obj_a_row_element->get_elements_by_tag_name
      EXPORTING
        name  = 'Cell'
        depth = 1
      RECEIVING
        rval  = v_obj_cell_collections.
* Make sure the collection is valid
    IF v_obj_cell_collections IS INITIAL.
      CONTINUE.
    ENDIF.
* create an iterator for Cell elements
    CALL METHOD v_obj_cell_collections->create_iterator
      RECEIVING
        rval = v_obj_cell_itr.
* Make sure the iterator is valid.
    IF v_obj_cell_itr IS INITIAL.
      CONTINUE.
    ENDIF.
* Reset the current Cell index before the first column
* The first column index is always one (1)
* Thus to reset it before the first column is value 0
    v_current_cell_index = 0.
* Reset the merged Cells to 0, which means no
* cells are merged.
    v_merged_cells = 0.
* loop through all the Cell elements
    DO.
* First clear any garbage objects
      CLEAR v_obj_a_cell_node.
      CLEAR v_obj_a_cell_element.
* Lets skip the index for the merged Cells
      v_current_cell_index =
      v_current_cell_index + v_merged_cells.
* Get the next Cell element.
      CALL METHOD v_obj_cell_itr->get_next
        RECEIVING
          rval = v_obj_a_cell_node.
* Make sure the Cell element is valid
      IF v_obj_a_cell_node IS INITIAL.
        EXIT.
      ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
      CLEAR g_cast.
      CALL METHOD v_obj_a_cell_node->query_interface
        EXPORTING
          iid  = 130 "IXML_IID_ELEMENT
        RECEIVING
          rval = g_cast.
      v_obj_a_cell_element ?= g_cast.
* Make sure the casting was success
      IF v_obj_a_cell_element IS INITIAL.
        EXIT.
      ENDIF.
* Calculate the current absolute Horizontal Index
* of the cell.
* First priority is to get any index value mentioned
* in the Cell as ss:Index attribute.
* Second priority is to predict based on the prevous
* value.
* get the attribute ss:Index of the current Cell element
      v_an_int = 0.
      CALL METHOD v_obj_a_cell_element->get_attribute
        EXPORTING
          name      = 'Index'
          namespace = 'ss'
        RECEIVING
          rval      = v_an_integer_string.
      CONDENSE v_an_integer_string.
* A Index attribute was received.
      IF v_an_integer_string <> ''.
        CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
          v_an_int = v_an_integer_string.
        ENDCATCH.
      ENDIF.
* Make sure a valid index is avaliable
      IF v_an_int > 0.
* Its visually set in the Excel Cell itself, so use it.
        v_current_cell_index = v_an_int.
      ELSE.
* no index is mentioned, so assume the next value
        v_current_cell_index = v_current_cell_index + 1.
      ENDIF.
* if required, cache the number of merged cells,
* as per the value mentioned in the attribute
* ss:MergeAcross.
*
* This can definitly occur when two or more cells
* are merged into one.
* Lets first assume there is no merged Cells
      v_merged_cells = 0.
* get the attribute ss:MergeAcross of the current
* Cell element
      v_an_int = 0.
      CALL METHOD v_obj_a_cell_element->get_attribute
        EXPORTING
          name      = 'MergeAcross'
          namespace = 'ss'
        RECEIVING
          rval      = v_an_integer_string.
      CONDENSE v_an_integer_string.
* A MergeAcross attribute was received.
      IF v_an_integer_string <> ''.
        CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
          v_an_int = v_an_integer_string.
        ENDCATCH.
      ENDIF.
* Make sure a valid MergeAcross attribute is avaliable
      IF v_an_int > 0.
        v_merged_cells = v_an_int.
      ELSE.
        v_merged_cells = 0.
      ENDIF.
* get the Data element under the Cell element.
      CALL METHOD v_obj_a_cell_element->find_from_name
        EXPORTING
          name      = 'Data'
*         namespace = ''
          depth     = 1
        RECEIVING
          rval      = v_obj_a_data_element.
      IF v_obj_a_data_element IS INITIAL.
        CONTINUE.
      ENDIF.
* get the text element under the Data element.
      CALL METHOD v_obj_a_data_element->get_children
        RECEIVING
          rval = v_obj_text_lst.
      IF v_obj_text_lst IS INITIAL.
        CONTINUE.
      ENDIF.
* Create an iterator
      CALL METHOD v_obj_text_lst->create_iterator
        RECEIVING
          rval = v_obj_text_itr.
      IF v_obj_text_itr IS INITIAL.
        CONTINUE.
      ENDIF.
      v_txt_val = ''.
      DO.
* get the next element of the iterator.
        CALL METHOD v_obj_text_itr->get_next
          RECEIVING
            rval = v_obj_a_txt_node.
* Validate of not null before proceeding.
        IF v_obj_a_txt_node IS INITIAL.
          EXIT.
        ENDIF.
* Get the current node type
        CALL METHOD v_obj_a_txt_node->get_type
          RECEIVING
            rval = v_txt_type.
* We are only concentrating on text type nodes
        IF v_txt_type <> if_ixml_node=>co_node_text.
          CONTINUE.
        ENDIF.
* We are only expecting one text node.
* If the DOM id is parsed from a document, only
* one text node will be present.
* Also its not standard to add more than one
* text nodes
        CALL METHOD v_obj_a_txt_node->get_value
          RECEIVING
            rval = v_txt_val.
        EXIT.
      ENDDO.
      CONDENSE v_txt_val.
* Make sure the field name is not empty
      IF v_txt_val = ''.
        CONTINUE.
      ENDIF.
* cache these values in the itab IT_EXCEL_FIELD
* with excel index as the master key for the field name
* resolving.CLEAR IS_EXCEL_FIELD.
      is_excel_field-excel_index = v_current_cell_index.
      is_excel_field-field_name = v_txt_val.
      INSERT is_excel_field INTO TABLE it_excel_field.
    ENDDO.
    IF NOT it_excel_field[] IS INITIAL.
* Loop (2) through all the remaining rows of the
* Current table.
      DO.
* Clear any previous header field details.
        LOOP AT it_excel_field INTO is_excel_field.
          CLEAR is_excel_field-field_value.
          MODIFY it_excel_field FROM is_excel_field INDEX sy-tabix.
        ENDLOOP.
* Clear an previous values
        CLEAR v_obj_a_row_node.
        CLEAR v_obj_a_row_element.
* Get the next row node
        CALL METHOD v_obj_row_itr->get_next
          RECEIVING
            rval = v_obj_a_row_node.
* Make sure the Row element is valid
        IF v_obj_a_row_node IS INITIAL.
          EXIT.
        ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
* V_OBJ_A_ROW_ELEMENT ?=
* V_OBJ_A_ROW_NODE->QUERY_INTERFACE( IXML_IID_ELEMENT ).
        CLEAR g_cast.
        CALL METHOD v_obj_a_row_node->query_interface
          EXPORTING
            iid  = 130 "IXML_IID_ELEMENT
          RECEIVING
            rval = g_cast.
        v_obj_a_row_element ?= g_cast.
* Make sure the casting was success
        IF v_obj_a_row_element IS INITIAL.
          CONTINUE.
        ENDIF.
* Loop through each Cell/Data element.
* Clear any garbage objects
        CLEAR v_obj_cell_collections.
        CLEAR v_obj_cell_itr.
* Get all the Cell elements
        CALL METHOD v_obj_a_row_element->get_elements_by_tag_name
          EXPORTING
            name      = 'Cell'
*           NAMESPACE = ''
            depth     = 1
          RECEIVING
            rval      = v_obj_cell_collections.
* Make sure the collection is valid
        IF v_obj_cell_collections IS INITIAL.
          CONTINUE.
        ENDIF.
* create an iterator for Cell elements
        CALL METHOD v_obj_cell_collections->create_iterator
          RECEIVING
            rval = v_obj_cell_itr.
* Make sure the iterator is valid.
        IF v_obj_cell_itr IS INITIAL.
          CONTINUE.
        ENDIF.
* Reset the current Cell index before the first column
* The first column index is always one (1)
* Thus to reset it before the first column is value 0
        v_current_cell_index = 0.
* Reset the merged Cells to 0, which means no
* cells are merged.
        v_merged_cells = 0.
* loop(3) through all the Cell elements
        DO.
* Lets skip the index for the merged Cells
          v_current_cell_index = v_current_cell_index + v_merged_cells.
* First clear any garbage objects
          CLEAR v_obj_a_cell_node.
          CLEAR v_obj_a_cell_element.
* Get the next Cell element.
          CALL METHOD v_obj_cell_itr->get_next
            RECEIVING
              rval = v_obj_a_cell_node.
* Make sure the Cell element is valid
          IF v_obj_a_cell_node IS INITIAL.
            EXIT.
          ENDIF.
* Lets type cast THE NODE OBJECT to ELEMENT OBJECT
* V_OBJ_A_CELL_ELEMENT ?=
* V_OBJ_A_CELL_NODE->QUERY_INTERFACE( IXML_IID_ELEMENT ).
          CLEAR g_cast.
          CALL METHOD v_obj_a_cell_node->query_interface
            EXPORTING
              iid  = 130 "IXML_IID_ELEMENT
            RECEIVING
              rval = g_cast.
          v_obj_a_cell_element ?= g_cast.
* Make sure the casting was success
          IF v_obj_a_cell_element IS INITIAL.
            EXIT.
          ENDIF.
* Calculate the current absolute Horizontal Index
* of the cell.
* First priority is to get any index value mentioned
* in the Cell as ss:Index attribute.
* Second priority is to predict based on the prevous
* value.
* get the attribute ss:Index of the current Cell element
          v_an_int = 0.
          CALL METHOD v_obj_a_cell_element->get_attribute
            EXPORTING
              name      = 'Index'
              namespace = 'ss'
            RECEIVING
              rval      = v_an_integer_string.
          CONDENSE v_an_integer_string.
* A Index attribute was received.
          IF v_an_integer_string <> ''.
            CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
              v_an_int = v_an_integer_string.
            ENDCATCH.
          ENDIF.
* Make sure a valid index is avaliable
          IF v_an_int > 0.
* Its visually set in the Excel Cell itself, so use it.
            v_current_cell_index = v_an_int.
          ELSE.
* no index is mentioned, so assume the next value
            v_current_cell_index = v_current_cell_index + 1.
          ENDIF.
* if required, cache the number of merged cells,
* as per the value mentioned in the attribute
* ss:MergeAcross.
*
* This can definitly occur when two or more cells
* are merged into one.
* Lets first assume there is no merged Cells
          v_merged_cells = 0.
* get the attribute ss:MergeAcross of the current
* Cell element
          v_an_int = 0.
          CALL METHOD v_obj_a_cell_element->get_attribute
            EXPORTING
              name      = 'MergeAcross'
              namespace = 'ss'
            RECEIVING
              rval      = v_an_integer_string.
          CONDENSE v_an_integer_string.
* A MergeAcross attribute was received.
          IF v_an_integer_string <> ''.
            CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
              v_an_int = v_an_integer_string.
            ENDCATCH.
          ENDIF.
* Make sure a valid MergeAcross attribute is avaliable
          IF v_an_int > 0.
            v_merged_cells = v_an_int.
          ELSE.
            v_merged_cells = 0.
          ENDIF.
* Resolve the field id based on its Horizontal Index
* from the itab IT_EXCEL_FIELD.
* get the Data element under the Cell element.
          CALL METHOD v_obj_a_cell_element->find_from_name
            EXPORTING
              name      = 'Data'
*             namespace = ''
              depth     = 1
            RECEIVING
              rval      = v_obj_a_data_element.
          IF v_obj_a_data_element IS INITIAL.
            CONTINUE.
          ENDIF.
* get the text element under the Data element.
          CALL METHOD v_obj_a_data_element->get_children
            RECEIVING
              rval = v_obj_text_lst.
          IF v_obj_text_lst IS INITIAL.
            CONTINUE.
          ENDIF.
* Create an iterator
          CALL METHOD v_obj_text_lst->create_iterator
            RECEIVING
              rval = v_obj_text_itr.
          IF v_obj_text_itr IS INITIAL.
            CONTINUE.
          ENDIF.
          v_txt_val = ''.
          DO.
* get the next element of the iterator.
            CALL METHOD v_obj_text_itr->get_next
              RECEIVING
                rval = v_obj_a_txt_node.
* Validate of not null before proceeding.
            IF v_obj_a_txt_node IS INITIAL.
              EXIT.
            ENDIF.
* Get the current node type
            CALL METHOD v_obj_a_txt_node->get_type
              RECEIVING
                rval = v_txt_type.
* We are only concentrating on text type nodes
            IF v_txt_type <> if_ixml_node=>co_node_text.
              CONTINUE.
            ENDIF.
* We are only expecting one text node.
* If the DOM id is parsed from a document, only
* one text node will be present.
* Also its not standard to add more than one
* text nodes
            CALL METHOD v_obj_a_txt_node->get_value
              RECEIVING
                rval = v_txt_val.
            EXIT.
          ENDDO.
          CONDENSE v_txt_val.
* Make sure the field name is not empty
          IF v_txt_val = ''.
            CONTINUE.
          ENDIF.
* Clear any previous header line
          CLEAR is_excel_field.
* Read the table IT_EXCEL_FIELD for the
* corrosponding entry.
          READ TABLE it_excel_field INTO is_excel_field
            WITH KEY excel_index = v_current_cell_index
            BINARY SEARCH.
* Make sure the read was success
          IF sy-subrc <> 0.
            CONTINUE.
          ENDIF.
          CLEAR is_excel_field-field_value.
          is_excel_field-field_value = v_txt_val.
          MODIFY it_excel_field FROM is_excel_field
            INDEX v_current_cell_index.
* Endo of loop (3)
        ENDDO.
*---------------------------------------------------------------------*
*STEP 6:
*At this stage, we have all the XML data in the internal table
*IT_EXCEL_FIELD. Now, populate the internal tables representing
*individual data dictionary objects.
*---------------------------------------------------------------------*
        CLEAR is_excel_field.
        READ TABLE it_excel_field INTO is_excel_field INDEX 1.
        IF sy-subrc = 0.
*If the data corresponds to DOMAINS
          IF is_excel_field-field_name = 'DOMNAME'.
*Loop over the Excel table and
*populate the internal tables representing DOMAINS
            CLEAR is_excel_field.
            LOOP AT it_excel_field INTO is_excel_field.
              CONCATENATE 'IS_DOMAINS-' is_excel_field-field_name
                INTO line.
              UNASSIGN <fs_value>.
              ASSIGN (line) TO <fs_value>.
              IF <fs_value> IS ASSIGNED.
                <fs_value> = is_excel_field-field_value.
              ENDIF.
            ENDLOOP.
            APPEND is_domains TO it_domains.
            CLEAR is_domains.
            CLEAR is_excel_field.
            LOOP AT it_excel_field INTO is_excel_field.
              CONCATENATE 'IS_DOMA_VALUES-' is_excel_field-field_name
                INTO line.
              UNASSIGN <fs_value>.
              ASSIGN (line) TO <fs_value>.
              IF <fs_value> IS ASSIGNED.
                <fs_value> = is_excel_field-field_value.
              ENDIF.
            ENDLOOP.
            APPEND is_doma_values TO it_doma_values.
            CLEAR is_doma_values.
          ENDIF. "IT_EXCEL_FIELD-FIELD_NAME = 'DOMANAME'
*If the data corresponds to DATAELEMENTS
          IF is_excel_field-field_name = 'ROLLNAME'.
*Loop over the Excel table and
*populate the internal tables representing DATA ELEMENTS
            CLEAR is_excel_field.
            LOOP AT it_excel_field INTO is_excel_field.
              CONCATENATE 'IS_DATAELEMENTS-' is_excel_field-field_name
                INTO line.
              UNASSIGN <fs_value>.
              ASSIGN (line) TO <fs_value>.
              IF <fs_value> IS ASSIGNED.
                <fs_value> = is_excel_field-field_value.
              ENDIF.
            ENDLOOP.
            APPEND is_dataelements TO it_dataelements.
            CLEAR is_dataelements.
          ENDIF. "IT_EXCEL_FIELD-FIELD_NAME = 'DTELNAME'.
*If the data corresponds to TABLES
          IF is_excel_field-field_name = 'TABNAME'.
*Loop over the Excel table and
*populate the internal tables representing TABLES
            CLEAR is_excel_field.
            LOOP AT it_excel_field INTO is_excel_field.
              CONCATENATE 'IS_TABLES-' is_excel_field-field_name
                INTO line.
              UNASSIGN <fs_value>.
              ASSIGN (line) TO <fs_value>.
              IF <fs_value> IS ASSIGNED.
                <fs_value> = is_excel_field-field_value.
              ENDIF.
            ENDLOOP.
            APPEND is_tables TO it_tables.
            CLEAR is_tables.
            CLEAR is_excel_field.
            LOOP AT it_excel_field INTO is_excel_field.
              CONCATENATE 'IS_TABL_TECHNICS-' is_excel_field-field_name
                INTO line.
              UNASSIGN <fs_value>.
              ASSIGN (line) TO <fs_value>.
              IF <fs_value> IS ASSIGNED.
                <fs_value> = is_excel_field-field_value.
              ENDIF.
            ENDLOOP.
            APPEND is_tabl_technics TO it_tabl_technics.
            CLEAR is_tabl_technics.
            CLEAR is_excel_field.
            LOOP AT it_excel_field INTO is_excel_field.
              CONCATENATE 'IS_TABL_FIELDS-' is_excel_field-field_name
                INTO line.
              UNASSIGN <fs_value>.
              ASSIGN (line) TO <fs_value>.
              IF <fs_value> IS ASSIGNED.
                <fs_value> = is_excel_field-field_value.
              ENDIF.
            ENDLOOP.
            APPEND is_tabl_fields TO it_tabl_fields.
            CLEAR is_tabl_fields.
          ENDIF. "IT_EXCEL_FIELD-FIELD_NAME = 'TABLNAME'.
        ENDIF. "READ TABLE IT_EXCEL_FIELD
      ENDDO.
    ENDIF. "IF NOT IT_EXCEL_FIELD[] IS INITIAL.
  ENDDO."Worksheets
*---------------------------------------------------------------------*
*STEP 7: Perform Data readiness
*---------------------------------------------------------------------*
*Delete duplicates
  DELETE ADJACENT DUPLICATES FROM it_tables COMPARING tabname.
  DELETE ADJACENT DUPLICATES FROM it_tabl_technics COMPARING tabname.
  DELETE it_tables WHERE tabname = space.
  DELETE it_tabl_technics WHERE tabname = space.
*The table category is always TRANSPARANT.
  CLEAR:is_tables.
  LOOP AT it_tables INTO is_tables.
    is_tables-tabclass = 'TRANSP'.
    MODIFY it_tables INDEX sy-tabix FROM is_tables.
  ENDLOOP.
*Sort data by object name
  SORT it_domains BY domname.
  SORT it_doma_values BY domname.
  SORT it_dataelements BY rollname.
  SORT it_tables BY tabname.
  SORT it_tabl_technics BY tabname.
  SORT it_tabl_fields BY tabname position.
ENDFORM.                    "F004_GET_FILE_DATA
*---------------------------------------------------------------------*
*F007_CREATE_DOMAINS.
*---------------------------------------------------------------------*
FORM f007_create_domains.
  DATA: ls_return TYPE bapiret2.
  CLEAR:is_domains, v_ext.

  LOOP AT it_domains INTO is_domains.
    CLEAR: ls_return.
*Check whether domain exists or not
    CALL FUNCTION 'DD_CHECK_NAME'
      EXPORTING
        name       = is_domains-domname
        objtyp     = 'DOMA'
      IMPORTING
        obj_exists = v_ext.
*If domain exists, populate information message
    IF v_ext EQ c_x.
      CLEAR v_text.
      v_text = 'Already Created'.
      v_msgid = space.
      v_msgty = 'I'.
      v_msgno = space.
      v_msgv1 = 'R3TR'.
      v_msgv2 = 'DOMA'.
      v_msgv3 = is_domains-domname.
      v_msgv4 = space.
      PERFORM f012_populate_messages
      USING v_msgid
            v_msgty
            v_msgno
            v_msgv1
            v_msgv2
            v_msgv3
            v_msgv4
            v_text.
    ENDIF.
*If domain doesn't exist
    IF v_ext <> c_x.
      CLEAR: v_create.
      v_create = c_yes.
    ENDIF. "V_EXT <> C_X
*Create the domain
    IF v_create = c_yes.
*Get domain attributes
      CLEAR is_domains_tmp.
      MOVE is_domains TO is_domains_tmp.
*Get domain range of values
* CLEAR:IS_DOMA_VALUES.
* LOOP AT IT_DOMA_VALUES INTO IS_DOMA_VALUES
* WHERE DOMNAME = IS_DOMAINS-DOMNAME.
*
* APPEND IS_DOMA_VALUES TO IT_DOMA_VALUES_TMP.
*
* ENDLOOP.
* Create domain
      CALL FUNCTION 'COM_GEN_DOMAIN_CREATE'
        EXPORTING
          iv_domain_name = is_domains-domname
          iv_devclass    = '$TMP'
          iv_activate    = 'X'
          is_dd01v       = is_domains_tmp
          it_dd07v       = it_doma_values_tmp
          iv_genflag     = 'T'
        IMPORTING
          es_return      = ls_return.
*Populate success message
      IF ls_return-type NE 'E'.
        CLEAR v_text.
        v_text = 'Created Successfully'.
        v_msgid = space.
        v_msgty = 'S'.
        v_msgno = space.
        v_msgv1 = 'R3TR'.
        v_msgv2 = 'DOMA'.
        v_msgv3 = is_domains-domname.
        v_msgv4 = space.
        PERFORM f012_populate_messages
        USING v_msgid
              v_msgty
              v_msgno
              v_msgv1
              v_msgv2
              v_msgv3
              v_msgv4
              v_text.
      ENDIF.
*Populate messages
      IF ls_return-type EQ 'E'.
        CLEAR v_text.
        PERFORM f012_populate_messages
        USING ls_return-id
              ls_return-type
              ls_return-number
              ls_return-message_v1
              ls_return-message_v2
              ls_return-message_v3
              ls_return-message_v4
              v_text.
      ENDIF.
    ENDIF. "V_CREATE = C_YES
    CLEAR: it_doma_values_tmp, it_doma_values_tmp[].
    CLEAR: v_create.
  ENDLOOP. "IT_DOMAINS
  CLEAR: is_domains_tmp, it_domains, it_domains[].
ENDFORM.                    "F007_CREATE_DOMAINS
*---------------------------------------------------------------------*
*F008_CREATE_DATAELEMENTS.
*---------------------------------------------------------------------*
FORM f008_create_dataelements.
  DATA: ls_return TYPE bapiret2.
  CLEAR: is_dataelements, v_ext.

  LOOP AT it_dataelements INTO is_dataelements.
*Check whether data element exists or not
    CALL FUNCTION 'DD_CHECK_NAME'
      EXPORTING
        name       = is_dataelements-rollname
        objtyp     = 'DTEL'
      IMPORTING
        obj_exists = v_ext.
*If exists, populate information message
    IF v_ext EQ c_x.
      CLEAR v_text.
      v_text = 'Already Created'.
      v_msgid = space.
      v_msgty = 'I'.
      v_msgno = space.
      v_msgv1 = 'R3TR'.
      v_msgv2 = 'DTEL'.
      v_msgv3 = is_dataelements-rollname.
      v_msgv4 = space.
      PERFORM f012_populate_messages
        USING v_msgid
              v_msgty
              v_msgno
              v_msgv1
              v_msgv2
              v_msgv3
              v_msgv4
              v_text.
    ENDIF.
*If data element doesn't exist
    IF v_ext <> c_x.
      CLEAR: v_create.
      v_create = c_yes.
    ENDIF. "V_EXT <> C_X
    IF v_create = c_yes.
*Get data element attributes
      CLEAR: is_dataelements_tmp.
      MOVE is_dataelements TO is_dataelements_tmp.
*Check if the domain exists, if not throw error
      CLEAR: v_exist.
      CALL FUNCTION 'DD_CHECK_NAME'
        EXPORTING
          name       = is_dataelements_tmp-domname
          objtyp     = 'DOMA'
        IMPORTING
          obj_exists = v_exist.
*If domain does not exist
      IF v_exist = c_space.
        CLEAR:line, v_text.
        CONCATENATE 'Domain' is_dataelements_tmp-domname
          'does not exist to create Data Element'
          is_dataelements-rollname
        INTO line SEPARATED BY space.
        v_text = line.
        v_msgv3 = is_dataelements-rollname.
        PERFORM f012_populate_messages
          USING space
                'E'
                v_msgno
                'R3TR'
                'DTEL'
                v_msgv3
                v_msgv4
                v_text.
      ENDIF.
*If domain exists
      IF v_exist = c_x.
*Create data element
        CLEAR: ls_return.
        CALL FUNCTION 'COM_GEN_DATAELEMENT_CREATE'
          EXPORTING
            iv_dtel_name = is_dataelements-rollname
            iv_devclass  = '$TMP'
            iv_activate  = 'X'
            is_dd04v     = is_dataelements_tmp
          IMPORTING
            es_return    = ls_return.
*Populate success message
        IF ls_return-type NE 'E'.
          CLEAR v_text.
          v_text = 'Created Successfully'.
          v_msgid = space.
          v_msgty = 'S'.
          v_msgno = space.
          v_msgv1 = 'R3TR'.
          v_msgv2 = 'DATE'.
          v_msgv3 = is_dataelements-rollname.
          v_msgv4 = space.
          PERFORM f012_populate_messages
            USING v_msgid
                  v_msgty
                  v_msgno
                  v_msgv1
                  v_msgv2
                  v_msgv3
                  v_msgv4
                  v_text.
        ENDIF.
*Populate messages
        IF ls_return-type EQ 'E'.
          CLEAR v_text.
          PERFORM f012_populate_messages
            USING ls_return-id
                  ls_return-type
                  ls_return-number
                  ls_return-message_v1
                  ls_return-message_v2
                  ls_return-message_v3
                  ls_return-message_v4
                  v_text.
        ENDIF.
      ENDIF. "V_EXIST = C_X
    ENDIF. "V_CREATE = C_YES
  ENDLOOP. "IT_DATAELEMENTS
  CLEAR: it_dataelements, it_dataelements[], is_dataelements_tmp.
ENDFORM.                    "F008_CREATE_DATAELEMENTS
*---------------------------------------------------------------------*
*F009_CREATE_TABLES.
*---------------------------------------------------------------------*
FORM f009_create_tables.
  DATA: ls_return TYPE bapiret2.
  CLEAR:is_tables, v_ext.
  LOOP AT it_tables INTO is_tables.
    CLEAR:v_table_name_check.
    v_table_name_check = is_tables-tabname.
*Check whether table exists or not
    CALL FUNCTION 'DD_CHECK_NAME'
      EXPORTING
        name       = v_table_name_check
        objtyp     = 'TABL'
      IMPORTING
        obj_exists = v_ext.
*If exists, populate information message
    IF v_ext EQ c_x.
      CLEAR v_text.
      v_text = 'Already Created'.
      v_msgid = space.
      v_msgty = 'I'.
      v_msgno = space.
      v_msgv1 = 'R3TR'.
      v_msgv2 = 'TABL'.
      v_msgv3 = v_table_name_check.
      v_msgv4 = space.
      PERFORM f012_populate_messages
        USING v_msgid
              v_msgty
              v_msgno
              v_msgv1
              v_msgv2
              v_msgv3
              v_msgv4
              v_text.
    ENDIF.
*If table doesn't exist
    IF v_ext <> c_x.
      CLEAR: v_create.
      v_create = c_yes.
    ENDIF. "V_EXT <> C_X
    IF v_create = c_yes.
*Get table header information
      CLEAR: it_tabl_inf_tmp.
      MOVE is_tables TO it_tabl_inf_tmp.
*Get table technics
      CLEAR: is_tabl_technics, is_tabl_technics_tmp.
      READ TABLE it_tabl_technics INTO is_tabl_technics WITH KEY tabname
      = is_tables-tabname.
      IF sy-subrc = 0.
        MOVE is_tabl_technics TO is_tabl_technics_tmp.
      ENDIF.
*Get table fields
      LOOP AT it_tabl_fields INTO is_tabl_fields
      WHERE tabname = is_tables-tabname.
        APPEND is_tabl_fields TO it_tabl_fields_tmp.
        CLEAR is_tabl_fields.
      ENDLOOP.
*Check if all the data elements exists or not
      CLEAR: v_exist.
      LOOP AT it_tabl_fields_tmp INTO is_tabl_fields.
        CLEAR:v_exist.
        CALL FUNCTION 'DD_CHECK_NAME'
          EXPORTING
            name       = is_tabl_fields-rollname
            objtyp     = 'DTEL'
          IMPORTING
            obj_exists = v_exist.
        IF v_exist = c_x.
          CONTINUE.
        ELSE.
          CLEAR:line, v_text.
          CONCATENATE 'Data Element' is_tabl_fields-rollname
            'does not exist to create Table'
            is_tabl_fields-tabname
          INTO line SEPARATED BY space.
          v_text = line.
          v_msgv3 = is_tabl_fields-tabname.
          PERFORM f012_populate_messages
            USING space
                  'E'
                  v_msgno
                  'R3TR'
                  'TABL'
                  v_msgv3
                  v_msgv4
                  v_text.
          EXIT.
        ENDIF.
      ENDLOOP. "IT_TABL_FIELDS_TMP
      CLEAR: is_tabl_fields.
*If all data elements exist
      IF v_exist = c_x.
*Create tables
        CLEAR: ls_return.
        CALL FUNCTION 'COM_GEN_DBTABLE_CREATE'
          EXPORTING
            iv_dbtable_name = is_tables-tabname
            iv_devclass     = '$TMP'
            iv_activate     = 'X'
            it_dd03p        = it_tabl_fields_tmp
            is_dd02v        = it_tabl_inf_tmp
            is_dd09l        = is_tabl_technics_tmp
            iv_genflag      = 'T'
          IMPORTING
            es_return       = ls_return.
*Populate success message
        IF ls_return-type NE 'E'.
          CLEAR v_text.
          v_text = 'Created Successfully'.
          v_msgid = space.
          v_msgty = 'S'.
          v_msgno = space.
          v_msgv1 = 'R3TR'.
          v_msgv2 = 'TABL'.
          v_msgv3 = is_tables-tabname.
          v_msgv4 = space.
          PERFORM f012_populate_messages
            USING v_msgid
                  v_msgty
                  v_msgno
                  v_msgv1
                  v_msgv2
                  v_msgv3
                  v_msgv4
                  v_text.
        ENDIF.
        IF ls_return-type EQ 'E'.
          CLEAR v_text.
          PERFORM f012_populate_messages
            USING ls_return-id
                  ls_return-type
                  ls_return-number
                  ls_return-message_v1
                  ls_return-message_v2
                  ls_return-message_v3
                  ls_return-message_v4
                  v_text.
        ENDIF.
      ENDIF. "V_EXIST = C_X
    ENDIF. "V_CREATE = C_YES
    CLEAR: it_tabl_fields_tmp,it_tabl_inf_tmp, is_tabl_technics_tmp.
    REFRESH: it_tabl_fields_tmp.
  ENDLOOP. "IT_TABLES
ENDFORM.                    "F009_CREATE_TABLES
*---------------------------------------------------------------------*
* Form F011_DISPLAY_MESSAGES
*---------------------------------------------------------------------*
FORM f011_display_messages.
  DATA: lt_fieldcat TYPE slis_t_fieldcat_alv,
        ls_fieldcat TYPE slis_fieldcat_alv.
  DATA: lv_repid TYPE sy-repid.
  lv_repid = sy-repid.
  IF it_messages IS INITIAL.
    MESSAGE i002(sy) WITH 'No data available for processing'.
    LEAVE PROGRAM.
  ENDIF.
  IF NOT it_messages IS INITIAL.
    ls_fieldcat-col_pos = 1.
    ls_fieldcat-fieldname = 'MANDT'.
    ls_fieldcat-seltext_l = 'Client'.
    ls_fieldcat-outputlen = 7.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 2.
    ls_fieldcat-fieldname = 'MSGID'.
    ls_fieldcat-seltext_l = 'Message ID'.
    ls_fieldcat-outputlen = 12.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 3.
    ls_fieldcat-fieldname = 'MSGTY'.
    ls_fieldcat-seltext_l = 'Type'.
    ls_fieldcat-outputlen = 5.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 4.
    ls_fieldcat-fieldname = 'MSGNO'.
    ls_fieldcat-seltext_l = 'Number'.
    ls_fieldcat-outputlen = 7.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 5.
    ls_fieldcat-fieldname = 'MSGV1'.
    ls_fieldcat-seltext_l = 'Variable 1'.
    ls_fieldcat-outputlen = 15.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 6.
    ls_fieldcat-fieldname = 'MSGV2'.
    ls_fieldcat-seltext_l = 'Variable 2'.
    ls_fieldcat-outputlen = 15.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 7.
    ls_fieldcat-fieldname = 'MSGV3'.
    ls_fieldcat-seltext_l = 'Variable 3'.
    ls_fieldcat-outputlen = 15.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 8.
    ls_fieldcat-fieldname = 'MSGV4'.
    ls_fieldcat-seltext_l = 'Variable 4'.
    ls_fieldcat-outputlen = 15.
    APPEND ls_fieldcat TO lt_fieldcat.
    ls_fieldcat-col_pos = 9.
    ls_fieldcat-fieldname = 'TEXT'.
    ls_fieldcat-seltext_l = 'Text'.
    ls_fieldcat-outputlen = 50.
    APPEND ls_fieldcat TO lt_fieldcat.
*List display
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program = lv_repid
        i_grid_title       = 'Messages'
        it_fieldcat        = lt_fieldcat
      TABLES
        t_outtab           = it_messages.
  ENDIF.
ENDFORM. "F011_DISPLAY_MESSAGES
*--------------------------------------------------------------------*
* Form F012_POPULATE_MESSAGES
*--------------------------------------------------------------------*
FORM f012_populate_messages USING v_msgid TYPE sy-msgid
                                  v_msgty TYPE sy-msgty
                                  v_msgno TYPE sy-msgno
                                  v_msgv1 TYPE sy-msgv1
                                  v_msgv2 TYPE sy-msgv2
                                  v_msgv3 TYPE sy-msgv3
                                  v_msgv4 TYPE sy-msgv4
                                  v_text TYPE any.
*If message text is not available
  IF v_text = c_space.
    is_messages-mandt = sy-mandt.
    is_messages-msgid = v_msgid.
    is_messages-msgty = v_msgty.
    is_messages-msgno = v_msgno.
    is_messages-msgv1 = v_msgv1.
    is_messages-msgv2 = v_msgv2.
    is_messages-msgv3 = v_msgv3.
    is_messages-msgv4 = v_msgv4.
*Build the message text
    CALL FUNCTION 'FORMAT_MESSAGE'
      EXPORTING
        id        = v_msgid
        lang      = 'E'
        no        = v_msgno
        v1        = v_msgv1
        v2        = v_msgv2
        v3        = v_msgv3
        v4        = v_msgv4
      IMPORTING
        msg       = is_messages-text
      EXCEPTIONS
        not_found = 1
        OTHERS    = 2.
    IF sy-subrc <> 0 OR is_messages-text = c_space.
      is_messages-text = 'MESSAGE TEXT NOT FOUND'.
    ENDIF.
    APPEND is_messages TO it_messages.
    CLEAR is_messages.
  ELSE. "V_TEXT = C_SPACE
    is_messages-mandt = sy-mandt.
    is_messages-msgid = v_msgid.
    is_messages-msgty = v_msgty.
    is_messages-msgno = v_msgno.
    is_messages-msgv1 = v_msgv1.
    is_messages-msgv2 = v_msgv2.
    is_messages-msgv3 = v_msgv3.
    is_messages-msgv4 = v_msgv4.
    is_messages-text = v_text.
    APPEND is_messages TO it_messages.
    CLEAR is_messages.
  ENDIF. "V_TEXT = C_SPACE.
ENDFORM. "F012_POPULATE_MESSAGES
*&---------------------------------------------------------------------*
*& Form F_FILE_CHECK
*&---------------------------------------------------------------------*
FORM f_file_check.
*Input file should not be blank
  IF p_file = c_space.
    SET CURSOR FIELD 'P_FILE'.
    MESSAGE e002(sy) WITH 'Enter Input File Name!'.
  ENDIF.
ENDFORM. " F_FILE_CHECK
*&---------------------------------------------------------------------*
*& Form f_top_of_page
*&---------------------------------------------------------------------*
FORM f_top_of_page.
  CLEAR:line.
  CONCATENATE 'Customized Database Tables Generation by'
    sy-uname
  INTO line SEPARATED BY space.
  WRITE :/ line.
  WRITE :/10 'DATE:',sy-datum.
ENDFORM. " F_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*& Form f_end_of_page
*&---------------------------------------------------------------------*
FORM f_end_of_page.
  WRITE:sy-uline(90).
  WRITE:/30 'PAGE:',sy-pagno CENTERED.
ENDFORM. " F_END_OF_PAGE
*&---------------------------------------------------------------------*
*& Form F_FREE_MEMORY
*&---------------------------------------------------------------------*
FORM f_free_memory.
*Clear internal tables.
  CLEAR:
  it_tables,
  it_tabl_inf_tmp,
  it_tabl_technics,
  is_tabl_technics_tmp,
  it_tabl_fields,
  it_tabl_fields_tmp,
  it_domains,
  is_domains_tmp,
  it_doma_values,
  it_doma_values_tmp,
  it_dataelements,
  is_dataelements_tmp,
  it_messages.
*Refresh internal tables
  REFRESH:
  it_tables,
  it_tabl_technics,
  it_tabl_fields,
  it_tabl_fields_tmp,
  it_domains,
  it_doma_values,
  it_doma_values_tmp,
  it_dataelements,
  it_messages.
*Free internal tables/work areas
  FREE:
  it_tables,
  it_tabl_inf_tmp,
  it_tabl_technics,
  is_tabl_technics_tmp,
  it_tabl_fields,
  it_tabl_fields_tmp,
  it_domains,
  is_domains_tmp,
  it_doma_values,
  it_doma_values_tmp,
  it_dataelements,
  is_dataelements_tmp,
  it_messages.
*Clear variables
  CLEAR:
  v_msgid ,
  v_msgty,
  v_msgno,
  v_msgv1,
  v_msgv2,
  v_msgv3,
  v_msgv4,
  v_text,
  line,
  v_table_name_check,
  v_ext,
  v_exist.
*Free variables
  FREE:
  v_msgid ,
  v_msgty,
  v_msgno,
  v_msgv1,
  v_msgv2,
  v_msgv3,
  v_msgv4,
  v_text,
  line,
  v_table_name_check,
  v_ext,
  v_exist.
*Unassign field symbols
  UNASSIGN <fs_value>.
ENDFORM. " F_FREE_MEMORY

This program reads a forecast file from PPC and loads
*                forecast quantities into APO Demand Planning LiveCache
*                - Key Figure 'Customer Forecast Units' (Z_CUSFU).
*                Include change request PD7K900203 in initial transport.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zpliusfcast MESSAGE-ID zj01
       LINE-SIZE 132.
************************************************************************
* PROGRAM....... ZPLIUSFCAST
* TITLE......... APO Forecast Interface
* AUTHOR........ Aveek Ghose
* DATE WRITTEN.. 15/09/2006
* R/3 RELEASE... 4.6C
* =====================================================================*
* COPIED FROM... N/A
* DESCRIPTION... This program reads a forecast file from PPC and loads
*                forecast quantities into APO Demand Planning LiveCache
*                - Key Figure 'Customer Forecast Units' (Z_CUSFU).
*                Include change request PD7K900203 in initial transport.
* =====================================================================*
* PROGRAM TYPE.. Inbound Interface
* DEV. CLASS.... ZPLN
* LOGICAL DB.... N/A
* =====================================================================*
* SCREENS....... N/A
* GUI TITLE..... N/A
* GUI STATUS.... N/A
* TRANSACTIONS.. N/A
* USER EXITS.... N/A
* =====================================================================*
* CHANGE HISTORY                                                       *
* Date       By        Correction Number & Brief Description     Release

*----------------------------------------------------------------------*
* Tables / Structures
*----------------------------------------------------------------------*

*MOD-011
*TABLES: zpln_ppc_loc,
*        zpln_ppc_mat.
*MOD-011

* Selection table for BAPI function modules
DATA: t_selection TYPE STANDARD TABLE OF bapi10030pbselection,
      g_selection_wa LIKE LINE OF t_selection.

* Key Figure table for BAPI function module
DATA: t_key_figure_selection TYPE STANDARD TABLE OF bapi10030keyfigure,
      g_key_figure_selection_wa LIKE LINE OF t_key_figure_selection.

* Group By table for BAPI function module
DATA: t_group_by TYPE STANDARD TABLE OF bapi10030pbgroupby,
      g_group_by_wa LIKE LINE OF t_group_by.

* Time Series table output from BAPI_PBSRVAPS_GETDETAIL
DATA: t_time_series TYPE STANDARD TABLE OF bapi10030pbtimeserieso,
      g_time_series_wa LIKE LINE OF t_time_series.

* Time Series table input to BAPI_PBSRVAPS_CHANGEKEYFIGVAL
DATA: t_time_series_chg TYPE STANDARD TABLE OF bapi10030pbtimeseries,
      g_time_series_chg_wa LIKE LINE OF t_time_series_chg.

* Time Bucket data input to BAPI_PBSRVAPS_CHANGEKEYFIGVAL
DATA: t_time_series_item TYPE STANDARD TABLE OF
                                           bapi10030pbtimeseriesitem,
      g_time_series_item_wa LIKE LINE OF t_time_series_item.

* Characteristic Combination table for BAPI_PBSRVAPS_CHANGEKEYFIGVAL
DATA: t_char_comb TYPE STANDARD TABLE OF bapi10030pbcharacteristics,
      g_char_comb_wa LIKE LINE OF t_char_comb.

* Return table from BAPI function modules
DATA: t_return TYPE STANDARD TABLE OF bapiret2,
      g_return_wa LIKE LINE OF t_return.

TYPES: BEGIN OF ty_forecast_data,
        source_code(2) TYPE c,       "Source Code
        lang_group(21) TYPE c,       "Language Group
        pres_code(6) TYPE c,         "Presentation Code
        pres_name(40) TYPE c,        "Presentation Name
        site(4)      TYPE c,         " PPC Location
        indication(3) TYPE c,        "Indication
        date         TYPE datum,     "Date
        gi_date      TYPE datum,     "Date
        ord_number(8) TYPE c,        "Order Number
        ord_line(6)  TYPE c,         "Order Line Number
        units(20) TYPE c,            "Units
      END OF ty_forecast_data.

TYPES: BEGIN OF ty_forecast_data1,
       source_code(2) TYPE c,       "Source Code
       lang_group(21) TYPE c,       "Language Group
       pres_code(6) TYPE c,         "Presentation Code
       r3_material(40) TYPE c,      " R/3 Material Code
       pres_name(40) TYPE c,        "Presentation Name
       site(4)      TYPE c,         " PPC Location
       r3_loc(10)    TYPE c,        " R/3 Location
       indication(3) TYPE c,        "Indication
       date         TYPE datum,     "Date
       gi_date      TYPE datum,     "Date
       ord_number(8) TYPE c,        "Order Number
       ord_line(6)  TYPE c,         "Order Line Number
       units(20) TYPE c,            "Units
       forecast_cv(20) TYPE c,      " Forecast Quantity (converted)
       calweek LIKE scal-week,      " Calendar Week
       calweek_end LIKE scal-week,       " Calendar Week
       calmonth(6) TYPE c,          " Calendar Month
       calweekend_date TYPE scal-date,   " Calendar Week End date
       calyear(6) TYPE c,           " Calendar Year
       calweek_diff(4) TYPE c,      "Week Diff
       startweek LIKE scal-week,
       endweek   LIKE scal-week,
       start_day LIKE scal-date,
       end_day   LIKE scal-date,
       calyear_start(4) TYPE c,     " Year Start
       calyear_end(4) TYPE c,       " Year End
       delete(1) TYPE c,            "Delete Flag
      END OF ty_forecast_data1.

TYPES: BEGIN OF ty_forecast_data2,
      source_code(2) TYPE c,       "Source Code
      lang_group(21) TYPE c,       "Language Group
      pres_code(6) TYPE c,         "Presentation Code
      r3_material(40) TYPE c,      " R/3 Material Code
      pres_name(40) TYPE c,        "Presentation Name
      site(4)      TYPE c,         " PPC Location
      r3_loc(10)    TYPE c,        " R/3 Location
      indication(3) TYPE c,        "Indication
      calweek LIKE scal-week,      " Calendar Week
      calweek_end TYPE scal-week,  " Calendar Week
      endweek   LIKE scal-week,
      forecast_cv(20) TYPE c,         " Forecast Quantity (converted)
      date         TYPE datum,     "Date
      gi_date      TYPE datum,     "Date
      ord_number(8) TYPE c,        "Order Number
      ord_line(6)  TYPE c,         "Order Line Number
      units(20) TYPE c,            "Units
      calyear_start(4) TYPE c,     " Year Start
      calyear_end(4) TYPE c,       " Year End
      calmonth(6) TYPE c,          " Calendar Month
      calyear(6) TYPE c,           " Calendar Year
      startweek LIKE scal-week,
      start_day LIKE scal-date,
      end_day   LIKE scal-date,
      delete(1) TYPE c,            "Delete Flag
     END OF ty_forecast_data2.

TYPES: BEGIN OF ty_forecast_data4,
        r3_material(40) TYPE c,      " R/3 Material Code
        site(4)      TYPE c,         " PPC Location
        r3_loc(10)    TYPE c,        " R/3 Location
        indication(3) TYPE c,        "Indication
        calweek LIKE scal-week,      " Calendar Week
        source_code(2) TYPE c,       "Source Code
        lang_group(21) TYPE c,       "Language Group
        pres_code(6) TYPE c,         "Presentation Code
        pres_name(40) TYPE c,        "Presentation Name
        calweek_end TYPE scal-week,  " Calendar Week
        endweek   LIKE scal-week,
        forecast_cv(20) TYPE c,         " Forecast Quantity (converted)
        date         TYPE datum,     "Date
        gi_date      TYPE datum,     "Date
        ord_number(8) TYPE c,        "Order Number
        ord_line(6)  TYPE c,         "Order Line Number
        units(20) TYPE c,            "Units
        calyear_start(4) TYPE c,     " Year Start
        calyear_end(4) TYPE c,       " Year End
        calmonth(6) TYPE c,          " Calendar Month
        calyear(6) TYPE c,           " Calendar Year
        startweek LIKE scal-week,
        start_day LIKE scal-date,
        end_day   LIKE scal-date,
        delete(1) TYPE c,            "Delete Flag
     END OF ty_forecast_data4.

DATA: t_forecast_data2 TYPE STANDARD TABLE OF
      ty_forecast_data2 INITIAL SIZE 0 WITH HEADER LINE.

DATA: t_forecast_data3 TYPE STANDARD TABLE OF
      ty_forecast_data2 INITIAL SIZE 0 WITH HEADER LINE.

DATA: t_forecast_data4 TYPE STANDARD TABLE OF
      ty_forecast_data4 INITIAL SIZE 0 WITH HEADER LINE.

DATA: t_forecast_data TYPE STANDARD TABLE OF
           ty_forecast_data INITIAL SIZE 0 WITH HEADER LINE,
      t_forecast_data1 TYPE STANDARD TABLE OF
                     ty_forecast_data1 INITIAL SIZE 0 WITH HEADER LINE,
      g_forecast_data_wa LIKE LINE OF t_forecast_data1,
      g_forecast_data_wa2 LIKE LINE OF t_forecast_data2,
      g_forecast_data_wa3 LIKE LINE OF t_forecast_data3,
      l_forecast_data TYPE STANDARD TABLE OF ty_forecast_data1,
      l_forecast_data_wa LIKE LINE OF t_forecast_data1,
      l_forecast_data2 TYPE STANDARD TABLE OF ty_forecast_data2,
      l_forecast_data_wa2 LIKE LINE OF t_forecast_data2.

* Smaller forecast table to hold data for specific product/plant
* combination]
*MOD-006
*DATA: t_forecast_sub TYPE STANDARD TABLE OF ty_forecast_data1,
*      g_forecast_sub_wa LIKE LINE OF t_forecast_sub.
*MOD-006

DATA: t_forecast_sub TYPE STANDARD TABLE OF ty_forecast_data2,
      t_forecast_sub1 TYPE STANDARD TABLE OF ty_forecast_data2,
      g_forecast_sub_wa LIKE LINE OF t_forecast_sub.

* Error table
TYPES: BEGIN OF ty_error_data.
        INCLUDE TYPE ty_forecast_data1.
TYPES:   error_type(1)   TYPE c,
       END OF ty_error_data.

DATA: t_error_data TYPE STANDARD TABLE OF ty_error_data,
      g_error_data_wa LIKE LINE OF t_error_data.

* Structure to hold input file data
TYPES: BEGIN OF g_input_data1,
        line(255) TYPE c,
       END OF g_input_data1.

DATA: g_input_data TYPE STANDARD TABLE OF
                  g_input_data1 INITIAL SIZE 0 WITH HEADER LINE.

*----------------------------------------------------------------------*
* Global Variables
*----------------------------------------------------------------------*
DATA: g_date_from    TYPE /sapapo/cdps_eval_date_from, " From Date
      g_date_to      TYPE /sapapo/cdps_eval_date_to.   " To Date

DATA: g_file  LIKE dxfile-filename,   " Filename
      g_material TYPE /sapapo/matnr,  " Material
      g_cnt_input_recs TYPE i,        " Number of file records
      g_cnt_changes    TYPE i,        " Number of changes attempted
      g_cnt_error_recs  TYPE i,       " Number of records with errors
      g_flg_error.                    " Error

*----------------------------------------------------------------------*
* Objects
*----------------------------------------------------------------------*
DATA: i_file        TYPE REF TO zcl_file.

*----------------------------------------------------------------------*
* Constants
*----------------------------------------------------------------------*
CONSTANTS: c_planningbook TYPE /sapapo/pb_mview      " Planning Book
              VALUE 'Z_PPC_FCST_DP',
           c_period_type TYPE /sapapo/cdps_eval_type " Period Type
              VALUE 'A'.

CONSTANTS:
     c_version(22) TYPE c            VALUE '000',  " Version
     c_log_type    TYPE c            VALUE 'L',    " Logical Filetype
     c_logqs     TYPE /sapapo/logqs  VALUE 'PSGA', " Business Sys Grp
     c_ppc_mat_error(1)   TYPE c VALUE '1',
                       " PPC Material not found in custom mapping table
     c_ppc_loc_error(1)   TYPE c VALUE '2',
                       " PPC Location not found in custom mapping table
     c_r3_mat_error(1)    TYPE c VALUE '3',
                       " R/3 to APO material mapping error
     c_r3_loc_error(1)    TYPE c VALUE '4',
                       " R/3 to APO location mapping error
     c_pbook_error(1)     TYPE c VALUE '5',
                       " Error getting planning book information
     c_date_error(1)      TYPE c VALUE '6',
                       " Invalid date error
     c_change_error(1)    TYPE c VALUE '7',
                       " Error changing key figure values
     c_per_ind     TYPE /sapapo/perkz  VALUE 'P',    " Period Indicator
     c_fisc_var    TYPE /sapapo/periv  VALUE 'JJ', " Fiscal year variant
     c_separator   TYPE c              VALUE ',',    " Field separator
     c_no_hdr_material(1) TYPE c VALUE '1',
     c_loctype TYPE /sapapo/c_loctype VALUE '1002'.
" No header material in file record

*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
* Files to process
SELECTION-SCREEN BEGIN OF BLOCK file WITH FRAME TITLE text-001.
SELECTION-SCREEN SKIP.

SELECTION-SCREEN BEGIN OF BLOCK infile WITH FRAME TITLE text-002.
PARAMETERS: p_login LIKE filename-fileintern OBLIGATORY
                    DEFAULT 'Z_DDS_43525_GPSG_EMEA_DEMAND'.
PARAMETERS: p_phyin LIKE filename-fileextern.
PARAMETERS: p_horz(4) TYPE c DEFAULT '60'.
PARAMETERS: p_dist(4) TYPE c DEFAULT '0200'.
PARAMETERS: p_logsys TYPE edipparnum  DEFAULT 'PD1120DEV'.
SELECTION-SCREEN END OF BLOCK infile.

SELECTION-SCREEN END OF BLOCK file.

*&---------------------------------------------------------------------*
* At Selection Screen
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.

  PERFORM sub_get_physical_file USING p_login p_phyin.

*&---------------------------------------------------------------------*
* At Selection Screen Output
*&---------------------------------------------------------------------*
* Make the physical file name display only
AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.
    IF screen-name = 'P_PHYIN'.
      screen-input = 0.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

*&---------------------------------------------------------------------*
* INITIALIZATION.
*&---------------------------------------------------------------------*
INITIALIZATION.

  CLEAR: g_return_wa,
         g_selection_wa,
         g_key_figure_selection_wa,
         g_group_by_wa,
         g_time_series_wa,
         g_time_series_chg_wa,
         g_time_series_item_wa,
         g_char_comb_wa,
         g_forecast_data_wa,
         g_forecast_sub_wa,
         g_error_data_wa,
         g_input_data,
         g_date_from,
         g_date_to,
         g_cnt_input_recs,
         g_cnt_changes,
         g_cnt_error_recs,
         g_flg_error.

  REFRESH: t_return,
           t_selection,
           t_key_figure_selection,
           t_group_by,
           t_time_series,
           t_time_series_chg,
           t_time_series_item,
           t_char_comb,
           t_forecast_data,
           t_forecast_sub,
           t_error_data.

  PERFORM sub_get_physical_file USING p_login p_phyin.

*&---------------------------------------------------------------------*
* Start-Of-Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* Read input file into internal table
  PERFORM sub_read_file.
  PERFORM validate_locmap.
  IF NOT t_forecast_data[] IS INITIAL.
* Map data to format for processing
    PERFORM sub_map_data.
    IF NOT t_forecast_data2[] IS INITIAL.
** Process valid data records
      PERFORM sub_process_data.
    ENDIF.
  ENDIF.
* Display program results
  PERFORM sub_display_results.

**&---------------------------------------------------------------------
**
*&      Form  sub_display_results
*&---------------------------------------------------------------------*
*       Write results to spool
*----------------------------------------------------------------------*
FORM sub_display_results.

*** Program Results
  SKIP.
  WRITE: / text-025.
*** File locations

* Input File
  SKIP.
  WRITE: / text-026,
        15 p_phyin.
*** Record Counts
  SKIP.
* Number of records in input file
  WRITE: / text-027,
        48 g_cnt_input_recs.
* Number of records with errors
  WRITE: / text-028,
        48 g_cnt_error_recs.
* Number of records successfully updated in planning book
  WRITE: / text-029,
        48 g_cnt_changes.

*MOD-010
  IF NOT t_error_data[] IS INITIAL.
* Error Details:
    SKIP.
    WRITE: / text-024.
    SKIP.
    WRITE: / text-006,
          13 text-007,
          29 text-008,
          41 text-009,
          61 text-010.

    WRITE: / text-011 UNDER text-006,
             text-012 UNDER text-007,
             text-013 UNDER text-008,
             text-014 UNDER text-009,
             text-015 UNDER text-010.

*MOD-010
    SORT t_error_data BY pres_code
                         site
                         date
                         forecast_cv.
    DELETE ADJACENT DUPLICATES FROM t_error_data COMPARING pres_code
                                                           site
                                                           date
                                                           forecast_cv.
*MOD-010

    LOOP AT t_error_data INTO g_error_data_wa.

      WRITE: / g_error_data_wa-pres_code UNDER text-006,
               g_error_data_wa-site     UNDER text-007,
               g_error_data_wa-date     UNDER text-008,
            48 g_error_data_wa-forecast_cv RIGHT-JUSTIFIED.

      CASE g_error_data_wa-error_type.
        WHEN c_ppc_mat_error.
          WRITE text-031 UNDER text-010.
        WHEN c_r3_mat_error.
          WRITE text-019 UNDER text-010.
        WHEN c_r3_loc_error.
          WRITE text-020 UNDER text-010.
        WHEN c_pbook_error.
          WRITE text-021 UNDER text-010.
        WHEN c_date_error.
          WRITE text-022 UNDER text-010.
        WHEN c_change_error.
          WRITE text-023 UNDER text-010.
      ENDCASE.
      CLEAR: g_error_data_wa.
    ENDLOOP.

    IF sy-batch IS INITIAL.
      MESSAGE i000 WITH text-030.
    ELSE.
*      MESSAGE e000 WITH text-030.                          "TPR1336-
      MESSAGE i000 WITH text-030.                           "TPR1336+
    ENDIF.

  ENDIF.

ENDFORM.                    " sub_display_results
*&---------------------------------------------------------------------*
*&      Form  sub_map_data
*&---------------------------------------------------------------------*
*       Map PPC codes to SAP codes and convert input values
*----------------------------------------------------------------------*
FORM sub_map_data.

  DATA: l_forecast(6) TYPE c,
        l_uom         TYPE i.
  DATA:
  l_sold_num(10) TYPE n,            " Sold To Quantity (numerical char.)
  l_sold(7)      TYPE p DECIMALS 3, " Sold To Quantity (Packed, 3 dec.)
  l_calweek LIKE scal-week,         " Calendar Week
  l_calweek_end LIKE scal-week,     " Calendar Week
  l_startcalweek LIKE scal-week,         " Calendar Week

  l_calweek1(2) TYPE c,             " Calendar Week
  l_calweek_end1(2) TYPE c,         " Calendar Week
  l_calweekend_date LIKE scal-week, " Calendar Week
  l_calmonth(6)  TYPE c,            " Calendar Month
  l_calyear_start(4) TYPE c,        " Calendar Year
  l_calyear_end(4) TYPE c,          " Calendar Year
  l_calweek_diff TYPE scal-week,    " Diff in week
  l_fiscper(7)   TYPE c.            " Fiscal Period

  DATA: l_temp_wa LIKE g_forecast_data_wa2,
        l_hold_wa LIKE g_forecast_data_wa2.
  DATA: lt_forecast_data2 TYPE STANDARD TABLE OF
        ty_forecast_data2 INITIAL SIZE 0 WITH HEADER LINE.
  DATA: lt_forecast_data3 TYPE STANDARD TABLE OF
        ty_forecast_data2 INITIAL SIZE 0 WITH HEADER LINE.
  DATA: lt_forecast_data4 TYPE STANDARD TABLE OF
        ty_forecast_data2 INITIAL SIZE 0 WITH HEADER LINE.

  DATA: l_wa_forecast_data TYPE ty_forecast_data2.
  DATA: l_wa_forecast_data1 TYPE ty_forecast_data1.
  DATA: l_wa_forecast_data2 TYPE ty_forecast_data2.
*MOD-008
* Local variables
  DATA: l_curr_date    TYPE sydatum,  " Program Run Date
        l_record_date  TYPE sydatum,  " Formatted Record Date
        l_curr_year(2) TYPE c,        " Program Year (without century)
        l_curr_year_num TYPE i,       " Integer value of Program Year
        l_century(2)     TYPE c,      " Program Century (without year)
        l_century_num    TYPE i,      " Integer value of Century
        l_input_year_num TYPE i.      " Integer value of Record Year

* Record date - format MM/DD/YY
  DATA: BEGIN OF l_input_date,
          month(2)  TYPE c,
          filler1(1) TYPE c,
          day(2)     TYPE c,
          filler2(1) TYPE c,
          year(2)    TYPE c,
        END OF l_input_date.
*MOD-008

  FIELD-SYMBOLS:
    <l_wa_forecast>     TYPE ty_forecast_data2.

  DATA: lv_forecast_cv TYPE f.
  CLEAR: lv_forecast_cv.

  DATA: l_first_day LIKE scal-date,
        l_last_day  LIKE scal-date.
  DATA: l_first_day1 LIKE scal-date,
        l_last_day1  LIKE scal-date.
  DATA: l_start_week LIKE scal-week,
        l_end_week   LIKE scal-week.
  DATA: l_start_week1 LIKE scal-week,
        l_end_week1   LIKE scal-week.
  DATA: lv_sap_code(50) TYPE c.
  DATA: return TYPE bapiret2.
  DATA: l_date_tabix LIKE sy-tabix.
  DATA: l_tabix TYPE sy-tabix.
  DATA: l_start_year(4) TYPE c,
        l_end_year(4) TYPE c,
        l_calyear_diff(4) TYPE c,
        l_calyear_data_start(4) TYPE c.

  LOOP AT t_forecast_data.
    MOVE-CORRESPONDING t_forecast_data TO t_forecast_data1.
    APPEND t_forecast_data1.
    CLEAR: t_forecast_data1.
  ENDLOOP.

* Loop through internal table
  LOOP AT t_forecast_data1 INTO g_forecast_data_wa.
* Map Material code to SAP product and conversion factor using
* cross-reference table; if not found, delete record and store
* error.

    CLEAR: lv_sap_code.
    CALL FUNCTION 'Z_XREF_LOOKUP' DESTINATION p_logsys
      EXPORTING
        transid  = 'ZELCUSMATLOOKUP'
        invalue  = g_forecast_data_wa-pres_code
      IMPORTING
        outvalue = lv_sap_code
        return   = return.

*  IF SY-SUBRC = 0.
    IF NOT lv_sap_code IS INITIAL. "RETURN IS INITIAL.
* Convert SAP Material Code to APO Material Number; if not found,
* delete record and store error
      SELECT SINGLE matnr FROM /sapapo/matmap
        INTO g_forecast_data_wa-r3_material
        WHERE ext_matnr = lv_sap_code
        AND logqs     = c_logqs.
      IF sy-subrc <> 0.
        MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
        g_error_data_wa-error_type = c_r3_mat_error.
        APPEND g_error_data_wa TO t_error_data.
        CLEAR g_error_data_wa.
        DELETE t_forecast_data1.
        CLEAR g_forecast_data_wa.
        ADD 1 TO g_cnt_error_recs.
        CONTINUE.
      ENDIF.
*MOD-011
*     l_uom = 1.
*MOD-011
    ELSE.
* Store material in error table; delete current record of data
* table; continue
      MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_ppc_mat_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_data1.
      CLEAR g_forecast_data_wa.
      ADD 1 TO g_cnt_error_recs.
      CONTINUE.
    ENDIF.
** Convert Forecast Quantity using conversion factor
    l_forecast = g_forecast_data_wa-units.
    g_forecast_data_wa-forecast_cv = g_forecast_data_wa-units.
*MOD-009

*MOD-006
    CLEAR:   l_calweek,
             l_calweek_end,
             l_calweekend_date,
             l_calweek_diff,
             l_fiscper,
             l_calmonth,
             l_calyear_start,
             l_calyear_end,
             l_start_week,
             l_first_day,
             l_end_week,
             l_last_day.
*MOD-006

*MOD-006
*Convert Monday Date to Calendar Week and Fiscal Period
*MOD-010
    PERFORM sub_format_dates USING g_forecast_data_wa-gi_date
                                  l_calweek
                                  l_calweek_end
                                  l_calweekend_date
                                  l_calweek_diff
                                  l_fiscper
                                  l_calmonth
                                  l_calyear_start
                                  l_calyear_end
                                  l_start_week
                                  l_first_day
                                  l_end_week
                                  l_last_day.

    g_forecast_data_wa-calweek          = l_calweek.
    g_forecast_data_wa-calweek_end      = l_calweek_end.
    g_forecast_data_wa-calmonth         = l_calmonth.
    g_forecast_data_wa-calyear_start    = l_calyear_start.
    g_forecast_data_wa-calyear_end      = l_calyear_end.
    g_forecast_data_wa-startweek        = l_start_week.
    g_forecast_data_wa-endweek          = l_end_week.
    g_forecast_data_wa-start_day        = l_first_day.
    g_forecast_data_wa-end_day          = l_last_day.
*MOD-010

* Modify table record
    MODIFY t_forecast_data1 FROM g_forecast_data_wa.
    CLEAR g_forecast_data_wa.
  ENDLOOP.

*MOD-010
  LOOP AT t_forecast_data1 INTO l_wa_forecast_data1.
    l_wa_forecast_data2-source_code =
         l_wa_forecast_data1-source_code.
    l_wa_forecast_data2-lang_group =
         l_wa_forecast_data1-lang_group.
    l_wa_forecast_data2-pres_code = l_wa_forecast_data1-pres_code.
    l_wa_forecast_data2-r3_material = l_wa_forecast_data1-r3_material.
    l_wa_forecast_data2-pres_name = l_wa_forecast_data1-pres_name.
    l_wa_forecast_data2-site    =  l_wa_forecast_data1-site.
    l_wa_forecast_data2-r3_loc  = l_wa_forecast_data1-r3_loc.
    l_wa_forecast_data2-indication = l_wa_forecast_data1-indication.
    l_wa_forecast_data2-date =  l_wa_forecast_data1-date.
    l_wa_forecast_data2-gi_date = l_wa_forecast_data1-gi_date.
    l_wa_forecast_data2-ord_number = l_wa_forecast_data1-ord_number.
    l_wa_forecast_data2-ord_line =  l_wa_forecast_data1-ord_line.
    l_wa_forecast_data2-units = l_wa_forecast_data1-units.
    l_wa_forecast_data2-forecast_cv = l_wa_forecast_data1-forecast_cv.
    l_wa_forecast_data2-calweek = l_wa_forecast_data1-calweek.
    l_wa_forecast_data2-calweek_end =  l_wa_forecast_data1-calweek_end.
    l_wa_forecast_data2-startweek =  l_wa_forecast_data1-startweek.
    l_wa_forecast_data2-endweek   = l_wa_forecast_data1-endweek.
    l_wa_forecast_data2-start_day = l_wa_forecast_data1-start_day.
    l_wa_forecast_data2-end_day   = l_wa_forecast_data1-end_day.
    l_wa_forecast_data2-calyear_start =
                    l_wa_forecast_data1-calyear_start.
    l_wa_forecast_data2-calyear_end  = l_wa_forecast_data1-calyear_end.
    l_wa_forecast_data2-delete = l_wa_forecast_data1-delete.
    APPEND l_wa_forecast_data2 TO t_forecast_data2.
    CLEAR: l_wa_forecast_data1.
    CLEAR: t_forecast_data2.
  ENDLOOP.
*MOD-010

  SORT t_forecast_data2 BY
        source_code
        lang_group
        pres_code
        r3_material
        pres_name
        site
        r3_loc
        indication
        calweek
        endweek
        calmonth
        calyear_start
        calyear_end
        calweek_end
        startweek
        start_day
        end_day
        delete.

*****************************************************************
**MOD-010
*****************************************************************
  CLEAR: lv_forecast_cv.
* collecting the product Variant.
*MOD-007
  lt_forecast_data2[] = t_forecast_data2[].
* Aggregation for JOR and NOR.
  DELETE lt_forecast_data2 WHERE indication <> 'JOR'
                             AND indication <> 'NOR'.
  IF NOT lt_forecast_data2[] IS INITIAL.
    LOOP AT lt_forecast_data2 ASSIGNING <l_wa_forecast>.
      lv_forecast_cv = lv_forecast_cv + <l_wa_forecast>-forecast_cv.
      <l_wa_forecast>-delete = 'X'.
      AT END OF calweek.
        <l_wa_forecast>-delete = ' '.
        <l_wa_forecast>-forecast_cv = lv_forecast_cv.
        CLEAR lv_forecast_cv.
      ENDAT.
    ENDLOOP.
  ENDIF.
  DELETE lt_forecast_data2 WHERE delete = 'X'.

* Aggregation where Indication ne JOR and NOR.
  lt_forecast_data3[] = t_forecast_data2[].
  LOOP AT lt_forecast_data3.
    IF lt_forecast_data3-indication = 'JOR'
    OR lt_forecast_data3-indication = 'NOR'.
      DELETE lt_forecast_data3.
    ELSE.
*MOD-009
*if lt_forecast_data3-calweek > lt_forecast_data3-calweek_end.
*MOD-009
***MOD-011
*      if lt_forecast_data3-calweek > lt_forecast_data3-endweek.
*        delete lt_forecast_data3.
*      else.
*        append lt_forecast_data3 to lt_forecast_data4.
*      endif.
***MOD-011
      APPEND lt_forecast_data3 TO lt_forecast_data4.
**MOD-011
*MOD-010
*      endif.
*MOD-010
    ENDIF.
  ENDLOOP.

* Aggregation beyond the horizon for all indication.
  IF NOT lt_forecast_data3[] IS INITIAL.
    LOOP AT lt_forecast_data3 ASSIGNING <l_wa_forecast>.
      lv_forecast_cv = lv_forecast_cv + <l_wa_forecast>-forecast_cv.
      <l_wa_forecast>-delete = 'X'.
      AT END OF calweek.
        <l_wa_forecast>-delete = ' '.
        <l_wa_forecast>-forecast_cv = lv_forecast_cv.
        CLEAR lv_forecast_cv.
      ENDAT.
    ENDLOOP.
  ENDIF.
  DELETE lt_forecast_data3 WHERE delete = 'X'.
  APPEND LINES OF lt_forecast_data3 TO lt_forecast_data2.
*MOD-007

*MOD-007
  REFRESH: t_forecast_data2.
  APPEND LINES OF lt_forecast_data2 TO t_forecast_data2.
  SORT t_forecast_data2 BY
        source_code
        lang_group
        pres_code
        r3_material
        pres_name
        site
        r3_loc
        indication
        calweek
        endweek
        calweek_end
        startweek
        start_day
        end_day
        calyear_start
        calyear_end
        delete.

  lt_forecast_data2[] = t_forecast_data2[].
  READ TABLE lt_forecast_data2 INDEX 1.
  l_start_year = lt_forecast_data2-calyear_start.
  DESCRIBE TABLE lt_forecast_data2 LINES l_tabix.
  READ TABLE lt_forecast_data2 INDEX l_tabix.
  l_end_year = lt_forecast_data2-calyear_end.
  LOOP AT lt_forecast_data2.
    IF sy-tabix = 1.
      l_calyear_data_start = ( lt_forecast_data2-calyear_start + 2 ).
    ENDIF.
    EXIT.
  ENDLOOP.
*MOD-009
  DELETE lt_forecast_data2 WHERE
          calyear_start > l_calyear_data_start.           "#EC PORTABLE
*MOD-009
  DELETE lt_forecast_data2 WHERE gi_date < sy-datum.
*MOD-009
  REFRESH: t_forecast_data2.
  APPEND LINES OF lt_forecast_data2 TO t_forecast_data2.
*MOD-009
*****************************************************************
**MOD-010
*****************************************************************

ENDFORM.                    " sub_map_data
**&---------------------------------------------------------------------
**
**&      Form  sub_process_data
**&---------------------------------------------------------------------
**
**       Loop through data records, separate into product/location
**       sub-groups, and process
**----------------------------------------------------------------------
**
FORM sub_process_data.

  DATA: l_temp_wa LIKE g_forecast_data_wa2,
        l_hold_wa LIKE g_forecast_data_wa2.

  CLEAR l_hold_wa.
**MOD-010
  SORT t_forecast_data2 BY r3_material
                           r3_loc
                           date
                           calweek ASCENDING.
**MOD-010

*** Loop through internal table, collecting sub-table for each
*** material/plant
  LOOP AT t_forecast_data2 INTO g_forecast_data_wa2.
    l_temp_wa = g_forecast_data_wa2.
* Initial Loop Pass -
*   Save hold variables during first loop pass
    AT FIRST.
      l_hold_wa = l_temp_wa.
    ENDAT.

* Normal Loop Processing -
*   If control break occurs:
*                       (1) process existing sub-table for
*                                material plant combination;
*                       (2) clear sub-table;
*                       (3) set new hold values;
*                       (4) append current work area to sub-table.
*
*   Otherwise, append current work area to sub-table
    IF ( l_hold_wa-r3_material <> g_forecast_data_wa2-r3_material ) OR
       ( l_hold_wa-r3_loc <> g_forecast_data_wa2-r3_loc ) OR
       ( l_hold_wa-calweek <> g_forecast_data_wa2-calweek ).

* Process sub-table; clear sub-table
* Set new hold values; append sub-table.
      l_hold_wa = l_temp_wa.
      APPEND g_forecast_data_wa2 TO t_forecast_sub.
      CLEAR g_forecast_data_wa2.
*MOD-009
      PERFORM sub_call_bapi.
      REFRESH t_forecast_sub.
*MOD-009
    ELSE.
* Append sub-table
      APPEND g_forecast_data_wa2 TO t_forecast_sub.
      CLEAR g_forecast_data_wa2.
    ENDIF.
* Final Loop Pass -
*   Process existing sub-table
    AT LAST.
      PERFORM sub_call_bapi.
    ENDAT.
  ENDLOOP.

ENDFORM.                    " sub_process_data
**&---------------------------------------------------------------------
**
*&      Form  sub_read_file
*&---------------------------------------------------------------------*
*       Read interface file
*----------------------------------------------------------------------*
FORM sub_read_file.

* Get Physical Filename
  IF p_phyin IS INITIAL.
    PERFORM sub_get_physical_file USING p_login p_phyin.
  ENDIF.

* Open a file object based on the selection screen information.
  g_file = p_login.

  CREATE OBJECT i_file
    EXPORTING
      u_f_filename = g_file
      u_f_filetype = c_log_type.

* Check authority for reading the interface file.
  CALL METHOD i_file->check_read_authority.

* Attempt to open file
  CALL METHOD i_file->open_for_input.

* Read file into data record and append to internal table
  PERFORM sub_read_data.

* Close file.
  CALL METHOD i_file->close.

ENDFORM.                    " sub_read_file
*&---------------------------------------------------------------------*
*&      Form  sub_get_physical_file
*&---------------------------------------------------------------------*
*       Determine Physical filename from Logical Filename
*----------------------------------------------------------------------*
*      -->P_LFILE  Logical Filename
*      -->P_PFILE  Physical Filename
*----------------------------------------------------------------------*
FORM sub_get_physical_file USING    p_lfile
                                    p_pfile.

  CHECK NOT p_lfile IS INITIAL.

  CALL FUNCTION 'FILE_GET_NAME'
    EXPORTING
      client           = sy-mandt
      logical_filename = p_lfile
      operating_system = sy-opsys
    IMPORTING
      file_name        = p_pfile
    EXCEPTIONS
      file_not_found   = 1
      OTHERS           = 2.
  IF sy-subrc NE 0.
    p_pfile = '' .
    MESSAGE e002 WITH p_lfile.
  ENDIF.

ENDFORM.                    " sub_get_physical_file
*&---------------------------------------------------------------------*
*&      Form  sub_read_data
*&---------------------------------------------------------------------*
*       Read data from input file
*----------------------------------------------------------------------*
FORM sub_read_data.

  DATA: delimeter(2) TYPE c VALUE ';'.
  DATA: l_wa_input_data TYPE g_input_data1.

  DO.
    READ DATASET i_file->phys_filename INTO g_input_data.
    IF sy-subrc NE 0.
      IF g_cnt_input_recs = 0.
        MESSAGE s000 WITH text-004 i_file->phys_filename
                          text-005.
*        MESSAGE s000 WITH 'Input file' i_file->phys_filename
*                          'is empty'.
        g_flg_error = 'Y'.
        STOP.
      ENDIF.
      EXIT.

    ELSE.
      APPEND g_input_data.
      ADD 1 TO g_cnt_input_recs.
      CLEAR: g_input_data,
             g_forecast_data_wa.
    ENDIF.
  ENDDO.

*MOD-002
  LOOP AT  g_input_data.
    t_forecast_data-source_code = g_input_data-line+0(4).
    SEARCH  t_forecast_data-source_code  FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-source_code+sy-fdpos = space.
    ENDIF.

    t_forecast_data-lang_group = g_input_data-line+6(24).
    CONDENSE t_forecast_data-lang_group.
    SEARCH  t_forecast_data-lang_group FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-lang_group+sy-fdpos = space.
    ENDIF.

    t_forecast_data-pres_code = g_input_data-line+30(9).
    CONDENSE t_forecast_data-pres_code.
    SEARCH  t_forecast_data-pres_code FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-pres_code+sy-fdpos = space.
    ENDIF.

    t_forecast_data-pres_name = g_input_data-line+39(38).
    CONDENSE t_forecast_data-pres_name.
    SEARCH  t_forecast_data-pres_name FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-pres_name+sy-fdpos = space.
    ENDIF.

    t_forecast_data-site = p_dist.

    t_forecast_data-indication = g_input_data-line+77(6).
    CONDENSE t_forecast_data-indication.
    SEARCH t_forecast_data-indication FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-indication+sy-fdpos = space.
    ENDIF.

    t_forecast_data-date = g_input_data-line+83(11).
    CONDENSE t_forecast_data-date.
    SEARCH  t_forecast_data-date FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-date+sy-fdpos = space.
    ENDIF.

    t_forecast_data-gi_date = g_input_data-line+94(12).
    CONDENSE t_forecast_data-gi_date.
    SEARCH  t_forecast_data-gi_date FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-gi_date+sy-fdpos = space.
    ENDIF.

    t_forecast_data-ord_number = g_input_data-line+106(10).
    CONDENSE t_forecast_data-ord_number.
    SEARCH  t_forecast_data-ord_number FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-ord_number+sy-fdpos = space.
    ENDIF.

    t_forecast_data-ord_line = g_input_data-line+116(8).
    CONDENSE t_forecast_data-ord_line.
    SEARCH  t_forecast_data-ord_line FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-ord_line+sy-fdpos = space.
    ENDIF.

    t_forecast_data-units = g_input_data-line+124(28).
    CONDENSE t_forecast_data-units.
    SEARCH  t_forecast_data-units FOR ';'.
    IF sy-subrc EQ 0.
      t_forecast_data-units+sy-fdpos = space.
    ENDIF.

    APPEND t_forecast_data.
    CLEAR t_forecast_data.
    CLEAR: g_input_data,
           g_forecast_data_wa.

  ENDLOOP.
*MOD-002

ENDFORM.                    " sub_read_data
*&---------------------------------------------------------------------*
*&      Form  sub_call_bapi
*&---------------------------------------------------------------------*
*       Read values of sub-table and update the Customer Forecast
*       key figure in the correct time buckets
*----------------------------------------------------------------------*
FORM sub_call_bapi.

  DATA: l_date         TYPE sydatum,
        l_start_date   TYPE sydatum,
        l_end_date     TYPE sydatum,
        l_period_start TYPE bapi10030pbperiodstart,
        l_period_end   TYPE bapi10030pbperiodend,
        l_matnr        TYPE /sapapo/matnr,
        l_locno        TYPE /sapapo/locno,
        l_lines        TYPE i.

*MOD-011
  DATA:
  l_calweek LIKE scal-week,         " Calendar Week
  l_calweek_end LIKE scal-week,     " Calendar Week
  l_startcalweek LIKE scal-week,         " Calendar Week
  l_calweek1(2) TYPE c,             " Calendar Week
  l_calweek_end1(2) TYPE c,         " Calendar Week
  l_calweekend_date LIKE scal-week, " Calendar Week
  l_calmonth(6)  TYPE c,            " Calendar Month
  l_calyear_start(4) TYPE c,        " Calendar Year
  l_calyear_end(4) TYPE c,          " Calendar Year
  l_calweek_diff TYPE scal-week,    " Diff in week
  p_calweek LIKE scal-week,         " Calendar Week
  p_calweek_end LIKE scal-week,     " Calendar Week
  p_startcalweek LIKE scal-week,         " Calendar Week
  p_calweek1(2) TYPE c,             " Calendar Week
  p_calweek_end1(2) TYPE c,         " Calendar Week
  p_calweekend_date LIKE scal-week, " Calendar Week
  p_calmonth(6)  TYPE c,            " Calendar Month
  p_calyear_start(4) TYPE c,        " Calendar Year
  p_calyear_end(4) TYPE c,          " Calendar Year
  p_calweek_diff TYPE scal-week,    " Diff in week
  p_calweek_start TYPE scal-week,    " Diff in week
  p_start_week_day TYPE scdatum,    " Date
  l_record_date TYPE sy-datum,      " Date
  lv_year_start TYPE cyear,         " Year
  lv_week_start TYPE scal-week,     " Week
  lv_week_end   TYPE scal-week.     " Week
*MOD-011
  DATA: l_time_series_id TYPE /sapapo/mxrow.
*MOD-011
  DATA: t_periotab TYPE /sapapo/periotab OCCURS 0 WITH HEADER LINE.
  DATA: g_periotab_wa TYPE /sapapo/periotab.
*MOD-011

  REFRESH: t_selection,
           t_group_by,
           t_key_figure_selection,
           t_time_series,
           t_time_series_chg,
           t_time_series_item,
           t_return.

  READ TABLE t_forecast_sub INDEX 1 INTO g_forecast_sub_wa.

  l_matnr = g_forecast_sub_wa-r3_material.
  l_locno = g_forecast_sub_wa-r3_loc.

**********************************************************
** Fill parameters/tables for BAPI_PBSRVAPS_GETDETAIL **
**********************************************************

**MOD-006
*** Date Parameters
  g_date_from = g_forecast_sub_wa-date.
  g_date_to   = g_forecast_sub_wa-gi_date.
**MOD-006

*MOD-006
* Selection table
  g_selection_wa-characteristic_name = '9AVERSION'. " Version
  g_selection_wa-char_val_sign = 'I'.
  g_selection_wa-char_val_option = 'EQ'.
  g_selection_wa-char_val_low = c_version.              " Default '000'

  APPEND g_selection_wa TO t_selection.
  CLEAR g_selection_wa.

*** Begin of change for TPR0817
  g_selection_wa-characteristic_name = 'ZMATNR'.   " APO-DP Product
*** End of change for TPR0817

  g_selection_wa-char_val_sign = 'I'.
  g_selection_wa-char_val_option = 'EQ'.
  g_selection_wa-char_val_low = l_matnr.

  APPEND g_selection_wa TO t_selection.
  CLEAR g_selection_wa.

*MOD-006
  g_selection_wa-characteristic_name = 'ZDCENTER'.   " Location
  g_selection_wa-char_val_sign = 'I'.
  g_selection_wa-char_val_option = 'EQ'.
  g_selection_wa-char_val_low = p_dist.
  APPEND g_selection_wa TO t_selection.
  CLEAR g_selection_wa.
*MOD-006

* Group By Table
*** Begin of change for TPR0817
  g_group_by_wa-characteristic_name = 'ZMATNR'.
*** End of change for TPR0817
  APPEND g_group_by_wa TO t_group_by.
  CLEAR g_group_by_wa.

* Key Figure Table - Customer Forecast in Units
  g_key_figure_selection_wa-key_figure = 'Z_CUSFU'.
  APPEND g_key_figure_selection_wa TO t_key_figure_selection.
  CLEAR g_key_figure_selection_wa.

  REFRESH t_return.

*MOD-006
* Call FM to get time series number
  CALL FUNCTION 'BAPI_PBSRVAPS_GETDETAIL'
    EXPORTING
      planningbook         = c_planningbook
*     SELECTION_ID         =
      period_type          = c_period_type
      date_from            = g_date_from
      date_to              = g_date_to
    TABLES
      selection            = t_selection
      group_by             = t_group_by
      key_figure_selection = t_key_figure_selection
      time_series          = t_time_series
      return               = t_return.
*MOD-006

  IF NOT t_return[] IS INITIAL.
    LOOP AT t_forecast_sub INTO g_forecast_sub_wa.
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_pbook_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
    ENDLOOP.
    EXIT.
  ENDIF.

****************************************************************
** Fill parameters/tables for BAPI_PBSRVAPS_CHANGEKEYFIGVAL **
****************************************************************

* Time Series table (containing time series ID)
  READ TABLE t_time_series INDEX 1 INTO g_time_series_wa.
  MOVE-CORRESPONDING g_time_series_wa TO g_time_series_chg_wa.
  APPEND g_time_series_chg_wa TO t_time_series_chg.
  l_time_series_id = g_time_series_wa-time_series_id.

  CLEAR g_time_series_chg_wa.

*MOD-009
  t_forecast_sub1[] = t_forecast_sub[].
* Time Series Item table (contains all values to be loaded)
  LOOP AT t_forecast_sub INTO g_forecast_sub_wa.
* Time Series ID
    g_time_series_item_wa-time_series_id    = l_time_series_id.
* Check date validity
    CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
      EXPORTING
        date                      = g_forecast_sub_wa-gi_date
      EXCEPTIONS
        plausibility_check_failed = 1
        OTHERS                    = 2.

    IF sy-subrc <> 0.
*MOD-010
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_date_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
      CONTINUE.
    ENDIF.

* Get Date Range for given date
    CALL FUNCTION '/SAPAPO/MC_DATE_GET_FIRST_LAST'
      EXPORTING
        i_date      = g_forecast_sub_wa-gi_date
        i_perkz     = 'W'
*       I_FABKL     = ' '
*       I_PERIV     = ' '
      IMPORTING
        i_firstdate = l_start_date
        i_lastdate  = l_end_date.
**********
*MOD-011
**********
** Starting Timestamp
*    CONVERT DATE l_start_date INTO TIME STAMP l_period_start
*                 TIME ZONE 'UTC   '.
*    g_time_series_item_wa-period_begin      = l_period_start.
*
** Ending Timestamp
*    CONVERT DATE l_end_date TIME '235959' INTO TIME STAMP l_period_end
*                 TIME ZONE 'UTC   '.
*    g_time_series_item_wa-period_end        = l_period_end.
*MOD-011
*********
*MOD-011
*********

* Data for forecast run date.
    l_record_date = sy-datum.
    CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
      EXPORTING
        date         = l_record_date
      IMPORTING
        week         = l_calweek
      EXCEPTIONS
        date_invalid = 1
        OTHERS       = 2.
    IF sy-subrc <> 0.
      g_flg_error = 'X'.
      EXIT.
    ELSE.
      p_calweek(2)   = l_calweek+4(2).
      p_calweek+2(4) = l_calweek(4).
    ENDIF.

    lv_year_start = p_calweek+2(4).
    lv_week_start = p_calweek+0(2).
    lv_week_end   = lv_week_start + 1.
    CONCATENATE  lv_year_start lv_week_end+4(2) INTO p_calweek_start.

    CALL FUNCTION 'WEEK_GET_FIRST_DAY'
      EXPORTING
        week         = p_calweek_start
      IMPORTING
        date         = p_start_week_day
      EXCEPTIONS
        week_invalid = 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.

    CALL FUNCTION '/SAPAPO/SDP_TB_GET_PERIOTAB'
      EXPORTING
        i_tbid              = '104  WEEKS'
        i_direction         = '+'
        i_startdate         = p_start_week_day
        i_offset_in_days    = '0'
      TABLES
        e_periotab          = t_periotab
      EXCEPTIONS
        tb_not_exists       = 1
        no_tb_defined       = 2
        wrong_tb_definition = 3
        OTHERS              = 4.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
         WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

    LOOP AT t_periotab INTO g_periotab_wa.
** Starting Timestamp
      CONVERT DATE g_periotab_wa-ertag
          INTO TIME STAMP l_period_start
                   TIME ZONE 'UTC   '.
      g_time_series_item_wa-period_begin      = l_period_start.

* Ending Timestamp
      CONVERT DATE g_periotab_wa-bistag
          TIME '235959' INTO TIME STAMP l_period_end
                                   TIME ZONE 'UTC   '.
      g_time_series_item_wa-period_end        = l_period_end.

* Forecast Value
      g_time_series_item_wa-time_series_value = 0.
      g_time_series_item_wa-time_series_id    = l_time_series_id.
      APPEND g_time_series_item_wa TO t_time_series_item.
      CLEAR g_time_series_item_wa.
    ENDLOOP.
*MOD-011
  ENDLOOP.

  REFRESH: t_return,
           t_char_comb.

  SORT t_time_series_item  BY time_series_id
                              period_begin
                              period_end.
  DELETE ADJACENT DUPLICATES FROM t_time_series_item
           COMPARING
           time_series_id
           period_begin
           period_end.
*********
**MOD-011
*********
*Delete the first and the last record from the time series table.
  DATA: l_tabix TYPE sy-tabix.
  DESCRIBE TABLE t_time_series_item LINES l_tabix.
  IF l_tabix NE 0.
    DELETE t_time_series_item INDEX l_tabix.
    DELETE t_time_series_item INDEX 1.
  ENDIF.
*********
**MOD-011
*********
*MOD-011
  IF  g_material NE g_forecast_sub_wa-r3_material.
*MOD-011
* Refresh the Planning Book
    CALL FUNCTION 'BAPI_PBSRVAPS_CHANGEKEYFIGVAL'
      EXPORTING
        planningbook                = c_planningbook
        commit_control              = 'E'
      TABLES
        selection                   = t_selection
        time_series                 = t_time_series_chg
        time_series_item            = t_time_series_item
        characteristics_combination = t_char_comb
        return                      = t_return.

* If error in return table, store message in error table and attempt
* rollback; otherwise commit
    IF NOT t_return[] IS INITIAL.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    ENDIF.
*MOD-011
    g_material = g_forecast_sub_wa-r3_material.
  ENDIF.
*MOD-011
*MOD-009
  REFRESH: t_time_series_item.

**************
***MOD-011
**************
* Time Series Item table (contains all values to be loaded)
  LOOP AT t_forecast_sub1 INTO g_forecast_sub_wa.

* Time Series ID
    g_time_series_item_wa-time_series_id    = l_time_series_id.

* Check date validity
    CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
      EXPORTING
        date                      = g_forecast_sub_wa-gi_date
      EXCEPTIONS
        plausibility_check_failed = 1
        OTHERS                    = 2.

    IF sy-subrc <> 0.
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_date_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub1.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
      CONTINUE.
    ENDIF.

* Get Date Range for given date
    CALL FUNCTION '/SAPAPO/MC_DATE_GET_FIRST_LAST'
      EXPORTING
        i_date      = g_forecast_sub_wa-gi_date
        i_perkz     = 'W'
*       I_FABKL     = ' '
*       I_PERIV     = ' '
      IMPORTING
        i_firstdate = l_start_date
        i_lastdate  = l_end_date.

* Starting Timestamp
    CONVERT DATE l_start_date INTO TIME STAMP l_period_start
                 TIME ZONE 'UTC   '.
    g_time_series_item_wa-period_begin      = l_period_start.

* Ending Timestamp
    CONVERT DATE l_end_date TIME '235959' INTO TIME STAMP l_period_end
                 TIME ZONE 'UTC   '.
    g_time_series_item_wa-period_end        = l_period_end.

* Forecast Value
    g_time_series_item_wa-time_series_value =
                                       g_forecast_sub_wa-forecast_cv.

    APPEND g_time_series_item_wa TO t_time_series_item.
    CLEAR g_time_series_item_wa.
  ENDLOOP.

* Call FM BAPI_PBSRVAPS_CHANGEKEYFIGVAL to change Z_CUSFU for all
* time buckets

  REFRESH: t_return,
           t_char_comb.

*MOD-006
  SORT t_time_series_item  BY time_series_id
                              period_begin
                              period_end.
  DELETE ADJACENT DUPLICATES FROM t_time_series_item
           COMPARING
           time_series_id
           period_begin
           period_end.

*MOD-006
  CALL FUNCTION 'BAPI_PBSRVAPS_CHANGEKEYFIGVAL'
    EXPORTING
      planningbook                = c_planningbook
      commit_control              = 'E'
    TABLES
      selection                   = t_selection
      time_series                 = t_time_series_chg
      time_series_item            = t_time_series_item
      characteristics_combination = t_char_comb
      return                      = t_return.

* If error in return table, store message in error table and attempt
* rollback; otherwise commit
  IF NOT t_return[] IS INITIAL.

    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
*     IMPORTING
*       RETURN        =
              .

    LOOP AT t_forecast_sub1 INTO g_forecast_sub_wa.
*MOD-010
      MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_change_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      DELETE t_forecast_sub1.
      CLEAR g_forecast_sub_wa.
      ADD 1 TO g_cnt_error_recs.
      CONTINUE.
    ENDLOOP.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' .
    DESCRIBE TABLE t_forecast_sub1 LINES l_lines.
    g_cnt_changes = g_cnt_changes + l_lines.
  ENDIF.
************
*MOD-011
************
ENDFORM.                    " sub_call_bapi
*&---------------------------------------------------------------------*
*&      Form  sub_format_dates
*&---------------------------------------------------------------------*
FORM sub_format_dates USING    p_date
                               p_calweek
                               p_calweek_end
                               p_weekend_date
                               p_week_diff
                               p_fiscper
                               p_calmonth
                               p_calyear_start
                               p_calyear_end
                               p_start_week
                               p_start_week_day
                               p_end_week
                               p_end_week_day.

* Local variables
  DATA: l_curr_date    TYPE sydatum,  " Program Run Date
        l_record_date  TYPE sydatum,  " Formatted Record Date
        l_curr_year(2) TYPE c,        " Program Year (without century)
        l_curr_year_num TYPE i,       " Integer value of Program Year
        l_century(2)     TYPE c,      " Program Century (without year)
        l_century_num    TYPE i,      " Integer value of Century
        l_input_year_num TYPE i.      " Integer value of Record Year
  DATA: con_period_week TYPE /sapapo/perkz VALUE 'W'.

* Record date - format MM/DD/YY
  DATA: BEGIN OF l_input_date,
          month(2)  TYPE c,
          filler1(1) TYPE c,
          day(2)     TYPE c,
          filler2(1) TYPE c,
          year(2)    TYPE c,
        END OF l_input_date.

  DATA: l_calweek TYPE kweek,
        l_fiscper TYPE /sapapo/perid.

*MOD-006
  DATA: l_calmonth(6) TYPE c,
        l_calyear(6) TYPE c,
        l_month(2) TYPE c,
        l_year(4) TYPE c,
        l_perid TYPE /sapapo/perid.
  DATA: p_calweek_start LIKE scal-week.

  DATA: f_date TYPE d,
        l_fromdate  LIKE scal-date,
        l_todate    LIKE scal-date.
  DATA: lv_year_start(4) TYPE c,
        lv_week_start(2) TYPE c,
        lv_week_end(2) TYPE c.
  DATA: lv_calweek_start TYPE scal-week.
  DATA: lv_start_week_day TYPE scal-date.
  DATA: lv_end_week_day TYPE scal-date.
  DATA: lv_length TYPE i.

  CLEAR: l_perid.

**MOD-006
* Data for forecast run date.
  l_record_date = sy-datum.
* Use /SAPAPO/DATE_GET_WEEK, /SAPAPO/PERIOD_GET_FOR_DATE (P/JJ)... but
* must convert to DATS format first
  CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek(2)   = l_calweek+4(2).
    p_calweek+2(4) = l_calweek(4).
  ENDIF.

  lv_year_start = p_calweek+2(4).
  lv_week_start = p_calweek+0(2).
  lv_week_end   = lv_week_start + 1.
  CONCATENATE  lv_year_start lv_week_end INTO p_calweek_start.

  CALL FUNCTION 'WEEK_GET_FIRST_DAY'
    EXPORTING
      week         = p_calweek_start
    IMPORTING
      date         = p_start_week_day
    EXCEPTIONS
      week_invalid = 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.

  p_end_week_day = p_start_week_day +  p_horz.
  l_record_date  = p_end_week_day.
  CLEAR: l_calweek.

  CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek_end = l_calweek.
    p_calyear_end = l_calweek+2(4).
  ENDIF.

  CLEAR: p_end_week_day.
  CALL FUNCTION 'WEEK_GET_FIRST_DAY'
    EXPORTING
      week         = p_calweek_end
    IMPORTING
      date         = p_end_week_day
    EXCEPTIONS
      week_invalid = 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.
  CLEAR: lv_week_start, lv_week_end.

*MOD-006
  lv_week_start = p_calweek_start+4(2).
  lv_year_start = p_calweek_start+0(4).
  CONCATENATE lv_week_start lv_year_start INTO p_start_week.
  p_calyear_start = lv_year_start.

  lv_week_start = p_calweek_end+4(2).
  lv_year_start = p_calweek_end+0(4).
  CONCATENATE  lv_week_start lv_year_start INTO p_end_week.
*MOD-006

  p_week_diff = p_calweek_end+0(2) - p_calweek+0(2).
  CONDENSE p_week_diff.
  p_week_diff = ceil( p_week_diff ).

*MOD-006
*Data for availability date.
  l_record_date = p_date.
* Use /SAPAPO/DATE_GET_WEEK, /SAPAPO/PERIOD_GET_FOR_DATE (P/JJ)... but
* must convert to DATS format first
  CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek(2)   = l_calweek+4(2).
    p_calweek+2(4) = l_calweek(4).
  ENDIF.

  lv_year_start = p_calweek+2(4).
  lv_week_start = p_calweek+0(2).
  lv_week_end   = lv_week_start + 1.
  lv_length = strlen( lv_week_end ).
  IF lv_length < 2.
    CONCATENATE '0' lv_week_end INTO lv_week_end.
  ENDIF.

  CONSTANTS: c_52(2) TYPE c VALUE '52'.
  CONCATENATE  lv_year_start lv_week_end INTO lv_calweek_start.
  IF lv_calweek_start+4(2) >  c_52.
    lv_calweek_start+4(2) = c_52.
  ENDIF.

  CALL FUNCTION 'WEEK_GET_FIRST_DAY'
    EXPORTING
      week         = lv_calweek_start
    IMPORTING
      date         = lv_start_week_day
    EXCEPTIONS
      week_invalid = 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.

  lv_end_week_day = lv_start_week_day + p_horz.
  l_record_date = lv_end_week_day.
  CLEAR: l_calweek.

  CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
    EXPORTING
      date         = l_record_date
    IMPORTING
      week         = l_calweek
    EXCEPTIONS
      date_invalid = 1
      OTHERS       = 2.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calweek_end = l_calweek.
  ENDIF.

  lv_year_start = p_calweek_end+0(4).
  lv_week_start = p_calweek_end+4(2).
  CONCATENATE  lv_week_start lv_year_start INTO p_calweek_end.
  p_calyear_end = lv_year_start.

*MOD-008
  CLEAR: l_perid.
  l_record_date = p_date.
  CALL FUNCTION '/SAPAPO/PERIOD_GET_FOR_DATE'
    EXPORTING
      i_date      = l_record_date
      i_perkz     = 'M'
      i_periv     = 'K0'
    IMPORTING
      e_perid     = l_perid
    EXCEPTIONS
      wrong_perkz = 1
      wrong_date  = 2
      wrong_periv = 3
      OTHERS      = 4.
  IF sy-subrc <> 0.
    g_flg_error = 'X'.
    EXIT.
  ELSE.
    p_calmonth(2)   = l_perid+4(2).
    p_calmonth+2(4) = l_perid(4).
    p_calyear_start = l_perid+0(4).
  ENDIF.
*MOD-008

ENDFORM.                    " sub_format_dates
*&---------------------------------------------------------------------*
*&      Form  validate_locmap
*&---------------------------------------------------------------------*
FORM validate_locmap .

*MOD-011
** Convert SAP Location Code to APO Location
  IF NOT t_forecast_data[] IS INITIAL.
    READ TABLE t_forecast_data INTO g_forecast_data_wa INDEX 1.
    SELECT SINGLE locno FROM /sapapo/locmap
      INTO g_forecast_data_wa-r3_loc
     WHERE ext_locno = p_dist
       AND loctype   = c_loctype
       AND logqs     = c_logqs.
    IF sy-subrc <> 0.
      MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
      g_error_data_wa-error_type = c_r3_loc_error.
      APPEND g_error_data_wa TO t_error_data.
      CLEAR g_error_data_wa.
      CLEAR g_forecast_data_wa.
      ADD 1 TO g_cnt_error_recs.
    ENDIF.
  ENDIF.
*MOD-011
ENDFORM.                    " validate_locmap

Code listing for: ZRGT_RFC_DESTINATION:

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zrgt_rfc_destination.

* 2.0
*
DATA: time1 TYPE p,
      time2 TYPE p,
      runtime(30),
      debug,
      atra.
DATA: rc LIKE sy-subrc ,
      v_rc LIKE sy-subrc.
DATA: desti LIKE rfcdes-rfcdest.
DATA: fieldname LIKE sy-repid.
DATA: g_upper TYPE c. "Groß/Kleinschreibung
*
TABLES : eufunc.
DATA: BEGIN OF unterschied, " Unterschiede bei Testdur
        parameter(30),
        alt(200),
        neu(200),                      " läufen
      END OF unterschied.
*
DATA : d102n_exportkey LIKE functdir.
*
DATA : BEGIN OF exportkey,
         dataid LIKE eufunc-nummer,
         seqid  LIKE eufunc-seqid,
       END OF exportkey.
DATA : BEGIN OF exceptions OCCURS 10,
         name(30),
       END OF exceptions.
DATA : vexception(30).
DATA : oexception(30).
DATA : nr2(250) TYPE c.
DATA : tablen TYPE i.
DATA : ex TYPE REF TO cx_root.
DATA : descr_ref TYPE REF TO cl_abap_typedescr.
DATA : resultat(30) TYPE c.
DATA : rest(30) TYPE c.
*
DATA: %_idestsysttype TYPE char1.
DATA: %_icharact_field TYPE char1.
DATA: %_icharact_value TYPE char20.
DATA: %_orfcdest TYPE rfcdest.
DATA: %_vrfcdest TYPE rfcdest.
DATA: no_destination_found(1).
*
DATA: own_logical_system_not_found(1).
*
FORM existence_check CHANGING varia.
  varia = 1.
ENDFORM.                    "EXISTENCE_CHECK
*&---------------------------------------------------------------------*
*&      Form  FUNCTION_CALL
*&---------------------------------------------------------------------*
FORM function_call.
  CLEAR %_orfcdest.
*  TRANSAKTION = 'Repo', PROGRAM = REPORT
  IF atra = '1'.
    SET RUN TIME ANALYZER ON %_INTERNAL.
  ENDIF.
  IF desti IS INITIAL.
    IF debug = '1'.
      BREAK-POINT AT NEXT APPLICATION STATEMENT.
    ENDIF.
    debug = space.
    GET RUN TIME FIELD time1.
    CALL FUNCTION 'ZRGT_RFC_DESTINATION'
      EXPORTING
        destsysttype                 = %_idestsysttype
        charact_field                = %_icharact_field
        charact_value                = %_icharact_value
      IMPORTING
        rfcdest                      = %_orfcdest
      EXCEPTIONS
        no_destination_found         = 001
        own_logical_system_not_found = 002.
    IF atra = '1'.
      SET RUN TIME ANALYZER OFF %_INTERNAL.
    ENDIF.
  ELSE.
    GET RUN TIME FIELD time1.
*  TRANSAKTION = 'Repo', PROGRAM = REPORT
    IF atra = '1'.
      SET RUN TIME ANALYZER ON %_INTERNAL.
    ENDIF.
    CALL FUNCTION 'ZRGT_RFC_DESTINATION' DESTINATION desti
      EXPORTING
        destsysttype                 = %_idestsysttype
        charact_field                = %_icharact_field
        charact_value                = %_icharact_value
      IMPORTING
        rfcdest                      = %_orfcdest
      EXCEPTIONS
        no_destination_found         = 001
        own_logical_system_not_found = 002.
    IF atra = '1'.
      SET RUN TIME ANALYZER OFF %_INTERNAL.
    ENDIF.
  ENDIF.
  GET RUN TIME FIELD time2.
  rc = sy-subrc.
  CLEAR exceptions.
  READ TABLE exceptions INDEX rc.
  IF sy-subrc = 0.
    oexception = exceptions-name.
  ELSE.
    oexception = resultat.
  ENDIF.
  time1 = time2 - time1.
  WRITE time1 TO runtime.
  CONDENSE runtime.
ENDFORM.                    "FUNCTION_CALL
*&---------------------------------------------------------------------*
*&      Form  CLEAR_DATA
*&---------------------------------------------------------------------*
FORM clear_data.
  CLEAR %_idestsysttype.
  CLEAR %_icharact_field.
  CLEAR %_icharact_value.
  CLEAR %_vrfcdest.
  CLEAR %_orfcdest.
ENDFORM.                    "CLEAR_DATA
*&---------------------------------------------------------------------*
*&      Form  CLEAR_TABLE
*&---------------------------------------------------------------------*
FORM clear_table.
ENDFORM.                    "CLEAR_TABLE
*&---------------------------------------------------------------------*
*&      Form  DELETE_LINE
*&---------------------------------------------------------------------*
FORM delete_line USING key index.
  CASE key.
  ENDCASE.
ENDFORM.                    "DELETE_LINE
*&---------------------------------------------------------------------*
*&      Form  INIT_EXCEPTIONS
*&---------------------------------------------------------------------*
FORM init_exceptions USING name.
  CLEAR exceptions.
  MOVE name TO exceptions-name.
  APPEND exceptions.
ENDFORM.                    "INIT_EXCEPTIONS
*&---------------------------------------------------------------------*
*&      Form  PARAM_ACCESS_TYPES
*&---------------------------------------------------------------------*
FORM param_access_types USING key dest p_mode p_upper.
  DATA : intkey(31).

  p_upper = g_upper.
  intkey = dest.
  intkey+1 = key.
  CONDENSE intkey.

  CASE intkey.
    WHEN 'IDESTSYSTTYPE'.
      CALL FUNCTION 'RS_COMPLEX_OBJECT_EDIT'
        EXPORTING
          object_name = 'DESTSYSTTYPE'
          mode        = p_mode
          insert_tab  = 'X'
          upper_case  = p_upper
        CHANGING
          object      = %_idestsysttype.
    WHEN 'ICHARACT_FIELD'.
      CALL FUNCTION 'RS_COMPLEX_OBJECT_EDIT'
        EXPORTING
          object_name = 'CHARACT_FIELD'
          mode        = p_mode
          insert_tab  = 'X'
          upper_case  = p_upper
        CHANGING
          object      = %_icharact_field.
    WHEN 'ICHARACT_VALUE'.
      CALL FUNCTION 'RS_COMPLEX_OBJECT_EDIT'
        EXPORTING
          object_name = 'CHARACT_VALUE'
          mode        = p_mode
          insert_tab  = 'X'
          upper_case  = p_upper
        CHANGING
          object      = %_icharact_value.
    WHEN 'ORFCDEST'.
      CALL FUNCTION 'RS_COMPLEX_OBJECT_EDIT'
        EXPORTING
          object_name = 'RFCDEST'
          mode        = p_mode
          insert_tab  = 'X'
          upper_case  = p_upper
        CHANGING
          object      = %_orfcdest.
    WHEN 'VRFCDEST'.
      CALL FUNCTION 'RS_COMPLEX_OBJECT_EDIT'
        EXPORTING
          object_name = 'RFCDEST'
          mode        = p_mode
          insert_tab  = 'X'
          upper_case  = p_upper
        CHANGING
          object      = %_vrfcdest.
  ENDCASE.
ENDFORM.                    "PARAM_ACCESS_TYPES
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_SET
*&---------------------------------------------------------------------*
FORM parameter_set USING name direction value.
  DATA: l_name(31).
  CONCATENATE direction name INTO l_name.
  CASE l_name.
    WHEN 'IDESTSYSTTYPE'.
      PERFORM cb_callback_value_set(saplseuj)
       USING value
       CHANGING %_idestsysttype.
    WHEN 'ICHARACT_FIELD'.
      PERFORM cb_callback_value_set(saplseuj)
       USING value
       CHANGING %_icharact_field.
    WHEN 'ICHARACT_VALUE'.
      PERFORM cb_callback_value_set(saplseuj)
       USING value
       CHANGING %_icharact_value.
    WHEN 'ORFCDEST'.
      PERFORM cb_callback_value_set(saplseuj)
       USING value
       CHANGING %_orfcdest.
  ENDCASE.
ENDFORM.                    "PARAMETER_SET
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_GET
*&---------------------------------------------------------------------*
FORM parameter_get USING name direction
                   CHANGING   value
                            old_value.
  DATA: l_name(31).
  CONCATENATE direction name INTO l_name.
  CASE l_name.
    WHEN 'IDESTSYSTTYPE'.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_idestsysttype
       CHANGING value.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_idestsysttype
       CHANGING old_value.
    WHEN 'ICHARACT_FIELD'.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_icharact_field
       CHANGING value.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_icharact_field
       CHANGING old_value.
    WHEN 'ICHARACT_VALUE'.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_icharact_value
       CHANGING value.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_icharact_value
       CHANGING old_value.
    WHEN 'ORFCDEST'.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_orfcdest
       CHANGING value.
      PERFORM cb_callback_value_get(saplseuj)
       USING %_orfcdest
       CHANGING old_value.
  ENDCASE.
ENDFORM.                    "PARAMETER_GET
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_TABLE_SET
*&---------------------------------------------------------------------*
FORM parameter_table_set USING name value.
  CASE name.
  ENDCASE.
ENDFORM.                    "PARAMETER_TABLE_SET
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_TABLE_GET
*&---------------------------------------------------------------------*
FORM parameter_table_get USING name
                   CHANGING   value.
  CASE name.
  ENDCASE.
ENDFORM.                    "PARAMETER_TABLE_GET
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_DESCRIBE
*&---------------------------------------------------------------------*
FORM parameter_describe USING cbprog
                              name.
  CASE name.
    WHEN 'DESTSYSTTYPE'.
      PERFORM cb_callback_describe IN PROGRAM (cbprog)
       USING %_idestsysttype
             name.
    WHEN 'CHARACT_FIELD'.
      PERFORM cb_callback_describe IN PROGRAM (cbprog)
       USING %_icharact_field
             name.
    WHEN 'CHARACT_VALUE'.
      PERFORM cb_callback_describe IN PROGRAM (cbprog)
       USING %_icharact_value
             name.
    WHEN 'RFCDEST'.
      PERFORM cb_callback_describe IN PROGRAM (cbprog)
       USING %_orfcdest
             name.
  ENDCASE.
ENDFORM.                    "PARAMETER_DESCRIBE
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_CONTAINER
*&---------------------------------------------------------------------*
FORM parameter_container TABLES lcont STRUCTURE swcont
                         USING name
                               direction .
  CASE name.
    WHEN 'IDESTSYSTTYPE'.
      IF direction = 'O'.
        CALL FUNCTION 'SWC_ELEMENT_SET'
          EXPORTING
            element   = name
            field     = %_idestsysttype
          TABLES
            container = lcont.
      ELSE.
        CALL FUNCTION 'SWC_ELEMENT_GET'
          EXPORTING
            element   = name
          IMPORTING
            field     = %_idestsysttype
          TABLES
            container = lcont.
      ENDIF.
    WHEN 'ICHARACT_FIELD'.
      IF direction = 'O'.
        CALL FUNCTION 'SWC_ELEMENT_SET'
          EXPORTING
            element   = name
            field     = %_icharact_field
          TABLES
            container = lcont.
      ELSE.
        CALL FUNCTION 'SWC_ELEMENT_GET'
          EXPORTING
            element   = name
          IMPORTING
            field     = %_icharact_field
          TABLES
            container = lcont.
      ENDIF.
    WHEN 'ICHARACT_VALUE'.
      IF direction = 'O'.
        CALL FUNCTION 'SWC_ELEMENT_SET'
          EXPORTING
            element   = name
            field     = %_icharact_value
          TABLES
            container = lcont.
      ELSE.
        CALL FUNCTION 'SWC_ELEMENT_GET'
          EXPORTING
            element   = name
          IMPORTING
            field     = %_icharact_value
          TABLES
            container = lcont.
      ENDIF.
    WHEN 'ORFCDEST'.
      IF direction = 'O'.
        CALL FUNCTION 'SWC_ELEMENT_SET'
          EXPORTING
            element   = name
            field     = %_orfcdest
          TABLES
            container = lcont.
      ELSE.
        CALL FUNCTION 'SWC_ELEMENT_GET'
          EXPORTING
            element   = name
          IMPORTING
            field     = %_orfcdest
          TABLES
            container = lcont.
      ENDIF.
  ENDCASE.
ENDFORM.                    "PARAMETER_CONTAINER
*&---------------------------------------------------------------------*
*&      Form  TABLES_CONTAINER
*&---------------------------------------------------------------------*
FORM tables_container TABLES lcont STRUCTURE swcont
                      USING name
                            direction.
  CASE name.
  ENDCASE.
ENDFORM.                    "TABLES_CONTAINER
*&---------------------------------------------------------------------*
*&      Form  SET_VARIABLES
*&---------------------------------------------------------------------*
FORM set_variables USING key value.
  CASE key.
    WHEN '%%DEBUGON'.
      debug = 1.
    WHEN '%%DEBUGOFF'.
      debug = 0.
    WHEN '%%DESTI'.
      desti = value.
    WHEN '%%ATRAON'.
      atra = 1.
    WHEN '%%ATRAOFF'.
      atra = 0.
    WHEN '%%UPPER'.
      g_upper = value.
  ENDCASE.
ENDFORM.                    "SET_VARIABLES
*&---------------------------------------------------------------------*
*&      Form  GET_VARIABLES
*&---------------------------------------------------------------------*
FORM get_variables USING key
                   CHANGING value.
  CASE key.
    WHEN '%%DEBUG'.
      value = debug.
    WHEN '%%DESTI'.
      value = desti.
    WHEN '%%ATRA'.
      value = atra.
    WHEN '%%SUBRC'.
      value = rc.
    WHEN 'V%%SUBRC'.
      value = v_rc.
    WHEN '%%TIME'.
      value = runtime.
    WHEN '%%UPPER'.
      value = g_upper.
    WHEN '%%EXCCL'.
      value = resultat.
  ENDCASE.
ENDFORM.                    "GET_VARIABLES
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_READ_INPUT
*&---------------------------------------------------------------------*
FORM parameter_read_input USING name
                                key
                       CHANGING value.
  CASE name.
    WHEN 'DESTSYSTTYPE'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT %_idestsysttype
               FROM DATABASE eufunc(fl) ID key.
      ENDCATCH.
      value = %_idestsysttype.
    WHEN 'CHARACT_FIELD'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT %_icharact_field
               FROM DATABASE eufunc(fl) ID key.
      ENDCATCH.
      value = %_icharact_field.
    WHEN 'CHARACT_VALUE'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT %_icharact_value
               FROM DATABASE eufunc(fl) ID key.
      ENDCATCH.
      value = %_icharact_value.
  ENDCASE.
ENDFORM.                    "PARAMETER_READ_INPUT
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_READ_OUTPUT
*&---------------------------------------------------------------------*
FORM parameter_read_output USING name
                                 key
                        CHANGING value.
  CASE name.
    WHEN 'DESTSYSTTYPE'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_type = 4
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT %_idestsysttype
               FROM DATABASE eufunc(fl) ID key.
      ENDCATCH.
    WHEN 'CHARACT_FIELD'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_type = 4
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT %_icharact_field
               FROM DATABASE eufunc(fl) ID key.
      ENDCATCH.
    WHEN 'CHARACT_VALUE'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_type = 4
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT %_icharact_value
               FROM DATABASE eufunc(fl) ID key.
      ENDCATCH.
    WHEN 'RFCDEST'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_type = 4
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT %_vrfcdest
               FROM DATABASE eufunc(fl) ID key.
      ENDCATCH.
  ENDCASE.
ENDFORM.                    "PARAMETER_READ_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  PARAMETER_COMPARE
*&---------------------------------------------------------------------*
FORM parameter_compare USING name
                       CHANGING value.
  CASE name.
    WHEN 'RFCDEST'.
      IF %_orfcdest <> %_vrfcdest.
        value = 'X'.
      ELSE.
        CLEAR value.
      ENDIF.
  ENDCASE.
ENDFORM.                    "PARAMETER_COMPARE
*&---------------------------------------------------------------------*
*&      Form  EXPORT_IMPORT_MEMORY
*&---------------------------------------------------------------------*
FORM export_import_memory USING sel key STRUCTURE exportkey.
  CASE sel.
    WHEN 'II'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT
      %_idestsysttype
      %_icharact_field
      %_icharact_value
      g_upper
      FROM MEMORY ID key.
      ENDCATCH.
    WHEN 'IO'.
      v_rc = rc.
      %_vrfcdest = %_orfcdest.
      vexception = oexception.
      EXPORT
    %_idestsysttype
    %_icharact_field
    %_icharact_value
    %_vrfcdest
    time1
    v_rc
    vexception
    g_upper
    TO MEMORY ID key.
    WHEN 'VI'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT
      %_vrfcdest
      time1
      v_rc
      vexception
      g_upper
      FROM MEMORY ID key.
      ENDCATCH.
    WHEN 'VO'.
      v_rc = rc.
      %_vrfcdest = %_orfcdest.
      vexception = oexception.
      EXPORT
    %_vrfcdest
    time1
    v_rc
    vexception
    g_upper
    TO MEMORY ID key.
    WHEN 'IV'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT
      %_vrfcdest
      time1
      v_rc
      vexception
      g_upper
      FROM MEMORY ID key.
      ENDCATCH.
      rc = v_rc.
      %_orfcdest = %_vrfcdest.
  ENDCASE.
ENDFORM.                    "EXPORT_IMPORT_MEMORY
*&---------------------------------------------------------------------*
*&      Form  EXPORT_IMPORT
*&---------------------------------------------------------------------*
FORM export_import USING sel key STRUCTURE d102n_exportkey.
  CASE sel.
    WHEN 'II'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT
      %_idestsysttype
      %_icharact_field
      %_icharact_value
      g_upper
      FROM DATABASE eufunc(fl) ID key ACCEPTING PADDING.
      ENDCATCH.
    WHEN 'IO'.
      v_rc = rc.
      %_vrfcdest = %_orfcdest.
      vexception = oexception.
      EXPORT
    %_idestsysttype
    %_icharact_field
    %_icharact_value
    %_vrfcdest
    time1
    v_rc
    vexception
    g_upper
    TO DATABASE eufunc(fl) ID key.
    WHEN 'VI'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT
      %_vrfcdest
      time1
      v_rc
      vexception
      g_upper
      FROM DATABASE eufunc(fl) ID key ACCEPTING PADDING.
      ENDCATCH.
    WHEN 'VO'.
      v_rc = rc.
      %_vrfcdest = %_orfcdest.
      vexception = oexception.
      EXPORT
    %_vrfcdest
    time1
    v_rc
    vexception
    g_upper
    TO DATABASE eufunc(fl) ID key.
    WHEN 'IV'.
      CATCH SYSTEM-EXCEPTIONS conne_import_wrong_comp_type = 1
                              conne_import_wrong_object_type = 2
                              conne_import_wrong_comp_leng = 3
                              conne_import_wrong_field_leng = 6
                              conne_import_wrong_structure  = 7
                              conne_import_wrong_field_type = 4
                              import_wrong_end_pos = 8
                              import_alignment_mismatch = 5.
        IMPORT
      %_vrfcdest
      time1
      v_rc
      vexception
      g_upper
      FROM DATABASE eufunc(fl) ID key ACCEPTING PADDING.
      ENDCATCH.
      rc = v_rc.
      %_orfcdest = %_vrfcdest.
  ENDCASE.
ENDFORM.                    "EXPORT_IMPORT
*&---------------------------------------------------------------------*
*&      Form  GET_EXCEPTION_DIFFERENCE
*&---------------------------------------------------------------------*
FORM get_exception_difference
 TABLES differences STRUCTURE unterschied.
  IF vexception NE oexception.
    MOVE 'EXCEPTION' TO differences-parameter.
    IF vexception = space.
      MOVE '-' TO differences-alt.
    ELSE.
      MOVE vexception TO differences-alt.
    ENDIF.
    IF oexception = space.
      MOVE '-' TO differences-neu.
    ELSE.
      MOVE oexception TO differences-neu.
    ENDIF.
    APPEND differences.
  ENDIF.
ENDFORM.                    "GET_EXCEPTION_DIFFERENCE
*&---------------------------------------------------------------------*
*&      Form  COMPARE_DATA
*&---------------------------------------------------------------------*
FORM compare_data
                  TABLES differences STRUCTURE unterschied
                  USING field
                  CHANGING retcode.
  DATA : subrc LIKE sy-subrc.
  FIELD-SYMBOLS: <l_f1> TYPE x.
  FIELD-SYMBOLS: <l_f2> TYPE x.
  retcode = '='.
  CLEAR differences.
  CASE field.
    WHEN 'EXCEPTION'.
      IF vexception NE oexception.
        retcode = space.
        MOVE field TO differences-parameter.
        MOVE vexception TO differences-alt.
        MOVE oexception TO differences-neu.
        APPEND differences.
      ENDIF.
  ENDCASE.
ENDFORM.                    "COMPARE_DATA
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zdev_chng_mot_in_trlane.

*---------------------------------------------------------------------*
*     Start of Data Declaration
*---------------------------------------------------------------------*

DATA: logical_system TYPE bapigenfields-logsystem.

DATA: transport_lane TYPE TABLE OF bapi11201trlane,
      wa_transport_lane  TYPE bapi11201trlane,

      transport_lanex    TYPE TABLE OF bapi11201trlanex,
      wa_transport_lanex TYPE bapi11201trlanex,

      means_of_transport TYPE TABLE OF bapi11201meansoftransp,
      wa_means_of_transport TYPE bapi11201meansoftransp,

      means_of_transportx TYPE TABLE OF bapi11201meansoftranspx,
      wa_means_of_transportx TYPE bapi11201meansoftranspx,

      wa_return TYPE bapiret2,
      wa1 LIKE bapi11201meansoftransp.

DATA: mot LIKE bapi11201meansoftransportout OCCURS 0 WITH HEADER LINE,
      loc_from LIKE bapilocfromrange OCCURS 0 WITH HEADER LINE,
      loc_to LIKE bapiloctorange OCCURS 0 WITH HEADER LINE,
      wa LIKE bapi11201meansoftransportout,
      ilocfr LIKE /sapapo/loc-locid,
      ilocto LIKE /sapapo/loc-locid.

DATA: return LIKE wa_return OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF wa_read_file,
             location_from       TYPE char30,
             location_to         TYPE char30,
             loctype_loc_from      TYPE char30,
             loctype_loc_to    TYPE char30,
             valfr               TYPE char30,
             valto               TYPE char30,
             ttype               TYPE char30,
             valid_all_prds_flg  TYPE char30,
             aggt_tpl_flg        TYPE char30,
             dtlt_tpl_flg        TYPE char30,
             short_text_tm       TYPE char30,
             trcal               TYPE char30,
             ttypecost           TYPE char30,
             duration            TYPE /sapapo/trm-durat,
             durfx_flg           TYPE char30,
             distfx_flg          TYPE char30,
             trans_cost          TYPE char30,
             trans_cost_uom      TYPE char30,
             dur_rnd_value       TYPE char30,
             treq_cover_type     TYPE char30,
             resource            TYPE char30,
              discrete_flg        TYPE char30,
             tlb_profile         TYPE char30,
             t_valfr             TYPE tzonref-tstamps,
             t_valto             TYPE tzonref-tstamps,
       END OF wa_read_file.

DATA: itab LIKE wa_read_file OCCURS 0 WITH HEADER LINE,
      count TYPE i.

*---------------------------------------------------------------------*
*     End of Data Declaration
*---------------------------------------------------------------------*

SELECTION-SCREEN: BEGIN OF BLOCK tlane WITH FRAME TITLE text-001.
PARAMETER: p_model TYPE /sapapo/s_trmodio-model OBLIGATORY DEFAULT '000' .
PARAMETER: p_file TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN: END OF BLOCK tlane.

PARAMETERS: p_test AS CHECKBOX.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM f4_file.

*---------------------------------------------------------------------*
*      Start of Selection
*---------------------------------------------------------------------*

START-OF-SELECTION.
  PERFORM get_logsys.
  PERFORM load_file.
  IF p_test IS INITIAL.
    PERFORM upload_data.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  F4_FILE
*&---------------------------------------------------------------------*
FORM f4_file .

  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      program_name  = syst-repid
      dynpro_number = syst-dynnr
      mask          = ',*.xls.'
    CHANGING
      file_name     = p_file
    EXCEPTIONS
      mask_too_long = 1
      OTHERS        = 2.

ENDFORM. " F4_FILE
*&---------------------------------------------------------------------*
*&      Form  GET_LOGSYS
*&---------------------------------------------------------------------*
FORM get_logsys .

  SELECT SINGLE logsys
      INTO logical_system
      FROM t000
     WHERE mandt EQ sy-mandt.

  IF sy-subrc <> 0.
    MESSAGE i002(zmak).
    STOP.
  ENDIF.

ENDFORM. " GET_LOGSYS
*&---------------------------------------------------------------------*
*&      Form  LOAD_FILE
*&---------------------------------------------------------------------*
FORM load_file .

  CLEAR: itab. REFRESH: itab.

  DATA lv_file TYPE string.

  lv_file = p_file.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = lv_file
      filetype                = 'DAT'
      has_field_separator     = 'X'
    TABLES
      data_tab                = itab
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.

*---------------------------------------------------------------------*
*      Start of Validation
*---------------------------------------------------------------------*

  DATA : BEGIN OF lt_loc OCCURS 0,
                 locno TYPE char30,
                 loctype TYPE char30,
         END OF lt_loc.

  DATA : BEGIN OF lt_ttype OCCURS 0,
                ttype TYPE char30,
         END OF lt_ttype.

  DATA : BEGIN OF lt_err OCCURS 0,
                 location_to      TYPE /sapapo/loc-locno,
                 loctype_loc_to   TYPE /sapapo/loc-loctype,
                 location_from    TYPE /sapapo/loc-locno,
                 loctype_loc_from TYPE /sapapo/loc-loctype,
                 valfr            LIKE itab-valfr,
                 valto            LIKE itab-valto,
                 ttype            TYPE char30,
        END OF lt_err.

  SELECT locno loctype
    FROM /sapapo/loc
    INTO TABLE lt_loc.

  IF sy-subrc <> 0.
    MESSAGE i004(zmak).
    STOP.
  ENDIF.

  SELECT ttype
    FROM /sapapo/trm
    INTO TABLE lt_ttype.

  IF sy-subrc <> 0.
    MESSAGE i003(zmak).
    STOP.
  ENDIF.

  LOOP AT itab.

****Validation for Location_to and loctype

    CLEAR lt_loc.
    READ TABLE lt_loc WITH KEY locno   = itab-location_to
                               loctype = itab-loctype_loc_to.
    IF sy-subrc <> 0.
      MOVE itab-location_to    TO lt_err-location_to.
      MOVE itab-loctype_loc_to TO lt_err-loctype_loc_to.
    ENDIF.

****Validation for Location_from and loctype

    CLEAR lt_loc.
    READ TABLE lt_loc WITH KEY locno = itab-location_from
                               loctype = itab-loctype_loc_from.
    IF sy-subrc <> 0.
      MOVE itab-location_from    TO lt_err-location_from.
      MOVE itab-loctype_loc_from TO lt_err-loctype_loc_from.
    ENDIF.

****Validation for Date(VALFR)

    IF itab-valfr(1)    =  ' '  OR
       itab-valfr+2(1)  <> '.' OR
       itab-valfr+5(1)  <> '.' OR
       itab-valfr+10(1) <> ' '.
      MOVE itab-valfr TO lt_err-valfr.
    ENDIF.

****Validation for Date(VALTO)

    IF itab-valto(1)    =  ' '  OR
       itab-valto+2(1)  <> '.' OR
       itab-valto+5(1)  <> '.' OR
       itab-valto+10(1) <> ' '.
      MOVE itab-valto TO lt_err-valto.
    ENDIF.

****validation for transport lane(ttype)

    CLEAR lt_ttype.
    READ TABLE lt_ttype WITH KEY ttype = itab-ttype.
    IF sy-subrc <> 0.
      MOVE itab-ttype TO lt_err-ttype.
    ENDIF.
    IF lt_err IS NOT INITIAL.
      APPEND lt_err.
      CLEAR lt_err.
    ENDIF.

  ENDLOOP.

  IF lt_err[] IS NOT INITIAL.
    WRITE : / 'Format of the Tab Delimited File'.
    SKIP.
    WRITE :  / 'LOCATION FROM'   COLOR COL_HEADING,
               'LOCATION TO'     COLOR COL_HEADING,
               'LOC TYPE FROM'   COLOR COL_HEADING,
               'LOC TYPE TO'     COLOR COL_HEADING,
               'VALFR'           COLOR COL_HEADING,
               'VALTO'           COLOR COL_HEADING,
               'MEANS OF TRANSPORT'   COLOR COL_HEADING,
               'VALID ALL PRDS FLAG'  COLOR COL_HEADING,
               'AGGR PLNG FLG'   COLOR COL_HEADING,
               'DETLT PLNG FLG'  COLOR COL_HEADING,
               'DESCRIPTION'      COLOR COL_HEADING,
               'TRSP. CAL'       COLOR COL_HEADING,
               'TTYPECOST'       COLOR COL_HEADING,
               'DURATION'        COLOR COL_HEADING,
               'DURFX FLG'       COLOR COL_HEADING,
               'DISTFX FLG'      COLOR COL_HEADING,
               'TRANS COST'      COLOR COL_HEADING,
               'TRANS COST UOM'  COLOR COL_HEADING,
               'BCKT OFFST'      COLOR COL_HEADING,
               'PERIOD FACTOR'   COLOR COL_HEADING,
               'RESOURCE'        COLOR COL_HEADING,
               'DISCRETE FLG'    COLOR COL_HEADING,
               'TLB PROFILE'     COLOR COL_HEADING.
    SKIP.
    WRITE : / 'Error in Excel File'.
    SKIP.

    WRITE :/5 'LOCATION TO', 25 'LOCTYPE', 35 'LOCATION FROM',
           50 'LOCTYPE', 60 'VALFR', 72 'VALTO', 84 'TLANE'.
    LOOP AT lt_err.
      WRITE : /5 lt_err-location_to, 25 lt_err-loctype_loc_to,
              35 lt_err-location_from, 50 lt_err-loctype_loc_from,
              60 lt_err-valfr, 72 lt_err-valto, 84 lt_err-ttype.
    ENDLOOP.
    IF p_test IS INITIAL.
      p_test = 'X'.
    ENDIF.
  ENDIF.

  IF lt_err[] IS INITIAL.

    WRITE : / ' File Format Proper '.

  ENDIF.

ENDFORM. " LOAD_FILE
*&---------------------------------------------------------------------*
*&      Form  UPLOAD_DATA
*&---------------------------------------------------------------------*
FORM upload_data .

  DATA lv_tmp(8).
  DATA lv_tmp1 LIKE sy-datlo.

  LOOP AT itab.

    count = count + 1.

    CLEAR: wa_transport_lane,
     wa_transport_lanex,
     wa_means_of_transport,
     wa_means_of_transportx,
     transport_lane,
     transport_lanex,
     means_of_transport,
     means_of_transportx.

    REFRESH: transport_lane,
             transport_lanex,
             means_of_transport,
             means_of_transportx.

    CONDENSE : itab-valto, itab-valfr.
    CLEAR: lv_tmp, lv_tmp1.
    CONCATENATE itab-valfr+6(4) itab-valfr+3(2) itab-valfr+0(2) INTO
    lv_tmp.
    lv_tmp1 = lv_tmp.

    CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP'
      EXPORTING
        i_datlo     = lv_tmp1
        i_timlo     = '000000'
        i_tzone     = 'UTC'
      IMPORTING
        e_timestamp = itab-t_valfr.

    CLEAR : lv_tmp, lv_tmp1.
    CONCATENATE itab-valto+6(4) itab-valto+3(2) itab-valto+0(2) INTO
    lv_tmp.
    lv_tmp1 = lv_tmp.

    CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP'
      EXPORTING
        i_datlo     = lv_tmp1
        i_timlo     = '235959'
        i_tzone     = 'UTC'
      IMPORTING
        e_timestamp = itab-t_valto.

    loc_from-sign = 'I'.
    loc_from-option = 'EQ'.
    loc_from-low = itab-location_from.

    APPEND loc_from.

    loc_to-sign = 'I'.
    loc_to-option = 'EQ'.
    loc_to-low = itab-location_to.

    APPEND loc_to.

    CALL FUNCTION 'BAPI_TRLSRVAPS_GETLIST'
      EXPORTING
        logical_system     = logical_system
        model              = p_model
      TABLES
        location_from      = loc_from[]
        location_to        = loc_to[]
        means_of_transport = mot[]
        return             = return.

    READ TABLE mot INTO wa
          WITH KEY ttype = itab-ttype.

    IF sy-subrc = 0.
      SELECT SINGLE locid FROM /sapapo/loc INTO ilocfr
            WHERE locno = itab-location_from.

      DELETE FROM /sapapo/trm WHERE
      locfr = ilocfr AND
      locto = ilocto AND
      ttype = itab-ttype.

      COMMIT WORK.
    ENDIF.

    wa_transport_lane-location_from          = itab-location_from.
    wa_transport_lane-loctype_loc_from       = itab-loctype_loc_from.
    wa_transport_lane-location_to            = itab-location_to.
    wa_transport_lane-loctype_loc_to         = itab-loctype_loc_to.
    APPEND wa_transport_lane TO transport_lane .

    wa_transport_lanex-location_from         = itab-location_from.
    wa_transport_lanex-loctype_loc_from      = wa_transport_lane-loctype_loc_from.
    wa_transport_lanex-location_to           = itab-location_to.
    wa_transport_lanex-loctype_loc_to        = wa_transport_lane-loctype_loc_to.
    APPEND wa_transport_lanex TO transport_lanex .

    wa_means_of_transport-location_from      = itab-location_from.
    wa_means_of_transport-loctype_loc_from   = itab-loctype_loc_from.
    wa_means_of_transport-location_to        = itab-location_to.
    wa_means_of_transport-loctype_loc_to     = itab-loctype_loc_to.
    wa_means_of_transport-valfr              = itab-t_valfr.
    wa_means_of_transport-valto              = itab-t_valto.
    wa_means_of_transport-ttype              = itab-ttype.
    wa_means_of_transport-valid_all_prds_flg  = itab-valid_all_prds_flg.

    wa_means_of_transport-aggt_tpl_flg       = itab-aggt_tpl_flg.
    wa_means_of_transport-dtlt_tpl_flg       = itab-dtlt_tpl_flg.
    wa_means_of_transport-short_text_tm      = itab-short_text_tm.
    wa_means_of_transport-trcal              = itab-trcal.
    wa_means_of_transport-ttypecost          = itab-ttypecost.
    wa_means_of_transport-duration           = itab-duration.
    wa_means_of_transport-durfx_flg          = itab-durfx_flg.
    wa_means_of_transport-distfx_flg         = itab-distfx_flg.
    wa_means_of_transport-trans_cost         = itab-trans_cost.
    wa_means_of_transport-trans_cost_uom     = itab-trans_cost_uom.
    wa_means_of_transport-dur_rnd_value      = itab-dur_rnd_value.
    wa_means_of_transport-treq_cover_type    = itab-treq_cover_type.
    wa_means_of_transport-resource           = itab-resource.
    wa_means_of_transport-discrete_flg       = itab-discrete_flg .
    wa_means_of_transport-tlb_profile        = itab-tlb_profile.
    APPEND wa_means_of_transport TO means_of_transport.

    wa_means_of_transportx-location_from     = itab-location_from.
    wa_means_of_transportx-loctype_loc_from  = itab-loctype_loc_from.
    wa_means_of_transportx-location_to       = itab-location_to.
    wa_means_of_transportx-loctype_loc_to    = itab-loctype_loc_to.
    wa_means_of_transportx-valfr             = itab-t_valfr.
    wa_means_of_transportx-valto             = itab-t_valto.
    wa_means_of_transportx-ttype             = itab-ttype.
    wa_means_of_transportx-valid_all_prds_flg = 'X'.
    wa_means_of_transportx-aggt_tpl_flg      = 'X'.
    wa_means_of_transportx-dtlt_tpl_flg      = 'X'.
    wa_means_of_transportx-short_text_tm     = 'X'.
    wa_means_of_transportx-trcal             = 'X'.
    wa_means_of_transportx-ttypecost         = 'X'.
    wa_means_of_transportx-duration          = 'X'.
    wa_means_of_transportx-durfx_flag        = 'X'.
    wa_means_of_transportx-distfx_flg        = 'X'.
    wa_means_of_transportx-trans_cost        = 'X'.
    wa_means_of_transportx-trans_cost_uom    = 'X'.
    wa_means_of_transportx-dur_rnd_value     = 'X'.
    wa_means_of_transportx-treq_cover_type   = 'X'.
    wa_means_of_transportx-resource          = 'X'.
    wa_means_of_transportx-discrete_flg      = 'X'.
    wa_means_of_transportx-tlb_profile       = 'X'.
    APPEND wa_means_of_transportx TO means_of_transportx.

    CALL FUNCTION 'BAPI_TRLSRVAPS_SAVEMULTI'
      EXPORTING
        logical_system      = logical_system
        model               = p_model
        commit_control      = 'E'
      TABLES
        transport_lane      = transport_lane
        transport_lanex     = transport_lanex
        means_of_transport  = means_of_transport
        means_of_transportx = means_of_transportx
        return              = return.

    READ TABLE return WITH KEY type = 'E'.
    IF sy-subrc <> 0.
      IF count = 1.
        WRITE : / ' program completed successfully '.
        WRITE : / ' Following record are created/updated '.
      ENDIF.
      LOOP AT means_of_transport INTO wa1.
        WRITE : / 'LOC_FROM = ' , wa1-location_from ,
        'LOC_TO = ' , wa1-location_to , 'Means of Transport =' ,wa1-ttype.
      ENDLOOP.
    ENDIF.

    LOOP AT return INTO wa_return WHERE
                    type = 'E'.
      WRITE :/ wa_return-type, wa_return-message.
    ENDLOOP.

  ENDLOOP.

ENDFORM. " UPLOAD_DATA

*Text elements
*----------------------------------------------------------
* 001 Changing Parameters of Means Of Transport in TLane

*Selection texts
*----------------------------------------------------------
* P_FILE         File path and name
* P_MODEL D       .
* P_TEST         Test Mode

*Messages
*----------------------------------------------------------
*
* Message class: ZDEV
*002   Logical System does not Exits.
*003   Transportation Lanes not maintained in /SAPAPO/TRM
*004   Locations not maintained in /SAPAPO/LOC
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zpbook_viewer MESSAGE-ID zdev.
***********************************************************************
TYPE-POOLS: slis.
***********************************************************************
** Constants ********************************************************
***********************************************************************
CONSTANTS:
c_1 TYPE i VALUE 1,
c_2 TYPE i VALUE 2,
c_3 TYPE i VALUE 3,
c_4 TYPE i VALUE 4,
* Begin of MOD-011
c_np TYPE char2 VALUE 'NP',
c_cp TYPE char2 VALUE 'CP',
* End of MOD-011
*** Begin of MOD-008
c_pum TYPE /sapapo/meins VALUE 'PUM', "Mod-003
c_0 TYPE sysubrc VALUE '0',
c_intern TYPE /sapapo/iobjnm VALUE 'INTERN*',
c_cell TYPE char4 VALUE 'CELL',
c_tabnam TYPE slis_tabname VALUE 'I_OUTTAB',
c_x TYPE c VALUE 'X',
c_w TYPE c VALUE 'W',
c_tabcell TYPE char14 VALUE 'WA_OUTTAB-CELL',
c_matnr TYPE /sapapo/iobjnm VALUE '9AMATNR',
c_locno TYPE /sapapo/iobjnm VALUE '9ALOCNO',
c_i TYPE c VALUE 'I',
c_eq TYPE char2 VALUE 'EQ',
c_pbook TYPE dynfnam VALUE 'P_PBOOK' ,
c_prgnam TYPE syrepid VALUE '/NESGLB/EXPORT_PBOOK_DATA' ,
c_dview TYPE fieldname VALUE 'DVIEW',
c_version TYPE /sapapo/iobjnm VALUE '9AVERSION' ,
c_e TYPE c VALUE 'E',
c_a TYPE c VALUE 'A',
c_address TYPE char3 VALUE 'C:\',
c_sym TYPE char9 VALUE ',.,..' ,
c_mode TYPE c VALUE 'O',
c_s TYPE ddbool_d VALUE 'S',
c_dynfam TYPE dynfnam VALUE 'DVIEW',
c_flg TYPE /sapapo/flg_use_sel VALUE '#',
c_show_lng_text TYPE /sapapo/flag VALUE 'X',
c_codepage TYPE char3 VALUE 'IBM',
c_filetype TYPE fileformat VALUE 'DAT',
c_outtab2 TYPE char15 VALUE 'WA_OUTTAB2-CELL',
c_svalue(7) TYPE c VALUE 's_value' ,
c_svalue1 TYPE char8 VALUE 's_value1',
c_svalue2 TYPE char8 VALUE 's_value2',
c_svalue3 TYPE char8 VALUE 's_value3',
c_svalue4 TYPE char8 VALUE 's_value4',
c_svalue5 TYPE char8 VALUE 's_value5',
c_tabcell_op(19) TYPE c VALUE 'WA_FILE_OUTPUT-CELL',
c_month TYPE char2 VALUE ' M'. "MOD-008
*** END OF MOD-008
***********************************************************************
** Types declarations ***********************************************
***********************************************************************
TYPES:
cell(40) TYPE c,
BEGIN OF ty_s_sel,
  option(2) TYPE c,
  sign TYPE c,
  low(45) TYPE c,
  high(45) TYPE c,
END OF ty_s_sel,
BEGIN OF ty_s_linealv,
  cell001 TYPE cell,
  cell002 TYPE cell,
  cell003 TYPE cell,
  cell004 TYPE cell,
  cell005 TYPE cell,
  cell006 TYPE cell,
  cell007 TYPE cell,
  cell008 TYPE cell,
  cell009 TYPE cell,
  cell010 TYPE cell,
  cell011 TYPE cell,
  cell012 TYPE cell,
  cell013 TYPE cell,
  cell014 TYPE cell,
  cell015 TYPE cell,
  cell016 TYPE cell,
  cell017 TYPE cell,
  cell018 TYPE cell,
  cell019 TYPE cell,
  cell020 TYPE cell,
  cell021 TYPE cell,
  cell022 TYPE cell,
  cell023 TYPE cell,
  cell024 TYPE cell,
  cell025 TYPE cell,
  cell026 TYPE cell,
  cell027 TYPE cell,
  cell028 TYPE cell,
  cell029 TYPE cell,
  cell030 TYPE cell,
  cell031 TYPE cell,
  cell032 TYPE cell,
  cell033 TYPE cell,
  cell034 TYPE cell,
  cell035 TYPE cell,
  cell036 TYPE cell,
  cell037 TYPE cell,
  cell038 TYPE cell,
  cell039 TYPE cell,
  cell040 TYPE cell,
  cell041 TYPE cell,
  cell042 TYPE cell,
  cell043 TYPE cell,
  cell044 TYPE cell,
  cell045 TYPE cell,
  cell046 TYPE cell,
  cell047 TYPE cell,
  cell048 TYPE cell,
  cell049 TYPE cell,
  cell050 TYPE cell,
  cell051 TYPE cell,
  cell052 TYPE cell,
  cell053 TYPE cell,
  cell054 TYPE cell,
  cell055 TYPE cell,
  cell056 TYPE cell,
  cell057 TYPE cell,
  cell058 TYPE cell,
  cell059 TYPE cell,
  cell060 TYPE cell,
  cell061 TYPE cell,
  cell062 TYPE cell,
  cell063 TYPE cell,
  cell064 TYPE cell,
  cell065 TYPE cell,
  cell066 TYPE cell,
  cell067 TYPE cell,
  cell068 TYPE cell,
  cell069 TYPE cell,
  cell070 TYPE cell,
  cell071 TYPE cell,
  cell072 TYPE cell,
  cell073 TYPE cell,
  cell074 TYPE cell,
  cell075 TYPE cell,
  cell076 TYPE cell,
  cell077 TYPE cell,
  cell078 TYPE cell,
  cell079 TYPE cell,
  cell080 TYPE cell,
  cell081 TYPE cell,
  cell082 TYPE cell,
  cell083 TYPE cell,
  cell084 TYPE cell,
  cell085 TYPE cell,
  cell086 TYPE cell,
  cell087 TYPE cell,
  cell088 TYPE cell,
  cell089 TYPE cell,
  cell090 TYPE cell,
  cell091 TYPE cell,
  cell092 TYPE cell,
  cell093 TYPE cell,
  cell094 TYPE cell,
  cell095 TYPE cell,
  cell096 TYPE cell,
  cell097 TYPE cell,
  cell098 TYPE cell,
  cell099 TYPE cell,
  cell100 TYPE cell,
END OF ty_s_linealv,
ty_i_linealv TYPE STANDARD TABLE OF ty_s_linealv.
TYPES BEGIN OF ty_dynp.
        INCLUDE STRUCTURE dynpread.
TYPES END OF ty_dynp.
*** Begin of MOD-008
TYPES :
BEGIN OF ty_items,
  time_series_id TYPE /sapapo/mxrow,
  week(60) TYPE c,
  time_series_value TYPE /sapapo/mxval,
  period_begin TYPE sydatum,"bapi10030pbperiodstart,
END OF ty_items,
ty_sel TYPE STANDARD TABLE OF bapi10030pbselection,
* Begin of MOD-011
* ty_time_series TYPE STANDARD TABLE OF
* bapi10030pbtimeserieso,
* ty_timeseries_item TYPE STANDARD TABLE OF
* bapi10030pbtimeseriesitemo,
* ty_char_combi TYPE STANDARD TABLE OF bapi10030pbcharo,
* End of MOD-011
ty_group TYPE STANDARD TABLE OF bapi10030pbgroupby,
ty_sel_group TYPE STANDARD TABLE OF bapi10030pbgroupby," MOD-011
* /sapapo/ts_group_by_str,
ty_period TYPE STANDARD TABLE OF /sapapo/ts_period,
ty_keyf TYPE STANDARD TABLE OF /sapapo/ts_keyfs_ext,
ty_mat TYPE STANDARD TABLE OF /sapapo/v_mattxt,
ty_loc TYPE STANDARD TABLE OF /sapapo/v_loctxt,
* Begin of MOD-011
ty_selection TYPE STANDARD TABLE OF bapi10030pbselection.
* ty_selection TYPE STANDARD TABLE OF /sapapo/ts_iobj_selection.
* End of MOD-011
TYPES :
BEGIN OF ty_value,
  sign TYPE c,
  option TYPE char2,
  low(60) TYPE c,
  high(60) TYPE c,
END OF ty_value.
*** End of MOD-008
***********************************************************************
** Data declarations ************************************************
***********************************************************************
DATA:
***********************************************************************
** Working varaible and table ***************************************
***********************************************************************
v_param TYPE n,
v_date(15) TYPE c,
v_date_from TYPE /sapapo/cdps_eval_date_from ,
v_date_to TYPE /sapapo/cdps_eval_date_to,
v_linenb(6) TYPE n VALUE '000000',
v_colnb(3) TYPE n VALUE '001',
v_charnb(3) TYPE n VALUE '001',
v_colord(3) TYPE n VALUE '001',
v_maxcol(3) TYPE n VALUE '100',
v_fieldname(20) TYPE c,
*Begin of MOD-007
v_value_c TYPE char40,
*End of MOD-007
v_value_n(15) TYPE c,
v_charvalue(60) TYPE c,
v_pareaname TYPE /sapapo/ts_pareaname,
v_baseume TYPE /sapapo/unit,
v_charname TYPE /sapapo/iobjnm,
v_plobj TYPE bapi10030char_comb_id_32,
v_plobid TYPE /sapapo/ts_plobid,
v_matnr TYPE /sapapo/matnr,
v_matid TYPE /sapapo/matid,
v_iobjnm TYPE /sapapo/iobjnm,
v_char_objnm TYPE /sapapo/iobjnm ,
*** Begin of MOD-008
* Outbounb buffer for ALV display
i_outtab TYPE ty_i_linealv,
* Outbounb buffer for ALV display
i_outtab2 TYPE ty_i_linealv,
* Outbound column format for ALV display
i_fieldcat TYPE slis_t_fieldcat_alv,
wa_outtab TYPE ty_s_linealv,
wa_outtab2 TYPE ty_s_linealv,
wa_fieldcat TYPE slis_fieldcat_alv,
is_layout TYPE slis_layout_alv,
i_sort TYPE slis_t_sortinfo_alv,
wa_sort TYPE slis_sortinfo_alv,
*** End of MOD-008
***********************************************************************
** Required table for calling PBOOK BAPI ***************************
***********************************************************************
* Logical system to call the BAPI
v_own_logical TYPE bapigenfields-logsystem,
* Selection table
*** Begin of MOD-008
lt_selection TYPE TABLE OF /sapapo/ts_iobj_selection,
ls_sel_head TYPE /sapapo/ts_selko,
lv_error_occured TYPE bapiyesno VALUE space,
i_sel TYPE ty_sel,
i_selection TYPE ty_selection,
i_sel_head TYPE /sapapo/ts_selko,
i_error_occured TYPE bapiyesno VALUE space,
i_group TYPE ty_group,
i_sel_group TYPE ty_sel_group,
i_keyf TYPE ty_keyf,
* Begin of Mod-011
* i_time_series TYPE ty_time_series,
* i_timeseries_item TYPE ty_timeseries_item,
* i_char_combi TYPE ty_char_combi,
* End of Mod-011
i_items TYPE STANDARD TABLE OF ty_items,
i_items_temp TYPE STANDARD TABLE OF ty_items,
wa_sel TYPE bapi10030pbselection,
wa_group TYPE bapi10030pbgroupby,
wa_keyf TYPE /sapapo/ts_keyfs_ext,
* Begin of Mod-011
* wa_time_series TYPE bapi10030pbtimeserieso,
* wa_timeseries_item TYPE bapi10030pbtimeseriesitemo,
* wa_char_combi TYPE bapi10030pbcharo,
wa_time_series TYPE bapi10030pbtimeseries2o,
wa_timeseries_item TYPE bapi10030pbtimeseriesitem2o,
wa_char_combi TYPE bapi10030pbcharcomb2o,
* End of Mod-011
wa_items TYPE ty_items,
wa_mat TYPE /sapapo/v_mattxt,
wa_loc TYPE /sapapo/v_loctxt,
wa_value TYPE ty_value,
l_v_time_temp(15) TYPE c,
l_v_time TYPE sydatum,
l_v_day(2) TYPE c,
l_v_mon(2) TYPE c,
l_v_year(4) TYPE c,
l_week TYPE kweek,
l_v_week_num(2) TYPE c,
l_v_week(60) TYPE c,
l_v_mat TYPE /sapapo/matnr,
l_v_count TYPE i,
l_v_colnb(3) TYPE n ,
l_v_maxrec_count TYPE /sapapo/maxtz,
l_v_msg TYPE natxt,
l_v_num TYPE char20,
l_v_maxrec TYPE /sapapo/maxtz,
flg_e TYPE c,
l_v_total_rec TYPE i ,
l_v_sweek(8) TYPE n,
l_v_eweek(8) TYPE n ,
v_fieldname_op(22) TYPE c ,
v_cell TYPE slis_fieldname.
DATA: i_dynp TYPE STANDARD TABLE OF ty_dynp,
      wa_dynp TYPE ty_dynp.
*** END OF MOD-008
DATA: v_uom TYPE /sapapo/meins. "MOD-003
***********************************************************************
** Screen declarations **********************************************
***********************************************************************
* PLANNING BOOK section ***********************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
*** Begin of MOD-008
PARAMETERS: p_pbook TYPE /sapapo/pb_mview
OBLIGATORY VALUE CHECK.
PARAMETERS: p_view TYPE /sapapo/pb_dview
OBLIGATORY.
* Keyfigure list to extract
SELECT-OPTIONS: s_keyf FOR v_iobjnm NO INTERVALS.
* Characteristic list to extract
SELECT-OPTIONS: s_char FOR v_char_objnm
NO INTERVALS.
*** END OF MOD-008
* Display description parameter
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(31) text-001 FOR FIELD p_descr.
PARAMETERS : p_descr TYPE checkbox.
SELECTION-SCREEN END OF LINE.
PARAMETERS : p_file TYPE rsfilenm. "MOD-008
* Planning Version
PARAMETERS: p_versn TYPE /sapapo/vrsioid
OBLIGATORY
DEFAULT '000'.
* Date range
SELECT-OPTIONS: s_date FOR sy-datum
MODIF ID ein.
* UOM
PARAMETERS: p_uom TYPE t006-msehi.
SELECTION-SCREEN: SKIP.
* Selection profile id
PARAMETERS: p_selid TYPE /sapapo/ts_sel_descr.
* Flexible selection of on charactersitic 1
SELECTION-SCREEN: SKIP.
PARAMETERS: p_char1 TYPE rsiobjnm.
SELECT-OPTIONS: s_value1 FOR v_charvalue.
SELECTION-SCREEN: SKIP.
PARAMETERS: p_char2 TYPE rsiobjnm.
SELECT-OPTIONS: s_value2 FOR v_charvalue.
SELECTION-SCREEN: SKIP.
PARAMETERS: p_char3 TYPE rsiobjnm.
SELECT-OPTIONS: s_value3 FOR v_charvalue.
SELECTION-SCREEN: SKIP.
PARAMETERS: p_char4 TYPE rsiobjnm.
SELECT-OPTIONS: s_value4 FOR v_charvalue.
SELECTION-SCREEN: SKIP.
PARAMETERS: p_char5 TYPE rsiobjnm.
SELECT-OPTIONS: s_value5 FOR v_charvalue.
SELECTION-SCREEN END OF BLOCK b1.
***********************************************************************
** Pointer declarations *********************************************
***********************************************************************
FIELD-SYMBOLS:
<fs_fieldn> TYPE cell,
<fs_param> TYPE rsiobjnm. "MOD-008
***********************************************************************
** Initialization ***************************************************
***********************************************************************
INITIALIZATION.
** Default Product Location in Group By selection
  PERFORM fill_char. "MOD-008
***********************************************************************
** At selection screen on field**************************************
***********************************************************************
AT SELECTION-SCREEN ON p_view.
  PERFORM validate_view. "MOD-008

AT SELECTION-SCREEN ON s_keyf.
  PERFORM validate_keyf. "MOD-008

AT SELECTION-SCREEN ON s_char.
  SELECT bas_plobid UP TO 1 ROWS
    FROM /sapapo/tsareako
    INTO v_plobid
    WHERE pareaname = v_pareaname.
  ENDSELECT.
  IF sy-subrc = 0.
*Begin of MOD-006
    TYPES :
    BEGIN OF ty_wa_iobjnm,
      iobjnm TYPE /sapapo/iobjnm,
    END OF ty_wa_iobjnm,
    ty_t_iobjnm TYPE STANDARD TABLE OF ty_wa_iobjnm.
    DATA: l_i_chars TYPE STANDARD TABLE OF /sapapo/dm_int_chars,
          l_wa_chars TYPE /sapapo/dm_int_chars,
          l_i_iobjnm TYPE ty_t_iobjnm,
          l_wa_iobjnm TYPE ty_wa_iobjnm.

    REFRESH: l_i_chars, l_i_iobjnm.
    CALL FUNCTION '/SAPAPO/TS_PSTRU_SINGLE_GET'
      EXPORTING
        iv_plobid      = v_plobid
      IMPORTING
        et_chars       = l_i_chars
      EXCEPTIONS
        plobid_invalid = 1
        OTHERS         = 2.
    IF sy-subrc <> 0.
      CASE sy-subrc.
        WHEN c_1.
          MESSAGE e000 WITH text-047.
        WHEN c_2.
          MESSAGE e000 WITH text-027.
      ENDCASE.
    ENDIF.
    LOOP AT s_char.
      l_wa_iobjnm-iobjnm = s_char-low.
      APPEND l_wa_iobjnm TO l_i_iobjnm.
      CLEAR: l_wa_iobjnm.
    ENDLOOP.
    LOOP AT l_i_chars INTO l_wa_chars.
      DELETE l_i_iobjnm WHERE iobjnm EQ l_wa_chars-iobjnm.
      CLEAR: l_wa_chars.
    ENDLOOP.
* IF sy-subrc <> 0.
    IF NOT l_i_iobjnm[] IS INITIAL.
      MESSAGE e000 WITH text-021 . " MOD-008
    ENDIF.
  ENDIF.

AT SELECTION-SCREEN ON p_selid.
  PERFORM validate_selid. "MOD-008

AT SELECTION-SCREEN ON p_char1.
  PERFORM validate_char USING p_char1.

AT SELECTION-SCREEN ON p_char2.
  PERFORM validate_char USING p_char2.

AT SELECTION-SCREEN ON p_char3.
  PERFORM validate_char USING p_char3.

AT SELECTION-SCREEN ON p_char4.
  PERFORM validate_char USING p_char4.

AT SELECTION-SCREEN ON p_char5.
  PERFORM validate_char USING p_char5.
***********************************************************************
** At selection on value request*************************************
***********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_view.
  PERFORM f4_dview_for_parea USING c_pbook . "MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_versn.
  PERFORM f4_version_for_parea USING c_pbook CHANGING p_versn."MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_keyf-low.
  PERFORM f4_keyf_for_parea USING c_pbook CHANGING s_keyf-low."MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM f4_filename CHANGING p_file.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_selid.
  PERFORM f4_selid_for_parea USING c_pbook CHANGING p_selid."MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_char-low.
  PERFORM f4_char_for_parea USING c_pbook CHANGING s_char-low."MOD-008.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_char1.
  PERFORM f4_char_for_parea USING c_pbook CHANGING p_char1. "MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_char2.
  PERFORM f4_char_for_parea USING c_pbook CHANGING p_char2."MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_char3.
  PERFORM f4_char_for_parea USING c_pbook CHANGING p_char3."MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_char4.
  PERFORM f4_char_for_parea USING c_pbook CHANGING p_char4."MOD-008

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_char5.
  PERFORM f4_char_for_parea USING c_pbook CHANGING p_char5."MOD-008

AT SELECTION-SCREEN ON p_pbook.
  SELECT SINGLE pareaname
    FROM /sapapo/pbmvw
    INTO v_pareaname
    WHERE mview = p_pbook.
  IF sy-subrc NE 0.
    MESSAGE s000 WITH text-019 . " MoD-008
  ENDIF.

AT SELECTION-SCREEN ON s_date.
  IF s_date[] IS INITIAL.
* MESSAGE s000 WITH 'Please enter a date range'. " MOD-004
    MESSAGE s000 WITH text-018. " MOD-008
    LEAVE TO SCREEN 1000.
  ENDIF.

AT SELECTION-SCREEN.
  IF NOT (  p_selid IS INITIAL OR
            ( p_char1 IS INITIAL AND
              p_char2 IS INITIAL AND
              p_char3 IS INITIAL AND
              p_char4 IS INITIAL AND
              p_char5 IS INITIAL ) ).
* MESSAGE s000 WITH 'Either enter selid or flex selection'. " MOD-004
    MESSAGE s000 WITH text-024 . " MOD-004
    LEAVE TO SCREEN 1000.
  ENDIF.
***********************************************************************
** Start of selection ***********************************************
***********************************************************************
START-OF-SELECTION .
*** Begin of MOD-008
  CLEAR: flg_e.
  PERFORM read_logical_system.
  PERFORM get_data_for_selection .
  PERFORM read_planning_book_bapi .
  IF flg_e = 'X'.
    EXIT.
  ENDIF.
  PERFORM display_alvdata.
*** END of MOD-008
***********************************************************************
** Form declarations ************************************************
***********************************************************************
*&---------------------------------------------------------------------*
*& Form f4_char_for_parea
*&---------------------------------------------------------------------*
FORM f4_char_for_parea
    USING pi_screenpbk
    CHANGING v_char TYPE rsiobjnm.

  DATA: l_parea TYPE /sapapo/ts_pareaname. "Planning Area

  PERFORM get_parea_from_screen USING pi_screenpbk
                                CHANGING l_parea.
  IF NOT l_parea IS INITIAL.
    CALL FUNCTION '/SAPAPO/TS_PAREA_CHARS_F4'
      EXPORTING
        iv_pareaid     = l_parea
*       IT_CHARS       =
      IMPORTING
        ev_chars       = v_char
*       EV_CHARS_TXTSH =
      EXCEPTIONS
        no_chance      = 1
        OTHERS         = 2.
  ENDIF.
ENDFORM. " f4_char_for_parea
*&---------------------------------------------------------------------*
*& Form f4_keyf_for_parea
*&---------------------------------------------------------------------*
FORM f4_keyf_for_parea
    USING pi_screenpbk
    CHANGING p_s_keyf_low TYPE rsiobjnm.

  DATA: l_parea TYPE /sapapo/ts_pareaname. "Planning Area

  PERFORM get_parea_from_screen USING pi_screenpbk
                                CHANGING l_parea.
  IF NOT l_parea IS INITIAL.
    CALL FUNCTION '/SAPAPO/TS_PAREA_KEYFS_F4'
      EXPORTING
        iv_pareaid        = l_parea
        iv_show_shorttext = ' '
        iv_show_longtext  = 'X'
      IMPORTING
        ev_keyfigure      = p_s_keyf_low
      EXCEPTIONS
        no_chance         = 1
        OTHERS            = 2.
    IF sy-subrc <> 0.
      CASE sy-subrc.
        WHEN 1.
          MESSAGE e000.
        WHEN 2.
          MESSAGE e000.
      ENDCASE.
    ENDIF.
  ENDIF.
ENDFORM. " f4_keyf_for_parea
*&---------------------------------------------------------------------*
*& Form f4_selid_for_parea
*&---------------------------------------------------------------------*
FORM f4_selid_for_parea
    USING pi_screenpbk
    CHANGING p_p_selid.

  DATA: l_selid TYPE /sapapo/guid,
        l_selection_text TYPE /sapapo/ts_sel_descr.
  DATA: l_parea TYPE /sapapo/ts_pareaname. "Planning Area

  PERFORM get_parea_from_screen USING pi_screenpbk
                                CHANGING l_parea.
  IF NOT l_parea IS INITIAL.
    CALL FUNCTION '/SAPAPO/MCPSH_SELECTIONID_F4'
      EXPORTING
        iv_pareaid        = l_parea
        iv_mview          = p_pbook
        iv_flg_used_for   = c_flg
      IMPORTING
        ev_selid          = l_selid
        ev_selection_text = l_selection_text.
    IF NOT l_selid IS INITIAL AND
       NOT l_selection_text IS INITIAL.
      p_p_selid = l_selection_text.
    ENDIF.
  ENDIF.
ENDFORM. " f4_selid_for_parea
*&---------------------------------------------------------------------*
*& Form f4_version_for_parea
*&---------------------------------------------------------------------*
FORM f4_version_for_parea
    USING pi_screenpbk
    CHANGING p_p_versfr TYPE /sapapo/vrsioid.
  DATA: l_parea TYPE /sapapo/ts_pareaname. "Planning Area
  PERFORM get_parea_from_screen USING pi_screenpbk
                                CHANGING l_parea.
  IF NOT l_parea IS INITIAL.
    CALL FUNCTION '/SAPAPO/TS_PAREA_VERSIO_F4'
      EXPORTING
        iv_pareaid = l_parea
      IMPORTING
        ev_vrsioid = p_p_versfr.
  ENDIF.
ENDFORM. " f4_version_for_parea
*&---------------------------------------------------------------------*
*& Form f4_dview_for_parea
*&---------------------------------------------------------------------*
FORM f4_dview_for_parea USING pi_screenpbk.
  DATA: l_progname TYPE progname, "Program Name
        l_scr_no TYPE sychar04, "Screen name
        l_pbook TYPE /sapapo/pb_mview,
        l_dview_tab TYPE /sapapo/pb_dvw_tab,
        rec_dview_tab TYPE /sapapo/pb_dvw_str.
  TYPES :
  BEGIN OF ty_pb_dview,
    mview TYPE /sapapo/pb_mview,
    dview TYPE /sapapo/pb_dview,
  END OF ty_pb_dview.
  DATA: li_pb_dview TYPE STANDARD TABLE OF ty_pb_dview
                    WITH HEADER LINE.
  MOVE: sy-repid TO l_progname,
        '1000' TO l_scr_no.
  MOVE pi_screenpbk TO wa_dynp-fieldname.
  APPEND wa_dynp TO i_dynp.
* Read the value entered in the screen
  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname               = l_progname
      dynumb               = l_scr_no
    TABLES
      dynpfields           = i_dynp
    EXCEPTIONS
      invalid_abapworkarea = 1
      invalid_dynprofield  = 2
      invalid_dynproname   = 3
      invalid_dynpronummer = 4
      invalid_request      = 5
      no_fielddescription  = 6
      invalid_parameter    = 7
      undefind_error       = 8
      double_conversion    = 9
      stepl_not_found      = 10
      OTHERS               = 11.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
* Getting the contents of the screen field (Planning Book)
  CLEAR wa_dynp.
  READ TABLE i_dynp INTO wa_dynp INDEX 1.
* Validate whether user has input the message type or not
  IF wa_dynp-fieldvalue IS INITIAL.
    MESSAGE s000 WITH text-015.
    EXIT.
  ELSE.
    MOVE wa_dynp-fieldvalue TO l_pbook.
  ENDIF.
  DATA: l_program TYPE syrepid,
        l_screen TYPE sydynnr,
        l_dynfam TYPE dynfnam.
  CALL FUNCTION '/SAPAPO/MSDP_PB_LIST_DVIEW'
    EXPORTING
      iv_uname = sy-uname
      iv_mview = l_pbook
    IMPORTING
      et_pbdvw = l_dview_tab.
  LOOP AT l_dview_tab INTO rec_dview_tab.
    MOVE-CORRESPONDING rec_dview_tab TO li_pb_dview.
    APPEND li_pb_dview.
    CLEAR li_pb_dview.
  ENDLOOP.
  l_program = sy-repid.
  l_screen = sy-dynnr.
  l_dynfam = c_dynfam .
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = c_dview
      dynpprog        = l_program
      dynpnr          = l_screen
      dynprofield     = l_dynfam
      value_org       = c_s
    TABLES
      value_tab       = li_pb_dview
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM. " f4_dview_for_parea
*&---------------------------------------------------------------------*
*& Form f4_filename
*&---------------------------------------------------------------------*
FORM f4_filename CHANGING p_file.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_path         = c_address
      mask             = c_sym
      mode             = c_mode
      title            = text-025
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
  IF sy-subrc <> 0.
    CLEAR p_file.
  ENDIF.
ENDFORM. " f4_filename
*&---------------------------------------------------------------------*
*& Form get_parea_from_screen
*&---------------------------------------------------------------------*
FORM get_parea_from_screen  USING pi_screenpbk
                            CHANGING po_parea.
  DATA: l_progname TYPE progname, "Program Name
        l_scr_no TYPE sychar04. "Screen name
* Populating program and screen names
  MOVE: sy-repid TO l_progname,
        '1000' TO l_scr_no.
  MOVE pi_screenpbk TO wa_dynp-fieldname.
  APPEND wa_dynp TO i_dynp.
* Read the value entered in the screen
  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname               = l_progname
      dynumb               = l_scr_no
    TABLES
      dynpfields           = i_dynp
    EXCEPTIONS
      invalid_abapworkarea = 1
      invalid_dynprofield  = 2
      invalid_dynproname   = 3
      invalid_dynpronummer = 4
      invalid_request      = 5
      no_fielddescription  = 6
      invalid_parameter    = 7
      undefind_error       = 8
      double_conversion    = 9
      stepl_not_found      = 10
      OTHERS               = 11.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
* Getting the contents of the screen field (Planning Book)
  CLEAR wa_dynp.
  READ TABLE i_dynp INTO wa_dynp INDEX 1.
* Validate whether user has input the message type or not
  IF wa_dynp-fieldvalue IS INITIAL.
    MESSAGE s000 WITH text-015. "'Please Enter Planning Book First'.
    EXIT.
  ENDIF.
  SET LOCALE LANGUAGE sy-langu.
  TRANSLATE wa_dynp-fieldvalue TO UPPER CASE.
* Select Planning Area for Planning Book
  SELECT SINGLE pareaname
    FROM /sapapo/pbmvw
    INTO po_parea
    WHERE mview = wa_dynp-fieldvalue.
  IF sy-subrc <> 0.
    MESSAGE s000 WITH text-024.
    EXIT.
  ENDIF.
  REFRESH: i_dynp.
ENDFORM. " get_parea_from_screen
*&---------------------------------------------------------------------*
*& form get_selection
*&---------------------------------------------------------------------*
* inserts the version guid for the version into selection table
* <<copied from source code of BAPI_PBSRVAPS_GETDETAIL>>
*----------------------------------------------------------------------*
* --> it_selection
* --> it_group_by
* <-- et_selection
* <-- et_sel_group
* <-- et_return
* --> iv_selection_id
* <-- es_sel_head
* <-- ev_error_occured
*----------------------------------------------------------------------*
FORM get_selection
      USING
        it_selection TYPE ty_sel
        iv_planning_book TYPE /sapapo/pb_mview
        iv_selection_id TYPE /sapapo/ts_sel_descr
        es_sel_head TYPE /sapapo/ts_selko
        ev_error_occured TYPE bapiyesno
        it_sel_group TYPE ty_group
      CHANGING
        et_selection TYPE ty_selection
        et_sel_group TYPE ty_sel_group.
  FIELD-SYMBOLS:
  <fs_sel> TYPE bapi10030pbselection,
  <fs_grp> TYPE bapi10030pbgroupby,
  <fs_selo> TYPE /sapapo/ts_iobj_selection.
  DATA:
  lv_snp_flg TYPE bapiyesno,
  lv_selection_id TYPE /sapapo/selectionid,
  lv_vrsioid TYPE /sapapo/vrsioid,
  ls_pbmview TYPE /sapapo/pb_mvw_str,
* Begin of Mod-011
* wa_selection TYPE /sapapo/ts_iobj_selection,
* wa_sel_group TYPE /sapapo/ts_group_by_str,
  wa_selection TYPE bapi10030pbselection,
  wa_sel_group TYPE bapi10030pbgroupby,
* End of Mod-011
  lt_pb_chars TYPE TABLE OF /sapapo/pb_mlcha_str,
  lv_tabix LIKE sy-tabix,
  ls_charinfo LIKE rsdvcha.

* -> initialization
  ev_error_occured = space.
  REFRESH et_selection.
  REFRESH et_sel_group.
  CLEAR es_sel_head.
* -> read information about the planning area
  CALL FUNCTION '/SAPAPO/MSDP_PB_READ_MVIEW'
    EXPORTING
      iv_mview        = iv_planning_book
    IMPORTING
      et_pbmlcha      = lt_pb_chars
      es_pbmvw        = ls_pbmview
      ev_snp_flg      = lv_snp_flg
    EXCEPTIONS
      err_mview_read  = 1
      err_mview_valid = 2
      OTHERS          = 3.
  IF sy-subrc <> 0.
    EXIT.
  ELSE.
    es_sel_head-pareaid = ls_pbmview-pareaname.
    SORT lt_pb_chars BY iobjnm.
  ENDIF.
  IF NOT lv_snp_flg IS INITIAL.
    CLEAR lv_snp_flg.
  ENDIF.
  IF ev_error_occured = space.
    IF NOT iv_selection_id IS INITIAL.
* -> get id for given selection
      CALL FUNCTION 'CONVERSION_EXIT_SELID_INPUT'
        EXPORTING
          input  = iv_selection_id
        IMPORTING
          output = lv_selection_id.
      IF lv_selection_id = iv_selection_id.
        EXIT.
      ENDIF.
* -> get selection data for selection id
      CALL FUNCTION '/SAPAPO/MCPSH_SELECTION_GET'
        EXPORTING
          iv_selid     = lv_selection_id
        IMPORTING
          es_sel_head  = es_sel_head
          et_selection = et_selection[]
        EXCEPTIONS
          id_not_exist = 1
          OTHERS       = 2.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
    ELSE.
* -> detailed selection was given
      LOOP AT it_selection ASSIGNING <fs_sel>.
        lv_tabix = sy-tabix.
* -> check, that characteristic exists in planning book
        READ TABLE lt_pb_chars
            WITH KEY iobjnm = <fs_sel>-characteristic_name
            BINARY SEARCH
            TRANSPORTING NO FIELDS.
        IF sy-subrc <> 0.
          CONTINUE.
        ENDIF.
* -> check if there is a info object and if conv. is necessary
        CLEAR ls_charinfo.
        CALL FUNCTION 'RSD_IOBJ_GET'
          EXPORTING
            i_iobjnm         = <fs_sel>-characteristic_name
            i_objvers        = 'A'
          IMPORTING
            e_s_vcha         = ls_charinfo
          EXCEPTIONS
            iobj_not_found   = 1
            illegal_input    = 2
            bct_comp_invalid = 3
            OTHERS           = 4.
* -> only do conversion if a conversion procedure is given
        IF sy-subrc = 0 AND NOT ls_charinfo-convexit IS INITIAL.
* -> convert low char value into internal format
          IF NOT <fs_sel>-char_val_low IS INITIAL.
            CALL FUNCTION 'RRSV_IOBJ_VALUE_OUTPUT'
              EXPORTING
                i_iobjnm = <fs_sel>-characteristic_name
                i_iobjvl_ext = <fs_sel>-char_val_low
                i_format = 'E'
                i_check = space
                i_text = space
                i_sid_requested = space
              IMPORTING
*   Begin of Mod-011
*               e_iobjvl_int = wa_selection-low
                e_iobjvl_int = wa_selection-char_val_low
*   End of Mod-011
              EXCEPTIONS
                error_message = 1
                OTHERS = 2.
            IF sy-subrc <> 0.
              EXIT.
            ELSE.
* Begin of Mod-011
* <fs_sel>-char_val_low = wa_selection-low.
              <fs_sel>-char_val_low = wa_selection-char_val_low.
* End of Mod-011
            ENDIF.
          ENDIF.
* -> convert high char value into internal format
          IF NOT <fs_sel>-char_val_high IS INITIAL.
            CALL FUNCTION 'RRSV_IOBJ_VALUE_OUTPUT'
              EXPORTING
                i_iobjnm = <fs_sel>-characteristic_name
                i_iobjvl_ext = <fs_sel>-char_val_high
                i_format = 'E'
                i_check = space
                i_text = space
                i_sid_requested = space
              IMPORTING
*   Begin of Mod-011
*               e_iobjvl_int = wa_selection-high
                e_iobjvl_int = wa_selection-char_val_high
*   End of Mod-011
              EXCEPTIONS
                error_message = 1
                OTHERS = 2.
            IF sy-subrc <> 0.
              EXIT.
            ELSE.
* Begin of Mod-011
* <fs_sel>-char_val_high = wa_selection-high.
              <fs_sel>-char_val_high = wa_selection-char_val_high.
* End of Mod-011
            ENDIF.
          ENDIF.
        ENDIF.
        CLEAR wa_selection.
* Begin of Mod-011
* wa_selection-iobjnm = <fs_sel>-characteristic_name.
* wa_selection-sign = <fs_sel>-char_val_sign.
* wa_selection-option = <fs_sel>-char_val_option.
* wa_selection-low = <fs_sel>-char_val_low.
* wa_selection-high = <fs_sel>-char_val_high.
        wa_selection-characteristic_name = <fs_sel>-characteristic_name.
        wa_selection-char_val_sign = <fs_sel>-char_val_sign.
        wa_selection-char_val_option = <fs_sel>-char_val_option.
        wa_selection-char_val_low = <fs_sel>-char_val_low.
        wa_selection-char_val_high = <fs_sel>-char_val_high.
* End of Mod-011
        APPEND wa_selection TO et_selection.
      ENDLOOP.
      IF NOT lv_tabix IS INITIAL.
        CLEAR lv_tabix.
      ENDIF.
    ENDIF.
  ENDIF.
* -> get guid of the version, version is mandotory !
  IF ev_error_occured = space.
    ev_error_occured = 'X'.
    LOOP AT et_selection ASSIGNING <fs_selo>
* Begin of Mod-011
*       WHERE iobjnm = '9AVERSION'.
        WHERE characteristic_name = c_version ."'9AVERSION'.
* End of Mod-011
      ev_error_occured = space.
      SELECT SINGLE vrsioid INTO lv_vrsioid
        FROM /sapapo/apo01 WHERE vrsioex = <fs_selo>-low.
      IF sy-subrc = 0.
        <fs_selo>-low = lv_vrsioid.
      ELSE.
        EXIT.
      ENDIF.
    ENDLOOP.
  ENDIF.
  IF ev_error_occured = space.
    IF NOT it_sel_group[] IS INITIAL.
* -> change group_by criteria
      REFRESH et_sel_group.
      LOOP AT it_sel_group ASSIGNING <fs_grp>.
* -> check, that characteristic exists in planning book
        READ TABLE lt_pb_chars
            WITH KEY iobjnm = <fs_grp>-characteristic_name
            BINARY SEARCH
            TRANSPORTING NO FIELDS.
        IF sy-subrc <> 0.
          CONTINUE.
        ENDIF.
        CLEAR wa_sel_group.
* Begin of Mod-011
* wa_sel_group-iobjnm = <fs_grp>-characteristic_name.
        wa_sel_group-characteristic_name = <fs_grp>-characteristic_name.
* End of Mod-011
        APPEND wa_sel_group TO et_sel_group.
        IF NOT et_sel_group IS INITIAL.
          CLEAR wa_sel_group.
        ENDIF.
      ENDLOOP.
    ENDIF.
  ENDIF.
*** Begin of MOD-010.
* Begin of Mod-011
* loop at et_selection into wa_selection
* where sign = 'I'
* and option = 'NP'.
* wa_selection-sign = 'E'.
* wa_selection-option = 'CP'.
* modify et_selection from wa_selection transporting sign option.
  LOOP AT et_selection INTO wa_selection
      WHERE char_val_sign = c_i "'I'
        AND char_val_option = c_np. "'NP'.
    wa_selection-char_val_sign = c_e. "'E'.
    wa_selection-char_val_option = c_cp. "'CP'.
    MODIFY et_selection FROM wa_selection
        TRANSPORTING char_val_sign char_val_option.
* End of Mod-011
  ENDLOOP.
*** ENDOF MOD-010
ENDFORM. " selection_insert_versionid
*&---------------------------------------------------------------------*
*& Form validate_char
*&---------------------------------------------------------------------*
FORM validate_char USING p_char.
  CHECK NOT p_char IS INITIAL.
*** Begin of MOD-005
  DATA: l_i_chars TYPE STANDARD TABLE OF /sapapo/dm_int_chars,
        l_wa_chars TYPE /sapapo/dm_int_chars.
  CLEAR: l_wa_chars.
  REFRESH: l_i_chars.
  CALL FUNCTION '/SAPAPO/TS_PSTRU_SINGLE_GET'
    EXPORTING
      iv_plobid      = v_plobid
    IMPORTING
      et_chars       = l_i_chars
    EXCEPTIONS
      plobid_invalid = 1
      OTHERS         = 2.
  IF sy-subrc <> 0.
    CASE sy-subrc.
      WHEN c_1.
        MESSAGE e000 WITH text-047.
      WHEN c_2.
        MESSAGE e000 WITH text-027.
    ENDCASE.
  ENDIF.
  READ TABLE l_i_chars INTO l_wa_chars
      WITH KEY iobjnm = p_char.
*** END OF MOD-005
  IF sy-subrc <> 0.
    MESSAGE e000 WITH text-014.
  ENDIF.
ENDFORM. " validate_char
*&---------------------------------------------------------------------*
*& Form get_uom_conversion
*&---------------------------------------------------------------------*
FORM get_uom_conversion USING p_p_uom TYPE /sapapo/meins
                              p_matid TYPE /sapapo/matid
                        CHANGING p_v TYPE /sapapo/mxval.
  DATA:
* Working Variables ***********************************************
    l_convfact TYPE /sapapo/umrechf.

  CALL FUNCTION '/SAPAPO/DM_UNIT_CONVERSION'
    EXPORTING
      i_matid               = p_matid
      i_meins_from          = c_pum "MOD-008
      i_meins_to            = p_p_uom
    IMPORTING
      e_umref               = l_convfact
    EXCEPTIONS
      material_not_found    = 1
      conversion_error      = 2
      no_fraction_available = 3
      OTHERS                = 4.
*** Begin of MOD-002.
  IF sy-subrc NE 0.
    CASE sy-subrc.
      WHEN c_1.
        MESSAGE e000 WITH text-002.
      WHEN c_2.
        MESSAGE e000 WITH text-003.
      WHEN c_3.
        MESSAGE e000 WITH text-026.
      WHEN c_4.
        MESSAGE e000 WITH text-027.
    ENDCASE.
  ELSE.
*** End of MOD-002.
    p_v = p_v * l_convfact.
  ENDIF.
ENDFORM. " get_uom_conversion
*---------------------------------------------------------------------*
* Form fill_char
*---------------------------------------------------------------------*
FORM fill_char.
*------- Neill C-C commented 30.04.2007
* Default Product Location in Group By selection
* s_char-sign = c_i .
* s_char-option = c_eq .
* s_char-low = c_matnr .
* APPEND s_char.
*
* s_char-low = c_locno .
* APPEND s_char.
*------ Neill C-C commented 30.04.2007
ENDFORM. "fill_char
*---------------------------------------------------------------------*
* Form read_logical_system
*---------------------------------------------------------------------*
FORM read_logical_system.
* Get the logical system name
  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system             = v_own_logical
    EXCEPTIONS
      own_logical_system_not_defined = 1
      OTHERS                         = 2.
  IF sy-subrc NE 0.
    FORMAT COLOR COL_POSITIVE INTENSIFIED OFF INVERSE ON.
    WRITE / text-013.
    FORMAT COLOR OFF INTENSIFIED ON INVERSE OFF.
    EXIT .
  ENDIF .
ENDFORM. " read_logical_system
*&---------------------------------------------------------------------*
*& Form get_data_for_selection
*&---------------------------------------------------------------------*
FORM get_data_for_selection.
* Get the planning area unit of measure
  SELECT basme UP TO 1 ROWS
    FROM /sapapo/tsareako INTO v_baseume
    WHERE pareaname = v_pareaname.
  ENDSELECT.
  IF p_uom IS INITIAL.
    p_uom = v_baseume.
  ENDIF.
* Prepare i_sel with mandatory selection on version
  CLEAR i_sel.
  wa_sel-characteristic_name = '9AVERSION'.
  wa_sel-char_val_sign = c_i .
  wa_sel-char_val_option = c_eq.
  wa_sel-char_val_low = p_versn.
  APPEND wa_sel TO i_sel.
* Include the flexible selection form p_char1 to 5
  v_param = 1.
  DO 5 TIMES.
    CLEAR wa_sel.
    CONCATENATE 'p_char' v_param INTO v_fieldname.
    ASSIGN (v_fieldname) TO <fs_param>.
    IF NOT <fs_param> IS INITIAL.
      wa_sel-characteristic_name = <fs_param>.
      CONCATENATE c_svalue v_param INTO v_fieldname.
      CASE v_fieldname.
        WHEN c_svalue1.
          IF NOT s_value1[] IS INITIAL.
            LOOP AT s_value1 INTO wa_value.
              wa_sel-char_val_sign = wa_value-sign.
              wa_sel-char_val_option = wa_value-option.
              wa_sel-char_val_low = wa_value-low.
              wa_sel-char_val_high = wa_value-high.
              APPEND wa_sel TO i_sel.
            ENDLOOP.
          ENDIF.
        WHEN c_svalue2 .
          IF NOT s_value2[] IS INITIAL.
            LOOP AT s_value2 INTO wa_value.
              wa_sel-char_val_sign = wa_value-sign.
              wa_sel-char_val_option = wa_value-option.
              wa_sel-char_val_low = wa_value-low.
              wa_sel-char_val_high = wa_value-high.
              APPEND wa_sel TO i_sel.
            ENDLOOP.
          ENDIF.
        WHEN c_svalue3.
          IF NOT s_value3[] IS INITIAL.
            LOOP AT s_value3 INTO wa_value.
              wa_sel-char_val_sign = wa_value-sign.
              wa_sel-char_val_option = wa_value-option.
              wa_sel-char_val_low = wa_value-low.
              wa_sel-char_val_high = wa_value-high.
              APPEND wa_sel TO i_sel.
            ENDLOOP.
          ENDIF.
        WHEN c_svalue4.
          IF NOT s_value4[] IS INITIAL.
            LOOP AT s_value4 INTO wa_value.
              wa_sel-char_val_sign = wa_value-sign.
              wa_sel-char_val_option = wa_value-option.
              wa_sel-char_val_low = wa_value-low.
              wa_sel-char_val_high = wa_value-high.
              APPEND wa_sel TO i_sel.
            ENDLOOP.
          ENDIF.
        WHEN c_svalue5.
          IF NOT s_value5[] IS INITIAL.
            LOOP AT s_value5 INTO wa_value.
              wa_sel-char_val_sign = wa_value-sign.
              wa_sel-char_val_option = wa_value-option.
              wa_sel-char_val_low = wa_value-low.
              wa_sel-char_val_high = wa_value-high.
              APPEND wa_sel TO i_sel.
            ENDLOOP.
          ENDIF.
      ENDCASE.
    ENDIF.
    ADD 1 TO v_param.
  ENDDO.
* Prepare i_group_by
  LOOP AT s_char.
    wa_group-characteristic_name = s_char-low.
    APPEND wa_group TO i_group.
    CLEAR wa_group.
  ENDLOOP.
* Prepare i_key_figure
  LOOP AT s_keyf.
    CHECK s_keyf-low NE '*'.
    wa_keyf-iobjnm = s_keyf-low.
    APPEND wa_keyf TO i_keyf.
  ENDLOOP.
  SORT i_keyf. " MOD-012
* Prepare date conversion
  v_date_from = s_date-low.
  v_date_to = s_date-high.
  IF NOT v_date_to IS INITIAL.
    CLEAR v_date_to.
  ENDIF.
  IF NOT v_date_from IS INITIAL.
    CLEAR v_date_from.
  ENDIF.
  PERFORM get_selection
  USING i_sel
  p_pbook
  p_selid
  i_sel_head
  i_error_occured
  i_group
  CHANGING i_selection
  i_sel_group.
ENDFORM. " get_data_for_selection
*---------------------------------------------------------------------*
* Form read_planning_book_BAPI
*---------------------------------------------------------------------*
FORM read_planning_book_bapi.
  DATA: l_i_return TYPE STANDARD TABLE OF bapiret2. "MOD-010
* Begin of Mod-011
* CALL FUNCTION 'BAPI_PBSRVAPS_GETDETAIL'
* EXPORTING
* planningbook = p_pbook
* data_view = p_view
* LOGICAL_SYSTEM = v_own_logical
* TABLES
* selection = i_selection
* group_by = i_sel_group
* key_figure_selection = i_keyf[]
* time_series = i_time_series[]
* time_series_item = i_timeseries_item[]
* characteristics_combination = i_char_combi[]
* return = l_i_return. "MOD-010
  DATA: l_i_key_figure TYPE STANDARD TABLE OF bapi10030pbtimeseries2o,
        l_i_key_figure_value TYPE STANDARD TABLE OF bapi10030pbtimeseriesitem2o,
        l_i_char_comb TYPE STANDARD TABLE OF bapi10030pbcharcomb2o.
  IF NOT p_selid IS INITIAL.
* Bapi call with selection id
    CALL FUNCTION 'BAPI_PBSRVAPS_GETDETAIL2'
      EXPORTING
        planningbook                = p_pbook
        data_view                   = p_view
        selection_id                = p_selid
      TABLES
        group_by                    = i_sel_group
        key_figure_selection        = i_keyf[]
        key_figure                  = l_i_key_figure
        key_figure_value            = l_i_key_figure_value
        characteristics_combination = l_i_char_comb[]
        return                      = l_i_return[].
  ELSE.
* Bapi call with selection and group by
    CALL FUNCTION 'BAPI_PBSRVAPS_GETDETAIL2'
      EXPORTING
        planningbook                = p_pbook
        data_view                   = p_view
      TABLES
        selection                   = i_selection
        group_by                    = i_sel_group
        key_figure_selection        = i_keyf[]
        key_figure                  = l_i_key_figure
        key_figure_value            = l_i_key_figure_value
        characteristics_combination = l_i_char_comb[]
        return                      = l_i_return[].
  ENDIF.
* End of Mod-011
* if no data found then return
* Begin of Mod-011
* IF i_timeseries_item[] IS INITIAL.
  IF l_i_key_figure_value[] IS INITIAL.
* End of Mod-011
    FORMAT COLOR COL_POSITIVE INTENSIFIED OFF INVERSE ON.
    WRITE: / text-004.
    FORMAT COLOR COL_NEGATIVE INTENSIFIED OFF INVERSE ON.
    flg_e = 'X'.
    EXIT .
  ENDIF .
* Begin of Mod-011
* Delete useless returned characteristics
* DELETE i_char_combi WHERE characteristic_name CP c_intern.
  DELETE l_i_char_comb WHERE characteristic_name CP c_intern.
* Initialize table order and variable before looping
* SORT i_time_series BY time_series_id.
* SORT i_timeseries_item BY time_series_id.
* SORT i_char_combi BY char_comb_id.
  SORT l_i_key_figure BY key_figure_id.
  SORT l_i_key_figure_value BY key_figure_id.
  SORT l_i_char_comb BY char_comb_id.
* End of Mod-011
* Create the ALV header line based on Characteristic
  REFRESH i_outtab.
  v_colnb = 1.
  v_charnb = 1.
  DO.
    READ TABLE s_char INDEX v_charnb.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    ADD 1 TO v_charnb.
* Begin of Mod-011
* READ TABLE i_char_combi INTO wa_char_combi WITH KEY
*characteristic_name = s_char-low.
    CLEAR wa_fieldcat.
* wa_fieldcat-seltext_l = wa_char_combi-characteristic_type.
    READ TABLE l_i_char_comb INTO wa_char_combi
        WITH KEY characteristic_name = s_char-low.
    wa_fieldcat-seltext_l = wa_char_combi-characteristic_text.
* End of Mod-011
    CONCATENATE c_cell v_colnb INTO wa_fieldcat-fieldname.
    wa_fieldcat-tabname = c_tabnam. "'I_OUTTAB'.
    wa_fieldcat-key = c_x ."'X'.
    APPEND wa_fieldcat TO i_fieldcat.
    wa_sort-fieldname = wa_fieldcat-fieldname.
    wa_sort-tabname = c_tabnam ."'I_OUTTAB'.
    wa_sort-up = c_x . "'X'.
    wa_sort-spos = v_colnb.
    APPEND wa_sort TO i_sort.
    ADD 1 TO v_colnb.
* Description required
    IF p_descr = c_x . "'X'.
* Add extra column if Product or location are part of the selection
      CASE wa_char_combi-characteristic_name.
        WHEN c_matnr . "'9AMATNR'.
          CLEAR wa_fieldcat.
          wa_fieldcat-seltext_s = text-005. "'Prd Descr.'.
          wa_fieldcat-seltext_m = text-006. "'Product Descr.'.
          wa_fieldcat-seltext_l = text-007. "'Product Description'.
* CONCATENATE 'CELL' v_colnb INTO I_fieldcat-fieldname.--MOD-004
          CONCATENATE c_cell v_colnb INTO wa_fieldcat-fieldname." MOD-004
          wa_fieldcat-tabname = c_tabnam. "'I_OUTTAB'.
          wa_fieldcat-key = c_x. "'X'.
          APPEND wa_fieldcat TO i_fieldcat.
          ADD 1 TO v_colnb.
        WHEN c_locno . "'9ALOCNO'.
          CLEAR wa_fieldcat.
          wa_fieldcat-seltext_s = text-008. "'Loc. Descr.'.
          wa_fieldcat-seltext_m = text-009. "'Location Descr.'.
          wa_fieldcat-seltext_l = text-010. "'Location Description'.
          CONCATENATE c_cell v_colnb INTO wa_fieldcat-fieldname.
          wa_fieldcat-tabname = c_tabnam. "'I_OUTTAB'.
          wa_fieldcat-key = c_x. "'X'.
          APPEND wa_fieldcat TO i_fieldcat.
          ADD 1 TO v_colnb.
        WHEN OTHERS.
      ENDCASE.
    ENDIF.
  ENDDO.
* APPEND keyfigure to layout
  CLEAR wa_fieldcat.
  CONCATENATE c_cell v_colnb INTO wa_fieldcat-fieldname.
  wa_fieldcat-tabname = c_tabnam. "'I_OUTTAB'.
  wa_fieldcat-seltext_s = text-011. "'Key Figure'.
  wa_fieldcat-seltext_l = text-011. "'Key Figure'.
  wa_fieldcat-key = c_x. "'X'.
  APPEND wa_fieldcat TO i_fieldcat.
  ADD 1 TO v_colnb.
* APPEND ume to layout
  CLEAR wa_fieldcat.
  CONCATENATE 'CELL' v_colnb INTO wa_fieldcat-fieldname.
  wa_fieldcat-tabname = c_tabnam. "'I_OUTTAB'.
  wa_fieldcat-seltext_s = text-012. "'Unit'.
  wa_fieldcat-seltext_l = text-012. "'Unit'.
  wa_fieldcat-key = c_x. "'X'.
  wa_fieldcat-outputlen = 5.
  APPEND wa_fieldcat TO i_fieldcat.
  ADD 1 TO v_colnb.
* Append the period header
  DATA : l_v_date TYPE scdatum.
* Begin of Mod-011
* SORT i_timeseries_item BY period_begin.
* LOOP AT i_timeseries_item INTO wa_timeseries_item.
  SORT l_i_key_figure_value BY period_begin.
  LOOP AT l_i_key_figure_value INTO wa_timeseries_item.
* End of Mod-011
    l_v_time_temp = wa_timeseries_item-period_begin.
    l_v_time = l_v_time_temp+0(8).
    l_v_day = l_v_time+6(2).
    l_v_mon = l_v_time+4(2).
    l_v_year = l_v_time+0(4).
    CONCATENATE l_v_year l_v_mon l_v_day INTO l_v_date.
* Maximum number of column reached
    IF l_v_date >= s_date-low AND
    l_v_date <= s_date-high.
*** Begin of MOD-008
      DATA: l_v_number TYPE i,
      l_c_num TYPE c,
      l_v_time1 TYPE sydatum,
      l_v_time_temp1(15) TYPE c.
      l_v_time_temp1 = wa_timeseries_item-period_end.
      l_v_time1 = l_v_time_temp1+0(8).
      l_v_number = l_v_time1 - l_v_time.
*** If the view is tecnical periods.
      IF p_view CS 'TECH' OR
      p_view CS 'PERIOD'.
*** Calculating the week number
*** Begin of Mod-014(Comment)
* CALL FUNCTION '/GLB/RRP_DATE_GET_WEEK_CUST'
* EXPORTING
* iv_date = l_v_date
* IMPORTING
* ev_week = l_week
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
*** End of Mod-014(Comment)
*** Begin of Mod-014
        CALL FUNCTION 'DATE_GET_WEEK'
          EXPORTING
            date         = l_v_date
          IMPORTING
            week         = l_week
          EXCEPTIONS
            date_invalid = 1
            OTHERS       = 2.
*** End of Mod-014
        IF sy-subrc EQ 0.
          CLEAR : l_v_year,
          l_v_week_num,
          l_v_week.
          l_v_year = l_week+0(4).
          l_v_week_num = l_week+4(2).
          IF l_v_number < 6.
            l_v_number = l_v_number + 1.
            l_c_num = l_v_number.
            CONCATENATE c_w l_v_week_num '(' l_c_num ').'
            l_v_year INTO l_v_week.
          ELSE.
            CONCATENATE c_w l_v_week_num '.' l_v_year INTO l_v_week.
          ENDIF.
        ENDIF.
*** If the view other than tecnical periods.
      ELSE.
        IF l_v_number = 0.
          CONCATENATE l_v_day '.' l_v_mon '.' l_v_year
          INTO l_v_week.
        ELSEIF l_v_number LT 7.
*** End of MOD-008
** Calculating the week number
*** Begin of Mod-013(Comment)
* CALL FUNCTION '/GLB/RRP_DATE_GET_WEEK_CUST'
* EXPORTING
* iv_date = l_v_date
* IMPORTING
* ev_week = l_week
* EXCEPTIONS
* date_invalid = 1
* OTHERS = 2.
*** End of Mod-013(Comment)
*** Begin of Mod-013
          CALL FUNCTION 'DATE_GET_WEEK'
            EXPORTING
              date         = l_v_date
            IMPORTING
              week         = l_week
            EXCEPTIONS
              date_invalid = 1
              OTHERS       = 2.
*** End of Mod-013
          IF sy-subrc EQ 0.
            CLEAR : l_v_year,
            l_v_week_num,
            l_v_week.
            l_v_year = l_week+0(4).
            l_v_week_num = l_week+4(2).
*** Begin of MOD-009
            CONCATENATE c_w l_v_week_num INTO l_v_week SEPARATED BY space.
            CONCATENATE l_v_week '.' l_v_year INTO l_v_week.
*** End of MOD-009
*** Begin of MOD-008
          ENDIF.
        ELSE.
          CONCATENATE c_month l_v_mon INTO l_v_week SEPARATED BY space.
          CONCATENATE l_v_week '.' l_v_year INTO l_v_week.
        ENDIF.
      ENDIF.
*** End of MOD-008
* Begin of Mod-011
* wa_items-time_series_id = wa_timeseries_item-time_series_id.
* wa_items-period_begin = l_v_time.
* wa_items-time_series_value = wa_timeseries_item-time_series_value.
      wa_items-time_series_id = wa_timeseries_item-key_figure_id.
      wa_items-period_begin = l_v_time.
      wa_items-time_series_value = wa_timeseries_item-value.
* End of Mod-011
      wa_items-week = l_v_week.
      APPEND wa_items TO i_items.
    ENDIF.
*** Begin of MOD-008
    CLEAR: wa_items, wa_timeseries_item, l_v_year, l_v_week, l_v_time,
    l_v_time1, l_v_day, l_v_mon, l_v_week_num, l_v_time_temp,
    l_v_time_temp1.
*** End of MOD-008
  ENDLOOP.
  i_items_temp[] = i_items[].
  DELETE ADJACENT DUPLICATES FROM i_items COMPARING week.
  DESCRIBE TABLE i_items LINES l_v_count.
* Append the period header
  LOOP AT i_items INTO wa_items.
* Maximum number of column reached
    IF v_colnb GE v_maxcol.
      EXIT.
    ENDIF.
    CLEAR wa_fieldcat.
    CONCATENATE c_cell v_colnb INTO wa_fieldcat-fieldname.
    wa_fieldcat-tabname = c_tabnam . "'I_OUTTAB'.
    wa_fieldcat-seltext_s = wa_items-week.
    wa_fieldcat-seltext_l = wa_items-week.
    wa_fieldcat-do_sum = c_x. "'X'.
    APPEND wa_fieldcat TO i_fieldcat.
    ADD 1 TO v_colnb.
  ENDLOOP.
  i_items[] = i_items_temp[] .
  SORT i_items.
* main loop on i_time_series to retrieve figures for ALV display
  v_linenb = 0.
* Begin of Mod-011
* LOOP AT i_time_series INTO wa_time_series.
  LOOP AT l_i_key_figure INTO wa_time_series.
* End of Mod-011
    v_linenb = v_linenb + 1.
    v_colnb = 1.
    v_charnb = 1.
* append the previous line in I_OUTTAB
    IF v_linenb > 1.
      APPEND wa_outtab TO i_outtab.
      CLEAR wa_outtab.
    ENDIF.
* Append the characterisitc values
* Begin of Mod-011
* READ TABLE i_char_combi INTO wa_char_combi WITH KEY
    READ TABLE l_i_char_comb INTO wa_char_combi WITH KEY
* End of Mod-011
    char_comb_id = wa_time_series-char_comb_id.
    v_plobj = wa_time_series-char_comb_id.
    DO.
      READ TABLE s_char INDEX v_charnb.
      IF sy-subrc NE 0.
        EXIT.
      ENDIF.
      ADD 1 TO v_charnb.
* Begin of Mod-011
* READ TABLE i_char_combi INTO wa_char_combi WITH KEY
      READ TABLE l_i_char_comb INTO wa_char_combi WITH KEY
* End of Mod-011
      char_comb_id = v_plobj
      characteristic_name = s_char-low.
      CLEAR wa_fieldcat.
      CONCATENATE c_tabcell v_colnb INTO v_fieldname.
      ASSIGN (v_fieldname) TO <fs_fieldn>.
      v_value_c = wa_char_combi-characteristic_value.
      <fs_fieldn> = v_value_c.
      IF wa_char_combi-characteristic_name = c_matnr . "'9AMATNR'.
        v_matnr = wa_char_combi-characteristic_value.
        CALL FUNCTION 'CONVERSION_EXIT_PRODU_INPUT'
          EXPORTING
            input        = v_matnr
          IMPORTING
            output       = v_matnr
          EXCEPTIONS
            length_error = 1
            OTHERS       = 2.
        IF sy-subrc EQ 0.
          CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
            EXPORTING
              iv_matnr        = v_matnr
            IMPORTING
              ev_matid        = v_matid
            EXCEPTIONS
              matid_not_found = 1
              OTHERS          = 2.
          IF sy-subrc <> 0.
            EXIT.
          ENDIF.
        ENDIF.
      ENDIF.
      ADD 1 TO v_colnb.
* Description required
      IF p_descr = c_x . "'X'.
* Add dexcription value if standard product or location
        CASE wa_char_combi-characteristic_name.
* Standard product characteristic required
          WHEN c_matnr . "'9AMATNR'.
            CLEAR wa_fieldcat.
            l_v_mat = wa_char_combi-characteristic_value.
* conver
            CALL FUNCTION 'CONVERSION_EXIT_PRODU_INPUT'
              EXPORTING
                input        = l_v_mat
              IMPORTING
                output       = l_v_mat
              EXCEPTIONS
                length_error = 1
                OTHERS       = 2.
            IF sy-subrc EQ 0.
              SELECT  mandt
                      matnr
                      matid
                      langu
                      meins
                      maktx
                      brgew
                      gewei
                      volum
                      voleh
                      kosch
                      compl
                      lvorm
                      kzkfg
                      maktg
                UP TO 1 ROWS
                FROM /sapapo/v_mattxt
                INTO wa_mat
                WHERE matnr = l_v_mat "i_char_combi-CHARACTERISTIC_VALUE
                  AND langu = sy-langu.
              ENDSELECT.
              IF sy-subrc EQ 0.
                CONCATENATE c_tabcell v_colnb INTO v_fieldname.
                ASSIGN (v_fieldname) TO <fs_fieldn>.
                v_value_c = wa_mat-maktx.
                <fs_fieldn> = v_value_c.
                ADD 1 TO v_colnb.
              ENDIF.
            ENDIF.
* Standard Location characteristic required
          WHEN c_locno ." '9ALOCNO'.
            CLEAR wa_fieldcat.
            SELECT  mandt
                    locid
                    locno
                    loctype
                    spras
                    descr40
              UP TO 1 ROWS
              FROM /sapapo/v_loctxt
              INTO wa_loc
              WHERE locno = wa_char_combi-characteristic_value
                AND spras = sy-langu.
            ENDSELECT.
            IF sy-subrc EQ 0.
              CONCATENATE c_tabcell v_colnb INTO v_fieldname.
              ASSIGN (v_fieldname) TO <fs_fieldn>.
              v_value_c = wa_loc-descr40.
              <fs_fieldn> = v_value_c.
              ADD 1 TO v_colnb.
            ENDIF.
          WHEN OTHERS.
        ENDCASE.
      ENDIF.
    ENDDO.
* Append keyfigure name to the line buffer
    CONCATENATE c_tabcell v_colnb INTO v_fieldname.
    ASSIGN (v_fieldname) TO <fs_fieldn>.
* Begin of Mod-011
* <fs_fieldn> = wa_time_series-key_figure_name.
    <fs_fieldn> = wa_time_series-key_figure_text.
* End of Mod-011
    ADD 1 TO v_colnb.
* Append Unit to the line buffer
    CONCATENATE c_tabcell v_colnb INTO v_fieldname.
    ASSIGN (v_fieldname) TO <fs_fieldn>.
    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
      EXPORTING
        input          = p_uom
      IMPORTING
        output         = v_uom
      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.
    <fs_fieldn> = v_uom.
    ADD 1 TO v_colnb.
***** Adding the time series values..
    SORT i_items BY time_series_id period_begin. "MOD-006
    l_v_colnb = v_colnb.
    LOOP AT i_items INTO wa_items WHERE time_series_id =
* Begin of Mod-011
* wa_time_series-time_series_id.
    wa_time_series-key_figure_id.
* End of Mod-011
      PERFORM get_uom_conversion USING p_uom
                                        v_matid
                              CHANGING wa_items-time_series_value.
      WRITE wa_items-time_series_value
        DECIMALS 3
        NO-ZERO
        EXPONENT 0
        TO v_value_n.

      CONCATENATE c_tabcell l_v_colnb INTO v_fieldname.
      ASSIGN (v_fieldname) TO <fs_fieldn>.
      <fs_fieldn> = v_value_n.
      ADD 1 TO l_v_colnb.
    ENDLOOP.
    CLEAR l_v_colnb.
  ENDLOOP.
* append last one line exist
  IF v_linenb GT 0.
    APPEND wa_outtab TO i_outtab.
  ENDIF.
ENDFORM. " read_planning_book_BAPI
*---------------------------------------------------------------------*
* Form display_alvdata
*---------------------------------------------------------------------*
FORM display_alvdata.
*Begin of MOD-012.
  DATA: v_matnr TYPE /sapapo/matid.
  LOOP AT i_outtab INTO wa_outtab.
    CALL FUNCTION 'CONVERSION_EXIT_PRODU_OUTPUT'
      EXPORTING
        input  = wa_outtab-cell001
      IMPORTING
        output = v_matnr.
    wa_outtab-cell001 = v_matnr.
    MODIFY i_outtab FROM wa_outtab TRANSPORTING cell001.
  ENDLOOP.
*End of MOD-012.
* General layout format
  is_layout-colwidth_optimize = c_x .
  is_layout-zebra = c_x .
* Display ALV or store in a file
  IF p_file IS INITIAL.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program = c_prgnam
        is_layout          = is_layout
        it_fieldcat        = i_fieldcat[]
        it_sort            = i_sort[]
      TABLES
        t_outtab           = i_outtab.
  ELSE.
* Prepare the buffer to be written with header first
    REFRESH i_outtab2.
    v_colnb = 1.
    LOOP AT i_fieldcat INTO wa_fieldcat.
      CONCATENATE c_outtab2 v_colnb INTO v_fieldname.
      ASSIGN (v_fieldname) TO <fs_fieldn>.
      <fs_fieldn> = wa_fieldcat-seltext_l.
      ADD 1 TO v_colnb.
    ENDLOOP.
    APPEND wa_outtab2 TO i_outtab2.
    DESCRIBE TABLE i_outtab LINES v_linenb.
    APPEND LINES OF i_outtab FROM 1 TO v_linenb TO i_outtab2.
* Option to store on the presentation server
    CALL FUNCTION 'DOWNLOAD'
      EXPORTING
        codepage            = c_codepage
        filename            = p_file
        filetype            = c_filetype
        item                = text-017
      TABLES
        data_tab            = i_outtab2
      EXCEPTIONS
        invalid_filesize    = 1
        invalid_table_width = 2
        invalid_type        = 3.
    IF sy-subrc <> 0.
      WRITE: text-016.
    ENDIF.
  ENDIF.
ENDFORM. "display_alvdata
*&---------------------------------------------------------------------*
*& Form validate_view
*&---------------------------------------------------------------------*
FORM validate_view.
  DATA : l_v_mview TYPE /sapapo/pb_mview.
  SELECT SINGLE mview INTO l_v_mview FROM /sapapo/pbdvw
    WHERE mview = p_pbook
      AND dview = p_view.
  IF sy-subrc <> 0.
    MESSAGE e000 WITH text-023.
  ENDIF.
  IF NOT l_v_mview IS INITIAL.
    CLEAR : l_v_mview .
  ENDIF.
ENDFORM. " validate_view
*&---------------------------------------------------------------------*
*& Form validate_keyf
*&---------------------------------------------------------------------*
FORM validate_keyf.
  DATA : i_mview TYPE /sapapo/pb_mview.
  SELECT mview UP TO 1 ROWS INTO i_mview
    FROM /sapapo/pbmlkey
    WHERE mview = p_pbook
      AND iobjnm IN s_keyf.
  ENDSELECT.
  IF sy-subrc <> 0.
    MESSAGE e000 WITH text-022.
  ENDIF.
  IF NOT i_mview IS INITIAL.
    CLEAR : i_mview .
  ENDIF.
ENDFORM. " validate_keyf
*&---------------------------------------------------------------------*
*& Form validate_selid
*&---------------------------------------------------------------------*
FORM validate_selid.
  DATA : l_v_selectionid TYPE /sapapo/selectionid2 .
  IF NOT p_selid IS INITIAL.
    SELECT selectionid UP TO 1 ROWS
      FROM /sapapo/ts_selko
      INTO l_v_selectionid
      WHERE pareaid = v_pareaname
        AND sel_desrciption = p_selid.
    ENDSELECT.
    IF sy-subrc <> 0.
      MESSAGE e000 WITH text-020.
    ENDIF.
  ENDIF.
  IF NOT l_v_selectionid IS INITIAL.
    CLEAR l_v_selectionid.
  ENDIF.
ENDFORM. " validate_selid
*** End of MOD-008
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT zaxxpr_to_apo.
* Data declarations
* include zaxxpr_to_apo_top.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_TO_APO_TOP
*&---------------------------------------------------------------------*
*Types declarations
TYPES :
BEGIN OF ty_flatfile,
  record TYPE char120,
END OF ty_flatfile.
TYPES :
BEGIN OF ty_text,
  line TYPE char128,
END OF ty_text.
*types for the directory table
TYPES :
BEGIN OF ty_direct,
  direct TYPE char50,
END OF ty_direct.
*types for the ftp command result internal table
TYPES :
BEGIN OF ty_result,
  text TYPE char512,
END OF ty_result.
*types for the reterieved files
TYPES :
BEGIN OF ty_files,
  file TYPE char200,
END OF ty_files.
*types for the data
TYPES :
BEGIN OF ty_data,
  data TYPE char2000,
END OF ty_data.
*types for the log data
TYPES :
BEGIN OF ty_log,
  text TYPE char2000,
END OF ty_log.
**types for the lock object table
TYPES :
BEGIN OF ty_locobj.
        INCLUDE STRUCTURE zap_lockobj.
TYPES : END OF ty_locobj.
*types for the archive and log filepath
TYPES :
BEGIN OF ty_path,
  funcproc TYPE rs38l_fnam,
  filepath TYPE zfilepath01,
  arcpath TYPE char512,
  logpath TYPE char512,
  location TYPE char40,
  file TYPE char200,
END OF ty_path.
*Internal table declarations
DATA: it_flatfile TYPE STANDARD TABLE OF ty_flatfile,
      it_result TYPE STANDARD TABLE OF ty_result,
      it_direct TYPE STANDARD TABLE OF ty_direct,
      it_files TYPE STANDARD TABLE OF ty_files,
      it_data TYPE STANDARD TABLE OF ty_data,
      it_log TYPE STANDARD TABLE OF ty_log,
      it_log1 TYPE STANDARD TABLE OF ty_log,
      it_locobj TYPE STANDARD TABLE OF zap_lockobj2,
      it_funcproc TYPE STANDARD TABLE OF zap_funcproc,
      it_seqno1 TYPE STANDARD TABLE OF zap_flatfile,
      it_seqno2 TYPE STANDARD TABLE OF zap_flatfile,
      it_seqno3 TYPE STANDARD TABLE OF zap_flatfile,
      it_seqno4 TYPE STANDARD TABLE OF zap_flatfile,
      it_seqno5 TYPE STANDARD TABLE OF zap_flatfile,
      it_lockobj TYPE STANDARD TABLE OF zap_lockobj2,
      it_path TYPE STANDARD TABLE OF ty_path.
*Work area declarations
DATA: l_wa_flatfile TYPE ty_flatfile,
      wa_result TYPE ty_result,
      wa_direct TYPE ty_direct,
      wa_files TYPE ty_files,
      wa_data TYPE ty_data,
      wa_log TYPE ty_log,
      wa_path TYPE ty_path.
*Table type declaration
TYPES : ty_t_direct TYPE TABLE OF ty_direct,
        ty_t_result TYPE TABLE OF ty_result,
        ty_t_files TYPE TABLE OF ty_files,
        ty_t_data TYPE TABLE OF ty_data,
        ty_t_log TYPE TABLE OF ty_log,
        ty_t_locobj TYPE TABLE OF zap_lockobj2,
        ty_t_funcproc TYPE TABLE OF zap_funcproc,
        ty_t_seqno TYPE TABLE OF zap_flatfile,
        ty_t_lockobj TYPE TABLE OF zap_lockobj2,
        ty_t_path TYPE TABLE OF ty_path.
*Global variable decleration
DATA: v_cmd(512) TYPE c,
      l_cmd1(150) TYPE c,
      v_handle TYPE i,
      v_filepath TYPE zfilepath01,
      v_fiseq TYPE char1,
      v_process TYPE char1,
      v_lock TYPE char1,
      v_funproc TYPE rs38l_fnam,
      v_seqno TYPE char1,
      v_locobj TYPE char1,
      v_logpath TYPE char512,
      v_arcpath TYPE char512,
      v_filepath1 TYPE char512,
      v_error TYPE char1,
      v_location TYPE char40.
*Constant declarations
CONSTANTS : c_path TYPE char128 VALUE
'\\pmipmftpaisdev.eu.pm.com\pmipmftpaisdev\',
c_host TYPE char100 VALUE
'\\pmipmftpaisdev.eu.pm.com\PMIPMFTPAISDEV',
c_e TYPE char1 VALUE 'E',
c_w TYPE char1 VALUE 'W',
c_slash TYPE char1 VALUE '/',
c_20 TYPE char2 VALUE '20',
c_y TYPE char1 VALUE 'Y',
c_x TYPE char1 VALUE 'X',
c_1 TYPE char1 VALUE '1',
c_2 TYPE char1 VALUE '2'.
*Subroutines
* include zaxxpr_to_apo_forms.
*----------------------------------------------------------------------*
* SELECTION SCREEN *
*----------------------------------------------------------------------*
SELECTION-SCREEN: BEGIN OF BLOCK blk1
WITH FRAME TITLE text-001. "Selection Options
SELECTION-SCREEN SKIP 1.
PARAMETERS : p_file TYPE char200.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN: END OF BLOCK blk1.
*----------------------------------------------------------------------*
* START OF SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*Check whether the time is open for processing from
*the timeslot table
  PERFORM f_check_open_time CHANGING v_process.
*Dont process the file if the time is not open for processing
  IF v_process <> c_y.
    LEAVE LIST-PROCESSING.
  ENDIF.
*Perform FTP connection with the windows NT server
  PERFORM f_ftp_connection.
*Get the directory from the FTP path
  PERFORM f_get_directory USING p_file
                          CHANGING it_direct.
  LOOP AT it_direct INTO wa_direct.
*Get the files inside the directory
    PERFORM f_get_files USING p_file
                              wa_direct
                        CHANGING it_files.
    LOOP AT it_files INTO wa_files.
*Lock the file before processing
      PERFORM f_lock_file USING p_file
                                wa_direct
                                wa_files
                          CHANGING v_filepath.
*Validate the files and issue the appropiate log
      PERFORM f_validate_file USING wa_files
                                    v_filepath
                              CHANGING v_fiseq
                                        v_funproc
                                        it_log.
*Read the files from the path
      PERFORM f_read_files USING p_file
                                  wa_direct
                                  wa_files
                          CHANGING v_arcpath
                                    v_filepath1
                                    v_location
                                    it_data
                                    it_log.
*Delete the file from the current directory and archive the
*file in the archive directory
      PERFORM f_archive_file USING it_data
                                    v_arcpath
                                    v_filepath1.
*Create the log table in the respective filepath
      PERFORM f_create_log USING p_file
                                  wa_direct
                                  wa_files
                                  it_log
                          CHANGING v_logpath.
*Update the archive path in the custom table for processing
      PERFORM f_update_archive_file USING v_funproc
                                          v_filepath
                                          v_arcpath
                                          v_logpath
                                          v_location
                                          wa_files
                                    CHANGING it_path.
      REFRESH : it_data,
                it_log.
      CLEAR : v_lock,
              v_locobj,
              v_seqno,
              v_funproc,
              v_filepath.
    ENDLOOP.
    REFRESH it_files.
  ENDLOOP.
  LOOP AT it_path INTO wa_path.
*Lock the file before processing
    PERFORM f_lock_filepath USING wa_path-filepath
                            CHANGING v_lock.
*If the file is already in processing skip the file
    IF v_lock = c_x.
      PERFORM f_log_data USING space
                                text-020
                                space
                        CHANGING it_log1.
      CONTINUE.
    ENDIF.
*Read the data from the archive directory
    PERFORM f_read_archive_data USING wa_path-arcpath
                                CHANGING it_data.
***Sequence the file
    PERFORM f_sequence_file USING wa_path-funcproc
                                  it_data
                            CHANGING v_seqno
                                      it_locobj
                                      it_funcproc
                                      it_seqno1
                                      it_seqno2
                                      it_seqno3
                                      it_seqno4
                                      it_seqno5.
*Lock the locations and the lock objects in the custom lock tabe
    PERFORM f_lock_objects USING it_locobj
                          CHANGING v_locobj
                                    it_lockobj.
*If any of the location and its lock object are locked
*dont process the file
    IF v_locobj = c_x.
      PERFORM f_log_data USING space
                                text-019
                                space
                        CHANGING it_log1.
      DELETE FROM zap_fileproc WHERE filepath = wa_path-filepath.
      COMMIT WORK.
      CONTINUE.
    ENDIF.
*Perform start processing log
    PERFORM f_start_process_log CHANGING it_log1.
*MOD-010
* IF it_seqno1[] IS NOT INITIAL.*
**Process the first sequence and get the log table
* PERFORM f_process_seqno USING it_seqno1
* it_funcproc
* c_1
* CHANGING v_error
* it_log1.
*
* ENDIF.
*
* IF it_seqno2[] IS NOT INITIAL.*
**Process the second sequence and get the log table
* PERFORM f_process_seqno USING it_seqno2
* it_funcproc
* c_2
* CHANGING v_error
* it_log1.*
* ENDIF.
*MOD-010
*Perform end of processing log
    PERFORM f_end_process_log CHANGING it_log1.
*append the log table in the respective filepath
    PERFORM f_append_log USING it_log1
                                wa_path-logpath.
** Delete the filepath from the custom table after processing
* PERFORM f_delete_lock_file USING wa_path-filepath
* it_lockobj.
*
* IF there is an error occured during loading or in the file
* send the email notification to the customer
    IF v_error = c_x.
      PERFORM f_send_email USING p_file
                                  wa_direct
                                  wa_path-file
                                  wa_path-logpath
                                  wa_path-arcpath
                                  wa_path-location.
    ENDIF.
*
    CLEAR : v_lock,
            v_locobj,
            v_seqno,
            v_funproc,
            v_filepath,
            v_error.
    REFRESH : it_locobj,
              it_funcproc,
              it_seqno1,
              it_seqno2,
              it_seqno3,
              it_seqno4,
              it_seqno5,
              it_log1,
              it_data,
              it_lockobj.
  ENDLOOP.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_TO_APO_FORMS
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_TO_APO_SIGN_TOP
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_TO_APO_SIGN_FORMS
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form f_ftp_connection
*&---------------------------------------------------------------------*
* FTP connectivity to Windows NT server
*----------------------------------------------------------------------*
FORM f_ftp_connection .
*Local variable declarations
  DATA: l_v_pwd TYPE char20 VALUE 'Tezomin4',
        l_v_key TYPE i VALUE 26101957,
        l_v_pwd_len TYPE i.

  DESCRIBE FIELD l_v_pwd LENGTH l_v_pwd_len IN CHARACTER MODE.
* Scramble the password before connecting to the FTP server

  CALL 'AB_RFC_X_SCRAMBLE_STRING'
    ID 'SOURCE' FIELD l_v_pwd ID 'KEY' FIELD l_v_key
    ID 'SCR' FIELD 'X' ID 'DESTINATION' FIELD l_v_pwd
    ID 'DSTLEN' FIELD l_v_pwd_len.
* Connect to the FTP server
  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user            = 'ap\s-ftpsgacd'
      password        = l_v_pwd
      host            = 'pmipmftpaisdev.eu.pm.com'
      rfc_destination = 'SAPFTPA'
    IMPORTING
      handle          = v_handle
    EXCEPTIONS
      not_connected   = 1
      OTHERS          = 2.

ENDFORM. " f_ftp_connection
*&---------------------------------------------------------------------*
*& Form f_get_directory
*&---------------------------------------------------------------------*
* Get the directorys for the paths specified in the selection
* screen
**----------------------------------------------------------------------*
* <--FP_IT_DIRECT Directorys internal table
*----------------------------------------------------------------------*
FORM f_get_directory USING fp_p_file TYPE char200
                    CHANGING fp_it_direct TYPE ty_t_direct.
* Local variables
  DATA: l_v_cmd TYPE char512,
        l_v_fdpos TYPE syfdpos.
* Changing directory
  CONCATENATE 'cd' fp_p_file INTO l_v_cmd SEPARATED BY space.
* Change the directory
  PERFORM f_ftp_command USING l_v_cmd
                        CHANGING it_result.
  IF sy-subrc = 0.
    REFRESH it_result.
    CLEAR l_v_cmd.
* Ascii mode
    l_v_cmd = 'ascii'.
    PERFORM f_ftp_command USING l_v_cmd
                          CHANGING it_result.
    IF sy-subrc = 0.
      REFRESH it_result.
      CLEAR l_v_cmd.
* Get the folders inside the directory
      l_v_cmd = 'dir'.
      PERFORM f_ftp_command USING l_v_cmd
                            CHANGING it_result.
      IF sy-subrc = 0.
        LOOP AT it_result INTO wa_result.
          SEARCH wa_result FOR '<DIR>'.
          IF sy-subrc = 0.
            l_v_fdpos = sy-fdpos.
            l_v_fdpos = l_v_fdpos + 5.
            DO.
              l_v_fdpos = l_v_fdpos + 1.
              IF wa_result+l_v_fdpos(1) <> ' '.
                wa_direct-direct = wa_result+l_v_fdpos(20).
                APPEND wa_direct TO fp_it_direct.
                EXIT.
              ENDIF.
            ENDDO.
          ENDIF.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.
  REFRESH it_result.
ENDFORM. " f_get_directory
*&---------------------------------------------------------------------*
*& Form f_ftp_command
*&---------------------------------------------------------------------*
* Execute the unix command
*----------------------------------------------------------------------*
* -->FP_L_CMD text
* <--FP_IT_RESULT text
*----------------------------------------------------------------------*
FORM f_ftp_command USING fp_l_cmd TYPE char512
                  CHANGING fp_it_result TYPE ty_t_result.
*Execute the FTP Command
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = v_handle
      command       = fp_l_cmd
    TABLES
      data          = fp_it_result
    EXCEPTIONS
      tcpip_#rror   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.

ENDFORM. " f_ftp_command
*&---------------------------------------------------------------------*
*& Form f_get_files
*&---------------------------------------------------------------------*
* Get the files from the directory
*----------------------------------------------------------------------*
* -->FP_P_FILE File path
* -->FP_WA_DIRECT Directory name
* <--FP_IT_FILES Files
*----------------------------------------------------------------------*
FORM f_get_files USING fp_p_file TYPE char200
                        fp_wa_direct TYPE ty_direct
                CHANGING fp_it_files TYPE ty_t_files.
* Local variable
  DATA: l_v_cmd TYPE char512,
        l_v_fdpos TYPE syfdpos.

  CONCATENATE 'cd' fp_wa_direct INTO l_v_cmd SEPARATED BY space.
* Change the directory
  PERFORM f_ftp_command USING l_v_cmd
                        CHANGING it_result.
  IF sy-subrc = 0.
    REFRESH it_result.
    CLEAR v_cmd.
    CONCATENATE 'dir' '*.csv' v_cmd INTO v_cmd SEPARATED BY space.
* Get the files from the directory
    PERFORM f_ftp_command USING v_cmd
                          CHANGING it_result.
    IF sy-subrc = 0.
      LOOP AT it_result INTO wa_result.
        IF sy-tabix = 1.
          CONTINUE.
        ENDIF.
        SEARCH wa_result FOR '.csv'.
        IF sy-subrc = 0.
          wa_files-file = wa_result+39(200).
          APPEND wa_files TO fp_it_files.
          l_v_fdpos = sy-fdpos.
          DO.
            l_v_fdpos = l_v_fdpos - 1.
            IF wa_result+l_v_fdpos(1) = ' '.
              l_v_fdpos = l_v_fdpos + 1.
              wa_files-file = wa_result+l_v_fdpos(100).
              APPEND wa_files TO fp_it_files.
              EXIT.
            ENDIF.
          ENDDO.
        ENDIF.
      ENDLOOP.
    ENDIF.
  ENDIF.
  l_v_cmd = 'cdup'.
* Change the directory by one
  PERFORM f_ftp_command USING l_v_cmd
                        CHANGING it_result.
  IF sy-subrc = 0.
    REFRESH it_result.
    CLEAR l_v_cmd.
  ENDIF.
ENDFORM. " f_get_files
*&---------------------------------------------------------------------*
*& Form f_validate_file
*&---------------------------------------------------------------------*
* Validate the file name and issue appropiate log
*----------------------------------------------------------------------*
* -->FP_WA_FILES File name
* -->FP_V_FILEPATH File Path
* <--FP_V_FISEQ File sequence
* <--FP_V_FUNPROC Function Module
* <--FP_IT_LOG Log internal table
*----------------------------------------------------------------------*
FORM f_validate_file USING fp_wa_files TYPE ty_files
                            fp_v_filepath TYPE zfilepath01
                    CHANGING fp_v_fiseq TYPE char1
                              fp_v_funproc TYPE rs38l_fnam
                              fp_it_log TYPE ty_t_log.
* Local variable
  DATA: l_v_fityp TYPE char4,
        l_v_date TYPE sy-datum,
        l_v_date1 TYPE char4.
* Add the log
  PERFORM f_log_data USING space
                            space
                            space
                    CHANGING fp_it_log.
* Append the file name to the log table
  CLEAR wa_log.

  CONCATENATE c_host fp_v_filepath INTO wa_log-text
      SEPARATED BY '/'.
  CONCATENATE text-005 wa_log-text INTO wa_log-text
      SEPARATED BY space.
  APPEND wa_log TO fp_it_log.
*Validate the file type
  SELECT SINGLE
      fityp
      fiseq
      funproc
    FROM zap_file
    INTO (l_v_fityp,
          fp_v_fiseq,
          fp_v_funproc)
    WHERE fityp = fp_wa_files+0(4).
  IF sy-subrc <> 0.
*Not valid file type
    PERFORM f_log_data USING c_e
                              text-003
                              space
                      CHANGING fp_it_log.
  ELSE.
    l_v_date = fp_wa_files+4(8).
*Validate the date in the file
    l_v_date1 = sy-datum - l_v_date.
    IF l_v_date1 > c_20.
*Date is not within 20 days
      PERFORM f_log_data USING c_e
                                text-004
                                space
                          CHANGING fp_it_log.
    ENDIF.
  ENDIF.
ENDFORM. " f_validate_file
*&---------------------------------------------------------------------*
*& Form f_read_files
*&---------------------------------------------------------------------*
* Read the file from the directory
*----------------------------------------------------------------------*
* -->FP_P_FILE File path
* -->FP_WA_DIRECT Directory
* -->FP_WA_FILES File name
* <--FP_IT_DATA Raw Data
* <--FP_IT_LOG Log internal table
*----------------------------------------------------------------------*
FORM f_read_files USING fp_p_file TYPE char200
                        fp_wa_direct TYPE ty_direct
                        fp_wa_files TYPE ty_files
                  CHANGING fp_v_arcpath TYPE char512
                            fp_v_filepath TYPE char512
                            fp_v_location TYPE char40
                            fp_it_data TYPE ty_t_data
                            fp_it_log TYPE ty_t_log.
*Local variables
  DATA: l_v_filepath TYPE char512.
* local constants
  CONSTANTS : c_host TYPE char100 VALUE
  '\\pmipmftpaisdev.eu.pm.com\PMIPMFTPAISDEV',
  l_c_archive TYPE char20 VALUE 'archive'.

  CONCATENATE c_host fp_p_file fp_wa_direct fp_wa_files
      INTO fp_v_filepath SEPARATED BY '\'.
  CONCATENATE fp_p_file fp_wa_direct INTO fp_v_location
      SEPARATED BY '\'.
* Open the file and transfer the data into the internal table
  OPEN DATASET fp_v_filepath
      FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc = 0.
    DO.
      READ DATASET fp_v_filepath INTO wa_data .
      IF sy-subrc = 0.
        APPEND wa_data TO fp_it_data.
        CLEAR wa_data.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
    CONCATENATE c_host fp_p_file fp_wa_direct l_c_archive
    fp_wa_files INTO fp_v_arcpath SEPARATED BY '\'.
*Open the archive directory
    OPEN DATASET fp_v_arcpath
      FOR
      OUTPUT IN TEXT MODE ENCODING DEFAULT.
    IF sy-subrc = 0.
*Put the file in the archive directory
      LOOP AT fp_it_data INTO wa_data.
        TRANSFER wa_data TO fp_v_arcpath.
      ENDLOOP.
*Delete the file in the current location
      DELETE DATASET fp_v_filepath.
    ENDIF.
  ENDIF.
ENDFORM. " f_read_files
*&---------------------------------------------------------------------*
*& Form f_log_data
*&---------------------------------------------------------------------*
* Read all the log information in the internal table
*----------------------------------------------------------------------*
* -->FP_MSGTY Message type
* -->FP_MSGTX Message text
* -->FP_MSGV1 Message number
* <--FP_IT_LOG Log table
*----------------------------------------------------------------------*
FORM f_log_data USING fp_msgty TYPE any
                      fp_msgtx TYPE char512
                      fp_msgv1 TYPE any
                CHANGING fp_it_log TYPE ty_t_log.
* Local variable
  DATA: l_v_log TYPE char512,
        l_v_datum TYPE char10,
        l_v_uzeit TYPE char8.
* Build the date format
  CONCATENATE sy-datum+0(4) sy-datum+4(2) sy-datum+6(2)
      INTO l_v_datum SEPARATED BY c_slash.
* Build the time format
  CONCATENATE sy-uzeit+0(2) sy-uzeit+2(2) sy-uzeit+4(2)
      INTO l_v_uzeit SEPARATED BY ':'.
* Build the log table
  CONCATENATE text-002 l_v_datum l_v_uzeit INTO l_v_log
      SEPARATED BY space.
  CONCATENATE l_v_log ':' fp_msgtx INTO l_v_log
      SEPARATED BY space.
  CLEAR wa_log.
* Append the log into the internal table
  wa_log-text = l_v_log.
  APPEND wa_log TO fp_it_log.
ENDFORM. " f_log_data
*&---------------------------------------------------------------------*
*& Form f_check_open_time
*&---------------------------------------------------------------------*
* Check whether the time is open for processing
*----------------------------------------------------------------------*
FORM f_check_open_time CHANGING fp_v_process.
*Local variable
  DATA: l_v_process TYPE char1,
        l_v_date TYPE char2,
        l_v_hour TYPE char2.
*Local constants
  CONSTANTS : l_c_star TYPE char1 VALUE '*'.

  l_v_date = sy-datum+6(2).
  l_v_hour = sy-uzeit+0(2).
  SELECT SINGLE
    process
    FROM zap_timeslot
    INTO l_v_process
    WHERE zdate = l_v_date AND
          zhour = l_v_hour.
  IF sy-subrc <> 0.
    SELECT SINGLE
      process
      FROM zap_timeslot
      INTO l_v_process
      WHERE zdate = l_c_star AND
            zhour = l_v_hour.
    IF sy-subrc = 0.
      fp_v_process = l_v_process.
    ENDIF.
  ELSE.
    fp_v_process = l_v_process.
  ENDIF.
ENDFORM. " f_check_open_time
*&---------------------------------------------------------------------*
*& Form f_lock_file
*&---------------------------------------------------------------------*
* Lock the file in the table
*----------------------------------------------------------------------*
* -->FP_P_FILE File Path
* -->FP_WA_DIRECT Directory
* -->FP_WA_FILES File name
* <--FP_V_FILEPATH FIle Path
* <--FP_V_LOCK Lock Key
*----------------------------------------------------------------------*
FORM f_lock_file USING fp_p_file TYPE char200
                        fp_wa_direct TYPE ty_direct
                        fp_wa_files TYPE ty_files
                CHANGING fp_v_filepath TYPE zfilepath01.
*Concatenate the files and get the file path
  CONCATENATE fp_p_file fp_wa_direct fp_wa_files
    INTO fp_v_filepath SEPARATED BY '\'.
ENDFORM. " f_lock_file
*&---------------------------------------------------------------------*
*& Form f_delete_lock_file
*&---------------------------------------------------------------------*
* Delete the filepath after processing
*----------------------------------------------------------------------*
* -->FP_V_FILEPATH File Path
* -->FP_IT_LOCKOBJ Lock Object Internal Table
*----------------------------------------------------------------------*
FORM f_delete_lock_file USING fp_v_filepath TYPE zfilepath01
                              fp_it_lockobj TYPE ty_t_locobj.
  DELETE FROM zap_fileproc WHERE filepath = fp_v_filepath.
* DELETE zap_lockobj FROM TABLE fp_it_lockobj.
  COMMIT WORK.
ENDFORM. " f_delete_lock_file
*&---------------------------------------------------------------------*
*& Form f_sequence_file
*&---------------------------------------------------------------------*
* Sequecnce the file using the file type function module
*----------------------------------------------------------------------*
** -->FP_V_FUNPROC Function Module for sequencing
* -->FP_IT_DATA Flatfile internal table
* <--FP_V_SEQNO Sequence Number
* <--FP_IT_LOCOBJ Lock Object internal table
* <--FP_IT_FUNCPROC Function modules for processing the sequence
* <--FP_IT_SEQNO1 Sequcence No 1 internal table
* <--FP_IT_SEQNO2 Sequcence No 2 internal table
* <--FP_IT_SEQNO3 Sequcence No 3 internal table
* <--FP_IT_SEQNO4 Sequcence No 4 internal table
* <--FP_IT_SEQNO5 Sequcence No 5 internal table
*--------------------------------#-----------------------------------*
FORM f_sequence_file USING fp_v_funproc TYPE rs38l_fnam
                            fp_it_data TYPE ty_t_data
                    CHANGING fp_v_seqno TYPE char1
                              fp_it_locobj TYPE ty_t_locobj
                              fp_it_funcproc TYPE ty_t_funcproc
                              fp_it_seqno1 TYPE ty_t_seqno
                              fp_it_seqno2 TYPE ty_t_seqno
                              fp_it_seqno3 TYPE ty_t_seqno
                              fp_it_seqno4 TYPE ty_t_seqno
                              fp_it_seqno5 TYPE ty_t_seqno.
  CALL FUNCTION fp_v_funproc
    IMPORTING
      seq_no                   = fp_v_seqno
    TABLES
      input_data               = fp_it_data
      lockobject               = fp_it_locobj
      funcproc                 = fp_it_funcproc
      seqno1                   = fp_it_seqno1
      seqno2                   = fp_it_seqno2
      seqno3                   = fp_it_seqno3
      seqno4                   = fp_it_seqno4
      seqno5                   = fp_it_seqno5
    EXCEPTIONS
      no_sequence_number_found = 1
      OTHERS                   = 2.

ENDFORM. " f_sequence_file
*&---------------------------------------------------------------------*
*& Form f_lock_objects
*&---------------------------------------------------------------------*
* Lock the location wise lock objects
*----------------------------------------------------------------------*
* -->FP_IT_LOCOBJ Location Lock object
* <--FP_V_LOCOBJ Lock Object
*----------------------------------------------------------------------*
FORM f_lock_objects USING fp_it_locobj TYPE ty_t_locobj
                    CHANGING fp_v_locobj TYPE char1
                              fp_it_lockobj TYPE ty_t_lockobj.
*Local internal table
  DATA: l_it_lockobj TYPE STANDARD TABLE OF zap_lockobj2.
*Local work area
  DATA: l_wa_lockobj TYPE zap_lockobj2,
  l_wa_lockobj1 TYPE zap_lockobj2.
*Check whether the lock entries are in the custom lock table
*or not
  SELECT location
    funproc
    FROM zap_lockobj2
    INTO TABLE l_it_lockobj
    FOR ALL ENTRIES IN fp_it_locobj
    WHERE location = fp_it_locobj-location AND
          funproc = fp_it_locobj-funproc.
  IF sy-subrc = 0.
    fp_v_locobj = c_x.
  ELSE.
    LOOP AT fp_it_locobj INTO l_wa_lockobj.
      l_wa_lockobj1-mandt = sy-mandt.
      l_wa_lockobj1-location = l_wa_lockobj-location.
      l_wa_lockobj1-funproc = l_wa_lockobj-funproc.
      APPEND l_wa_lockobj1 TO fp_it_lockobj.
    ENDLOOP.
    MODIFY zap_lockobj2 FROM TABLE fp_it_lockobj.
    COMMIT WORK.
  ENDIF.
ENDFORM. " f_lock_objects
*&---------------------------------------------------------------------*
*& Form f_create_log
*&---------------------------------------------------------------------*
* Transfer the log information
*----------------------------------------------------------------------*
* -->FP_P_FILE File Path
* -->FP_WA_DIRECT Directories Name
* -->FP_WA_FILES File Name
* -->FP_IT_LOG Log internal table
* <--FP_V_LOGPATH File path for Log
*----------------------------------------------------------------------*
FORM f_create_log USING fp_p_file TYPE char200
                        fp_wa_direct TYPE ty_direct
                        fp_wa_files TYPE ty_files
                        fp_it_log TYPE ty_t_log
                CHANGING fp_v_logpath TYPE char512.
*Local variables
  DATA: l_v_files TYPE char200.

  CONCATENATE 'log_' fp_wa_files INTO l_v_files.
  REPLACE '.csv' IN l_v_files WITH '.txt'.
  CONCATENATE c_host fp_p_file fp_wa_direct 'log' l_v_files
    INTO v_logpath SEPARATED BY '\'.
*Open the log directory
  OPEN DATASET fp_v_logpath
    FOR APPENDING
    IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc = 0.
*Put the file in the log directory
    LOOP AT fp_it_log INTO wa_log.
      TRANSFER wa_log TO fp_v_logpath.
    ENDLOOP.
    CLOSE DATASET fp_v_logpath.
  ENDIF.
ENDFORM. " f_create_log
*&---------------------------------------------------------------------*
*& Form f_send_email
*&---------------------------------------------------------------------*
* Send the Email Notification to the customer
*----------------------------------------------------------------------*
* -->FP_P_FILE File path
* -->FP_WA_FILES File Name
* -->FP_V_LOGPATH Log File path
* -->FP_V_ARCPATH Archive file path
*----------------------------------------------------------------------*
FORM f_send_email USING fp_p_file TYPE char200
                        fp_wa_direct TYPE ty_direct
                        fp_wa_files TYPE char200
                        fp_v_logpath TYPE char512
                        fp_v_arcpath TYPE char512
                        fp_v_location TYPE char40.
* Local variables
  DATA: l_v_lines TYPE char2,
        l_v_text TYPE char200,
        l_v_datum TYPE char10,
        l_v_uzeit TYPE char8,
        l_v_smtpadr TYPE ad_smtpadr,
        l_v_location TYPE char40.
* Local work area
  DATA: l_wa_content TYPE solisti1,
        l_wa_document TYPE sodocchgi1,
        l_wa_recieve TYPE somlreci1.
* Local internal table
  DATA: l_it_content TYPE STANDARD TABLE OF solisti1,
        l_it_recieve TYPE STANDARD TABLE OF somlreci1.
  CONCATENATE fp_p_file fp_wa_direct INTO l_v_location
    SEPARATED BY '\'.
*Select the email address based on the file path
  SELECT SINGLE
    smtpadr
    FROM zap_email
    INTO l_v_smtpadr
    WHERE location = fp_v_location.
  IF sy-subrc = 0.
*Mail contents
    CLEAR l_wa_content.
    l_wa_content-line = 'Dear Customer,'.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    APPEND l_wa_content TO l_it_content.
    CONCATENATE text-007 text-008 INTO l_v_text
      SEPARATED BY space.
    l_wa_content-line = l_v_text.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    l_wa_content-line = text-009.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    l_wa_content-line = fp_v_logpath.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    l_wa_content-line = text-010.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    l_wa_content-line = fp_v_arcpath.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    CONCATENATE text-011 text-012 INTO l_v_text
      SEPARATED BY space.
    l_wa_content-line = l_v_text.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    l_wa_content-line = text-013.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    APPEND l_wa_content TO l_it_content.
    CONCATENATE text-006 fp_wa_files INTO l_wa_content-line
      SEPARATED BY space.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    APPEND l_wa_content TO l_it_content.
*Build the date format
    CONCATENATE sy-datum+0(4) sy-datum+4(2) sy-datum+6(2)
      INTO l_v_datum SEPARATED BY c_slash.
*Build the time format
    CONCATENATE sy-uzeit+0(2) sy-uzeit+2(2) sy-uzeit+4(2)
      INTO l_v_uzeit SEPARATED BY ':'.
    CONCATENATE text-014 l_v_datum l_v_uzeit INTO l_wa_content-line
      SEPARATED BY space.
    APPEND l_wa_content TO l_it_content.
    CLEAR l_wa_content.
    APPEND l_wa_content TO l_it_content.
    l_wa_content-line = text-015.
    APPEND l_wa_content TO l_it_content.
*Heading of the mail
    l_wa_document-obj_name = 'ERROR'.
    CONCATENATE text-006 fp_wa_files INTO l_wa_document-obj_descr
      SEPARATED BY '-'.
    l_wa_document-obj_langu = sy-langu.
    l_wa_document-sensitivty = 'O'.
    l_wa_document-priority = '1'.
*Reciptents Mail Id
    l_wa_recieve-receiver = l_v_smtpadr.
    l_wa_recieve-rec_type = 'U'.
    l_wa_recieve-com_type = 'SOTR'.
    APPEND l_wa_recieve TO l_it_recieve.

    CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
      EXPORTING
        document_data              = l_wa_document
        document_type              = 'RAW'
        put_in_outbox              = c_x
        commit_work                = c_x
      TABLES
        object_content             = l_it_content
        receivers                  = l_it_recieve
      EXCEPTIONS
        too_many_receivers         = 1
        document_not_sent          = 2
        document_type_not_exist    = 3
        operation_no_authorization = 4
        parameter_error            = 5
        x_error                    = 6
        enqueue_error              = 7
        OTHERS                     = 8.

  ENDIF.
ENDFORM. " f_send_email
*&---------------------------------------------------------------------*
*& Form f_process_seqno1
*&---------------------------------------------------------------------*
* Process the first sequence and get the log table
*----------------------------------------------------------------------*
* -->FP_IT_SEQNO1 Sequence Internal table
* -->FP_IT_FUNCPROC Function module for processing
* -->FP_V_SEQNO Sequence number
* <--FP_IT_LOG Log table
*----------------------------------------------------------------------*
FORM f_process_seqno USING fp_it_seqno TYPE ty_t_seqno
                            fp_it_funcproc TYPE ty_t_funcproc
                            fp_v_seqno TYPE char1
                    CHANGING fp_v_error TYPE char1
                              fp_it_log TYPE ty_t_log.
*Local variable
  DATA: l_v_error TYPE char1,
        l_v_reprocess TYPE char1,
        l_v_cnt TYPE int1.
*Local work area
  DATA: l_wa_funcproc TYPE zap_funcproc.
*Local internal table
  DATA: l_it_log TYPE STANDARD TABLE OF ty_log.
* READ TABLE fp_it_funcproc INTO l_wa_funcproc
* WITH KEY fiseq = fp_v_seqno.
* IF sy-subrc = 0.
* REFRESH l_it_log.
* CALL FUNCTION l_wa_funcproc-funproc
* IMPORTING
* flg_error = l_v_error
* flg_reprocess = l_v_reprocess
* TABLES
* it_seq = fp_it_seqno
* it_log = l_it_log.
*
* IF sy-subrc = 0.
*
* IF l_v_reprocess = c_x.
* l_v_cnt = l_v_cnt + 1.
*
* IF l_v_cnt LE 3.
*
* CLEAR fp_v_error.
** REFRESH fp_it_log.
* PERFORM f_process_seqno USING fp_it_seqno
* fp_it_funcproc
* fp_v_seqno
* CHANGING fp_v_error
* fp_it_log.
* ENDIF.
*
* ENDIF.
*
* LOOP AT l_it_log INTO wa_log.
* APPEND wa_log TO fp_it_log.
* ENDLOOP.
*
* IF l_v_error = c_x.
* fp_v_error = l_v_error.
* ENDIF.
* ENDIF.
* ENDIF.
  CLEAR l_v_error.
  CLEAR wa_log.
  APPEND wa_log TO fp_it_log.
  REFRESH l_it_log.
ENDFORM. " f_process_seqno1
*&---------------------------------------------------------------------*
*& Form f_start_process_log
*&---------------------------------------------------------------------*
* Start processing Log
*----------------------------------------------------------------------*#
* <--FP_IT_LOG Log table
*----------------------------------------------------------------------*
FORM f_start_process_log CHANGING fp_it_log TYPE ty_t_log.
* Local variable
  DATA: l_v_log TYPE char512,
        l_v_datum TYPE char8,
        l_v_uzeit TYPE char6.
* Build the date format
  CONCATENATE sy-datum+4(2) sy-datum+6(2) sy-datum+2(2)
    INTO l_v_datum SEPARATED BY c_slash.
* Build the time format
  CONCATENATE sy-uzeit+0(2) sy-uzeit+2(2)
    INTO l_v_uzeit SEPARATED BY ':'.
  CONCATENATE text-016 l_v_uzeit l_v_datum text-017
    INTO l_v_log SEPARATED BY space.
  PERFORM f_log_data USING space
                            l_v_log
                            space
                    CHANGING fp_it_log.
  CLEAR wa_log.
  APPEND wa_log TO fp_it_log.
ENDFORM. " f_start_process_log
*&---------------------------------------------------------------------*
*& Form f_end_process_log
*&---------------------------------------------------------------------*
* Process log end
*----------------------------------------------------------------------*
* <--FP_IT_LOG Log internal table
*----------------------------------------------------------------------*
FORM f_end_process_log CHANGING fp_it_log TYPE ty_t_log.
  CLEAR wa_log.
  APPEND wa_log TO fp_it_log.
  PERFORM f_log_data USING space
                            text-018
                            space
                    CHANGING fp_it_log.
ENDFORM. " f_end_process_log
*&---------------------------------------------------------------------*
*& Form f_archive_file
*&---------------------------------------------------------------------*
* Archive the file in the archive directory
*----------------------------------------------------------------------*
* -->FP_IT_DATA Raw file data
* -->FP_V_ARCPATH Archive Path
* -->FP_V_FILEPATH1 original File path
*----------------------------------------------------------------------*
FORM f_archive_file USING fp_it_data TYPE ty_t_data
                          fp_v_arcpath TYPE char512
                          fp_v_filepath TYPE char512.
* Open the archive directory
  OPEN DATASET fp_v_arcpath
  FOR
  OUTPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc = 0.
* Put the file in the archive directory
    LOOP AT fp_it_data INTO wa_data.
      TRANSFER wa_data TO fp_v_arcpath.
    ENDLOOP.
    CLOSE DATASET fp_v_arcpath.
* Delete the file in the current location
    DELETE DATASET fp_v_filepath.
  ENDIF.
ENDFORM. " f_archive_file
*&---------------------------------------------------------------------*
*& Form f_update_archive_file
*&---------------------------------------------------------------------*
* Update the archive path in the internal table
*----------------------------------------------------------------------*
* -->FP_V_ARCPATH text
* -->FP_V_LOGPATH text
* <--FP_IT_PATH text
*----------------------------------------------------------------------*
FORM f_update_archive_file USING fp_v_funcproc TYPE rs38l_fnam
                                  fp_v_filepath TYPE zfilepath01
                                  fp_v_arcpath TYPE char512
                                  fp_v_logpath TYPE char512
                                  fp_v_location TYPE char40
                                  fp_wa_files TYPE ty_files
                          CHANGING fp_it_path TYPE ty_t_path.
* Update the path table
  wa_path-funcproc = fp_v_funcproc.
  wa_path-filepath = fp_v_filepath.
  wa_path-arcpath = fp_v_arcpath.
  wa_path-logpath = fp_v_logpath.
  wa_path-location = fp_v_location.
  wa_path-file = fp_wa_files.
  APPEND wa_path TO fp_it_path.
ENDFORM. " f_update_archive_file
*&---------------------------------------------------------------------*
*& Form f_read_archive_data
*&---------------------------------------------------------------------*
* Read the archive data
*----------------------------------------------------------------------*
* -->FP_WA_PATH_ARCPATH Archive path
* <--FP_IT_DATA Raw data
*----------------------------------------------------------------------*
FORM f_read_archive_data USING fp_wa_path_arcpath TYPE char512
                        CHANGING fp_it_data TYPE ty_t_data.
* Open the file and transfer the data into the internal table
  OPEN DATASET fp_wa_path_arcpath
    FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc = 0.
    DO.
      READ DATASET
      fp_wa_path_arcpath
      INTO wa_data .
      IF sy-subrc = 0.
        APPEND wa_data TO fp_it_data.
        CLEAR wa_data.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
  ENDIF.
ENDFORM. " f_read_archive_data
*&---------------------------------------------------------------------*
*& Form f_append_log
*&---------------------------------------------------------------------*
* Append the log in the respective log path
*----------------------------------------------------------------------*
* -->FP_IT_LOG Log internal table
* -->FP_WA_LOGPATH Log path
*----------------------------------------------------------------------*
FORM f_append_log USING fp_it_log TYPE ty_t_log
                        fp_wa_path-logpath TYPE char512.
* Open the log directory
  OPEN DATASET fp_wa_path-logpath
    FOR APPENDING
    IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc = 0.
* Put the file in the log directory
    LOOP AT fp_it_log INTO wa_log.
      TRANSFER wa_log TO fp_wa_path-logpath.
    ENDLOOP.
  ENDIF.
ENDFORM. " f_append_log
*&---------------------------------------------------------------------*
*& Form f_lock_filepath
*&---------------------------------------------------------------------*
* Lock the file path in the custom table
*----------------------------------------------------------------------*
* -->FP_WA_PATH_FILE Filepath
* <--FP_V_LOCK Lock key
*----------------------------------------------------------------------*
FORM f_lock_filepath USING fp_wa_path_file TYPE zfilepath01
                    CHANGING fp_v_lock TYPE char1.
*Local variable
  DATA: l_v_filepath TYPE zfilepath01.
*Local work area
  DATA: l_wa_fileproc TYPE zap_fileproc.
*Check the file is in the processing table
  SELECT filepath
    FROM zap_fileproc
    INTO l_v_filepath
    UP TO 1 ROWS
    WHERE filepath = fp_wa_path_file.
  ENDSELECT.
  IF sy-subrc <> 0.
    l_wa_fileproc-mandt = sy-mandt.
    l_wa_fileproc-filepath = fp_wa_path_file.
    MODIFY zap_fileproc FROM l_wa_fileproc.
    COMMIT WORK.
  ELSE.
    fp_v_lock = c_x.
  ENDIF.
ENDFORM. " f_lock_filepath>
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*&************************************************************************
* PROGRAM--INITIAL CREATION
* This report is run in the foreground to remove the product from the
* transportation lane once the products are obsoete. After this the
* program will also delete the products from the corresponding markets
* warehouses and production center locations.
************************************************************************
************************************************************************
* PROGRAM--Modification
*MOD-1771404
* Program name : ZAXXPR_DELETE_MASTER_DATA
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2008
* Author : Aveek Ghose.
* Description :
*
* This report is run in the foreground to remove the product from the
* transportation lane once the products are obsoete. After this the
* program will also delete the products from the corresponding markets
* warehouses and production center locations.
************************************************************************
************************************************************************
* MOD-1887120
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 10.03.2008
* Author : Aveek Ghose
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA.
************************************************************************
REPORT zaxxpr_delete_master_data MESSAGE-ID zaxxp_sign_fpv.
* Include for global data and constants declaration
* INCLUDE zaxxpr_delete_master_data_top.
* include ZAXXPR_DEL_MASTER_DATA_TOP.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DEL_MASTER_DATA_TOP
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_TOP
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--INITIAL CREATION
* Description XXXXXXXXXX :
*
* This is the Top include that contains the global data declarations
* and the selection screen of the main program
* ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
* PROGRAM--Modification: MOD-1771404
* Program name : ZAXXPR_DELETE_MASTER_DATA_TOP
* Reference program (copied from) : na.
* Development specification ID :
* Initial Change request number : PMI_SIGN_0002
* Author : Aveek Ghose..
* Description :
*
* This is the Top include that contains the global data declarations
* and the selection screen of the main program
* ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
**********************TYPES DECLARATION***************************
************************************************************************
TYPE-POOLS: slis...
* Types dclaration for view table data.
TYPES: BEGIN OF s_view_data,
pcode TYPE matnr, " Product
maktx TYPE char40, " Prod Desc
markt TYPE char4, " Market
martx TYPE char20, " Market Desc
pdctr TYPE char2, " Product Center
pctxt TYPE char20, " Description
whse1 TYPE char3, " Warehouse1
whse2 TYPE char3, " Warehouse2
prvar TYPE char18, " Product Variant
vartx TYPE char30, " Variant Description
status TYPE char1, " Status
END OF s_view_data,
t_view_data TYPE STANDARD TABLE OF s_view_data.
*MOD-1771404*****
TYPES: BEGIN OF l_view_data,
pcode TYPE matnr, " Product
markt TYPE char4, " Market
pdctr TYPE char2, " Product Center
whse1 TYPE char3, " Warehouse1
whse2 TYPE char3, " Warehouse2
prvar TYPE char18, " Product Variant
status TYPE char1, " Status
END OF l_view_data,
lv_view_data TYPE STANDARD TABLE OF s_view_data.
*MOD-1771404****
* Types declaration for F4 help Product data
TYPES: BEGIN OF s_f4_data_matnr,
pcode TYPE matnr, " Product
maktx TYPE maktx, " Description
END OF s_f4_data_matnr,
t_f4_data_matnr TYPE STANDARD TABLE OF s_f4_data_matnr.
* Types declaration for F4 help Location data
TYPES: BEGIN OF s_f4_data_loc,
locno TYPE /sapapo/locno, " Location
desc TYPE /sapapo/loc_descr40, " Description
pcode TYPE /sapapo/matnr, " Product
prvar TYPE /sapapo/matnr, " Product Variant
*MOD-012
pdctr TYPE char2, "Production Center
*MOD-012
vartx TYPE char30, " Variant Description
maktx TYPE char40, " Prod Desc
status TYPE char1, " Status
loctyp TYPE /sapapo/c_loctype, " Location Type
whse1 TYPE char3,
whse2 TYPE char3,
lv_exist TYPE i, " Variable
END OF s_f4_data_loc,
t_f4_data_loc TYPE STANDARD TABLE OF s_f4_data_loc.
* Types declaration for Warehouse description
TYPES: BEGIN OF s_whse,
whse TYPE char3, " Location
whdesc TYPE char30, " Description
END OF s_whse,
t_whse TYPE STANDARD TABLE OF s_whse.
* Types declaration for F4 help Location data
TYPES: BEGIN OF s_f4_loc,
locno TYPE /sapapo/locno, " Location
desc TYPE /sapapo/loc_descr40, " Description
END OF s_f4_loc,
t_f4_loc TYPE STANDARD TABLE OF s_f4_loc.
* Types declaration for SCREEN TABLE used in submit statement
TYPES: t_rsparams TYPE STANDARD TABLE OF rsparams.
* Types declaration for Transportation Lane data
TYPES: BEGIN OF s_tlane_data,
trpid TYPE /sapapo/tr_trpid, " Trip Id
locto TYPE /sapapo/locno, " Location to
matid TYPE matnr, " Material Id
locfr TYPE /sapapo/locno, " Location from
END OF s_tlane_data,
t_tlane_data TYPE STANDARD TABLE OF s_tlane_data.
* Types Declaration for Range Table
TYPES: BEGIN OF s_range_matid,
sign TYPE char1, " Sign
option TYPE char2, " Option
low TYPE /sapapo/matid, " Matid low
high TYPE /sapapo/matid, " Matid high
END OF s_range_matid,
t_matid_range TYPE STANDARD TABLE OF s_range_matid.
* Types Declaration for Range Table
TYPES: BEGIN OF s_range_locid,
sign TYPE char1, " Sign
option TYPE char2, " Option
low TYPE /sapapo/locid, " Locid low
high TYPE /sapapo/locid, " locid high
END OF s_range_locid,
t_locid_range TYPE STANDARD TABLE OF s_range_locid.
* Types Declaration for ALV Display
TYPES: BEGIN OF s_display,
prod TYPE /sapapo/matnr, " Product
p_des TYPE char40, " Product Desc
loc TYPE /sapapo/locno, " Location
l_des TYPE /sapapo/loc_descr40, " Location Desc
status TYPE char15, " Deletion status
light TYPE iconname, " Light Indicator
END OF s_display,
t_display TYPE STANDARD TABLE OF s_display.
TYPES: BEGIN OF s_displayprd,
prod TYPE /sapapo/matnr, " Product
p_des TYPE char40, " Product Desc
status TYPE char15, " Deletion status
light TYPE iconname, " Light Indicator
END OF s_displayprd,
t_displayprd TYPE STANDARD TABLE OF s_displayprd.
* Types Declaration for Product select options
TYPES: BEGIN OF s_range_product,
sign TYPE char1, " Sign
option TYPE char2, " Option
low TYPE matnr, " Product low
high TYPE matnr, " Product high
END OF s_range_product.
* Types Declaration for Location select option
TYPES: BEGIN OF s_range_location,
sign TYPE char1, " Sign
option TYPE char2, " Option
low TYPE /sapapo/locno, " Location low
high TYPE /sapapo/locno, " Location high
END OF s_range_location.
* Types Declaration for Matids for the Products
TYPES: BEGIN OF s_matid,
matid TYPE /sapapo/matid, " Matid
matnr TYPE matnr, " Product
END OF s_matid,
t_matid TYPE STANDARD TABLE OF s_matid.
* Types Declaration for Locid and Locations
TYPES: BEGIN OF s_locid,
locid TYPE /sapapo/locid, " Matid
locno TYPE /sapapo/locno, " Product
END OF s_locid,
t_locid TYPE STANDARD TABLE OF s_locid.
TYPES: BEGIN OF s_tlaneupd,
locfr TYPE /sapapo/locno,
locto TYPE /sapapo/locno,
matnr TYPE matnr,
matid TYPE /sapapo/matid,
msg TYPE /fre/bif_messagetext,
END OF s_tlaneupd,
t_tlaneupd TYPE STANDARD TABLE OF s_tlaneupd.
TYPES: BEGIN OF s_matloc,
matnr TYPE matnr,
locno TYPE /sapapo/locno,
END OF s_matloc,
t_matloc TYPE STANDARD TABLE OF s_matloc.
*MOD-1771404
TYPES: t_masterdata TYPE STANDARD TABLE OF zap_scmpf,
s_tship TYPE zap_tshpt,
t_tship TYPE STANDARD TABLE OF s_tship,
t_log_table TYPE STANDARD TABLE OF bal_s_msg,
t_log_location TYPE STANDARD TABLE OF bal_s_msg,
t_log_product TYPE STANDARD TABLE OF bal_s_msg,
t_log_locprd TYPE STANDARD TABLE OF bal_s_msg,
t_log_tlane TYPE STANDARD TABLE OF bal_s_msg.
TYPES: BEGIN OF s_location,
locno TYPE /sapapo/locno,
loctp TYPE /sapapo/c_loctype,
locct TYPE char1,
END OF s_location,
t_location TYPE STANDARD TABLE OF s_location,
BEGIN OF s_product,
matnr TYPE /sapapo/matnr,
matct TYPE char1,
END OF s_product,
t_product TYPE STANDARD TABLE OF s_product,
BEGIN OF s_tlane,
locfr TYPE /sapapo/locno,
locto TYPE /sapapo/locno,
matnr TYPE /sapapo/matnr,
ltpfr TYPE /sapapo/c_loctype,
ltpto TYPE /sapapo/c_loctype,
lidfr TYPE /sapapo/locid,
lidto TYPE /sapapo/locid,
matid TYPE /sapapo/matid,
datfr TYPE timestamp,
datto TYPE timestamp,
crtyp TYPE flag,
lv_exist TYPE i,
END OF s_tlane,
t_tlane TYPE STANDARD TABLE OF s_tlane,
BEGIN OF s_loc_extloc,
locno TYPE /sapapo/locno,
extloc TYPE /sapapo/loc_extlocno,
END OF s_loc_extloc,
t_loc_extloc TYPE STANDARD TABLE OF s_loc_extloc,
BEGIN OF s_prd_extprd,
matnr TYPE /sapapo/matnr,
extmat TYPE /sapapo/ext_matnr,
END OF s_prd_extprd,
t_prd_extprd TYPE STANDARD TABLE OF s_prd_extprd,
BEGIN OF s_locprd,
locno TYPE /sapapo/locno,
matnr TYPE /sapapo/matnr,
loctp TYPE /sapapo/c_loctype,
prctp TYPE /sapapo/beskz,
END OF s_locprd,
t_locprd TYPE STANDARD TABLE OF s_locprd.
DATA: t_sign TYPE STANDARD TABLE OF zap_sign.
DATA: t_sign1 TYPE STANDARD TABLE OF zap_sign.
DATA: it_zdu1002 TYPE STANDARD TABLE OF zdu1002.
DATA: it_zdm0705 TYPE STANDARD TABLE OF zdm0705.
*MOD-1771404
************************************************************************
******************CONSTANTS DECLARATION***************************
************************************************************************
CONSTANTS:
* Selection Screen field names
gc_prd_low TYPE dynfnam VALUE 'S_PRD-LOW',
gc_prd_high TYPE dynfnam VALUE 'S_PRD-HIGH',
gc_mkt_low TYPE dynfnam VALUE 'S_MKT-LOW',
gc_mkt_high TYPE dynfnam VALUE 'S_MKT-HIGH',
* Transaction Name and Program Names
gc_bp2 TYPE baltcode VALUE '/SAPAPO/BP2',
gc_slg1 TYPE baltcode VALUE 'SLG1',
gc_mode_e TYPE ctu_mode VALUE 'E',
gc_mode_n TYPE ctu_mode VALUE 'N',
gc_foreground TYPE char1 VALUE 'A',
gc_rscsel TYPE dynfnam VALUE 'RSCSEL-SLOW_I',
gc_l_brac TYPE char1 VALUE '(',
gc_r_brac TYPE char1 VALUE ')',
* Selection Screen Elements for /SAPAPO/RLCDELETE
gc_radio_btn TYPE rsscr_kind VALUE 'R',
gc_parameter TYPE rsscr_kind VALUE 'P',
gc_sel_optn TYPE rsscr_kind VALUE 'S',
gc_sign_i TYPE tvarv_sign VALUE 'I',
gc_option_eq TYPE tvarv_opti VALUE 'EQ',
gc_manual_sel TYPE rsscr_name VALUE 'SHUF_PA1',
gc_product TYPE rsscr_name VALUE 'SEL_PROD',
gc_location TYPE rsscr_name VALUE 'SEL_LOC',
gc_plan_vrsn TYPE rsscr_name VALUE 'P_VERSIO',
gc_fixed_ord TYPE rsscr_name VALUE 'P_FIXED',
gc_category TYPE rsscr_name VALUE 'RB_CAT',
gc_key_fig TYPE rsscr_name VALUE 'RB_KEYF',
gc_only_apo TYPE rsscr_name VALUE 'P_APO',
gc_test_run TYPE rsscr_name VALUE 'P_TEST',
gc_pvrsn_000 TYPE tvarv_val VALUE '000',
* Selection screen fields for /SAPAPO/DELETE_PP_ORDER
gc_pl_vrsn TYPE rsscr_name VALUE 'LV_SIMID',
gc_prod TYPE rsscr_name VALUE 'LT_MATAB',
gc_loc TYPE rsscr_name VALUE 'LT_LOTAB',
gc_plan_prod TYPE rsscr_name VALUE 'LV_EXT',
gc_contract TYPE rsscr_name VALUE 'LV_SUBCO',
gc_display_ord TYPE rsscr_name VALUE 'LV_LIST',
gc_delete_ord TYPE rsscr_name VALUE 'LV_DEL',
gc_send_delete TYPE rsscr_name VALUE 'LV_EVENT',
* ALV grid display fields
gc_pos_1 TYPE lvc_colpos VALUE '0001',
gc_pos_2 TYPE lvc_colpos VALUE '0002',
gc_pos_3 TYPE lvc_colpos VALUE '0003',
gc_pos_4 TYPE lvc_colpos VALUE '0004',
gc_pos_5 TYPE lvc_colpos VALUE '0005',
gc_pos_6 TYPE lvc_colpos VALUE '0006',
gc_prodct TYPE lvc_fname VALUE 'PROD',
gc_status TYPE lvc_fname VALUE 'STATUS',
gc_light TYPE lvc_fname VALUE 'LIGHT',
gc_prod_des TYPE lvc_fname VALUE 'P_DES',
gc_loctn TYPE lvc_fname VALUE 'LOC',
gc_loc_des TYPE lvc_fname VALUE 'L_DES',
gc_tabname TYPE lvc_tname VALUE 'LT_DISPLAY',
gc_left TYPE lvc_just VALUE 'L',
gc_center TYPE lvc_just VALUE 'C',
gc_len_prod TYPE lvc_outlen VALUE '18',
gc_len_p_des TYPE lvc_outlen VALUE '40',
gc_len_loc TYPE lvc_outlen VALUE '20',
gc_len_l_des TYPE lvc_outlen VALUE '30',
gc_len_stat TYPE lvc_outlen VALUE '20',
gc_len_lt TYPE lvc_outlen VALUE '5',
gc_set_pf TYPE slis_formname VALUE 'F_SET_PF',
gc_show_log TYPE slis_formname VALUE 'F_SHOW_LOG',
gc_lt_top TYPE slis_formname VALUE 'F_TOP_TL',
gc_tlupd_top TYPE slis_formname VALUE 'F_TOP_TLUPD',
gc_prd_top TYPE slis_formname VALUE 'F_TOP_PRD',
gc_td_log TYPE gui_code VALUE 'TD_LOG',
gc_tlane TYPE gui_code VALUE 'TLANE',
gs_type_s TYPE char1 VALUE 'S',
gs_type_h TYPE char1 VALUE 'H',
gs_type_a TYPE char1 VALUE 'A',
gc_save TYPE char1 VALUE 'A',
gc_red TYPE iconname VALUE ' (5C)',
gc_green TYPE iconname VALUE ' (5B)',
* ALV display for Tlane
gc_prod_tl TYPE lvc_fname VALUE 'MATID',
gc_locto TYPE lvc_fname VALUE 'LOCTO',
gc_locfr TYPE lvc_fname VALUE 'LOCFR',
gc_matnr TYPE lvc_fname VALUE 'MATNR',
gc_msg TYPE lvc_fname VALUE 'MSG',
gc_tl_tab TYPE lvc_tname VALUE 'GT_TLANE',
gc_tl_tabu TYPE lvc_tname VALUE 'IT_TLANEUPD',
* Others
gc_s TYPE ddbool_d VALUE 'S',
gc_inactive TYPE char1 VALUE 'I',
gc_set TYPE char1 VALUE 'X',
gc_set_val TYPE tvarv_val VALUE 'X',
gc_int_commit TYPE bapi10001commctrl VALUE 'E',
gc_bs_group TYPE /sapapo/logqs VALUE 'BSGAPO',
gc_error TYPE bapi_mtype VALUE 'E',
gc_abend TYPE bapi_mtype VALUE 'A',
gc_dot TYPE char1 VALUE '.',
gc_colon TYPE char1 VALUE ':',
gc_space TYPE char4 VALUE ' ',
gc_star TYPE char1 VALUE '*',
*MOD-1771404
gc_core TYPE char1 VALUE 'C',
gc_var TYPE char1 VALUE 'V',
* Message class name
gc_msgid TYPE symsgid VALUE 'ZAXXP_SIGN_FPV',
* Message types
gc_warn TYPE symsgty VALUE 'W',
gc_err TYPE symsgty VALUE 'E',
gc_succ TYPE symsgty VALUE 'S',
* Message nos
gc_msgno_021 TYPE symsgno VALUE '021',
gc_msgno_023 TYPE symsgno VALUE '023',
gc_msgno_024 TYPE symsgno VALUE '024',
gc_msgno_025 TYPE symsgno VALUE '025',
gc_msgno_026 TYPE symsgno VALUE '026',
gc_msgno_027 TYPE symsgno VALUE '027',
gc_msgno_028 TYPE symsgno VALUE '028',
gc_msgno_029 TYPE symsgno VALUE '029',
gc_msgno_030 TYPE symsgno VALUE '030',
gc_msgno_031 TYPE symsgno VALUE '031',
gc_msgno_032 TYPE symsgno VALUE '032',
gc_msgno_033 TYPE symsgno VALUE '033',
gc_msgno_034 TYPE symsgno VALUE '034',
gc_msgno_035 TYPE symsgno VALUE '035',
gc_msgno_037 TYPE symsgno VALUE '037',
gc_msgno_038 TYPE symsgno VALUE '038',
gc_msgno_039 TYPE symsgno VALUE '039',
gc_msgno_040 TYPE symsgno VALUE '040',
gc_msgno_041 TYPE symsgno VALUE '041',
gc_msgno_042 TYPE symsgno VALUE '042',
gc_msgno_043 TYPE symsgno VALUE '043',
gc_msgno_044 TYPE symsgno VALUE '044',
gc_msgno_050 TYPE symsgno VALUE '050',
gc_msgno_051 TYPE symsgno VALUE '051',
gc_msgno_052 TYPE symsgno VALUE '052',
gc_msgno_053 TYPE symsgno VALUE '053',
gc_msgno_054 TYPE symsgno VALUE '054',
gc_msgno_055 TYPE symsgno VALUE '055',
gc_msgno_061 TYPE symsgno VALUE '061',
gc_msgno_075 TYPE symsgno VALUE '075',
gc_msgno_076 TYPE symsgno VALUE '076',
gc_msgno_078 TYPE symsgno VALUE '078',
gc_msgno_087 TYPE symsgno VALUE '087',
gc_msgno_096 TYPE symsgno VALUE '096',
gc_msgno_98 TYPE symsgno VALUE '098',
gc_msgno_100 TYPE symsgno VALUE '100',
* Location types
gc_ltp_1001 TYPE /sapapo/c_loctype VALUE '1001', "For Prd centre
gc_ltp_1002 TYPE /sapapo/c_loctype VALUE '1002', "For Mkt/whouse
* Procurement types
gc_ptp_x TYPE /sapapo/beskz VALUE 'X', "For Prd centre
gc_ptp_f TYPE /sapapo/beskz VALUE 'F', "For Mkt/whouse
* Variant status
gc_stat_p TYPE char1 VALUE 'P',
gc_stat_a TYPE char1 VALUE 'A',
* Timestamps
gc_startti TYPE syuzeit VALUE '000000',
gc_endti TYPE syuzeit VALUE '235959',
* Default means of transport
gc_ttype_0001 TYPE /sapapo/tr_traty VALUE '0001',
* Business system grp
gc_bsgapo TYPE /sapapo/loc_logqs VALUE 'BSGAPO',
gc_utc TYPE timezone VALUE 'UTC'. "UTC time zome
*MOD-1771404
************************************************************************
**********************DATA DECLARATION****************************
************************************************************************
DATA: gv_flgstart TYPE flag,
gv_prd TYPE matnr,
gv_mkt TYPE /sapapo/locno,
gv_date_before TYPE sy-datum,
gv_time_before TYPE sy-uzeit,
gv_flg_error TYPE char1,
gv_msg_error TYPE char1. " Flag for Call Tr fail
* Internal table declarations
DATA: gt_f4_data_matnr TYPE STANDARD TABLE OF s_f4_data_matnr,
gt_f4_data_loc TYPE STANDARD TABLE OF s_f4_data_loc,
gt_matloc TYPE t_matloc,
gt_prod_data TYPE STANDARD TABLE OF s_f4_data_loc,
gt_product TYPE STANDARD TABLE OF bapi10001key,
*MOD-1771404
gt_new_product TYPE STANDARD TABLE OF s_product,
gt_location TYPE STANDARD TABLE OF s_location,
gt_new_tlane TYPE STANDARD TABLE OF s_tlane,
*MOD-1771404
gt_core_prod TYPE STANDARD TABLE OF s_f4_data_loc,
gt_prod_var TYPE STANDARD TABLE OF s_f4_data_loc,
gt_loc_data TYPE STANDARD TABLE OF s_f4_data_loc,
gt_tlane TYPE STANDARD TABLE OF s_tlane_data,
gt_matid_all TYPE STANDARD TABLE OF s_matid,
gt_view_data TYPE STANDARD TABLE OF s_view_data,
gt_bdc_tab TYPE STANDARD TABLE OF bdcdata,
gt_variant_table TYPE STANDARD TABLE OF rsparams,
gt_fcat TYPE STANDARD TABLE OF slis_fieldcat_alv,
gt_top_page TYPE slis_t_listheader,
gt_masterdata TYPE t_masterdata,
gt_tship TYPE t_tship,
gt_whse TYPE t_whse,
*MOD-1771404
gt_log_location TYPE t_log_location,
gt_log_product TYPE t_log_product,
gt_log_locprd TYPE t_log_locprd,
gt_log_tlane TYPE t_log_tlane.
DATA:
lt_tlane TYPE t_tlane,
lt_tlane_new TYPE t_tlane,
lt_tlane_dat TYPE t_tlane,
lt_location TYPE t_location,
lt_product TYPE t_product,
lt_prd_extprd TYPE t_prd_extprd,
lt_loc_extloc TYPE t_loc_extloc,
lt_locprd TYPE t_locprd.
DATA: l_wa_zdm0705 TYPE zdm0705.
DATA: l_wa_zdu1002 TYPE zdu1002.
DATA: lv_prvar TYPE char40.
*MOD-1771404
************************************************************************
*****************SELECTION SCREEN DEFINITION**********************
************************************************************************
* Block to enter the product and location data
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
* Option to delete master data
*************************************************************
*MOD-001
*************************************************************
* SELECTION-SCREEN: BEGIN OF LINE,
* COMMENT 5(27) text-033 FOR FIELD p_mdel.
* PARAMETERS: p_mdel RADIOBUTTON GROUP grp1 USER-COMMAND ucom
* DEFAULT 'X'.*PARAMETERS: p_mdel RADIOBUTTON GROUP grp1.*
* PARAMETERS p_mdel AS CHECKBOX DEFAULT 'X'.
*
* SELECTION-SCREEN END OF LINE.
* SELECTION-SCREEN SKIP.
* selection-screen: begin of line,
* comment (79) text-043.
* selection-screen end of line.
* selection-screen: begin of line,
* comment (79) text-044.
* selection-screen end of line.
* SELECTION-SCREEN SKIP.
**************************************************************
*MOD-001
*************************************************************
PARAMETERS: p_log TYPE logsys OBLIGATORY MEMORY ID mid.
SELECT-OPTIONS:
s_prd FOR gv_prd OBLIGATORY, " Product variant
s_mkt FOR gv_mkt OBLIGATORY. " Market
SELECTION-SCREEN: SKIP.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN POSITION 33.
PARAMETERS: p_test AS CHECKBOX DEFAULT 'X'.                 "#EC
** No Selection Text Maintained as we are displaying comment for it
SELECTION-SCREEN COMMENT 1(16) text-002 FOR FIELD p_test.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN PUSHBUTTON 5(10) text-042 USER-COMMAND wuf.
*MOD-1771404
* Option to update transportation lanes
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-032 FOR FIELD p_tlan.
PARAMETERS: p_tlan RADIOBUTTON GROUP grp1.
SELECTION-SCREEN END OF LINE.
* Sourcing for tlane
SELECTION-SCREEN SKIP.
PARAMETERS: p_src TYPE /sapapo/locno OBLIGATORY,
p_des TYPE /sapapo/locno OBLIGATORY,
p_prd TYPE matnr OBLIGATORY.
* Radio button for validity date change
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-028 FOR FIELD p_tdat.
PARAMETERS: p_tdat RADIOBUTTON GROUP grp USER-COMMAND ucom
MODIF ID mod DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
* Fields for dates
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 10(15) text-030 FOR FIELD p_frd.
PARAMETERS p_frd TYPE sydatum.
SELECTION-SCREEN: COMMENT 40(10) text-031 FOR FIELD p_tod.
PARAMETERS p_tod TYPE sydatum.
SELECTION-SCREEN: END OF LINE.
* Radio button for product removal
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-029 FOR FIELD p_tprd.
PARAMETERS: p_tprd RADIOBUTTON GROUP grp.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b1.
*MOD-1771404
************************************************************************
**********************INITIALIZATION******************************
************************************************************************
INITIALIZATION.
  CLEAR gv_flgstart.
* Gets Product and location data for populatiing the search helps
  PERFORM f_get_f4_data CHANGING gt_f4_data_matnr
  gt_f4_data_loc
  gt_view_data.
* Adjusts screen fields
  PERFORM f_adj_screen_flds.
***********************************************************************
****************SELECTION SCREEN EVENTS**************************
***********************************************************************
AT SELECTION-SCREEN ON s_prd.
* Validates product against the SIGN view
  PERFORM f_validate_product.

AT SELECTION-SCREEN ON s_mkt.
* Validate market against the SIGN view
  PERFORM f_validate_market.
* Custom search help for Product-Low
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-low.
  PERFORM f_f4_for_prodcentre USING gc_prd_low
  gt_f4_data_matnr.
* Custom search help for Product-High
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-high.
  PERFORM f_f4_for_prodcentre USING gc_prd_high
  gt_f4_data_matnr.
* Custom search help for Location-Low
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-low.
  PERFORM f_f4_for_market USING gc_mkt_low
  gt_f4_data_loc.
* Custom search help for Location-High
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-high.
  PERFORM f_f4_for_market USING gc_mkt_high
  gt_f4_data_loc.
* Adjusting the screen fields
AT SELECTION-SCREEN OUTPUT.
  CLEAR gv_flgstart.
*mod-099
  PERFORM f_clear_tables.
*mod-099
  PERFORM f_adj_screen_flds.

AT SELECTION-SCREEN.
  IF sy-ucomm = 'WUF'.
    REFRESH gt_matloc.
    PERFORM f_check_product_status USING gt_f4_data_loc
    CHANGING gt_prod_data
    gt_core_prod
    gt_prod_var
    gt_loc_data
    gt_matloc.
* MOD-1887120 *
    SORT gt_core_prod BY locno pcode.
    DELETE ADJACENT DUPLICATES FROM gt_core_prod
    COMPARING locno
    pcode.
    SORT gt_prod_var BY locno pcode prvar.
    DELETE ADJACENT DUPLICATES FROM gt_prod_var
    COMPARING locno
    pcode
    prvar.
* MOD-1887120 *
* Call the WUF transaction
    PERFORM launch_wuf USING gt_core_prod
    gt_prod_var.
  ENDIF.
***********************************************************************
***********************START OF SELECTION**************************
************************************************************************
START-OF-SELECTION.
  gv_flgstart = 'X'.
  IF p_mdel IS NOT INITIAL.
* Filter all the Products selected for deletion with status inactive
    REFRESH gt_matloc.
* wait up to 5 seconds.
    PERFORM f_check_product_status USING gt_f4_data_loc
  CHANGING gt_prod_data
  gt_core_prod
  gt_prod_var
  gt_loc_data
  gt_matloc.
* Set the Date and Time before deleting the Data.
    gv_date_before = sy-datum.
    gv_time_before = sy-uzeit.
    IF gt_core_prod IS INITIAL.
      MESSAGE i085.
    ENDIF.
    IF gt_prod_var IS INITIAL.
      MESSAGE i086.
      LEAVE LIST-PROCESSING.
    ENDIF.
    IF p_test IS INITIAL.
* Delete the Transactional data for the Product and Location Combination
* Using the call transaction /SAPAPO/BP2
*MOD-1887120SORT gt_core_prod BY locno pcode.
      DELETE ADJACENT DUPLICATES FROM gt_core_prod
      COMPARING locno
      pcode.
      SORT gt_prod_var BY locno pcode prvar.
      DELETE ADJACENT DUPLICATES FROM gt_prod_var
      COMPARING locno
      pcode
      prvar.
      SORT gt_loc_data BY locno.
      DELETE ADJACENT DUPLICATES FROM gt_loc_data
      COMPARING locno.
*MOD-1887120
      PERFORM f_delete_transactional_data USING gt_core_prod
      gt_prod_var
      gt_loc_data
      CHANGING gt_prod_data
      gt_bdc_tab.
* Delete the Transactional data created manually by submitting to the
* SAP standard program /SAPAPO/RLCDEL via selection screen variant.
      PERFORM f_delete_manual_created_data USING gt_core_prod
      gt_prod_var
      gt_loc_data
      CHANGING gt_variant_table.
* Delete the PPDS orders for selection screen Product and Location
* Done by submitting to SAP Standard program /SAPAPO/DELETE_PP_ORDER
* Via screen table.
      PERFORM f_delete_ppds_orders USING gt_core_prod
      gt_prod_var
      gt_loc_data
      CHANGING gt_variant_table.
    ENDIF.
* Delete Products from the Transportation Lanes for the Products and
* locations entered at the selection screen.
    PERFORM f_delete_product_from_tlane USING gt_core_prod
    gt_prod_var
    CHANGING gt_loc_data
    gt_matid_all
    gt_tlane.
* Run the Transportation Lane consistency check program
* PERFORM f_run_consistency_check.
* Set the Deletion flag for the Products selected for deletion.
    IF p_test IS INITIAL.
      PERFORM f_set_deletion_flag USING gt_core_prod
      gt_prod_var
      gt_loc_data
      gt_matloc.
    ENDIF.
* Display the ALV Report.
    PERFORM f_display_alv USING gt_prod_data
    gt_matloc
    CHANGING gt_fcat
    gt_matid_all.
  ENDIF.
* If the user chooses to update the transportation lane
**MOD-1771404
  IF p_tlan IS NOT INITIAL.
*MOD-1771404
* Retrieve the master data from teh SIGN portfolio meeting the user
* selection
    PERFORM f_retrieve_masterdata CHANGING gt_masterdata
    gt_tship
    gt_whse.
* Collect all possible transportation lanes
    PERFORM f_collect_tlanes USING gt_masterdata
    gt_tship
    gt_location
    gt_new_product
    CHANGING gt_new_tlane.
    IF gt_new_tlane IS INITIAL.
      EXIT.
    ENDIF.
* The Tlanes can be categorised into 3 categories:
* 1> To be created fresh
* 2> Exists, but new product need to be assigned
* 3> Only validity dates need to be changed
    PERFORM f_categorise_tlanes USING gt_new_tlane
    CHANGING lt_tlane_new
    lt_tlane_dat.
*MOD-1771404 changed by Aveek
    PERFORM f_update_tlane.
*MOD-1771404 changed by Aveek
* PERFORM f_update_tlane using gt_new_tlane.
*MOD-1771404
  ENDIF.
** Include for subroutine declaration
  INCLUDE zaxxpr_delete_master_data_f02.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F01
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--INITIAL CREATION
*
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA...
************************************************************************
************************************************************************
* PROGRAM--Modification MOD-1771404
************************************************************************
*
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2006
* Author : Aveek Ghose..
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
* MOD-1887120
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 10.03.2006
* Author : Aveek Ghose
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA
************************************************************************
*&---------------------------------------------------------------------*
*& Form f_validate_product
*&---------------------------------------------------------------------*
* Validates the product
*----------------------------------------------------------------------*
FORM f_validate_product.
* Local Date Declaration
  DATA: lv_prd TYPE matnr,
  lv_matnr TYPE /sapapo/matnr.
* Field symbols:
  FIELD-SYMBOLS:
  <lfs_prd> TYPE s_range_product.
  IF p_mdel IS INITIAL.
    EXIT.
  ENDIF.
* Both Product and Location can not be a range at the selection screen
  IF s_prd-low IS NOT INITIAL AND
  s_prd-high IS NOT INITIAL AND
  s_mkt-low IS NOT INITIAL AND
  s_mkt-high IS NOT INITIAL.
* Error Message: Both Product and Location can not be multiple
    MESSAGE e056.
  ENDIF.
* Check if both Product and location dont have multiple entries
  READ TABLE s_prd INDEX 2 TRANSPORTING NO FIELDS.
  IF sy-subrc = 0.
    READ TABLE s_mkt INDEX 2 TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
* Error Message: Both Product and Location can not be multiple
      MESSAGE e056.
    ENDIF.
  ENDIF.
* Check if user has entered * in the select option for location.
  READ TABLE s_prd ASSIGNING <lfs_prd> WITH KEY low = gc_star.
  IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
    MESSAGE e059.
  ELSE.
    READ TABLE s_prd ASSIGNING <lfs_prd> WITH KEY high = gc_star.
    IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
      MESSAGE e059.
    ENDIF.
  ENDIF.
  SELECT pcode UP TO 1 ROWS
  INTO lv_prd
  FROM zap_sign
  WHERE pcode IN s_prd.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_prd IS INITIAL.
* Product does not exist in APO
    MESSAGE e016.
  ENDIF.
ENDFORM. " f_validate_product
*&---------------------------------------------------------------------*
*& Form f_validate_market
*&---------------------------------------------------------------------*
* Validates the market
*----------------------------------------------------------------------*
FORM f_validate_market .
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
  lv_pdctr TYPE char2, " Production center
  lv_whse1 TYPE char3, " Warehouse1
  lv_whse2 TYPE char3. " Warehouse2
* Field Symbols:
  FIELD-SYMBOLS:
  <lfs_mkt> TYPE s_range_location.
  IF p_mdel IS INITIAL.
    EXIT.
  ENDIF.
* Both Product and Location can not be multiple at the selection screen
  IF s_prd-low IS NOT INITIAL AND
  s_prd-high IS NOT INITIAL AND
  s_mkt-low IS NOT INITIAL AND
  s_mkt-high IS NOT INITIAL.
* Error Message: Both Product and Location can not be multiple
    MESSAGE e056.
  ENDIF.
* Check if both Product and location dont have multiple entries
  READ TABLE s_prd INDEX 2 TRANSPORTING NO FIELDS.
  IF sy-subrc = 0.
    READ TABLE s_mkt INDEX 2 TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
* Error Message: Both Product and Location can not be multiple
      MESSAGE e056.
    ENDIF.
  ENDIF.
* Check if user has entered * in the select option for location.
  READ TABLE s_mkt ASSIGNING <lfs_mkt> WITH KEY low = gc_star.
  IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
    MESSAGE e060.
  ELSE.
    READ TABLE s_mkt ASSIGNING <lfs_mkt> WITH KEY high = gc_star.
    IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
      MESSAGE e060.
    ENDIF.
  ENDIF.
* Check if the Location is a market
  SELECT markt UP TO 1 ROWS
  INTO lv_mkt
  FROM zap_sign
  WHERE markt IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Production center
  SELECT pdctr UP TO 1 ROWS
  INTO lv_pdctr
  FROM zap_sign
  WHERE pdctr IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_pdctr IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Warehouse 1
  SELECT whse1 UP TO 1 ROWS
  INTO lv_whse1
  FROM zap_sign
  WHERE whse1 IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_whse1 IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Warehouse 2
  SELECT whse2 UP TO 1 ROWS
  INTO lv_whse2
  FROM zap_sign
  WHERE whse2 IN s_mkt.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_whse2 IS NOT INITIAL.
* Location does not exist in APO
    MESSAGE e017.
  ENDIF.
ENDFORM. " f_validate_market
*&---------------------------------------------------------------------*
*& Form F_GET_F4_DATA
*&---------------------------------------------------------------------*
* Get all the data required for custom F4 helps
*----------------------------------------------------------------------*
* <--ct_f4_data_matnr Itab with products
* <--ct_f4_data_loc Itab with F4 data for location
* <--ct_view_data Itab wiht data selected from view table
*----------------------------------------------------------------------*
FORM f_get_f4_data CHANGING ct_f4_data_matnr TYPE t_f4_data_matnr
ct_f4_data_loc TYPE t_f4_data_loc
ct_view_data TYPE t_view_data.
* Local Data Declaration
  DATA: ls_f4_data_matnr TYPE s_f4_data_matnr,
  ls_f4_data_loc TYPE s_f4_data_loc,
  lts_warehouse TYPE SORTED TABLE OF s_whse
  WITH NON-UNIQUE KEY whse,
  lt_whse TYPE STANDARD TABLE OF s_whse,
  ls_whse TYPE s_whse.
* Local Field-symbols
  FIELD-SYMBOLS: <lfs_view> TYPE s_view_data,
  <lfs_whse> TYPE s_whse.
  IF ( ct_f4_data_matnr IS INITIAL AND
  ct_f4_data_loc IS INITIAL ).
    SELECT pcode "Material Number
    maktx "Product Desc
    markt "Market
    pdctr "Production center
    pctxt "Prod center desc
    whse1 "Warehouse 1
    whse2 "Warehouse 2
    prvar "Prod Variant
    vartx "Variant Description
    INTO TABLE ct_view_data
    FROM zap_sign.
    IF sy-subrc <> 0.
* no sourcing info is maintained in the sign portfolio
      MESSAGE i005.
      LEAVE LIST-PROCESSING.
    ENDIF.
    LOOP AT ct_view_data ASSIGNING <lfs_view>.
      ls_whse-whse = <lfs_view>-whse1.
      APPEND ls_whse TO lt_whse.
      ls_whse-whse = <lfs_view>-whse2.
      APPEND ls_whse TO lt_whse.
    ENDLOOP.
    SORT lt_whse BY whse.
    DELETE ADJACENT DUPLICATES FROM lt_whse COMPARING whse.
    IF NOT lt_whse IS INITIAL.
* Get the Warehouse Descriptions
      SELECT whse "Warehouse Name
      whdesc "Warehouse Description
      FROM zap_whse
      INTO TABLE lts_warehouse
      FOR ALL ENTRIES IN lt_whse
      WHERE whse = lt_whse-whse.
    ENDIF.
* Populate the Internal Table with Product,Product Variant
* and Location. The location can be the Market,Production center,
* Warehouse 1,Warehouse 2. All the four locations will be clubbed
* under the field Location in the internal table
    LOOP AT ct_view_data ASSIGNING <lfs_view>.
      ls_f4_data_matnr-pcode = <lfs_view>-pcode.
      ls_f4_data_matnr-maktx = <lfs_view>-maktx.
      APPEND ls_f4_data_matnr TO ct_f4_data_matnr.
      CLEAR ls_f4_data_matnr.
      ls_f4_data_loc-pcode = <lfs_view>-pcode.
      ls_f4_data_loc-desc = <lfs_view>-martx.
      ls_f4_data_loc-locno = <lfs_view>-markt.
      ls_f4_data_loc-prvar = <lfs_view>-prvar.
      ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
      ls_f4_data_loc-vartx = <lfs_view>-vartx.
      ls_f4_data_loc-maktx = <lfs_view>-maktx.
      ls_f4_data_loc-status = <lfs_view>-status.
      APPEND ls_f4_data_loc TO ct_f4_data_loc.
      CLEAR ls_f4_data_loc.
      ls_f4_data_loc-pcode = <lfs_view>-pcode.
      ls_f4_data_loc-desc = <lfs_view>-pctxt.
      ls_f4_data_loc-locno = <lfs_view>-pdctr.
      ls_f4_data_loc-prvar = <lfs_view>-prvar.
      ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
      ls_f4_data_loc-vartx = <lfs_view>-vartx.
      ls_f4_data_loc-maktx = <lfs_view>-maktx.
      ls_f4_data_loc-status = <lfs_view>-status.
      APPEND ls_f4_data_loc TO ct_f4_data_loc.
      CLEAR ls_f4_data_loc.
* Read the description for Warehouse 1
      READ TABLE lts_warehouse ASSIGNING <lfs_whse> WITH KEY
      whse = <lfs_view>-whse1.
      IF sy-subrc = 0.
        ls_f4_data_loc-pcode = <lfs_view>-pcode.
        ls_f4_data_loc-desc = <lfs_whse>-whdesc.
        ls_f4_data_loc-locno = <lfs_view>-whse1.
        ls_f4_data_loc-prvar = <lfs_view>-prvar.
        ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
        ls_f4_data_loc-vartx = <lfs_view>-vartx.
        ls_f4_data_loc-maktx = <lfs_view>-maktx.
        ls_f4_data_loc-status = <lfs_view>-status.
        APPEND ls_f4_data_loc TO ct_f4_data_loc.
        CLEAR ls_f4_data_loc.
      ENDIF.
* Read the description for Warehouse 2
      READ TABLE lts_warehouse ASSIGNING <lfs_whse> WITH KEY
      whse = <lfs_view>-whse2.
      IF sy-subrc = 0.
        ls_f4_data_loc-pcode = <lfs_view>-pcode.
        ls_f4_data_loc-desc = <lfs_whse>-whdesc.
        ls_f4_data_loc-locno = <lfs_view>-whse2.
        ls_f4_data_loc-prvar = <lfs_view>-prvar.
        ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
        ls_f4_data_loc-vartx = <lfs_view>-vartx.
        ls_f4_data_loc-maktx = <lfs_view>-maktx.
        ls_f4_data_loc-status = <lfs_view>-status.
        APPEND ls_f4_data_loc TO ct_f4_data_loc.
        CLEAR ls_f4_data_loc.
      ENDIF.
    ENDLOOP.
    SORT ct_f4_data_matnr BY pcode.
    DELETE ADJACENT DUPLICATES FROM ct_f4_data_matnr COMPARING pcode.
  ENDIF.
ENDFORM. " F_GET_F4_DATA
*&---------------------------------------------------------------------*
*& Form f_f4_for_prodcentre
*&---------------------------------------------------------------------*
* Display F4 Help for Product
*----------------------------------------------------------------------*
* -->IV_PRD Field Name for Product
* -->IT_F4_DATA Itab with F4 data
*----------------------------------------------------------------------*
FORM f_f4_for_prodcentre USING iv_prd TYPE dynfnam
it_f4_data_matnr TYPE t_f4_data_matnr.
* Local Constants Declaration
  CONSTANTS: lc_pcode TYPE fieldname VALUE 'PCODE'.
  PERFORM f_launch_f4_window USING lc_pcode
  iv_prd
  text-016
  it_f4_data_matnr.
ENDFORM. " f4_for_prodcentre
*&---------------------------------------------------------------------*
*& Form f_f4_for_market
*&---------------------------------------------------------------------*
* Display F4 Help for locations
*----------------------------------------------------------------------*
* -->IV_LOC Field Name for Location
* -->IT_F4_DATA Itab with the F4 Data
*----------------------------------------------------------------------*
FORM f_f4_for_market USING iv_loc TYPE dynfnam
it_f4_data_loc TYPE t_f4_data_loc.
* Local Data Declaratino
  DATA: lt_loc_for_f4 TYPE STANDARD TABLE OF s_f4_loc.
* Local Constants Declaration
  CONSTANTS:
  lc_pdctr TYPE fieldname VALUE 'LOCNO'.
  lt_loc_for_f4 = it_f4_data_loc .
  SORT lt_loc_for_f4 BY locno.
  DELETE ADJACENT DUPLICATES FROM lt_loc_for_f4 COMPARING locno.
  DELETE lt_loc_for_f4 WHERE locno IS INITIAL.
  PERFORM f_launch_f4_window USING lc_pdctr
  iv_loc
  text-017
  lt_loc_for_f4.
ENDFORM. " f_f4_for_market
*&---------------------------------------------------------------------*
*& Form f_launch_f4_window
*&---------------------------------------------------------------------*
* Displays the Custom F4 Help for the field name passed
*----------------------------------------------------------------------*
* -->IV_RETFLD Return field
* -->IV_DYNFLD Screen field
* -->IV_WINTITLE Window title
* -->IT_VALUETAB Value table
*----------------------------------------------------------------------*
FORM f_launch_f4_window USING iv_retfld TYPE fieldname
iv_dynfld TYPE dynfnam
iv_wintitle TYPE c
it_valuetab TYPE STANDARD TABLE.
  DATA:
  lv_dynprog TYPE syrepid,
  lv_dynpnr TYPE sydynnr.
  lv_dynprog = sy-repid.
  lv_dynpnr = sy-dynnr.
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = iv_retfld
      dynpprog        = lv_dynprog
      dynpnr          = lv_dynpnr
      dynprofield     = iv_dynfld
      window_title    = iv_wintitle
      value_org       = gc_s
    TABLES
      value_tab       = it_valuetab
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.
  IF sy-subrc <> 0.
* Error while displaying search help
    MESSAGE i058.
    EXIT.
  ENDIF.
ENDFORM. " f_launch_f4_window
*&---------------------------------------------------------------------*
*& Form f_check_product_status
*&---------------------------------------------------------------------*
* Get all the Products with status Inactive selected for deletion
*----------------------------------------------------------------------*
* -->IT_F4_LOC_DATA Itab wiht the products to be deleted
* <--CT_PROD_DATA Itab with the Product and Locs to be deleted
* <--CT_CORE_PROD Itab with the core products to be deleted
* <--CT_PROD_VAR Itab with the product varients to be deleted
* <--CT_LOC_DATA Itab with the locations to be deleted
*----------------------------------------------------------------------
FORM f_check_product_status USING it_f4_data_loc TYPE t_f4_data_loc
CHANGING ct_prod_data TYPE t_f4_data_loc
ct_core_prod TYPE t_f4_data_loc
ct_prod_var TYPE t_f4_data_loc
ct_loc_data TYPE t_f4_data_loc
ct_matloc TYPE t_matloc.
*MOD-1771404
** Local Date Declaration
* DATA: lv_mkt TYPE char4, " Market
* lv_pdctr TYPE char2, " Production center
* lv_whse1 TYPE char3, " Warehouse1
* lv_whse2 TYPE char3. " Warehouse2
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
lv_pdctr TYPE char2, " Production center
lv_whse1 TYPE char3, " Warehouse1
lv_whse2 TYPE char3. " Warehouse2
  DATA: ls_sign TYPE zap_sign.
  DATA: lv_pdctr1 TYPE char2,
  lv_ltext1 TYPE char40.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid,
  lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  DATA:
  lv_matnr TYPE /sapapo/matnr,
  lt_matloc TYPE STANDARD TABLE OF /sapapo/v_matloc,
  lv_prod_data TYPE s_f4_data_loc,
  lv_tabix TYPE sy-tabix,
  lv_line TYPE i,
  lv_count TYPE i,
  lv_count1 TYPE i.
*MOD-011
  RANGES: lv_mkt1 FOR s_mkt.
  DATA: int TYPE i,
  rspar TYPE TABLE OF rsparams,
  wa_rspar LIKE LINE OF rspar,
  lv_indicator TYPE i.
  DATA: lv_indicator1 TYPE i VALUE 0.
  DATA: l_wa_prod TYPE s_f4_data_loc.
  DATA: it_itab_prod TYPE STANDARD TABLE OF s_f4_data_loc.
*MOD-011
  FIELD-SYMBOLS:
  <lfs_core_prod> TYPE s_f4_data_loc,
  <lfs_prod_data> TYPE s_f4_data_loc,
  <lfs_tsign> TYPE zap_sign.
* <lfs_zdm0705> TYPE ZDM0705.
  FIELD-SYMBOLS:
  <lfs_view> TYPE s_view_data,
*MOD-1771404
  <lfs_loc_data> TYPE s_f4_data_loc.
*MOD-1771404
  ct_prod_data = it_f4_data_loc.
***MOD-1771404
* Check if the Location is a market
  SELECT *
  INTO TABLE t_sign
  FROM zap_sign
  WHERE pcode IN s_prd AND
  ( markt IN s_mkt OR
  pdctr IN s_mkt OR
  whse1 IN s_mkt OR
  whse2 IN s_mkt ).
  IF sy-subrc <> 0.
    MESSAGE i004 WITH s_prd.
    LEAVE LIST-PROCESSING.
  ENDIF.
* DELETE t_sign WHERE status <> gc_inactive.
*MOD-1771404
  DELETE: ct_prod_data WHERE status <> gc_inactive,
  ct_prod_data WHERE NOT ( pcode IN s_prd ).
**MOD-1771404: This was commneted out for WHH.
* ct_prod_data WHERE NOT ( locno IN s_mkt ).
**MOD-1771404: This was commneted out for WHH.
***MOD-1771404: This was commneted out for WHH.
  IF NOT ct_prod_data[] IS INITIAL.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
        WITH KEY pcode = <lfs_prod_data>-pcode
        prvar = <lfs_prod_data>-prvar
* whse2 = <lfs_prod_data>-locno BINARY SEARCH.
*
        whse2 = s_mkt-low BINARY SEARCH.
* whse1 = <lfs_prod_data>-locno BINARY SEARCH.
        IF sy-subrc <> 0.
*MOD-1771404
*MOD-1887120
          READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY pcode = <lfs_prod_data>-pcode
          prvar = <lfs_prod_data>-prvar
* whse1 = <lfs_prod_data>-locno BINARY SEARCH.
          whse1 = s_mkt-low BINARY SEARCH.
          IF sy-subrc <> 0.
*MOD-1771404
            READ TABLE t_sign ASSIGNING <lfs_tsign>
            WITH KEY pcode = <lfs_prod_data>-pcode
            prvar = <lfs_prod_data>-prvar
            markt = s_mkt-low BINARY SEARCH.
*MOD-1887120
* markt = <lfs_prod_data>-locno binary search.
*
*MOD-1887120
            IF sy-subrc <> 0.
              READ TABLE t_sign ASSIGNING <lfs_tsign>
              WITH KEY pcode = <lfs_prod_data>-pcode
              prvar = <lfs_prod_data>-prvar
              pdctr = s_mkt-low BINARY SEARCH.
* pdctr IN s_mkt BINARY SEARCH.
* pdctr = <lfs_prod_data>-locno BINARY SEARCH.
              IF sy-subrc = 0.
                IF s_mkt-low = <lfs_prod_data>-locno AND
                s_mkt-low = <lfs_tsign>-pdctr.
                  <lfs_prod_data>-lv_exist = 0.
                  CONTINUE.
*MOD-1887120
* ELSE.
* <lfs_prod_data>-lv_exist = 1.
* Continue.
*MOD-1887120
                ENDIF.
*MOD-1887120
                IF s_mkt-low = <lfs_prod_data>-locno.
                  <lfs_prod_data>-lv_exist = 0.
                  CONTINUE.
*MOD-1887120
* ELSE.
* <lfs_prod_data>-lv_exist = 1.
* Continue.
*MOD-1887120
                ENDIF.
*MOD-1887120
              ELSE.
                <lfs_prod_data>-lv_exist = 1.
                CONTINUE.
              ENDIF.
            ELSE.
              READ TABLE t_sign ASSIGNING <lfs_tsign>
              WITH KEY pcode = <lfs_prod_data>-pcode
              prvar = <lfs_prod_data>-prvar
* markt = <lfs_prod_data>-locno BINARY SEARCH.
              markt = s_mkt-low BINARY SEARCH.
* pdctr = <lfs_prod_data>-locno BINARY SEARCH.
              IF sy-subrc = 0.
                <lfs_prod_data>-lv_exist = 0.
                CONTINUE.
              ELSE.
                <lfs_prod_data>-lv_exist = 1.
                CONTINUE.
              ENDIF.
            ENDIF.
          ELSE.
            <lfs_prod_data>-lv_exist = 0.
            <lfs_prod_data>-whse1 = <lfs_tsign>-whse1.
            EXIT.
          ENDIF.
        ELSE.
          <lfs_prod_data>-lv_exist = 0.
*MOD-1771404
          <lfs_prod_data>-whse1 = <lfs_tsign>-whse1.
          <lfs_prod_data>-whse2 = <lfs_tsign>-whse2.
*MOD-1771404
          EXIT.
        ENDIF.
      ENDLOOP.
    ENDLOOP.
  ENDIF.
*MOD-1887120
* DELETE: ct_prod_data WHERE lv_exist eq 1.
* DELETE ADJACENT DUPLICATES FROM CT_PROD_DATA COMPARING
* LOCNO
* PCODE
* PRVAR
* WHSE1
* WHSE2.
*
*MOD-1887120
*MOD-1887120
* To Handle Multiple production centres
  IF NOT ct_prod_data[] IS INITIAL.
    SELECT *
    INTO TABLE t_sign
    FROM zap_sign
    WHERE pcode IN s_prd AND
    pdctr IN s_mkt.
    IF sy-subrc <> 0.
* MESSAGE I004 with s_prd.
    ELSE.
* DELETE t_sign WHERE status <> gc_inactive.
      lv_tabix = 0.
      IF NOT t_sign IS INITIAL.
        LOOP AT t_sign ASSIGNING <lfs_tsign>.
          lv_tabix = lv_tabix + 1.
          LOOP AT s_mkt.
* Get the Warehouse Descriptions
* SELECT pdctr ltext up to 1 rows
* INTO (LV_PDCTR1, LV_LTEXT1)
* FROM ZDU1000
* WHERE PDCTR = S_MKT-LOW.
* ENDSELECT.
*MOD-1887120
            IF sy-subrc = 0.
*MOD-1887120
              l_wa_prod-locno = <lfs_tsign>-pdctr.
              l_wa_prod-desc = lv_ltext1.
              l_wa_prod-pcode = <lfs_tsign>-pcode.
              l_wa_prod-prvar = <lfs_tsign>-prvar.
              l_wa_prod-vartx = <lfs_tsign>-vartx.
              l_wa_prod-pdctr = s_mkt-low.
              l_wa_prod-maktx = <lfs_tsign>-maktx.
              l_wa_prod-whse1 = <lfs_tsign>-whse1.
              l_wa_prod-whse2 = <lfs_tsign>-whse2.
* l_wa_prod-status = <lfs_tsign>-status.
* l_wa_prod-loctyp = <lfs_tsign>-loctyp.
              APPEND l_wa_prod TO it_itab_prod.
              APPEND LINES OF it_itab_prod TO ct_prod_data.
*MOD-1887120
            ENDIF.
*MOD-1887120
          ENDLOOP.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.
*MOD-099
* DELETE: ct_prod_data WHERE lv_exist eq 1.
*MOD-099
  SORT ct_prod_data BY locno
  pcode
  prvar
  pdctr
  status
  whse1
  whse2.
  DELETE ADJACENT DUPLICATES FROM ct_prod_data COMPARING
  locno
  pcode
  prvar
  pdctr
  status
  whse1
  whse2.
*MOD-1887120IF ct_prod_data IS INITIAL.
* No Inactive products are present in the view table.
  MESSAGE i063.
  LEAVE LIST-PROCESSING.
ENDIF.
SELECT matnr locno INTO TABLE ct_matloc
FROM /sapapo/v_matloc
FOR ALL ENTRIES IN ct_prod_data
WHERE ( matnr = ct_prod_data-pcode OR
matnr = ct_prod_data-prvar ) AND
locno = ct_prod_data-locno.
*MOD-1887120* LOCNO IN S_MKT.
*MOD-1887120
IF sy-subrc <> 0.
  MESSAGE i066.
  IF gv_flgstart IS NOT INITIAL.
    LEAVE LIST-PROCESSING.
  ELSE.
    LEAVE TO SCREEN 1000.
  ENDIF.
ENDIF.
* Delete the entries that are not in data base.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE lt_matloc TRANSPORTING NO FIELDS
* WITH KEY matnr = <lfs_prod_data>-prvar
* locno = <lfs_prod_data>-locno.
* IF sy-subrc <> 0.
* CLEAR <lfs_prod_data>-prvar.
* ENDIF.
* ENDLOOP.
* DELETE ct_prod_data WHERE prvar IS INITIAL.
ct_core_prod = ct_prod_data.
ct_prod_var = ct_prod_data.
ct_loc_data = ct_prod_data.
SORT ct_core_prod BY pcode.
*MOD-1887120
* DELETE ADJACENT DUPLICATES FROM ct_core_prod COMPARING pcode.
*MOD-1887120
* DELETE ADJACENT DUPLICATES FROM CT_CORE_PROD COMPARING
* locno
* pcode
* prvar
* whse1
* whse2.
SORT ct_prod_data BY locno
pcode
prvar
pdctr
status
whse1
whse2.
DELETE ADJACENT DUPLICATES FROM ct_prod_data COMPARING
locno
pcode
prvar
pdctr
status
whse1
whse2.
*MOD-1887120
*MOD-1887120
DELETE ct_core_prod WHERE pcode IS INITIAL.
LOOP AT ct_core_prod ASSIGNING <lfs_core_prod>.
  LOOP AT gt_view_data TRANSPORTING NO FIELDS
  WHERE pcode = <lfs_core_prod>-pcode AND
  status <> 'I'.
    CLEAR <lfs_core_prod>-pcode.
    EXIT.
  ENDLOOP.
ENDLOOP.
DELETE ct_core_prod WHERE pcode IS INITIAL.
**MOD-1887120
* LV_TABIX = 0.
* IF not S_MKT IS INITIAL.
* LOOP AT S_MKT.
* LV_TABIX = LV_TABIX + 1.
* IF LV_TABIX > 1.
* delete: ct_core_prod WHERE NOT ( locno IN s_mkt ).
* ENDIF.
* ENDLOOP.
* ENDIF.
**MOD-1887120
*MOD-1771404
* SORT ct_prod_var BY prvar.
* DELETE ADJACENT DUPLICATES FROM ct_prod_var COMPARING prvar.
*MOD-1771404
LOOP AT ct_prod_var ASSIGNING <lfs_core_prod>.
  SELECT SINGLE matnr INTO lv_matnr
  FROM /sapapo/matkey
  WHERE matnr = <lfs_core_prod>-prvar.
  IF sy-subrc <> 0.
    CLEAR <lfs_core_prod>-prvar.
  ENDIF.
ENDLOOP.
DELETE ct_prod_var WHERE prvar IS INITIAL.
*MOD-1771404
* SORT ct_loc_data BY locno.
* DELETE ADJACENT DUPLICATES FROM ct_loc_data COMPARING locno.
*MOD-1771404
SORT ct_loc_data BY locno pcode prvar.
DELETE ADJACENT DUPLICATES FROM ct_loc_data COMPARING locno
pcode
prvar.
*MOD-1771404
DELETE ct_loc_data WHERE locno IS INITIAL.
*MOD-1771404
*MOD-1887120 for multiple locations
lv_indicator = 0.
LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE CT_MATLOC TRANSPORTING NO FIELDS
* WITH KEY MATNR = <lfs_core_prod>-PCODE
* locno = <lfs_core_prod>-locno binary search.
* if sy-subrc <> 0.
  CLEAR: lv_whse1.
  SELECT whse1 UP TO 1 ROWS
  INTO lv_whse1
  FROM zap_sign
  WHERE whse1 = <lfs_prod_data>-locno OR
  whse2 = <lfs_prod_data>-locno AND
  pcode IN s_prd.
  ENDSELECT.
  IF sy-subrc = 0.
    PERFORM get_desc_loc USING <lfs_prod_data>
    CHANGING lv_ware_desc.
    IF sy-subrc = 0.
      <lfs_prod_data>-desc = lv_ware_desc.
      lv_indicator = 1.
    ENDIF.
  ELSE.
    <lfs_prod_data>-lv_exist = 1.
  ENDIF.
* ENDIF.
ENDLOOP.
* DELETE CT_PROD_DATA WHERE LV_EXIST = 1.
*MOD-1887120
**MOD-1887120 for multiple locations
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE pdctr = <lfs_prod_data>-locno and
* pcode in s_prd.
* ENDSELECT.
* IF SY-SUBRC = 0.
* lv_indicator = 1.
* else.
* <lfs_prod_data>-LV_EXIST = 1.
* ENDIF.
* ENDLOOP.
**MOD-1887120
**MOD-1887120 for multiple locations
* LOOP AT ct_core_prod ASSIGNING <lfs_core_prod>.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE pdctr = <lfs_core_prod>-locno and
* pcode in s_prd.
* ENDSELECT.
* IF SY-SUBRC = 0.
* lv_indicator = 1.
* else.
* <lfs_core_prod>-LV_EXIST = 1.
* ENDIF.
* ENDLOOP.
**MOD-1887120
*MOD-1771404: 1 warehouse case
IF lv_indicator NE 1.
  CLEAR: lv_whse1.
  SELECT whse1 UP TO 1 ROWS
  INTO lv_whse1
  FROM zap_sign
  WHERE whse1 IN s_mkt OR
  whse2 IN s_mkt AND
  pcode IN s_prd.
  ENDSELECT.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* LOOP AT S_MKT.
    READ TABLE t_sign ASSIGNING <lfs_tsign>
    WITH KEY pcode = <lfs_prod_data>-pcode
    prvar = <lfs_prod_data>-prvar
    whse1 = s_mkt-low BINARY SEARCH.
**MOD-1887120
* whse1 = lv_whse1.
**MOD-1887120
    IF sy-subrc = 0.
      <lfs_prod_data>-locno = s_mkt-low.
*MOD-1887120
* <lfs_prod_data>-whse1 = s_mkt-low.
      <lfs_prod_data>-whse1 = lv_whse1.
*MOD-1887120
* PERFORM GET_DESC_LOC USING lv_prod_data
      PERFORM get_desc_loc USING <lfs_prod_data>
      CHANGING lv_ware_desc.
      IF sy-subrc = 0.
        <lfs_prod_data>-desc = lv_ware_desc.
      ENDIF.
    ENDIF.
  ENDLOOP.
*MOD-1887120
ENDIF.
*MOD-1887120
**MOD-1887120 Multiple Location Case.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE whse1 IN s_mkt or
* whse2 in s_mkt and
* pcode in s_prd.
* ENDSELECT.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* LOOP AT S_MKT.
* READ TABLE t_sign ASSIGNING <lfs_tsign>
* WITH KEY pcode = <lfs_prod_data>-pcode
* prvar = <lfs_prod_data>-prvar
* markt = s_mkt-low BINARY SEARCH.
* if sy-subrc = 0.
* <lfs_prod_data>-locno = s_mkt-low.
* <lfs_prod_data>-whse1 = lv_whse1.
* PERFORM GET_DESC_LOC USING lv_prod_data
* changing lv_ware_desc.
* if sy-subrc = 0.
* <lfs_prod_data>-desc = lv_ware_desc.
* endif.
* endif.
* ENDLOOP.
* ENDLOOP.
**MOD-1887120
REFRESH: ct_loc_data, ct_prod_var.
CLEAR: ct_loc_data, ct_prod_var.
ct_loc_data = ct_prod_data.
ct_prod_var = ct_prod_data.
SELECT matnr locno INTO TABLE ct_matloc
FROM /sapapo/v_matloc
FOR ALL ENTRIES IN ct_prod_data
WHERE ( matnr = ct_prod_data-pcode OR
matnr = ct_prod_data-prvar ) AND
*MOD-1887120
locno IN s_mkt.
* locno = ct_prod_data-locno.
*MOD-1887120
IF sy-subrc <> 0.
  MESSAGE i066.
  IF gv_flgstart IS NOT INITIAL.
    LEAVE LIST-PROCESSING.
  ELSE.
    LEAVE TO SCREEN 1000.
  ENDIF.
ENDIF.
*MOD-1771404
*MOD-1771404: 2 warehouse case.
*MOD-1771404
* Check if the Location is a Warehouse 1
*MOD-1887120
IF lv_indicator NE 1.
*MOD-1887120
  CLEAR: lv_whse1, lv_whse2.
  SELECT whse1 UP TO 1 ROWS
  INTO lv_whse1
  FROM zap_sign
  WHERE whse1 IN s_mkt OR
  whse2 IN s_mkt AND
  pcode IN s_prd.
  ENDSELECT.
* IF sy-subrc = 0 OR
* lv_whse1 IS NOT INITIAL.
* EXIT.
* ENDIF.
* Check if the Location is a Warehouse 2
  SELECT whse2 UP TO 1 ROWS
  INTO lv_whse2
  FROM zap_sign
  WHERE whse1 IN s_mkt OR
  whse2 IN s_mkt AND
  pcode IN s_prd.
  ENDSELECT.
* IF sy-subrc <> 0 OR
* lv_whse2 IS INITIAL.
** Location does not exist in APO
* MESSAGE e017.
* ENDIF.
*MOD-1887120
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
**MOD-011
* LOOP AT S_MKT.
**MOD-011
* READ TABLE t_sign ASSIGNING <lfs_tsign>
* WITH KEY pcode = <lfs_prod_data>-pcode
* prvar = <lfs_prod_data>-prvar
* whse2 = lv_whse2 BINARY SEARCH.
* if sy-subrc = 0.
* <lfs_prod_data>-locno = s_mkt-low.
* <lfs_prod_data>-whse1 = lv_whse1.
* ENDIF.
**MOD-011
* ENDLOOP.
**MOD-011
* ENDLOOP.
**MOD-1771404
*MOD-1887120
*MOD-1887120LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120LOOP AT S_MKT.
*MOD-1887120
  READ TABLE t_sign ASSIGNING <lfs_tsign>
  WITH KEY pcode = <lfs_prod_data>-pcode
  prvar = <lfs_prod_data>-prvar
* WHSE2 = lv_whse2 binary search.
  whse2 = s_mkt-low BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_prod_data>-locno = s_mkt-low.
    <lfs_prod_data>-whse1 = lv_whse1.
  ENDIF.
*MOD-1887120ENDLOOP.
*MOD-1887120ENDLOOP.
*MOD-1887120
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE t_sign ASSIGNING <lfs_tsign>
      WITH KEY pcode = <lfs_prod_data>-pcode
      prvar = <lfs_prod_data>-prvar
      whse2 = s_mkt-low BINARY SEARCH.
      IF sy-subrc = 0.
        <lfs_prod_data>-locno = s_mkt-low.
*MOD-1771404<lfs_prod_data>-whse1 = <lfs_prod_data>-whse1.
        <lfs_prod_data>-whse2 = s_mkt-low.
*MOD-1771404
        PERFORM get_desc_whse2 USING lv_prod_data
        CHANGING lv_ware_desc.
        IF sy-subrc = 0.
          <lfs_prod_data>-desc = lv_ware_desc.
        ENDIF.
      ENDIF.
*MOD-1887120
    ENDLOOP.
*MOD-1887120
  ENDLOOP.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE t_sign ASSIGNING <lfs_tsign>
      WITH KEY pcode = <lfs_prod_data>-pcode
      prvar = <lfs_prod_data>-prvar
      whse2 = s_mkt-low BINARY SEARCH.
* whse2 = lv_whse2 BINARY SEARCH.
      IF sy-subrc = 0.
        <lfs_prod_data>-locno = s_mkt-low.
*MOD-1771404
        <lfs_prod_data>-whse1 = <lfs_prod_data>-whse1.
        <lfs_prod_data>-whse2 = s_mkt-low.
* <lfs_prod_data>-whse2 = lv_whse2.
*MOD-1887120
*MOD-1771404
        PERFORM get_desc_whse2 USING lv_prod_data
        CHANGING lv_ware_desc.
        IF sy-subrc = 0.
          <lfs_prod_data>-desc = lv_ware_desc.
        ENDIF.
      ENDIF.
*MOD-1887120
    ENDLOOP.
*MOD-1887120
  ENDLOOP.
*MOD-1887120
ELSE.
  DELETE: ct_prod_data WHERE NOT ( locno IN s_mkt ).
ENDIF.
*MOD-1887120
REFRESH: ct_loc_data, ct_prod_var.
CLEAR: ct_loc_data, ct_prod_var.
ct_loc_data = ct_prod_data.
ct_prod_var = ct_prod_data.
*MOD-1887120
**MOD-1887120
LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
  <lfs_prod_data>-lv_exist = 0.
ENDLOOP.
FIELD-SYMBOLS:<lfs_matloc> TYPE s_matloc.
LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
  READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
  matnr = <lfs_prod_data>-pcode
  locno = <lfs_prod_data>-locno.
  IF sy-subrc NE 0.
    READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
    matnr = <lfs_prod_data>-prvar
    locno = <lfs_prod_data>-locno.
    IF sy-subrc NE 0.
      <lfs_prod_data>-lv_exist = 1.
    ENDIF.
  ENDIF.
ENDLOOP.
DELETE ct_prod_data WHERE lv_exist = 1.
REFRESH: ct_loc_data, ct_prod_var, ct_core_prod.
CLEAR: ct_loc_data, ct_prod_var, ct_core_prod.
ct_core_prod = ct_prod_data.
ct_prod_var = ct_prod_data.
ct_loc_data = ct_prod_data.
*MOD-1887120
**MOD-1887120 Changes made after release of transport on 28.03
DATA: l_wa_zdm0705 TYPE zdm0705.
DATA: l_wa_zdu1002 TYPE zdu1002.
DATA: lv_prvar TYPE char18.
REFRESH: it_zdm0705.
REFRESH: it_zdu1002.
LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
  SELECT * APPENDING TABLE it_zdm0705
  FROM zdm0705
  WHERE parpr = <lfs_prod_data>-pcode.
ENDLOOP.
SELECT * INTO TABLE it_zdu1002
FROM zdu1002
FOR ALL ENTRIES IN it_zdm0705
WHERE prvar = it_zdm0705-prvar.
SORT it_zdu1002 BY markt prvar.
DATA: lv_product TYPE char18.
CLEAR: l_wa_zdu1002.
CLEAR: l_wa_zdm0705.
SORT it_zdm0705 BY prvar parpr.
DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
LOOP AT it_zdm0705 INTO l_wa_zdm0705.
  READ TABLE it_zdu1002 INTO l_wa_zdu1002
  WITH KEY prvar = l_wa_zdm0705-prvar BINARY SEARCH.
  IF sy-subrc = 0.
    lv_count = lv_count + 1.
    IF l_wa_zdu1002-status NE 'I'.
      lv_indicator1 = 1.
    ENDIF.
  ENDIF.
ENDLOOP.
**MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* LOOP AT IT_ZDM0705 into l_wa_zdm0705
* WHERE prvar = l_wa_zdu1002-prvar.
** READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
** prvar = l_wa_zdu1002-prvar binary search.
** where dmoap NE 'Z'.
** IF L_WA_ZDU1002-PRVAR NE L_WA_ZDM0705-PRVAR.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
*
** else.
** l_wa_zdm0705-dmoap = 'Z'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
** exit.
* ENDLOOP.
* endif.
**MOD-1887120 Changes made after release of transport on 28.03
**MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* READ TABLE IT_ZDM0705 ASSIGNING <LFS_ZDM0705>
* WITH KEY PRVAR = L_WA_ZDU1002-PRVAR BINARY SEARCH.
** LOOP AT IT_ZDM0705 into l_wa_zdm0705
** WHERE prvar = l_wa_zdu1002-prvar.
* IF SY-SUBRC = 0.
* <lfs_zdm0705>-dmoap = 'X'.
** l_wa_zdm0705-dmoap = 'X'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* ENDIF.
* ENDLOOP.
** ENDLOOP.
* endif.
*MOD-1887120 Changes made after release of transport on 28.03
* LOOP AT it_zdu1002 into l_wa_zdu1002.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* prvar = l_wa_zdu1002-prvar binary search.
* if sy-subrc ne 0.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* endif.
* ENDLOOP.
*mod-099
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
** READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
** parpr = <lfs_prod_data>-pcode binary
*search.
** if sy-subrc ne 0.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* prvar = <lfs_prod_data>-prvar binary search.
* if sy-subrc ne 0.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* endif.
** endif.
* ENDLOOP.
**mod-099
*MOD-1887120 Changes made after release of transport on 28.03
IF lv_indicator1 = 1.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* parpr = <lfs_prod_data>-pcode
* dmoap = 'X'.
    IF sy-subrc = 0.
      DELETE ct_core_prod WHERE pcode = <lfs_prod_data>-pcode.
* Indicates that the core product should not be included in
* transportation lane and /sapapo/bp2 transaction.
      READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
      matnr = <lfs_prod_data>-pcode
      locno = <lfs_prod_data>-locno.
      IF sy-subrc NE 0.
        READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
        matnr = <lfs_prod_data>-prvar
        locno = <lfs_prod_data>-locno.
        IF sy-subrc NE 0.
          <lfs_prod_data>-lv_exist = 1.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDIF.
DELETE ct_prod_data WHERE lv_exist = 1.
*mod-099 changed on 04042006
*MOD-099 changed on 04042006
REFRESH: ct_loc_data, ct_prod_var. " ct_core_prod.
CLEAR: ct_loc_data, ct_prod_var. " ct_core_prod.
*MOD-1887120
* ct_core_prod = ct_prod_data.
*MOD-1887120
ct_prod_var = ct_prod_data.
ct_loc_data = ct_prod_data.
lv_indicator1 = 0.
*MOD-1887120 Changes made after release of transport on 28.03
*MOD-1771404
* SELECT matnr locno INTO TABLE ct_matloc
* FROM /sapapo/v_matloc
* FOR ALL ENTRIES IN ct_prod_data
* WHERE ( matnr = ct_prod_data-pcode OR
* matnr = ct_prod_data-prvar ) AND
**
* locno = ct_prod_data-locno.
**
* IF sy-subrc <> 0.
* MESSAGE i066.
* IF gv_flgstart IS NOT INITIAL.
* LEAVE LIST-PROCESSING.
* ELSE.
* LEAVE TO SCREEN 1000.
* ENDIF.
* ENDIF.
*MOD-1771404
ENDFORM. " f_check_product_status
*&---------------------------------------------------------------------*
*& Form f_delete_transactional_data
*&---------------------------------------------------------------------*
* Delete the transactional data for the Product and Location
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_PROD_DATA Itab wiht Product and Locations selected
* <--CT_BDC_DATA BDC TABLE
*----------------------------------------------------------------------*
FORM f_delete_transactional_data USING it_core_prod TYPE t_f4_data_loc
it_prod_var TYPE t_f4_data_loc
it_loc_data TYPE t_f4_data_loc
CHANGING ct_prod_data TYPE t_f4_data_loc
ct_bdc_data TYPE STANDARD TABLE.                            "#EC *
* CT_BDC_DATA is populated in the Perform populate_bdc_tab
* Local Data Declaration
  DATA: lv_ctu_params TYPE ctu_params,
  lv_fname TYPE dynfnam,
  lv_count(2) TYPE n,
  lt_msg_tab TYPE STANDARD TABLE OF bdcmsgcoll.
  DATA: lv_prev_pcode TYPE matnr.
  DATA: percentage_text TYPE c LENGTH 5.
  DATA: lv_tabix(20).
  DATA: lv_line TYPE i.
  DATA: lv_line1 TYPE i.
  DATA: lv_line2 TYPE i.
  DATA: lv_pcode TYPE /sapapo/matnr.
  DATA: s_product TYPE RANGE OF bapi10001apxprodlockey-product,
  s_product_wa LIKE LINE OF s_product.
* Field-symbols
  FIELD-SYMBOLS: <lfs_msg_ret> TYPE bdcmsgcoll,
  <lfs_data> TYPE s_f4_data_loc,
  <lfs_data1> TYPE s_f4_data_loc.
  REFRESH ct_bdc_data.
*MOD-1887120
  SORT it_core_prod BY pcode.
  DELETE ADJACENT DUPLICATES FROM it_core_prod
  COMPARING pcode.
  DESCRIBE TABLE it_core_prod LINES lv_line.
  SORT it_prod_var BY pcode prvar.
  DELETE ADJACENT DUPLICATES FROM it_prod_var
  COMPARING pcode prvar.
  DESCRIBE TABLE it_prod_var LINES lv_line1.
  lv_line2 = lv_line + lv_line1.
  IF lv_line2 <= 7.
*Version 0002
*MOD-1887120: Code that is fixed till TESTAVEEK27.
*Populate the general selection criteria on first screen
    PERFORM f_populate_bdc_tab USING:
    'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
    ' ' 'BDC_OKCODE' '=DUMMY',
    ' ' 'PLVERS' '000',
    ' ' 'MD_SYS' p_log,
    ' ' 'EXTRB' 'X',
    ' ' 'MO_DEL' 'X',
    ' ' 'SIM' space,
    ' ' 'P_LOGDEL' '5'.
*Populate the select options for Product
    PERFORM f_populate_bdc_tab USING:
    'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
    ' ' 'BDC_OKCODE' '=%011'.
*Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLALDB' '3000',
* ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
    ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_core_prod ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
      ' ' lv_fname <lfs_data>-pcode.
      CLEAR lv_fname.
    ENDLOOP.
    LOOP AT it_prod_var ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
      ' ' lv_fname <lfs_data>-prvar.
      CLEAR lv_fname.
    ENDLOOP.
*Populate the select options for the Locations
    PERFORM f_populate_bdc_tab USING:
    'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
    ' ' 'BDC_OKCODE' '=%012'.
*Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLALDB' '3000',
* ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
    ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_loc_data ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
      ' ' lv_fname <lfs_data>-locno.
      CLEAR lv_fname.
    ENDLOOP.
*Populate the objects to be deleted on first screen
    PERFORM f_populate_bdc_tab USING:
    'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
    ' ' 'BDC_OKCODE' '=ONLI',
    ' ' 'PLVERS' '000',
    ' ' 'MD_SYS' p_log,
    ' ' 'EXTRB' 'X',
    ' ' 'MO_DEL' 'X',
    ' ' 'MO_STAT' 'X',
    ' ' 'PO_DEL' 'X',
    ' ' 'SL_DEL' 'X',
    ' ' 'ST_DEL' 'X',
    ' ' 'PIR_DEL' 'X',
    ' ' 'SIM' space,
    ' ' ' ' ' '.
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLSPO1' '0500',
    ' ' 'BDC_OKCODE' '=OPT1'.
    CLEAR gv_msg_error.
    lv_ctu_params-nobinpt = space.
    lv_ctu_params-dismode = gc_mode_e.
    lv_ctu_params-updmode = 'S'.
    lv_ctu_params-racommit = 'X'.
* Call Transaction /SAPAPO/BP2 for deleting the Transactional data
    CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS FROM lv_ctu_params
    MESSAGES INTO lt_msg_tab.
*MOD-1887120: Code that is fixed till TESTAVEEK27.
*Version 0002
  ELSE.
**Version 0005
*CTS D7AK900899 New Version code fixed for testaveek28 case.
*MOD-1887120: Code that is changed after the release of transport.
* Populate the general selection criteria on first screen
    PERFORM f_populate_bdc_tab USING:
    'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
    ' ' 'BDC_OKCODE' '=%011',
* ' ' 'BDC_OKCODE' '=DUMMY',
    ' ' 'PLVERS' '000',
    ' ' 'MD_SYS' p_log,
    ' ' 'EXTRB' 'X',
    ' ' 'MO_DEL' 'X',
    ' ' 'SIM' space,
    ' ' 'P_LOGDEL' '5'.
    SORT it_core_prod BY pcode.
    DELETE ADJACENT DUPLICATES FROM it_core_prod
    COMPARING pcode.
    DESCRIBE TABLE it_core_prod LINES lv_line.
    CLEAR lv_count.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
*** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-pcode,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
* ENDLOOP.
*Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
    SORT it_prod_var BY pcode prvar.
    DELETE ADJACENT DUPLICATES FROM it_prod_var
    COMPARING pcode prvar.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000'.
*MOD-1887120:
    IF NOT it_core_prod[] IS INITIAL.
*MOD-1887120:
      LOOP AT it_core_prod ASSIGNING <lfs_data>.
        lv_pcode = <lfs_data>-pcode.
*lv_count = lv_count + 1.
        lv_count = 1.
        CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_line > 1.
        PERFORM f_populate_bdc_tab USING:
        'X' 'SAPLALDB' '3000',
        ' ' lv_fname <lfs_data>-pcode,
        ' ' 'BDC_OKCODE' '=LINS'.
        CLEAR lv_fname.
        DESCRIBE TABLE it_prod_var LINES lv_line.
* sort <lfs_data> by locno pcode prvar.
        LOOP AT it_prod_var ASSIGNING <lfs_data1> WHERE pcode = lv_pcode.
          lv_count = 1.
          CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* if lv_line > 1.
          PERFORM f_populate_bdc_tab USING:
          'X' 'SAPLALDB' '3000',
          ' ' lv_fname <lfs_data1>-prvar,
          ' ' 'BDC_OKCODE' '=LINS'.
* else.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
* endif.
          CLEAR lv_fname.
        ENDLOOP.
*MOD-1887120: Changed after 04.04.2006
        LOOP AT it_prod_var ASSIGNING <lfs_data1>.
          lv_count = 1.
          CONCATENATE gc_rscsel gc_l_brac lv_count
          gc_r_brac INTO lv_fname.
          PERFORM f_populate_bdc_tab USING:
          'X' 'SAPLALDB' '3000',
          ' ' lv_fname <lfs_data1>-prvar,
          ' ' 'BDC_OKCODE' '=LINS'.
          CLEAR lv_fname.
        ENDLOOP.
      ENDLOOP.
*MOD-1887120:
*MOD-1887120:
    ELSE.
*MOD-1887120:
      DESCRIBE TABLE it_prod_var LINES lv_line.
* sort <lfs_data> by locno pcode prvar.
      LOOP AT it_prod_var ASSIGNING <lfs_data1>. "where pcode=lv_pcode.
* lv_count = lv_count + 1.
        lv_count = 1.
        CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname
        .
* if lv_line > 1.
        PERFORM f_populate_bdc_tab USING:
        'X' 'SAPLALDB' '3000',
        ' ' lv_fname <lfs_data1>-prvar,
        ' ' 'BDC_OKCODE' '=LINS'.
* else.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
* endif.
        CLEAR lv_fname.
      ENDLOOP.
*MOD-1887120:
    ENDIF.
*MOD-1887120:
* Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
    ' ' 'BDC_OKCODE' '=ACPT'.
* Populate the select options for the Locations
    PERFORM f_populate_bdc_tab USING:
    'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_CURSOR' 'EXT_PRD-LOW',
    ' ' 'BDC_OKCODE' '=%012',
    ' ' 'PLVERS' '000',
    ' ' 'MD_SYS' p_log,
    ' ' 'EXTRB' 'X',
    ' ' 'MO_DEL' 'X',
    ' ' 'SIM' space,
    ' ' 'P_LOGDEL' '5'.
* Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLALDB' '3000',
* ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
    ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_loc_data ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
* lv_count = 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
      ' ' lv_fname <lfs_data>-locno.
* ' ' 'BDC_OKCODE' '=LINS'.
      CLEAR lv_fname.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-locno,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
    ENDLOOP.
* CLEAR lv_count.
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-locno,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
* ENDLOOP.
*
* Populate the objects to be deleted on first screen
    PERFORM f_populate_bdc_tab USING:
    'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
    ' ' 'BDC_OKCODE' '=DUMMY',
    ' ' 'PLVERS' '000',
    ' ' 'MD_SYS' p_log,
    ' ' 'EXTRB' 'X',
    ' ' 'MO_DEL' 'X',
    ' ' 'MO_STAT' 'X',
    ' ' 'PO_DEL' 'X',
    ' ' 'SL_DEL' 'X',
    ' ' 'ST_DEL' 'X',
    ' ' 'PIR_DEL' 'X',
    ' ' 'SIM' space,
    ' ' ' ' ' '.
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLSPO1' '0500',
    ' ' 'BDC_OKCODE' '=OPT1'.
    CLEAR gv_msg_error.
    lv_ctu_params-nobinpt = space.
* lv_ctu_params-dismode = gc_mode_e.
    lv_ctu_params-dismode = 'A'.
    lv_ctu_params-updmode = 'S'.
    lv_ctu_params-racommit = 'X'.
* Call Transaction /SAPAPO/BP2 for deleting the Transactional data
    CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS FROM lv_ctu_params
    MESSAGES INTO lt_msg_tab.
***MOD-1887120: Code that is changed after the release of transport.
*CTS D7AK900899 New Version
*Code fixed for testaveek28 case.
**Version 0005
  ENDIF.
*MOD-1887120
**Version 0002
**MOD-1887120: Code that is fixed till TESTAVEEK27.
**Populate the general selection criteria on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
**Populate the select options for Product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%011'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-pcode.
* CLEAR lv_fname.
*
* ENDLOOP.
*
* LOOP AT it_prod_var ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar.
* CLEAR lv_fname.
* ENDLOOP.
*
**Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%012'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
*
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
*
* CLEAR lv_fname.
*
* ENDLOOP.
*
**Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=ONLI',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
*
**MOD-1887120: Code that is fixed till TESTAVEEK27.
**Version 0002
***Version 0005
**CTS D7AK900899 New Version code fixed for testaveek28 case.
**MOD-1887120: Code that is changed after the release of transport.
** Populate the general selection criteria on first screen
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
* ' ' 'BDC_OKCODE' '=%011',
** ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
* sort it_core_prod by pcode.
* delete adjacent duplicates from it_core_prod
* comparing pcode.
* describe table it_core_prod lines lv_line.
*
* CLEAR lv_count.
** LOOP AT it_core_prod ASSIGNING <lfs_data>.
** lv_count = 1.
** CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
**** if lv_line > 1.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-pcode,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
** ENDLOOP.
*
**Populate the select options screen for product
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
*** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
** ' ' 'BDC_OKCODE' '=ACPT'.
*
* sort it_prod_var by pcode prvar.
* delete adjacent duplicates from it_prod_var
* comparing pcode prvar.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000'.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_pcode = <lfs_data>-pcode.
**lv_count = lv_count + 1.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
*** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-pcode,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
*
* describe table it_prod_var lines lv_line.
*
** sort <lfs_data> by locno pcode prvar.
* LOOP AT it_prod_var ASSIGNING <lfs_data1> where pcode = lv_pcode.
** lv_count = lv_count + 1.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data1>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
** else.
** PERFORM f_populate_bdc_tab USING:
** ' ' lv_fname <lfs_data>-prvar,
** ' ' 'BDC_OKCODE' '=LINS'.
** endif.
* CLEAR lv_fname.
* ENDLOOP.
* ENDLOOP.
*
** Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
*
** Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PRD-LOW',
* ' ' 'BDC_OKCODE' '=%012',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
*
** Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
*
* CLEAR lv_count.
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
** lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
** ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-locno,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
* ENDLOOP.
*
*
** CLEAR lv_count.
** LOOP AT it_loc_data ASSIGNING <lfs_data>.
** lv_count = 1.
** CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-locno,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
** ENDLOOP.
*
**
** Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
****MOD-1887120: Code that is changed after the release of transport.
**CTS D7AK900899 New Version
**Code fixed for testaveek28 case.
***Version 0005
**Version 0006 latest version.
**Code for looping at it_prod_var and doing the call transaction for
**each record
**CTS D7AK900899 New Version
*
* sort it_prod_var by pcode prvar.
* delete adjacent duplicates from it_prod_var
* comparing pcode prvar.
*
* clear: lv_prev_pcode.
*
* LOOP AT it_prod_var ASSIGNING <lfs_data>.
*
** Populate the general selection criteria on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
* ' ' 'BDC_OKCODE' '=%011',
** ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_prev_pcode ne <lfs_data>-pcode.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-pcode.
* CLEAR lv_fname.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar.
* CLEAR lv_fname.
* clear: lv_count.
*
*
**Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%012'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
* CLEAR lv_fname.
* clear: lv_count.
*
** Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
* CLEAR gv_msg_error.
* lv_ctu_params-nobinpt = space.
** lv_ctu_params-dismode = gc_mode_e.
* lv_ctu_params-dismode = 'A'.
* lv_ctu_params-updmode = 'S'.
* lv_ctu_params-racommit = 'X'.
*
** Call Transaction /SAPAPO/BP2 for deleting the Transactional data
* CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS
* FROM lv_ctu_params
* MESSAGES INTO lt_msg_tab.
*
* clear: ct_bdc_data.
*
* ENDLOOP.
***
******MOD-1887120: Code that is changed after the release of transport.
****CTS D7AK900899 New Version
****Version 0006
**MOD-1887120:
* CLEAR gv_msg_error.
* lv_ctu_params-nobinpt = space.
** lv_ctu_params-dismode = gc_mode_e.
* lv_ctu_params-dismode = 'A'.
* lv_ctu_params-updmode = 'S'.
* lv_ctu_params-racommit = 'X'.
*
** Call Transaction /SAPAPO/BP2 for deleting the Transactional data
* CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS FROM lv_ctu_params
* MESSAGES INTO lt_msg_tab.
**MOD-1887120:
** commit work and wait.
*MOD-1771404
  DO 10 TIMES.
    WAIT UP TO '0.1' SECONDS.
    percentage_text(3) = sy-index.
    percentage_text+3 = '%'.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        percentage = sy-index
        text       = percentage_text.
  ENDDO.
*MOD-1771404
  LOOP AT lt_msg_tab ASSIGNING <lfs_msg_ret>
  WHERE msgtyp = gc_error OR
  msgtyp = gc_abend.
    gv_msg_error = gc_set.
    EXIT.
  ENDLOOP.
ENDFORM. " f_delete_transactional_data
*&---------------------------------------------------------------------*
*& Form F_POPULATE_BDC_TAB
*&---------------------------------------------------------------------*
* Pupulates BDC Table with the parameters passed to the perform
*----------------------------------------------------------------------*
* -->IV_FLG Flag for New Screem
* -->IV_VAR1 Program Name / Field Name
* -->IV_VAR2 Screen Number / Field Value
*----------------------------------------------------------------------*
FORM f_populate_bdc_tab USING iv_flg TYPE any
iv_var1 TYPE any
iv_var2 TYPE any.
* Local Data Declaration
  DATA: ls_bdc_data TYPE bdcdata.
* Populate the BDC Table with the variables passed in it.
  CLEAR ls_bdc_data.
  IF iv_flg = gc_set. " = 'X'
    ls_bdc_data-program = iv_var1.
    ls_bdc_data-dynpro = iv_var2.
    ls_bdc_data-dynbegin = gc_set.
  ELSE.
    ls_bdc_data-fnam = iv_var1.
    ls_bdc_data-fval = iv_var2.
  ENDIF.
  APPEND ls_bdc_data TO gt_bdc_tab.
ENDFORM. " F_POPULATE_BDC_TAB
*&---------------------------------------------------------------------*
*& Form f_delete_manual_created_data
*&---------------------------------------------------------------------*
* Delete the Manually entered transactional data for prod and loc
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_VARIANT_TABLE Screen Varient Table.
*----------------------------------------------------------------------*
FORM f_delete_manual_created_data USING it_core_prod TYPE t_f4_data_loc
it_prod_var TYPE t_f4_data_loc
it_loc_data TYPE t_f4_data_loc
CHANGING ct_variant_table TYPE t_rsparams.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc.
* Populate the selection screen table
  PERFORM f_populate_screen_table USING:
* Field Name Type Sign Option Low Value
* Populate the Additional selection options
* Select the Manual selection button
  gc_manual_sel gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Populate the Planning Version
  gc_plan_vrsn gc_parameter gc_sign_i gc_option_eq gc_pvrsn_000 space.
* Populate the Core Products to be deleted
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_product gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-pcode space.
  ENDLOOP.
* Populate the Product Variants to be deleted
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_product gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-prvar space.
  ENDLOOP.
* Populate the Locations to be deleted.
  LOOP AT it_loc_data ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_location gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-locno space.
  ENDLOOP.
* Populate the Fixed Orders Checkbox
  PERFORM f_populate_screen_table USING:
  gc_fixed_ord gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Populate the Selection Objects
* Unselect the Key Figure Radio Button
  gc_key_fig gc_radio_btn gc_sign_i gc_option_eq space space,
* By Category Radio Button
  gc_category gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Populate the Settings
* Select Delete Transactional data only in APO Radio Button
  gc_only_apo gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Test Run check box.
  gc_test_run gc_parameter gc_sign_i gc_option_eq space space.
* Submit to SAP Standard report /SAPAPO/RLCDELETE
* For deleting manually entered data for the product and location
  SUBMIT /sapapo/rlcdelete WITH SELECTION-TABLE ct_variant_table
  AND RETURN.
ENDFORM. " f_delete_manual_created_data
*&---------------------------------------------------------------------*
*& Form F_POPULATE_SCREEN_TABLE
*&---------------------------------------------------------------------*
* Populate the screen table with the parameters passed
*----------------------------------------------------------------------*
* -->IV_NAME Screen field Name
* -->IV_TYPE Screen field type
* -->IV_SIGN Sign I / E
* -->IV_OPTION Option BT/EQ
* -->IV_LOW Lower value
* -->IV_HIGH Higher value
*----------------------------------------------------------------------*
FORM f_populate_screen_table USING iv_name TYPE rsscr_name
iv_type TYPE rsscr_kind
iv_sign TYPE tvarv_sign
iv_option TYPE tvarv_opti
iv_low TYPE any
iv_high TYPE tvarv_val.
* Local Data Declaration
  DATA: ls_variant TYPE rsparams.
* Populate the Screen Table
  ls_variant-selname = iv_name.
  ls_variant-kind = iv_type.
  ls_variant-sign = iv_sign.
  ls_variant-option = iv_option.
  ls_variant-low = iv_low.
  ls_variant-high = iv_high.
  APPEND ls_variant TO gt_variant_table.
  CLEAR ls_variant.
ENDFORM. " F_POPULATE_SCREEN_TABLE
*&---------------------------------------------------------------------*
*& Form f_delete_ppds_orders
*&---------------------------------------------------------------------*
* Delete the PPDS orders
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_VARIANT_TABLE Screen Varient Table.
*----------------------------------------------------------------------*
FORM f_delete_ppds_orders USING it_core_prod TYPE t_f4_data_loc
it_prod_var TYPE t_f4_data_loc
it_loc_data TYPE t_f4_data_loc
CHANGING ct_variant_table TYPE t_rsparams.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc.
  REFRESH ct_variant_table.
* Populate the selection screen variant
  PERFORM f_populate_screen_table USING:
* Field Name Type Sign Option Low Value
* Populate the Location Product details
  gc_pl_vrsn gc_parameter gc_sign_i gc_option_eq gc_pvrsn_000 space.
* Populate the Core Products to be deleted
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_prod gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-pcode space.
  ENDLOOP.
* Populate the Product Variants to be deleted
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_prod gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-prvar space.
  ENDLOOP.
* Populate the Locations to be deleted.
  LOOP AT it_loc_data ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_loc gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-locno space.
  ENDLOOP.
* Populate Product planned externally checkbox
  PERFORM f_populate_screen_table USING:
  gc_plan_prod gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Populate Subcontracting Planned Orders
  gc_contract gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Display order check box
  gc_display_ord gc_parameter gc_sign_i gc_option_eq space space,
* Check the Delete Order check box
  gc_delete_ord gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Send Deleted orders checkbox
  gc_send_delete gc_parameter gc_sign_i gc_option_eq space space.
* Submit standard program /SAPAPO/DELETE_PP_ORDER
* For deleting PPDS orders created for the product and location
  SUBMIT /sapapo/delete_pp_order WITH SELECTION-TABLE ct_variant_table
  AND RETURN.
ENDFORM. " f_delete_ppds_orders
*&---------------------------------------------------------------------*
*& Form f_delete_product_from_tlane
*&---------------------------------------------------------------------*
* Delete Products from the Transportation Lane
*&---------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* <--CT_LOC_DATA Itab with the locations to be deleted
* <--CT_MATID_ALL Itab with Matid for all the Products
* <--CT_TLANE Itab with transportation lane data
*----------------------------------------------------------------------*
FORM f_delete_product_from_tlane USING it_core_prod TYPE t_f4_data_loc
it_prod_var TYPE t_f4_data_loc
CHANGING ct_loc_data TYPE t_f4_data_loc
ct_matid_all TYPE t_matid
ct_tlane TYPE t_tlane_data.
  CONSTANTS:
  lc_stime TYPE t VALUE '000000',
  lc_etime TYPE t VALUE '235959',
  lc_utc TYPE timezone VALUE 'UTC'.
  DATA:
  lv_dummy TYPE /sapapo/c_loctype,
  lv_valfr TYPE timestamp,
  lv_valto TYPE timestamp,
  lv_matid TYPE /sapapo/matid,
  lv_srcid TYPE /sapapo/locid,
  lv_desid TYPE /sapapo/locid.
  DATA:
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_matid TYPE t_matid,
  ls_matid TYPE s_matid,
  lt_tlaneupd TYPE t_tlaneupd,
  ls_tlaneupd TYPE s_tlaneupd.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.
* Local Data Declaration
  DATA: lv_matid_low TYPE /sapapo/matid,
  lv_locid_low TYPE /sapapo/locid,
  lv_loctyp TYPE /sapapo/c_loctype,
  lr_matid TYPE RANGE OF /sapapo/matid,
  ls_trprod TYPE s_tlane_data,
  lr_locid TYPE RANGE OF /sapapo/locid,
  ls_locid TYPE s_locid,
  ls_loc_new TYPE /sapapo/loc,
  lt_locid_all TYPE STANDARD TABLE OF s_locid,
  lv_locid TYPE /sapapo/locid,
  lv_locno TYPE /sapapo/locno.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc,
  <lfs_locid> TYPE s_locid,
  <lfs_matid> TYPE s_matid.
*MOD-1771404
  FIELD-SYMBOLS:
* <lfs_zdm0705> TYPE zdm0705,
  <lfs_prod_data> TYPE s_f4_data_loc,
  <lfs_matloc> TYPE s_matloc.
*MOD-1771404
  TYPES: BEGIN OF s_view_data,
  pcode TYPE matnr, " Product
  maktx TYPE char40, " Prod Desc
  markt TYPE char4, " Market
  martx TYPE char20, " Market Desc
  pdctr TYPE char2, " Product Center
  pctxt TYPE char20, " Description
  whse1 TYPE char3, " Warehouse1
  whse2 TYPE char3, " Warehouse2
  prvar TYPE char18, " Product Variant
  vartx TYPE char30, " Variant Description
  status TYPE char1, " Status
  END OF s_view_data,
  lv_view_data TYPE STANDARD TABLE OF s_view_data.
* For Core Products
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
* Get the MATID for the Core Products
    PERFORM f_get_matid_for_prod USING <lfs_data>-pcode
    CHANGING lv_matid_low.
    ls_matid-matid = lv_matid_low.
    ls_matid-matnr = <lfs_data>-pcode.
    APPEND ls_matid TO ct_matid_all.
    CLEAR ls_matid.
* Populate the Range Table with the Core Products
    PERFORM f_populate_range_table_matid USING lv_matid_low
    CHANGING lr_matid.
    CLEAR lv_matid_low.
  ENDLOOP.
* For Product Variants
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
* Get the MATID for the Core Products
    PERFORM f_get_matid_for_prod USING <lfs_data>-prvar
    CHANGING lv_matid_low.
    ls_matid-matnr = <lfs_data>-prvar.
    ls_matid-matid = lv_matid_low.
    APPEND ls_matid TO ct_matid_all.
    CLEAR ls_matid.
* Populate the Range Table with the Core Products
    PERFORM f_populate_range_table_matid USING lv_matid_low
    CHANGING lr_matid.
    CLEAR lv_matid_low.
  ENDLOOP.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lr_matid COMPARING sign option
  low high.
*MOD-1771404
* For Locations
  LOOP AT ct_loc_data ASSIGNING <lfs_data>.
* Get the Locid for the Location
    PERFORM f_get_locid_for_loc USING <lfs_data>-locno
    CHANGING lv_locid_low
    lv_loctyp.
    <lfs_data>-loctyp = lv_loctyp.
    ls_locid-locno = <lfs_data>-locno.
    ls_locid-locid = lv_locid_low.
    APPEND ls_locid TO lt_locid_all.
    CLEAR ls_locid.
* Populate the Range Table with the Location
    PERFORM f_populate_range_table_locid USING lv_locid_low
    CHANGING lr_locid.
    CLEAR: lv_locid_low, lv_loctyp.
  ENDLOOP.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lr_locid COMPARING sign option
  low high.
*MOD-1771404
*MOD-1771404
** Get all the transportation lanes for the above Product and Locations
* SELECT *
* FROM /sapapo/trprod
* INTO TABLE lt_trprod
* WHERE matid IN lr_matid AND
* ( locfr IN lr_locid OR
* locto IN lr_locid ).
* IF sy-subrc <> 0.
** No Transportation lane selected for deletion
* MESSAGE i047.
** EXIT.
* ENDIF.
*MOD-1771404
* Get all the transportation lanes for the above Product and Locations
  SELECT *
  FROM /sapapo/trprod
  INTO TABLE lt_trprod
  WHERE matid IN lr_matid AND
  ( locfr IN lr_locid OR
  locto IN lr_locid ).
  IF sy-subrc <> 0.
* No Transportation lane selected for deletion
    MESSAGE i047.
* EXIT.
  ENDIF.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lt_locid_all COMPARING locid locno.
*MOD-1771404
  LOOP AT lt_trprod ASSIGNING <lfs_trprod>.
    READ TABLE lt_locid_all ASSIGNING <lfs_locid> WITH KEY
    locid = <lfs_trprod>-locto.
    IF sy-subrc = 0.
      ls_trprod-locto = <lfs_locid>-locno.
    ENDIF.
    CLEAR ls_loc_new.
    CALL FUNCTION '/SAPAPO/CMDS_LOC_SINGLE'
      EXPORTING
        iv_locid          = <lfs_trprod>-locfr
      IMPORTING
        es_loc            = ls_loc_new
      EXCEPTIONS
        empty_locid       = 1
        no_location_found = 2
        OTHERS            = 3.
    IF sy-subrc = 0.
      ls_trprod-locfr = ls_loc_new-locno.
    ENDIF.
*MOD-1771404
    CLEAR: lv_locid, lv_locno.
    SELECT SINGLE locid locno
    FROM /sapapo/loc
    INTO (lv_locid, lv_locno)
    WHERE locid = <lfs_trprod>-locto.
    IF sy-subrc = 0.
      IF ls_trprod-locto IS INITIAL.
        ls_trprod-locto = lv_locno.
      ENDIF.
    ENDIF.
*MOD-1771404
    READ TABLE ct_matid_all ASSIGNING <lfs_matid> WITH KEY
    matid = <lfs_trprod>-matid.
    IF sy-subrc = 0.
      ls_trprod-matid = <lfs_matid>-matnr.
    ENDIF.
    APPEND ls_trprod TO ct_tlane.
    CLEAR ls_trprod.
  ENDLOOP.
**MOD-1887120 Changes made after release of transport on 28.03
  DATA: l_wa_zdm0705 TYPE zdm0705.
  DATA: l_wa_zdu1002 TYPE zdu1002.
  DATA: lv_prvar TYPE char18.
  DATA: lv_indicator1 TYPE i.
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
**MOD-1887120
  DATA: lv_product TYPE char18.
* clear: l_wa_zdu1002.
* clear: l_wa_zdm0705.
* SORT it_zdm0705 BY prvar parpr.
** DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* LOOP AT IT_ZDM0705 INTO L_WA_ZDM0705.
* READ TABLE IT_ZDU1002 INTO L_WA_ZDU1002
* WITH KEY PRVAR = L_WA_ZDM0705-PRVAR BINARY SEARCH.
* IF SY-SUBRC = 0.
* IF L_WA_ZDU1002-STATUS NE 'I'.
* lv_indicator1 = 1.
* ENDIF.
* ENDIF.
* ENDLOOP.
***MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* READ TABLE IT_ZDM0705 ASSIGNING <LFS_ZDM0705>
* WITH KEY PRVAR = L_WA_ZDU1002-PRVAR BINARY SEARCH.
** LOOP AT IT_ZDM0705 into l_wa_zdm0705
** WHERE prvar = l_wa_zdu1002-prvar.
* IF SY-SUBRC = 0.
* <lfs_zdm0705>-dmoap = 'X'.
** l_wa_zdm0705-dmoap = 'X'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* ENDIF.
* ENDLOOP.
** ENDLOOP.
* endif.
*
**MOD-1887120 Changes made after release of transport on 28.03
  IF lv_indicator1 = 1.
    LOOP AT it_prod_var ASSIGNING <lfs_prod_data>.
      READ TABLE it_zdm0705 INTO l_wa_zdm0705 WITH KEY
      parpr = <lfs_prod_data>-pcode
      dmoap = 'X'.
      IF sy-subrc = 0.
        DELETE it_core_prod WHERE pcode = <lfs_prod_data>-pcode.
* Indicates that the core product should not be included in
* transportation lane and /sapapo/bp2 transaction.
        READ TABLE gt_matloc ASSIGNING <lfs_matloc> WITH KEY
        matnr = <lfs_prod_data>-pcode
        locno = <lfs_prod_data>-locno.
        IF sy-subrc NE 0.
          READ TABLE gt_matloc ASSIGNING <lfs_matloc> WITH KEY
          matnr = <lfs_prod_data>-prvar
          locno = <lfs_prod_data>-locno.
          IF sy-subrc NE 0.
            <lfs_prod_data>-lv_exist = 1.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.
  DELETE it_prod_var WHERE lv_exist = 1.
*MOD-1887120 Changes made after release of transport on 28.03
  LOOP AT ct_tlane INTO ls_trprod.
    READ TABLE it_zdm0705 INTO l_wa_zdm0705 WITH KEY
    prvar = ls_trprod-matid
    dmoap = 'X'.
    IF sy-subrc = 0.
      ls_trprod-matid = 'X'.
      MODIFY ct_tlane FROM ls_trprod TRANSPORTING matid.
      COMMIT WORK AND WAIT.
    ELSE.
      READ TABLE it_zdm0705 INTO l_wa_zdm0705 WITH KEY
      parpr = ls_trprod-matid
      dmoap = 'X'.
      IF sy-subrc = 0.
        ls_trprod-matid = 'X'.
        MODIFY ct_tlane FROM ls_trprod TRANSPORTING matid.
        COMMIT WORK AND WAIT.
      ENDIF.
    ENDIF.
  ENDLOOP.
  DELETE ct_tlane WHERE matid = 'X'.
**MOD-1887120 Changes made after release of transport on 28.03
  IF p_test IS NOT INITIAL.
    EXIT.
  ENDIF.
*MOD-1771404
* Delete SAP Table /SAPAPO/TRPROD
  DELETE /sapapo/trprod FROM TABLE lt_trprod.
  IF sy-subrc = 0.
    COMMIT WORK.
  ELSE.
    ROLLBACK WORK.
  ENDIF.
*MOD-1771404
ENDFORM. " f_delete_product_from_tlane
*&---------------------------------------------------------------------*
*& Form f_get_matid_for_prod
*&---------------------------------------------------------------------*
* Get the MATID for the material passed
*----------------------------------------------------------------------*
* -->IV_PRD Product (Material Number)
* <--CV_MATID MATID for the above material no
*----------------------------------------------------------------------*
FORM f_get_matid_for_prod USING iv_prd TYPE any
CHANGING cv_matid TYPE /sapapo/matid.
* Local Data Declaration
  DATA: lv_matnr TYPE /sapapo/matnr.
  CHECK NOT iv_prd IS INITIAL.
  CLEAR lv_matnr.
  lv_matnr = iv_prd.
  CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
    EXPORTING
      iv_matnr        = lv_matnr
    IMPORTING
      ev_matid        = cv_matid
    EXCEPTIONS
      matid_not_found = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    CLEAR cv_matid.
  ENDIF.
ENDFORM. " f_get_matid_for_prod
*&---------------------------------------------------------------------*
*& Form f_get_locid_for_loc
*&---------------------------------------------------------------------*
* Get the LOCID for the Location passed
*----------------------------------------------------------------------*
* -->IV_MKT Location Number
* <--CV_LOCID LOCID for the above Location
* <--CV_LOCTYP Location Type
*----------------------------------------------------------------------*
FORM f_get_locid_for_loc USING iv_mkt TYPE /sapapo/locno
CHANGING cv_locid_low TYPE /sapapo/locid
cv_loctyp TYPE /sapapo/c_loctype.
* Local Data Declaration
  DATA: ls_loc TYPE /sapapo/loc.
  CHECK NOT iv_mkt IS INITIAL.
  CLEAR ls_loc.
  CALL FUNCTION '/SAPAPO/LOC_LOCNO_GET_LOCID'
  EXPORTING
  iv_locno = iv_mkt
  IMPORTING
* ev_locid = cv_locid_low
  es_location = ls_loc
  EXCEPTIONS
  location_not_found = 1
  not_qualified = 2
  OTHERS = 3.
  IF sy-subrc <> 0.
    CLEAR cv_locid_low.
  ELSE.
    cv_loctyp = ls_loc-loctype.
    cv_locid_low = ls_loc-locid.
  ENDIF.
ENDFORM. " f_get_locid_for_loc
*&---------------------------------------------------------------------*
*& Form f_populate_range_table_matid
*&---------------------------------------------------------------------*
* Populate the Range thable wiht the values passed
*----------------------------------------------------------------------*
* -->IV_VALUE_LOW Lower Value of the Range
* <--ES_RANGE Structure of type Range
* <--CT_RANGE_TAB Table of type Range
*----------------------------------------------------------------------*
FORM f_populate_range_table_matid USING iv_value_low TYPE any
CHANGING ct_range_tab TYPE t_matid_range.
* Local Data Declaration
  DATA: ls_locid TYPE s_range_locid.
  ls_locid-sign = gc_sign_i.
  ls_locid-option = gc_option_eq.
  ls_locid-low = iv_value_low.
  APPEND ls_locid TO ct_range_tab.
  CLEAR ls_locid.
ENDFORM. " f_populate_range_table_matid
*&---------------------------------------------------------------------*
*& Form f_set_deletion_flag
*&---------------------------------------------------------------------*
* Set the deletion flag for the Products to be deleted
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the location details
*----------------------------------------------------------------------*
FORM f_set_deletion_flag USING it_core_prod TYPE t_f4_data_loc
it_prod_var TYPE t_f4_data_loc
it_loc_data TYPE t_f4_data_loc
ct_matloc TYPE t_matloc.
* Local Data Declaration
  DATA: lv_own_sys TYPE bapiapologsys,
  lv_matnr TYPE /sapapo/matnr,
  lt_product TYPE STANDARD TABLE OF bapi10001key,
  ls_product TYPE bapi10001key,
  ls_prod_key TYPE bapi_prodloc_key,
  lt_prod_key TYPE STANDARD TABLE OF bapi_prodloc_key,
  lt_matloc TYPE t_matloc,
  lt_return TYPE STANDARD TABLE OF bapiret2.
* Field Symbols
  FIELD-SYMBOLS: <lfs_mat> TYPE s_f4_data_loc,
  <lfs_product> TYPE bapi10001key,
  <lfs_loc> TYPE s_f4_data_loc,
  <lfs_return> TYPE bapiret2.
*MOD-1771404
*MOD-1887120
  SORT it_prod_var BY locno
  pcode
  prvar.
  DELETE ADJACENT DUPLICATES FROM it_prod_var COMPARING locno
  pcode
  prvar.
  SORT it_loc_data BY locno
  pcode
  prvar
  loctyp
  whse1.
  DELETE ADJACENT DUPLICATES FROM it_loc_data COMPARING locno
  pcode
  prvar
  loctyp
  whse1.
*MOD-1771404
* Populate the Core Product
  LOOP AT it_core_prod ASSIGNING <lfs_mat>.
    LOOP AT it_loc_data ASSIGNING <lfs_loc>.
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-pcode
      locno = <lfs_loc>-locno.
      IF sy-subrc = 0.
        ls_prod_key-loctype = <lfs_loc>-loctyp.
        ls_prod_key-product = <lfs_mat>-pcode.
        ls_prod_key-location = <lfs_loc>-locno.
        APPEND ls_prod_key TO lt_prod_key.
        CLEAR ls_prod_key.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
*MOD-1887120
  SORT lt_prod_key BY product location loctype.
  DELETE ADJACENT DUPLICATES FROM lt_prod_key
  COMPARING product
  location
  loctype.
*MOD-1887120
* Populate the Product Variant
  LOOP AT it_prod_var ASSIGNING <lfs_mat>.
    LOOP AT it_loc_data ASSIGNING <lfs_loc>.
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-prvar
      locno = <lfs_loc>-locno.
      IF sy-subrc = 0.
        ls_prod_key-loctype = <lfs_loc>-loctyp.
        ls_prod_key-product = <lfs_mat>-prvar.
        ls_prod_key-location = <lfs_loc>-locno.
        APPEND ls_prod_key TO lt_prod_key.
        CLEAR ls_prod_key.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
*MOD-1771404
*MOD-1887120
  SORT lt_prod_key BY product location loctype.
*MOD-1887120
  DELETE ADJACENT DUPLICATES FROM lt_prod_key COMPARING product
  location
  loctype.
*MOD-1771404
* Get the Own Logical system name
  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system             = lv_own_sys
    EXCEPTIONS
      own_logical_system_not_defined = 1
      OTHERS                         = 2.
  IF sy-subrc <> 0.
    CLEAR lv_own_sys.
  ENDIF.
* This BAPI will set the deletion flag for the materials selected
* for deletion in the SAP table /SAPAPO/MATLOC
  CALL FUNCTION 'BAPI_PRDSRVAPS_REMOVEITEMS'
    EXPORTING
      logical_system        = lv_own_sys
      business_system_group = gc_bs_group
      commit_control        = space
    TABLES
      product_location_keys = lt_prod_key
      return                = lt_return.
  CLEAR gv_flg_error.
  LOOP AT lt_return ASSIGNING <lfs_return>
  WHERE type = gc_error OR
  type = gc_abend .
    gv_flg_error = gc_set.
    EXIT.
  ENDLOOP.
  IF gv_flg_error = gc_set.
* Product deletion failed for all the selected products
    MESSAGE i048.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
* Delete the Products using the SAP report /SAPAPO/DELETE_PRODUCTS
    SUBMIT /sapapo/delete_products AND RETURN.
    SORT lt_prod_key BY product.
    DELETE ADJACENT DUPLICATES FROM lt_prod_key
    COMPARING product.
* Look for products which dont have a location
    SELECT matnr locno
    INTO TABLE lt_matloc
    FROM /sapapo/v_matloc
    FOR ALL ENTRIES IN gt_prod_data
    WHERE matnr = gt_prod_data-pcode OR
    matnr = gt_prod_data-prvar.
    SORT lt_matloc BY matnr.
    DELETE ADJACENT DUPLICATES FROM lt_matloc
    COMPARING matnr.
    LOOP AT gt_prod_data ASSIGNING <lfs_mat>.
      READ TABLE lt_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-pcode
      BINARY SEARCH.
      IF sy-subrc <> 0.
        ls_product-product = <lfs_mat>-pcode.
        APPEND ls_product TO lt_product.
      ENDIF.
      READ TABLE lt_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-prvar
      BINARY SEARCH.
      IF sy-subrc <> 0.
        ls_product-product = <lfs_mat>-prvar.
        APPEND ls_product TO lt_product.
      ENDIF.
    ENDLOOP.
    SORT lt_product.
    DELETE ADJACENT DUPLICATES FROM lt_product COMPARING ALL FIELDS.
* In case of copre products check if they have any active variants
* before deleting them
    LOOP AT lt_product ASSIGNING <lfs_product>.
      SELECT SINGLE matnr INTO lv_matnr
      FROM /sapapo/matkey
      WHERE matnr = <lfs_product>-product.
      IF sy-subrc <> 0.
        CLEAR <lfs_product>-product.
        CONTINUE.
      ENDIF.
      FIND gc_dot IN <lfs_product>-product.
      IF sy-subrc <> 0.
        LOOP AT gt_view_data TRANSPORTING NO FIELDS
        WHERE pcode = <lfs_product>-product AND
        status <> 'I'.
          CLEAR <lfs_product>-product.
          EXIT.
        ENDLOOP.
      ENDIF.
    ENDLOOP.
    DELETE lt_product WHERE product IS INITIAL.
    REFRESH gt_product.
    gt_product = lt_product.
    IF lt_product IS NOT INITIAL.
* Delete the products which dont have any locations
      CALL FUNCTION 'BAPI_PRDSRVAPS_DELMULTI'
        EXPORTING
          logical_system        = lv_own_sys
          business_system_group = gc_bs_group
          commit_control        = space
        TABLES
          product_keys          = lt_product
          return                = lt_return.
      CLEAR gv_flg_error.
      LOOP AT lt_return ASSIGNING <lfs_return>
      WHERE type = gc_error OR
      type = gc_abend .
        gv_flg_error = gc_set.
        EXIT.
      ENDLOOP.
      IF gv_flg_error = gc_set.
* Product deletion failed for all the selected products
        MESSAGE i048.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
* Delete the Products using the SAP report /SAPAPO/DELETE_PRODUCTS
        SUBMIT /sapapo/delete_products AND RETURN.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM. " f_set_deletion_flag
*&---------------------------------------------------------------------*
*& Form f_display_alv
*&---------------------------------------------------------------------*
* Display ALV report with selected Product and Locations
*----------------------------------------------------------------------*
* -->IT_PROD_DATA Itab with the Product and Location combs
* <--CT_FCAT Itab for Field Catelogue
* <--CT_MATID_ALL Itab with Matid for all the Products
*----------------------------------------------------------------------*
FORM f_display_alv USING it_prod_data TYPE t_f4_data_loc
ct_matloc TYPE t_matloc
CHANGING ct_fcat TYPE slis_t_fieldcat_alv
ct_matid_all TYPE t_matid.
*Local constants:
  CONSTANTS:
  lc_tabprd TYPE tabname VALUE 'LT_DISPLAYPRD'.
* Local Data Declaration
  DATA: ls_display TYPE s_display,
  ls_product TYPE bapi10001key,
  ls_layout TYPE slis_layout_alv,
  ls_displayprd TYPE s_displayprd,
  lt_displayprd TYPE t_displayprd,
  lv_flg_del TYPE char1,
  lv_flg_prd TYPE flag,
  lv_repid TYPE syrepid,
  lt_events TYPE STANDARD TABLE OF slis_alv_event,
  lt_eventsprd TYPE STANDARD TABLE OF slis_alv_event,
  lt_display TYPE STANDARD TABLE OF s_display,
  lt_prod_1 TYPE STANDARD TABLE OF s_f4_data_loc,
  lt_prod_2 TYPE STANDARD TABLE OF s_f4_data_loc,
  lt_matloc TYPE STANDARD TABLE OF /sapapo/v_matloc,
  lt_matlocdel TYPE STANDARD TABLE OF /sapapo/matloc,
  ls_matloc TYPE /sapapo/matloc,
  ls_matid TYPE s_matid.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid,
  lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
  lv_pdctr TYPE char2, " Production center
  lv_whse1 TYPE char3, " Warehouse1
  lv_whse2 TYPE char3, " Warehouse2
  l_wa_matloc TYPE s_matloc.
  DATA: l_wa_display TYPE s_display.
  lv_repid = sy-repid.
* Field-Symbols
  FIELD-SYMBOLS: <lfs_prod> TYPE s_f4_data_loc,
  <lfs_prod_data> TYPE s_f4_data_loc,
  <lfs_display> TYPE s_display,
  <lfs_displayprd> TYPE s_displayprd,
  <lfs_matloc> TYPE /sapapo/v_matloc,
  <lfs_matid> TYPE s_matid.
**MOD-1887120
* LOOP AT IT_PROD_DATA ASSIGNING <lfs_prod_data>.
* READ TABLE ct_matloc into l_wa_matloc
* WITH KEY matnr = <lfs_prod_data>-prvar
* locno = <lfs_prod_data>-locno.
* IF sy-subrc = 0.
* CLEAR: ls_display, ls_displayprd.
* ls_display-prod = <lfs_prod_data>-prvar.
* ls_display-p_des = <lfs_prod_data>-vartx.
**MOD-1771404
** ls_display-loc = <lfs_prod>-locno.
** ls_display-l_des = <lfs_prod>-desc.
**MOD-1771404
* ls_display-loc = l_wa_matloc-LOCNO.
* ls_display-l_des = lv_ware_desc.
* APPEND ls_display TO lt_display.
* <lfs_prod_data>-locno = l_wa_matloc-LOCNO.
* ENDIF.
* READ TABLE gt_product TRANSPORTING NO FIELDS
* WITH KEY product = <lfs_prod_data>-prvar.
* IF sy-subrc = 0.
* ls_displayprd-prod = <lfs_prod_data>-prvar.
* ls_displayprd-p_des = <lfs_prod_data>-vartx.
* APPEND ls_displayprd TO lt_displayprd.
* ENDIF.
* ENDLOOP.
**MOD-1887120
  lt_prod_1 = it_prod_data.
  lt_prod_2 = it_prod_data.
  CLEAR lv_flg_del.
*MOD-1887120
* SORT lt_prod_1 BY prvar locno.
  SORT lt_prod_1 BY locno pcode prvar pdctr status whse1 whse2.
* DELETE ADJACENT DUPLICATES FROM lt_prod_1 COMPARING prvar locno.
  DELETE ADJACENT DUPLICATES FROM lt_prod_1 COMPARING locno
  pcode
  prvar
  pdctr
  status
  whse1
  whse2.
*MOD-1887120
*MOD-1887120
* SORT lt_prod_2 BY pcode locno.
  SORT lt_prod_2 BY locno pcode prvar pdctr status whse1 whse2.
* DELETE ADJACENT DUPLICATES FROM lt_prod_2 COMPARING pcode locno.
  DELETE ADJACENT DUPLICATES FROM lt_prod_2 COMPARING locno
  pcode
  prvar
  pdctr
  status
  whse1
  whse2.
*MOD-1887120
**MOD-1771404
* PERFORM F_GET_LOC_DESC USING IT_PROD_DATA
* CHANGING lv_ware_desc.
**MOD-1771404
  LOOP AT lt_prod_1 ASSIGNING <lfs_prod>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_prod>-prvar
      locno = s_mkt-low.
*MOD-1887120
* locno = <lfs_prod>-locno.
*MOD-1887120
      IF sy-subrc = 0.
        CLEAR: ls_display, ls_displayprd.
        ls_display-prod = <lfs_prod>-prvar.
        ls_display-p_des = <lfs_prod>-vartx.
*MOD-1771404
* ls_display-loc = <lfs_prod>-locno.
* ls_display-l_des = <lfs_prod>-desc.
*MOD-1771404
*MOD-1887120
        ls_display-loc = s_mkt-low.
*MOD-1887120
*MOD-1887120
* ls_display-loc = <lfs_prod>-locno.
*MOD-1887120
**MOD-1887120
        PERFORM f_get_loc_desc CHANGING lv_ware_desc.
**MOD-1887120
        ls_display-l_des = lv_ware_desc.
        APPEND ls_display TO lt_display.
      ENDIF.
      READ TABLE gt_product TRANSPORTING NO FIELDS
      WITH KEY product = <lfs_prod>-prvar.
      IF sy-subrc = 0.
        ls_displayprd-prod = <lfs_prod>-prvar.
        ls_displayprd-p_des = <lfs_prod>-vartx.
        APPEND ls_displayprd TO lt_displayprd.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
  LOOP AT lt_prod_2 ASSIGNING <lfs_prod>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_prod>-pcode
      locno = s_mkt-low.
*MOD-1887120
* locno = <lfs_prod>-locno.
*MOD-1887120
      IF sy-subrc = 0.
        CLEAR: ls_display, lv_flg_prd.
        LOOP AT gt_view_data TRANSPORTING NO FIELDS
        WHERE pcode = <lfs_prod>-pcode AND
        status <> 'I'.
          lv_flg_prd = 'X'.
        ENDLOOP.
        IF lv_flg_prd IS INITIAL.
          ls_display-prod = <lfs_prod>-pcode.
          ls_display-p_des = <lfs_prod>-maktx.
* ls_display-loc = <lfs_prod>-locno.
* ls_display-l_des = <lfs_prod>-desc.
*MOD-1887120
          ls_display-loc = s_mkt-low.
*MOD-1887120
* ls_display-loc = <lfs_prod>-locno.
*MOD-1887120
**MOD-1887120
* PERFORM F_GET_LOC_DESC USING lt_prod_2
          PERFORM f_get_loc_desc CHANGING lv_ware_desc.
**MOD-1887120
          ls_display-l_des = lv_ware_desc.
          APPEND ls_display TO lt_display.
          READ TABLE gt_product TRANSPORTING NO FIELDS
          WITH KEY product = <lfs_prod>-pcode.
          IF sy-subrc = 0.
            ls_displayprd-prod = <lfs_prod>-pcode.
            ls_displayprd-p_des = <lfs_prod>-maktx.
            APPEND ls_displayprd TO lt_displayprd.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
  LOOP AT gt_product INTO ls_product.
    ls_displayprd-prod = ls_product.
    FIND gc_dot IN ls_product.
    IF sy-subrc = 0.
      READ TABLE gt_prod_data ASSIGNING <lfs_prod>
      WITH KEY prvar = ls_product-product.
      IF sy-subrc = 0.
        ls_displayprd-p_des = <lfs_prod>-vartx.
      ENDIF.
    ELSE.
      READ TABLE it_prod_data ASSIGNING <lfs_prod>
      WITH KEY pcode = ls_product-product.
      IF sy-subrc = 0.
        ls_displayprd-p_des = <lfs_prod>-maktx.
      ENDIF.
    ENDIF.
  ENDLOOP.
  SORT lt_display BY prod loc.
*MOD-1887120
  DELETE ADJACENT DUPLICATES FROM lt_display
  COMPARING prod loc.
*MOD-1887120
**MOD-1887120
*LOOP AT lt_display into l_wa_display.
* READ TABLE CT_MATLOC INTO L_WA_MATLOC
* WITH KEY matnr = l_wa_display-prod
* locno = l_wa_display-loc.
* IF sy-subrc ne 0.
* l_wa_display-status = 'X'.
* modify lt_display from l_wa_display transporting status.
* ENDIF.
*ENDLOOP.
*DELETE lt_display WHERE status = 'X'.
**MOD-1887120
  SORT lt_displayprd BY prod.
  DELETE ADJACENT DUPLICATES FROM lt_displayprd
  COMPARING prod.
  IF lt_display IS INITIAL.
    EXIT.
  ENDIF.
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
    EXPORTING
      i_callback_program       = lv_repid
      i_callback_user_command  = gc_show_log
      i_callback_pf_status_set = gc_set_pf.
* Populate the top of page event.
  PERFORM f_get_events CHANGING lt_events.
  REFRESH gt_fcat.
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_3 gc_prodct gc_tabname text-003 gc_len_prod gc_left,
  gc_pos_4 gc_prod_des gc_tabname text-004 gc_len_p_des gc_left,
  gc_pos_5 gc_loctn gc_tabname text-005 gc_len_loc gc_left,
  gc_pos_6 gc_loc_des gc_tabname text-006 gc_len_l_des gc_left.
* If not test mode then display status of deletion
  IF p_test <> gc_set.
    PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
    gc_pos_2 gc_status gc_tabname text-019 gc_len_stat gc_left,
    gc_pos_1 gc_light gc_tabname space gc_len_lt gc_center.
    SELECT *
    FROM /sapapo/v_matloc
    INTO TABLE lt_matloc
    FOR ALL ENTRIES IN lt_display
    WHERE matnr = lt_display-prod AND
    locno = lt_display-loc.
*Check if the product is deleted.
    LOOP AT lt_display ASSIGNING <lfs_display>.
      READ TABLE lt_matloc ASSIGNING <lfs_matloc>
      WITH KEY matnr = <lfs_display>-prod
      locno = <lfs_display>-loc.
      IF sy-subrc = 0.
        <lfs_display>-status = text-020.
        <lfs_display>-light = gc_red.
        CLEAR <lfs_matloc>-lvorm.
        MOVE-CORRESPONDING <lfs_matloc> TO ls_matloc.
        APPEND ls_matloc TO lt_matlocdel.
        lv_flg_del = gc_set.
      ELSE.
        <lfs_display>-status = text-021.
        <lfs_display>-light = gc_green.
      ENDIF.
    ENDLOOP.
    IF lv_flg_del = gc_set. " Deletion failed for atleast one prod
* Deletion failed for some products.
      MODIFY /sapapo/matloc FROM TABLE lt_matlocdel.
      MESSAGE s064.
    ELSE.
* Deletion successful for all the products
      MESSAGE s065.
    ENDIF.
  ENDIF. " For IF p_test <> gc_set.
* Display the ALV List Display
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
    EXPORTING
      is_layout                  = ls_layout
      it_fieldcat                = gt_fcat
      i_tabname                  = gc_tabname
      it_events                  = lt_events
    TABLES
      t_outtab                   = lt_display
    EXCEPTIONS
      program_error              = 1
      maximum_of_appends_reached = 2
      OTHERS                     = 3.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
  IF p_test IS INITIAL.
    PERFORM f_get_events_prd CHANGING lt_eventsprd.
    REFRESH gt_fcat.
* Populate the Field Catelog
    PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
    gc_pos_3 gc_prodct lc_tabprd text-003 gc_len_prod gc_left,
    gc_pos_4 gc_prod_des lc_tabprd text-004 gc_len_p_des gc_left.
    IF p_test <> gc_set.
      PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
      gc_pos_2 gc_status lc_tabprd text-019 gc_len_stat gc_left,
      gc_pos_1 gc_light lc_tabprd space gc_len_lt gc_center.
* Check the Products from the /SAPAPO/MATKEY to check for deletion
      SELECT matid matnr INTO TABLE ct_matid_all
      FROM /sapapo/matkey
      FOR ALL ENTRIES IN lt_displayprd
      WHERE matnr = lt_displayprd-prod.
      LOOP AT lt_displayprd ASSIGNING <lfs_displayprd>.
        READ TABLE ct_matid_all INTO ls_matid
        WITH KEY matnr = <lfs_displayprd>-prod.
        IF sy-subrc = 0.
          <lfs_displayprd>-status = text-020.
          <lfs_displayprd>-light = gc_red.
          lv_flg_del = gc_set.
        ELSE.
          <lfs_displayprd>-status = text-021.
          <lfs_displayprd>-light = gc_green.
        ENDIF.
      ENDLOOP.
      IF lv_flg_del = gc_set. " Deletion failed for atleast one prod
* Deletion failed for some products.
        MESSAGE s064.
      ELSE.
* Deletion successful for all the products
        MESSAGE s065.
      ENDIF.
    ENDIF.
* Display the ALV List Display
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
      EXPORTING
        is_layout                  = ls_layout
        it_fieldcat                = gt_fcat
        i_tabname                  = lc_tabprd
        it_events                  = lt_eventsprd
      TABLES
        t_outtab                   = lt_displayprd
      EXCEPTIONS
        program_error              = 1
        maximum_of_appends_reached = 2
        OTHERS                     = 3.
    IF sy-subrc <> 0.
* Error in displaying end report
      MESSAGE e062.
      LEAVE LIST-PROCESSING.
    ENDIF.
  ENDIF.
* Displays the ALV list
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'
    EXCEPTIONS
      program_error = 1
      OTHERS        = 2.
ENDFORM. " f_display_alv
*&---------------------------------------------------------------------*
*& Form f_get_events
*&---------------------------------------------------------------------*
* Get the TOP OF PAGE event
*----------------------------------------------------------------------*
* <--CT_EVENTS Itab with the events to be triggred
*----------------------------------------------------------------------*
FORM f_get_events CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
  WITH KEY name = slis_ev_top_of_page
  BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = slis_ev_top_of_page.
  ENDIF.
ENDFORM. " f_get_events
*&---------------------------------------------------------------------*
*& Form vpopulate_fieldcat
*&---------------------------------------------------------------------*
* Populate the fieldcatelog for ALV report
*----------------------------------------------------------------------*
* -->IV_CPOS Column Position
* -->IV_FNAM Field Name
* -->IV_TABNAM Internal Table Name
* -->IV_TITLE Column Title
* -->IV_OPT_LEN Output Length
* -->IV_JUSTIFY Justify left / right
*----------------------------------------------------------------------*
FORM f_populate_fieldcat USING iv_cpos TYPE lvc_colpos
iv_fnam TYPE lvc_fname
iv_tabnam TYPE lvc_tname
iv_title TYPE reptext
iv_opt_len TYPE lvc_outlen
iv_justify TYPE lvc_just.
* Local Data Declaration
  DATA: ls_fcat TYPE slis_fieldcat_alv.
  CLEAR ls_fcat.
  ls_fcat-col_pos = iv_cpos.
  ls_fcat-fieldname = iv_fnam.
  ls_fcat-tabname = iv_tabnam.
  ls_fcat-reptext_ddic = iv_title.
  ls_fcat-outputlen = iv_opt_len.
  ls_fcat-just = iv_justify.
  IF iv_fnam = gc_light.
    ls_fcat-icon = gc_set.
  ENDIF.
  APPEND ls_fcat TO gt_fcat.
ENDFORM. " f_populate_fieldcat
*&---------------------------------------------------------------------*
*& Form F_SET_PF
*&---------------------------------------------------------------------*
* Set the PF Status for the display and log screen
*----------------------------------------------------------------------*
* -->CT_EXTAB
*----------------------------------------------------------------------*
FORM f_set_pf USING ct_extab TYPE slis_t_extab.             "#EC CALLED
* Dynamic perform required to set custom PF status for ALV report
                                                            "#EC *
* CT_EXTAB is required as per the standard exit perform of ALVgrid fm
* Local Data Declation.
  DATA: ls_extab TYPE slis_extab,
  lt_extab TYPE STANDARD TABLE OF slis_extab.
* If the Test Run check box is selected then disable the Log Icons
  IF p_test = gc_set.
    ls_extab-fcode = gc_td_log.
    APPEND ls_extab TO lt_extab.
    CLEAR ls_extab.
    SET PF-STATUS 'PF_DISPLAY_LOG' EXCLUDING lt_extab.
  ENDIF.
  IF gt_tlane IS INITIAL.
    ls_extab-fcode = gc_tlane.
    APPEND ls_extab TO lt_extab.
    CLEAR ls_extab.
  ENDIF.
  SET PF-STATUS 'PF_DISPLAY_LOG' EXCLUDING lt_extab.
ENDFORM. " F_SET_PF
*&---------------------------------------------------------------------*
*& Form f_show_log
*&---------------------------------------------------------------------*
* Display Log report using
*----------------------------------------------------------------------*
* --> iv_ucomm OK Code for the user command
* --> is_selfield Selected field information
*----------------------------------------------------------------------*
FORM f_show_log USING iv_ucomm TYPE sy-ucomm
is_selfield TYPE slis_selfield.                             "#EC CALLED
* Dynamic perform for displaying log based on user action
                                                            "#EC *
* IS_SELFIELD is passed as per the standard exit perform for ALVgrid
  IF iv_ucomm = gc_td_log.
* Local Data Delaration.
    DATA: lt_msg_dtl TYPE STANDARD TABLE OF bdcmsgcoll,
    lv_mode TYPE char1,
    lv_error TYPE char1,
    lv_date TYPE char10,
    lv_time TYPE char8,
    lv_date_to TYPE char10,
    lv_time_to TYPE char8.
* Field-Symbols
    FIELD-SYMBOLS: <lfs_msg_dtl> TYPE bdcmsgcoll.
    REFRESH gt_bdc_tab.
    CLEAR: lv_date, lv_time.
    CONCATENATE gv_date_before+6(2) gv_date_before+4(2)
    gv_date_before+0(4) INTO lv_date SEPARATED BY gc_dot.
    CONCATENATE gv_time_before+0(2) gv_time_before+2(2)
    gv_time_before+4(2) INTO lv_time SEPARATED BY gc_colon.
    CONCATENATE sy-datum+6(2) sy-datum+4(2)
    sy-datum+0(4) INTO lv_date_to SEPARATED BY gc_dot.
    CONCATENATE sy-uzeit+0(2) sy-uzeit+2(2)
    sy-uzeit+4(2) INTO lv_time_to SEPARATED BY gc_colon.
* Populate the BDC Table for SLG1 Transaction
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLSLG3' '0100',
    ' ' 'BDC_CURSOR' 'BALHDR-ALPROG',
* ' ' 'BDC_OKCODE' '=SELE',
    ' ' 'BALHDR-OBJECT' '*',
    ' ' 'BALHDR-SUBOBJECT' '*',
    ' ' 'BALHDR-EXTNUMBER' '*',
    ' ' 'BALHDR-ALDATE' lv_date,
    ' ' 'BALHDR-ALTIME' lv_time,
    ' ' '*BALHDR-ALDATE' lv_date_to,
    ' ' '*BALHDR-ALTIME' lv_time_to,
    ' ' 'BALHDR-ALUSER' sy-uname,
    ' ' 'BALHDR-ALTCODE' '*',
    ' ' 'BALHDR-ALPROG' '*',
    ' ' 'MODE_ALL' 'X',
    ' ' 'PROBCLASS_4' 'X'.
    CLEAR lv_error.
    lv_mode = gc_foreground.
* Call Transaction SLG1 for displaying the LOG for the action.
    CALL TRANSACTION gc_slg1 USING gt_bdc_tab MODE lv_mode
    MESSAGES INTO lt_msg_dtl.
    LOOP AT lt_msg_dtl ASSIGNING <lfs_msg_dtl> WHERE
    msgtyp = gc_error OR
    msgtyp = gc_abend.
      lv_error = gc_set.
      EXIT.
    ENDLOOP.
    IF lv_error = gc_set.
* Error while displaying Log.
      MESSAGE i049.
    ENDIF.
  ELSEIF iv_ucomm = gc_tlane.
* Display Tlane deletion data.
    PERFORM f_display_tlane USING gt_tlane.
  ENDIF.
ENDFORM. " f_show_log
*&---------------------------------------------------------------------*
*& Form top_of_page
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for ALV report
*----------------------------------------------------------------------*
FORM top_of_page.                                           "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
  lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_h gc_space text-015,
  gs_type_s text-007 sy-uname,
  gs_type_s text-008 lv_date,
  gs_type_s text-009 lv_time,
  gs_type_s gc_space gc_space.
* If Test Run then select
  IF p_test = gc_set. " IF test run
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-013,
    gs_type_a space text-014.
  ELSEIF gv_msg_error = gc_set. "IF error in call trns
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-018.
  ELSEIF gv_flg_error = gc_set. " IF error in deletion
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-012.
  ELSEIF gv_flg_error <> gc_set. " IF no error
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-011.
  ENDIF.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " top_of_page
*&---------------------------------------------------------------------*
*& Form f_populate_top_page
*&---------------------------------------------------------------------*
* Populate the Header for ALV top of page with parameters passed
*----------------------------------------------------------------------*
* -->IV_TYPE Line Type
* -->IV_KEY Key
* -->IV_INFO Info to be printed
*----------------------------------------------------------------------*
FORM f_populate_top_page USING iv_type TYPE char1
iv_key TYPE any
iv_info TYPE any.
* Local Data Declaration
  DATA: ls_top_page TYPE slis_listheader.
  CLEAR ls_top_page.
  ls_top_page-typ = iv_type.
  ls_top_page-key = iv_key.
  ls_top_page-info = iv_info.
  APPEND ls_top_page TO gt_top_page.
ENDFORM. " f_populate_top_page
*&---------------------------------------------------------------------*
*& Form f_populate_range_table_LOCID
*&---------------------------------------------------------------------*
* Populate the Range thable wiht the values passed
*----------------------------------------------------------------------*
* -->IV_VALUE_LOW Lower Value of the Range
* <--CT_RANGE_TAB Table of type Range
*----------------------------------------------------------------------*
FORM f_populate_range_table_locid USING iv_value_low TYPE any
CHANGING ct_range_tab TYPE t_locid_range.
* Local Data Declaration
  DATA: ls_locid TYPE s_range_locid.
  ls_locid-sign = gc_sign_i.
  ls_locid-option = gc_option_eq.
  ls_locid-low = iv_value_low.
  APPEND ls_locid TO ct_range_tab.
  CLEAR ls_locid.
ENDFORM. " f_populate_range_table_LOCID
*&---------------------------------------------------------------------*
*& Form f_display_tlane
*&---------------------------------------------------------------------*
* Display Transportation lane deletion data
*----------------------------------------------------------------------*
* -->CT_TLANE Itab with details of Tlane deleted
*----------------------------------------------------------------------*
FORM f_display_tlane USING ct_tlane TYPE t_tlane_data.
* Local Data Declaration
  DATA: lt_tl_events TYPE STANDARD TABLE OF slis_alv_event.
* Populate the top of page event.
  PERFORM f_get_events_tl CHANGING lt_tl_events.
  REFRESH gt_fcat.
*MOD-1887120
  SORT ct_tlane BY locfr locto matid.
*MOD-1887120
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_1 gc_locfr gc_tl_tab text-023 gc_len_l_des gc_left,
  gc_pos_2 gc_locto gc_tl_tab text-024 gc_len_l_des gc_left,
  gc_pos_3 gc_prod_tl gc_tl_tab text-022 gc_len_l_des gc_left.
* Display the ALV Grid Display
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = gt_fcat
      i_save             = gc_save
      it_events          = lt_tl_events
    TABLES
      t_outtab           = ct_tlane
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM. " f_display_tlane
*&---------------------------------------------------------------------*
*& Form f_get_events_tl
*&---------------------------------------------------------------------*
* Get event top of page for Tlane report
*----------------------------------------------------------------------*
* <--CT_EVENTS Itab for top of page
*----------------------------------------------------------------------*
FORM f_get_events_tl CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
  WITH KEY name = slis_ev_top_of_page
  BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = gc_lt_top.
  ENDIF.
ENDFORM. " f_get_events_tl
*&---------------------------------------------------------------------*
*& Form F_TOP_TL
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for TLANE report
*----------------------------------------------------------------------*
FORM f_top_tl.                                              "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
  lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_h gc_space text-025,
  gs_type_s text-007 sy-uname,
  gs_type_s text-008 lv_date,
  gs_type_s text-009 lv_time,
  gs_type_s gc_space gc_space.
  IF p_test IS INITIAL.
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-026.
  ELSE.
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-041.
  ENDIF.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " F_TOP_TL
*&---------------------------------------------------------------------*
*& Form f_adj_screen_flds
*&---------------------------------------------------------------------*
* Adjusts the screen fields
*----------------------------------------------------------------------*
FORM f_adj_screen_flds .
*MOD-1771404 by Aveek
* IF p_mdel IS INITIAL.
* LOOP AT SCREEN.
* IF screen-name = 'P_LOG' OR
* screen-name = 'S_PRD-LOW' OR
* screen-name = 'S_PRD-HIGH' OR
* screen-name = 'S_MKT-LOW' OR
* screen-name = 'S_MKT-HIGH' OR
* screen-name = 'P_TEST'.
* screen-input = 0.
* REFRESH: s_prd, s_mkt.
* MODIFY SCREEN.
* ENDIF.
* ENDLOOP.
* ENDIF.
* IF p_tlan IS INITIAL.
* LOOP AT SCREEN.
* IF screen-name = 'P_SRC' OR
* screen-name = 'P_DES' OR
* screen-name = 'P_PRD' OR
* screen-name = 'P_TDAT' OR
* screen-name = 'P_TPRD' OR
* screen-name = 'P_FRD' OR
* screen-name = 'P_TOD'.
* screen-input = 0.
* CLEAR: p_src, p_des, p_prd.
* MODIFY SCREEN.
* ENDIF.
* ENDLOOP.
* ENDIF.
*
* IF p_tdat IS INITIAL.
* LOOP AT SCREEN.
* IF screen-name = 'P_FRD' OR
* screen-name = 'P_TOD'.
* screen-input = 0.
* CLEAR: p_frd, p_tod.
* MODIFY SCREEN.
* ENDIF.
* ENDLOOP.
* ENDIF.
***MOD-1771404 by Aveek
ENDFORM. " f_adj_screen_flds
*&---------------------------------------------------------------------*
*& Form f_update_tlane
*&---------------------------------------------------------------------*
* Updates transportation lane
*----------------------------------------------------------------------*
**MOD-1771404 by Aveek
FORM f_update_tlane .
  CONSTANTS:
  lc_stime TYPE t VALUE '000000',
  lc_etime TYPE t VALUE '235959',
  lc_utc TYPE timezone VALUE 'UTC'.
  DATA:
  lv_dummy TYPE /sapapo/c_loctype,
  lv_valfr TYPE timestamp,
  lv_valto TYPE timestamp,
  lv_matid TYPE /sapapo/matid,
  lv_srcid TYPE /sapapo/locid,
  lv_desid TYPE /sapapo/locid.
  DATA:
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_matid TYPE t_matid,
  ls_matid TYPE s_matid,
  lt_tlaneupd TYPE t_tlaneupd,
  ls_tlaneupd TYPE s_tlaneupd.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.
  PERFORM f_get_locid_for_loc USING p_src
  CHANGING lv_srcid
  lv_dummy.
  PERFORM f_get_locid_for_loc USING p_des
  CHANGING lv_desid
  lv_dummy.
  PERFORM f_get_matid_for_prod USING p_prd
  CHANGING lv_matid.
  ls_matid-matid = lv_matid.
  ls_matid-matnr = p_prd.
  APPEND ls_matid TO lt_matid.
  SELECT b~matid a~prvar
  APPENDING TABLE lt_matid
  FROM zap_sign AS a JOIN
  /sapapo/matkey AS b
  ON a~prvar = b~matnr
  WHERE pcode = p_prd AND
  ( pdctr = p_src OR
  whse1 = p_src OR
  whse2 = p_src ) AND
  ( whse1 = p_des OR
  whse2 = p_des OR
  markt = p_des ).
**MOD-1771404 Changed code
* SELECT b~matid a~prvar
* APPENDING TABLE lt_matid
* FROM zm_zap_sign AS a JOIN
* /sapapo/matkey AS b
* ON a~prvar = b~matnr
* WHERE pcode = p_prd AND
* ( markt = p_src or
* pdctr = p_src OR
* whse1 = p_src OR
* whse2 = p_src ) OR
* ( pdctr = p_des OR
* whse1 = p_des OR
* whse2 = p_des OR
* markt = p_des ).
*MOD-1771404
  IF sy-subrc <> 0.
    MESSAGE i072 WITH p_prd p_src p_des.
    LEAVE LIST-PROCESSING.
  ENDIF.
  SELECT * INTO TABLE lt_trprod
  FROM /sapapo/trprod
  FOR ALL ENTRIES IN lt_matid
  WHERE locfr = lv_srcid AND
  locto = lv_desid AND
  matid = lt_matid-matid.
*MOD-1771404
* SELECT * INTO TABLE lt_trprod
* FROM /sapapo/trprod
* FOR ALL ENTRIES IN lt_matid
* WHERE ( locfr = lv_srcid OR
* locfr = lv_desid ) AND
* matid = lt_matid-matid OR
* ( locto = lv_srcid OR
* locto = lv_desid ) AND
* matid = lt_matid-matid.
*MOD-1771404
  IF sy-subrc <> 0.
    MESSAGE i067 WITH p_src p_des p_prd.
    LEAVE LIST-PROCESSING.
  ENDIF.
* Change of validity dates
  IF p_tdat IS NOT INITIAL.
    IF p_frd IS INITIAL OR p_tod IS INITIAL.
      MESSAGE i070.
      LEAVE LIST-PROCESSING.
    ELSE.
      IF p_frd > p_tod.
        MESSAGE i071.
        LEAVE LIST-PROCESSING.
      ENDIF.
    ENDIF.
    CONVERT: DATE p_frd TIME lc_stime
    INTO TIME STAMP lv_valfr TIME ZONE lc_utc,
    DATE p_tod TIME lc_etime
    INTO TIME STAMP lv_valto TIME ZONE lc_utc.
    LOOP AT lt_trprod ASSIGNING <lfs_trprod>.
      <lfs_trprod>-valfr = lv_valfr.
      <lfs_trprod>-valto = lv_valto.
    ENDLOOP.
*MOD-1771404
    MODIFY /sapapo/trprod FROM TABLE lt_trprod.
*MOD-1771404
    IF sy-subrc = 0.
      COMMIT WORK AND WAIT.
      MESSAGE s068 WITH p_src p_des p_prd.
    ENDIF.
  ENDIF.
* Removal of product assignment
  IF p_tprd IS NOT INITIAL.
*MOD-1771404
    DELETE /sapapo/trprod FROM TABLE lt_trprod.
*MOD-1771404
    IF sy-subrc = 0.
      COMMIT WORK AND WAIT.
      MESSAGE s069 WITH p_prd p_src p_des.
    ENDIF.
  ENDIF.
* Display the lanes updated in an ALV
  CLEAR ls_tlaneupd.
  ls_tlaneupd-locfr = p_src.
  ls_tlaneupd-locto = p_des.
  IF p_tdat IS NOT INITIAL.
    ls_tlaneupd-msg = text-034.
  ELSE.
    ls_tlaneupd-msg = text-035.
  ENDIF.
  LOOP AT lt_matid INTO ls_matid.
    ls_tlaneupd-matnr = ls_matid-matnr.
    APPEND ls_tlaneupd TO lt_tlaneupd.
  ENDLOOP.
*MOD-1771404
* LOOP AT lt_trprod INTO ls_trprod.
* CALL FUNCTION '/SAPAPO/DM_MATID_GET_MATERIAL'
* EXPORTING
* iv_matid = ls_trprod-matid
* iv_langu = sy-langu
* IMPORTING
* ev_matnr = lv_matnr.
*
* ls_tlaneupd-matnr = lv_matnr.
* ls_tlaneupd-locfr
* ls_tlaneupd-locto
* ls_tlaneupd-matnr
* ls_tlaneupd-matid
*
*
* APPEND ls_tlaneupd TO lt_tlaneupd.
* ENDLOOP.
*
* ls_tlaneupd-matnr = ls_matid-matnr.
* APPEND ls_tlaneupd TO lt_tlaneupd.
* ENDLOOP.
*MOD-1771404
  PERFORM display_alv_tlaneupd USING lt_tlaneupd.
ENDFORM. " f_update_tlane
***MOD-1771404 by Aveek
*&---------------------------------------------------------------------*
*& Form display_alv_tlaneupd
*&---------------------------------------------------------------------*
* Displays the ALV for updated Tlane
*----------------------------------------------------------------------*
* -->IT_TLANEUPD Tlane table
*----------------------------------------------------------------------*
FORM display_alv_tlaneupd USING it_tlaneupd TYPE t_tlaneupd.
* Local Data Declaration
  DATA: lt_tl_events TYPE STANDARD TABLE OF slis_alv_event.
* Populate the top of page event.
  PERFORM f_get_events_tlupd CHANGING lt_tl_events.
  REFRESH gt_fcat.
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_1 gc_locfr gc_tl_tabu text-023 gc_len_l_des gc_left,
  gc_pos_2 gc_locto gc_tl_tabu text-024 gc_len_l_des gc_left,
  gc_pos_3 gc_matnr gc_tl_tabu text-022 gc_len_l_des gc_left,
  gc_pos_4 gc_msg gc_tl_tabu text-036 gc_len_l_des gc_left.
* Display the ALV Grid Display
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = gt_fcat
      i_save             = gc_save
      it_events          = lt_tl_events
    TABLES
      t_outtab           = it_tlaneupd
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM. " display_alv_tlaneupd
*&---------------------------------------------------------------------*
*& Form f_get_events_tlupd
*&---------------------------------------------------------------------*
* Gets the events tab
*----------------------------------------------------------------------*
* <--CT_EVENTS Event table
*----------------------------------------------------------------------*
FORM f_get_events_tlupd CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
  WITH KEY name = slis_ev_top_of_page
  BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = gc_tlupd_top.
  ENDIF.
ENDFORM. " f_get_events_tlupd
*&---------------------------------------------------------------------*
*& Form F_TOP_TLUPD
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for TLANE report
*----------------------------------------------------------------------*
FORM f_top_tlupd.                                           "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
  lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_h gc_space text-037,
  gs_type_s text-007 sy-uname,
  gs_type_s text-008 lv_date,
  gs_type_s text-009 lv_time,
  gs_type_s gc_space gc_space,
  gs_type_a text-010 text-038.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " F_TOP_TLUPD
*&---------------------------------------------------------------------*
*& Form f_get_events_prd
*&---------------------------------------------------------------------*
* Gets the events for product
*----------------------------------------------------------------------*
* <--CT_EVENTS Events table
*----------------------------------------------------------------------*
FORM f_get_events_prd CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
  WITH KEY name = slis_ev_top_of_page
  BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = gc_prd_top.
  ENDIF.
ENDFORM. " f_get_events_prd
*&---------------------------------------------------------------------*
*& Form F_TOP_PRD
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for TLANE report
*----------------------------------------------------------------------*
FORM f_top_prd.                                             "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
  lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_s gc_space gc_space.
  IF p_test IS NOT INITIAL.
    PERFORM f_populate_top_page USING
    gs_type_a text-010 text-039.
  ELSE.
    PERFORM f_populate_top_page USING
    gs_type_a text-010 text-040.
  ENDIF.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " F_TOP_TLUPD
*&---------------------------------------------------------------------*
*& Form launch_WUF
*&---------------------------------------------------------------------*
* Launches the WUF
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Core product
* -->IT_PROD_VAR Product variant
*----------------------------------------------------------------------*
FORM launch_wuf USING it_core_prod TYPE t_f4_data_loc
it_prod_var TYPE t_f4_data_loc.
  CONSTANTS:
  lc_rscsel TYPE fieldname VALUE 'RSCSEL-SLOW_I'.
  DATA: lv_ctu_params TYPE ctu_params,
  lv_fname TYPE dynfnam,
  lv_count(2) TYPE n,
  lt_msg_tab TYPE STANDARD TABLE OF bdcmsgcoll.
* Field-symbols
  FIELD-SYMBOLS: <lfs_msg_ret> TYPE bdcmsgcoll,
  <lfs_data> TYPE s_f4_data_loc.
  PERFORM f_populate_bdc_tab USING:
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=OBSEL',
  space 'WUF_OBJ_NAME_STRUC-NAME' 'Location Product',
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=MORE__1',
  space 'WUF_OPT_FIELDS-DISPLAY_DEPS' 'X',
  space 'BDC_CURSOR' 'WUF_SEL_FIELDS-LOW__1',
  space 'WUF_SEL_FIELDS-FIELDNAMETXT__1' 'Product',
  'X' 'SAPLALDB' '3000',
  space 'BDC_OKCODE' '=ACPT'.
  CLEAR lv_count.
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    lv_count = lv_count + 1.
    CONCATENATE lc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
    PERFORM f_populate_bdc_tab USING:
    ' ' lv_fname <lfs_data>-pcode.
    CLEAR lv_fname.
  ENDLOOP.
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    lv_count = lv_count + 1.
    CONCATENATE lc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
    PERFORM f_populate_bdc_tab USING:
    ' ' lv_fname <lfs_data>-prvar.
    CLEAR lv_fname.
  ENDLOOP.
  PERFORM f_populate_bdc_tab USING:
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=EXEC_DIR',
  'X' 'SAPMSSY0' '0120',
  space 'BDC_OKCODE' '=&F15',
  'X' 'SAPLWUF_SELECTION' '0100',
  space 'BDC_OKCODE' '=EXIT'.
  lv_ctu_params-nobinpt = space.
  lv_ctu_params-dismode = 'E'.
  lv_ctu_params-updmode = 'S'.
  lv_ctu_params-racommit = 'X'.
  CALL TRANSACTION 'WUF' USING gt_bdc_tab OPTIONS FROM lv_ctu_params
  MESSAGES INTO lt_msg_tab.
ENDFORM. " launch_WUF
*&---------------------------------------------------------------------*
*& Form f_retrieve_masterdata
*&---------------------------------------------------------------------*
* Gets the master data from the SIGN portfolio meeting the
* user selection
*----------------------------------------------------------------------*
* <--ET_MASTERDATA Master data table
*----------------------------------------------------------------------*
FORM f_retrieve_masterdata CHANGING et_masterdata TYPE t_masterdata
et_tship TYPE t_tship
et_whse TYPE t_whse.
  DATA:
  lt_masterdata TYPE t_masterdata,
  lt_tship TYPE t_tship,
  ls_tship TYPE s_tship,
  ls_whse TYPE s_whse.
  SELECT * INTO TABLE et_masterdata
  FROM zap_scmpf
  WHERE ( pcode = p_prd ) AND
  ( markt = p_src OR
  pdctr = p_src ) OR
  ( pdctr = p_des OR
  markt = p_des ) AND
  delfl = space.
  IF sy-subrc <> 0.
* No sourcing info is maintained for the user selection
    MESSAGE i018.
    LEAVE LIST-PROCESSING.
  ENDIF.
* Obtaining all the possible transhipments in the sourcing
  lt_masterdata = et_masterdata.
  DELETE lt_masterdata WHERE whind IS INITIAL.
  IF lt_masterdata IS INITIAL.
    EXIT.
  ENDIF.
** MOD-1771404
  SELECT * INTO TABLE et_tship
  FROM zap_tshpt
  FOR ALL ENTRIES IN lt_masterdata
  WHERE markt = lt_masterdata-markt AND
  ( pcode = lt_masterdata-pcode OR
  pcode = lt_masterdata-prvar ). " AND
* pcode = lt_masterdata-pcode AND
* pdctr = lt_masterdata-pdctr.
** MOD-1771404
  IF sy-subrc = 0.
    SORT et_tship BY markt pcode pdctr altscn sloc dloc.
    DELETE ADJACENT DUPLICATES FROM et_tship
    COMPARING markt pcode pdctr altscn sloc dloc.
    lt_tship = et_tship.
    SORT lt_tship BY sloc dloc.
    DELETE ADJACENT DUPLICATES FROM lt_tship
    COMPARING sloc dloc.
    LOOP AT lt_tship INTO ls_tship.
      CLEAR ls_whse.
      ls_whse-whse = ls_tship-sloc.
      APPEND ls_whse TO et_whse.
      IF ls_tship-dloc IS INITIAL.
        CONTINUE.
      ENDIF.
      CLEAR ls_whse.
      ls_whse-whse = ls_tship-dloc.
      APPEND ls_whse TO et_whse.
    ENDLOOP.
    SORT et_whse BY whse.
    DELETE ADJACENT DUPLICATES FROM et_whse
    COMPARING whse.
  ENDIF.
ENDFORM. " f_retrieve_masterdata
*&---------------------------------------------------------------------*
*& Form f_collect_tlanes
*&---------------------------------------------------------------------*
* Collects all possible transportation lanes
*----------------------------------------------------------------------*
* -->IT_MASTERDATA Master data table
* -->IT_LOCATION Location table
* -->IT_PRODUCT Product table
* <--ET_TLANE Transportation lane table
*----------------------------------------------------------------------*
FORM f_collect_tlanes USING it_masterdata TYPE t_masterdata
it_tship TYPE t_tship
it_location TYPE t_location
it_product TYPE t_product
CHANGING et_tlane TYPE t_tlane.
  CONSTANTS:
  lc_stime TYPE t VALUE '000000',
  lc_etime TYPE t VALUE '235959',
  lc_utc TYPE timezone VALUE 'UTC'.
  DATA:
  lv_dummy TYPE /sapapo/c_loctype,
  lv_valfr TYPE timestamp,
  lv_valto TYPE timestamp,
  lv_matid TYPE /sapapo/matid,
  lv_srcid TYPE /sapapo/locid,
  lv_desid TYPE /sapapo/locid.
  DATA:
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_matid TYPE t_matid,
  ls_matid TYPE s_matid,
  lt_tlaneupd TYPE t_tlaneupd,
  ls_tlaneupd TYPE s_tlaneupd.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.
  DATA:
  ls_tlane TYPE s_tlane.
  DATA: lv_exist TYPE i.
  DATA:
  lv_dtfrmin TYPE timestamp,
  lv_dttomax TYPE timestamp,
  lv_tabix TYPE sytabix,
  lv_locfr TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno,
  lv_matnr TYPE /sapapo/matnr.
  DATA:
  ls_masterdata TYPE zap_scmpf.
  FIELD-SYMBOLS:
  <lfs_tlanetmp> TYPE s_tlane,
  <lfs_tlane> TYPE s_tlane.
  LOOP AT it_masterdata INTO ls_masterdata
  WHERE status = 'P' OR
  status = 'A'.
* Check if the core product exists in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
    WITH KEY matnr = ls_masterdata-pcode
    BINARY SEARCH.
    IF sy-subrc <> 0.
* Core product exists, find the tlanes involving it
      PERFORM f_find_tlane_comb USING ls_masterdata
      it_tship
      gc_core
      it_location
      CHANGING et_tlane.
    ELSE.
*MOD-1771404
* Product doesnt exist in database, update log
      lv_locfr = ls_masterdata-pdctr.
      lv_locto = ls_masterdata-markt.
      lv_matnr = ls_masterdata-pcode.
      PERFORM f_update_log_table USING gc_msgid
      gc_err
      gc_msgno_028
      lv_locfr lv_locto lv_matnr
      space space
      CHANGING gt_log_tlane.
*MOD-1771404
    ENDIF.
* Check if the variant exists in the data base
    READ TABLE it_product TRANSPORTING NO FIELDS
    WITH KEY matnr = ls_masterdata-prvar
    BINARY SEARCH.
    IF sy-subrc <> 0.
* Variant exists, find the tlanes involving it
      PERFORM f_find_tlane_comb USING ls_masterdata
      it_tship
      gc_var
      it_location
      CHANGING et_tlane.
    ELSE.
*MOD-1771404
* Product doesnt exist in database, update log
      lv_locfr = ls_masterdata-pdctr.
      lv_locto = ls_masterdata-markt.
      lv_matnr = ls_masterdata-prvar.
      PERFORM f_update_log_table USING gc_msgid
      gc_err
      gc_msgno_029
      lv_locfr lv_locto lv_matnr
      space space
      CHANGING gt_log_tlane.
    ENDIF.
*MOD-1771404
  ENDLOOP.
*MOD-1771404
  IF et_tlane IS INITIAL.
* The location(s)/product(s) not available in the db for tlane creation
    MESSAGE i023.
    PERFORM f_update_log_table USING gc_msgid
    gc_err
    gc_msgno_023
    space space space
    space space
    CHANGING gt_log_tlane.
    EXIT.
  ENDIF.
*MOD-1771404
  SORT et_tlane BY locfr locto matnr datfr datto.
  DELETE ADJACENT DUPLICATES FROM et_tlane
  COMPARING locfr locto matnr datfr datto.
  READ TABLE et_tlane ASSIGNING <lfs_tlane> INDEX 1.
  lv_dtfrmin = <lfs_tlane>-datfr.
  lv_dttomax = <lfs_tlane>-datto.
  LOOP AT et_tlane ASSIGNING <lfs_tlane>.
    IF <lfs_tlane>-datfr < lv_dtfrmin.
      lv_dtfrmin = <lfs_tlane>-datfr.
    ENDIF.
    IF <lfs_tlane>-datto > lv_dttomax.
      lv_dttomax = <lfs_tlane>-datto.
    ENDIF.
    AT END OF matnr.
      <lfs_tlane>-datfr = lv_dtfrmin.
      <lfs_tlane>-datto = lv_dttomax.
      lv_tabix = sy-tabix + 1.
      READ TABLE et_tlane ASSIGNING <lfs_tlanetmp> INDEX lv_tabix.
      IF sy-subrc = 0.
        lv_dtfrmin = <lfs_tlanetmp>-datfr.
        lv_dttomax = <lfs_tlanetmp>-datto.
      ENDIF.
    ENDAT.
  ENDLOOP.
  SORT et_tlane BY locfr locto matnr datfr ASCENDING datto DESCENDING.
  DELETE ADJACENT DUPLICATES FROM et_tlane
  COMPARING locfr locto matnr.
** Get the internal guids for material and locations
  LOOP AT et_tlane ASSIGNING <lfs_tlane>.
    PERFORM f_get_locid_from_locno USING <lfs_tlane>-locfr
    CHANGING <lfs_tlane>-lidfr.
    PERFORM f_get_locid_from_locno USING <lfs_tlane>-locto
    CHANGING <lfs_tlane>-lidto.
    PERFORM f_get_matid_from_matnr USING <lfs_tlane>-matnr
    CHANGING <lfs_tlane>-matid.
  ENDLOOP.
  PERFORM f_get_locid_for_loc USING p_src
  CHANGING lv_srcid
  lv_dummy.
  PERFORM f_get_locid_for_loc USING p_des
  CHANGING lv_desid
  lv_dummy.
  PERFORM f_get_matid_for_prod USING p_prd
  CHANGING lv_matid.
  ls_matid-matid = lv_matid.
  ls_matid-matnr = p_prd.
  APPEND ls_matid TO lt_matid.
  LOOP AT et_tlane INTO ls_tlane.
    READ TABLE lt_matid TRANSPORTING NO FIELDS
    WITH KEY matnr = ls_tlane-matnr
    BINARY SEARCH.
    IF sy-subrc <> 0.
      lv_exist = 1.
      MODIFY et_tlane FROM ls_tlane TRANSPORTING lv_exist.
    ENDIF.
  ENDLOOP.
  DELETE et_tlane WHERE lv_exist = 1.
ENDFORM. " f_collect_tlanes
*&---------------------------------------------------------------------*
*& Form f_find_tlane_comb
*&---------------------------------------------------------------------*
* Finds tlanes involving a product
*----------------------------------------------------------------------*
* -->IS_MASTERDATA Masterdata table
* -->IV_PRDTP Prioduct type
* -->IT_LOCATION Location table
* <--ET_TLANE Tlane table
*----------------------------------------------------------------------*
FORM f_find_tlane_comb USING is_masterdata TYPE zap_scmpf
it_tship TYPE t_tship
iv_prdtp TYPE c
it_location TYPE t_location
CHANGING et_tlane TYPE t_tlane.
  DATA:
  lv_matnr TYPE /sapapo/matnr,
  ls_tship TYPE s_tship,
  ls_tship_tmp TYPE s_tship,
  lv_locfr TYPE /sapapo/locno,
  lv_locwh TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno.
  CASE iv_prdtp.
    WHEN gc_core.
      lv_matnr = is_masterdata-pcode.
    WHEN gc_var.
      lv_matnr = is_masterdata-prvar.
  ENDCASE.
* First see if the Source and Market exist in the database.
* Check the presence of transhipments in the sourcing. We assume that
* the destination can only be blank for the last entry of a given
* sourcing alternative
  READ TABLE it_location TRANSPORTING NO FIELDS
  WITH KEY locno = is_masterdata-markt
  BINARY SEARCH.
  IF sy-subrc <> 0.
* Market exists in the data base
    READ TABLE it_location TRANSPORTING NO FIELDS
    WITH KEY locno = is_masterdata-pdctr
    BINARY SEARCH.
    IF sy-subrc <> 0.
* Source exists in the data base
      IF is_masterdata-whind IS INITIAL.
        lv_locfr = is_masterdata-pdctr.
        lv_locto = is_masterdata-markt.
        PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
        gc_ltp_1001 gc_ltp_1002
        is_masterdata-datab
        is_masterdata-datbi
        CHANGING et_tlane.
      ELSE.
        LOOP AT it_tship INTO ls_tship
        WHERE markt = is_masterdata-markt AND
        pcode = is_masterdata-pcode AND
* pcode = is_masterdata-prvar AND
        pdctr = is_masterdata-pdctr.
          ls_tship_tmp = ls_tship.
* For every alternative sourcing
          AT NEW altscn.
* Define lane between the source and the first transhipment
            READ TABLE it_location TRANSPORTING NO FIELDS
            WITH KEY locno = ls_tship-sloc
            BINARY SEARCH.
            IF sy-subrc <> 0.
              lv_locfr = is_masterdata-pdctr.
              lv_locto = ls_tship_tmp-sloc.
              PERFORM f_build_tlane USING lv_locfr
              lv_locto
              lv_matnr
              gc_ltp_1001
              gc_ltp_1002
              ls_tship_tmp-datab
              ls_tship_tmp-datbi
              CHANGING et_tlane.
            ELSE.
              lv_locfr = is_masterdata-pdctr.
              lv_locto = is_masterdata-markt.
              lv_locwh = ls_tship-sloc.
              PERFORM f_update_log_table USING gc_msgid
              gc_err
              gc_msgno_087
              lv_locfr lv_locto
              lv_locwh space
              space
              CHANGING gt_log_tlane.
            ENDIF.
          ENDAT.
* Define lanes between the successive transhipments in the srcing
          IF ls_tship-dloc IS NOT INITIAL.
            READ TABLE it_location TRANSPORTING NO FIELDS
            WITH KEY locno = ls_tship-dloc
            BINARY SEARCH.
            IF sy-subrc <> 0.
              lv_locfr = ls_tship_tmp-sloc.
              lv_locto = ls_tship_tmp-dloc.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
              gc_ltp_1002 gc_ltp_1002
              ls_tship_tmp-datab
              ls_tship_tmp-datbi
              CHANGING et_tlane.
            ELSE.
              lv_locfr = is_masterdata-pdctr.
              lv_locto = is_masterdata-markt.
              lv_locwh = ls_tship-dloc.
              PERFORM f_update_log_table USING gc_msgid
              gc_err
              gc_msgno_087
              lv_locfr lv_locto
              lv_locwh space
              space
              CHANGING gt_log_tlane.
            ENDIF.
          ENDIF.
          AT END OF altscn.
* Define lane between the final transhipment and the market
            IF ls_tship_tmp-dloc IS NOT INITIAL.
              lv_locfr = ls_tship_tmp-dloc.
              lv_locto = is_masterdata-markt.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
              gc_ltp_1002 gc_ltp_1002
              ls_tship_tmp-datab
              ls_tship_tmp-datbi
              CHANGING et_tlane.
            ELSE.
              lv_locfr = ls_tship_tmp-sloc.
              lv_locto = is_masterdata-markt.
              PERFORM f_build_tlane USING lv_locfr lv_locto lv_matnr
              gc_ltp_1002 gc_ltp_1002
              ls_tship_tmp-datab
              ls_tship_tmp-datbi
              CHANGING et_tlane.
            ENDIF.
          ENDAT.
          CLEAR ls_tship_tmp.
        ENDLOOP.
      ENDIF.
    ELSE.
* Source is yet to be created in the database, update log
      lv_locfr = is_masterdata-pdctr.
      lv_locto = is_masterdata-markt.
      PERFORM f_update_log_table USING gc_msgid
      gc_err
      gc_msgno_031
      lv_locfr lv_locto
      lv_matnr space
      space
      CHANGING gt_log_tlane.
    ENDIF.
  ELSE.
* Market is yet to be created in the database, update log
    lv_locfr = is_masterdata-pdctr.
    lv_locto = is_masterdata-markt.
    PERFORM f_update_log_table USING gc_msgid
    gc_err
    gc_msgno_030
    lv_locfr lv_locto
    lv_matnr space
    space
    CHANGING gt_log_tlane.
  ENDIF.
ENDFORM. " f_find_tlane_comb
*&---------------------------------------------------------------------*
*& Form f_build_tlane
*&---------------------------------------------------------------------*
* Builds a Tlane
*----------------------------------------------------------------------*
* -->IV_LOCFR Location from
* -->IV_LOCTO Location to
* -->IV_MATNR Product
* -->IV_LTPFR Loctype from
* -->IV_LTPTO Loctype to
* -->IS_MASTERDATA Master data
* <--ET_TLANE Tlane table
*----------------------------------------------------------------------*
FORM f_build_tlane USING iv_locfr TYPE /sapapo/locno
iv_locto TYPE /sapapo/locno
iv_matnr TYPE /sapapo/matnr
iv_ltpfr TYPE /sapapo/c_loctype
iv_ltpto TYPE /sapapo/c_loctype
iv_datfr TYPE dats
iv_datto TYPE dats
CHANGING et_tlane TYPE t_tlane.
  DATA:
  ls_tlane TYPE s_tlane.
  ls_tlane-locfr = iv_locfr.
  ls_tlane-locto = iv_locto.
  ls_tlane-matnr = iv_matnr.
  ls_tlane-ltpfr = iv_ltpfr.
  ls_tlane-ltpto = iv_ltpto.
  PERFORM f_get_tstamps USING iv_datfr iv_datto
  CHANGING ls_tlane-datfr ls_tlane-datto.
  APPEND ls_tlane TO et_tlane.
ENDFORM. " f_build_tlane
*&---------------------------------------------------------------------*
*& Form f_get_tstamps
*&---------------------------------------------------------------------*
* Gets the timestamps for the Tlane validity dates
*----------------------------------------------------------------------*
* -->IS_MASTERDATA Master data table
* <--EV_DATFR Valid from tstamp
* <--EV_DATTO Valid to tstamp
*----------------------------------------------------------------------*
FORM f_get_tstamps USING iv_datfr TYPE d
iv_datto TYPE d
CHANGING ev_datfr TYPE timestamp
ev_datto TYPE timestamp.
  CONVERT DATE iv_datfr TIME gc_startti
  INTO TIME STAMP ev_datfr TIME ZONE gc_utc.
  CONVERT DATE iv_datto TIME gc_endti
  INTO TIME STAMP ev_datto TIME ZONE gc_utc.
ENDFORM. " f_get_tstamps
*&---------------------------------------------------------------------*
*& Form f_update_log_table
*&---------------------------------------------------------------------*
* Update logs
*----------------------------------------------------------------------*
* -->IV_MSGID Message class
* -->IV_MSGTY Message type
* -->IV_MSGNO Message no
* -->IV_MSGV1 Variable1
* -->IV_MSGV2 Variable2
* -->IV_MSGV3 Variable3
* -->IV_MSGV4 Variable4
* -->IV_MSG Message
* <--ET_LOG_TABLE Log table
*----------------------------------------------------------------------*
FORM f_update_log_table USING iv_msgid TYPE symsgid
iv_msgty TYPE symsgty
iv_msgno TYPE symsgno
iv_msgv1 TYPE any
iv_msgv2 TYPE any
iv_msgv3 TYPE any
iv_msgv4 TYPE any
iv_msg TYPE bapi_msg
CHANGING et_log_table TYPE t_log_table.
  DATA:
  ls_log_table TYPE bal_s_msg.
  DATA: lv_datto TYPE timestamp.
  ls_log_table-msgty = iv_msgty.
  ls_log_table-msgid = iv_msgid.
  ls_log_table-msgno = iv_msgno.
  ls_log_table-msgv1 = iv_msgv1.
  ls_log_table-msgv2 = iv_msgv2.
  ls_log_table-msgv3 = iv_msgv3.
  ls_log_table-msgv4 = iv_msgv4.
  ls_log_table-context-value = iv_msg.
  APPEND ls_log_table TO et_log_table.
ENDFORM. " f_update_log_table
*&---------------------------------------------------------------------*
*& Form f_get_locid_from_locno
*&---------------------------------------------------------------------*
* Gets the guid for locations
*----------------------------------------------------------------------*
* -->IV_LOCNO Location no
* <--EV_LOCID Location id
*----------------------------------------------------------------------*
FORM f_get_locid_from_locno USING iv_locno TYPE /sapapo/locno
CHANGING ev_locid TYPE /sapapo/locid.
  CALL FUNCTION '/SAPAPO/DM_LOCNO_GET_LOCID'
    EXPORTING
      iv_locno           = iv_locno
    IMPORTING
      ev_locid           = ev_locid
    EXCEPTIONS
      location_not_found = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_get_locid_from_locno
**&---------------------------------------------------------------------
**
**& Form f_get_locid_from_locno
**&---------------------------------------------------------------------
**
** Gets the guid for locations
**----------------------------------------------------------------------
**
** -->IV_LOCNO Location no
** <--EV_LOCID Location id
**----------------------------------------------------------------------
*MOD-1771404
*FORM f_get_locid_from_locno USING iv_locno TYPE /sapapo/locno
* CHANGING ev_locid TYPE /sapapo/locid.
*
* CALL FUNCTION '/SAPAPO/DM_LOCNO_GET_LOCID'
* EXPORTING
* iv_locno = iv_locno
* IMPORTING
* ev_locid = ev_locid
* EXCEPTIONS
* location_not_found = 1
* OTHERS = 2.
*
* IF sy-subrc <> 0.
** Do nothing
* ENDIF.
*
*
*ENDFORM. " f_get_locid_from_locno
*MOD-1771404
*&---------------------------------------------------------------------*
*& Form f_get_matid_from_matnr
*&---------------------------------------------------------------------*
* Gets the guid for material
*----------------------------------------------------------------------*
* -->IV_MATNR Material no
* <--EV_MATID Material id
*----------------------------------------------------------------------*
FORM f_get_matid_from_matnr USING iv_matnr TYPE /sapapo/matnr
CHANGING ev_matid TYPE /sapapo/matid.
  CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
    EXPORTING
      iv_matnr        = iv_matnr
    IMPORTING
      ev_matid        = ev_matid
    EXCEPTIONS
      matid_not_found = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
* Do nothing
  ENDIF.
ENDFORM. " f_get_matid_from_matnr
*&---------------------------------------------------------------------*
*& Form f_categorise_tlanes
*&---------------------------------------------------------------------*
* Categorises Transp lanes
*----------------------------------------------------------------------*
* -->IT_TLANE Tlane table
* <--ET_TLANE_NEW Tlane table with new entries
* <--ET_TLANE_DAT Tlane table with date changes
*----------------------------------------------------------------------*
FORM f_categorise_tlanes USING it_tlane TYPE t_tlane
CHANGING et_tlane_new TYPE t_tlane
et_tlane_dat TYPE t_tlane.
  DATA:
  lt_tlane_cr TYPE STANDARD TABLE OF /sapapo/trprod,
  ls_tlane_cr TYPE /sapapo/trprod,                          "#EC NEEDED
  ls_tlane_tmp TYPE s_tlane,
  ls_tlane TYPE s_tlane,
  ls_tlane_new TYPE s_tlane.
  DATA: lv_exist TYPE i.
  DATA: lv_exist1 TYPE i.
  DATA:
  lv_dtfrmin TYPE timestamp,
  lv_dttomax TYPE timestamp,
  lv_tabix TYPE sytabix,
  lv_locfr TYPE /sapapo/locno,
  lv_locto TYPE /sapapo/locno,
  lv_matnr TYPE /sapapo/matnr.
  DATA:
  ls_masterdata TYPE zap_scmpf.
  FIELD-SYMBOLS:
  <lfs_tlanetmp> TYPE s_tlane,
  <lfs_tlane> TYPE s_tlane.
*MOD-1771404
  SORT it_tlane BY locfr locto matnr datfr datto.
  DELETE ADJACENT DUPLICATES FROM it_tlane
  COMPARING locfr locto matnr datfr datto.
  READ TABLE it_tlane ASSIGNING <lfs_tlane> INDEX 1.
  lv_dtfrmin = <lfs_tlane>-datfr.
  lv_dttomax = <lfs_tlane>-datto.
  LOOP AT it_tlane ASSIGNING <lfs_tlane>.
    IF <lfs_tlane>-datfr < lv_dtfrmin.
      lv_dtfrmin = <lfs_tlane>-datfr.
    ENDIF.
    IF <lfs_tlane>-datto > lv_dttomax.
      lv_dttomax = <lfs_tlane>-datto.
    ENDIF.
    AT END OF matnr.
      <lfs_tlane>-datfr = lv_dtfrmin.
      <lfs_tlane>-datto = lv_dttomax.
      lv_tabix = sy-tabix + 1.
      READ TABLE it_tlane ASSIGNING <lfs_tlanetmp> INDEX lv_tabix.
      IF sy-subrc = 0.
        lv_dtfrmin = <lfs_tlanetmp>-datfr.
        lv_dttomax = <lfs_tlanetmp>-datto.
      ENDIF.
    ENDAT.
  ENDLOOP.
  SORT it_tlane BY locfr locto matnr datfr ASCENDING datto DESCENDING.
  DELETE ADJACENT DUPLICATES FROM it_tlane
  COMPARING locfr locto matnr.
*MOD-1771404
* The table /SAPAPO/TRPROD the key field is /SAPAPO/TRPID.
* But over here we are retrieving data from it with respect to
* the source and destination which anyway is unique for a given
* product involved in the lane.
  SELECT locfr locto matid valfr valto
  INTO CORRESPONDING FIELDS OF TABLE lt_tlane_cr
  FROM /sapapo/trprod
  FOR ALL ENTRIES IN it_tlane
  WHERE locfr = it_tlane-lidfr AND
  locto = it_tlane-lidto.
* valfr = it_tlane-datfr and
* valto = it_tlane-datto.
*MOD-1771404
  IF sy-subrc = 0.
    SORT lt_tlane_cr BY locfr locto matid valfr valto.
  ENDIF.
  LOOP AT it_tlane INTO ls_tlane.
    READ TABLE lt_tlane_cr INTO ls_tlane_cr
    WITH KEY locfr = ls_tlane-lidfr
    locto = ls_tlane-lidto
    matid = ls_tlane-matid.
*MOD-1771404
* BINARY SEARCH.
*MOD-1771404
    IF sy-subrc = 0.
      APPEND ls_tlane TO et_tlane_dat.
    ELSE.
* Check whether a product needs to be assigned to an existing
* Tlane
      READ TABLE lt_tlane_cr INTO ls_tlane_cr
      WITH KEY locfr = ls_tlane-lidfr
      locto = ls_tlane-lidto
      BINARY SEARCH.
      IF sy-subrc = 0.
* Mark the record to denote assignment of prod to existing
* Tlane
        ls_tlane-crtyp = 'X'.
      ENDIF.
      IF ls_tlane-locfr = ls_tlane_tmp-locfr AND
      ls_tlane-locto = ls_tlane_tmp-locto.
        ls_tlane-crtyp = 'X'.
      ENDIF.
      APPEND ls_tlane TO et_tlane_new.
    ENDIF.
    ls_tlane_tmp = ls_tlane.
    CLEAR ls_tlane.
  ENDLOOP.
*MOD-1771404
  SORT et_tlane_new BY locfr locto matnr.
  SORT et_tlane_dat BY locfr locto matnr.
  IF NOT et_tlane_new[] IS INITIAL.
    DELETE ADJACENT DUPLICATES FROM et_tlane_new
    COMPARING locfr locto matnr.
  ENDIF.
  IF NOT et_tlane_dat[] IS INITIAL.
    DELETE ADJACENT DUPLICATES FROM et_tlane_dat
    COMPARING locfr locto matnr.
  ENDIF.
*MOD-1771404
ENDFORM. " f_categorise_tlanes
*&---------------------------------------------------------------------*
*& Form F_GET_LOC_DESC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_IT_PROD_DATA text
* <--P_LV_WARE_DESC text
*----------------------------------------------------------------------*
FORM f_get_loc_desc CHANGING lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  FIELD-SYMBOLS:
  <lfs_prod_data> TYPE s_f4_data_loc.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid.
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
  lv_pdctr TYPE char2, " Production center
  lv_whse1 TYPE char3, " Warehouse1
  lv_whse2 TYPE char3. " Warehouse2
* LOOP AT it_prod_data ASSIGNING <lfs_prod_data>.
* Check if the Location is a market
  SELECT markt UP TO 1 ROWS
  INTO lv_mkt
  FROM zap_sign
* where markt = <lfs_prod_data>-locno.
  WHERE markt = s_mkt-low.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
    INTO lv_locid
    FROM /sapapo/loc
    WHERE locno = lv_mkt.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
      INTO lv_ware_desc
      FROM /sapapo/loct
      WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = s_mkt-low.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
* endif.
*MOD-1887120
        EXIT.
      ENDIF.
    ENDIF.
  ENDIF.
* Check if the Location is a Production center
  SELECT pdctr UP TO 1 ROWS
  INTO lv_pdctr
  FROM zap_sign
* WHERE pdctr IN s_mkt.
  WHERE pdctr = s_mkt-low.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_pdctr IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
    INTO lv_locid
    FROM /sapapo/loc
    WHERE locno = lv_pdctr.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
      INTO lv_ware_desc
      FROM /sapapo/loct
      WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = s_mkt-low.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
*MOD-1887120
        EXIT.
      ENDIF.
    ENDIF.
  ENDIF.
* Check if the Location is a Warehouse 1
  SELECT whse1 UP TO 1 ROWS
  INTO lv_whse1
  FROM zap_sign
* WHERE whse1 IN s_mkt.
  WHERE whse1 = s_mkt-low.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_whse1 IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
    INTO lv_locid
    FROM /sapapo/loc
    WHERE locno = lv_whse1.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
      INTO lv_ware_desc
      FROM /sapapo/loct
      WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = lv_whse1.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
*MOD-1887120
        EXIT.
      ENDIF.
    ENDIF.
  ENDIF.
* Check if the Location is a Warehouse 2
  SELECT whse2 UP TO 1 ROWS
  INTO lv_whse2
  FROM zap_sign
* WHERE whse2 IN s_mkt.
  WHERE whse2 = s_mkt-low.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_whse2 IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
    INTO lv_locid
    FROM /sapapo/loc
    WHERE locno = lv_whse2.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
      INTO lv_ware_desc
      FROM /sapapo/loct
      WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
*MOD-1887120
* <LFS_PROD_DATA>-LOCNO = LV_WHSE2.
* <LFS_PROD_DATA>-DESC = lv_ware_desc.
*MOD-1887120
        EXIT.
      ENDIF.
* Location does not exist in APO
      MESSAGE e017.
    ENDIF.
  ENDIF.
* ENDLOOP.
ENDFORM. " F_GET_LOC_DESC
*&---------------------------------------------------------------------*
*& Form GET_DESC_LOC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_<LFS_PROD_DATA> text
* <--P_LV_WARE_DESC text
*----------------------------------------------------------------------*
FORM get_desc_loc USING p_lv_prod_data TYPE s_f4_data_loc
CHANGING p_lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  FIELD-SYMBOLS:
  <lfs_prod_data> TYPE s_f4_data_loc.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid.
  DATA: lv_ware_desc TYPE /sapapo/loc_descr40.
** Local Date Declaration
* DATA: lv_mkt TYPE zdmarkt, " Market
* lv_pdctr TYPE zdpdctr, " Production center
* lv_whse1 TYPE zawhse1, " Warehouse1
* lv_whse2 TYPE zawhse2. " Warehouse2
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
  lv_pdctr TYPE char2, " Production center
  lv_whse1 TYPE char3, " Warehouse1
  lv_whse2 TYPE char3. " Warehouse2
* Check if the Location is a market
  SELECT whse1 UP TO 1 ROWS
  INTO lv_whse1
  FROM zap_sign
* WHERE whse1 eq s_mkt-low.
  WHERE whse1 = p_lv_prod_data-locno.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
    INTO lv_locid
    FROM /sapapo/loc
    WHERE locno = lv_whse1.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
      INTO p_lv_ware_desc
      FROM /sapapo/loct
      WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
        lv_ware_desc = p_lv_ware_desc.
      ENDIF.
      EXIT.
    ENDIF.
  ENDIF.
ENDFORM. " GET_DESC_LOC
*&---------------------------------------------------------------------*
*& Form GET_DESC_WHSE2
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LV_PROD_DATA text
* <--P_LV_WARE_DESC text
*----------------------------------------------------------------------*
FORM get_desc_whse2 USING p_lv_prod_data TYPE s_f4_data_loc
CHANGING p_lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  FIELD-SYMBOLS:
  <lfs_prod_data> TYPE s_f4_data_loc.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid.
  DATA: lv_ware_desc TYPE /sapapo/loc_descr40.
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
  lv_pdctr TYPE char2, " Production center
  lv_whse1 TYPE char3, " Warehouse1
  lv_whse2 TYPE char3. " Warehouse2
*MOD-1771404
* Check if the Location is a market
* SELECT whse2 UP TO 1 ROWS
* INTO lv_whse2
* FROM zm_zap_sign
* WHERE whse1 IN s_mkt.
* ENDSELECT.
*MOD-1771404
  SELECT whse2 UP TO 1 ROWS
  INTO lv_whse2
  FROM zap_sign
  WHERE whse1 IN s_mkt OR
  whse2 IN s_mkt AND
  pcode IN s_prd.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    SELECT locid UP TO 1 ROWS
    INTO lv_locid
    FROM /sapapo/loc
    WHERE locno = lv_whse2.
    ENDSELECT.
    IF sy-subrc = 0.
      SELECT descr40 UP TO 1 ROWS
      INTO p_lv_ware_desc
      FROM /sapapo/loct
      WHERE locid = lv_locid.
      ENDSELECT.
      IF sy-subrc = 0.
        lv_ware_desc = p_lv_ware_desc.
      ENDIF.
      EXIT.
    ENDIF.
  ENDIF.
ENDFORM. " GET_DESC_WHSE2
*&---------------------------------------------------------------------*
*& Form f_clear_tables
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_clear_tables.
  REFRESH: it_zdm0705, it_zdu1002, gt_prod_data,
  gt_core_prod, gt_loc_data.
  CLEAR: it_zdm0705, it_zdu1002, gt_prod_data,
  gt_core_prod, gt_loc_data.
ENDFORM. " f_clear_tables
*Text elements
*----------------------------------------------------------
* 002 Test Delete
* 028 Validity Date Change
* 029 Product Removal
* 030 From date
* 031 To date
* 032 Transportation Lane
* 042 WUF
*Selection texts
*----------------------------------------------------------
* P_DES Destination
* P_FRD From Date
* P_LOG Logical system
* P_MDEL Model
* P_PRD Production
* P_SRC Source
* P_TDAT To Date
* P_TEST Test
* P_TLAN Transportation Lane
* P_TOD To Date
* P_TPRD Product Removal
* S_MKT Market
* S_PRD Product
*----------------------------------------------------------*
*Messages
*----------------------------------------------------------*
*Message class: ZAXXP_SIGN_FPV
*005 no sourcing info is maintained in the sign portfolio
*016 Product does not exist in APO
*017 Location does not exist in APO
*056 Both Product and Location can not be multiple
*059 Product value as * not allowed at the selection screen
*060 Product value as * not allowed at the selection screen
*085 Core Product is Initial.
*086 Product Variant is Initial >
* This report is run in the foreground to remove the product from the
* transportation lane once the products are obsolete. After this the
* program will also delete the products from the corresponding markets
* warehouses and production center locations.
*&************************************************************************
* PROGRAM--INITIAL CREATION
* This report is run in the foreground to remove the product from the
* transportation lane once the products are obsolete. After this the
* program will also delete the products from the corresponding markets
* warehouses and production center locations.
************************************************************************
************************************************************************
* PROGRAM--Modification*MOD-1771404
* Program name : ZAXXPR_DELETE_MASTER_DATA
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2008
* Initial Change request number : PMI_SIGN_0002
* Author : Aveek Ghose.
* Description :
*
* This report is run in the foreground to remove the product from the
* transportation lane once the products are obsoete. After this the
* program will also delete the products from the corresponding markets
* warehouses and production center locations.
************************************************************************
************************************************************************
* MOD-1887120
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 10.03.2008
* Initial Change request number : PMI_SIGN_2
* Author : Aveek Ghose
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA.
************************************************************************
REPORT zaxxpr_delete_master_data MESSAGE-ID zaxxp_sign_fpv.
* Include for global data and constants declaration
* INCLUDE zaxxpr_delete_master_data_top.
* include ZAXXPR_DEL_MASTER_DATA_TOP.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DEL_MASTER_DATA_TOP
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_TOP
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--INITIAL CREATION
* Description XXXXXXXXXX :
*
* This is the Top include that contains the global data declarations
* and the selection screen of the main program
* ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
* PROGRAM--Modification: MOD-1771404
* Program name : ZAXXPR_DELETE_MASTER_DATA_TOP
* Reference program (copied from) : na.
* Development specification ID :
* Date created : 15.02.2006
* Initial Change request number : PMI_SIGN_0002
* Author : Aveek Ghose..
* Description :
*
* This is the Top include that contains the global data declarations
* and the selection screen of the main program
* ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
**********************TYPES DECLARATION***************************
************************************************************************
TYPE-POOLS: slis...
* Types dclaration for view table data.
TYPES: BEGIN OF s_view_data,
  pcode TYPE matnr, " Product
  maktx TYPE char40, " Prod Desc
  markt TYPE char4, " Market
  martx TYPE char20, " Market Desc
  pdctr TYPE char2, " Product Center
  pctxt TYPE char20, " Description
  whse1 TYPE char3, " Warehouse1
  whse2 TYPE char3, " Warehouse2
  prvar TYPE char18, " Product Variant
  vartx TYPE char30, " Variant Description
  status TYPE char1, " Status
END OF s_view_data,
t_view_data TYPE STANDARD TABLE OF s_view_data.
*MOD-1771404*****
TYPES: BEGIN OF l_view_data,
  pcode TYPE matnr, " Product
  markt TYPE char4, " Market
  pdctr TYPE char2, " Product Center
  whse1 TYPE char3, " Warehouse1
  whse2 TYPE char3, " Warehouse2
  prvar TYPE char18, " Product Variant
  status TYPE char1, " Status
END OF l_view_data,
lv_view_data TYPE STANDARD TABLE OF s_view_data.
*MOD-1771404****
* Types declaration for F4 help Product data
TYPES: BEGIN OF s_f4_data_matnr,
  pcode TYPE matnr, " Product
  maktx TYPE maktx, " Description
END OF s_f4_data_matnr,
t_f4_data_matnr TYPE STANDARD TABLE OF s_f4_data_matnr.
* Types declaration for F4 help Location data
TYPES: BEGIN OF s_f4_data_loc,
  locno TYPE /sapapo/locno, " Location
  desc TYPE /sapapo/loc_descr40, " Description
  pcode TYPE /sapapo/matnr, " Product
  prvar TYPE /sapapo/matnr, " Product Variant
*  MOD-012
  pdctr TYPE char2, "Production Center
*  MOD-012
  vartx TYPE char30, " Variant Description
  maktx TYPE char40, " Prod Desc
  status TYPE char1, " Status
  loctyp TYPE /sapapo/c_loctype, " Location Type
  whse1 TYPE char3,
  whse2 TYPE char3,
  lv_exist TYPE i, " Variable
END OF s_f4_data_loc,
t_f4_data_loc TYPE STANDARD TABLE OF s_f4_data_loc.
* Types declaration for Warehouse description
TYPES: BEGIN OF s_whse,
  whse TYPE char3, " Location
  whdesc TYPE char30, " Description
END OF s_whse,
t_whse TYPE STANDARD TABLE OF s_whse.
* Types declaration for F4 help Location data
TYPES: BEGIN OF s_f4_loc,
  locno TYPE /sapapo/locno, " Location
  desc TYPE /sapapo/loc_descr40, " Description
END OF s_f4_loc,
t_f4_loc TYPE STANDARD TABLE OF s_f4_loc.
* Types declaration for SCREEN TABLE used in submit statement
TYPES: t_rsparams TYPE STANDARD TABLE OF rsparams.
* Types declaration for Transportation Lane data
TYPES: BEGIN OF s_tlane_data,
  trpid TYPE /sapapo/tr_trpid, " Trip Id
  locto TYPE /sapapo/locno, " Location to
  matid TYPE matnr, " Material Id
  locfr TYPE /sapapo/locno, " Location from
END OF s_tlane_data,
t_tlane_data TYPE STANDARD TABLE OF s_tlane_data.
* Types Declaration for Range Table
TYPES: BEGIN OF s_range_matid,
  sign TYPE char1, " Sign
  option TYPE char2, " Option
  low TYPE /sapapo/matid, " Matid low
  high TYPE /sapapo/matid, " Matid high
END OF s_range_matid,
t_matid_range TYPE STANDARD TABLE OF s_range_matid.
* Types Declaration for Range Table
TYPES: BEGIN OF s_range_locid,
  sign TYPE char1, " Sign
  option TYPE char2, " Option
  low TYPE /sapapo/locid, " Locid low
  high TYPE /sapapo/locid, " locid high
END OF s_range_locid,
t_locid_range TYPE STANDARD TABLE OF s_range_locid.
* Types Declaration for ALV Display
TYPES: BEGIN OF s_display,
  prod TYPE /sapapo/matnr, " Product
  p_des TYPE char40, " Product Desc
  loc TYPE /sapapo/locno, " Location
  l_des TYPE /sapapo/loc_descr40, " Location Desc
  status TYPE char15, " Deletion status
  light TYPE iconname, " Light Indicator
END OF s_display,
t_display TYPE STANDARD TABLE OF s_display.
TYPES: BEGIN OF s_displayprd,
  prod TYPE /sapapo/matnr, " Product
  p_des TYPE char40, " Product Desc
  status TYPE char15, " Deletion status
  light TYPE iconname, " Light Indicator
END OF s_displayprd,
t_displayprd TYPE STANDARD TABLE OF s_displayprd.
* Types Declaration for Product select options
TYPES: BEGIN OF s_range_product,
  sign TYPE char1, " Sign
  option TYPE char2, " Option
  low TYPE matnr, " Product low
  high TYPE matnr, " Product high
END OF s_range_product.
* Types Declaration for Location select option
TYPES: BEGIN OF s_range_location,
  sign TYPE char1, " Sign
  option TYPE char2, " Option
  low TYPE /sapapo/locno, " Location low
  high TYPE /sapapo/locno, " Location high
END OF s_range_location.
* Types Declaration for Matids for the Products
TYPES: BEGIN OF s_matid,
  matid TYPE /sapapo/matid, " Matid
  matnr TYPE matnr, " Product
END OF s_matid,
t_matid TYPE STANDARD TABLE OF s_matid.
* Types Declaration for Locid and Locations
TYPES: BEGIN OF s_locid,
  locid TYPE /sapapo/locid, " Matid
  locno TYPE /sapapo/locno, " Product
END OF s_locid,
t_locid TYPE STANDARD TABLE OF s_locid.
TYPES: BEGIN OF s_tlaneupd,
  locfr TYPE /sapapo/locno,
  locto TYPE /sapapo/locno,
  matnr TYPE matnr,
  matid TYPE /sapapo/matid,
  msg TYPE /fre/bif_messagetext,
END OF s_tlaneupd,
t_tlaneupd TYPE STANDARD TABLE OF s_tlaneupd.
TYPES: BEGIN OF s_matloc,
  matnr TYPE matnr,
  locno TYPE /sapapo/locno,
END OF s_matloc,
t_matloc TYPE STANDARD TABLE OF s_matloc.
*MOD-1771404
TYPES: t_masterdata TYPE STANDARD TABLE OF zap_scmpf,
        s_tship TYPE zap_tshpt,
        t_tship TYPE STANDARD TABLE OF s_tship,
        t_log_table TYPE STANDARD TABLE OF bal_s_msg,
        t_log_location TYPE STANDARD TABLE OF bal_s_msg,
        t_log_product TYPE STANDARD TABLE OF bal_s_msg,
        t_log_locprd TYPE STANDARD TABLE OF bal_s_msg,
        t_log_tlane TYPE STANDARD TABLE OF bal_s_msg.
TYPES: BEGIN OF s_location,
  locno TYPE /sapapo/locno,
  loctp TYPE /sapapo/c_loctype,
  locct TYPE char1,
END OF s_location,
t_location TYPE STANDARD TABLE OF s_location,
BEGIN OF s_product,
  matnr TYPE /sapapo/matnr,
  matct TYPE char1,
END OF s_product,
t_product TYPE STANDARD TABLE OF s_product,
BEGIN OF s_tlane,
  locfr TYPE /sapapo/locno,
  locto TYPE /sapapo/locno,
  matnr TYPE /sapapo/matnr,
  ltpfr TYPE /sapapo/c_loctype,
  ltpto TYPE /sapapo/c_loctype,
  lidfr TYPE /sapapo/locid,
  lidto TYPE /sapapo/locid,
  matid TYPE /sapapo/matid,
  datfr TYPE timestamp,
  datto TYPE timestamp,
  crtyp TYPE flag,
  lv_exist TYPE i,
END OF s_tlane,
t_tlane TYPE STANDARD TABLE OF s_tlane,
BEGIN OF s_loc_extloc,
  locno TYPE /sapapo/locno,
  extloc TYPE /sapapo/loc_extlocno,
END OF s_loc_extloc,
t_loc_extloc TYPE STANDARD TABLE OF s_loc_extloc,
BEGIN OF s_prd_extprd,
  matnr TYPE /sapapo/matnr,
  extmat TYPE /sapapo/ext_matnr,
END OF s_prd_extprd,
t_prd_extprd TYPE STANDARD TABLE OF s_prd_extprd,
BEGIN OF s_locprd,
  locno TYPE /sapapo/locno,
  matnr TYPE /sapapo/matnr,
  loctp TYPE /sapapo/c_loctype,
  prctp TYPE /sapapo/beskz,
END OF s_locprd,
t_locprd TYPE STANDARD TABLE OF s_locprd.
DATA: t_sign TYPE STANDARD TABLE OF zap_sign.
DATA: t_sign1 TYPE STANDARD TABLE OF zap_sign.
DATA: it_zdu1002 TYPE STANDARD TABLE OF zdu1002.
DATA: it_zdm0705 TYPE STANDARD TABLE OF zdm0705.
*MOD-1771404
************************************************************************
******************CONSTANTS DECLARATION***************************
************************************************************************
CONSTANTS:
* Selection Screen field names
gc_prd_low TYPE dynfnam VALUE 'S_PRD-LOW',
gc_prd_high TYPE dynfnam VALUE 'S_PRD-HIGH',
gc_mkt_low TYPE dynfnam VALUE 'S_MKT-LOW',
gc_mkt_high TYPE dynfnam VALUE 'S_MKT-HIGH',
* Transaction Name and Program Names
gc_bp2 TYPE baltcode VALUE '/SAPAPO/BP2',
gc_slg1 TYPE baltcode VALUE 'SLG1',
gc_mode_e TYPE ctu_mode VALUE 'E',
gc_mode_n TYPE ctu_mode VALUE 'N',
gc_foreground TYPE char1 VALUE 'A',
gc_rscsel TYPE dynfnam VALUE 'RSCSEL-SLOW_I',
gc_l_brac TYPE char1 VALUE '(',
gc_r_brac TYPE char1 VALUE ')',
* Selection Screen Elements for /SAPAPO/RLCDELETE
gc_radio_btn TYPE rsscr_kind VALUE 'R',
gc_parameter TYPE rsscr_kind VALUE 'P',
gc_sel_optn TYPE rsscr_kind VALUE 'S',
gc_sign_i TYPE tvarv_sign VALUE 'I',
gc_option_eq TYPE tvarv_opti VALUE 'EQ',
gc_manual_sel TYPE rsscr_name VALUE 'SHUF_PA1',
gc_product TYPE rsscr_name VALUE 'SEL_PROD',
gc_location TYPE rsscr_name VALUE 'SEL_LOC',
gc_plan_vrsn TYPE rsscr_name VALUE 'P_VERSIO',
gc_fixed_ord TYPE rsscr_name VALUE 'P_FIXED',
gc_category TYPE rsscr_name VALUE 'RB_CAT',
gc_key_fig TYPE rsscr_name VALUE 'RB_KEYF',
gc_only_apo TYPE rsscr_name VALUE 'P_APO',
gc_test_run TYPE rsscr_name VALUE 'P_TEST',
gc_pvrsn_000 TYPE tvarv_val VALUE '000',
* Selection screen fields for /SAPAPO/DELETE_PP_ORDER
gc_pl_vrsn TYPE rsscr_name VALUE 'LV_SIMID',
gc_prod TYPE rsscr_name VALUE 'LT_MATAB',
gc_loc TYPE rsscr_name VALUE 'LT_LOTAB',
gc_plan_prod TYPE rsscr_name VALUE 'LV_EXT',
gc_contract TYPE rsscr_name VALUE 'LV_SUBCO',
gc_display_ord TYPE rsscr_name VALUE 'LV_LIST',
gc_delete_ord TYPE rsscr_name VALUE 'LV_DEL',
gc_send_delete TYPE rsscr_name VALUE 'LV_EVENT',
* ALV grid display fields
gc_pos_1 TYPE lvc_colpos VALUE '0001',
gc_pos_2 TYPE lvc_colpos VALUE '0002',
gc_pos_3 TYPE lvc_colpos VALUE '0003',
gc_pos_4 TYPE lvc_colpos VALUE '0004',
gc_pos_5 TYPE lvc_colpos VALUE '0005',
gc_pos_6 TYPE lvc_colpos VALUE '0006',
gc_prodct TYPE lvc_fname VALUE 'PROD',
gc_status TYPE lvc_fname VALUE 'STATUS',
gc_light TYPE lvc_fname VALUE 'LIGHT',
gc_prod_des TYPE lvc_fname VALUE 'P_DES',
gc_loctn TYPE lvc_fname VALUE 'LOC',
gc_loc_des TYPE lvc_fname VALUE 'L_DES',
gc_tabname TYPE lvc_tname VALUE 'LT_DISPLAY',
gc_left TYPE lvc_just VALUE 'L',
gc_center TYPE lvc_just VALUE 'C',
gc_len_prod TYPE lvc_outlen VALUE '18',
gc_len_p_des TYPE lvc_outlen VALUE '40',
gc_len_loc TYPE lvc_outlen VALUE '20',
gc_len_l_des TYPE lvc_outlen VALUE '30',
gc_len_stat TYPE lvc_outlen VALUE '20',
gc_len_lt TYPE lvc_outlen VALUE '5',
gc_set_pf TYPE slis_formname VALUE 'F_SET_PF',
gc_show_log TYPE slis_formname VALUE 'F_SHOW_LOG',
gc_lt_top TYPE slis_formname VALUE 'F_TOP_TL',
gc_tlupd_top TYPE slis_formname VALUE 'F_TOP_TLUPD',
gc_prd_top TYPE slis_formname VALUE 'F_TOP_PRD',
gc_td_log TYPE gui_code VALUE 'TD_LOG',
gc_tlane TYPE gui_code VALUE 'TLANE',
gs_type_s TYPE char1 VALUE 'S',
gs_type_h TYPE char1 VALUE 'H',
gs_type_a TYPE char1 VALUE 'A',
gc_save TYPE char1 VALUE 'A',
gc_red TYPE iconname VALUE ' (5C)',
gc_green TYPE iconname VALUE ' (5B)',
* ALV display for Tlane
gc_prod_tl TYPE lvc_fname VALUE 'MATID',
gc_locto TYPE lvc_fname VALUE 'LOCTO',
gc_locfr TYPE lvc_fname VALUE 'LOCFR',
gc_matnr TYPE lvc_fname VALUE 'MATNR',
gc_msg TYPE lvc_fname VALUE 'MSG',
gc_tl_tab TYPE lvc_tname VALUE 'GT_TLANE',
gc_tl_tabu TYPE lvc_tname VALUE 'IT_TLANEUPD',
* Others
gc_s TYPE ddbool_d VALUE 'S',
gc_inactive TYPE char1 VALUE 'I',
gc_set TYPE char1 VALUE 'X',
gc_set_val TYPE tvarv_val VALUE 'X',
gc_int_commit TYPE bapi10001commctrl VALUE 'E',
gc_bs_group TYPE /sapapo/logqs VALUE 'BSGAPO',
gc_error TYPE bapi_mtype VALUE 'E',
gc_abend TYPE bapi_mtype VALUE 'A',
gc_dot TYPE char1 VALUE '.',
gc_colon TYPE char1 VALUE ':',
gc_space TYPE char4 VALUE ' ',
gc_star TYPE char1 VALUE '*',
*MOD-1771404
gc_core TYPE char1 VALUE 'C',
gc_var TYPE char1 VALUE 'V',
* Message class name
gc_msgid TYPE symsgid VALUE 'ZAXXP_SIGN_FPV',
* Message types
gc_warn TYPE symsgty VALUE 'W',
gc_err TYPE symsgty VALUE 'E',
gc_succ TYPE symsgty VALUE 'S',
* Message nos
gc_msgno_021 TYPE symsgno VALUE '021',
gc_msgno_023 TYPE symsgno VALUE '023',
gc_msgno_024 TYPE symsgno VALUE '024',
gc_msgno_025 TYPE symsgno VALUE '025',
gc_msgno_026 TYPE symsgno VALUE '026',
gc_msgno_027 TYPE symsgno VALUE '027',
gc_msgno_028 TYPE symsgno VALUE '028',
gc_msgno_029 TYPE symsgno VALUE '029',
gc_msgno_030 TYPE symsgno VALUE '030',
gc_msgno_031 TYPE symsgno VALUE '031',
gc_msgno_032 TYPE symsgno VALUE '032',
gc_msgno_033 TYPE symsgno VALUE '033',
gc_msgno_034 TYPE symsgno VALUE '034',
gc_msgno_035 TYPE symsgno VALUE '035',
gc_msgno_037 TYPE symsgno VALUE '037',
gc_msgno_038 TYPE symsgno VALUE '038',
gc_msgno_039 TYPE symsgno VALUE '039',
gc_msgno_040 TYPE symsgno VALUE '040',
gc_msgno_041 TYPE symsgno VALUE '041',
gc_msgno_042 TYPE symsgno VALUE '042',
gc_msgno_043 TYPE symsgno VALUE '043',
gc_msgno_044 TYPE symsgno VALUE '044',
gc_msgno_050 TYPE symsgno VALUE '050',
gc_msgno_051 TYPE symsgno VALUE '051',
gc_msgno_052 TYPE symsgno VALUE '052',
gc_msgno_053 TYPE symsgno VALUE '053',
gc_msgno_054 TYPE symsgno VALUE '054',
gc_msgno_055 TYPE symsgno VALUE '055',
gc_msgno_061 TYPE symsgno VALUE '061',
gc_msgno_075 TYPE symsgno VALUE '075',
gc_msgno_076 TYPE symsgno VALUE '076',
gc_msgno_078 TYPE symsgno VALUE '078',
gc_msgno_087 TYPE symsgno VALUE '087',
gc_msgno_096 TYPE symsgno VALUE '096',
gc_msgno_98 TYPE symsgno VALUE '098',
gc_msgno_100 TYPE symsgno VALUE '100',
* Location types
gc_ltp_1001 TYPE /sapapo/c_loctype VALUE '1001', "For Prd centre
gc_ltp_1002 TYPE /sapapo/c_loctype VALUE '1002', "For Mkt/whouse
* Procurement types
gc_ptp_x TYPE /sapapo/beskz VALUE 'X', "For Prd centre
gc_ptp_f TYPE /sapapo/beskz VALUE 'F', "For Mkt/whouse
* Variant status
gc_stat_p TYPE char1 VALUE 'P',
gc_stat_a TYPE char1 VALUE 'A',
* Timestamps
gc_startti TYPE syuzeit VALUE '000000',
gc_endti TYPE syuzeit VALUE '235959',
* Default means of transport
gc_ttype_0001 TYPE /sapapo/tr_traty VALUE '0001',
* Business system grp
gc_bsgapo TYPE /sapapo/loc_logqs VALUE 'BSGAPO',
gc_utc TYPE timezone VALUE 'UTC'. "UTC time zome
*MOD-1771404
************************************************************************
**********************DATA DECLARATION****************************
************************************************************************
DATA: gv_flgstart TYPE flag,
      gv_prd TYPE matnr,
      gv_mkt TYPE /sapapo/locno,
      gv_date_before TYPE sy-datum,
      gv_time_before TYPE sy-uzeit,
      gv_flg_error TYPE char1,
      gv_msg_error TYPE char1. " Flag for Call Tr fail
* Internal table declarations
DATA: gt_f4_data_matnr TYPE STANDARD TABLE OF s_f4_data_matnr,
      gt_f4_data_loc TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_matloc TYPE t_matloc,
      gt_prod_data TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_product TYPE STANDARD TABLE OF bapi10001key,
*      MOD-1771404
      gt_new_product TYPE STANDARD TABLE OF s_product,
      gt_location TYPE STANDARD TABLE OF s_location,
      gt_new_tlane TYPE STANDARD TABLE OF s_tlane,
*      MOD-1771404
      gt_core_prod TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_prod_var TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_loc_data TYPE STANDARD TABLE OF s_f4_data_loc,
      gt_tlane TYPE STANDARD TABLE OF s_tlane_data,
      gt_matid_all TYPE STANDARD TABLE OF s_matid,
      gt_view_data TYPE STANDARD TABLE OF s_view_data,
      gt_bdc_tab TYPE STANDARD TABLE OF bdcdata,
      gt_variant_table TYPE STANDARD TABLE OF rsparams,
      gt_fcat TYPE STANDARD TABLE OF slis_fieldcat_alv,
      gt_top_page TYPE slis_t_listheader,
      gt_masterdata TYPE t_masterdata,
      gt_tship TYPE t_tship,
      gt_whse TYPE t_whse,
*      MOD-1771404
      gt_log_location TYPE t_log_location,
      gt_log_product TYPE t_log_product,
      gt_log_locprd TYPE t_log_locprd,
      gt_log_tlane TYPE t_log_tlane.
DATA:
  lt_tlane TYPE t_tlane,
  lt_tlane_new TYPE t_tlane,
  lt_tlane_dat TYPE t_tlane,
  lt_location TYPE t_location,
  lt_product TYPE t_product,
  lt_prd_extprd TYPE t_prd_extprd,
  lt_loc_extloc TYPE t_loc_extloc,
  lt_locprd TYPE t_locprd.
DATA: l_wa_zdm0705 TYPE zdm0705.
DATA: l_wa_zdu1002 TYPE zdu1002.
DATA: lv_prvar TYPE char40.
*MOD-1771404
************************************************************************
*****************SELECTION SCREEN DEFINITION**********************
************************************************************************
* Block to enter the product and location data
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
* Option to delete master data
*************************************************************
*MOD-001
*************************************************************
* SELECTION-SCREEN: BEGIN OF LINE,
* COMMENT 5(27) text-033 FOR FIELD p_mdel.
* PARAMETERS: p_mdel RADIOBUTTON GROUP grp1 USER-COMMAND ucom
* DEFAULT 'X'.
*
PARAMETERS: p_mdel RADIOBUTTON GROUP grp1.
*
* PARAMETERS p_mdel AS CHECKBOX DEFAULT 'X'.
*
* SELECTION-SCREEN END OF LINE.
* SELECTION-SCREEN SKIP.
* selection-screen: begin of line,
* comment (79) text-043.
* selection-screen end of line.
* selection-screen: begin of line,
* comment (79) text-044.
* selection-screen end of line.
* SELECTION-SCREEN SKIP.
**************************************************************
*MOD-001
*************************************************************
PARAMETERS: p_log TYPE logsys OBLIGATORY MEMORY ID mid.
SELECT-OPTIONS:
s_prd FOR gv_prd OBLIGATORY, " Product variant
s_mkt FOR gv_mkt OBLIGATORY. " Market
SELECTION-SCREEN: SKIP.
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN POSITION 33.
PARAMETERS: p_test AS CHECKBOX DEFAULT 'X'.                 "#EC *
* No Selection Text Maintained as we are displaying comment for it
SELECTION-SCREEN COMMENT 1(16) text-002 FOR FIELD p_test.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN PUSHBUTTON 5(10) text-042 USER-COMMAND wuf.
*MOD-1771404
* Option to update transportation lanes
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-032 FOR FIELD p_tlan.
PARAMETERS: p_tlan RADIOBUTTON GROUP grp1.
SELECTION-SCREEN END OF LINE.
* Sourcing for tlane
SELECTION-SCREEN SKIP.
PARAMETERS: p_src TYPE /sapapo/locno OBLIGATORY,
p_des TYPE /sapapo/locno OBLIGATORY,
p_prd TYPE matnr OBLIGATORY.
* Radio button for validity date change
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-028 FOR FIELD p_tdat.
PARAMETERS: p_tdat RADIOBUTTON GROUP grp USER-COMMAND ucom
MODIF ID mod DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
* Fields for dates
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 10(15) text-030 FOR FIELD p_frd.
PARAMETERS p_frd TYPE sydatum.
SELECTION-SCREEN: COMMENT 40(10) text-031 FOR FIELD p_tod.
PARAMETERS p_tod TYPE sydatum.
SELECTION-SCREEN: END OF LINE.
* Radio button for product removal
SELECTION-SCREEN SKIP.
SELECTION-SCREEN: BEGIN OF LINE,
COMMENT 5(27) text-029 FOR FIELD p_tprd.
PARAMETERS: p_tprd RADIOBUTTON GROUP grp.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b1.
*MOD-1771404
************************************************************************
**********************INITIALIZATION******************************
************************************************************************
INITIALIZATION.
  CLEAR gv_flgstart.
* Gets Product and location data for populatiing the search helps
  PERFORM f_get_f4_data CHANGING gt_f4_data_matnr
                                  gt_f4_data_loc
                                  gt_view_data.
* Adjusts screen fields
  PERFORM f_adj_screen_flds.
***********************************************************************
****************SELECTION SCREEN EVENTS**************************
***********************************************************************
AT SELECTION-SCREEN ON s_prd.
* Validates product against the SIGN view
  PERFORM f_validate_product.

AT SELECTION-SCREEN ON s_mkt.
* Validate market against the SIGN view
  PERFORM f_validate_market.
* Custom search help for Product-Low
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-low.
  PERFORM f_f4_for_prodcentre USING gc_prd_low
                                    gt_f4_data_matnr.
* Custom search help for Product-High
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prd-high.
  PERFORM f_f4_for_prodcentre USING gc_prd_high
                                    gt_f4_data_matnr.
* Custom search help for Location-Low
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-low.
  PERFORM f_f4_for_market USING gc_mkt_low
                                gt_f4_data_loc.
* Custom search help for Location-High
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_mkt-high.
  PERFORM f_f4_for_market USING gc_mkt_high
                                gt_f4_data_loc.
* Adjusting the screen fields
AT SELECTION-SCREEN OUTPUT.
  CLEAR gv_flgstart.
*mod-099
  PERFORM f_clear_tables.
*mod-099
  PERFORM f_adj_screen_flds.

AT SELECTION-SCREEN.
  IF sy-ucomm = 'WUF'.
    REFRESH gt_matloc.
    PERFORM f_check_product_status USING gt_f4_data_loc
    CHANGING gt_prod_data
              gt_core_prod
              gt_prod_var
              gt_loc_data
              gt_matloc.
* MOD-1887120 *
    SORT gt_core_prod BY locno pcode.
    DELETE ADJACENT DUPLICATES FROM gt_core_prod
      COMPARING locno
                pcode.
    SORT gt_prod_var BY locno pcode prvar.
    DELETE ADJACENT DUPLICATES FROM gt_prod_var
      COMPARING locno
                pcode
                prvar.
* MOD-1887120 *
* Call the WUF transaction
    PERFORM launch_wuf USING gt_core_prod
                                gt_prod_var.
  ENDIF.
************************************************************************
**********************START OF SELECTION**************************
************************************************************************
START-OF-SELECTION.
  gv_flgstart = 'X'.
  IF p_mdel IS NOT INITIAL.
* Filter all the Products selected for deletion with status inactive
    REFRESH gt_matloc.
* wait up to 5 seconds.
    PERFORM f_check_product_status USING gt_f4_data_loc
      CHANGING gt_prod_data
                gt_core_prod
                gt_prod_var
                gt_loc_data
                gt_matloc.
* Set the Date and Time before deleting the Data.
    gv_date_before = sy-datum.
    gv_time_before = sy-uzeit.
    IF gt_core_prod IS INITIAL.
      MESSAGE i085.
    ENDIF.
    IF gt_prod_var IS INITIAL.
      MESSAGE i086.
      LEAVE LIST-PROCESSING.
    ENDIF.
    IF p_test IS INITIAL.
* Delete the Transactional data for the Product and Location Combination
* Using the call transaction /SAPAPO/BP2
*MOD-1887120
      SORT gt_core_prod BY locno pcode.
      DELETE ADJACENT DUPLICATES FROM gt_core_prod
        COMPARING locno
                  pcode.
      SORT gt_prod_var BY locno pcode prvar.
      DELETE ADJACENT DUPLICATES FROM gt_prod_var
        COMPARING locno
                  pcode
                  prvar.
      SORT gt_loc_data BY locno.
      DELETE ADJACENT DUPLICATES FROM gt_loc_data
        COMPARING locno.
*MOD-1887120
      PERFORM f_delete_transactional_data USING gt_core_prod
                                                gt_prod_var
                                                gt_loc_data
                                        CHANGING gt_prod_data
                                                  gt_bdc_tab.
* Delete the Transactional data created manually by submitting to the
* SAP standard program /SAPAPO/RLCDEL via selection screen variant.
      PERFORM f_delete_manual_created_data USING gt_core_prod
                                                  gt_prod_var
                                                  gt_loc_data
                                          CHANGING gt_variant_table.
* Delete the PPDS orders for selection screen Product and Location
* Done by submitting to SAP Standard program /SAPAPO/DELETE_PP_ORDER
* Via screen table.
      PERFORM f_delete_ppds_orders USING gt_core_prod
                                          gt_prod_var
                                          gt_loc_data
                                CHANGING gt_variant_table.
    ENDIF.
* Delete Products from the Transportation Lanes for the Products and
* locations entered at the selection screen.
    PERFORM f_delete_product_from_tlane USING gt_core_prod
                                              gt_prod_var
                                      CHANGING gt_loc_data
                                                gt_matid_all
                                                gt_tlane.
* Run the Transportation Lane consistency check program
* PERFORM f_run_consistency_check.
* Set the Deletion flag for the Products selected for deletion.
    IF p_test IS INITIAL.
      PERFORM f_set_deletion_flag USING gt_core_prod
                                        gt_prod_var
                                        gt_loc_data
                                        gt_matloc.
    ENDIF.
* Display the ALV Report.
    PERFORM f_display_alv USING gt_prod_data
                                gt_matloc
                        CHANGING gt_fcat
                                  gt_matid_all.
  ENDIF.
* If the user chooses to update the transportation lane
**MOD-1771404
  IF p_tlan IS NOT INITIAL.
*MOD-1771404
* Retrieve the master data from teh SIGN portfolio meeting the user
* selection
    PERFORM f_retrieve_masterdata CHANGING gt_masterdata
                                            gt_tship
                                            gt_whse.
* Collect all possible transportation lanes
    PERFORM f_collect_tlanes USING gt_masterdata
                                    gt_tship
                                    gt_location
                                    gt_new_product
                          CHANGING gt_new_tlane.
    IF gt_new_tlane IS INITIAL.
      EXIT.
    ENDIF.
* The Tlanes can be categorised into 3 categories:
* 1> To be created fresh
* 2> Exists, but new product need to be assigned
* 3> Only validity dates need to be changed
    PERFORM f_categorise_tlanes USING gt_new_tlane
    CHANGING lt_tlane_new
    lt_tlane_dat.
*MOD-1771404 changed by Aveek
    PERFORM f_update_tlane.
*MOD-1771404 changed by Aveek
* PERFORM f_update_tlane using gt_new_tlane.
*MOD-1771404
  ENDIF.
** Include for subroutine declaration
  INCLUDE zaxxpr_delete_master_data_f02.
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Include ZAXXPR_DELETE_MASTER_DATA_F01
*&---------------------------------------------------------------------*
************************************************************************
* PROGRAM--INITIAL CREATION
*
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA...
************************************************************************
************************************************************************
* PROGRAM--Modification MOD-1771404
************************************************************************
*
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Process team contact person/team name: Nandakumar
* Date created : 15.02.2006
* Initial Change request number : PMI_SIGN_2
* Author : Aveek Ghose..
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA
************************************************************************
************************************************************************
* MOD-1887120
* Program name : ZAXXPR_DELETE_MASTER_DATA_F01
* Reference program (copied from) : na.
* Development specification ID :
* Process team contact person/team name: Nandakumar
* Date created : 10.03.2006
* Initial Change request number : PMI_SIGN_2
* Author : Aveek Ghose
* Description :
*
* This is the Form include that contains the local subroutines of the
* main program ZAXXPR_DELETE_MASTER_DATA
************************************************************************
*&---------------------------------------------------------------------*
*& Form f_validate_product
*&---------------------------------------------------------------------*
* Validates the product
*----------------------------------------------------------------------*
FORM f_validate_product.
* Local Date Declaration
  DATA: lv_prd TYPE matnr,
        lv_matnr TYPE /sapapo/matnr.
* Field symbols:
  FIELD-SYMBOLS:
    <lfs_prd> TYPE s_range_product.
  IF p_mdel IS INITIAL.
    EXIT.
  ENDIF.
* Both Product and Location can not be a range at the selection screen
  IF s_prd-low IS NOT INITIAL AND
      s_prd-high IS NOT INITIAL AND
      s_mkt-low IS NOT INITIAL AND
      s_mkt-high IS NOT INITIAL.
* Error Message: Both Product and Location can not be multiple
    MESSAGE e056.
  ENDIF.
* Check if both Product and location dont have multiple entries
  READ TABLE s_prd INDEX 2 TRANSPORTING NO FIELDS.
  IF sy-subrc = 0.
    READ TABLE s_mkt INDEX 2 TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
* Error Message: Both Product and Location can not be multiple
      MESSAGE e056.
    ENDIF.
  ENDIF.
* Check if user has entered * in the select option for location.
  READ TABLE s_prd ASSIGNING <lfs_prd> WITH KEY low = gc_star.
  IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
    MESSAGE e059.
  ELSE.
    READ TABLE s_prd ASSIGNING <lfs_prd> WITH KEY high = gc_star.
    IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
      MESSAGE e059.
    ENDIF.
  ENDIF.
  SELECT pcode UP TO 1 ROWS
    INTO lv_prd
    FROM zap_sign
    WHERE pcode IN s_prd.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_prd IS INITIAL.
* Product does not exist in APO
    MESSAGE e016.
  ENDIF.
ENDFORM. " f_validate_product
*&---------------------------------------------------------------------*
*& Form f_validate_market
*&---------------------------------------------------------------------*
* Validates the market
*----------------------------------------------------------------------*
FORM f_validate_market .
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3. " Warehouse2
* Field Symbols:
  FIELD-SYMBOLS:
  <lfs_mkt> TYPE s_range_location.
  IF p_mdel IS INITIAL.
    EXIT.
  ENDIF.
* Both Product and Location can not be multiple at the selection screen
  IF s_prd-low IS NOT INITIAL AND
      s_prd-high IS NOT INITIAL AND
      s_mkt-low IS NOT INITIAL AND
      s_mkt-high IS NOT INITIAL.
* Error Message: Both Product and Location can not be multiple
    MESSAGE e056.
  ENDIF.
* Check if both Product and location dont have multiple entries
  READ TABLE s_prd INDEX 2 TRANSPORTING NO FIELDS.
  IF sy-subrc = 0.
    READ TABLE s_mkt INDEX 2 TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
* Error Message: Both Product and Location can not be multiple
      MESSAGE e056.
    ENDIF.
  ENDIF.
* Check if user has entered * in the select option for location.
  READ TABLE s_mkt ASSIGNING <lfs_mkt> WITH KEY low = gc_star.
  IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
    MESSAGE e060.
  ELSE.
    READ TABLE s_mkt ASSIGNING <lfs_mkt> WITH KEY high = gc_star.
    IF sy-subrc = 0.
* Product value as * not allowed at the selection screen
      MESSAGE e060.
    ENDIF.
  ENDIF.
* Check if the Location is a market
  SELECT markt UP TO 1 ROWS
    INTO lv_mkt
    FROM zap_sign
    WHERE markt IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_mkt IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Production center
  SELECT pdctr UP TO 1 ROWS
    INTO lv_pdctr
    FROM zap_sign
    WHERE pdctr IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_pdctr IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Warehouse 1
  SELECT whse1 UP TO 1 ROWS
    INTO lv_whse1
    FROM zap_sign
    WHERE whse1 IN s_mkt.
  ENDSELECT.
  IF sy-subrc = 0 OR
  lv_whse1 IS NOT INITIAL.
    EXIT.
  ENDIF.
* Check if the Location is a Warehouse 2
  SELECT whse2 UP TO 1 ROWS
    INTO lv_whse2
    FROM zap_sign
    WHERE whse2 IN s_mkt.
  ENDSELECT.
  IF sy-subrc <> 0 OR
  lv_whse2 IS NOT INITIAL.
* Location does not exist in APO
    MESSAGE e017.
  ENDIF.
ENDFORM. " f_validate_market
*&---------------------------------------------------------------------*
*& Form F_GET_F4_DATA
*&---------------------------------------------------------------------*
* Get all the data required for custom F4 helps
*----------------------------------------------------------------------*
* <--ct_f4_data_matnr Itab with products
* <--ct_f4_data_loc Itab with F4 data for location
* <--ct_view_data Itab wiht data selected from view table
*----------------------------------------------------------------------*
FORM f_get_f4_data CHANGING ct_f4_data_matnr TYPE t_f4_data_matnr
                            ct_f4_data_loc TYPE t_f4_data_loc
                            ct_view_data TYPE t_view_data.
* Local Data Declaration
  DATA: ls_f4_data_matnr TYPE s_f4_data_matnr,
        ls_f4_data_loc TYPE s_f4_data_loc,
        lts_warehouse TYPE SORTED TABLE OF s_whse
                      WITH NON-UNIQUE KEY whse,
        lt_whse TYPE STANDARD TABLE OF s_whse,
        ls_whse TYPE s_whse.
* Local Field-symbols
  FIELD-SYMBOLS: <lfs_view> TYPE s_view_data,
                  <lfs_whse> TYPE s_whse.
  IF ( ct_f4_data_matnr IS INITIAL AND
      ct_f4_data_loc IS INITIAL ).
    SELECT pcode "Material Number
            maktx "Product Desc
            markt "Market
            pdctr "Production center
            pctxt "Prod center desc
            whse1 "Warehouse 1
            whse2 "Warehouse 2
            prvar "Prod Variant
            vartx "Variant Description
      INTO TABLE ct_view_data
      FROM zap_sign.
    IF sy-subrc <> 0.
* no sourcing info is maintained in the sign portfolio
      MESSAGE i005.
      LEAVE LIST-PROCESSING.
    ENDIF.
    LOOP AT ct_view_data ASSIGNING <lfs_view>.
      ls_whse-whse = <lfs_view>-whse1.
      APPEND ls_whse TO lt_whse.
      ls_whse-whse = <lfs_view>-whse2.
      APPEND ls_whse TO lt_whse.
    ENDLOOP.
    SORT lt_whse BY whse.
    DELETE ADJACENT DUPLICATES FROM lt_whse COMPARING whse.
    IF NOT lt_whse IS INITIAL.
* Get the Warehouse Descriptions
      SELECT whse "Warehouse Name
              whdesc "Warehouse Description
        FROM zap_whse
        INTO TABLE lts_warehouse
        FOR ALL ENTRIES IN lt_whse
        WHERE whse = lt_whse-whse.
    ENDIF.
* Populate the Internal Table with Product,Product Variant
* and Location. The location can be the Market,Production center,
* Warehouse 1,Warehouse 2. All the four locations will be clubbed
* under the field Location in the internal table
    LOOP AT ct_view_data ASSIGNING <lfs_view>.
      ls_f4_data_matnr-pcode = <lfs_view>-pcode.
      ls_f4_data_matnr-maktx = <lfs_view>-maktx.
      APPEND ls_f4_data_matnr TO ct_f4_data_matnr.
      CLEAR ls_f4_data_matnr.
      ls_f4_data_loc-pcode = <lfs_view>-pcode.
      ls_f4_data_loc-desc = <lfs_view>-martx.
      ls_f4_data_loc-locno = <lfs_view>-markt.
      ls_f4_data_loc-prvar = <lfs_view>-prvar.
      ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
      ls_f4_data_loc-vartx = <lfs_view>-vartx.
      ls_f4_data_loc-maktx = <lfs_view>-maktx.
      ls_f4_data_loc-status = <lfs_view>-status.
      APPEND ls_f4_data_loc TO ct_f4_data_loc.
      CLEAR ls_f4_data_loc.
      ls_f4_data_loc-pcode = <lfs_view>-pcode.
      ls_f4_data_loc-desc = <lfs_view>-pctxt.
      ls_f4_data_loc-locno = <lfs_view>-pdctr.
      ls_f4_data_loc-prvar = <lfs_view>-prvar.
      ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
      ls_f4_data_loc-vartx = <lfs_view>-vartx.
      ls_f4_data_loc-maktx = <lfs_view>-maktx.
      ls_f4_data_loc-status = <lfs_view>-status.
      APPEND ls_f4_data_loc TO ct_f4_data_loc.
      CLEAR ls_f4_data_loc.
* Read the description for Warehouse 1
      READ TABLE lts_warehouse ASSIGNING <lfs_whse> WITH KEY
          whse = <lfs_view>-whse1.
      IF sy-subrc = 0.
        ls_f4_data_loc-pcode = <lfs_view>-pcode.
        ls_f4_data_loc-desc = <lfs_whse>-whdesc.
        ls_f4_data_loc-locno = <lfs_view>-whse1.
        ls_f4_data_loc-prvar = <lfs_view>-prvar.
        ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
        ls_f4_data_loc-vartx = <lfs_view>-vartx.
        ls_f4_data_loc-maktx = <lfs_view>-maktx.
        ls_f4_data_loc-status = <lfs_view>-status.
        APPEND ls_f4_data_loc TO ct_f4_data_loc.
        CLEAR ls_f4_data_loc.
      ENDIF.
* Read the description for Warehouse 2
      READ TABLE lts_warehouse ASSIGNING <lfs_whse> WITH KEY
      whse = <lfs_view>-whse2.
      IF sy-subrc = 0.
        ls_f4_data_loc-pcode = <lfs_view>-pcode.
        ls_f4_data_loc-desc = <lfs_whse>-whdesc.
        ls_f4_data_loc-locno = <lfs_view>-whse2.
        ls_f4_data_loc-prvar = <lfs_view>-prvar.
        ls_f4_data_loc-pdctr = <lfs_view>-pdctr.
        ls_f4_data_loc-vartx = <lfs_view>-vartx.
        ls_f4_data_loc-maktx = <lfs_view>-maktx.
        ls_f4_data_loc-status = <lfs_view>-status.
        APPEND ls_f4_data_loc TO ct_f4_data_loc.
        CLEAR ls_f4_data_loc.
      ENDIF.
    ENDLOOP.
    SORT ct_f4_data_matnr BY pcode.
    DELETE ADJACENT DUPLICATES FROM ct_f4_data_matnr COMPARING pcode.
  ENDIF.
ENDFORM. " F_GET_F4_DATA
*&---------------------------------------------------------------------*
*& Form f_f4_for_prodcentre
*&---------------------------------------------------------------------*
* Display F4 Help for Product
*----------------------------------------------------------------------*
* -->IV_PRD Field Name for Product
* -->IT_F4_DATA Itab with F4 data
*----------------------------------------------------------------------*
FORM f_f4_for_prodcentre USING iv_prd TYPE dynfnam
it_f4_data_matnr TYPE t_f4_data_matnr.
* Local Constants Declaration
  CONSTANTS: lc_pcode TYPE fieldname VALUE 'PCODE'.
  PERFORM f_launch_f4_window USING lc_pcode
                                    iv_prd
                                    text-016
                                    it_f4_data_matnr.
ENDFORM. " f4_for_prodcentre
*&---------------------------------------------------------------------*
*& Form f_f4_for_market
*&---------------------------------------------------------------------*
* Display F4 Help for locations
*----------------------------------------------------------------------*
* -->IV_LOC Field Name for Location
* -->IT_F4_DATA Itab with the F4 Data
*----------------------------------------------------------------------*
FORM f_f4_for_market USING iv_loc TYPE dynfnam
it_f4_data_loc TYPE t_f4_data_loc.
* Local Data Declaratino
  DATA: lt_loc_for_f4 TYPE STANDARD TABLE OF s_f4_loc.
* Local Constants Declaration
  CONSTANTS:
  lc_pdctr TYPE fieldname VALUE 'LOCNO'.
  lt_loc_for_f4 = it_f4_data_loc .
  SORT lt_loc_for_f4 BY locno.
  DELETE ADJACENT DUPLICATES FROM lt_loc_for_f4 COMPARING locno.
  DELETE lt_loc_for_f4 WHERE locno IS INITIAL.
  PERFORM f_launch_f4_window USING lc_pdctr
                                    iv_loc
                                    text-017
                                    lt_loc_for_f4.
ENDFORM. " f_f4_for_market
*&---------------------------------------------------------------------*
*& Form f_launch_f4_window
*&---------------------------------------------------------------------*
* Displays the Custom F4 Help for the field name passed
*----------------------------------------------------------------------*
* -->IV_RETFLD Return field
* -->IV_DYNFLD Screen field
* -->IV_WINTITLE Window title
* -->IT_VALUETAB Value table
*----------------------------------------------------------------------*
FORM f_launch_f4_window USING iv_retfld TYPE fieldname
                              iv_dynfld TYPE dynfnam
                              iv_wintitle TYPE c
                              it_valuetab TYPE STANDARD TABLE.
  DATA:
    lv_dynprog TYPE syrepid,
    lv_dynpnr TYPE sydynnr.
    lv_dynprog = sy-repid.
    lv_dynpnr = sy-dynnr.
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = iv_retfld
      dynpprog        = lv_dynprog
      dynpnr          = lv_dynpnr
      dynprofield     = iv_dynfld
      window_title    = iv_wintitle
      value_org       = gc_s
    TABLES
      value_tab       = it_valuetab
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.
  IF sy-subrc <> 0.
* Error while displaying search help
    MESSAGE i058.
    EXIT.
  ENDIF.
ENDFORM. " f_launch_f4_window
*&---------------------------------------------------------------------*
*& Form f_check_product_status
*&---------------------------------------------------------------------*
* Get all the Products with status Inactive selected for deletion
*----------------------------------------------------------------------*
* -->IT_F4_LOC_DATA Itab wiht the products to be deleted
* <--CT_PROD_DATA Itab with the Product and Locs to be deleted
* <--CT_CORE_PROD Itab with the core products to be deleted
* <--CT_PROD_VAR Itab with the product varients to be deleted
* <--CT_LOC_DATA Itab with the locations to be deleted
*----------------------------------------------------------------------*
FORM f_check_product_status USING it_f4_data_loc TYPE t_f4_data_loc
                          CHANGING ct_prod_data TYPE t_f4_data_loc
                                    ct_core_prod TYPE t_f4_data_loc
                                    ct_prod_var TYPE t_f4_data_loc
                                    ct_loc_data TYPE t_f4_data_loc
                                    ct_matloc TYPE t_matloc.
*MOD-1771404
** Local Date Declaration
* DATA: lv_mkt TYPE char4, " Market
* lv_pdctr TYPE char2, " Production center
* lv_whse1 TYPE char3, " Warehouse1
* lv_whse2 TYPE char3. " Warehouse2
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
        lv_pdctr TYPE char2, " Production center
        lv_whse1 TYPE char3, " Warehouse1
        lv_whse2 TYPE char3. " Warehouse2
  DATA: ls_sign TYPE zap_sign.
  DATA: lv_pdctr1 TYPE char2,
        lv_ltext1 TYPE char40.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid,
        lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
  DATA:
    lv_matnr TYPE /sapapo/matnr,
    lt_matloc TYPE STANDARD TABLE OF /sapapo/v_matloc,
    lv_prod_data TYPE s_f4_data_loc,
    lv_tabix TYPE sy-tabix,
    lv_line TYPE i,
    lv_count TYPE i,
    lv_count1 TYPE i.
*MOD-011
  RANGES: lv_mkt1 FOR s_mkt.
  DATA: int TYPE i,
        rspar TYPE TABLE OF rsparams,
        wa_rspar LIKE LINE OF rspar,
        lv_indicator TYPE i.
  DATA: lv_indicator1 TYPE i VALUE 0.
  DATA: l_wa_prod TYPE s_f4_data_loc.
  DATA: it_itab_prod TYPE STANDARD TABLE OF s_f4_data_loc.
*MOD-011
  FIELD-SYMBOLS:
  <lfs_core_prod> TYPE s_f4_data_loc,
  <lfs_prod_data> TYPE s_f4_data_loc,
  <lfs_tsign> TYPE zap_sign.
* <lfs_zdm0705> TYPE ZDM0705.
  FIELD-SYMBOLS:
  <lfs_view> TYPE s_view_data,
*MOD-1771404
  <lfs_loc_data> TYPE s_f4_data_loc.
*MOD-1771404
  ct_prod_data = it_f4_data_loc.
***MOD-1771404
* Check if the Location is a market
  SELECT *
    INTO TABLE t_sign
    FROM zap_sign
    WHERE pcode IN s_prd AND
          ( markt IN s_mkt OR
          pdctr IN s_mkt OR
          whse1 IN s_mkt OR
          whse2 IN s_mkt ).
  IF sy-subrc <> 0.
    MESSAGE i004 WITH s_prd.
    LEAVE LIST-PROCESSING.
  ENDIF.
* DELETE t_sign WHERE status <> gc_inactive.
*MOD-1771404
  DELETE: ct_prod_data WHERE status <> gc_inactive,
          ct_prod_data WHERE NOT ( pcode IN s_prd ).
**MOD-1771404: This was commneted out for WHH.
* ct_prod_data WHERE NOT ( locno IN s_mkt ).
**MOD-1771404: This was commneted out for WHH.
***MOD-1771404: This was commneted out for WHH.
  IF NOT ct_prod_data[] IS INITIAL.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
* whse2 = <lfs_prod_data>-locno BINARY SEARCH.*
                    whse2 = s_mkt-low BINARY SEARCH.
* whse1 = <lfs_prod_data>-locno BINARY SEARCH.
        IF sy-subrc <> 0.
*MOD-1771404
*MOD-1887120
          READ TABLE t_sign ASSIGNING <lfs_tsign>
            WITH KEY pcode = <lfs_prod_data>-pcode
                      prvar = <lfs_prod_data>-prvar
* whse1 = <lfs_prod_data>-locno BINARY SEARCH.
          whse1 = s_mkt-low BINARY SEARCH.
          IF sy-subrc <> 0.
*MOD-1771404
            READ TABLE t_sign ASSIGNING <lfs_tsign>
              WITH KEY pcode = <lfs_prod_data>-pcode
                        prvar = <lfs_prod_data>-prvar
                        markt = s_mkt-low BINARY SEARCH.
*MOD-1887120
* markt = <lfs_prod_data>-locno binary search.
*
*MOD-1887120
            IF sy-subrc <> 0.
              READ TABLE t_sign ASSIGNING <lfs_tsign>
                WITH KEY pcode = <lfs_prod_data>-pcode
                          prvar = <lfs_prod_data>-prvar
                          pdctr = s_mkt-low BINARY SEARCH.
* pdctr IN s_mkt BINARY SEARCH.
* pdctr = <lfs_prod_data>-locno BINARY SEARCH.
              IF sy-subrc = 0.
                IF s_mkt-low = <lfs_prod_data>-locno AND
                    s_mkt-low = <lfs_tsign>-pdctr.
                  <lfs_prod_data>-lv_exist = 0.
                  CONTINUE.
*MOD-1887120
* ELSE.
* <lfs_prod_data>-lv_exist = 1.
* Continue.
*MOD-1887120
                ENDIF.
*MOD-1887120
                IF s_mkt-low = <lfs_prod_data>-locno.
                  <lfs_prod_data>-lv_exist = 0.
                  CONTINUE.
*MOD-1887120
* ELSE.
* <lfs_prod_data>-lv_exist = 1.
* Continue.
*MOD-1887120
                ENDIF.
*MOD-1887120
              ELSE.
                <lfs_prod_data>-lv_exist = 1.
                CONTINUE.
              ENDIF.
            ELSE.
              READ TABLE t_sign ASSIGNING <lfs_tsign>
                WITH KEY pcode = <lfs_prod_data>-pcode
                          prvar = <lfs_prod_data>-prvar
* markt = <lfs_prod_data>-locno BINARY SEARCH.
              markt = s_mkt-low BINARY SEARCH.
* pdctr = <lfs_prod_data>-locno BINARY SEARCH.
              IF sy-subrc = 0.
                <lfs_prod_data>-lv_exist = 0.
                CONTINUE.
              ELSE.
                <lfs_prod_data>-lv_exist = 1.
                CONTINUE.
              ENDIF.
            ENDIF.
          ELSE.
            <lfs_prod_data>-lv_exist = 0.
            <lfs_prod_data>-whse1 = <lfs_tsign>-whse1.
            EXIT.
          ENDIF.
        ELSE.
          <lfs_prod_data>-lv_exist = 0.
*MOD-1771404
          <lfs_prod_data>-whse1 = <lfs_tsign>-whse1.
          <lfs_prod_data>-whse2 = <lfs_tsign>-whse2.
*MOD-1771404
          EXIT.
        ENDIF.
      ENDLOOP.
    ENDLOOP.
  ENDIF.
*MOD-1887120
* DELETE: ct_prod_data WHERE lv_exist eq 1.
* DELETE ADJACENT DUPLICATES FROM CT_PROD_DATA COMPARING
* LOCNO
* PCODE
* PRVAR
* WHSE1
* WHSE2.
*
*MOD-1887120
*MOD-1887120
* To Handle Multiple production centres
  IF NOT ct_prod_data[] IS INITIAL.
    SELECT *
      INTO TABLE t_sign
      FROM zap_sign
      WHERE pcode IN s_prd AND
            pdctr IN s_mkt.
    IF sy-subrc <> 0.
* MESSAGE I004 with s_prd.
    ELSE.
* DELETE t_sign WHERE status <> gc_inactive.
      lv_tabix = 0.
      IF NOT t_sign IS INITIAL.
        LOOP AT t_sign ASSIGNING <lfs_tsign>.
          lv_tabix = lv_tabix + 1.
          LOOP AT s_mkt.
* Get the Warehouse Descriptions
* SELECT pdctr ltext up to 1 rows
* INTO (LV_PDCTR1, LV_LTEXT1)
* FROM ZDU1000
* WHERE PDCTR = S_MKT-LOW.
* ENDSELECT.
*MOD-1887120
            IF sy-subrc = 0.
*MOD-1887120
              l_wa_prod-locno = <lfs_tsign>-pdctr.
              l_wa_prod-desc = lv_ltext1.
              l_wa_prod-pcode = <lfs_tsign>-pcode.
              l_wa_prod-prvar = <lfs_tsign>-prvar.
              l_wa_prod-vartx = <lfs_tsign>-vartx.
              l_wa_prod-pdctr = s_mkt-low.
              l_wa_prod-maktx = <lfs_tsign>-maktx.
              l_wa_prod-whse1 = <lfs_tsign>-whse1.
              l_wa_prod-whse2 = <lfs_tsign>-whse2.
* l_wa_prod-status = <lfs_tsign>-status.
* l_wa_prod-loctyp = <lfs_tsign>-loctyp.
              APPEND l_wa_prod TO it_itab_prod.
              APPEND LINES OF it_itab_prod TO ct_prod_data.
*MOD-1887120
            ENDIF.
*MOD-1887120
          ENDLOOP.
        ENDLOOP.
      ENDIF.
    ENDIF.
  ENDIF.
*MOD-099
* DELETE: ct_prod_data WHERE lv_exist eq 1.
*MOD-099
  SORT ct_prod_data BY locno
                        pcode
                        prvar
                        pdctr
                        status
                        whse1
                        whse2.
  DELETE ADJACENT DUPLICATES FROM ct_prod_data COMPARING
      locno
      pcode
      prvar
      pdctr
      status
      whse1
      whse2.
*MOD-1887120
  IF ct_prod_data IS INITIAL.
* No Inactive products are present in the view table.
    MESSAGE i063.
    LEAVE LIST-PROCESSING.
  ENDIF.
  SELECT matnr locno INTO TABLE ct_matloc
    FROM /sapapo/v_matloc
    FOR ALL ENTRIES IN ct_prod_data
    WHERE ( matnr = ct_prod_data-pcode OR
            matnr = ct_prod_data-prvar ) AND
            locno = ct_prod_data-locno.
*MOD-1887120
* LOCNO IN S_MKT.
*MOD-1887120
  IF sy-subrc <> 0.
    MESSAGE i066.
    IF gv_flgstart IS NOT INITIAL.
      LEAVE LIST-PROCESSING.
    ELSE.
      LEAVE TO SCREEN 1000.
    ENDIF.
  ENDIF.
* Delete the entries that are not in data base.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE lt_matloc TRANSPORTING NO FIELDS
* WITH KEY matnr = <lfs_prod_data>-prvar
* locno = <lfs_prod_data>-locno.
* IF sy-subrc <> 0.
* CLEAR <lfs_prod_data>-prvar.
* ENDIF.
* ENDLOOP.
* DELETE ct_prod_data WHERE prvar IS INITIAL.
  ct_core_prod = ct_prod_data.
  ct_prod_var = ct_prod_data.
  ct_loc_data = ct_prod_data.
  SORT ct_core_prod BY pcode.
*MOD-1887120
* DELETE ADJACENT DUPLICATES FROM ct_core_prod COMPARING pcode.
*MOD-1887120
* DELETE ADJACENT DUPLICATES FROM CT_CORE_PROD COMPARING
* locno
* pcode
* prvar
* whse1
* whse2.
  SORT ct_prod_data BY locno
      pcode
      prvar
      pdctr
      status
      whse1
      whse2.
  DELETE ADJACENT DUPLICATES FROM ct_prod_data COMPARING
      locno
      pcode
      prvar
      pdctr
      status
      whse1
      whse2.
*MOD-1887120
*MOD-1887120
  DELETE ct_core_prod WHERE pcode IS INITIAL.
  LOOP AT ct_core_prod ASSIGNING <lfs_core_prod>.
    LOOP AT gt_view_data TRANSPORTING NO FIELDS
    WHERE pcode = <lfs_core_prod>-pcode AND
    status <> 'I'.
      CLEAR <lfs_core_prod>-pcode.
      EXIT.
    ENDLOOP.
  ENDLOOP.
  DELETE ct_core_prod WHERE pcode IS INITIAL.
**MOD-1887120
* LV_TABIX = 0.
* IF not S_MKT IS INITIAL.
* LOOP AT S_MKT.
* LV_TABIX = LV_TABIX + 1.
* IF LV_TABIX > 1.
* delete: ct_core_prod WHERE NOT ( locno IN s_mkt ).
* ENDIF.
* ENDLOOP.
* ENDIF.
**MOD-1887120
*MOD-1771404
* SORT ct_prod_var BY prvar.
* DELETE ADJACENT DUPLICATES FROM ct_prod_var COMPARING prvar.
*MOD-1771404
  LOOP AT ct_prod_var ASSIGNING <lfs_core_prod>.
    SELECT SINGLE matnr INTO lv_matnr
      FROM /sapapo/matkey
      WHERE matnr = <lfs_core_prod>-prvar.
    IF sy-subrc <> 0.
      CLEAR <lfs_core_prod>-prvar.
    ENDIF.
  ENDLOOP.
  DELETE ct_prod_var WHERE prvar IS INITIAL.
*MOD-1771404
* SORT ct_loc_data BY locno.
* DELETE ADJACENT DUPLICATES FROM ct_loc_data COMPARING locno.
*MOD-1771404
  SORT ct_loc_data BY locno pcode prvar.
  DELETE ADJACENT DUPLICATES FROM ct_loc_data COMPARING locno
      pcode
      prvar.
*MOD-1771404
  DELETE ct_loc_data WHERE locno IS INITIAL.
*MOD-1771404
*MOD-1887120 for multiple locations
  lv_indicator = 0.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE CT_MATLOC TRANSPORTING NO FIELDS
* WITH KEY MATNR = <lfs_core_prod>-PCODE
* locno = <lfs_core_prod>-locno binary search.
* if sy-subrc <> 0.
    CLEAR: lv_whse1.
    SELECT whse1 UP TO 1 ROWS
      INTO lv_whse1
      FROM zap_sign
      WHERE whse1 = <lfs_prod_data>-locno OR
            whse2 = <lfs_prod_data>-locno AND
            pcode IN s_prd.
    ENDSELECT.
    IF sy-subrc = 0.
      PERFORM get_desc_loc USING <lfs_prod_data>
                        CHANGING lv_ware_desc.
      IF sy-subrc = 0.
        <lfs_prod_data>-desc = lv_ware_desc.
        lv_indicator = 1.
      ENDIF.
    ELSE.
      <lfs_prod_data>-lv_exist = 1.
    ENDIF.
* ENDIF.
  ENDLOOP.
* DELETE CT_PROD_DATA WHERE LV_EXIST = 1.
*MOD-1887120
**MOD-1887120 for multiple locations
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE pdctr = <lfs_prod_data>-locno and
* pcode in s_prd.
* ENDSELECT.
* IF SY-SUBRC = 0.
* lv_indicator = 1.
* else.
* <lfs_prod_data>-LV_EXIST = 1.
* ENDIF.
* ENDLOOP.
**MOD-1887120
**MOD-1887120 for multiple locations
* LOOP AT ct_core_prod ASSIGNING <lfs_core_prod>.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE pdctr = <lfs_core_prod>-locno and
* pcode in s_prd.
* ENDSELECT.
* IF SY-SUBRC = 0.
* lv_indicator = 1.
* else.
* <lfs_core_prod>-LV_EXIST = 1.
* ENDIF.
* ENDLOOP.
**MOD-1887120
*MOD-1771404: 1 warehouse case
  IF lv_indicator NE 1.
    CLEAR: lv_whse1.
    SELECT whse1 UP TO 1 ROWS
      INTO lv_whse1
      FROM zap_sign
      WHERE whse1 IN s_mkt OR
            whse2 IN s_mkt AND
            pcode IN s_prd.
    ENDSELECT.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* LOOP AT S_MKT.
      READ TABLE t_sign ASSIGNING <lfs_tsign>
        WITH KEY pcode = <lfs_prod_data>-pcode
                  prvar = <lfs_prod_data>-prvar
                  whse1 = s_mkt-low BINARY SEARCH.
**MOD-1887120
* whse1 = lv_whse1.
**MOD-1887120
      IF sy-subrc = 0.
        <lfs_prod_data>-locno = s_mkt-low.
*MOD-1887120
* <lfs_prod_data>-whse1 = s_mkt-low.
        <lfs_prod_data>-whse1 = lv_whse1.
*MOD-1887120
* PERFORM GET_DESC_LOC USING lv_prod_data
        PERFORM get_desc_loc USING <lfs_prod_data>
                          CHANGING lv_ware_desc.
        IF sy-subrc = 0.
          <lfs_prod_data>-desc = lv_ware_desc.
        ENDIF.
      ENDIF.
    ENDLOOP.
*MOD-1887120
  ENDIF.
*MOD-1887120
**MOD-1887120 Multiple Location Case.
* clear: lv_whse1.
* SELECT whse1 UP TO 1 ROWS
* INTO lv_whse1
* FROM zm_zap_sign
* WHERE whse1 IN s_mkt or
* whse2 in s_mkt and
* pcode in s_prd.
* ENDSELECT.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* LOOP AT S_MKT.
* READ TABLE t_sign ASSIGNING <lfs_tsign>
* WITH KEY pcode = <lfs_prod_data>-pcode
* prvar = <lfs_prod_data>-prvar
* markt = s_mkt-low BINARY SEARCH.
* if sy-subrc = 0.
* <lfs_prod_data>-locno = s_mkt-low.
* <lfs_prod_data>-whse1 = lv_whse1.
* PERFORM GET_DESC_LOC USING lv_prod_data
* changing lv_ware_desc.
* if sy-subrc = 0.
* <lfs_prod_data>-desc = lv_ware_desc.
* endif.
* endif.
* ENDLOOP.
* ENDLOOP.
**MOD-1887120
  REFRESH: ct_loc_data, ct_prod_var.
  CLEAR: ct_loc_data, ct_prod_var.
  ct_loc_data = ct_prod_data.
  ct_prod_var = ct_prod_data.
  SELECT matnr locno INTO TABLE ct_matloc
    FROM /sapapo/v_matloc
    FOR ALL ENTRIES IN ct_prod_data
    WHERE ( matnr = ct_prod_data-pcode OR
            matnr = ct_prod_data-prvar ) AND
*MOD-1887120
  locno IN s_mkt.
* locno = ct_prod_data-locno.
*MOD-1887120
  IF sy-subrc <> 0.
    MESSAGE i066.
    IF gv_flgstart IS NOT INITIAL.
      LEAVE LIST-PROCESSING.
    ELSE.
      LEAVE TO SCREEN 1000.
    ENDIF.
  ENDIF.
*MOD-1771404
*MOD-1771404: 2 warehouse case.
*MOD-1771404
* Check if the Location is a Warehouse 1
*MOD-1887120
  IF lv_indicator NE 1.
*MOD-1887120
    CLEAR: lv_whse1, lv_whse2.
    SELECT whse1 UP TO 1 ROWS
      INTO lv_whse1
      FROM zap_sign
      WHERE whse1 IN s_mkt OR
            whse2 IN s_mkt AND
            pcode IN s_prd.
    ENDSELECT.
* IF sy-subrc = 0 OR
* lv_whse1 IS NOT INITIAL.
* EXIT.
* ENDIF.
* Check if the Location is a Warehouse 2
    SELECT whse2 UP TO 1 ROWS
      INTO lv_whse2
      FROM zap_sign
      WHERE whse1 IN s_mkt OR
            whse2 IN s_mkt AND
            pcode IN s_prd.
    ENDSELECT.
* IF sy-subrc <> 0 OR
* lv_whse2 IS INITIAL.
** Location does not exist in APO
* MESSAGE e017.
* ENDIF.
*MOD-1887120
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
**MOD-011
* LOOP AT S_MKT.
**MOD-011
* READ TABLE t_sign ASSIGNING <lfs_tsign>
* WITH KEY pcode = <lfs_prod_data>-pcode
* prvar = <lfs_prod_data>-prvar
* whse2 = lv_whse2 BINARY SEARCH.
* if sy-subrc = 0.
* <lfs_prod_data>-locno = s_mkt-low.
* <lfs_prod_data>-whse1 = lv_whse1.
* ENDIF.
**MOD-011
* ENDLOOP.
**MOD-011
* ENDLOOP.
**MOD-1771404
*MOD-1887120
*MOD-1887120
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
* WHSE2 = lv_whse2 binary search.
                    whse2 = s_mkt-low BINARY SEARCH.
        IF sy-subrc = 0.
          <lfs_prod_data>-locno = s_mkt-low.
          <lfs_prod_data>-whse1 = lv_whse1.
        ENDIF.
*MOD-1887120
      ENDLOOP.
*MOD-1887120
    ENDLOOP.
*MOD-1887120
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
                    whse2 = s_mkt-low BINARY SEARCH.
        IF sy-subrc = 0.
          <lfs_prod_data>-locno = s_mkt-low.
*MOD-1771404
          <lfs_prod_data>-whse1 = <lfs_prod_data>-whse1.
          <lfs_prod_data>-whse2 = s_mkt-low.
*MOD-1771404
          PERFORM get_desc_whse2 USING lv_prod_data
                              CHANGING lv_ware_desc.
          IF sy-subrc = 0.
            <lfs_prod_data>-desc = lv_ware_desc.
          ENDIF.
        ENDIF.
*MOD-1887120
      ENDLOOP.
*MOD-1887120
    ENDLOOP.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
*MOD-1887120
      LOOP AT s_mkt.
*MOD-1887120
        READ TABLE t_sign ASSIGNING <lfs_tsign>
          WITH KEY pcode = <lfs_prod_data>-pcode
                    prvar = <lfs_prod_data>-prvar
                    whse2 = s_mkt-low BINARY SEARCH.
* whse2 = lv_whse2 BINARY SEARCH.
        IF sy-subrc = 0.
          <lfs_prod_data>-locno = s_mkt-low.
*MOD-1771404
          <lfs_prod_data>-whse1 = <lfs_prod_data>-whse1.
          <lfs_prod_data>-whse2 = s_mkt-low.
* <lfs_prod_data>-whse2 = lv_whse2.
*MOD-1887120
*MOD-1771404
          PERFORM get_desc_whse2 USING lv_prod_data
            CHANGING lv_ware_desc.
          IF sy-subrc = 0.
            <lfs_prod_data>-desc = lv_ware_desc.
          ENDIF.
        ENDIF.
*MOD-1887120
      ENDLOOP.
*MOD-1887120
    ENDLOOP.
*MOD-1887120
  ELSE.
    DELETE: ct_prod_data WHERE NOT ( locno IN s_mkt ).
  ENDIF.
*MOD-1887120
  REFRESH: ct_loc_data, ct_prod_var.
  CLEAR: ct_loc_data, ct_prod_var.
  ct_loc_data = ct_prod_data.
  ct_prod_var = ct_prod_data.
*MOD-1887120
**MOD-1887120
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
    <lfs_prod_data>-lv_exist = 0.
  ENDLOOP.
  FIELD-SYMBOLS:<lfs_matloc> TYPE s_matloc.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
    READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
        matnr = <lfs_prod_data>-pcode
        locno = <lfs_prod_data>-locno.
    IF sy-subrc NE 0.
      READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
          matnr = <lfs_prod_data>-prvar
          locno = <lfs_prod_data>-locno.
      IF sy-subrc NE 0.
        <lfs_prod_data>-lv_exist = 1.
      ENDIF.
    ENDIF.
  ENDLOOP.
  DELETE ct_prod_data WHERE lv_exist = 1.
  REFRESH: ct_loc_data, ct_prod_var, ct_core_prod.
  CLEAR: ct_loc_data, ct_prod_var, ct_core_prod.
  ct_core_prod = ct_prod_data.
  ct_prod_var = ct_prod_data.
  ct_loc_data = ct_prod_data.
*MOD-1887120
**MOD-1887120 Changes made after release of transport on 28.03
  DATA: l_wa_zdm0705 TYPE zdm0705.
  DATA: l_wa_zdu1002 TYPE zdu1002.
  DATA: lv_prvar TYPE char18.
  REFRESH: it_zdm0705.
  REFRESH: it_zdu1002.
  LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
    SELECT * APPENDING TABLE it_zdm0705
      FROM zdm0705
      WHERE parpr = <lfs_prod_data>-pcode.
  ENDLOOP.
  SELECT * INTO TABLE it_zdu1002
    FROM zdu1002
    FOR ALL ENTRIES IN it_zdm0705
    WHERE prvar = it_zdm0705-prvar.
  SORT it_zdu1002 BY markt prvar.
  DATA: lv_product TYPE char18.
  CLEAR: l_wa_zdu1002.
  CLEAR: l_wa_zdm0705.
  SORT it_zdm0705 BY prvar parpr.
  DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
  LOOP AT it_zdm0705 INTO l_wa_zdm0705.
    READ TABLE it_zdu1002 INTO l_wa_zdu1002
      WITH KEY prvar = l_wa_zdm0705-prvar BINARY SEARCH.
    IF sy-subrc = 0.
      lv_count = lv_count + 1.
      IF l_wa_zdu1002-status NE 'I'.
        lv_indicator1 = 1.
      ENDIF.
    ENDIF.
  ENDLOOP.
**MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* LOOP AT IT_ZDM0705 into l_wa_zdm0705
* WHERE prvar = l_wa_zdu1002-prvar.
** READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
** prvar = l_wa_zdu1002-prvar binary search.
** where dmoap NE 'Z'.
** IF L_WA_ZDU1002-PRVAR NE L_WA_ZDM0705-PRVAR.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
*
** else.
** l_wa_zdm0705-dmoap = 'Z'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
** exit.
* ENDLOOP.
* endif.
**MOD-1887120 Changes made after release of transport on 28.03
**MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* READ TABLE IT_ZDM0705 ASSIGNING <LFS_ZDM0705>
* WITH KEY PRVAR = L_WA_ZDU1002-PRVAR BINARY SEARCH.
** LOOP AT IT_ZDM0705 into l_wa_zdm0705
** WHERE prvar = l_wa_zdu1002-prvar.
* IF SY-SUBRC = 0.
* <lfs_zdm0705>-dmoap = 'X'.
** l_wa_zdm0705-dmoap = 'X'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* ENDIF.
* ENDLOOP.
** ENDLOOP.
* endif.
*MOD-1887120 Changes made after release of transport on 28.03
* LOOP AT it_zdu1002 into l_wa_zdu1002.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* prvar = l_wa_zdu1002-prvar binary search.
* if sy-subrc ne 0.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* endif.
* ENDLOOP.
*mod-099
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
** READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
** parpr = <lfs_prod_data>-pcode binary
*search.
** if sy-subrc ne 0.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* prvar = <lfs_prod_data>-prvar binary search.
* if sy-subrc ne 0.
* l_wa_zdm0705-dmoap = 'X'.
* modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* endif.
** endif.
* ENDLOOP.
**mod-099
*MOD-1887120 Changes made after release of transport on 28.03
  IF lv_indicator1 = 1.
    LOOP AT ct_prod_data ASSIGNING <lfs_prod_data>.
* READ TABLE IT_ZDM0705 into l_wa_zdm0705 with key
* parpr = <lfs_prod_data>-pcode
* dmoap = 'X'.
      IF sy-subrc = 0.
        DELETE ct_core_prod WHERE pcode = <lfs_prod_data>-pcode.
* Indicates that the core product should not be included in
* transportation lane and /sapapo/bp2 transaction.
        READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
            matnr = <lfs_prod_data>-pcode
            locno = <lfs_prod_data>-locno.
        IF sy-subrc NE 0.
          READ TABLE ct_matloc ASSIGNING <lfs_matloc> WITH KEY
              matnr = <lfs_prod_data>-prvar
              locno = <lfs_prod_data>-locno.
          IF sy-subrc NE 0.
            <lfs_prod_data>-lv_exist = 1.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.
  DELETE ct_prod_data WHERE lv_exist = 1.
*mod-099 changed on 04042006
*MOD-099 changed on 04042006
  REFRESH: ct_loc_data, ct_prod_var. " ct_core_prod.
  CLEAR: ct_loc_data, ct_prod_var. " ct_core_prod.
*MOD-1887120
* ct_core_prod = ct_prod_data.
*MOD-1887120
  ct_prod_var = ct_prod_data.
  ct_loc_data = ct_prod_data.
  lv_indicator1 = 0.
*MOD-1887120 Changes made after release of transport on 28.03
*MOD-1771404
* SELECT matnr locno INTO TABLE ct_matloc
* FROM /sapapo/v_matloc
* FOR ALL ENTRIES IN ct_prod_data
* WHERE ( matnr = ct_prod_data-pcode OR
* matnr = ct_prod_data-prvar ) AND
**
* locno = ct_prod_data-locno.
**
* IF sy-subrc <> 0.
* MESSAGE i066.
* IF gv_flgstart IS NOT INITIAL.
* LEAVE LIST-PROCESSING.
* ELSE.
* LEAVE TO SCREEN 1000.
* ENDIF.
* ENDIF.
*MOD-1771404
ENDFORM. " f_check_product_status
*&---------------------------------------------------------------------*
*& Form f_delete_transactional_data
*&---------------------------------------------------------------------*
* Delete the transactional data for the Product and Location
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_PROD_DATA Itab wiht Product and Locations selected
* <--CT_BDC_DATA BDC TABLE
*----------------------------------------------------------------------*
FORM f_delete_transactional_data USING it_core_prod TYPE t_f4_data_loc
    it_prod_var TYPE t_f4_data_loc
    it_loc_data TYPE t_f4_data_loc
CHANGING ct_prod_data TYPE t_f4_data_loc
ct_bdc_data TYPE STANDARD TABLE.
                                                            "#EC *
* CT_BDC_DATA is populated in the Perform populate_bdc_tab
* Local Data Declaration
  DATA: lv_ctu_params TYPE ctu_params,
        lv_fname TYPE dynfnam,
        lv_count(2) TYPE n,
        lt_msg_tab TYPE STANDARD TABLE OF bdcmsgcoll.
  DATA: lv_prev_pcode TYPE matnr.
  DATA: percentage_text TYPE c LENGTH 5.
  DATA: lv_tabix(20).
  DATA: lv_line TYPE i.
  DATA: lv_line1 TYPE i.
  DATA: lv_line2 TYPE i.
  DATA: lv_pcode TYPE /sapapo/matnr.
  DATA: s_product TYPE RANGE OF bapi10001apxprodlockey-product,
        s_product_wa LIKE LINE OF s_product.
* Field-symbols
  FIELD-SYMBOLS: <lfs_msg_ret> TYPE bdcmsgcoll,
                <lfs_data> TYPE s_f4_data_loc,
                <lfs_data1> TYPE s_f4_data_loc.
  REFRESH ct_bdc_data.
*MOD-1887120
  SORT it_core_prod BY pcode.
  DELETE ADJACENT DUPLICATES FROM it_core_prod
      COMPARING pcode.
  DESCRIBE TABLE it_core_prod LINES lv_line.
  SORT it_prod_var BY pcode prvar.
  DELETE ADJACENT DUPLICATES FROM it_prod_var
      COMPARING pcode prvar.
  DESCRIBE TABLE it_prod_var LINES lv_line1.
  lv_line2 = lv_line + lv_line1.
  IF lv_line2 <= 7.
*Version 0002
*MOD-1887120: Code that is fixed till TESTAVEEK27.
*Populate the general selection criteria on first screen
    PERFORM f_populate_bdc_tab USING:
        'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
        ' ' 'BDC_OKCODE' '=DUMMY',
        ' ' 'PLVERS' '000',
        ' ' 'MD_SYS' p_log,
        ' ' 'EXTRB' 'X',
        ' ' 'MO_DEL' 'X',
        ' ' 'SIM' space,
        ' ' 'P_LOGDEL' '5'.
*Populate the select options for Product
    PERFORM f_populate_bdc_tab USING:
      'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
      ' ' 'BDC_OKCODE' '=%011'.
*Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
        'X' 'SAPLALDB' '3000',
*       ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
        ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_core_prod ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
          ' ' lv_fname <lfs_data>-pcode.
      CLEAR lv_fname.
    ENDLOOP.
    LOOP AT it_prod_var ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
          ' ' lv_fname <lfs_data>-prvar.
      CLEAR lv_fname.
    ENDLOOP.
*Populate the select options for the Locations
    PERFORM f_populate_bdc_tab USING:
        'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
        ' ' 'BDC_OKCODE' '=%012'.
*Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
        'X' 'SAPLALDB' '3000',
*       ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
          ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_loc_data ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
          ' ' lv_fname <lfs_data>-locno.
      CLEAR lv_fname.
    ENDLOOP.
*Populate the objects to be deleted on first screen
    PERFORM f_populate_bdc_tab USING:
        'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
        ' ' 'BDC_OKCODE' '=ONLI',
        ' ' 'PLVERS' '000',
        ' ' 'MD_SYS' p_log,
        ' ' 'EXTRB' 'X',
        ' ' 'MO_DEL' 'X',
        ' ' 'MO_STAT' 'X',
        ' ' 'PO_DEL' 'X',
        ' ' 'SL_DEL' 'X',
        ' ' 'ST_DEL' 'X',
        ' ' 'PIR_DEL' 'X',
        ' ' 'SIM' space,
        ' ' ' ' ' '.
    PERFORM f_populate_bdc_tab USING:
        'X' 'SAPLSPO1' '0500',
        ' ' 'BDC_OKCODE' '=OPT1'.
    CLEAR gv_msg_error.
    lv_ctu_params-nobinpt = space.
    lv_ctu_params-dismode = gc_mode_e.
    lv_ctu_params-updmode = 'S'.
    lv_ctu_params-racommit = 'X'.
* Call Transaction /SAPAPO/BP2 for deleting the Transactional data
    CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS FROM lv_ctu_params
    MESSAGES INTO lt_msg_tab.
*MOD-1887120: Code that is fixed till TESTAVEEK27.
*Version 0002
  ELSE.
**Version 0005
*CTS D7AK900899 New Version code fixed for testaveek28 case.
*MOD-1887120: Code that is changed after the release of transport.
* Populate the general selection criteria on first screen
    PERFORM f_populate_bdc_tab USING:
        'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
*       ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
        ' ' 'BDC_OKCODE' '=%011',
*       ' ' 'BDC_OKCODE' '=DUMMY',
        ' ' 'PLVERS' '000',
        ' ' 'MD_SYS' p_log,
        ' ' 'EXTRB' 'X',
        ' ' 'MO_DEL' 'X',
        ' ' 'SIM' space,
        ' ' 'P_LOGDEL' '5'.
    SORT it_core_prod BY pcode.
    DELETE ADJACENT DUPLICATES FROM it_core_prod
    COMPARING pcode.
    DESCRIBE TABLE it_core_prod LINES lv_line.
    CLEAR lv_count.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
*** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-pcode,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
* ENDLOOP.
*Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
    SORT it_prod_var BY pcode prvar.
    DELETE ADJACENT DUPLICATES FROM it_prod_var
        COMPARING pcode prvar.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000'.
*MOD-1887120:
    IF NOT it_core_prod[] IS INITIAL.
*MOD-1887120:
      LOOP AT it_core_prod ASSIGNING <lfs_data>.
        lv_pcode = <lfs_data>-pcode.
*lv_count = lv_count + 1.
        lv_count = 1.
        CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_line > 1.
        PERFORM f_populate_bdc_tab USING:
            'X' 'SAPLALDB' '3000',
            ' ' lv_fname <lfs_data>-pcode,
            ' ' 'BDC_OKCODE' '=LINS'.
        CLEAR lv_fname.
        DESCRIBE TABLE it_prod_var LINES lv_line.
* sort <lfs_data> by locno pcode prvar.
        LOOP AT it_prod_var ASSIGNING <lfs_data1> WHERE pcode = lv_pcode.
          lv_count = 1.
          CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* if lv_line > 1.
          PERFORM f_populate_bdc_tab USING:
              'X' 'SAPLALDB' '3000',
              ' ' lv_fname <lfs_data1>-prvar,
              ' ' 'BDC_OKCODE' '=LINS'.
* else.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
* endif.
          CLEAR lv_fname.
        ENDLOOP.
*MOD-1887120: Changed after 04.04.2006
        LOOP AT it_prod_var ASSIGNING <lfs_data1>.
          lv_count = 1.
          CONCATENATE gc_rscsel gc_l_brac lv_count
                      gc_r_brac INTO lv_fname.
          PERFORM f_populate_bdc_tab USING:
              'X' 'SAPLALDB' '3000',
              ' ' lv_fname <lfs_data1>-prvar,
              ' ' 'BDC_OKCODE' '=LINS'.
          CLEAR lv_fname.
        ENDLOOP.
      ENDLOOP.
*MOD-1887120:
*MOD-1887120:
    ELSE.
*MOD-1887120:
      DESCRIBE TABLE it_prod_var LINES lv_line.
* sort <lfs_data> by locno pcode prvar.
      LOOP AT it_prod_var ASSIGNING <lfs_data1>. "where pcode=lv_pcode.
* lv_count = lv_count + 1.
        lv_count = 1.
        CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname
        .
* if lv_line > 1.
        PERFORM f_populate_bdc_tab USING:
            'X' 'SAPLALDB' '3000',
            ' ' lv_fname <lfs_data1>-prvar,
            ' ' 'BDC_OKCODE' '=LINS'.
* else.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
* endif.
        CLEAR lv_fname.
      ENDLOOP.
*MOD-1887120:
    ENDIF.
*MOD-1887120:
* Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
*       'X' 'SAPLALDB' '3000',
*       ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
        ' ' 'BDC_OKCODE' '=ACPT'.
* Populate the select options for the Locations
    PERFORM f_populate_bdc_tab USING:
        'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
*       ' ' 'BDC_CURSOR' 'EXT_PRD-LOW',
        ' ' 'BDC_OKCODE' '=%012',
        ' ' 'PLVERS' '000',
        ' ' 'MD_SYS' p_log,
        ' ' 'EXTRB' 'X',
        ' ' 'MO_DEL' 'X',
        ' ' 'SIM' space,
        ' ' 'P_LOGDEL' '5'.
* Populate the select options screen for product
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLALDB' '3000',
* ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
    ' ' 'BDC_OKCODE' '=ACPT'.
    CLEAR lv_count.
    LOOP AT it_loc_data ASSIGNING <lfs_data>.
      lv_count = lv_count + 1.
* lv_count = 1.
      CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
      PERFORM f_populate_bdc_tab USING:
          ' ' lv_fname <lfs_data>-locno.
* ' ' 'BDC_OKCODE' '=LINS'.
      CLEAR lv_fname.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-locno,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
    ENDLOOP.
* CLEAR lv_count.
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-locno,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
* ENDLOOP.
*
* Populate the objects to be deleted on first screen
    PERFORM f_populate_bdc_tab USING:
        'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
        ' ' 'BDC_OKCODE' '=DUMMY',
        ' ' 'PLVERS' '000',
        ' ' 'MD_SYS' p_log,
        ' ' 'EXTRB' 'X',
        ' ' 'MO_DEL' 'X',
        ' ' 'MO_STAT' 'X',
        ' ' 'PO_DEL' 'X',
        ' ' 'SL_DEL' 'X',
        ' ' 'ST_DEL' 'X',
        ' ' 'PIR_DEL' 'X',
        ' ' 'SIM' space,
        ' ' ' ' ' '.
    PERFORM f_populate_bdc_tab USING:
        'X' 'SAPLSPO1' '0500',
        ' ' 'BDC_OKCODE' '=OPT1'.
    CLEAR gv_msg_error.
    lv_ctu_params-nobinpt = space.
* lv_ctu_params-dismode = gc_mode_e.
    lv_ctu_params-dismode = 'A'.
    lv_ctu_params-updmode = 'S'.
    lv_ctu_params-racommit = 'X'.
* Call Transaction /SAPAPO/BP2 for deleting the Transactional data
    CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS FROM lv_ctu_params
    MESSAGES INTO lt_msg_tab.
***MOD-1887120: Code that is changed after the release of transport.
*CTS D7AK900899 New Version
*Code fixed for testaveek28 case.
**Version 0005
  ENDIF.
*MOD-1887120
**Version 0002
**MOD-1887120: Code that is fixed till TESTAVEEK27.
**Populate the general selection criteria on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
**Populate the select options for Product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%011'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-pcode.
* CLEAR lv_fname.
*
* ENDLOOP.
*
* LOOP AT it_prod_var ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar.
* CLEAR lv_fname.
* ENDLOOP.
*
**Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%012'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
*
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
*
* CLEAR lv_fname.
*
* ENDLOOP.
*
**Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=ONLI',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
*
**MOD-1887120: Code that is fixed till TESTAVEEK27.
**Version 0002
***Version 0005
**CTS D7AK900899 New Version code fixed for testaveek28 case.
**MOD-1887120: Code that is changed after the release of transport.
** Populate the general selection criteria on first screen
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
* ' ' 'BDC_OKCODE' '=%011',
** ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
* sort it_core_prod by pcode.
* delete adjacent duplicates from it_core_prod
* comparing pcode.
* describe table it_core_prod lines lv_line.
*
* CLEAR lv_count.
** LOOP AT it_core_prod ASSIGNING <lfs_data>.
** lv_count = 1.
** CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
**** if lv_line > 1.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-pcode,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
** ENDLOOP.
*
**Populate the select options screen for product
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
*** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
** ' ' 'BDC_OKCODE' '=ACPT'.
*
* sort it_prod_var by pcode prvar.
* delete adjacent duplicates from it_prod_var
* comparing pcode prvar.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000'.
* LOOP AT it_core_prod ASSIGNING <lfs_data>.
* lv_pcode = <lfs_data>-pcode.
**lv_count = lv_count + 1.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
*** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data>-pcode,
* ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
*
* describe table it_prod_var lines lv_line.
*
** sort <lfs_data> by locno pcode prvar.
* LOOP AT it_prod_var ASSIGNING <lfs_data1> where pcode = lv_pcode.
** lv_count = lv_count + 1.
* lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_line > 1.
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
* ' ' lv_fname <lfs_data1>-prvar,
* ' ' 'BDC_OKCODE' '=LINS'.
** else.
** PERFORM f_populate_bdc_tab USING:
** ' ' lv_fname <lfs_data>-prvar,
** ' ' 'BDC_OKCODE' '=LINS'.
** endif.
* CLEAR lv_fname.
* ENDLOOP.
* ENDLOOP.
*
** Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
*
** Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PRD-LOW',
* ' ' 'BDC_OKCODE' '=%012',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
*
** Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
*
* CLEAR lv_count.
* LOOP AT it_loc_data ASSIGNING <lfs_data>.
* lv_count = lv_count + 1.
** lv_count = 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
** ' ' 'BDC_OKCODE' '=LINS'.
* CLEAR lv_fname.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-locno,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
* ENDLOOP.
*
*
** CLEAR lv_count.
** LOOP AT it_loc_data ASSIGNING <lfs_data>.
** lv_count = 1.
** CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** PERFORM f_populate_bdc_tab USING:
** 'X' 'SAPLALDB' '3000',
** ' ' lv_fname <lfs_data>-locno,
** ' ' 'BDC_OKCODE' '=LINS'.
** CLEAR lv_fname.
** ENDLOOP.
*
**
** Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
****MOD-1887120: Code that is changed after the release of transport.
**CTS D7AK900899 New Version
**Code fixed for testaveek28 case.
***Version 0005
**Version 0006 latest version.
**Code for looping at it_prod_var and doing the call transaction for
**each record
**CTS D7AK900899 New Version
*
* sort it_prod_var by pcode prvar.
* delete adjacent duplicates from it_prod_var
* comparing pcode prvar.
*
* clear: lv_prev_pcode.
*
* LOOP AT it_prod_var ASSIGNING <lfs_data>.
*
** Populate the general selection criteria on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
** ' ' 'BDC_CURSOR' 'EXT_PROD-LOW',
* ' ' 'BDC_OKCODE' '=%011',
** ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'SIM' space,
* ' ' 'P_LOGDEL' '5'.
*
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
** if lv_prev_pcode ne <lfs_data>-pcode.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-pcode.
* CLEAR lv_fname.
*
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-prvar.
* CLEAR lv_fname.
* clear: lv_count.
*
*
**Populate the select options for the Locations
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=%012'.
*
**Populate the select options screen for product
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLALDB' '3000',
** ' ' 'BDC_SUBSCR' 'SAPLALDB 3010SCREEN_HEADER',
* ' ' 'BDC_OKCODE' '=ACPT'.
*
* CLEAR lv_count.
* lv_count = lv_count + 1.
* CONCATENATE gc_rscsel gc_l_brac lv_count gc_r_brac INTO lv_fname.
* PERFORM f_populate_bdc_tab USING:
* ' ' lv_fname <lfs_data>-locno.
* CLEAR lv_fname.
* clear: lv_count.
*
** Populate the objects to be deleted on first screen
* PERFORM f_populate_bdc_tab USING:
* 'X' 'APO_BAPI_DEL_TRANS_OBJECTS' '1000',
* ' ' 'BDC_OKCODE' '=DUMMY',
* ' ' 'PLVERS' '000',
* ' ' 'MD_SYS' p_log,
* ' ' 'EXTRB' 'X',
* ' ' 'MO_DEL' 'X',
* ' ' 'MO_STAT' 'X',
* ' ' 'PO_DEL' 'X',
* ' ' 'SL_DEL' 'X',
* ' ' 'ST_DEL' 'X',
* ' ' 'PIR_DEL' 'X',
* ' ' 'SIM' space,
* ' ' ' ' ' '.
*
* PERFORM f_populate_bdc_tab USING:
* 'X' 'SAPLSPO1' '0500',
* ' ' 'BDC_OKCODE' '=OPT1'.
*
* CLEAR gv_msg_error.
* lv_ctu_params-nobinpt = space.
** lv_ctu_params-dismode = gc_mode_e.
* lv_ctu_params-dismode = 'A'.
* lv_ctu_params-updmode = 'S'.
* lv_ctu_params-racommit = 'X'.
*
** Call Transaction /SAPAPO/BP2 for deleting the Transactional data
* CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS
* FROM lv_ctu_params
* MESSAGES INTO lt_msg_tab.
*
* clear: ct_bdc_data.
*
* ENDLOOP.
***
******MOD-1887120: Code that is changed after the release of transport.
****CTS D7AK900899 New Version
****Version 0006
**MOD-1887120:
* CLEAR gv_msg_error.
* lv_ctu_params-nobinpt = space.
** lv_ctu_params-dismode = gc_mode_e.
* lv_ctu_params-dismode = 'A'.
* lv_ctu_params-updmode = 'S'.
* lv_ctu_params-racommit = 'X'.
*
** Call Transaction /SAPAPO/BP2 for deleting the Transactional data
* CALL TRANSACTION gc_bp2 USING ct_bdc_data OPTIONS FROM lv_ctu_params
* MESSAGES INTO lt_msg_tab.
**MOD-1887120:
** commit work and wait.
*MOD-1771404
  DO 10 TIMES.
    WAIT UP TO '0.1' SECONDS.
    percentage_text(3) = sy-index.
    percentage_text+3 = '%'.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        percentage = sy-index
        text       = percentage_text.
  ENDDO.
*MOD-1771404
  LOOP AT lt_msg_tab ASSIGNING <lfs_msg_ret>
    WHERE msgtyp = gc_error OR
          msgtyp = gc_abend.
    gv_msg_error = gc_set.
    EXIT.
  ENDLOOP.
ENDFORM. " f_delete_transactional_data
*&---------------------------------------------------------------------*
*& Form F_POPULATE_BDC_TAB
*&---------------------------------------------------------------------*
* Pupulates BDC Table with the parameters passed to the perform
*----------------------------------------------------------------------*
* -->IV_FLG Flag for New Screem
* -->IV_VAR1 Program Name / Field Name
* -->IV_VAR2 Screen Number / Field Value
*----------------------------------------------------------------------*
FORM f_populate_bdc_tab USING iv_flg TYPE any
                              iv_var1 TYPE any
                              iv_var2 TYPE any.
* Local Data Declaration
  DATA: ls_bdc_data TYPE bdcdata.
* Populate the BDC Table with the variables passed in it.
  CLEAR ls_bdc_data.
  IF iv_flg = gc_set. " = 'X'
    ls_bdc_data-program = iv_var1.
    ls_bdc_data-dynpro = iv_var2.
    ls_bdc_data-dynbegin = gc_set.
  ELSE.
    ls_bdc_data-fnam = iv_var1.
    ls_bdc_data-fval = iv_var2.
  ENDIF.
  APPEND ls_bdc_data TO gt_bdc_tab.
ENDFORM. " F_POPULATE_BDC_TAB
*&---------------------------------------------------------------------*
*& Form f_delete_manual_created_data
*&---------------------------------------------------------------------*
* Delete the Manually entered transactional data for prod and loc
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_VARIANT_TABLE Screen Varient Table.
*----------------------------------------------------------------------*
FORM f_delete_manual_created_data USING it_core_prod TYPE t_f4_data_loc
                                        it_prod_var TYPE t_f4_data_loc
                                        it_loc_data TYPE t_f4_data_loc
CHANGING ct_variant_table TYPE t_rsparams.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc.
* Populate the selection screen table
  PERFORM f_populate_screen_table USING:
* Field Name Type Sign Option Low Value
* Populate the Additional selection options
* Select the Manual selection button
  gc_manual_sel gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Populate the Planning Version
  gc_plan_vrsn gc_parameter gc_sign_i gc_option_eq gc_pvrsn_000 space.
* Populate the Core Products to be deleted
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_product gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-pcode space.
  ENDLOOP.
* Populate the Product Variants to be deleted
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_product gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-prvar space.
  ENDLOOP.
* Populate the Locations to be deleted.
  LOOP AT it_loc_data ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_location gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-locno space.
  ENDLOOP.
* Populate the Fixed Orders Checkbox
  PERFORM f_populate_screen_table USING:
  gc_fixed_ord gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Populate the Selection Objects
* Unselect the Key Figure Radio Button
  gc_key_fig gc_radio_btn gc_sign_i gc_option_eq space space,
* By Category Radio Button
  gc_category gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Populate the Settings
* Select Delete Transactional data only in APO Radio Button
  gc_only_apo gc_radio_btn gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Test Run check box.
  gc_test_run gc_parameter gc_sign_i gc_option_eq space space.
* Submit to SAP Standard report /SAPAPO/RLCDELETE
* For deleting manually entered data for the product and location
  SUBMIT /sapapo/rlcdelete WITH SELECTION-TABLE ct_variant_table
  AND RETURN.
ENDFORM. " f_delete_manual_created_data
*&---------------------------------------------------------------------*
*& Form F_POPULATE_SCREEN_TABLE
*&---------------------------------------------------------------------*
* Populate the screen table with the parameters passed
*----------------------------------------------------------------------*
* -->IV_NAME Screen field Name
* -->IV_TYPE Screen field type
* -->IV_SIGN Sign I / E
* -->IV_OPTION Option BT/EQ
* -->IV_LOW Lower value
* -->IV_HIGH Higher value
*----------------------------------------------------------------------*
FORM f_populate_screen_table USING iv_name TYPE rsscr_name
                                    iv_type TYPE rsscr_kind
                                    iv_sign TYPE tvarv_sign
                                    iv_option TYPE tvarv_opti
                                    iv_low TYPE any
                                    iv_high TYPE tvarv_val.
* Local Data Declaration
  DATA: ls_variant TYPE rsparams.
* Populate the Screen Table
  ls_variant-selname = iv_name.
  ls_variant-kind = iv_type.
  ls_variant-sign = iv_sign.
  ls_variant-option = iv_option.
  ls_variant-low = iv_low.
  ls_variant-high = iv_high.
  APPEND ls_variant TO gt_variant_table.
  CLEAR ls_variant.
ENDFORM. " F_POPULATE_SCREEN_TABLE
*&---------------------------------------------------------------------*
*& Form f_delete_ppds_orders
*&---------------------------------------------------------------------*
* Delete the PPDS orders
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the locations to be deleted
* <--CT_VARIANT_TABLE Screen Varient Table.
*----------------------------------------------------------------------*
FORM f_delete_ppds_orders USING it_core_prod TYPE t_f4_data_loc
                                it_prod_var TYPE t_f4_data_loc
                                it_loc_data TYPE t_f4_data_loc
CHANGING ct_variant_table TYPE t_rsparams.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc.
  REFRESH ct_variant_table.
* Populate the selection screen variant
  PERFORM f_populate_screen_table USING:
* Field Name Type Sign Option Low Value
* Populate the Location Product details
  gc_pl_vrsn gc_parameter gc_sign_i gc_option_eq gc_pvrsn_000 space.
* Populate the Core Products to be deleted
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_prod gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-pcode space.
  ENDLOOP.
* Populate the Product Variants to be deleted
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_prod gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-prvar space.
  ENDLOOP.
* Populate the Locations to be deleted.
  LOOP AT it_loc_data ASSIGNING <lfs_data>.
    PERFORM f_populate_screen_table USING:
    gc_loc gc_sel_optn gc_sign_i gc_option_eq <lfs_data>-locno space.
  ENDLOOP.
* Populate Product planned externally checkbox
  PERFORM f_populate_screen_table USING:
  gc_plan_prod gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Populate Subcontracting Planned Orders
  gc_contract gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Display order check box
  gc_display_ord gc_parameter gc_sign_i gc_option_eq space space,
* Check the Delete Order check box
  gc_delete_ord gc_parameter gc_sign_i gc_option_eq gc_set_val space,
* Uncheck the Send Deleted orders checkbox
  gc_send_delete gc_parameter gc_sign_i gc_option_eq space space.
* Submit standard program /SAPAPO/DELETE_PP_ORDER
* For deleting PPDS orders created for the product and location
  SUBMIT /sapapo/delete_pp_order WITH SELECTION-TABLE ct_variant_table
  AND RETURN.
ENDFORM. " f_delete_ppds_orders
*&---------------------------------------------------------------------*
*& Form f_delete_product_from_tlane
*&---------------------------------------------------------------------*
* Delete Products from the Transportation Lane
*&---------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* <--CT_LOC_DATA Itab with the locations to be deleted
* <--CT_MATID_ALL Itab with Matid for all the Products
* <--CT_TLANE Itab with transportation lane data
*----------------------------------------------------------------------*
FORM f_delete_product_from_tlane USING it_core_prod TYPE t_f4_data_loc
                                        it_prod_var TYPE t_f4_data_loc
                              CHANGING ct_loc_data TYPE t_f4_data_loc
                                        ct_matid_all TYPE t_matid
                                        ct_tlane TYPE t_tlane_data.
  CONSTANTS:
  lc_stime TYPE t VALUE '000000',
  lc_etime TYPE t VALUE '235959',
  lc_utc TYPE timezone VALUE 'UTC'.
  DATA:
  lv_dummy TYPE /sapapo/c_loctype,
  lv_valfr TYPE timestamp,
  lv_valto TYPE timestamp,
  lv_matid TYPE /sapapo/matid,
  lv_srcid TYPE /sapapo/locid,
  lv_desid TYPE /sapapo/locid.
  DATA:
  lt_trprod TYPE STANDARD TABLE OF /sapapo/trprod,
  lt_matid TYPE t_matid,
  ls_matid TYPE s_matid,
  lt_tlaneupd TYPE t_tlaneupd,
  ls_tlaneupd TYPE s_tlaneupd.
  FIELD-SYMBOLS:
  <lfs_trprod> TYPE /sapapo/trprod.
* Local Data Declaration
  DATA: lv_matid_low TYPE /sapapo/matid,
  lv_locid_low TYPE /sapapo/locid,
  lv_loctyp TYPE /sapapo/c_loctype,
  lr_matid TYPE RANGE OF /sapapo/matid,
  ls_trprod TYPE s_tlane_data,
  lr_locid TYPE RANGE OF /sapapo/locid,
  ls_locid TYPE s_locid,
  ls_loc_new TYPE /sapapo/loc,
  lt_locid_all TYPE STANDARD TABLE OF s_locid,
  lv_locid TYPE /sapapo/locid,
  lv_locno TYPE /sapapo/locno.
* Field-symbols
  FIELD-SYMBOLS: <lfs_data> TYPE s_f4_data_loc,
  <lfs_locid> TYPE s_locid,
  <lfs_matid> TYPE s_matid.
*MOD-1771404
  FIELD-SYMBOLS:
* <lfs_zdm0705> TYPE zdm0705,
  <lfs_prod_data> TYPE s_f4_data_loc,
  <lfs_matloc> TYPE s_matloc.
*MOD-1771404
  TYPES: BEGIN OF s_view_data,
  pcode TYPE matnr, " Product
  maktx TYPE char40, " Prod Desc
  markt TYPE char4, " Market
  martx TYPE char20, " Market Desc
  pdctr TYPE char2, " Product Center
  pctxt TYPE char20, " Description
  whse1 TYPE char3, " Warehouse1
  whse2 TYPE char3, " Warehouse2
  prvar TYPE char18, " Product Variant
  vartx TYPE char30, " Variant Description
  status TYPE char1, " Status
  END OF s_view_data,
  lv_view_data TYPE STANDARD TABLE OF s_view_data.
* For Core Products
  LOOP AT it_core_prod ASSIGNING <lfs_data>.
* Get the MATID for the Core Products
    PERFORM f_get_matid_for_prod USING <lfs_data>-pcode
                              CHANGING lv_matid_low.
    ls_matid-matid = lv_matid_low.
    ls_matid-matnr = <lfs_data>-pcode.
    APPEND ls_matid TO ct_matid_all.
    CLEAR ls_matid.
* Populate the Range Table with the Core Products
    PERFORM f_populate_range_table_matid USING lv_matid_low
                                      CHANGING lr_matid.
    CLEAR lv_matid_low.
  ENDLOOP.
* For Product Variants
  LOOP AT it_prod_var ASSIGNING <lfs_data>.
* Get the MATID for the Core Products
    PERFORM f_get_matid_for_prod USING <lfs_data>-prvar
                              CHANGING lv_matid_low.
    ls_matid-matnr = <lfs_data>-prvar.
    ls_matid-matid = lv_matid_low.
    APPEND ls_matid TO ct_matid_all.
    CLEAR ls_matid.
* Populate the Range Table with the Core Products
    PERFORM f_populate_range_table_matid USING lv_matid_low
                                      CHANGING lr_matid.
    CLEAR lv_matid_low.
  ENDLOOP.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lr_matid 
      COMPARING sign option low high.
*MOD-1771404
* For Locations
  LOOP AT ct_loc_data ASSIGNING <lfs_data>.
* Get the Locid for the Location
    PERFORM f_get_locid_for_loc USING <lfs_data>-locno
                              CHANGING lv_locid_low
                                        lv_loctyp.
    <lfs_data>-loctyp = lv_loctyp.
    ls_locid-locno = <lfs_data>-locno.
    ls_locid-locid = lv_locid_low.
    APPEND ls_locid TO lt_locid_all.
    CLEAR ls_locid.
* Populate the Range Table with the Location
    PERFORM f_populate_range_table_locid USING lv_locid_low
    CHANGING lr_locid.
    CLEAR: lv_locid_low, lv_loctyp.
  ENDLOOP.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lr_locid 
      COMPARING sign option low high.
*MOD-1771404
*MOD-1771404
** Get all the transportation lanes for the above Product and Locations
* SELECT *
* FROM /sapapo/trprod
* INTO TABLE lt_trprod
* WHERE matid IN lr_matid AND
* ( locfr IN lr_locid OR
* locto IN lr_locid ).
* IF sy-subrc <> 0.
** No Transportation lane selected for deletion
* MESSAGE i047.
** EXIT.
* ENDIF.
*MOD-1771404
* Get all the transportation lanes for the above Product and Locations
  SELECT *
    FROM /sapapo/trprod
    INTO TABLE lt_trprod
    WHERE matid IN lr_matid AND
          ( locfr IN lr_locid OR
          locto IN lr_locid ).
  IF sy-subrc <> 0.
* No Transportation lane selected for deletion
    MESSAGE i047.
* EXIT.
  ENDIF.
*MOD-1771404
  DELETE ADJACENT DUPLICATES FROM lt_locid_all COMPARING locid locno.
*MOD-1771404
  LOOP AT lt_trprod ASSIGNING <lfs_trprod>.
    READ TABLE lt_locid_all ASSIGNING <lfs_locid> WITH KEY
    locid = <lfs_trprod>-locto.
    IF sy-subrc = 0.
      ls_trprod-locto = <lfs_locid>-locno.
    ENDIF.
    CLEAR ls_loc_new.
    CALL FUNCTION '/SAPAPO/CMDS_LOC_SINGLE'
      EXPORTING
        iv_locid          = <lfs_trprod>-locfr
      IMPORTING
        es_loc            = ls_loc_new
      EXCEPTIONS
        empty_locid       = 1
        no_location_found = 2
        OTHERS            = 3.
    IF sy-subrc = 0.
      ls_trprod-locfr = ls_loc_new-locno.
    ENDIF.
*MOD-1771404
    CLEAR: lv_locid, lv_locno.
    SELECT SINGLE locid locno
    FROM /sapapo/loc
    INTO (lv_locid, lv_locno)
    WHERE locid = <lfs_trprod>-locto.
    IF sy-subrc = 0.
      IF ls_trprod-locto IS INITIAL.
        ls_trprod-locto = lv_locno.
      ENDIF.
    ENDIF.
*MOD-1771404
    READ TABLE ct_matid_all ASSIGNING <lfs_matid> WITH KEY
    matid = <lfs_trprod>-matid.
    IF sy-subrc = 0.
      ls_trprod-matid = <lfs_matid>-matnr.
    ENDIF.
    APPEND ls_trprod TO ct_tlane.
    CLEAR ls_trprod.
  ENDLOOP.
**MOD-1887120 Changes made after release of transport on 28.03
  DATA: l_wa_zdm0705 TYPE zdm0705.
  DATA: l_wa_zdu1002 TYPE zdu1002.
  DATA: lv_prvar TYPE char18.
  DATA: lv_indicator1 TYPE i.
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
**MOD-1887120
  DATA: lv_product TYPE char18.
* clear: l_wa_zdu1002.
* clear: l_wa_zdm0705.
* SORT it_zdm0705 BY prvar parpr.
** DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* LOOP AT IT_ZDM0705 INTO L_WA_ZDM0705.
* READ TABLE IT_ZDU1002 INTO L_WA_ZDU1002
* WITH KEY PRVAR = L_WA_ZDM0705-PRVAR BINARY SEARCH.
* IF SY-SUBRC = 0.
* IF L_WA_ZDU1002-STATUS NE 'I'.
* lv_indicator1 = 1.
* ENDIF.
* ENDIF.
* ENDLOOP.
***MOD-1887120 Changes made after release of transport on 28.03
* SORT it_zdm0705 BY prvar parpr.
* DELETE ADJACENT DUPLICATES FROM it_zdm0705 COMPARING ALL FIELDS.
* if lv_indicator1 = 1.
* LOOP AT it_zdu1002 into l_wa_zdu1002 WHERE STATUS NE 'I'.
* READ TABLE IT_ZDM0705 ASSIGNING <LFS_ZDM0705>
* WITH KEY PRVAR = L_WA_ZDU1002-PRVAR BINARY SEARCH.
** LOOP AT IT_ZDM0705 into l_wa_zdm0705
** WHERE prvar = l_wa_zdu1002-prvar.
* IF SY-SUBRC = 0.
* <lfs_zdm0705>-dmoap = 'X'.
** l_wa_zdm0705-dmoap = 'X'.
** modify it_zdm0705 from l_wa_zdm0705 transporting dmoap.
* ENDIF.
* ENDLOOP.
** ENDLOOP.
* endif.
*
**MOD-1887120 Changes made after release of transport on 28.03
  IF lv_indicator1 = 1.
    LOOP AT it_prod_var ASSIGNING <lfs_prod_data>.
      READ TABLE it_zdm0705 INTO l_wa_zdm0705 WITH KEY
      parpr = <lfs_prod_data>-pcode
      dmoap = 'X'.
      IF sy-subrc = 0.
        DELETE it_core_prod WHERE pcode = <lfs_prod_data>-pcode.
* Indicates that the core product should not be included in
* transportation lane and /sapapo/bp2 transaction.
        READ TABLE gt_matloc ASSIGNING <lfs_matloc> WITH KEY
        matnr = <lfs_prod_data>-pcode
        locno = <lfs_prod_data>-locno.
        IF sy-subrc NE 0.
          READ TABLE gt_matloc ASSIGNING <lfs_matloc> WITH KEY
          matnr = <lfs_prod_data>-prvar
          locno = <lfs_prod_data>-locno.
          IF sy-subrc NE 0.
            <lfs_prod_data>-lv_exist = 1.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDIF.
  DELETE it_prod_var WHERE lv_exist = 1.
*MOD-1887120 Changes made after release of transport on 28.03
  LOOP AT ct_tlane INTO ls_trprod.
    READ TABLE it_zdm0705 INTO l_wa_zdm0705 WITH KEY
    prvar = ls_trprod-matid
    dmoap = 'X'.
    IF sy-subrc = 0.
      ls_trprod-matid = 'X'.
      MODIFY ct_tlane FROM ls_trprod TRANSPORTING matid.
      COMMIT WORK AND WAIT.
    ELSE.
      READ TABLE it_zdm0705 INTO l_wa_zdm0705 WITH KEY
      parpr = ls_trprod-matid
      dmoap = 'X'.
      IF sy-subrc = 0.
        ls_trprod-matid = 'X'.
        MODIFY ct_tlane FROM ls_trprod TRANSPORTING matid.
        COMMIT WORK AND WAIT.
      ENDIF.
    ENDIF.
  ENDLOOP.
  DELETE ct_tlane WHERE matid = 'X'.
**MOD-1887120 Changes made after release of transport on 28.03
  IF p_test IS NOT INITIAL.
    EXIT.
  ENDIF.
*MOD-1771404
* Delete SAP Table /SAPAPO/TRPROD
  DELETE /sapapo/trprod FROM TABLE lt_trprod.
  IF sy-subrc = 0.
    COMMIT WORK.
  ELSE.
    ROLLBACK WORK.
  ENDIF.
*MOD-1771404
ENDFORM. " f_delete_product_from_tlane
*&---------------------------------------------------------------------*
*& Form f_get_matid_for_prod
*&---------------------------------------------------------------------*
* Get the MATID for the material passed
*----------------------------------------------------------------------*
* -->IV_PRD Product (Material Number)
* <--CV_MATID MATID for the above material no
*----------------------------------------------------------------------*
FORM f_get_matid_for_prod USING iv_prd TYPE any
CHANGING cv_matid TYPE /sapapo/matid.
* Local Data Declaration
  DATA: lv_matnr TYPE /sapapo/matnr.
  CHECK NOT iv_prd IS INITIAL.
  CLEAR lv_matnr.
  lv_matnr = iv_prd.
  CALL FUNCTION '/SAPAPO/DM_MATERIAL_GET_MATID'
    EXPORTING
      iv_matnr        = lv_matnr
    IMPORTING
      ev_matid        = cv_matid
    EXCEPTIONS
      matid_not_found = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    CLEAR cv_matid.
  ENDIF.
ENDFORM. " f_get_matid_for_prod
*&---------------------------------------------------------------------*
*& Form f_get_locid_for_loc
*&---------------------------------------------------------------------*
* Get the LOCID for the Location passed
*----------------------------------------------------------------------*
* -->IV_MKT Location Number
* <--CV_LOCID LOCID for the above Location
* <--CV_LOCTYP Location Type
*----------------------------------------------------------------------*
FORM f_get_locid_for_loc USING iv_mkt TYPE /sapapo/locno
CHANGING cv_locid_low TYPE /sapapo/locid
cv_loctyp TYPE /sapapo/c_loctype.
* Local Data Declaration
  DATA: ls_loc TYPE /sapapo/loc.
  CHECK NOT iv_mkt IS INITIAL.
  CLEAR ls_loc.
  CALL FUNCTION '/SAPAPO/LOC_LOCNO_GET_LOCID'
    EXPORTING
      iv_locno           = iv_mkt
    IMPORTING
*     ev_locid           = cv_locid_low
      es_location        = ls_loc
    EXCEPTIONS
      location_not_found = 1
      not_qualified      = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    CLEAR cv_locid_low.
  ELSE.
    cv_loctyp = ls_loc-loctype.
    cv_locid_low = ls_loc-locid.
  ENDIF.
ENDFORM. " f_get_locid_for_loc
*&---------------------------------------------------------------------*
*& Form f_populate_range_table_matid
*&---------------------------------------------------------------------*
* Populate the Range thable wiht the values passed
*----------------------------------------------------------------------*
* -->IV_VALUE_LOW Lower Value of the Range
* <--ES_RANGE Structure of type Range
* <--CT_RANGE_TAB Table of type Range
*----------------------------------------------------------------------*
FORM f_populate_range_table_matid USING iv_value_low TYPE any
CHANGING ct_range_tab TYPE t_matid_range.
* Local Data Declaration
  DATA: ls_locid TYPE s_range_locid.
  ls_locid-sign = gc_sign_i.
  ls_locid-option = gc_option_eq.
  ls_locid-low = iv_value_low.
  APPEND ls_locid TO ct_range_tab.
  CLEAR ls_locid.
ENDFORM. " f_populate_range_table_matid
*&---------------------------------------------------------------------*
*& Form f_set_deletion_flag
*&---------------------------------------------------------------------*
* Set the deletion flag for the Products to be deleted
*----------------------------------------------------------------------*
* -->IT_CORE_PROD Itab with the core products to be deleted
* -->IT_PROD_VAR Itab with the product varients to be deleted
* -->IT_LOC_DATA Itab with the location details
*----------------------------------------------------------------------*
FORM f_set_deletion_flag USING it_core_prod TYPE t_f4_data_loc
it_prod_var TYPE t_f4_data_loc
it_loc_data TYPE t_f4_data_loc
ct_matloc TYPE t_matloc.
* Local Data Declaration
  DATA: lv_own_sys TYPE bapiapologsys,
  lv_matnr TYPE /sapapo/matnr,
  lt_product TYPE STANDARD TABLE OF bapi10001key,
  ls_product TYPE bapi10001key,
  ls_prod_key TYPE bapi_prodloc_key,
  lt_prod_key TYPE STANDARD TABLE OF bapi_prodloc_key,
  lt_matloc TYPE t_matloc,
  lt_return TYPE STANDARD TABLE OF bapiret2.
* Field Symbols
  FIELD-SYMBOLS: <lfs_mat> TYPE s_f4_data_loc,
  <lfs_product> TYPE bapi10001key,
  <lfs_loc> TYPE s_f4_data_loc,
  <lfs_return> TYPE bapiret2.
*MOD-1771404
*MOD-1887120
  SORT it_prod_var BY locno
  pcode
  prvar.
  DELETE ADJACENT DUPLICATES FROM it_prod_var COMPARING locno
  pcode
  prvar.
  SORT it_loc_data BY locno
  pcode
  prvar
  loctyp
  whse1.
  DELETE ADJACENT DUPLICATES FROM it_loc_data COMPARING locno
  pcode
  prvar
  loctyp
  whse1.
*MOD-1771404
* Populate the Core Product
  LOOP AT it_core_prod ASSIGNING <lfs_mat>.
    LOOP AT it_loc_data ASSIGNING <lfs_loc>.
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-pcode
      locno = <lfs_loc>-locno.
      IF sy-subrc = 0.
        ls_prod_key-loctype = <lfs_loc>-loctyp.
        ls_prod_key-product = <lfs_mat>-pcode.
        ls_prod_key-location = <lfs_loc>-locno.
        APPEND ls_prod_key TO lt_prod_key.
        CLEAR ls_prod_key.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
*MOD-1887120
  SORT lt_prod_key BY product location loctype.
  DELETE ADJACENT DUPLICATES FROM lt_prod_key
  COMPARING product
  location
  loctype.
*MOD-1887120
* Populate the Product Variant
  LOOP AT it_prod_var ASSIGNING <lfs_mat>.
    LOOP AT it_loc_data ASSIGNING <lfs_loc>.
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-prvar
      locno = <lfs_loc>-locno.
      IF sy-subrc = 0.
        ls_prod_key-loctype = <lfs_loc>-loctyp.
        ls_prod_key-product = <lfs_mat>-prvar.
        ls_prod_key-location = <lfs_loc>-locno.
        APPEND ls_prod_key TO lt_prod_key.
        CLEAR ls_prod_key.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
*MOD-1771404
*MOD-1887120
  SORT lt_prod_key BY product location loctype.
*MOD-1887120
  DELETE ADJACENT DUPLICATES FROM lt_prod_key COMPARING product
  location
  loctype.
*MOD-1771404
* Get the Own Logical system name
  CALL FUNCTION 'OWN_LOGICAL_SYSTEM_GET'
    IMPORTING
      own_logical_system             = lv_own_sys
    EXCEPTIONS
      own_logical_system_not_defined = 1
      OTHERS                         = 2.
  IF sy-subrc <> 0.
    CLEAR lv_own_sys.
  ENDIF.
* This BAPI will set the deletion flag for the materials selected
* for deletion in the SAP table /SAPAPO/MATLOC
  CALL FUNCTION 'BAPI_PRDSRVAPS_REMOVEITEMS'
    EXPORTING
      logical_system        = lv_own_sys
      business_system_group = gc_bs_group
      commit_control        = space
    TABLES
      product_location_keys = lt_prod_key
      return                = lt_return.
  CLEAR gv_flg_error.
  LOOP AT lt_return ASSIGNING <lfs_return>
  WHERE type = gc_error OR
  type = gc_abend .
    gv_flg_error = gc_set.
    EXIT.
  ENDLOOP.
  IF gv_flg_error = gc_set.
* Product deletion failed for all the selected products
    MESSAGE i048.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
* Delete the Products using the SAP report /SAPAPO/DELETE_PRODUCTS
    SUBMIT /sapapo/delete_products AND RETURN.
    SORT lt_prod_key BY product.
    DELETE ADJACENT DUPLICATES FROM lt_prod_key
    COMPARING product.
* Look for products which dont have a location
    SELECT matnr locno
    INTO TABLE lt_matloc
    FROM /sapapo/v_matloc
    FOR ALL ENTRIES IN gt_prod_data
    WHERE matnr = gt_prod_data-pcode OR
    matnr = gt_prod_data-prvar.
    SORT lt_matloc BY matnr.
    DELETE ADJACENT DUPLICATES FROM lt_matloc
    COMPARING matnr.
    LOOP AT gt_prod_data ASSIGNING <lfs_mat>.
      READ TABLE lt_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-pcode
      BINARY SEARCH.
      IF sy-subrc <> 0.
        ls_product-product = <lfs_mat>-pcode.
        APPEND ls_product TO lt_product.
      ENDIF.
      READ TABLE lt_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_mat>-prvar
      BINARY SEARCH.
      IF sy-subrc <> 0.
        ls_product-product = <lfs_mat>-prvar.
        APPEND ls_product TO lt_product.
      ENDIF.
    ENDLOOP.
    SORT lt_product.
    DELETE ADJACENT DUPLICATES FROM lt_product COMPARING ALL FIELDS.
* In case of copre products check if they have any active variants
* before deleting them
    LOOP AT lt_product ASSIGNING <lfs_product>.
      SELECT SINGLE matnr INTO lv_matnr
      FROM /sapapo/matkey
      WHERE matnr = <lfs_product>-product.
      IF sy-subrc <> 0.
        CLEAR <lfs_product>-product.
        CONTINUE.
      ENDIF.
      FIND gc_dot IN <lfs_product>-product.
      IF sy-subrc <> 0.
        LOOP AT gt_view_data TRANSPORTING NO FIELDS
        WHERE pcode = <lfs_product>-product AND
        status <> 'I'.
          CLEAR <lfs_product>-product.
          EXIT.
        ENDLOOP.
      ENDIF.
    ENDLOOP.
    DELETE lt_product WHERE product IS INITIAL.
    REFRESH gt_product.
    gt_product = lt_product.
    IF lt_product IS NOT INITIAL.
* Delete the products which dont have any locations
      CALL FUNCTION 'BAPI_PRDSRVAPS_DELMULTI'
        EXPORTING
          logical_system        = lv_own_sys
          business_system_group = gc_bs_group
          commit_control        = space
        TABLES
          product_keys          = lt_product
          return                = lt_return.
      CLEAR gv_flg_error.
      LOOP AT lt_return ASSIGNING <lfs_return>
      WHERE type = gc_error OR
      type = gc_abend .
        gv_flg_error = gc_set.
        EXIT.
      ENDLOOP.
      IF gv_flg_error = gc_set.
* Product deletion failed for all the selected products
        MESSAGE i048.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
* Delete the Products using the SAP report /SAPAPO/DELETE_PRODUCTS
        SUBMIT /sapapo/delete_products AND RETURN.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM. " f_set_deletion_flag
*&---------------------------------------------------------------------*
*& Form f_display_alv
*&---------------------------------------------------------------------*
* Display ALV report with selected Product and Locations
*----------------------------------------------------------------------*
* -->IT_PROD_DATA Itab with the Product and Location combs
* <--CT_FCAT Itab for Field Catelogue
* <--CT_MATID_ALL Itab with Matid for all the Products
*----------------------------------------------------------------------*
FORM f_display_alv USING it_prod_data TYPE t_f4_data_loc
ct_matloc TYPE t_matloc
CHANGING ct_fcat TYPE slis_t_fieldcat_alv
ct_matid_all TYPE t_matid.
*Local constants:
  CONSTANTS:
  lc_tabprd TYPE tabname VALUE 'LT_DISPLAYPRD'.
* Local Data Declaration
  DATA: ls_display TYPE s_display,
  ls_product TYPE bapi10001key,
  ls_layout TYPE slis_layout_alv,
  ls_displayprd TYPE s_displayprd,
  lt_displayprd TYPE t_displayprd,
  lv_flg_del TYPE char1,
  lv_flg_prd TYPE flag,
  lv_repid TYPE syrepid,
  lt_events TYPE STANDARD TABLE OF slis_alv_event,
  lt_eventsprd TYPE STANDARD TABLE OF slis_alv_event,
  lt_display TYPE STANDARD TABLE OF s_display,
  lt_prod_1 TYPE STANDARD TABLE OF s_f4_data_loc,
  lt_prod_2 TYPE STANDARD TABLE OF s_f4_data_loc,
  lt_matloc TYPE STANDARD TABLE OF /sapapo/v_matloc,
  lt_matlocdel TYPE STANDARD TABLE OF /sapapo/matloc,
  ls_matloc TYPE /sapapo/matloc,
  ls_matid TYPE s_matid.
*MOD-1771404
  DATA: lv_locid TYPE /sapapo/locid,
  lv_ware_desc TYPE /sapapo/loc_descr40.
*MOD-1771404
* Local Date Declaration
  DATA: lv_mkt TYPE char4, " Market
  lv_pdctr TYPE char2, " Production center
  lv_whse1 TYPE char3, " Warehouse1
  lv_whse2 TYPE char3, " Warehouse2
  l_wa_matloc TYPE s_matloc.
  DATA: l_wa_display TYPE s_display.
  lv_repid = sy-repid.
* Field-Symbols
  FIELD-SYMBOLS: <lfs_prod> TYPE s_f4_data_loc,
  <lfs_prod_data> TYPE s_f4_data_loc,
  <lfs_display> TYPE s_display,
  <lfs_displayprd> TYPE s_displayprd,
  <lfs_matloc> TYPE /sapapo/v_matloc,
  <lfs_matid> TYPE s_matid.
**MOD-1887120
* LOOP AT IT_PROD_DATA ASSIGNING <lfs_prod_data>.
* READ TABLE ct_matloc into l_wa_matloc
* WITH KEY matnr = <lfs_prod_data>-prvar
* locno = <lfs_prod_data>-locno.
* IF sy-subrc = 0.
* CLEAR: ls_display, ls_displayprd.
* ls_display-prod = <lfs_prod_data>-prvar.
* ls_display-p_des = <lfs_prod_data>-vartx.
**MOD-1771404
** ls_display-loc = <lfs_prod>-locno.
** ls_display-l_des = <lfs_prod>-desc.
**MOD-1771404
* ls_display-loc = l_wa_matloc-LOCNO.
* ls_display-l_des = lv_ware_desc.
* APPEND ls_display TO lt_display.
* <lfs_prod_data>-locno = l_wa_matloc-LOCNO.
* ENDIF.
* READ TABLE gt_product TRANSPORTING NO FIELDS
* WITH KEY product = <lfs_prod_data>-prvar.
* IF sy-subrc = 0.
* ls_displayprd-prod = <lfs_prod_data>-prvar.
* ls_displayprd-p_des = <lfs_prod_data>-vartx.
* APPEND ls_displayprd TO lt_displayprd.
* ENDIF.
* ENDLOOP.
**MOD-1887120
  lt_prod_1 = it_prod_data.
  lt_prod_2 = it_prod_data.
  CLEAR lv_flg_del.
*MOD-1887120
* SORT lt_prod_1 BY prvar locno.
  SORT lt_prod_1 BY locno pcode prvar pdctr status whse1 whse2.
* DELETE ADJACENT DUPLICATES FROM lt_prod_1 COMPARING prvar locno.
  DELETE ADJACENT DUPLICATES FROM lt_prod_1 COMPARING locno
  pcode
  prvar
  pdctr
  status
  whse1
  whse2.
*MOD-1887120
*MOD-1887120
* SORT lt_prod_2 BY pcode locno.
  SORT lt_prod_2 BY locno pcode prvar pdctr status whse1 whse2.
* DELETE ADJACENT DUPLICATES FROM lt_prod_2 COMPARING pcode locno.
  DELETE ADJACENT DUPLICATES FROM lt_prod_2 COMPARING locno
  pcode
  prvar
  pdctr
  status
  whse1
  whse2.
*MOD-1887120
**MOD-1771404
* PERFORM F_GET_LOC_DESC USING IT_PROD_DATA
* CHANGING lv_ware_desc.
**MOD-1771404
  LOOP AT lt_prod_1 ASSIGNING <lfs_prod>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_prod>-prvar
      locno = s_mkt-low.
*MOD-1887120
* locno = <lfs_prod>-locno.
*MOD-1887120
      IF sy-subrc = 0.
        CLEAR: ls_display, ls_displayprd.
        ls_display-prod = <lfs_prod>-prvar.
        ls_display-p_des = <lfs_prod>-vartx.
*MOD-1771404
* ls_display-loc = <lfs_prod>-locno.
* ls_display-l_des = <lfs_prod>-desc.
*MOD-1771404
*MOD-1887120
        ls_display-loc = s_mkt-low.
*MOD-1887120
*MOD-1887120
* ls_display-loc = <lfs_prod>-locno.
*MOD-1887120
**MOD-1887120
        PERFORM f_get_loc_desc CHANGING lv_ware_desc.
**MOD-1887120
        ls_display-l_des = lv_ware_desc.
        APPEND ls_display TO lt_display.
      ENDIF.
      READ TABLE gt_product TRANSPORTING NO FIELDS
      WITH KEY product = <lfs_prod>-prvar.
      IF sy-subrc = 0.
        ls_displayprd-prod = <lfs_prod>-prvar.
        ls_displayprd-p_des = <lfs_prod>-vartx.
        APPEND ls_displayprd TO lt_displayprd.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
  LOOP AT lt_prod_2 ASSIGNING <lfs_prod>.
*MOD-1887120
    LOOP AT s_mkt.
*MOD-1887120
      READ TABLE ct_matloc TRANSPORTING NO FIELDS
      WITH KEY matnr = <lfs_prod>-pcode
      locno = s_mkt-low.
*MOD-1887120
* locno = <lfs_prod>-locno.
*MOD-1887120
      IF sy-subrc = 0.
        CLEAR: ls_display, lv_flg_prd.
        LOOP AT gt_view_data TRANSPORTING NO FIELDS
        WHERE pcode = <lfs_prod>-pcode AND
        status <> 'I'.
          lv_flg_prd = 'X'.
        ENDLOOP.
        IF lv_flg_prd IS INITIAL.
          ls_display-prod = <lfs_prod>-pcode.
          ls_display-p_des = <lfs_prod>-maktx.
* ls_display-loc = <lfs_prod>-locno.
* ls_display-l_des = <lfs_prod>-desc.
*MOD-1887120
          ls_display-loc = s_mkt-low.
*MOD-1887120
* ls_display-loc = <lfs_prod>-locno.
*MOD-1887120
**MOD-1887120
* PERFORM F_GET_LOC_DESC USING lt_prod_2
          PERFORM f_get_loc_desc CHANGING lv_ware_desc.
**MOD-1887120
          ls_display-l_des = lv_ware_desc.
          APPEND ls_display TO lt_display.
          READ TABLE gt_product TRANSPORTING NO FIELDS
          WITH KEY product = <lfs_prod>-pcode.
          IF sy-subrc = 0.
            ls_displayprd-prod = <lfs_prod>-pcode.
            ls_displayprd-p_des = <lfs_prod>-maktx.
            APPEND ls_displayprd TO lt_displayprd.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
  LOOP AT gt_product INTO ls_product.
    ls_displayprd-prod = ls_product.
    FIND gc_dot IN ls_product.
    IF sy-subrc = 0.
      READ TABLE gt_prod_data ASSIGNING <lfs_prod>
      WITH KEY prvar = ls_product-product.
      IF sy-subrc = 0.
        ls_displayprd-p_des = <lfs_prod>-vartx.
      ENDIF.
    ELSE.
      READ TABLE it_prod_data ASSIGNING <lfs_prod>
      WITH KEY pcode = ls_product-product.
      IF sy-subrc = 0.
        ls_displayprd-p_des = <lfs_prod>-maktx.
      ENDIF.
    ENDIF.
  ENDLOOP.
  SORT lt_display BY prod loc.
*MOD-1887120
  DELETE ADJACENT DUPLICATES FROM lt_display
  COMPARING prod loc.
*MOD-1887120
**MOD-1887120
*LOOP AT lt_display into l_wa_display.
* READ TABLE CT_MATLOC INTO L_WA_MATLOC
* WITH KEY matnr = l_wa_display-prod
* locno = l_wa_display-loc.
* IF sy-subrc ne 0.
* l_wa_display-status = 'X'.
* modify lt_display from l_wa_display transporting status.
* ENDIF.
*ENDLOOP.
*DELETE lt_display WHERE status = 'X'.
**MOD-1887120
  SORT lt_displayprd BY prod.
  DELETE ADJACENT DUPLICATES FROM lt_displayprd
  COMPARING prod.
  IF lt_display IS INITIAL.
    EXIT.
  ENDIF.
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
    EXPORTING
      i_callback_program       = lv_repid
      i_callback_user_command  = gc_show_log
      i_callback_pf_status_set = gc_set_pf.
* Populate the top of page event.
  PERFORM f_get_events CHANGING lt_events.
  REFRESH gt_fcat.
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_3 gc_prodct gc_tabname text-003 gc_len_prod gc_left,
  gc_pos_4 gc_prod_des gc_tabname text-004 gc_len_p_des gc_left,
  gc_pos_5 gc_loctn gc_tabname text-005 gc_len_loc gc_left,
  gc_pos_6 gc_loc_des gc_tabname text-006 gc_len_l_des gc_left.
* If not test mode then display status of deletion
  IF p_test <> gc_set.
    PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
    gc_pos_2 gc_status gc_tabname text-019 gc_len_stat gc_left,
    gc_pos_1 gc_light gc_tabname space gc_len_lt gc_center.
    SELECT *
    FROM /sapapo/v_matloc
    INTO TABLE lt_matloc
    FOR ALL ENTRIES IN lt_display
    WHERE matnr = lt_display-prod AND
    locno = lt_display-loc.
*Check if the product is deleted.
    LOOP AT lt_display ASSIGNING <lfs_display>.
      READ TABLE lt_matloc ASSIGNING <lfs_matloc>
      WITH KEY matnr = <lfs_display>-prod
      locno = <lfs_display>-loc.
      IF sy-subrc = 0.
        <lfs_display>-status = text-020.
        <lfs_display>-light = gc_red.
        CLEAR <lfs_matloc>-lvorm.
        MOVE-CORRESPONDING <lfs_matloc> TO ls_matloc.
        APPEND ls_matloc TO lt_matlocdel.
        lv_flg_del = gc_set.
      ELSE.
        <lfs_display>-status = text-021.
        <lfs_display>-light = gc_green.
      ENDIF.
    ENDLOOP.
    IF lv_flg_del = gc_set. " Deletion failed for atleast one prod
* Deletion failed for some products.
      MODIFY /sapapo/matloc FROM TABLE lt_matlocdel.
      MESSAGE s064.
    ELSE.
* Deletion successful for all the products
      MESSAGE s065.
    ENDIF.
  ENDIF. " For IF p_test <> gc_set.
* Display the ALV List Display
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
    EXPORTING
      is_layout                  = ls_layout
      it_fieldcat                = gt_fcat
      i_tabname                  = gc_tabname
      it_events                  = lt_events
    TABLES
      t_outtab                   = lt_display
    EXCEPTIONS
      program_error              = 1
      maximum_of_appends_reached = 2
      OTHERS                     = 3.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
  IF p_test IS INITIAL.
    PERFORM f_get_events_prd CHANGING lt_eventsprd.
    REFRESH gt_fcat.
* Populate the Field Catelog
    PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
    gc_pos_3 gc_prodct lc_tabprd text-003 gc_len_prod gc_left,
    gc_pos_4 gc_prod_des lc_tabprd text-004 gc_len_p_des gc_left.
    IF p_test <> gc_set.
      PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
      gc_pos_2 gc_status lc_tabprd text-019 gc_len_stat gc_left,
      gc_pos_1 gc_light lc_tabprd space gc_len_lt gc_center.
* Check the Products from the /SAPAPO/MATKEY to check for deletion
      SELECT matid matnr INTO TABLE ct_matid_all
      FROM /sapapo/matkey
      FOR ALL ENTRIES IN lt_displayprd
      WHERE matnr = lt_displayprd-prod.
      LOOP AT lt_displayprd ASSIGNING <lfs_displayprd>.
        READ TABLE ct_matid_all INTO ls_matid
        WITH KEY matnr = <lfs_displayprd>-prod.
        IF sy-subrc = 0.
          <lfs_displayprd>-status = text-020.
          <lfs_displayprd>-light = gc_red.
          lv_flg_del = gc_set.
        ELSE.
          <lfs_displayprd>-status = text-021.
          <lfs_displayprd>-light = gc_green.
        ENDIF.
      ENDLOOP.
      IF lv_flg_del = gc_set. " Deletion failed for atleast one prod
* Deletion failed for some products.
        MESSAGE s064.
      ELSE.
* Deletion successful for all the products
        MESSAGE s065.
      ENDIF.
    ENDIF.
* Display the ALV List Display
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
      EXPORTING
        is_layout                  = ls_layout
        it_fieldcat                = gt_fcat
        i_tabname                  = lc_tabprd
        it_events                  = lt_eventsprd
      TABLES
        t_outtab                   = lt_displayprd
      EXCEPTIONS
        program_error              = 1
        maximum_of_appends_reached = 2
        OTHERS                     = 3.
    IF sy-subrc <> 0.
* Error in displaying end report
      MESSAGE e062.
      LEAVE LIST-PROCESSING.
    ENDIF.
  ENDIF.
* Displays the ALV list
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'
    EXCEPTIONS
      program_error = 1
      OTHERS        = 2.
ENDFORM. " f_display_alv
*&---------------------------------------------------------------------*
*& Form f_get_events
*&---------------------------------------------------------------------*
* Get the TOP OF PAGE event
*----------------------------------------------------------------------*
* <--CT_EVENTS Itab with the events to be triggred
*----------------------------------------------------------------------*
FORM f_get_events CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
  WITH KEY name = slis_ev_top_of_page
  BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = slis_ev_top_of_page.
  ENDIF.
ENDFORM. " f_get_events
*&---------------------------------------------------------------------*
*& Form vpopulate_fieldcat
*&---------------------------------------------------------------------*
* Populate the fieldcatelog for ALV report
*----------------------------------------------------------------------*
* -->IV_CPOS Column Position
* -->IV_FNAM Field Name
* -->IV_TABNAM Internal Table Name
* -->IV_TITLE Column Title
* -->IV_OPT_LEN Output Length
* -->IV_JUSTIFY Justify left / right
*----------------------------------------------------------------------*
FORM f_populate_fieldcat USING iv_cpos TYPE lvc_colpos
iv_fnam TYPE lvc_fname
iv_tabnam TYPE lvc_tname
iv_title TYPE reptext
iv_opt_len TYPE lvc_outlen
iv_justify TYPE lvc_just.
* Local Data Declaration
  DATA: ls_fcat TYPE slis_fieldcat_alv.
  CLEAR ls_fcat.
  ls_fcat-col_pos = iv_cpos.
  ls_fcat-fieldname = iv_fnam.
  ls_fcat-tabname = iv_tabnam.
  ls_fcat-reptext_ddic = iv_title.
  ls_fcat-outputlen = iv_opt_len.
  ls_fcat-just = iv_justify.
  IF iv_fnam = gc_light.
    ls_fcat-icon = gc_set.
  ENDIF.
  APPEND ls_fcat TO gt_fcat.
ENDFORM. " f_populate_fieldcat
*&---------------------------------------------------------------------*
*& Form F_SET_PF
*&---------------------------------------------------------------------*
* Set the PF Status for the display and log screen
*----------------------------------------------------------------------*
* -->CT_EXTAB
*----------------------------------------------------------------------*
FORM f_set_pf USING ct_extab TYPE slis_t_extab.             "#EC CALLED
* Dynamic perform required to set custom PF status for ALV report
                                                            "#EC *
* CT_EXTAB is required as per the standard exit perform of ALVgrid fm
* Local Data Declation.
  DATA: ls_extab TYPE slis_extab,
  lt_extab TYPE STANDARD TABLE OF slis_extab.
* If the Test Run check box is selected then disable the Log Icons
  IF p_test = gc_set.
    ls_extab-fcode = gc_td_log.
    APPEND ls_extab TO lt_extab.
    CLEAR ls_extab.
    SET PF-STATUS 'PF_DISPLAY_LOG' EXCLUDING lt_extab.
  ENDIF.
  IF gt_tlane IS INITIAL.
    ls_extab-fcode = gc_tlane.
    APPEND ls_extab TO lt_extab.
    CLEAR ls_extab.
  ENDIF.
  SET PF-STATUS 'PF_DISPLAY_LOG' EXCLUDING lt_extab.
ENDFORM. " F_SET_PF
*&---------------------------------------------------------------------*
*& Form f_show_log
*&---------------------------------------------------------------------*
* Display Log report using
*----------------------------------------------------------------------*
* --> iv_ucomm OK Code for the user command
* --> is_selfield Selected field information
*----------------------------------------------------------------------*
FORM f_show_log USING iv_ucomm TYPE sy-ucomm
is_selfield TYPE slis_selfield.                             "#EC CALLED
* Dynamic perform for displaying log based on user action
                                                            "#EC *
* IS_SELFIELD is passed as per the standard exit perform for ALVgrid
  IF iv_ucomm = gc_td_log.
* Local Data Delaration.
    DATA: lt_msg_dtl TYPE STANDARD TABLE OF bdcmsgcoll,
    lv_mode TYPE char1,
    lv_error TYPE char1,
    lv_date TYPE char10,
    lv_time TYPE char8,
    lv_date_to TYPE char10,
    lv_time_to TYPE char8.
* Field-Symbols
    FIELD-SYMBOLS: <lfs_msg_dtl> TYPE bdcmsgcoll.
    REFRESH gt_bdc_tab.
    CLEAR: lv_date, lv_time.
    CONCATENATE gv_date_before+6(2) gv_date_before+4(2)
    gv_date_before+0(4) INTO lv_date SEPARATED BY gc_dot.
    CONCATENATE gv_time_before+0(2) gv_time_before+2(2)
    gv_time_before+4(2) INTO lv_time SEPARATED BY gc_colon.
    CONCATENATE sy-datum+6(2) sy-datum+4(2)
    sy-datum+0(4) INTO lv_date_to SEPARATED BY gc_dot.
    CONCATENATE sy-uzeit+0(2) sy-uzeit+2(2)
    sy-uzeit+4(2) INTO lv_time_to SEPARATED BY gc_colon.
* Populate the BDC Table for SLG1 Transaction
    PERFORM f_populate_bdc_tab USING:
    'X' 'SAPLSLG3' '0100',
    ' ' 'BDC_CURSOR' 'BALHDR-ALPROG',
* ' ' 'BDC_OKCODE' '=SELE',
    ' ' 'BALHDR-OBJECT' '*',
    ' ' 'BALHDR-SUBOBJECT' '*',
    ' ' 'BALHDR-EXTNUMBER' '*',
    ' ' 'BALHDR-ALDATE' lv_date,
    ' ' 'BALHDR-ALTIME' lv_time,
    ' ' '*BALHDR-ALDATE' lv_date_to,
    ' ' '*BALHDR-ALTIME' lv_time_to,
    ' ' 'BALHDR-ALUSER' sy-uname,
    ' ' 'BALHDR-ALTCODE' '*',
    ' ' 'BALHDR-ALPROG' '*',
    ' ' 'MODE_ALL' 'X',
    ' ' 'PROBCLASS_4' 'X'.
    CLEAR lv_error.
    lv_mode = gc_foreground.
* Call Transaction SLG1 for displaying the LOG for the action.
    CALL TRANSACTION gc_slg1 USING gt_bdc_tab MODE lv_mode
    MESSAGES INTO lt_msg_dtl.
    LOOP AT lt_msg_dtl ASSIGNING <lfs_msg_dtl> WHERE
    msgtyp = gc_error OR
    msgtyp = gc_abend.
      lv_error = gc_set.
      EXIT.
    ENDLOOP.
    IF lv_error = gc_set.
* Error while displaying Log.
      MESSAGE i049.
    ENDIF.
  ELSEIF iv_ucomm = gc_tlane.
* Display Tlane deletion data.
    PERFORM f_display_tlane USING gt_tlane.
  ENDIF.
ENDFORM. " f_show_log
*&---------------------------------------------------------------------*
*& Form top_of_page
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for ALV report
*----------------------------------------------------------------------*
FORM top_of_page.                                           "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
  lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot.
  CONCATENATE sy-uzeit+0(2) sy-datum+2(2) sy-datum+4(2) INTO lv_time
  SEPARATED BY gc_dot.
* Populate the Header
  PERFORM f_populate_top_page USING:
  gs_type_h gc_space text-015,
  gs_type_s text-007 sy-uname,
  gs_type_s text-008 lv_date,
  gs_type_s text-009 lv_time,
  gs_type_s gc_space gc_space.
* If Test Run then select
  IF p_test = gc_set. " IF test run
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-013,
    gs_type_a space text-014.
  ELSEIF gv_msg_error = gc_set. "IF error in call trns
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-018.
  ELSEIF gv_flg_error = gc_set. " IF error in deletion
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-012.
  ELSEIF gv_flg_error <> gc_set. " IF no error
    PERFORM f_populate_top_page USING:
    gs_type_a text-010 text-011.
  ENDIF.
* Write the Header in the TOP OF PAGE of ALV
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = gt_top_page.
ENDFORM. " top_of_page
*&---------------------------------------------------------------------*
*& Form f_populate_top_page
*&---------------------------------------------------------------------*
* Populate the Header for ALV top of page with parameters passed
*----------------------------------------------------------------------*
* -->IV_TYPE Line Type
* -->IV_KEY Key
* -->IV_INFO Info to be printed
*----------------------------------------------------------------------*
FORM f_populate_top_page USING iv_type TYPE char1
iv_key TYPE any
iv_info TYPE any.
* Local Data Declaration
  DATA: ls_top_page TYPE slis_listheader.
  CLEAR ls_top_page.
  ls_top_page-typ = iv_type.
  ls_top_page-key = iv_key.
  ls_top_page-info = iv_info.
  APPEND ls_top_page TO gt_top_page.
ENDFORM. " f_populate_top_page
*&---------------------------------------------------------------------*
*& Form f_populate_range_table_LOCID
*&---------------------------------------------------------------------*
* Populate the Range thable wiht the values passed
*----------------------------------------------------------------------*
* -->IV_VALUE_LOW Lower Value of the Range
* <--CT_RANGE_TAB Table of type Range
*----------------------------------------------------------------------*
FORM f_populate_range_table_locid USING iv_value_low TYPE any
CHANGING ct_range_tab TYPE t_locid_range.
* Local Data Declaration
  DATA: ls_locid TYPE s_range_locid.
  ls_locid-sign = gc_sign_i.
  ls_locid-option = gc_option_eq.
  ls_locid-low = iv_value_low.
  APPEND ls_locid TO ct_range_tab.
  CLEAR ls_locid.
ENDFORM. " f_populate_range_table_LOCID
*&---------------------------------------------------------------------*
*& Form f_display_tlane
*&---------------------------------------------------------------------*
* Display Transportation lane deletion data
*----------------------------------------------------------------------*
* -->CT_TLANE Itab with details of Tlane deleted
*----------------------------------------------------------------------*
FORM f_display_tlane USING ct_tlane TYPE t_tlane_data.
* Local Data Declaration
  DATA: lt_tl_events TYPE STANDARD TABLE OF slis_alv_event.
* Populate the top of page event.
  PERFORM f_get_events_tl CHANGING lt_tl_events.
  REFRESH gt_fcat.
*MOD-1887120
  SORT ct_tlane BY locfr locto matid.
*MOD-1887120
* Populate the Field Catelog
  PERFORM f_populate_fieldcat USING :
* Position Field TABLE TITLE OUTPUT_LEN JUSTIFY
  gc_pos_1 gc_locfr gc_tl_tab text-023 gc_len_l_des gc_left,
  gc_pos_2 gc_locto gc_tl_tab text-024 gc_len_l_des gc_left,
  gc_pos_3 gc_prod_tl gc_tl_tab text-022 gc_len_l_des gc_left.
* Display the ALV Grid Display
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = gt_fcat
      i_save             = gc_save
      it_events          = lt_tl_events
    TABLES
      t_outtab           = ct_tlane
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* Error in displaying end report
    MESSAGE e062.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM. " f_display_tlane
*&---------------------------------------------------------------------*
*& Form f_get_events_tl
*&---------------------------------------------------------------------*
* Get event top of page for Tlane report
*----------------------------------------------------------------------*
* <--CT_EVENTS Itab for top of page
*----------------------------------------------------------------------*
FORM f_get_events_tl CHANGING ct_events TYPE slis_t_event.
* Field Symbols
  FIELD-SYMBOLS: <lfs_events> TYPE slis_alv_event.
* Collect all the events triggered
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    IMPORTING
      et_events       = ct_events
    EXCEPTIONS
      list_type_wrong = 1
      OTHERS          = 2.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
  SORT ct_events BY name.
  READ TABLE ct_events ASSIGNING <lfs_events>
  WITH KEY name = slis_ev_top_of_page
  BINARY SEARCH.
  IF sy-subrc = 0.
    <lfs_events>-form = gc_lt_top.
  ENDIF.
ENDFORM. " f_get_events_tl
*&---------------------------------------------------------------------*
*& Form F_TOP_TL
*&---------------------------------------------------------------------*
* Write Header details in the TOP OF PAGE for TLANE report
*----------------------------------------------------------------------*
FORM f_top_tl.                                              "#EC CALLED
* Dynamic perform for displaying the Top of Page heading in ALV report
* Local Data Declaration
  DATA: lv_time TYPE char8,
  lv_date TYPE char10.
  REFRESH gt_top_page.
  CLEAR: lv_time,lv_date.
  CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum+0(4) INTO lv_date
  SEPARATED BY gc_dot