ZSDR017-客户订货价格和库存

*----------------------------------------------------------------------
*ZSDR017-客户订货价格和库存
*----------------------------------------------------------------------

REPORT ZSDR017 NO STANDARD PAGE HEADING
MESSAGE-ID ZSD
LINE-SIZE 132
LINE-COUNT 65.

*----------------------------------------------------------------------
* TYPE-POOLS
*----------------------------------------------------------------------
TYPE-POOLS: SLIS, "ALV
TRUXS.

*----------------------------------------------------------------------
* TABLES/Structure
*----------------------------------------------------------------------
TABLES: MARA,
MARM,
T179,
KONP,
MARD,
KNA1,
VBAK,
TWVK,
RV130.
" zstatu ZSD008 ZSD13

*----------------------------------------------------------------------
* DAGTA /Internal table
*----------------------------------------------------------------------
*&(contain all)
DATA: BEGIN OF IT_DATA OCCURS 0,
MATNR LIKE MARA-MATNR,"kunnr ->model(s) ->matnr(s)
PRDHA LIKE MARA-PRDHA,"产品层次
ZMDNM LIKE MARA-ZMDNM,"型号
UMREZ LIKE MARM-UMREZ,"单位
MATKL LIKE MARA-MATKL,"品牌
UMREZ1(10) TYPE C, "单位
ZONGL(200) TYPE C, "总类别
FENEL(200) TYPE C, "分类别
STATU(50) TYPE C, "料状态
XHTXT(100) TYPE C, "型号描述
XHTXT1(100) TYPE C, "Internal comment
KBETR LIKE KONP-KBETR,"价格:
KCNGE LIKE MARD-LABST,"现库存
DINSH LIKE MARD-LABST,"default 0,供用户输入
LAST1 LIKE MARD-LABST,"最近一次订单数
LAST2 LIKE MARD-LABST,"倒数第二次订单数
LAST3 LIKE MARD-LABST,"倒数第三次订单数
KONWA LIKE KONP-KONWA,

END OF IT_DATA.
DATA: WA_DATA LIKE IT_DATA.

*&PDF:
DATA: BEGIN OF IT_DATAP OCCURS 0,
KUNNR LIKE KNA1-KUNNR,"kunnr
ZMDNM LIKE MARA-ZMDNM,"型号 "Model to get matkl to decide log.
MATKL LIKE MARA-MATKL,"brand

ZONGL(200) TYPE C, "总类别 Main Category
FENEL(200) TYPE C, "分类别 Sub Category
STATU(50) TYPE C, "料状态 Status
UMREZ1(10) TYPE C, "单位 MC Qty
XHTXT(50) TYPE C, "型号描述 Item text
XHTXT1(100) TYPE C, "Internal comment Prod Subhead
*&CHAGNE ON 20140214.
KBETR TYPE STRING,
* KBETR LIKE KONP-KBETR,"价格:US$
KCNGE LIKE MARD-LABST,"现库存 Current Availabile fixed value
KCSHU(50) TYPE C, "库存 district
END OF IT_DATAP.

*&CSV:
DATA: BEGIN OF IT_DATAC OCCURS 0,
STATU(50) TYPE C, "料状态
ZONGL(200) TYPE C, "总类别 Main Category
FENEL(200) TYPE C, "分类别 Sub Category
UMREZ1(10) TYPE C, "单位 MC Qty

ZMDNM LIKE MARA-ZMDNM,"型号 Model Name
KCNGE LIKE MARD-LABST,"现库存 Current Available
KCSHU(50) TYPE C, "库存
DINSH LIKE MARD-LABST,"default 0,供用户输入 Ordered Qty
LAST1 LIKE MARD-LABST,"最近一次订单数 Last-1 Ordered Qty
LAST2 LIKE MARD-LABST,"倒数第二次订单数 Last-1 Ordered Qty
LAST3 LIKE MARD-LABST,"倒数第三次订单数 Last-1 Ordered Qty
END OF IT_DATAC.

DATA:ITAB1 TYPE TRUXS_T_TEXT_DATA,
WA_ITAB1 LIKE LINE OF ITAB1.

*----------------------------------------------------------------------
* Data parameters for alv report use
*----------------------------------------------------------------------
DATA: G_PROGRAM TYPE SY-REPID,
GW_LAYOUT TYPE SLIS_LAYOUT_ALV,
GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
GT_EVENT TYPE SLIS_T_EVENT,
WA_EVENT TYPE SLIS_ALV_EVENT,
TOP_OF_PAGE TYPE SLIS_T_LISTHEADER.

DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA L_LEN TYPE I.

CONSTANTS:ER_FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE'.
DATA:G_FILE LIKE RLGRAP-FILENAME.
DATA:S_FILE LIKE RLGRAP-FILENAME.
DATA:LINE(1000) TYPE C.

*======================================================================
* SELECTION-SCREEN
*======================================================================
SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-001.

PARAMETERS: P_KUNNR LIKE KNA1-KUNNR OBLIGATORY,
P_KSCHL LIKE TWVK-KSCHL DEFAULT 'ZP01'.

*S_KUNNR FOR KNA1-KUNNR,
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR. "NO-DISPLAY.
SELECT-OPTIONS: S_VKORG FOR VBAK-VKORG DEFAULT '1100',
S_VTWEG FOR VBAK-VTWEG DEFAULT '10'.

PARAMETERS: P_ERDAT LIKE MARA-ERSDA DEFAULT SY-DATUM.

SELECTION-SCREEN SKIP 1.

PARAMETERS:P_SEFILE LIKE RLGRAP-FILENAME DEFAULT '/mnt/ReceiveFromMG/Report/'.

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK BLK2 WITH FRAME TITLE TEXT-002.
PARAMETERS: RB_PDF RADIOBUTTON GROUP R1 DEFAULT 'X',
RB_CSV RADIOBUTTON GROUP R1,
RB_ALV RADIOBUTTON GROUP R1.

SELECTION-SCREEN END OF BLOCK BLK2.

