健康一贴灵,专注医药行业管理信息化

SAP SD销售订单执行情况表

销售订单执行情况表

*&---------------------------------------------------------------------*
*& 包含               ZSDR0003_FUN
*&---------------------------------------------------------------------*
FORM init_method.
* 日期改为手动输入
*  CALL FUNCTION 'FIMA_DATE_CREATE'
*    EXPORTING
*      i_date                  = sy-datum "输入日期
*      i_flg_end_of_month      = ' '
*      i_years                 = 0       "两年后的日期. 即输入日期的年加2所得日期,可为负数,表示前两年
*      i_months                = -1       "一个月后的日期.即输入日期的月加1所得日期,可为负数,表示前一个月
*      i_days                  = 0      "23天后的日期。可为负数,表示23天前的日期
*      i_calendar_days         = 0      "10天后的日历。同I_DAYS参数。
*      i_set_last_day_of_month = ''     "返回的日期为当前月份的最后一天
*    IMPORTING
*      e_date                  = s_erdat-low.
*  s_erdat-high = sy-datum.
*  s_erdat-sign = 'I'.
*  s_erdat-option = 'BT'.
*  APPEND s_erdat.
ENDFORM.

FORM frm_check_vkorg.
  DATA: lv_message        TYPE string. " 在选择屏幕基础上有权限销售组织数量
  SELECT tvkos~vkorg,tvkos~spart,tvkov~vtweg
  INTO CORRESPONDING FIELDS OF TABLE @gt_tvko
  FROM tvkos
  INNER JOIN tvkov  ON tvkos~vkorg = tvkov~vkorg
  WHERE tvkov~vkorg IN @s_vkorg
  AND tvkov~vtweg IN @s_vtweg
  AND tvkos~spart IN @s_spart.
  LOOP AT gt_tvko INTO wa_tvko.
    AUTHORITY-CHECK OBJECT 'V_VBAK_VKO'
     ID 'VKORG' FIELD wa_tvko-vkorg
     ID 'VTWEG' FIELD wa_tvko-vtweg
     ID 'SPART' FIELD wa_tvko-spart
     ID 'ACTVT' FIELD '03'
     .
    IF sy-subrc <> 0.
      lv_message = '销售组织:' && wa_tvko-vkorg && '销售渠道:' && wa_tvko-vtweg && '产品组' && wa_tvko-spart.
      MESSAGE e053(zmm) WITH   lv_message.
    ENDIF.
    CLEAR: wa_tvko.
  ENDLOOP.
ENDFORM.
FORM fun_get_data.
  DATA lv_sl TYPE p.
  DATA sql_string TYPE string.
*  read text 参数
  DATA: l_id       TYPE thead-tdid,
        l_language TYPE thead-tdspras,
        l_name     TYPE thead-tdname,
        l_object   TYPE thead-tdobject.
  DATA:lt_line TYPE STANDARD TABLE OF tline WITH HEADER LINE.
  l_id = '0001'.
  l_language = '1'. " 'E' for English
  IF p_alv1 = 'X'.
    IF s_lfsta IS NOT INITIAL.
*    选择屏幕上无法输入空值,用X代替空值,有X的话 在这里修改为空
      READ TABLE s_lfsta WITH KEY low = 'X'.
      IF sy-subrc = 0.
        s_lfsta-low = ''.
        MODIFY s_lfsta TRANSPORTING low WHERE low = 'X'.
      ENDIF.
    ENDIF.
