SqlReports

+

3/24/2021 2:32:43 PM

Details

diff --git a/GetShopsListByCompanies.sql b/GetShopsListByCompanies.sql
index 9e99b68..1b6640a 100644
--- a/GetShopsListByCompanies.sql
+++ b/GetShopsListByCompanies.sql
@@ -48,9 +48,18 @@ begin
 
 	from dbo.Shops s with (nolock)
 		inner join @CompaniesTable ct on ct.CompanyID = s.CompanyId
-		inner join dbo.Terminals t with (nolock) on 
+		left join dbo.Terminals t with (nolock) on 
 				t.ShopId = s.Id
 			and isnull(t.HostName, '') <> ''
+	where
+			isnull(t.HostName, '') <> ''
+		or	isnull(s.ExternalId, '') <> ''
 
+	order by
+		case 
+			when isnull(s.Name,'') = '' 
+				 then '(ShopId = ' + convert(nvarchar(16), s.Id) + ')' 
+			 else s.Name 
+		end
 	
 end
\ No newline at end of file

ShopSales.sql 13(+12 -1)

diff --git a/ShopSales.sql b/ShopSales.sql
index ee00a73..1653fe4 100644
--- a/ShopSales.sql
+++ b/ShopSales.sql
@@ -27,8 +27,17 @@ begin
 --set @CompanyID = 1
 
 	
+	declare @CatalogID int
+
 	declare @LastEncashmentsList table (TerminalID nvarchar(256), ShopID Int, ToSessionId Int, LastEncashmentDate DateTime)
 	
+	select top 1
+		@CatalogID = c.ProductCatalogId
+	
+	from dbo.Companies c with (nolock)
+	where 
+			c.Id = @CompanyID
+	
 	insert into @LastEncashmentsList
 		select 
 			t.Id as TerminalID,
@@ -116,7 +125,9 @@ begin
 			Sum (s.Summ) as TotalSumm
 	
 		from @Sales s
-			inner join dbo.Products p with (nolock) on p.Id = s.ProductID
+			inner join dbo.Products p with (nolock) on 
+					p.Code = s.ProductID
+				and p.CatalogId = @CatalogID
 		
 		group by s.ShopID
 	
diff --git a/TotalRevenueByCategories.sql b/TotalRevenueByCategories.sql
index bef5f7c..a10f921 100644
--- a/TotalRevenueByCategories.sql
+++ b/TotalRevenueByCategories.sql
@@ -61,18 +61,21 @@ begin
 	where
 		pt.ID = @PeriodID
 
-	declare @TerminalsList table (TerminalID nvarchar(256), ShopID int, CompanyID int)
+	declare @TerminalsList table (TerminalID nvarchar(256), ShopID int, CompanyID int, CatalogId int)
 
 	insert into @TerminalsList
 		select
 			t.Id as TerminalID,
 			t.ShopID as ShopID,
-			s.CompanyID as CompanyID
+			s.CompanyID as CompanyID,
+			c.ProductCatalogId as CatalogId
 
 		from dbo.Terminals t with (nolock)
 			inner join dbo.Shops s with (nolock) on 
 					s.Id = t.ShopId
 				and isnull(s.CompanyId, 0) <> 0
+			
+			inner join dbo.Companies c on c.Id = s.CompanyId
 
 	
 	declare @Sales table (
@@ -104,8 +107,10 @@ begin
 				sl.TerminalId = tl.TerminalID
 			and sl.SessionId = ss.SessionId
 
-		left join dbo.Products p with (nolock) on p.Id = sl.ProductId
-			
+		left join dbo.Products p with (nolock) on 
+				p.Code = sl.ProductId
+			and p.CatalogId = tl.CatalogId
+		
 		group by
 			tl.CompanyID,
 			tl.ShopID,