SELECTION-SCREEN END OF BLOCK BLK1.

*----------------------------------------------------------------------
* INITIALIZATION
*----------------------------------------------------------------------
INITIALIZATION.

* 初始化数据
PERFORM FRM_INITIAL.

*----------------------------------------------------------------------
* AT SELECTION-SCREEN
*----------------------------------------------------------------------
AT SELECTION-SCREEN.

* 权限检查部分
PERFORM FRM_CHECK_AUTHOR.

*======================================================================
* MAIN PROGRAM
*======================================================================
* START-OF-SELECTION
*----------------------------------------------------------------------
START-OF-SELECTION.
*&获取数据

PERFORM FRM_GET_DATA.

CASE 'X'.

WHEN RB_PDF.
CHECK IT_DATAP[] IS NOT INITIAL.
PERFORM FRM_PDF_OUT.

WHEN RB_CSV.
CHECK IT_DATAC[] IS NOT INITIAL.
PERFORM FRM_CSV_OUT.

WHEN RB_ALV.
*&ALV展示输出
CHECK IT_DATA[] IS NOT INITIAL.
PERFORM FRM_ALV_OUTPUT.

WHEN OTHERS.

ENDCASE.

*----------------------------------------------------------------------
* END-OF-SELECTION
*----------------------------------------------------------------------

*&---------------------------------------------------------------------*
*& Form FRM_INITIAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_INITIAL.

ENDFORM. " FRM_INITIAL

*&---------------------------------------------------------------------*
*& Form FRM_CHECK_AUTHOR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_CHECK_AUTHOR.

ENDFORM. " FRM_CHECK_AUTHOR

*&---------------------------------------------------------------------*
*& Form frm_get_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GET_DATA .

DATA:BEGIN OF LT_ZSTATU OCCURS 0, ZMDNM
LIKE ZT0001-ZMDNM, STATU
(50) TYPE C, "料状态
END OF LT_ZSTATU.
DATA:BEGIN OF LT_KEHU OCCURS 0, KUNNR
LIKE KNA1-KUNNR, MATNR
LIKE MARA-MATNR, MATN1
LIKE MARA-MATNR, ZMDNM
LIKE MARA-ZMDNM,
END OF LT_KEHU.
DATA:BEGIN OF LT_BANB OCCURS 0, KUNNR
LIKE KNVV-KUNNR, KVGR1
LIKE ZSD008-MATVD, KVGR2
LIKE ZSD008-MATVD, KVGR3
LIKE ZSD008-MATVD, KVGR4
LIKE ZSD008-MATVD, KVGR5
LIKE ZSD008-MATVD,
END OF LT_BANB.
DATA:BEGIN OF LT_KOTG OCCURS 0, KSCHL
LIKE KOTG001-KSCHL, KUNNR
LIKE KOTG001-KUNNR, MATNR
LIKE KOTG001-MATNR,
END OF LT_KOTG.
DATA:G_TABIX TYPE SY-TABIX.

DATA:BEGIN OF LT_ZSD008 OCCURS 0, MATVN
LIKE ZSD008-MATVN, MATVD
LIKE ZSD008-MATVD,
END OF LT_ZSD008.

DATA:BEGIN OF LT_MARD OCCURS 0, MATNR
LIKE MARD-MATNR, LABST
LIKE MARD-LABST,
END OF LT_MARD.
DATA:BEGIN OF LT_MARD3 OCCURS 0, MATNR
LIKE MARD-MATNR, LABST
LIKE MARD-LABST,"未交 qty
END OF LT_MARD3.

DATA: BEGIN OF LT_A004 OCCURS 0, KSCHL
LIKE A004-KSCHL, MWSKZ
LIKE A003-MWSKZ, MATNR
LIKE A004-MATNR, KBETR
LIKE KONP-KBETR,
END OF LT_A004.
DATA: BEGIN OF LT_A005 OCCURS 0, MATNR
LIKE A005-MATNR, KNUMH
LIKE A005-KNUMH, KBETR
LIKE KONP-KBETR, KMEIN
LIKE KONP-KMEIN, KONWA
LIKE KONP-KONWA,
END OF LT_A005.
DATA:BEGIN OF LT_KONP OCCURS 0, KNUMH
LIKE KONP-KNUMH, KBETR
LIKE KONP-KBETR, KMEIN
LIKE KONP-KMEIN, KONWA
LIKE KONP-KONWA,
END OF LT_KONP.

DATA: LV_NAME LIKE THEAD-TDNAME, LT_LINE
LIKE STANDARD TABLE OF TLINE, LWA_LINE
LIKE LINE OF LT_LINE, LV_TEXT
TYPE STRING.

DATA: BEGIN OF LT_ZSD13 OCCURS 0, VBELN
LIKE ZSD13-VBELN, POSNR
LIKE ZSD13-POSNR, MATNR
LIKE ZSD13-MATNR, KWMENG
LIKE ZSD13-KWMENG, "订单数量 RFMNGM
LIKE ZSD13-RFMNGM, "确认的数量
*BMENG LIKE ZSD13-BMENG, "确认的数量 ERDAT
LIKE ZSD13-ERDAT,
END OF LT_ZSD13.

DATA: BEGIN OF LT_ZSD131 OCCURS 0, MATNR
LIKE VBAP-MATNR, WJENG
LIKE VBAP-KWMENG,"未交 qty
END OF LT_ZSD131.

DATA: BEGIN OF LT_VBAK OCCURS 0, MATNR
LIKE VBAP-MATNR, VBELN
LIKE VBAK-VBELN, ERDAT
LIKE VBAK-ERDAT, ERZET
LIKE VBAK-ERZET, VBTYP
LIKE VBAK-VBTYP, KUNNR
LIKE VBAK-KUNNR,
END OF LT_VBAK, WA_VBAK
LIKE LT_VBAK.

