
                DECLARE @ShouldUpdate AS BIT
                SET @ShouldUpdate = (SELECT UpdateEntityItemUnits FROM tbVendor WITH (NOLOCK) WHERE VendorID = @VendorId)
                IF (@ShouldUpdate = 1)
                BEGIN
                    	      DECLARE @TEMP_UNITS TABLE (ItemID BIGINT, ConversionRate Float, CurrentUnit BIGINT)
                  -- Get the Largest Selected Purchase Unit Conversion Rate.
                  INSERT INTO
                    	        @TEMP_UNITS
                  SELECT
                    	        z.ItemID
                    	        ,MAX(z.fConversionRate) AS ConversionRate
                    	        ,NULL AS CurrentUnit
                  FROM
                  (SELECT
                    	        i.ItemID
                    	        ,vei.PurchaseUnit
                    	        ,u.fConversionRate
                  FROM tbEntityItem AS ei WITH (NOLOCK)
                    	        INNER JOIN tbItem AS i WITH (NOLOCK) ON
                    		        i.ItemID = ei.ItemID
                    	        INNER JOIN tbVendorEntityItem AS vei WITH (NOLOCK) ON
                    		        vei.EntityID = ei.EntityID
                    		        AND vei.ItemID = ei.ItemID
                    	        INNER JOIN tbUnit AS u WITH (NOLOCK) ON
                    		        u.sFromUnit_cd = vei.PurchaseUnit
                    		        AND u.sToUnit_cd = i.InventoryUnit
                  WHERE
                    	        ei.EntityID = @EntityId
                    	        AND vei.VendorID = @VendorId AND
                           ( dbo.fn_IsActiveInDateRange(vei.EntityID,vei.IntroductionDate,
                    	                                    vei.DiscontinueDate,ei.InUse,
                    	                                    dbo.fn_LocalTime(vei.Entityid, GETUTCDATE(), GETDATE()),
                    	                                    vei.DiscontinueDateOverrideEnabled ,vei.DiscontinueDateOverridden )=1)
                    	       
                  UNION
                  SELECT
                    	        i.ItemID
                    	        ,vei.PurchaseUnit
                    	        ,1 AS fConversionRate
                  FROM tbEntityItem AS ei WITH (NOLOCK)
                    	        INNER JOIN tbVendorEntityItem AS vei WITH (NOLOCK) ON
                    		        vei.EntityID = ei.EntityID
                    		        AND vei.ItemID = ei.ItemID
                    	        INNER JOIN tbItem AS i WITH (NOLOCK) ON
                    		        i.ItemID = ei.ItemID
                    		        AND i.InventoryUnit = vei.PurchaseUnit
                  WHERE
                    	        ei.EntityID = @EntityId
                    	        AND vei.VendorID = @VendorId AND
                           ( dbo.fn_IsActiveInDateRange(vei.EntityID,vei.IntroductionDate,
                    	                                    vei.DiscontinueDate,ei.InUse,
                    	                                    dbo.fn_LocalTime(vei.Entityid, GETUTCDATE(), GETDATE()),
                    	                                    vei.DiscontinueDateOverrideEnabled ,vei.DiscontinueDateOverridden )=1)
                    	       ) AS z
                  GROUP BY
                    	        z.ItemID
                    	      -- Get the Corresponding UnitID for the largest conversion rate from selected items. Need to make sure the UnitID found is being used by the Vendor Items selected
                    	      UPDATE
                    		      @TEMP_UNITS
                    	      SET
                    		      CurrentUnit = tbUnit.FromUnit
                    	      FROM @TEMP_UNITS AS u
                    		      INNER JOIN tbItem AS i WITH (NOLOCK) ON
                    			      i.ItemID = u.ItemID
                    		      INNER JOIN tbUnit WITH (NOLOCK) ON
                    			      tbUnit.sToUnit_cd = i.InventoryUnit AND
                    			      tbUnit.fConversionRate = u.ConversionRate
                    		      INNER JOIN tbVendorEntityItem AS vei WITH (NOLOCK) ON
                    			      vei.PurchaseUnit = tbUnit.sFromUnit_cd AND
                    			      vei.ItemID = i.ItemID
                    inner join tbEntityItem  as ei WITH (NOLOCK) ON  
                    			       ei.EntityID=vei.EntityID and
                    			       ei.ItemID=vei.ItemID 
                    	      WHERE
                    		      vei.EntityID = @EntityId AND
                    		      vei.VendorID = @VendorId AND
                           ( dbo.fn_IsActiveInDateRange(vei.EntityID,vei.IntroductionDate,
                    	                                    vei.DiscontinueDate,ei.InUse,
                    	                                    dbo.fn_LocalTime(vei.Entityid, GETUTCDATE(), GETDATE()),
                    	                                    vei.DiscontinueDateOverrideEnabled ,vei.DiscontinueDateOverridden )=1) 
                    		           
                  UPDATE
                    @TEMP_UNITS
                  SET
                    CurrentUnit = uom.UnitOfMeasureID
                  FROM @TEMP_UNITS AS u
                    INNER JOIN tbItem AS i WITH (NOLOCK) ON
                      i.ItemID = u.ItemID
                    INNER JOIN tbVendorEntityItem AS vei WITH (NOLOCK) ON
                      vei.PurchaseUnit = i.InventoryUnit AND
                      vei.ItemID = i.ItemID
                    INNER JOIN tbUnitOfMeasure AS uom WITH (NOLOCK) ON
                      uom.UomName = i.InventoryUnit
                   inner join tbEntityItem  as ei WITH (NOLOCK) ON  
                    			       ei.EntityID=vei.EntityID and
                    			       ei.ItemID=vei.ItemID 
                  WHERE
                    vei.EntityID = @EntityId AND
                    vei.VendorID = @VendorId AND
                           ( dbo.fn_IsActiveInDateRange(vei.EntityID,vei.IntroductionDate,
                    	                                    vei.DiscontinueDate,ei.InUse,
                    	                                    dbo.fn_LocalTime(vei.Entityid, GETUTCDATE(), GETDATE()),
                    	                                    vei.DiscontinueDateOverrideEnabled ,vei.DiscontinueDateOverridden )=1) AND
                    u.CurrentUnit IS NULL
                    	      -- Update Items where the largest selected Purchase Unit is not set as the Stocktake Box or the Reporting Unit.
                    	      UPDATE
                    		      tbEntityItem
                    	      SET
                    		      StocktakeUnit1ID = u.CurrentUnit,
                    		      ReportingUnitID = u.CurrentUnit
                    	      FROM tbEntityItem AS ei WITH (NOLOCK)
                    		      INNER JOIN @TEMP_UNITS AS u ON
                    			      u.ItemID = ei.ItemID
                    	      WHERE
                    		      ei.EntityID = @EntityId AND
                    		      ((ei.StocktakeUnit1ID <> u.CurrentUnit OR ei.ReportingUnitID <> u.CurrentUnit) OR
                    		      (ei.StocktakeUnit1ID IS NULL OR ei.ReportingUnitID IS NULL))
                END



