Skip to content

Commit

Permalink
Rename fee table
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Dec 20, 2024
1 parent 99aa780 commit 3785fb1
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions scripts/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE name = 'accounts' AND type = 'U')
-- CONSTRAINT FK_accounts_rooms FOREIGN KEY (room) REFERENCES rooms(room) -- room records are not always available
)

IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE name = 'fee' AND type = 'U')
CREATE TABLE fee (
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE name = 'fees' AND type = 'U')
CREATE TABLE fees (
id BIGINT PRIMARY KEY,
name NVARCHAR(255) COLLATE Vietnamese_100_CS_AS_KS_WS NOT NULL,
lower BIGINT NOT NULL, -- lower = 100 * [amount in VND]
Expand All @@ -67,7 +67,7 @@ IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE name = 'fee' AND type = 'U')
deadline DATE NOT NULL,
description NVARCHAR(max) COLLATE Vietnamese_100_CS_AS_KS_WS,
flags TINYINT NOT NULL,
CONSTRAINT CHECK_fee CHECK (lower <= upper)
CONSTRAINT CHECK_fees CHECK (lower <= upper)
)

IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE name = 'payments' AND type = 'U')
Expand All @@ -77,7 +77,7 @@ IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE name = 'payments' AND type = 'U')
amount BIGINT NOT NULL, -- amount = 100 * [amount in VND]
fee_id BIGINT NOT NULL,
CONSTRAINT FK_payments_rooms FOREIGN KEY (room) REFERENCES rooms(room),
CONSTRAINT FK_payments_fee FOREIGN KEY (fee_id) REFERENCES fee(id),
CONSTRAINT FK_payments_fees FOREIGN KEY (fee_id) REFERENCES fees(id),
CONSTRAINT UQ_payments_room_fee_id UNIQUE (room, fee_id)
)

Expand Down
2 changes: 1 addition & 1 deletion scripts/drop.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DROP TABLE IF EXISTS payments
GO

DROP TABLE IF EXISTS fee
DROP TABLE IF EXISTS fees
GO

DROP TABLE IF EXISTS accounts
Expand Down
2 changes: 1 addition & 1 deletion scripts/procedures/count_fees.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BEGIN
DECLARE @FromId BIGINT = DATEDIFF_BIG(MILLISECOND, @Epoch, @CreatedAfter) << 16
DECLARE @ToId BIGINT = (DATEDIFF_BIG(MILLISECOND, @Epoch, @CreatedBefore) << 16) | 0xFFFF