DATA: BEGIN OF LT_VBAK1 OCCURS 0, MATNR
LIKE VBAP-MATNR, VBELN
LIKE VBAK-VBELN, VBTYP
LIKE VBAK-VBTYP, KUNNR
LIKE VBAK-KUNNR, KWMENG
LIKE VBAP-KWMENG,
INDEX TYPE SY-TABIX,
END OF LT_VBAK1.
DATA:L_KBETR TYPE STRING, L_KONWA
TYPE STRING.

*&G1.1.kunnr get matnr
SELECT A~KUNAG AS KUNNR B
~MATNR
INTO TABLE LT_KEHU
FROM KOTG951 AS A
JOIN MVKE AS B
ON A~ZZMVGR2 EQ B~MVGR2
WHERE A~KUNAG EQ P_KUNNR
AND A~DATBI GE P_ERDAT
AND A~DATAB LE P_ERDAT
AND B~VKORG IN S_VKORG
AND B~VTWEG IN S_VTWEG
*&->CHANGE
AND B~MATNR IN S_MATNR.

*&-> use for test D300
IF SY-MANDT NE '300'.
IF SY-SUBRC NE 0.
MESSAGE S001 DISPLAY LIKE 'W'.
EXIT.
ENDIF.
ENDIF.
DELETE ADJACENT DUPLICATES FROM LT_KEHU COMPARING ALL FIELDS.

*&G1.2.kunnr get kvgr1 ->kvgr5(versions) delete
SELECT KUNNR KVGR1 KVGR2 KVGR3 KVGR4 KVGR5
INTO TABLE LT_BANB
FROM KNVV
WHERE KUNNR EQ P_KUNNR
AND VKORG IN S_VKORG
AND VTWEG IN S_VTWEG.

*&G1.3 get delete condition(version code) matnr+8(2) compare
SELECT MATVN MATVD

INTO TABLE LT_ZSD008
FROM ZSD008
FOR ALL ENTRIES IN LT_BANB
WHERE MATVD EQ LT_BANB-KVGR1
OR MATVD EQ LT_BANB-KVGR2
OR MATVD EQ LT_BANB-KVGR3
OR MATVD EQ LT_BANB-KVGR4
OR MATVD EQ LT_BANB-KVGR5.
**&G1.4.delete by version:
* LOOP AT LT_KEHU.
* G_TABIX = SY-TABIX.
* LT_KEHU-MATN1 = LT_KEHU-MATNR+7(2).
* READ TABLE LT_ZSD008 WITH KEY MATVN = LT_KEHU-MATN1.
* IF SY-SUBRC NE 0.
* DELETE LT_KEHU INDEX G_TABIX.
* CLEAR LT_KEHU.
* ELSE.
* MODIFY LT_KEHU TRANSPORTING MATN1.
* CLEAR LT_KEHU.
* ENDIF.
**MODIFY LT_KEHU TRANSPORTING MATN1.
**SELECT SINGLE ZMDNM
* ENDLOOP.

*&->CHANGE BY VN0037 ON 20130214.
*&G1.7.delete matnr
LOOP AT LT_KEHU. G_TABIX
= SY-TABIX.
*&2.delete not end with '10'.
IF LT_KEHU-MATNR+13(2) NE '10'.
DELETE LT_KEHU INDEX G_TABIX.
CLEAR:LT_KEHU.
CONTINUE.
ENDIF.
*&1.delete by version: LT_KEHU
-MATN1 = LT_KEHU-MATNR+7(2).
READ TABLE LT_ZSD008 WITH KEY MATVN = LT_KEHU-MATN1.
IF SY-SUBRC NE 0.
DELETE LT_KEHU INDEX G_TABIX.
CLEAR LT_KEHU.
CONTINUE.
ENDIF.
MODIFY LT_KEHU TRANSPORTING MATN1.
CLEAR LT_KEHU.

ENDLOOP.

*&G1.5. kunnr 'ZDIV' get useful matnr
SELECT KSCHL KUNNR MATNR

INTO TABLE LT_KOTG
FROM KOTG001
WHERE KSCHL IN ('ZDIV','B001')
AND KUNNR EQ P_KUNNR
AND DATAB LE P_ERDAT
AND DATBI GE P_ERDAT.

*&G1.6.add'ZDIV' type, delete‘B001’type->Last kunnr matnr.
LOOP AT LT_KOTG." WHERE KSCHL EQ 'ZDIV'.
*& +'ZDIV'.
IF LT_KOTG-KSCHL EQ 'ZDIV'. LT_KEHU
-KUNNR = LT_KOTG-KUNNR. LT_KEHU
-MATNR = LT_KOTG-MATNR.
APPEND LT_KEHU.
ELSE.
*& - 'B001'.
DELETE LT_KEHU WHERE MATNR EQ LT_KOTG-MATNR.
ENDIF.
ENDLOOP.

**&G1.7.delete matnr
* LOOP AT LT_KEHU.
* G_TABIX = SY-TABIX.
**&2.delete not end with '10'.
* IF LT_KEHU-MATNR+13(2) NE '10'.
* DELETE LT_KEHU INDEX G_TABIX.
* CLEAR:LT_KEHU.
* CONTINUE.
* ENDIF.
**&1.delete by version:
* LT_KEHU-MATN1 = LT_KEHU-MATNR+7(2).
* READ TABLE LT_ZSD008 WITH KEY MATVN = LT_KEHU-MATN1.
* IF SY-SUBRC NE 0.
* DELETE LT_KEHU INDEX G_TABIX.
* CLEAR LT_KEHU.
* CONTINUE.
* ENDIF.
* MODIFY LT_KEHU TRANSPORTING MATN1.
* CLEAR LT_KEHU.
*
* ENDLOOP.

*&G2.1 Matnr geted to find mara + MODLE
*&->test for D300.
IF LT_KEHU[] IS NOT INITIAL.

SELECT A~MATNR A
~PRDHA A
~ZMDNM B
~UMREZ A
~MATKL
INTO TABLE IT_DATA
FROM MARA AS A
JOIN MARM AS B
ON A~MATNR EQ B~MATNR
FOR ALL ENTRIES IN LT_KEHU
WHERE A~MATNR EQ LT_KEHU-MATNR
AND B~MEINH EQ 'KAR'.
ELSE.

