-- https://www.phpmyadmin.net/
--

START TRANSACTION;



--
-- Database: `wumca`
--

-- --------------------------------------------------------

--
-- Table structure for table `accounting_periods`
--

CREATE TABLE `accounting_periods` (
  `id` int(10) UNSIGNED NOT NULL,
  `period_start` date NOT NULL,
  `period_end` date NOT NULL,
  `status` enum('open','closed') NOT NULL DEFAULT 'open',
  `closed_by` int(10) UNSIGNED DEFAULT NULL,
  `closed_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `alerts`
--

CREATE TABLE `alerts` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `alert_type` varchar(50) NOT NULL,
  `reference_type` varchar(50) NOT NULL,
  `reference_id` bigint(20) UNSIGNED NOT NULL,
  `target_role` varchar(50) NOT NULL,
  `message` varchar(255) NOT NULL,
  `is_read` tinyint(1) NOT NULL DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `read_by` int(10) UNSIGNED DEFAULT NULL,
  `read_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `approvals`
--

CREATE TABLE `approvals` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `approvable_type` varchar(50) NOT NULL,
  `approvable_id` bigint(20) UNSIGNED NOT NULL,
  `requested_by` int(10) UNSIGNED NOT NULL,
  `approver_role` varchar(50) NOT NULL,
  `status` enum('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  `decided_by` int(10) UNSIGNED DEFAULT NULL,
  `decision_notes` varchar(255) DEFAULT NULL,
  `requested_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `decided_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `approvals`
--

INSERT INTO `approvals` (`id`, `approvable_type`, `approvable_id`, `requested_by`, `approver_role`, `status`, `decided_by`, `decision_notes`, `requested_at`, `decided_at`) VALUES
(1, 'purchase_order', 1, 1, 'Accountant', 'approved', 1, 'good', '2026-08-02 04:50:06', '2026-08-02 04:59:33'),
(2, 'purchase_order', 1, 1, 'Production Manager', 'approved', 1, 'very good', '2026-08-02 04:59:33', '2026-08-02 05:00:02'),
(3, 'purchase_order', 2, 1, 'Accountant', 'approved', 1, 'good', '2026-08-03 04:39:17', '2026-08-03 04:39:41'),
(4, 'purchase_order', 2, 1, 'Production Manager', 'approved', 1, 'v good', '2026-08-03 04:39:41', '2026-08-03 04:39:56'),
(5, 'purchase_order', 3, 1, 'Accountant', 'rejected', 1, 'adjust the unit price', '2026-08-03 08:27:55', '2026-08-03 08:28:13'),
(6, 'purchase_order', 4, 1, 'Accountant', 'approved', 1, '', '2026-08-05 04:55:42', '2026-08-05 04:55:45'),
(7, 'purchase_order', 4, 1, 'Production Manager', 'approved', 1, '', '2026-08-05 04:55:45', '2026-08-05 04:55:46'),
(8, 'purchase_order', 5, 1, 'Accountant', 'approved', 1, '', '2026-08-27 16:43:51', '2026-08-27 16:43:58'),
(9, 'purchase_order', 5, 1, 'Production Manager', 'approved', 1, '', '2026-08-27 16:43:58', '2026-08-27 16:44:03'),
(10, 'expense_claim', 1, 1, 'Admin', 'pending', NULL, NULL, '2026-08-29 21:02:08', NULL);

-- --------------------------------------------------------

--
-- Table structure for table `assets_depreciation`
--

CREATE TABLE `assets_depreciation` (
  `id` int(10) UNSIGNED NOT NULL,
  `asset_name` varchar(150) NOT NULL,
  `purchase_cost` decimal(12,2) NOT NULL,
  `purchase_date` date NOT NULL,
  `useful_life_years` int(10) UNSIGNED NOT NULL,
  `salvage_value` decimal(12,2) NOT NULL DEFAULT 0.00,
  `depreciation_method` enum('straight_line') NOT NULL DEFAULT 'straight_line',
  `accumulated_depreciation` decimal(12,2) NOT NULL DEFAULT 0.00,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `disposed_at` date DEFAULT NULL,
  `disposal_proceeds` decimal(12,2) DEFAULT NULL,
  `disposal_journal_entry_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `assets_depreciation`
--

INSERT INTO `assets_depreciation` (`id`, `asset_name`, `purchase_cost`, `purchase_date`, `useful_life_years`, `salvage_value`, `depreciation_method`, `accumulated_depreciation`, `is_active`, `disposed_at`, `disposal_proceeds`, `disposal_journal_entry_id`, `created_by`, `created_at`) VALUES
(1, 'Wring', 543000.00, '2026-06-01', 5, 0.00, 'straight_line', 9050.00, 1, NULL, NULL, NULL, 1, '2026-08-27 08:43:04'),
(2, 'Asset 02', 875000.00, '2026-03-02', 1, 150000.00, 'straight_line', 0.00, 1, NULL, NULL, NULL, 1, '2026-08-27 09:37:16');

-- --------------------------------------------------------

--
-- Table structure for table `asset_depreciation_runs`
--

CREATE TABLE `asset_depreciation_runs` (
  `id` int(10) UNSIGNED NOT NULL,
  `asset_id` int(10) UNSIGNED NOT NULL,
  `period_start` date NOT NULL,
  `period_end` date NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `journal_entry_id` bigint(20) UNSIGNED NOT NULL,
  `run_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `asset_depreciation_runs`
--

INSERT INTO `asset_depreciation_runs` (`id`, `asset_id`, `period_start`, `period_end`, `amount`, `journal_entry_id`, `run_by`, `created_at`) VALUES
(1, 1, '2026-08-01', '2026-08-31', 9050.00, 4, 1, '2026-08-27 08:43:27');

-- --------------------------------------------------------

--
-- Table structure for table `bank_accounts`
--

CREATE TABLE `bank_accounts` (
  `id` int(10) UNSIGNED NOT NULL,
  `account_name` varchar(120) NOT NULL,
  `bank_name` varchar(120) NOT NULL,
  `account_number` varchar(30) NOT NULL,
  `chart_of_accounts_id` int(10) UNSIGNED NOT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `bank_accounts`
--

INSERT INTO `bank_accounts` (`id`, `account_name`, `bank_name`, `account_number`, `chart_of_accounts_id`, `is_active`, `created_at`) VALUES
(1, 'Account 01', 'Access Bank', '0022558899', 16, 1, '2026-08-27 08:36:03'),
(2, 'Account 02', 'Monie Point', '0078541236', 20, 1, '2026-08-28 09:23:29');

-- --------------------------------------------------------

--
-- Table structure for table `chart_of_accounts`
--

CREATE TABLE `chart_of_accounts` (
  `id` int(10) UNSIGNED NOT NULL,
  `code` varchar(20) NOT NULL,
  `name` varchar(120) NOT NULL,
  `account_type` enum('asset','liability','equity','income','expense') NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `chart_of_accounts`
--

INSERT INTO `chart_of_accounts` (`id`, `code`, `name`, `account_type`, `created_at`) VALUES
(1, '1000', 'Cash', 'asset', '2026-08-01 06:02:59'),
(2, '1100', 'Accounts Receivable', 'asset', '2026-08-01 06:02:59'),
(3, '1200', 'Raw Materials Inventory', 'asset', '2026-08-01 06:02:59'),
(4, '1300', 'Finished Goods Inventory', 'asset', '2026-08-01 06:02:59'),
(5, '1500', 'Equipment', 'asset', '2026-08-01 06:02:59'),
(6, '2000', 'Accounts Payable', 'liability', '2026-08-01 06:02:59'),
(7, '3000', 'Owner Equity', 'equity', '2026-08-01 06:02:59'),
(8, '4000', 'Sales Revenue', 'income', '2026-08-01 06:02:59'),
(9, '5000', 'Cost of Goods Sold', 'expense', '2026-08-01 06:02:59'),
(10, '5100', 'Direct Labor Expense', 'expense', '2026-08-01 06:02:59'),
(11, '5200', 'Manufacturing Overhead', 'expense', '2026-08-01 06:02:59'),
(12, '5300', 'Depreciation Expense', 'expense', '2026-08-01 06:02:59'),
(13, '5400', 'Operating Expenses', 'expense', '2026-08-01 06:02:59'),
(14, '5250', 'Waste / Spoilage Expense', 'expense', '2026-08-03 16:41:27'),
(15, '1550', 'Accumulated Depreciation', 'asset', '2026-08-26 14:52:51'),
(16, '1010', 'Account 01', 'asset', '2026-08-27 08:36:03'),
(17, '4300', 'Gain on Disposal of Assets', 'income', '2026-08-27 09:32:08'),
(18, '5350', 'Loss on Disposal of Assets', 'expense', '2026-08-27 09:32:08'),
(19, '1250', 'Work In Process', 'asset', '2026-08-28 02:37:58'),
(20, '1011', 'Account 02', 'asset', '2026-08-28 09:23:29'),
(21, '4900', 'Other Income', 'income', '2026-08-29 21:25:43'),
(22, '2100', 'VAT Payable', 'liability', '2026-08-30 03:42:37'),
(23, '5150', 'Salaries & Wages Expense', 'expense', '2026-08-30 03:42:37');

-- --------------------------------------------------------

--
-- Table structure for table `customers`
--

CREATE TABLE `customers` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `type` enum('walk_in','marketer','distributor') NOT NULL DEFAULT 'walk_in',
  `phone` varchar(30) DEFAULT NULL,
  `email` varchar(150) DEFAULT NULL,
  `credit_limit` decimal(12,2) NOT NULL DEFAULT 0.00,
  `balance_due` decimal(12,2) NOT NULL DEFAULT 0.00,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `customers`
--

INSERT INTO `customers` (`id`, `name`, `type`, `phone`, `email`, `credit_limit`, `balance_due`, `created_at`) VALUES
(1, 'lanre', 'marketer', '0205879465', '', 500000.00, 19780.00, '2026-08-27 08:48:11'),
(2, 'Tobiloba', 'distributor', '07085236987', '', 430000.00, 12760.00, '2026-08-27 08:48:41');

-- --------------------------------------------------------

--
-- Table structure for table `documents`
--

CREATE TABLE `documents` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `attachable_type` varchar(50) NOT NULL,
  `attachable_id` bigint(20) UNSIGNED NOT NULL,
  `file_path` varchar(255) NOT NULL,
  `original_filename` varchar(255) NOT NULL,
  `file_size` int(10) UNSIGNED NOT NULL,
  `mime_type` varchar(100) NOT NULL,
  `uploaded_by` int(10) UNSIGNED NOT NULL,
  `uploaded_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `expense_categories`
--

CREATE TABLE `expense_categories` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(80) NOT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `expense_categories`
--

INSERT INTO `expense_categories` (`id`, `name`, `is_active`, `created_by`, `created_at`) VALUES
(1, 'Transport', 1, NULL, '2026-08-29 21:25:42'),
(2, 'Supplies', 1, NULL, '2026-08-29 21:25:42'),
(3, 'Maintenance', 1, NULL, '2026-08-29 21:25:42'),
(4, 'Utilities', 1, NULL, '2026-08-29 21:25:42'),
(5, 'Other', 1, NULL, '2026-08-29 21:25:42');

-- --------------------------------------------------------

--
-- Table structure for table `expense_claims`
--

CREATE TABLE `expense_claims` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `claim_code` varchar(40) NOT NULL,
  `claimed_by` int(10) UNSIGNED NOT NULL,
  `category_id` int(10) UNSIGNED NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `status` enum('pending','approved','rejected','reimbursed') NOT NULL DEFAULT 'pending',
  `approved_by` int(10) UNSIGNED DEFAULT NULL,
  `approved_at` timestamp NULL DEFAULT NULL,
  `reimbursed_at` timestamp NULL DEFAULT NULL,
  `payment_method` enum('cash','bank_transfer') DEFAULT NULL,
  `bank_account_id` int(10) UNSIGNED DEFAULT NULL,
  `reference` varchar(100) DEFAULT NULL,
  `journal_entry_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `expense_claims`
--

INSERT INTO `expense_claims` (`id`, `claim_code`, `claimed_by`, `category_id`, `amount`, `description`, `status`, `approved_by`, `approved_at`, `reimbursed_at`, `payment_method`, `bank_account_id`, `reference`, `journal_entry_id`, `created_at`) VALUES
(1, 'EXP-20260829-9A4E6', 1, 3, 50000.00, 'Gen repair', 'pending', NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2026-08-29 21:02:08');

-- --------------------------------------------------------

--
-- Table structure for table `income_categories`
--

CREATE TABLE `income_categories` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(80) NOT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `income_categories`
--

INSERT INTO `income_categories` (`id`, `name`, `is_active`, `created_by`, `created_at`) VALUES
(1, 'Interest', 1, NULL, '2026-08-29 21:25:43'),
(2, 'Scrap/Waste Sales', 1, NULL, '2026-08-29 21:25:43'),
(3, 'Rental', 1, NULL, '2026-08-29 21:25:43'),
(4, 'Refund', 1, NULL, '2026-08-29 21:25:43'),
(5, 'Other', 1, NULL, '2026-08-29 21:25:43');

-- --------------------------------------------------------

--
-- Table structure for table `income_records`
--

CREATE TABLE `income_records` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `income_code` varchar(40) NOT NULL,
  `category_id` int(10) UNSIGNED NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `payment_method` enum('cash','bank_transfer') NOT NULL,
  `bank_account_id` int(10) UNSIGNED DEFAULT NULL,
  `reference` varchar(100) DEFAULT NULL,
  `recorded_by` int(10) UNSIGNED NOT NULL,
  `journal_entry_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `item_transfers`
--

CREATE TABLE `item_transfers` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `transfer_code` varchar(40) NOT NULL,
  `from_warehouse_id` int(10) UNSIGNED NOT NULL,
  `to_warehouse_id` int(10) UNSIGNED NOT NULL,
  `raw_material_id` int(10) UNSIGNED NOT NULL,
  `quantity` decimal(12,3) NOT NULL,
  `selling_price` decimal(10,2) DEFAULT NULL,
  `status` enum('pending','received','cancelled') NOT NULL DEFAULT 'pending',
  `requested_by` int(10) UNSIGNED NOT NULL,
  `received_by` int(10) UNSIGNED DEFAULT NULL,
  `requested_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `received_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `item_transfers`
--

INSERT INTO `item_transfers` (`id`, `transfer_code`, `from_warehouse_id`, `to_warehouse_id`, `raw_material_id`, `quantity`, `selling_price`, `status`, `requested_by`, `received_by`, `requested_at`, `received_at`) VALUES
(1, 'TR-20260802-A6803', 1, 2, 2, 2.000, NULL, 'received', 1, 1, '2026-08-02 04:34:23', '2026-08-02 04:34:33');

-- --------------------------------------------------------

--
-- Table structure for table `journal_entries`
--

CREATE TABLE `journal_entries` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `entry_date` date NOT NULL,
  `memo` varchar(255) DEFAULT NULL,
  `reference_type` varchar(50) DEFAULT NULL,
  `reference_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `journal_entries`
--

INSERT INTO `journal_entries` (`id`, `entry_date`, `memo`, `reference_type`, `reference_id`, `created_by`, `created_at`) VALUES
(1, '2026-08-03', 'Production batch B-20260803-3432D', 'production_batch', 1, 2, '2026-08-03 18:43:51'),
(2, '2026-08-03', 'QC reject: 2 unit(s) from batch B-20260803-3432D', 'qc_reject', 1, 1, '2026-08-03 20:54:23'),
(3, '2026-08-05', 'Production batch B-20260805-7A547', 'production_batch', 2, 2, '2026-08-05 04:59:06'),
(4, '2026-08-31', 'Depreciation: Wring (2026-08-01 to 2026-08-31)', 'asset_depreciation', 1, 1, '2026-08-27 08:43:27'),
(5, '2026-08-27', 'Sales order SO-20260827-7C5B1', 'sales_order', 1, 1, '2026-08-27 08:49:37'),
(6, '2026-08-27', 'Sales order SO-20260827-F7858', 'sales_order', 2, 1, '2026-08-27 08:56:10'),
(7, '2026-08-27', 'Sales order SO-20260827-B70EB', 'sales_order', 3, 1, '2026-08-27 09:33:51'),
(8, '2026-03-02', 'Asset acquisition: Asset 02', 'asset_acquisition', 2, 1, '2026-08-27 09:37:16'),
(9, '2026-08-27', 'Production batch B-20260827-B9784', 'production_batch', 3, 2, '2026-08-27 15:52:57'),
(10, '2026-08-27', 'Production batch B-20260827-47F72', 'production_batch', 4, 2, '2026-08-27 17:16:35'),
(11, '2026-08-28', 'Production batch B-20260828-C9E2D', 'production_batch', 5, 2, '2026-08-28 01:53:58'),
(12, '2026-08-28', 'QC reject: 2 unit(s) from batch B-20260828-C9E2D', 'qc_reject', 5, 1, '2026-08-28 01:56:56'),
(13, '2026-08-28', 'Material release: PR-20260828-15EF4', 'material_request', 1, 1, '2026-08-28 03:30:24'),
(14, '2026-08-28', 'Production batch B-20260828-DF715', 'production_batch', 7, 2, '2026-08-28 04:32:58'),
(15, '2026-08-28', 'QC reject: 1 unit(s) from batch B-20260828-DF715', 'qc_reject', 7, 1, '2026-08-28 04:33:53'),
(16, '2026-08-28', 'Production batch B-20260828-0807B', 'production_batch', 10, 2, '2026-08-28 05:04:31'),
(17, '2026-08-28', 'Production batch B-20260828-55C35', 'production_batch', 11, 2, '2026-08-28 05:04:31'),
(18, '2026-08-28', 'QC reject: 1 unit(s) from batch B-20260828-55C35', 'qc_reject', 11, 1, '2026-08-28 05:07:53'),
(19, '2026-08-28', 'Material release: PR-20260828-E96AD', 'material_request', 2, 1, '2026-08-28 06:27:50'),
(20, '2026-08-28', 'Material release: PR-20260828-61853', 'material_request', 3, 1, '2026-08-28 06:28:22'),
(21, '2026-08-28', 'Production batch B-20260828-8144F', 'production_batch', 12, 2, '2026-08-28 06:29:20'),
(22, '2026-08-28', 'Production batch B-20260828-5E03B', 'production_batch', 13, 2, '2026-08-28 06:29:20'),
(23, '2026-08-28', 'Production batch B-20260828-9D134', 'production_batch', 14, 2, '2026-08-28 06:30:45'),
(24, '2026-08-28', 'Production batch B-20260828-727D3', 'production_batch', 15, 2, '2026-08-28 06:30:45'),
(25, '2026-08-28', 'Production batch B-20260828-CB6A1', 'production_batch', 16, 2, '2026-08-28 06:30:45'),
(26, '2026-08-28', 'QC reject: 1 unit(s) from batch B-20260828-727D3', 'qc_reject', 15, 1, '2026-08-28 06:32:03'),
(27, '2026-08-28', 'Sales order SO-20260828-EA102', 'sales_order', 4, 1, '2026-08-28 09:24:27'),
(28, '2026-08-28', 'COGS: SO-20260828-EA102', 'sales_order_cogs', 4, 1, '2026-08-28 09:24:27'),
(29, '2026-08-29', 'Payment received: SO-20260827-B70EB', 'sales_order_payment', 3, 1, '2026-08-29 11:54:10'),
(30, '2026-08-03', 'Backfill: goods received PO-20260802-545D0 (historical, predates AP tracking)', 'purchase_order', 1, 1, '2026-08-29 12:55:45'),
(31, '2026-08-03', 'Backfill: goods received PO-20260803-6B750 (historical, predates AP tracking)', 'purchase_order', 2, 1, '2026-08-29 12:55:45'),
(32, '2026-08-05', 'Backfill: goods received PO-20260805-6B9E4 (historical, predates AP tracking)', 'purchase_order', 4, 1, '2026-08-29 12:55:45');

-- --------------------------------------------------------

--
-- Table structure for table `journal_lines`
--

CREATE TABLE `journal_lines` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `journal_entry_id` bigint(20) UNSIGNED NOT NULL,
  `account_id` int(10) UNSIGNED NOT NULL,
  `debit` decimal(14,2) NOT NULL DEFAULT 0.00,
  `credit` decimal(14,2) NOT NULL DEFAULT 0.00
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `journal_lines`
--

INSERT INTO `journal_lines` (`id`, `journal_entry_id`, `account_id`, `debit`, `credit`) VALUES
(1, 1, 4, 333100.00, 0.00),
(2, 1, 3, 0.00, 333100.00),
(3, 2, 14, 66620.00, 0.00),
(4, 2, 4, 0.00, 66620.00),
(5, 3, 4, 305300.00, 0.00),
(6, 3, 3, 0.00, 305300.00),
(7, 4, 12, 9050.00, 0.00),
(8, 4, 15, 0.00, 9050.00),
(9, 5, 2, 12980.00, 0.00),
(10, 5, 8, 0.00, 12980.00),
(11, 6, 2, 12760.00, 0.00),
(12, 6, 8, 0.00, 12760.00),
(13, 7, 2, 13800.00, 0.00),
(14, 7, 8, 0.00, 13800.00),
(15, 8, 5, 875000.00, 0.00),
(16, 8, 1, 0.00, 875000.00),
(17, 9, 4, 147600.00, 0.00),
(18, 9, 3, 0.00, 147600.00),
(19, 10, 4, 261412.50, 0.00),
(20, 10, 3, 0.00, 261412.50),
(21, 11, 4, 305300.00, 0.00),
(22, 11, 3, 0.00, 305300.00),
(23, 12, 14, 24424.00, 0.00),
(24, 12, 4, 0.00, 24424.00),
(25, 13, 19, 742112.50, 0.00),
(26, 13, 3, 0.00, 742112.50),
(27, 14, 4, 147600.00, 0.00),
(28, 14, 19, 0.00, 147600.00),
(29, 15, 14, 12300.00, 0.00),
(30, 15, 4, 0.00, 12300.00),
(31, 16, 4, 333100.00, 0.00),
(32, 16, 19, 0.00, 333100.00),
(33, 17, 4, 261412.50, 0.00),
(34, 17, 19, 0.00, 261412.50),
(35, 18, 14, 898.32, 0.00),
(36, 18, 4, 0.00, 898.32),
(37, 19, 19, 638400.00, 0.00),
(38, 19, 3, 0.00, 638400.00),
(39, 20, 19, 2699437.50, 0.00),
(40, 20, 3, 0.00, 2699437.50),
(41, 21, 4, 333100.00, 0.00),
(42, 21, 19, 0.00, 333100.00),
(43, 22, 4, 305300.00, 0.00),
(44, 22, 19, 0.00, 305300.00),
(45, 23, 4, 784237.50, 0.00),
(46, 23, 19, 0.00, 784237.50),
(47, 24, 4, 915900.00, 0.00),
(48, 24, 19, 0.00, 915900.00),
(49, 25, 4, 999300.00, 0.00),
(50, 25, 19, 0.00, 999300.00),
(51, 26, 14, 13084.29, 0.00),
(52, 26, 4, 0.00, 13084.29),
(53, 27, 16, 1000.00, 0.00),
(54, 27, 1, 700.00, 0.00),
(55, 27, 8, 0.00, 1700.00),
(56, 28, 9, 33806.06, 0.00),
(57, 28, 4, 0.00, 33806.06),
(58, 29, 16, 7000.00, 0.00),
(59, 29, 2, 0.00, 7000.00),
(60, 30, 3, 4056000.00, 0.00),
(61, 30, 6, 0.00, 4056000.00),
(62, 31, 3, 23111000.00, 0.00),
(63, 31, 6, 0.00, 23111000.00),
(64, 32, 3, 112500.00, 0.00),
(65, 32, 6, 0.00, 112500.00);

-- --------------------------------------------------------

--
-- Table structure for table `login_attempts`
--

CREATE TABLE `login_attempts` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `username` varchar(100) NOT NULL,
  `succeeded` tinyint(1) NOT NULL,
  `attempted_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `login_attempts`
--

INSERT INTO `login_attempts` (`id`, `username`, `succeeded`, `attempted_at`) VALUES
(1, 'admin@gmail.com', 1, '2026-08-30 04:25:36'),
(2, 'admin@gmail.com', 1, '2026-08-30 05:08:27'),
(3, 'admin@gmail.com', 1, '2026-09-06 16:09:51');

-- --------------------------------------------------------

--
-- Table structure for table `material_requests`
--

CREATE TABLE `material_requests` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `request_code` varchar(40) NOT NULL,
  `recipe_id` int(10) UNSIGNED DEFAULT NULL,
  `requested_batches` int(10) UNSIGNED DEFAULT NULL,
  `status` enum('pending','released','partially_released','rejected') NOT NULL DEFAULT 'pending',
  `requested_by` int(10) UNSIGNED NOT NULL,
  `released_by` int(10) UNSIGNED DEFAULT NULL,
  `requested_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `released_at` timestamp NULL DEFAULT NULL,
  `notes` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `material_requests`
--

INSERT INTO `material_requests` (`id`, `request_code`, `recipe_id`, `requested_batches`, `status`, `requested_by`, `released_by`, `requested_at`, `released_at`, `notes`) VALUES
(1, 'PR-20260828-15EF4', NULL, NULL, 'released', 1, 1, '2026-08-28 03:27:39', '2026-08-28 03:30:24', NULL),
(2, 'PR-20260828-E96AD', NULL, NULL, 'released', 1, 1, '2026-08-28 03:31:58', '2026-08-28 06:27:50', NULL),
(3, 'PR-20260828-61853', NULL, NULL, 'released', 1, 1, '2026-08-28 06:27:25', '2026-08-28 06:28:22', NULL);

-- --------------------------------------------------------

--
-- Table structure for table `material_request_items`
--

CREATE TABLE `material_request_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `material_request_id` bigint(20) UNSIGNED DEFAULT NULL,
  `material_request_product_id` bigint(20) UNSIGNED NOT NULL,
  `raw_material_id` int(10) UNSIGNED NOT NULL,
  `quantity_requested` decimal(12,3) NOT NULL,
  `quantity_released` decimal(12,3) DEFAULT NULL,
  `unit_cost_at_release` decimal(10,4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `material_request_items`
--

INSERT INTO `material_request_items` (`id`, `material_request_id`, `material_request_product_id`, `raw_material_id`, `quantity_requested`, `quantity_released`, `unit_cost_at_release`) VALUES
(1, NULL, 1, 1, 1.000, 1.000, 52000.0000),
(2, NULL, 1, 2, 2.971, 2.971, 37500.0000),
(3, NULL, 1, 3, 5.000, 5.000, 17800.0000),
(4, NULL, 1, 4, 2.000, 2.000, 4500.0000),
(5, NULL, 2, 1, 1.000, 1.000, 52000.0000),
(6, NULL, 2, 2, 1.000, 1.000, 37500.0000),
(7, NULL, 2, 3, 2.000, 2.000, 17800.0000),
(8, NULL, 2, 4, 5.000, 5.000, 4500.0000),
(9, NULL, 3, 1, 5.000, 5.000, 52000.0000),
(10, NULL, 3, 2, 1.000, 1.000, 37500.0000),
(11, NULL, 3, 3, 2.000, 2.000, 17800.0000),
(12, NULL, 4, 1, 4.000, 4.000, 52000.0000),
(13, NULL, 4, 2, 2.000, 2.000, 37500.0000),
(14, NULL, 4, 3, 1.000, 1.000, 17800.0000),
(15, NULL, 4, 4, 1.000, 1.000, 4500.0000),
(16, NULL, 5, 1, 5.000, 5.000, 52000.0000),
(17, NULL, 5, 2, 1.000, 1.000, 37500.0000),
(18, NULL, 5, 3, 2.000, 2.000, 17800.0000),
(19, NULL, 6, 1, 15.000, 15.000, 52000.0000),
(20, NULL, 6, 2, 3.000, 3.000, 37500.0000),
(21, NULL, 6, 3, 6.000, 6.000, 17800.0000),
(22, NULL, 7, 1, 12.000, 12.000, 52000.0000),
(23, NULL, 7, 2, 6.000, 6.000, 37500.0000),
(24, NULL, 7, 3, 3.000, 3.000, 17800.0000),
(25, NULL, 7, 4, 3.000, 3.000, 4500.0000),
(26, NULL, 8, 1, 3.000, 3.000, 52000.0000),
(27, NULL, 8, 2, 8.913, 8.913, 37500.0000),
(28, NULL, 8, 3, 15.000, 15.000, 17800.0000),
(29, NULL, 8, 4, 6.000, 6.000, 4500.0000);

-- --------------------------------------------------------

--
-- Table structure for table `material_request_products`
--

CREATE TABLE `material_request_products` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `material_request_id` bigint(20) UNSIGNED NOT NULL,
  `recipe_id` int(10) UNSIGNED NOT NULL,
  `requested_batches` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `material_request_products`
--

INSERT INTO `material_request_products` (`id`, `material_request_id`, `recipe_id`, `requested_batches`) VALUES
(1, 1, 4, 1),
(2, 1, 3, 1),
(3, 1, 1, 1),
(4, 2, 2, 1),
(5, 2, 1, 1),
(6, 3, 1, 3),
(7, 3, 2, 3),
(8, 3, 4, 3);

-- --------------------------------------------------------

--
-- Table structure for table `metrics`
--

CREATE TABLE `metrics` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(60) NOT NULL,
  `abbreviation` varchar(15) NOT NULL,
  `equivalent_quantity` decimal(12,3) DEFAULT NULL,
  `equivalent_unit_id` int(10) UNSIGNED DEFAULT NULL,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `metrics`
--

INSERT INTO `metrics` (`id`, `name`, `abbreviation`, `equivalent_quantity`, `equivalent_unit_id`, `created_by`, `created_at`) VALUES
(1, 'Kilogram', 'kg', NULL, NULL, NULL, '2026-08-02 03:54:34'),
(2, 'Gram', 'g', NULL, NULL, NULL, '2026-08-02 03:54:34'),
(3, 'Galon', 'galon', 25.000, 4, NULL, '2026-08-02 03:54:34'),
(4, 'Millilitre', 'ml', NULL, NULL, NULL, '2026-08-02 03:54:34'),
(5, 'Bag', 'bag', 50.000, 1, NULL, '2026-08-02 03:54:34'),
(6, 'Pack', 'pack', 12.000, 3, NULL, '2026-08-02 03:54:34'),
(7, 'Sachet', 'sachet', 12.000, 2, NULL, '2026-08-02 03:54:34'),
(8, 'Pieces', 'Pieces', 12.000, 7, NULL, '2026-08-02 03:54:34'),
(9, 'dam', 'dam', NULL, NULL, NULL, '2026-08-02 04:15:20');

-- --------------------------------------------------------

--
-- Table structure for table `packaging_logs`
--

CREATE TABLE `packaging_logs` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `production_batch_id` bigint(20) UNSIGNED NOT NULL,
  `packaging_type` enum('retail','wholesale','rough') NOT NULL,
  `quantity_packed` int(10) UNSIGNED NOT NULL,
  `quantity_remaining` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `packed_by` int(10) UNSIGNED NOT NULL,
  `notes` varchar(255) DEFAULT NULL,
  `packed_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `packaging_logs`
--

INSERT INTO `packaging_logs` (`id`, `production_batch_id`, `packaging_type`, `quantity_packed`, `quantity_remaining`, `packed_by`, `notes`, `packed_at`) VALUES
(1, 1, 'wholesale', 4, 4, 1, NULL, '2026-08-03 20:56:55'),
(2, 1, 'retail', 2, 1, 1, 'retailssss', '2026-08-03 20:57:28'),
(3, 2, 'retail', 1510, 1509, 1, NULL, '2026-08-27 16:46:49'),
(4, 3, 'retail', 500, 499, 1, NULL, '2026-08-27 16:47:07'),
(5, 4, 'retail', 195, 195, 1, NULL, '2026-08-27 17:18:39'),
(6, 10, 'rough', 1, 1, 2, 'Auto-packaged rough loaf byproduct at production time', '2026-08-28 05:04:31'),
(7, 11, 'rough', 5, 5, 2, 'Auto-packaged rough loaf byproduct at production time', '2026-08-28 05:04:31'),
(8, 12, 'rough', 2, 2, 2, 'Auto-packaged rough loaf byproduct at production time', '2026-08-28 06:29:20'),
(9, 13, 'rough', 5, 5, 2, 'Auto-packaged rough loaf byproduct at production time', '2026-08-28 06:29:20'),
(10, 14, 'rough', 10, 10, 2, 'Auto-packaged rough loaf byproduct at production time', '2026-08-28 06:30:45'),
(11, 15, 'rough', 5, 5, 2, 'Auto-packaged rough loaf byproduct at production time', '2026-08-28 06:30:45'),
(12, 16, 'rough', 1, 1, 2, 'Auto-packaged rough loaf byproduct at production time', '2026-08-28 06:30:45');

-- --------------------------------------------------------

--
-- Table structure for table `payroll_runs`
--

CREATE TABLE `payroll_runs` (
  `id` int(10) UNSIGNED NOT NULL,
  `period_start` date NOT NULL,
  `period_end` date NOT NULL,
  `status` enum('draft','approved','paid') NOT NULL DEFAULT 'draft',
  `created_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `payroll_runs`
--

INSERT INTO `payroll_runs` (`id`, `period_start`, `period_end`, `status`, `created_by`, `created_at`) VALUES
(1, '2026-07-22', '2026-07-31', 'draft', 1, '2026-08-01 11:26:31');

-- --------------------------------------------------------

--
-- Table structure for table `payslips`
--

CREATE TABLE `payslips` (
  `id` int(10) UNSIGNED NOT NULL,
  `payroll_run_id` int(10) UNSIGNED NOT NULL,
  `user_id` int(10) UNSIGNED NOT NULL,
  `hours_total` decimal(8,2) NOT NULL DEFAULT 0.00,
  `sessions_total` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `batches_total` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `gross_pay` decimal(12,2) NOT NULL DEFAULT 0.00,
  `deductions` decimal(12,2) NOT NULL DEFAULT 0.00,
  `net_pay` decimal(12,2) NOT NULL DEFAULT 0.00,
  `deduction_notes` varchar(255) DEFAULT NULL,
  `generated_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `permissions`
--

CREATE TABLE `permissions` (
  `id` int(10) UNSIGNED NOT NULL,
  `slug` varchar(80) NOT NULL,
  `name` varchar(120) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `category` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `permissions`
--

INSERT INTO `permissions` (`id`, `slug`, `name`, `description`, `category`) VALUES
(1, 'approve_purchase_orders', 'Approve Purchase Orders', 'Approve or reject purchase orders awaiting sign-off', 'Purchasing'),
(2, 'approve_expense_claims', 'Approve Expense Claims', 'Approve or reject staff expense claims', 'Finance'),
(3, 'approve_salary_advances', 'Approve Salary Advances/Deductions', 'Approve or reject salary advance and deduction requests', 'Payroll'),
(4, 'approve_sales_credit', 'Approve Credit-Blocked Sales', 'Approve a sale that exceeds a customer\'s credit limit', 'Sales'),
(5, 'manage_payroll', 'Manage Payroll', 'Run payroll and view payslips', 'Payroll'),
(6, 'manage_employees', 'Manage Employees', 'Create and edit employee accounts', 'HR'),
(7, 'manage_roles_permissions', 'Manage Roles & Permissions', 'Create roles and assign permissions', 'HR'),
(8, 'view_financial_reports', 'View Financial Reports', 'View the ledger, P&L, and aging reports', 'Finance'),
(9, 'close_accounting_periods', 'Close Accounting Periods', 'Lock a date range against further ledger postings', 'Finance'),
(10, 'manage_inventory', 'Manage Inventory', 'Adjust stock, process returns, manage suppliers', 'Inventory'),
(11, 'process_sales', 'Process Sales', 'Use the POS and manage sales orders', 'Sales'),
(12, 'manage_production', 'Manage Production', 'Manage recipes, batches, and material requests', 'Production'),
(13, 'mark_attendance', 'Mark Attendance', 'Record casual staff attendance by session', 'HR'),
(14, 'manage_settings', 'Manage Settings', 'Change system-wide settings such as VAT', 'System');

-- --------------------------------------------------------

--
-- Table structure for table `pos_terminals`
--

CREATE TABLE `pos_terminals` (
  `id` int(10) UNSIGNED NOT NULL,
  `terminal_name` varchar(80) NOT NULL,
  `terminal_code` varchar(30) NOT NULL,
  `bank_account_id` int(10) UNSIGNED NOT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `pos_terminals`
--

INSERT INTO `pos_terminals` (`id`, `terminal_name`, `terminal_code`, `bank_account_id`, `is_active`, `created_at`) VALUES
(1, 'Terminal 01', 'T01', 1, 1, '2026-08-27 08:36:52'),
(2, 'Terminal 02', 'T02', 2, 1, '2026-08-28 09:24:03');

-- --------------------------------------------------------

--
-- Table structure for table `production_batches`
--

CREATE TABLE `production_batches` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `batch_code` varchar(40) NOT NULL,
  `recipe_id` int(10) UNSIGNED NOT NULL,
  `material_request_id` bigint(20) UNSIGNED DEFAULT NULL,
  `material_request_product_id` bigint(20) UNSIGNED DEFAULT NULL,
  `baker_id` int(10) UNSIGNED NOT NULL,
  `batches_run` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `output_quantity` int(10) UNSIGNED NOT NULL,
  `reject_quantity` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `rough_loaf_quantity` int(10) UNSIGNED NOT NULL DEFAULT 0,
  `labor_cost` decimal(12,2) NOT NULL DEFAULT 0.00,
  `material_cost` decimal(12,2) NOT NULL DEFAULT 0.00,
  `overhead_cost` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total_cost` decimal(12,2) NOT NULL DEFAULT 0.00,
  `unit_cost` decimal(12,4) NOT NULL DEFAULT 0.0000,
  `status` enum('in_progress','completed','cancelled') NOT NULL DEFAULT 'completed',
  `qc_status` enum('pending','pass','partial','fail') NOT NULL DEFAULT 'pending',
  `produced_at` datetime NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `production_batches`
--

INSERT INTO `production_batches` (`id`, `batch_code`, `recipe_id`, `material_request_id`, `material_request_product_id`, `baker_id`, `batches_run`, `output_quantity`, `reject_quantity`, `rough_loaf_quantity`, `labor_cost`, `material_cost`, `overhead_cost`, `total_cost`, `unit_cost`, `status`, `qc_status`, `produced_at`, `created_at`) VALUES
(1, 'B-20260803-3432D', 1, NULL, NULL, 2, 1, 10, 2, 0, 0.00, 333100.00, 0.00, 333100.00, 33310.0000, 'completed', 'pass', '2026-08-02 19:43:00', '2026-08-03 18:43:51'),
(2, 'B-20260805-7A547', 2, NULL, NULL, 2, 1, 1520, 10, 0, 0.00, 305300.00, 0.00, 305300.00, 200.8550, 'completed', 'pass', '2026-08-04 17:58:00', '2026-08-05 04:59:06'),
(3, 'B-20260827-B9784', 3, NULL, NULL, 2, 1, 500, 0, 0, 0.00, 147600.00, 0.00, 147600.00, 295.2000, 'completed', 'pass', '2026-08-26 16:52:00', '2026-08-27 15:52:57'),
(4, 'B-20260827-47F72', 4, NULL, NULL, 2, 1, 300, 5, 0, 0.00, 261412.50, 0.00, 261412.50, 871.3750, 'completed', 'pass', '2026-08-27 18:16:00', '2026-08-27 17:16:35'),
(5, 'B-20260828-C9E2D', 2, NULL, NULL, 2, 1, 25, 2, 0, 0.00, 305300.00, 0.00, 305300.00, 12212.0000, 'completed', 'pass', '2026-08-28 02:53:00', '2026-08-28 01:53:58'),
(7, 'B-20260828-DF715', 3, NULL, 2, 2, 1, 12, 1, 0, 0.00, 147600.00, 0.00, 147600.00, 12300.0000, 'completed', 'pass', '2026-08-27 05:32:00', '2026-08-28 04:32:58'),
(10, 'B-20260828-0807B', 1, NULL, 3, 2, 1, 8, 1, 1, 0.00, 333100.00, 0.00, 333100.00, 41637.5000, 'completed', 'pass', '2026-08-28 06:04:00', '2026-08-28 05:04:31'),
(11, 'B-20260828-55C35', 4, NULL, 1, 2, 1, 291, 4, 5, 0.00, 261412.50, 0.00, 261412.50, 898.3247, 'completed', 'pass', '2026-08-28 06:04:00', '2026-08-28 05:04:31'),
(12, 'B-20260828-8144F', 1, NULL, 5, 2, 1, 18, 0, 2, 0.00, 333100.00, 0.00, 333100.00, 18505.5556, 'completed', 'pass', '2026-08-29 07:29:00', '2026-08-28 06:29:20'),
(13, 'B-20260828-5E03B', 2, NULL, 4, 2, 1, 28, 2, 5, 0.00, 305300.00, 0.00, 305300.00, 10903.5714, 'completed', 'pass', '2026-08-29 07:29:00', '2026-08-28 06:29:20'),
(14, 'B-20260828-9D134', 4, NULL, 8, 2, 3, 890, 0, 10, 0.00, 784237.50, 0.00, 784237.50, 881.1657, 'completed', 'pass', '2026-08-29 07:30:00', '2026-08-28 06:30:45'),
(15, 'B-20260828-727D3', 2, NULL, 7, 2, 3, 70, 0, 5, 0.00, 915900.00, 0.00, 915900.00, 13084.2857, 'completed', 'pass', '2026-08-29 07:30:00', '2026-08-28 06:30:45'),
(16, 'B-20260828-CB6A1', 1, NULL, 6, 2, 3, 29, 0, 1, 0.00, 999300.00, 0.00, 999300.00, 34458.6207, 'completed', 'pass', '2026-08-29 07:30:00', '2026-08-28 06:30:45');

-- --------------------------------------------------------

--
-- Table structure for table `products`
--

CREATE TABLE `products` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(120) NOT NULL,
  `sku` varchar(50) NOT NULL,
  `unit_price` decimal(10,2) NOT NULL DEFAULT 0.00,
  `rough_loaf_price` decimal(10,2) DEFAULT NULL,
  `overhead_per_batch` decimal(10,2) NOT NULL DEFAULT 0.00,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`id`, `name`, `sku`, `unit_price`, `rough_loaf_price`, `overhead_per_batch`, `is_active`, `created_by`, `created_at`) VALUES
(1, 'Slice Bread', 'SB01', 750.00, 650.00, 0.00, 1, 1, '2026-08-03 14:58:23'),
(2, 'Small Bread', 'SB002', 430.00, 330.00, 0.00, 1, 1, '2026-08-03 15:02:28'),
(3, 'Jumbo Bread', 'JB01', 520.00, 420.00, 0.00, 1, 1, '2026-08-03 15:24:54'),
(4, 'Chocolate Bread', 'CB01', 600.00, NULL, 0.00, 1, 1, '2026-08-28 14:50:40');

-- --------------------------------------------------------

--
-- Table structure for table `product_prices`
--

CREATE TABLE `product_prices` (
  `id` int(10) UNSIGNED NOT NULL,
  `product_id` int(10) UNSIGNED NOT NULL,
  `customer_type` enum('walk_in','marketer','distributor') NOT NULL,
  `price` decimal(10,2) NOT NULL,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `purchase_orders`
--

CREATE TABLE `purchase_orders` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `po_code` varchar(40) NOT NULL,
  `supplier_id` int(10) UNSIGNED NOT NULL,
  `warehouse_id` int(10) UNSIGNED DEFAULT NULL,
  `requested_by` int(10) UNSIGNED NOT NULL,
  `status` enum('draft','pending_accountant_approval','pending_manager_approval','approved','rejected','received','cancelled') NOT NULL DEFAULT 'draft',
  `payment_status` enum('unpaid','partial','paid') NOT NULL DEFAULT 'unpaid',
  `subtotal` decimal(12,2) NOT NULL DEFAULT 0.00,
  `notes` varchar(255) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `received_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `purchase_orders`
--

INSERT INTO `purchase_orders` (`id`, `po_code`, `supplier_id`, `warehouse_id`, `requested_by`, `status`, `payment_status`, `subtotal`, `notes`, `created_at`, `received_at`) VALUES
(1, 'PO-20260802-545D0', 1, 1, 1, 'received', 'unpaid', 4056000.00, 'first order', '2026-08-02 04:50:06', '2026-08-03 04:41:38'),
(2, 'PO-20260803-6B750', 2, 1, 1, 'received', 'unpaid', 23111000.00, '', '2026-08-03 04:39:17', '2026-08-03 04:41:18'),
(3, 'PO-20260803-C3897', 1, 1, 1, 'rejected', 'unpaid', 5625000.00, 'second order', '2026-08-03 08:27:55', NULL),
(4, 'PO-20260805-6B9E4', 2, 1, 1, 'received', 'unpaid', 112500.00, '', '2026-08-05 04:55:42', '2026-08-05 04:56:40'),
(5, 'PO-20260827-24592', 2, 1, 1, 'received', 'unpaid', 147837000.00, '', '2026-08-27 16:43:51', '2026-08-27 16:44:09');

-- --------------------------------------------------------

--
-- Table structure for table `purchase_order_items`
--

CREATE TABLE `purchase_order_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `purchase_order_id` bigint(20) UNSIGNED NOT NULL,
  `raw_material_id` int(10) UNSIGNED NOT NULL,
  `quantity` decimal(12,3) NOT NULL,
  `unit_cost` decimal(10,4) NOT NULL,
  `line_total` decimal(12,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `purchase_order_items`
--

INSERT INTO `purchase_order_items` (`id`, `purchase_order_id`, `raw_material_id`, `quantity`, `unit_cost`, `line_total`) VALUES
(1, 1, 1, 78.000, 52000.0000, 4056000.00),
(2, 2, 2, 500.000, 37500.0000, 18750000.00),
(3, 2, 3, 245.000, 17800.0000, 4361000.00),
(4, 3, 2, 150.000, 37500.0000, 5625000.00),
(5, 4, 4, 25.000, 4500.0000, 112500.00),
(6, 5, 3, 1500.000, 17800.0000, 26700000.00),
(7, 5, 1, 1800.000, 52000.0000, 93600000.00),
(8, 5, 4, 286.000, 4500.0000, 1287000.00),
(9, 5, 2, 700.000, 37500.0000, 26250000.00);

-- --------------------------------------------------------

--
-- Table structure for table `purchase_payments`
--

CREATE TABLE `purchase_payments` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `purchase_order_id` bigint(20) UNSIGNED NOT NULL,
  `payment_method` enum('cash','bank_transfer') NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `bank_account_id` int(10) UNSIGNED DEFAULT NULL,
  `reference` varchar(100) DEFAULT NULL,
  `paid_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `purchase_returns`
--

CREATE TABLE `purchase_returns` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `purchase_order_id` bigint(20) UNSIGNED NOT NULL,
  `raw_material_id` int(10) UNSIGNED NOT NULL,
  `quantity` decimal(12,3) NOT NULL,
  `reason` enum('damaged','missing','wrong_item','excess_delivered','other') NOT NULL,
  `processed_by` int(10) UNSIGNED NOT NULL,
  `journal_entry_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `raw_materials`
--

CREATE TABLE `raw_materials` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(120) NOT NULL,
  `sku` varchar(50) NOT NULL,
  `unit` varchar(20) NOT NULL,
  `unit_id` int(10) UNSIGNED DEFAULT NULL,
  `unit_cost` decimal(10,4) NOT NULL DEFAULT 0.0000,
  `quantity_on_hand` decimal(12,3) NOT NULL DEFAULT 0.000,
  `reorder_level` decimal(12,3) NOT NULL DEFAULT 0.000,
  `supplier_id` int(10) UNSIGNED DEFAULT NULL,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `raw_materials`
--

INSERT INTO `raw_materials` (`id`, `name`, `sku`, `unit`, `unit_id`, `unit_cost`, `quantity_on_hand`, `reorder_level`, `supplier_id`, `created_by`, `created_at`, `updated_at`) VALUES
(1, 'Flour', 'F01', 'bag', 5, 52000.0000, 1817.000, 10.000, 1, NULL, '2026-08-02 04:26:07', '2026-08-28 06:28:22'),
(2, 'Sugar', 'S01', 'bag', 5, 37500.0000, 1188.145, 10.000, 1, NULL, '2026-08-02 04:27:34', '2026-08-28 06:28:22'),
(3, 'Butter', 'B001', 'pack', 6, 17800.0000, 1698.000, 12.000, 1, 1, '2026-08-03 04:08:31', '2026-08-28 06:28:22'),
(4, 'Honey', 'H001', 'L', 3, 4500.0000, 285.000, 50.000, 2, 1, '2026-08-03 08:33:45', '2026-08-28 06:28:22');

-- --------------------------------------------------------

--
-- Table structure for table `recipes`
--

CREATE TABLE `recipes` (
  `id` int(10) UNSIGNED NOT NULL,
  `product_id` int(10) UNSIGNED NOT NULL,
  `version` int(10) UNSIGNED NOT NULL DEFAULT 1,
  `yield_quantity` int(10) UNSIGNED NOT NULL,
  `labor_minutes` decimal(8,2) NOT NULL DEFAULT 0.00,
  `is_active` tinyint(1) NOT NULL DEFAULT 1,
  `created_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `recipes`
--

INSERT INTO `recipes` (`id`, `product_id`, `version`, `yield_quantity`, `labor_minutes`, `is_active`, `created_by`, `created_at`) VALUES
(1, 1, 1, 10, 40.00, 1, 1, '2026-08-03 15:04:15'),
(2, 2, 1, 25, 45.00, 1, 1, '2026-08-03 15:23:58'),
(3, 3, 1, 12, 5.00, 1, 1, '2026-08-27 15:51:31'),
(4, 3, 2, 300, 20.00, 1, 1, '2026-08-27 17:15:48');

-- --------------------------------------------------------

--
-- Table structure for table `recipe_ingredients`
--

CREATE TABLE `recipe_ingredients` (
  `id` int(10) UNSIGNED NOT NULL,
  `recipe_id` int(10) UNSIGNED NOT NULL,
  `raw_material_id` int(10) UNSIGNED NOT NULL,
  `quantity_required` decimal(12,3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `recipe_ingredients`
--

INSERT INTO `recipe_ingredients` (`id`, `recipe_id`, `raw_material_id`, `quantity_required`) VALUES
(1, 1, 2, 1.000),
(2, 1, 3, 2.000),
(3, 1, 1, 5.000),
(4, 2, 1, 4.000),
(5, 2, 2, 2.000),
(6, 2, 3, 1.000),
(7, 2, 4, 1.000),
(8, 3, 3, 2.000),
(9, 3, 1, 1.000),
(10, 3, 4, 5.000),
(11, 3, 2, 1.000),
(12, 4, 3, 5.000),
(13, 4, 1, 1.000),
(14, 4, 4, 2.000),
(15, 4, 2, 2.971);

-- --------------------------------------------------------

--
-- Table structure for table `roles`
--

CREATE TABLE `roles` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(50) NOT NULL,
  `description` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `roles`
--

INSERT INTO `roles` (`id`, `name`, `description`) VALUES
(1, 'Admin', 'Full system access'),
(2, 'Production Manager', 'Manages recipes, batches, QC'),
(3, 'Baker', 'Logs production batches'),
(4, 'Sales Rep', 'Handles POS and sales orders'),
(5, 'Accountant', 'Manages ledger and payroll'),
(6, 'Warehouse Manager', 'Reviews and releases material requests from the warehouse'),
(7, 'QC', 'Performs quality control checks on production batches'),
(8, 'Auditor', 'Auditor of the company');

-- --------------------------------------------------------

--
-- Table structure for table `role_permissions`
--

CREATE TABLE `role_permissions` (
  `role_id` int(10) UNSIGNED NOT NULL,
  `permission_id` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `role_permissions`
--

INSERT INTO `role_permissions` (`role_id`, `permission_id`) VALUES
(1, 1),
(1, 2),
(1, 3),
(1, 4),
(1, 5),
(1, 6),
(1, 7),
(1, 8),
(1, 9),
(1, 10),
(1, 11),
(1, 12),
(1, 13),
(1, 14),
(2, 1),
(2, 12),
(2, 13),
(4, 11),
(5, 1),
(5, 2),
(5, 3),
(5, 4),
(5, 5),
(5, 8),
(5, 9),
(5, 14),
(6, 10),
(8, 1),
(8, 2),
(8, 3),
(8, 4),
(8, 5),
(8, 6),
(8, 8),
(8, 9),
(8, 10),
(8, 11),
(8, 12),
(8, 13);

-- --------------------------------------------------------

--
-- Table structure for table `salary_advances`
--

CREATE TABLE `salary_advances` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` int(10) UNSIGNED NOT NULL,
  `type` enum('advance','deduction') NOT NULL DEFAULT 'advance',
  `amount` decimal(12,2) NOT NULL,
  `installment_amount` decimal(12,2) DEFAULT NULL,
  `outstanding_balance` decimal(12,2) NOT NULL,
  `reason` varchar(255) DEFAULT NULL,
  `status` enum('pending','approved','rejected','fully_repaid') NOT NULL DEFAULT 'pending',
  `requested_by` int(10) UNSIGNED NOT NULL,
  `decided_by` int(10) UNSIGNED DEFAULT NULL,
  `requested_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `decided_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `salary_advance_repayments`
--

CREATE TABLE `salary_advance_repayments` (
  `id` int(10) UNSIGNED NOT NULL,
  `salary_advance_id` int(10) UNSIGNED NOT NULL,
  `payslip_id` int(10) UNSIGNED NOT NULL,
  `amount_deducted` decimal(12,2) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `sales_orders`
--

CREATE TABLE `sales_orders` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `order_code` varchar(40) NOT NULL,
  `customer_id` int(10) UNSIGNED DEFAULT NULL,
  `sales_rep_id` int(10) UNSIGNED NOT NULL,
  `order_type` enum('pos','delivery') NOT NULL DEFAULT 'pos',
  `subtotal` decimal(12,2) NOT NULL DEFAULT 0.00,
  `tax_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `total_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `payment_status` enum('paid','partial','unpaid') NOT NULL DEFAULT 'unpaid',
  `approval_status` enum('not_required','pending','approved','rejected') NOT NULL DEFAULT 'not_required',
  `order_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `sales_orders`
--

INSERT INTO `sales_orders` (`id`, `order_code`, `customer_id`, `sales_rep_id`, `order_type`, `subtotal`, `tax_amount`, `total_amount`, `payment_status`, `approval_status`, `order_date`) VALUES
(1, 'SO-20260827-7C5B1', 1, 1, 'delivery', 12980.00, 0.00, 12980.00, 'unpaid', 'not_required', '2026-08-27 09:49:37'),
(2, 'SO-20260827-F7858', 2, 1, 'delivery', 12760.00, 0.00, 12760.00, 'unpaid', 'not_required', '2026-08-27 09:56:10'),
(3, 'SO-20260827-B70EB', 1, 1, 'delivery', 13800.00, 0.00, 13800.00, 'partial', 'not_required', '2026-08-27 10:33:51'),
(4, 'SO-20260828-EA102', NULL, 1, 'pos', 1700.00, 0.00, 1700.00, 'paid', 'not_required', '2026-08-28 10:24:27');

-- --------------------------------------------------------

--
-- Table structure for table `sales_order_items`
--

CREATE TABLE `sales_order_items` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `sales_order_id` bigint(20) UNSIGNED NOT NULL,
  `product_id` int(10) UNSIGNED NOT NULL,
  `packaging_type` enum('retail','wholesale','rough') NOT NULL DEFAULT 'retail',
  `quantity` int(10) UNSIGNED NOT NULL,
  `unit_price` decimal(10,2) NOT NULL,
  `line_total` decimal(12,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `sales_order_items`
--

INSERT INTO `sales_order_items` (`id`, `sales_order_id`, `product_id`, `packaging_type`, `quantity`, `unit_price`, `line_total`) VALUES
(1, 1, 1, 'retail', 11, 750.00, 8250.00),
(2, 1, 2, 'retail', 11, 430.00, 4730.00),
(3, 2, 3, 'retail', 8, 520.00, 4160.00),
(4, 2, 2, 'retail', 20, 430.00, 8600.00),
(5, 3, 1, 'retail', 8, 750.00, 6000.00),
(6, 3, 3, 'retail', 15, 520.00, 7800.00),
(7, 4, 2, 'retail', 1, 430.00, 430.00),
(8, 4, 3, 'retail', 1, 520.00, 520.00),
(9, 4, 1, 'retail', 1, 750.00, 750.00);

-- --------------------------------------------------------

--
-- Table structure for table `sales_order_item_batches`
--

CREATE TABLE `sales_order_item_batches` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `sales_order_item_id` bigint(20) UNSIGNED NOT NULL,
  `packaging_log_id` bigint(20) UNSIGNED NOT NULL,
  `quantity` int(10) UNSIGNED NOT NULL,
  `unit_cost` decimal(10,4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `sales_order_item_batches`
--

INSERT INTO `sales_order_item_batches` (`id`, `sales_order_item_id`, `packaging_log_id`, `quantity`, `unit_cost`) VALUES
(1, 7, 3, 1, 200.8550),
(2, 8, 4, 1, 295.2000),
(3, 9, 2, 1, 33310.0000);

-- --------------------------------------------------------

--
-- Table structure for table `sales_returns`
--

CREATE TABLE `sales_returns` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `sales_order_id` bigint(20) UNSIGNED NOT NULL,
  `sales_order_item_id` bigint(20) UNSIGNED DEFAULT NULL,
  `packaging_log_id` bigint(20) UNSIGNED DEFAULT NULL,
  `quantity` int(10) UNSIGNED NOT NULL,
  `reason` enum('damaged','wrong_item','customer_reject','expired','other') NOT NULL,
  `refund_amount` decimal(12,2) NOT NULL DEFAULT 0.00,
  `journal_entry_id` bigint(20) UNSIGNED DEFAULT NULL,
  `processed_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `sale_payments`
--

CREATE TABLE `sale_payments` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `sales_order_id` bigint(20) UNSIGNED NOT NULL,
  `payment_method` enum('cash','bank_transfer','card') NOT NULL,
  `amount` decimal(12,2) NOT NULL,
  `pos_terminal_id` int(10) UNSIGNED DEFAULT NULL,
  `bank_account_id` int(10) UNSIGNED DEFAULT NULL,
  `reference` varchar(100) DEFAULT NULL,
  `received_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `sale_payments`
--

INSERT INTO `sale_payments` (`id`, `sales_order_id`, `payment_method`, `amount`, `pos_terminal_id`, `bank_account_id`, `reference`, `received_by`, `created_at`) VALUES
(1, 4, 'bank_transfer', 1000.00, NULL, 1, '', 1, '2026-08-28 09:24:27'),
(2, 4, 'cash', 700.00, NULL, NULL, '', 1, '2026-08-28 09:24:27'),
(3, 3, 'bank_transfer', 7000.00, NULL, 1, '', 1, '2026-08-29 11:54:10');

-- --------------------------------------------------------

--
-- Table structure for table `settings`
--

CREATE TABLE `settings` (
  `id` int(10) UNSIGNED NOT NULL,
  `setting_key` varchar(80) NOT NULL,
  `value` varchar(255) NOT NULL,
  `updated_by` int(10) UNSIGNED DEFAULT NULL,
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `settings`
--

INSERT INTO `settings` (`id`, `setting_key`, `value`, `updated_by`, `updated_at`) VALUES
(1, 'vat_rate', '7.5', NULL, '2026-08-30 03:42:37'),
(2, 'vat_enabled', '0', NULL, '2026-08-30 03:42:37');

-- --------------------------------------------------------

--
-- Table structure for table `shifts`
--

CREATE TABLE `shifts` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` int(10) UNSIGNED NOT NULL,
  `shift_date` date NOT NULL,
  `session` enum('morning','night') DEFAULT NULL,
  `clock_in` time DEFAULT NULL,
  `clock_out` time DEFAULT NULL,
  `hours_worked` decimal(5,2) GENERATED ALWAYS AS (if(`clock_out` is not null,time_to_sec(timediff(`clock_out`,`clock_in`)) / 3600,0)) STORED,
  `notes` varchar(255) DEFAULT NULL,
  `marked_by` int(10) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `shift_schedules`
--

CREATE TABLE `shift_schedules` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` int(10) UNSIGNED NOT NULL,
  `schedule_date` date NOT NULL,
  `session` enum('morning','night') NOT NULL,
  `created_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `stock_movements`
--

CREATE TABLE `stock_movements` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `raw_material_id` int(10) UNSIGNED NOT NULL,
  `warehouse_id` int(10) UNSIGNED DEFAULT NULL,
  `movement_type` enum('purchase_in','production_out','adjustment','waste','damaged','missing','excess','return_in','return_out') NOT NULL,
  `quantity` decimal(12,3) NOT NULL,
  `reference_type` varchar(50) DEFAULT NULL,
  `reference_id` bigint(20) UNSIGNED DEFAULT NULL,
  `created_by` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `stock_movements`
--

INSERT INTO `stock_movements` (`id`, `raw_material_id`, `warehouse_id`, `movement_type`, `quantity`, `reference_type`, `reference_id`, `created_by`, `created_at`) VALUES
(1, 2, 1, 'adjustment', -2.000, 'item_transfer', 1, 1, '2026-08-02 04:34:23'),
(2, 2, 2, 'adjustment', 2.000, 'item_transfer', 1, 1, '2026-08-02 04:34:33'),
(3, 2, 1, 'purchase_in', 500.000, 'purchase_order', 2, 1, '2026-08-03 04:41:18'),
(4, 3, 1, 'purchase_in', 245.000, 'purchase_order', 2, 1, '2026-08-03 04:41:18'),
(5, 1, 1, 'purchase_in', 78.000, 'purchase_order', 1, 1, '2026-08-03 04:41:38'),
(6, 1, NULL, 'production_out', -5.000, 'production_batch', 1, 2, '2026-08-03 18:43:51'),
(7, 2, NULL, 'production_out', -1.000, 'production_batch', 1, 2, '2026-08-03 18:43:51'),
(8, 3, NULL, 'production_out', -2.000, 'production_batch', 1, 2, '2026-08-03 18:43:51'),
(12, 4, 1, 'purchase_in', 25.000, 'purchase_order', 4, 1, '2026-08-05 04:56:40'),
(13, 1, NULL, 'production_out', -4.000, 'production_batch', 2, 2, '2026-08-05 04:59:06'),
(14, 2, NULL, 'production_out', -2.000, 'production_batch', 2, 2, '2026-08-05 04:59:06'),
(15, 3, NULL, 'production_out', -1.000, 'production_batch', 2, 2, '2026-08-05 04:59:06'),
(16, 4, NULL, 'production_out', -1.000, 'production_batch', 2, 2, '2026-08-05 04:59:06'),
(17, 1, NULL, 'production_out', -1.000, 'production_batch', 3, 2, '2026-08-27 15:52:57'),
(18, 2, NULL, 'production_out', -1.000, 'production_batch', 3, 2, '2026-08-27 15:52:57'),
(19, 3, NULL, 'production_out', -2.000, 'production_batch', 3, 2, '2026-08-27 15:52:57'),
(20, 4, NULL, 'production_out', -5.000, 'production_batch', 3, 2, '2026-08-27 15:52:57'),
(21, 3, 1, 'purchase_in', 1500.000, 'purchase_order', 5, 1, '2026-08-27 16:44:09'),
(22, 1, 1, 'purchase_in', 1800.000, 'purchase_order', 5, 1, '2026-08-27 16:44:09'),
(23, 4, 1, 'purchase_in', 286.000, 'purchase_order', 5, 1, '2026-08-27 16:44:09'),
(24, 2, 1, 'purchase_in', 700.000, 'purchase_order', 5, 1, '2026-08-27 16:44:09'),
(25, 1, NULL, 'production_out', -1.000, 'production_batch', 4, 2, '2026-08-27 17:16:35'),
(26, 2, NULL, 'production_out', -2.971, 'production_batch', 4, 2, '2026-08-27 17:16:35'),
(27, 3, NULL, 'production_out', -5.000, 'production_batch', 4, 2, '2026-08-27 17:16:35'),
(28, 4, NULL, 'production_out', -2.000, 'production_batch', 4, 2, '2026-08-27 17:16:35'),
(29, 1, NULL, 'production_out', -4.000, 'production_batch', 5, 2, '2026-08-28 01:53:58'),
(30, 2, NULL, 'production_out', -2.000, 'production_batch', 5, 2, '2026-08-28 01:53:58'),
(31, 3, NULL, 'production_out', -1.000, 'production_batch', 5, 2, '2026-08-28 01:53:58'),
(32, 4, NULL, 'production_out', -1.000, 'production_batch', 5, 2, '2026-08-28 01:53:58'),
(33, 1, NULL, 'production_out', -1.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(34, 1, NULL, 'production_out', -1.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(35, 1, NULL, 'production_out', -5.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(36, 2, NULL, 'production_out', -2.971, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(37, 2, NULL, 'production_out', -1.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(38, 2, NULL, 'production_out', -1.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(39, 3, NULL, 'production_out', -5.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(40, 3, NULL, 'production_out', -2.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(41, 3, NULL, 'production_out', -2.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(42, 4, NULL, 'production_out', -2.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(43, 4, NULL, 'production_out', -5.000, 'material_request', 1, 1, '2026-08-28 03:30:24'),
(44, 1, NULL, 'production_out', -4.000, 'material_request', 2, 1, '2026-08-28 06:27:50'),
(45, 2, NULL, 'production_out', -2.000, 'material_request', 2, 1, '2026-08-28 06:27:50'),
(46, 3, NULL, 'production_out', -1.000, 'material_request', 2, 1, '2026-08-28 06:27:50'),
(47, 4, NULL, 'production_out', -1.000, 'material_request', 2, 1, '2026-08-28 06:27:50'),
(48, 1, NULL, 'production_out', -5.000, 'material_request', 2, 1, '2026-08-28 06:27:50'),
(49, 2, NULL, 'production_out', -1.000, 'material_request', 2, 1, '2026-08-28 06:27:50'),
(50, 3, NULL, 'production_out', -2.000, 'material_request', 2, 1, '2026-08-28 06:27:50'),
(51, 1, NULL, 'production_out', -15.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(52, 1, NULL, 'production_out', -12.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(53, 1, NULL, 'production_out', -3.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(54, 2, NULL, 'production_out', -3.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(55, 2, NULL, 'production_out', -6.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(56, 2, NULL, 'production_out', -8.913, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(57, 3, NULL, 'production_out', -6.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(58, 3, NULL, 'production_out', -3.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(59, 3, NULL, 'production_out', -15.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(60, 4, NULL, 'production_out', -3.000, 'material_request', 3, 1, '2026-08-28 06:28:22'),
(61, 4, NULL, 'production_out', -6.000, 'material_request', 3, 1, '2026-08-28 06:28:22');

-- --------------------------------------------------------

--
-- Table structure for table `suppliers`
--

CREATE TABLE `suppliers` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(150) NOT NULL,
  `contact_name` varchar(120) DEFAULT NULL,
  `phone` varchar(30) DEFAULT NULL,
  `email` varchar(150) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `balance_due` decimal(12,2) NOT NULL DEFAULT 0.00,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `suppliers`
--

INSERT INTO `suppliers` (`id`, `name`, `contact_name`, `phone`, `email`, `address`, `balance_due`, `created_by`, `created_at`) VALUES
(1, 'deryl ltd', 'wale edun', '0105489635', 'edun@gmail.com', 'deryl add', 4056000.00, NULL, '2026-08-02 04:16:39'),
(2, 'Damil Enterprise', 'Tunde Obidairo', '0201478963', 'tunde@gmail.com', 'Damil add', 23223500.00, NULL, '2026-08-03 04:27:32'),
(3, 'Gamdal', 'Tunji', '0258974562', 'tun@gmail.com', 'Gamdal add', 0.00, 1, '2026-08-03 15:42:57');

-- --------------------------------------------------------

--
-- Table structure for table `units`
--

CREATE TABLE `units` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(60) NOT NULL,
  `abbreviation` varchar(15) NOT NULL,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `units`
--

INSERT INTO `units` (`id`, `name`, `abbreviation`, `created_by`, `created_at`) VALUES
(1, 'Kilogram', 'KG', 1, '2026-08-03 13:00:05'),
(2, 'Pieces', 'pieces', 1, '2026-08-03 15:26:16'),
(3, 'Sachet', 'sachet', 1, '2026-08-05 03:39:38'),
(4, 'Litre', 'litre', 1, '2026-08-05 03:42:51');

-- --------------------------------------------------------

--
-- Table structure for table `warehouses`
--

CREATE TABLE `warehouses` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(120) NOT NULL,
  `location` varchar(255) DEFAULT NULL,
  `phone` varchar(30) DEFAULT NULL,
  `is_default` tinyint(1) NOT NULL DEFAULT 0,
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `warehouses`
--

INSERT INTO `warehouses` (`id`, `name`, `location`, `phone`, `is_default`, `created_by`, `created_at`) VALUES
(1, 'Main Warehouse', 'Central production facility', NULL, 1, NULL, '2026-08-02 03:54:34'),
(2, 'w2', 'gaa akanbi', '', 0, NULL, '2026-08-02 04:17:46');

-- --------------------------------------------------------

--
-- Table structure for table `warehouse_stock`
--

CREATE TABLE `warehouse_stock` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `warehouse_id` int(10) UNSIGNED NOT NULL,
  `raw_material_id` int(10) UNSIGNED NOT NULL,
  `quantity_on_hand` decimal(12,3) NOT NULL DEFAULT 0.000,
  `selling_price` decimal(10,2) DEFAULT NULL,
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `warehouse_stock`
--

INSERT INTO `warehouse_stock` (`id`, `warehouse_id`, `raw_material_id`, `quantity_on_hand`, `selling_price`, `updated_at`) VALUES
(1, 2, 2, 2.000, NULL, '2026-08-02 04:34:33');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `accounting_periods`
--
ALTER TABLE `accounting_periods`
  ADD PRIMARY KEY (`id`),
  ADD KEY `closed_by` (`closed_by`),
  ADD KEY `idx_period_range` (`period_start`,`period_end`);

--
-- Indexes for table `alerts`
--
ALTER TABLE `alerts`
  ADD PRIMARY KEY (`id`),
  ADD KEY `read_by` (`read_by`),
  ADD KEY `idx_alerts_role_unread` (`target_role`,`is_read`);

--
-- Indexes for table `approvals`
--
ALTER TABLE `approvals`
  ADD PRIMARY KEY (`id`),
  ADD KEY `requested_by` (`requested_by`),
  ADD KEY `decided_by` (`decided_by`),
  ADD KEY `idx_approvable` (`approvable_type`,`approvable_id`),
  ADD KEY `idx_approval_status` (`status`);

--
-- Indexes for table `assets_depreciation`
--
ALTER TABLE `assets_depreciation`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_assets_created_by` (`created_by`),
  ADD KEY `fk_assets_disposal_entry` (`disposal_journal_entry_id`);

--
-- Indexes for table `asset_depreciation_runs`
--
ALTER TABLE `asset_depreciation_runs`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_asset_period` (`asset_id`,`period_start`,`period_end`),
  ADD KEY `journal_entry_id` (`journal_entry_id`),
  ADD KEY `run_by` (`run_by`);

--
-- Indexes for table `bank_accounts`
--
ALTER TABLE `bank_accounts`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_bank_account_number` (`account_number`),
  ADD KEY `chart_of_accounts_id` (`chart_of_accounts_id`);

--
-- Indexes for table `chart_of_accounts`
--
ALTER TABLE `chart_of_accounts`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `code` (`code`);

--
-- Indexes for table `customers`
--
ALTER TABLE `customers`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `documents`
--
ALTER TABLE `documents`
  ADD PRIMARY KEY (`id`),
  ADD KEY `uploaded_by` (`uploaded_by`),
  ADD KEY `idx_documents_attachable` (`attachable_type`,`attachable_id`);

--
-- Indexes for table `expense_categories`
--
ALTER TABLE `expense_categories`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`),
  ADD KEY `created_by` (`created_by`);

--
-- Indexes for table `expense_claims`
--
ALTER TABLE `expense_claims`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `claim_code` (`claim_code`),
  ADD KEY `claimed_by` (`claimed_by`),
  ADD KEY `approved_by` (`approved_by`),
  ADD KEY `bank_account_id` (`bank_account_id`),
  ADD KEY `journal_entry_id` (`journal_entry_id`),
  ADD KEY `idx_expense_status` (`status`),
  ADD KEY `fk_expense_category` (`category_id`);

--
-- Indexes for table `income_categories`
--
ALTER TABLE `income_categories`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`),
  ADD KEY `created_by` (`created_by`);

--
-- Indexes for table `income_records`
--
ALTER TABLE `income_records`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `income_code` (`income_code`),
  ADD KEY `category_id` (`category_id`),
  ADD KEY `bank_account_id` (`bank_account_id`),
  ADD KEY `recorded_by` (`recorded_by`),
  ADD KEY `journal_entry_id` (`journal_entry_id`);

--
-- Indexes for table `item_transfers`
--
ALTER TABLE `item_transfers`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `transfer_code` (`transfer_code`),
  ADD KEY `from_warehouse_id` (`from_warehouse_id`),
  ADD KEY `to_warehouse_id` (`to_warehouse_id`),
  ADD KEY `raw_material_id` (`raw_material_id`),
  ADD KEY `requested_by` (`requested_by`),
  ADD KEY `received_by` (`received_by`),
  ADD KEY `idx_transfer_status` (`status`);

--
-- Indexes for table `journal_entries`
--
ALTER TABLE `journal_entries`
  ADD PRIMARY KEY (`id`),
  ADD KEY `created_by` (`created_by`),
  ADD KEY `idx_journal_date` (`entry_date`),
  ADD KEY `idx_journal_ref` (`reference_type`,`reference_id`);

--
-- Indexes for table `journal_lines`
--
ALTER TABLE `journal_lines`
  ADD PRIMARY KEY (`id`),
  ADD KEY `journal_entry_id` (`journal_entry_id`),
  ADD KEY `idx_lines_account` (`account_id`);

--
-- Indexes for table `login_attempts`
--
ALTER TABLE `login_attempts`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_login_attempts_username_time` (`username`,`attempted_at`);

--
-- Indexes for table `material_requests`
--
ALTER TABLE `material_requests`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `request_code` (`request_code`),
  ADD KEY `recipe_id` (`recipe_id`),
  ADD KEY `requested_by` (`requested_by`),
  ADD KEY `released_by` (`released_by`),
  ADD KEY `idx_material_request_status` (`status`);

--
-- Indexes for table `material_request_items`
--
ALTER TABLE `material_request_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `material_request_id` (`material_request_id`),
  ADD KEY `raw_material_id` (`raw_material_id`),
  ADD KEY `fk_item_product_line` (`material_request_product_id`);

--
-- Indexes for table `material_request_products`
--
ALTER TABLE `material_request_products`
  ADD PRIMARY KEY (`id`),
  ADD KEY `material_request_id` (`material_request_id`),
  ADD KEY `recipe_id` (`recipe_id`);

--
-- Indexes for table `metrics`
--
ALTER TABLE `metrics`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`),
  ADD KEY `fk_units_equivalent_unit` (`equivalent_unit_id`),
  ADD KEY `fk_units_created_by` (`created_by`);

--
-- Indexes for table `packaging_logs`
--
ALTER TABLE `packaging_logs`
  ADD PRIMARY KEY (`id`),
  ADD KEY `production_batch_id` (`production_batch_id`),
  ADD KEY `packed_by` (`packed_by`);

--
-- Indexes for table `payroll_runs`
--
ALTER TABLE `payroll_runs`
  ADD PRIMARY KEY (`id`),
  ADD KEY `created_by` (`created_by`),
  ADD KEY `idx_payroll_period` (`period_start`,`period_end`);

--
-- Indexes for table `payslips`
--
ALTER TABLE `payslips`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_payslip_run_user` (`payroll_run_id`,`user_id`),
  ADD KEY `user_id` (`user_id`);

--
-- Indexes for table `permissions`
--
ALTER TABLE `permissions`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `slug` (`slug`);

--
-- Indexes for table `pos_terminals`
--
ALTER TABLE `pos_terminals`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_terminal_code` (`terminal_code`),
  ADD KEY `bank_account_id` (`bank_account_id`);

--
-- Indexes for table `production_batches`
--
ALTER TABLE `production_batches`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `batch_code` (`batch_code`),
  ADD UNIQUE KEY `uq_batch_material_request_product` (`material_request_product_id`),
  ADD KEY `baker_id` (`baker_id`),
  ADD KEY `idx_batch_recipe` (`recipe_id`),
  ADD KEY `idx_batch_date` (`produced_at`);

--
-- Indexes for table `products`
--
ALTER TABLE `products`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `sku` (`sku`),
  ADD KEY `fk_products_created_by` (`created_by`);

--
-- Indexes for table `product_prices`
--
ALTER TABLE `product_prices`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_price_product_type` (`product_id`,`customer_type`),
  ADD KEY `created_by` (`created_by`);

--
-- Indexes for table `purchase_orders`
--
ALTER TABLE `purchase_orders`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `po_code` (`po_code`),
  ADD KEY `supplier_id` (`supplier_id`),
  ADD KEY `requested_by` (`requested_by`),
  ADD KEY `idx_po_status` (`status`),
  ADD KEY `fk_po_warehouse` (`warehouse_id`);

--
-- Indexes for table `purchase_order_items`
--
ALTER TABLE `purchase_order_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `purchase_order_id` (`purchase_order_id`),
  ADD KEY `raw_material_id` (`raw_material_id`);

--
-- Indexes for table `purchase_payments`
--
ALTER TABLE `purchase_payments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `bank_account_id` (`bank_account_id`),
  ADD KEY `paid_by` (`paid_by`),
  ADD KEY `idx_purchase_payments_po` (`purchase_order_id`);

--
-- Indexes for table `purchase_returns`
--
ALTER TABLE `purchase_returns`
  ADD PRIMARY KEY (`id`),
  ADD KEY `purchase_order_id` (`purchase_order_id`),
  ADD KEY `raw_material_id` (`raw_material_id`),
  ADD KEY `processed_by` (`processed_by`),
  ADD KEY `fk_purchase_return_journal` (`journal_entry_id`);

--
-- Indexes for table `raw_materials`
--
ALTER TABLE `raw_materials`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `sku` (`sku`),
  ADD KEY `supplier_id` (`supplier_id`),
  ADD KEY `idx_raw_materials_stock` (`quantity_on_hand`,`reorder_level`),
  ADD KEY `fk_raw_materials_unit` (`unit_id`),
  ADD KEY `fk_raw_materials_created_by` (`created_by`);

--
-- Indexes for table `recipes`
--
ALTER TABLE `recipes`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_recipe_product_version` (`product_id`,`version`),
  ADD KEY `created_by` (`created_by`),
  ADD KEY `idx_recipe_product_active` (`product_id`,`is_active`);

--
-- Indexes for table `recipe_ingredients`
--
ALTER TABLE `recipe_ingredients`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_recipe_material` (`recipe_id`,`raw_material_id`),
  ADD KEY `raw_material_id` (`raw_material_id`);

--
-- Indexes for table `roles`
--
ALTER TABLE `roles`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`);

--
-- Indexes for table `role_permissions`
--
ALTER TABLE `role_permissions`
  ADD PRIMARY KEY (`role_id`,`permission_id`),
  ADD KEY `permission_id` (`permission_id`);

--
-- Indexes for table `salary_advances`
--
ALTER TABLE `salary_advances`
  ADD PRIMARY KEY (`id`),
  ADD KEY `requested_by` (`requested_by`),
  ADD KEY `decided_by` (`decided_by`),
  ADD KEY `idx_advance_user_status` (`user_id`,`status`);

--
-- Indexes for table `salary_advance_repayments`
--
ALTER TABLE `salary_advance_repayments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `salary_advance_id` (`salary_advance_id`),
  ADD KEY `payslip_id` (`payslip_id`);

--
-- Indexes for table `sales_orders`
--
ALTER TABLE `sales_orders`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `order_code` (`order_code`),
  ADD KEY `customer_id` (`customer_id`),
  ADD KEY `sales_rep_id` (`sales_rep_id`),
  ADD KEY `idx_orders_date` (`order_date`);

--
-- Indexes for table `sales_order_items`
--
ALTER TABLE `sales_order_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `sales_order_id` (`sales_order_id`),
  ADD KEY `product_id` (`product_id`);

--
-- Indexes for table `sales_order_item_batches`
--
ALTER TABLE `sales_order_item_batches`
  ADD PRIMARY KEY (`id`),
  ADD KEY `sales_order_item_id` (`sales_order_item_id`),
  ADD KEY `packaging_log_id` (`packaging_log_id`);

--
-- Indexes for table `sales_returns`
--
ALTER TABLE `sales_returns`
  ADD PRIMARY KEY (`id`),
  ADD KEY `sales_order_id` (`sales_order_id`),
  ADD KEY `packaging_log_id` (`packaging_log_id`),
  ADD KEY `processed_by` (`processed_by`),
  ADD KEY `fk_return_item` (`sales_order_item_id`),
  ADD KEY `fk_return_journal` (`journal_entry_id`);

--
-- Indexes for table `sale_payments`
--
ALTER TABLE `sale_payments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `received_by` (`received_by`),
  ADD KEY `idx_sale_payments_order` (`sales_order_id`),
  ADD KEY `idx_sale_payments_terminal` (`pos_terminal_id`),
  ADD KEY `idx_sale_payments_account` (`bank_account_id`);

--
-- Indexes for table `settings`
--
ALTER TABLE `settings`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `setting_key` (`setting_key`),
  ADD KEY `updated_by` (`updated_by`);

--
-- Indexes for table `shifts`
--
ALTER TABLE `shifts`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_shift_user_date_session` (`user_id`,`shift_date`,`session`),
  ADD KEY `idx_shifts_user_date` (`user_id`,`shift_date`),
  ADD KEY `fk_shift_marked_by` (`marked_by`);

--
-- Indexes for table `shift_schedules`
--
ALTER TABLE `shift_schedules`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_schedule_user_date_session` (`user_id`,`schedule_date`,`session`),
  ADD KEY `created_by` (`created_by`),
  ADD KEY `idx_schedule_date` (`schedule_date`);

--
-- Indexes for table `stock_movements`
--
ALTER TABLE `stock_movements`
  ADD PRIMARY KEY (`id`),
  ADD KEY `created_by` (`created_by`),
  ADD KEY `idx_stock_mv_material` (`raw_material_id`),
  ADD KEY `idx_stock_mv_ref` (`reference_type`,`reference_id`),
  ADD KEY `fk_stock_movements_warehouse` (`warehouse_id`);

--
-- Indexes for table `suppliers`
--
ALTER TABLE `suppliers`
  ADD PRIMARY KEY (`id`),
  ADD KEY `fk_suppliers_created_by` (`created_by`);

--
-- Indexes for table `units`
--
ALTER TABLE `units`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`),
  ADD KEY `fk_base_units_created_by` (`created_by`);

--
-- Indexes for table `warehouses`
--
ALTER TABLE `warehouses`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`),
  ADD KEY `fk_warehouses_created_by` (`created_by`);

--
-- Indexes for table `warehouse_stock`
--
ALTER TABLE `warehouse_stock`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_warehouse_material` (`warehouse_id`,`raw_material_id`),
  ADD KEY `raw_material_id` (`raw_material_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `accounting_periods`
--
ALTER TABLE `accounting_periods`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `alerts`
--
ALTER TABLE `alerts`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `approvals`
--
ALTER TABLE `approvals`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;

--
-- AUTO_INCREMENT for table `assets_depreciation`
--
ALTER TABLE `assets_depreciation`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- AUTO_INCREMENT for table `asset_depreciation_runs`
--
ALTER TABLE `asset_depreciation_runs`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `bank_accounts`
--
ALTER TABLE `bank_accounts`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- AUTO_INCREMENT for table `chart_of_accounts`
--
ALTER TABLE `chart_of_accounts`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=24;

--
-- AUTO_INCREMENT for table `customers`
--
ALTER TABLE `customers`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- AUTO_INCREMENT for table `documents`
--
ALTER TABLE `documents`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `expense_categories`
--
ALTER TABLE `expense_categories`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

--
-- AUTO_INCREMENT for table `expense_claims`
--
ALTER TABLE `expense_claims`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `income_categories`
--
ALTER TABLE `income_categories`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

--
-- AUTO_INCREMENT for table `income_records`
--
ALTER TABLE `income_records`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `item_transfers`
--
ALTER TABLE `item_transfers`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `journal_entries`
--
ALTER TABLE `journal_entries`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=33;

--
-- AUTO_INCREMENT for table `journal_lines`
--
ALTER TABLE `journal_lines`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=66;

--
-- AUTO_INCREMENT for table `login_attempts`
--
ALTER TABLE `login_attempts`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `material_requests`
--
ALTER TABLE `material_requests`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `material_request_items`
--
ALTER TABLE `material_request_items`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=30;

--
-- AUTO_INCREMENT for table `material_request_products`
--
ALTER TABLE `material_request_products`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

--
-- AUTO_INCREMENT for table `metrics`
--
ALTER TABLE `metrics`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

--
-- AUTO_INCREMENT for table `packaging_logs`
--
ALTER TABLE `packaging_logs`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;

--
-- AUTO_INCREMENT for table `payroll_runs`
--
ALTER TABLE `payroll_runs`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `payslips`
--
ALTER TABLE `payslips`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `permissions`
--
ALTER TABLE `permissions`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15;

--
-- AUTO_INCREMENT for table `pos_terminals`
--
ALTER TABLE `pos_terminals`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- AUTO_INCREMENT for table `production_batches`
--
ALTER TABLE `production_batches`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;

--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

--
-- AUTO_INCREMENT for table `product_prices`
--
ALTER TABLE `product_prices`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `purchase_orders`
--
ALTER TABLE `purchase_orders`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

--
-- AUTO_INCREMENT for table `purchase_order_items`
--
ALTER TABLE `purchase_order_items`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

--
-- AUTO_INCREMENT for table `purchase_payments`
--
ALTER TABLE `purchase_payments`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `purchase_returns`
--
ALTER TABLE `purchase_returns`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `raw_materials`
--
ALTER TABLE `raw_materials`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

--
-- AUTO_INCREMENT for table `recipes`
--
ALTER TABLE `recipes`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

--
-- AUTO_INCREMENT for table `recipe_ingredients`
--
ALTER TABLE `recipe_ingredients`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16;

--
-- AUTO_INCREMENT for table `roles`
--
ALTER TABLE `roles`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

--
-- AUTO_INCREMENT for table `salary_advances`
--
ALTER TABLE `salary_advances`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `salary_advance_repayments`
--
ALTER TABLE `salary_advance_repayments`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `sales_orders`
--
ALTER TABLE `sales_orders`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

--
-- AUTO_INCREMENT for table `sales_order_items`
--
ALTER TABLE `sales_order_items`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;

--
-- AUTO_INCREMENT for table `sales_order_item_batches`
--
ALTER TABLE `sales_order_item_batches`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `sales_returns`
--
ALTER TABLE `sales_returns`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `sale_payments`
--
ALTER TABLE `sale_payments`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `settings`
--
ALTER TABLE `settings`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- AUTO_INCREMENT for table `shifts`
--
ALTER TABLE `shifts`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `shift_schedules`
--
ALTER TABLE `shift_schedules`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `stock_movements`
--
ALTER TABLE `stock_movements`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=62;

--
-- AUTO_INCREMENT for table `suppliers`
--
ALTER TABLE `suppliers`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `units`
--
ALTER TABLE `units`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

--
-- AUTO_INCREMENT for table `warehouses`
--
ALTER TABLE `warehouses`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- AUTO_INCREMENT for table `warehouse_stock`
--
ALTER TABLE `warehouse_stock`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `accounting_periods`
--
ALTER TABLE `accounting_periods`
  ADD CONSTRAINT `accounting_periods_ibfk_1` FOREIGN KEY (`closed_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `alerts`
--
ALTER TABLE `alerts`
  ADD CONSTRAINT `alerts_ibfk_1` FOREIGN KEY (`read_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `approvals`
--
ALTER TABLE `approvals`
  ADD CONSTRAINT `approvals_ibfk_1` FOREIGN KEY (`requested_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `approvals_ibfk_2` FOREIGN KEY (`decided_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `assets_depreciation`
--
ALTER TABLE `assets_depreciation`
  ADD CONSTRAINT `fk_assets_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_assets_disposal_entry` FOREIGN KEY (`disposal_journal_entry_id`) REFERENCES `journal_entries` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `asset_depreciation_runs`
--
ALTER TABLE `asset_depreciation_runs`
  ADD CONSTRAINT `asset_depreciation_runs_ibfk_1` FOREIGN KEY (`asset_id`) REFERENCES `assets_depreciation` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `asset_depreciation_runs_ibfk_2` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries` (`id`),
  ADD CONSTRAINT `asset_depreciation_runs_ibfk_3` FOREIGN KEY (`run_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `bank_accounts`
--
ALTER TABLE `bank_accounts`
  ADD CONSTRAINT `bank_accounts_ibfk_1` FOREIGN KEY (`chart_of_accounts_id`) REFERENCES `chart_of_accounts` (`id`);

--
-- Constraints for table `documents`
--
ALTER TABLE `documents`
  ADD CONSTRAINT `documents_ibfk_1` FOREIGN KEY (`uploaded_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `expense_categories`
--
ALTER TABLE `expense_categories`
  ADD CONSTRAINT `expense_categories_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `expense_claims`
--
ALTER TABLE `expense_claims`
  ADD CONSTRAINT `expense_claims_ibfk_1` FOREIGN KEY (`claimed_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `expense_claims_ibfk_2` FOREIGN KEY (`approved_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `expense_claims_ibfk_3` FOREIGN KEY (`bank_account_id`) REFERENCES `bank_accounts` (`id`),
  ADD CONSTRAINT `expense_claims_ibfk_4` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_expense_category` FOREIGN KEY (`category_id`) REFERENCES `expense_categories` (`id`);

--
-- Constraints for table `income_categories`
--
ALTER TABLE `income_categories`
  ADD CONSTRAINT `income_categories_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `income_records`
--
ALTER TABLE `income_records`
  ADD CONSTRAINT `income_records_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `income_categories` (`id`),
  ADD CONSTRAINT `income_records_ibfk_2` FOREIGN KEY (`bank_account_id`) REFERENCES `bank_accounts` (`id`),
  ADD CONSTRAINT `income_records_ibfk_3` FOREIGN KEY (`recorded_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `income_records_ibfk_4` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `item_transfers`
--
ALTER TABLE `item_transfers`
  ADD CONSTRAINT `item_transfers_ibfk_1` FOREIGN KEY (`from_warehouse_id`) REFERENCES `warehouses` (`id`),
  ADD CONSTRAINT `item_transfers_ibfk_2` FOREIGN KEY (`to_warehouse_id`) REFERENCES `warehouses` (`id`),
  ADD CONSTRAINT `item_transfers_ibfk_3` FOREIGN KEY (`raw_material_id`) REFERENCES `raw_materials` (`id`),
  ADD CONSTRAINT `item_transfers_ibfk_4` FOREIGN KEY (`requested_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `item_transfers_ibfk_5` FOREIGN KEY (`received_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `journal_entries`
--
ALTER TABLE `journal_entries`
  ADD CONSTRAINT `journal_entries_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `journal_lines`
--
ALTER TABLE `journal_lines`
  ADD CONSTRAINT `journal_lines_ibfk_1` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `journal_lines_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES `chart_of_accounts` (`id`);

--
-- Constraints for table `material_requests`
--
ALTER TABLE `material_requests`
  ADD CONSTRAINT `material_requests_ibfk_1` FOREIGN KEY (`recipe_id`) REFERENCES `recipes` (`id`),
  ADD CONSTRAINT `material_requests_ibfk_2` FOREIGN KEY (`requested_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `material_requests_ibfk_3` FOREIGN KEY (`released_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `material_request_items`
--
ALTER TABLE `material_request_items`
  ADD CONSTRAINT `fk_item_product_line` FOREIGN KEY (`material_request_product_id`) REFERENCES `material_request_products` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `material_request_items_ibfk_1` FOREIGN KEY (`material_request_id`) REFERENCES `material_requests` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `material_request_items_ibfk_2` FOREIGN KEY (`raw_material_id`) REFERENCES `raw_materials` (`id`);

--
-- Constraints for table `material_request_products`
--
ALTER TABLE `material_request_products`
  ADD CONSTRAINT `material_request_products_ibfk_1` FOREIGN KEY (`material_request_id`) REFERENCES `material_requests` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `material_request_products_ibfk_2` FOREIGN KEY (`recipe_id`) REFERENCES `recipes` (`id`);

--
-- Constraints for table `metrics`
--
ALTER TABLE `metrics`
  ADD CONSTRAINT `fk_units_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `packaging_logs`
--
ALTER TABLE `packaging_logs`
  ADD CONSTRAINT `packaging_logs_ibfk_1` FOREIGN KEY (`production_batch_id`) REFERENCES `production_batches` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `packaging_logs_ibfk_2` FOREIGN KEY (`packed_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `payroll_runs`
--
ALTER TABLE `payroll_runs`
  ADD CONSTRAINT `payroll_runs_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `payslips`
--
ALTER TABLE `payslips`
  ADD CONSTRAINT `payslips_ibfk_1` FOREIGN KEY (`payroll_run_id`) REFERENCES `payroll_runs` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `payslips_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `pos_terminals`
--
ALTER TABLE `pos_terminals`
  ADD CONSTRAINT `pos_terminals_ibfk_1` FOREIGN KEY (`bank_account_id`) REFERENCES `bank_accounts` (`id`);

--
-- Constraints for table `production_batches`
--
ALTER TABLE `production_batches`
  ADD CONSTRAINT `fk_batch_material_request_product` FOREIGN KEY (`material_request_product_id`) REFERENCES `material_request_products` (`id`),
  ADD CONSTRAINT `production_batches_ibfk_1` FOREIGN KEY (`recipe_id`) REFERENCES `recipes` (`id`),
  ADD CONSTRAINT `production_batches_ibfk_2` FOREIGN KEY (`baker_id`) REFERENCES `users` (`id`);

--
-- Constraints for table `products`
--
ALTER TABLE `products`
  ADD CONSTRAINT `fk_products_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `product_prices`
--
ALTER TABLE `product_prices`
  ADD CONSTRAINT `product_prices_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `product_prices_ibfk_2` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `purchase_orders`
--
ALTER TABLE `purchase_orders`
  ADD CONSTRAINT `fk_po_warehouse` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`),
  ADD CONSTRAINT `purchase_orders_ibfk_1` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`id`),
  ADD CONSTRAINT `purchase_orders_ibfk_2` FOREIGN KEY (`requested_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `purchase_order_items`
--
ALTER TABLE `purchase_order_items`
  ADD CONSTRAINT `purchase_order_items_ibfk_1` FOREIGN KEY (`purchase_order_id`) REFERENCES `purchase_orders` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `purchase_order_items_ibfk_2` FOREIGN KEY (`raw_material_id`) REFERENCES `raw_materials` (`id`);

--
-- Constraints for table `purchase_payments`
--
ALTER TABLE `purchase_payments`
  ADD CONSTRAINT `purchase_payments_ibfk_1` FOREIGN KEY (`purchase_order_id`) REFERENCES `purchase_orders` (`id`),
  ADD CONSTRAINT `purchase_payments_ibfk_2` FOREIGN KEY (`bank_account_id`) REFERENCES `bank_accounts` (`id`),
  ADD CONSTRAINT `purchase_payments_ibfk_3` FOREIGN KEY (`paid_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `purchase_returns`
--
ALTER TABLE `purchase_returns`
  ADD CONSTRAINT `fk_purchase_return_journal` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `purchase_returns_ibfk_1` FOREIGN KEY (`purchase_order_id`) REFERENCES `purchase_orders` (`id`),
  ADD CONSTRAINT `purchase_returns_ibfk_2` FOREIGN KEY (`raw_material_id`) REFERENCES `raw_materials` (`id`),
  ADD CONSTRAINT `purchase_returns_ibfk_3` FOREIGN KEY (`processed_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `raw_materials`
--
ALTER TABLE `raw_materials`
  ADD CONSTRAINT `fk_raw_materials_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `fk_raw_materials_unit` FOREIGN KEY (`unit_id`) REFERENCES `metrics` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `raw_materials_ibfk_1` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `recipes`
--
ALTER TABLE `recipes`
  ADD CONSTRAINT `recipes_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `recipes_ibfk_2` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `recipe_ingredients`
--
ALTER TABLE `recipe_ingredients`
  ADD CONSTRAINT `recipe_ingredients_ibfk_1` FOREIGN KEY (`recipe_id`) REFERENCES `recipes` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `recipe_ingredients_ibfk_2` FOREIGN KEY (`raw_material_id`) REFERENCES `raw_materials` (`id`);

--
-- Constraints for table `role_permissions`
--
ALTER TABLE `role_permissions`
  ADD CONSTRAINT `role_permissions_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `role_permissions_ibfk_2` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `salary_advances`
--
ALTER TABLE `salary_advances`
  ADD CONSTRAINT `salary_advances_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `salary_advances_ibfk_2` FOREIGN KEY (`requested_by`) REFERENCES `users` (`id`),
  ADD CONSTRAINT `salary_advances_ibfk_3` FOREIGN KEY (`decided_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `salary_advance_repayments`
--
ALTER TABLE `salary_advance_repayments`
  ADD CONSTRAINT `salary_advance_repayments_ibfk_1` FOREIGN KEY (`salary_advance_id`) REFERENCES `salary_advances` (`id`),
  ADD CONSTRAINT `salary_advance_repayments_ibfk_2` FOREIGN KEY (`payslip_id`) REFERENCES `payslips` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `sales_orders`
--
ALTER TABLE `sales_orders`
  ADD CONSTRAINT `sales_orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `sales_orders_ibfk_2` FOREIGN KEY (`sales_rep_id`) REFERENCES `users` (`id`);

--
-- Constraints for table `sales_order_items`
--
ALTER TABLE `sales_order_items`
  ADD CONSTRAINT `sales_order_items_ibfk_1` FOREIGN KEY (`sales_order_id`) REFERENCES `sales_orders` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `sales_order_items_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`);

--
-- Constraints for table `sales_order_item_batches`
--
ALTER TABLE `sales_order_item_batches`
  ADD CONSTRAINT `sales_order_item_batches_ibfk_1` FOREIGN KEY (`sales_order_item_id`) REFERENCES `sales_order_items` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `sales_order_item_batches_ibfk_2` FOREIGN KEY (`packaging_log_id`) REFERENCES `packaging_logs` (`id`);

--
-- Constraints for table `sales_returns`
--
ALTER TABLE `sales_returns`
  ADD CONSTRAINT `fk_return_item` FOREIGN KEY (`sales_order_item_id`) REFERENCES `sales_order_items` (`id`),
  ADD CONSTRAINT `fk_return_journal` FOREIGN KEY (`journal_entry_id`) REFERENCES `journal_entries` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `sales_returns_ibfk_1` FOREIGN KEY (`sales_order_id`) REFERENCES `sales_orders` (`id`),
  ADD CONSTRAINT `sales_returns_ibfk_2` FOREIGN KEY (`packaging_log_id`) REFERENCES `packaging_logs` (`id`),
  ADD CONSTRAINT `sales_returns_ibfk_3` FOREIGN KEY (`processed_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `sale_payments`
--
ALTER TABLE `sale_payments`
  ADD CONSTRAINT `sale_payments_ibfk_1` FOREIGN KEY (`sales_order_id`) REFERENCES `sales_orders` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `sale_payments_ibfk_2` FOREIGN KEY (`pos_terminal_id`) REFERENCES `pos_terminals` (`id`),
  ADD CONSTRAINT `sale_payments_ibfk_3` FOREIGN KEY (`bank_account_id`) REFERENCES `bank_accounts` (`id`),
  ADD CONSTRAINT `sale_payments_ibfk_4` FOREIGN KEY (`received_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `settings`
--
ALTER TABLE `settings`
  ADD CONSTRAINT `settings_ibfk_1` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `shifts`
--
ALTER TABLE `shifts`
  ADD CONSTRAINT `fk_shift_marked_by` FOREIGN KEY (`marked_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `shifts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;

--
-- Constraints for table `shift_schedules`
--
ALTER TABLE `shift_schedules`
  ADD CONSTRAINT `shift_schedules_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `shift_schedules_ibfk_2` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `stock_movements`
--
ALTER TABLE `stock_movements`
  ADD CONSTRAINT `fk_stock_movements_warehouse` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`) ON DELETE SET NULL,
  ADD CONSTRAINT `stock_movements_ibfk_1` FOREIGN KEY (`raw_material_id`) REFERENCES `raw_materials` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `stock_movements_ibfk_2` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`);

--
-- Constraints for table `suppliers`
--
ALTER TABLE `suppliers`
  ADD CONSTRAINT `fk_suppliers_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `units`
--
ALTER TABLE `units`
  ADD CONSTRAINT `fk_base_units_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `warehouses`
--
ALTER TABLE `warehouses`
  ADD CONSTRAINT `fk_warehouses_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL;

--
-- Constraints for table `warehouse_stock`
--
ALTER TABLE `warehouse_stock`
  ADD CONSTRAINT `warehouse_stock_ibfk_1` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `warehouse_stock_ibfk_2` FOREIGN KEY (`raw_material_id`) REFERENCES `raw_materials` (`id`) ON DELETE CASCADE;
COMMIT;

