在SAP的應用開發中,對於報表的效率是非常重視的。當然優化報表效率的方法有很多,當時當我們遇到需要處理大量數據的報表時會發現用盡所有優化的語句,報表的運行效率還是無法達到用戶部門的需求。
下面,我介紹一種方法,也許會對你有所幫助。ABAP的
並行處理(原理類似於java的多線程),它是通過RFC接口進行遠程函數的異步調用實現程序的並處理。
同步/異步調用函數語法:
同步(sRFC):
CALL FUNCTION 'AAA' ;
同步調用的實質:程序進行單線程執行;
異步(aRFC):
CALL
FUNCTION 'AAA'
STARTING
NEWTASK <taskname>
"任務名稱
DESTINATION IN GROUP <RFC Serve Group>
PERFORMING <subroutine>
ON END OF TASK
。
"子程序
異步調用的實質:程序進行多線程執行;
在使用並行處理的過程中通常會遇到如下的可能會發生的
問題:
1、重復運行產生的后台任務相互沖突
2、異步調用獲取的最終結果與同步調用結果存在差異
3、固定RFC Server Group如system
=
'parallel_generators'
,無法保證程序在不同服務器中通用性。
對上述三個問題的
解決方法,如下:
1、為了避免后台任務相互沖突,導致輸出的數據差異,應確保在相同時間段內同一程序只能被一個用戶
占用。所以在開發的程序中添加所對象可以解決。(程序鎖的設置,如附件)
2、分析:在LOOP循環中采用異步調用函數的模式,通過SY-SUBRC = 0來判斷任務啟動成功,
當SY-SUBRC <> 0時,則獲取先前啟動的進程返回的值,但是這樣就遇到一個問題:如第N次循環正好
分配給程序的進程被占用完,這樣本次無法啟動一個任務進程,導致本次的原始數據通過函數無法獲取
目標,從而最終結果出現數據不完整和數值不斷變化的現象。
解決:犧牲部分性能保證數據的完整。通過RZ12獲取服務器的Max. requests in queue 的值,LOOP
循環的時候統計啟動的啟動的進程數是否 = Max. requests inqueue,如果等於則獲取先前啟動的進程
返回的值,然后再重新啟動進程,重復此操作。系統分配給每個程序的最大進程數> Max. requests in
queue,但是把啟動的進程數限制在Max.requests in queue的水平可以保證獲取結果的完整性
3、如何知道系統設置的並行運行進程數呢,通過
RZ12,並雙擊當前登入組,即可以查看到最大並行進程數,
如下圖:

一般系統直接指定<RFC Serve Group> ='
parallel_generators ',如上圖的“服務器組”對應的內容,為了保持一般性通過如下邏輯段獲取:
變量定義-
DATA: g_classname
Type rzlli_apcl, "Server Group Name
g_applserver
Type
rzllitab-applserver."RFC Serve Group
取數邏輯-
CALL 'C_SAPGPARAM' "#EC CI_CCALL
ID 'NAME' FIELD 'rdisp/myname'
ID 'VALUE' FIELD g_applserver.
ID 'NAME' FIELD 'rdisp/myname'
ID 'VALUE' FIELD g_applserver.
SELECT SINGLE classname
FROM rzllitab
INTO g_classname "Server Group Name
WHERE applserver = g_applserver
AND grouptype = 'S'. "S:服務器組,空:登陸組
通過上述的描述,可以通過一個實例來串聯一下,實例如附件。
1、獲取服務組
2、異步調用函數

3、處理子例程

4、如果調用的子例程非系統標准,需要在SE37中定義自己需要的處理邏輯子例程

