-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
201 lines (200 loc) · 7.53 KB
/
schema.sql
File metadata and controls
201 lines (200 loc) · 7.53 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TYPE UserRole AS ENUM (
'administrator',
'customer',
'vendor'
);
CREATE TYPE ApartmentContractRole AS ENUM (
'host',
'board'
);
CREATE TYPE SubscriptionType AS ENUM (
'monthly',
'yearly'
);
CREATE TYPE InvoiceStatus AS ENUM (
'paid',
'overdue',
'pending'
);
CREATE TYPE PaymentMethodType AS ENUM (
'Cash',
'Card',
'Bank',
'eWallet'
);
CREATE TABLE "User" (
"UserID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"HashedPassword" TEXT NOT NULL,
"PasswordSalt" TEXT NOT NULL,
"Username" VARCHAR(255) UNIQUE NOT NULL,
"PhoneNumber" VARCHAR(20),
"Email" TEXT,
"Role" UserRole NOT NULL,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now(),
"RequirePasswordChange" BOOLEAN DEFAULT false
);
CREATE TABLE "Administrator" (
"AdminID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"UserID" UUID UNIQUE NOT NULL REFERENCES "User"("UserID") ON DELETE CASCADE,
"FullName" TEXT NOT NULL,
"Position" TEXT,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "Customer" (
"CustomerID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"UserID" UUID UNIQUE NOT NULL REFERENCES "User"("UserID") ON DELETE CASCADE,
"FullName" TEXT NOT NULL,
"DateOfBirth" DATE,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "Vendor" (
"VendorID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"UserID" UUID UNIQUE NOT NULL REFERENCES "User"("UserID") ON DELETE CASCADE,
"CompanyName" VARCHAR(255) NOT NULL,
"ServiceType" VARCHAR(255),
"ContactEmail" TEXT,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "Apartment" (
"ApartmentID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"Building" INTEGER,
"Floor" INTEGER,
"Occupied" BOOLEAN DEFAULT false,
"ApartmentNumber" TEXT UNIQUE NOT NULL,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "ApartmentContract" (
"ApartmentContractID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"ApartmentID" UUID NOT NULL REFERENCES "Apartment"("ApartmentID"),
"CustomerID" UUID NOT NULL REFERENCES "Customer"("CustomerID"),
"StartDate" TIMESTAMP,
"EndDate" TIMESTAMP,
"IsActive" BOOLEAN DEFAULT true,
"Role" ApartmentContractRole,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "Service" (
"ServiceID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"ServiceName" VARCHAR(255) NOT NULL,
"ServicePrice" DECIMAL NOT NULL,
"ServiceType" VARCHAR(255),
"Availability" BOOLEAN DEFAULT true,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "ServiceContract" (
"ServiceContractID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"ServiceID" UUID NOT NULL REFERENCES "Service"("ServiceID"),
"CustomerID" UUID NOT NULL REFERENCES "Customer"("CustomerID")
"Subscription" SubscriptionType,
"ContractPrice" DECIMAL NOT NULL,
"StartDate" TIMESTAMP,
"EndDate" TIMESTAMP,
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "Invoice" (
"InvoiceID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"CustomerID" UUID NOT NULL REFERENCES "Customer"("CustomerID"),
"VendorID" UUID REFERENCES "Vendor"("VendorID"),
"DateIssued" TIMESTAMP WITH TIME ZONE DEFAULT now(),
"BillNumber" VARCHAR(255) UNIQUE NOT NULL,
"DueDate" TIMESTAMP,
"Status" InvoiceStatus DEFAULT 'pending',
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now(),
"TotalPrice" DECIMAL NOT NULL,
"InvoiceType" VARCHAR(50) DEFAULT 'service',
"MeterReadingID" UUID REFERENCES "MeterReading"("ReadingID")
);
CREATE TABLE "InvoiceDetail" (
"InvoiceDetailID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"ServiceContractID" UUID NOT NULL REFERENCES "ServiceContract"("ServiceContractID"),
"InvoiceID" UUID NOT NULL REFERENCES "Invoice"("InvoiceID") ON DELETE CASCADE,
"ContractPrice" DECIMAL NOT NULL
);
CREATE TABLE "Payment" (
"PaymentID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"InvoiceID" UUID NOT NULL REFERENCES "Invoice"("InvoiceID"),
"AmountPaid" DECIMAL NOT NULL,
"PaymentDate" TIMESTAMP WITH TIME ZONE DEFAULT now(),
"PaymentMethod" PaymentMethodType,
"TransactionRef" VARCHAR(255),
"Note" VARCHAR(1000),
"BatchPaymentID" UUID
);
CREATE TABLE "BatchPayment" (
"BatchPaymentID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"CustomerID" UUID NOT NULL REFERENCES "Customer"("CustomerID"),
"TotalAmount" DECIMAL NOT NULL,
"PaymentMethod" VARCHAR(50) NOT NULL,
"TransactionRef" VARCHAR(255),
"PaymentDate" TIMESTAMP WITH TIME ZONE DEFAULT now(),
"Status" VARCHAR(50) DEFAULT 'pending',
"Note" VARCHAR(1000)
);
ALTER TABLE "Payment"
ADD CONSTRAINT "fk_payment_batch"
FOREIGN KEY ("BatchPaymentID") REFERENCES "BatchPayment"("BatchPaymentID");
CREATE TABLE "MeterReading" (
"ReadingID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"ApartmentID" UUID NOT NULL REFERENCES "Apartment"("ApartmentID"),
"UtilityType" VARCHAR(50) NOT NULL,
"ReadingDate" TIMESTAMP WITH TIME ZONE DEFAULT now(),
"PreviousReading" DECIMAL NOT NULL DEFAULT 0,
"CurrentReading" DECIMAL NOT NULL,
"UnitsConsumed" DECIMAL NOT NULL,
"PricePerUnit" DECIMAL NOT NULL,
"TotalCost" DECIMAL NOT NULL,
"BillingMonth" VARCHAR(7) NOT NULL,
"RecordedBy" UUID REFERENCES "Administrator"("AdminID"),
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE TABLE "CustomerRequest" (
"RequestID" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"CustomerID" UUID NOT NULL REFERENCES "Customer"("CustomerID"),
"ServiceID" UUID REFERENCES "Service"("ServiceID"),
"RequestType" VARCHAR(100) NOT NULL,
"RequestedSubscription" SubscriptionType,
"RequestDate" TIMESTAMP WITH TIME ZONE DEFAULT now(),
"Status" VARCHAR(50) DEFAULT 'pending',
"Notes" TEXT,
"ProcessedBy" UUID REFERENCES "Administrator"("AdminID"),
"ProcessedDate" TIMESTAMP WITH TIME ZONE,
"AdminNotes" TEXT
);
CREATE TABLE IF NOT EXISTS "VNPayTransaction" (
"TxnRef" TEXT PRIMARY KEY,
"InvoiceID" UUID REFERENCES "Invoice"("InvoiceID"),
"BatchPaymentID" UUID REFERENCES "BatchPayment"("BatchPaymentID"),
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT now()
);
CREATE INDEX ON "Administrator" ("UserID");
CREATE INDEX ON "Customer" ("UserID");
CREATE INDEX ON "Vendor" ("UserID");
CREATE INDEX ON "ApartmentContract" ("ApartmentID");
CREATE INDEX ON "ApartmentContract" ("CustomerID");
CREATE INDEX ON "ServiceContract" ("ServiceID");
CREATE INDEX ON "ServiceContract" ("CustomerID");
CREATE INDEX ON "Invoice" ("CustomerID");
CREATE INDEX ON "InvoiceDetail" ("ServiceContractID");
CREATE INDEX ON "InvoiceDetail" ("InvoiceID");
CREATE INDEX ON "Payment" ("InvoiceID");
CREATE INDEX ON "Payment" ("BatchPaymentID");
CREATE INDEX ON "BatchPayment" ("CustomerID");
CREATE INDEX ON "BatchPayment" ("Status");
CREATE INDEX ON "MeterReading" ("ApartmentID");
CREATE INDEX ON "MeterReading" ("UtilityType");
CREATE INDEX ON "MeterReading" ("BillingMonth");
CREATE INDEX ON "Invoice" ("InvoiceType");
CREATE INDEX ON "Invoice" ("MeterReadingID");
CREATE INDEX ON "CustomerRequest" ("CustomerID");
CREATE INDEX ON "CustomerRequest" ("ServiceID");
CREATE INDEX ON "CustomerRequest" ("Status");
CREATE INDEX ON "VNPayTransaction" ("InvoiceID");
CREATE INDEX ON "VNPayTransaction" ("BatchPaymentID");
--Create indexes
CREATE INDEX idx_invoice_vendor_id ON "Invoice" ("VendorID");
CREATE INDEX idx_invoice_status ON "Invoice" ("Status");
CREATE INDEX idx_invoice_date_issued ON "Invoice" ("DateIssued" DESC);
CREATE INDEX idx_invoice_due_date ON "Invoice" ("DueDate");
CREATE INDEX idx_apartmentcontract_active ON "ApartmentContract" ("IsActive") WHERE "IsActive" = true;
CREATE INDEX idx_apartment_occupied ON "Apartment" ("Occupied") WHERE "Occupied" = true;