<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc="铁运提单编制">

	<query id="FrmPipeTrainBill.querySourse" desc="通过合同的主键查询库存信息">
        <![CDATA[
       SELECT T2.* FROM (
           SELECT T.JUDGE_STOVE_NO,
       T.BATCH_NO,
       T.BATCH_GROUD_NO,
       T1.STORAGE_NO,
       T1.LOCATION_NO,
       (SELECT T4.STORAGE_NAME
          FROM YDM_BS_STORAGE T4
         WHERE T4.STORAGE_NO = T1.STORAGE_NO) STORAGE_NAME,
       (SELECT T5.LOCATION_NAME
          FROM YDM_BS_LOCATION T5
         WHERE T5.LOCATION_NO = T1.LOCATION_NO) LOCATION_NAME,
          T.PLINE_CODE,T.PLINE_NAME,
       T.STEELCODE,
       T.STEELNAME,
       (SELECT COM.BASENAME FROM COM_BASE_INFO COM WHERE COM.BASECODE=T.IN_MOLD) IN_MOLD,
       T.PRODUCCODE,
       T.PRODUCNAME,
       T.STD_CODE,
       T.STD_NAME,
       T.STD_STYLE,
       T.STD_STYLE_DESC,
       T.SPEC_CODE,
       T.SPEC_NAME,
       T.MODEL_CODE,
       T.MODEL_DESC,
       T.ORD_PK,
       T.ORD_LN_PK,
       T.ORD_LN_DLY_PK,
       T.memo,
       TO_CHAR(T.ACT_LEN_MIN,'FM99990.00') AS ACT_LEN_MIN,
       TO_CHAR(T.ACT_LEN_MAX,'FM99990.00') AS ACT_LEN_MAX, 
       TO_CHAR(MAX(T.INSTOCK_TIME), 'YYYY-MM-DD HH24:MI:SS') JUDGE_RESULT_TIME,
       '0' SEND_NUM,
        SUM(CASE
             WHEN T.MAT_STATUS = '80150301' THEN
              NVL(T.ACT_COUNT,0)
             ELSE
              0
           END) ACT_COUNT,
       SUM(CASE
             WHEN T.MAT_STATUS = '80150301' THEN
              NVL(T.ACT_WEIGHT,0)
             ELSE
              0
           END) ACT_WEIGHT,
           SUM(CASE
             WHEN T.MAT_STATUS = '80150301' THEN
              NVL(T.ACT_LEN,0)
             ELSE
              0
           END) ACT_LEN,
       SUM(CASE
             WHEN T.MAT_STATUS = '80150302' THEN
              NVL(T.ACT_COUNT,0)
             ELSE
              0
           END) YB_COUNT,
       SUM(CASE
             WHEN T.MAT_STATUS = '80150302' THEN
              NVL(T.ACT_WEIGHT,0)
             ELSE
              0
           END) YB_WEIGHT,
             SUM(CASE
             WHEN T.MAT_STATUS = '80150302' THEN
              NVL(T.ACT_LEN,0)
             ELSE
              0
           END) YB_LEN,
       SUM(CASE
             WHEN T.MAT_STATUS = '80150301' THEN
              NVL(T.ACT_COUNT,0)
             WHEN T.MAT_STATUS = '80150302' THEN
              NVL(T.ACT_COUNT,0)
             ELSE
              0
           END) KC_COUNT,
       SUM(CASE
             WHEN T.MAT_STATUS = '80150301' THEN
              NVL(T.ACT_WEIGHT,0)
             WHEN T.MAT_STATUS = '80150302' THEN
              NVL(T.ACT_WEIGHT,0)
             ELSE
              0
           END) KC_WEIGHT,
           SUM(CASE
             WHEN T.MAT_STATUS = '80150301' THEN
              NVL(T.ACT_LEN,0)
             WHEN T.MAT_STATUS = '80150302' THEN
              NVL(T.ACT_LEN,0)
             ELSE
              0
           END) KC_LEN,
          DECODE(T.ZBS_FLAG,'1','产销出质保书 ','2','老系统出质保书') ZBS_FLAG,
          (SELECT B.BASENAME FROM COM_BASE_INFO B WHERE B.BASECODE=T.JUDGE_RESULT_CODE AND B.VALIDFLAG='1') JUDGE_RESULT_CODE
  FROM MAT_ZC_M T, YDM_ZC_BUTTRESS T1
 WHERE T.MAT_NO = T1.MAT_NO
     AND T.MNG_LOCK_FLAG='0'
     AND T.LOCK_FLAG='0'
     AND T.LOCK_LIKU='0'
     AND (T.JUDGE_RESULT_CODE IS NULL OR T.JUDGE_RESULT_CODE IN ('40740701','40740709'))
   AND T.ORD_LN_DLY_PK = ?
 GROUP BY T.JUDGE_STOVE_NO,
          T.BATCH_NO,
          T.BATCH_GROUD_NO,
          T1.STORAGE_NO,
          T1.LOCATION_NO,
          T.STEELCODE,
          T.STEELNAME,
          T.IN_MOLD,
          T.PRODUCCODE,
          T.PRODUCNAME,
          T.STD_CODE,
          T.STD_NAME,
          T.STD_STYLE,
          T.STD_STYLE_DESC,
          T.SPEC_CODE,
          T.SPEC_NAME,
          T.MODEL_CODE,
          T.MODEL_DESC,
          T.ORD_PK,
          T.ORD_LN_PK,
          T.ORD_LN_DLY_PK,
          T.ACT_LEN_MIN,
          T.ACT_LEN_MAX,
          T.memo, T.PLINE_CODE,T.PLINE_NAME,T.ZBS_FLAG,t.JUDGE_RESULT_CODE
 ORDER BY T.JUDGE_STOVE_NO, T.BATCH_NO
)T2  WHERE T2.ACT_COUNT != '0'

          ]]>
	</query>

	<query id="FrmPipeTrainBill.querySendPlan" desc="查询发运计划信息">
        <![CDATA[
      SELECT DISTINCT
       T.PLAN_YEAR_MONTH,
       T.SEND_PLAN_NO,
       T.SALE_ORG,
       T.SALE_ORG_DESC,
       T.CUSTOMER_NO,
       T.CUSTOMER_NM,
       T.RECEIV_NO,
       T.RECEIV_NM,
        (SELECT T3.BASENAME FROM COM_BASE_INFO T3 WHERE T3.BASECODE = T.TRANS_TYPE) TRANS_TYPE,
       T.ACT_TRANS_TYPE,
       T.STATION_NO,
       T.STATION_NM,
       T.SPCL_LN_NO,
       T.SPCL_LN_NM,
       T.PORT_NO,
       T.PORT_DESC,
       TO_CHAR(T.PLAN_START_TIME,'YYYY-MM-DD') PLAN_START_TIME,
       TO_CHAR(T.PLAN_END_TIME,'YYYY-MM-DD') PLAN_END_TIME,
       TO_CHAR(T.PLAN_REL_TIME,'YYYY-MM-DD HH24:MI:SS') PLAN_REL_TIME,
       T.PLAN_WT,
       T.PLAN_NUM,
       T.PLAN_TOTL_LEN,
       DECODE(T.SEND_PLAN_STATUS,'0','计划编制','1','计划下发','2','计划执行','3','计划关闭','异常') SEND_PLAN_STATUS,
       T.MEMO,
       T.CREATE_NAME,
       TO_CHAR(T.CREATE_TIME,'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
       T.DEPARTMENT_CODE,
       T.DEPARTMENT_DESC,
       T.MANAGEMENT_NO,
       T. MANAGEMENT_NAME,
       T.SUPPLY_UNIT,
       T.SUPPLY_UNIT_DESC
  FROM YDM_ZC_SENDPLAN_M T, YDM_ZC_SENDPLAN_C T1, YDM_ZC_BILL_M T2
 WHERE T.SEND_PLAN_NO = T1.SEND_PLAN_NO(+)
   AND T.SEND_PLAN_NO = T2.SEND_PLAN_NO(+)
   AND T.PLAN_YEAR_MONTH || '&' LIKE '%' || ? || '%'
   AND T1.ORDER_NO || '&' LIKE '%' || ? || '%'
   AND T.SEND_PLAN_NO || '&' LIKE '%' || ? || '%'
   AND T.STATION_NO || '&' LIKE '%' || ? || '%'
   AND T.SPCL_LN_NO || '&' LIKE '%' || ? || '%'
   AND T.RECEIV_NM || '&' LIKE '%' || ? || '%'
   AND T.CUSTOMER_NM || '&' LIKE '%' || ? || '%'
   AND T2.BILL_NO || '&' LIKE '%' || ? || '%'
   AND T.SEND_PLAN_STATUS|| '&' LIKE '%' || ? || '%'
   AND T.VALIDFLAG = '1'
   AND T.TRANS_TYPE='110502'
   AND T.SEND_PLAN_STATUS!='0'
          ]]>
	</query>

	<query id="FrmPipeTrainBill.queryOrderInfo" desc="查询发运合同信息">
		<![CDATA[
	 SELECT T.SEND_PLAN_NO,
       T.ORDER_NO || '/' || T3.ORDER_SEQ AS ORDER_SEQ_NO,
       T.ORDER_NO,
       T3.ORDER_SEQ,
       T2.DELIVERY_NO,
       T.PRODUCCODE,
       T.PRODUCNAME,
       T.SPEC_CODE,
       T.SPEC_NAME,
       T3.STD_CODE,
       T3.STD_NAME,
       T3.STEELCODE,
       T3.STEELNAME,
       T3.STD_STYLE,
       T3.STD_STYLE_DESC,
       T3.MODEL_CODE,
       T3.MODEL_DESC,
       T3.PRD_CLS,
       T.PSC,
       T.PSC_DESC,   
       T.LEN_NO,
       T.LEN_DESC,
       T.ORD_PK,
       T.ORD_LN_PK,
       T.ORD_LN_DLY_PK,
       T2.ORDER_UNIT,
       t3.OUTBOUND_CALCULATE,
       NVL(T2.WGT_ESTMT, 0) AS WGT_ESTMT,
       NVL(T.PLAN_WT, '0') AS PLAN_WT,
       NVL(T.PLAN_NUM, '0') AS PLAN_NUM,
       NVL(T.PLAN_TOTL_LEN, '0') AS PLAN_TOTL_LEN,
       NVL(P.PREP_COUNT, 0) PREP_COUNT,
       NVL(P.PREP_WT, 0) PREP_WT,
       NVL(P.PREP_LEN, 0) PREP_LEN
  FROM YDM_ZC_SENDPLAN_C T,
       (SELECT T1.ORD_LN_DLY_PK,
               T1.SEND_PLAN_NO,
               SUM(NVL(T1.PLAN_NUM, '0')) PREP_COUNT,
               SUM(NVL(T1.PLAN_WT, '0')) PREP_WT,
               SUM(NVL(T1.PLAN_TOTL_LEN, '0')) PREP_LEN
          FROM YDM_ZC_BILL_M T1
         GROUP BY T1.ORD_LN_DLY_PK, T1.SEND_PLAN_NO) P,
       SLM_ORDER_LINE_DELIVERY T2,SLM_ORDER_LINE T3
 WHERE T.ORD_LN_DLY_PK = P.ORD_LN_DLY_PK(+)
   AND T.SEND_PLAN_NO = P.SEND_PLAN_NO(+)
   AND T.ORD_LN_DLY_PK = T2.ORD_LN_DLY_PK
   AND T2.ORD_LN_PK=T3.ORD_LN_PK
   AND T2.ORD_PK=T3.ORD_PK
   AND T.SEND_PLAN_NO = ?
   AND T.VALIDFLAG = '1'
   AND T2.VALIDFLAG ='1'
 ORDER BY T.ORDER_NO, T.ORDER_SEQ, T.DELIVERY_NO
		]]>
	</query>

	<query id="FrmPipeTrainBill.queryBillM" 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,
       (CASE
		 WHEN MIN(T.SALE_ORG) = '100102' THEN
		  DECODE(MIN(T.REALINVOICENO1), '', '0', '1')
		 ELSE
		  DECODE(MIN(T.REALINVOICENO), '', '0', '1')
	   END) AS REALINVOICENO1,
       DECODE(LOADVEHICLE_TYPE, '1', '实际', '虚拟') LOADVEHICLE_TYPE,
       DECODE(T.LOADVEHICLE_TYPE,'2',DECODE(ISPRECLOSE,'0','未关闭','1','一次关闭','2','货款对齐','无'),'') ISPRECLOSE,
       (SELECT T1.BASENAME FROM COM_BASE_INFO T1 WHERE T1.BASECODE=T.PLAN_TRANS_TYPE) PLAN_TRANS_TYPE,
       T.SEND_PLAN_NO,
       SUM(T.PLAN_NUM) PLAN_NUM,
       SUM(T.PLAN_WT) PLAN_WT,
       SUM(T.PLAN_TOTL_LEN) PLAN_TOTL_LEN,
       T.CUSTOMER_NO,
       T.CUSTOMER_NM,
       T.RECEIV_NO,
       T.RECEIV_NM,
       T.STATION_NO,
       T.STATION_NM,
       T.STLMT_UNIT_NO,
       T.STLMT_UNIT_NM,
       T.SALE_ORG,
       T.SALE_ORG_DESC,
       (SELECT LISTAGG(B.CAR_NO, ';') WITHIN GROUP(ORDER BY B.BILL_NO)
				          FROM YDM_BILL_CARNO B
				         WHERE B.BILL_NO = T.BILL_NO)CAR_NO,
       MAX(T.PRINT_NUMBER) PRINT_NUMBER,
       MAX(TO_CHAR(T.PRINT_TIME, 'YYYY-MM-DD HH24:MI:SS')) PRINT_TIME,
       MIN(TO_CHAR(T.ACT_START_TIME, 'YYYY-MM-DD')) ACT_START_TIME,
       MIN(TO_CHAR(T.ACT_END_TIME, 'YYYY-MM-DD')) ACT_END_TIME,
       T.CREATE_NAME,
       T.DELVRY_ADDR,
       DECODE(T.ZOUT_FLAG,'1','是','0','否') AS ZOUT_FLAG,
       TO_CHAR(MIN(T.CREATE_TIME), 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
       BILL_SEND_NUM(T.BILL_NO) SEND_NUM,
       MAX(T.DELVRY_WHEREABOUTS) DELVRY_WHEREABOUTS
  FROM YDM_ZC_BILL_M T
 WHERE T.CREATE_TIME BETWEEN TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') AND
       TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')
   AND T.ORDER_NO || '&' LIKE ('%' || ? || '%')
   AND T.SEND_PLAN_NO || '&' LIKE ('%' || ? || '%')
   AND T.STATION_NO || '&' LIKE ('%' || ? || '%')
   AND T.SPCL_LN_NO || '&' LIKE ('%' || ? || '%')
   AND T.RECEIV_NM || '&' LIKE ('%' || ? || '%')
   AND T.CUSTOMER_NM || '&' LIKE ('%' || ? || '%')
   AND T.BILL_NO || '&' LIKE ('%' || ? || '%')
   AND T.BILL_STATUS || '&' LIKE ('%' || ? || '%')
   AND T.VALIDFLAG = '1'
   AND T.PLAN_TRANS_TYPE = '110502'
   AND T.RETURN_FLAG!='1'
          ]]>
	</query>

	<query id="FrmPipeTrainBill.queryBillC" desc="查询提单材料表信息">
 <![CDATA[
SELECT T.BILL_NO,
       T.ORDER_NO||'/'|| T.ORDER_SEQ AS ORDER_SEQ_NO,
       T.ORDER_NO,
       T.ORDER_SEQ,
       T.DELIVERY_NO,
       T.JUDGE_STOVE_NO,
       T.BATCH_NO,
       T.GROUP_NO,
       T.MATERIAL_NAME,
       T.PRODUCNAME,
       T.STD_NAME,
       T.GRADENAME,
       T.STEELNAME,
       T.STD_STYLE_DESC,
       T.SPEC_NAME,
       T.MODEL_DESC,
       T.PLINE_NAME,
       T.ACT_DIMATER,
       T.ACT_HEIGHT,
       T.ACT_LEN_MIN,
       T.ACT_LEN_MAX,
       T.STORAGE_NO,
       T.LOCATION_NO,
       (SELECT T1.LOCATION_NAME FROM YDM_BS_LOCATION T1 WHERE T1.LOCATION_NO = T.LOCATION_NO)LOCATION_NAME,
       T.CREATE_NAME,
       T.ORD_PK,
       T.ORD_LN_PK,
       T.ORD_LN_DLY_PK,
       T.VSTEL_DESC,
       T.VSTEL,
       DECODE(T.EDIT_FLAG,'1','已理货','未理货') EDIT_FLAG,
       DECODE(T.LOADVEHICLE_FLAG,'1','已装车','未装车') LOADVEHICLE_FLAG,
       SUM(CASE WHEN T.EDIT_FLAG='1' THEN T.ACT_LEN ELSE 0 END) PREP_LEN,
       SUM(CASE WHEN T.EDIT_FLAG='1' THEN T.ACT_COUNT ELSE 0 END) PREP_COUNT,
       SUM(CASE WHEN T.EDIT_FLAG='1' THEN T.ACT_WEIGHT ELSE 0 END) PREP_WEIGHT,
       SUM(CASE WHEN T.EDIT_FLAG='1' THEN T.ACT_THEORY_WEIGHT ELSE 0 END) PREP_THEORY_WEIGHT,
       SUM(CASE WHEN T.EDIT_FLAG='0' THEN T.ACT_LEN ELSE 0 END) BEEN_LEN,
       SUM(CASE WHEN T.EDIT_FLAG='0' THEN T.ACT_COUNT ELSE 0 END) BEEN_COUNT,
       SUM(CASE WHEN T.EDIT_FLAG='0' THEN T.ACT_WEIGHT ELSE 0 END) BEEN_WEIGHT,
       SUM(CASE WHEN T.EDIT_FLAG='0' THEN T.ACT_THEORY_WEIGHT ELSE 0 END) BEEN_THEORY_WEIGHT,
       NVL(SUM(T.ACT_COUNT), '0') AS ACT_COUNT,
       NVL(SUM(T.ACT_LEN), '0') AS ACT_LEN,
       NVL(SUM(T.ACT_WEIGHT), '0') AS ACT_WEIGHT,
       NVL(SUM(T.ACT_THEORY_WEIGHT), '0') AS ACT_THEORY_WEIGHT,
       NVL(SUM(T.SEND_NUM),0) AS SEND_NUM
  FROM YDM_ZC_BILL_C T
  LEFT JOIN YDM_BS_STORAGE B
    ON T.STORAGE_NO = B.STORAGE_NO
 WHERE T.BILL_NO = ?
 AND T.RETURN_FLAG='0'
 GROUP BY T.BILL_NO,
          T.ORDER_NO,
          T.ORDER_SEQ,
          T.DELIVERY_NO,
          T.JUDGE_STOVE_NO,
          T.BATCH_NO,
          T.GROUP_NO,
          T.MATERIAL_NAME,
          T.PRODUCNAME,
          T.STD_NAME,
          T.GRADENAME,
          T.STEELNAME,
          T.STD_STYLE_DESC,
          T.SPEC_NAME,
          T.MODEL_DESC,
          T.PLINE_NAME,
          T.ACT_DIMATER,
          T.ACT_HEIGHT,
          T.ACT_LEN_MIN,
          T.ACT_LEN_MAX,
          T.STORAGE_NO,
          T.LOCATION_NO,
          T.CREATE_NAME,
          T.EDIT_FLAG,
          T.LOADVEHICLE_FLAG,
          T.ORD_PK,
          T.ORD_LN_PK,
          T.ORD_LN_DLY_PK,
          T.VSTEL_DESC,T.VSTEL
 ORDER BY T.ORDER_NO,T.ORDER_SEQ,T.DELIVERY_NO,T.JUDGE_STOVE_NO, T.BATCH_NO 
 ]]>
	</query>
<query id="FrmPipeTrainBill.UpdateBillCStorage" desc="修改提单从表出库地点">
       <![CDATA[
      UPDATE YDM_ZC_BILL_C T SET T.STORAGE_NO=? , T.LOCATION_NO=?
       WHERE T.BILL_NO=? AND T.MAT_NO=?
        ]]>
	</query>
	<query id="FrmPipeTrainBill.Updatebuttress" desc="修改位置表出库地点">
       <![CDATA[
      UPDATE YDM_ZC_BUTTRESS T SET T.STORAGE_NO=? ,T.LOCATION_NO=? WHERE T.MAT_NO=?
        ]]>
	</query>
	<query id="FrmPipeTrainBill.UpdateBillCLoad" desc="修改提单从表装车地点">
       <![CDATA[
      UPDATE YDM_ZC_BILL_C T SET T.VSTEL=? , T.VSTEL_DESC=?
       WHERE T.BILL_NO=? AND T.MAT_NO=?
        ]]>
	</query>
</queryMap>