<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "红冲管理">
<query id = "FrmRedChong.doQuery" desc = "查询">
SELECT T1.STOVE_NO,
       T1.LOT_NO,
       T1.JUDGE_STOVE_NO,
       T1.BATCH_NO,
       T1.BATCH_GROUD_NO,
       T1.PRODUCCODE,
       T1.PRODUCNAME,
       T1.STEELCODE,
       T1.STEELNAME,
       T1.SPEC_CODE,
       T1.SPEC_NAME,
       T1.MODEL_CODE,
       T1.MODEL_DESC,
       T1.STD_CODE,
       T1.STD_NAME,
       T1.ACT_DIMATER,
       T1.ACT_HEIGHT,
       T1.BELONG_CODE,
       T1.BELONG_NAME,
       T1.STD_STYLE,
       T1.STD_STYLE_DESC,
       T1.GRADECODE,
       T1.GRADENAME,
       SUM(T1.ACT_COUNT) ACT_COUNT,
       SUM(T1.ACT_COUNT) REDCOUNT,
       TO_CHAR(SUM(T1.ACT_WEIGHT), 'FM999999990.000') ACT_WEIGHT,
       TO_CHAR(SUM(T1.ACT_WEIGHT), 'FM999999990.000') REDWEIGHT,
       SUM(NVL(T1.ACT_LEN, 0)) AS ACT_LEN,
       SUM(NVL(T1.ACT_LEN, 0)) AS LENGTH,
       MAX(TO_CHAR(T1.ACT_LEN_MIN, 'FM999990.00')) AS ACT_LEN_MIN,
       MAX(TO_CHAR(T1.ACT_LEN_MAX, 'FM999990.00')) AS ACT_LEN_MAX,
       MAX(TO_CHAR(T1.INSTOCK_TIME, 'YYYY-MM-DD HH24:MI:SS')) INSTOCK_TIME,
       T2.STORAGE_NO,
       S.STORAGE_NAME,
       T1.MEMO,
       T1.MAT_STATUS,
       T1.PLINE_CODE,
       T1.PLINE_NAME,
       MAX(TO_CHAR(TO_DATE(T1.BAL_YEAR_MONTH, 'YYYY-MM'), 'YYYY-MM')) AS BAL_YEAR_MONTH,
       T1.PRODUCT_FLAG,
       T1.MATERIAL_NO,
       T1.MATERIAL_NAME,
       T1.PRO_ORDER_NO,
       T1.JUDGE_RESULT,
        MAX(L.LOCATION_NAME),
       T1.SUPPLY_UNIT_CODE,
       T1.SUPPLY_UNIT
  FROM MAT_ZC_M T1, YDM_ZC_BUTTRESS T2
  LEFT JOIN YDM_BS_STORAGE S ON S.STORAGE_NO = T2.STORAGE_NO
    left join YDM_BS_LOCATION L ON L.LOCATION_NO = T2.LOCATION_NO
 WHERE T1.JUDGE_STOVE_NO = T2.JUDGE_STOVE_NO
   AND T1.BATCH_NO = T2.BATCH_NO
   AND T1.MAT_NO = T2.MAT_NO
   AND T1.PRODUCT_FLAG = '801402' AND T1.MNG_LOCK_FLAG = '0'
</query>
<query id = "FrmRedChong.doQuerySeq" desc = "按支查询">
SELECT T.*,(SELECT LOCATION_NAME FROM YDM_BS_LOCATION WHERE LOCATION_NO = S.LOCATION_NO) LOCATION_NAME
  FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
 WHERE T.MAT_NO = S.MAT_NO
    AND T.MNG_LOCK_FLAG = '0'
   AND T.JUDGE_STOVE_NO = '#JUDGE_STOVE_NO#'
   AND T.BATCH_NO = '#BATCH_NO#'
   AND T.BATCH_GROUD_NO = '#BATCH_GROUD_NO#'
   AND T.PRODUCT_FLAG = '801402'
   AND S.STORAGE_NO = '#STORAGE_NO#'
    AND T.SUPPLY_UNIT_CODE = '#SUPPLY_UNIT_CODE#'
