-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.sql
264 lines (244 loc) · 12.8 KB
/
database.sql
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
CREATE TABLE purchases_paymentmade (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
payment_method VARCHAR(50) NOT NULL,
payment_date DATETIME NOT NULL,
transaction_id VARCHAR(50) NOT NULL UNIQUE,
supplier_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
created_by_id BIGINT NULL,
CONSTRAINT fk_purchases_paymentmade_supplier FOREIGN KEY (supplier_id) REFERENCES purchases_supplier(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_paymentmade_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_paymentmade_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE sales_customer (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(254) NOT NULL UNIQUE,
phone_number VARCHAR(15) NULL,
address TEXT NULL,
tenant_id BIGINT NULL,
created_by_id BIGINT NULL,
CONSTRAINT fk_sales_customer_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_customer_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE sales_salesitem (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
quantity INT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
vat INT NOT NULL,
vat_amount DECIMAL(10, 2) AS (CAST((CAST((price * quantity) AS DECIMAL(10, 2)) * vat / 100) AS DECIMAL(10, 2))) STORED,
product_id BIGINT NOT NULL,
sales_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
stock_snapshot INT NULL,
created_by_id BIGINT NULL,
CONSTRAINT fk_sales_salesitem_product FOREIGN KEY (product_id) REFERENCES purchases_product(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_salesitem_sales FOREIGN KEY (sales_id) REFERENCES sales_sales(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_salesitem_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_salesitem_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE sales_salesinvoice (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
billing_address TEXT NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
payment_status VARCHAR(20) NOT NULL,
tenant_id BIGINT NULL,
sales_id BIGINT NOT NULL UNIQUE,
created_by_id BIGINT NULL,
CONSTRAINT fk_sales_salesinvoice_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_salesinvoice_sales FOREIGN KEY (sales_id) REFERENCES sales_sales(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_salesinvoice_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE purchases_product (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
name VARCHAR(100) NOT NULL,
sku VARCHAR(50) NOT NULL UNIQUE,
tenant_id BIGINT NULL,
uom_id BIGINT NULL,
opening_stock INT NULL,
stock_quantity REAL NULL,
created_by_id BIGINT NULL,
CONSTRAINT fk_purchases_product_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_product_uom FOREIGN KEY (uom_id) REFERENCES purchases_unitofmeasurements(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_product_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE accounts_account (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
tenant_id BIGINT NULL,
balance DECIMAL(10, 2) NOT NULL,
CONSTRAINT fk_accounts_account_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE accounts_bankaccount (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
tenant_id BIGINT NULL,
accounttype VARCHAR(25) NOT NULL,
balance DECIMAL(10, 2) NOT NULL,
CONSTRAINT fk_accounts_bankaccount_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE accounts_cashaccount (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NULL,
tenant_id BIGINT NULL,
balance DECIMAL(10, 2) NOT NULL,
CONSTRAINT fk_accounts_cashaccount_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE purchases_purchaseitem (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
quantity INT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
product_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
created_by_id BIGINT NULL,
purchase_id BIGINT NOT NULL,
CONSTRAINT fk_purchases_purchaseitem_product FOREIGN KEY (product_id) REFERENCES purchases_product(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchaseitem_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchaseitem_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchaseitem_purchase FOREIGN KEY (purchase_id) REFERENCES purchases_purchaseinvoice(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE sales_paymentreceived (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
payment_method VARCHAR(50) NOT NULL,
payment_date DATETIME NOT NULL,
customer_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
created_by_id BIGINT NULL,
transaction_id VARCHAR(100) NOT NULL,
CONSTRAINT fk_sales_paymentreceived_customer FOREIGN KEY (customer_id) REFERENCES sales_customer(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_paymentreceived_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_paymentreceived_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE purchases_unitofmeasurements (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
name VARCHAR(100) NOT NULL,
tenant_id BIGINT NULL,
created_by_id BIGINT NULL,
field VARCHAR(255) NULL,
CONSTRAINT fk_purchases_unitofmeasurements_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_unitofmeasurements_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE purchases_purchasereturn (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
return_date DATETIME NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
created_by_id BIGINT NULL,
purchase_invoice_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
CONSTRAINT fk_purchases_purchasereturn_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchasereturn_invoice FOREIGN KEY (purchase_invoice_id) REFERENCES purchases_purchaseinvoice(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchasereturn_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE purchases_purchasereturnitem (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
quantity INT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
created_by_id BIGINT NULL,
product_id BIGINT NOT NULL,
purchase_return_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
CONSTRAINT fk_purchases_purchasereturnitem_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchasereturnitem_product FOREIGN KEY (product_id) REFERENCES purchases_product(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchasereturnitem_return FOREIGN KEY (purchase_return_id) REFERENCES purchases_purchasereturn(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchasereturnitem_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE purchases_stockmovement (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
quantity INT NOT NULL,
date DATETIME NOT NULL,
description TEXT NULL,
product_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
created_by_id BIGINT NULL,
movement_type VARCHAR(20) NOT NULL,
CONSTRAINT fk_purchases_stockmovement_product FOREIGN KEY (product_id) REFERENCES purchases_product(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_stockmovement_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_stockmovement_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE tenant_tenantmodel (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
domain VARCHAR(10) NOT NULL UNIQUE,
api_key VARCHAR(20) NULL UNIQUE
);
CREATE TABLE dashboard_message (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
room_name VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
timestamp DATETIME NOT NULL,
user_id BIGINT NOT NULL,
CONSTRAINT fk_dashboard_message_user FOREIGN KEY (user_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE purchases_purchaseinvoice (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
invoice_number VARCHAR(100) NULL,
purchase_date DATETIME NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
received_date DATETIME NULL,
tenant_id BIGINT NULL,
supplier_id BIGINT NOT NULL,
created_by_id BIGINT NULL,
order_date DATETIME NULL,
returned BOOL NOT NULL,
CONSTRAINT fk_purchases_purchaseinvoice_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchaseinvoice_supplier FOREIGN KEY (supplier_id) REFERENCES purchases_supplier(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_purchases_purchaseinvoice_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE sales_sales (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
customer_id BIGINT NOT NULL,
tenant_id BIGINT NULL,
created_by_id BIGINT NULL,
returned BOOL NOT NULL,
CONSTRAINT fk_sales_sales_customer FOREIGN KEY (customer_id) REFERENCES sales_customer(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_sales_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_sales_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE sales_salesreturn (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
created_by_id BIGINT NULL,
tenant_id BIGINT NULL,
CONSTRAINT fk_sales_salesreturn_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_salesreturn_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE sales_salesreturneditems (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME NOT NULL,
updated_at DATE NOT NULL,
created_by_id BIGINT NULL,
tenant_id BIGINT NULL,
CONSTRAINT fk_sales_salesreturneditems_user FOREIGN KEY (created_by_id) REFERENCES users_customuser(id) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_sales_salesreturneditems_tenant FOREIGN KEY (tenant_id) REFERENCES tenant_tenantmodel(id) ON DELETE SET NULL ON UPDATE CASCADE
);