SELECT A~MATNR A
~PRDHA A
~ZMDNM B
~UMREZ
INTO CORRESPONDING FIELDS OF TABLE IT_DATA
FROM MARA AS A
JOIN MARM AS B
ON A~MATNR EQ B~MATNR
WHERE A~MATNR IN S_MATNR
AND B~MEINH EQ 'KAR'. "

ENDIF.

DESCRIBE TABLE IT_DATA LINES L_LEN.
IF L_LEN EQ 0.
MESSAGE S001 WITH 'No data!' DISPLAY LIKE 'W'.
EXIT.
ENDIF.

SORT IT_DATA BY MATNR.

*&G2.2.Get model statu
SELECT ZMDNM "STATU
INTO TABLE LT_ZSTATU
FROM ZT0001
FOR ALL ENTRIES IN IT_DATA
*&->change
*WHERE ZMDNM EQ IT_DATA-ZMDNM
WHERE ZMDNM EQ IT_DATA-ZMDNM
AND ZDATE GE P_ERDAT.

* SELECT ZMDNM "STATU
* INTO TABLE LT_ZSTATU
* FROM ZSTATU
* SELECT A~MATNR
* B~KBETR
* INTO TABLE LT_A004
* FROM A004 AS A
* JOIN KONP AS B
* ON A~KNUMH EQ B~KNUMH
* AND A~KSCHL EQ B~KSCHL
* WHERE A~KSCHL EQ p_KSCHL
* AND A~VKORG IN S_VKORG
* AND A~VTWEG IN S_VTWEG
* AND A~DATAB LE P_ERDAT
*& A006.

IF IT_DATA[] IS NOT INITIAL.
*&G2.3 KUNNR MATNR GET KUNMH :得条件记录号
SELECT MATNR KNUMH
INTO TABLE LT_A005
FROM A005
FOR ALL ENTRIES IN IT_DATA
WHERE KAPPL EQ 'V'
AND KSCHL EQ P_KSCHL
AND VKORG IN S_VKORG
AND VTWEG IN S_VTWEG
AND KUNNR EQ P_KUNNR
AND MATNR EQ IT_DATA-MATNR
AND DATBI GE P_ERDAT
AND DATAB LE P_ERDAT.

DATA:LV_KONDA LIKE KNVV-KONDA.
CLEAR LV_KONDA.
SELECT SINGLE KONDA
INTO LV_KONDA
FROM KNVV
WHERE KUNNR EQ P_KUNNR.

SELECT MATNR KNUMH
APPENDING TABLE LT_A005
FROM A032
FOR ALL ENTRIES IN IT_DATA
WHERE KAPPL EQ 'V'
AND KSCHL EQ P_KSCHL
AND VKORG IN S_VKORG
AND VTWEG IN S_VTWEG
AND KONDA EQ LV_KONDA
AND MATNR EQ IT_DATA-MATNR
AND DATBI GE P_ERDAT
AND DATAB LE P_ERDAT.

ENDIF.

IF SY-SUBRC EQ 0.
*&G2.4 matnr get kbetr.
SELECT KNUMH KBETR KMEIN
*&CHAGNE ON 20140214. KONWA

INTO TABLE LT_KONP
FROM KONP
FOR ALL ENTRIES IN LT_A005
WHERE KNUMH EQ LT_A005-KNUMH
AND KSCHL EQ P_KSCHL
AND KOPOS EQ '1'.
SORT LT_KONP BY KNUMH.
ENDIF.

*&G2.5 get matnr kbetr.
LOOP AT LT_A005.
READ TABLE LT_KONP WITH KEY KNUMH = LT_A005-KNUMH BINARY SEARCH.
IF SY-SUBRC EQ 0. LT_A005
-KBETR = LT_KONP-KBETR. LT_A005
-KMEIN = LT_KONP-KMEIN. LT_A005
-KONWA = LT_KONP-KONWA.

ENDIF.
MODIFY LT_A005.
ENDLOOP.

SORT LT_A005 BY MATNR.

*&G3.1 Get kucun
*& 是否存在负库存,?
SELECT MATNR LABST

INTO TABLE LT_MARD
FROM MARD
FOR ALL ENTRIES IN IT_DATA
WHERE MATNR EQ IT_DATA-MATNR
AND ( WERKS EQ '1100' AND LGORT EQ '1100' OR LGORT EQ '1181'
OR ( WERKS EQ '3300' AND LGORT EQ '3351' OR LGORT EQ '3352' OR LGORT EQ '3353' ) ).

*&G3.2. get matnr labst
LOOP AT LT_MARD.
MOVE-CORRESPONDING LT_MARD TO LT_MARD3.
COLLECT LT_MARD3.
ENDLOOP.

SORT LT_MARD3 BY MATNR.

*&G3.3 zsd13 open so-aty (根据matnr 已做好 car 和pc 转换) order-qty quren-qyr
SELECT A~VBELN A
~POSNR A
~MATNR A
~KWMENG"订单数量 A
~RFMNGM"确认数量 A
~ERDAT "创建日期
INTO TABLE LT_ZSD13
FROM ZSD13 AS A
JOIN VBAK AS B
ON A~VBELN EQ B~VBELN
FOR ALL ENTRIES IN IT_DATA
WHERE A~MATNR EQ IT_DATA-MATNR
*&->change
* AND A~KUNNR EQ P_KUNNR
AND B~VBTYP EQ 'C'.

*&G3.4. get open-qty
LOOP AT LT_ZSD13. LT_ZSD131
-MATNR = LT_ZSD13-MATNR. LT_ZSD131
-WJENG = LT_ZSD13-KWMENG - LT_ZSD13-RFMNGM.
COLLECT LT_ZSD131.
ENDLOOP.
SORT LT_ZSD131 BY MATNR.

