START TRANSACTION ;
ALTER TABLE `notifications`
  ADD COLUMN IF NOT EXISTS `settings` text DEFAULT NULL AFTER `is_active`;

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_tokens', 'backend', 'Notifications / Company tokens', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_tokens' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Company tokens', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_name', 'backend', 'Notifications / Company token name', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_name' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company name', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_logo', 'backend', 'Notifications / Company token logo', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_logo' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company logo', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_address', 'backend', 'Notifications / Company token address', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_address' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company address', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_zip', 'backend', 'Notifications / Company token zip', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_zip' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company ZIP or postal code', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_city', 'backend', 'Notifications / Company token city', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_city' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company city', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_country', 'backend', 'Notifications / Company token country', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_country' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company country', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_state', 'backend', 'Notifications / Company token state', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_state' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company state or region', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_phone', 'backend', 'Notifications / Company token phone', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_phone' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company phone number', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_email', 'backend', 'Notifications / Company token email', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_email' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company email address', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'notifications_company_token_url', 'backend', 'Notifications / Company token URL', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'notifications_company_token_url' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'company website URL', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'menu_request_customization', 'backend', 'Menu / Request customization', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'menu_request_customization' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Request customization', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'menuHelpManual', 'backend', 'Menu / Help manual', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'menuHelpManual' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Help manual', 'script');

INSERT IGNORE INTO `fields` VALUES (NULL, 'pjAdminOrders_pjActionHelp', 'backend', 'pjAdminOrders_pjActionHelp', 'script', NULL);
SET @id := (SELECT `id` FROM `fields` WHERE `key` = 'pjAdminOrders_pjActionHelp' LIMIT 1);
INSERT IGNORE INTO `multi_lang` VALUES (NULL, @id, 'pjBaseField', '::LOCALE::', 'title', 'Help manual', 'script');

INSERT INTO `plugin_auth_permissions` (`parent_id`, `key`)
SELECT p.id, 'pjAdminOrders_pjActionHelp'
FROM `plugin_auth_permissions` p
WHERE p.`key` = 'pjAdminOrders'
  AND NOT EXISTS (SELECT 1 FROM `plugin_auth_permissions` WHERE `key` = 'pjAdminOrders_pjActionHelp');

INSERT IGNORE INTO `plugin_auth_roles_permissions` (`role_id`, `permission_id`)
SELECT rp.role_id, child.id
FROM `plugin_auth_roles_permissions` rp
JOIN `plugin_auth_permissions` parent ON parent.id = rp.permission_id
JOIN `plugin_auth_permissions` child ON child.`key` = 'pjAdminOrders_pjActionHelp'
WHERE parent.`key` = 'pjAdminOrders';

INSERT IGNORE INTO `plugin_auth_users_permissions` (`user_id`, `permission_id`)
SELECT up.user_id, child.id
FROM `plugin_auth_users_permissions` up
JOIN `plugin_auth_permissions` parent ON parent.id = up.permission_id
JOIN `plugin_auth_permissions` child ON child.`key` = 'pjAdminOrders_pjActionHelp'
WHERE parent.`key` = 'pjAdminOrders';
