用友U8库存展望包含锁定状态工单存储过程

USE [UFDATA_001_2018]
GO
/****** Object:  StoredProcedure [dbo].[P_Quick_StockExpectationNull]    Script Date: 2023-07-14 9:10:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[P_Quick_StockExpectationNull]
    @dtTime DATE,
    @state INT
AS
--Select   Warehouse.cWhCode,cWhName ,isnull(factory.cfactoryname,N'') as cfactoryname    FROM  Warehouse left join factory on Warehouse.cfactorycode = factory.cfactorycode    where isnull(binavailcalcu,0) = 1  order by factory.cfactoryname,Warehouse.cwhcode 
CREATE TABLE #TempKCView_1
(
    cInvCode NVARCHAR(60),
    YQGL FLOAT,
    DDZT FLOAT,
    DHDJ FLOAT,
    SCDD FLOAT,
    SCWL FLOAT
        DEFAULT 0,
    WWDD FLOAT
        DEFAULT 0,
    WWWL FLOAT
        DEFAULT 0,
    DBZT FLOAT,
    YDGL FLOAT,
    DFHL FLOAT,
    DBDF FLOAT,
    BLJH FLOAT,
    dDate DATETIME,
    cFree1 NVARCHAR(20),
    cFree2 NVARCHAR(20),
    cFree3 NVARCHAR(20),
    cFree4 NVARCHAR(20),
    cFree5 NVARCHAR(20),
    cFree6 NVARCHAR(20),
    cFree7 NVARCHAR(20),
    cFree8 NVARCHAR(20),
    cFree9 NVARCHAR(20),
    cFree10 NVARCHAR(20),
    isotype SMALLINT
        DEFAULT 0,
    isodid NVARCHAR(40)
        DEFAULT N''
)
CREATE INDEX kcview_1_inx_inv ON #TempKCView_1 (cInvCode)
INSERT INTO #TempKCView_1
SELECT TransVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       (CASE
            WHEN ISNULL(rdrecord.cHandler, N'') = N'' THEN
                rdrecords.iQuantity
            ELSE
                0
        END
       ) AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       rdrecord.dDate,
       TransVouchs.cFree1,
       TransVouchs.cFree2,
       TransVouchs.cFree3,
       TransVouchs.cFree4,
       TransVouchs.cFree5,
       TransVouchs.cFree6,
       TransVouchs.cFree7,
       TransVouchs.cFree8,
       TransVouchs.cFree9,
       TransVouchs.cFree10,
       ISNULL(TransVouchs.iSSoType, 0),
       ISNULL(TransVouchs.iSSodid, N'')
FROM rdrecords08 rdrecords
    INNER JOIN RdRecord08 rdrecord
        ON rdrecords.ID = rdrecord.ID
    INNER JOIN TransVouch
        INNER JOIN TransVouchs
            ON TransVouch.ID = TransVouchs.ID
        ON rdrecords.iTrIds = TransVouchs.autoID
    INNER JOIN Inventory
        ON TransVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON rdrecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND ISNULL(TransVouch.cVerifyPerson, N'') <> N''
      AND rdrecord.bRdFlag <> 0
      AND rdrecord.cBusType = N'调拨入库'
      AND rdrecord.dDate <= @dtTime

INSERT INTO #TempKCView_1
SELECT TransVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       (CASE
            WHEN ISNULL(rdrecord.cHandler, N'') = N'' THEN
                TransVouchs.iTVQuantity
            ELSE
                0
        END
       ) AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       rdrecord.dDate,
       TransVouchs.cFree1,
       TransVouchs.cFree2,
       TransVouchs.cFree3,
       TransVouchs.cFree4,
       TransVouchs.cFree5,
       TransVouchs.cFree6,
       TransVouchs.cFree7,
       TransVouchs.cFree8,
       TransVouchs.cFree9,
       TransVouchs.cFree10,
       ISNULL(TransVouchs.iSSoType, 0),
       ISNULL(TransVouchs.iSSodid, N'')
FROM rdrecords09 rdrecords
    INNER JOIN RdRecord09 rdrecord
        ON rdrecords.ID = rdrecord.ID
    INNER JOIN TransVouch
        INNER JOIN TransVouchs
            ON TransVouch.ID = TransVouchs.ID
        ON rdrecords.iTrIds = TransVouchs.autoID
    INNER JOIN Inventory
        ON TransVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON rdrecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND ISNULL(TransVouch.cVerifyPerson, N'') <> N''
      AND rdrecord.bRdFlag = 0
      AND rdrecord.cBusType = N'调拨出库'
      AND rdrecord.dDate <= @dtTime
      AND rdrecords.iQuantity < 0

INSERT INTO #TempKCView_1
SELECT TransVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       TransVouchs.iTVQuantity AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       TransVouch.dTVDate,
       TransVouchs.cFree1,
       TransVouchs.cFree2,
       TransVouchs.cFree3,
       TransVouchs.cFree4,
       TransVouchs.cFree5,
       TransVouchs.cFree6,
       TransVouchs.cFree7,
       TransVouchs.cFree8,
       TransVouchs.cFree9,
       TransVouchs.cFree10,
       ISNULL(TransVouchs.iSSoType, 0),
       ISNULL(TransVouchs.iSSodid, N'')
FROM TransVouch
    INNER JOIN TransVouchs
        ON TransVouch.ID = TransVouchs.ID
    INNER JOIN Inventory
        ON TransVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON TransVouch.cIWhCode = Warehouse.cWhCode
WHERE Warehouse.bInAvailCalcu = 1
      AND ISNULL(TransVouch.cVerifyPerson, N'') = N''
      AND TransVouch.dTVDate <= @dtTime

INSERT INTO #TempKCView_1
SELECT PU_AppVouchs.cInvCode,
       (CASE
            WHEN (PU_AppVouchs.fQuantity - ISNULL(PU_AppVouchs.iReceivedQTY, 0) + ISNULL(PO_Podetails.iQuantity, 0)) < 0 THEN
                0
            ELSE
       (PU_AppVouchs.fQuantity - ISNULL(PU_AppVouchs.iReceivedQTY, 0) + ISNULL(PO_Podetails.iQuantity, 0))
        END
       ) AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       (CASE
            WHEN ISNULL(PU_AppVouchs.dArriveDate, N'') = N'' THEN
                PU_AppVouch.dDate
            ELSE
                PU_AppVouchs.dArriveDate
        END
       ) AS dDate,
       PU_AppVouchs.cFree1,
       PU_AppVouchs.cFree2,
       PU_AppVouchs.cFree3,
       PU_AppVouchs.cFree4,
       PU_AppVouchs.cFree5,
       PU_AppVouchs.cFree6,
       PU_AppVouchs.cFree7,
       PU_AppVouchs.cFree8,
       PU_AppVouchs.cFree9,
       PU_AppVouchs.cFree10,
       ISNULL(PU_AppVouchs.SoType, 0),
       ISNULL(PU_AppVouchs.SoDId, N'')
FROM PU_AppVouch
    INNER JOIN PU_AppVouchs
        ON PU_AppVouch.ID = PU_AppVouchs.ID
    LEFT JOIN
    (
        SELECT i.cInvCode,
               iAppIds,
               SUM(iQuantity) AS iQuantity
        FROM PO_Podetails
            INNER JOIN PO_Pomain
                ON PO_Podetails.POID = PO_Pomain.POID
            INNER JOIN Inventory i
                ON i.cInvCode = PO_Podetails.cInvCode
        WHERE ISNULL(PO_Pomain.cVerifier, N'') = N''
        GROUP BY i.cInvCode,
                 iAppIds
    ) PO_Podetails
        ON PU_AppVouchs.AutoID = PO_Podetails.iAppIds
    INNER JOIN Inventory
        ON PU_AppVouchs.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON Warehouse.cWhCode = ISNULL(Inventory.cDefWareHouse, N'')
WHERE ISNULL(PU_AppVouch.cVerifier, N'') <> N''
      AND ISNULL(PU_AppVouchs.cbcloser, N'') = N''
      AND PU_AppVouch.dDate <= @dtTime
      AND
      (
          ISNULL(Warehouse.bInAvailCalcu, 0) = 1
          OR ISNULL(Inventory.cDefWareHouse, N'') = N''
      )
INSERT INTO #TempKCView_1
SELECT PO_Podetails.cInvCode,
       0 AS YQGL,
       (CASE
            WHEN (PO_Podetails.iQuantity - ISNULL(iArrQTY, 0) - ISNULL(iReceivedQTY, 0)) < 0 THEN
                0
            ELSE
       (PO_Podetails.iQuantity - ISNULL(iArrQTY, 0) - ISNULL(iReceivedQTY, 0))
        END
       ) AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       (CASE
            WHEN ISNULL(PO_Podetails.dArriveDate, N'') = N'' THEN
                PO_Pomain.dPODate
            ELSE
                PO_Podetails.dArriveDate
        END
       ) AS dDate,
       PO_Podetails.cFree1,
       PO_Podetails.cFree2,
       PO_Podetails.cFree3,
       PO_Podetails.cFree4,
       PO_Podetails.cFree5,
       PO_Podetails.cFree6,
       PO_Podetails.cFree7,
       PO_Podetails.cFree8,
       PO_Podetails.cFree9,
       PO_Podetails.cFree10,
       ISNULL(PO_Podetails.SoType, 0),
       ISNULL(PO_Podetails.SoDId, N'')
FROM PO_Pomain
    INNER JOIN PO_Podetails
        ON PO_Pomain.POID = PO_Podetails.POID
    INNER JOIN Inventory
        ON PO_Podetails.cInvCode = Inventory.cInvCode
WHERE (ISNULL(cVerifier, N'') <> N'')
      AND ISNULL(cbCloser, N'') = N''
      AND ISNULL(PO_Pomain.cBusType, N'') <> N'直运采购'
      AND (CASE
               WHEN ISNULL(PO_Podetails.dArriveDate, N'') = N'' THEN
                   PO_Pomain.dPODate
               ELSE
                   PO_Podetails.dArriveDate
           END
          ) <= @dtTime


INSERT INTO #TempKCView_1
SELECT v_im_orderforkcview.cinvcode,
       0 AS YQGL,
       (v_im_orderforkcview.fquantity) AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       v_im_orderforkcview.ddate AS dDate,
       v_im_orderforkcview.cfree1,
       v_im_orderforkcview.cfree2,
       v_im_orderforkcview.cfree3,
       v_im_orderforkcview.cfree4,
       v_im_orderforkcview.cfree5,
       v_im_orderforkcview.cfree6,
       v_im_orderforkcview.cfree7,
       v_im_orderforkcview.cfree8,
       v_im_orderforkcview.cfree9,
       v_im_orderforkcview.cfree10,
       ISNULL(v_im_orderforkcview.sotypeid, 0),
       ISNULL(v_im_orderforkcview.sodid, N'')
FROM v_im_orderforkcview
    INNER JOIN Inventory
        ON v_im_orderforkcview.cinvcode = Inventory.cInvCode
WHERE v_im_orderforkcview.ddate <= @dtTime
      AND v_im_orderforkcview.iimbustype <> N'30'


INSERT INTO #TempKCView_1
SELECT v_EX_WaitInForStock.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (ABS(fInQuantity)) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       CONVERT(NVARCHAR(10), v_EX_WaitInForStock.ddate, 121) AS dDate,
       v_EX_WaitInForStock.cFree1,
       v_EX_WaitInForStock.cFree2,
       v_EX_WaitInForStock.cFree3,
       v_EX_WaitInForStock.cFree4,
       v_EX_WaitInForStock.cFree5,
       v_EX_WaitInForStock.cFree6,
       v_EX_WaitInForStock.cFree7,
       v_EX_WaitInForStock.cFree8,
       v_EX_WaitInForStock.cFree9,
       v_EX_WaitInForStock.cFree10,
       ISNULL(v_EX_WaitInForStock.sotype, 0),
       ISNULL(v_EX_WaitInForStock.sodid, N'')
FROM v_EX_WaitInForStock
    INNER JOIN Inventory
        ON v_EX_WaitInForStock.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse W
        ON v_EX_WaitInForStock.cWhCode = W.cWhCode
WHERE (
          bInAvailCalcu = 1
          OR ISNULL(v_EX_WaitInForStock.cWhCode, N'') = N''
      )
      AND CONVERT(NVARCHAR(10), v_EX_WaitInForStock.ddate, 121) <= @dtTime

INSERT INTO #TempKCView_1
SELECT PU_ArrivalVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (CASE
            WHEN iBillType = 0 THEN
       (CASE
            WHEN PU_ArrivalVouchs.iQuantity - ISNULL(PU_ArrivalVouchs.fValidInQuan, 0)
                 - ISNULL(PU_ArrivalVouchs.fInValidInQuan, 0) - ISNULL(PU_ArrivalVouchs.fDTQuantity, 0)
                 - ISNULL(PU_ArrivalVouchs.fRefuseQuantity, 0) >= 0 THEN
                PU_ArrivalVouchs.iQuantity - ISNULL(PU_ArrivalVouchs.fValidInQuan, 0)
                - ISNULL(PU_ArrivalVouchs.fInValidInQuan, 0) - ISNULL(PU_ArrivalVouchs.fDTQuantity, 0)
                - ISNULL(PU_ArrivalVouchs.fRefuseQuantity, 0)
            ELSE
                0
        END
       )
            ELSE
       (CASE
            WHEN PU_ArrivalVouchs.iQuantity - ISNULL(PU_ArrivalVouchs.fValidInQuan, 0)
                 - ISNULL(PU_ArrivalVouchs.fInValidInQuan, 0) - ISNULL(PU_ArrivalVouchs.fDTQuantity, 0)
                 - ISNULL(PU_ArrivalVouchs.fRefuseQuantity, 0) <= 0 THEN
                PU_ArrivalVouchs.iQuantity - ISNULL(PU_ArrivalVouchs.fValidInQuan, 0)
                - ISNULL(PU_ArrivalVouchs.fInValidInQuan, 0) - ISNULL(PU_ArrivalVouchs.fDTQuantity, 0)
                - ISNULL(PU_ArrivalVouchs.fRefuseQuantity, 0)
            ELSE
                0
        END
       )
        END
       ) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       PU_ArrivalVouchs.cFree1,
       PU_ArrivalVouchs.cFree2,
       PU_ArrivalVouchs.cFree3,
       PU_ArrivalVouchs.cFree4,
       PU_ArrivalVouchs.cFree5,
       PU_ArrivalVouchs.cFree6,
       PU_ArrivalVouchs.cFree7,
       PU_ArrivalVouchs.cFree8,
       PU_ArrivalVouchs.cFree9,
       PU_ArrivalVouchs.cFree10,
       ISNULL(PU_ArrivalVouchs.SoType, 0),
       ISNULL(PU_ArrivalVouchs.SoDId, N'')
FROM PU_ArrivalVouch
    INNER JOIN PU_ArrivalVouchs
        ON PU_ArrivalVouch.ID = PU_ArrivalVouchs.ID
    INNER JOIN Inventory
        ON PU_ArrivalVouchs.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON PU_ArrivalVouchs.cWhCode = Warehouse.cWhCode
WHERE (
          bInAvailCalcu = 1
          OR ISNULL(PU_ArrivalVouchs.cWhCode, N'') = N''
      )
      AND ISNULL(PU_ArrivalVouch.iBillType, 0) <> 2
      AND dDate <= @dtTime
      AND ISNULL(PU_ArrivalVouchs.cbcloser, N'') = N''

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (iQuantity) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM rdrecord10 rdrecord
    INNER JOIN rdrecords10 RdRecords
        ON rdrecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON rdrecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(rdrecord.cHandler, N'') = N''
      AND cSource <> N'调拨'
      AND rdrecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (iQuantity) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM RdRecord01 rdrecord
    INNER JOIN rdrecords01 RdRecords
        ON rdrecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON rdrecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(rdrecord.cHandler, N'') = N''
      AND cSource <> N'调拨'
      AND rdrecord.cBusType <> N'假退料'


INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (iQuantity) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM RdRecord08 rdrecord
    INNER JOIN rdrecords08 RdRecords
        ON rdrecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON rdrecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(rdrecord.cHandler, N'') = N''
      AND cSource <> N'调拨'
      AND rdrecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (ABS(iQuantity)) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM RdRecord09 RdRecord
    INNER JOIN rdrecords09 RdRecords
        ON RdRecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON RdRecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(RdRecord.cHandler, N'') = N''
      AND RdRecords.iQuantity < 0
      AND cSource <> N'调拨'
      AND RdRecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (ABS(iQuantity)) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM rdrecord11 RdRecord
    INNER JOIN rdrecords11 RdRecords
        ON RdRecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON RdRecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(RdRecord.cHandler, N'') = N''
      AND RdRecords.iQuantity < 0
      AND cSource <> N'调拨'
      AND RdRecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (ABS(iQuantity)) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM rdrecord32 RdRecord
    INNER JOIN rdrecords32 RdRecords
        ON RdRecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON RdRecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(RdRecord.cHandler, N'') = N''
      AND RdRecords.iQuantity < 0
      AND cSource <> N'调拨'
      AND RdRecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT ScrapVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (ABS(iQuantity)) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       cFree1,
       cFree2,
       cFree3,
       cFree4,
       cFree5,
       cFree6,
       cFree7,
       cFree8,
       cFree9,
       cFree10,
       ISNULL(ScrapVouchs.iSoType, 0),
       ISNULL(ScrapVouchs.iSodid, N'')
FROM ScrapVouch
    INNER JOIN ScrapVouchs
        ON ScrapVouch.Id = ScrapVouchs.ID
    INNER JOIN Inventory
        ON ScrapVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse W
        ON ScrapVouch.cInWhcode = W.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(ScrapVouch.cVerifyPerson, N'') = N''
      AND ScrapVouchs.iQuantity > 0
      AND cVouchType = N'45'
      AND
      (
          cSource = N'入库验收单'
          OR cSource = N'退货验收单'
      )


INSERT INTO #TempKCView_1
SELECT GSP_VOUCHSQC.CINVCODE,
       0 AS YQGL,
       0 AS DDZT,
       (CASE
            WHEN ISNULL(IREPORT, 0) = 0
                 AND ISNULL(BCHECK, 0) = 1 THEN
                ISNULL(FQUANTITY, 0)
            ELSE
                ISNULL(FELGQUANTITY, 0)
        END
       ) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       DDATE,
       GSP_VOUCHSQC.CFREE1,
       GSP_VOUCHSQC.CFREE2,
       GSP_VOUCHSQC.CFREE3,
       GSP_VOUCHSQC.CFREE4,
       GSP_VOUCHSQC.CFREE5,
       GSP_VOUCHSQC.CFREE6,
       GSP_VOUCHSQC.CFREE7,
       GSP_VOUCHSQC.CFREE8,
       GSP_VOUCHSQC.CFREE9,
       GSP_VOUCHSQC.CFREE10,
       0,
       0
FROM GSP_VOUCHSQC
    INNER JOIN GSP_VOUCHQC
        ON GSP_VOUCHSQC.ID = GSP_VOUCHQC.ID
    INNER JOIN Inventory
        ON GSP_VOUCHSQC.CINVCODE = Inventory.cInvCode
    LEFT JOIN Warehouse W
        ON GSP_VOUCHSQC.CWHCODE = W.cWhCode
WHERE bInAvailCalcu = 1
      AND ISNULL(BMAKEPURIN, 0) <> 1
      AND ISNULL(BMAKESALEOUT, 0) <> 1
      AND ISNULL(ICODE_T, 0) = 0
      AND dDate <= @dtTime

INSERT INTO #TempKCView_1
SELECT DispatchLists.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (CASE
            WHEN (ABS(iQuantity) - ABS(ISNULL(fOutQuantity, 0))) < 0 THEN
                0
            ELSE
                ABS(iQuantity) - ABS(ISNULL(fOutQuantity, 0))
        END
       ) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       DispatchLists.cFree1,
       DispatchLists.cFree2,
       DispatchLists.cFree3,
       DispatchLists.cFree4,
       DispatchLists.cFree5,
       DispatchLists.cFree6,
       DispatchLists.cFree7,
       DispatchLists.cFree8,
       DispatchLists.cFree9,
       DispatchLists.cFree10,
       ISNULL(DispatchLists.idemandtype, 0),
       ISNULL(DispatchLists.cdemandid, N'')
FROM DispatchList
    INNER JOIN DispatchLists
        ON DispatchList.DLID = DispatchLists.DLID
    INNER JOIN Inventory
        ON DispatchLists.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON DispatchLists.cWhCode = Warehouse.cWhCode
WHERE ISNULL(DispatchLists.bSettleAll, 0) = 0
      AND ISNULL(iQuantity, 0) < 0
      AND Inventory.bService <> 1
      AND ISNULL(DispatchLists.iTB, 0) <> 1
      AND
      (
          DispatchList.dDate <= @dtTime
          OR ISNULL(bIsSTQc, 0) = 1
      )
      AND DispatchList.dDate >= N'2018-01-01'

INSERT INTO #TempKCView_1
SELECT SaleBillVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       (ABS(iQuantity)) AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       SaleBillVouchs.cFree1,
       SaleBillVouchs.cFree2,
       SaleBillVouchs.cFree3,
       SaleBillVouchs.cFree4,
       SaleBillVouchs.cFree5,
       SaleBillVouchs.cFree6,
       SaleBillVouchs.cFree7,
       SaleBillVouchs.cFree8,
       SaleBillVouchs.cFree9,
       SaleBillVouchs.cFree10,
       ISNULL(SaleBillVouchs.idemandtype, 0),
       ISNULL(SaleBillVouchs.cdemandid, N'')
FROM SaleBillVouch
    INNER JOIN SaleBillVouchs
        ON SaleBillVouch.SBVID = SaleBillVouchs.SBVID
    INNER JOIN Inventory
        ON SaleBillVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON SaleBillVouchs.cWhCode = Warehouse.cWhCode
WHERE ISNULL(iQuantity, 0) < 0
      AND ISNULL(iDisp, 0) = 0
      AND Inventory.bService <> 1
      AND SaleBillVouchs.iTB <> 1
      AND ISNULL(SaleBillVouch.cChecker, N'') = N''
      AND ISNULL(SaleBillVouch.cInvalider, N'') = N''
      AND ISNULL(SaleBillVouch.cBusType, N'') <> N'直运销售'
      AND dDate >= N'2018-01-01'
      AND dDate <= @dtTime

IF @state = 1
BEGIN
    INSERT INTO #TempKCView_1
    (
        cInvCode,
        cFree1,
        cFree2,
        cFree3,
        cFree4,
        cFree5,
        cFree6,
        cFree7,
        cFree8,
        cFree9,
        cFree10,
        isotype,
        isodid,
        SCDD
    ) /*Qty:生产订单数量,DeclaredQty:累计报检数量, QualifiedInQty:累计入库数量*/
    SELECT R.InvCode,
           R.Free1,
           R.Free2,
           R.Free3,
           R.Free4,
           R.Free5,
           R.Free6,
           R.Free7,
           R.Free8,
           R.Free9,
           R.Free10,
           ISNULL(R.SoType, 0),
           ISNULL(R.SoDId, N''),
           (CASE
                WHEN CASE R.ByProductFlag
                         WHEN 1 THEN
                             R.Qty
                         ELSE
                             R.MrpQty
                     END - ISNULL(R.QualifiedInQty, 0) >= 0 THEN
                    CASE R.ByProductFlag
                        WHEN 1 THEN
                            R.Qty
                        ELSE
                            R.MrpQty
                    END - ISNULL(R.QualifiedInQty, 0)
                ELSE
                    0
            END
           ) AS SCDD
    FROM v_mom_orderdetail R
        INNER JOIN Inventory I
            ON R.InvCode = I.cInvCode
        LEFT JOIN Warehouse w
            ON w.cWhCode = R.WhCode
    WHERE (
              w.bInAvailCalcu = 1
              OR ISNULL(R.WhCode, N'') = N''
          )
          AND
          (
              R.Status = 2
              OR R.Status = 3
          ) /*不用考虑是否关闭*/ --生产订单状态--1,未审核--2,锁定--3,审核
          AND R.DueDate <= @dtTime /*DueDate 完工日期*/
