-- Add fee structure columns to organizations table ALTER TABLE organizations ADD COLUMN platform_fee_type VARCHAR(20) DEFAULT 'percentage_plus_fixed', ADD COLUMN platform_fee_percentage DECIMAL(5,4) DEFAULT 0.0300, ADD COLUMN platform_fee_fixed INTEGER DEFAULT 30, ADD COLUMN platform_fee_notes TEXT; -- Add comments for clarity COMMENT ON COLUMN organizations.platform_fee_type IS 'Fee type: percentage, fixed, percentage_plus_fixed'; COMMENT ON COLUMN organizations.platform_fee_percentage IS 'Percentage fee (0.03 = 3%)'; COMMENT ON COLUMN organizations.platform_fee_fixed IS 'Fixed fee in cents (30 = $0.30)'; COMMENT ON COLUMN organizations.platform_fee_notes IS 'Notes about the fee structure for this organization'; -- Update existing organizations with default fees UPDATE organizations SET platform_fee_type = 'percentage_plus_fixed', platform_fee_percentage = 0.0300, platform_fee_fixed = 30 WHERE platform_fee_type IS NULL; -- Add fee tracking to tickets table ALTER TABLE tickets ADD COLUMN platform_fee_charged INTEGER DEFAULT 0, ADD COLUMN organizer_net INTEGER DEFAULT 0; COMMENT ON COLUMN tickets.platform_fee_charged IS 'Platform fee charged in cents'; COMMENT ON COLUMN tickets.organizer_net IS 'Net amount organizer receives in cents'; -- Create fee_structures table for historical tracking and templates CREATE TABLE fee_structures ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), name TEXT NOT NULL, description TEXT, fee_type VARCHAR(20) NOT NULL DEFAULT 'percentage_plus_fixed', fee_percentage DECIMAL(5,4) DEFAULT 0.0000, fee_fixed INTEGER DEFAULT 0, is_template BOOLEAN DEFAULT false, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ); -- Insert some common fee structure templates INSERT INTO fee_structures (name, description, fee_type, fee_percentage, fee_fixed, is_template) VALUES ('Standard Platform Fee', 'Default 3% + $0.30 per transaction', 'percentage_plus_fixed', 0.0300, 30, true), ('Percentage Only 3%', '3% of transaction, no fixed fee', 'percentage', 0.0300, 0, true), ('Percentage Only 2.5%', '2.5% of transaction, no fixed fee', 'percentage', 0.0250, 0, true), ('Fixed Fee Only', '$1.00 flat fee per transaction', 'fixed', 0.0000, 100, true), ('Premium Rate', '3.5% + $0.50 for premium features', 'percentage_plus_fixed', 0.0350, 50, true), ('Volume Discount', '2% + $0.25 for high-volume clients', 'percentage_plus_fixed', 0.0200, 25, true); -- Enable RLS on fee_structures ALTER TABLE fee_structures ENABLE ROW LEVEL SECURITY; -- Anyone can read templates CREATE POLICY "Anyone can view fee structure templates" ON fee_structures FOR SELECT USING (is_template = true); -- Add indexes for performance CREATE INDEX idx_organizations_platform_fee_type ON organizations(platform_fee_type); CREATE INDEX idx_tickets_platform_fee ON tickets(platform_fee_charged); CREATE INDEX idx_fee_structures_template ON fee_structures(is_template);