*  判断订单类型是否包含贷项订单,筛选条件为空 默认所有订单
*    IF s_auart IS INITIAL.
*      sql_string = 'vbak~auart IN @s_auart AND vbak~vkorg IN @s_vkorg AND vbak~vtweg IN @s_vtweg AND vbak~spart IN @s_spart AND  vbak~erdat IN @s_erdat AND vbak~vbeln IN @s_vbeln AND vbap~abgru = '''''.
*    ELSE.
*      LOOP AT s_auart WHERE low = 'ZDX1' OR low = 'ZDX2'.
*      ENDLOOP.
*      IF sy-subrc = 0.
*        sql_string = 'vbak~auart IN @s_auart AND vbak~vkorg IN @s_vkorg AND vbak~vtweg IN @s_vtweg AND vbak~spart IN @s_spart AND  vbak~erdat IN @s_erdat AND vbak~vbeln IN @s_vbeln AND vbap~abgru = '''''.
*      ELSE.
*        sql_string = 'vbak~auart IN @s_auart AND vbap~lfsta IN @s_lfsta AND  vbak~vkorg IN @s_vkorg AND vbak~vtweg IN @s_vtweg AND vbak~spart IN @s_spart AND  vbak~erdat IN @s_erdat AND vbak~vbeln IN @s_vbeln AND vbap~abgru = '''''.
*      ENDIF.
*    ENDIF.
*    s_lfsta-low =  ''.
*    s_lfsta-option = 'EQ'.
*    s_lfsta-sign = 'I'.
*    APPEND s_lfsta.
    SELECT
       vbak~vbeln,
       vbak~ernam,
       vbfa~vbelv,"系统合同号
       vbkd~bstkd,
     vbak~kunnr,
       vbak~vkorg, "组织
     vbak~vtweg,"渠道
     vbak~spart,"产品组
     vbak~auart,
     vbak~vkbur,"销售办公室
     vbak~vkgrp,"销售组
     vbkd~bzirk,"销售地区
     vbak~erdat,
     vbkd~kdgrp,"客户类型
     vbkd~zlsch,"付款类型
     vbkd~vsart,"运输方式
       vbkd~bstdk,
       vbap~posnr,
     vbap~matnr,
     vbap~arktx,
       ausp~atwrt AS ztype,
       vbap~kwmeng,
       vbap~kbmeng,
     vbap~vrkme , "销售单位
     vbap~umvkz,"销售单位和基本单位的换算因子
     vbap~meins,"基本单位
       vbap~kzwi1 AS zje,
       vpa1~kunnr AS ywy,
       vpa2~kunnr AS ck,
*       vbap~lfsta,
       vbak~knumv,
       prcd_elements~kbetr, "折扣单价
       prcd_elements~kwert"折扣金额
       INTO TABLE @DATA(table)
       FROM vbak
       INNER JOIN vbap ON vbap~vbeln = vbak~vbeln
       LEFT JOIN vbfa ON vbfa~vbeln = vbap~vbeln AND  vbfa~posnn = vbap~posnr AND vbtyp_v = 'G' "合同
       INNER JOIN vbkd ON vbak~vbeln = vbkd~vbeln AND vbkd~posnr = ''
       LEFT JOIN vbpa AS vpa1 ON vbak~vbeln = vpa1~vbeln AND vpa1~parvw = 'Z1'  "业务员
       INNER JOIN vbpa AS vpa2 ON vbak~vbeln = vpa2~vbeln AND vpa2~parvw = 'WE' AND vpa2~posnr = '000000' "送货仓库
       LEFT JOIN ausp ON ausp~objek = vbap~matnr AND atinn = '0000000811'
       LEFT JOIN prcd_elements ON prcd_elements~knumv = vbak~knumv AND prcd_elements~kschl = 'K005'  AND prcd_elements~kposn = vbap~posnr "折扣单价和折扣金额
     WHERE vbak~auart IN @s_auart
     AND vbap~lfsta IN @s_lfsta
     AND  vbak~vkorg IN @s_vkorg
     AND vbak~vtweg IN @s_vtweg
     AND vbak~spart IN @s_spart
     AND  vbak~erdat IN @s_erdat
     AND vbak~vbeln IN @s_vbeln
     AND vbak~kunnr IN @s_kunnr
     AND vpa1~kunnr IN @s_ywy
     AND vbap~abgru = ''
       .
    IF table IS  INITIAL.
      MESSAGE s078(zmm) DISPLAY LIKE 'E'.
      LEAVE LIST-PROCESSING.
    ENDIF.
*              查询订单类型
    SELECT auart,bezei AS ddlx INTO TABLE @DATA(ddlx) FROM tvakt FOR ALL ENTRIES IN @table WHERE auart = @table-auart AND spras = '1'.
*    查税额
    SELECT kbetr,knumv,kposn INTO TABLE @DATA(sl) FROM prcd_elements  FOR ALL ENTRIES IN @table WHERE kschl = 'MWSI' AND  knumv = @table-knumv AND kposn = @table-posnr.

*    查询运输方式
    SELECT bezei, vsart INTO TABLE @DATA(vsart) FROM t173t FOR ALL ENTRIES IN @table WHERE vsart = @table-vsart.
*      查询代理商和代理商名称
    SELECT vbpa~vbeln,
           vbpa~kunnr,
           kna1~name1
     INTO TABLE @DATA(dls)
     FROM vbpa INNER JOIN kna1 ON vbpa~kunnr = kna1~kunnr
     FOR ALL ENTRIES IN @table WHERE vbeln = @table-vbeln AND parvw = 'Z8'.
*        查询生产厂家 T024X
*    SELECT matnr,zzproduc INTO TABLE @DATA(sccj) FROM zmmt0036a FOR ALL ENTRIES IN @table WHERE matnr = @table-matnr.
    SELECT matnr, lbtxt
    INTO TABLE @DATA(sccj)
    FROM mara
    INNER JOIN t024x ON mara~labor = t024x~labor AND spras = '1'
    FOR ALL ENTRIES IN @table WHERE matnr = @table-matnr.
    SORT sccj BY matnr.
*  查询送货仓库信息
    SELECT ck~kunnr,ck~name1,adrc~city1,adrc~street,adrc~tel_number,adrc~name_co
     INTO TABLE @DATA(lt_ck) FROM kna1 AS ck
    INNER JOIN adrc ON ck~adrnr = adrc~addrnumber
  FOR ALL ENTRIES IN @table WHERE ck~kunnr = @table-ck.
*           查询返利和红票金额
    SELECT vbfa~vbelv,vbfa~posnv,vbap~kzwi1 INTO TABLE @DATA(lt_fl) FROM vbfa
    INNER JOIN vbap ON vbap~vbeln = vbfa~vbeln AND vbap~posnr = vbfa~posnn
     FOR ALL ENTRIES IN @table WHERE
       vbfa~vbelv = @table-vbeln
      AND vbfa~posnv = @table-posnr
      AND  vbtyp_n = 'K'.
*        查询出库数量时 for all entries in 要求数据长度一样才能匹配 所以这里要定义一个结构 不能按照@data方式定义
*    DATA:BEGIN OF lt_fhsl OCCURS 0,
*           vbelv TYPE vbfa-vbelv,
*           posnv TYPE  vbfa-posnv,
*           lfimg TYPE lips-lfimg,
*           vbeln TYPE  vbfa-vbeln,
*           posnn TYPE   mseg-zeile,
*         END OF lt_fhsl.
*   查询发货数量和出库数量,按照lips的库存地点判断是否发货
    SELECT v1~vbelv,v1~posnv,lips~lfimg,v1~vbeln,v1~posnn,lips~lgort
*      INTO TABLE @DATA(lt_fhsl)
     FROM vbfa AS v1
*     and  xauto = 'X'
   INNER JOIN lips ON lips~vbeln = v1~vbeln
*   and lips~lfimg <> 0
   AND lips~posnr = v1~posnn
    FOR ALL ENTRIES IN @table WHERE
      v1~vbelv = @table-vbeln
     AND v1~posnv = @table-posnr
     AND  vbtyp_n = 'J'
     AND lips~lfimg <> 0
           INTO  TABLE @DATA(lt_fhsl).
*       查询出库数量
*    SELECT
*              vbeln_im AS jhdh,
*              bwart,
*              erfmg, "数量
*              erfme"单位
*      FROM mseg
*    FOR ALL ENTRIES IN @lt_fhsl
*    WHERE vbeln_im = @lt_fhsl-vbeln
*    AND bwart IN ( 'Z01','Z02' )
*    INTO @DATA(lt_cksl).
*   查询开票数量和开票金额   vbrp~posnr 保证当其他数据完全相同时 for all entries 自动去重加上行号避免自动去重  贷项订单后续凭证类型为O
    SELECT vbfa~vbelv,vbfa~posnv,vbrp~fkimg,vbrp~posnr,vbrp~kzwi1,vbfa~vbtyp_n INTO TABLE @DATA(lt_kpsl) FROM vbfa
   INNER JOIN vbrp ON vbrp~vbeln = vbfa~vbeln AND vbrp~posnr = vbfa~posnn
    FOR ALL ENTRIES IN @table WHERE
      vbfa~vbelv = @table-vbeln
     AND vbfa~posnv = @table-posnr
     AND  vbtyp_n  IN ( 'M','N','O' ).
*查询回款金额 如果想匹配到具体的订单 目前的逻辑是按照发票从bsad表中取值 bsad中是已经清账的发票 意味着肯定回款了
*如果没有清账的发票 可能没有回款 这时只能按照DZ类型的财务凭证判断回款 但是DZ类型的财务凭证无法匹配到销售订单的发票,
*因此可能存在客户回款但是财务没有及时清账的情况 只能要求财务及时清账 程序上做不到百分百准确

*    关联bsad表 根据如果没有过账 basd财务凭证是空的 我们只取不为空的  数量求和以后就是已经过账的数量 数量乘单价就是回款  for FOR ALL ENTRIES IN不能使用聚合函数 无语
    SELECT vbfa~vbelv,vbfa~posnv, vbfa~rfmng  INTO TABLE @DATA(lt_hksl) FROM vbfa
    LEFT JOIN bsad ON bsad~zuonr = vbfa~vbeln
    FOR ALL ENTRIES IN @table WHERE
      vbfa~vbelv = @table-vbeln
     AND vbfa~posnv = @table-posnr
     AND bsad~belnr IS NOT NULL
     AND vbfa~rfmng <> 0
     AND  vbtyp_n  IN ( 'M','O' ).

    SORT lt_hksl BY vbelv posnv.

*  查询客户名称
    SELECT kunnr,name1 INTO TABLE @DATA(lt_khmc) FROM kna1 FOR ALL ENTRIES IN @table WHERE kunnr = @table-kunnr.
*  查询业务员
    SELECT ywy~kunnr,ywy~name1 INTO TABLE @DATA(lt_ywy) FROM kna1 AS ywy
    FOR ALL ENTRIES IN @table WHERE ywy~kunnr = @table-ywy.
*    进度条相关变量
    DATA: l_lin  TYPE p,
          l_txt  TYPE string,
          l_lin1 TYPE string,
          l_perc TYPE p DECIMALS 2.
*          处理数据
    LOOP AT table ASSIGNING FIELD-SYMBOL(<row>).

      DESCRIBE TABLE table LINES l_lin.

      l_perc = sy-tabix / l_lin * 100.

      l_lin1 =  l_perc.

      CONCATENATE l_lin1 '%' ',' '正在处理数据...' INTO l_txt.

      CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
        EXPORTING
          percentage = l_perc
          text       = l_txt.
      CLEAR gt_out.
*    客户名称
      READ TABLE lt_khmc ASSIGNING FIELD-SYMBOL(<khmc>) WITH KEY  kunnr = <row>-kunnr.
      IF sy-subrc = 0.
        gt_out-zkhmc = <khmc>-name1.
      ENDIF.
      READ TABLE sl ASSIGNING FIELD-SYMBOL(<sl>) WITH KEY  knumv = <row>-knumv kposn = <row>-posnr.
      IF sy-subrc = 0.
        lv_sl = <sl>-kbetr.
        gt_out-zsl = lv_sl.
        CONCATENATE gt_out-zsl '%' INTO gt_out-zsl.
      ENDIF.
*    业务员
      READ TABLE lt_ywy ASSIGNING FIELD-SYMBOL(<ywy>) WITH KEY kunnr = <row>-ywy.
      IF sy-subrc = 0.
        gt_out-ywyxm = <ywy>-name1.
      ENDIF.
*    仓库名称
      READ TABLE lt_ck ASSIGNING FIELD-SYMBOL(<ck>) WITH KEY kunnr = <row>-ck.
      IF sy-subrc = 0.
        gt_out-zshckmc = <ck>-name1.
        gt_out-city1 = <ck>-city1.
        gt_out-street = <ck>-street.
        gt_out-tel_number = <ck>-tel_number.
        gt_out-name_co = <ck>-name_co.
      ENDIF.
      IF <row>-bstdk IS INITIAL.
        gt_out-zzxzt = '现款'.
      ELSE.
        gt_out-zzxzt = '赊销'.
      ENDIF.
*        生产厂家
      READ TABLE sccj ASSIGNING FIELD-SYMBOL(<sccj>) WITH KEY matnr = <row>-matnr BINARY SEARCH.
      IF sy-subrc = 0.
        gt_out-sccj = <sccj>-lbtxt.
      ENDIF.

*        代理商
      READ TABLE dls ASSIGNING FIELD-SYMBOL(<dls1>) WITH KEY vbeln = <row>-vbeln.
      IF sy-subrc = 0.
        gt_out-zdls = <dls1>-kunnr.
        gt_out-zdlsmc = <dls1>-name1.
      ENDIF.
*    销售单位单价
      IF <row>-kwmeng <> 0.
        gt_out-zkpjg = <row>-zje / <row>-kwmeng.
      ELSE.
        gt_out-zkpjg = 0.
      ENDIF.
      READ TABLE lt_fl ASSIGNING FIELD-SYMBOL(<fl>) WITH KEY vbelv = <row>-vbeln  posnv = <row>-posnr.
      IF sy-subrc = 0.
        gt_out-zhpje = <fl>-kzwi1.
      ENDIF.
*    发货数量和出库数量
      LOOP AT  lt_fhsl ASSIGNING FIELD-SYMBOL(<fhsl>)  WHERE vbelv = <row>-vbeln  AND posnv = <row>-posnr.
        IF <fhsl>-lgort = '9999'.
          gt_out-cksl = <fhsl>-lfimg + gt_out-cksl.
        ENDIF.
        gt_out-lfimg = gt_out-lfimg + <fhsl>-lfimg.
*        gt_out-jhdh = <fhsl>-vbeln.
      ENDLOOP.
      LOOP AT  lt_kpsl ASSIGNING FIELD-SYMBOL(<lt_kpsl>)  WHERE vbelv = <row>-vbeln  AND posnv = <row>-posnr.
        IF <lt_kpsl>-vbtyp_n = 'N'.
          <lt_kpsl>-fkimg = 0 - <lt_kpsl>-fkimg.
          <lt_kpsl>-kzwi1 = 0 - <lt_kpsl>-kzwi1.
        ENDIF.
        gt_out-fkimg = gt_out-fkimg + <lt_kpsl>-fkimg.
        gt_out-zkpje = gt_out-zkpje + <lt_kpsl>-kzwi1.
      ENDLOOP.
      gt_out-vkorg = <row>-vkorg.
      gt_out-vtweg = <row>-vtweg.
      gt_out-ddlx = <row>-auart.
      gt_out-zjhhkrq = <row>-bstdk.
      gt_out-vkbur = <row>-vkbur.
      gt_out-spart = <row>-spart.
      gt_out-kdgrp = <row>-kdgrp.
      gt_out-zlsch = <row>-zlsch.
      gt_out-bzirk = <row>-bzirk.
      gt_out-vkgrp = <row>-vkgrp.
      gt_out-vbeln = <row>-vbeln.
      gt_out-ernam = <row>-ernam.
      gt_out-vbelv = <row>-vbelv.
      gt_out-bstkd = <row>-bstkd.
      gt_out-kunnr = <row>-kunnr.
      gt_out-erdat = <row>-erdat.
      gt_out-zywy = <row>-ywy.
      gt_out-zshck = <row>-ck.
      gt_out-posnr = <row>-posnr.
      gt_out-matnr = <row>-matnr.
      gt_out-arktx = <row>-arktx.
      gt_out-ztype = <row>-ztype.
      gt_out-matnr = <row>-matnr.
      gt_out-kwmeng = <row>-kwmeng.
      gt_out-vrkme = <row>-vrkme.
*      基本单位
      gt_out-jbdw = <row>-meins.
      IF <row>-auart = 'ZRE' OR  <row>-auart = 'ZDX1' OR  <row>-auart = 'ZDX2'.
        gt_out-zje = 0 - <row>-zje.
      ELSE.
        gt_out-zje = <row>-zje.
      ENDIF.
      gt_out-kbmeng = <row>-kbmeng.
      gt_out-zwjh = gt_out-kwmeng - gt_out-cksl .
      gt_out-zwkp = gt_out-kwmeng - gt_out-fkimg.
      gt_out-kbetr = <row>-kbetr.
      gt_out-kwert = <row>-kwert.
*      基本单位数量
      gt_out-jbdwsl = <row>-umvkz * <row>-kwmeng.
      READ TABLE vsart ASSIGNING FIELD-SYMBOL(<vsart>) WITH KEY vsart = <row>-vsart .
      IF sy-subrc = 0.
        gt_out-vsart = <vsart>-bezei.
      ENDIF.
*      基本单位单价
      IF gt_out-jbdw = gt_out-vrkme.
        gt_out-jbdwdj = gt_out-zkpjg.
      ELSE.
        TRY.
            gt_out-jbdwdj = gt_out-zje / gt_out-jbdwsl.
          CATCH cx_sy_zerodivide.
            gt_out-jbdwdj = 0.
        ENDTRY.
      ENDIF.
*      回款金额是按照已经清账的数量乘基本单位的单价
      LOOP AT lt_hksl INTO DATA(wa_hksl) WHERE posnv = <row>-posnr AND vbelv = <row>-vbeln .
        gt_out-qzsl = gt_out-qzsl + wa_hksl-rfmng.
      ENDLOOP."jbdwsl
      gt_out-hkje = gt_out-qzsl * gt_out-jbdwdj.
      gt_out-whkje = gt_out-zje - gt_out-hkje.
      gt_out-wqzsl = gt_out-jbdwsl - gt_out-qzsl.
      gt_out-ckje = gt_out-cksl * gt_out-zkpjg. "出库金额
      gt_out-jhje = gt_out-lfimg * gt_out-zkpjg. "交货金额
      IF gt_out-cksl  = 0.
        gt_out-lfsta = '未交货'.
      ELSEIF gt_out-cksl > 0 AND gt_out-cksl < gt_out-kwmeng.
        gt_out-lfsta = '部分交货'.
      ELSE.
        gt_out-lfsta = '已交货'.
      ENDIF.
      l_name = ''.
      CONCATENATE  <row>-vbeln   "SO
             <row>-posnr       "Item
        INTO l_name.
      l_object = 'VBBP'.
*      读取订单文本
      CALL FUNCTION 'READ_TEXT'
        EXPORTING
          client                  = sy-mandt
          id                      = l_id
          language                = l_language
          name                    = l_name
          object                  = l_object
        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 = 0.
        LOOP AT lt_line.
          CONCATENATE gt_out-ztext lt_line-tdline INTO gt_out-ztext.
        ENDLOOP.
      ENDIF.
      APPEND gt_out.
    ENDLOOP.
*  查询组织各结构
*  查询组织
    SELECT
    vkorg,
    vtext
    INTO TABLE @DATA(vkorg)
    FROM tvkot FOR ALL ENTRIES IN @gt_out WHERE vkorg = @gt_out-vkorg AND spras = '1'
    .
    LOOP AT vkorg  ASSIGNING FIELD-SYMBOL(<vkorg>).
      CLEAR gt_out.
      gt_out-zzz = <vkorg>-vtext.
      MODIFY gt_out FROM gt_out  TRANSPORTING zzz WHERE vkorg = <vkorg>-vkorg.
    ENDLOOP.
*  查询渠道
    SELECT
  vtweg,
  vtext
  INTO TABLE @DATA(vtweg)
  FROM tvtwt  FOR ALL ENTRIES IN @gt_out WHERE vtweg = @gt_out-vtweg AND spras = '1'
  .
    LOOP AT vtweg  ASSIGNING FIELD-SYMBOL(<vtweg>).
      CLEAR gt_out.
      gt_out-zqd = <vtweg>-vtext.
      MODIFY gt_out FROM gt_out  TRANSPORTING zqd WHERE vtweg = <vtweg>-vtweg.
    ENDLOOP.
*  查询办公室
    SELECT
  vkbur,
  bezei
  INTO TABLE @DATA(vkbur)
  FROM tvkbt FOR ALL ENTRIES IN @gt_out WHERE vkbur = @gt_out-vkbur AND spras = '1'
  .
    LOOP AT vkbur  ASSIGNING FIELD-SYMBOL(<vkbur>).
      CLEAR gt_out.
      gt_out-zbgs = <vkbur>-bezei.
      MODIFY gt_out FROM gt_out  TRANSPORTING zbgs WHERE vkbur = <vkbur>-vkbur.
    ENDLOOP.
*  查询销售组
    SELECT
  vkgrp,
  bezei
  INTO TABLE @DATA(vkgrp)
  FROM tvgrt   FOR ALL ENTRIES IN @gt_out WHERE vkgrp = @gt_out-vkgrp AND spras = '1'
  .
    LOOP AT vkgrp  ASSIGNING FIELD-SYMBOL(<vkgrp>).
      CLEAR gt_out.
      gt_out-zxsz = <vkgrp>-bezei.
      MODIFY gt_out FROM gt_out  TRANSPORTING zxsz WHERE vkgrp = <vkgrp>-vkgrp.
    ENDLOOP.
*  查询产品组
    SELECT
  spart,
  vtext
  INTO TABLE @DATA(spart)
  FROM tspat   FOR ALL ENTRIES IN @gt_out WHERE spart = @gt_out-spart AND spras = '1'
  .
    LOOP AT spart  ASSIGNING FIELD-SYMBOL(<spart>).
      CLEAR gt_out.
      gt_out-zcpz = <spart>-vtext.
      MODIFY gt_out FROM gt_out  TRANSPORTING zcpz WHERE spart = <spart>-spart.
    ENDLOOP.
*  查询销售地区
    SELECT
  bzirk,
  bztxt
  INTO TABLE @DATA(bzirk)
  FROM t171t      FOR ALL ENTRIES IN @gt_out WHERE bzirk = @gt_out-bzirk AND spras = '1'
  .
    LOOP AT bzirk  ASSIGNING FIELD-SYMBOL(<bzirk>).
      CLEAR gt_out.
      gt_out-zxsdq = <bzirk>-bztxt.
      MODIFY gt_out FROM gt_out  TRANSPORTING zxsdq WHERE bzirk = <bzirk>-bzirk.
    ENDLOOP.
*        查询客户类型描述
    SELECT kdgrp,ktext INTO TABLE @DATA(khlx) FROM t151t FOR ALL ENTRIES IN @table WHERE kdgrp = @table-kdgrp AND spras = '1'.
    LOOP AT khlx ASSIGNING FIELD-SYMBOL(<khlx>).
      CLEAR gt_out.
      gt_out-khlxms = <khlx>-ktext.
      MODIFY gt_out FROM gt_out  TRANSPORTING khlxms WHERE kdgrp = <khlx>-kdgrp.
    ENDLOOP.
*   更改订单类型
    LOOP AT ddlx ASSIGNING FIELD-SYMBOL(<ddlx>).
      CLEAR gt_out.
      gt_out-ddlx = <ddlx>-ddlx.
      MODIFY gt_out FROM gt_out  TRANSPORTING ddlx WHERE ddlx = <ddlx>-auart.
    ENDLOOP.
*         查询单位描述
    SELECT DISTINCT msehi,mseh6
    INTO TABLE @DATA(dw) FROM t006a
    FOR ALL ENTRIES IN @table WHERE  ( msehi = @table-meins OR msehi = @table-vrkme ) AND spras = '1'.
    SORT dw BY msehi.
*    DATA length TYPE int2.
*    length = lines( dw ) * 2.
*    DO length TIMES.
*      READ TABLE dw ASSIGNING FIELD-SYMBOL(<dw>)   INDEX sy-index.
*    ENDDO.
*  更改销售单位描述
    LOOP AT dw INTO DATA(dw1).
      CLEAR gt_out.
      gt_out-vrkme = dw1-mseh6.
      MODIFY gt_out   TRANSPORTING vrkme WHERE vrkme = dw1-msehi.
    ENDLOOP.
*      更改销售单位描述
    LOOP AT dw INTO dw1.
      CLEAR gt_out.
      gt_out-jbdw = dw1-mseh6.
      MODIFY gt_out   TRANSPORTING jbdw WHERE jbdw = dw1-msehi.
    ENDLOOP.
  ELSE.
    SELECT
 vbak~vbeln,
 vbak~ernam,
 vbfa~vbelv,"系统合同号
 vbkd~bstkd,
 vbkd~kdgrp,
 vbak~bstdk,
vbak~kunnr,
 vbak~vkorg, "组织
vbak~vtweg,"渠道
vbak~spart,"产品组
vbak~vkbur,"销售办公室
vbak~vkgrp,"销售组
vbkd~bzirk,"销售地区
vbak~erdat,
 SUM( vbap~kzwi1 ) AS zje,
 vpa1~kunnr AS ywy,
 vpa2~kunnr AS dls
 INTO TABLE @DATA(table1)
 FROM vbak
 INNER JOIN vbap ON vbap~vbeln = vbak~vbeln
 LEFT JOIN vbfa ON vbfa~vbeln = vbap~vbeln  AND vbtyp_v = 'G' "系统合同号
 INNER JOIN vbkd ON vbak~vbeln = vbkd~vbeln AND vbkd~posnr = ''
 LEFT JOIN vbpa AS vpa1 ON vbak~vbeln = vpa1~vbeln AND vpa1~parvw = 'Z1'  "业务员
 LEFT JOIN vbpa AS vpa2 ON vbak~vbeln = vpa2~vbeln AND vpa2~parvw = 'Z8' "代理商
  WHERE vbak~auart IN ( 'ZOR','ZOR1' )
  AND   vbak~vkorg IN @s_vkorg
  AND vbak~vtweg IN @s_vtweg
   AND vbak~spart IN @s_spart
  AND  vbak~erdat IN @s_erdat
 GROUP BY  vbak~vbeln,
 vbak~ernam,
 vbfa~vbelv,"系统合同号
 vbkd~bstkd,
 vbak~bstdk,
vbak~kunnr,
 vbkd~kdgrp,
 vbak~vkorg, "组织
vbak~vtweg,"渠道
vbak~spart,"产品组
vbak~vkbur,"销售办公室
vbak~vkgrp,"销售组
vbkd~bzirk,"销售地区
vbak~erdat,
 vpa1~kunnr ,
 vpa2~kunnr.
    IF table1 IS  INITIAL.
      MESSAGE s078(zmm) DISPLAY LIKE 'E'.
      LEAVE LIST-PROCESSING.
    ENDIF.
* 查询结果集中的订单是否有退货订单
    SELECT vbap~vgbel, kzwi1 INTO TABLE @DATA(lt_zre) FROM  vbap INNER JOIN vbak ON vbak~vbeln = vbap~vbeln AND vbak~auart = 'ZRE'
    FOR ALL ENTRIES IN @table1 WHERE vbak~vgbel = @table1-vbeln .
*    查询客户名称
    SELECT kunnr,name1 INTO TABLE @DATA(lt_khmc1) FROM kna1 FOR ALL ENTRIES IN @table1 WHERE kunnr = @table1-kunnr.
*  查询业务员
    SELECT ywy~kunnr,ywy~name1 INTO TABLE @DATA(lt_ywy1) FROM kna1 AS ywy
    FOR ALL ENTRIES IN @table1 WHERE ywy~kunnr = @table1-ywy.
*    查询代理商
    SELECT dls~kunnr,dls~name1 INTO TABLE @DATA(lt_dls) FROM kna1 AS dls
    FOR ALL ENTRIES IN @table1 WHERE dls~kunnr = @table1-dls.
*     查询实际回款日期
    SELECT DISTINCT vbfa~vbelv,bsad~augdt INTO TABLE @DATA(lt_sjhkrq) FROM vbfa
   INNER JOIN bsad ON bsad~zuonr = vbfa~vbeln
    FOR ALL ENTRIES IN @table1 WHERE
      vbfa~vbelv = @table1-vbeln
     AND  vbtyp_n = 'M'.


*    处理数据
    LOOP AT table1 ASSIGNING FIELD-SYMBOL(<row1>).
      CLEAR gt_out.
*    客户名称
      READ TABLE lt_khmc1 ASSIGNING FIELD-SYMBOL(<khmc1>) WITH KEY  kunnr = <row1>-kunnr.
      IF sy-subrc = 0.
        gt_out-zkhmc = <khmc1>-name1.
      ENDIF.
*    业务员
      READ TABLE lt_ywy1 ASSIGNING FIELD-SYMBOL(<ywy1>) WITH KEY kunnr = <row1>-ywy.
      IF sy-subrc = 0.
        gt_out-ywyxm = <ywy1>-name1.
      ENDIF.
*          代理商
      READ TABLE lt_dls ASSIGNING FIELD-SYMBOL(<dls>) WITH KEY kunnr = <row1>-ywy.
      IF sy-subrc = 0.
        gt_out-zdlsmc = <dls>-name1.
      ENDIF.
*          实际回款日期
      READ TABLE lt_sjhkrq ASSIGNING FIELD-SYMBOL(<sjhkrq>) WITH KEY vbelv = <row1>-vbeln.
      IF sy-subrc = 0.
        gt_out-zsjhkrq = <sjhkrq>-augdt.
      ENDIF.
      gt_out-zzl = sy-datum - <row1>-bstdk.
      IF gt_out-zsjhkrq  IS INITIAL.
        gt_out-zsjzl = gt_out-zsjhkrq - <row1>-bstdk.
      ELSE.
        gt_out-zsjzl = gt_out-zsjhkrq.
      ENDIF.
      gt_out-vkorg = <row1>-vkorg.
      gt_out-vtweg = <row1>-vtweg.
      gt_out-vkbur = <row1>-vkbur.
      gt_out-spart = <row1>-spart.
      gt_out-bzirk = <row1>-bzirk.
      gt_out-kdgrp = <row1>-kdgrp.
      gt_out-vkgrp = <row1>-vkgrp.
      gt_out-vbeln = <row1>-vbeln.
      gt_out-ernam = <row1>-ernam.
      gt_out-vbelv = <row1>-vbelv.
      gt_out-bstkd = <row1>-bstkd.
      gt_out-kunnr = <row1>-kunnr.
      gt_out-erdat = <row1>-erdat.
      gt_out-zywy = <row1>-ywy.
      gt_out-zdls = <row1>-dls.
      gt_out-zje = <row1>-zje.
*      是否有该条订单的退货订单 如果有减去退货金额
      LOOP AT  lt_zre ASSIGNING FIELD-SYMBOL(<zre>) WHERE vgbel = gt_out-vbeln.
        gt_out-zje = gt_out-zje - <zre>-kzwi1.
      ENDLOOP.
      gt_out-zjhhkrq = <row1>-bstdk.
      APPEND gt_out.
    ENDLOOP.
*    查询组织各结构
*  查询组织
    SELECT
    vkorg,
    vtext
    INTO TABLE @DATA(vkorg1)
    FROM tvkot FOR ALL ENTRIES IN @gt_out WHERE vkorg = @gt_out-vkorg AND spras = '1'
    .
    LOOP AT vkorg1  ASSIGNING FIELD-SYMBOL(<vkorg1>).
      CLEAR gt_out.
      gt_out-zzz = <vkorg1>-vtext.
      MODIFY gt_out FROM gt_out  TRANSPORTING zzz WHERE vkorg = <vkorg1>-vkorg.
    ENDLOOP.
*  查询渠道
    SELECT
  vtweg,
  vtext
  INTO TABLE @DATA(vtweg1)
  FROM tvtwt  FOR ALL ENTRIES IN @gt_out WHERE vtweg = @gt_out-vtweg AND spras = '1'
  .
    LOOP AT vtweg1  ASSIGNING FIELD-SYMBOL(<vtweg1>).
      CLEAR gt_out.
      gt_out-zqd = <vtweg1>-vtext.
      MODIFY gt_out FROM gt_out  TRANSPORTING zqd WHERE vtweg = <vtweg1>-vtweg.
    ENDLOOP.
*  查询办公室
    SELECT
  vkbur,
  bezei
  INTO TABLE @DATA(vkbur1)
  FROM tvkbt FOR ALL ENTRIES IN @gt_out WHERE vkbur = @gt_out-vkbur AND spras = '1'
  .
    LOOP AT vkbur1  ASSIGNING FIELD-SYMBOL(<vkbur1>).
      CLEAR gt_out.
      gt_out-zbgs = <vkbur1>-bezei.
      MODIFY gt_out FROM gt_out  TRANSPORTING zbgs WHERE vkbur = <vkbur1>-vkbur.
    ENDLOOP.
*  查询销售组
    SELECT
  vkgrp,
  bezei
  INTO TABLE @DATA(vkgrp1)
  FROM tvgrt   FOR ALL ENTRIES IN @gt_out WHERE vkgrp = @gt_out-vkgrp AND spras = '1'
  .
    LOOP AT vkgrp1  ASSIGNING FIELD-SYMBOL(<vkgrp1>).
      CLEAR gt_out.
      gt_out-zxsz = <vkgrp1>-bezei.
      MODIFY gt_out FROM gt_out  TRANSPORTING zxsz WHERE vkgrp = <vkgrp1>-vkgrp.
    ENDLOOP.
*  查询产品组
    SELECT
  spart,
  vtext
  INTO TABLE @DATA(spart1)
  FROM tspat   FOR ALL ENTRIES IN @gt_out WHERE spart = @gt_out-spart AND spras = '1'
  .
    LOOP AT spart1  ASSIGNING FIELD-SYMBOL(<spart1>).
      CLEAR gt_out.
      gt_out-zcpz = <spart1>-vtext.
      MODIFY gt_out FROM gt_out  TRANSPORTING zcpz WHERE spart = <spart1>-spart.
    ENDLOOP.
*  查询销售地区
    SELECT
  bzirk,
  bztxt
  INTO TABLE @DATA(bzirk1)
  FROM t171t  FOR ALL ENTRIES IN @gt_out WHERE bzirk = @gt_out-bzirk AND spras = '1'.
    LOOP AT bzirk1  ASSIGNING FIELD-SYMBOL(<bzirk1>).
      CLEAR gt_out.
      gt_out-zxsdq = <bzirk1>-bztxt.
      MODIFY gt_out FROM gt_out  TRANSPORTING zxsdq WHERE bzirk = <bzirk1>-bzirk.
    ENDLOOP.

*查询客户类型描述
    SELECT kdgrp,ktext INTO TABLE @DATA(khlx1) FROM t151t FOR ALL ENTRIES IN @gt_out WHERE kdgrp = @gt_out-kdgrp AND spras = '1'.
    LOOP AT khlx1 ASSIGNING FIELD-SYMBOL(<khlx1>).
      CLEAR gt_out.
      gt_out-khlxms = <khlx1>-ktext.
      MODIFY gt_out FROM gt_out  TRANSPORTING khlxms WHERE kdgrp = <khlx1>-kdgrp.
    ENDLOOP.
  ENDIF.
ENDFORM.
FORM fun_show_data.
  DEFINE append_alv_field.
    CLEAR g_line.
    g_line-fieldname    = &1.
    g_line-tabname      = 'GT_ALV'.
    g_line-reptext_ddic = &2.
    g_line-seltext_l    = &2.
    g_line-seltext_m    = &2.
    g_line-seltext_s    = &2.
    g_line-just         = 'C'.
    g_line-ddictxt      = 'L'.
    IF zero = ''.
        g_line-no_zero = &3.
    ENDIF.
    APPEND g_line TO gt_fieldcat.
  END-OF-DEFINITION.
  gt_layout-colwidth_optimize = 'X'. " 自适应宽度
  gt_layout-zebra = 'X'.

  REFRESH:gt_fieldcat.
  IF p_alv1 = 'X'.
    append_alv_field 'vbeln' '销售订单号' '' .
    append_alv_field 'ddlx' '订单类型' '' .
    append_alv_field 'ernam' '创建者' '' .
    append_alv_field 'vbelv' '系统合同号' '' .
    append_alv_field 'bstkd' '纸质合同号' '' .
    append_alv_field 'kunnr' '客户编码' ''  .
    append_alv_field 'zkhmc' '客户名称' '' .
    append_alv_field 'kdgrp' '客户类型' '' .
    append_alv_field 'khlxms' '客户类型描述' '' .
    append_alv_field 'ZLSCH' '付款方式' '' .
    append_alv_field 'vkorg' '销售组织' '' .
    append_alv_field 'zzz' '销售组织描述' '' .
    append_alv_field 'vtweg' '渠道' '' .
    append_alv_field 'zqd' '渠道描述' '' .
    append_alv_field 'spart' '产品组' '' .
    append_alv_field 'zcpz' '产品组描述' '' .
    append_alv_field 'vkbur' '销售办公室' '' .
    append_alv_field 'zbgs' '销售办公室描述' '' .
    append_alv_field 'vkgrp' '销售组' '' .
    append_alv_field 'zxsz' '销售组描述' '' .
    append_alv_field 'bzirk' '销售地区' '' .
    append_alv_field 'zxsdq' '销售地区描述' '' .
    append_alv_field 'erdat' '订单日期' '' .
    append_alv_field 'zywy' '业务员' '' .
    append_alv_field 'ywyxm' '业务员姓名' '' .
    append_alv_field 'zdls' '代理商' '' .
    append_alv_field 'zdlsmc' '代理商描述' '' .
    append_alv_field 'sccj' '实验室' '' .
    append_alv_field 'zshck' '送货仓库' '' .
    append_alv_field 'zshckmc' '送货仓库名称' '' .
    append_alv_field 'city1' '送货仓库城市' '' .
    append_alv_field 'street' '送货仓库地址' '' .
    append_alv_field 'name_co' '收货人' '' .
    append_alv_field 'tel_number' '电话' '' .
    append_alv_field 'zzxzt' '资信状态' '' .
    append_alv_field 'zjhhkrq' '计划回款日期' '' .
    append_alv_field 'posnr' '行号' ''.
    append_alv_field 'matnr' '物料编码' '' .
    append_alv_field 'arktx' '物料描述' '' .
    append_alv_field 'ztype' 'A/B/C分类' '' .
    append_alv_field 'kwmeng' '销售单位数量' 'X' .
    append_alv_field 'vrkme' '销售单位' '' .
    append_alv_field 'jbdwsl' '基本单位数量' 'X' .
    append_alv_field 'jbdw' '基本单位' '' .
    append_alv_field 'zkpjg' '销售单位含税单价' 'X'.
    append_alv_field 'jbdwdj' '基本单位含税单价' 'X'.
    append_alv_field 'zje' '含税金额' 'X' .
    append_alv_field 'kbmeng' '可用库存' 'X' .
*    append_alv_field 'vrkme' '可用库存单位' '' '' '' '' '' '' ''.
    append_alv_field 'lfimg' '发货数量' 'X' .
*        append_alv_field 'vrkme' '发货数量单位' '' '' '' '' '' '' ''.
    append_alv_field 'LFSTA' '交货描述' '' .
    append_alv_field 'cksl' '出库数量' 'X' .
*            append_alv_field 'vrkme' '出库数量单位' '' '' '' '' '' '' ''.
    append_alv_field 'zwjh' '未交货数量' 'X' .
    append_alv_field 'fkimg' '开票数量' 'X' .
    append_alv_field 'zwkp' '未开票数量' 'X' .
    append_alv_field 'zkpje' '开票金额' 'X' .
    append_alv_field 'zhpje' '返利金额' 'X' .
    append_alv_field 'ztext' '行项目文本' '' .
    append_alv_field 'VSART' '运输方式' '' .
    append_alv_field 'kbetr' '折扣单价' 'X' .
    append_alv_field 'kwert' '折扣金额' 'X' .
    append_alv_field 'zsl' '税率' '' .
    append_alv_field 'hkje' '回款金额' 'X' .
    append_alv_field 'whkje' '未回款金额' 'X' .
    append_alv_field 'qzsl' '清账数量' 'X' .
    append_alv_field 'jbdw' '清账数量单位' 'X' .
    append_alv_field 'wqzsl' '未清账数量' 'X' .
    append_alv_field 'jhje' '交货金额' 'X' .
    append_alv_field 'ckje' '出库金额' 'X' .
  ELSE.
    append_alv_field 'vbeln' '销售订单号' '' .
    append_alv_field 'ernam' '创建者' '' .
    append_alv_field 'vbelv' '系统合同号' '' .
    append_alv_field 'bstkd' '纸质合同号' '' .
    append_alv_field 'kunnr' '客户编码' '' .
    append_alv_field 'zkhmc' '客户名称' '' .
    append_alv_field 'khlxms' '客户类型描述' '' .
    append_alv_field 'vkorg' '销售组织' '' .
    append_alv_field 'zzz' '销售组织描述' '' .
    append_alv_field 'vtweg' '渠道' '' .
    append_alv_field 'zqd' '渠道描述' '' .
    append_alv_field 'spart' '产品组' '' .
    append_alv_field 'zcpz' '产品组描述' '' .
    append_alv_field 'vkbur' '销售办公室' '' .
    append_alv_field 'zbgs' '销售办公室描述' '' .
    append_alv_field 'vkgrp' '销售组' '' .
    append_alv_field 'zxsz' '销售组描述' '' .
    append_alv_field 'bzirk' '销售地区' '' .
    append_alv_field 'zxsdq' '销售地区描述' '' .
    append_alv_field 'erdat' '订单日期' '' .
    append_alv_field 'zjhhkrq' '计划回款日期' '' .
    append_alv_field 'zsjhkrq' '实际回款日期' '' .
    append_alv_field 'zywy' '业务员' '' .
    append_alv_field 'ywyxm' '业务员姓名' '' .
    append_alv_field 'zdls' '代理商' '' .
    append_alv_field 'zshckmc' '代理商名称' '' .
    append_alv_field 'zje' '订单金额' '' .
    append_alv_field 'zzl' '账龄' '' .
    append_alv_field 'zsjzl' '实际账龄' '' .
  ENDIF.
*  定义事件
  wa_events-name = 'USER_COMMAND'."用户相应事件
  wa_events-form = 'FORM_USER_COMMAND'.
  APPEND wa_events TO gt_events.
  CLEAR wa_events.
  wa_events-name = 'PF_STATUS_SET'.
  wa_events-form = 'FORM_PF_STATUS_SET'."设置gui_status
  APPEND wa_events TO gt_events.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      is_layout                = gt_layout
      it_fieldcat              = gt_fieldcat[]
      i_save                   = 'U'
      i_callback_user_command  = 'FORM_USER_COMMAND' "客户命令指定子程序
      i_callback_pf_status_set = 'FORM_PF_STATUS_SET'   "GUI指定子程序
      it_events                = gt_events
    TABLES
      t_outtab                 = gt_out[]
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
ENDFORM.
FORM form_user_command USING r_ucomm   LIKE sy-ucomm  rs_selfield TYPE slis_selfield.
  CASE:r_ucomm.
    WHEN '&IC1'.
      READ TABLE gt_out ASSIGNING <fs> INDEX rs_selfield-tabindex.
      PERFORM command_action USING rs_selfield-fieldname.

  ENDCASE.
ENDFORM.
FORM command_action  USING fieldname.
*  TRANSLATE fieldname TO UPPER CASE.       "将栏位名称转换为大写
  SET PARAMETER ID: 'AUN' FIELD <fs>-vbeln.
  IF sy-subrc = 0.
    CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. "跳过第一个屏幕到VA02屏幕
  ENDIF.
ENDFORM.
FORM form_pf_status_set  USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD'.
ENDFORM.

 

posted @ 2021-11-10 13:58  一贴灵  阅读(674)  评论(0编辑  收藏  举报
学以致用,效率第一