SELECT COUNT(1) FROM fee
SELECT COUNT(1) FROM fees
WHERE id >= @FromId AND id <= @ToId AND (
@Name IS NULL
OR CHARINDEX(@Name, name) > 0
Expand Down
8 changes: 4 additions & 4 deletions scripts/procedures/count_room_fees.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ BEGIN
DECLARE @ToId BIGINT = (DATEDIFF_BIG(MILLISECOND, @Epoch, @CreatedBefore) << 16) | 0xFFFF

SELECT COUNT(1)
FROM fee
WHERE fee.id >= @FromId AND fee.id <= @ToId AND (
FROM fees
WHERE fees.id >= @FromId AND fees.id <= @ToId AND (
@Paid IS NULL
OR (@Paid = 0 AND NOT EXISTS (SELECT 1 FROM payments WHERE fee_id = fee.id AND room = @Room))
OR (@Paid = 1 AND EXISTS (SELECT 1 FROM payments WHERE fee_id = fee.id AND room = @Room))
OR (@Paid = 0 AND NOT EXISTS (SELECT 1 FROM payments WHERE fee_id = fees.id AND room = @Room))
OR (@Paid = 1 AND EXISTS (SELECT 1 FROM payments WHERE fee_id = fees.id AND room = @Room))
)
END
2 changes: 1 addition & 1 deletion scripts/procedures/create_fee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BEGIN
DECLARE @Id BIGINT
EXECUTE GenerateId @Id = @Id OUTPUT

INSERT INTO fee (
INSERT INTO fees (
id,
name,
lower,
Expand Down
2 changes: 1 addition & 1 deletion scripts/procedures/create_payment.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BEGIN
IF NOT EXISTS (SELECT 1 FROM rooms WHERE room = @Room)
SELECT '01' AS code, 'Invalid room number' AS message

ELSE IF NOT EXISTS (SELECT 1 FROM fee WHERE id = @FeeId)
ELSE IF NOT EXISTS (SELECT 1 FROM fees WHERE id = @FeeId)
SELECT '01' AS code, 'Invalid fee ID' AS message

ELSE IF EXISTS (SELECT 1 FROM payments WHERE room = @Room AND fee_id = @FeeId)
Expand Down
2 changes: 1 addition & 1 deletion scripts/procedures/query_fees.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BEGIN
DECLARE @FromId BIGINT = DATEDIFF_BIG(MILLISECOND, @Epoch, @CreatedAfter) << 16
DECLARE @ToId BIGINT = (DATEDIFF_BIG(MILLISECOND, @Epoch, @CreatedBefore) << 16) | 0xFFFF

SELECT * FROM fee
SELECT * FROM fees
WHERE id >= @FromId AND id <= @ToId AND (
@Name IS NULL
OR CHARINDEX(@Name, name) > 0
Expand Down
32 changes: 16 additions & 16 deletions scripts/procedures/query_room_fees.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ BEGIN
DECLARE @ToId BIGINT = (DATEDIFF_BIG(MILLISECOND, @Epoch, @CreatedBefore) << 16) | 0xFFFF

SELECT
fee.id AS fee_id,
fee.name AS fee_name,
fee.lower AS fee_lower,
fee.upper AS fee_upper,
fee.per_area AS fee_per_area,
fee.per_motorbike AS fee_per_motorbike,
fee.per_car AS fee_per_car,
fee.deadline AS fee_deadline,
fee.description AS fee_description,
fee.flags AS fee_flags,
fee.lower + rooms.area / 100 * fee.per_area + fee.per_motorbike * rooms.motorbike + fee.per_car * rooms.car AS lower_bound,
fee.upper + rooms.area / 100 * fee.per_area + fee.per_motorbike * rooms.motorbike + fee.per_car * rooms.car AS upper_bound,
fees.id AS fee_id,
fees.name AS fee_name,
fees.lower AS fee_lower,
fees.upper AS fee_upper,
fees.per_area AS fee_per_area,
fees.per_motorbike AS fee_per_motorbike,
fees.per_car AS fee_per_car,
fees.deadline AS fee_deadline,
fees.description AS fee_description,
fees.flags AS fee_flags,
fees.lower + rooms.area / 100 * fees.per_area + fees.per_motorbike * rooms.motorbike + fees.per_car * rooms.car AS lower_bound,
fees.upper + rooms.area / 100 * fees.per_area + fees.per_motorbike * rooms.motorbike + fees.per_car * rooms.car AS upper_bound,
payments.id AS payment_id,
payments.room AS payment_room,
payments.amount AS payment_amount,
payments.fee_id AS payment_fee_id
FROM fee
FROM fees
INNER JOIN rooms ON rooms.room = @Room
LEFT JOIN payments ON payments.fee_id = fee.id AND payments.room = @Room
WHERE fee.id >= @FromId AND fee.id <= @ToId AND (
LEFT JOIN payments ON payments.fee_id = fees.id AND payments.room = @Room
WHERE fees.id >= @FromId AND fees.id <= @ToId AND (
@Paid IS NULL
OR (@Paid = 0 AND payments.id IS NULL)
OR (@Paid = 1 AND payments.id IS NOT NULL)
)
ORDER BY fee.id
ORDER BY fees.id
OFFSET @Offset ROWS
FETCH NEXT @FetchNext ROWS ONLY
END

0 comments on commit 3785fb1

Please sign in to comment.