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

START TRANSACTION;



--
-- Database: `statsmain_main`
--

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

--
-- Table structure for table `custom_page_layouts`
--

CREATE TABLE `custom_page_layouts` (
  `id` int(11) NOT NULL,
  `slug` varchar(64) NOT NULL,
  `title` varchar(200) DEFAULT NULL,
  `view_permission_key` varchar(80) DEFAULT NULL,
  `layout_json` text NOT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

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

--
-- Table structure for table `gp_thresholds`
--

CREATE TABLE `gp_thresholds` (
  `id` int(11) NOT NULL,
  `department_type` varchar(50) NOT NULL,
  `green_min` float NOT NULL,
  `yellow_min` float NOT NULL,
  `yellow_max` float NOT NULL,
  `red_max` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `gp_thresholds`
--

INSERT INTO `gp_thresholds` (`id`, `department_type`, `green_min`, `yellow_min`, `yellow_max`, `red_max`) VALUES
(1, 'electrical', 55, 40, 55, 40),
(2, 'commercial', 55, 40, 55, 40),
(3, 'air', 40, 35, 40, 35),
(4, 'solar', 40, 35, 40, 35),
(5, 'residential', 55, 40, 55, 40);

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

--
-- Table structure for table `invoices`
--

CREATE TABLE `invoices` (
  `id` int(11) NOT NULL,
  `invoice_id` int(11) NOT NULL,
  `branch_id` varchar(50) NOT NULL,
  `company_id` int(11) NOT NULL,
  `company_name` varchar(100) NOT NULL,
  `date_issued` datetime NOT NULL,
  `total_ex_tax` float NOT NULL,
  `is_paid` tinyint(1) DEFAULT NULL,
  `customer` text DEFAULT NULL,
  `jobs` text DEFAULT NULL,
  `stage` varchar(100) DEFAULT NULL,
  `status` varchar(100) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

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

--
-- Table structure for table `kpi_target_overrides`
--

CREATE TABLE `kpi_target_overrides` (
  `id` int(11) NOT NULL,
  `branch` varchar(80) NOT NULL,
  `department` varchar(80) NOT NULL,
  `metric_key` varchar(80) NOT NULL,
  `target` float DEFAULT NULL,
  `weight` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `kpi_target_overrides`
--

INSERT INTO `kpi_target_overrides` (`id`, `branch`, `department`, `metric_key`, `target`, `weight`) VALUES
(1, 'Busselton', 'Air', 'quoteConversion', 50, NULL),
(2, 'Bunbury', 'Air', 'quoteConversion', 50, NULL),
(3, 'Mandurah', 'Air', 'quoteConversion', 50, NULL);

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

--
-- Table structure for table `revenue_targets`
--

CREATE TABLE `revenue_targets` (
  `id` int(11) NOT NULL,
  `branch_id` varchar(50) NOT NULL,
  `company_id` int(11) NOT NULL,
  `target` int(11) DEFAULT NULL,
  `breakeven` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

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

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

CREATE TABLE `roles` (
  `id` int(11) NOT NULL,
  `name` varchar(60) NOT NULL,
  `slug` varchar(20) DEFAULT NULL,
  `is_system` tinyint(1) NOT NULL,
  `created_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

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

INSERT INTO `roles` (`id`, `name`, `slug`, `is_system`, `created_at`) VALUES
(1, 'User', 'user', 1, '2026-02-17 22:18:11'),
(2, 'Admin', 'admin', 1, '2026-02-17 22:18:11'),
(3, 'Director', 'director', 1, '2026-02-17 22:18:11'),
(5, 'Ops Manager Busselton', NULL, 0, '2026-02-23 00:47:05'),
(6, 'Ops Manager Bunbury Air', NULL, 0, '2026-02-23 01:08:56'),
(7, 'Ops Manager Bunbury Electrical', NULL, 0, '2026-02-23 01:11:56'),
(8, 'Ops Manager Bunbury Commercial', NULL, 0, '2026-02-23 01:12:40'),
(9, 'Nixon Directors', NULL, 0, '2026-02-23 01:25:14'),
(10, 'Ops Manager Bunbury Solar', NULL, 0, '2026-02-23 01:31:51'),
(11, 'Ops Manager Mandurah Air', NULL, 0, '2026-02-23 01:32:32'),
(12, 'Ops Manager Mandurah Electrical', NULL, 0, '2026-02-23 01:33:01'),
(13, 'Mandurah Director', NULL, 0, '2026-02-23 01:33:49'),
(14, 'Ops Manager Bunbury Residential', NULL, 0, '2026-02-23 01:37:56'),
(15, 'Office Manager', NULL, 0, '2026-02-23 01:46:13'),
(16, 'adam', NULL, 0, '2026-02-23 03:05:40');

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

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

CREATE TABLE `role_permissions` (
  `role_id` int(11) NOT NULL,
  `permission_id` int(11) 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, 9),
(1, 10),
(1, 11),
(1, 12),
(1, 13),
(1, 14),
(1, 15),
(1, 16),
(1, 17),
(1, 18),
(1, 19),
(1, 20),
(2, 1),
(2, 2),
(2, 3),
(2, 4),
(2, 5),
(2, 6),
(2, 8),
(2, 9),
(2, 10),
(2, 11),
(2, 12),
(2, 13),
(2, 14),
(2, 15),
(2, 16),
(2, 17),
(2, 18),
(2, 19),
(2, 20),
(2, 21),
(2, 22),
(2, 23),
(2, 24),
(2, 25),
(3, 1),
(3, 2),
(3, 3),
(3, 4),
(3, 5),
(3, 6),
(3, 7),
(3, 8),
(3, 9),
(3, 10),
(3, 11),
(3, 12),
(3, 13),
(3, 14),
(3, 15),
(3, 16),
(3, 17),
(3, 18),
(3, 19),
(3, 20),
(3, 21),
(3, 22),
(3, 23),
(3, 24),
(3, 25),
(5, 10),
(5, 13),
(5, 14),
(6, 1),
(6, 2),
(6, 3),
(6, 4),
(6, 11),
(6, 16),
(7, 1),
(7, 2),
(7, 3),
(7, 4),
(7, 6),
(7, 7),
(7, 11),
(7, 13),
(8, 1),
(8, 2),
(8, 3),
(8, 4),
(8, 6),
(8, 7),
(8, 11),
(8, 18),
(9, 2),
(9, 4),
(9, 5),
(9, 8),
(9, 9),
(9, 10),
(9, 11),
(9, 12),
(9, 13),
(9, 14),
(9, 15),
(9, 16),
(9, 17),
(9, 18),
(9, 19),
(9, 20),
(10, 1),
(10, 2),
(10, 3),
(10, 4),
(10, 11),
(10, 17),
(11, 1),
(11, 2),
(11, 3),
(11, 4),
(11, 12),
(11, 20),
(12, 1),
(12, 2),
(12, 3),
(12, 4),
(12, 12),
(12, 19),
(13, 12),
(13, 19),
(13, 20),
(14, 1),
(14, 2),
(14, 3),
(14, 4),
(14, 11),
(14, 15),
(15, 1),
(15, 2),
(15, 3),
(15, 4),
(15, 11),
(15, 15),
(15, 16),
(15, 17),
(15, 18),
(16, 11),
(16, 17);

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

--
-- Table structure for table `sync_metadata`
--

CREATE TABLE `sync_metadata` (
  `id` int(11) NOT NULL,
  `branch_id` varchar(50) NOT NULL,
  `company_id` int(11) NOT NULL,
  `last_sync_date` datetime DEFAULT NULL,
  `last_sync_timestamp` datetime NOT NULL,
  `invoice_count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

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

--
-- Table structure for table `user_permissions`
--

CREATE TABLE `user_permissions` (
  `user_id` int(11) NOT NULL,
  `permission_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `user_permissions`
--

INSERT INTO `user_permissions` (`user_id`, `permission_id`) VALUES
(2, 1),
(2, 2),
(2, 3),
(2, 4),
(2, 5),
(2, 6),
(2, 7),
(2, 8),
(2, 9),
(2, 10),
(2, 11),
(2, 12),
(2, 13),
(2, 14),
(2, 15),
(2, 16),
(2, 17),
(2, 18),
(2, 19),
(2, 20),
(12, 1),
(12, 2),
(12, 3),
(12, 4),
(12, 5),
(12, 6),
(12, 7),
(12, 8),
(12, 9),
(12, 10),
(12, 11),
(12, 12),
(12, 13),
(12, 14),
(12, 15),
(12, 16),
(12, 17),
(12, 18),
(12, 19),
(12, 20),
(17, 10),
(17, 14),
(18, 10),
(18, 21),
(20, 10),
(20, 13),
(20, 14),
(21, 11),
(21, 15),
(22, 11),
(22, 16),
(23, 11),
(23, 18),
(24, 12),
(24, 19),
(25, 12),
(25, 20),
(27, 11),
(27, 18),
(28, 8),
(28, 9),
(28, 10),
(28, 11),
(28, 12),
(28, 13),
(28, 14),
(28, 15),
(28, 16),
(28, 17),
(28, 18),
(28, 19),
(28, 20),
(29, 8),
(29, 9),
(29, 10),
(29, 11),
(29, 12),
(29, 13),
(29, 14),
(29, 15),
(29, 16),
(29, 17),
(29, 18),
(29, 19),
(29, 20),
(30, 8),
(30, 9),
(30, 10),
(30, 11),
(30, 12),
(30, 13),
(30, 14),
(30, 15),
(30, 16),
(30, 17),
(30, 18),
(30, 19),
(30, 20),
(31, 12),
(31, 19),
(31, 20),
(32, 11),
(32, 15),
(32, 16),
(32, 17),
(32, 18),
(33, 8),
(33, 9),
(33, 10),
(33, 11),
(33, 12),
(33, 13),
(33, 14),
(33, 15),
(33, 16),
(33, 17),
(33, 18),
(33, 19),
(33, 20),
(34, 11),
(34, 17),
(35, 10),
(35, 21),
(36, 11),
(36, 22),
(37, 12),
(37, 23),
(39, 11),
(39, 17);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `custom_page_layouts`
--
ALTER TABLE `custom_page_layouts`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `ix_custom_page_layouts_slug` (`slug`),
  ADD KEY `ix_custom_page_layouts_view_permission_key` (`view_permission_key`);

--
-- Indexes for table `gp_thresholds`
--
ALTER TABLE `gp_thresholds`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `department_type` (`department_type`),
  ADD UNIQUE KEY `idx_gp_thresholds_dept` (`department_type`);

--
-- Indexes for table `invoices`
--
ALTER TABLE `invoices`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `idx_unique_invoice` (`branch_id`,`company_id`,`invoice_id`),
  ADD KEY `ix_invoices_invoice_id` (`invoice_id`),
  ADD KEY `idx_branch_company_date` (`branch_id`,`company_id`,`date_issued`),
  ADD KEY `ix_invoices_branch_id` (`branch_id`),
  ADD KEY `ix_invoices_company_id` (`company_id`),
  ADD KEY `ix_invoices_date_issued` (`date_issued`);

--
-- Indexes for table `kpi_target_overrides`
--
ALTER TABLE `kpi_target_overrides`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `idx_kpi_overrides_branch_dept_metric` (`branch`,`department`,`metric_key`);

--
-- Indexes for table `revenue_targets`
--
ALTER TABLE `revenue_targets`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `idx_revenue_targets_branch_company` (`branch_id`,`company_id`);

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

--
-- 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 `sync_metadata`
--
ALTER TABLE `sync_metadata`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `idx_branch_company_sync` (`branch_id`,`company_id`);

--
-- Indexes for table `user_permissions`
--
ALTER TABLE `user_permissions`
  ADD PRIMARY KEY (`user_id`,`permission_id`),
  ADD KEY `permission_id` (`permission_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `custom_page_layouts`
--
ALTER TABLE `custom_page_layouts`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `gp_thresholds`
--
ALTER TABLE `gp_thresholds`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

--
-- AUTO_INCREMENT for table `invoices`
--
ALTER TABLE `invoices`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `kpi_target_overrides`
--
ALTER TABLE `kpi_target_overrides`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- AUTO_INCREMENT for table `revenue_targets`
--
ALTER TABLE `revenue_targets`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `roles`
--
ALTER TABLE `roles`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;

--
-- AUTO_INCREMENT for table `sync_metadata`
--
ALTER TABLE `sync_metadata`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- Constraints for dumped tables
--

--
-- 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 `user_permissions`
--
ALTER TABLE `user_permissions`
  ADD CONSTRAINT `user_permissions_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `user_permissions_ibfk_2` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE;
COMMIT;