END
ELSE
    INSERT INTO #TempKCView_1
    (
        cInvCode,
        cFree1,
        cFree2,
        cFree3,
        cFree4,
        cFree5,
        cFree6,
        cFree7,
        cFree8,
        cFree9,
        cFree10,
        isotype,
        isodid,
        SCDD
    ) /*Qty:生产订单数量,DeclaredQty:累计报检数量, QualifiedInQty:累计入库数量*/
    SELECT R.InvCode,
           R.Free1,
           R.Free2,
           R.Free3,
           R.Free4,
           R.Free5,
           R.Free6,
           R.Free7,
           R.Free8,
           R.Free9,
           R.Free10,
           ISNULL(R.SoType, 0),
           ISNULL(R.SoDId, N''),
           (CASE
                WHEN CASE R.ByProductFlag
                         WHEN 1 THEN
                             R.Qty
                         ELSE
                             R.MrpQty
                     END - ISNULL(R.QualifiedInQty, 0) >= 0 THEN
                    CASE R.ByProductFlag
                        WHEN 1 THEN
                            R.Qty
                        ELSE
                            R.MrpQty
                    END - ISNULL(R.QualifiedInQty, 0)
                ELSE
                    0
            END
           ) AS SCDD
    FROM v_mom_orderdetail R
        INNER JOIN Inventory I
            ON R.InvCode = I.cInvCode
        LEFT JOIN Warehouse w
            ON w.cWhCode = R.WhCode
    WHERE (
              w.bInAvailCalcu = 1
              OR ISNULL(R.WhCode, N'') = N''
          )
          AND R.Status = 3 /*不用考虑是否关闭*/ --生产订单状态--1,未审核--2,锁定--3,审核
          AND R.DueDate <= @dtTime /*DueDate 完工日期*/

