init
This commit is contained in:
@@ -1 +0,0 @@
|
||||
DROP TABLE users;
|
||||
@@ -1,22 +0,0 @@
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
CREATE TABLE users
|
||||
(
|
||||
id BIGSERIAL,
|
||||
email VARCHAR(100) UNIQUE NOT NULL,
|
||||
password VARCHAR(100) NOT NULL,
|
||||
name VARCHAR,
|
||||
status VARCHAR(50) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
user_type varchar,
|
||||
phone_number varchar,
|
||||
nik varchar,
|
||||
created_by INTEGER,
|
||||
updated_by INTEGER,
|
||||
deleted_at timestamp,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
|
||||
INSERT INTO users(id, name, email, password, status)
|
||||
VALUES (1, 'Super Admin', 'admin@enaklo-pos.id', '$2a$10$gsDEsviRfy5gQ8ubFH/lYObL.6FuRSAoXUh6e9ZDXmWTXYzK51pPO', 'Active');
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS organizations;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Organizations table
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
CREATE TABLE organizations (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(255) NOT NULL,
|
||||
plan_type VARCHAR(50) NOT NULL CHECK (plan_type IN ('basic', 'premium', 'enterprise')),
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_organizations_plan_type ON organizations(plan_type);
|
||||
CREATE INDEX idx_organizations_created_at ON organizations(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE roles;
|
||||
@@ -1,11 +0,0 @@
|
||||
CREATE TABLE roles (
|
||||
role_id SERIAL PRIMARY KEY,
|
||||
role_name VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO roles(role_id, role_name)
|
||||
VALUES (1, 'super_admin'),
|
||||
(2, 'admin'),
|
||||
(3, 'admin_partner'),
|
||||
(4, 'admin_site'),
|
||||
(5, 'cashier');
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS outlets;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Outlets table
|
||||
CREATE TABLE outlets (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
address TEXT,
|
||||
timezone VARCHAR(50),
|
||||
currency VARCHAR(3) DEFAULT 'USD',
|
||||
tax_rate DECIMAL(5,4) DEFAULT 0.0000 CHECK (tax_rate >= 0 AND tax_rate <= 1),
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_outlets_organization_id ON outlets(organization_id);
|
||||
CREATE INDEX idx_outlets_is_active ON outlets(is_active);
|
||||
CREATE INDEX idx_outlets_created_at ON outlets(created_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,22 @@
|
||||
-- Users table
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
outlet_id UUID REFERENCES outlets(id) ON DELETE SET NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(50) NOT NULL CHECK (role IN ('admin', 'manager', 'cashier', 'waiter')),
|
||||
permissions JSONB DEFAULT '{}',
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_users_organization_id ON users(organization_id);
|
||||
CREATE INDEX idx_users_outlet_id ON users(outlet_id);
|
||||
CREATE INDEX idx_users_email ON users(email);
|
||||
CREATE INDEX idx_users_role ON users(role);
|
||||
CREATE INDEX idx_users_is_active ON users(is_active);
|
||||
CREATE INDEX idx_users_name ON users(name);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE user_roles;
|
||||
@@ -1,12 +0,0 @@
|
||||
CREATE TABLE user_roles
|
||||
(
|
||||
user_role_id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER REFERENCES users (id) ON DELETE CASCADE,
|
||||
role_id INTEGER REFERENCES roles (role_id) ON DELETE CASCADE,
|
||||
partner_id INTEGER,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
INSERT INTO user_roles(user_id, role_id)
|
||||
VALUES (1, 1);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS categories;
|
||||
@@ -0,0 +1,16 @@
|
||||
-- Categories table
|
||||
CREATE TABLE categories (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
business_type VARCHAR(50) DEFAULT 'restaurant',
|
||||
metadata JSONB DEFAULT '{}',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_categories_organization_id ON categories(organization_id);
|
||||
CREATE INDEX idx_categories_business_type ON categories(business_type);
|
||||
CREATE INDEX idx_categories_created_at ON categories(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE partners CASCADE;
|
||||
@@ -1,17 +0,0 @@
|
||||
CREATE TABLE partners
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
status VARCHAR(50),
|
||||
license_expired_date DATE,
|
||||
address VARCHAR(255),
|
||||
bank_name VARCHAR(255),
|
||||
bank_account_number VARCHAR(50),
|
||||
bank_account_holder_name VARCHAR(255),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
created_by INTEGER,
|
||||
updated_by INTEGER,
|
||||
admin_user_id INTEGER,
|
||||
deleted_at TIMESTAMP
|
||||
);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE sites;
|
||||
@@ -1,22 +0,0 @@
|
||||
CREATE TABLE sites
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
partner_id INTEGER,
|
||||
image varchar,
|
||||
address varchar,
|
||||
location_link varchar,
|
||||
description varchar,
|
||||
highlight varchar,
|
||||
contact_person varchar,
|
||||
tnc varchar,
|
||||
additional_info varchar,
|
||||
status varchar,
|
||||
is_season_ticket boolean,
|
||||
is_discount_active boolean,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
deleted_at TIMESTAMP,
|
||||
created_by INTEGER,
|
||||
updated_by INTEGER
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS products;
|
||||
@@ -0,0 +1,24 @@
|
||||
-- Products table
|
||||
CREATE TABLE products (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
category_id UUID NOT NULL REFERENCES categories(id) ON DELETE CASCADE,
|
||||
sku VARCHAR(100),
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
price DECIMAL(10,2) NOT NULL CHECK (price >= 0),
|
||||
cost DECIMAL(10,2) DEFAULT 0.00 CHECK (cost >= 0),
|
||||
business_type VARCHAR(50) DEFAULT 'restaurant',
|
||||
metadata JSONB DEFAULT '{}',
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_products_organization_id ON products(organization_id);
|
||||
CREATE INDEX idx_products_category_id ON products(category_id);
|
||||
CREATE INDEX idx_products_sku ON products(sku);
|
||||
CREATE INDEX idx_products_business_type ON products(business_type);
|
||||
CREATE INDEX idx_products_is_active ON products(is_active);
|
||||
CREATE INDEX idx_products_created_at ON products(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE products;
|
||||
@@ -1,16 +0,0 @@
|
||||
CREATE TABLE products
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
partner_id INTEGER,
|
||||
site_id INTEGER,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
type VARCHAR,
|
||||
price decimal,
|
||||
status VARCHAR,
|
||||
description VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
deleted_at TIMESTAMP,
|
||||
created_by INTEGER,
|
||||
updated_by INTEGER
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS product_variants;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Product variants table
|
||||
CREATE TABLE product_variants (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
product_id UUID NOT NULL REFERENCES products(id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
price_modifier DECIMAL(10,2) DEFAULT 0.00,
|
||||
metadata JSONB DEFAULT '{}',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_product_variants_product_id ON product_variants(product_id);
|
||||
CREATE INDEX idx_product_variants_created_at ON product_variants(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE orders cascade;
|
||||
@@ -1,13 +0,0 @@
|
||||
CREATE TABLE orders
|
||||
(
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
ref_id varchar,
|
||||
partner_id INTEGER,
|
||||
status VARCHAR,
|
||||
amount DECIMAL NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
created_by INTEGER,
|
||||
payment_type VARCHAR,
|
||||
updated_by INTEGER
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS inventory;
|
||||
@@ -0,0 +1,16 @@
|
||||
-- Inventory table
|
||||
CREATE TABLE inventory (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
outlet_id UUID NOT NULL REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
product_id UUID NOT NULL REFERENCES products(id) ON DELETE CASCADE,
|
||||
quantity INTEGER NOT NULL DEFAULT 0 CHECK (quantity >= 0),
|
||||
reorder_level INTEGER DEFAULT 0 CHECK (reorder_level >= 0),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(outlet_id, product_id)
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_inventory_outlet_id ON inventory(outlet_id);
|
||||
CREATE INDEX idx_inventory_product_id ON inventory(product_id);
|
||||
CREATE INDEX idx_inventory_quantity ON inventory(quantity);
|
||||
CREATE INDEX idx_inventory_updated_at ON inventory(updated_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE order_items;
|
||||
@@ -1,13 +0,0 @@
|
||||
CREATE TABLE order_items
|
||||
(
|
||||
order_item_id BIGSERIAL PRIMARY KEY,
|
||||
order_id INTEGER REFERENCES orders (id),
|
||||
item_id INTEGER,
|
||||
item_type VARCHAR,
|
||||
price DECIMAL NOT NULL,
|
||||
qty INTEGER,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
created_by INTEGER,
|
||||
updated_by INTEGER
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS orders;
|
||||
@@ -0,0 +1,30 @@
|
||||
-- Orders table
|
||||
CREATE TABLE orders (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
outlet_id UUID NOT NULL REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
customer_id UUID,
|
||||
order_number VARCHAR(50) UNIQUE NOT NULL,
|
||||
table_number VARCHAR(20),
|
||||
order_type VARCHAR(50) NOT NULL CHECK (order_type IN ('dine_in', 'takeout', 'delivery')),
|
||||
status VARCHAR(50) DEFAULT 'pending' CHECK (status IN ('pending', 'preparing', 'ready', 'completed', 'cancelled')),
|
||||
subtotal DECIMAL(10,2) NOT NULL CHECK (subtotal >= 0),
|
||||
tax_amount DECIMAL(10,2) NOT NULL CHECK (tax_amount >= 0),
|
||||
discount_amount DECIMAL(10,2) DEFAULT 0.00 CHECK (discount_amount >= 0),
|
||||
total_amount DECIMAL(10,2) NOT NULL CHECK (total_amount >= 0),
|
||||
payment_status VARCHAR(50) DEFAULT 'pending' CHECK (payment_status IN ('pending', 'completed', 'failed', 'refunded')),
|
||||
metadata JSONB DEFAULT '{}',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_orders_organization_id ON orders(organization_id);
|
||||
CREATE INDEX idx_orders_outlet_id ON orders(outlet_id);
|
||||
CREATE INDEX idx_orders_user_id ON orders(user_id);
|
||||
CREATE INDEX idx_orders_order_number ON orders(order_number);
|
||||
CREATE INDEX idx_orders_order_type ON orders(order_type);
|
||||
CREATE INDEX idx_orders_status ON orders(status);
|
||||
CREATE INDEX idx_orders_payment_status ON orders(payment_status);
|
||||
CREATE INDEX idx_orders_created_at ON orders(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE wallets cascade;
|
||||
@@ -1,11 +0,0 @@
|
||||
CREATE TABLE public.wallets
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
partner_id INTEGER NOT NULL,
|
||||
balance DECIMAL(18, 2) NOT NULL DEFAULT 0.00,
|
||||
currency VARCHAR(3) NOT NULL,
|
||||
status VARCHAR(50),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
FOREIGN KEY (partner_id) REFERENCES partners (id) ON DELETE CASCADE
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS order_items;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- Order items table
|
||||
CREATE TABLE order_items (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
order_id UUID NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
|
||||
product_id UUID NOT NULL REFERENCES products(id) ON DELETE RESTRICT,
|
||||
product_variant_id UUID REFERENCES product_variants(id) ON DELETE SET NULL,
|
||||
quantity INTEGER NOT NULL CHECK (quantity > 0),
|
||||
unit_price DECIMAL(10,2) NOT NULL CHECK (unit_price >= 0),
|
||||
total_price DECIMAL(10,2) NOT NULL CHECK (total_price >= 0),
|
||||
modifiers JSONB DEFAULT '[]',
|
||||
status VARCHAR(50) DEFAULT 'pending' CHECK (status IN ('pending', 'preparing', 'ready', 'served', 'cancelled')),
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_order_items_order_id ON order_items(order_id);
|
||||
CREATE INDEX idx_order_items_product_id ON order_items(product_id);
|
||||
CREATE INDEX idx_order_items_product_variant_id ON order_items(product_variant_id);
|
||||
CREATE INDEX idx_order_items_status ON order_items(status);
|
||||
CREATE INDEX idx_order_items_created_at ON order_items(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE payments cascade;
|
||||
@@ -1,16 +0,0 @@
|
||||
CREATE TABLE public.payments
|
||||
(
|
||||
id uuid NOT NULL DEFAULT uuid_generate_v4(),
|
||||
partner_id numeric NOT NULL,
|
||||
order_id numeric NOT NULL,
|
||||
reference_id varchar NOT NULL,
|
||||
channel varchar NOT NULL,
|
||||
payment_type varchar NOT NULL,
|
||||
amount numeric NOT NULL,
|
||||
state varchar NOT NULL,
|
||||
request_metadata json NOT NULL,
|
||||
created_at timestamp NOT NULL,
|
||||
updated_at timestamp NOT NULL,
|
||||
finished_at timestamp NOT NULL,
|
||||
CONSTRAINT payment_pkey PRIMARY KEY (id)
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS payment_methods;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Payment methods table
|
||||
CREATE TABLE payment_methods (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
type VARCHAR(50) NOT NULL CHECK (type IN ('cash', 'card', 'digital_wallet')),
|
||||
processor VARCHAR(100),
|
||||
configuration JSONB DEFAULT '{}',
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_payment_methods_organization_id ON payment_methods(organization_id);
|
||||
CREATE INDEX idx_payment_methods_type ON payment_methods(type);
|
||||
CREATE INDEX idx_payment_methods_is_active ON payment_methods(is_active);
|
||||
CREATE INDEX idx_payment_methods_created_at ON payment_methods(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE licenses cascade;
|
||||
@@ -1,14 +0,0 @@
|
||||
CREATE TABLE licenses
|
||||
(
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
partner_id NUMERIC NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
start_date DATE NOT NULL,
|
||||
end_date DATE NOT NULL,
|
||||
renewal_date DATE,
|
||||
serial_number VARCHAR(255) UNIQUE NOT NULL,
|
||||
created_by numeric not null,
|
||||
updated_by numeric not null,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS payments;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Payments table
|
||||
CREATE TABLE payments (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
order_id UUID NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
|
||||
payment_method_id UUID NOT NULL REFERENCES payment_methods(id) ON DELETE RESTRICT,
|
||||
amount DECIMAL(10,2) NOT NULL CHECK (amount >= 0),
|
||||
status VARCHAR(50) DEFAULT 'pending' CHECK (status IN ('pending', 'completed', 'failed', 'refunded')),
|
||||
transaction_id VARCHAR(255),
|
||||
metadata JSONB DEFAULT '{}',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_payments_order_id ON payments(order_id);
|
||||
CREATE INDEX idx_payments_payment_method_id ON payments(payment_method_id);
|
||||
CREATE INDEX idx_payments_status ON payments(status);
|
||||
CREATE INDEX idx_payments_transaction_id ON payments(transaction_id);
|
||||
CREATE INDEX idx_payments_created_at ON payments(created_at);
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE transaction cascade;
|
||||
@@ -1,13 +0,0 @@
|
||||
|
||||
CREATE TABLE transaction
|
||||
(
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
partner_id NUMERIC NOT NULL,
|
||||
transaction_type varchar not null,
|
||||
reference_id varchar,
|
||||
status varchar,
|
||||
created_by numeric not null,
|
||||
updated_by numeric not null,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
@@ -0,0 +1,7 @@
|
||||
-- Remove indexes for email and phone_number columns
|
||||
DROP INDEX IF EXISTS idx_organizations_email;
|
||||
DROP INDEX IF EXISTS idx_organizations_phone_number;
|
||||
|
||||
-- Remove email and phone_number columns from organizations table
|
||||
ALTER TABLE organizations DROP COLUMN IF EXISTS email;
|
||||
ALTER TABLE organizations DROP COLUMN IF EXISTS phone_number;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- Add email and phone_number columns to organizations table
|
||||
ALTER TABLE organizations ADD COLUMN email VARCHAR(255);
|
||||
ALTER TABLE organizations ADD COLUMN phone_number VARCHAR(20);
|
||||
|
||||
-- Create indexes for email and phone_number columns
|
||||
CREATE INDEX idx_organizations_email ON organizations(email);
|
||||
CREATE INDEX idx_organizations_phone_number ON organizations(phone_number);
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Remove cost column from product_variants table
|
||||
ALTER TABLE product_variants DROP COLUMN cost;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Add cost column to product_variants table
|
||||
ALTER TABLE product_variants ADD COLUMN cost DECIMAL(10,2) DEFAULT 0.00;
|
||||
@@ -1,5 +0,0 @@
|
||||
-- Remove partner_id column from cashier_sessions table
|
||||
ALTER TABLE cashier_sessions DROP COLUMN IF EXISTS partner_id;
|
||||
|
||||
-- Remove index
|
||||
DROP INDEX IF EXISTS idx_cashier_sessions_partner_id;
|
||||
@@ -1,8 +0,0 @@
|
||||
-- Add partner_id column to cashier_sessions table
|
||||
ALTER TABLE cashier_sessions ADD COLUMN partner_id BIGINT NOT NULL DEFAULT 1;
|
||||
|
||||
-- Add index for better query performance
|
||||
CREATE INDEX idx_cashier_sessions_partner_id ON cashier_sessions(partner_id);
|
||||
|
||||
-- Add foreign key constraint (assuming partners table exists)
|
||||
-- ALTER TABLE cashier_sessions ADD CONSTRAINT fk_cashier_sessions_partner_id FOREIGN KEY (partner_id) REFERENCES partners(id);
|
||||
@@ -0,0 +1,30 @@
|
||||
-- Drop order_sequences table
|
||||
DROP TABLE IF EXISTS order_sequences;
|
||||
|
||||
-- Remove boolean flags and related fields from orders
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS refunded_by;
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS refunded_at;
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS refund_reason;
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS voided_by;
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS voided_at;
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS void_reason;
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS is_refund;
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS is_void;
|
||||
|
||||
-- Remove refund_amount field from orders
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS refund_amount;
|
||||
|
||||
-- Revert payment status constraints
|
||||
ALTER TABLE orders DROP CONSTRAINT orders_payment_status_check;
|
||||
ALTER TABLE orders ADD CONSTRAINT orders_payment_status_check CHECK (payment_status IN ('pending', 'completed', 'failed', 'refunded'));
|
||||
|
||||
-- Revert order status constraints
|
||||
ALTER TABLE orders DROP CONSTRAINT orders_status_check;
|
||||
ALTER TABLE orders ADD CONSTRAINT orders_status_check CHECK (status IN ('pending', 'preparing', 'ready', 'completed', 'cancelled'));
|
||||
|
||||
-- Remove cost fields from order_items table
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS total_cost;
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS unit_cost;
|
||||
|
||||
-- Remove cost fields from orders table
|
||||
ALTER TABLE orders DROP COLUMN IF EXISTS total_cost;
|
||||
@@ -0,0 +1,44 @@
|
||||
-- Add cost fields to orders table
|
||||
ALTER TABLE orders ADD COLUMN total_cost DECIMAL(10,2) DEFAULT 0.00 CHECK (total_cost >= 0);
|
||||
|
||||
-- Add cost fields to order_items table
|
||||
ALTER TABLE order_items ADD COLUMN unit_cost DECIMAL(10,2) DEFAULT 0.00 CHECK (unit_cost >= 0);
|
||||
ALTER TABLE order_items ADD COLUMN total_cost DECIMAL(10,2) DEFAULT 0.00 CHECK (total_cost >= 0);
|
||||
|
||||
-- Add boolean flags for void and refund instead of status enums
|
||||
ALTER TABLE orders ADD COLUMN is_void BOOLEAN DEFAULT FALSE;
|
||||
ALTER TABLE orders ADD COLUMN is_refund BOOLEAN DEFAULT FALSE;
|
||||
ALTER TABLE orders ADD COLUMN void_reason VARCHAR(255);
|
||||
ALTER TABLE orders ADD COLUMN voided_at TIMESTAMP WITH TIME ZONE;
|
||||
ALTER TABLE orders ADD COLUMN voided_by UUID REFERENCES users(id);
|
||||
ALTER TABLE orders ADD COLUMN refund_reason VARCHAR(255);
|
||||
ALTER TABLE orders ADD COLUMN refunded_at TIMESTAMP WITH TIME ZONE;
|
||||
ALTER TABLE orders ADD COLUMN refunded_by UUID REFERENCES users(id);
|
||||
|
||||
-- Add new payment statuses (keep this for payment tracking)
|
||||
ALTER TABLE orders DROP CONSTRAINT orders_payment_status_check;
|
||||
ALTER TABLE orders ADD CONSTRAINT orders_payment_status_check CHECK (payment_status IN ('pending', 'completed', 'failed', 'refunded', 'partially_refunded'));
|
||||
|
||||
-- Add refund_amount field to orders
|
||||
ALTER TABLE orders ADD COLUMN refund_amount DECIMAL(10,2) DEFAULT 0.00 CHECK (refund_amount >= 0);
|
||||
|
||||
-- Add sequence number tracking for order numbers
|
||||
CREATE TABLE order_sequences (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
outlet_id UUID NOT NULL REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
year INTEGER NOT NULL,
|
||||
month INTEGER NOT NULL,
|
||||
sequence_number INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(organization_id, outlet_id, year, month)
|
||||
);
|
||||
|
||||
-- Indexes for order_sequences
|
||||
CREATE INDEX idx_order_sequences_org_outlet ON order_sequences(organization_id, outlet_id);
|
||||
CREATE INDEX idx_order_sequences_year_month ON order_sequences(year, month);
|
||||
|
||||
-- Indexes for the new boolean flags
|
||||
CREATE INDEX idx_orders_is_void ON orders(is_void);
|
||||
CREATE INDEX idx_orders_is_refund ON orders(is_refund);
|
||||
@@ -0,0 +1,22 @@
|
||||
-- Remove refund tracking from order items
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS refunded_by;
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS refunded_at;
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS refund_reason;
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS is_fully_refunded;
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS is_partially_refunded;
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS refund_quantity;
|
||||
ALTER TABLE order_items DROP COLUMN IF EXISTS refund_amount;
|
||||
|
||||
-- Remove refund tracking from payments
|
||||
ALTER TABLE payments DROP COLUMN IF EXISTS refunded_by;
|
||||
ALTER TABLE payments DROP COLUMN IF EXISTS refunded_at;
|
||||
ALTER TABLE payments DROP COLUMN IF EXISTS refund_reason;
|
||||
ALTER TABLE payments DROP COLUMN IF EXISTS refund_amount;
|
||||
|
||||
-- Drop payment_order_items table
|
||||
DROP TABLE IF EXISTS payment_order_items;
|
||||
|
||||
-- Remove split payment tracking from payments table
|
||||
ALTER TABLE payments DROP COLUMN IF EXISTS split_description;
|
||||
ALTER TABLE payments DROP COLUMN IF EXISTS split_total;
|
||||
ALTER TABLE payments DROP COLUMN IF EXISTS split_number;
|
||||
@@ -0,0 +1,37 @@
|
||||
-- Add split payment tracking to payments table
|
||||
ALTER TABLE payments ADD COLUMN split_number INTEGER DEFAULT 1;
|
||||
ALTER TABLE payments ADD COLUMN split_total INTEGER DEFAULT 1;
|
||||
ALTER TABLE payments ADD COLUMN split_description VARCHAR(255);
|
||||
|
||||
-- Add item-specific payment tracking
|
||||
CREATE TABLE payment_order_items (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
payment_id UUID NOT NULL REFERENCES payments(id) ON DELETE CASCADE,
|
||||
order_item_id UUID NOT NULL REFERENCES order_items(id) ON DELETE CASCADE,
|
||||
amount DECIMAL(10,2) NOT NULL CHECK (amount >= 0),
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes for payment_order_items
|
||||
CREATE INDEX idx_payment_order_items_payment_id ON payment_order_items(payment_id);
|
||||
CREATE INDEX idx_payment_order_items_order_item_id ON payment_order_items(order_item_id);
|
||||
|
||||
-- Add refund tracking to payments
|
||||
ALTER TABLE payments ADD COLUMN refund_amount DECIMAL(10,2) DEFAULT 0.00 CHECK (refund_amount >= 0);
|
||||
ALTER TABLE payments ADD COLUMN refund_reason VARCHAR(255);
|
||||
ALTER TABLE payments ADD COLUMN refunded_at TIMESTAMP WITH TIME ZONE;
|
||||
ALTER TABLE payments ADD COLUMN refunded_by UUID REFERENCES users(id);
|
||||
|
||||
-- Add refund tracking to order items with boolean flags
|
||||
ALTER TABLE order_items ADD COLUMN refund_amount DECIMAL(10,2) DEFAULT 0.00 CHECK (refund_amount >= 0);
|
||||
ALTER TABLE order_items ADD COLUMN refund_quantity INTEGER DEFAULT 0 CHECK (refund_quantity >= 0);
|
||||
ALTER TABLE order_items ADD COLUMN is_partially_refunded BOOLEAN DEFAULT FALSE;
|
||||
ALTER TABLE order_items ADD COLUMN is_fully_refunded BOOLEAN DEFAULT FALSE;
|
||||
ALTER TABLE order_items ADD COLUMN refund_reason VARCHAR(255);
|
||||
ALTER TABLE order_items ADD COLUMN refunded_at TIMESTAMP WITH TIME ZONE;
|
||||
ALTER TABLE order_items ADD COLUMN refunded_by UUID REFERENCES users(id);
|
||||
|
||||
-- Indexes for order item refund flags
|
||||
CREATE INDEX idx_order_items_is_partially_refunded ON order_items(is_partially_refunded);
|
||||
CREATE INDEX idx_order_items_is_fully_refunded ON order_items(is_fully_refunded);
|
||||
@@ -0,0 +1,12 @@
|
||||
-- Drop foreign key constraints first
|
||||
ALTER TABLE files DROP CONSTRAINT IF EXISTS fk_files_organization_id;
|
||||
ALTER TABLE files DROP CONSTRAINT IF EXISTS fk_files_user_id;
|
||||
|
||||
-- Drop indexes
|
||||
DROP INDEX IF EXISTS idx_files_organization_id;
|
||||
DROP INDEX IF EXISTS idx_files_user_id;
|
||||
DROP INDEX IF EXISTS idx_files_file_type;
|
||||
DROP INDEX IF EXISTS idx_files_created_at;
|
||||
|
||||
-- Drop the table
|
||||
DROP TABLE IF EXISTS files;
|
||||
@@ -0,0 +1,29 @@
|
||||
CREATE TABLE files (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL,
|
||||
user_id UUID NOT NULL,
|
||||
file_name VARCHAR(255) NOT NULL,
|
||||
original_name VARCHAR(255) NOT NULL,
|
||||
file_url VARCHAR(500) NOT NULL,
|
||||
file_size BIGINT NOT NULL,
|
||||
mime_type VARCHAR(100) NOT NULL,
|
||||
file_type VARCHAR(50) NOT NULL,
|
||||
upload_path VARCHAR(500) NOT NULL,
|
||||
is_public BOOLEAN DEFAULT TRUE,
|
||||
metadata JSONB,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create indexes for better performance
|
||||
CREATE INDEX idx_files_organization_id ON files(organization_id);
|
||||
CREATE INDEX idx_files_user_id ON files(user_id);
|
||||
CREATE INDEX idx_files_file_type ON files(file_type);
|
||||
CREATE INDEX idx_files_created_at ON files(created_at);
|
||||
|
||||
-- Add foreign key constraints
|
||||
ALTER TABLE files ADD CONSTRAINT fk_files_organization_id
|
||||
FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE;
|
||||
|
||||
ALTER TABLE files ADD CONSTRAINT fk_files_user_id
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS customers CASCADE;
|
||||
@@ -0,0 +1,27 @@
|
||||
CREATE TABLE customers (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255) UNIQUE,
|
||||
phone VARCHAR(20),
|
||||
address VARCHAR(500),
|
||||
is_default BOOLEAN DEFAULT FALSE,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
metadata JSONB DEFAULT '{}',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
|
||||
CONSTRAINT fk_customers_organization
|
||||
FOREIGN KEY (organization_id)
|
||||
REFERENCES organizations(id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Create indexes
|
||||
CREATE INDEX idx_customers_organization_id ON customers(organization_id);
|
||||
CREATE INDEX idx_customers_email ON customers(email);
|
||||
CREATE INDEX idx_customers_is_default ON customers(is_default);
|
||||
CREATE INDEX idx_customers_is_active ON customers(is_active);
|
||||
|
||||
-- Create unique constraint to ensure only one default customer per organization
|
||||
CREATE UNIQUE INDEX idx_customers_org_default ON customers(organization_id, is_default) WHERE is_default = TRUE;
|
||||
@@ -0,0 +1,5 @@
|
||||
-- Drop trigger
|
||||
DROP TRIGGER IF EXISTS trigger_create_default_customer ON organizations;
|
||||
|
||||
-- Drop function
|
||||
DROP FUNCTION IF EXISTS create_default_customer();
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Function to create default customer for new organization
|
||||
CREATE OR REPLACE FUNCTION create_default_customer()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
INSERT INTO customers (organization_id, name, is_default, is_active, created_at, updated_at)
|
||||
VALUES (NEW.id, 'Walk In Customer', true, true, NOW(), NOW());
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Trigger to automatically create default customer when organization is created
|
||||
CREATE TRIGGER trigger_create_default_customer
|
||||
AFTER INSERT ON organizations
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION create_default_customer();
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Remove default customers created for existing organizations
|
||||
DELETE FROM customers
|
||||
WHERE name = 'Walk In Customer' AND is_default = true;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Create default customers for existing organizations that don't have one
|
||||
INSERT INTO customers (organization_id, name, is_default, is_active, created_at, updated_at)
|
||||
SELECT
|
||||
o.id as organization_id,
|
||||
'Walk In Customer' as name,
|
||||
true as is_default,
|
||||
true as is_active,
|
||||
NOW() as created_at,
|
||||
NOW() as updated_at
|
||||
FROM organizations o
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM customers c
|
||||
WHERE c.organization_id = o.id AND c.is_default = true
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Remove notes and metadata columns from order_items table
|
||||
ALTER TABLE order_items
|
||||
DROP COLUMN IF EXISTS notes,
|
||||
DROP COLUMN IF EXISTS metadata;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Add notes and metadata columns to order_items table
|
||||
ALTER TABLE order_items
|
||||
ADD COLUMN notes VARCHAR(500),
|
||||
ADD COLUMN metadata JSONB DEFAULT '{}';
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Revert 'paid' status from orders table status constraint
|
||||
ALTER TABLE orders DROP CONSTRAINT IF EXISTS orders_status_check;
|
||||
ALTER TABLE orders ADD CONSTRAINT orders_status_check CHECK (status IN ('pending', 'preparing', 'ready', 'completed', 'cancelled'));
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Add 'paid' status to orders table status constraint
|
||||
ALTER TABLE orders DROP CONSTRAINT IF EXISTS orders_status_check;
|
||||
ALTER TABLE orders ADD CONSTRAINT orders_status_check CHECK (status IN ('pending', 'preparing', 'ready', 'completed', 'cancelled', 'paid'));
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS outlet_settings;
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Outlet settings table
|
||||
CREATE TABLE outlet_settings (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
outlet_id UUID NOT NULL REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
key VARCHAR(255) NOT NULL,
|
||||
value TEXT,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(outlet_id, key)
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_outlet_settings_outlet_id ON outlet_settings(outlet_id);
|
||||
CREATE INDEX idx_outlet_settings_key ON outlet_settings(key);
|
||||
CREATE INDEX idx_outlet_settings_created_at ON outlet_settings(created_at);
|
||||
Reference in New Issue
Block a user