<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc="现货合同未提单处理">

	<query id="CoreOrderLineSpotLineTask.GetLineSql" desc="获取合同行的信息>9">
		<![CDATA[
	 	 SELECT t.order_no,T1.ORD_PK, T1.ORD_LN_PK, T1.CREATE_TIME

  FROM SLM_ORDER_HEAD T, SLM_ORDER_LINE T1
 WHERE T1.ORD_PK = T.ORD_PK
   AND T1.CREATE_TIME >=
       TO_DATE('2016-02-24 00:00:01', 'YYYY-MM-DD HH24:MI:SS')
   AND (TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM-DD'), 'YYYY-MM-DD') -
       TO_DATE(TO_CHAR(T1.CREATE_TIME, 'YYYY-MM-DD'), 'YYYY-MM-DD')) > 18
   AND T.ORDER_TYP = '120102'
   AND T1.BALNCE_PRICE_STS = '0'
   AND T1.VALIDFLAG = '1'
   AND T.VALIDFLAG = '1'
	 	 
		]]>
	</query>
	
	<query id="CoreOrderLineSpotLineTask.IsExistsBill" desc="合同是否存在提单">
		<![CDATA[
	 	 
	 	 SELECT 1 FROM YDM_ZC_BILL_M T WHERE T.ORD_PK = ?

		]]>
	</query>
	
	<query id="CoreOrderLineSpotLineTask.TakeOffLine" desc="合同是否存在提单">
		<![CDATA[
	 	 
	 	 UPDATE MAT_ZC_M T
	 	 
   SET T.ORDER_NO      = '',
       T.ORDER_SEQ     = '',
       T.ORDER_SEQ_NO  = '',
       T.ORD_PK        = '',
       T.ORD_LN_PK     = '',
       T.ORD_LN_DLY_PK = '',
       T.PRODUCT_FLAG  = '801402',
       T.MAT_STATUS    = '80150202'
    WHERE T.ORD_LN_PK = ?
    AND T.PRODUCT_FLAG = '801403'
    AND T.MAT_STATUS = '80150301'

		]]>
	</query>
	
	<query id="CoreOrderLineSpotLineTask.DeleteLineDlyLineTransit" desc="合同是否存在提单">
		<![CDATA[
	 	 
	 	 DECLARE
  BEGIN
   
   DELETE FROM  PROCESS_ORDER_M T
   WHERE T.ORD_LN_PK = ?;

   UPDATE SLM_ORDER_HEAD T
   SET T.ORDER_STS = '12020101',
       T.ORDER_STS_DESC = '录入',
       T.PRICE_NO = ''
   WHERE T.ORD_PK = ?;

	INSERT INTO CRM_SLM_ORDER
  ( ORD_PK,
  ORD_LN_PK,
  ORD_LN_DLY_PK,
  ORDER_NO,
  ORDER_SEQ,
  DELIVERY_NO,
  CUSTM_ORDER_NO,

  ORDER_TYP,
  ORDER_TYP_DESC,

  REGION_NO,
  REGION_NAME,

  SALE_ORG,
  SALE_ORG_DESC,

  SALE_AREA_NO,
  SALE_AREA_DESC,

  CUSTOMER_NO,
  CUSTOMER_NM,

  RECEIV_NO,
  RECEIV_NM,

  PRICE_FL,
  ORDER_QTY,

  BALNCE_PRICE,
  CURRENCY,
  MNY_ASK_TYP,
  CONTAIN_TAX,
  ORDER_UNIT,

  PRODUCCODE,
  PRODUCNAME,

  STD_CODE,
  STD_NAME,

  STEELCODE,
  STEELNAME,

  STD_STYLE,
  STD_STYLE_DESC,

  MODEL_CODE,
  MODEL_DESC,

  ORDER_SPEC_CODE,
  ORDER_SPEC_DESC,

  SPEC_CODE,
  SPEC_NAME,

  DIMATER,
  HEIGHT,

  LEN_NO,
  LEN_DESC,
  LEN_UNIT,

  OUTS_BILLET_FL,
  OUTS_STL_PIPE_FL,
  MNY_ASK_DESC,
  ISLOCK,
  PRDCR_DESC,
  MPS_DESC,
  STLMT_UNIT_NO,
  STLMT_UNIT_NM,
  DELVRY_RANGE_TPE,
  DELVRY_RANGE_MIN,
  DELVRY_RANGE_MAX,
  ADD_ASK_NO,
  ORDER_ADD_DESC,
  ORDER_SPECIAL_DESC,
  ORDER_STS,
  ORDER_STS_DESC,
  ORDER_LN_STS,
  ORDER_LN_STS_DESC,
  SUPPLY_UNIT_CODE,
  SUPPLY_UNIT,
  WGT_ESTMT,
  DELVRY_EDATE,
  EFFECT_TIME,
  TIBAO_TIME,
  DATA_STATUS,
  CREATE_TIME,
  CREATE_NAME,
  FST_DRAWEE_UNIT,
  BALNCE_PRICE_STS,
  DATA_FLAG,
  KEEP_TYPE,
  ORDER_HEAD_STS,
  ORDER_HEAD_STS_DESC)
  SELECT   H.ORD_PK,
    L.ORD_LN_PK,
    D.ORD_LN_DLY_PK,
    H.ORDER_NO,
    L.ORDER_SEQ,
    D.DELIVERY_NO,
    H.CUSTM_ORDER_NO,
    H.ORDER_TYP,
    H.ORDER_TYP_DESC,
    C.REGION_NO,
    (SELECT T.REGION_NM FROM SEL_ADMIN_REGION T WHERE T.REGION_NO=C.REGION_NO) REGION_NAME,
    H.SALE_ORG,
    H.SALE_ORG_DESC,
    H.SALE_AREA_NO,
    H.SALE_AREA_DESC,
    H.CUSTOMER_NO,
    H.CUSTOMER_NM,


    D.RECEIV_NO,
    D.RECEIV_NM,

    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=H.PRICE_FL) PRICE_FL,

    D.DELVRY_QTY ORDER_QTY,
    L.BALNCE_PRICE,
    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=H.CURRENCY) CURRENCY,
    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=H.MNY_ASK_TYP) MNY_ASK_TYP,

    H.CONTAIN_TAX,
    D.ORDER_UNIT,

    L.PRODUCCODE,
    L.PRODUCNAME,

    L.STD_CODE,
    L.STD_NAME,

    L.STEELCODE,
    L.STEELNAME,

    L.STD_STYLE,
    L.STD_STYLE_DESC,

    L.MODEL_CODE,
    L.MODEL_DESC,

    L.ORDER_SPEC_CODE,
    L.ORDER_SPEC_DESC,

    L.SPEC_CODE,
    L.SPEC_NAME,

    L.DIMATER,
    L.HEIGHT,

    L.LEN_NO,
    L.LEN_DESC,
    L.LEN_UNIT,

    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=L.OUTS_BILLET_FL) OUTS_BILLET_FL,
    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=L.OUTS_STL_PIPE_FL) OUTS_STL_PIPE_FL,
    H.MNY_ASK_DESC,
    L.ISLOCK,
    L.PRDCR_DESC,
    L.MPS_DESC,
    H.STLMT_UNIT_NO,
    H.STLMT_UNIT_NM,
    L.DELVRY_RANGE_TPE,
    L.DELVRY_RANGE_MIN,
    L.DELVRY_RANGE_MAX,
    L.ADD_ASK_NO,
    L.ORDER_ADD_DESC,
    L.ORDER_SPECIAL_DESC,
    CASE
      WHEN P.PORDERSTATUS = '0' THEN
        '12020402'
      WHEN P.PORDERSTATUS > '0' THEN
        '12020502'
      ELSE
        D.DELIVERY_STATUS
    END ORDER_STS,
    CASE
      WHEN P.PORDERSTATUS = '0' THEN
        '待接收'
      WHEN P.PORDERSTATUS > '0' THEN
        '已接收'
      ELSE
        (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=D.DELIVERY_STATUS)
    END ORDER_STS_DESC,
    L.ORDER_LN_STATUS ORDER_LN_STS,
    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=L.ORDER_LN_STATUS) ORDER_LN_STS_DESC,
    H.SUPPLY_UNIT SUPPLY_UNIT_CODE,
    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=H.SUPPLY_UNIT) SUPPLY_UNIT,
    D.WGT_ESTMT,
    D.DELVRY_EDATE DELVRY_EDATE,
    H.EFFECT_TIME EFFECT_TIME,
    (SELECT T.CREATE_TIME FROM PLN_SALEORD T WHERE T.ORD_LN_DLY_PK=D.ORD_LN_DLY_PK) TIBAO_TIME,
    '2', -- 数据类型(0 新增 1修改 2删除)
    SYSDATE,
    H.CREATE_NAME,
    (SELECT T.BASENAME FROM COM_BASE_INFO T WHERE T.BASECODE=H.FST_DRAWEE_UNIT) FST_DRAWEE_UNIT,
    L.BALNCE_PRICE_STS,
    '1', -- 数据状态(0 合同头 1合同行 2交货行)
    DECODE(l.KEEP_TYPE,'0','保单价','1','保总价') KEEP_TYPE,
	H.ORDER_STS ORDER_HEAD_STS,
	H.ORDER_STS_DESC ORDER_HEAD_STS_DESC
    FROM  SLM_ORDER_HEAD H
    inner join SLM_ORDER_LINE L on H.ORD_PK = L.ORD_PK
    LEFT JOIN SLM_BASE_CUSTOMER C ON H.CUSTOMER_NO = C.CUSTOMER_NO
    LEFT JOIN SLM_ORDER_LINE_DELIVERY D ON L.ORD_LN_PK = D.ORD_LN_PK
    LEFT JOIN PLN_SALEORD P ON D.ORD_LN_DLY_PK = P.ORD_LN_DLY_PK
    WHERE 1 = 1
     AND L.ORD_LN_PK=?
	 AND H.VALIDFLAG='1'
	 AND ROWNUM = 1
     order BY
     ORDER_NO,ORDER_SEQ;

   UPDATE SLM_ORDER_LINE T
     SET T.VALIDFLAG = '0',
         T.MEMO      = TO_CHAR(SYSDATE, 'YYYY-MM-DD') || ' 现货合同到期合同行作废'
   WHERE T.ORD_LN_PK = ?;

   UPDATE SLM_ORDER_LINE_DELIVERY T
     SET T.VALIDFLAG  = '0',
         T.OBJ_STS_OP = TO_CHAR(SYSDATE, 'YYYY-MM-DD') || ' 现货合同到期交货行作废'
   WHERE T.ORD_LN_PK = ?;

   UPDATE SLM_ORDER_DELIVERY_TRANSIT T
     SET T.VALIDFLAG = '0'
   WHERE T.ORD_LN_PK = ?;

   DELETE FROM SLM_LAW_REVIEW_RECORD T
   WHERE T.ORD_PK = ?;

  END;
	 	 		
		]]>
	</query>
	
	 
	 
</queryMap>