1 *&---------------------------------------------------------------------* 2 *& Report ZPPR0056 3 *&----------------------------------------------------------------------* 4 *& Program Name : ZPPR0056 5 *& Author's Name : QLB 6 *& Written Date : 2014.01.08 7 *& Request Number: DEVK940516 8 *& Program Description: 共用料區分建議報表--並行處理 9 *&----------------------------------------------------------------------* 10 * Changer Changed Date Request NO. Dec. 11 *&----------------------------------------------------------------------* 12 13 report zppr0056 message-id zppmess. 14 15 tables: mara,marc. 16 17 types: begin of gs_out, 18 werks type marc-werks,"工廠 19 dispo type marc-dispo,"MRP控制員 20 matnr type marc-matnr,"物料 21 maktx type makt-maktx,"物料描述 22 meins type mara-meins,"單位 23 matkl type mara-matkl,"物料組 24 labst type mard-labst,"NO-X非限制庫存 25 zsupl type mard-labst,"NO-X過期供給 26 zreqd type mard-labst,"NO-X過期需求 27 ztype type c, "期間類型 28 ztext type c length 20,"期間類型文本 29 dat01 type mard-labst, "期間-one 30 dat02 type mard-labst, "期間-two 31 dat03 type mard-labst, "期間-three 32 dat04 type mard-labst, "期間-four 33 dat05 type mard-labst, "期間-five 34 dat06 type mard-labst, "期間-six 35 dat07 type mard-labst, "期間-seven 36 dat08 type mard-labst, "期間-eight 37 dat09 type mard-labst, "期間-nine 38 dat10 type mard-labst, "期間-ten 39 dat11 type mard-labst, "期間-evelen 40 dat12 type mard-labst, "期間-twelve 41 end of gs_out, 42 43 begin of gs_dtl, 44 werks type marc-werks,"工廠 45 matnr type marc-matnr,"物料 46 zdate type sy-datum, "需求日期 47 delb0 type t457t-delb0,"MRP元素 48 extra type mdez-extra,"MRP元素數據 49 mng01 type mdez-mng01,"收貨數量或需求數量 50 end of gs_dtl, 51 52 begin of gs_marc, 53 matnr type marc-matnr,"物料ID 54 werks type marc-werks,"工廠 55 dispo type marc-dispo,"MRP控制員 56 matkl type mara-matkl,"物料組 57 meins type mara-meins,"基本單位 58 end of gs_marc, 59 60 begin of gs_mard, 61 matnr type mard-matnr,"物料 62 werks type mard-werks,"工廠 63 lgort type mard-lgort,"庫存地點 64 labst type mard-labst,"庫存數 65 end of gs_mard, 66 67 begin of gs_makt, 68 matnr type makt-matnr,"物料 69 maktx type makt-maktx,"物料描述 70 end of gs_makt, 71 72 begin of gs_task, 73 matnr type marc-matnr,"物料 74 werks type marc-werks,"工廠 75 dispo type marc-dispo,"MRP控制員 76 matkl type mara-matkl,"物料組 77 meins type mara-meins,"單位 78 maktx type makt-maktx,"物料描述 79 labst type mard-labst,"庫存 80 taskname type c length 12, 81 end of gs_task. 82 83 data: gw_task type gs_task, 84 gt_task type standard table of gs_task. 85 86 data: gw_out type gs_out, 87 gw_dtl type gs_dtl, 88 gw_mard type gs_mard. 89 90 data: gt_out type standard table of gs_out, 91 gt_dtl type standard table of gs_dtl,"明細alv 92 gt_marc type standard table of gs_marc, 93 gt_marc_d type standard table of gs_marc, 94 gt_makt type standard table of gs_makt, 95 gt_mard type standard table of gs_mard, 96 gt_mard_s type standard table of gs_mard. 97 data: gt_noxlog type standard table of znoxlog with header line. 98 99 field-symbols:<fs_out> type gs_out, 100 <fs_mard> type gs_mard, 101 <fs_marc> type gs_marc, 102 <fs_makt> type gs_makt, 103 <fs_mrp> type bapi_mrp_items, 104 <fs>, 105 <fs1>, 106 <fs2>, 107 <fs3>. 108 109 data: gt_mrp_items type standard table of bapi_mrp_items."MRP_ITEMs數據 110 111 112 data: g_txt type c length 20, 113 g_num type n length 2. 114 data: g_mondy type d, 115 g_sundy type d. 116 data: g_post type i. 117 data: g_mark type c. 118 119 *****-------GRID_ALV定義 120 class lcl_alv definition deferred. 121 data: g_alv type ref to lcl_alv. 122 data: gt_fcat type lvc_t_fcat, 123 gw_fcat type lvc_s_fcat, 124 is_layo type lvc_s_layo, 125 is_vart type disvariant. 126 data: g_grid1 type ref to cl_gui_alv_grid, 127 g_custom_container1 type ref to cl_gui_custom_container, 128 g_container1 type scrfname value 'GRID_01'. 129 data: ok_code type sy-ucomm, 130 save_ok type sy-ucomm. 131 132 *-----------------------------------------------------------------------* 133 *DESC:異步取數變量定義 134 *-----------------------------------------------------------------------* 135 data: g_taskname(12) type c, "task name(同時運行的任務名稱必須保持唯一) 136 g_classname type rzlli_apcl, "Server Group Name 137 g_applserver type rzllitab-applserver."RFC Serve Group 138 139 data: snd_jobs type i, 140 rcv_jobs type i, 141 functioncall1(1) type c. 142 143 constants: done(1) type c value 'X', 144 pnum type i value 5."進程數 145 146 *----------------------------------------------------------------------* 147 * CLASS LCL_ALV DEFINITION 148 *----------------------------------------------------------------------* 149 * 150 *----------------------------------------------------------------------* 151 class lcl_alv definition. 152 public section. 153 methods: 154 create_object, 155 fcat_setting, 156 layo_setting, 157 disp, 158 main. 159 160 private section. 161 162 endclass. "LCL_ALV DEFINITION 163 164 *----------------------------------------------------------------------* 165 * CLASS LCL_ALV IMPLEMENTATION 166 *----------------------------------------------------------------------* 167 * 168 *----------------------------------------------------------------------* 169 class lcl_alv implementation. 170 method create_object. 171 if g_custom_container1 is initial. 172 "創建容器 173 create object g_custom_container1 174 exporting 175 container_name = g_container1 176 exceptions 177 cntl_error = 1 178 cntl_system_error = 2 179 create_error = 3 180 lifetime_error = 4 181 lifetime_dynpro_dynpro_link = 5 182 others = 6. 183 if sy-subrc ne 0. 184 message 'error1' type 'E'. 185 endif. 186 187 "創建ALV 188 create object g_grid1 189 exporting 190 i_parent = g_custom_container1 191 exceptions 192 error_cntl_create = 1 193 error_cntl_init = 2 194 error_cntl_link = 3 195 error_dp_create = 4 196 others = 5. 197 if sy-subrc ne 0. 198 message 'error2' type 'E'. 199 endif. 200 201 else. 202 g_grid1->refresh_table_display( ). 203 endif. 204 205 endmethod. "CREATE_OBJECT 206 207 method fcat_setting. 208 perform frm_set_fcat. 209 210 endmethod. "FCAT_SETTING 211 212 method layo_setting. 213 is_layo-zebra = 'X'."隔行顏色 214 is_layo-sel_mode = 'A'."可選 215 is_layo-cwidth_opt = 'X'."輸出列顯示最優化 216 217 is_vart-report = sy-repid. 218 endmethod. "LAYO_SETTING 219 220 method disp. 221 "輸出ALV 222 call method g_grid1->set_table_for_first_display 223 exporting 224 i_structure_name = 'GS_DTL' 225 is_variant = is_vart 226 i_save = 'A' 227 i_default = 'X' 228 is_layout = is_layo 229 changing 230 it_outtab = gt_dtl[] 231 it_fieldcatalog = gt_fcat[] 232 "it_sort = gt_sort[] 233 exceptions 234 invalid_parameter_combination = 1 235 program_error = 2 236 too_many_lines = 3 237 others = 4. 238 if sy-subrc ne 0. 239 message 'error3' type 'E'. 240 endif. 241 242 call method cl_gui_control=>set_focus 243 exporting 244 control = g_grid1. 245 endmethod. "DISP 246 247 method main. 248 me->create_object( ). 249 me->fcat_setting( ). 250 me->layo_setting( ). 251 me->disp( ). 252 endmethod. "MAIN 253 254 endclass. "LCL_ALV IMPLEMENTATION 255 256 *---------------------------------------------------------------------* 257 * DESC:ALV 變量定義 258 *---------------------------------------------------------------------* 259 type-pools: slis. 260 data: gt_fieldcat type slis_t_fieldcat_alv with header line, "定義存放輸出字段的內表 261 gt_sort type slis_t_sortinfo_alv, 262 gw_layout type slis_layout_alv, "定義ALV布局設置的工作區 263 g_repid like sy-repid. "定義系統當前程序名變量 264 data: gw_stru_disvar type disvariant, "ALV 顯示格式 265 gt_events type slis_t_event. "ALV 事件 266 267 *-----------------------------------------------------------------------* 268 *DESC:選擇屏幕定義 269 *-----------------------------------------------------------------------* 270 selection-screen begin of block blk with frame title text-001. 271 select-options:s_werks for marc-werks obligatory,"工廠 272 s_matnr for marc-matnr,"物料 273 s_dispo for marc-dispo obligatory,"MRP控制員 274 s_matkl for mara-matkl."物料組 275 select-options:s_lgort for marc-lgpro no-display."NO-X庫存 276 parameters: p_date type mara-ersda default sy-datum obligatory."開始日期 277 selection-screen end of block blk. 278 279 *-----------------------------------------------------------------------* 280 *INITIALIZATION 281 *-----------------------------------------------------------------------* 282 initialization. 283 perform frm_init. 284 285 *-----------------------------------------------------------------------* 286 *START-OF-SELECTION 287 *-----------------------------------------------------------------------* 288 start-of-selection. 289 perform frm_status(zpercent) using 10 '正在讀取其他關聯數據...'. 290 perform frm_get_date."獲取計算期間的起始值 291 perform frm_get_data."獲取基本數據 292 if g_mark eq 'X'. 293 stop. 294 else. 295 perform frm_set_lock."設置鎖 296 perform frm_fill_alv. 297 perform frm_set_unlock."解鎖 298 perform frm_get_event. 299 perform frm_init_layout. 300 perform frm_sort_build changing gt_sort. 301 perform frm_fcat_setting. 302 perform frm_output tables gt_out. 303 endif. 304 305 306 *&---------------------------------------------------------------------* 307 *& Form frm_init 308 *&---------------------------------------------------------------------* 309 * 初始化 310 *----------------------------------------------------------------------* 311 form frm_init. 312 clear: gt_noxlog,g_mondy,g_sundy. 313 314 "初始化NO-X庫存位置 315 select * 316 into table gt_noxlog 317 from znoxlog. 318 319 loop at gt_noxlog. 320 s_lgort-low = gt_noxlog-lgort. 321 s_lgort-option = 'EQ'. 322 s_lgort-sign = 'I'. 323 append s_lgort. 324 endloop. 325 326 endform. "frm_init 327 328 *&---------------------------------------------------------------------* 329 *& Form frm_get_date 330 *&---------------------------------------------------------------------* 331 * 獲取日期 332 *----------------------------------------------------------------------* 333 form frm_get_date. 334 "獲取開始日期所在周及時間 335 call function 'GET_WEEK_INFO_BASED_ON_DATE' 336 exporting 337 date = p_date 338 importing 339 monday = g_mondy 340 sunday = g_sundy. 341 342 g_mondy = p_date. 343 endform. "frm_init 344 345 *&---------------------------------------------------------------------* 346 *& Form FRM_get_data 347 *&---------------------------------------------------------------------* 348 * 獲取基本數據 349 *----------------------------------------------------------------------* 350 form frm_get_data. 351 "根據輸入條件到表MARC及MARA中獲取基本數據 352 select a~matnr "物料 353 a~werks "工廠 354 a~dispo "MRP控制員 355 b~matkl "物料組 356 b~meins "單位 357 into table gt_marc 358 from marc as a join mara as b on a~matnr = b~matnr 359 where a~matnr in s_matnr 360 and a~werks in s_werks 361 and a~dispo in s_dispo 362 and a~lvorm ne 'X' 363 and a~mmsta ne '99' 364 and b~matkl in s_matkl. 365 366 if gt_marc is not initial. 367 368 "獲取物料描述 369 gt_marc_d = gt_marc. 370 sort: gt_marc_d by matnr. 371 delete adjacent duplicates from gt_marc_d comparing matnr. 372 373 select matnr "物料 374 maktx "物料描述 375 into table gt_makt 376 from makt 377 for all entries in gt_marc_d 378 where matnr = gt_marc_d-matnr 379 and spras = '1'. 380 381 "根據內表gt_marc到表MARD中獲取庫存數 382 select matnr "物料 383 werks "工廠 384 lgort "庫存地 385 labst "庫存 386 into table gt_mard 387 from mard 388 for all entries in gt_marc 389 where matnr = gt_marc-matnr 390 and werks = gt_marc-werks 391 and lgort in s_lgort 392 and diskz ne '1'. 393 394 "匯總 395 loop at gt_mard assigning <fs_mard>. 396 clear:gw_mard. 397 move <fs_mard>-matnr to gw_mard-matnr."物料 398 move <fs_mard>-werks to gw_mard-werks."工廠 399 move <fs_mard>-labst to gw_mard-labst."庫存 400 collect gw_mard into gt_mard_s. 401 endloop. 402 else. 403 g_mark = 'X'. 404 message s006(zppmess) display like 'E'. 405 endif. 406 407 endform. "FRM_get_data 408 409 *&---------------------------------------------------------------------* 410 *& Form frm_fill_alv 411 *&---------------------------------------------------------------------* 412 * 填充ALV輸出 413 *----------------------------------------------------------------------* 414 form frm_fill_alv. 415 data: l_lgort type mard-lgort,"庫存點變量 416 l_count type i,"列 417 l_mondy type d,"周一 418 l_sundy type d,"周末 419 l_num(2) type n, 420 l_txt(20) type c. 421 data: l_labst type mard-labst,"庫存 422 l_zsupl type mard-labst,"供給 423 l_zreqd type mard-labst."需求 424 data: lt_mrp_01 type standard table of bapi_mrp_items,"計算-NO-X過期供給 425 lt_mrp_02 type standard table of bapi_mrp_items,"計算-NO-X過期需求 426 lt_mrp_03 type standard table of bapi_mrp_items,"計算-列所對應的NO-X供給 427 lt_mrp_04 type standard table of bapi_mrp_items."計算-列所對應的NO-X需求 428 429 sort: gt_makt by matnr, 430 gt_mard_s by matnr werks, 431 gt_marc by matnr werks. 432 433 "臨時變量 434 data:mess(40) type c, "並發執行出錯信息 435 open_task_num type i. "啟動任務數量 436 data:l_tabix type sy-tabix, "索引變量 437 l_lines type i, "總行數 438 l_counts type p. 439 440 441 "獲取 RFC Serve Group name Start--* 442 "一般系統默認g_classname = 'parallel_generators',但為了通用性按照如下方法獲取 443 call 'C_SAPGPARAM' "#EC CI_CCALL 444 id 'NAME' field 'rdisp/myname' 445 id 'VALUE' field g_applserver. 446 447 select single classname 448 from rzllitab 449 into g_classname "Server Group Name 450 where applserver = g_applserver 451 and grouptype = 'S'. "S:服務器組,空:登陸組 452 "獲取 RFC Serve Group name End--* 453 454 clear: open_task_num,l_lines. 455 456 describe table gt_marc lines l_lines. 457 l_counts = l_lines. 458 459 loop at gt_marc assigning <fs_marc>. 460 clear: gw_out,lt_mrp_01,lt_mrp_02,lt_mrp_03,lt_mrp_04, 461 l_labst,l_zsupl,l_zreqd. 462 l_tabix = sy-tabix. 463 464 perform frm_percent(zpercent) using sy-tabix l_counts ''. 465 move <fs_marc>-matnr to gw_out-matnr."物料 466 move <fs_marc>-werks to gw_out-werks."工廠 467 move <fs_marc>-dispo to gw_out-dispo."MRP控制員 468 move <fs_marc>-matkl to gw_out-matkl."物料組 469 470 "單位轉換 471 call function 'CONVERSION_EXIT_CUNIT_OUTPUT' 472 exporting 473 input = <fs_marc>-meins 474 importing 475 output = gw_out-meins. 476 477 "獲取物料描述 478 read table gt_makt assigning <fs_makt> with key matnr = <fs_marc>-matnr binary search. 479 if sy-subrc eq 0. 480 move <fs_makt>-maktx to gw_out-maktx. 481 endif. 482 483 "獲取NO-X非限制庫存 484 read table gt_mard_s assigning <fs_mard> with key matnr = <fs_marc>-matnr binary search. 485 if sy-subrc eq 0. 486 move <fs_mard>-labst to gw_out-labst. 487 endif. 488 489 *--優化,異步調用獲取數據、 490 "生成任務名稱 = 'Task' + sy-tabix Start--* 491 move l_tabix to g_taskname. 492 condense g_taskname. 493 concatenate 'Task' g_taskname into g_taskname. 494 "生成任務名稱 = 'Task' + sy-tabix End--* 495 496 clear: gw_task. 497 gw_task-matnr = <fs_marc>-matnr. 498 gw_task-werks = <fs_marc>-werks. 499 gw_task-dispo = <fs_marc>-dispo. 500 gw_task-matkl = <fs_marc>-matkl. 501 gw_task-meins = gw_out-meins. 502 gw_task-maktx = gw_out-maktx. 503 gw_task-labst = gw_out-labst. 504 gw_task-taskname = g_taskname. 505 append gw_task to gt_task. 506 507 * 異步調用函數 Start--* 508 call function 'BAPI_MATERIAL_STOCK_REQ_LIST' starting new task g_taskname 509 destination in group g_classname 510 performing frm_subroutine_done on end of task "子程序 511 * 只要將函數的EXPORTING參數放在此處,其他參數放到子程序中 512 exporting 513 material = <fs_marc>-matnr 514 plant = <fs_marc>-werks 515 get_item_details = 'X' 516 get_ind_lines = 'X' 517 * 系統標准報錯信息 518 exceptions 519 communication_failure = 1 message mess 520 system_failure = 2 message mess 521 resource_failure = 3. 522 523 if sy-subrc = 0. 524 snd_jobs = snd_jobs + 1. 525 endif. 526 * 異步調用函數 End--* 527 528 open_task_num = open_task_num + 1. "記錄啟動的進程數量 529 530 if open_task_num = pnum. 531 * 獲取並發進程返回的結果 532 wait until rcv_jobs >= snd_jobs. 533 534 clear:open_task_num,rcv_jobs,snd_jobs. 535 free:gt_task. 536 else. 537 if l_tabix = l_lines. 538 * 獲取並發進程返回的結果 539 wait until rcv_jobs >= snd_jobs. 540 541 clear:open_task_num,rcv_jobs,snd_jobs. 542 free:gt_task. 543 endif. 544 endif. 545 546 endloop. 547 548 endform. "frm_fill_alv 549 550 *&---------------------------------------------------------------------* 551 *& Form frm_subroutine_done 552 *&---------------------------------------------------------------------* 553 * 異步處理子例程 554 *----------------------------------------------------------------------* 555 * -->G_TASKNAME text 556 *----------------------------------------------------------------------* 557 form frm_subroutine_done using g_taskname. 558 data: l_lgort type mard-lgort,"庫存點變量 559 l_auffx type plaf-auffx,"固定標識 560 l_count type i,"列 561 l_mondy type d,"周一 562 l_sundy type d,"周末 563 l_num(2) type n, 564 l_txt(20) type c. 565 data: l_labst type mard-labst,"庫存 566 l_zsupl type mard-labst,"供給 567 l_zreqd type mard-labst."需求 568 data: l_mark1 type c, 569 l_mark2 type c, 570 l_mark3 type c, 571 l_mark4 type c, 572 l_lines type i. 573 data: lt_mrp_01 type standard table of bapi_mrp_items,"計算-NO-X過期供給 574 lt_mrp_02 type standard table of bapi_mrp_items,"計算-NO-X過期需求 575 lt_mrp_03 type standard table of bapi_mrp_items,"計算-列所對應的NO-X供給 576 lt_mrp_04 type standard table of bapi_mrp_items."計算-列所對應的NO-X需求 577 578 rcv_jobs = rcv_jobs + 1. ""Receiving data 579 580 receive results from function 'BAPI_MATERIAL_STOCK_REQ_LIST' 581 tables 582 mrp_items = gt_mrp_items. 583 584 read table gt_task into gw_task with key taskname = g_taskname binary search. 585 if sy-subrc eq 0. 586 clear:l_mark1,l_mark2,l_mark3,l_mark4. 587 588 gw_out-matnr = gw_task-matnr."物料 589 gw_out-werks = gw_task-werks."工廠 590 gw_out-dispo = gw_task-dispo. 591 gw_out-meins = gw_task-meins. 592 gw_out-maktx = gw_task-maktx. 593 gw_out-labst = gw_task-labst. 594 595 delete gt_mrp_items where ( mrp_element_ind = 'WB' and rec_reqd_qty is initial ). 596 597 if gt_mrp_items is initial and gw_out-labst is initial. "如果MD04為空 598 "CONTINUE. 599 else. 600 601 lt_mrp_01 = lt_mrp_02 = gt_mrp_items. 602 603 "計算-NO-X過期供給 604 delete lt_mrp_01 where ( avail_date is initial ) or ( avail_date ge p_date ). 605 delete lt_mrp_01 where plngsegno is not initial or plus_minus ne '+' or rec_reqd_qty is initial. 606 607 loop at lt_mrp_01 assigning <fs_mrp>. 608 if <fs_mrp>-storage_loc in s_lgort. 609 if <fs_mrp>-mrp_element_ind = 'PA' and <fs_mrp>-firmed ne 'X'. 610 continue. 611 else. 612 gw_out-zsupl = gw_out-zsupl + <fs_mrp>-rec_reqd_qty. 613 endif. 614 endif. 615 endloop. 616 617 "計算-NO-X過期需求 618 delete lt_mrp_02 where ( avail_date is initial ) or ( avail_date ge p_date ). 619 delete lt_mrp_02 where plngsegno is not initial or plus_minus ne '-' or rec_reqd_qty is initial. 620 621 loop at lt_mrp_02 assigning <fs_mrp>. 622 clear:l_lgort,l_auffx. 623 624 case <fs_mrp>-order_type. 625 when 'BB'."外協采購 626 "到表EKPO中獲取滿足NO-X庫存的 627 select single lgort 628 into l_lgort 629 from ekpo 630 where ebeln = <fs_mrp>-source_no 631 and ebelp = <fs_mrp>-source_item 632 and lgort in s_lgort. 633 634 if sy-subrc eq 0. 635 gw_out-zreqd = gw_out-zreqd + <fs_mrp>-rec_reqd_qty. 636 endif. 637 638 when 'SB'."計划訂單-下階 639 "到表PLAF中獲取滿足NO-X庫存的 640 select single lgort auffx 641 into (l_lgort,l_auffx) 642 from plaf 643 where plnum = <fs_mrp>-source_no 644 and lgort in s_lgort. 645 646 if sy-subrc eq 0. 647 if l_auffx eq 'X'."固定 648 gw_out-zreqd = gw_out-zreqd + <fs_mrp>-rec_reqd_qty. 649 endif. 650 endif. 651 652 when 'AR'."生產訂單預留 653 "到表AFPO中獲取滿足NO-X庫存的 654 select single lgort 655 into l_lgort 656 from afpo 657 where aufnr = <fs_mrp>-mrp_no12 658 and lgort in s_lgort. 659 660 if sy-subrc eq 0. 661 gw_out-zreqd = gw_out-zreqd + <fs_mrp>-rec_reqd_qty. 662 endif. 663 664 when others. 665 endcase. 666 endloop. 667 668 "標識one 669 if gw_out-labst is initial and gw_out-zsupl is initial and gw_out-zreqd is initial. 670 l_mark1 = 'X'. 671 endif. 672 673 "計算-列 674 clear:l_count. 675 do 3 times. 676 l_count = l_count + 1. 677 case l_count. 678 when 1."計算-列所對應的NO-X供給 679 clear:g_num. 680 l_mondy = g_mondy. 681 l_sundy = g_sundy. 682 gw_out-ztype = 'A'. 683 gw_out-ztext = 'NO-X供給'. 684 do 12 times. 685 clear:g_txt. 686 g_num = g_num + 1. 687 g_txt = |GW_OUT-DAT{ g_num }|. 688 assign (g_txt) to <fs>. 689 690 lt_mrp_03 = gt_mrp_items. 691 delete lt_mrp_03 where ( avail_date gt l_sundy ) or ( avail_date lt l_mondy ). 692 delete lt_mrp_03 where plngsegno is not initial or plus_minus ne '+' or rec_reqd_qty is initial. 693 694 loop at lt_mrp_03 assigning <fs_mrp>. 695 if <fs_mrp>-storage_loc in s_lgort. 696 if <fs_mrp>-mrp_element_ind = 'PA' and <fs_mrp>-firmed ne 'X'. 697 continue. 698 else. 699 <fs> = <fs> + <fs_mrp>-rec_reqd_qty. 700 endif. 701 endif. 702 endloop. 703 l_mondy = l_sundy + 1. 704 l_sundy = l_sundy + 7. 705 706 "標識two,只要其中一列不為空 707 if <fs> is not initial. 708 l_mark2 = 'X'. 709 endif. 710 enddo. 711 append gw_out to gt_out. 712 clear:gw_out-labst,gw_out-zsupl,gw_out-zreqd. 713 714 when 2."計算-列所對應的NO-X需求 715 clear:g_num,gw_out-dat01,gw_out-dat02,gw_out-dat03,gw_out-dat04,gw_out-dat05,gw_out-dat06, 716 gw_out-dat07,gw_out-dat08,gw_out-dat09,gw_out-dat10,gw_out-dat11,gw_out-dat12. 717 l_mondy = g_mondy. 718 l_sundy = g_sundy. 719 gw_out-ztype = 'B'. 720 gw_out-ztext = 'NO-X需求'. 721 do 12 times. 722 clear:g_txt. 723 g_num = g_num + 1. 724 g_txt = |GW_OUT-DAT{ g_num }|. 725 assign (g_txt) to <fs>. 726 727 lt_mrp_04 = gt_mrp_items. 728 delete lt_mrp_04 where ( avail_date gt l_sundy ) or ( avail_date lt l_mondy ). 729 delete lt_mrp_04 where plngsegno is not initial or plus_minus ne '-' or rec_reqd_qty is initial. 730 731 loop at lt_mrp_04 assigning <fs_mrp>. 732 clear:l_lgort,l_auffx. 733 734 case <fs_mrp>-order_type. 735 when 'BB'."外協采購 736 "到表EKPO中獲取滿足NO-X庫存的 737 select single lgort 738 into l_lgort 739 from ekpo 740 where ebeln = <fs_mrp>-source_no 741 and ebelp = <fs_mrp>-source_item 742 and lgort in s_lgort. 743 744 if sy-subrc eq 0. 745 <fs> = <fs> + <fs_mrp>-rec_reqd_qty. 746 endif. 747 748 when 'SB'."計划訂單-下階 749 "到表PLAF中獲取滿足NO-X庫存的 750 select single lgort auffx 751 into (l_lgort,l_auffx) 752 from plaf 753 where plnum = <fs_mrp>-source_no 754 and lgort in s_lgort. 755 756 if sy-subrc eq 0. 757 if l_auffx = 'X'. 758 <fs> = <fs> + <fs_mrp>-rec_reqd_qty. 759 endif. 760 endif. 761 762 when 'AR'."生產訂單預留 763 "到表AFPO中獲取滿足NO-X庫存的 764 select single lgort 765 into l_lgort 766 from afpo 767 where aufnr = <fs_mrp>-mrp_no12 768 and lgort in s_lgort. 769 770 if sy-subrc eq 0. 771 <fs> = <fs> + <fs_mrp>-rec_reqd_qty. 772 endif. 773 774 when others. 775 endcase. 776 endloop. 777 l_mondy = l_sundy + 1. 778 l_sundy = l_sundy + 7. 779 780 "標識three,只要其中一列不為空 781 if <fs> is not initial. 782 l_mark3 = 'X'. 783 endif. 784 enddo. 785 append gw_out to gt_out. 786 787 when 3."計算-NO-X建議供給數量 788 clear:g_num,gw_out-dat01,gw_out-dat02,gw_out-dat03,gw_out-dat04,gw_out-dat05,gw_out-dat06, 789 gw_out-dat07,gw_out-dat08,gw_out-dat09,gw_out-dat10,gw_out-dat11,gw_out-dat12. 790 gw_out-ztype = 'C'. 791 gw_out-ztext = 'NO-X建議供給數量'. 792 793 do 12 times. 794 clear:g_txt,l_num,l_txt. 795 g_num = g_num + 1. 796 g_txt = |GW_OUT-DAT{ g_num }|. 797 assign (g_txt) to <fs>. 798 799 if g_num = 01."第一列 800 l_num = g_num. 801 "NO-X供給 802 read table gt_out assigning <fs_out> with key matnr = gw_out-matnr 803 werks = gw_out-werks 804 ztype = 'A'. 805 if sy-subrc eq 0. 806 l_txt = |<FS_OUT>-DAT{ l_num }|. 807 assign (l_txt) to <fs1>. 808 l_labst = <fs_out>-labst."庫存 809 l_zsupl = <fs_out>-zsupl."供給 810 l_zreqd = <fs_out>-zreqd."需求 811 endif. 812 "NO-X需求 813 read table gt_out assigning <fs_out> with key matnr = gw_out-matnr 814 werks = gw_out-werks 815 ztype = 'B'. 816 if sy-subrc eq 0. 817 l_txt = |<FS_OUT>-DAT{ l_num }|. 818 assign (l_txt) to <fs2>. 819 endif. 820 821 "第一列建議供給數量 = 非限制庫存 + 過期供給 - 過期需求 + 第一列供給 - 第一列的需求 822 <fs> = l_labst + l_zsupl - l_zreqd + <fs1> - <fs2>. 823 else. 824 l_num = g_num - 1. 825 "NO-X供給 826 read table gt_out assigning <fs_out> with key matnr = gw_out-matnr 827 werks = gw_out-werks 828 ztype = 'A'. 829 if sy-subrc eq 0. 830 l_txt = |<FS_OUT>-DAT{ g_num }|. 831 assign (l_txt) to <fs1>. 832 endif. 833 "NO-X需求 834 read table gt_out assigning <fs_out> with key matnr = gw_out-matnr 835 werks = gw_out-werks 836 ztype = 'B'. 837 if sy-subrc eq 0. 838 l_txt = |<FS_OUT>-DAT{ g_num }|. 839 assign (l_txt) to <fs2>. 840 endif. 841 842 "前一列的NO-X建議供給數量 843 l_txt = |GW_OUT-DAT{ l_num }|. 844 assign (l_txt) to <fs3>. 845 846 "當前NO-X建議供給數量 = 前一列建議供給數 + 當前列供給 - 當前列需求 847 if <fs3> ge 0. 848 <fs> = abs( <fs3> ) + <fs1> - <fs2>. 849 else. 850 <fs> = <fs1> - <fs2>. 851 endif. 852 853 "判斷建議數是否大於0,若大於0,清空;否則,取絕對值 854 if g_num = 12."最后一次循環,要判斷最后兩列 855 if <fs> gt 0. 856 clear <fs>. 857 else. 858 <fs> = abs( <fs> ). 859 endif. 860 861 if <fs3> gt 0. 862 clear <fs3>. 863 else. 864 <fs3> = abs( <fs3> ). 865 endif. 866 else."否則只要判斷前一列 867 if <fs3> ge 0. 868 clear <fs3>. 869 else. 870 <fs3> = abs( <fs3> ). 871 endif. 872 endif. 873 874 endif. 875 876 "標識four,只要其中一列不為空 877 if <fs> is not initial. 878 l_mark4 = 'X'. 879 endif. 880 enddo. 881 append gw_out to gt_out. 882 endcase. 883 enddo. 884 885 "刪除為空記錄 886 if l_mark1 = 'X' and l_mark2 is initial and l_mark3 is initial and l_mark4 is initial. 887 describe table gt_out lines l_lines. 888 delete gt_out index l_lines. 889 delete gt_out index l_lines - 1. 890 delete gt_out index l_lines - 2. 891 endif. 892 endif. 893 endif. 894 895 endform. "frm_subroutine_done 896 897 898 899 *&---------------------------------------------------------------------* 900 *& Form frm_get_detail 901 *&---------------------------------------------------------------------* 902 * text 903 *----------------------------------------------------------------------* 904 * -->ALV text 905 * -->MATNR text 906 * -->WERKS text 907 * -->FNAME text 908 *----------------------------------------------------------------------* 909 form frm_get_detail tables alv structure gw_dtl 910 using matnr type marc-matnr 911 werks type marc-werks 912 fname type slis_fieldname 913 type type c. 914 915 data: lt_mrp type standard table of bapi_mrp_items."計算-NO-X過期供給 916 data: l_lgort type mard-lgort, 917 l_auffx type plaf-auffx, 918 l_mondy type d, 919 l_sundy type d, 920 l_times type i. 921 922 "獲取MD04 923 call function 'BAPI_MATERIAL_STOCK_REQ_LIST' 924 exporting 925 material = matnr 926 plant = werks 927 get_item_details = 'X' 928 get_ind_lines = 'X' 929 tables 930 mrp_items = lt_mrp. 931 932 case fname+0(2). 933 when 'ZS'."供給 934 delete lt_mrp where ( avail_date is initial ) or ( avail_date ge p_date ). 935 delete lt_mrp where plngsegno is not initial or plus_minus ne '+' or rec_reqd_qty is initial. 936 937 loop at lt_mrp assigning <fs_mrp>. 938 clear:gw_dtl. 939 if <fs_mrp>-storage_loc in s_lgort. 940 if <fs_mrp>-mrp_element_ind = 'PA' and <fs_mrp>-firmed ne 'X'. 941 continue. 942 else. 943 gw_dtl-werks = werks. 944 gw_dtl-matnr = matnr. 945 gw_dtl-zdate = <fs_mrp>-avail_date."日期 946 "MRP元素 947 select single delb0 948 into gw_dtl-delb0 949 from t457t 950 where delkz = <fs_mrp>-mrp_element_ind 951 and spras = '1'. 952 953 "MRP元素數據 954 if <fs_mrp>-peggedrqmt is initial. 955 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 956 else. 957 gw_dtl-extra = <fs_mrp>-peggedrqmt. 958 endif. 959 960 "數量 961 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 962 append gw_dtl to alv. 963 endif. 964 endif. 965 endloop. 966 967 when 'ZR'."需求 968 delete lt_mrp where ( avail_date is initial ) or ( avail_date ge p_date ). 969 delete lt_mrp where plngsegno is not initial or plus_minus ne '-' or rec_reqd_qty is initial. 970 971 loop at lt_mrp assigning <fs_mrp>. 972 clear:gw_dtl,l_lgort,l_auffx. 973 case <fs_mrp>-order_type. 974 when 'BB'."外協采購 975 "到表EKPO中獲取滿足NO-X庫存的 976 select single lgort 977 into l_lgort 978 from ekpo 979 where ebeln = <fs_mrp>-source_no 980 and ebelp = <fs_mrp>-source_item 981 and lgort in s_lgort. 982 983 if sy-subrc eq 0. 984 gw_dtl-werks = werks. 985 gw_dtl-matnr = matnr. 986 gw_dtl-zdate = <fs_mrp>-avail_date."日期 987 "MRP元素 988 select single delb0 989 into gw_dtl-delb0 990 from t457t 991 where delkz = <fs_mrp>-mrp_element_ind 992 and spras = '1'. 993 994 "MRP元素數據 995 if <fs_mrp>-peggedrqmt is initial. 996 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 997 else. 998 gw_dtl-extra = <fs_mrp>-peggedrqmt. 999 endif. 1000 1001 "數量 1002 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 1003 append gw_dtl to alv. 1004 endif. 1005 1006 when 'SB'."計划訂單-下階 1007 "到表PLAF中獲取滿足NO-X庫存的 1008 select single lgort auffx 1009 into (l_lgort,l_auffx) 1010 from plaf 1011 where plnum = <fs_mrp>-source_no 1012 and lgort in s_lgort. 1013 1014 if sy-subrc eq 0. 1015 if l_auffx eq 'X'. 1016 gw_dtl-werks = werks. 1017 gw_dtl-matnr = matnr. 1018 gw_dtl-zdate = <fs_mrp>-avail_date."日期 1019 "MRP元素 1020 select single delb0 1021 into gw_dtl-delb0 1022 from t457t 1023 where delkz = <fs_mrp>-mrp_element_ind 1024 and spras = '1'. 1025 1026 "MRP元素數據 1027 if <fs_mrp>-peggedrqmt is initial. 1028 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 1029 else. 1030 gw_dtl-extra = <fs_mrp>-peggedrqmt. 1031 endif. 1032 1033 "數量 1034 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 1035 append gw_dtl to alv. 1036 endif. 1037 endif. 1038 1039 when 'AR'."生產訂單預留 1040 "到表AFPO中獲取滿足NO-X庫存的 1041 select single lgort 1042 into l_lgort 1043 from afpo 1044 where aufnr = <fs_mrp>-mrp_no12 1045 and lgort in s_lgort. 1046 1047 if sy-subrc eq 0. 1048 gw_dtl-werks = werks. 1049 gw_dtl-matnr = matnr. 1050 gw_dtl-zdate = <fs_mrp>-avail_date."日期 1051 "MRP元素 1052 select single delb0 1053 into gw_dtl-delb0 1054 from t457t 1055 where delkz = <fs_mrp>-mrp_element_ind 1056 and spras = '1'. 1057 1058 "MRP元素數據 1059 if <fs_mrp>-peggedrqmt is initial. 1060 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 1061 else. 1062 gw_dtl-extra = <fs_mrp>-peggedrqmt. 1063 endif. 1064 1065 "數量 1066 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 1067 append gw_dtl to alv. 1068 endif. 1069 1070 when others. 1071 endcase. 1072 endloop. 1073 1074 when 'DA'."期間 1075 l_sundy = g_sundy. 1076 l_times = fname+3(2) - 1. 1077 do l_times times. 1078 l_mondy = l_sundy + 1. 1079 l_sundy = l_sundy + 7. 1080 enddo. 1081 1082 if type = 'A'."供給 1083 1084 delete lt_mrp where ( avail_date gt l_sundy ) or ( avail_date lt l_mondy ). 1085 delete lt_mrp where plngsegno is not initial or plus_minus ne '+' or rec_reqd_qty is initial. 1086 1087 loop at lt_mrp assigning <fs_mrp>. 1088 clear:gw_dtl. 1089 if <fs_mrp>-storage_loc in s_lgort. 1090 if <fs_mrp>-mrp_element_ind = 'PA' and <fs_mrp>-firmed ne 'X'. 1091 continue. 1092 else. 1093 gw_dtl-werks = werks. 1094 gw_dtl-matnr = matnr. 1095 gw_dtl-zdate = <fs_mrp>-avail_date."日期 1096 "MRP元素 1097 select single delb0 1098 into gw_dtl-delb0 1099 from t457t 1100 where delkz = <fs_mrp>-mrp_element_ind 1101 and spras = '1'. 1102 1103 "MRP元素數據 1104 if <fs_mrp>-peggedrqmt is initial. 1105 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 1106 else. 1107 gw_dtl-extra = <fs_mrp>-peggedrqmt. 1108 endif. 1109 1110 "數量 1111 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 1112 append gw_dtl to alv. 1113 endif. 1114 endif. 1115 endloop. 1116 elseif type = 'B'."需求 1117 delete lt_mrp where ( avail_date gt l_sundy ) or ( avail_date lt l_mondy ). 1118 delete lt_mrp where plngsegno is not initial or plus_minus ne '-' or rec_reqd_qty is initial. 1119 1120 loop at lt_mrp assigning <fs_mrp>. 1121 clear:gw_dtl,l_lgort,l_auffx. 1122 case <fs_mrp>-order_type. 1123 when 'BB'."外協采購 1124 "到表EKPO中獲取滿足NO-X庫存的 1125 select single lgort 1126 into l_lgort 1127 from ekpo 1128 where ebeln = <fs_mrp>-source_no 1129 and ebelp = <fs_mrp>-source_item 1130 and lgort in s_lgort. 1131 1132 if sy-subrc eq 0. 1133 gw_dtl-werks = werks. 1134 gw_dtl-matnr = matnr. 1135 gw_dtl-zdate = <fs_mrp>-avail_date."日期 1136 "MRP元素 1137 select single delb0 1138 into gw_dtl-delb0 1139 from t457t 1140 where delkz = <fs_mrp>-mrp_element_ind 1141 and spras = '1'. 1142 1143 "MRP元素數據 1144 if <fs_mrp>-peggedrqmt is initial. 1145 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 1146 else. 1147 gw_dtl-extra = <fs_mrp>-peggedrqmt. 1148 endif. 1149 1150 "數量 1151 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 1152 append gw_dtl to alv. 1153 endif. 1154 1155 when 'SB'."計划訂單-下階 1156 "到表PLAF中獲取滿足NO-X庫存的 1157 select single lgort auffx 1158 into (l_lgort,l_auffx) 1159 from plaf 1160 where plnum = <fs_mrp>-source_no 1161 and lgort in s_lgort. 1162 1163 if sy-subrc eq 0. 1164 if l_auffx eq 'X'. 1165 gw_dtl-werks = werks. 1166 gw_dtl-matnr = matnr. 1167 gw_dtl-zdate = <fs_mrp>-avail_date."日期 1168 "MRP元素 1169 select single delb0 1170 into gw_dtl-delb0 1171 from t457t 1172 where delkz = <fs_mrp>-mrp_element_ind 1173 and spras = '1'. 1174 1175 "MRP元素數據 1176 if <fs_mrp>-peggedrqmt is initial. 1177 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 1178 else. 1179 gw_dtl-extra = <fs_mrp>-peggedrqmt. 1180 endif. 1181 1182 "數量 1183 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 1184 append gw_dtl to alv. 1185 endif. 1186 endif. 1187 1188 when 'AR'."生產訂單預留 1189 "到表AFPO中獲取滿足NO-X庫存的 1190 select single lgort 1191 into l_lgort 1192 from afpo 1193 where aufnr = <fs_mrp>-mrp_no12 1194 and lgort in s_lgort. 1195 1196 if sy-subrc eq 0. 1197 gw_dtl-werks = werks. 1198 gw_dtl-matnr = matnr. 1199 gw_dtl-zdate = <fs_mrp>-avail_date."日期 1200 "MRP元素 1201 select single delb0 1202 into gw_dtl-delb0 1203 from t457t 1204 where delkz = <fs_mrp>-mrp_element_ind 1205 and spras = '1'. 1206 1207 "MRP元素數據 1208 if <fs_mrp>-peggedrqmt is initial. 1209 gw_dtl-extra = |{ <fs_mrp>-mrp_no }/{ <fs_mrp>-mrp_pos }|. 1210 else. 1211 gw_dtl-extra = <fs_mrp>-peggedrqmt. 1212 endif. 1213 1214 "數量 1215 gw_dtl-mng01 = <fs_mrp>-rec_reqd_qty. 1216 append gw_dtl to alv. 1217 endif. 1218 1219 when others. 1220 endcase. 1221 endloop. 1222 endif. 1223 1224 when others. 1225 1226 endcase. 1227 1228 endform. "frm_get_detail 1229 1230 *&---------------------------------------------------------------------* 1231 *& Form frm_get_text 1232 *&---------------------------------------------------------------------* 1233 * 獲取提示文本 1234 *----------------------------------------------------------------------* 1235 * -->PERCENT text 1236 * -->TEXT text 1237 *----------------------------------------------------------------------* 1238 form frm_get_text using percent type i 1239 text type string. 1240 1241 call function 'SAPGUI_PROGRESS_INDICATOR' 1242 exporting 1243 percentage = percent 1244 text = text. 1245 1246 endform. "frm_get_text 1247 1248 *&---------------------------------------------------------------------* 1249 *& Form frm_get_event 1250 *&---------------------------------------------------------------------* 1251 * text 1252 *----------------------------------------------------------------------* 1253 form frm_get_event . 1254 data l_events type slis_alv_event."slis_alv_event是一個包含name和form字段的結構,均為字符型 1255 1256 call function 'REUSE_ALV_EVENTS_GET' 1257 exporting 1258 i_list_type = 0 1259 importing 1260 et_events = gt_events 1261 exceptions 1262 list_type_wrong = 1 1263 others = 2. 1264 if sy-subrc <> 0. 1265 message id sy-msgid type sy-msgty number sy-msgno 1266 with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 1267 endif. 1268 1269 endform. "frm_get_event 1270 1271 *&---------------------------------------------------------------------* 1272 *& Form frm_sort_build 1273 *&---------------------------------------------------------------------* 1274 * 輸出排序設置 1275 *----------------------------------------------------------------------* 1276 * -->LT_SORT text 1277 *----------------------------------------------------------------------* 1278 form frm_sort_build changing lt_sort type slis_t_sortinfo_alv. 1279 data: ls_sort type slis_sortinfo_alv. 1280 refresh lt_sort. 1281 1282 clear ls_sort. 1283 ls_sort-spos = 1. 1284 ls_sort-fieldname = 'WERKS'. 1285 ls_sort-up = 'X'. 1286 append ls_sort to lt_sort. 1287 1288 clear ls_sort. 1289 ls_sort-spos = 2. 1290 ls_sort-fieldname = 'DISPO'. 1291 ls_sort-up = 'X'. 1292 append ls_sort to lt_sort. 1293 1294 clear ls_sort. 1295 ls_sort-spos = 3. 1296 ls_sort-fieldname = 'MATNR'. 1297 ls_sort-up = 'X'. 1298 append ls_sort to lt_sort. 1299 1300 clear ls_sort. 1301 ls_sort-spos = 4. 1302 ls_sort-fieldname = 'MEINS'. 1303 ls_sort-up = 'X'. 1304 append ls_sort to lt_sort. 1305 1306 clear ls_sort. 1307 ls_sort-spos = 5. 1308 ls_sort-fieldname = 'MAKTX'. 1309 ls_sort-up = 'X'. 1310 append ls_sort to lt_sort. 1311 1312 endform. "frm_sort_build 1313 1314 *&---------------------------------------------------------------------* 1315 *& Form frm_init_layout 1316 *&---------------------------------------------------------------------* 1317 * 布局設置 1318 *----------------------------------------------------------------------* 1319 form frm_init_layout. 1320 gw_layout-zebra = 'X'."間隔行顏色變換 1321 gw_layout-detail_popup = 'X'. 1322 gw_layout-colwidth_optimize = 'X'."自動調整列寬 1323 1324 endform. " FRM_INIT_LAYOUT 1325 1326 *&---------------------------------------------------------------------* 1327 *& Form frm_fcat_setting 1328 *&---------------------------------------------------------------------* 1329 * 設置輸出字段 1330 *----------------------------------------------------------------------* 1331 form frm_fcat_setting. 1332 data: l_num type n length 2, 1333 l_txt type c length 20,"字段 1334 l_dis type c length 20,"描述 1335 l_mondy type d, 1336 l_sundy type d. 1337 refresh gt_fieldcat. 1338 1339 perform frm_init_fieldcat using 'WERKS' '工廠' 'X' 'X' ' ' ' ' ' '. 1340 perform frm_init_fieldcat using 'DISPO' 'MRP控制員' 'X' 'X' ' ' ' ' ' '. 1341 perform frm_init_fieldcat using 'MATNR' '物料號' 'X' 'X' ' ' 'X' ' '. 1342 perform frm_init_fieldcat using 'MEINS' '單位' 'X' 'X' ' ' ' ' ' '. 1343 perform frm_init_fieldcat using 'MAKTX' '物料描述' 'X' 'X' ' ' ' ' ' '. 1344 perform frm_init_fieldcat using 'LABST' 'NO-X非限制庫存' 'X' 'X' ' ' 'X' ' '. 1345 perform frm_init_fieldcat using 'ZSUPL' 'NO-X過期供給' 'X' 'X' 'X' 'X' ' '. 1346 perform frm_init_fieldcat using 'ZREQD' 'NO-X過期需求' 'X' 'X' 'X' 'X' ' '. 1347 perform frm_init_fieldcat using 'ZTEXT' '期間' 'X' 'X' ' ' ' ' ' '. 1348 "動態輸出期間字段 1349 clear:l_num,l_txt,l_dis. 1350 l_mondy = g_mondy. 1351 l_sundy = g_sundy. 1352 do 12 times. 1353 l_num = l_num + 1. 1354 l_txt = |DAT{ l_num }|. 1355 l_dis = |{ l_mondy }-{ l_sundy }|. 1356 perform frm_init_fieldcat using l_txt l_dis ' ' ' ' 'X' 'X' ' '. 1357 l_mondy = l_sundy + 1. 1358 l_sundy = l_sundy + 7. 1359 enddo. 1360 1361 endform. "frm_fcat_setting 1362 1363 *&---------------------------------------------------------------------* 1364 *& Form frm_init_fieldcat 1365 *&---------------------------------------------------------------------* 1366 * 初始化輸出字段 1367 *----------------------------------------------------------------------* 1368 * -->VALU01 text 1369 * -->VALU02 text 1370 * -->VALU03 text 1371 * -->VALU04 text 1372 * -->VALU05 text 1373 * -->VALU06 text 1374 * -->VALU07 text 1375 *----------------------------------------------------------------------* 1376 form frm_init_fieldcat using valu01 1377 valu02 1378 valu03 1379 valu04 1380 valu05 1381 valu06 1382 valu07. 1383 1384 clear:gt_fieldcat. 1385 gt_fieldcat-fieldname = valu01."字段名 1386 gt_fieldcat-seltext_m = valu02."字段描述 1387 gt_fieldcat-key = valu03."主鍵 1388 gt_fieldcat-fix_column = valu04."固定列 1389 gt_fieldcat-hotspot = valu05."熱點 1390 gt_fieldcat-no_zero = valu06."隱藏前導零 1391 gt_fieldcat-no_sign = valu07."符號 1392 append gt_fieldcat. 1393 1394 endform. "frm_set_fcat 1395 1396 1397 *&---------------------------------------------------------------------* 1398 *& Form frm_output 1399 *&---------------------------------------------------------------------* 1400 * text 1401 *----------------------------------------------------------------------* 1402 * -->TAB text 1403 *----------------------------------------------------------------------* 1404 form frm_output tables tab. 1405 *ALV輸出 1406 g_repid = sy-repid. 1407 1408 call function 'REUSE_ALV_GRID_DISPLAY' 1409 exporting 1410 i_callback_program = g_repid 1411 is_layout = gw_layout 1412 it_fieldcat = gt_fieldcat[] 1413 i_save = 'X' 1414 is_variant = gw_stru_disvar"變量信息 1415 it_events = gt_events[] 1416 it_sort = gt_sort[] 1417 i_callback_user_command = 'FRM_USER_COMMAND' 1418 tables 1419 t_outtab = tab 1420 exceptions 1421 program_error = 1 1422 others = 2. 1423 if sy-subrc <> 0. 1424 message id sy-msgid type sy-msgty number sy-msgno 1425 with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. 1426 endif. 1427 endform. "frm_output 1428 1429 *&---------------------------------------------------------------------* 1430 *& Form FRM_USER_COMMAND 1431 *&---------------------------------------------------------------------* 1432 * 事件處理 1433 *----------------------------------------------------------------------* 1434 * -->R_UCOMM text 1435 * -->RS_SELFIELD text 1436 *----------------------------------------------------------------------* 1437 form frm_user_command using r_ucomm like sy-ucomm 1438 rs_selfield type slis_selfield. 1439 1440 clear:gt_dtl. 1441 case r_ucomm. 1442 when '&IC1'. 1443 read table gt_out assigning <fs_out> index rs_selfield-tabindex. 1444 if rs_selfield-fieldname = 'ZSUPL' or rs_selfield-fieldname = 'ZREQD'."供給/需求 1445 perform frm_get_detail tables gt_dtl 1446 using <fs_out>-matnr 1447 <fs_out>-werks 1448 rs_selfield-fieldname 1449 <fs_out>-ztype. 1450 1451 if gt_dtl is not initial. 1452 call screen 0100 starting at 30 5. 1453 endif. 1454 else. 1455 if rs_selfield-fieldname+0(3) = 'DAT'. 1456 perform frm_get_detail tables gt_dtl 1457 using <fs_out>-matnr 1458 <fs_out>-werks 1459 rs_selfield-fieldname 1460 <fs_out>-ztype. 1461 1462 if gt_dtl is not initial. 1463 call screen 0100 starting at 30 5. 1464 endif. 1465 endif. 1466 endif. 1467 1468 when others. 1469 endcase. 1470 endform. "FRM_USER_COMMAND 1471 1472 *&---------------------------------------------------------------------* 1473 *& Form frm_set_fcat 1474 *&---------------------------------------------------------------------* 1475 * 輸出字段設置 1476 *----------------------------------------------------------------------* 1477 form frm_set_fcat. 1478 if gt_fcat is initial. 1479 clear: gw_fcat. 1480 gw_fcat-col_pos = 1. 1481 gw_fcat-fieldname = 'WERKS'. 1482 gw_fcat-tabname = 'MARC'. 1483 gw_fcat-scrtext_m = '工廠'. 1484 append gw_fcat to gt_fcat. 1485 1486 clear: gw_fcat. 1487 gw_fcat-col_pos = 2. 1488 gw_fcat-fieldname = 'MATNR'. 1489 gw_fcat-tabname = 'MARC'. 1490 gw_fcat-scrtext_m = '物料'. 1491 gw_fcat-no_zero = 'X'. 1492 append gw_fcat to gt_fcat. 1493 1494 clear: gw_fcat. 1495 gw_fcat-col_pos = 3. 1496 gw_fcat-fieldname = 'ZDATE'. 1497 gw_fcat-scrtext_m = '日期'. 1498 append gw_fcat to gt_fcat. 1499 1500 clear: gw_fcat. 1501 gw_fcat-col_pos = 4. 1502 gw_fcat-fieldname = 'DELB0'. 1503 gw_fcat-tabname = 'T457T'. 1504 gw_fcat-scrtext_m = 'MRP元素'. 1505 append gw_fcat to gt_fcat. 1506 1507 clear: gw_fcat. 1508 gw_fcat-col_pos = 5. 1509 gw_fcat-fieldname = 'EXTRA'. 1510 gw_fcat-tabname = 'MDEZ'. 1511 gw_fcat-scrtext_m = 'MRP元素數據'. 1512 gw_fcat-fix_column = 'X'. 1513 append gw_fcat to gt_fcat. 1514 1515 clear: gw_fcat. 1516 gw_fcat-col_pos = 6. 1517 gw_fcat-fieldname = 'MNG01'. 1518 gw_fcat-tabname = 'MDEZ'. 1519 gw_fcat-scrtext_m = '收貨數量或需求數量'. 1520 gw_fcat-fix_column = 'X'. 1521 gw_fcat-do_sum = 'X'. 1522 append gw_fcat to gt_fcat. 1523 endif. 1524 endform. "frm_set_fcat 1525 1526 *&---------------------------------------------------------------------* 1527 *& Form frm_set_lock 1528 *&---------------------------------------------------------------------* 1529 * 上鎖 1530 *----------------------------------------------------------------------* 1531 form frm_set_lock. 1532 "鎖定程序 1533 call function 'ENQUEUE_EZZSOPR0032' 1534 exporting 1535 mode_trdir = 'E' 1536 name = 'ZPPR0056' 1537 x_name = ' ' 1538 _scope = '2' 1539 _wait = ' ' 1540 _collect = ' ' 1541 exceptions 1542 foreign_lock = 1 1543 system_failure = 2 1544 others = 3. 1545 1546 if sy-subrc <> 0. 1547 * Implement suitable error handling here 1548 message '對象已被鎖定,請稍后執行' type 'S' display like 'E'. 1549 stop. 1550 else. 1551 "鎖定成功 1552 endif. 1553 endform. "frm_set_lock 1554 1555 *&---------------------------------------------------------------------* 1556 *& Form frm_set_unlock 1557 *&---------------------------------------------------------------------* 1558 * 解鎖 1559 *----------------------------------------------------------------------* 1560 form frm_set_unlock. 1561 "解除程序的鎖定 1562 call function 'DEQUEUE_EZZSOPR0032' 1563 exporting 1564 mode_trdir = 'E' 1565 name = 'ZPPR0056' 1566 x_name = ' ' 1567 _scope = '3' 1568 _synchron = ' ' 1569 _collect = ' '. 1570 endform. "frm_set_unlock 1571 1572 *&---------------------------------------------------------------------* 1573 *& Module STATUS_0100 OUTPUT 1574 *&---------------------------------------------------------------------* 1575 * text 1576 *----------------------------------------------------------------------* 1577 module status_0100 output. 1578 set pf-status 'ZSTA1'. 1579 * SET TITLEBAR 'xxx'. 1580 create object g_alv. 1581 g_alv->main( ). 1582 endmodule. " STATUS_0100 OUTPUT 1583 1584 *&---------------------------------------------------------------------* 1585 *& Module EXIT INPUT 1586 *&---------------------------------------------------------------------* 1587 * text 1588 *----------------------------------------------------------------------* 1589 module exit input. 1590 leave to screen 0. 1591 1592 endmodule. " EXIT INPUT
(備注:參考來源 http://blog.csdn.net/zhongguomao/article/details/8963815 )