<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "管坯库存">
<query id = "FrmMatGpM.doQuery" desc = "查询">
<![CDATA[
SELECT T1.*, S1.GOWHERE_NAME AS MAT_GOWHERE_NAME
  FROM (SELECT T2.*, K.ORDER_NO || '/' || K.ORDER_SEQ AS ORDER_NO
          FROM (SELECT DECODE(T.PRODUCT_FLAG,
                              '801401',
                              '在制品',
                              '801402',
                              '成品',
                              '801403',
                              '商品') PRODUCT_FLAG,
                       DECODE(T.MAT_STATUS,
                              '80150101',
                              '材料产出待判',
                              '80150102',
                              '材料产出(已判定)',
                              '80150103',
                              '材料已编计划',
                              '80150104',
                              '材料待处理',
                              '80150105',
                              '材料加工',
                              '80150201',
                              '材料产出待判（未综合判定）',
                              '80150202',
                              '材料产出（已综合判定）') MAT_STATUS,
                       DECODE(T.HANGING_FLAG, '0', '否', '1', '是') HANGING_FLAG,
                       
                       T.STOVE_NO,
                       T.JUDGE_STOVE_NO,
                       T.GROUD_NO,
                       T.MATERIAL_NO,
                       T.MATERIAL_NAME,
                       T.GRADECODE,
                       T.GRADENAME,
                       T.STD_CODE,
                       T.STD_NAME,
                       T.PRO_ORDER_NO,
                       T.PLINE_CODE,
                       T.PLINE_NAME,
                       T.DIMATER,
                       T.ACT_LEN,
                       T.FIX_NUM,
                       T.FIX_LEN,
                       SUM(T.ACT_COUNT) ACT_COUNT,
                       SUM(T.ACT_WEIGHT) ACT_WEIGHT,
                       T.BELONG_CODE,
                       T.BELONG_NAME,
                       (SELECT C.STORAGE_NAME
                          FROM YDM_BS_STORAGE C
                         WHERE C.STORAGE_NO = S.STORAGE_NO) STORAGE_NAME,
                       S.STORAGE_NO,
                       DECODE(T.QCM_STAUS,
                              '00',
                              '合格',
                              '11',
                              '待处理',
                              '22',
                              '废品',
                              '10',
                              '处理合格',
                              '12',
                              '处理判废') QCM_STAUS,
                       
                       DECODE(T.GPTYPE, '1', '铸坯', '2', '锻坯') GPTYPE,
                       T.FURNACENO,
                       T.SEQUENCENO,
                       S.LOCATION_NO,
                       S.LARY_NO,
                       S.LOCATION_NAME,
                       T.HEAT_PLAN_NO_NEXT,
                       T.PRO_PLAN_ID_ZG,
                       T.GX_PLAN_NO_ZG,
                       T.BAL_YEAR_MONTH,
                       MAX(TO_CHAR(T.INSTOCK_TIME, 'YYYY-MM-DD HH:MM:SS')) INSTOCK_TIME,
                       DECODE(T.INSTANCY_FLAG, '0', '否', '1', '是') INSTANCY_FLAG,
                       (SELECT T1.BASENAME
                          FROM COM_BASE_INFO T1
                         WHERE T1.BASECODE = T.JUDGE_RESULT_CODE) JUDGE_RESULT_CODE
                  FROM MAT_GP_M T, YDM_GP_BUTTRESS S
                 WHERE T.MAT_NO = S.MAT_NO
                   AND T.QCM_STAUS = '00' 
                   AND T.PRODUCT_FLAG = '801401'
                   #BAL_YEAR_MONTH#
                   #JUDGE_STOVE_NO#
                 GROUP BY T.STOVE_NO,
                          T.JUDGE_STOVE_NO,
                          T.MATERIAL_NO,
                          T.MATERIAL_NAME,
                          T.GROUD_NO,
                          T.FURNACENO,
                          T.SEQUENCENO,
                          T.GRADECODE,
                          T.GRADENAME,
                          T.STD_CODE,
                          T.STD_NAME,
                          T.PRO_ORDER_NO,
                          T.PLINE_CODE,
                          T.PLINE_NAME,
                          T.QCM_STAUS,
                          T.GPTYPE,
                          T.JUDGE_RESULT_CODE,
                          T.DIMATER,
                          T.ACT_LEN,
                          T.FIX_NUM,
                          T.FIX_LEN,
                          T.PRODUCT_FLAG,
                          T.MAT_STATUS,
                          S.STORAGE_NO,
                          T.BELONG_CODE,
                          T.BELONG_NAME,
                          S.LOCATION_NO,
                          S.LARY_NO,
                          S.LOCATION_NAME,
                          T.HANGING_FLAG,
                          T.INSTANCY_FLAG,
                          T.BAL_YEAR_MONTH,
                          T.HEAT_PLAN_NO_NEXT,
                          T.PRO_PLAN_ID_ZG,
                          T.GX_PLAN_NO_ZG) T2
          LEFT JOIN PLN_ORDER_ZG_S K
            ON T2.PRO_PLAN_ID_ZG = K.PRO_PLAN_ID
           AND T2.GX_PLAN_NO_ZG = K.GX_PLAN_NO) T1,
       (SELECT S.STOVE_NO,
               S.JUDGE_STOVE_NO,
               MAX(S.GOWHERE_NAME) GOWHERE_NAME
          FROM PLN_STEELFOR_ORD S
         GROUP BY S.STOVE_NO, S.JUDGE_STOVE_NO) S1
 WHERE T1.STOVE_NO = S1.STOVE_NO(+)
   AND T1.JUDGE_STOVE_NO = S1.JUDGE_STOVE_NO(+)
 ]]>