*&G4.1 kunnr -> vbeln
SELECT B~MATNR A
~VBELN A
~ERDAT A
~ERZET A
~VBTYP"凭证类别 AUART "销售凭证类型 A
~KUNNR
INTO TABLE LT_VBAK
FROM VBAK AS A
JOIN VBAP AS B
ON A~VBELN EQ B~VBELN
FOR ALL ENTRIES IN IT_DATA

WHERE A~KUNNR EQ P_KUNNR
AND A~VKORG IN S_VKORG
AND A~VTWEG IN S_VTWEG
AND A~VBTYP EQ 'C' "订单 -》‘H’退货
AND B~MATNR EQ IT_DATA-MATNR.

SORT LT_VBAK BY MATNR VBELN DESCENDING.

DATA: LV_CNT TYPE I, LV_MATNR
TYPE MARA-MATNR.

LOOP AT LT_VBAK.
*&G4.2 get vbeln matnr kwmeng
* 得到订单数量是否要考虑 kunnr + matnr.
* MOVE LT_VBAK TO WA_VBAK.
IF LV_MATNR EQ ''. LV_MATNR
= LT_VBAK-MATNR. LV_CNT
= '1'. LT_VBAK1
-INDEX = LV_CNT. LT_VBAK1
-MATNR = LT_VBAK-MATNR. LT_VBAK1
-VBELN = LT_VBAK-VBELN. LT_VBAK1
-VBTYP = LT_VBAK-VBTYP. LT_VBAK1
-KUNNR = LT_VBAK-KUNNR.
SELECT SINGLE SUM( KWMENG )
INTO LT_VBAK1-KWMENG
FROM VBAP
WHERE VBELN EQ LT_VBAK-VBELN
AND MATNR EQ LT_VBAK-MATNR.

ELSE.

IF LV_MATNR = LT_VBAK-MATNR. LV_CNT
= LV_CNT + 1. LT_VBAK1
-INDEX = LV_CNT. LT_VBAK1
-MATNR = LT_VBAK-MATNR. LT_VBAK1
-VBELN = LT_VBAK-VBELN. LT_VBAK1
-VBTYP = LT_VBAK-VBTYP. LT_VBAK1
-KUNNR = LT_VBAK-KUNNR.
SELECT SINGLE SUM( KWMENG )
INTO LT_VBAK1-KWMENG
FROM VBAP
WHERE VBELN EQ LT_VBAK-VBELN
AND MATNR EQ LT_VBAK-MATNR.

IF LV_CNT GT 3.
CONTINUE.
ENDIF.

ELSE. LV_MATNR
= LT_VBAK-MATNR. LV_CNT
= 1. LT_VBAK1
-INDEX = LV_CNT. LT_VBAK1
-MATNR = LT_VBAK-MATNR. LT_VBAK1
-VBELN = LT_VBAK-VBELN. LT_VBAK1
-VBTYP = LT_VBAK-VBTYP. LT_VBAK1
-KUNNR = LT_VBAK-KUNNR.
SELECT SINGLE SUM( KWMENG )
INTO LT_VBAK1-KWMENG
FROM VBAP
WHERE VBELN EQ LT_VBAK-VBELN
AND MATNR EQ LT_VBAK-MATNR.
ENDIF.

ENDIF.

APPEND LT_VBAK1.

ENDLOOP.

SORT LT_VBAK1 BY MATNR INDEX.

**&G5.Get us from knop.
LOOP AT IT_DATA.
*&L1.GET PRODH name.
SELECT SINGLE VTEXT
INTO IT_DATA-ZONGL
FROM T179T
WHERE SPRAS EQ SY-LANGU
AND PRODH EQ IT_DATA-PRDHA+0(4).
SELECT SINGLE VTEXT
INTO IT_DATA-FENEL
FROM T179T
WHERE SPRAS EQ SY-LANGU
AND PRODH EQ IT_DATA-PRDHA+0(10). LV_NAME

= IT_DATA-MATNR.
*&L2.Get TXT for model descibe
CALL FUNCTION 'READ_TEXT'
EXPORTING
CLIENT = SY-MANDT
ID = 'GRUN' "P_TEXTID
LANGUAGE = SY-LANGU NAME
= LV_NAME OBJECT
= 'MATERIAL'
TABLES
LINES = LT_LINE
EXCEPTIONS
ID = 1
LANGUAGE = 2 NAME
= 3 NOT_FOUND
= 4 OBJECT
= 5 REFERENCE_CHECK
= 6 WRONG_ACCESS_TO_ARCHIVE
= 7
OTHERS = 8.

IF SY-SUBRC EQ 0.
LOOP AT LT_LINE INTO LWA_LINE.
CONCATENATE LWA_LINE-TDLINE LV_TEXT INTO LV_TEXT.
ENDLOOP. IT_DATA
-XHTXT = LV_TEXT.
REPLACE '<(>&<)>' IN IT_DATA-XHTXT WITH '&'.
CLEAR:LT_LINE[], LT_LINE, LV_TEXT.
ENDIF.

*&L3.Get long txt.
CALL FUNCTION 'READ_TEXT'
EXPORTING
CLIENT = SY-MANDT
ID = 'IVER' "P_TEXTID
LANGUAGE = SY-LANGU NAME
= LV_NAME OBJECT
= 'MATERIAL'
TABLES
LINES = LT_LINE
EXCEPTIONS
ID = 1
LANGUAGE = 2 NAME
= 3 NOT_FOUND
= 4 OBJECT
= 5 REFERENCE_CHECK
= 6 WRONG_ACCESS_TO_ARCHIVE
= 7
OTHERS = 8.

IF SY-SUBRC EQ 0.
LOOP AT LT_LINE INTO LWA_LINE.
CONCATENATE LWA_LINE-TDLINE LV_TEXT INTO LV_TEXT.
ENDLOOP. IT_DATA
-XHTXT1 = LV_TEXT.
REPLACE '<(>&<)>' IN IT_DATA-XHTXT1 WITH '&'.
CLEAR:LT_LINE[], LT_LINE, LV_TEXT.
ENDIF.

