You are on page 1of 24

*&---------------------------------------------------------------------*

*& include zpmmi1120_chess_demandfeed_f01 *


*&---------------------------------------------------------------------*
* author : gopeekrishnan gopinathan. *
* creation date : 06/06/2007 *
* component function : chess demand feed to clt *
* der : pmmi1120 *
* correction no : devk939622 *
* includes : zpmmi1120_chess_demandfeed_top. *
* zpmmi1120_chess_demandfeed_f01. *
*----------------------------------------------------------------------*
* logical database : none *
* sapscript layout set: none *
* short description : chess demand feed to clt *
* sort requirements : none *
* unix shell script : none *
* file locations : none *
* legacy file : none *
* logical file names : none *
* related der's : *
* text elements : 001-100 selection-option & parameter criteria *
* 101-200 error & informational messages *
* 201-300 titles, headers, etc. *
* ---------------------------------------------------------------------*
* copyright warning *
* this unpublished work, first created in 2004 and updated thereafter, *
* is protected under the copyright laws of the united states and of *
* other countries throughout the world. use disassembly, reproduction, *
* distribution etc. without the express written consent of united *
* technologies corporation is prohibited. copyright united technologies*
* corporation <2004, 2004>. all rights reserved. information contained *
* herein is proprietary and confidential and improper use or *
* disclosure may result in civil and penal sanctions. *
*--------------------------------------------------------------------- *
* direct table update warning *
* qr reviewers will not approve any qr 0/1/2 involving direct table *
* updates. *
*--------------------------------------------------------------------- *
*--------------------------------------------------------------------- *
* modification history *
* modified by: *
* modification date: *
* correction no: *
* der: tpr or scr: *
* phase: *
* description: *
*--------------------------------------------------------------------- *

*&---------------------------------------------------------------------*
*& form f_get_f4_chess
*&---------------------------------------------------------------------*
* >>>> f4 help for p_chess
*----------------------------------------------------------------------*
form f_get_f4_chess .

call function '/sapdmc/lsm_f4_server_file'


importing
serverfile = p_chess
exceptions
canceled_by_user = 1
others = 2.
if sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. " f_get_f4_chess
*&---------------------------------------------------------------------*
*& form f_get_f4_visual
*&---------------------------------------------------------------------*
* >>>>f4 help for p_visual
*----------------------------------------------------------------------*

form f_get_f4_visual .
call function '/sapdmc/lsm_f4_server_file'
importing
serverfile = p_visual
exceptions
canceled_by_user = 1
others = 2.
if sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. " f_get_f4_visual
*&---------------------------------------------------------------------*
*& form f_get_f4_error
*&---------------------------------------------------------------------*
* >>>>f4 help for p_error
*----------------------------------------------------------------------*

form f_get_f4_error .
call function '/sapdmc/lsm_f4_server_file'
importing
serverfile = p_error
exceptions
canceled_by_user = 1
others = 2.
if sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. " f_get_f4_error

*&---------------------------------------------------------------------*
*& form f_validate_p_chess
*&---------------------------------------------------------------------*
* >>> chess demand file validation *
*----------------------------------------------------------------------*

form f_validate_p_chess .

* replacing all ~*~ with system id in the file path


replace all occurrences of c_wild in p_chess with sy-sysid.

clear : i_files , v_file_out,v_out,v_out1.


* file path validation
call function 'cv120_split_path'
exporting
pf_path = p_chess
importing
pfx_path = v_out
pfx_file = v_file_out.
move v_out to v_out1.
call function 'rzl_read_dir_local'
exporting
name = v_out1
tables
file_tbl = i_files
exceptions
argument_error = 1
not_found = 2
others = 3.
if sy-subrc <> 0.
message e000 with text-102 p_chess text-120 .
endif.

endform. " f_validate_p_chess


*&---------------------------------------------------------------------*
*& form f_validate_p_visual
*&---------------------------------------------------------------------*
* >>> visual factory demand file validation *
*----------------------------------------------------------------------*
form f_validate_p_visual .

* replacing all ~*~ with system id in the file path


replace all occurrences of c_wild in p_visual with sy-sysid.

clear : i_files , v_file_out,v_out,v_out1.

* file path validation


call function 'cv120_split_path'
exporting
pf_path = p_visual
importing
pfx_path = v_out
pfx_file = v_file_out.
move v_out to v_out1.
call function 'rzl_read_dir_local'
exporting
name = v_out1
tables
file_tbl = i_files
exceptions
argument_error = 1
not_found = 2
others = 3.
if sy-subrc <> 0.
message e000 with text-103 p_visual text-120 .
endif.
endform. " f_validate_p_visual
*&---------------------------------------------------------------------*
*& form f_validate_p_error
*&---------------------------------------------------------------------*
* >>>error log file validation
*----------------------------------------------------------------------*
form f_validate_p_error .

* replacing all ~*~ with system id in the file path


replace all occurrences of c_wild in p_error with sy-sysid.

clear : i_files , v_file_out,v_out,v_out1.

* file path validation


