FUNCTION zmm_jsd_creatpo.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(JSD) TYPE ZMM_YS_JSD_HEAD-JSD
*" EXPORTING
*" VALUE(E_FLAG) TYPE CHAR1
*" VALUE(E_INFO) TYPE TEXT60
*" VALUE(EBELN) TYPE EBELN
*"----------------------------------------------------------------------
* 根据结算单生成采购订单 根据税码、物料组 分成多个行项目
*"----------------------------------------------------------------------
DATA : header LIKE bapimepoheader,
headerx LIKE bapimepoheaderx.
DATA: it_return TYPE TABLE OF bapiret2 WITH HEADER LINE,
it_poitem TYPE TABLE OF bapimepoitem WITH HEADER LINE,
it_poitemx TYPE TABLE OF bapimepoitemx WITH HEADER LINE,
it_poschedule TYPE TABLE OF bapimeposchedule WITH HEADER LINE,
it_poschedulex TYPE TABLE OF bapimeposchedulx WITH HEADER LINE,
it_poaccount TYPE TABLE OF bapimepoaccount WITH HEADER LINE,
it_poaccountx TYPE TABLE OF bapimepoaccountx WITH HEADER LINE,
it_polimits TYPE TABLE OF bapiesuhc WITH HEADER LINE,
it_povalues TYPE TABLE OF bapiesklc WITH HEADER LINE,
it_poservice TYPE TABLE OF bapiesllc WITH HEADER LINE.
DATA ext_line TYPE n LENGTH 10.
DATA line_no TYPE n LENGTH 10.
DATA pckg_no TYPE n LENGTH 10.
DATA po_item TYPE n LENGTH 5. "行项目编号
DATA serial_no TYPE n LENGTH 2.
DATA short_text TYPE c LENGTH 40. "项目短文本
DATA head_zterm TYPE lfm1-zterm. "付款条件
DATA: gw_jsd_head LIKE zmm_ys_jsd_head,
gw_jsd_item LIKE zmm_ys_jsd_item,
gt_jsd_item LIKE TABLE OF zmm_ys_jsd_item.
"结算单暂存结构
TYPES: BEGIN OF ty_ys_jsd,
beznk LIKE zmm_ys_jsd_item-beznk, "税码
matkl LIKE asmd-matkl, "物料组
asnum LIKE zmm_ys_jsd_item-asnum, "服务编号
jsd LIKE zmm_ys_jsd_item-jsd,
jsdhh LIKE zmm_ys_jsd_item-jsdhh,
qd LIKE zmm_ys_jsd_item-qd,
zd LIKE zmm_ys_jsd_item-zd,
smatnr LIKE zmm_ys_jsd_item-smatnr,
kostl LIKE zmm_ys_jsd_item-kostl,
shdw LIKE zmm_ys_jsd_item-shdw,
ctype LIKE zmm_ys_jsd_item-ctype,
menge LIKE zmm_ys_jsd_item-menge,
meins LIKE zmm_ys_jsd_item-meins,
hsdj LIKE zmm_ys_jsd_item-hsdj,
bhsdj LIKE zmm_ys_jsd_item-bhsdj,
cbckk LIKE zmm_ys_jsd_item-cbckk,
se LIKE zmm_ys_jsd_item-se,
bhsj LIKE zmm_ys_jsd_item-bhsj,
hsj LIKE zmm_ys_jsd_item-hsj,
beizhu LIKE zmm_ys_jsd_item-beizhu.
TYPES: END OF ty_ys_jsd.
DATA: gw_jsd_date TYPE ty_ys_jsd,
gt_jsd_date TYPE TABLE OF ty_ys_jsd.
DATA: g_jsdlx TYPE zmm_ys_jsd_head-jsdlx, "结算单类型
g_lxms TYPE zmm_ys_jsdlx-lxms. "类型描述
e_flag = ‘0‘.
e_info = ‘采购订单创建成功!‘.
CLEAR:gw_jsd_head,gt_jsd_item.
"结算单抬头
SELECT * FROM zmm_ys_jsd_head INTO gw_jsd_head WHERE jsd = jsd.
IF sy-subrc NE 0.
e_flag = ‘1‘.
e_info = ‘该结算单不存在!‘.
ELSE.
IF gw_jsd_head-ebeln IS NOT INITIAL.
e_flag = ‘2‘.
e_info = ‘该结算单已经创建采购订单!‘.
ENDIF.
ENDIF.
"取得结算单类型描述
g_jsdlx = gw_jsd_head-jsdlx.
SELECT SINGLE lxms FROM zmm_ys_jsdlx INTO g_lxms WHERE jsdlx EQ g_jsdlx.
"结算单行项目
SELECT * FROM zmm_ys_jsd_item INTO TABLE gt_jsd_item WHERE jsd = jsd.
"根据 供应商 与采购组织 找到付款条件
SELECT SINGLE zterm FROM lfm1 INTO head_zterm WHERE lifnr = gw_jsd_head-lifnr AND ekorg = ‘1000‘.
ENDSELECT.
IF e_flag NE ‘0‘.
RETURN.
ENDIF.
CLEAR:gw_jsd_item, gt_jsd_date.
"数据库数据存入自建结构
LOOP AT gt_jsd_item INTO gw_jsd_item.
CLEAR: gw_jsd_date,gw_jsd_date-matkl.
MOVE-CORRESPONDING gw_jsd_item TO gw_jsd_date.
"根据服务编号取得物料组
SELECT SINGLE matkl FROM asmd INTO gw_jsd_date-matkl WHERE asnum EQ gw_jsd_date-asnum.
IF sy-subrc <> 0.
gw_jsd_date-matkl = ‘无物料组‘.
ENDIF.
APPEND gw_jsd_date TO gt_jsd_date.
ENDLOOP.
"根据税码、物料组排序。
SORT gt_jsd_date BY beznk matkl.
***采购订单抬头
header-comp_code = ‘1000‘. "公司代码EKKO-BUKRS
header-doc_type = ‘YS‘. "YS(运输服务采购订单)
header-creat_date = sy-datum . "创建日期
header-vendor = gw_jsd_head-lifnr.
header-pmnttrms = head_zterm. "付款条件 "0001
header-incoterms2 = jsd. "国际贸易条件 保存结算单号
header-purch_org = ‘1000‘. "采购组织EKKO-EKORG
header-pur_group = ‘900‘. "采购组EKKO-EKGRP:900(服务采购)
header-currency = ‘RMB‘. "货币代码
header-doc_date = sy-datum. "采购凭证日期
headerx-comp_code = ‘X‘.
headerx-doc_type = ‘X‘.
headerx-creat_date = ‘X‘.
headerx-vendor = ‘X‘.
headerx-langu = ‘X‘.
headerx-pmnttrms = ‘X‘ .
headerx-incoterms2 = ‘X‘.
headerx-purch_org = ‘X‘.
headerx-pur_group = ‘X‘.
headerx-currency = ‘X‘.
headerx-doc_date = ‘X‘.
serial_no = ‘00‘. "帐户分配规范序号:服务行
ext_line = ‘0000000000‘. "行号
line_no = ‘0000000001‘. "内部行编号
po_item = ‘00000‘. "行项目号
pckg_no = ‘0000000000‘.
"行项目,数据集中处理
LOOP AT gt_jsd_date INTO gw_jsd_date.
"服务描述
CLEAR:short_text.
CONCATENATE sy-datum+4(2) ‘月_‘ g_lxms INTO short_text.
"采购订单项目
AT NEW matkl.
pckg_no = pckg_no + 1.
po_item = po_item + 10.
ext_line = ‘0000000000‘.
line_no = ‘0000000001‘.
it_poitem-po_item = po_item.
it_poitem-short_text = short_text. "服务描述
it_poitem-plant = ‘1000‘. "工厂EKPO-WERKS
it_poitem-matl_group = gw_jsd_date-matkl."服务主数据中的物料组
it_poitem-tax_code = gw_jsd_date-beznk."结算单中服务对应税码
it_poitem-unlimited_dlv = ‘X‘.
it_poitem-item_cat = ‘D‘.
it_poitem-acctasscat = ‘X‘.
it_poitem-srv_based_iv = ‘X‘.
pckg_no = pckg_no + 1.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = pckg_no
IMPORTING
output = pckg_no.
it_poitem-pckg_no = pckg_no.
APPEND it_poitem.
it_poitemx-po_item = po_item.
it_poitemx-po_itemx = ‘X‘.
it_poitemx-short_text = ‘X‘.
it_poitemx-plant = ‘X‘.
it_poitemx-matl_group = ‘X‘.
it_poitemx-tax_code = ‘X‘.
it_poitemx-unlimited_dlv = ‘X‘.
it_poitemx-item_cat = ‘X‘.
it_poitemx-acctasscat = ‘X‘.
it_poitemx-pckg_no = ‘X‘.
it_poitemx-srv_based_iv = ‘X‘.
APPEND it_poitemx.
"FILL SCHEDULE
it_poschedule-po_item = po_item.
it_poschedule-del_datcat_ext = ‘D‘.
it_poschedule-delivery_date = sy-datum .
it_poschedule-quantity = ‘1‘. "计划数量
* it_poschedule-sched_line = ‘0001‘ .
APPEND it_poschedule.
it_poschedulex-po_item = po_item.
it_poschedulex-po_itemx = ‘X‘.
it_poschedulex-del_datcat_ext = ‘X‘.
it_poschedulex-delivery_date = ‘X‘.
it_poschedulex-sched_line = ‘X‘.
it_poschedulex-sched_linex = ‘X‘.
it_poschedulex-quantity = ‘X‘.
APPEND it_poschedulex.
"服务包数据,一个行项目只有一条此数据
it_poservice-pckg_no = pckg_no.
it_poservice-line_no = line_no.
it_poservice-ext_line = ext_line.
pckg_no = pckg_no + 1.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = pckg_no
IMPORTING
output = pckg_no.
it_poservice-subpckg_no = pckg_no .
APPEND it_poservice.
ENDAT.
"开始处理服务明细
ext_line = ext_line + 10 .
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = ext_line
IMPORTING
output = ext_line.
line_no = line_no + 1 .
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = line_no
IMPORTING
output = line_no.
it_poservice-pckg_no = pckg_no.
it_poservice-line_no = line_no.
it_poservice-ext_line = ext_line .
it_poservice-subpckg_no = ‘0000000000‘.
it_poservice-ext_serv = gw_jsd_date-jsdhh. "结算单行号
it_poservice-service = gw_jsd_date-asnum. "服务编码
it_poservice-matl_group = gw_jsd_date-matkl. "物料组
it_poservice-quantity = gw_jsd_date-menge. "数量
it_poservice-base_uom = gw_jsd_date-meins. "单位
it_poservice-gr_price = gw_jsd_date-bhsdj. "不含税单价
it_poservice-tax_code = gw_jsd_date-beznk. "税码
it_poservice-short_text = gw_jsd_date-beizhu. "备注
APPEND it_poservice.
CLEAR it_poservice.
"*FILL ACCOUNT VALUE
serial_no = serial_no + 1 .
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
input = serial_no
IMPORTING
output = serial_no.
it_povalues-pckg_no = pckg_no .
it_povalues-line_no = line_no .
it_povalues-serno_line = serial_no .
it_povalues-serial_no = serial_no .
APPEND it_povalues .
CLEAR it_povalues .
"科目成本中心信息,一个行项目只有一条此数据
it_poaccount-po_item = po_item.
it_poaccount-serial_no = serial_no.
* it_poaccount-gl_account = ‘1480000010‘ .
it_poaccount-costcenter = gw_jsd_date-kostl. "成本中心 ‘1000510000‘."
it_poaccount-co_area = ‘1000‘.
APPEND it_poaccount .
it_poaccountx-po_item = po_item.
it_poaccountx-gl_account = ‘X‘ .
it_poaccountx-costcenter = ‘X‘.
it_poaccountx-co_area = ‘X‘.
APPEND it_poaccountx .
ENDLOOP.
"创建 BAPI
CALL FUNCTION ‘BAPI_PO_CREATE1‘
EXPORTING
poheader = header
poheaderx = headerx
IMPORTING
exppurchaseorder = ebeln
TABLES
return = it_return[]
poitem = it_poitem[]
poitemx = it_poitemx[]
poschedule = it_poschedule[]
poschedulex = it_poschedulex[]
poaccount = it_poaccount[]
poaccountx = it_poaccountx[]
polimits = it_polimits[]
poservices = it_poservice[]
posrvaccessvalues = it_povalues[].
DATA: str TYPE c LENGTH 60.
LOOP AT it_return WHERE type = ‘E‘ .
e_flag = ‘2‘.
* str = e_info.
CLEAR e_info.
CONCATENATE str it_return-message INTO e_info.
ENDLOOP.
IF e_flag = ‘0‘.
e_info = ‘采购订单创建成功!‘.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT‘ .
ENDIF.
ENDFUNCTION.