INSERT INTO #TempKCView_1
(
    cInvCode,
    cFree1,
    cFree2,
    cFree3,
    cFree4,
    cFree5,
    cFree6,
    cFree7,
    cFree8,
    cFree9,
    cFree10,
    isotype,
    isodid,
    SCWL
)
SELECT Rs.InvCode,
       Rs.Free1,
       Rs.Free2,
       Rs.Free3,
       Rs.Free4,
       Rs.Free5,
       Rs.Free6,
       Rs.Free7,
       Rs.Free8,
       Rs.Free9,
       Rs.Free10,
       ISNULL(Rs.SoType, 0),
       ISNULL(Rs.SoDId, N''), /*Qty:数量,IssQty:已领量*/
       (CASE
            WHEN Rs.Qty - ISNULL(Rs.IssQty, 0) + ISNULL(mom_replenishapplydtl.issueqty, 0)
                 + ISNULL(mom_replenishapplydtl.CmplQty, 0)
                 - (CASE
                        WHEN ISNULL(mom_replenishapplydtl.withdrawqty, 0) > ISNULL(mom_replenishapplydtl.applyqty, 0) THEN
                            ISNULL(mom_replenishapplydtl.applyqty, 0)
                        ELSE
                            ISNULL(mom_replenishapplydtl.withdrawqty, 0)
                    END
                   ) >= 0 THEN
                Rs.Qty - ISNULL(Rs.IssQty, 0) + ISNULL(mom_replenishapplydtl.issueqty, 0)
                + ISNULL(mom_replenishapplydtl.CmplQty, 0)
                - (CASE
                       WHEN ISNULL(mom_replenishapplydtl.withdrawqty, 0) > ISNULL(mom_replenishapplydtl.applyqty, 0) THEN
                           ISNULL(mom_replenishapplydtl.applyqty, 0)
                       ELSE
                           ISNULL(mom_replenishapplydtl.withdrawqty, 0)
                   END
                  )
            ELSE
                0
        END
       ) AS SCWL