call function 'cv120_split_path'
exporting
pf_path = p_error
importing
pfx_path = v_out
pfx_file = v_file_out.
move v_out to v_out1.
call function 'rzl_read_dir_local'
exporting
name = v_out1
tables
file_tbl = i_files
exceptions
argument_error = 1
not_found = 2
others = 3.
if sy-subrc <> 0.
message e000 with text-104 p_error text-120 .
endif.
endform. " f_validate_p_error
*&---------------------------------------------------------------------*
*& form f_fetch_data
*&---------------------------------------------------------------------*
* >>>>step1 : fetch the records from the database tables
*----------------------------------------------------------------------*
form f_fetch_data .

* selection of records from database table plaf.(planned order)


select plnum
matnr
plwrk
paart
gsmng
psttr
pedtr
kdauf
kdpos
verid
trmkz
pedti
from plaf
into table i_plaf
where plwrk eq c_2000. "c_2000.

if sy-subrc eq 0.
sort i_plaf by plnum ascending.
* selection of records from database table mdsb.
select rsnum
rspos
plnum
matnr
erfmg
from mdsb
into table i_mdsb
for all entries in i_plaf
where plnum eq i_plaf-plnum.

if sy-subrc eq 0.
sort i_mdsb by matnr ascending.
endif.
else.
* text-105 - no planned order numbers are available for the plant 2000
wa_error-message = text-105.
append wa_error to i_error.
clear wa_error.
endif.

* fetch the entire fields from the database table z1120_chs_dmnd


select * from z1120_chs_dmnd
into table i_chess_dmnd.

if sy-subrc eq 0.
sort i_chess_dmnd by zplnum ascending.
endif.

* fetch all the fields from the database table z1120_chs_wo_cnt


select * from z1120_chs_wo_cnt
into table i_wo_cnt.
if sy-subrc eq 0.
sort i_wo_cnt by zwo_counter ascending.
endif.

endform. " f_fetch_data


*&---------------------------------------------------------------------*
*& form f_maintain_po_numbers
*&---------------------------------------------------------------------*
* >>>step2 : maintain the planned order numbers in ztable
*----------------------------------------------------------------------*
form f_maintain_po_numbers .

if i_chess_dmnd[] is not initial.

* for each record in the internal table i_chess_dmnd based on


* the condition zplanord_del eq �n�.

loop at i_chess_dmnd into wa_chess_dmnd where zplanord_del = c_n.

* read the internal table i_plaf


clear wa_plaf.
read table i_plaf into wa_plaf
with key plnum = wa_chess_dmnd-zplnum
binary search.
if sy-subrc <> 0.
* update the error table
concatenate text-122 wa_chess_dmnd-zplnum
text-123 wa_chess_dmnd-zorig_rectyp wa_chess_dmnd-zmts_mto_flag
wa_chess_dmnd-zxr_serialno wa_chess_dmnd-zcurr_partnum
wa_chess_dmnd-zorig_catnum wa_chess_dmnd-zwonum
wa_chess_dmnd-zcurr_prdline into wa_error-message.

* append the internal table i_error from wa_error


append wa_error to i_error.

wa_chess_dmnd-zplanord_del = c_y.
wa_chess_dmnd-zdel_date = sy-datum.
wa_chess_dmnd-zlast_date = sy-datum.

* move the wa_chess_dmnd to wa_chess_dmnd1


move wa_chess_dmnd to wa_chess_dmnd1.

* append the internal table i_chess_dmnd1 from wa_chess_dmnd1


append wa_chess_dmnd1 to i_chess_dmnd1.

endif.
endloop.

endif.

endform. " f_maintain_po_numbers


*&---------------------------------------------------------------------*
*& form f_get_components_to_chess
*&---------------------------------------------------------------------*
* >>> step3 : identify the components that are sent to chess
*----------------------------------------------------------------------*
form f_get_components_to_chess .

* sort i_round
sort i_round by matnr ascending.

if i_plaf[] is not initial.


*loop the internal table i_plaf into wa_plaf
loop at i_plaf into wa_plaf.

*if the value of wa_plaf-paart = �vp�


if wa_plaf-paart = c_vp.
perform f_components_to_chess_demand.
else.
perform f_get_2179_info.
if v_flag1 = c_x.
clear v_flag1.
continue.
endif.
if v_mts_mto_flag = c_xr.
while ( v_counter <= v_ser ).
perform f_populate_1120_chs.
v_counter = v_counter + 1.
endwhile.
elseif v_mts_mto_flag ne c_xr.
if v_disc_flag = c_x.
* text-203 : no discrete part number found for planned order
* text-204 : not processing this planned order
concatenate text-203 wa_plaf-plnum text-204
into wa_error-message.
* append the internal table i_error from wa_error
append wa_error to i_error.
continue.
elseif v_disc_flag ne c_x.
perform f_populate_1120_chs .
endif.
endif.
endif.

endloop.

endif.

endform. " f_get_components_to_chess


*&---------------------------------------------------------------------*
*& form f_components_to_chess_demand
*&---------------------------------------------------------------------*
* >>> step4: form components_to_chess_demand section
*----------------------------------------------------------------------*
form f_components_to_chess_demand .
** sort i_round
* sort i_round by matnr ascending.

