08-16-2007 5:14 PM
Hi,
I have to create the credit memo based on user selected records. My program is doing it, but I am having two problems,
1)After creating the credit memo it is not clearing those records, if I choose next set it is adding 2nd time selected records to the first time selected records and adding to previous credit memo number.
2)If the credit memo number is already there to a record, it should not allow the same record to create another CM.
Please help me to solve these.
Thanks,
Neelu.
REPORT zsd_creditmemo NO STANDARD PAGE HEADING.
TABLES: zppprice, kna1.
TYPES: BEGIN OF ty_zprice,
mandt TYPE mandt,
vkorg TYPE vkorg,
vtweg TYPE vtweg,
spart TYPE spart,
kunnr TYPE kunnr,
matnr TYPE matnr,
crdate TYPE zcrdate,
efdate TYPE zefdate,
sprice TYPE zprice3,
eprice TYPE zprice4,
eohqty TYPE zquantity1,
aohqty TYPE zquantity2,
auart TYPE auart,
bstkd TYPE bstkd,
werks TYPE werks_d,
augru TYPE augru,
xblnr TYPE xblnr,
crmemo TYPE vbeln,
index TYPE sy-index,
END OF ty_zprice.
DATA: it_ztable TYPE TABLE OF zppprice WITH HEADER LINE,
gt_zprice TYPE TABLE OF zppprice WITH HEADER LINE,
gt1_zprice TYPE TABLE OF zppprice,
gt2_zprice TYPE TABLE OF ty_zprice,
gt3_zprice TYPE TABLE OF ty_zprice,
gt4_zprice TYPE TABLE OF ty_zprice,
gs_zprice TYPE zppprice,
gs_zprice1 TYPE ty_zprice,
ls_zprice TYPE ty_zprice,
wa LIKE zppprice.
DATA: salesdocument TYPE bapivbeln-vbeln.
DATA: header LIKE bapisdhd1.
DATA: headerx LIKE bapisdhd1x.
DATA: item LIKE bapisditm OCCURS 0 WITH HEADER LINE.
DATA: itemx LIKE bapisditmx OCCURS 0 WITH HEADER LINE.
DATA: partner LIKE bapiparnr OCCURS 0 WITH HEADER LINE.
DATA: return1 LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
DATA: lt_schedules_in TYPE STANDARD TABLE OF bapischdl
WITH HEADER LINE.
DATA: lt_schedules_inx TYPE STANDARD TABLE OF bapischdlx
WITH HEADER LINE.
DATA: conditions1 LIKE bapicond OCCURS 0 WITH HEADER LINE.
DATA: order_text LIKE bapisdtext OCCURS 0 WITH HEADER LINE.
DATA: lv_index TYPE sy-index,
v_tabix TYPE sytabix,
chbox(1) TYPE c VALUE ' ',
repid TYPE sy-repid,
lv_itemno TYPE i VALUE 10,
lv_pprice TYPE zprice3,
lv_lines TYPE i,
lv_flg TYPE c,
lv_cnt TYPE i.
repid = sy-repid.
PERFORM displaydata.
AT USER-COMMAND.
CASE sy-ucomm.
WHEN 'CMEMO'.
DESCRIBE TABLE gt_zprice LINES lv_cnt.
* lv_index = 1.
DO lv_cnt TIMES.
lv_index = sy-index + 1.
READ LINE lv_index FIELD VALUE chbox.
IF chbox = 'X'.
READ TABLE gt_zprice INTO gs_zprice1 INDEX sy-index.
IF sy-subrc EQ 0.
gs_zprice1-index = sy-index.
APPEND gs_zprice1 TO gt2_zprice.
CLEAR gs_zprice1.
ENDIF.
* ELSE.
* WRITE: /01(253) 'Please Select At least One Record'.
ENDIF.
clear chbox.
ENDDO.
gt4_zprice[] = gt2_zprice[].
SORT gt2_zprice BY vkorg vtweg spart kunnr.
DELETE ADJACENT DUPLICATES FROM gt2_zprice
COMPARING vkorg vtweg spart kunnr.
DESCRIBE TABLE gt2_zprice LINES lv_lines.
IF lv_lines = 1.
LOOP AT gt4_zprice INTO gs_zprice1.
AT NEW kunnr.
MOVE 'Y' TO lv_flg.
ENDAT.
IF lv_flg EQ 'Y'.
PERFORM headerdata.
PERFORM texts.
CLEAR lv_flg.
ENDIF.
PERFORM itemdata.
APPEND gs_zprice1 TO gt3_zprice.
ENDLOOP.
PERFORM call_function.
PERFORM update_pricetable.
PERFORM errorcheckandcommit.
PERFORM displaydata.
* WRITE: /01(253) 'Please Select Again To Create Another Credit Memo'.
REFRESH gt2_zprice.
CLEAR chbox.
CLEAR lv_lines.
ELSE.
* WRITE: /01(253) 'Please Select Same Customer'.
REFRESH gt2_zprice.
CLEAR chbox.
EXIT.
ENDIF.
ENDCASE.
*&---------------------------------------------------------------------*
*& Form headerdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM headerdata.
* header data
* Sales document type
header-doc_type = gs_zprice1-auart.
headerx-doc_type = 'X'.
* Sales organization
header-sales_org = gs_zprice1-vkorg.
headerx-sales_org = 'X'.
* Distribution channel
header-distr_chan = gs_zprice1-vtweg.
headerx-distr_chan = 'X'.
* Division
header-division = gs_zprice1-spart.
headerx-division = 'X'.
* Customer PO Number
header-purch_no_c = gs_zprice1-bstkd.
headerx-purch_no_c = 'X'.
* Order Reason
header-ord_reason = gs_zprice1-augru.
headerx-ord_reason = 'X'.
* Ref Doc Number
header-ref_doc_l = gs_zprice1-xblnr.
headerx-ref_doc_l = 'X'.
headerx-updateflag = 'I'.
ENDFORM. " headerdata
*&---------------------------------------------------------------------*
*& Form Texts
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM texts.
*Texts
order_text-itm_number = '000000'.
order_text-text_id = '0001'.
order_text-langu = sy-langu.
order_text-text_line = gs_zprice1-xblnr.
APPEND order_text.
ENDFORM. " Texts
*&---------------------------------------------------------------------*
*& Form itemdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM itemdata.
* Partner data
* Sold to
partner-partn_role = 'AG'.
partner-partn_numb = gs_zprice1-kunnr.
APPEND partner.
* ITEM DATA
itemx-updateflag = 'I'.
* Line item number.
item-itm_number = lv_itemno.
itemx-itm_number = 'X'.
* Material
item-material = gs_zprice1-matnr.
itemx-material = 'X'.
* Plant
item-plant = gs_zprice1-werks.
itemx-plant = 'X'.
* Quantity
item-target_qty = gs_zprice1-eohqty.
itemx-target_qty = 'X'.
APPEND item.
APPEND itemx.
*Fill schedule lines
lt_schedules_in-itm_number = lv_itemno.
lt_schedules_in-sched_line = '0001'.
lt_schedules_in-req_qty = gs_zprice1-eohqty.
APPEND lt_schedules_in.
*Fill schedule line flags
lt_schedules_inx-itm_number = lv_itemno.
lt_schedules_inx-sched_line = '0001'.
lt_schedules_inx-updateflag = 'I'.
lt_schedules_inx-req_qty = 'X'.
APPEND lt_schedules_inx.
*Conditions
lv_pprice = gs_zprice1-sprice - gs_zprice1-eprice.
conditions1-itm_number = lv_itemno.
conditions1-cond_type = 'ZCPP'.
conditions1-cond_value = lv_pprice.
conditions1-currency = 'USD'.
conditions1-cond_unit = 'EA'.
conditions1-cond_p_unt = 1.
APPEND conditions1.
lv_itemno = lv_itemno + 10.
ENDFORM. " itemdata
*&---------------------------------------------------------------------*
*& Form call_function
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM call_function.
CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
EXPORTING
sales_header_in = header
sales_header_inx = headerx
IMPORTING
salesdocument_ex = salesdocument
TABLES
return = return1
sales_items_in = item
sales_items_inx = itemx
sales_partners = partner
sales_schedules_in = lt_schedules_in
sales_schedules_inx = lt_schedules_inx
sales_conditions_in = conditions1
sales_text = order_text.
ENDFORM. " call_function
*&---------------------------------------------------------------------*
*& Form errorcheck
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM errorcheckandcommit.
* Check the return table.
LOOP AT return1 WHERE type = 'E' OR type = 'A'.
EXIT.
ENDLOOP.
break singireddyn.
IF sy-subrc = 0.
WRITE: / 'Error in creating document'.
ELSE.
*Commit the work.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
ENDFORM. " errorcheckandcommit
*&---------------------------------------------------------------------*
*& Form update_pricetable
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM update_pricetable.
LOOP AT gt3_zprice INTO ls_zprice.
READ TABLE gt_zprice INTO gs_zprice INDEX ls_zprice-index.
IF sy-subrc EQ 0.
v_tabix = sy-tabix.
gs_zprice-crmemo = salesdocument.
MODIFY gt_zprice
FROM gs_zprice INDEX v_tabix TRANSPORTING crmemo.
ENDIF.
ENDLOOP.
* REFRESH:gt3_zprice.
LOOP AT gt_zprice.
wa-vkorg = gt_zprice-vkorg.
wa-vtweg = gt_zprice-vtweg.
wa-spart = gt_zprice-spart.
wa-kunnr = gt_zprice-kunnr.
wa-matnr = gt_zprice-matnr.
wa-crdate = gt_zprice-crdate.
wa-efdate = gt_zprice-efdate.
wa-sprice = gt_zprice-sprice.
wa-eprice = gt_zprice-eprice.
wa-eohqty = gt_zprice-eohqty.
wa-aohqty = gt_zprice-aohqty.
wa-auart = gt_zprice-auart.
wa-bstkd = gt_zprice-bstkd.
wa-werks = gt_zprice-werks.
wa-augru = gt_zprice-augru.
wa-xblnr = gt_zprice-xblnr.
wa-crmemo = gt_zprice-crmemo.
MODIFY zppprice FROM wa.
CLEAR: wa, zppprice.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form Displaydata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM displaydata.
SET PF-STATUS 'LIST'.
NEW-PAGE LINE-SIZE 253.
SELECT * FROM zppprice
INTO TABLE gt_zprice.
WRITE: /01(253) sy-uline.
LOOP AT gt_zprice INTO gs_zprice.
SELECT SINGLE name1 FROM kna1 INTO kna1-name1
WHERE kunnr = gs_zprice-kunnr.
WRITE: /01 sy-vline,
02 chbox AS CHECKBOX,
04 sy-vline,
05 gs_zprice-vkorg,
10 sy-vline,
11 gs_zprice-vtweg,
14 sy-vline,
15 gs_zprice-spart,
18 sy-vline,
19 gs_zprice-kunnr,
30 sy-vline,
31 kna1-name1,
67 sy-vline,
68 gs_zprice-matnr,
87 sy-vline,
88 gs_zprice-crdate,
98 sy-vline,
99 gs_zprice-efdate,
110 sy-vline,
111 gs_zprice-sprice,
127 sy-vline,
128 gs_zprice-eprice,
145 sy-vline,
146 gs_zprice-eohqty,
157 sy-vline,
158 gs_zprice-aohqty,
169 sy-vline,
170 gs_zprice-auart,
175 sy-vline,
176 gs_zprice-bstkd,
212 sy-vline,
213 gs_zprice-werks,
218 sy-vline,
219 gs_zprice-augru,
223 sy-vline,
224 gs_zprice-xblnr,
241 sy-vline,
242 gs_zprice-crmemo,
253 sy-vline.
* CLEAR zppprice.
ENDLOOP.
WRITE: /01(253) sy-uline.
* CLEAR zppprice.
* CLEAR chbox.
ENDFORM. " Displaydata
08-16-2007 5:30 PM
hi
i think u should clear the output table once the contents r displayed......
08-16-2007 5:30 PM
hi
i think u should clear the output table once the contents r displayed......
08-16-2007 6:24 PM
Hi Prema,
I added 'clear gt_zprice' at the end before the endform "Display Data. I selected record num 10 and created the CM 31003023, next I selected Records 8 and 9 and created the CM. Now CM 31003024 is created but with record numbers 8, 9 & 10 instead of just 8 & 9.
How can I do this change. Please help me.
Thanks,
Neelu.
08-16-2007 6:42 PM
Hello Neelu,
This would be issue at Refresh..
Please use clear command and as well refresh command.
now see the results
Thanks
Seshu
08-16-2007 7:19 PM
Hi Sheshu,
With ' clear gt3_zprice ' it is working fine now.
Can you please help me to add the condition if CM number is already there it should not allow the user to create another CM for that record.
Thanks,
Neelima.
08-16-2007 7:39 PM
Hello Neelu,
Before creating Credit memo ,check the data against database table,if sy-subrc eq 0.
then give message like Credit memo already created.
else.
create credit memo using FM.
How will you check the data ? do you have any other condition.
Thanks
Seshu
08-16-2007 9:19 PM
Hi Sheshu,
I am checking based on the user selected records. gt_zprice has all the data and gt2_zprice has data of user selected records.
Can I write the if condition based on gt2_zprice-crmemo value or is there any other way. If this is correct, how can I write this.
If gt2_zprice-crmemo is not empty
then
exit
else
-
-
the code to create CM
endif.
please help me to implement this condition.
Thanks,
Neelu.
08-16-2007 9:46 PM
08-16-2007 9:47 PM
How you are getting the data gt_zprice ?
gt2_zprice - > this will have user selected records
now once you updated the data using FM. after
delete the data gt2_zprice and as well selected records by comparing gt2_zprice in gt_zprice .
Thanks
Seshu
08-17-2007 12:45 AM
Hi Sheshu,
I am getting data from custom table zppprice.
SELECT * FROM zppprice
INTO TABLE gt_zprice.
it is in display data.
I didnot understand the delete stmt you have mentioned.
it any of the selected records have CM number can I come out of the loop and exit.
Thank you for your help.
Regards,
Neelu.
08-17-2007 2:59 AM
Hello Neelu,
once user select check box and it will create credit memo,so selected records it contains one value would be 'X'.,
After BAPI FM use delete command where field = 'X'. so it will delete those records. so now you have remaining records.
do not use exit or someother command,here you are just deleting the internal table data and so no worries.
<b>it any of the selected records have CM number can I come out of the loop and exit.</b>
look at real time requirement ,some time user will select one check box and create credit memo ,again he will select other check boxes too. so use simple delete command.
Thanks
Seshu