query("SHOW COLUMNS FROM products LIKE 'member_price'")->fetchAll(); if (empty($columns)) { $db->exec("ALTER TABLE products ADD COLUMN member_price DECIMAL(15, 2) NOT NULL DEFAULT 0 AFTER selling_price"); // Update member_price to be slightly lower than selling_price for existing products $db->exec("UPDATE products SET member_price = selling_price * 0.9 WHERE member_price = 0"); } // 2. Create members table $db->exec("CREATE TABLE IF NOT EXISTS members ( id INT AUTO_INCREMENT PRIMARY KEY, code VARCHAR(50) UNIQUE NOT NULL, name VARCHAR(255) NOT NULL, phone VARCHAR(20), email VARCHAR(100), points INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB;"); // 3. Create sales table $db->exec("CREATE TABLE IF NOT EXISTS sales ( id INT AUTO_INCREMENT PRIMARY KEY, invoice_no VARCHAR(50) UNIQUE NOT NULL, member_id INT NULL, total_amount DECIMAL(15, 2) NOT NULL, discount DECIMAL(15, 2) DEFAULT 0, points_earned INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (member_id) REFERENCES members(id) ) ENGINE=InnoDB;"); // 4. Create sale_items table $db->exec("CREATE TABLE IF NOT EXISTS sale_items ( id INT AUTO_INCREMENT PRIMARY KEY, sale_id INT NOT NULL, product_id INT NOT NULL, quantity INT NOT NULL, price DECIMAL(15, 2) NOT NULL, subtotal DECIMAL(15, 2) NOT NULL, FOREIGN KEY (sale_id) REFERENCES sales(id), FOREIGN KEY (product_id) REFERENCES products(id) ) ENGINE=InnoDB;"); echo "Member and Points Tables Initialized"; } catch (Exception $e) { echo "Error: " . $e->getMessage(); }