<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc="提单关闭">

	<query id="CorePipeBillOfLadingClose.getBiltNo" desc="查询提单号">
	<![CDATA[ 
	        SELECT T.BILL_NO,
       DECODE(CASE
                WHEN T.BILL_TICKET_STATUS = 0 AND T.OUTSTOCK_TICKET_STATUS = 0 AND
                     T.TRANSMNY_TICKET_STATUS = 0 THEN
                 0
                WHEN T.BILL_TICKET_STATUS = 2 AND T.OUTSTOCK_TICKET_STATUS = 2 AND
                     T.TRANSMNY_TICKET_STATUS = 2 THEN
                 2
                ELSE
                 (CASE
                   WHEN T.BILL_STATUS >= 4 THEN
                    1
                   ELSE
                    4
                 END)
              END,
              '1',
              '票据对齐('||T.BILL_TICKET_STATUS || T.OUTSTOCK_TICKET_STATUS ||
              T.TRANSMNY_TICKET_STATUS
              ||')',
              '2',
              '提单结束',            
              DECODE(T.BILL_STATUS,
                     '0',
                     '提单编制',
                     '1',
                     '提单下发',
                     '2',
                     '(预留)',
                     '3',
                     '提单执行',
                     '4',
                     '提单关闭',
                     '5',
                     '结算接收')) AS BILL_STATUS,
       DECODE(T.LOADVEHICLE_TYPE, '1', '实际', '2', '虚拟', '异常') LOADVEHICLE_TYPE,
       DECODE(T.LOADVEHICLE_TYPE,'2',DECODE(T.ISPRECLOSE,'0','未关闭','1','一次关闭','2','货款对齐'),'') ISPRECLOSE,
       T.SEND_PLAN_NO,MAX(T.PRINT_NUM) AS PRINT_NUM,MAX(TO_CHAR(T.PRINT_FLAG,'YYYY-MM-DD HH24:MI:SS'))AS PRINT_FLAG,MAX(TO_CHAR(T.DELETE_TIME,'YYYY-MM-DD HH24:MI:SS'))AS DELETE_TIME,T.DELVRY_ADDR,
       T.SALE_ORG_DESC,T.SALE_ORG,
       DECODE(T.ZOUT_FLAG,'1','是','0','否') AS ZOUT_FLAG,T.RECEIV_NM,MIN(T.PRD_CLS) PRD_CLS,
       DECODE(MIN(T.ZBS_FLAG),'1','已打印','0','未打印','2','老系统打印') ZBS_FLAG
  FROM YDM_ZC_BILL_M T,SLM_BASE_SALE_AREA_TYP A
 WHERE 
    T.BILL_NO || '&' LIKE ('%' || ? || '%')
   AND T.ORDER_NO || '&' LIKE ('%' || ? || '%')
   AND T.BILL_STATUS || '&' LIKE ('%' || ? || '%')
   AND T.CREATE_TIME BETWEEN TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND
       TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')
   AND T.VALIDFLAG='1'
   AND T.RETURN_FLAG='0'
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.getSaleorg" desc="查询对应的销售组织">
		select SALE_ORG from SLM_BASE_SALE_AREA_TYP t where t.departid = ?
</query>
	<query id="CorePipeBillOfLadingClose.getOrderInfoByBiltNo" desc="通过提单号查询合同信息">
	<![CDATA[ 
SELECT  CASE
         WHEN T1.REALINVOICENO IS NOT NULL AND T1.REALINVOICENO1 IS NOT NULL THEN
          T1.REALINVOICENO || '/' || T1.REALINVOICENO1
         WHEN T1.REALINVOICENO IS NULL THEN
          T1.REALINVOICENO1
         WHEN T1.REALINVOICENO1 IS NULL THEN
          T1.REALINVOICENO
       END AS REALINVOICENO,
	   T1.BILL_NO,
	   T1.BILL_NO_SEQ,
	   T1.ORDER_NO||'/'||T1.ORDER_SEQ AS ORDER_SEQ_NO,
       T1.ORDER_NO,
       T1.ORDER_SEQ,
       T1.DELIVERY_NO,
       T1.ORD_PK,
       T1.ORD_LN_PK,
       T1.ORD_LN_DLY_PK,
       T2.PRODUCCODE,
       T2.PRODUCNAME,
       T2.STEELCODE,
       T2.STEELNAME,
       T2.STD_CODE,
       T2.STD_NAME,
       T2.STD_STYLE,
       T2.STD_STYLE_DESC,
       T2.MODEL_CODE,
       T2.MODEL_DESC,
       T2.SPEC_CODE,
       T2.SPEC_NAME,
       T1.DELVRY_ADDR,
       TO_CHAR(T2.LEN_MIN,'FM9990.00') AS LEN_MIN,
       TO_CHAR(T2.LEN_MAX,'FM9990.00') AS LEN_MAX,
       T3.DELVRY_QTY,
       T3.ORDER_UNIT,
       T3.WGT_ESTMT,
       (SELECT T5.BASENAME FROM COM_BASE_INFO T5 WHERE T5.BASECODE=T1.PLAN_TRANS_TYPE) AS ACT_TRANS_TYPE,
       T1.RECEIV_NO,
       T1.RECEIV_NM,
       T4.STLMT_UNIT_NO,
       T4.STLMT_UNIT_NM,
       T1.CUSTOMER_NO,
       T1.CUSTOMER_NM,
       NVL(T1.PLAN_WT,0) AS PLAN_WT,
       NVL(T1.PLAN_NUM,0) AS PLAN_NUM,
       --T1.ACT_WT,
       (SELECT NVL(SUM(T6.ACT_COUNT), 0)
          FROM YDM_ZC_LOADVEHICLE_MAT T6
         WHERE T6.BILL_NO = T1.BILL_NO
           AND T6.ORD_LN_DLY_PK = T1.ORD_LN_DLY_PK) AS ACT_NUM,
       (SELECT  NVL(SUM(CASE
                     WHEN NVL(T6.PONDER_GROSS_WT, 0) != 0 THEN
                     T6.PONDER_GROSS_WT
                     WHEN NVL(T6.PONDER_GROSS_WT, 0) = 0 AND T.ORDER_UNIT = '吨' THEN
                         T6.SEND_NUM
                         ELSE
                         T6.ACT_WEIGHT
                   END),0)
          FROM YDM_ZC_LOADVEHICLE_MAT T6,SLM_ORDER_LINE T
         WHERE T6.BILL_NO = T1.BILL_NO
           AND T6.ORD_LN_PK = T.ORD_LN_PK
           AND T6.ORD_LN_DLY_PK = T1.ORD_LN_DLY_PK) AS ACT_WT,
       (select wm_concat(DISTINCT b.STORAGE_NAME) from
			YDM_ZC_BILL_c a left join YDM_BS_STORAGE b on a.STORAGE_NO = b.STORAGE_NO
			where a.BILL_NO = T1.BILL_NO) STORAGE_NAME,
	   (SELECT DECODE(PRT_FLAG,'1','是','否') FROM YDM_ZC_BILL_C WHERE BILL_NO = T1.BILL_NO AND ROWNUM = 1) PRT_FLAG,
	   T4.SALE_AREA_DESC,T4.CREATE_NAME
  FROM YDM_ZC_BILL_M T1, SLM_ORDER_LINE T2, SLM_ORDER_LINE_DELIVERY T3,SLM_ORDER_HEAD T4
 WHERE T1.ORD_LN_PK = T2.ORD_LN_PK(+)
   AND T1.ORD_LN_DLY_PK = T3.ORD_LN_DLY_PK(+)
   AND T1.ORD_PK=T4.ORD_PK(+)
   AND T1.BILL_NO = ?
   AND T1.VALIDFLAG = '1'
   AND T1.RETURN_FLAG='0'
	]]>
	</query>

	<query id="CorePipeBillOfLadingClose.getJustStoveNoInfoByBiltNo"
		desc="通过提单号查询炉信息">
	<![CDATA[ 
	SELECT P1.BILL_NO,
       P1.JUDGE_STOVE_NO,
       P1.BATCH_NO,
       P1.GROUP_NO,
       P1.PRODUCCODE,
       P1.PRODUCNAME,
       P1.STD_CODE,
       P1.STD_NAME,
       P1.GRADECODE,
       P1.GRADENAME,
       P1.STEELCODE,
       P1.STEELNAME,
       P1.STD_STYLE,
       P1.STD_STYLE_DESC,
       P1.SPEC_CODE,
       P1.SPEC_NAME,
       P1.MODEL_CODE,
       P1.MODEL_DESC,
       P1.ACT_DIMATER,
       P1.ACT_HEIGHT,
       TO_CHAR(P1.ACT_LEN_MIN,'FM9990.00') AS ACT_LEN_MIN,
        TO_CHAR(P1.ACT_LEN_MAX,'FM9990.00') AS ACT_LEN_MAX,
       P1.ORD_PK,
       P1.ORD_LN_PK,
       P1.ORD_LN_DLY_PK,
       P1.ORDER_UNIT,
       P1.PRT_FLAG,
       NVL(P1.ACT_COUNT,0) AS ACT_COUNT,
        NVL(P1.ACT_WEIGHT,0) AS ACT_WEIGHT,
        NVL(P1.ACT_COUNT_LOAD,0) AS ACT_COUNT_LOAD,
        NVL(P1.ACT_WEIGHT_LOAD,0) AS ACT_WEIGHT_LOAD,
        NVL(P1.ACT_TALL_COUNT,0) AS ACT_TALL_COUNT,
        NVL(P1.ACT_TALL_WT,0) AS ACT_TALL_WT,
        NVL(P1.SEND_NUM,0) AS SEND_NUM,
       TO_NUMBER(P1.ACT_WEIGHT_LOADMAT)-TO_NUMBER(P1.ACT_WEIGHT_LOAD) ACT_WEIGHT_POST
  FROM (SELECT T.BILL_NO,
               T.JUDGE_STOVE_NO,
               T.BATCH_NO,
               T.GROUP_NO,
               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.ORD_PK,
               T.ORD_LN_PK,
               T.ORD_LN_DLY_PK,
               T2.ORDER_UNIT,
               T.PRT_FLAG,
               SUM(NVL(T.ACT_COUNT,0)) ACT_COUNT,
               SUM(NVL(T.ACT_WEIGHT,0)) ACT_WEIGHT,
               SUM(NVL(T1.ACT_COUNT,0)) ACT_COUNT_LOAD,
               SUM(case WHEN NVL(T1.PONDER_GROSS_WT, 0) = '0' THEN NVL( T1.SEND_NUM, 0 ) ELSE NVL(T1.PONDER_GROSS_WT, 0) END ) SEND_NUM,
               SUM(CASE
                     WHEN T.EDIT_FLAG = '1' THEN
                      T.ACT_COUNT
                     ELSE
                      0
                   END) ACT_TALL_COUNT,
                   SUM(CASE
                     WHEN  T.EDIT_FLAG = '1' THEN
                      T.ACT_WEIGHT
                     ELSE
                      0
                   END) ACT_TALL_WT,
               SUM(NVL(CASE
                     WHEN T.PRT_FLAG = '0' THEN
                      T1.ACT_WEIGHT
                     ELSE
                      T1.PONDER_GROSS_WT
                   END,0)) ACT_WEIGHT_LOAD,
               SUM(NVL(T1.ACT_WEIGHT,0)) ACT_WEIGHT_LOADMAT
          FROM YDM_ZC_BILL_C T, YDM_ZC_LOADVEHICLE_MAT T1,SLM_ORDER_LINE_DELIVERY T2
         WHERE T.MAT_NO = T1.MAT_NO(+)
         AND T.BILL_NO=T1.BILL_NO(+)
         AND T.ORD_LN_DLY_PK=T2.ORD_LN_DLY_PK(+)
         AND T.BILL_NO = ?
         GROUP BY T.BILL_NO,
                  T.JUDGE_STOVE_NO,
                  T.BATCH_NO,
                  T.GROUP_NO,
                  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.ORD_PK,
                  T.ORD_LN_PK,
                  T.ORD_LN_DLY_PK,
                  T.PRT_FLAG,T2.ORDER_UNIT) P1
            ORDER BY P1.JUDGE_STOVE_NO,
       P1.BATCH_NO,
       P1.GROUP_NO
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.getBiltNoInfoByBillNo" desc="查询提单号信息">
	<![CDATA[ 
	   SELECT T.BILL_NO, T.BILL_STATUS, T.LOADVEHICLE_TYPE,T.ISPRECLOSE,T.BILL_TICKET_STATUS,T.OUTSTOCK_TICKET_STATUS,T.TRANSMNY_TICKET_STATUS,T.ORDER_NO,T.ORDER_SEQ,T.CUSTOMER_NM,T.DELETE_NAME,T.DELETE_TIME
  FROM YDM_ZC_BILL_M T
 WHERE T.BILL_NO = ?
   AND T.VALIDFLAG = '1'
 GROUP BY T.BILL_NO, T.BILL_STATUS, T.LOADVEHICLE_TYPE,T.ISPRECLOSE,T.BILL_TICKET_STATUS,T.OUTSTOCK_TICKET_STATUS,T.TRANSMNY_TICKET_STATUS,T.ORDER_NO,T.ORDER_SEQ,T.CUSTOMER_NM,T.DELETE_NAME,T.DELETE_TIME
	]]>
	</query>

	<query id="CorePipeBillOfLadingClose.updateYdmZcBillC" desc="关闭提单从表">
	<![CDATA[ 
	 DELETE YDM_ZC_BILL_C 
 WHERE LOADVEHICLE_FLAG = '0'
   AND EDIT_FLAG='0'
   AND VALIDFLAG = '1'
   AND BILL_NO = ?
   AND ORD_LN_DLY_PK=?
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.updateYdmZcBillM" desc="关闭提单主表">
	<![CDATA[ 
UPDATE YDM_ZC_BILL_M T
   SET T.BILL_STATUS = '4',
       T.PLAN_NUM     = TO_NUMBER(NVL(T.PLAN_NUM,0)) - TO_NUMBER(?), 
       T.PLAN_WT = TO_NUMBER(NVL(T.PLAN_WT,0)) - TO_NUMBER(?),
       T.PLAN_TOTL_LEN = TO_NUMBER(NVL(T.PLAN_TOTL_LEN,0)) - TO_NUMBER(?), 
       T.DELETE_NAME=?,
       T.DELETE_TIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS')
       WHERE T.BILL_NO = ?
       AND T.ORD_LN_DLY_PK = ? 
       AND T.VALIDFLAG ='1'
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.getLoadvehicle" desc="查询是否存在未确认的装车单">
		SELECT COUNT(1)ACOUNT FROM YDM_ZC_LOADVEHICLE T WHERE T.BILL_NO = ? AND
		T.AFFIRM_FLAG = '0'
	</query>

	<query id="CorePipeBillOfLadingClose.updateYdmZcBillM1" desc="关闭虚拟提单">
		update YDM_ZC_BILL_M T
		SET
		T.BILL_STATUS = '4',
	    T.DELETE_NAME=?,
	    T.DELETE_TIME =TO_DATE(?,'YYYY-MM-DD HH24:MI:SS')
		WHERE T.BILL_NO = ?
		AND T.VALIDFLAG = '1'
</query>

	<query id="CorePipeBillOfLadingClose.getJustStoveInfoByBillNo"
		desc="根据提单号查询炉信息">
	<![CDATA[ 
	SELECT T.BILL_NO,
       T.JUDGE_STOVE_NO,
       T.BATCH_NO,
       T.GROUP_NO,
       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.ORD_LN_PK
  FROM YDM_ZC_BILL_C T
 WHERE T.BILL_NO = ?
   AND T.VALIDFLAG = '1'
   AND T.RETURN_FLAG='0'
 GROUP BY T.BILL_NO,
          T.JUDGE_STOVE_NO,
          T.BATCH_NO,
          T.GROUP_NO,
          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.ORD_LN_PK
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.updateMagment" desc="关闭提单改变物料表">
	<![CDATA[ 
	UPDATE MAT_ZC_M M
   SET M.MAT_STATUS = '80150301',M.LOCK_BILL_FLAG = '0'
 WHERE M.MAT_STATUS = '80150302'
   AND M.MAT_NO IN((SELECT T.MAT_NO
                      FROM YDM_ZC_BILL_C T
                     WHERE T.BILL_NO = ?
                       AND T.EDIT_FLAG = '0'
                       AND T.LOADVEHICLE_FLAG = '0'
                       AND T.VALIDFLAG = '1'
                       AND M.MAT_NO=T.MAT_NO))
	]]>
	</query>

	<query id="CorePipeBillOfLadingClose.getTallysheet" desc="查询提单下的理货单">
	<![CDATA[ 
	  SELECT COUNT(1) COUNT
  FROM YDM_ZC_TALLYSHEET_M T
 WHERE T.BILL_NO = ?
   AND T.VALIDFLAG = '1'
   AND T.LOADVEHICLE_FLAG = '0'
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.getTallysheetC" desc="查询未装完车的理货单">
		SELECT COUNT(1) COUNT
		FROM YDM_ZC_TALLYSHEET_M T
		WHERE T.BILL_NO = ?
		AND T.VALIDFLAG = '1'
		AND T.LOADVEHICLE_FLAG = '1'
</query>
	<query id="CorePipeBillOfLadingClose.queryYdmZcBillC" desc="查询未理货未装车提单">
		SELECT T.ORD_PK,T.ORD_LN_PK,T.ORD_LN_DLY_PK,SUM(NVL(T.ACT_COUNT,0)) AS
		ACT_COUNT,SUM(NVL(T.ACT_WEIGHT,0)) AS ACT_WEIGHT,SUM(NVL(T.ACT_LEN,0))
		AS ACT_LEN
		FROM YDM_ZC_BILL_C T
		WHERE T.BILL_NO = ?
		AND T.EDIT_FLAG = '0'
		AND T.LOADVEHICLE_FLAG = '0'
		AND T.VALIDFLAG='1'
		GROUP BY T.ORD_PK,T.ORD_LN_PK,T.ORD_LN_DLY_PK
</query>
<query id="CorePipeBillOfLadingClose.queryYdmZcBillC1" desc="查询未理货未装车材料号">
		SELECT T.MAT_NO
		FROM YDM_ZC_BILL_C T
		WHERE T.BILL_NO = '#BILL_NO#'
		AND T.EDIT_FLAG = '0'
		AND T.LOADVEHICLE_FLAG = '0'
		AND T.VALIDFLAG='1'
        ORDER BY T.MAT_NO ASC
</query>
	<query id="CorePipeBillOfLadingClose.updateYdmZcBillM2" desc="关闭提单主表">
	<![CDATA[ 
	update YDM_ZC_BILL_M T
   SET T.BILL_STATUS = '4',
   T.DELETE_NAME=?,
   T.DELETE_TIME=TO_DATE(?,'YYYY-MM-DD HH24:MI:SS')
 WHERE T.BILL_NO = ?
   AND T.VALIDFLAG = '1'
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.querySendBill" desc="通过出厂计划号查询提单">
	<![CDATA[ 
	 SELECT T.ORD_PK,
         T.ORD_LN_PK,
         T.ORD_LN_DLY_PK,
         SUM(NVL(T.PLAN_NUM, 0)) AS PLAN_NUM,
         SUM(NVL(T.PLAN_WT, 0)) AS PLAN_WT,
         SUM(NVL(T.PLAN_TOTL_LEN, 0)) AS PLAN_TOTL_LEN
    FROM YDM_ZC_BILL_M T
   WHERE T.SEND_PLAN_NO = ?
     AND T.BILL_STATUS > '3'
   GROUP BY T.ORD_PK, T.ORD_LN_PK, T.ORD_LN_DLY_PK
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.updateSendM" desc="修改计划主表实绩量">
	<![CDATA[ 
	 UPDATE YDM_ZC_SENDPLAN_M
   SET ACT_WT      = TO_NUMBER(?),
       UPDATE_NAME = ?,
       UPDATE_TIME = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')
 WHERE SEND_PLAN_NO = ?
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.updateSendC" desc="修改计划从表实绩量">
	<![CDATA[ 
	  UPDATE YDM_ZC_SENDPLAN_C
   SET ACT_WT = TO_NUMBER(?), 
   UPDATE_NAME = ?, 
   UPDATE_TIME = TO_DATE(?,'YYYY-MM-DD HH24:MI:SS') 
   WHERE SEND_PLAN_NO = ?
    AND ORD_LN_DLY_PK = ?
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.QUERYBILLREPORT" desc="提单报表">
	<![CDATA[
SELECT T1.*,
       TO_NUMBER(T1.ACT_WEIGHT_LOADMAT) - TO_NUMBER(T1.BEEN_WEIGHT) ACT_WEIGHT_POST
  FROM (SELECT T.BILL_NO,
               T.ORDER_NO || '/' || T.ORDER_SEQ AS ORDER_SEQ_NO,
               T.ORDER_NO,
               T.ORDER_SEQ,
               T.DELIVERY_NO,
               (SELECT B.BASENAME
                  FROM COM_BASE_INFO B
                 WHERE B.BASECODE = T.PLAN_TRANS_TYPE) AS PLAN_TRANS_TYPE,
               C.JUDGE_STOVE_NO,
               C.BATCH_NO,
               C.GROUP_NO,
               (SELECT S.STORAGE_NAME
                  FROM YDM_BS_STORAGE S
                 WHERE S.STORAGE_NO = C.STORAGE_NO) AS STORAGE_NO,
               T.CREATE_NAME,
               TO_CHAR(MAX(T.UPDATE_TIME), 'YYYY-MM-DD HH24:MI:SS') AS UPDATE_TIME,
               TO_CHAR(MAX(T.CREATE_TIME), 'YYYY-MM-DD HH24:MI:SS') AS CREATE_TIME,
               TO_CHAR(MIN(T.DELETE_TIME), 'YYYY-MM-DD HH24:MI:SS') AS DELETE_TIME,
               (SELECT TO_CHAR(MAX(M.CREATE_TIME), 'YYYY-MM-DD HH24:MI:SS')
                  FROM YDM_ZC_TALLYSHEET_M M
                 WHERE T.BILL_NO = M.BILL_NO) AS TALL_TIME,
               (SELECT TO_CHAR(MAX(LD.CREATE_TIME), 'YYYY-MM-DD HH24:MI:SS')
                  FROM YDM_ZC_LOADVEHICLE LD
                 WHERE T.BILL_NO = LD.BILL_NO) AS LOAD_TIME,
               C.PRODUCNAME,
               C.STEELNAME,
               C.SPEC_NAME,
               C.STD_NAME,
               C.GRADENAME,
               C.STD_STYLE_DESC,
               C.MODEL_DESC,
               C.VSTEL_DESC,
               G.BALNCE_PRICE,
               DECODE(T.LOADVEHICLE_TYPE, '1', '实际', '2', '虚拟') AS LOADVEHICLE_TYPE,
               DECODE(CASE
                        WHEN T.BILL_TICKET_STATUS = 0 AND
                             T.OUTSTOCK_TICKET_STATUS = 0 AND
                             T.TRANSMNY_TICKET_STATUS = 0 THEN
                         0
                        WHEN T.BILL_TICKET_STATUS = 2 AND
                             T.OUTSTOCK_TICKET_STATUS = 2 AND
                             T.TRANSMNY_TICKET_STATUS = 2 THEN
                         2
                        ELSE
                         (CASE
                           WHEN T.BILL_STATUS >= 4 THEN
                            1
                           ELSE
                            4
                         END)
                      END,
                      '1',
                      '票据对齐(' || T.BILL_TICKET_STATUS ||
                      T.OUTSTOCK_TICKET_STATUS || T.TRANSMNY_TICKET_STATUS || ')',
                      '2',
                      '提单结束',
                      DECODE(T.BILL_STATUS,
                             '0',
                             '提单编制',
                             '1',
                             '提单下发',
                             '2',
                             '(预留)',
                             '3',
                             '提单执行',
                             '4',
                             '提单关闭',
                             '5',
                             '结算接收')) AS BILL_STATUS,
               DECODE(T.LOADVEHICLE_TYPE,
                      '2',
                      DECODE(T.ISPRECLOSE,
                             '0',
                             '未关闭',
                             '1',
                             '一次关闭',
                             '2',
                             '货款对齐'),
                      '') AS ISPRECLOSE,
               DECODE(MAX(C.PRT_FLAG), '0', '否', '1', '是') AS PRT_FLAG,
               DECODE(T.PICKUP_FLAG, '0', '否', '1', '是') AS PICKUP_FLAG,
               DECODE(T.ZOUT_FLAG, '0', '否', '1', '是') AS ZOUT_FLAG,
               T.STATION_NM,
               T.SPCL_LN_NM,
               T.RECEIV_NM,
               T.ORD_PK,
               T.ORD_LN_PK,
               T.ORD_LN_DLY_PK,
               T.SALE_ORG,
               T.SALE_ORG_DESC,
               H.SALE_AREA_DESC,
               (CASE
                 WHEN T.SALE_ORG = '100102' THEN
                  DECODE(T.REALINVOICENO1, '', '未结算', '已结算')
                 ELSE
                  DECODE(T.REALINVOICENO, '', '未结算', '已结算')
               END) AS REALINVOICENO,
               CASE
                 WHEN T.LOADVEHICLE_TYPE = 2 AND T.BILL_STATUS > '3' THEN
                  DECODE(L.VALIDFLAG, '1', '未确认', '2', '销售确认','3','销售确认')
                 WHEN T.LOADVEHICLE_TYPE = 2 AND T.BILL_STATUS < '4' THEN
                  DECODE(L.VALIDFLAG, '1', '未确认', '2', '装车确认')
                 WHEN T.LOADVEHICLE_TYPE = 1 THEN
                  DECODE(L.VALIDFLAG,
                         '1',
                         '未确认',
                         '2',
                         '装车确认',
                         '3',
                         '销售确认')
               END AS VALIDFLAG,
               max(NVL(L.BAL_YEAR_MONTH, t.BAL_YEAR_MONTH)) BAL_YEAR_MONTH,
               SUM(NVL(C.ACT_COUNT, 0)) AS ACT_COUNT,
               SUM(NVL(C.ACT_WEIGHT, 0)) AS ACT_WEIGHT,
               SUM(NVL(C.ACT_LEN, 0)) AS ACT_LEN,
               SUM(C.SEND_NUM) AS SEND_NUM,
               SUM(NVL(CASE
                         WHEN C.EDIT_FLAG = '1' THEN
                          C.ACT_COUNT
                         ELSE
                          0
                       END,
                       0)) PLAN_COUNT,
               SUM(NVL(CASE
                         WHEN C.EDIT_FLAG = '1' THEN
                          C.ACT_WEIGHT
                         ELSE
                          0
                       END,
                       0)) PLAN_WEIGHT,
               SUM(NVL(CASE
                         WHEN C.EDIT_FLAG = '1' THEN
                          C.ACT_LEN
                         ELSE
                          0
                       END,
                       0)) PLAN_LEN,
               SUM(NVL(CASE
                         WHEN L.VALIDFLAG > '1' THEN
                          L.ACT_COUNT
                         ELSE
                          0
                       END,
                       0)) BEEN_COUNT,
               SUM(NVL(CASE
                         WHEN L.VALIDFLAG > '1' THEN
                          L.ACT_LEN
                         ELSE
                          0
                       END,
                       0)) BEEN_LEN,
               SUM(NVL(CASE
                         WHEN C.PRT_FLAG = '0' AND L.VALIDFLAG > '1' THEN
                          L.ACT_WEIGHT
                         WHEN C.PRT_FLAG = '1' AND L.VALIDFLAG > '1' THEN
                          L.PONDER_GROSS_WT
                       END,
                       0)) BEEN_WEIGHT,
               
               SUM(NVL(CASE
                         WHEN L.VALIDFLAG > '1' THEN
                          L.ACT_WEIGHT
                         ELSE
                          0
                       END,
                       0)) ACT_WEIGHT_LOADMAT
          FROM YDM_ZC_BILL_C          C,
               YDM_ZC_LOADVEHICLE_MAT L,
               YDM_ZC_BILL_M          T,
               SLM_ORDER_HEAD         H,
               SLM_ORDER_LINE  		  G
         WHERE C.MAT_NO = L.MAT_NO(+)
           AND C.BILL_NO = T.BILL_NO
           AND C.BILL_NO=L.BILL_NO(+)
           AND H.ORD_PK = T.ORD_PK(+)
           AND C.ORD_LN_PK=G.ORD_LN_PK(+)
           --AND T.ORD_LN_DLY_PK = C.ORD_LN_DLY_PK 暂时先注释
           AND T.order_no = C.order_no
           and t.order_seq = c.order_seq
           AND T.VALIDFLAG = '1'
           AND T.RETURN_FLAG='0'
           AND C.RETURN_FLAG='0'
           AND (L.RETURN_FLAG = '0' OR L.RETURN_FLAG IS NULL)
         
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.QUERYBILLMREPORT" desc="提单主表报表">
	<![CDATA[
	SELECT T1.*,
       TO_NUMBER(T1.ACT_WEIGHT_LOADMAT) - TO_NUMBER(T1.BEEN_WEIGHT) ACT_WEIGHT_POST
  FROM (SELECT T2.BILL_NO,
               T2.PLAN_TRANS_TYPE,
               T2.CREATE_NAME,
               T2.BILL_STATUS,
               T2.LOADVEHICLE_TYPE,
               T2.STATION_NM,
               T2.SPCL_LN_NM,
               T2.RECEIV_NM,
               T2.ISPRECLOSE,
               T2.SALE_ORG,
               T2.SALE_ORG_DESC,
               T2.REALINVOICENO,
               T2.PICKUP_FLAG,
               T2.UPDATE_TIME,
               T2.CREATE_TIME,
               T2.DELETE_TIME,
               T2.TALL_TIME,
               T2.LOAD_TIME,
               L.BAL_YEAR_MONTH,
               H.SALE_AREA_DESC,
               T2.ZOUT_FLAG,
               SUM(NVL(C.ACT_COUNT, 0)) AS ACT_COUNT,
               SUM(NVL(C.ACT_WEIGHT, 0)) AS ACT_WEIGHT,
               SUM(NVL(CASE
                         WHEN C.EDIT_FLAG = '1' THEN
                          C.ACT_COUNT
                         ELSE
                          0
                       END,
                       0)) PLAN_COUNT,
               SUM(NVL(CASE
                         WHEN C.EDIT_FLAG = '1' THEN
                          C.ACT_WEIGHT
                         ELSE
                          0
                       END,
                       0)) PLAN_WEIGHT,
               
               SUM(NVL(CASE
                         WHEN L.VALIDFLAG > '1' THEN
                          L.ACT_COUNT
                         ELSE
                          0
                       END,
                       0)) BEEN_COUNT,
               
               SUM(NVL(CASE
                         WHEN C.PRT_FLAG = '0' AND L.VALIDFLAG > '1' THEN
                          L.ACT_WEIGHT
                         WHEN C.PRT_FLAG = '1' AND L.VALIDFLAG > '1' THEN
                          L.PONDER_GROSS_WT
                       END,
                       0)) BEEN_WEIGHT,
               
               SUM(NVL(CASE
                         WHEN L.VALIDFLAG > '1' THEN
                          L.ACT_WEIGHT
                         ELSE
                          0
                       END,
                       0)) ACT_WEIGHT_LOADMAT
          FROM (SELECT T.BILL_NO,
                        (SELECT B.BASENAME
                           FROM COM_BASE_INFO B
                          WHERE B.BASECODE = T.PLAN_TRANS_TYPE) AS PLAN_TRANS_TYPE,
                        T.CREATE_NAME,
                        TO_CHAR(MAX(T.UPDATE_TIME), 'YYYY-MM-DD HH24:MI:SS') AS UPDATE_TIME,
                        TO_CHAR(MAX(T.CREATE_TIME), 'YYYY-MM-DD HH24:MI:SS') AS CREATE_TIME,
                         TO_CHAR(MIN(T.DELETE_TIME), 'YYYY-MM-DD HH24:MI:SS') AS DELETE_TIME,
                        (SELECT TO_CHAR(MAX(M.CREATE_TIME),
                                        'YYYY-MM-DD HH24:MI:SS')
                           FROM YDM_ZC_TALLYSHEET_M M
                          WHERE T.BILL_NO = M.BILL_NO) AS TALL_TIME,
                        (SELECT TO_CHAR(MAX(LD.CREATE_TIME),
                                        'YYYY-MM-DD HH24:MI:SS')
                           FROM YDM_ZC_LOADVEHICLE LD
                          WHERE T.BILL_NO = LD.BILL_NO) AS LOAD_TIME,
                        
                        DECODE(T.LOADVEHICLE_TYPE, '1', '实际', '2', '虚拟') AS LOADVEHICLE_TYPE,
                        DECODE(CASE
                WHEN T.BILL_TICKET_STATUS = 0 AND T.OUTSTOCK_TICKET_STATUS = 0 AND
                     T.TRANSMNY_TICKET_STATUS = 0 THEN
                 0
                WHEN T.BILL_TICKET_STATUS = 2 AND T.OUTSTOCK_TICKET_STATUS = 2 AND
                     T.TRANSMNY_TICKET_STATUS = 2 THEN
                 2
                ELSE
                 (CASE
                   WHEN T.BILL_STATUS >= 4 THEN
                    1
                   ELSE
                    4
                 END)
              END,
              '1',
              '票据对齐('||T.BILL_TICKET_STATUS || T.OUTSTOCK_TICKET_STATUS ||
              T.TRANSMNY_TICKET_STATUS
              ||')',
              '2',
              '提单结束',            
              DECODE(T.BILL_STATUS,
                     '0',
                     '提单编制',
                     '1',
                     '提单下发',
                     '2',
                     '(预留)',
                     '3',
                     '提单执行',
                     '4',
                     '提单关闭',
                     '5',
                     '结算接收')) AS BILL_STATUS,
                        DECODE(T.LOADVEHICLE_TYPE,
                               '2',
                               DECODE(T.ISPRECLOSE,
                                      '0',
                                      '未关闭',
                                      '1',
                                      '一次关闭',
                                      '2',
                                      '货款对齐'),
                               '') AS ISPRECLOSE,
                        DECODE(T.PICKUP_FLAG, '0', '否', '1', '是') AS PICKUP_FLAG,
                        DECODE(T.ZOUT_FLAG, '0', '否', '1', '是') AS ZOUT_FLAG,
                        T.STATION_NM,
                        T.SPCL_LN_NM,
                        T.RECEIV_NM,
                        T.SALE_ORG,
                        T.SALE_ORG_DESC,
                         (CASE
                         WHEN T.SALE_ORG = '100102' THEN
                          DECODE(MIN(T.REALINVOICENO1), '', '未结算', '已结算')
                         ELSE
                          DECODE(MIN(T.REALINVOICENO), '', '未结算', '已结算')
                       END) AS REALINVOICENO
                   FROM YDM_ZC_BILL_M T
                   LEFT JOIN SLM_ORDER_HEAD O ON T.ORD_PK = O. ORD_PK
                  WHERE T.VALIDFLAG = '1'
                   AND T.RETURN_FLAG='0'
                  
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.getBillStatus" desc="提单状态">
	<![CDATA[
SELECT T.BILL_STATUS,T.LOADVEHICLE_TYPE,T.ISPRECLOSE,MIN(T.PRD_CLS) PRD_CLS  FROM YDM_ZC_BILL_M T WHERE T.BILL_NO=? 
 AND T.VALIDFLAG='1' 
GROUP BY T.BILL_STATUS,T.LOADVEHICLE_TYPE,T.ISPRECLOSE
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.queryOrderUnit" desc="查询交货行计量单位">
	<![CDATA[
 SELECT T.ORDER_UNIT FROM SLM_ORDER_LINE_DELIVERY T WHERE T.ORD_LN_DLY_PK=? AND T.VALIDFLAG='1'
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.updateBillSendNum" desc="修改提单材料表长度">
	<![CDATA[
    UPDATE YDM_ZC_BILL_C T SET T.SEND_NUM =TO_NUMBER(?)  WHERE T.BILL_NO=? AND T.MAT_NO=?
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.updateLoadMatSeq" desc="修改装车从表交货行对应的提单序号">
	<![CDATA[
    DECLARE
  V_FLAG YDM_ZC_BILL_M.LOADVEHICLE_TYPE%TYPE := NULL;
BEGIN
  SELECT T.LOADVEHICLE_TYPE
    INTO V_FLAG
    FROM YDM_ZC_BILL_M T
   WHERE T.BILL_NO = ?
     AND ROWNUM = 1;
  IF (V_FLAG = '2') THEN
    UPDATE YDM_ZC_LOADVEHICLE_MAT T
       SET T.VALIDFLAG = '3'
     WHERE T.BILL_NO = ?;
  END IF;
UPDATE YDM_ZC_LOADVEHICLE_MAT T SET T.BILL_NO_SEQ=? WHERE T.BILL_NO=? AND T.ORD_LN_DLY_PK=?;
END;
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.querySendNUM" desc="提单关闭时通过发货量查询提单从表已发材料号">
	<![CDATA[
    SELECT T.MAT_NO
  FROM YDM_ZC_LOADVEHICLE_MAT T
 WHERE T.BILL_NO = ?
   AND T.ORD_LN_DLY_PK = ?
   AND T.JUDGE_STOVE_NO = ?
   AND T.BATCH_NO = ?
   AND T.GROUP_NO = ?
   AND NVL(T.STD_CODE,'/') = NVL(?,'/')
ORDER BY T.MAT_NO ASC
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.updatePintFlag" desc="回写打印次数，打印时间">
	<![CDATA[
   UPDATE YDM_ZC_BILL_M T
   SET T.PRINT_NUM  = TO_NUMBER(NVL(T.PRINT_NUM, 0)) + '1',
       T.PRINT_FLAG = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')
 WHERE T.BILL_NO =?
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.deleteBillM" desc="撤销关闭的提单">
	<![CDATA[
  UPDATE YDM_ZC_BILL_M T  SET T.BILL_STATUS = '3'  WHERE T.BILL_NO = ?
	]]>
	</query>
	<query id="CorePipeBillOfLadingClose.getBillCtall" desc="查询未理货的提单">
	<![CDATA[
		SELECT COUNT(1) COUNT
		FROM YDM_ZC_BILL_C T
		WHERE T.BILL_NO = ?
		AND T.EDIT_FLAG = '0'
		]]>
</query>
	<query id="CorePipeBillOfLadingClose.updateLoadNum" desc="修改装车材料表实发数">
	<![CDATA[
    UPDATE YDM_ZC_LOADVEHICLE_MAT T SET T.SEND_NUM =TO_NUMBER(?)  WHERE T.BILL_NO=? AND T.MAT_NO=?
    ]]>
</query>
<query id="CorePipeBillOfLadingClose.updateOutSendNum" desc="修改出库材料表实发数">
	<![CDATA[
	UPDATE Ydm_Zc_Outlist T
       SET T.SEND_NUM = TO_NUMBER(?)
     WHERE T.MAT_NO = ?
       AND T.OUTSTOCK_TYPE_CODE = '800805';
    ]]>
</query>
<query id="CorePipeBillOfLadingClose.updateOutSendNumPZ" desc="修改出库材料表实发数(坯子)">
	<![CDATA[
    UPDATE YMD_GP_Outlist T SET T.SEND_NUM =TO_NUMBER(?)  WHERE T.MAT_NO=? AND T.OUTSTOCK_TYPE_CODE='800805'
    ]]>
</query>
<query id="CorePipeBillOfLadingClose.updatebillM" desc="修改提单主表实际量">
	<![CDATA[
 UPDATE YDM_ZC_BILL_M T
   SET T.ACT_NUM      = NVL((SELECT SUM(T1.ACT_COUNT)
                              FROM YDM_ZC_LOADVEHICLE_MAT T1
                             WHERE T1.BILL_NO = T.BILL_NO
                               AND T.RETURN_FLAG = '0'),
                            0),
       T.ACT_WT       = NVL((SELECT SUM(CASE
                                         WHEN T1.PONDER_NO IS NOT NULL THEN
                                          T1.PONDER_GROSS_WT
                                         ELSE
                                          (CASE
                                            WHEN L.ORDER_UNIT = '吨' THEN
                                             T1.SEND_NUM
                                            ELSE
                                             T1.ACT_WEIGHT
                                          END)
                                       END)
                              FROM YDM_ZC_LOADVEHICLE_MAT T1,
                                   SLM_ORDER_LINE         L
                             WHERE T1.ORD_LN_PK = L.ORD_LN_PK
                               AND T1.BILL_NO = T.BILL_NO
                               AND T1.RETURN_FLAG = '0'),
                            0),
       T.ACT_TOTL_LEN = NVL((SELECT SUM(T1.ACT_LEN)
                              FROM YDM_ZC_LOADVEHICLE_MAT T1
                             WHERE T1.BILL_NO = T.BILL_NO
                               AND T.RETURN_FLAG = '0'),
                            0)
 WHERE T.BILL_NO = ?
    ]]>
</query>
<query id="CorePipeBillOfLadingClose.updatebillM1" desc="修改提单主表实际量">
	<![CDATA[
 UPDATE YDM_ZC_BILL_M T
   SET T.ACT_NUM      = '0',
       T.ACT_WT       = '0',
       T.ACT_TOTL_LEN ='0'
 WHERE T.BILL_NO = ?
    ]]>
</query>
</queryMap>