SqlReports

*

4/25/2019 6:13:35 PM

Changes

Details

diff --git a/RevenueComparation.sql b/RevenueComparation.sql
index 5bbed27..9ea63f7 100644
--- a/RevenueComparation.sql
+++ b/RevenueComparation.sql
@@ -1,4 +1,3 @@
--- RevenueComparation
 
 set noexec off
 go
@@ -19,9 +18,9 @@ set noexec off
 go
 alter procedure dbo.RevenueComparation
 (
-	 @CompanyID Int,
-	 @PeriodTypeID Int,
-	 @PeriodID Int,
+	 @CompanyID int,
+	 @PeriodTypeID int,
+	 @PeriodID int,
 	 @ActualPeriodDateFrom DateTime = null,
 	 @ActualPeriodDateTo DateTime = null,
 	 @LastPeriodDateFrom DateTime = null,
@@ -31,9 +30,9 @@ as
 
 begin
 
---declare @CompanyID Int
---declare @PeriodTypeID Int
---declare @PeriodID Int
+--declare @CompanyID int
+--declare @PeriodTypeID int
+--declare @PeriodID int
 --declare @ActualPeriodDateFrom as DateTime
 --declare @ActualPeriodDateTo as DateTime
 --declare @LastPeriodDateFrom as DateTime
@@ -104,7 +103,7 @@ begin
 
 	end
 
-	declare @TerminalsList table (TerminalID nvarchar(256), ShopID Int)
+	declare @TerminalsList table (TerminalID nvarchar(256), ShopID int)
 
 	insert into @TerminalsList
 		select
@@ -117,7 +116,7 @@ begin
 				and s.CompanyId = @CompanyID 
 
 	
-	declare @SalesTotals table (ShopID Int, LastPeriodSumm decimal(15, 2), ActualPeriodSumm decimal(15, 2))
+	declare @SalesTotals table (ShopID int, LastPeriodSumm decimal(15, 2), ActualPeriodSumm decimal(15, 2), SalesLastPeriodCount int, SalesActualPeriodCount int)
 
 	insert into @SalesTotals
 		select
@@ -141,9 +140,29 @@ begin
 					
 					else 0
 				end
-			) as ActualPeriodSumm
+			) as ActualPeriodSumm,
 
 
+			Sum
+			(
+				case
+					when ss.CloseTime between @LastPeriodDateFrom and @LastPeriodDateTo
+						then sl.Count
+
+					else 0
+				end
+			) as SalesLastPeriodCount,
+			
+			Sum
+			(
+				case
+					when ss.CloseTime between @ActualPeriodDateFrom and @ActualPeriodDateTo
+						then sl.Count
+					
+					else 0
+				end
+			) as SalesActualPeriodCount
+
 		from @TerminalsList tl 
 			inner join dbo.Sessions ss with (nolock) on
 					ss.TerminalId = tl.TerminalID
@@ -160,31 +179,51 @@ begin
 		group by tl.ShopID
 
 
-	declare @WithdrawsTotals table (ShopID Int, LastPeriodSumm Int, ActualPeriodSumm Int)
+	declare @WithdrawsLoadingsTotals table (ShopID int, WithdrawsLastPeriodCount int, WithdrawsActualPeriodCount int, LoadingsLastPeriodCount int, LoadingsActualPeriodCount int)
 
-	insert into @WithdrawsTotals
+	insert into @WithdrawsLoadingsTotals
 		select
 			tl.ShopID,
 			
 			Sum
 			(
 				case
-					when pr.CreatedOn between @LastPeriodDateFrom and @LastPeriodDateTo
+					when prc.Type = 0 and pr.CreatedOn between @LastPeriodDateFrom and @LastPeriodDateTo
 						then prc.Value
 
 					else 0
 				end
-			) as LastPeriodSumm,
+			) as WithdrawsLastPeriodCount,
 			
 			Sum
 			(
 				case
-					when pr.CreatedOn between @ActualPeriodDateFrom and @ActualPeriodDateTo
+					when prc.Type = 0 and pr.CreatedOn between @ActualPeriodDateFrom and @ActualPeriodDateTo
 						then prc.Value
 					
 					else 0
 				end
-			) as ActualPeriodSumm
+			) as WithdrawsActualPeriodCount,
+
+			Sum
+			(
+				case
+					when prc.Type = 1 and pr.CreatedOn between @LastPeriodDateFrom and @LastPeriodDateTo
+						then prc.Value
+
+					else 0
+				end
+			) as LoadingsLastPeriodCount,
+			
+			Sum
+			(
+				case
+					when prc.Type = 1 and pr.CreatedOn between @ActualPeriodDateFrom and @ActualPeriodDateTo
+						then prc.Value
+					
+					else 0
+				end
+			) as LoadingsActualPeriodCount
 		
 		from @TerminalsList tl
 			inner join dbo.ProductReports pr with (nolock) on
@@ -197,16 +236,15 @@ begin
 
 			inner join dbo.ProductReportCounters prc with (nolock) on
 					prc.ReportId = pr.Id
-				and	prc.Type = 0
+				and	prc.Type in (0, 1)
 		
 		group by tl.ShopID
 
 	select
+		gtl.ShopID, 
 		isnull(s.Description, s.Name) as ShopName,
 		isnull(st.LastPeriodSumm, 0) as LastPeriodSumm,
-		isnull(wt.LastPeriodSumm, 0) as WithdrawsLastPeriodSumm,
 		isnull(st.ActualPeriodSumm, 0) as ActualPeriodSumm,
-		isnull(wt.ActualPeriodSumm, 0) as WithdrawsActualPeriodSumm,
 
 		isnull(st.ActualPeriodSumm, 0) - isnull(st.LastPeriodSumm, 0) as SummDifference,
 
@@ -216,7 +254,17 @@ begin
 					Cast(isnull(st.ActualPeriodSumm, 0)/isnull(st.LastPeriodSumm, 0) * 100  - 100 as decimal (15, 2))
 			
 			else 0
-		end as SummDifferenceRate 
+		end as SummDifferenceRate,
+		
+		isnull(wlt.LoadingsLastPeriodCount, 0) as LoadingsLastPeriodCount,
+		isnull(wlt.LoadingsActualPeriodCount, 0) as LoadingsActualPeriodCount,
+		
+		isnull(st.SalesLastPeriodCount, 0) as SalesLastPeriodCount,
+		isnull(st.SalesActualPeriodCount, 0) as SalesActualPeriodCount,
+
+		isnull(wlt.WithdrawsLastPeriodCount, 0) as WithdrawsLastPeriodCount,
+		isnull(wlt.WithdrawsActualPeriodCount, 0) as WithdrawsActualPeriodCount
+		
 	
 	from (
 			select 
@@ -228,7 +276,7 @@ begin
 		
 			inner join dbo.Shops s with (nolock) on s.Id = gtl.ShopID
 			left join @SalesTotals st on st.ShopID = gtl.ShopID
-			left join @WithdrawsTotals wt on wt.ShopID = gtl.ShopID
+			left join @WithdrawsLoadingsTotals wlt on wlt.ShopID = gtl.ShopID
 end
 
 go