天天看點

SAP貨物移動BAPI BAPI_GOODSMVT_CREATE(WMS TO SAP)

移動代碼

01 -- MB01

02 -- MB31

03 -- MB1A

04 -- MB1B

05 -- MB1C

06 -- MB11

07 -- MB04

一、收貨(101)

說明:采購訂單、生産訂單收貨

[plain]  view plain  copy

  1. form recvfromord tables pt_tran structure zwttran.  
  2.   "移庫定義  
  3.   data: goodsmvt_header  type   bapi2017_gm_head_01,  
  4.         goodsmvt_code    type   bapi2017_gm_code,  
  5.         goodsmvt_headret type   bapi2017_gm_head_ret,  
  6.         materialdocument type   bapi2017_gm_head_ret-mat_doc,  
  7.         matdocumentyear  type   bapi2017_gm_head_ret-doc_year,  
  8.         goodsmvt_item    type standard table of   /afs/bapi2017_gm_item_create,  
  9.         return           type standard table of   bapiret2,  
  10.         w_creturn        type   bapiret2,  
  11.         w_goodsmvt_item  type                     /afs/bapi2017_gm_item_create,  
  12.         w_return         type   bapiret2.  
  13.   data:lt_retmsg like table of bapiret2 with header line,  
  14.        lt_return like table of bapiret2 with header line.  
  15.   data:l_tran_nbr like zwttran-tran_nbr,  
  16.        l_tranpo   like zwttran-tranpo,  
  17.        l_matdoc   like zwttran-matdoc,  
  18.        l_flag     like zwttran-flag,  
  19.        l_line_id type i.  
  20.   data:l_asn_nbr type ze_ref_field, "asn号  
  21.        l_po type ze_ref_field,"po号  
  22.        l_po_line(5) type n,"po行号  
  23.        l_matnr type matnr."貨号  
  24.   data:l_type type char1,"類型  
  25.        l_afpo type afpo,"生産訂單  
  26.        l_j_3abssi type  j_3abssi,"生産訂單計劃行  
  27.        l_eket type eket,"采購訂單計劃行  
  28.        l_ekpo type ekpo."采購訂單行項目  
  29.   data:lt_bom like table of zwmbom with header line.  
  30.   refresh:t_tran_nbr.  
  31.   loop at pt_tran.  
  32.     clear :t_tran_nbr.  
  33.     t_tran_nbr-tran_nbr = pt_tran-tran_nbr.  
  34.     if pt_tran-tran_type = '603' and pt_tran-tran_code = '02' and pt_tran-proc_stat_code = '10'  and pt_tran-ref_field_6 = 'P'.  
  35.       t_tran_nbr-po = pt_tran-ref_field_3.  
  36.     endif.  
  37.     append t_tran_nbr.  
  38.   endloop.  
  39.   sort t_tran_nbr by tran_nbr po.  
  40.   delete adjacent duplicates from t_tran_nbr.  
  41.   loop at t_tran_nbr.  
  42.     clear:l_tran_nbr,l_matdoc,l_matnr,l_asn_nbr,l_po,l_matnr,l_type,l_afpo,l_j_3abssi,l_eket,l_ekpo,l_tranpo,lt_retmsg,lt_return,pt_tran.  
  43.     refresh:lt_return.  
  44.     clear:goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.  
  45.     refresh:goodsmvt_item ,return.  
  46.     "頭  
  47.     goodsmvt_header-pstng_date = sy-datum.  
  48.     goodsmvt_header-doc_date = sy-datum.  
  49.     "明細  
  50.     loop at pt_tran where tran_type = '603' and tran_code = '02'  and proc_stat_code = '10' and units_rcvd > 0  
  51.                       and tran_nbr = t_tran_nbr-tran_nbr and ref_field_3 = t_tran_nbr-po.  
  52.       if pt_tran-ref_field_6 = 'P'.  
  53.         l_tran_nbr =  pt_tran-tran_nbr.  
  54.         "擷取asn号  
  55.         l_asn_nbr = pt_tran-ref_field_1.  
  56.         l_po = pt_tran-ref_field_3.  
  57.         concatenate l_tran_nbr l_po into l_tranpo.  
  58.         if pt_tran-ref_field_4 is not initial.  
  59.           l_po_line = pt_tran-ref_field_4.  
  60.         else.  
  61.           clear l_po_line.  
  62.         endif.  
  63.         l_matnr = pt_tran-matnr.  
  64.         clear:lt_retmsg.  
  65.         refresh:lt_retmsg.  
  66.         call function 'ZLC_GET_PREMOVE101'  
  67.           EXPORTING  
  68.             p_matnr    = l_matnr  
  69.             p_po       = l_po  
  70.             p_po_line  = l_po_line  
  71.           IMPORTING  
  72.             p_type     = l_type  
  73.             p_afpo     = l_afpo  
  74.             p_j_3abssi = l_j_3abssi  
  75.             p_ekpo     = l_ekpo  
  76.             p_eket     = l_eket  
  77.           TABLES  
  78.             pt_retmsg  = lt_retmsg.  
  79.         read table lt_retmsg with key type = 'E'.  
  80.         if sy-subrc <> 0.  
  81.           case l_type.  
  82.             when 'E'.  
  83.               "事務代碼  
  84.               goodsmvt_code-gm_code = '02'.                 "mb31  
  85.               "明細  
  86.               w_goodsmvt_item-line_id = w_goodsmvt_item-line_id + 1.  
  87.               w_goodsmvt_item-move_type = '101'."移動類型  
  88.               w_goodsmvt_item-material = l_j_3abssi-matnr."大貨号  
  89.               w_goodsmvt_item-grid_value = l_j_3abssi-j_3asize."網格值  
  90.               w_goodsmvt_item-plant = g_plant."工廠  
  91.               w_goodsmvt_item-stge_loc = g_stge_loc."地點  
  92.               w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd."數量  
  93.               w_goodsmvt_item-orderid = l_j_3abssi-j_3absnr."訂單号  
  94.               w_goodsmvt_item-order_itno = l_j_3abssi-j_3ahbsp."行項目  
  95.               w_goodsmvt_item-sched_line_sku = l_j_3abssi-j_3aebsp."計劃行項目  
  96.               w_goodsmvt_item-mvt_ind = 'F'."生産訂單  
  97.               w_goodsmvt_item-stck_type = 'E'.  
  98.               w_goodsmvt_item-gr_rcpt  = pt_tran-user_id.  
  99.               append w_goodsmvt_item to goodsmvt_item.  
  100.             when 'F'.  
  101.               "事務代碼  
  102.               goodsmvt_code-gm_code = '01'.                 "mb01  
  103.               "明細  
  104.               w_goodsmvt_item-line_id = w_goodsmvt_item-line_id + 1.  
  105.               l_line_id = w_goodsmvt_item-line_id.  
  106.               w_goodsmvt_item-move_type = '101'."移動類型  
  107.               w_goodsmvt_item-material = l_ekpo-matnr."大貨号  
  108.               w_goodsmvt_item-grid_value = l_eket-j_3asize."網格值  
  109.               w_goodsmvt_item-plant = g_plant."工廠  
  110.               w_goodsmvt_item-stge_loc = g_stge_loc."地點  
  111.               clear w_goodsmvt_item-vendor.  
  112.               w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd."數量  
  113.               clear w_goodsmvt_item-batch.  
  114.               w_goodsmvt_item-po_number = l_eket-ebeln."采購訂單  
  115.               w_goodsmvt_item-po_item = l_eket-ebelp."采購訂單行号  
  116.               w_goodsmvt_item-sched_line_sku = l_eket-etenr."計劃行  
  117.               clear:w_goodsmvt_item-parent_id,w_goodsmvt_item-line_depth.  
  118.               w_goodsmvt_item-mvt_ind = 'B'."采購訂單  
  119.               w_goodsmvt_item-stck_type = 'E'."庫存類型  
  120.               w_goodsmvt_item-gr_rcpt  = pt_tran-user_id.  
  121.               append w_goodsmvt_item to goodsmvt_item.  
  122.               refresh:lt_bom.  
  123.               call function 'ZLC_GET_BOM'  
  124.                 EXPORTING  
  125.                   p_ebeln = l_eket-ebeln  
  126.                   p_ebelp = l_eket-ebelp  
  127.                   p_etenr = l_eket-etenr  
  128.                 TABLES  
  129.                   pt_bom  = lt_bom.  
  130.               if lines( lt_bom ) > 0.  
  131.                 loop at lt_bom.  
  132.                   w_goodsmvt_item-line_id = w_goodsmvt_item-line_id + 1.  
  133.                   w_goodsmvt_item-move_type = '543'."移動類型  
  134.                   w_goodsmvt_item-material = lt_bom-matnr."貨号  
  135.                   if lt_bom-grdv is not initial.  
  136.                     w_goodsmvt_item-grid_value = lt_bom-grdv.  
  137.                   else.  
  138.                     clear w_goodsmvt_item-grid_value.  
  139.                   endif.  
  140.                   w_goodsmvt_item-plant = '1000'."工廠  
  141.                   clear w_goodsmvt_item-stge_loc.  
  142.                   w_goodsmvt_item-vendor = lt_bom-vendor."供應商  
  143.                   w_goodsmvt_item-entry_qnt = lt_bom-entry_qnt * pt_tran-units_rcvd."數量  
  144.                   if lt_bom-charg is not initial.  
  145.                     w_goodsmvt_item-batch = lt_bom-charg.  
  146.                   else.  
  147.                     clear w_goodsmvt_item-batch.  
  148.                   endif.  
  149.                   w_goodsmvt_item-parent_id = l_line_id.  
  150.                   w_goodsmvt_item-line_depth = 1.  
  151.                   w_goodsmvt_item-mvt_ind = 'B'."采購訂單  
  152.                   w_goodsmvt_item-stck_type = 'E'."庫存類型  
  153.                   w_goodsmvt_item-gr_rcpt  = pt_tran-user_id.  
  154.                   append w_goodsmvt_item to goodsmvt_item.  
  155.                 endloop.  
  156.               endif.  
  157.           endcase.  
  158.         else.  
  159.           append lines of lt_retmsg to lt_return.  
  160.         endif.  
  161.       endif.  
  162.     endloop.  
  163.     read table lt_return with key type = 'E'.  
  164.     if sy-subrc <> 0.  
  165.       if goodsmvt_item[] is not initial.  
  166.         "删除日志  
  167.         perform deletepixtranlog using l_tranpo.  
  168.         "執行操作  
  169.         call function '/AFS/BAPI_GOODSMVT_CREATE'  
  170.           EXPORTING  
  171.             goodsmvt_header   = goodsmvt_header  
  172.             goodsmvt_code     = goodsmvt_code  
  173.           IMPORTING  
  174.             goodsmvt_headret  = goodsmvt_headret  
  175.             materialdocument  = materialdocument  
  176.             matdocumentyear   = matdocumentyear  
  177.           TABLES  
  178.             afs_goodsmvt_item = goodsmvt_item  
  179.             return            = return.  
  180.         read table return with key type = 'E' into w_return.  
  181.         if sy-subrc <> 0.  
  182.           "送出  
  183.           call function 'BAPI_TRANSACTION_COMMIT'  
  184.             EXPORTING  
  185.               wait = 'X'.  
  186.           l_flag = 'S'.  
  187.           l_matdoc = materialdocument.  
  188.         else.  
  189.           call function 'BAPI_TRANSACTION_ROLLBACK'.  
  190.           l_flag = 'F'.  
  191.         endif.  
  192.         update zwttran set flag = l_flag matdoc = l_matdoc where tranpo = l_tranpo.  
  193.         if l_flag = 'F'.  
  194.           perform writepixtranlog tables return using l_tranpo.  
  195.         endif.  
  196.       endif.  
  197.     else.  
  198.       update zwttran set flag = 'F' where tranpo = l_tranpo.  
  199.       perform writepixtranlog tables lt_return using l_tranpo.  
  200.     endif.  
  201.   endloop.  
  202. endform.                    "recvfromord  