if i_mdsb[] is not initial.


*loop the internal table i_mdsb
loop at i_mdsb into wa_mdsb where plnum = wa_plaf-plnum.

wa_chess-status = c_1.
wa_chess-plant = c_nc1.
wa_chess-cur = c_cur.
wa_chess-part_number = wa_mdsb-matnr.
wa_chess-stream = c_forecast.
v_date = wa_plaf-psttr+6(2).
v_month = wa_plaf-psttr+4(2).
v_year = wa_plaf-psttr+2(2).

concatenate v_date v_month v_year into v_date1.

wa_chess-date = v_date1.
wa_chess-blank2 = space.

* read the internal table i_round


* binary search is avoided since we are appending the records
* to i_round inside the loop.

clear wa_round.
read table i_round into wa_round
with key matnr = wa_mdsb-matnr.
* binary search.
if sy-subrc eq 0.

if wa_round-rflag = c_x.
wa_chess-qty = floor( wa_mdsb-erfmg ).
wa_round-rflag = space.
* modify the internal table i_round from wa_round
modify table i_round from wa_round
transporting rflag.
elseif wa_round-rflag ne c_x.
wa_chess-qty = ceil( wa_mdsb-erfmg ).
wa_round-rflag = c_x.
* modify the internal table i_round from wa_round
modify table i_round from wa_round
transporting rflag.
endif.

else.
* get the nearest value using ceil
wa_chess-qty = ceil( wa_mdsb-erfmg ) .
wa_round-matnr = wa_mdsb-matnr.
wa_round-rflag = c_x.

* append the internal table i_round from wa_round


append wa_round to i_round.
endif.
* append the internal table i_chess from wa_chess
append wa_chess to i_chess.
clear wa_chess.
endloop.
endif.
endform. " f_components_to_chess_demand
*&---------------------------------------------------------------------*
*& form f_get_2179_info
*&---------------------------------------------------------------------*
* >>>>form get_2179_info section
*----------------------------------------------------------------------*

form f_get_2179_info .

data : v_return type i.

* call the function module z_get_prdcat_serial to get the flag

call function 'z_get_prdcat_serial'


exporting
i_plant = wa_plaf-plwrk
i_matnr = wa_plaf-matnr
i_mts_mto_xr_only = c_x
i_order = wa_plaf-kdauf
i_item = wa_plaf-kdpos
i_get_serial = c_y
importing
e_prd_cat = v_mts_mto_flag
e_matwa = v_matwa
e_no_serial = v_ser
tables
t_serial = i_serial
t_return = i_return.
if i_return is not initial.
describe table i_return lines v_return.

read table i_return into wa_return index v_return.


concatenate wa_return-message wa_plaf-plnum
into wa_error-message
separated by space.

append wa_error to i_error.


v_flag1 = c_x.
clear : i_return,v_return,wa_error.

else.

wa_mat_plant-plant = wa_plaf-plwrk.
wa_mat_plant-material = wa_plaf-matnr.
append wa_mat_plant to i_mat_plant.
clear wa_mat_plant.

case v_mts_mto_flag.

when c_mts.

* call the fm 1161_get_discrete_catalog_part_numer to get the


* discrete part number " to do fn does not exists

call function 'z_pmmc1161_get_discrete_cat_no'


exporting
i_map_disc = c_x
i_effective_date = wa_plaf-pedtr
i_prod = c_x
importing
i_no_discno_found = v_disc_flag
tables
t_material_plant = i_mat_plant
t_error_table = i_error_tab
t_prod_table = i_prd_table
t_full_data_table = i_full_data
exceptions
select_either_prod_or_fulldata = 1
select_one_cat_or_disc = 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.

when c_mto or c_eto.

wa_mat_plant-plant = wa_plaf-plwrk.
wa_mat_plant-material = v_matwa.
append wa_mat_plant to i_mat_plant.
clear wa_mat_plant.

call function 'z_pmmc1161_get_discrete_cat_no'


exporting
i_map_disc = c_x
i_effective_date = wa_plaf-pedtr
i_prod = c_x
importing
i_no_discno_found = v_disc_flag
tables
t_material_plant = i_mat_plant
t_error_table = i_error_tab
t_prod_table = i_prd_table
t_full_data_table = i_full_data
exceptions
select_either_prod_or_fulldata = 1
select_one_cat_or_disc = 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.

endcase.
endif.

endform. " f_get_2179_info


*&---------------------------------------------------------------------*
*& form f_populate_1120_chs
*&---------------------------------------------------------------------*
* >>>form populate_1120_chs section
*----------------------------------------------------------------------*
form f_populate_1120_chs .

* read the internal table i_chess_dmnd


clear wa_chess_dmnd.
read table i_chess_dmnd into wa_chess_dmnd
with key zplnum = wa_plaf-plnum
binary search.
if sy-subrc ne 0.
perform write_new_record.
else.
perform f_update_existing_records.
endif.

endform. " f_populate_1120_chs


*&---------------------------------------------------------------------*
*& form write_new_record
*&---------------------------------------------------------------------*
* >>>step7: form write_new_record section
*----------------------------------------------------------------------*