FROM v_mom_moallocate_st Rs
    INNER JOIN v_mom_orderdetail R
        ON R.MoDId = Rs.MoDId
    INNER JOIN Inventory
        ON Rs.InvCode = Inventory.cInvCode
    LEFT JOIN
    (
        SELECT mom_replenishapplydtl.MoAllocateId,
               SUM(   CASE
                          WHEN ISNULL(mom_replenishapply.CloseUser, N'') = N'' THEN
                              mom_replenishapplydtl.issueqty
                          ELSE
                              0
                      END
                  ) AS issueqty,
               SUM(   CASE
                          WHEN ISNULL(mom_replenishapply.CloseUser, N'') = N'' THEN
                              mom_replenishapplydtl.CmplQty
                          ELSE
                              0
                      END
                  ) AS CmplQty,
               SUM(   CASE
                          WHEN ISNULL(mom_replenishapply.CloseUser, N'') = N'' THEN
                              mom_replenishapplydtl.applyqty
                          ELSE
                              0
                      END
                  ) AS applyqty,
               SUM(WithdrawQty) AS withdrawqty
        FROM mom_replenishapplydtl
            LEFT JOIN mom_replenishapply
                ON mom_replenishapply.ApplyId = mom_replenishapplydtl.ApplyId
        WHERE mom_replenishapply.Status = 3
        GROUP BY mom_replenishapplydtl.MoAllocateId
    ) mom_replenishapplydtl
        ON mom_replenishapplydtl.MoAllocateId = Rs.AllocateId
    LEFT JOIN Warehouse W
        ON Rs.WhCode = W.cWhCode
WHERE (
          bInAvailCalcu = 1
          OR ISNULL(Rs.WhCode, N'') = N''
      )
      AND R.Status = 3 /*不用考虑是否关闭and Rs.ByproductFlag = 0 不用考虑*/
      AND R.ByProductFlag = 0
      AND Rs.ByproductFlag = 0
      AND Rs.Demdate <= @dtTime

INSERT INTO #TempKCView_1
(
    cInvCode,
    cFree1,
    cFree2,
    cFree3,
    cFree4,
    cFree5,
    cFree6,
    cFree7,
    cFree8,
    cFree9,
    cFree10,
    isotype,
    isodid,
    SCWL
)
SELECT Rs.InvCode,
       Rs.Free1,
       Rs.Free2,
       Rs.Free3,
       Rs.Free4,
       Rs.Free5,
       Rs.Free6,
       Rs.Free7,
       Rs.Free8,
       Rs.Free9,
       Rs.Free10,
       ISNULL(Rs.SoType, 0),
       ISNULL(Rs.SoDId, N''), /*Qty:数量,IssQty:已领量*/
       (CASE
            WHEN mom_replenishapplydtl.ApplyQty - ISNULL(mom_replenishapplydtl.CmplQty, 0)
                 - ISNULL(mom_replenishapplydtl.IssueQty, 0) >= 0 THEN
                mom_replenishapplydtl.ApplyQty - ISNULL(mom_replenishapplydtl.CmplQty, 0)
                - ISNULL(mom_replenishapplydtl.IssueQty, 0)
            ELSE
                0
        END
       ) AS SCWL
FROM mom_replenishapplydtl
    INNER JOIN mom_replenishapply R
        ON R.ApplyId = mom_replenishapplydtl.ApplyId
    LEFT OUTER JOIN mom_orderdetail
        ON mom_replenishapplydtl.MoDId = mom_orderdetail.MoDId
    LEFT OUTER JOIN mom_moallocate Rs
        ON Rs.AllocateId = mom_replenishapplydtl.MoAllocateId
    INNER JOIN Inventory
        ON Rs.InvCode = Inventory.cInvCode
    LEFT JOIN Warehouse W
        ON Rs.WhCode = W.cWhCode
WHERE (
          bInAvailCalcu = 1
          OR ISNULL(Rs.WhCode, N'') = N''
      )
      AND R.Status = 3
      AND ISNULL(R.CloseUser, N'') = N''
      AND Rs.StartDemDate <= @dtTime

INSERT INTO #TempKCView_1
(
    cInvCode,
    cFree1,
    cFree2,
    cFree3,
    cFree4,
    cFree5,
    cFree6,
    cFree7,
    cFree8,
    cFree9,
    cFree10,
    isotype,
    isodid,
    WWDD
)
SELECT Rs.cInvCode,
       cFree1,
       cFree2,
       cFree3,
       cFree4,
       cFree5,
       cFree6,
       cFree7,
       cFree8,
       cFree9,
       cFree10,
       ISNULL(Rs.SOType, 0),
       ISNULL(Rs.SODID, N''),
       (CASE
            WHEN Rs.iQuantity - ISNULL(Rs.iReceivedQTY, 0) - ISNULL(Rs.iArrQTY, 0) >= 0 THEN
                Rs.iQuantity - ISNULL(Rs.iReceivedQTY, 0) - ISNULL(Rs.iArrQTY, 0)
            ELSE
                0
        END
       ) AS WWDD
FROM OM_MODetails Rs
    INNER JOIN OM_MOMain R
        ON R.MOID = Rs.MOID
    INNER JOIN Inventory Inventory
        ON Rs.cInvCode = Inventory.cInvCode
WHERE 1 = 1
      AND
      (
          ISNULL(R.cVerifier, N'') <> N''
          OR ISNULL(Rs.iSourceMOCode, N'') <> N''
          OR ISNULL(R.cLocker, N'') <> N''
      )
      AND
      (
          ISNULL(R.cChanger, N'') = N''
          OR ISNULL(R.cChangeVerifier, N'') <> N''
      )
      AND ISNULL(Rs.cbCloser, N'') = N''
      AND ISNULL(Rs.dArriveDate, R.dDate) <= @dtTime

INSERT INTO #TempKCView_1
(
    cInvCode,
    cFree1,
    cFree2,
    cFree3,
    cFree4,
    cFree5,
    cFree6,
    cFree7,
    cFree8,
    cFree9,
    cFree10,
    isotype,
    isodid,
    WWWL
)
SELECT Rss.cInvCode,
       Rss.cFree1,
       Rss.cFree2,
       Rss.cFree3,
       Rss.cFree4,
       Rss.cFree5,
       Rss.cFree6,
       Rss.cFree7,
       Rss.cFree8,
       Rss.cFree9,
       Rss.cFree10,
       ISNULL(Rss.sotype, 0),
       ISNULL(Rss.sodid, N''),
       (CASE
            WHEN Rss.iQuantity - ISNULL(Rss.iSendQTY, 0) >= 0 THEN
                Rss.iQuantity - ISNULL(Rss.iSendQTY, 0)
            ELSE
                0
        END
       ) AS WWWL
FROM OM_MOMain R
    INNER JOIN OM_MODetails Rs
        ON R.MOID = Rs.MOID
    INNER JOIN OM_MOMaterials Rss
        ON Rs.MODetailsID = Rss.MoDetailsID
    INNER JOIN Inventory
        ON Rss.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse W
        ON Rss.cWhCode = W.cWhCode
WHERE (
          ISNULL(R.cVerifier, N'') <> N''
          OR ISNULL(Rs.iSourceMOCode, N'') <> N''
          OR ISNULL(R.cLocker, N'') <> N''
      )
      AND
      (
          ISNULL(R.cChanger, N'') = N''
          OR ISNULL(R.cChangeVerifier, N'') <> N''
      )
      AND ISNULL(Rs.cbCloser, N'') = N''
      AND
      (
          ISNULL(Rss.cWhCode, N'') = N''
          OR W.bInAvailCalcu = 1
      )
      AND ISNULL(Rss.dRequiredDate, R.dDate) <= @dtTime
      AND ISNULL(Rss.iProductType, 0) = 0


INSERT INTO #TempKCView_1
SELECT SO_SODetails.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       (CASE
            WHEN (iQuantity - ISNULL(iFHQuantity, 0)) < 0 THEN
                0
            ELSE
       (iQuantity - ISNULL(iFHQuantity, 0))
        END
       ) AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       (CASE
            WHEN ISNULL(SO_SODetails.dPreDate, N'') = N'' THEN
                SO_SOMain.dDate
            ELSE
                SO_SODetails.dPreDate
        END
       ) AS dDate,
       SO_SODetails.cFree1,
       SO_SODetails.cFree2,
       SO_SODetails.cFree3,
       SO_SODetails.cFree4,
       SO_SODetails.cFree5,
       SO_SODetails.cFree6,
       SO_SODetails.cFree7,
       SO_SODetails.cFree8,
       SO_SODetails.cFree9,
       SO_SODetails.cFree10,
       ISNULL(SO_SODetails.idemandtype, 0),
       (CASE
            WHEN ISNULL(SO_SODetails.idemandtype, 0) = 1 THEN
                CONVERT(NVARCHAR(40), ISNULL(SO_SODetails.iSOsID, N''))
            WHEN ISNULL(SO_SODetails.idemandtype, 0) = 4 THEN
       (CASE
            WHEN ISNULL(SO_SODetails.cdemandcode, N'') = N'' THEN
                N'Systemdefault'
            ELSE
                ISNULL(SO_SODetails.cdemandcode, N'')
        END
       )
            WHEN ISNULL(SO_SODetails.idemandtype, 0) = 5 THEN
                ISNULL(SO_SODetails.cSOCode, N'')
            ELSE
                N''
        END
       )