*ENDIF.

*&L4.get dinsh: IT_DATA
-DINSH = 0.

*&L5.get umrez1 IT_DATA
-UMREZ1 = IT_DATA-UMREZ.
CONDENSE IT_DATA-UMREZ1 NO-GAPS.

CONCATENATE IT_DATA-UMREZ1 ' pc(s)' INTO IT_DATA-UMREZ1.

*&L6.get statu:
READ TABLE LT_ZSTATU WITH KEY ZMDNM = IT_DATA-ZMDNM BINARY SEARCH.
IF SY-SUBRC EQ 0. IT_DATA
-STATU = 'New'.
ENDIF.
*&L7.get us$
READ TABLE LT_A005 WITH KEY MATNR = IT_DATA-MATNR BINARY SEARCH.
IF SY-SUBRC EQ 0. IT_DATA
-KONWA = LT_A005-KONWA.
*& Convert units
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
EXPORTING
INPUT = LT_A005-KMEIN
LANGUAGE = '1'
IMPORTING
* LONG_TEXT =
OUTPUT = LT_A005-KMEIN
* SHORT_TEXT =
EXCEPTIONS UNIT_NOT_FOUND
= 1
OTHERS = 2.
IF LT_A005-KMEIN EQ 'PC'. IT_DATA
-KBETR = LT_A005-KBETR.
ELSE. IT_DATA
-KBETR = LT_A005-KBETR / IT_DATA-UMREZ."转换单位
ENDIF.
ENDIF.

*&L8.get labst
READ TABLE LT_MARD3 WITH KEY MATNR = IT_DATA-MATNR BINARY SEARCH.
IF SY-SUBRC EQ 0. IT_DATA
-KCNGE = LT_MARD3-LABST.
ENDIF.

READ TABLE LT_ZSD131 WITH KEY MATNR = IT_DATA-MATNR BINARY SEARCH.
IF SY-SUBRC EQ 0. IT_DATA
-KCNGE = IT_DATA-KCNGE - LT_ZSD131-WJENG.
ENDIF.

*&L9.get last3 order qty:--->这里的是以CAR 为单位的数量
READ TABLE LT_VBAK1 WITH KEY MATNR = IT_DATA-MATNR INDEX = 3.
IF SY-SUBRC EQ 0. IT_DATA
-LAST3 = LT_VBAK1-KWMENG * IT_DATA-UMREZ.

ENDIF.

*&L10 get last2 order qty:
READ TABLE LT_VBAK1 WITH KEY MATNR = IT_DATA-MATNR INDEX = 2.
IF SY-SUBRC EQ 0. IT_DATA
-LAST2 = LT_VBAK1-KWMENG * IT_DATA-UMREZ.
ENDIF.

*&L11 get last1 order qty:
READ TABLE LT_VBAK1 WITH KEY MATNR = IT_DATA-MATNR INDEX = 1.
IF SY-SUBRC EQ 0. IT_DATA
-LAST1 = LT_VBAK1-KWMENG * IT_DATA-UMREZ.
ENDIF.

IF RB_PDF EQ 'X'. L_KBETR
= IT_DATA-KBETR. L_KONWA
= IT_DATA-KONWA.
MOVE-CORRESPONDING IT_DATA TO IT_DATAP.
CONCATENATE L_KBETR L_KONWA INTO IT_DATAP-KBETR SEPARATED BY SPACE.
COLLECT IT_DATAP.
DELETE IT_DATAP WHERE ZONGL IS INITIAL.
ELSE.
MOVE-CORRESPONDING IT_DATA TO IT_DATAC.
COLLECT IT_DATAC.
DELETE IT_DATAC WHERE ZONGL IS INITIAL.
ENDIF.

MODIFY IT_DATA." INDEX SY-TABIX.
ENDLOOP.

* DESCRIBE TABLE LT_data LINES L_LEN.

ENDFORM. " frm_get_data

*&---------------------------------------------------------------------*
*& Form frm_alv_output
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_ALV_OUTPUT .
DATA: L_NN TYPE I.
DATA: LS_EVENT TYPE SLIS_ALV_EVENT.

*&===>fieldcat
DEFINE ADD_FIELDCAT.
CLEAR WA_FIELDCAT. WA_FIELDCAT
-FIELDNAME = &1. WA_FIELDCAT
-SELTEXT_L = &2. WA_FIELDCAT
-KEY = &3. WA_FIELDCAT
-COL_POS = L_NN + 1. WA_FIELDCAT
-JUST = &4. WA_FIELDCAT
-OUTPUTLEN = &5. WA_FIELDCAT
-FIX_COLUMN = &6. WA_FIELDCAT
-NO_ZERO = &7. WA_FIELDCAT
-EDIT = &8.
APPEND WA_FIELDCAT TO GT_FIELDCAT.
END-OF-DEFINITION.

CLEAR GT_FIELDCAT.
REFRESH GT_FIELDCAT. ADD_FIELDCAT

'MATNR' 'Material number' '' '' '18' '' '' ''."1 ADD_FIELDCAT
'STATU' 'Status' '' '' '' '' '' ''."1 ADD_FIELDCAT

'ZONGL' 'Main Category' '' '' '' '' '' ''."2 ADD_FIELDCAT
'FENEL' 'Sub Category' '' '' '' '' '' ''."3 ADD_FIELDCAT
'ZMDNM' 'Model name' '' '' '' '' '' ''."5 ADD_FIELDCAT
'UMREZ' 'PC per MC' '' '' '' '' '' ''."4 ADD_FIELDCAT

'XHTXT' 'Item text' '' '' '' '' '' ''."5 ADD_FIELDCAT
'XHTXT1' 'Prod Subhead' '' '' '' '' '' ''."5

* ADD_FIELDCAT 'KBETR' 'US$' '' '' '' '' '' ''."7