form write_new_record .
wa_chess_dmnd2-zplant_code = wa_plaf-plwrk.

if v_mts_mto_flag = c_xr.
wa_chess_dmnd2-zorig_rectyp = c_1.
elseif v_mts_mto_flag <> c_xr and wa_plaf-trmkz <> c_x.
wa_chess_dmnd2-zorig_rectyp = c_2.
elseif v_mts_mto_flag <> c_xr and wa_plaf-trmkz = c_x.
wa_chess_dmnd2-zorig_rectyp = c_3 .
endif.

wa_chess_dmnd2-zmts_mto_flag = v_mts_mto_flag.
wa_chess_dmnd2-zplnum = wa_plaf-plnum.
if v_mts_mto_flag = c_xr.
* read the internal table i_serial into wa_serial with index v_count
clear wa_serial.
read table i_serial into wa_serial
index v_counter.
if sy-subrc eq 0.
wa_chess_dmnd2-zxr_serialno = wa_serial-serial_no.
endif.

if wa_serial-status <> c_avlb.

concatenate text-201 ',' text-202 wa_plaf-plnum


wa_chess_dmnd2-zxr_serialno into wa_error-message.

*append the internal table i_error from wa_error


append wa_error to i_error.

endif.
endif.

wa_chess_dmnd2-zorig_partnum = v_matnr.
wa_chess_dmnd2-zcurr_partnum = v_matnr.
wa_chess_dmnd2-zorig_catnum = v_matwa.
wa_chess_dmnd2-zcurr_catnum = v_matwa.

if v_mts_mto_flag = c_xr.

wa_chess_dmnd2-zwonum = wa_chess_dmnd2-zxr_serialno.

else.
if wa_plaf-trmkz = c_x.
* read the table i_wo_cnt into wa_wo_cnt index 1.
clear wa_wo_cnt.
read table i_wo_cnt into wa_wo_cnt
index 1.
if sy-subrc eq 0.
wa_wo_cnt-zwo_counter = wa_wo_cnt-zwo_counter + 1.
wa_chess_dmnd2-zwonum = wa_wo_cnt-zwo_counter.

* update the database table z1120_chs_wo_cnt from wa_wo_cnt


update z1120_chs_wo_cnt from wa_wo_cnt.
endif.
endif.
endif.
v_verid = wa_plaf-verid+1(3) .
concatenate c_0 v_verid into v_verid.
wa_chess_dmnd2-zorig_prdline = v_verid.
wa_chess_dmnd2-zcurr_prdline = v_verid.
wa_chess_dmnd2-zorig_sdate = wa_plaf-psttr.
wa_chess_dmnd2-zcurr_sdate = wa_plaf-psttr.
wa_chess_dmnd2-zorig_fdate = wa_plaf-pedtr.
wa_chess_dmnd2-zcurr_fdate = wa_plaf-pedtr.
wa_chess_dmnd2-zorig_ftime = wa_plaf-pedti.
wa_chess_dmnd2-zcurr_ftime = wa_plaf-pedti.
wa_chess_dmnd2-zorig_qty = wa_plaf-gsmng.
wa_chess_dmnd2-zcurr_qty = wa_plaf-gsmng.
wa_chess_dmnd2-zdel_date = c_n.
wa_chess_dmnd2-zcreate_date = sy-datum.
wa_chess_dmnd2-zlast_date = space.

* append the internal table i_chess_dmnd2 from wa_chess_dmnd2


append wa_chess_dmnd2 to i_chess_dmnd2.
endform. " write_new_record
*&---------------------------------------------------------------------*
*& form f_update_existing_records
*&---------------------------------------------------------------------*
* >>> step8 : form update_existing_record section
*----------------------------------------------------------------------*

form f_update_existing_records .

if wa_chess_dmnd-zorig_rectyp = c_2 and wa_plaf-trmkz = c_x.

move wa_chess_dmnd to wa_chess_dmnd1 .


wa_chess_dmnd1-zlast_date = sy-datum.
wa_chess_dmnd1-zorig_rectyp = c_3.

* append the internal table i_chess_dmnd1 from wa_chess_dmnd1


append wa_chess_dmnd1 to i_chess_dmnd1.
concatenate wa_plaf-plnum text-205 into wa_error-message.

* append the internal table i_error from wa_error


append wa_error to i_error.

endif.

if wa_chess_dmnd-zorig_rectyp = c_3 and wa_plaf-trmkz <> c_x.

move wa_chess_dmnd to wa_chess_dmnd1.


wa_chess_dmnd1-zlast_date = sy-datum.
wa_chess_dmnd1-zorig_rectyp = c_2.

* append the internal table i_chess_dmnd1 from wa_chess_dmnd1

append wa_chess_dmnd1 to i_chess_dmnd1.

concatenate wa_plaf-plnum text-205 into wa_error-message.

* append the internal table i_error from wa_error


append wa_error to i_error.
endif.

if wa_chess_dmnd-zcurr_partnum <> v_matnr.

move wa_chess_dmnd to wa_chess_dmnd1.


