

-- Insert Quick Transaction right (ID 110)
INSERT INTO `rights` (`id`, `name`, `description`, `category`) 
VALUES (110, 'Quick Transaction', 'Create quick financial transactions', 'Financials')
ON DUPLICATE KEY UPDATE 
    `name` = VALUES(`name`),
    `description` = VALUES(`description`),
    `category` = VALUES(`category`);

-- Assign Quick Transaction right to Admin role (role_id = 1)
INSERT INTO `role_rights` (`role_id`, `right_id`) 
VALUES (1, 110)
ON DUPLICATE KEY UPDATE 
    `role_id` = VALUES(`role_id`), 
    `right_id` = VALUES(`right_id`);

-- Insert Journal Entries right (ID 111)
INSERT INTO `rights` (`id`, `name`, `description`, `category`) 
VALUES (111, 'Journal Entries', 'View and manage journal entries', 'Financials')
ON DUPLICATE KEY UPDATE 
    `name` = VALUES(`name`),
    `description` = VALUES(`description`),
    `category` = VALUES(`category`);

-- Assign Journal Entries right to Admin role (role_id = 1)
INSERT INTO `role_rights` (`role_id`, `right_id`) 
VALUES (1, 111)
ON DUPLICATE KEY UPDATE 
    `role_id` = VALUES(`role_id`), 
    `right_id` = VALUES(`right_id`);


-- 1) Create the item category "General"
INSERT INTO `categories` (`name`, `icon`)
VALUES ('General', 'assets/img/categories/category-01.png');

-- 2) Create the sub-category "Sub General" under that new category
INSERT INTO `sub_categories` (`category_id`, `name`, `display_color`)
VALUES (LAST_INSERT_ID(), 'Sub General', NULL);



-- 1) Speed up item lookups (POS grid + search_items.php)
CREATE INDEX idx_items_category_id      ON items (category_id);
CREATE INDEX idx_items_sub_category_id  ON items (sub_category_id);
CREATE INDEX idx_items_item_code        ON items (item_code);
CREATE INDEX idx_items_name             ON items (name);

-- If you often filter by role on POS:
CREATE INDEX idx_items_item_role        ON items (item_role);

-- 2) Speed up tax lookups (getTaxRate / tax_helpers)
CREATE INDEX idx_tax_rates_id           ON tax_rates (id);

-- 3) Speed up customer lookups (POS + save_sale.php + reports)
CREATE INDEX idx_customers_name         ON customers (name);
CREATE INDEX idx_customers_is_walk_in   ON customers (is_walk_in);

-- 4) Sales / sale_items (common report filters + joins)
CREATE INDEX idx_sales_invoice_number   ON sales (invoice_number);
CREATE INDEX idx_sales_date             ON sales (sale_date);       -- or your date column name
CREATE INDEX idx_sales_customer_id      ON sales (customer_id);
CREATE INDEX idx_sales_order_mode       ON sales (order_mode);

CREATE INDEX idx_sale_items_sale_id     ON sale_items (sale_id);
CREATE INDEX idx_sale_items_item_id     ON sale_items (item_id);

-- 5) Settings table (you query by code a lot)
CREATE UNIQUE INDEX idx_settings_code   ON settings (code);


ALTER TABLE hold_orders
  ADD COLUMN rider_id INT NULL AFTER order_mode;


INSERT INTO rights (id, name, description, category)
VALUES
(112, 'Rider Sales Report', 'View rider-wise delivery sales report', 'Reports');
INSERT INTO role_rights (role_id, right_id)
VALUES (1, 112);


ALTER TABLE delivery_orders
  ADD KEY idx_delivery_orders_created (created_at);



INSERT INTO `settings` (`store_id`, `category_id`, `name`, `code`, `type`, `value`, `default_value`, `description`, `is_active`)
SELECT NULL, sc.id, 'Enable Financials (GL)', 'financials_enabled', 'boolean', '1', '1',
'When enabled, the system creates and posts journal entries (double-entry) for sales, purchases, expenses, payments, transfers, and adjustments. When disabled, day-to-day operations continue without writing to the general ledger.', 1
FROM `setting_categories` sc
WHERE sc.code = 'accounts'
  AND NOT EXISTS (SELECT 1 FROM `settings` s WHERE s.code = 'financials_enabled')
LIMIT 1;



INSERT IGNORE INTO `settings` (`store_id`, `category_id`, `name`, `code`, `type`, `value`, `default_value`, `description`, `is_active`)
VALUES (NULL, 1, 'Invoice Footer Note', 'invoice_footer_note', 'text', '', '', 'Optional note shown at the bottom of invoices and receipts (thermal, A4, POS preview, LAN/USB). Use multiple lines if needed.', 1);
