forked from zennon-sml/xicas-back
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.sql
More file actions
57 lines (52 loc) · 1.73 KB
/
db.sql
File metadata and controls
57 lines (52 loc) · 1.73 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
-- Create table for administrators
CREATE TABLE admins (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password_hash TEXT NOT NULL, -- Password hash for security
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create table for products
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name VARCHAR(150) NOT NULL,
barcode TEXT,
description TEXT,
price NUMERIC(10, 2) NOT NULL,
cost NUMERIC(10, 2) NOT NULL,
quantity NUMERIC(10, 2) NOT NULL,
image TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create table for sales
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
saler_id INTEGER REFERENCES admins(id),
products JSONB NOT NULL DEFAULT '[]',
payment_types JSONB NOT NULL DEFAULT '{"money": 0, "pix": 0, "debit": 0, "credit": 0, "other": 0}',
general_discount FLOAT NOT NULL DEFAULT 0,
sale_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- CREATE TABLE sales (
-- id SERIAL PRIMARY KEY,
-- product_id INT NOT NULL,
-- quantity INT NOT NULL,
-- total NUMERIC(10, 2) NOT NULL,
-- admin_id INT,
-- sale_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- CONSTRAINT fk_product_sale FOREIGN KEY (product_id) REFERENCES products (id),
-- CONSTRAINT fk_admin_sale FOREIGN KEY (admin_id) REFERENCES admins (id)
-- );
CREATE FUNCTION update_product_quantity_on_sale() RETURNS TRIGGER AS $$
BEGIN
UPDATE products
SET quantity = quantity - NEW.quantity, updated_at = CURRENT_TIMESTAMP
WHERE id = NEW.product_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER after_sale_insert
AFTER INSERT ON sales
FOR EACH ROW
EXECUTE FUNCTION update_product_quantity_on_sale();