<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "盘亏">
<query id = "FrmStuffInventoryOut.doQueryMat" desc = "查询库存信息">
<![CDATA[
 SELECT 
       max(TO_CHAR(T.INSTOCK_TIME, 'YYYY-MM-DD HH24:MI:SS')) INSTOCK_TIME,
       T.MEMO,
       --(SELECT C.BASENAME FROM COM_BASE_INFO C WHERE C.BASECODE =  T.PRODUCT_FLAG) PRODUCT_FLAG,
       --(SELECT D.BASENAME FROM COM_BASE_INFO D WHERE D.BASECODE =  T.MAT_STATUS)MAT_STATUS,
       T.PRODUCT_FLAG,
       T.MAT_STATUS,
       T.STOVE_NO,
       T.JUDGE_STOVE_NO,
       T.BATCH_NO,
       T.CAST_NO,
       T.CCMID,
       T.SAMPLE_FLAG,
       T.CUTTING_FLAG,
       T.CUTTING_NUM,
       T.MATERIAL_NO,
       T.MATERIAL_NAME,
       T.PRODUCCODE,
       T.PRODUCNAME,
       T.JUDGE_GRADECODE,
       T.JUDGE_GRADENAME,
       T.JUDGE_STD_CODE,
       T.JUDGE_STD_NAME,
       T.PRO_ORDER_NO,
       T.PRO_ORDER_ID,
       T.PLINE_CODE,
       T.PLINE_NAME,
       T.ACT_DIMATER,
       T.ACT_LEN,
       T.ACT_LEN_MIN,
       T.ACT_LEN_MAX,
       T.ACT_FIX_NUM,
       T.ACT_FIX_LEN,
       sum(T.ACT_COUNT)ACT_COUNT,
       sum(T.ACT_WEIGHT)ACT_WEIGHT,
       sum(T.ACT_COUNT)Out_Act_Count,
       T.BELONG_CODE,
       T.BELONG_NAME,
       (SELECT C.STORAGE_NAME FROM YDM_BS_STORAGE C WHERE C.STORAGE_NO = S.STORAGE_NO)STORAGE_NO
       --T1.STORAGE_NAME
 FROM MAT_GP_M T,YDM_GP_BUTTRESS S
 --LEFT JOIN ydm_bs_storage T1 ON S.STORAGE_NO=T1.STORAGE_NO
 WHERE T.MAT_NO = S.MAT_NO 
 AND T.MAT_STATUS<>'80150302'
 AND T.JUDGE_STOVE_NO || '&' LIKE ('%' || ? || '%')
]]>
</query>
<query id = "FrmStuffInventoryOut.doQueryStock" desc = "查询盘亏实绩">
<![CDATA[
SELECT 
       BELONG_NAME,
       PRO_ORDER_NO,
       PRO_ORDER_ID,
       TO_CHAR(STOCKTAKING_TIME, 'YYYY-MM-DD HH24:MI:SS') STOCKTAKING_TIME,
       STORAGE_NO,
       BAL_YEAR_MONTH,
       MEMO,
       CREATE_NAME,
       STOCKTAKING_DOC,
       STOCKTAKING_RSN_NAME,
       STOVE_NO,
       FLAG_STOVE_NO,
       JUDGE_STOVE_NO,
       GROUD_NO,
       BATCH_NO,
       CAST_NO,
       CCMID,
       SAMPLE_FLAG,
       CUTTING_FLAG,
       CUTTING_NUM,
       MATERIAL_NAME,
       PRODUCNAME,
       JUDGE_GRADENAME,
       JUDGE_STD_NAME,
       ACT_DIMATER,
       ACT_LEN,
       ACT_LEN_MIN,
       ACT_LEN_MAX,
       ACT_FIX_NUM,
       ACT_FIX_LEN,
       ACT_COUNT,
       ACT_WEIGHT,
       PLINE_NAME
 FROM YDM_GP_STOCKTAKINGLIST
 WHERE STOCKTAKING_TYPE_CODE = '800602'
 AND JUDGE_STOVE_NO || '&' LIKE ('%' || ? || '%')
 ]]>
</query>
<query id = "FrmStuffInventoryOut.selectMatStaus" desc= "查询材料状态">
SELECT SUM(T.ACT_COUNT)ACOUNT
  FROM MAT_GP_M T, YDM_GP_BUTTRESS S
 WHERE T.MAT_NO = S.MAT_NO
   AND T.STOVE_NO = ?
   AND T.JUDGE_STOVE_NO =?
   AND T.BATCH_NO=?
   AND T.GROUD_NO = ?
   AND T.PRODUCT_FLAG = ?
   AND T.MAT_STATUS = ?
   AND S.STORAGE_NO = ?