ADD_FIELDCAT
'KBETR' 'Price' '' '' '' '' '' ''."7 ADD_FIELDCAT
'KONWA' 'Currency' '' '' '' '' '' ''."7 ADD_FIELDCAT
'KCNGE' 'Current Availabile' '' '' '' '' '' ''."7 ADD_FIELDCAT

'DINSH' 'Ordered Qty' '' '' '' '' '' ''."8 ADD_FIELDCAT
'LAST1' 'Last-1 Ordered Qty' '' '' '' '' '' ''."9 ADD_FIELDCAT
'LAST2' 'Last-2 Ordered Qty' '' '' '' '' '' ''."10 ADD_FIELDCAT
'LAST3' 'Last-3 Ordered Qty' '' '' '' '' '' ''."11

*&===>layout GW_LAYOUT
-COLWIDTH_OPTIMIZE = 'X'. GW_LAYOUT
-ZEBRA = 'X'. GW_LAYOUT
-DETAIL_POPUP = 'X'.

*&===>Way1:
*&use event to get frm_top_of_page.
*&===>event
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING I_LIST_TYPE
= 0
IMPORTING ET_EVENTS
= GT_EVENT
EXCEPTIONS LIST_TYPE_WRONG
= 1
OTHERS = 2.
READ TABLE GT_EVENT WITH KEY NAME = SLIS_EV_TOP_OF_PAGE INTO LS_EVENT.
IF SY-SUBRC EQ 0.
MOVE ER_FORMNAME_TOP_OF_PAGE TO LS_EVENT-FORM.
APPEND LS_EVENT TO GT_EVENT.
ENDIF.

*&TOP OF PAGE.
PERFORM COMMENT_BUILD USING TOP_OF_PAGE[].

*& 调用函数显示ALV列表
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING I_CALLBACK_PROGRAM
= SY-REPID I_SAVE
= 'A' IS_LAYOUT
= GW_LAYOUT IT_FIELDCAT
= GT_FIELDCAT IT_EVENTS
= GT_EVENT
TABLES T_OUTTAB
= IT_DATA
EXCEPTIONS PROGRAM_ERROR
= 1
OTHERS = 2.

ENDFORM. " frm_alv_output

*&---------------------------------------------------------------------*
*& Form comment_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->U_ER_LIST_TOP_OF_PAGE text
*----------------------------------------------------------------------*
FORM COMMENT_BUILD USING U_ER_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER.

DATA: LS_LINE TYPE SLIS_LISTHEADER, LV_CUST
TYPE STRING, LV_STR
TYPE STRING.

CLEAR: LS_LINE, LV_STR. LS_LINE
-TYP = 'H'.

SELECT SINGLE NAME1
INTO LV_CUST
FROM KNA1
WHERE KUNNR EQ P_KUNNR.

CONCATENATE LV_CUST 'Sold-to Price list' INTO LV_STR. LS_LINE
-INFO = LV_STR.
APPEND LS_LINE TO U_ER_LIST_TOP_OF_PAGE.

CLEAR: LS_LINE, LV_STR. LS_LINE
-TYP = 'S'.
CONCATENATE 'Valid as:' P_ERDAT+0(4) '-' P_ERDAT+4(2) '-' P_ERDAT+6(2) INTO LV_STR.

LS_LINE
-KEY = LV_STR.
APPEND LS_LINE TO U_ER_LIST_TOP_OF_PAGE.

ENDFORM. " COMMENT_BUILDS

*---------------------------------------------------------------------*
* FORM top_of_page *
*---------------------------------------------------------------------*
FORM TOP_OF_PAGE.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING I_LOGO
= 'ENJOYSAP_LOGO'"'ZMG_LOGO_NEW' IT_LIST_COMMENTARY
= TOP_OF_PAGE.

ENDFORM. "TOP_OF_PAGE

*&---------------------------------------------------------------------*
*& Form FRM_PDF_OUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_PDF_OUT.
*& get print data:
*& one brad ,on pdf fiel, use a new logos
DATA:BEGIN OF LT_MATKL OCCURS 0, MATKL
LIKE MARA-MATKL,
END OF LT_MATKL.
DATA: LT_DATAP LIKE TABLE OF IT_DATAP WITH HEADER LINE.
*& smartform print:
DATA: FM_NAME TYPE RS38L_FNAM,"smartforms 函数 W_CTRLOP
TYPE SSFCTRLOP, "Smart Forms: 控制结构 "W_CONTROL W_COMPOP
TYPE SSFCOMPOP, "SAP Smart Forms: 智能写作器 (传输) 选项"W_OUTPUT. W_RETURN
TYPE SSFCRESCL. "Smart Forms: 表格打印结束时返回值
DATA: H_FILE TYPE STRING, "保存文件的名字 P_PATH
TYPE STRING. "保存路径
DATA: P_MATKL LIKE MARA-MATKL.
DATA: JOB_OUTPUT_INFO TYPE SSFCRESCL, JOB_OUTPUT_OPTIONS
TYPE SSFCRESOP, GT_TABLE
LIKE STANDARD TABLE OF SPFLI.
*&pdf打印需要用到的参数
DATA L_LINES TYPE TABLE OF TLINE WITH HEADER LINE.
DATA L_DOCS TYPE TABLE OF DOCS.
DATA LEN TYPE I.
DATA: G_PDF_XSTRING TYPE XSTRING.

LOOP AT IT_DATAP. IT_DATAP
-KUNNR = P_KUNNR.
SELECT SINGLE VALUE
INTO IT_DATAP-KCSHU
FROM ZT0002
WHERE FROMB LE IT_DATAP-KCNGE
AND FROME GE IT_DATAP-KCNGE. LT_MATKL

-MATKL = IT_DATAP-MATKL.
COLLECT LT_MATKL.
MODIFY IT_DATAP TRANSPORTING KUNNR. "MATKL.
ENDLOOP.
SORT IT_DATAP BY MATKL. LT_DATAP[]

= IT_DATAP[].

REFRESH IT_DATAP[]. W_CTRLOP