</query>
<query id = "FrmRedChong.getState" desc = "查询结转控制标识">
select T.MEMO from COM_BASE_INFO t WHERE T.BASECODE = ?
</query>
<query id = "FrmRedChong.doQuerySum" desc = "查询总支数、重量、长度">
SELECT NVL(SUM(T.ACT_COUNT),0) ACT_COUNT,
       NVL(SUM(T.ACT_WEIGHT),0) ACT_WEIGHT,
       NVL(SUM(T.ACT_LEN),0) ACT_LEN
  FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
 WHERE T.MAT_NO = S.MAT_NO
   AND T.JUDGE_STOVE_NO = '#JUDGE_STOVE_NO#'
   AND T.BATCH_NO = '#BATCH_NO#'
   AND T.BATCH_GROUD_NO = '#BATCH_GROUD_NO#'
   AND S.STORAGE_NO = '#STORAGE_NO#'
   AND T.PRODUCT_FLAG = '801402'
   AND T.MNG_LOCK_FLAG = '0'
   
</query>
    <query id = "FrmRedChong.doQuerySumInstockDoc" desc = "查询总支数、重量、长度">
        SELECT NVL(SUM(T.ACT_COUNT),0) ACT_COUNT,
        NVL(SUM(T.ACT_WEIGHT),0) ACT_WEIGHT,
        NVL(SUM(T.ACT_LEN),0) ACT_LEN
        FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
        WHERE T.MAT_NO = S.MAT_NO
        AND T.INSTOCK_DOC = '#INSTOCK_DOC#'

    </query>
<query id = "FrmRedChong.queryRedDoc" desc = "生成红冲单号">
SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') ||
         LPAD(NVL(MAX(SUBSTR(T.INSTOCK_DOC, 9)), 0) + 1, 4, 0) INSTOCK_DOC
    FROM YDM_ZC_REDCHONG T
   WHERE T.INSTOCK_DOC LIKE TO_CHAR(SYSDATE, 'YYYYMMDD') || '____'
</query>
<query id = "FrmRedChong.insertRedChong" desc = "生成红冲审批单">
DECLARE
  V_ACT_COUNT  MAT_ZC_M.ACT_COUNT % TYPE := NULL;
  V_ACT_WEIGHT MAT_ZC_M.ACT_WEIGHT % TYPE := NULL;
  V_ACT_LEN    MAT_ZC_M.ACT_LEN % TYPE := NULL;
