Anmerkung dazu: die Typen ty_* habe ich in SAP nicht gefunden, aber si_* existiert für alle außer KNVV...?solidforms hat geschrieben:Code: Alles auswählen.
FUNCTION z_calculate_price . *"---------------------------------------------------------------------- *"*"Lokale Schnittstelle: *" IMPORTING *" VALUE(IV_KUNNR) TYPE KUNNR DEFAULT '100000' *" VALUE(IV_VKORG) TYPE VKORG DEFAULT '1100' *" VALUE(IV_VTWEG) TYPE VTWEG DEFAULT '10' *" VALUE(IV_SPART) TYPE SPART DEFAULT '10' *" VALUE(IV_WERKS) TYPE WERKS_D DEFAULT '1110' *" VALUE(IV_WAERS) TYPE WAERS DEFAULT 'EUR' *" VALUE(IT_ITEM_IN) TYPE ZITEM01_T OPTIONAL *" EXPORTING *" VALUE(IT_ITEM_OUT) TYPE ZITEM02_T *" EXCEPTIONS *" VKORG_NOT_FOUND *" BUKRS_NOT_FOUND *" WERKS_NOT_FOUND *" KNA1_NOT_FOUND *" KNVV_NOT_FOUND *"---------------------------------------------------------------------- * Deklarationen ------------------------------------------------------- * DATA: ls_item_in TYPE zitem01_s, ls_item_out TYPE zitem02_s, ls_t001 TYPE ty_t001, ls_tvko TYPE ty_tvko, ls_t001w TYPE ty_t001w, ls_kna1 TYPE ty_kna1, ls_knvv TYPE ty_knvv. REFRESH: it_item_out. * TVKO lesen ---------------------------------------------------------- * SELECT SINGLE * INTO CORRESPONDING FIELDS OF ls_tvko FROM tvko WHERE vkorg = iv_vkorg. IF sy-subrc NE 0. RAISE vkorg_not_found. EXIT. ENDIF. * T001 lesen ---------------------------------------------------------- * SELECT SINGLE * INTO CORRESPONDING FIELDS OF ls_t001 FROM t001 WHERE bukrs = ls_tvko-bukrs. IF sy-subrc NE 0. RAISE bukrs_not_found. EXIT. ENDIF. * T001W lesen --------------------------------------------------------- * SELECT SINGLE * INTO CORRESPONDING FIELDS OF ls_t001w FROM t001w WHERE werks = iv_werks. IF sy-subrc NE 0. RAISE werks_not_found. EXIT. ENDIF. * KNA1 lesen ---------------------------------------------------------- * SELECT SINGLE * INTO CORRESPONDING FIELDS OF ls_kna1 FROM kna1 WHERE kunnr = iv_kunnr. IF sy-subrc NE 0. RAISE kna1_not_found. EXIT. ENDIF. * KNVV lesen ---------------------------------------------------------- * SELECT SINGLE * INTO CORRESPONDING FIELDS OF ls_knvv FROM knvv WHERE kunnr = iv_kunnr AND vkorg = iv_vkorg AND vtweg = iv_vtweg AND spart = iv_spart. IF sy-subrc NE 0. RAISE knvv_not_found. EXIT. ENDIF. LOOP AT it_item_in INTO ls_item_in. PERFORM calculate_price USING ls_item_in ls_t001 ls_tvko ls_t001w ls_kna1 ls_knvv iv_waers CHANGING ls_item_out. APPEND ls_item_out TO it_item_out. ENDLOOP. ENDFUNCTION. *----------------------------------------------------------------------* ***INCLUDE LZCALCULATE_PRICEF01 . *----------------------------------------------------------------------* *&---------------------------------------------------------------------* *& Form CALCULATE_PRICE *&---------------------------------------------------------------------* FORM calculate_price USING ps_item_in TYPE zitem01_s ps_t001 TYPE ty_t001 ps_tvko TYPE ty_tvko ps_t001w TYPE ty_t001w ps_kna1 TYPE ty_kna1 ps_knvv TYPE ty_knvv pv_waers TYPE waers CHANGING ps_item_out TYPE zitem02_s. * Deklarationen ------------------------------------------------------ * DATA: lv_spras TYPE spras, lv_bwkey TYPE mbew-bwkey. DATA: ls_komk_i TYPE komk, ls_komp_i TYPE komp. DATA: ls_komk_e TYPE komk, ls_komp_e TYPE komp, ls_mara TYPE ty_mara, ls_mvke TYPE ty_mvke, ls_mbew TYPE ty_mbew, ls_vbpa TYPE vbpavb, ls_mtcom TYPE mtcom, ls_maapv TYPE maapv, lv_vtext TYPE bezei20, lv_vbeln TYPE vbeln, lv_kontr TYPE boole_d. DATA: lt_komv TYPE TABLE OF komv, lt_tkomvd TYPE TABLE OF komvd, lt_vbpa TYPE TABLE OF vbpavb, lv_pltyp TYPE knvv-pltyp, lv_matnr TYPE matnr, lv_kunnr_za TYPE kunnr. * Initialisieren ----------------------------------------------------- * ps_item_out-errormsg = ''. ps_item_out-errorcode = 0. * Materialview lesen ------------------------------------------------- * ls_mtcom-kenng = 'MAAPV'. ls_mtcom-matnr = ps_item_in-matnr. ls_mtcom-vkorg = ps_knvv-vkorg. ls_mtcom-vtweg = ps_knvv-vtweg. ls_mtcom-spras = sy-langu. ls_mtcom-maxtz = 100. CALL FUNCTION 'MATERIAL_READ' EXPORTING schluessel = ls_mtcom IMPORTING matdaten = ls_maapv * RETURN = * MATPER = EXCEPTIONS OTHERS = 25. IF sy-subrc <> 0. ps_item_out-errorcode = 200. CONCATENATE 'material not found: ' ls_mtcom-matnr INTO ps_item_out-errormsg. ENDIF. * Kundenhierarchie lesen --------------------------------------------- * CALL FUNCTION 'SD_CUSTOMER_HIERARCHY_PATH' EXPORTING customer = ps_kna1-kunnr date = sy-datum htype = 'Z' sales_channel = ps_knvv-vtweg sales_division = ps_knvv-spart sales_org = ps_knvv-vkorg TABLES hpath = lt_vbpa EXCEPTIONS hityp_not_exist = 1 node_not_exist = 2 parvw_not_exist = 3 OTHERS = 4. IF sy-subrc <> 0 AND sy-subrc <> 2. ps_item_out-errorcode = 300 + sy-subrc. CASE sy-subrc. WHEN 1. ps_item_out-errormsg = 'Hityp not found: Z'. WHEN 3. ps_item_out-errormsg = 'Parvw not found.'. ENDCASE. ENDIF. * Preisliste ggfs. aus Partner ZA ermitteln -------------------------- * READ TABLE lt_vbpa INTO ls_vbpa WITH KEY posnr = '000000' parvw = 'ZA'. IF sy-subrc = 0. lv_kunnr_za = ls_vbpa-kunnr. SELECT SINGLE pltyp INTO lv_pltyp FROM knvv WHERE kunnr = lv_kunnr_za AND vkorg = ps_knvv-vkorg AND vtweg = ps_knvv-vtweg AND spart = ps_knvv-spart. SELECT SINGLE matnr INTO lv_matnr FROM a953 WHERE kappl = 'V' AND ( kschl = 'ZPRF' OR kschl = 'ZPR0' ) AND pltyp = lv_pltyp AND matnr = ps_item_in-matnr AND kfrst = space AND datbi >= sy-datum. IF NOT lv_pltyp IS INITIAL AND NOT lv_matnr IS INITIAL. ELSE. CLEAR lv_pltyp. ENDIF. ELSE. lv_pltyp = ps_knvv-pltyp. ENDIF. * Materialgruppentext ermitteln (T178T) ------------------------------ * SELECT SINGLE vtext INTO lv_vtext FROM T178T WHERE spras = ps_item_in-spras AND kondm = ls_maapv-kondm. * Kontrakte suchen (VAPMA) ------------------------------------------- * CLEAR lv_vbeln. SELECT SINGLE vbeln INTO lv_vbeln FROM VAPMA WHERE matnr = ps_item_in-matnr AND vkorg = ps_knvv-vkorg AND trvog = '4' AND vtweg = ps_knvv-vtweg AND spart = ps_knvv-spart AND auart = 'ZKM' AND kunnr = ps_kna1-kunnr AND datab <= sy-datum AND datbi >= sy-datum. IF lv_vbeln IS INITIAL. lv_kontr = ' '. ELSE. lv_kontr = 'X'. ENDIF. * KOMK füllen -------------------------------------------------------- * ls_komk_i-vkorg = ps_knvv-vkorg. ls_komk_i-vtweg = ps_knvv-vtweg. ls_komk_i-spart = ps_knvv-spart. ls_komk_i-vkbur = ps_knvv-vkbur. ls_komk_i-wkreg = ps_t001w-regio. ls_komk_i-zterm = ps_knvv-zterm. ls_komk_i-vsbed = ps_knvv-vsbed. ls_komk_i-pltyp = lv_pltyp. ls_komk_i-aland = ps_t001-land1. ls_komk_i-bukrs = ps_t001-bukrs. ls_komk_i-hwaer = ps_t001-waers. ls_komk_i-waerk = pv_waers. ls_komk_i-kunnr = ps_kna1-kunnr. ls_komk_i-knrze = ps_kna1-kunnr. ls_komk_i-kunre = ps_kna1-kunnr. ls_komk_i-kunwe = ps_kna1-kunnr. ls_komk_i-kappl = 'V'. ls_komk_i-kalsm = 'ZBAA02'. ls_komk_i-prsdt = sy-datum. ls_komk_i-fkdat = sy-datum. ls_komk_i-fbuda = sy-datum. ls_komk_i-audat = sy-datum. ls_komk_i-erdat = sy-datum. ls_komk_i-land1 = ps_kna1-land1. ls_komk_i-taxk1 = '1'. ls_komk_i-land1_we = ps_kna1-land1. ls_komk_i-aland_werk = ps_t001w-land1. ls_komk_i-auart = 'ZES'. ls_komk_i-vbtyp = 'C'. ls_komk_i-vkorgau = ps_knvv-vkorg. ls_komk_i-auart_sd = 'ZES'. ls_komk_i-trtyp = 'H'. ls_komk_i-werks = ps_t001w-werks. * ls_komk_i-vsbed = '01'. ls_komk_i-ix_komk = 1. LOOP AT lt_vbpa INTO ls_vbpa WHERE prfre = 'X' AND histunr GT 0 AND posnr = '000000'. CASE ls_vbpa-histunr. WHEN '01'. ls_komk_i-hienr01 = ls_vbpa-kunnr. WHEN '02'. ls_komk_i-hienr02 = ls_vbpa-kunnr. WHEN '03'. ls_komk_i-hienr03 = ls_vbpa-kunnr. WHEN '04'. ls_komk_i-hienr04 = ls_vbpa-kunnr. WHEN '05'. ls_komk_i-hienr05 = ls_vbpa-kunnr. WHEN '06'. ls_komk_i-hienr06 = ls_vbpa-kunnr. WHEN '07'. ls_komk_i-hienr07 = ls_vbpa-kunnr. WHEN '08'. ls_komk_i-hienr08 = ls_vbpa-kunnr. WHEN '09'. ls_komk_i-hienr09 = ls_vbpa-kunnr. WHEN '10'. ls_komk_i-hienr10 = ls_vbpa-kunnr. WHEN '11'. ls_komk_i-hienr11 = ls_vbpa-kunnr. WHEN '12'. ls_komk_i-hienr12 = ls_vbpa-kunnr. WHEN '13'. ls_komk_i-hienr13 = ls_vbpa-kunnr. WHEN '14'. ls_komk_i-hienr14 = ls_vbpa-kunnr. WHEN '15'. ls_komk_i-hienr15 = ls_vbpa-kunnr. ENDCASE. ENDLOOP. * KOMP füllen -------------------------------------------------------- * ls_komp_i-kposn = '000010'. ls_komp_i-matnr = ps_item_in-matnr. ls_komp_i-werks = ps_t001w-werks. ls_komp_i-bwkey = ps_t001w-werks. ls_komp_i-matkl = ls_maapv-matkl. ls_komp_i-taxps = '000010'. ls_komp_i-ean11 = ls_maapv-ean11. ls_komp_i-prodh = ls_maapv-prdha. ls_komp_i-pmatn = ps_item_in-matnr. ls_komp_i-meins = ls_maapv-meins. ls_komp_i-mglme = ps_item_in-menge. ls_komp_i-lagme = ps_item_in-vrkme. ls_komp_i-mgame = ps_item_in-menge. ls_komp_i-vrkme = ps_item_in-vrkme. ls_komp_i-brgew = ls_maapv-brgew. ls_komp_i-ntgew = ls_maapv-ntgew. ls_komp_i-gewei = ls_maapv-gewei. ls_komp_i-volum = ls_maapv-volum. ls_komp_i-voleh = ls_maapv-voleh. ls_komp_i-sktof = ls_maapv-sktof. ls_komp_i-kondm = ls_maapv-kondm. ls_komp_i-wkreg = ps_t001w-regio. ls_komp_i-prsfd = 'X'. ls_komp_i-evrwr = 'X'. ls_komp_i-kursk = 1. ls_komp_i-taxm1 = '1'. ls_komp_i-prsok = 'A'. ls_komp_i-aubel = '$TEMP'. ls_komp_i-aupos = '000010'. ls_komp_i-pstyv = 'ZTAN'. * ls_komp_i-zzroute = ls_kna1-route. ls_komp_i-ix_komk = 1. * Preisfindung simulieren -------------------------------------------- * CALL FUNCTION 'PRICING' EXPORTING calculation_type = 'C' comm_head_i = ls_komk_i comm_item_i = ls_komp_i IMPORTING comm_head_e = ls_komk_e comm_item_e = ls_komp_e TABLES tkomv = lt_komv. * Ausgabestruktur füllen --------------------------------------------- * MOVE-CORRESPONDING ps_item_in TO ps_item_out. * ps_item_out-kbetr = ls_komp_e-netpr. IF ps_item_in-menge NE 0 AND ps_item_in-menge NE 0. ps_item_out-kbetr = ls_komp_e-kzwi2 / ps_item_in-menge / ls_komp_e-kpein. ELSE. ps_item_out-kbetr = ls_komp_e-netpr. ENDIF. ps_item_out-kpein = ls_komp_e-kpein. ps_item_out-kmein = ls_komp_e-kmein. ps_item_out-kwert = ls_komp_e-kzwi2. ps_item_out-waers = pv_waers. ps_item_out-kondm = ls_maapv-kondm. ps_item_out-vtext = lv_vtext. ps_item_out-kontr = lv_kontr. ENDFORM. " CALCULATE_PRICE