wa_chess_dmnd1-zlast_date = sy-datum.
wa_chess_dmnd1-zcurr_partnum = v_matnr.

* append the internal table i_chess_dmnd1 from wa_chess_dmnd1


append wa_chess_dmnd1 to i_chess_dmnd1.

concatenate wa_plaf-plnum text-206 wa_chess_dmnd-zcurr_partnum


v_matnr into wa_error-message.
* append the internal table i_error from wa_error
append wa_error to i_error.
endif.
if wa_chess_dmnd-zcurr_catnum <> v_matwa.
move wa_chess_dmnd to wa_chess_dmnd1.
wa_chess_dmnd1-zlast_date = sy-datum.
wa_chess_dmnd1-zcurr_catnum = v_matwa.
* append the internal table i_chess_dmnd1 from wa_chess_dmnd1
append wa_chess_dmnd1 to i_chess_dmnd1.
concatenate wa_plaf-plnum text-207 wa_chess_dmnd-zcurr_catnum
v_matwa into wa_error-message.

* append the internal table i_error from wa_error


append wa_error to i_error.
endif.

v_verid = wa_plaf-verid+1(3).
concatenate c_0 v_verid into v_verid.

if wa_chess_dmnd-zcurr_prdline <> v_verid.

move wa_chess_dmnd to wa_chess_dmnd1.


wa_chess_dmnd1-zlast_date = sy-datum.
wa_chess_dmnd1-zcurr_prdline = v_verid.

* append the internal table i_chess_dmnd1 from wa_chess_dmnd1


append wa_chess_dmnd1 to i_chess_dmnd1.
concatenate wa_plaf-plnum text-208 wa_chess_dmnd-zcurr_prdline
v_verid into wa_error-message.

* append the internal table i_error from wa_error


append wa_error to i_error.
endif.
if wa_chess_dmnd-zcurr_sdate <> wa_plaf-psttr.

move wa_chess_dmnd to wa_chess_dmnd1.


wa_chess_dmnd1-zcurr_sdate = wa_plaf-psttr.
wa_chess_dmnd1-zlast_date = sy-datum.
*append the internal table i_chess_dmnd1 from wa_chess_dmnd1
append wa_chess_dmnd1 to i_chess_dmnd1.

concatenate wa_plaf-plnum text-209 wa_chess_dmnd-zcurr_sdate


wa_plaf-psttr into wa_error-message.

* append the internal table i_error from wa_error


append wa_error to i_error.
endif.

if wa_chess_dmnd-zcurr_fdate <> wa_plaf-pedtr.


move wa_chess_dmnd to wa_chess_dmnd1.
wa_chess_dmnd1-zcurr_fdate = wa_plaf-pedtr.
wa_chess_dmnd1-zlast_date = sy-datum.
*append the internal table i_chess_dmnd1 from wa_chess_dmnd1
append wa_chess_dmnd1 to i_chess_dmnd1.
concatenate wa_plaf-plnum text-210
wa_chess_dmnd-zcurr_fdate wa_plaf-pedtr into
wa_error-message.
* append the internal table i_error from wa_error
append wa_error to i_error.

endif.

if wa_chess_dmnd-zcurr_ftime <> wa_plaf-pedti.


move wa_chess_dmnd to wa_chess_dmnd1.
wa_chess_dmnd1-zcurr_ftime = wa_plaf-pedti.
wa_chess_dmnd1-zlast_date = sy-datum.

*append the internal table i_chess_dmnd1 from wa_chess_dmnd1


append wa_chess_dmnd1 to i_chess_dmnd1.
* text-211 : finish time has changed
concatenate wa_plaf-plnum text-211 wa_chess_dmnd-zcurr_ftime
wa_plaf-pedti into wa_error-message.
* append the internal table i_error from wa_error
append wa_error to i_error.

endif.
if wa_chess_dmnd-zcurr_qty <> wa_plaf-gsmng.

move wa_chess_dmnd to wa_chess_dmnd1.


wa_chess_dmnd1-zcurr_qty = wa_plaf-gsmng.
wa_chess_dmnd1-zlast_date = sy-datum.

*append the internal table i_chess_dmnd1 from wa_chess_dmnd1


append wa_chess_dmnd1 to i_chess_dmnd1.

*qty must be char type so move to a variable of type c and concatenate


move wa_chess_dmnd-zcurr_qty to v_zcurr_qty.
move wa_plaf-gsmng to v_gsmng.

concatenate wa_plaf-plnum text-212 v_zcurr_qty


v_gsmng into wa_error-message.

* append the internal table i_error from wa_error


append wa_error to i_error.
endif.

endform. " f_update_existing_records


*&---------------------------------------------------------------------*
*& form f_update_and_insert_records
*&---------------------------------------------------------------------*
* >>> step9: update and insert the records in the table z1120_chs_dmnd
*----------------------------------------------------------------------*

form f_update_and_insert_records .

if i_chess_dmnd1[] is not initial or i_chess_dmnd2[] is not initial.

* call the function module enqueue_e_tablee to lock the database table