BEGIN
  SELECT NVL(SUM(T.ACT_COUNT), 0) ACT_COUNT,
         NVL(SUM(T.ACT_WEIGHT), 0) ACT_WEIGHT,
         NVL(SUM(T.ACT_LEN), 0) ACT_LEN
    into V_ACT_COUNT, V_ACT_WEIGHT, V_ACT_LEN
    FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
   WHERE T.MAT_NO = S.MAT_NO
     AND T.INSTOCK_DOC = '#INSTOCK_DOC#';
  FOR MYROW IN (SELECT T.*, S.STORAGE_NO
                  FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
                 WHERE T.MAT_NO = S.MAT_NO
                   AND T.JUDGE_STOVE_NO = '#JUDGE_STOVE_NO#'
                   AND T.BATCH_NO = '#BATCH_NO#'
                   AND T.BATCH_GROUD_NO = '#BATCH_GROUD_NO#'
                   AND T.PRODUCT_FLAG = '801402'
                   AND T.SUPPLY_UNIT_CODE = '#SUPPLY_UNIT_CODE#'
                   AND ROWNUM = 1) LOOP
    INSERT INTO YDM_ZC_REDCHONG
      (INSTOCK_DOC,
       LOT_NO,
       STOVE_NO,
       JUDGE_STOVE_NO,
       BATCH_NO,
       BATCH_GROUD_NO,
       MATERIAL_NO,
       MATERIAL_NAME,
       PRODUCCODE,
       PRODUCNAME,
       STD_CODE,
       STD_NAME,
       GRADECODE,
       GRADENAME,
       STEELCODE,
       STEELNAME,
       STD_STYLE,
       STD_STYLE_DESC,
       SPEC_CODE,
       SPEC_NAME,
       MODEL_CODE,
       MODEL_DESC,
       ACT_DIMATER,
       ACT_HEIGHT,
       ACT_LEN_MIN,
       ACT_LEN_MAX,
       ACT_LEN,
       ACT_COUNT,
       ACT_WEIGHT,
       ACT_THEORY_WEIGHT,
       BELONG_CODE,
       BELONG_NAME,
       STORAGE_NO,
       PRODUCT_FLAG,
       MAT_STATUS,
       BAL_YEAR_MONTH,
       CREATE_NAME,
       CREATE_TIME,
       REDCHONG_MEMO,
       RED_TYPE,
       PLINE_CODE,
       PLINE_NAME,
       RED_FLAG,
       SUPPLY_UNIT_CODE,
       SUPPLY_UNIT,
       ORDER_NO,
       ORDER_SEQ,
       ORDER_SEQ_NO)
    VALUES
      ('#INSTOCK_DOC#',
       MYROW.LOT_NO,
       MYROW.STOVE_NO,
       MYROW.JUDGE_STOVE_NO,
       MYROW.BATCH_NO,
       MYROW.BATCH_GROUD_NO,
       MYROW.MATERIAL_NO,
       MYROW.MATERIAL_NAME,
       MYROW.PRODUCCODE,
       MYROW.PRODUCNAME,
       MYROW.STD_CODE,
       MYROW.STD_NAME,
       MYROW.GRADECODE,
       MYROW.GRADENAME,
       MYROW.STEELCODE,
       MYROW.STEELNAME,
       MYROW.STD_STYLE,
       MYROW.STD_STYLE_DESC,
       MYROW.SPEC_CODE,
       MYROW.SPEC_NAME,
       MYROW.MODEL_CODE,
       MYROW.MODEL_DESC,
       MYROW.ACT_DIMATER,
       MYROW.ACT_HEIGHT,
       MYROW.ACT_LEN_MIN,
       MYROW.ACT_LEN_MAX,
       V_ACT_LEN,
       V_ACT_COUNT,
       V_ACT_WEIGHT,
       V_ACT_WEIGHT,
       MYROW.BELONG_CODE,
       MYROW.BELONG_NAME,
       MYROW.STORAGE_NO,
       MYROW.PRODUCT_FLAG,
       MYROW.MAT_STATUS,
       '#BAL_YEAR_MONTH#',
       '#CREATE_NAME#',
       SYSDATE,
       '#REDCHONG_MEMO#',
       '#RED_TYPE#',
       'C071',
       '制造管理部',
       '#RED_FLAG#',
       '#SUPPLY_UNIT_CODE#',
       '#SUPPLY_UNIT#',
       MYROW.ORDER_NO,
       MYROW.ORDER_SEQ,
       MYROW.ORDER_SEQ_NO);
  END LOOP;
