-
Notifications
You must be signed in to change notification settings - Fork 0
/
TR_TRANSFER_MONEY_TO_SHOPS.txt
79 lines (59 loc) · 2.02 KB
/
TR_TRANSFER_MONEY_TO_SHOPS.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
create trigger TR_TRANSFER_MONEY_TO_SHOPS
on Orders
for update
as
begin
if( UPDATE(receivedTime) )
begin
declare @kursor cursor
declare @recTime datetime
declare @sentTime datetime
declare @daysA int
declare @daysB int
declare @amount int
declare @IdO int
set @kursor = cursor for
select receivedTime, IdO, sentTime, daysA, daysB
from inserted
open @kursor
fetch next from @kursor
into @recTime, @IdO, @sentTime, @daysA, @daysB
while @@FETCH_STATUS = 0
begin
declare @kursor1 cursor
declare @IdS int
set @kursor1 = cursor for
select distinct Article.IdS from Article, inOrder
where Article.IdA = inOrder.IdA and inOrder.IdO = @IdO
open @kursor1
fetch next from @kursor1
into @IdS
while @@FETCH_STATUS = 0
begin
set @amount = (select 0.95 * sum(Article.Price * inOrder.Count * (1 - 1.0 * Shop.Discount / 100.0))
from Article, inOrder, Shop
where inOrder.IdO = @IdO and inOrder.IdA = Article.IdA and Article.IdS = Shop.IdS and Shop.IdS = @IdS)
insert into Transactions(IdO, IdS, IdB, Amount, Time) values (@IdO, @IdS, NULL, @amount, DATEADD(d, @daysA + @daysB, @sentTime))
fetch next from @kursor1
into @IdS
end
close @kursor1
deallocate @kursor1
set @amount = (select sum(Amount) from Transactions where IdB = (select IdB from Orders where IdO = @IdO) and Time < @recTime and Time >= dateadd(d, -30, @recTime))
if @amount > 10000
begin
insert into Profit(Amount) values ((select 0.03 * sum(Article.Price * inOrder.Count * (1 - 1.0 * Shop.Discount / 100.0))
from Article, inOrder, Shop where inOrder.IdO = @IdO and inOrder.IdA = Article.IdA and Article.IdS = Shop.IdS))
end
else
begin
insert into Profit(Amount) values ((select 0.05 * sum(Article.Price * inOrder.Count * (1 - 1.0 * Shop.Discount / 100.0))
from Article, inOrder, Shop where inOrder.IdO = @IdO and inOrder.IdA = Article.IdA and Article.IdS = Shop.IdS))
end
fetch next from @kursor
into @recTime, @IdO, @sentTime, @daysA, @daysB
end
close @kursor
deallocate @kursor
end
end