Posted: Sat Sep 20, 2008 10:05 pm Post subject: Create contract from Quotation
Code:
REPORT zinsd_quot_cont.
TYPES : BEGIN OF ty_vbak,
vbeln TYPE vbeln_va,
vkorg TYPE vkorg,
vtweg TYPE vtweg,
spart TYPE spart,
vkbur TYPE vkbur,
vkgrp TYPE vkgrp,
angdt TYPE angdt_v,
bnddt TYPE bnddt,
kunnr TYPE kunnr,
kwmeng TYPE kwmeng,
meins TYPE meins,
kunwe TYPE kunnr,
name1 TYPE name1_gp ,
name2 TYPE name1_gp ,
sel(1),
expand(1),
salesdocument TYPE bapivbeln-vbeln,
message TYPE bapi_msg,
END OF ty_vbak.
DATA : w_vbak TYPE ty_vbak,
t_vbak TYPE TABLE OF ty_vbak.
DATA : w_update TYPE ty_vbak,
t_update TYPE TABLE OF ty_vbak.
TYPES : BEGIN OF ty_kna1,
kunnr TYPE kunnr,
name1 TYPE name1_gp ,
END OF ty_kna1.
DATA : w_kna1 TYPE ty_kna1,
t_kna1 TYPE TABLE OF ty_kna1.
TYPES : BEGIN OF ty_vbap,
vbeln TYPE vbeln_va,
posnr TYPE posnr_va,
matnr TYPE matnr,
matkl TYPE matkl,
werks TYPE werks_ext,
kwmeng TYPE kwmeng,
meins TYPE meins,
mvgr5 TYPE mvgr5,
maktx TYPE maktx,
END OF ty_vbap.
DATA : w_vbap TYPE ty_vbap,
t_vbap TYPE TABLE OF ty_vbap.
TYPES : BEGIN OF ty_makt,
matnr TYPE matnr,
maktx TYPE maktx,
END OF ty_makt.
DATA : w_makt TYPE ty_makt,
t_makt TYPE TABLE OF ty_makt.
TYPES : BEGIN OF ty_sum,
vbeln TYPE vbeln_va,
kwmeng TYPE kwmeng,
END OF ty_sum.
DATA : w_sum TYPE ty_sum,
t_sum TYPE TABLE OF ty_sum.
TYPES : BEGIN OF ty_vbpa,
vbeln TYPE vbeln,
posnr TYPE posnr,
parvw TYPE parvw,
kunnr TYPE kunnr,
END OF ty_vbpa.
DATA : w_vbpa TYPE ty_vbpa,
t_vbpa TYPE TABLE OF ty_vbpa.
TYPES : BEGIN OF ty_vbup,
vbeln TYPE vbeln,
posnr TYPE posnr ,
gbsta TYPE gbsta ,
END OF ty_vbup.
DATA : w_vbup TYPE ty_vbup,
t_vbup TYPE TABLE OF ty_vbup.
TYPE-POOLS slis.
DATA: t_fieldcatalog TYPE slis_t_fieldcat_alv,
w_fieldcatalog TYPE slis_fieldcat_alv,
w_layout TYPE slis_layout_alv,
gs_keyinfo TYPE slis_keyinfo_alv.
DATA: g_tabname_header TYPE slis_tabname,
g_tabname_item TYPE slis_tabname.
* data definition
* Batchinputdata of single transaction
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.
* messages of call transaction
DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
DATA : g_lines TYPE i.
*Selection Screen
TABLES : vbak,vbap.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-000.
SELECT-OPTIONS s_vkorg FOR vbak-vkorg. " Sales Org
SELECT-OPTIONS s_vtweg FOR vbak-vtweg. " Dist Channel
SELECT-OPTIONS s_spart FOR vbak-spart. " Division
SELECT-OPTIONS s_vkbur FOR vbak-vkbur. " Sales Off
SELECT-OPTIONS s_vkgrp FOR vbak-vkgrp. " Sales Grp
SELECT-OPTIONS s_matkl FOR vbap-matkl. " Mat Grp
SELECT-OPTIONS s_werks FOR vbap-werks. " Plant
SELECT-OPTIONS s_period FOR vbak-angdt. " Sales Off
SELECTION-SCREEN END OF BLOCK b1.
*---------------------------------------------------------------------
** Form f_getdata
*---------------------------------------------------------------------
* text
* --> p1 text
*
FORM f_getdata .
*Sales Header
SELECT
vbeln
vkorg
vtweg
spart
vkbur
vkgrp
angdt
bnddt
kunnr
INTO TABLE t_vbak
FROM vbak
WHERE
vkorg IN s_vkorg " Sales Org
AND vtweg IN s_vtweg " Dist Channel
AND spart IN s_spart " Division
AND vkbur IN s_vkbur " Sales Off
AND vkgrp IN s_vkgrp " Sales Grp
AND angdt IN s_period
AND bnddt IN s_period
AND auart = 'ZQMO'
AND vbtyp = 'B'.
*CUST NAME
SELECT
kunnr
name1
INTO TABLE t_kna1
FROM kna1
FOR ALL ENTRIES IN t_vbak
WHERE
kunnr = t_vbak-kunnr
.
*Sales Item
SELECT
vbeln
posnr
matnr
matkl
werks
kwmeng
meins
mvgr5
INTO TABLE t_vbap
FROM vbap
FOR ALL ENTRIES IN t_vbak
WHERE
vbeln = t_vbak-vbeln
AND matkl IN s_matkl " Mat Grp
AND werks IN s_werks " Plant
.
*Partners
SELECT
vbeln
posnr
parvw
kunnr
INTO TABLE t_vbpa
FROM vbpa
FOR ALL ENTRIES IN t_vbak
WHERE
vbeln = t_vbak-vbeln.
IF t_vbpa[] IS NOT INITIAL.
SELECT
kunnr
name1
APPENDING TABLE t_kna1
FROM kna1
FOR ALL ENTRIES IN t_vbpa
WHERE
kunnr = t_vbpa-kunnr
.
ENDIF.
*Status - Sales Doc
SELECT
vbeln
posnr
gbsta
INTO TABLE t_vbup
FROM vbup
FOR ALL ENTRIES IN t_vbap
WHERE
vbeln = t_vbap-vbeln
AND posnr = t_vbap-posnr
AND gbsta NE 'C'.
SELECT
matnr
maktx
INTO TABLE t_makt
FROM makt
FOR ALL ENTRIES IN t_vbap
WHERE
matnr = t_vbap-matnr
AND spras = 'E'.
ENDFORM. " f_getdata
*---------------------------------------------------------------------
** Form f_initdata
*---------------------------------------------------------------------
* text
* --> p1 text
*
FORM f_initdata .
SORT t_vbap BY vbeln DESCENDING.
*delete all closed stat items
LOOP AT t_vbap INTO w_vbap.
READ TABLE t_vbup INTO w_vbup
WITH KEY
vbeln = w_vbap-vbeln
posnr = w_vbap-posnr.
IF sy-subrc
DELETE t_vbap.
CONTINUE.
ELSE.
READ TABLE t_makt INTO w_makt
WITH
KEY matnr = w_vbap-matnr.
IF sy-subrc = 0.
w_vbap-maktx = w_makt-maktx.
MODIFY t_vbap FROM w_vbap.
ENDIF.
ENDIF.
ENDLOOP.
*find the total quantity
LOOP AT t_vbap INTO w_vbap.
w_sum-vbeln = w_vbap-vbeln.
w_sum-kwmeng = w_vbap-kwmeng.
COLLECT w_sum INTO t_sum.
CLEAR w_sum.
ENDLOOP.
*populate header
LOOP AT t_vbak INTO w_vbak.
* Quantity - Total
READ TABLE t_sum INTO w_sum
WITH KEY
vbeln = w_vbak-vbeln.
IF sy-subrc = 0.
w_vbak-kwmeng = w_sum-kwmeng.
ELSE.
CLEAR w_vbak-kwmeng.
ENDIF.
* UoM
READ TABLE t_vbap INTO w_vbap
WITH KEY
vbeln = w_vbak-vbeln.
IF sy-subrc = 0.
w_vbak-meins = w_vbap-meins.
ELSE.
CLEAR w_vbak-meins.
ENDIF.
* Partner
READ TABLE t_vbpa INTO w_vbpa
WITH KEY
vbeln = w_vbak-vbeln
parvw = 'WE'.
IF sy-subrc = 0.
w_vbak-kunwe = w_vbpa-kunnr.
ELSE.
CLEAR w_vbak-kunwe.
ENDIF.
* SHIP TO PARTY NAME
READ TABLE t_kna1 INTO w_kna1
WITH KEY
kunnr = w_vbak-kunwe.
IF sy-subrc = 0.
w_vbak-name2 = w_kna1-name1.
ENDIF.
* CUST NAM - SOLD TO PARTY
READ TABLE t_kna1 INTO w_kna1
WITH KEY
kunnr = w_vbak-kunnr.
IF sy-subrc = 0.
w_vbak-name1 = w_kna1-name1.
ENDIF.
MODIFY t_vbak FROM w_vbak.
ENDLOOP.
DELETE t_vbak WHERE kwmeng IS INITIAL.
SORT t_vbak BY vbeln DESCENDING.
SORT t_vbap BY vbeln DESCENDING posnr ASCENDING.
ENDFORM. " f_initdata
*---------------------------------------------------------------------
** Form f_createalv
*---------------------------------------------------------------------
* Create Field Cat.
* --> p1 text
*
*---------------------------------------------------------------------
** Form set_pf_status
*---------------------------------------------------------------------
* PF stat
* -->RT_EXTAB text
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'ZINSD_QUOT_CONT_ST'.
ENDFORM. "set_pf_status
*---------------------------------------------------------------------
** Form user_command
*---------------------------------------------------------------------
* Process List UCOMM
* -->R_UCOMM text
* -->RS_SELFIELD text
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
REFRESH t_update.
IF r_ucomm = 'EXIT'.
LEAVE PROGRAM.
ENDIF.
IF r_ucomm = 'EXECUTE'.
LOOP AT t_vbak INTO w_update
WHERE
sel = 'X'
AND salesdocument = ' '.
APPEND w_update TO t_update.
ENDLOOP.
IF t_update[] IS NOT INITIAL.
LOOP AT t_update INTO w_update.
* PERFORM f_bapi_contract_createfromdata USING w_update.
PERFORM f_bdc_contract_from_quotation.
MODIFY t_update FROM w_update.
ENDLOOP.
ELSE.
ENDIF.
PERFORM f_dispalv.
ENDIF.
IF r_ucomm = '*IC1'.
IF rs_selfield-sel_tab_field = 't_vbak-VBELN'.
SET PARAMETER ID 'AGN' FIELD rs_selfield-value.
CALL TRANSACTION 'VA23' AND SKIP FIRST SCREEN.
ENDIF.
IF rs_selfield-sel_tab_field = 't_vbak-SALESDOCUMENT' AND
rs_selfield-value NE ' '.
SET PARAMETER ID 'KTN' FIELD rs_selfield-value.
CALL TRANSACTION 'VA43' AND SKIP FIRST SCREEN.
ENDIF.
ENDIF.
ENDFORM. "user_command
*---------------------------------------------------------------------
** Form F_BAPI_CONTRACT_CREATEFROMDATA
*---------------------------------------------------------------------
* BAPI CALL
* -->P_W_UPDATE text
FORM f_bapi_contract_createfromdata USING p_w_update STRUCTURE w_update .
DATA : w_contract_header_in TYPE bapisdhd1 ,
w_contract_header_inx TYPE bapisdhd1x ,
t_contract_items_in TYPE TABLE OF bapisditm WITH HEADER LINE,
t_contract_items_inx TYPE TABLE OF bapisditmx WITH HEADER LINE,
t_contract_partners TYPE TABLE OF bapiparnr WITH HEADER LINE,
t_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
READ TABLE t_vbak INTO w_vbak WITH KEY vbeln = w_update-vbeln.
IF sy-subrc = 0.
MODIFY t_vbak INDEX sy-tabix FROM w_update transporting message
salesdocument.
ENDIF.
ENDFORM. " f_bdc_contract_from_Quotation
* Start new screen *
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "bdc_dynpro
* Insert field *
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
APPEND bdcdata.
ENDFORM. "bdc_field
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.