FROM SO_SOMain
    INNER JOIN SO_SODetails
        ON SO_SOMain.ID = SO_SODetails.ID
    INNER JOIN Inventory
        ON SO_SODetails.cInvCode = Inventory.cInvCode
WHERE ISNULL(SO_SOMain.cVerifier, N'') <> N''
      AND ISNULL(SO_SODetails.cSCloser, N'') = N''
      AND ISNULL(SO_SOMain.cBusType, N'') <> N'直运销售'
      AND ISNULL(SO_SODetails.cParentCode, N'') = N''
      AND (CASE
               WHEN ISNULL(SO_SODetails.dPreDate, N'') = N'' THEN
                   SO_SOMain.dDate
               ELSE
                   SO_SODetails.dPreDate
           END
          ) <= @dtTime

INSERT INTO #TempKCView_1
SELECT v_ex_order_inuseFotSt.cinvcode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       (ABS(fQuantity)) AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       CONVERT(NVARCHAR(10), v_ex_order_inuseFotSt.dexpdate, 121) AS dDate,
       v_ex_order_inuseFotSt.cfree1,
       v_ex_order_inuseFotSt.cfree2,
       v_ex_order_inuseFotSt.cfree3,
       v_ex_order_inuseFotSt.cfree4,
       v_ex_order_inuseFotSt.cfree5,
       v_ex_order_inuseFotSt.cfree6,
       v_ex_order_inuseFotSt.cfree7,
       v_ex_order_inuseFotSt.cfree8,
       v_ex_order_inuseFotSt.cfree9,
       v_ex_order_inuseFotSt.cfree10,
       ISNULL(v_ex_order_inuseFotSt.idemandtype, 0),
       (CASE
            WHEN ISNULL(v_ex_order_inuseFotSt.idemandtype, 0) = 3 THEN
                CONVERT(NVARCHAR(40), ISNULL(v_ex_order_inuseFotSt.autoid, N''))
            WHEN ISNULL(v_ex_order_inuseFotSt.idemandtype, 0) = 4 THEN
                ISNULL(v_ex_order_inuseFotSt.cdemandcode, N'')
            WHEN ISNULL(v_ex_order_inuseFotSt.idemandtype, 0) = 6 THEN
                ISNULL(v_ex_order_inuseFotSt.ccode, N'')
            ELSE
                N''
        END
       )
FROM v_ex_order_inuseFotSt
    INNER JOIN Inventory
        ON v_ex_order_inuseFotSt.cinvcode = Inventory.cInvCode
           AND CONVERT(NVARCHAR(10), v_ex_order_inuseFotSt.dexpdate, 121) <= @dtTime

INSERT INTO #TempKCView_1
SELECT DispatchLists.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       (CASE
            WHEN (iQuantity - ISNULL(fOutQuantity, 0)) < 0 THEN
                0
            ELSE
       (iQuantity - ISNULL(fOutQuantity, 0))
        END
       ) AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       DispatchLists.cFree1,
       DispatchLists.cFree2,
       DispatchLists.cFree3,
       DispatchLists.cFree4,
       DispatchLists.cFree5,
       DispatchLists.cFree6,
       DispatchLists.cFree7,
       DispatchLists.cFree8,
       DispatchLists.cFree9,
       DispatchLists.cFree10,
       ISNULL(DispatchLists.idemandtype, 0),
       ISNULL(DispatchLists.cdemandid, N'')
FROM DispatchList
    INNER JOIN DispatchLists
        ON DispatchList.DLID = DispatchLists.DLID
    INNER JOIN Inventory
        ON DispatchLists.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON DispatchLists.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND ISNULL(DispatchLists.bSettleAll, 0) = 0
      AND ISNULL(iQuantity, 0) > 0
      AND Inventory.bService <> 1
      AND ISNULL(DispatchLists.iTB, 0) <> 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND
      (
          DispatchLists.bIsSTQc = 1
          OR
          (
              ISNULL(DispatchList.bFirst, 0) = 0
              AND ISNULL(DispatchList.bIAFirst, 0) = 0
          )
      )

INSERT INTO #TempKCView_1
SELECT v_EX_WaitOutForStock.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       (v_EX_WaitOutForStock.fOutQuantity) AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       CONVERT(NVARCHAR(10), v_EX_WaitOutForStock.ddate, 121) AS dDate,
       v_EX_WaitOutForStock.cFree1,
       v_EX_WaitOutForStock.cFree2,
       v_EX_WaitOutForStock.cFree3,
       v_EX_WaitOutForStock.cFree4,
       v_EX_WaitOutForStock.cFree5,
       v_EX_WaitOutForStock.cFree6,
       v_EX_WaitOutForStock.cFree7,
       v_EX_WaitOutForStock.cFree8,
       v_EX_WaitOutForStock.cFree9,
       v_EX_WaitOutForStock.cFree10,
       ISNULL(v_EX_WaitOutForStock.sotype, 0),
       ISNULL(v_EX_WaitOutForStock.sodid, N'')
FROM v_EX_WaitOutForStock
    INNER JOIN Inventory
        ON v_EX_WaitOutForStock.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse W
        ON v_EX_WaitOutForStock.cWhCode = W.cWhCode
WHERE (
          bInAvailCalcu = 1
          OR ISNULL(v_EX_WaitOutForStock.cWhCode, N'') = N''
      )
      AND CONVERT(NVARCHAR(10), v_EX_WaitOutForStock.ddate, 121) <= @dtTime

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       (iQuantity) AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM RdRecord09 RdRecord
    INNER JOIN rdrecords09 RdRecords
        ON RdRecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON RdRecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(RdRecord.cHandler, N'') = N''
      AND RdRecords.iQuantity > 0
      AND cSource <> N'调拨'
      AND RdRecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       (iQuantity) AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM rdrecord11 RdRecord
    INNER JOIN rdrecords11 RdRecords
        ON RdRecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON RdRecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(RdRecord.cHandler, N'') = N''
      AND RdRecords.iQuantity > 0
      AND cSource <> N'调拨'
      AND RdRecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT RdRecords.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       (iQuantity) AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       RdRecords.cFree1,
       RdRecords.cFree2,
       RdRecords.cFree3,
       RdRecords.cFree4,
       RdRecords.cFree5,
       RdRecords.cFree6,
       RdRecords.cFree7,
       RdRecords.cFree8,
       RdRecords.cFree9,
       RdRecords.cFree10,
       ISNULL(RdRecords.isotype, 0),
       ISNULL(RdRecords.isodid, N'')
FROM rdrecord32 RdRecord
    INNER JOIN rdrecords32 RdRecords
        ON RdRecord.ID = RdRecords.ID
    INNER JOIN Inventory
        ON RdRecords.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON RdRecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND dDate <= @dtTime
      AND dDate >= N'2018-01-01'
      AND ISNULL(RdRecord.cHandler, N'') = N''
      AND RdRecords.iQuantity > 0
      AND cSource <> N'调拨'
      AND RdRecord.cBusType <> N'假退料'

INSERT INTO #TempKCView_1
SELECT SaleBillVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       (iQuantity) AS DFHL,
       0 AS DBDF,
       0 AS BLJH,
       dDate,
       SaleBillVouchs.cFree1,
       SaleBillVouchs.cFree2,
       SaleBillVouchs.cFree3,
       SaleBillVouchs.cFree4,
       SaleBillVouchs.cFree5,
       SaleBillVouchs.cFree6,
       SaleBillVouchs.cFree7,
       SaleBillVouchs.cFree8,
       SaleBillVouchs.cFree9,
       SaleBillVouchs.cFree10,
       ISNULL(SaleBillVouchs.idemandtype, 0),
       ISNULL(SaleBillVouchs.cdemandid, N'')
FROM SaleBillVouch
    INNER JOIN SaleBillVouchs
        ON SaleBillVouch.SBVID = SaleBillVouchs.SBVID
    INNER JOIN Inventory
        ON SaleBillVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON SaleBillVouchs.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND ISNULL(iQuantity, 0) > 0
      AND ISNULL(iDisp, 0) <> 1
      AND ISNULL(cBusType, N'') <> N'委托'
      AND ISNULL(Inventory.bInvType, 0) <> 1
      AND Inventory.bService <> 1
      AND SaleBillVouchs.iTB <> 1
      AND ISNULL(SaleBillVouch.cChecker, N'') = N''
      AND (SaleBillVouch.cDLCode IS NULL)
      AND ISNULL(SaleBillVouch.cInvalider, N'') = N''
      AND ISNULL(SaleBillVouch.cBusType, N'') <> N'直运销售'
      AND dDate >= N'2018-01-01'
      AND dDate <= @dtTime