* z1120_chs_dmnd
call function 'enqueue_e_table'
exporting
tabname = v_tabname
exceptions
foreign_lock = 1
system_failure = 2
others = 3.

if sy-subrc eq 0.

if i_chess_dmnd1[] is not initial.


update z1120_chs_dmnd from table i_chess_dmnd1.

if sy-subrc ne 0.
wa_error-message = text-213.
* append the internal table i_error from wa_error
append wa_error to i_error.
endif.
endif.
if i_chess_dmnd2[] is not initial.
insert z1120_chs_dmnd from table i_chess_dmnd2
accepting duplicate keys.
if sy-subrc <> 0.
wa_error-message = text-214.
* append the internal table i_error from wa_error
append wa_error to i_error.
endif.
endif.
endif.

* call the function module dequeue_e_table for unlocking


* the database table
call function 'dequeue_e_table'
exporting
tabname = v_tabname.

endif.

endform. " f_update_and_insert_records


*&---------------------------------------------------------------------*
*& form f_write_noncomp_rec_to_chess
*&---------------------------------------------------------------------*
* >>>> step10: write 2179 non component records to chess
*----------------------------------------------------------------------*
form f_write_noncomp_rec_to_chess .

* append lines of i_chess_dmnd1 into i_chess_dmnd2

if i_chess_dmnd2[] is not initial.


loop at i_chess_dmnd1 into wa_chess_dmnd1.
append wa_chess_dmnd1 to i_chess_dmnd2.
clear wa_chess_dmnd1.
endloop.
else.
i_chess_dmnd2[] = i_chess_dmnd1[].
endif.

if i_chess_dmnd2[] is not initial.

loop at i_chess_dmnd2 into wa_chess_dmnd2.


wa_chess-status = c_1.
wa_chess-plant = c_nc1.
wa_chess-cur = c_cur.

case wa_chess_dmnd2-zmts_mto_flag.
when c_xr.
wa_chess-part_number = wa_chess_dmnd2-zxr_serialno.
when c_eto.
wa_chess-part_number = wa_chess_dmnd2-zcurr_catnum.
when c_others.
wa_chess-part_number = wa_chess_dmnd2-zcurr_partnum.
endcase.

if wa_chess_dmnd2-zmts_mto_flag = c_xr.

wa_chess-stream = wa_chess_dmnd2-zxr_serialno.

elseif wa_chess_dmnd2-zorig_rectyp = c_3.


concatenate wa_chess_dmnd2-zcurr_prdline wa_chess_dmnd2-zwonum
into wa_chess-stream.
else.
concatenate wa_chess_dmnd2-zcurr_prdline c_999999 into
wa_chess-stream .

endif.

if wa_chess_dmnd2-zorig_fdate lt sy-datum.
wa_chess_dmnd2-zorig_fdate = sy-datum + 1.
endif.
v_date = wa_chess_dmnd2-zorig_fdate+6(2).
v_month = wa_chess_dmnd2-zorig_fdate+4(2).
v_year = wa_chess_dmnd2-zorig_fdate+2(2).
concatenate v_date v_month v_year into v_date1.
wa_chess-date = v_date1.
wa_chess-blank2 = space.

*read the internal table i_round " select statement is missing


clear wa_round.
read table i_round into wa_round
with key matnr = wa_chess_dmnd2-zcurr_partnum.
* binary search.
if sy-subrc eq 0.

if wa_round-rflag = c_x.

wa_chess-qty = floor( wa_chess_dmnd2-zcurr_qty ).


wa_round-rflag = space.

* modify the internal table i_round from wa_round


modify table i_round from wa_round
transporting rflag.

elseif wa_round-rflag ne c_x.

wa_chess-qty = ceil( wa_chess_dmnd2-zcurr_qty ) .


wa_round-rflag = c_x.
* modify the internal table i_round from wa_round
modify table i_round from wa_round
transporting rflag..
elseif sy-subrc <> 0.
wa_chess-qty = ceil( wa_chess_dmnd2-zcurr_qty ).
wa_round-matnr = wa_chess_dmnd2-zcurr_partnum.
wa_round-rflag = c_x.

* append the internal table i_round from wa_round


append wa_round to i_round.
endif.
endif.
* append the internal table i_chess from wa_chess
append wa_chess to i_chess.

endloop.
endif.

sort i_chess by date ascending.

endform. " f_write_noncomp_rec_to_chess


*&---------------------------------------------------------------------*
*& form f_write_vf_records
*&---------------------------------------------------------------------*
* >>> step11: write vf_visual factory records
*----------------------------------------------------------------------*

form f_write_vf_records .

* loop the internal table i_chess_dmnd2 into wa_chess_dmnd2

loop at i_chess_dmnd2 into wa_chess_dmnd2.

case wa_chess_dmnd2-zmts_mto_flag.
when c_xr.
wa_visual-bom = wa_chess_dmnd2-zxr_serialno.
when c_eto.
wa_visual-bom = wa_chess_dmnd2-zcurr_catnum.
when c_others.
wa_visual-bom = wa_chess_dmnd2-zcurr_partnum.
endcase.
wa_visual-catalog = wa_chess_dmnd2-zcurr_catnum.
wa_visual-work_order = wa_chess_dmnd2-zwonum.
if wa_chess_dmnd2-zcurr_fdate < sy-datum.
wa_chess_dmnd2-zcurr_fdate = wa_chess_dmnd2-zcurr_fdate + 1.
endif.