</query>

<query id = "FrmMatGpM.QueryPlanStatus" desc = "查询计划状态">
<![CDATA[
select t.* from PLN_ZY_ZG_M t WHERE T.HEAT_PLAN_NO = ?
 ]]>
</query>

<query id = "FrmMatGpM.takeBill" desc = "置余材">
<![CDATA[
DECLARE
BEGIN
  FOR MYROW IN (SELECT *
                  FROM (SELECT T.*, S.STORAGE_NO
                          FROM MAT_GP_M T
                          LEFT JOIN YDM_GP_BUTTRESS S
                            ON T.MAT_NO = S.MAT_NO
                         WHERE T.JUDGE_STOVE_NO = '#JUDGE_STOVE_NO#'
                           AND NVL(T.HEAT_PLAN_NO_NEXT, '/') =
                               NVL('#HEAT_PLAN_NO_NEXT#', '/')
                           AND NVL(T.PRO_PLAN_ID_ZG, '/') =
                               NVL('#PRO_PLAN_ID_ZG#', '/')
                           AND NVL(T.GX_PLAN_NO_ZG, '/') =
                               NVL('#GX_PLAN_NO_ZG#', '/')
                           AND T.ACT_LEN = '#ACT_LEN#'
                           AND S.STORAGE_NO = '#STORAGE_NO#'
                           AND T.QCM_STAUS = '00'
                           AND T.PRODUCT_FLAG = '801401'
                           AND T.BELONG_CODE = '002001001'
                         ORDER BY T.MAT_NO DESC)
                 WHERE ROWNUM <= '#ACT_COUNT#') LOOP
    IF (MYROW.INSTANCY_FLAG = '1') THEN
      UPDATE MAT_GP_M T
         SET T.MAT_STATUS        = '80150101',
             T.HANGING_FLAG      = '0',
             T.HEAT_PLAN_NO_NEXT = '',
             T.PRO_PLAN_ID_ZG    = '',
             T.GX_PLAN_NO_ZG     = ''
       WHERE T.MAT_NO = MYROW.MAT_NO
         AND T.PLINE_CODE = MYROW.PLINE_CODE;
    ELSE
      UPDATE MAT_GP_M T
         SET T.MAT_STATUS        = '80150102',
             T.HANGING_FLAG      = '0',
             T.HEAT_PLAN_NO_NEXT = '',
             T.PRO_PLAN_ID_ZG    = '',
             T.GX_PLAN_NO_ZG     = ''
       WHERE T.MAT_NO = MYROW.MAT_NO
         AND T.PLINE_CODE = MYROW.PLINE_CODE;
    END IF;
  END LOOP;
  --删除轧管炉计划
  DELETE FROM PLN_ZY_ZG_M T WHERE T.HEAT_PLAN_NO = '#HEAT_PLAN_NO_NEXT#';
  DELETE FROM PLN_ZY_ZG_C T WHERE T.HEAT_PLAN_NO = '#HEAT_PLAN_NO_NEXT#';
END;
 ]]>
</query>
</queryMap>