二、移庫、當機、解凍、内部領用、盤點、報廢、轉包、門店退貨等

[plain]  view plain  copy

  1. form dopixtran tables pt_tran structure zwttran.  
  2.   data: l_tran_nbr like zwttran-tran_nbr,  
  3.         l_matdoc   like zwttran-matdoc,  
  4.         l_flag     like zwttran-flag,  
  5.         l_mara     like mara,  
  6.         lt_mard like table of mard with header line.  
  7.   data:goodsmvt_header  type   bapi2017_gm_head_01,  
  8.         goodsmvt_code    type   bapi2017_gm_code,  
  9.         goodsmvt_headret type   bapi2017_gm_head_ret,  
  10.         materialdocument type   bapi2017_gm_head_ret-mat_doc,  
  11.         matdocumentyear  type   bapi2017_gm_head_ret-doc_year,  
  12.         goodsmvt_item    type standard table of   bapi2017_gm_item_create,  
  13.         return           type standard table of   bapiret2,  
  14.         w_goodsmvt_item  type                     bapi2017_gm_item_create,  
  15.         w_return         type bapiret2.  
  16.   data:lt_retmsg like table of bapiret2 with header line.  
  17.   refresh:t_tran_nbr.  
  18.   loop at pt_tran.  
  19.     clear:t_tran_nbr.  
  20.     t_tran_nbr-tran_nbr = pt_tran-tran_nbr.  
  21.     collect t_tran_nbr.  
  22.   endloop.  
  23.   sort t_tran_nbr by tran_nbr.  
  24.   loop at t_tran_nbr.  
  25.     "&--------------------------------------------------------------------------------------------------------------&  
  26.     "& BEGIN 從3,4倉移庫到物流中心倉庫  
  27.     "&--------------------------------------------------------------------------------------------------------------&  
  28.     clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.  
  29.     refresh:goodsmvt_item ,return.  
  30.     "事物碼  
  31.     goodsmvt_code-gm_code = '04'.  
  32.     "頭  
  33.     goodsmvt_header-pstng_date = sy-datum.  
  34.     goodsmvt_header-doc_date = sy-datum.  
  35.     "明細  
  36.     loop at pt_tran where tran_type = '603' and tran_code = '02' and proc_stat_code = '10' and units_rcvd > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  37.       if pt_tran-ref_field_6 = 'Y'.  
  38.         l_tran_nbr = pt_tran-tran_nbr.  
  39.         w_goodsmvt_item-move_type = '311'."移庫  
  40.         w_goodsmvt_item-plant = g_plant.  
  41.         w_goodsmvt_item-stge_loc = pt_tran-ref_field_7.  
  42.         w_goodsmvt_item-material = pt_tran-matnr.  
  43.         w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd.  
  44.         w_goodsmvt_item-move_plant = g_plant.  
  45.         w_goodsmvt_item-move_stloc = g_stge_loc.  
  46.         append w_goodsmvt_item to goodsmvt_item.  
  47.       endif.  
  48.     endloop.  
  49.     "執行操作  
  50.     if goodsmvt_item[] is not initial.  
  51.       l_flag = 'F'.  
  52.       call function 'BAPI_GOODSMVT_CREATE'  
  53.         EXPORTING  
  54.           goodsmvt_header  = goodsmvt_header  
  55.           goodsmvt_code    = goodsmvt_code  
  56.         IMPORTING  
  57.           goodsmvt_headret = goodsmvt_headret  
  58.           materialdocument = materialdocument  
  59.           matdocumentyear  = matdocumentyear  
  60.         TABLES  
  61.           goodsmvt_item    = goodsmvt_item  
  62.           return           = return.  
  63.       read table return with key type = 'E' into w_return.  
  64.       if sy-subrc <> 0.  
  65.         call function 'BAPI_TRANSACTION_COMMIT'  
  66.           EXPORTING  
  67.             wait = 'X'.  
  68.         if materialdocument is not initial.  
  69.           l_flag = 'S'.  
  70.           l_matdoc = materialdocument.  
  71.         endif.  
  72.       else.  
  73.         call function 'BAPI_TRANSACTION_ROLLBACK'.  
  74.         perform writepixtranlog tables return using l_tran_nbr.  
  75.       endif.  
  76.       update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  77.     endif.  
  78.     "&--------------------------------------------------------------------------------------------------------------&  
  79.     "& END 移入  
  80.     "&--------------------------------------------------------------------------------------------------------------&  
  81.     "&--------------------------------------------------------------------------------------------------------------&  
  82.     "& BEGIN 從物流中心倉庫移至3,4倉  
  83.     "&--------------------------------------------------------------------------------------------------------------&  
  84.     clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.  
  85.     refresh:goodsmvt_item ,return.  
  86.     "事物碼  
  87.     goodsmvt_code-gm_code = '04'.  
  88.     "頭  
  89.     goodsmvt_header-pstng_date = sy-datum.  
  90.     goodsmvt_header-doc_date = sy-datum.  
  91.     "明細  
  92.     loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  93.       if pt_tran-rsn_code = 'YC'.  
  94.         l_tran_nbr = pt_tran-tran_nbr.  
  95.         w_goodsmvt_item-move_type = '311'."移庫  
  96.         w_goodsmvt_item-plant = g_plant.  
  97.         w_goodsmvt_item-stge_loc =  g_stge_loc.  
  98.         w_goodsmvt_item-material = pt_tran-matnr.  
  99.         w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.  
  100.         w_goodsmvt_item-move_plant = g_plant.  
  101.         w_goodsmvt_item-move_stloc = pt_tran-ref_field_5.  
  102.         append w_goodsmvt_item to goodsmvt_item.  
  103.       endif.  
  104.     endloop.  
  105.     "執行操作  
  106.     if goodsmvt_item[] is not initial.  
  107.       l_flag = 'F'.  
  108.       call function 'BAPI_GOODSMVT_CREATE'  
  109.         EXPORTING  
  110.           goodsmvt_header  = goodsmvt_header  
  111.           goodsmvt_code    = goodsmvt_code  
  112.         IMPORTING  
  113.           goodsmvt_headret = goodsmvt_headret  
  114.           materialdocument = materialdocument  
  115.           matdocumentyear  = matdocumentyear  
  116.         TABLES  
  117.           goodsmvt_item    = goodsmvt_item  
  118.           return           = return.  
  119.       read table return with key type = 'E' into w_return.  
  120.       if sy-subrc <> 0.  
  121.         call function 'BAPI_TRANSACTION_COMMIT'  
  122.           EXPORTING  
  123.             wait = 'X'.  
  124.         if materialdocument is not initial.  
  125.           l_flag = 'S'.  
  126.           l_matdoc = materialdocument.  
  127.         endif.  
  128.       else.  
  129.         call function 'BAPI_TRANSACTION_ROLLBACK'.  
  130.         perform writepixtranlog tables return using l_tran_nbr.  
  131.       endif.  
  132.       update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  133.     endif.  
  134.     "&--------------------------------------------------------------------------------------------------------------&  
  135.     "& END 移出  
  136.     "&--------------------------------------------------------------------------------------------------------------&  
  137.     "&--------------------------------------------------------------------------------------------------------------&  
  138.     "& begin 當機庫存  
  139.     "&--------------------------------------------------------------------------------------------------------------&  
  140.     clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.  
  141.     refresh:goodsmvt_item ,return.  
  142.     "事物碼  
  143.     goodsmvt_code-gm_code = '04'.  
  144.     "頭  
  145.     goodsmvt_header-pstng_date = sy-datum.  
  146.     goodsmvt_header-doc_date = sy-datum.  
  147.     "明細  
  148.     loop at pt_tran where tran_type = '300' and tran_code = '01' and actn_code = '05' and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  149.       l_tran_nbr = pt_tran-tran_nbr.  
  150.       w_goodsmvt_item-move_type = '344'."當機庫存  
  151.       w_goodsmvt_item-plant = g_plant.  
  152.       w_goodsmvt_item-stge_loc = g_stge_loc.  
  153.       w_goodsmvt_item-material = pt_tran-matnr.  
  154.       w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.  
  155.       append w_goodsmvt_item to goodsmvt_item.  
  156.     endloop.  
  157.     "執行操作  
  158.     if goodsmvt_item[] is not initial.  
  159.       l_flag = 'F'.  
  160.       call function 'BAPI_GOODSMVT_CREATE'  
  161.         EXPORTING  
  162.           goodsmvt_header  = goodsmvt_header  
  163.           goodsmvt_code    = goodsmvt_code  
  164.         IMPORTING  
  165.           goodsmvt_headret = goodsmvt_headret  
  166.           materialdocument = materialdocument  
  167.           matdocumentyear  = matdocumentyear  
  168.         TABLES  
  169.           goodsmvt_item    = goodsmvt_item  
  170.           return           = return.  
  171.       read table return with key type = 'E' into w_return.  
  172.       if sy-subrc <> 0.  
  173.         call function 'BAPI_TRANSACTION_COMMIT'  
  174.           EXPORTING  
  175.             wait = 'X'.  
  176.         if materialdocument is not initial.  
  177.           l_flag = 'S'.  
  178.           l_matdoc = materialdocument.  
  179.         endif.  
  180.       else.  
  181.         call function 'BAPI_TRANSACTION_ROLLBACK'.  
  182.         perform writepixtranlog tables return using l_tran_nbr.  
  183.       endif.  
  184.       update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  185.     endif.  
  186.     "&--------------------------------------------------------------------------------------------------------------&  
  187.     "& END 當機庫存  
  188.     "&--------------------------------------------------------------------------------------------------------------&  
  189.     "&--------------------------------------------------------------------------------------------------------------&  
  190.     "& begin 解凍庫存  
  191.     "&--------------------------------------------------------------------------------------------------------------&  
  192.     clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return.  
  193.     refresh:goodsmvt_item ,return.  
  194.     "事物碼  
  195.     goodsmvt_code-gm_code = '04'.  
  196.     "頭  
  197.     goodsmvt_header-pstng_date = sy-datum.  
  198.     goodsmvt_header-doc_date = sy-datum.  
  199.     "明細  
  200.     loop at pt_tran where tran_type = '606' and tran_code = '02' and actn_code = '06' and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  201.       l_tran_nbr = pt_tran-tran_nbr.  
  202.       w_goodsmvt_item-move_type = '343'."解凍庫存  
  203.       w_goodsmvt_item-plant = g_plant.  
  204.       w_goodsmvt_item-stge_loc = g_stge_loc.  
  205.       w_goodsmvt_item-material = pt_tran-matnr.  
  206.       w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.  
  207.       append w_goodsmvt_item to goodsmvt_item.  
  208.     endloop.  
  209.     "執行操作  
  210.     if goodsmvt_item[] is not initial.  
  211.       l_flag = 'F'.  
  212.       call function 'BAPI_GOODSMVT_CREATE'  
  213.         EXPORTING  
  214.           goodsmvt_header  = goodsmvt_header  
  215.           goodsmvt_code    = goodsmvt_code  
  216.         IMPORTING  
  217.           goodsmvt_headret = goodsmvt_headret  
  218.           materialdocument = materialdocument  
  219.           matdocumentyear  = matdocumentyear  
  220.         TABLES  
  221.           goodsmvt_item    = goodsmvt_item  
  222.           return           = return.  
  223.       read table return with key type = 'E' into w_return.  
  224.       if sy-subrc <> 0.  
  225.         call function 'BAPI_TRANSACTION_COMMIT'  
  226.           EXPORTING  
  227.             wait = 'X'.  
  228.         if materialdocument is not initial.  
  229.           l_flag = 'S'.  
  230.           l_matdoc = materialdocument.  
  231.         endif.  
  232.       else.  
  233.         call function 'BAPI_TRANSACTION_ROLLBACK'.  
  234.         perform writepixtranlog tables return using l_tran_nbr.  
  235.       endif.  
  236.       update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  237.     endif.  
  238.     "&--------------------------------------------------------------------------------------------------------------&  
  239.     "& END 解凍庫存  
  240.     "&--------------------------------------------------------------------------------------------------------------&  
  241.     "&--------------------------------------------------------------------------------------------------------------&  
  242.     "& BEGIN 内部領用  
  243.     "&--------------------------------------------------------------------------------------------------------------&  
  244.     data:l_kostl like csks-kostl,"成本中心  
  245.          l_prctr like cepc-prctr,"利潤中心  
  246.          l_csks like csks,"成本中心結構  
  247.          l_cepc like cepc."利潤中心結構  
  248.     data:lt_tpmsg like table of bapiret2 with header line.  
  249.     clear:l_kostl,l_prctr,l_csks,l_cepc.  
  250.     clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.  
  251.     refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.  
  252.     "事物碼  
  253.     goodsmvt_code-gm_code = '03'.                           "MB1A  
  254.     "頭  
  255.     goodsmvt_header-pstng_date = sy-datum.  
  256.     goodsmvt_header-doc_date = sy-datum.  
  257.     "明細  
  258.     loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  259.       if pt_tran-rsn_code = 'NL'.  
  260.         clear:l_kostl,l_csks,lt_tpmsg.  
  261.         refresh:lt_tpmsg.  
  262.         l_tran_nbr = pt_tran-tran_nbr.  
  263.         l_kostl = pt_tran-ref_field_5.  
  264.         call function 'ZLC_GET_CSKS'  
  265.           EXPORTING  
  266.             P_KOKRS   = '1000'  
  267.             P_KOSTL   = l_kostl  
  268.           IMPORTING  
  269.             p_csks    = l_csks  
  270.           TABLES  
  271.             pt_retmsg = lt_tpmsg.  
  272.         read table lt_tpmsg with key type = 'E'.  
  273.         if sy-subrc <> 0.  
  274.           w_goodsmvt_item-move_type = '201'."内部領用  
  275.           w_goodsmvt_item-plant = g_plant.  
  276.           w_goodsmvt_item-stge_loc = g_stge_loc.  
  277.           w_goodsmvt_item-material = pt_tran-matnr.  
  278.           w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.  
  279.           w_goodsmvt_item-costcenter = l_kostl."成本中心  
  280.           w_goodsmvt_item-profit_ctr = l_csks-prctr."利潤中心  
  281.           append w_goodsmvt_item to goodsmvt_item.  
  282.         else.  
  283.           append lines of lt_tpmsg to lt_retmsg.  
  284.         endif.  
  285.       endif.  
  286.     endloop.  
  287.     "執行操作  
  288.     read table lt_retmsg with key type = 'E'.  
  289.     if sy-subrc <> 0.  
  290.       if goodsmvt_item[] is not initial.  
  291.         l_flag = 'F'.  
  292.         call function 'BAPI_GOODSMVT_CREATE'  
  293.           EXPORTING  
  294.             goodsmvt_header  = goodsmvt_header  
  295.             goodsmvt_code    = goodsmvt_code  
  296.           IMPORTING  
  297.             goodsmvt_headret = goodsmvt_headret  
  298.             materialdocument = materialdocument  
  299.             matdocumentyear  = matdocumentyear  
  300.           TABLES  
  301.             goodsmvt_item    = goodsmvt_item  
  302.             return           = return.  
  303.         read table return with key type = 'E' into w_return.  
  304.         if sy-subrc <> 0.  
  305.           call function 'BAPI_TRANSACTION_COMMIT'  
  306.             EXPORTING  
  307.               wait = 'X'.  
  308.           if materialdocument is not initial.  
  309.             l_flag = 'S'.  
  310.             l_matdoc = materialdocument.  
  311.           endif.  
  312.         else.  
  313.           call function 'BAPI_TRANSACTION_ROLLBACK'.  
  314.           perform writepixtranlog tables return using l_tran_nbr.  
  315.         endif.  
  316.         update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  317.       endif.  
  318.     else.  
  319.       update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.  
  320.       perform writepixtranlog tables lt_retmsg using l_tran_nbr.  
  321.     endif.  
  322.     "&--------------------------------------------------------------------------------------------------------------&  
  323.     "& END 内部領用  
  324.     "&--------------------------------------------------------------------------------------------------------------&  
  325.     "&--------------------------------------------------------------------------------------------------------------&  
  326.     "& BEGIN 内部領用退回  
  327.     "&--------------------------------------------------------------------------------------------------------------&  
  328.     clear:l_kostl,l_prctr,l_csks,l_cepc.  
  329.     clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.  
  330.     refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.  
  331.     "事物碼  
  332.     goodsmvt_code-gm_code = '03'.                           "MB1A  
  333.     "頭  
  334.     goodsmvt_header-pstng_date = sy-datum.  
  335.     goodsmvt_header-doc_date = sy-datum.  
  336.     "明細  
  337.     loop at pt_tran where tran_type = '603' and tran_code = '02' and proc_stat_code = '10' and units_rcvd > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  338.       if pt_tran-ref_field_6 = 'N'.  
  339.         clear:l_kostl,l_csks,lt_tpmsg.  
  340.         refresh:lt_tpmsg.  
  341.         l_tran_nbr = pt_tran-tran_nbr.  
  342.         l_kostl = pt_tran-ref_field_5.  
  343.         call function 'ZLC_GET_CSKS'  
  344.           EXPORTING  
  345.             P_KOKRS   = '1000'  
  346.             P_KOSTL   = l_kostl  
  347.           IMPORTING  
  348.             p_csks    = l_csks  
  349.           TABLES  
  350.             pt_retmsg = lt_tpmsg.  
  351.         read table lt_tpmsg with key type = 'E'.  
  352.         if sy-subrc <> 0.  
  353.           w_goodsmvt_item-move_type = '202'."内部領用沖銷  
  354.           w_goodsmvt_item-plant = g_plant.  
  355.           w_goodsmvt_item-stge_loc = g_stge_loc.  
  356.           w_goodsmvt_item-material = pt_tran-matnr.  
  357.           w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd.  
  358.           w_goodsmvt_item-costcenter = l_kostl."成本中心  
  359.           w_goodsmvt_item-profit_ctr = l_csks-prctr."利潤中心  
  360.           append w_goodsmvt_item to goodsmvt_item.  
  361.         else.  
  362.           append lines of lt_tpmsg to lt_retmsg.  
  363.         endif.  
  364.       endif.  
  365.     endloop.  
  366.     "執行操作  
  367.     read table lt_retmsg with key type = 'E'.  
  368.     if sy-subrc <> 0.  
  369.       if goodsmvt_item[] is not initial.  
  370.         l_flag = 'F'.  
  371.         call function 'BAPI_GOODSMVT_CREATE'  
  372.           EXPORTING  
  373.             goodsmvt_header  = goodsmvt_header  
  374.             goodsmvt_code    = goodsmvt_code  
  375.           IMPORTING  
  376.             goodsmvt_headret = goodsmvt_headret  
  377.             materialdocument = materialdocument  
  378.             matdocumentyear  = matdocumentyear  
  379.           TABLES  
  380.             goodsmvt_item    = goodsmvt_item  
  381.             return           = return.  
  382.         read table return with key type = 'E' into w_return.  
  383.         if sy-subrc <> 0.  
  384.           call function 'BAPI_TRANSACTION_COMMIT'  
  385.             EXPORTING  
  386.               wait = 'X'.  
  387.           if materialdocument is not initial.  
  388.             l_flag = 'S'.  
  389.             l_matdoc = materialdocument.  
  390.           endif.  
  391.         else.  
  392.           call function 'BAPI_TRANSACTION_ROLLBACK'.  
  393.           perform writepixtranlog tables return using l_tran_nbr.  
  394.         endif.  
  395.         update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  396.       endif.  
  397.     else.  
  398.       update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.  
  399.       perform writepixtranlog tables lt_retmsg using l_tran_nbr.  
  400.     endif.  
  401.     "&--------------------------------------------------------------------------------------------------------------&  
  402.     "& END 内部領用退回  
  403.     "&--------------------------------------------------------------------------------------------------------------&  
  404.     "&--------------------------------------------------------------------------------------------------------------&  
  405.     "& BEGIN 報廢  
  406.     "&--------------------------------------------------------------------------------------------------------------&  
  407.     clear:l_kostl,l_prctr,l_csks,l_cepc.  
  408.     clear:l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.  
  409.     refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.  
  410.     "事物碼  
  411.     goodsmvt_code-gm_code = '03'.  
  412.     "頭  
  413.     goodsmvt_header-pstng_date = sy-datum.  
  414.     goodsmvt_header-doc_date = sy-datum.  
  415.     "明細  
  416.     loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  417.       if pt_tran-rsn_code = 'BF'.  
  418.         clear:l_prctr,l_cepc.  
  419.         refresh:lt_tpmsg.  
  420.         l_tran_nbr = pt_tran-tran_nbr.  
  421.         l_prctr = pt_tran-ref_field_5.  
  422.         call function 'ZLC_GET_CEPC'  
  423.           EXPORTING  
  424.             p_kokrs   = '1000'  
  425.             p_prctr   = l_prctr  
  426.           IMPORTING  
  427.             p_cepc    = l_cepc  
  428.           TABLES  
  429.             pt_retmsg = lt_tpmsg.  
  430.         read table lt_tpmsg with key type = 'E'.  
  431.         if sy-subrc <> 0.  
  432.           w_goodsmvt_item-move_type = '551'."報廢  
  433.           w_goodsmvt_item-plant = g_plant.  
  434.           w_goodsmvt_item-stge_loc = g_stge_loc.  
  435.           w_goodsmvt_item-material = pt_tran-matnr.  
  436.           w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.  
  437.           w_goodsmvt_item-profit_ctr = l_prctr."利潤中心  
  438.           append w_goodsmvt_item to goodsmvt_item.  
  439.         else.  
  440.           append lines of lt_tpmsg to lt_retmsg.  
  441.         endif.  
  442.       endif.  
  443.     endloop.  
  444.     "執行操作  
  445.     read table lt_retmsg with key type = 'E'.  
  446.     if sy-subrc <> 0.  
  447.       if goodsmvt_item[] is not initial.  
  448.         l_flag = 'F'.  
  449.         call function 'BAPI_GOODSMVT_CREATE'  
  450.           EXPORTING  
  451.             goodsmvt_header  = goodsmvt_header  
  452.             goodsmvt_code    = goodsmvt_code  
  453.           IMPORTING  
  454.             goodsmvt_headret = goodsmvt_headret  
  455.             materialdocument = materialdocument  
  456.             matdocumentyear  = matdocumentyear  
  457.           TABLES  
  458.             goodsmvt_item    = goodsmvt_item  
  459.             return           = return.  
  460.         read table return with key type = 'E' into w_return.  
  461.         if sy-subrc <> 0.  
  462.           call function 'BAPI_TRANSACTION_COMMIT'  
  463.             EXPORTING  
  464.               wait = 'X'.  
  465.           if materialdocument is not initial.  
  466.             l_flag = 'S'.  
  467.             l_matdoc = materialdocument.  
  468.           endif.  
  469.         else.  
  470.           call function 'BAPI_TRANSACTION_ROLLBACK'.  
  471.           perform writepixtranlog tables return using l_tran_nbr.  
  472.         endif.  
  473.         update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  474.       endif.  
  475.     else.  
  476.       update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.  
  477.       perform writepixtranlog tables lt_retmsg using l_tran_nbr.  
  478.     endif.  
  479.     "&--------------------------------------------------------------------------------------------------------------&  
  480.     "& END 報廢  
  481.     "&--------------------------------------------------------------------------------------------------------------&  
  482.     "&--------------------------------------------------------------------------------------------------------------&  
  483.     "& BEGIN 轉包  
  484.     "&--------------------------------------------------------------------------------------------------------------&  
  485.     data:l_lifnr like lfa1-lifnr.  
  486.     clear:l_lifnr,l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.  
  487.     refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.  
  488.     "事物碼  
  489.     goodsmvt_code-gm_code = '04'.  
  490.     "頭  
  491.     goodsmvt_header-pstng_date = sy-datum.  
  492.     goodsmvt_header-doc_date = sy-datum.  
  493.     "明細  
  494.     loop at pt_tran where  tran_type = '300' and tran_code = '01' and actn_code = '07'and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  495.       if pt_tran-rsn_code = 'ZB'.  
  496.         clear:l_lifnr.  
  497.         refresh:lt_tpmsg.  
  498.         l_tran_nbr = pt_tran-tran_nbr.  
  499.         l_lifnr = pt_tran-ref_field_5.  
  500.         call function 'CONVERSION_EXIT_ALPHA_INPUT'  
  501.           EXPORTING  
  502.             input  = l_lifnr  
  503.           IMPORTING  
  504.             output = l_lifnr.  
  505.         select count(*) from lfa1 where lifnr = l_lifnr.  
  506.         if sy-subrc <> 0.  
  507.           lt_tpmsg-type = 'E'.  
  508.           concatenate '供應商' l_lifnr '不存在!' into lt_tpmsg-message.  
  509.           append lt_tpmsg.  
  510.         endif.  
  511.         read table lt_tpmsg with key type = 'E'.  
  512.         if sy-subrc <> 0.  
  513.           w_goodsmvt_item-move_type = '541'."轉包  
  514.           w_goodsmvt_item-plant = g_plant.  
  515.           w_goodsmvt_item-stge_loc = g_stge_loc.  
  516.           w_goodsmvt_item-material = pt_tran-matnr.  
  517.           w_goodsmvt_item-entry_qnt = pt_tran-invn_adjmt_qty.  
  518.           w_goodsmvt_item-vendor = l_lifnr."供應商  
  519.           append w_goodsmvt_item to goodsmvt_item.  
  520.         else.  
  521.           append lines of lt_tpmsg to lt_retmsg.  
  522.         endif.  
  523.       endif.  
  524.     endloop.  
  525.     "執行操作  
  526.     read table lt_retmsg with key type = 'E'.  
  527.     if sy-subrc <> 0.  
  528.       if goodsmvt_item[] is not initial.  
  529.         l_flag = 'F'.  
  530.         call function 'BAPI_GOODSMVT_CREATE'  
  531.           EXPORTING  
  532.             goodsmvt_header  = goodsmvt_header  
  533.             goodsmvt_code    = goodsmvt_code  
  534.           IMPORTING  
  535.             goodsmvt_headret = goodsmvt_headret  
  536.             materialdocument = materialdocument  
  537.             matdocumentyear  = matdocumentyear  
  538.           TABLES  
  539.             goodsmvt_item    = goodsmvt_item  
  540.             return           = return.  
  541.         read table return with key type = 'E' into w_return.  
  542.         if sy-subrc <> 0.  
  543.           call function 'BAPI_TRANSACTION_COMMIT'  
  544.             EXPORTING  
  545.               wait = 'X'.  
  546.           if materialdocument is not initial.  
  547.             l_flag = 'S'.  
  548.             l_matdoc = materialdocument.  
  549.           endif.  
  550.         else.  
  551.           call function 'BAPI_TRANSACTION_ROLLBACK'.  
  552.           perform writepixtranlog tables return using l_tran_nbr.  
  553.         endif.  
  554.         update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  555.       endif.  
  556.     else.  
  557.       update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.  
  558.       perform writepixtranlog tables lt_retmsg using l_tran_nbr.  
  559.     endif.  
  560.     "&--------------------------------------------------------------------------------------------------------------&  
  561.     "& END 轉包  
  562.     "&--------------------------------------------------------------------------------------------------------------&  
  563.     "&--------------------------------------------------------------------------------------------------------------&  
  564.     "& BEGIN 轉包沖銷  
  565.     "&--------------------------------------------------------------------------------------------------------------&  
  566.     clear:l_lifnr,l_tran_nbr,l_flag,l_matdoc,goodsmvt_header,goodsmvt_code,goodsmvt_headret,materialdocument,matdocumentyear, goodsmvt_item ,return,w_return,lt_tpmsg,lt_retmsg.  
  567.     refresh:goodsmvt_item,return,lt_tpmsg,lt_retmsg.  
  568.     "事物碼  
  569.     goodsmvt_code-gm_code = '04'.  
  570.     "頭  
  571.     goodsmvt_header-pstng_date = sy-datum.  
  572.     goodsmvt_header-doc_date = sy-datum.  
  573.     "明細  
  574.     loop at pt_tran where tran_type = '603' and tran_code = '02' and proc_stat_code = '10' and units_rcvd > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  575.       if pt_tran-ref_field_6 = 'T'.  
  576.         clear:l_lifnr.  
  577.         refresh:lt_tpmsg.  
  578.         l_tran_nbr = pt_tran-tran_nbr.  
  579.         l_lifnr = pt_tran-ref_field_7.  
  580.         call function 'CONVERSION_EXIT_ALPHA_INPUT'  
  581.           EXPORTING  
  582.             input  = l_lifnr  
  583.           IMPORTING  
  584.             output = l_lifnr.  
  585.         select count(*) from lfa1 where lifnr = l_lifnr.  
  586.         if sy-subrc <> 0.  
  587.           lt_tpmsg-type = 'E'.  
  588.           concatenate '供應商' l_lifnr '不存在!' into lt_tpmsg-message.  
  589.           append lt_tpmsg.  
  590.         endif.  
  591.         read table lt_tpmsg with key type = 'E'.  
  592.         if sy-subrc <> 0.  
  593.           w_goodsmvt_item-move_type = '542'."轉包沖銷  
  594.           w_goodsmvt_item-plant = g_plant.  
  595.           w_goodsmvt_item-stge_loc = g_stge_loc.  
  596.           w_goodsmvt_item-material = pt_tran-matnr.  
  597.           w_goodsmvt_item-entry_qnt = pt_tran-units_rcvd.  
  598.           w_goodsmvt_item-vendor = l_lifnr."供應商  
  599.           append w_goodsmvt_item to goodsmvt_item.  
  600.         else.  
  601.           append lines of lt_tpmsg to lt_retmsg.  
  602.         endif.  
  603.       endif.  
  604.     endloop.  
  605.     "執行操作  
  606.     read table lt_retmsg with key type = 'E'.  
  607.     if sy-subrc <> 0.  
  608.       if goodsmvt_item[] is not initial.  
  609.         l_flag = 'F'.  
  610.         call function 'BAPI_GOODSMVT_CREATE'  
  611.           EXPORTING  
  612.             goodsmvt_header  = goodsmvt_header  
  613.             goodsmvt_code    = goodsmvt_code  
  614.           IMPORTING  
  615.             goodsmvt_headret = goodsmvt_headret  
  616.             materialdocument = materialdocument  
  617.             matdocumentyear  = matdocumentyear  
  618.           TABLES  
  619.             goodsmvt_item    = goodsmvt_item  
  620.             return           = return.  
  621.         read table return with key type = 'E' into w_return.  
  622.         if sy-subrc <> 0.  
  623.           call function 'BAPI_TRANSACTION_COMMIT'  
  624.             EXPORTING  
  625.               wait = 'X'.  
  626.           if materialdocument is not initial.  
  627.             l_flag = 'S'.  
  628.             l_matdoc = materialdocument.  
  629.           endif.  
  630.         else.  
  631.           call function 'BAPI_TRANSACTION_ROLLBACK'.  
  632.           perform writepixtranlog tables return using l_tran_nbr.  
  633.         endif.  
  634.         update zwttran set flag = l_flag matdoc = l_matdoc where tran_nbr = l_tran_nbr.  
  635.       endif.  
  636.     else.  
  637.       update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.  
  638.       perform writepixtranlog tables lt_retmsg using l_tran_nbr.  
  639.     endif.  
  640.     "&--------------------------------------------------------------------------------------------------------------&  
  641.     "& END 轉包沖銷  
  642.     "&--------------------------------------------------------------------------------------------------------------&  
  643.     "&--------------------------------------------------------------------------------------------------------------&  
  644.     "& BEGIN 盤點  
  645.     "&--------------------------------------------------------------------------------------------------------------&  
  646.     data:l_physinv_head type bapi_physinv_create_head,  
  647.          lt_physinv_item   like table of bapi_physinv_create_items with header line.  
  648.     data:it_count_item like table of bapi_physinv_count_items with header line,  
  649.          lt_temp like table of bapi_physinv_count_items with header line,  
  650.          l_invdoc type iblnr,  
  651.          l_nblnr(10) type c,  
  652.          l_count type i.  
  653.     "&--------------------------------------------盤點-------------------------------------------------------------&  
  654.     "初始化  
  655.     clear:l_tran_nbr,l_flag,l_matdoc,l_physinv_head,lt_physinv_item, lt_retmsg,it_count_item,lt_temp,l_invdoc,l_nblnr,l_count.  
  656.     refresh:lt_physinv_item, lt_retmsg,lt_temp,it_count_item.  
  657.     "盤點單頭  
  658.     l_physinv_head-plant = g_plant.  
  659.     l_physinv_head-stge_loc = g_stge_loc.  
  660.     l_physinv_head-doc_date = sy-datum.  
  661.     l_physinv_head-plan_date = sy-datum.  
  662.     loop at pt_tran where  ( ( tran_type = '300' and ( tran_code = '04' or tran_code = '01') and actn_code = '14' )  
  663.       or ( tran_type = '300' and tran_code = '04' and actn_code = '' ) ) and invn_adjmt_qty > 0 and tran_nbr = t_tran_nbr-tran_nbr.  
  664.       clear:lt_temp.  
  665.       l_tran_nbr = pt_tran-tran_nbr.  
  666.       "盤點單計數  
  667.       lt_temp-material = pt_tran-matnr.  
  668.       case pt_tran-invn_adjmt_type.  
  669.         when 'A'.  
  670.           lt_temp-entry_qnt = pt_tran-invn_adjmt_qty.  
  671.         when 'S'.  
  672.           lt_temp-entry_qnt = - pt_tran-invn_adjmt_qty.  
  673.       endcase.  
  674.       collect lt_temp.  
  675.     endloop.  
  676.     loop at lt_temp.  
  677.       add 1 to l_count.  
  678.       "初始化  
  679.       clear:lt_mard.  
  680.       refresh:lt_mard.  
  681.       "擷取目前庫存  
  682.       call function 'ZLC_GET_STOCK'  
  683.         EXPORTING  
  684.           p_matnr = lt_temp-material  
  685.           p_werks = g_plant  
  686.           p_lgort = g_stge_loc  
  687.         TABLES  
  688.           pt_mard = lt_mard.  
  689.       read table lt_mard index 1.  
  690.       "盤點處理  
  691.       if sy-subrc = 0.  
  692.         "盤點單物料  
  693.         lt_physinv_item-material = lt_temp-material.  
  694.         append lt_physinv_item.  
  695.         "盤點單計數  
  696.         it_count_item-item = l_count.  
  697.         it_count_item-material = lt_temp-material.  
  698.         it_count_item-entry_qnt = lt_mard-labst + lt_temp-entry_qnt.  
  699.         clear:l_mara.  
  700.         call function 'ZLC_GET_MARA'  
  701.           EXPORTING  
  702.             p_matnr = lt_temp-material  
  703.           IMPORTING  
  704.             p_mara  = l_mara.  
  705.         if l_mara is not initial.  
  706.           it_count_item-entry_uom = l_mara-meins.  
  707.         endif.  
  708.         append it_count_item.  
  709.       else.  
  710.         clear:lt_retmsg.  
  711.         lt_retmsg-type = 'E'.  
  712.         concatenate '商品' lt_temp-material ',' g_plant ',' g_stge_loc  '不存在!' into lt_retmsg-message.  
  713.         append lt_retmsg.  
  714.       endif.  
  715.     endloop.  
  716.     read table lt_retmsg with key type = 'E'.  
  717.     if sy-subrc <> 0.  
  718.       if it_count_item[] is not initial.  
  719.         l_flag = 'F'.  
  720.         clear:lt_retmsg.  
  721.         refresh:lt_retmsg.  
  722.         call function 'BAPI_MATPHYSINV_CREATE_MULT'"建立盤點憑證  
  723.         exporting  
  724.           head   = l_physinv_head  
  725.         tables  
  726.           items  = lt_physinv_item  
  727.           return = lt_retmsg.  
  728.         loop at lt_retmsg.  
  729.           if lt_retmsg-type = 'S' and lt_retmsg-id = 'M7'  
  730.           and lt_retmsg-number = '710'.  
  731.             l_nblnr = lt_retmsg-message_v1.  
  732.             shift l_nblnr right deleting trailing space.  
  733.             overlay l_nblnr with '0000000000'.  
  734.             l_invdoc = l_nblnr.  
  735.           endif.  
  736.         endloop.  
  737.         read table lt_retmsg with key type = 'E'.  
  738.         if sy-subrc <> 0.  
  739.           call function 'BAPI_TRANSACTION_COMMIT'  
  740.             EXPORTING  
  741.               wait = 'X'.  
  742.           clear:lt_retmsg.  
  743.           refresh:lt_retmsg.  
  744.           call function 'BAPI_MATPHYSINV_COUNT'"輸入盤點數量  
  745.           exporting  
  746.             physinventory = l_invdoc  
  747.             fiscalyear    = sy-datum+0(4)  
  748.             count_date    = sy-datum  
  749.           tables  
  750.             items         = it_count_item  
  751.             return        = lt_retmsg.  
  752.           read table lt_retmsg with key type = 'E'.  
  753.           if sy-subrc <> 0.  
  754.             call function 'BAPI_TRANSACTION_COMMIT'  
  755.               EXPORTING  
  756.                 wait = 'X'.  
  757.             clear:lt_retmsg.  
  758.             refresh:lt_retmsg.  
  759.             call function 'BAPI_MATPHYSINV_POSTDIFF'"處理差異  
  760.             exporting  
  761.               physinventory = l_invdoc  
  762.               fiscalyear    = sy-datum+0(4)  
  763.               pstng_date    = sy-datum  
  764.             tables  
  765.               return        = lt_retmsg.  
  766.             read table lt_retmsg with key type = 'E'.  
  767.             if sy-subrc <> 0.  
  768.               call function 'BAPI_TRANSACTION_COMMIT'  
  769.                 EXPORTING  
  770.                   wait = 'X'.  
  771.               loop at lt_retmsg.  
  772.                 if lt_retmsg-type = 'S' and lt_retmsg-id = 'M7'  
  773.                 and lt_retmsg-number = '716'.  
  774.                   l_nblnr = lt_retmsg-message_v2.  
  775.                   shift l_nblnr right deleting trailing space.  
  776.                   overlay l_nblnr with '0000000000'.  
  777.                   l_matdoc = l_nblnr.  
  778.                 endif.  
  779.               endloop.  
  780.               l_flag = 'S'.  
  781.             else.  
  782.               call function 'BAPI_TRANSACTION_ROLLBACK'.  
  783.             endif.  
  784.           else.  
  785.             call function 'BAPI_TRANSACTION_ROLLBACK'.  
  786.           endif.  
  787.         else.  
  788.           call function 'BAPI_TRANSACTION_ROLLBACK'.  
  789.         endif.  
  790.         update zwttran set flag = l_flag matdoc = l_matdoc iblnr = l_invdoc where tran_nbr = l_tran_nbr.  
  791.         if l_flag = 'F'.  
  792.           perform writepixtranlog tables lt_retmsg using l_tran_nbr.  
  793.         endif.  
  794.       endif.  
  795.     else.  
  796.       update zwttran set flag = 'F' where tran_nbr = l_tran_nbr.  
  797.       perform writepixtranlog tables lt_retmsg using l_tran_nbr.  
  798.     endif.  
  799.     "  &--------------------------------------------------------------------------------------------------------------&  
  800.     "  & end 報損、報溢、盤點  
  801.     "  &--------------------------------------------------------------------------------------------------------------&  
  802.   endloop.  

繼續閱讀