*&---------------------------------------------------------------------*
*& Report ZFI_R_29_AR
*&
*&---------------------------------------------------------------------*
*&Changing History:
*& DEVK901971 CHEN.JR 100818_Get correct order quantity
*&---------------------------------------------------------------------*
report zfi_r_29_ar message-id zfi
line-size 500 line-count 34(2) no standard page heading.
include zbc_alv_reuse.
*&---------------------------------------------------------------------*
* Global Data Declarations: *
*&---------------------------------------------------------------------*
tables: bsis,likp,lips.
data: item_num like bsis-buzei,
item_mod type i,
g_buzei type i,
g_dmbtr like bsis-dmbtr,
g_vbeln like vbfa-vbeln,
g_posnn like vbfa-posnn,
g_tmp_vbeln like lips-vbeln,
g_tmp_vgbel like lips-vgbel,
g_tmp_vgpos like lips-vgpos.
constants:c_package_size type i value 300.
data: l_cursor type cursor,
l_xflag_end_of_data.
*&--------------------------------------------------------------------*
* internal tables
*&--------------------------------------------------------------------*
types: begin of t_bsis,
belnr like bsis-belnr,
gjahr like bsis-gjahr,
buzei like bsis-buzei,
budat like bsis-budat,
shkzg like bsis-shkzg,
xblnr like bsis-xblnr,
blart like bsis-blart,
dmbtr like bsis-dmbtr,
pswsl like bsis-pswsl,
vbeln like lips-vbeln,
posnr like lips-posnr,
vgbel like lips-vgbel, "销售凭证
vgpos like lips-vgpos, "销售凭证项目
buzei_rv like bsis-buzei,
vbeln_rv like vbrp-vbeln,
posnr_rv like vbrp-posnr,
wrbtr type bsis-wrbtr, "成本价
end of t_bsis.
data: gt_bsis_wl type table of t_bsis initial size 300 with header line,
gt_bsis_wl_sum type table of t_bsis initial size 0 with header line,
gt_bsis_rv type table of t_bsis initial size 300 with header line,
gt_bsis_rv_sum type table of t_bsis initial size 0 with header line,
gt_bsis_others type table of t_bsis initial size 0 with header line.
data: begin of gt_bsis occurs 0,
belnr like bsis-belnr,
xblnr like bsis-xblnr,
end of gt_bsis.
types: begin of t_output,
selfd type c,
vbeln like lips-vbeln,
posnr like lips-posnr,
vgbel like lips-vgbel, "销售凭证
vgpos like lips-vgpos, "销售凭证项目
matnr like lips-matnr, "物料号
arktx like lips-arktx, "物料描述
lfimg like lips-lfimg, "交货数量
wadat_ist like likp-wadat_ist,
vkorg like vkdfif-vkorg,
kunnr like vkdfif-kunnr,
name1 like vkdfif-name1,
netwr like vkdfif-netwr,
waerk like vkdfif-waerk, "货币
netpr type vbap-netpr, "净价
kpein type vbap-kpein, "销售单位
kwmeng type vbap-kwmeng, "订单数量
*{ INSERT DEVK901971
zmeng type vbap-zmeng, "目标数量
*} INSERT
mwsbp like vbap-mwsbp, "税额
belnr like bsis-belnr,
buzei like bsis-buzei,
wrbtr type bsis-wrbtr, "成本价
pswsl like bsis-pswsl, "成本货币
gjahr like bsis-gjahr,
vtext type tvkot-vtext, "销售机构名称
dmbtr like bsis-dmbtr,
vbeln_rv like vbrp-vbeln,
posnr_rv like vbrp-posnr,
fkimg like vbrp-fkimg, "库存单位开票量
wavwr like vbrp-wavwr, "开票金额
end of t_output.
data: gt_output type table of t_output initial size 0 with header line,
gt_lips type table of t_output initial size 0 with header line.
*&---------------------------------------------------------------------*
* Selection Screen:
*&---------------------------------------------------------------------*
selection-screen begin of block scr01 with frame title t1.
select-options: so_bukrs for bsis-bukrs no-extension no intervals obligatory,
so_budat for bsis-budat no-extension, "出具发票日期
so_hkont for bsis-hkont obligatory,
* added by zhuboyi,20110601
so_kunag for likp-kunag,
so_vgbel for lips-vgbel,
so_matnr for lips-matnr,
* end by zhuboyi
so_vbeln for lips-vbeln,"交货单号
so_xblnr for bsis-xblnr,"发票号码
so_belnr for bsis-belnr.
*
selection-screen end of block scr01.
*公司代码的权限控制
at selection-screen on so_bukrs.
authority-check object 'F_BKPF_BUK' id 'BUKRS' field so_bukrs-low
id 'ACTVT' field '03'.
if sy-subrc ne 0.
message e007(zfi) with '你没有权限查看' so_bukrs-low '公司代码的数据!'.
exit.
endif.
*&---------------------------------------------------------------------*
* INITIALIZATION
*&---------------------------------------------------------------------*
initialization.
t1 = '条件'.
perform set_so_value.
*&--------------------------------------------------------------------*
* START-OF-SELECTION:
*&--------------------------------------------------------------------*
start-of-selection.
perform frm_get_document.
perform frm_collect_bsis_wl.
perform frm_collect_bsis_rv.
perform frm_cleaning.
perform frm_gather_data.
*&--------------------------------------------------------------------*
* END-OF-SELECTION:
*&--------------------------------------------------------------------*
end-of-selection.
refresh gt_list_top_of_page[].
perform e04_comment_build using: 'S' '发出商品科目余额' ''.
perform build_fieldcat.
perform build_eventcat.
perform alv_display_grid tables gt_output[].
*&---------------------------------------------------------------------*
*& Form frm_get_document
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form frm_get_document.
open cursor with hold l_cursor for
select belnr gjahr budat buzei
shkzg xblnr blart dmbtr pswsl
from bsis
where bukrs in so_bukrs
and hkont in so_hkont
and budat in so_budat
and blart eq 'WL'
and xblnr in so_xblnr
and belnr in so_belnr
and buzid = 'S'.
while l_xflag_end_of_data = space.
fetch next cursor l_cursor
appending corresponding fields of table gt_bsis_wl
package size c_package_size.
if sy-subrc <> 0.
l_xflag_end_of_data = 'X'.
endif.
endwhile.
close cursor l_cursor.
clear l_xflag_end_of_data.
loop at gt_bsis_wl.
move-corresponding gt_bsis_wl to gt_bsis.
collect gt_bsis.
endloop.
sort gt_bsis by xblnr belnr descending.
delete adjacent duplicates from gt_bsis comparing xblnr.
loop at gt_bsis_wl.
read table gt_bsis with key belnr = gt_bsis_wl-belnr.
if sy-subrc ne 0.
delete gt_bsis_wl.
endif.
endloop.
open cursor with hold l_cursor for
select belnr gjahr buzei budat
shkzg xblnr blart dmbtr pswsl wrbtr
from bsis
where bukrs in so_bukrs
and hkont in so_hkont
and budat in so_budat
and blart = 'RV'
and xblnr in so_xblnr
and belnr in so_belnr. "
while l_xflag_end_of_data = space.
fetch next cursor l_cursor
appending corresponding fields of table gt_bsis_rv
package size c_package_size.
if sy-subrc <> 0.
l_xflag_end_of_data = 'X'.
endif.
endwhile.
close cursor l_cursor.
clear l_xflag_end_of_data.
endform. "frm_get_document
*&---------------------------------------------------------------------*
*& Form frm_collect_bsis_wl
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form frm_collect_bsis_wl.
* DATA:buzei TYPE i.
sort gt_bsis_wl by xblnr belnr buzei.
* LOOP AT gt_bsis_wl.
* AT NEW belnr.
* buzei = 1.
* ENDAT.
* gt_bsis_wl-buzei = buzei.
* buzei = buzei + 1.
* MODIFY gt_bsis_wl.
* ENDLOOP.
loop at gt_bsis_wl.
if gt_bsis_wl-shkzg eq 'H'.
gt_bsis_wl-dmbtr = -1 * gt_bsis_wl-dmbtr.
endif.
move: gt_bsis_wl-xblnr to gt_bsis_wl_sum-xblnr,
gt_bsis_wl-buzei to gt_bsis_wl_sum-buzei,
gt_bsis_wl-dmbtr to gt_bsis_wl_sum-dmbtr,
gt_bsis_wl-pswsl to gt_bsis_wl_sum-pswsl.
collect gt_bsis_wl_sum.
clear gt_bsis_wl_sum.
endloop.
delete gt_bsis_wl_sum where dmbtr eq 0.
sort gt_bsis_wl by belnr buzei descending.
loop at gt_bsis_wl_sum.
data tabix type i.
read table gt_bsis_wl with key xblnr = gt_bsis_wl_sum-xblnr
buzei = gt_bsis_wl_sum-buzei.
if sy-subrc = 0.
move: gt_bsis_wl-gjahr to gt_bsis_wl_sum-gjahr,
gt_bsis_wl-belnr to gt_bsis_wl_sum-belnr.
endif.
select single vbeln vbel2 as vgbel posn2 as vgpos
into corresponding fields of gt_bsis_wl_sum
from bseg
where bukrs in so_bukrs
and belnr eq gt_bsis_wl_sum-belnr
and gjahr eq gt_bsis_wl_sum-gjahr
and buzei eq gt_bsis_wl_sum-buzei
and buzid = 'S'
.
if gt_bsis_wl_sum-vbeln is initial.
move gt_bsis_wl_sum-xblnr to gt_bsis_wl_sum-vbeln.
endif.
if gt_bsis_wl_sum-vbeln ne g_tmp_vbeln or
gt_bsis_wl_sum-vgbel ne g_tmp_vgbel or
gt_bsis_wl_sum-vgpos ne g_tmp_vgpos.
refresh gt_lips.
item_num = 1.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = gt_bsis_wl_sum-vbeln
importing
output = gt_bsis_wl_sum-vbeln.
select vbeln posnr uecha
into corresponding fields of table gt_lips
from lips
where vbeln eq gt_bsis_wl_sum-vbeln
and lfimg ne 0
and vgbel eq gt_bsis_wl_sum-vgbel
and vgpos eq gt_bsis_wl_sum-vgpos.
elseif gt_bsis_wl_sum-vbeln eq g_tmp_vbeln and
gt_bsis_wl_sum-vgbel eq g_tmp_vgbel and
gt_bsis_wl_sum-vgpos eq g_tmp_vgpos.
add 1 to item_num.
endif.
move: gt_bsis_wl_sum-vbeln to g_tmp_vbeln,
gt_bsis_wl_sum-vgbel to g_tmp_vgbel,
gt_bsis_wl_sum-vgpos to g_tmp_vgpos,
item_num to tabix.
read table gt_lips index tabix.
if sy-subrc = 0.
move: gt_lips-posnr to gt_bsis_wl_sum-posnr.
endif.
modify gt_bsis_wl_sum.
clear gt_bsis_wl_sum.
endloop.
endform. "frm_collect_bsis_wl
*&---------------------------------------------------------------------*
*& Form frm_collect_bsis_rv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form frm_collect_bsis_rv.
sort gt_bsis_rv by belnr buzei.
loop at gt_bsis_rv.
at new belnr.
clear g_buzei.
refresh gt_lips.
select vbrp~vbeln as vbeln_rv
vbrp~posnr as posnr_rv
vbrp~vgbel as vbeln
vbrp~vgpos as posnr
vbrp~aubel as vgbel
vbrp~aupos as vgpos
vbrp~fkimg as fkimg
vbrp~wavwr
into corresponding fields of table gt_lips
from vbrp
join vbrk on vbrp~vbeln eq vbrk~vbeln
where vbrp~vbeln eq gt_bsis_rv-belnr
* 批次拆分
and vbrp~fkimg ne 0
* AND vbrk~fkdat LE so_budat-low
.
"批次拆分处理
* LOOP AT GT_LIPS.
*
* IF GT_LIPS-FKIMG = 0.
*
* IF GT_LIPS-posnr_rv+0(1) = '9'.
*
* DELETE GT_LIPS WHERE vbeln_rv = GT_LIPS-vbeln_rv
* AND vgbel = GT_LIPS-vgbel
* AND vgpos = GT_LIPS-vgpos
* AND posnr < 1000.
*
*
* ELSE.
*
* DELETE GT_LIPS WHERE vbeln_rv = GT_LIPS-vbeln_rv
* AND vgbel = GT_LIPS-vgbel
* AND vgpos = GT_LIPS-vgpos
* .
*
*
* ENDIF.
*
* ENDIF.
* ENDLOOP.
endat.
if gt_lips[] is initial.
delete gt_bsis_rv.
continue.
endif.
add 1 to g_buzei.
read table gt_lips index g_buzei.
if sy-subrc = 0.
if gt_lips-vbeln eq gt_lips-vgbel and
gt_lips-posnr eq gt_lips-vgpos.
select single vbeln posnn from vbfa
into (g_vbeln,g_posnn)
where vbelv eq gt_lips-vbeln
and posnv eq gt_lips-posnr
and vbtyp_n in ('J','T')
and rfmng ne 0.
if sy-subrc eq 0.
move: g_vbeln to gt_lips-vbeln,
g_posnn to gt_lips-posnr.
clear: g_vbeln, g_posnn.
endif.
endif.
move: gt_lips-vbeln to gt_bsis_rv-vbeln,
gt_lips-posnr to gt_bsis_rv-posnr,
gt_lips-vbeln_rv to gt_bsis_rv-vbeln_rv,
gt_lips-posnr_rv to gt_bsis_rv-posnr_rv.
endif.
if gt_bsis_rv-shkzg eq 'H'.
gt_bsis_rv-dmbtr = -1 * gt_bsis_rv-dmbtr.
endif.
move: gt_bsis_rv-vbeln to gt_bsis_rv_sum-vbeln,
gt_bsis_rv-posnr to gt_bsis_rv_sum-posnr,
gt_bsis_rv-dmbtr to gt_bsis_rv_sum-dmbtr,
gt_bsis_rv-pswsl to gt_bsis_rv_sum-pswsl.
collect gt_bsis_rv_sum.
clear gt_bsis_rv_sum.
modify gt_bsis_rv transporting vbeln posnr dmbtr
vbeln_rv posnr_rv.
clear gt_bsis_rv.
endloop.
delete gt_bsis_rv_sum where dmbtr eq 0.
endform. "frm_collect_bsis_rv
*&---------------------------------------------------------------------*
*& Form frm_cleaning
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form frm_cleaning.
sort gt_bsis_rv_sum by vbeln posnr.
loop at gt_bsis_wl_sum.
read table gt_bsis_rv_sum with key vbeln = gt_bsis_wl_sum-vbeln
posnr = gt_bsis_wl_sum-posnr
binary search.
if sy-subrc = 0.
g_dmbtr = gt_bsis_wl_sum-dmbtr + gt_bsis_rv_sum-dmbtr.
if g_dmbtr is initial.
delete gt_bsis_rv_sum where vbeln = gt_bsis_wl_sum-vbeln
and posnr = gt_bsis_wl_sum-posnr.
delete gt_bsis_wl_sum.
else.
modify gt_bsis_wl_sum transporting vbeln posnr gjahr belnr.
endif.
endif.
endloop.
endform. "frm_cleaning
*&---------------------------------------------------------------------*
*& Form frm_gather_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form frm_gather_data.
loop at gt_bsis_wl_sum.
move-corresponding gt_bsis_wl_sum to gt_output.
select single matnr lfimg arktx
into corresponding fields of gt_output
from lips
where vbeln eq gt_output-vbeln
and posnr eq gt_output-posnr
and lfimg ne 0.
select single vkorg kunag as kunnr wadat_ist
into corresponding fields of gt_output
from likp
where vbeln eq gt_output-vbeln.
perform frm_get_other_field changing gt_output.
append gt_output.
clear gt_output.
endloop.
sort gt_bsis_rv by vbeln posnr.
loop at gt_bsis_rv_sum.
read table gt_bsis_rv with key vbeln = gt_bsis_rv_sum-vbeln
posnr = gt_bsis_rv_sum-posnr
binary search.
if sy-subrc eq 0.
move: gt_bsis_rv-vbeln_rv to gt_bsis_rv_sum-vbeln_rv,
gt_bsis_rv-posnr_rv to gt_bsis_rv_sum-posnr_rv,
gt_bsis_rv-belnr to gt_bsis_rv_sum-belnr,
gt_bsis_rv-buzei to gt_bsis_rv_sum-buzei,
gt_bsis_rv-gjahr to gt_bsis_rv_sum-gjahr.
endif.
move-corresponding gt_bsis_rv_sum to gt_output.
select single fkimg as lfimg
matnr arktx
aubel as vgbel
aupos as vgpos
into corresponding fields of gt_output
from vbrp
where vbeln eq gt_output-vbeln_rv
and posnr eq gt_output-posnr_rv
and fkimg ne 0.
select single vkorg kunag as kunnr
fkdat as wadat_ist
into corresponding fields of gt_output
from vbrk
where vbeln eq gt_output-vbeln_rv.
perform frm_get_other_field changing gt_output.
append gt_output.
clear gt_output.
endloop.
*
* LOOP AT gt_bsis_others.
* MOVE-CORRESPONDING gt_bsis_others TO gt_output.
* APPEND gt_output.
* CLEAR gt_output.
* ENDLOOP.
delete gt_output where kunnr is initial.
loop at gt_output where vgbel is initial
and kunnr is not initial.
select single vgbel
into corresponding fields of gt_output
from lips
where vbeln eq gt_output-vbeln
and lfimg ne 0.
endloop.
loop at gt_output.
"删除批次拆分重复项目
if gt_output-posnr+0(1) = '9'.
delete gt_output where
vbeln = gt_output-vbeln
and matnr = gt_output-matnr
and posnr+0(1) = '0'
.
modify gt_output.
endif.
endloop.
endform. "frm_gather_data
*&---------------------------------------------------------------------*
*& Form frm_get_other_field
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PS_OUTPUT Work Area of GT_OUTPUT
*----------------------------------------------------------------------*
form frm_get_other_field changing ps_output like line of gt_output.
select single netpr kpein kwmeng zmeng mwsbp waerk
into corresponding fields of ps_output
from vbap
where vbeln = ps_output-vgbel
and posnr = ps_output-vgpos.
*{ INSERT DEVK901971
if ps_output-kwmeng is initial.
move ps_output-zmeng to ps_output-kwmeng.
endif.
*} INSERT
ps_output-netpr = sign( ps_output-dmbtr ) * ps_output-netpr /
ps_output-kpein * ps_output-lfimg.
ps_output-mwsbp = sign( ps_output-dmbtr ) * ps_output-mwsbp /
ps_output-kwmeng * ps_output-lfimg.
ps_output-lfimg = sign( ps_output-dmbtr ) * ps_output-lfimg.
select single name1
into corresponding fields of ps_output
from kna1
where kunnr eq ps_output-kunnr.
call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
exporting
input = ps_output-kunnr
importing
output = ps_output-kunnr.
call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
exporting
input = ps_output-vbeln
importing
output = ps_output-vbeln.
call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
exporting
input = ps_output-matnr
importing
output = ps_output-matnr.
call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
exporting
input = ps_output-belnr
importing
output = ps_output-belnr.
endform. "frm_get_other_field
*&--------------------------------------------------------------------*
*& Form build_fieldcat
*&--------------------------------------------------------------------*
form build_fieldcat.
data : col_pos type i value 1.
clear fieldcat.
refresh fieldcat.
ps_layout-zebra = 'X'.
ps_layout-get_selinfos = 'X'.
ps_layout-colwidth_optimize = 'X'.
ps_layout-detail_popup = 'X'.
ps_layout-no_keyfix = ''.
ps_layout-colwidth_optimize = 'X'.
ps_layout-box_fieldname = 'SELFD'.
*-----------------------------------------------------------------------
*& Rerference
*& 1_col_pos 2_fieldname 3_key 4_no_zero 5_scrtext_l
*-----------------------------------------------------------------------
*---------------------------- 1 2 3 4 5
perform fieldcat_int using col_pos 'KUNNR' 'X' '' '客户编号'.
perform fieldcat_int using col_pos 'NAME1' 'X' '' '客户名称'.
perform fieldcat_int using col_pos 'VKORG' 'X' '' '销售组织'.
perform fieldcat_int using col_pos 'VBELN' 'X' '' '发货单号'.
perform fieldcat_int using col_pos 'POSNR' 'X' '' '发货单行项目'.
perform fieldcat_int using col_pos 'VGBEL' ' ' '' '销售订单'.
perform fieldcat_int using col_pos 'VGPOS' ' ' '' '订单行项目'.
perform fieldcat_int using col_pos 'MATNR' ' ' '' '物料'.
perform fieldcat_int using col_pos 'ARKTX' ' ' '' '物料描述'.
perform fieldcat_int using col_pos 'LFIMG' ' ' '' '发货数量'.
perform fieldcat_int using col_pos 'NETPR' ' ' '' '净价值'.
perform fieldcat_int using col_pos 'MWSBP' ' ' '' '税额'.
perform fieldcat_int using col_pos 'WAERK' ' ' '' '货币'.
perform fieldcat_int using col_pos 'DMBTR' ' ' '' '成本'.
perform fieldcat_int using col_pos 'PSWSL' ' ' '' '货币'.
perform fieldcat_int using col_pos 'WADAT_IST' ' ' '' '发货日期'.
perform fieldcat_int using col_pos 'BELNR' ' ' '' '会计凭证'.
perform fieldcat_int using col_pos 'BUZEI' ' ' '' '凭证项目'.
perform fieldcat_int using col_pos 'GJAHR' ' ' '' '会计年度'.
endform. "build_fieldcat
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->R_UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
form user_command using r_ucomm like sy-ucomm
rs_selfield type slis_selfield.
case r_ucomm.
when '&IC1'.
read table gt_output index rs_selfield-tabindex.
check sy-subrc = 0.
case rs_selfield-fieldname.
when 'BELNR'.
set parameter id 'BLN' field gt_output-belnr.
set parameter id 'BUK' field so_bukrs-low.
set parameter id 'GJR' field gt_output-gjahr.
call transaction 'FB03' and skip first screen .
when others.
endcase.
endcase.
endform. "user_command
*&---------------------------------------------------------------------*
*& Form set_so_value
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form set_so_value.
* Default Account
move: 'I' to so_hkont-sign,
'BT' to so_hkont-option,
'0014060000' to so_hkont-low,
'0014069999' to so_hkont-high.
append so_hkont.
* Default Comp.
move '1000' to so_bukrs-low.
append so_bukrs.
* Default Date
move: 'I' to so_budat-sign,
'BT' to so_budat-option,
'00000000' to so_budat-low.
* GET Pre-Month
call function 'CCM_GO_BACK_MONTHS'
exporting
currdate = sy-datum "EG:20100101
backmonths = '001'
importing
newdate = so_budat-high. "EG:20091201
* Get Last day of pre-month
call function 'RP_LAST_DAY_OF_MONTHS'
exporting
day_in = so_budat-high "EG:20091201
importing
last_day_of_month = so_budat-high "EG:20091231
exceptions
day_in_no_date = 1
others = 2.
append so_budat.
endform. "set_so_value
********************************************************************************************************
SO_BELNR 凭证编号
SO_BUDAT 过帐日期
SO_BUKRS 公司代码
SO_HKONT 总帐帐目
SO_KUNAG 售达方
SO_MATNR 物料
SO_VBELN ?...
SO_VGBEL 参考凭证
SO_XBLNR 参照
________ ______________________________