采购单与调拨单或销售订单关联,增加表FRET记录,在对采购单做收货时,
系统自动对调拔单或销售订单,创建拣配单(交货单),具体做法:
1、往FRET增加记录;
2、修改采购单抬头和行项目相应关联标识。
代码:
(1)在SE11创建结构: ZST_SAVE_FRET
BLNRB CHAR
10 0 凭证号,采购
BPOSB NUMC
6 0 凭证项目,采购
BLNRA CHAR
10 0 发货凭证号
BPOSA NUMC
6 0 凭证项目,发货
(2)在SE37创建函数如下:
FUNCTION ZF_SAVE_FRET.
*"----------------------------------------------------------------------
*"*"Local interface:
*" EXPORTING
*" REFERENCE(ERRCODE) TYPE CHAR1
*" REFERENCE(EXEMSG) TYPE CHAR100
*" TABLES
*" IT_FRET STRUCTURE ZST_SAVE_FRET
*"----------------------------------------------------------------------
DATA:F_IT_FRET_SAVE LIKE FRET OCCURS 0 WITH HEADER LINE,
F_IT_CGD_TT LIKE EKKO OCCURS 0 WITH HEADER LINE,
F_IT_CGD_HXM LIKE EKPO OCCURS 0 WITH HEADER LINE,
F_IT_XSD_TT LIKE VBAK OCCURS 0 WITH HEADER LINE,
F_IT_XSD_HXM LIKE VBAP OCCURS 0 WITH HEADER LINE,
F_IT_DBD_HXM LIKE EKPO OCCURS 0 WITH HEADER LINE,
F_CGD_HT_INDEX TYPE SY-TABIX,
F_CGD_HXM_INDEX TYPE SY-TABIX.
CLEAR:ERRCODE.
EXEMSG = ‘数据保存成功‘.
IF IT_FRET[] IS INITIAL.
EXEMSG = ‘传入内表[IT_FRET]为空‘.
RETURN.
ENDIF.
"-------------------- 1、取采购单信息 --------------------
"--**-- (1) 行项目数据
SELECT * FROM EKPO INTO CORRESPONDING FIELDS OF TABLE F_IT_CGD_HXM
FOR ALL ENTRIES IN IT_FRET
WHERE EBELN = IT_FRET-BLNRB AND
EBELP = IT_FRET-BPOSB+1.
"--**-- (2) 抬头数据
SELECT * FROM EKKO INTO CORRESPONDING FIELDS OF TABLE F_IT_CGD_TT
FOR ALL ENTRIES IN IT_FRET
WHERE EBELN = IT_FRET-BLNRB.
"-------------------- 2、取销售订单信息 --------------------
"--**-- (1) 行项目数据
SELECT * FROM VBAP INTO CORRESPONDING FIELDS OF TABLE F_IT_XSD_HXM
FOR ALL ENTRIES IN IT_FRET
WHERE VBELN = IT_FRET-BLNRA AND
POSNR = IT_FRET-BPOSA.
"--**-- (2) 抬头数据
SELECT * FROM VBAK INTO CORRESPONDING FIELDS OF TABLE F_IT_XSD_TT
FOR ALL ENTRIES IN IT_FRET
WHERE VBELN = IT_FRET-BLNRA.
"-------------------- 3、取调拨单信息 -----------------------
SELECT * FROM EKPO INTO CORRESPONDING FIELDS OF TABLE F_IT_DBD_HXM
FOR ALL ENTRIES IN IT_FRET
WHERE EBELN = IT_FRET-BLNRA AND
EBELP = IT_FRET-BPOSA+1.
SORT F_IT_CGD_HXM BY EBELN EBELP.
SORT F_IT_XSD_TT BY VBELN.
SORT F_IT_XSD_HXM BY VBELN POSNR.
SORT F_IT_DBD_HXM BY EBELN EBELP.
CLEAR F_IT_FRET_SAVE[].
LOOP AT IT_FRET.
CLEAR F_IT_FRET_SAVE.
MOVE-CORRESPONDING IT_FRET TO F_IT_FRET_SAVE.
"检查采购单
READ TABLE F_IT_CGD_HXM WITH KEY EBELN = IT_FRET-BLNRB
EBELP = IT_FRET-BPOSB+1
BINARY SEARCH.
F_CGD_HXM_INDEX = SY-TABIX.
IF SY-SUBRC NE 0.
CONTINUE.
ENDIF.
"检查销售订单
READ TABLE F_IT_XSD_HXM WITH KEY VBELN = IT_FRET-BLNRA
POSNR = IT_FRET-BPOSA
BINARY SEARCH.
IF SY-SUBRC EQ 0.
F_IT_FRET_SAVE-BTYPA = ‘2‘.
F_IT_FRET_SAVE-MATNR = F_IT_XSD_HXM-MATNR.
F_IT_FRET_SAVE-ABNTP = ‘01‘.
F_IT_FRET_SAVE-PMENA = F_IT_XSD_HXM-KWMENG.
F_IT_FRET_SAVE-PEINA = F_IT_XSD_HXM-MEINS.
READ TABLE F_IT_XSD_TT WITH KEY VBELN = IT_FRET-BLNRA BINARY SEARCH.
IF SY-SUBRC EQ 0.
F_IT_FRET_SAVE-ABNNR = F_IT_XSD_TT-KUNNR.
ENDIF.
ELSE.
"检查调拨订单
READ TABLE F_IT_DBD_HXM WITH KEY EBELN = IT_FRET-BLNRA
EBELP = IT_FRET-BPOSA+1
BINARY SEARCH.
IF SY-SUBRC EQ 0.
F_IT_FRET_SAVE-BTYPA = ‘1‘.
F_IT_FRET_SAVE-MATNR = F_IT_DBD_HXM-MATNR.
F_IT_FRET_SAVE-PMENA = F_IT_DBD_HXM-MENGE.
F_IT_FRET_SAVE-PEINA = F_IT_DBD_HXM-MEINS.
F_IT_FRET_SAVE-ABNTP = ‘02‘.
F_IT_FRET_SAVE-ABNNR = F_IT_DBD_HXM-WERKS.
ELSE.
CONTINUE.
ENDIF.
ENDIF.
F_IT_FRET_SAVE-BTYPB = ‘1‘.
F_IT_FRET_SAVE-GABLV = ‘02‘.
F_IT_FRET_SAVE-BARTV = ‘5‘.
F_IT_FRET_SAVE-VERVF = ‘1‘.
F_IT_FRET_SAVE-KZVST = ‘X‘.
F_IT_FRET_SAVE-PMENB = F_IT_CGD_HXM-MENGE.
F_IT_FRET_SAVE-PEINB = F_IT_CGD_HXM-MEINS.
F_IT_FRET_SAVE-WERKA = F_IT_CGD_HXM-WERKS.
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT‘
EXPORTING
INPUT = F_IT_FRET_SAVE-ABNNR
IMPORTING
OUTPUT = F_IT_FRET_SAVE-ABNNR.
F_IT_FRET_SAVE-ERFDAT = SY-DATUM.
F_IT_FRET_SAVE-ERFUSR = SY-UNAME.
GET TIME FIELD F_IT_FRET_SAVE-ERFZT.
APPEND F_IT_FRET_SAVE.
"修改采购单行项目内表数据状态
F_IT_CGD_HXM-AUREL = ‘5‘.
MODIFY F_IT_CGD_HXM INDEX F_CGD_HXM_INDEX TRANSPORTING AUREL.
AT END OF BLNRA.
"修改采购单表头数据状态
READ TABLE F_IT_CGD_TT WITH KEY EBELN = F_IT_CGD_HXM-EBELN BINARY SEARCH.
F_CGD_HT_INDEX = SY-TABIX.
IF SY-SUBRC EQ 0.
F_IT_CGD_TT-STATU = ‘S‘.
MODIFY F_IT_CGD_TT INDEX F_CGD_HT_INDEX TRANSPORTING STATU.
ENDIF.
ENDAT.
ENDLOOP.
IF F_IT_FRET_SAVE[] IS INITIAL.
ERRCODE = ‘X‘.
EXEMSG = ‘没有数据需要保存,请检查内表IT_FRET中的单据是否有效‘.
RETURN.
ENDIF.
"保存数据到FRET
MODIFY FRET FROM TABLE F_IT_FRET_SAVE.
IF SY-SUBRC NE 0.
ERRCODE = ‘X‘.
EXEMSG = ‘保存数据到FRET失败‘.
ROLLBACK WORK.
RETURN.
ENDIF.
"修改采购的状态
MODIFY EKKO FROM TABLE F_IT_CGD_TT.
IF SY-SUBRC NE 0.
ERRCODE = ‘X‘.
EXEMSG = ‘修改采购单数据[EKKO]失败‘.
ROLLBACK WORK.
RETURN.
ENDIF.
MODIFY EKPO FROM TABLE F_IT_CGD_HXM.
IF SY-SUBRC NE 0.
ERRCODE = ‘X‘.
EXEMSG = ‘修改采购单数据[EKPO]失败‘.
ROLLBACK WORK.
RETURN.
ENDIF.
COMMIT WORK AND WAIT.
ENDFUNCTION.