INSERT INTO #TempKCView_1
SELECT TransVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       (CASE
            WHEN ISNULL(RdRecord.cHandler, N'') = N'' THEN
                TransVouchs.iTVQuantity
            ELSE
                0
        END
       ) AS DBDF,
       0 AS BLJH,
       RdRecord.dDate,
       TransVouchs.cFree1,
       TransVouchs.cFree2,
       TransVouchs.cFree3,
       TransVouchs.cFree4,
       TransVouchs.cFree5,
       TransVouchs.cFree6,
       TransVouchs.cFree7,
       TransVouchs.cFree8,
       TransVouchs.cFree9,
       TransVouchs.cFree10,
       ISNULL(TransVouchs.iDSoType, 0),
       ISNULL(TransVouchs.iDSodid, N'')
FROM rdrecords09 RdRecords
    INNER JOIN RdRecord09 RdRecord
        ON RdRecords.ID = RdRecord.ID
    INNER JOIN TransVouch
        INNER JOIN TransVouchs
            ON TransVouch.ID = TransVouchs.ID
        ON RdRecords.iTrIds = TransVouchs.autoID
    INNER JOIN Inventory
        ON TransVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON RdRecord.cWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND ISNULL(TransVouch.cVerifyPerson, N'') <> N''
      AND RdRecord.bRdFlag <> 1
      AND RdRecord.cBusType = N'调拨出库'
      AND RdRecord.dDate <= @dtTime
      AND RdRecords.iQuantity > 0

INSERT INTO #TempKCView_1
SELECT TransVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       TransVouchs.iTVQuantity AS DBDF,
       0 AS BLJH,
       TransVouch.dTVDate,
       TransVouchs.cFree1,
       TransVouchs.cFree2,
       TransVouchs.cFree3,
       TransVouchs.cFree4,
       TransVouchs.cFree5,
       TransVouchs.cFree6,
       TransVouchs.cFree7,
       TransVouchs.cFree8,
       TransVouchs.cFree9,
       TransVouchs.cFree10,
       ISNULL(TransVouchs.iDSoType, 0),
       ISNULL(TransVouchs.iDSodid, N'')
FROM TransVouch
    INNER JOIN TransVouchs
        ON TransVouch.ID = TransVouchs.ID
    INNER JOIN Inventory
        ON TransVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON TransVouch.cOWhCode = Warehouse.cWhCode
WHERE bInAvailCalcu = 1
      AND ISNULL(cVerifyPerson, N'') = N''
      AND TransVouch.dTVDate <= @dtTime

INSERT INTO #TempKCView_1
SELECT MatchVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       (CASE
            WHEN (iPlanQuantity - ISNULL(iSumActiQuantity, 0)) < 0 THEN
                0
            ELSE
       (iPlanQuantity - ISNULL(iSumActiQuantity, 0))
        END
       ) AS BLJH,
       MatchVouch.PlanDate,
       MatchVouchs.cFree1,
       MatchVouchs.cFree2,
       MatchVouchs.cFree3,
       MatchVouchs.cFree4,
       MatchVouchs.cFree5,
       MatchVouchs.cFree6,
       MatchVouchs.cFree7,
       MatchVouchs.cFree8,
       MatchVouchs.cFree9,
       MatchVouchs.cFree10,
       0,
       0
FROM MatchVouch
    INNER JOIN MatchVouchs
        ON MatchVouch.ID = MatchVouchs.ID
    INNER JOIN Inventory
        ON MatchVouchs.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON MatchVouchs.cWhCode = Warehouse.cWhCode
WHERE Warehouse.bInAvailCalcu = 1
      AND ISNULL(MatchVouch.cCloser, N'') = N''
      AND CONVERT(NVARCHAR(10), MatchVouch.PlanDate, 121) <= @dtTime

INSERT INTO #TempKCView_1
SELECT MaterialAppVouchs.cInvCode,
       0 AS YQGL,
       0 AS DDZT,
       0 AS DHDJ,
       0 AS SCDD,
       0 AS SCWL,
       0 AS WWDD,
       0 AS WWWL,
       0 AS DBZT,
       0 AS YDGL,
       0 AS DFHL,
       0 AS DBDF,
       (CASE
            WHEN (iQuantity - ISNULL(fOutQuantity, 0)) < 0 THEN
                0
            ELSE
       (iQuantity - ISNULL(fOutQuantity, 0))
        END
       ) AS BLJH,
       ISNULL(MaterialAppVouchs.dDueDate, MaterialAppVouch.dDate) AS dduedateCaculate,
       MaterialAppVouchs.cFree1,
       MaterialAppVouchs.cFree2,
       MaterialAppVouchs.cFree3,
       MaterialAppVouchs.cFree4,
       MaterialAppVouchs.cFree5,
       MaterialAppVouchs.cFree6,
       MaterialAppVouchs.cFree7,
       MaterialAppVouchs.cFree8,
       MaterialAppVouchs.cFree9,
       MaterialAppVouchs.cFree10,
       0,
       0
FROM MaterialAppVouch
    INNER JOIN MaterialAppVouchs
        ON MaterialAppVouch.ID = MaterialAppVouchs.ID
    INNER JOIN Inventory
        ON MaterialAppVouchs.cInvCode = Inventory.cInvCode
    LEFT JOIN Warehouse
        ON MaterialAppVouchs.cWhCode = Warehouse.cWhCode
WHERE (
          Warehouse.bInAvailCalcu = 1
          OR ISNULL(MaterialAppVouchs.cWhCode, N'') = N''
      )
      AND ISNULL(MaterialAppVouch.cCloser, N'') = N''
      AND ISNULL(MaterialAppVouch.cHandler, N'') <> N''
      AND CONVERT(NVARCHAR(10), ISNULL(MaterialAppVouchs.dDueDate, MaterialAppVouch.dDate), 121) <= @dtTime
      AND ISNULL(MaterialAppVouch.cSource, N'') = N''
      AND ISNULL(MaterialAppVouchs.cBCloser, N'') = N''

SELECT *
INTO #TempKCView_6
FROM
(
    SELECT k.cInvCode,
           k.YQGL,
           k.DDZT,
           k.DHDJ,
           k.SCDD,
           k.SCWL,
           k.WWDD,
           k.WWWL,
           k.DBZT,
           k.YDGL,
           k.DFHL,
           k.DBDF,
           k.BLJH,
           k.dDate,
           k.cFree1,
           k.cFree2,
           k.cFree3,
           k.cFree4,
           k.cFree5,
           k.cFree6,
           k.cFree7,
           k.cFree8,
           k.cFree9,
           k.cFree10,
           (CASE
                WHEN Inventory.cSRPolicy = N'LP'
                     AND ISNULL(Inventory.bSpecialOrder, 0) = 1 THEN
                    k.isotype
                ELSE
                    0
            END
           ) AS isotype,
           (CASE
                WHEN Inventory.cSRPolicy = N'LP'
                     AND ISNULL(Inventory.bSpecialOrder, 0) = 1 THEN
           (CASE
                WHEN k.isotype = 1 THEN
                    SO_SODetails.cSOCode
                WHEN k.isotype = 3 THEN
                    ex_order.ccode
                WHEN k.isotype = 0 THEN
                    N''
                ELSE
                    k.isodid
            END
           )
                ELSE
                    N''
            END
           ) AS csocode,
           (CASE
                WHEN Inventory.cSRPolicy = N'LP'
                     AND ISNULL(Inventory.bSpecialOrder, 0) = 1 THEN
           (CASE
                WHEN k.isotype = 1 THEN
                    CONVERT(VARCHAR(10), SO_SODetails.iRowNo)
                WHEN k.isotype = 3 THEN
                    CONVERT(VARCHAR(10), ex_orderdetail.irowno)
                ELSE
                    N''
            END
           )
                ELSE
                    N''
            END
           ) AS isoseq
    FROM #TempKCView_1 AS k
        INNER JOIN Inventory
            ON Inventory.cInvCode = k.cInvCode
        LEFT JOIN SO_SODetails
            ON k.isodid = CONVERT(NVARCHAR(40), SO_SODetails.iSOsID)
               AND k.isotype = 1
        LEFT JOIN ex_orderdetail
            ON k.isodid = CONVERT(NVARCHAR(40), ex_orderdetail.autoid)
               AND k.isotype = 3
        LEFT JOIN ex_order
            ON ex_orderdetail.id = ex_order.id
) T
WHERE 1 = 1
CREATE INDEX kcview_6_inx_inv ON #TempKCView_6 (cInvCode)

SELECT cInvCode,
       ISNULL(iSafeNum, 0) AS iSafeNum,
       ISNULL(iTopSum, 0) AS iTopSum,
       ISNULL(iLowSum, 0) AS iLowSum