-GETOTF = 'X'."OTF 表的返回 W_CTRLOP
-NO_DIALOG = 'X'."关闭打印对话框 W_CTRLOP
-LANGU = SY-LANGU. W_COMPOP
-TDDEST = 'LP01'. "LP01 W_COMPOP
-TDPRINTER = 'CNSAPWIN'. "LP01 W_COMPOP
-TDIMMED = 'X'. W_COMPOP
-TDDELETE = ' '. W_COMPOP
-TDTITLE = SY-TITLE.

*&deal with brand 1 by 1.
LOOP AT LT_MATKL.
* READ TABLE LT_MATKL INDEX 1. IT_DATAP[]
= LT_DATAP[]. P_MATKL
= LT_MATKL-MATKL.
DELETE IT_DATAP[] WHERE MATKL NE LT_MATKL-MATKL.

*&smartforms 函数名字转换
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING FORMNAME
= 'ZOK_CUSTOMER6'
IMPORTING FM_NAME
= FM_NAME.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*&smartforms打印 ,将打印的值输入到W_RETURN-OTFDATA[]中
CALL FUNCTION FM_NAME
EXPORTING CONTROL_PARAMETERS
= W_CTRLOP OUTPUT_OPTIONS
= W_COMPOP USER_SETTINGS
= 'X'" USER_SETTINGS = 'X'
IMPORTING JOB_OUTPUT_INFO
= W_RETURN.

* CALL FUNCTION FM_NAME
* EXPORTING
* CONTROL_PARAMETERS = W_CTRLOP
* IMPORTING
* JOB_OUTPUT_INFO = W_RETURN
* JOB_OUTPUT_OPTIONS = JOB_OUTPUT_OPTIONS
* TABLES
* IT_TABLE = GT_TABLE
* EXCEPTIONS
* FORMATTING_ERROR = 1
* INTERNAL_ERROR = 2
* SEND_ERROR = 3
* USER_CANCELED = 4
* OTHERS = 5.

*&W3.call fc 'convert_otf' to get g_pdf_xstring up files.
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
FORMAT = 'PDF'
* MAX_LINEWIDTH = 132
* ARCHIVE_INDEX = ' '
* COPYNUMBER = 0
* ASCII_BIDI_VIS2LOG = ' '
* PDF_DELETE_OTFTAB = ' '
* PDF_USERNAME = ' '
* PDF_PREVIEW = ' '
* USE_CASCADING = ' '
IMPORTING BIN_FILESIZE
= LEN BIN_FILE
= G_PDF_XSTRING
TABLES OTF
= W_RETURN-OTFDATA[]
LINES = L_LINES[]
EXCEPTIONS ERR_MAX_LINEWIDTH
= 1 ERR_FORMAT
= 2 ERR_CONV_NOT_POSSIBLE
= 3 ERR_BAD_OTF
= 4
OTHERS = 5.

IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* INTO G_DUMMY
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
* PERFORM protocol_update.
* RETURN.
ENDIF.
CONCATENATE P_SEFILE P_KUNNR P_MATKL '_Price list.pdf' INTO S_FILE. "addrss
OPEN DATASET S_FILE FOR OUTPUT IN BINARY MODE." MESSAGE L_MESSAGE_DB.
IF SY-SUBRC EQ 0.
TRANSFER G_PDF_XSTRING TO S_FILE.
CLOSE DATASET S_FILE.
ELSE.
MESSAGE 'OPEN ERROR!!' TYPE 'E'.
STOP.
ENDIF.

ENDLOOP.

ENDFORM. " FRM_PDF_OUT

*&---------------------------------------------------------------------*
*& Form FRM_CSV_OUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_CSV_OUT .
DATA:UMREZ(20), DINSH
(20), LAST1
(20), LAST2
(20), LAST3
(20).

LOOP AT IT_DATAC.
CONCATENATE '"' IT_DATAC-ZONGL '"' INTO IT_DATAC-ZONGL.
CONCATENATE '"' IT_DATAC-FENEL '"' INTO IT_DATAC-FENEL.

SELECT SINGLE VALUE
INTO IT_DATAC-KCSHU
FROM ZT0002
WHERE FROMB LE IT_DATAC-KCNGE
AND FROME GE IT_DATAC-KCNGE.
MODIFY IT_DATAC TRANSPORTING ZONGL FENEL KCSHU.
ENDLOOP.

SORT IT_DATAC BY ZONGL FENEL ZMDNM.

CONCATENATE P_SEFILE P_KUNNR '_Order Form.csv' INTO S_FILE. "服务器保存的目录和文

件名
OPEN DATASET S_FILE FOR OUTPUT IN TEXT MODE ENCODING DEFAULT . "打开文件
IF SY-SUBRC = 0.

CONCATENATE 'Status' 'Main Category' 'Sub Category' 'Model Name' 'PC per MC'

'Current Available' 'Ordered Qty' 'Last-1' 'Last-2' 'Last-3'
INTO LINE SEPARATED BY ','.
TRANSFER LINE TO S_FILE .

LOOP AT IT_DATAC. DINSH
= IT_DATAC-DINSH. LAST1
= IT_DATAC-LAST1. LAST2
= IT_DATAC-LAST2. LAST3
= IT_DATAC-LAST3.

CONCATENATE IT_DATAC-STATU IT_DATAC
-ZONGL IT_DATAC
-FENEL IT_DATAC
-ZMDNM IT_DATAC
-UMREZ1 IT_DATAC
-KCSHU DINSH LAST1 LAST2

LAST3

INTO LINE SEPARATED BY ','.

TRANSFER LINE TO S_FILE .
IF SY-SUBRC NE 0 .
MESSAGE 'ERROR!!' TYPE 'E'.
STOP.
ENDIF.
CLEAR LINE .
ENDLOOP.
CLOSE DATASET S_FILE.
ELSE.
MESSAGE 'OPEN ERROR!!' TYPE 'E'.
STOP.
ENDIF.

ENDFORM. " FRM_CSV_OUT

上一篇:数据库导出到excel


下一篇:虚机下Ubuntu与Win7文件共享