</query>
<query id = "FrmStuffInventoryOut.selectMatWgt" desc= "查询盘亏重量">
<![CDATA[
SELECT SUM(T.ACT_WEIGHT)WEIGHT,SUM(T.ACT_LEN)LEN
  FROM MAT_GP_M T, YDM_GP_BUTTRESS S
 WHERE T.MAT_NO = S.MAT_NO
   AND T.STOVE_NO = ?
   AND T.JUDGE_STOVE_NO =?
   AND T.BATCH_NO=?
   AND T.GROUD_NO = ?
   AND T.PRODUCT_FLAG = ?
   AND T.MAT_STATUS = ?
   AND S.STORAGE_NO = ?
   AND ROWNUM < ?
   ORDER BY T.MAT_NO DESC
   ]]>
</query>
<query id = "FrmStuffInventoryOut.doAddOut" desc = "盘亏">
<![CDATA[
DECLARE
  V_OUTSTOCK_DOC    YMD_GP_OUTLIST.OUTSTOCK_DOC%TYPE := NULL; --出库单号
  V_OUTSTOCK_SQE    YMD_GP_OUTLIST.OUTSTOCK_SQE%TYPE := NULL; --出库流水
  v_stocktaking_doc ydm_gp_stocktakinglist.stocktaking_doc%type := null; --盘亏单号
  v_stocktaking_sqe ydm_gp_stocktakinglist.stocktaking_sqe%type := null; --盘亏流水

  V_MEMO       COM_BASE_INFO.MEMO%TYPE := NULL;
  V_BASENAME   COM_BASE_INFO.BASENAME%TYPE := NULL;
  v_TKMemo     com_base_info.memo%type := null;
  V_TKBASENAME COM_BASE_INFO.BASENAME%TYPE := NULL;
  V_OCOUNT     NUMBER := 1;
  v_TKOUNT     NUMBER := 1;
  V_TIME       YMD_GP_OUTLIST.CREATE_TIME%TYPE := SYSDATE;
  V_BATHYEAR   MAT_ZC_M.BAL_YEAR_MONTH%TYPE := TO_CHAR(SYSDATE, 'YYYYMM');
  v_index      Number := 0;
BEGIN
  --生成盘亏单号
  SELECT MEMO, BASENAME
    INTO v_TKMemo, V_TKBASENAME
    FROM COM_BASE_INFO
   WHERE BASECODE = ?;
  SELECT v_TKMemo || TO_CHAR(SYSDATE, 'YYMMDD') ||
         LPAD(NVL(MAX(SUBSTR(stocktaking_doc, 9)), 0) + 1, 5, 0) stocktaking_doc
    INTO v_stocktaking_doc
    FROM ydm_gp_stocktakinglist T
   WHERE T.stocktaking_type_code = ?
     AND TO_CHAR(T.CREATE_TIME, 'YYYY-MM-DD') =
         TO_CHAR(SYSDATE, 'YYYY-MM-DD');
  --生成出库单号
  SELECT MEMO, BASENAME
    INTO V_MEMO, V_BASENAME
    FROM COM_BASE_INFO
   WHERE BASECODE = ?;
  SELECT V_MEMO || TO_CHAR(SYSDATE, 'YYMMDD') ||
         LPAD(NVL(MAX(SUBSTR(OUTSTOCK_DOC, 9)), 0) + 1, 5, 0) OUTSTOCK_DOC
    INTO V_OUTSTOCK_DOC
    FROM YMD_GP_OUTLIST T
   WHERE T.OUTSTOCK_TYPE_CODE = ?
     AND TO_CHAR(T.CREATE_TIME, 'YYYY-MM-DD') =
         TO_CHAR(SYSDATE, 'YYYY-MM-DD');
  FOR MYROW IN (SELECT T.*,
                       S.STORAGE_NO,
                       S.LOCATION_NO,
                       S.STORAGE_ATTR,
                       S.STORAGE_ATTR_NAME,
                       S.STORAGE_TYPE_NO,
                       S.STORAGE_TYPE_NAME
                  FROM MAT_GP_M T, YDM_GP_BUTTRESS S
                 WHERE T.MAT_NO = S.MAT_NO
                   AND T.STOVE_NO = ?
                   AND T.JUDGE_STOVE_NO = ?
                   AND T.BATCH_NO = ?
                   AND T.GROUD_NO = ?
                   AND T.PRODUCT_FLAG = ?
                   AND T.MAT_STATUS = ?
                   AND S.STORAGE_NO = ?
                   AND ROWNUM < ?
                 ORDER BY T.MAT_NO DESC) LOOP
   if(v_index = 0)then
   v_stocktaking_sqe := v_stocktaking_doc || LPAD(v_TKOUNT, 4, 0);
    INSERT INTO YDM_GP_STOCKTAKINGLIST
      (BELONG_NAME,
       PRO_ORDER_NO,
       PRO_ORDER_ID,
       STOCKTAKING_TIME,
       STORAGE_NO,
       LOCATION_NO,
       STORAGE_ATTR,
       STORAGE_ATTR_NAME,
       STORAGE_TYPE_NO,
       STORAGE_TYPE_NAME,
       DEPARTMENT_CODE,
       DEPARTMENT_DESC,
       MANAGEMENT_NO,
       MANAGEMENT_NAME,
       BAL_YEAR_MONTH,
       MEMO,
       CREATE_NAME,
       CREATE_TIME,
       STOCKTAKING_SQE,
       STOCKTAKING_DOC,
       STOCKTAKING_TYPE_CODE,
       STOCKTAKING_TYPE_NAME,
       STOCKTAKING_RSN_CODE,
       STOCKTAKING_RSN_NAME,
       PRODUCT_FLAG,
       MAT_STATUS,
       STOVE_NO,
       FLAG_STOVE_NO,
       LOT_NO,
       JUDGE_STOVE_NO,
       GROUD_NO,
       BATCH_NO,
       CAST_NO,
       CCMID,
       SAMPLE_FLAG,
       CUTTING_FLAG,
       CUTTING_NUM,
       MATERIAL_NO,
       MATERIAL_NAME,
       PRODUCCODE,
       PRODUCNAME,
       JUDGE_GRADECODE,
       JUDGE_GRADENAME,
       JUDGE_STD_CODE,
       JUDGE_STD_NAME,
       ACT_DIMATER,
       ACT_HEIGHT,
       ACT_LEN,
       ACT_LEN_MIN,
       ACT_LEN_MAX,
       ACT_FIX_NUM,
       ACT_FIX_LEN,
       ACT_COUNT,
       ACT_WEIGHT,
       ACT_THEORY_WEIGHT,
       PLINE_CODE,
       PLINE_NAME,
       BELONG_CODE)
    VALUES
      (myrow.belong_name,
       myrow.pro_order_no,
       myrow.pro_order_id,
       v_time,
       myrow.storage_no,
       myrow.location_no,
       myrow.storage_attr,
       myrow.storage_attr_name,
       myrow.storage_type_no,
       myrow.storage_type_name,
       ?,
       ?,
       ?,
       ?,
       V_BATHYEAR,
       myrow.memo,
       ?,
       v_time,
       v_stocktaking_sqe,
       v_stocktaking_doc,
       ?,
       V_TKBASENAME,
       '',
       ?,
       myrow.product_flag,
       myrow.mat_status,
       myrow.stove_no,
       myrow.flag_stove_no,
       myrow.lot_no,
       myrow.judge_stove_no,
       myrow.groud_no,
       myrow.batch_no,
       myrow.cast_no,
       myrow.ccmid,
       myrow.sample_flag,
       myrow.cutting_flag,
       myrow.cutting_num,
       myrow.material_no,
       myrow.material_name,
       myrow.produccode,
       myrow.producname,
       myrow.judge_gradecode,
       myrow.judge_gradename,
       myrow.judge_std_code,
       myrow.judge_std_name,
       myrow.act_dimater,
       myrow.act_height,
       ?,
       myrow.act_len_min,
       myrow.ACT_LEN_MAX,
       myrow.ACT_FIX_NUM,
       myrow.ACT_FIX_LEN,
       ?,
       ?,
       '',
       myrow.PLINE_CODE,
       myrow.PLINE_NAME,
       myrow.BELONG_CODE);
     end if;
    V_OUTSTOCK_SQE := V_OUTSTOCK_DOC || LPAD(V_OCOUNT, 4, 0);
    INSERT INTO YMD_GP_OUTLIST
      (OUTSTOCK_SQE,
       OUTSTOCK_PLAN_NO,
       OUTSTOCK_DOC,
       OUTSTOCK_TYPE_CODE,
       OUTSTOCK_TYPE_NAME,
       VEHICLE_NO,
       MAT_NO,
       MAT_SQE_NO,
       PRODUCT_FLAG,
       MAT_STATUS,
       STOVE_NO,
       FLAG_STOVE_NO,
       LOT_NO,
       JUDGE_STOVE_NO,
       BATCH_NO,
       CAST_NO,
       CCMID,
       SAMPLE_FLAG,
       CUTTING_FLAG,
       CUTTING_NUM,
       MATERIAL_NO,
       MATERIAL_NAME,
       PRODUCCODE,
       PRODUCNAME,
       JUDGE_GRADECODE,
       JUDGE_GRADENAME,
       JUDGE_STD_CODE,
       JUDGE_STD_NAME,
       ACT_DIMATER,
       ACT_HEIGHT,
       ACT_LEN,
       ACT_LEN_MIN,
       ACT_LEN_MAX,
       ACT_FIX_NUM,
       ACT_FIX_LEN,
       ACT_COUNT,
       ACT_WEIGHT,
       ACT_THEORY_WEIGHT,
       PLINE_CODE,
       PLINE_NAME,
       PRO_ORDER_NO,
       PRO_ORDER_ID,
       BELONG_CODE,
       BELONG_NAME,
       STORAGE_NO,
       LOCATION_NO,
       DEPARTMENT_CODE,
       DEPARTMENT_DESC,
       MANAGEMENT_NO,
       MANAGEMENT_NAME,
       TAR_STORAGE_NO,
       TAR_LOCATION_NO,
       TAR_DEPARTMENT_CODE,
       TAR_DEPARTMENT_DESC,
       TAR_MANAGEMENT_NO,
       TAR_MANAGEMENT_NAME,
       TAR_REC_FLAG,
       BAL_YEAR_MONTH,
       OUTSTOCK_TIME,
       OUT_BC,
       OUT_BZ,
       STORAGE_ATTR,
       STORAGE_ATTR_NAME,
       STORAGE_TYPE_NO,
       STORAGE_TYPE_NAME,
       MEMO,
       CREATE_NAME,
       CREATE_TIME,
       GROUD_NO)
    VALUES
      (V_OUTSTOCK_SQE,
       '',
       V_OUTSTOCK_DOC,
       ?,
       V_BASENAME,
       '',
       MYROW.MAT_NO,
       MYROW.MAT_SQE_NO,
       MYROW.PRODUCT_FLAG,
       MYROW.MAT_STATUS,
       MYROW.STOVE_NO,
       MYROW.FLAG_STOVE_NO,
       MYROW.LOT_NO,
       MYROW.JUDGE_STOVE_NO,
       MYROW.BATCH_NO,
       MYROW.CAST_NO,
       MYROW.CCMID,
       MYROW.SAMPLE_FLAG,
       MYROW.CUTTING_FLAG,
       MYROW.CUTTING_NUM,
       MYROW.MATERIAL_NO,
       MYROW.MATERIAL_NAME,
       MYROW.PRODUCCODE,
       MYROW.PRODUCNAME,
       MYROW.JUDGE_GRADECODE,
       MYROW.JUDGE_GRADENAME,
       MYROW.JUDGE_STD_CODE,
       MYROW.JUDGE_STD_NAME,
       MYROW.ACT_DIMATER,
       MYROW.ACT_HEIGHT,
       MYROW.ACT_LEN,
       MYROW.ACT_LEN_MIN,
       MYROW.ACT_LEN_MAX,
       MYROW.ACT_FIX_NUM,
       MYROW.ACT_FIX_LEN,
       MYROW.ACT_COUNT,
       MYROW.ACT_WEIGHT,
       MYROW.ACT_THEORY_WEIGHT,
       MYROW.PLINE_CODE,
       MYROW.PLINE_NAME,
       MYROW.PRO_ORDER_NO,
       MYROW.PRO_ORDER_ID,
       MYROW.BELONG_CODE,
       MYROW.BELONG_NAME,
       MYROW.STORAGE_NO,
       MYROW.LOCATION_NO,
       ?,
       ?,
       ?,
       ?,
       '',
       '',
       '',
       '',
       '',
       '',
       '0',
       V_BATHYEAR,
       V_TIME,
       '1',
       '1',
       MYROW.STORAGE_ATTR,
       MYROW.STORAGE_ATTR_NAME,
       MYROW.STORAGE_TYPE_NO,
       MYROW.STORAGE_TYPE_NAME,
       MYROW.MEMO,
       ?,
       V_TIME,
       MYROW.GROUD_NO);
    DELETE YDM_GP_BUTTRESS T WHERE T.MAT_NO = MYROW.MAT_NO;
    V_OCOUNT := V_OCOUNT + 1;
    v_index  := v_index + 1;
  END LOOP;
END;

]]>
</query>
</queryMap>