INTO #TempKCView_3
FROM Inventory
CREATE INDEX kcview_3_inx_inv ON #TempKCView_3 (cInvCode)
SELECT TopLowSafeRefer.cInvCode,
       SPACE(20) AS cFree1,
       SPACE(20) AS cFree2,
       SPACE(20) AS cFree3,
       SPACE(20) AS cFree4,
       SPACE(20) AS cFree5,
       SPACE(20) AS cFree6,
       SPACE(20) AS cFree7,
       SPACE(20) AS cFree8,
       SPACE(20) AS cFree9,
       SPACE(20) AS cFree10,
       T.isotype,
       T.csocode,
       ISNULL(T.isoseq, N'') AS isoseq,
       SUM(YQGL) AS YQGL,
       SUM(DDZT) AS DDZT,
       SUM(DHDJ) AS DHDJ,
       SUM(SCDD) AS SCDD,
       SUM(SCWL) AS SCWL,
       SUM(WWDD) AS WWDD,
       SUM(WWWL) AS WWWL,
       SUM(DBZT) AS DBZT,
       (ISNULL(SUM(YQGL), 0) + ISNULL(SUM(DDZT), 0) + ISNULL(SUM(DHDJ), 0) + ISNULL(SUM(SCDD), 0)
        + ISNULL(SUM(WWDD), 0) + ISNULL(SUM(DBZT), 0)
       ) AS RKHJ,
       SUM(YDGL) AS YDGL,
       SUM(DFHL) AS DFHL,
       SUM(DBDF) AS DBDF,
       SUM(BLJH) AS BLJH,
       (ISNULL(SUM(YDGL), 0) + ISNULL(SUM(DFHL), 0) + ISNULL(SUM(DBDF), 0) + ISNULL(SUM(BLJH), 0)
        + ISNULL(SUM(SCWL), 0) + ISNULL(SUM(WWWL), 0)
       ) AS CKHJ,
       ISNULL(TopLowSafeRefer.iSafeNum, 0) AS iSafeNum,
       ISNULL(TopLowSafeRefer.iTopSum, 0) AS iTopSum,
       ISNULL(TopLowSafeRefer.iLowSum, 0) AS iLowSum
INTO #TempKCView_2
FROM #TempKCView_6 AS T
    RIGHT JOIN #TempKCView_3 TopLowSafeRefer
        ON T.cInvCode = TopLowSafeRefer.cInvCode
WHERE (1 = 1)
GROUP BY TopLowSafeRefer.cInvCode,
         T.isotype,
         T.csocode,
         T.isoseq,
         TopLowSafeRefer.iSafeNum,
         TopLowSafeRefer.iTopSum,
         TopLowSafeRefer.iLowSum
SELECT Inventory.cInvCode,
       cInvName,
       cInvAddCode,
       cInvStd,
       cComUnitName,
       SPACE(20) AS cFree1,
       SPACE(20) AS cFree2,
       SPACE(20) AS cFree3,
       SPACE(20) AS cFree4,
       SPACE(20) AS cFree5,
       SPACE(20) AS cFree6,
       SPACE(20) AS cFree7,
       SPACE(20) AS cFree8,
       SPACE(20) AS cFree9,
       SPACE(20) AS cFree10,
       T.isotype,
       T.csocode,
       ISNULL(T.isoseq, N'') AS isoseq,
       CONVERT(FLOAT, 0.0) AS XCL,
       CONVERT(FLOAT, 0.0) AS DJL,
       CONVERT(FLOAT, 0.0) AS BHGP,
       YQGL,
       DDZT,
       DHDJ,
       DBZT,
       SCDD,
       SCWL,
       WWDD,
       WWWL,
       RKHJ,
       YDGL,
       DFHL,
       DBDF,
       BLJH,
       CKHJ,
       ISNULL(T.iSafeNum, CONVERT(FLOAT, 0.0)) AS iSafeNum,
       ISNULL(T.iTopSum, CONVERT(FLOAT, 0.0)) AS iTopSum,
       ISNULL(T.iLowSum, CONVERT(FLOAT, 0.0)) AS iLowSum,
       cInvDefine1,
       cInvDefine2,
       cInvDefine3,
       cInvDefine4,
       cInvDefine5,
       cInvDefine6,
       cInvDefine7,
       cInvDefine8,
       cInvDefine9,
       cInvDefine10,
       cInvDefine11,
       cInvDefine12,
       cInvDefine13,
       cInvDefine14,
       cInvDefine15,
       cInvDefine16
INTO #TempKCView_4
FROM ComputationUnit
    INNER JOIN Inventory
        ON ComputationUnit.cComunitCode = Inventory.cComUnitCode
    LEFT OUTER JOIN #TempKCView_2 T
        ON T.cInvCode = Inventory.cInvCode
