Quantcast
Channel: SCN : All Content - ABAP Development
Viewing all articles
Browse latest Browse all 8332

Creating sales order (credit note) with reference to an invoice. Function SD_SALESDOCUMENT_CREATE

$
0
0

Hi Experts,

 

maybe some of you are able to give me a hint on my problem.

 

I try to create a sales oder with reference to an existing invoice.

The sales order should be of type Credit note (category VBTYP K).

Everthing goes fine except of the net_value and the SHKZG flag as well as the VBTYP. VBTYP is L instead of K. This results in a negative NETWR.

 

When i create a sales order via VA01 (with reference to the invoice) the document is created successfully with VBTYP = K and positive values.

 

Her is my coding for filling SD_SALESDOCUMENT_CREATE:

 

 

HEADER:

  ls_order_header_inx-updateflag = 'I'.

    ls_order_header_inx-doc_type   = 'X'.

*    ls_order_header_inx-sd_doc_cat = 'X'.

    ls_order_header_inx-sales_org = 'X'.

    ls_order_header_inx-distr_chan = 'X'.

    ls_order_header_inx-division = 'X'.

    ls_order_header_inx-ref_doc = 'X'.

    ls_order_header_inx-refdoc_cat = 'X'.

 

      ls_order_header_in-doc_type   = 'ZXXX'.                   

    ls_order_header_in-sales_org  = ls_vbrk-vkorg.      

    ls_order_header_in-distr_chan = ls_vbrk-vtweg.              

    ls_order_header_in-division   = ls_vbrk-spart.            

*    ls_order_header_in-sales_org  = ls_vbrk-sales_org.

*    ls_order_header_in-distr_chan = ls_vbrk-distr_chan.

*    ls_order_header_in-division   = ls_header_out-division.

    ls_order_header_in-bill_date  = lv_vdatu. "vbdkr-fkdat.           

    ls_order_header_in-price_date = ls_vbrk-fkdat. "vbdkr-fkdat.   

    ls_order_header_in-ord_reason = p_tvpod-grund.

    ls_order_header_in-purch_no_c = 'TEST'.

 

    ls_order_header_in-currency   = ls_vbrk-waerk.

 

    ls_order_header_in-ref_doc_l  = ls_vbrk-vbeln.        

    ls_order_header_in-ref_doc    = ls_vbrk-vbeln.       

    ls_order_header_in-refdoc_cat = ls_vbrk-vbtyp.     

 

PARTNER:

 

READ TABLE lt_order_partners_out INTO ls_order_partners_out

                                    WITH KEY sd_doc = p_vbeln-vbeln_zmta

                                             partn_role = 'AG'.

  IF sy-subrc = 0.

    ls_order_partners-partn_role = 'AG'.

    ls_order_partners-partn_numb   = ls_order_partners_out-customer.

    APPEND ls_order_partners TO lt_order_partners.

  ENDIF.

 

 

ITEMS:

 

LOOP AT lt_order_items_out INTO ls_order_items_out .

 

      ls_order_items_in-itm_number  = ls_order_items_out-itm_number.

***Material

      ls_order_items_in-material    = ls_order_items_out-material.

 

 

***Calculation

      LOOP AT gt_lips INTO ls_lips WHERE uecha EQ ls_order_items_out-itm_number.

        READ TABLE gt_tvpod INTO ls_tvpod_tmp WITH KEY vbeln = p_vbeln-vbeln_lf posnr = ls_lips-posnr.

        IF sy-subrc EQ 0.

          ADD ls_tvpod_tmp-lfimg_diff TO ls_order_items_in-target_qty.

        ENDIF.

      ENDLOOP.

      IF sy-subrc NE 0.

        READ TABLE gt_tvpod INTO ls_tvpod WITH KEY vbeln = p_vbeln-vbeln_lf posnr = ls_order_items_out-itm_number.

        IF sy-subrc EQ 0.

          ls_order_items_in-target_qty  = ls_tvpod-lfimg_diff.

        ENDIF.

      ENDIF.

 

      ls_order_items_in-sales_unit = ls_tvpod-vrkme. 

      ls_order_items_in-target_qu  = ls_tvpod-vrkme.

 

 

      ls_order_items_in-ref_doc     = p_vbeln-vbeln.                              "invoice reference

      ls_order_items_in-ref_doc_it  = ls_order_items_out-itm_number.

      ls_order_items_in-ref_doc_ca  = ls_vbrk-vbtyp. "'M'.

 

*      ls_order_items_in-refobjkey   = p_vbeln-vbeln.

*      ls_order_items_in-item_categ  = 'ZXXX'.

 

       APPEND ls_order_items_in TO lt_order_items_in.

      CLEAR: ls_order_items_in.

 

       ls_order_items_inx-updateflag = 'I'.

      ls_order_items_inx-itm_number = 'X'.

      ls_order_items_inx-ref_doc    = 'X'.

      ls_order_items_inx-ref_doc_it = 'X'.

      ls_order_items_inx-ref_doc_ca = 'X'.

      ls_order_items_inx-target_qty = 'X'.

      ls_order_items_inx-target_val = 'X'.

      ls_order_items_inx-material   = 'X'.

*      ls_order_items_inx-ITEM_CATEG = 'X'.

      APPEND ls_order_items_inx TO lt_order_items_inx.

 

ENDLOOP.

 

 

CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'

            EXPORTING

              sales_header_in     = ls_order_header_in

              sales_header_inx    = ls_order_header_inx

              business_object     = 'BUS2094'         "<- credit note ????

*             logic_switch        = lv_logic_switch

            IMPORTING

              salesdocument_ex    = lv_salesdocument

*             SALES_HEADER_OUT    =

*             SALES_HEADER_STATUS =

            TABLES

              return              = return

              sales_items_in      = lt_order_items_in

              sales_items_inx     = lt_order_items_inx

*             sales_conditions_in = t_conditions_in

              sales_partners      = lt_order_partners

              items_ex            = lt_order_items_out2.

 

No errors appear in return table!

But NET_VAL in lt_order_items_out2 is negative. And in document flow you see VBTYP is L (debit) instead of K( credit note ).

Furthermore the SHKZG is not set. Via VA01 SHKZG in VBRP is set to 'X'.

 

Hope, someone has a helpful idea!

 

Thank y guys!

 

Best greetings,

Marcel


Viewing all articles
Browse latest Browse all 8332

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>