-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateOffer.sql
More file actions
79 lines (60 loc) · 1.58 KB
/
Copy pathcreateOffer.sql
File metadata and controls
79 lines (60 loc) · 1.58 KB
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
DROP TRIGGER IF EXISTS [dbo].TR_TransportOffer;
GO
CREATE TRIGGER TR_TransportOffer
ON [dbo].[Package]
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @IdP int;
DECLARE @IdStartAddress int;
DECLARE @IdEndAddress int;
DECLARE @PackageType int;
DECLARE @Weight decimal(10, 2);
DECLARE @StartX int;
DECLARE @StartY int;
DECLARE @EndX int;
DECLARE @EndY int;
DECLARE @EuclideanDistance decimal(10, 3);
DECLARE @BasePrice int;
DECLARE @PricePerKg int;
DECLARE @cursor CURSOR
SET @cursor = CURSOR FOR
SELECT IdP, IdStartAddress, IdEndAddress, PackageType, Weight
FROM inserted
OPEN @cursor
FETCH NEXT FROM @cursor
INTO @IdP, @IdStartAddress, @IdEndAddress, @PackageType, @Weight
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @StartX = Xcoord, @StartY = Ycoord
FROM [dbo].[Address]
WHERE IdA = @IdStartAddress
SELECT @EndX = Xcoord, @EndY = Ycoord
FROM [dbo].[Address]
WHERE IdA = @IdEndAddress
SET @EuclideanDistance = SQRT(POWER(@StartX - @EndX, 2)+ POWER(@StartY - @EndY, 2))
IF @PackageType = 0 BEGIN
SET @BasePrice = 115;
SET @PricePerKg = 0;
END
IF @PackageType = 1 BEGIN
SET @BasePrice = 175;
SET @PricePerKg = 100;
END
IF @PackageType = 2 BEGIN
SET @BasePrice = 250;
SET @PricePerKg = 100;
END
IF @PackageType = 3 BEGIN
SET @BasePrice = 350;
SET @PricePerKg = 500;
END
UPDATE [dbo].[Package]
SET Price = (@BasePrice + @Weight * @PricePerKg) * @EuclideanDistance
WHERE IdP = @IdP
FETCH NEXT FROM @cursor
INTO @IdP, @IdStartAddress, @IdEndAddress, @PackageType, @Weight
END
CLOSE @cursor
DEALLOCATE @cursor
END