v_date = wa_chess_dmnd2-zorig_fdate+6(2).
v_month = wa_chess_dmnd2-zorig_fdate+4(2).
v_year = wa_chess_dmnd2-zorig_fdate+2(2).
concatenate v_date v_month v_year into v_date1.
wa_chess-date = v_date1.
wa_visual-ftime = wa_chess_dmnd2-zcurr_ftime.

*read the internal table i_round " select statement is missing


clear wa_round.
read table i_round into wa_round
with key matnr = wa_chess_dmnd2-zcurr_partnum.
* binary search.
if sy-subrc eq 0.

if wa_round-rflag = c_x.

wa_chess-qty = floor( wa_chess_dmnd2-zcurr_qty ).


wa_round-rflag = space.

* modify the internal table i_round from wa_round


modify table i_round from wa_round
transporting rflag.

elseif wa_round-rflag ne c_x.

*wa_chess-qty = ceil (wa_mdsb-erfmg)


wa_chess-qty = ceil( wa_chess_dmnd2-zcurr_qty ) .
wa_round-rflag = c_x.
* modify the internal table i_round from wa_round
modify table i_round from wa_round
transporting rflag.
elseif sy-subrc <> 0.

* use ceil function to get the nearest integer value


wa_chess-qty = ceil( wa_chess_dmnd2-zcurr_qty ) .
wa_round-matnr = wa_chess_dmnd2-zcurr_partnum.
wa_round-rflag = c_x.

* append the internal table i_round from wa_round


append wa_round to i_round.

wa_visual-production_line = wa_chess_dmnd2-zcurr_prdline.

* call the fm bapi_companycode_get_period to calculate the fiscal year


call function 'bapi_companycode_get_period'
exporting
companycodeid = c_1000
posting_date = wa_plaf-pedtr
importing
fiscal_year = v_fisc_year
fiscal_period = v_period
return = i_bapireturn.
wa_visual-fiscyear = v_fisc_year.

* call the fm date_get_week to calculate the fiscal week.

call function 'date_get_week'


exporting
date = wa_plaf-pedtr
importing
week = v_week
exceptions
date_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.

wa_visual-fiscweek = v_week+4(2).
v_qtr = ceil( wa_plaf-pedtr+4(2) / 3 ).
wa_visual-fiscquart = v_qtr.

* append the internal table i_visual from wa_visual

endif.
endif.
append wa_visual to i_visual.
endloop.
sort i_visual by fdate ascending.

endform. " f_write_vf_records


*&---------------------------------------------------------------------*
*& form f_move_chess_to_file
*&---------------------------------------------------------------------*
* >>>>step12: move the chess records to the file
*----------------------------------------------------------------------*

form f_move_chess_to_file .

if i_chess[] is not initial.

describe table i_chess lines v_chess.

* to open error file (entered on the selection screen) for writing


* in text mode.
open dataset p_chess for output in text mode encoding default.

* to check whether the operation is successful or not.


if sy-subrc ne 0.
skip.
* text-130 - unable to transfer chess records to application server
* write: text-130,p_chess.
concatenate text-130 p_chess into v_chess_message
separated by space .
wa_error-message = text-130.

append wa_error to i_error.

exit.
else.
clear: wa_chess ,v_string,v_chess_message.

loop at i_chess into wa_chess.


concatenate wa_chess-status
wa_chess-plant
wa_chess-cur
wa_chess-blank
wa_chess-part_number
wa_chess-stream
wa_chess-date
wa_chess-blank2
wa_chess-qty into v_string.
* moves the internal table i_chess contents to the chess file on
* the application server.
transfer v_string to p_chess.
clear: wa_chess,v_string.
endloop.
endif.
* text-223 : outbound file
* text-226 : for chess created successfully
concatenate text-223 p_chess text-226 into v_chess_msg
separated by space .

* to close the chess file


close dataset p_chess.
endif.

endform. " f_move_chess_to_file


*&---------------------------------------------------------------------*
*& form f_move_vf_to_file
*&---------------------------------------------------------------------*
* >>>>step13: move the visual factory records to the file
*----------------------------------------------------------------------*

form f_move_vf_to_file .

if i_visual[] is not initial.

describe table i_visual lines v_visual.

* display the message �total no of records extracted to vf file


* is� v_chess in the report layout

* to open vf file (entered on the selection screen) for writing


* in text mode.
open dataset p_visual for output in text mode encoding default.

* to check whether the operation is successful or not.


if sy-subrc ne 0.
skip.
* text-130 - unable to transfer vf records to application server
* write: text-131,p_visual.
concatenate text-131 p_visual into v_visual_message
separated by space .
wa_error-message = text-131.
append wa_error to i_error.
exit.
else.
clear: wa_visual ,v_string,v_visual_message.

move wa_visual-work_order to v_work_order.


move wa_visual-qty to v_qty.