INSERT INTO #TempKCView_4
(
    cInvCode,
    cInvName,
    cInvAddCode,
    cInvStd,
    cComUnitName,
    XCL,
    DJL,
    BHGP,
    cFree1,
    cFree2,
    cFree3,
    cFree4,
    cFree5,
    cFree6,
    cFree7,
    cFree8,
    cFree9,
    cFree10,
    isotype,
    csocode,
    isoseq,
    cInvDefine1,
    cInvDefine2,
    cInvDefine3,
    cInvDefine4,
    cInvDefine5,
    cInvDefine6,
    cInvDefine7,
    cInvDefine8,
    cInvDefine9,
    cInvDefine10,
    cInvDefine11,
    cInvDefine12,
    cInvDefine13,
    cInvDefine14,
    cInvDefine15,
    cInvDefine16,
    iSafeNum,
    iTopSum,
    iLowSum
)
SELECT Inventory.cInvCode,
       cInvName,
       cInvAddCode,
       cInvStd,
       cComUnitName,
       (ISNULL(iQuantity, 0) + ISNULL(ipeqty, 0)) AS XCL,
       (CASE
            WHEN
            (
                ISNULL(bStopFlag, 0) <> 0
                OR ISNULL(BGSPSTOP, 0) <> 0
            ) THEN
                iQuantity
            ELSE
                fStopQuantity
        END
       ) AS DJL,
       (fDisableQuantity) AS BHGP,
       SPACE(20) AS cFree1,
       SPACE(20) AS cFree2,
       SPACE(20) AS cFree3,
       SPACE(20) AS cFree4,
       SPACE(20) AS cFree5,
       SPACE(20) AS cFree6,
       SPACE(20) AS cFree7,
       SPACE(20) AS cFree8,
       SPACE(20) AS cFree9,
       SPACE(20) AS cFree10,
       ISNULL(isotype, 0) AS isotype,
       csocode,
       ISNULL(CONVERT(VARCHAR(10), isoseq), N'') AS isoseq,
       cInvDefine1,
       cInvDefine2,
       cInvDefine3,
       cInvDefine4,
       cInvDefine5,
       cInvDefine6,
       cInvDefine7,
       cInvDefine8,
       cInvDefine9,
       cInvDefine10,
       cInvDefine11,
       cInvDefine12,
       cInvDefine13,
       cInvDefine14,
       cInvDefine15,
       cInvDefine16,
       (#TempKCView_3.iSafeNum),
       (#TempKCView_3.iTopSum),
       (#TempKCView_3.iLowSum)
FROM V_CurrentStock_PE T
    INNER JOIN Inventory
        ON T.cInvCode = Inventory.cInvCode
    INNER JOIN Warehouse
        ON T.cWhCode = Warehouse.cWhCode
    INNER JOIN ComputationUnit
        ON Inventory.cComUnitCode = ComputationUnit.cComunitCode
    LEFT JOIN #TempKCView_3
        ON T.cInvCode = #TempKCView_3.cInvCode
WHERE bInAvailCalcu = 1

DELETE FROM #TempKCView_4
WHERE cInvCode IN
      (
          SELECT cInvCode
          FROM Inventory WITH (NOLOCK)
          WHERE ISNULL(bService, 0) = 1
      )
--Select (Case When IsNull(cValue,N'') = N'' Then cDefault Else cValue End) As cValue ,* From Accinformation Where cSysid = N'ST' And cName = N'bShowAllDataForKCView'


SELECT cInvCode,
       cInvName,
       cInvAddCode,
       cInvStd,
       cComUnitName,
       SPACE(20) AS cFree1,
       SPACE(20) AS cFree2,
       SPACE(20) AS cFree3,
       SPACE(20) AS cFree4,
       SPACE(20) AS cFree5,
       SPACE(20) AS cFree6,
       SPACE(20) AS cFree7,
       SPACE(20) AS cFree8,
       SPACE(20) AS cFree9,
       SPACE(20) AS cFree10,
       LTRIM(STR(SUM(ISNULL(XCL, 0)), 30, 2)) AS XCL,
       LTRIM(STR(SUM(ISNULL(DJL, 0)), 30, 2)) AS DJL,
       LTRIM(STR(SUM(ISNULL(YQGL, 0)), 30, 2)) AS YQGL,
       LTRIM(STR(SUM(ISNULL(DDZT, 0)), 30, 2)) AS DDZT,
       LTRIM(STR(SUM(ISNULL(DHDJ, 0)), 30, 2)) AS DHDJ,
       LTRIM(STR(SUM(ISNULL(DBZT, 0)), 30, 2)) AS DBZT,
       LTRIM(STR(SUM(ISNULL(SCDD, 0)), 30, 2)) AS SCDD,
       LTRIM(STR(SUM(ISNULL(WWDD, 0)), 30, 2)) AS WWDD,
       LTRIM(STR(SUM(ISNULL(RKHJ, 0)), 30, 2)) AS RKHJ,
       LTRIM(STR(SUM(ISNULL(YDGL, 0)), 30, 2)) AS YDGL,
       LTRIM(STR(SUM(ISNULL(DFHL, 0)), 30, 2)) AS DFHL,
       LTRIM(STR(SUM(ISNULL(DBDF, 0)), 30, 2)) AS DBDF,
       LTRIM(STR(SUM(ISNULL(BLJH, 0)), 30, 2)) AS BLJH,
       LTRIM(STR(SUM(ISNULL(SCWL, 0)), 30, 2)) AS SCWL,
       LTRIM(STR(SUM(ISNULL(WWWL, 0)), 30, 2)) AS WWWL,
       LTRIM(STR(SUM(ISNULL(CKHJ, 0)), 30, 2)) AS CKHJ,
       LTRIM(STR(SUM(BHGP), 30, 2)) AS BHGP,
       LTRIM(STR(
                    SUM(ISNULL(
                                  (ISNULL(XCL, 0) - ISNULL(DJL, 0) + ISNULL(YQGL, 0) + ISNULL(DDZT, 0)
                                   + ISNULL(DHDJ, 0) + ISNULL(DBZT, 0) + ISNULL(SCDD, 0) - ISNULL(SCWL, 0)
                                   + ISNULL(WWDD, 0) - ISNULL(WWWL, 0) - ISNULL(YDGL, 0) - ISNULL(DFHL, 0)
                                   - ISNULL(DBDF, 0) - ISNULL(BLJH, 0)
                                  ),
                                  0
                              )
                       ),
                    30,
                    2
                )
            ) AS KYL,
       LTRIM(STR(MAX(ISNULL(iSafeNum, 0)), 30, 2)) AS iSafeNum,
       LTRIM(STR(
                    CASE
                        WHEN MAX(ISNULL(iSafeNum, 0))
                             - SUM(ISNULL(
                                             (ISNULL(XCL, 0) - ISNULL(DJL, 0) + ISNULL(YQGL, 0) + ISNULL(DDZT, 0)
                                              + ISNULL(DHDJ, 0) + ISNULL(DBZT, 0) + ISNULL(SCDD, 0) - ISNULL(SCWL, 0)
                                              + ISNULL(WWDD, 0) - ISNULL(WWWL, 0) - ISNULL(YDGL, 0) - ISNULL(DFHL, 0)
                                              - ISNULL(DBDF, 0) - ISNULL(BLJH, 0)
                                             ),
                                             0
                                         )
                                  ) > 0 THEN
                            MAX(ISNULL(iSafeNum, 0))
                            - SUM(ISNULL(
                                            (ISNULL(XCL, 0) - ISNULL(DJL, 0) + ISNULL(YQGL, 0) + ISNULL(DDZT, 0)
                                             + ISNULL(DHDJ, 0) + ISNULL(DBZT, 0) + ISNULL(SCDD, 0) - ISNULL(SCWL, 0)
                                             + ISNULL(WWDD, 0) - ISNULL(WWWL, 0) - ISNULL(YDGL, 0) - ISNULL(DFHL, 0)
                                             - ISNULL(DBDF, 0) - ISNULL(BLJH, 0)
                                            ),
                                            0
                                        )
                                 )
                        ELSE
                            NULL
                    END,
                    30,
                    2
                )
            ) AS DYAQL,
       LTRIM(STR(MAX(ISNULL(iTopSum, 0)), 30, 2)) AS iTopSum,
       LTRIM(STR(
                    CASE
                        WHEN SUM(ISNULL(
                                           (ISNULL(XCL, 0) - ISNULL(DJL, 0) + ISNULL(YQGL, 0) + ISNULL(DDZT, 0)
                                            + ISNULL(DHDJ, 0) + ISNULL(DBZT, 0) + ISNULL(SCDD, 0) - ISNULL(SCWL, 0)
                                            + ISNULL(WWDD, 0) - ISNULL(WWWL, 0) - ISNULL(YDGL, 0) - ISNULL(DFHL, 0)
                                            - ISNULL(DBDF, 0) - ISNULL(BLJH, 0)
                                           ),
                                           0
                                       )
                                ) - MAX(ISNULL(iTopSum, 0)) > 0 THEN
                            SUM(ISNULL(
                                          (ISNULL(XCL, 0) - ISNULL(DJL, 0) + ISNULL(YQGL, 0) + ISNULL(DDZT, 0)
                                           + ISNULL(DHDJ, 0) + ISNULL(DBZT, 0) + ISNULL(SCDD, 0) - ISNULL(SCWL, 0)
                                           + ISNULL(WWDD, 0) - ISNULL(WWWL, 0) - ISNULL(YDGL, 0) - ISNULL(DFHL, 0)
                                           - ISNULL(DBDF, 0) - ISNULL(BLJH, 0)
                                          ),
                                          0
                                      )
                               ) - MAX(ISNULL(iTopSum, 0))
                        ELSE
                            NULL
                    END,
                    30,
                    2
                )
            ) AS CCL,
       LTRIM(STR(MAX(ISNULL(iLowSum, 0)), 30, 2)) AS iLowSum,
       LTRIM(STR(
                    CASE
                        WHEN MAX(ISNULL(iLowSum, 0))
                             - SUM(ISNULL(
                                             (ISNULL(XCL, 0) - ISNULL(DJL, 0) + ISNULL(YQGL, 0) + ISNULL(DDZT, 0)
                                              + ISNULL(DHDJ, 0) + ISNULL(DBZT, 0) + ISNULL(SCDD, 0) - ISNULL(SCWL, 0)
                                              + ISNULL(WWDD, 0) - ISNULL(WWWL, 0) - ISNULL(YDGL, 0) - ISNULL(DFHL, 0)
                                              - ISNULL(DBDF, 0) - ISNULL(BLJH, 0)
                                             ),
                                             0
                                         )
                                  ) > 0 THEN
                            MAX(ISNULL(iLowSum, 0))
                            - SUM(ISNULL(
                                            (ISNULL(XCL, 0) - ISNULL(DJL, 0) + ISNULL(YQGL, 0) + ISNULL(DDZT, 0)
                                             + ISNULL(DHDJ, 0) + ISNULL(DBZT, 0) + ISNULL(SCDD, 0) - ISNULL(SCWL, 0)
                                             + ISNULL(WWDD, 0) - ISNULL(WWWL, 0) - ISNULL(YDGL, 0) - ISNULL(DFHL, 0)
                                             - ISNULL(DBDF, 0) - ISNULL(BLJH, 0)
                                            ),
                                            0
                                        )
                                 )
                        ELSE
                            NULL
                    END,
                    30,
                    2
                )
            ) AS DQL,
       cInvDefine1,
       cInvDefine2,
       cInvDefine3,
       cInvDefine4,
       cInvDefine5,
       cInvDefine6,
       cInvDefine7,
       cInvDefine8,
       cInvDefine9,
       cInvDefine10,
       cInvDefine11,
       cInvDefine12,
       CAST(cInvDefine13 AS DECIMAL(30, 6)) AS cinvdefine13,
       CAST(cInvDefine14 AS DECIMAL(30, 6)) AS cinvdefine14,
       CONVERT(CHAR(10), cInvDefine15, 121) AS cinvdefine15,
       CONVERT(CHAR(10), cInvDefine16, 121) AS cinvdefine16,
       (
           SELECT EnumName
           FROM v_aa_enum
           WHERE EnumCode = ISNULL(isotype, 0)
                 AND EnumType = N'ST.Sotype'
       ) AS isotypename,
       ISNULL(csocode, N'') AS csocode,
       ISNULL(isoseq, N'') AS isoseq
FROM #TempKCView_4
GROUP BY cInvCode,
         cInvName,
         cInvAddCode,
         cInvStd,
         cComUnitName,
         cInvDefine1,
         cInvDefine2,
         cInvDefine3,
         cInvDefine4,
         cInvDefine5,
         cInvDefine6,
         cInvDefine7,
         cInvDefine8,
         cInvDefine9,
         cInvDefine10,
         cInvDefine11,
         cInvDefine12,
         CAST(cinvdefine13 AS DECIMAL(30, 6)),
         CAST(cinvdefine14 AS DECIMAL(30, 6)),
         CONVERT(CHAR(10), cinvdefine15, 121),
         CONVERT(CHAR(10), cinvdefine16, 121),
         ISNULL(isotype, 0),
         ISNULL(csocode, N''),
         ISNULL(isoseq, N'')
HAVING (
           SUM(ISNULL(XCL, 0)) <> 0
           OR SUM(ISNULL(DJL, 0)) <> 0
           OR SUM(ISNULL(YQGL, 0)) <> 0
           OR SUM(ISNULL(DDZT, 0)) <> 0
           OR SUM(ISNULL(DHDJ, 0)) <> 0
           OR SUM(ISNULL(DBZT, 0)) <> 0
           OR SUM(ISNULL(SCDD, 0)) <> 0
           OR SUM(ISNULL(SCWL, 0)) <> 0
           OR SUM(ISNULL(WWDD, 0)) <> 0
           OR SUM(ISNULL(WWWL, 0)) <> 0
           OR SUM(ISNULL(YDGL, 0)) <> 0
           OR SUM(ISNULL(DFHL, 0)) <> 0
           OR SUM(ISNULL(DBDF, 0)) <> 0
           OR SUM(ISNULL(BHGP, 0)) <> 0
           OR SUM(ISNULL(BLJH, 0)) <> 0
       )
ORDER BY cInvCode,
         ISNULL(isotype, 0),
         ISNULL(csocode, N''),
         ISNULL(isoseq, N'')
--DROP TABLE #TempKCView_1
--DROP TABLE #TempKCView_2
--DROP TABLE #TempKCView_3
--DROP TABLE #TempKCView_4
--DROP TABLE #TempKCView_6
 

你可能感兴趣的:(用友,数据库,sql)