-- Kayal Aqua 2025 - Database Setup -- Generated on: 2025-10-28 -- Use this file to set up the database SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00"; -- -------------------------------------------------------- -- Table structure for table `buyers` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `buyers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `phone` varchar(20) DEFAULT NULL, `rate_tilapia` decimal(10,2) DEFAULT NULL, `rate_small_fish` decimal(10,2) DEFAULT NULL, `rate_big_fish` decimal(10,2) DEFAULT NULL, `delivery_rate` decimal(10,2) DEFAULT NULL, `harvesting_fee_tilapia` decimal(10,2) DEFAULT NULL, `harvesting_fee_small_fish` decimal(10,2) DEFAULT NULL, `harvesting_fee_big_fish` decimal(10,2) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -------------------------------------------------------- -- Table structure for table `expenses` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `expenses` ( `id` int(11) NOT NULL AUTO_INCREMENT, `date` date NOT NULL, `buyer_id` int(11) DEFAULT NULL, `category_id` int(11) DEFAULT NULL, `head_id` int(11) DEFAULT NULL, `amount` decimal(10,2) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), KEY `buyer_id` (`buyer_id`), KEY `category_id` (`category_id`), KEY `head_id` (`head_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -------------------------------------------------------- -- Table structure for table `expense_categories` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `expense_categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -------------------------------------------------------- -- Table structure for table `expense_heads` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `expense_heads` ( `id` int(11) NOT NULL AUTO_INCREMENT, `category_id` int(11) DEFAULT NULL, `name` varchar(100) NOT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), KEY `category_id` (`category_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -------------------------------------------------------- -- Table structure for table `initial_values` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `initial_values` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cash_in_hand` decimal(10,2) NOT NULL DEFAULT 0.00, `madhu_balance` decimal(10,2) NOT NULL DEFAULT 0.00, `rathna_balance` decimal(10,2) NOT NULL DEFAULT 0.00, `jambukkutti_balance` decimal(10,2) NOT NULL DEFAULT 0.00, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- Insert default initial values INSERT INTO `initial_values` (`id`, `cash_in_hand`, `madhu_balance`, `rathna_balance`, `jambukkutti_balance`) VALUES (1, 0.00, 0.00, 0.00, 0.00); -- -------------------------------------------------------- -- Table structure for table `payments` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `payments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `date` date NOT NULL, `buyer_id` int(11) DEFAULT NULL, `amount_paid` decimal(10,2) DEFAULT NULL, `balance` decimal(10,2) DEFAULT NULL, `balance_override` decimal(10,2) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), KEY `buyer_id` (`buyer_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -------------------------------------------------------- -- Table structure for table `sales` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `sales` ( `id` int(11) NOT NULL AUTO_INCREMENT, `date` date NOT NULL, `buyer_id` int(11) DEFAULT NULL, `weight_tilapia` decimal(10,2) DEFAULT NULL, `weight_small_fish` decimal(10,2) DEFAULT NULL, `weight_big_fish` decimal(10,2) DEFAULT NULL, `include_delivery` tinyint(1) DEFAULT 0, `final_amount` decimal(10,2) DEFAULT NULL, `final_amount_override` decimal(10,2) DEFAULT NULL, `harvesting_charges` decimal(10,2) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), KEY `buyer_id` (`buyer_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -------------------------------------------------------- -- Table structure for table `users` -- -------------------------------------------------------- CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(255) NOT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- Insert default admin user (username: admin, password: admin123) INSERT INTO `users` (`username`, `password`) VALUES ('admin', '$2y$10$vWJYH9MCqNzKGzEqKLmXOe4jYZVQGZj5uyxqVZ7h.wjCKxJjr7Bfi'); -- -------------------------------------------------------- -- Add Foreign Keys -- -------------------------------------------------------- ALTER TABLE `expenses` ADD CONSTRAINT `expenses_ibfk_1` FOREIGN KEY (`buyer_id`) REFERENCES `buyers` (`id`) ON DELETE SET NULL, ADD CONSTRAINT `expenses_ibfk_2` FOREIGN KEY (`category_id`) REFERENCES `expense_categories` (`id`) ON DELETE SET NULL, ADD CONSTRAINT `expenses_ibfk_3` FOREIGN KEY (`head_id`) REFERENCES `expense_heads` (`id`) ON DELETE SET NULL; ALTER TABLE `expense_heads` ADD CONSTRAINT `expense_heads_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `expense_categories` (`id`) ON DELETE CASCADE; ALTER TABLE `payments` ADD CONSTRAINT `payments_ibfk_1` FOREIGN KEY (`buyer_id`) REFERENCES `buyers` (`id`) ON DELETE SET NULL; ALTER TABLE `sales` ADD CONSTRAINT `sales_ibfk_1` FOREIGN KEY (`buyer_id`) REFERENCES `buyers` (`id`) ON DELETE SET NULL; COMMIT;