*&--------------------------------------------------------------*
*& FUNCTION:ZRFC_MM_ME21N
*& Module : Function Group 2012
*$ Short Text Create Purchase Order
*& Function Group ZGRFC_MM
*& Maraco MC_CONDITION_INSERT
*& Short Text 创建采购订单函数
*&--------------------------------------------------------------*
*& Created : Gary.C (2008.07.29) From Hanhe Information Technoligy CD. LTD.
*& Modified : Name (Date) From
* Intention : 1. User could use this function to create purchase order.
*&--------------------------------------------------------------*
FUNCTION zrfc_mm_me21n.
*"---------------------------------------------------------------------- *"*"Local interface:
*" IMPORTING
*" VALUE(PO_HEADER) LIKE BAPIMEPOHEADER STRUCTURE BAPIMEPOHEADER
*" OPTIONAL
*" VALUE(PO_HEADERX) LIKE BAPIMEPOHEADERX STRUCTURE
*" BAPIMEPOHEADERX OPTIONAL
*" EXPORTING
*" VALUE(EP_PONUMBER) LIKE BAPIMEPOHEADER-PO_NUMBER
*" VALUE(EP_MSG) TYPE CHAR255
*" VALUE(EP_SUBRC) LIKE SY-SUBRC
*" VALUE(EP_FRGKE) LIKE EKKO-FRGKE
*" TABLES
*" ET_LOG STRUCTURE BAPIRET2 OPTIONAL
*" IT_POITEM STRUCTURE ZBAPIMEPOITEM OPTIONAL
*" IT_POITEMX STRUCTURE BAPIMEPOITEMX OPTIONAL
*" IT_POCOND STRUCTURE BAPIMEPOCOND OPTIONAL
*" IT_POCONDX STRUCTURE BAPIMEPOCONDX OPTIONAL
*" IT_DELIVERY_SCHEDULE STRUCTURE BAPIMEPOSCHEDULE OPTIONAL
*" IT_DELIVERY_SCHEDULEX STRUCTURE BAPIMEPOSCHEDULX OPTIONAL
*" IT_POHEADER_TEXT STRUCTURE ZRFC_ME21N_TEXTSEVICE OPTIONAL
*" IT_POITEM_TEXT STRUCTURE ZRFC_ME21N_TEXTSEVICE1 OPTIONAL
*" IT_PO_REL_CODE STRUCTURE ZRFC_ME21N_REL_CODE OPTIONAL
*" IT_POITEMCOND STRUCTURE ZME21NPOCOND OPTIONAL
*"----------------------------------------------------------------------
*&---------------------------------Global variant-----------------------------*
DATA: gp_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,
gp_potext_header LIKE bapimepotextheader OCCURS 0 WITH HEADER LINE,
gp_potext_item LIKE bapimepotext OCCURS 0 WITH HEADER LINE.
DATA: gc_vendor LIKE lfa1-lifnr,
gc_country LIKE lfa1-land1,
gc_frgke LIKE ekko-frgke.
CLEAR gc_country.
*&---------------------------------Po Header-----------------------------* po_headerx-doc_type = 'X'. "Document_type.
po_headerx-vendor = 'X'. "Supplyer.
po_headerx-purch_org = 'X'. "Purchase orgnization.
po_headerx-pur_group = 'X'. "Purchase Group
*&-----addition_data
po_headerx-vper_start = 'X'. "validity date start.
po_headerx-vper_end = 'X'. "validity date end.
*&-----Payment
po_headerx-pmnttrms = 'X'. "Code for Payment
po_headerx-incoterms1 = 'X'. "Interetional incoming terms 1
po_headerx-incoterms2 = 'X'. "Interetional incoming terms 2
*&-----Add Zero
PERFORM f_add_zero USING po_header-vendor.
PERFORM f_add_zero USING po_header-purch_org.
*&---------------------------------Po Item-----------------------------* IF it_poitem[] IS INITIAL.
ep_msg = '行项目不能为空'.
ep_subrc = -1.
EXIT.
ENDIF.
LOOP AT it_poitem.
it_poitem-po_item(4) = sy-tabix. "Po item No.
it_poitemx-po_item(4) = sy-tabix.
it_poitemx-po_itemx = 'X'. "Po item No
it_poitemx-material = 'X'. "Material No.
it_poitemx-quantity = 'X'. "Quantity
it_poitemx-net_price = 'X'. "Net Price
it_poitemx-plant = 'X'. "Plant.
it_poitemx-preq_name = 'X'. "Prchase_Requirmenter's Name .
it_poitemx-preq_no = 'X'. "Prchase_Requirment No.
it_poitemx-preq_item = 'X'. "Prchase_Requirment Item.
*&-----Detail
it_poitemx-over_dlv_tol = 'X'. "Over Delivery Tolerance
it_poitemx-under_dlv_tol = 'X'. "Under Delivery Tolerance
*&-----Tax Code
it_poitemx-tax_code = 'X'. "Tax Code
*&-----Add Zero
PERFORM f_add_zero USING it_poitem-material.
PERFORM f_add_zero USING it_poitem-preq_no.
MODIFY it_poitem.
APPEND it_poitemx.
ENDLOOP.
*&-----Delivery Schedule
IF it_delivery_schedule[] IS INITIAL.
ep_msg = '请输入行项目交货计划日期'.
ep_subrc = -1.
EXIT.
ENDIF.
LOOP AT it_delivery_schedule.
it_delivery_schedule-po_item(4) = sy-tabix. "Po item No.
it_delivery_schedulex-po_item(4) = sy-tabix.
it_delivery_schedulex-po_itemx = 'X'.
it_delivery_schedulex-delivery_date = 'X'. "Delivery_Date.
MODIFY it_delivery_schedule.
APPEND it_delivery_schedulex.
ENDLOOP.
*&---------------------------------Text-service-----------------------------* *&---Po_Header_Text
LOOP AT it_poheader_text. "PO抬头文本---活价标识F01
IF it_poheader_text-text_head01 IS NOT INITIAL.
gp_potext_header-text_line = it_poheader_text-text_head01.
gp_potext_header-text_id = 'F01'.
APPEND gp_potext_header.
ENDIF.
IF it_poheader_text-text_head02 IS NOT INITIAL. "PO抬头文本---保险费F02
gp_potext_header-text_line = it_poheader_text-text_head02.
gp_potext_header-text_id = 'F02'.
APPEND gp_potext_header.
ENDIF.
IF it_poheader_text-text_head03 IS NOT INITIAL. "PO抬头文本---码头、港建费F03 gp_potext_header-text_line = it_poheader_text-text_head03.
gp_potext_header-text_id = 'F03'.
APPEND gp_potext_header.
ENDIF.
IF it_poheader_text-text_head04 IS NOT INITIAL. "PO抬头文本---商检费F04
gp_potext_header-text_line = it_poheader_text-text_head04.
gp_potext_header-text_id = 'F04'.
APPEND gp_potext_header.
ENDIF.
IF it_poheader_text-text_head05 IS NOT INITIAL. "PO抬头文本---采购类型F05
gp_potext_header-text_line = it_poheader_text-text_head05.
gp_potext_header-text_id = 'F05'.
APPEND gp_potext_header.
ENDIF.
IF it_poheader_text-text_head06 IS NOT INITIAL. "PO抬头文本---发运地F06
gp_potext_header-text_line = it_poheader_text-text_head06.
gp_potext_header-text_id = 'F06'.
APPEND gp_potext_header.
ENDIF.
IF it_poheader_text-text_head07 IS NOT INITIAL. "PO抬头文本---备注F07
gp_potext_header-text_line = it_poheader_text-text_head07.
gp_potext_header-text_id = 'F07'.
APPEND gp_potext_header.
ENDIF.
IF it_poheader_text-text_head08 IS NOT INITIAL. "PO抬头文本---合同号F08
gp_potext_header-text_line = it_poheader_text-text_head08.
gp_potext_header-text_id = 'F08'.
APPEND gp_potext_header.
ENDIF.
ENDLOOP.
LOOP AT it_poitem_text. "Warning:Po_Number And Po_item must reference to IT_POITEM-PO_ITEM.
IF it_poitem_text-text_item01 IS NOT INITIAL. "采购单行项目文本----运费f01
gp_potext_item-text_line = it_poitem_text-text_item01.
gp_potext_item-po_item = it_poitem_text-po_item.
gp_potext_item-text_id = 'F01'.
APPEND gp_potext_item.
ENDIF.
IF it_poitem_text-text_item02 IS NOT INITIAL. "采购单行项目文本----运输方式F02
gp_potext_item-text_line = it_poitem_text-text_item02.
gp_potext_item-po_item = it_poitem_text-po_item.
gp_potext_item-text_id = 'F02'.
APPEND gp_potext_item.
ENDIF.
IF it_poitem_text-text_item03 IS NOT INITIAL. "采购单行项目文本----费用支付情况F03 gp_potext_item-text_line = it_poitem_text-text_item03.
gp_potext_item-po_item = it_poitem_text-po_item.
gp_potext_item-text_id = 'F03'.
APPEND gp_potext_item.
ENDIF.
IF it_poitem_text-text_item04 IS NOT INITIAL. "采购单行项目文本----备注F04
gp_potext_item-text_line = it_poitem_text-text_item04.
gp_potext_item-po_item = it_poitem_text-po_item.
gp_potext_item-text_id = 'F04'.
APPEND gp_potext_item.
ENDIF.
IF it_poitem_text-text_item05 IS NOT INITIAL. "采购单行项目文本----杂费F05
gp_potext_item-text_line = it_poitem_text-text_item05.
gp_potext_item-po_item = it_poitem_text-po_item.
gp_potext_item-text_id = 'F05'.
APPEND gp_potext_item.
ENDIF.
IF it_poitem_text-text_item06 IS NOT INITIAL. "采购单行项目文本----车船号F06
gp_potext_item-text_line = it_poitem_text-text_item06.
gp_potext_item-po_item = it_poitem_text-po_item.
gp_potext_item-text_id = 'F06'.
APPEND gp_potext_item.
ENDIF.
IF it_poitem_text-text_item07 IS NOT INITIAL. "采购单行项目文本----质量标准F07
gp_potext_item-text_line = it_poitem_text-text_item07.
gp_potext_item-po_item = it_poitem_text-po_item.
gp_potext_item-text_id = 'F07'.
APPEND gp_potext_item.
ENDIF.
ENDLOOP.
*&---------------------------------Check Input-----------------------------*
IF po_header-incoterms1 IS NOT INITIAL.
IF po_header-incoterms2 IS INITIAL .
ep_msg = '请检查国际贸易付款条件的输入'.
ep_subrc = -1.
EXIT.
ENDIF.
ENDIF.
gc_vendor = po_header-vendor.
SELECT SINGLE lfa1~land1 INTO gc_country FROM lfa1 WHERE lfa1~lifnr = gc_vendor.
IF gc_country EQ 'CN'.
READ TABLE it_poitem WITH KEY tax_code = ''.
IF sy-subrc = 0.
ep_msg = '请输入税务代码'.
ep_subrc = -1.
EXIT.
ENDIF.
ENDIF.
*&---------------------------------Condition-----------------------------*
LOOP AT it_poitem.
IF gc_country EQ 'CN'.
mc_condition_insert it_poitem-pbxx_value it_poitem-pbxx_unit 'PBXX'. "总价格:单价(元/吨)
* mc_condition_insert it_poitem-znf101_value it_poitem-znf101_unit 'ZNF1'. "进项增值税
mc_condition_insert it_poitem-znf501_value it_poitem-znf501_unit 'ZNF5'. "运费(值)
mc_condition_insert it_poitem-znf6_value it_poitem-znf6_unit 'ZNF6'. "运费/数量
mc_condition_insert it_poitem-znf401_value it_poitem-znf401_unit 'ZNF4'. "运费税率 %, 默认7.0% mc_condition_insert it_poitem-znf701_value it_poitem-znf701_unit 'ZNF7'. "杂费(值)
* mc_condition_insert it_poitem-znfb01_value it_poitem-znfb01_unit 'ZNFB'. "不含税运费
mc_condition_insert it_poitem-znf801_value it_poitem-znf801_unit 'ZNF8'. "杂费(数量)
ELSE.
mc_condition_insert it_poitem-pbxx_value it_poitem-pbxx_unit 'PBXX'. "总价格:单价(元/吨)
mc_condition_insert it_poitem-znf202_value it_poitem-znf202_unit 'ZNF2'. "关税
mc_condition_insert it_poitem-znf302_value it_poitem-znf302_unit 'ZNF3'. "消费税
mc_condition_insert it_poitem-znf902_value it_poitem-znf902_unit 'ZNF9'. "保险费/数量mc_condition_insert it_poitem-znfa02_value it_poitem-znfa02_unit 'ZNFA'. "其它费用/数量mc_condition_insert it_poitem-znfc02_value it_poitem-znfc02_unit 'ZNFC'. "其它费用(值) mc_condition_insert it_poitem-znf6_value it_poitem-znf6_unit 'ZNF6'. "运费/数量
ENDIF.
ENDLOOP.
*&---------------------------------Date Translation-----------------------------*
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = po_header
poheaderx = po_headerx
no_price_from_po = 'X'
IMPORTING
exppurchaseorder = ep_ponumber
TABLES
return = et_log
poitem = it_poitem
poitemx = it_poitemx
poschedule = it_delivery_schedule
poschedulex = it_delivery_schedulex
potextheader = gp_potext_header
potextitem = gp_potext_item
pocond = it_pocond
pocondx = it_pocondx
EXCEPTIONS
OTHERS = -1.
IF ep_ponumber NE space.
READ TABLE et_log WITH KEY type = 'E'.
IF sy-subrc = 0.
ep_msg = '无法创建采购订单,请查看错误日志'.
ep_subrc = -1.
ROLLBACK WORK.
ELSE.
LOOP AT et_log.
IF et_log-type = 'W'.
ep_msg = '订单已经成功创建但出现系统警告,请查看ET_LOG日志'.
ep_subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ELSE.
* READ TABLE et_log WITH KEY type = 'E'.
* IF sy-subrc = 0.
* ep_msg = '无法创建采购订单,请查看错误日志'.
* ep_subrc = -1.
* ROLLBACK WORK.
ep_msg = '创建采购订单成功'.
ep_subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
ENDLOOP.
ENDIF.
ELSE.
ROLLBACK WORK.
ep_msg = '创建采购订单失败,请查看错误日志'.
ep_subrc = -1.
EXIT.
ENDIF.
*
*
**&---------------------------------Po Release status-----------------------------* IF ep_subrc = 0.
SELECT SINGLE ekko~frgke INTO gc_frgke FROM ekko WHERE ekko~ebeln = ep_ponumber.
ep_frgke = gc_frgke.
ENDIF.
**&---------------------------------Po Release-----------------------------*
* LOOP AT it_po_rel_code.
* CALL FUNCTION 'BAPI_PO_RELEASE'
* EXPORTING
* purchaseorder = ep_ponumber
* po_rel_code = it_po_rel_code-rel_code
** USE_EXCEPTIONS = 'X'
** NO_COMMIT = ' '
** IMPORTING
** REL_STATUS_NEW =
** REL_INDICATOR_NEW =
** RET_CODE =
** TABLES
** RETURN =
** EXCEPTIONS
** AUTHORITY_CHECK_FAIL = 1
** DOCUMENT_NOT_FOUND = 2
** ENQUEUE_FAIL = 3
** PREREQUISITE_FAIL = 4
** RELEASE_ALREADY_POSTED = 5
** RESPONSIBILITY_FAIL = 6
** OTHERS = 7
* .
*
* ENDLOOP.
*
*
----------------------mc_condition_insert------------------ DEFINE mc_condition_insert.
if ( &1 is not initial ) or ( &2 is not initial ).
it_pocond-itm_number = it_poitem-po_item.
it_pocond-cond_type = &3 .
it_pocond-cond_value = &1. "it_poitem-pbxx01_value.
it_pocond-cond_unit = &2 . "it_poitem-pbxx01_unit.
it_pocond-change_id = 'U'.
it_pocond-currency = &4.
it_pocond-cond_p_unt = &5.
append it_pocond.
it_pocondx-itm_number = it_poitem-po_item.
it_pocondx-change_id = 'X'.
* it_pocondx-itm_number = 'X'.
it_pocondx-itm_numberx = 'X'.
it_pocondx-cond_type = 'X'. "Condition type.
if &4 is not initial.
it_pocondx-currency = 'X'.
endif.
if &1 is not initial.
it_pocondx-cond_value = 'X'. "Condition value.
endif.
*** it_pocondx-currency = 'X'. "Currency
*** it_pocondx-cond_p_unt = 'X'. "Condition per unit if &2 is not initial.
it_pocondx-cond_unit = 'X'. "Condition unit.
endif.
if &5 is not initial.
it_pocondx-cond_p_unt = 'X'. "Condition value. endif.
append it_pocondx.
endif.
END-OF-DEFINITION.
ENDFUNCTION.