END;
</query>
<query id = "FrmRedChong.updateMatZc" desc = "修改库存">
<![CDATA[
DECLARE
BEGIN
  FOR MYROW IN (SELECT *
                  FROM (SELECT T.*, S.STORAGE_NO
                          FROM MAT_ZC_M T, YDM_ZC_BUTTRESS S
                         WHERE T.MAT_NO = S.MAT_NO
                           AND T.JUDGE_STOVE_NO = '#JUDGE_STOVE_NO#'
                           AND T.BATCH_NO = '#BATCH_NO#'
                           AND T.BATCH_GROUD_NO = '#BATCH_GROUD_NO#'
                           AND S.STORAGE_NO = '#STORAGE_NO#'
                           AND T.PRODUCT_FLAG = '801402'
                           AND T.MNG_LOCK_FLAG = '0'
                         ORDER BY T.MAT_NO DESC)
                 WHERE ROWNUM <= #REDCOUNT#) LOOP
    UPDATE MAT_ZC_M T
       SET T.INSTOCK_DOC = '#INSTOCK_DOC#',
       T.MNG_LOCK_FLAG = '1',
       T.MNG_LOCK_REMARK = '红冲'
     WHERE T.MAT_NO = MYROW.MAT_NO;
  END LOOP;
END;
]]>
</query>
<query id = "FrmRedChong.updateMatZcSeq" desc = "修改库存红冲单号">
UPDATE MAT_ZC_M T
       SET T.INSTOCK_DOC = '#INSTOCK_DOC#',
       T.MNG_LOCK_FLAG = '1',
       T.MNG_LOCK_REMARK = '红冲'
     WHERE T.MAT_NO = '#MAT_NO#'
</query>
<query id = "FrmRedChong.doQueryRedList" desc = "查询红冲实绩">
 SELECT 
       T.INSTOCK_DOC,
       T.LOT_NO,
       T.STOVE_NO,
       T.JUDGE_STOVE_NO,
       T.BATCH_NO,
       T.BATCH_GROUD_NO,
       T.MATERIAL_NO,
       T.MATERIAL_NAME,
       T.PRODUCCODE,
       T.PRODUCNAME,
       T.STD_CODE,
       T.STD_NAME,
       T.GRADECODE,
       T.GRADENAME,
       T.STEELCODE,
       T.STEELNAME,
       T.STD_STYLE,
       T.STD_STYLE_DESC,
       T.SPEC_CODE,
       T.SPEC_NAME,
       T.MODEL_CODE,
       T.MODEL_DESC,
       T.ACT_DIMATER,
       T.ACT_HEIGHT,
       T.ACT_LEN_MIN,
       T.ACT_LEN_MAX,
       T.ACT_LEN,
       T.ACT_COUNT,
       T.ACT_WEIGHT,
       T.ACT_THEORY_WEIGHT,
       T.BELONG_CODE,
       T.BELONG_NAME,
       T.STORAGE_NO,
       T.PRODUCT_FLAG,
       T.MAT_STATUS,
       T.BAL_YEAR_MONTH,
       T.CREATE_NAME,
       TO_CHAR(T.CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
       DECODE(T.FRIM_FLAG,'0','未审核','1','审核通过','2','审核不通过')FRIM_FLAG,
       T.FRIM_NAME,
       TO_CHAR(T.FRIM_TIME, 'YYYY-MM-DD HH24:MI:SS') FRIM_TIME,
       T.FRIM_MEMO,
       T.REDCHONG_MEMO,
       T.FRIM_PLINE_CODE,
       T.FRIM_PLINE_NAME,
       T.PLINE_CODE,
       T.PLINE_NAME,
       DECODE(T.RED_FLAG,'0','返线加工','1','不返线加工')RED_FLAG,
       T.SUPPLY_UNIT_CODE,
       T.SUPPLY_UNIT,
       T.ORDER_NO || '/' || T.ORDER_SEQ ORDER_NO
 FROM YDM_ZC_REDCHONG T
 WHERE 1= 1
</query>
<query id = "FrmRedChong.cancelRed" desc = "撤销红冲">
DECLARE
BEGIN
  FOR MYROW IN (SELECT *
                  FROM MAT_ZC_M T
                 WHERE T.INSTOCK_DOC = '#INSTOCK_DOC#'
                   AND T.JUDGE_STOVE_NO = '#JUDGE_STOVE_NO#'
                   AND T.BATCH_NO = '#BATCH_NO#'
                   AND T.BATCH_GROUD_NO = '#BATCH_GROUD_NO#'
                 ORDER BY T.MAT_NO ASC) LOOP
    UPDATE MAT_ZC_M T
       SET T.MNG_LOCK_FLAG   = '0',
           T.MNG_LOCK_REMARK = '',
           T.INSTOCK_DOC     = ''
     WHERE T.MAT_NO = MYROW.MAT_NO;
  END LOOP;
  DELETE FROM YDM_ZC_REDCHONG T WHERE T.INSTOCK_DOC = '#INSTOCK_DOC#';
END;
</query>
<query id = "FrmRedChong.selectFrimFlag" desc = "查询审核标识">
SELECT T.FRIM_FLAG FROM YDM_ZC_REDCHONG T WHERE T.INSTOCK_DOC = '#INSTOCK_DOC#'
</query>
</queryMap>