loop at i_visual into wa_visual.


concatenate wa_visual-bom
wa_visual-model_number
wa_visual-catalog
v_work_order
v_qty
wa_visual-fdate
wa_visual-ftime
wa_visual-production_line
wa_visual-fiscweek
wa_visual-fiscquart
wa_visual-fiscyear
into v_string
separated by c_coma.
* moves the internal table i_visual contents to the vf file on
* the application server.
transfer v_string to p_visual.
clear: wa_visual,v_string.
endloop.
endif.
* text-223 : outbound file
* text-227 : for visual factory created successfully
concatenate text-223 p_visual text-227 into v_visual_msg
separated by space .

* to close the vf file


close dataset p_visual.

endif.

endform. " f_move_vf_to_file


*&---------------------------------------------------------------------*
*& form f_move_error_to_file
*&---------------------------------------------------------------------*
* >>>> step14: move the error records to the file
*----------------------------------------------------------------------*
form f_move_error_to_file .

if i_error[] is not initial.

describe table i_error lines v_error.

* display the message �total no of records extracted to error file


* is� v_error in the report layout

* to open error file (entered on the selection screen) for writing


* in text mode.
open dataset p_error for output in text mode encoding default.

* to check whether the operation is successful or not.


if sy-subrc ne 0.
skip.
* text-132 - unable to transfer error records to application server
* write: text-132,p_visual.
concatenate text-132 p_error into v_error_message
separated by space .
wa_error-message = text-132.
append wa_error to i_error.
exit.
else.
clear: wa_error,v_error_message .

loop at i_error into wa_error.

* moves the internal table i_error contents to the error file on


* the application server.
transfer wa_error to p_error.
clear: wa_error.
endloop.
endif.
* text-223 : outbound file
* text-228 : for error log created successfully
concatenate text-223 p_error text-228 into v_error_msg
separated by space .
* to close the error file
close dataset p_error.
endif.

endform. " f_move_error_to_file


*&---------------------------------------------------------------------*
*& form f_display_error
*&---------------------------------------------------------------------*
* >>>report to disply error
*----------------------------------------------------------------------*
form f_display_error .

uline.
format color col_heading intensified off.
write:/1 sy-vline,3 text-121,at sy-linsz sy-vline.
format color off.
uline.
write: /1 sy-vline,3 text-116,45 v_chess,
at sy-linsz sy-vline,
/1 sy-vline,3 text-117,45 v_visual,
at sy-linsz sy-vline,
/1 sy-vline,3 text-118,45 v_error,
at sy-linsz sy-vline.
uline.

skip.
if v_chess_msg is not initial or
v_visual_msg is not initial or
v_error_msg is not initial.

uline.
format color col_heading intensified off.
write:/1 sy-vline,3 text-222,at sy-linsz sy-vline.
format color off.
uline.

if v_chess_msg is not initial.


write: /1 sy-vline,3 v_chess_msg,
at sy-linsz sy-vline.
endif.
if v_visual_msg is not initial.
write: /1 sy-vline,3 v_visual_msg,
at sy-linsz sy-vline.
endif.
if v_error_msg is not initial.
write: /1 sy-vline,3 v_error_msg,
at sy-linsz sy-vline.
endif.
uline.
endif.
endform. " f_display_error
*&---------------------------------------------------------------------*
*& form f_report_header
*&---------------------------------------------------------------------*
* >>>carrier standard report header
*----------------------------------------------------------------------*
form f_report_header .

format color col_heading intensified on.


uline.
* headings
* text-215 - company: carrier
* text-216 - chess demand feed to clt
* text-217 - system: sap
write: /1 sy-vline, 3 text-215, 50 text-216, 100(15) text-217 , at
sy-linsz sy-vline.
* text-218 - program:
* text-219 - date/time:
write: /1 sy-vline, 3 text-218, sy-repid, 100(10) text-219,
sy-datum, c_slash, sy-uzeit , at sy-linsz sy-vline.
* text-220 - user id:
* text-221 - page:
write: /1 sy-vline , 3 text-220, sy-uname, 100 text-221, sy-pagno,
c_slash,v_temp,at sy-linsz sy-vline.
uline.
format color off.

endform. " f_report_header


*&---------------------------------------------------------------------*
*& form f_replace_total_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*

form f_replace_total_page .
* for displaying line number at every top of page .
move sy-pagno to v_page.
do v_page times.
v_count = v_count + 1.
read line c_line of page v_count.
replace v_temp with v_page into sy-lisel.
modify line c_line of page v_count.
enddo.
endform. " f_replace_total_page
*&---------------------------------------------------------------------*
*& form f_clear_internal_tables
*&---------------------------------------------------------------------*
* >>>clear internal tables
*----------------------------------------------------------------------*

form f_clear_internal_tables .

clear : v_temp,
v_chess,
v_error,
v_visual,
v_chess_message,
v_error_message,
v_visual_message,
wa_round,
wa_plaf,
wa_error,
wa_chess,
wa_visual.

refresh : i_plaf,
i_round,
i_chess,
i_visual,
i_error.

endform. " f_clear_internal_tables

You might also like