<?xml version="1.0" encoding="UTF-8"?>

<queryMap desc= "合同结转情况查询">

    <query id = "CoreOrderCarryOver.Query" desc = "查询">

     <![CDATA[
     
  SELECT R.*, (CASE WHEN (
                        (CASE  ORDER_UNIT 
                         WHEN '支' THEN  (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_COUNT THEN '0'
                                            ELSE '1'
                                            END)
                         WHEN '吨' THEN (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_WEIGHT THEN '0'
                                         ELSE '1'
                                         END )
                         ELSE (CASE WHEN R.ORDER_QTY*(1-DELVRY_RANGE_MIN/100) <= R.SEND_LEN THEN '0'
                                ELSE '1'
                                END)
                         END ))='0'
                       AND NVL(R.BILL_COUNT,'0')='0' 
                       AND NVL(MAT_COUNT,'0')='0'
                       AND NVL(OUT_COUNT,'0')= '0'
                       AND NVL(SEND_COUNT,'0') <>'0'
              THEN 'OK'
              ELSE
              'NO'
              END) IS_CARRY
  FROM (SELECT B.ORDER_NO || '/' || A.ORDER_SEQ ORD_NO_SEQ,
               A.DELVRY_RANGE_MIN,
               --A.PRDCT_WGT,
               A.PRODUCNAME,
               A.STD_NAME,
               A.STEELNAME,
               A.STD_STYLE_DESC,
               A.SPEC_NAME,
               A.MODEL_DESC,
               A.ORDER_UNIT,
               B.CUSTOMER_NM,
               A.FINAL_USER_DESC,
               A.ORDER_QTY,
               NVL(F.PAY_COUNT,'0') PAY_COUNT,    --缴库量
               NVL(F.PAY_WEIGHT,'0') PAY_WEIGHT,
               NVL(Q1.PLAN_WEIGHT,'0') PLAN_WEIGHT,   --排产量
               NVL(Q1.PLAN_COUNT,'0') PLAN_COUNT,
               NVL(Q.SEND_WEIGHT,'0')  SEND_WEIGHT,  --发运量
               NVL(Q.SEND_COUNT,'0') SEND_COUNT,
               NVL(Q.SEND_LEN,'0') SEND_LEN,
               NVL(Q2.BILL_COUNT,'0') BILL_COUNT,   --在库提单量
               NVL(Q2.BILL_WEIGHT,'0') BILL_WEIGHT,
               NVL(Q3.MAT_WEIGHT,'0')  MAT_WEIGHT,  --在库合同量
               NVL(Q3.MAT_COUNT,'0') MAT_COUNT,
               NVL(Q4.ACT_COUNT,'0') OUT_COUNT,  --出库未结量
               NVL(Q4.ACT_WGT,'0') OUT_WEIGHT,
               A.LEN_DESC,
               (SELECT BASENAME FROM COM_BASE_INFO WHERE BASECODE = A.ORDER_LN_STATUS) ORD_LN_STATUS,
               (SELECT BASENAME FROM COM_BASE_INFO WHERE BASECODE = B.ORDER_STS) ORDER_STS
        
          FROM SLM_ORDER_LINE A
        
          LEFT JOIN SLM_REPORT_TEMP_PAY F
            ON A.ORD_LN_PK = F.ORD_LN_PK
        
          LEFT JOIN SLM_REPORT_TEMP_PLAN Q1
            ON A.ORD_LN_PK = Q1.ORD_LN_PK
        
          LEFT JOIN SLM_REPORT_TEMP_SEND Q
            ON Q.ORD_LN_PK = A.ORD_LN_PK
        
          LEFT JOIN SLM_REPORT_TEMP_BILL Q2
            ON Q2.ORD_LN_PK = A.ORD_LN_PK
        
          LEFT JOIN SLM_REPORT_TEMP_MAT Q3
            ON Q3.ORD_LN_PK = A.ORD_LN_PK
        
          LEFT JOIN SLM_REPORT_TEMP_OUT Q4
            ON Q4.ORD_LN_PK = A.ORD_LN_PK
           AND Q4.INVFLAG = '0', SLM_ORDER_HEAD B
        
         WHERE A.ORD_PK = B.ORD_PK
           AND B.ORDER_STS >= '12020301'
           AND A.VALIDFLAG = '1'
           AND B.VALIDFLAG = '1'
           AND A.CREATE_NAME IS NOT NULL
           AND B.CREATE_NAME IS NOT NULL
           AND B.ORDER_NO || '&' LIKE '%' || ? || '%' #TIMECON#) R
      WHERE 1 = 1

    
     ]]>

    </query>
    
    <query id = "CoreOrderCarryOver.MoveData" desc = "转移临时数据"> 
    
     <![CDATA[
     
   DECLARE
  BEGIN

  DELETE FROM SLM_REPORT_TEMP_BILL;
  DELETE FROM SLM_REPORT_TEMP_MAT;
  DELETE FROM SLM_REPORT_TEMP_OUT;
  DELETE FROM SLM_REPORT_TEMP_PAY;
  DELETE FROM SLM_REPORT_TEMP_PLAN;
  DELETE FROM SLM_REPORT_TEMP_SEND;
  
  END;

     ]]>
    
    
    </query>
    
     <query id = "CoreOrderCarryOver.insertPlan" desc = "插入计划临时表"> 
    
     <![CDATA[
     
 INSERT INTO SLM_REPORT_TEMP_PLAN
    (PLAN_WEIGHT, PLAN_COUNT, ORD_LN_PK)
  
    SELECT NVL(SUM(T.PLAN_P_MAIN),'0') PLAN_WEIGHT,
           NVL(SUM(T.PLAN_NUM_D),'0') PLAN_COUNT,
           SUBSTR(ORD_LN_DLY_PK, 1, 35) ORD_LN_PK
      FROM PLN_SALEORD_PROC T, SLM_ORDER_LINE A, SLM_ORDER_HEAD B
     WHERE T.ORDER_SOURCE != '备料需求'
       AND SUBSTR(T.ORD_LN_DLY_PK, 1, 35) = A.ORD_LN_PK
       AND A.ORD_PK = B.ORD_PK
       AND B.ORDER_STS >= '12020301'
       AND B.Create_Name is not NULL
       AND B.VALIDFLAG = '1'
       AND B.ORDER_NO || '&' LIKE '%' || '#ORDER_NO#' || '%'
       AND A.CREATE_NAME IS NOT NULL
       AND A.VALIDFLAG = '1' 
       #TIMECON#
     GROUP BY SUBSTR(T.ORD_LN_DLY_PK, 1, 35)

      ]]>
    
    </query>
    
     <query id = "CoreOrderCarryOver.insertSend" desc = "插入计划临时表"> 
    
     <![CDATA[
     
     
  INSERT INTO SLM_REPORT_TEMP_SEND
    (SEND_WEIGHT, SEND_COUNT,SEND_LEN, ORD_LN_PK)
  
    SELECT SUM(CASE
                 WHEN M.PONDER_NO IS NOT NULL THEN
                  M.PONDER_GROSS_WT
                 ELSE
                  M.ACT_WEIGHT
               END) SEND_WEIGHT,
           SUM(M.ACT_COUNT) SEND_COUNT,
           SUM(M.ACT_LEN) SEND_LEN,
           M.ORD_LN_PK
      FROM YDM_ZC_LOADVEHICLE_MAT M, SLM_ORDER_LINE A, SLM_ORDER_HEAD B
     WHERE M.RETURN_FLAG = '0'
       AND M.VALIDFLAG > '1'
       AND A.CREATE_NAME IS NOT NULL
       AND A.ORD_LN_PK = M.ORD_LN_PK
       AND A.ORD_PK = B.ORD_PK
       AND B.ORDER_STS >= '12020301'
       AND B.Create_Name is not NULL
       AND B.VALIDFLAG = '1'
       AND B.ORDER_NO || '&' LIKE '%' || '#ORDER_NO#' || '%'
       AND A.VALIDFLAG = '1' 
       #TIMECON#
     GROUP BY M.ORD_LN_PK

     ]]>
    
    </query>
    
      <query id = "CoreOrderCarryOver.insertBill" desc = "插入计划临时表"> 
    
     <![CDATA[
     
      INSERT INTO SLM_REPORT_TEMP_BILL
    (ORD_LN_PK, BILL_COUNT, BILL_WEIGHT)
    SELECT C1.ORD_LN_PK,
           SUM(C1.ACT_COUNT) BILL_COUNT,
           SUM(C1.ACT_WEIGHT) BILL_WEIGHT
      FROM YDM_ZC_BILL_C C1, SLM_ORDER_LINE A, SLM_ORDER_HEAD B
     WHERE C1.LOADVEHICLE_FLAG = '0'
       AND A.CREATE_NAME IS NOT NULL
       AND A.ORD_LN_PK = C1.ORD_LN_PK
       AND A.ORD_PK = B.ORD_PK
       AND B.ORDER_STS >= '12020301'
       AND B.Create_Name is not NULL
       AND B.VALIDFLAG = '1'
       AND B.ORDER_NO || '&' LIKE '%' || '#ORDER_NO#' || '%'
       AND A.VALIDFLAG = '1' 
       #TIMECON#
     GROUP BY C1.ORD_LN_PK

     ]]>
    
    </query>
    
      <query id = "CoreOrderCarryOver.insertPay" desc = "插入计划临时表"> 
    
     <![CDATA[
     
      INSERT INTO SLM_REPORT_TEMP_PAY
    (PAY_COUNT, PAY_WEIGHT, ORD_LN_PK)
    SELECT SUM(NVL(E.PAYDOWN_NUM, 0)) AS PAY_COUNT,
           SUM(NVL(E.PAYDOWNWEIGHT, 0)) PAY_WEIGHT,
           E.ORD_LN_PK
      FROM PLN_SALEORD E, SLM_ORDER_LINE A, SLM_ORDER_HEAD B
     WHERE A.CREATE_NAME IS NOT NULL
       AND A.ORD_LN_PK = E.ORD_LN_PK
       AND A.VALIDFLAG = '1'
       AND A.ORD_PK = B.ORD_PK
       AND B.ORDER_STS >= '12020301'
       AND B.Create_Name is not NULL
       AND B.VALIDFLAG = '1'
       AND B.ORDER_NO || '&' LIKE '%' || '#ORDER_NO#' || '%'
     #TIMECON#
     GROUP BY E.ORD_LN_PK

     ]]>
    
    </query>
    
      <query id = "CoreOrderCarryOver.insertMat" desc = "插入计划临时表"> 
    
     <![CDATA[
     
  INSERT INTO SLM_REPORT_TEMP_MAT
    (MAT_WEIGHT, MAT_COUNT, ORD_LN_PK)
    SELECT SUM(M2.ACT_WEIGHT) MAT_WEIGHT, COUNT(1) MAT_COUNT, M2.ORD_LN_PK
      FROM MAT_ZC_M        M2,
           YDM_ZC_BUTTRESS T2

     WHERE M2.MAT_STATUS IN ('80150301', '80150302')
       AND M2.MAT_NO = T2.MAT_NO
     GROUP BY M2.ORD_LN_PK
     

     ]]>
    
    </query>
    
      <query id = "CoreOrderCarryOver.insertOut" desc = "插入计划临时表"> 
    
     <![CDATA[
     
      INSERT INTO SLM_REPORT_TEMP_OUT
    (INVFLAG, ACT_COUNT, ACT_WGT, ORD_LN_PK)
    SELECT MAX(CASE
                 WHEN S1.BALANCESTATUS_ORG = '3' AND
                      S1.REALINVOICENO_ORG IS NOT NULL THEN
                  '1'
                 ELSE
                  '0'
               END) INVFLAG,
           SUM(S2.PRD_QTY) AS ACT_COUNT,
           SUM(S2.ACT_WGT) AS ACT_WGT,
           S1.ORD_LN_PK
    
      FROM SLM_BALANCEINFO S1
      JOIN SLM_BALANCEDETAIL_ALL S2
        ON S1.ORD_NO = S2.ORD_NO
       AND S1.ORD_SEQ = S2.ORD_SEQ
       AND S1.ORD_LN_DLY = S2.ORD_LN_DLY
       AND S1.ASKPLAN_ID = S2.ASKPLAN_ID
       AND S1.ASKPLAN_ID_SEQ = S2.ASKPLAN_ID_SEQ
       AND S1.STEELCODEKEY = S2.STEELCODEKEY
       AND S1.SPETKEY = S2.SPETKEY, SLM_ORDER_LINE A
     WHERE S1.FORECORPCODE = '国际贸易部'
       AND S1.ORD_LN_PK = A.ORD_LN_PK
       AND A.CREATE_NAME IS NOT NULL
       AND A.VALIDFLAG = '1' 
       AND S1.ORD_NO || '&' LIKE '%' || '#ORDER_NO#' || '%'
       #TIMECON#
     GROUP BY S1.ORD_LN_PK
    UNION ALL
    SELECT MAX(CASE
                 WHEN S3.BALANCESTATUS = '3' AND S3.REALINVOICENO IS NOT NULL THEN
                  '1'
                 ELSE
                  '0'
               END) INVFLAG,
           SUM(S4.PRD_QTY) AS ACT_COUNT,
           SUM(S4.ACT_WGT) AS ACT_WGT,
           S3.ORD_LN_PK
      FROM SLM_BALANCEINFO S3
      JOIN SLM_BALANCEDETAIL_ALL S4
        ON S3.ORD_NO = S4.ORD_NO
       AND S3.ORD_SEQ = S4.ORD_SEQ
       AND S3.ORD_LN_DLY = S4.ORD_LN_DLY
       AND S3.ASKPLAN_ID = S4.ASKPLAN_ID
       AND S3.ASKPLAN_ID_SEQ = S4.ASKPLAN_ID_SEQ
       AND S3.STEELCODEKEY = S4.STEELCODEKEY
       AND S3.SPETKEY = S4.SPETKEY, SLM_ORDER_LINE A
     WHERE S3.FORECORPCODE <> '国际贸易部'
       AND S3.ORD_LN_PK = A.ORD_LN_PK
       AND A.CREATE_NAME IS NOT NULL
       AND A.VALIDFLAG = '1' 
       AND S3.ORD_NO || '&' LIKE '%' || '#ORDER_NO#' || '%'
       #TIMECON#
     GROUP BY S3.ORD_LN_PK
     

     ]]>
    
    </query>
    


</queryMap>