<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "物料收发存">
<query id = "FrmStuffProductStore.queryData" desc= "查询">
<![CDATA[
select PRODUCNAME,
       JUDGE_GRADENAME,
       ACT_DIMATER,
       JUDGE_STD_NAME,
       sum(ORIGINAL_COUNT) ORIGINAL_COUNT,
       sum(ORIGINAL_AMOUNT) ORIGINAL_AMOUNT,
       sum(DAY_1COUNT) DAY_1COUNT,
       sum(DAY_1) DAY_1,
       sum(MONTH_1COUNT) MONTH_1COUNT,
       sum(MONTH_1) MONTH_1,
       sum(DAY_2COUNT) DAY_2COUNT,
       sum(DAY_2) DAY_2,
       sum(MONTH_2COUNT) MONTH_2COUNT,
       sum(MONTH_2) MONTH_2,
       sum(AWEIGHT) AWEIGHT,
       sum((ORIGINAL_COUNT + MONTH_1COUNT - MONTH_2COUNT)) as END_COUNT,
       sum((ORIGINAL_AMOUNT + MONTH_1 - MONTH_2 + AWEIGHT)) as END_AMOUNT,
       sum(MATCOUNT) MATCOUNT,
       sum(MATWGT) MATWGT,
       sum((ORIGINAL_COUNT + MONTH_1COUNT - MONTH_2COUNT - MATCOUNT)) as QUANTITYCOUNT,
       sum((ORIGINAL_AMOUNT + MONTH_1 - MONTH_2 + AWEIGHT - MATWGT)) as QUANTITYWGT
  from (select PRODUCNAME,
               JUDGE_GRADENAME,
               ACT_DIMATER,
               JUDGE_STD_NAME,
               STORAGE_NO,
               sum(k.ORIGINAL_COUNT) as ORIGINAL_COUNT,
               sum(k.ORIGINAL_AMOUNT) as ORIGINAL_AMOUNT,
               nvl(sum(case
                         when trunc(k.INSTOCK_TIME) = to_date(?, 'yyyymmdd') then
                          nvl(k.inListCount, 0)
                       end),
                   0) DAY_1COUNT,
               nvl(sum(case
                         when trunc(k.INSTOCK_TIME) = to_date(?, 'yyyymmdd') then
                          nvl(k.inListWgt, 0)
                       end),
                   0) DAY_1,
               nvl(sum(case
                         when trunc(k.INSTOCK_TIME) >= to_date(?, 'yyyymmdd') and
                              trunc(k.INSTOCK_TIME) <= to_date(?, 'yyyymmdd') then
                          nvl(k.inListCount, 0)
                       end),
                   0) MONTH_1COUNT,
               nvl(sum(case
                         when trunc(k.INSTOCK_TIME) >= to_date(?, 'yyyymmdd') and
                              trunc(k.INSTOCK_TIME) <= to_date(?, 'yyyymmdd') then
                          nvl(k.inListWgt, 0)
                       end),
                   0) MONTH_1,
               nvl(sum(case
                         when trunc(k.OUTSTOCK_TIME) = to_date(?, 'yyyymmdd') then
                          nvl(k.outListCount, 0)
                       end),
                   0) DAY_2COUNT,
               nvl(sum(case
                         when trunc(k.OUTSTOCK_TIME) = to_date(?, 'yyyymmdd') then
                          nvl(k.outListWgt, 0)
                       end),
                   0) DAY_2,
               nvl(sum(case
                         when trunc(k.OUTSTOCK_TIME) >= to_date(?, 'yyyymmdd') and
                              trunc(k.OUTSTOCK_TIME) <= to_date(?, 'yyyymmdd') then
                          nvl(k.outListCount, 0)
                       end),
                   0) MONTH_2COUNT,
               nvl(sum(case
                         when trunc(k.OUTSTOCK_TIME) >= to_date(?, 'yyyymmdd') and
                              trunc(k.OUTSTOCK_TIME) <= to_date(?, 'yyyymmdd') then
                          nvl(k.outListWgt, 0)
                       end),
                   0) MONTH_2,
               sum(k.AWEIGHT) as AWEIGHT,
               sum(k.MATCOUNT) as MATCOUNT,
               sum(k.MATWGT) as MATWGT
          from (select PRODUCNAME,
                       JUDGE_GRADENAME,
                       ACT_DIMATER,
                       JUDGE_STD_NAME,
                       STORAGE_NO,
                       sum(ACT_COUNT) ORIGINAL_COUNT,
                       sum(ACT_WEIGHT) ORIGINAL_AMOUNT,
                       0 inListCount,
                       0 inListWgt,
                       0 outListCount,
                       0 outListWgt,
                       0 AWEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       null OUTSTOCK_TIME,
                       null INSTOCK_TIME
                  from ydm_gp_initial t
                 where t.bal_year_month = ?
                   and t.storage_no || '&' like ('%'||?||'%')
                   and t.handle_log = '1' #condtions#
                 group by PRODUCNAME,
                       JUDGE_GRADENAME,
                       ACT_DIMATER,
                       JUDGE_STD_NAME,
                       STORAGE_NO
                union all
                SELECT PRODUCNAME,
                       JUDGE_GRADENAME,
                       ACT_DIMATER,
                       JUDGE_STD_NAME,
                       STORAGE_NO,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       ACT_COUNT inListCount,
                       ACT_WEIGHT inListWgt,
                       0 outListCount,
                       0 outListWgt,
                       0 AWEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       null OUTSTOCK_TIME,
                       INSTOCK_TIME INSTOCK_TIME
                  FROM ymd_gp_inlist
                 WHERE INSTOCK_TYPE_CODE IN
                       ('800701', '800702', '800704', '800706', '800707')
                   AND TRUNC(INSTOCK_TIME) >= TO_DATE(?, 'YYYYMMDD')
                   AND TRUNC(INSTOCK_TIME) <= TO_DATE(?, 'YYYYMMDD')
                   and storage_no || '&' like ('%'||?||'%')
                 #condtions#
                union all
                select PRODUCNAME,
                       JUDGE_GRADENAME,
                       ACT_DIMATER,
                       JUDGE_STD_NAME,
                       STORAGE_NO,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       0 inListCount,
                       0 inListWgt,
                       ACT_COUNT outListCount,
                       (case
                         when PONDER_NO is null then
                          nvl(ACT_WEIGHT, 0)
                         else
                          nvl(PONDER_GROSS_WT, 0)
                       end) outListWgt,
                       0 AWEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       OUTSTOCK_TIME OUTSTOCK_TIME,
                       null INSTOCK_TIME
                  from ymd_gp_outlist
                 where OUTSTOCK_TYPE_CODE IN
                       ('800805', '800806', '800802', '800808', '800806')
                   and TRUNC(OUTSTOCK_TIME) >= TO_DATE(?, 'YYYYMMDD')
                   AND TRUNC(OUTSTOCK_TIME) <= TO_DATE(?, 'YYYYMMDD')
                   and storage_no || '&' LIKE ('%' || ? || '%')
                   
                 #condtions#
                union all
                select PRODUCNAME,
                       JUDGE_GRADENAME,
                       ACT_DIMATER,
                       JUDGE_STD_NAME,
                       STORAGE_NO,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       0 inListCount,
                       0 inListWgt,
                       0 outListCount,
                       0 outListWgt,
                       sum(ACT_WEIGHT) AWEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       null OUTSTOCK_TIME,
                       null INSTOCK_TIME
                  from ydm_gp_stocktakinglist e
                 where TRUNC(e.stocktaking_time) >= to_date(?, 'YYYYMMDD')
                   and TRUNC(e.stocktaking_time) <= to_date(?, 'YYYYMMDD')
                   and e.judge_stove_no || '&' LIKE ('%' || ? || '%')
                 #condtions#
                 group by PRODUCNAME,
                          JUDGE_GRADENAME,
                          ACT_DIMATER,
                          JUDGE_STD_NAME,
                          STORAGE_NO
                union all
                SELECT F.PRODUCNAME,
                       F.JUDGE_GRADENAME,
                       F.ACT_DIMATER,
                       F.JUDGE_STD_NAME,
                       G.STORAGE_NO AS STORAGE_NO,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       0 inListCount,
                       0 inListWgt,
                       0 outListCount,
                       0 outListWgt,
                       0 AWEIGHT,
                       sum(F.Act_Count) MATCOUNT,
                       sum(F.Act_Weight) MATWGT,
                       null OUTSTOCK_TIME,
                       null INSTOCK_TIME
                  FROM mat_gp_m F, ydm_gp_buttress G
                 WHERE F.MAT_NO = G.MAT_NO 
                 and g.storage_no || '&' LIKE ('%' || ? || '%')
                 #condtions#
                 group by F.PRODUCNAME,
                          F.JUDGE_GRADENAME,
                          F.ACT_DIMATER,
                          F.JUDGE_STD_NAME,
                          G.Storage_No) k
         group by PRODUCNAME,
                  JUDGE_GRADENAME,
                  ACT_DIMATER,
                  JUDGE_STD_NAME,
                  STORAGE_NO) h
 group by PRODUCNAME, JUDGE_GRADENAME, ACT_DIMATER, JUDGE_STD_NAME
               ]]>
</query>
</queryMap>