Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 10.38 KB | None | 0 0
  1. IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[PV_IRWebGetOnSiteManagerEquipmentForJob]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
  2. DROP PROCEDURE [dbo].PV_IRWebGetOnSiteManagerEquipmentForJob
  3. GO
  4. PRINT 'Creating procedure PV_IRWebGetOnSiteManagerEquipmentForJob';
  5. GO
  6. CREATE PROCEDURE  [dbo].PV_IRWebGetOnSiteManagerEquipmentForJob
  7.     @ClientID BIGINT,
  8.     @JobID BIGINT,
  9.     @SortField VARCHAR(100) = 'ManufacturerModel',
  10.     @SortDirection VARCHAR(5) = 'ASC',
  11.     @PageSize INT = 10,
  12.     @PageIndex BIGINT = 1,
  13.     @RecordCount BIGINT OUTPUT
  14. AS
  15. BEGIN
  16.     CREATE TABLE #Results
  17.     (
  18.           ID BIGINT
  19.         , EquipmentId BIGINT
  20.         , ActualEquipmentId BIGINT
  21.         , ManufacturerModel NVARCHAR(400)
  22.         , Manufacturer NVARCHAR(200)
  23.         , Model NVARCHAR(200)
  24.         , Barcode NVARCHAR(200)
  25.         , Serial NVARCHAR(200)
  26.         , ModelId BIGINT
  27.         , Requested INT
  28.         , Needed INT
  29.         , Assigned INT
  30.         , Unhandled INT
  31.         , OnStock INT
  32.         , [Status] TINYINT
  33.         , IsDeleted BIT
  34.         , IsAsset BIT
  35.         , RowId INT
  36.     )
  37.    
  38.     IF @SortField = ''
  39.     BEGIN
  40.         SET @SortField = 'ManufacturerModel'
  41.     END
  42.     IF @SortDirection = ''
  43.     BEGIN
  44.         SET @SortDirection = 'ASC'
  45.     END
  46.  
  47.     -- get main rows (model)
  48.     INSERT INTO #Results
  49.     SELECT ID, EquipmentId, NULL as ActualEquipmentId, ManufacturerModel, Manufacturer, Model, Barcode, Serial, ModelId, Requested, Needed, Assigned , Unhandled, OnStock, [Status], IsDeleted, IsAsset,
  50.             (ROW_NUMBER() OVER (ORDER BY
  51.                 CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'ASC' THEN ManufacturerModel END ASC,
  52.                 CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'DESC' THEN ManufacturerModel END DESC
  53.             )) as RowId
  54.     FROM
  55.     (
  56.         SELECT jte.ID
  57.             , jte.IREquipmentD as EquipmentId
  58.             , m.Manufacturer + N' ' + m.Model as ManufacturerModel
  59.             , m.Manufacturer
  60.             , m.Model
  61.             , '' as Barcode
  62.             , '' as Serial
  63.             , jte.IRModelID as ModelId
  64.             , jte.InProcessCount as Requested
  65.             , jte.NeededCount as Needed
  66.             , 0 as Assigned
  67.             , jte.UnhandledCount as Unhandled
  68.             , jte.OnStock as OnStock
  69.             , jte.[Status] as [Status]
  70.             , jte.IsDeleted
  71.             , 0 as IsAsset
  72.         FROM IRJobTechnician as jt INNER JOIN
  73.             IRJob as j ON jt.IRJobID = j.ID INNER JOIN
  74.             IRJobTechnicianEquipment as jte ON jte.IRJobTechnicianID = jt.ID
  75.             OUTER APPLY dbo.fn_GetModelsAndModelClient(jte.IRModelID, NULL, @ClientID, 0, NULL, NULL) as m
  76.         WHERE jt.IsOnSiteManager = 1
  77.             AND jt.IRJobID = @JobID
  78.             AND jt.IsDeleted = 0
  79.             AND jte.IsDeleted = 0
  80.     ) r
  81.  
  82.     DECLARE @TechnicianEquipments TABLE
  83.     (
  84.         EquipmentID BIGINT
  85.     )
  86.     --INSERT INTO @TechnicianEquipments
  87.     --SELECT e.ID
  88.     --FROM IREquipment as e
  89.     --  INNER JOIN IRJobTechnician as jt ON e.IRUserIDTechnician = jt.IRUserID
  90.     --WHERE (e.IRJobID IS NULL
  91.     --      OR e.IRJobID <> @JobID)
  92.     --  AND jt.IRJobID = @JobID
  93.     --  AND jt.IsDeleted = 0
  94.     --  AND jt.IsOnSiteManager = 0
  95.     --  AND e.IsOnStock = 1
  96.     --  AND e.IsDropped = 0
  97.     --  AND e.[Status] = 1 /* Normal */
  98.     --  --AND dbo.fn_GetEquipmentStatus(e.ID) <> 2
  99.  
  100.     INSERT INTO @TechnicianEquipments
  101.     SELECT e.ID
  102.     FROM IREquipment as e
  103.         INNER JOIN IRJobTechnician as jt ON e.IRUserIDTechnician = jt.IRUserID
  104.     WHERE (e.IRJobID IS NULL
  105.             OR e.IRJobID <> @JobID)
  106.         AND jt.IRJobID = @JobID
  107.         AND jt.IsDeleted = 0
  108.         AND jt.IsOnSiteManager = 0
  109.         AND e.IsOnStock = 1
  110.         AND e.IsDropped = 0
  111.         AND e.[Status] = 1 /* Normal */
  112.          --jest przypisany do innego joba, gdzie project leadem jest jeden z techników na tym jobie
  113.          -- takich equipmentów nie wyświetlamy.
  114.          --Muszą być bezpośrednio przypisane albo do technika albo do tego konkretnego joba
  115.          -- to jak rozumiem i zwalcza ten przypadek:
  116.          -- equipment jest przypisany do innego joba, użtykownik nie jest przypisany do joba jako technik
  117.         AND jt.IRUserID NOT IN (SELECT jt1.IRUserID /*not add equipment have project lead*/
  118.                                 FROM IRJobTechnician as jt1
  119.                                 WHERE jt1.IRJobID = @JobID
  120.                                     AND jt1.IsDeleted = 0
  121.                                     AND jt1.IsOnSiteManager = 1)
  122.         --AND dbo.fn_GetEquipmentStatus(e.ID) <> 2
  123.  
  124.     -- get subrows (equipment)
  125.     INSERT INTO #Results
  126.     SELECT ID, EquipmentId, EquipmentId as ActualEquipmentId, ManufacturerModel, Manufacturer, Model, Barcode, Serial, ModelId, Requested, Needed, Assigned , Unhandled, OnStock, [Status], IsDeleted, IsAsset,
  127.             (ROW_NUMBER() OVER (ORDER BY
  128.                 CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'ASC' THEN ManufacturerModel END ASC,
  129.                 CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'DESC' THEN ManufacturerModel END DESC
  130.             )) as RowId
  131.     FROM
  132.     (
  133.         SELECT rr.ID as ID
  134.             , e.ID as EquipmentId
  135.             , m.Manufacturer + N' ' + m.Model + N' ' + e.BarCode + N' ' + e.Serial as ManufacturerModel
  136.             , m.Manufacturer as Manufacturer
  137.             , m.Model as Model
  138.             , e.BarCode as Barcode
  139.             , e.Serial as Serial
  140.             , e.IRModelID as ModelId
  141.             , 0 as Requested
  142.             , 0 as Needed
  143.             , 0 as Assigned
  144.             , 0 as Unhandled
  145.             , 0 as OnStock
  146.             , 0 as [Status]
  147.             , 0 as IsDeleted
  148.             , 1 as IsAsset
  149.             , dbo.fn_GetEquipmentStatus(e.ID) as EquipmentStatus
  150.         FROM IREquipment as e
  151.             LEFT JOIN #Results as rr ON rr.ModelId = e.IRModelID
  152.             OUTER APPLY dbo.fn_GetModelsAndModelClient(e.IRModelID, NULL, @ClientID, 0, NULL, NULL) as m
  153.         WHERE e.IRJobID = @JobID
  154.             AND (rr.ID IS NULL OR rr.IsDeleted = 0)
  155.  
  156.         UNION ALL
  157.  
  158.         SELECT rr.ID as ID
  159.             , e.ID as EquipmentId
  160.             , m.Manufacturer + N' ' + m.Model + N' ' + e.BarCode + N' ' + e.Serial as ManufacturerModel
  161.             , m.Manufacturer as Manufacturer
  162.             , m.Model as Model
  163.             , e.BarCode as Barcode
  164.             , e.Serial as Serial
  165.             , e.IRModelID as ModelId
  166.             , 0 as Requested
  167.             , 0 as Needed
  168.             , 0 as Assigned
  169.             , 0 as Unhandled
  170.             , 0 as OnStock
  171.             , 0 as [Status]
  172.             , 0 as IsDeleted
  173.             , 1 as IsAsset
  174.             , dbo.fn_GetEquipmentStatus(e.ID) as EquipmentStatus
  175.         FROM IREquipment as e
  176.             INNER JOIN @TechnicianEquipments as te ON e.ID = te.EquipmentID
  177.             LEFT JOIN #Results as rr ON rr.ModelId = e.IRModelID
  178.             OUTER APPLY dbo.fn_GetModelsAndModelClient(e.IRModelID, NULL, @ClientID, 0, NULL, NULL) as m
  179.         WHERE (e.IRJobID IS NULL
  180.                 OR e.IRJobID <> @JobID)
  181.             AND (rr.ID IS NULL OR rr.IsDeleted = 0)
  182.     ) r
  183.     WHERE r.EquipmentStatus NOT IN (4 /* Rental Return */, 6 /* Sold */, 7 /* Disposal */, 3 /* Service */)
  184.  
  185.     -- insert main rows (model) for existing equipment but not existing JobTechnicianEquipment
  186.     INSERT INTO #Results
  187.     SELECT ID, EquipmentId, NULL as ActualEquipmentId, ManufacturerModel, Manufacturer, Model, Barcode, Serial, ModelId, Requested, Needed, Assigned , Unhandled, OnStock, [Status], IsDeleted, IsAsset,
  188.             (ROW_NUMBER() OVER (ORDER BY
  189.                 CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'ASC' THEN ManufacturerModel END ASC,
  190.                 CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'DESC' THEN ManufacturerModel END DESC
  191.             )) as RowId
  192.     FROM
  193.     (
  194.         SELECT rr.EquipmentId as ID
  195.             , rr.EquipmentId as EquipmentId
  196.             , rr.Manufacturer + N' ' + rr.Model as ManufacturerModel
  197.             , rr.Manufacturer as Manufacturer
  198.             , rr.Model as Model
  199.             , '' as Barcode
  200.             , '' as Serial
  201.             , rr.ModelId
  202.             , 0 as Requested
  203.             , 0 as Needed
  204.             , 1 as Assigned
  205.             , 0 as Unhandled
  206.             , 0 as OnStock
  207.             , 0 as [Status]
  208.             , 0 as IsDeleted
  209.             , 0 as IsAsset
  210.         FROM #Results as rr
  211.         WHERE rr.IsAsset = 1
  212.             AND rr.ID IS NULL
  213.     ) r
  214.     -- group all subrows (equipment) to one main row (model)
  215.     DECLARE @ManyEquipments TABLE
  216.     (
  217.         ModelId BIGINT,
  218.         [Count] INT,
  219.         EquipmentId BIGINT
  220.     )
  221.     INSERT INTO @ManyEquipments
  222.     SELECT e.ModelId, COUNT(e.EquipmentId), (SELECT MIN(r.EquipmentId) FROM #Results as r WHERE r.ModelId = e.ModelId AND ID IS NOT NULL) as EquipmentId
  223.     FROM (SELECT *
  224.         FROM #Results
  225.         WHERE EquipmentId > 0
  226.             AND ID IS NULL
  227.     ) e
  228.     GROUP BY e.ModelId
  229.     HAVING COUNT(e.EquipmentId) > 1
  230.  
  231.     -- update subrow.EquipmentId to mainrow.EquipmentId
  232.     UPDATE r
  233.     SET r.EquipmentId = e.EquipmentId
  234.     FROM #Results as r
  235.         INNER JOIN @ManyEquipments as e ON r.ModelId = e.ModelId
  236.     WHERE r.EquipmentId > 0
  237.         AND r.ID IS NULL
  238.  
  239.     -- update mainrow.Assigned
  240.     UPDATE r
  241.     SET r.Assigned = e.[Count]
  242.     FROM #Results as r
  243.         INNER JOIN @ManyEquipments as e ON r.ModelId = e.ModelId
  244.     WHERE r.EquipmentId = e.EquipmentId
  245.         AND r.ID = e.EquipmentId
  246.  
  247.     -- remove mainrows with no subrow
  248.     DELETE r
  249.     FROM #Results as r
  250.     WHERE r.ID IS NOT NULL
  251.         AND r.EquipmentId IS NOT NULL
  252.         AND r.IsAsset = 0
  253.         AND NOT EXISTS (SELECT 1 FROM #Results as rr WHERE rr.EquipmentId > 0 AND rr.ID IS NULL AND rr.EquipmentId = r.ID)
  254.  
  255.        
  256.     -- update Assigned count
  257.     UPDATE rr
  258.     SET rr.Assigned = (SELECT ISNULL(COUNT(rr2.ID), 0) FROM #Results as rr2 WHERE rr2.IsAsset = 1 AND rr2.ID = rr.ID)
  259.     FROM #Results as rr
  260.     WHERE rr.IsAsset = 0
  261.         AND rr.EquipmentId IS NULL
  262.  
  263.      --remove equipment transfered, status = 1 - pending
  264.      --equipment jest przypisany do joba, ale jest też w transferze.
  265.      --takich equipmentów nie pokazujemy w jobie
  266.     DELETE r
  267.     FROM #Results as r
  268.     WHERE r.EquipmentId IN (SELECT rr.EquipmentId
  269.                                 FROM #Results as rr INNER JOIN
  270.                                     IRDocumentLine as dl ON dl.IREquipmentID = rr.ID
  271.                                                             OR dl.IREquipmentID = rr.EquipmentId
  272.                                                             OR dl.IREquipmentID = rr.ActualEquipmentId
  273.                                                             AND dl.IsDeleted = 0 INNER JOIN
  274.                                     IRDocument as d ON d.ID = dl.IRDocumentID  
  275.                                                                 AND d.IsDeleted = 0
  276.                                 WHERE dl.Status = 1 /*1 - pending*/
  277.                                         AND d.[Type] = 12)
  278.  
  279.    
  280.     SELECT @RecordCount = COUNT(1)
  281.     FROM #Results
  282.    
  283.     IF ((((@PageIndex - 1) * @PageSize) + 1) > @RecordCount)
  284.     BEGIN
  285.         SET @PageIndex = 1
  286.     END
  287.  
  288.     SELECT ID, ActualEquipmentId as EquipmentId, ManufacturerModel, Manufacturer, Model, Barcode, Serial, ModelId, Requested, Needed, Assigned, Unhandled, OnStock, [Status], IsDeleted, IsAsset, RowId
  289.     FROM #Results
  290.     --WHERE RowId BETWEEN (((@PageIndex - 1) * @PageSize) + 1) AND (@PageIndex * @PageSize)
  291.     ORDER BY CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'ASC' THEN Manufacturer END ASC,
  292.              CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'ASC' THEN Model END ASC,
  293.              CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'DESC' THEN Manufacturer END DESC,
  294.              CASE WHEN @SortField = 'ManufacturerModel' AND @SortDirection = 'DESC' THEN Model END DESC
  295.         , IsAsset ASC
  296.    
  297.     DROP TABLE #Results
  298.  
  299.     IF @@ERROR<>0 GOTO ERROR_PROC;
  300.  
  301.     --------------------------------------------------------------------------
  302.     -- exit point of the store proc
  303.     RETURN(0);
  304.  
  305.     ERROR_PROC:
  306.     RETURN(99);
  307. END;
  308. GO
  309. -- end PV_IRWebGetOnSiteManagerEquipmentForJob
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement