-- Permission records and labels for current Shopping Cart modules.
-- Visible labels use the default fields -> LAST_INSERT_ID() -> multi_lang pattern.

INSERT INTO `plugin_auth_permissions` (`parent_id`, `key`, `is_shown`)
SELECT p.id, child_key.`key`, 'T'
FROM `plugin_auth_permissions` p
JOIN (
  SELECT 'pjInvoice_pjActionSaveInvoice' AS `key`
  UNION ALL SELECT 'pjInvoice_generatePdf'
  UNION ALL SELECT 'pjInvoice_pjActionDownloadBulk'
  UNION ALL SELECT 'pjInvoice_pjActionDeleteBulk'
) AS child_key
WHERE p.`key` = 'pjInvoice'
  AND NOT EXISTS (SELECT 1 FROM `plugin_auth_permissions` WHERE `key` = child_key.`key`);

UPDATE `plugin_auth_permissions`
SET `is_shown` = 'T'
WHERE `key` IN (
  'pjInvoice',
  'pjInvoice_pjActionIndex',
  'pjInvoice_pjActionInvoices',
  'pjInvoice_pjActionGetInvoices',
  'pjInvoice_pjActionView',
  'pjInvoice_pjActionSend',
  'pjInvoice_pjActionPrint',
  'pjInvoice_pjActionCreate',
  'pjInvoice_pjActionDelete',
  'pjInvoice_pjActionGeneratePdf',
  'pjInvoice_generatePdf',
  'pjInvoice_pjActionDownloadBulk',
  'pjInvoice_pjActionDeleteBulk',
  'pjInvoice_pjActionSaveInvoice',
  'pjAdminProfile',
  'pjAdminProfile_pjActionIndex',
  'pjAdminProfile_pjActionDeleteLogo',
  'pjAdminProfile_pjActionDeleteEmailLogo',
  'pjAdminOptions_pjActionGrid',
  'pjAdminOptions_pjActionSaveThemeColors',
  'pjBaseOptions_pjActionImages',
  'pjBaseOptions_pjActionUploadSidebarLogo',
  'pjBaseOptions_pjActionRemoveSidebarLogo',
  'pjBaseUsers_pjActionProfile',
  'pjBasePermissions_pjActionAjaxSet'
);

INSERT INTO `plugin_base_multi_lang` (`foreign_id`, `model`, `locale`, `field`, `content`, `source`)
SELECT p.id, 'pjAuthPermission', l.id, 'title', labels.title, 'data'
FROM `plugin_auth_permissions` p
JOIN `plugin_base_locale` l
JOIN (
  SELECT 'pjAdminOptions' AS `key`, 'Settings Menu' AS title
  UNION ALL SELECT 'pjAdminOptions_pjActionBooking', 'Order settings'
  UNION ALL SELECT 'pjPayments_pjActionIndex', 'Payments'
  UNION ALL SELECT 'pjAdminOptions_pjActionBookingForm', 'Checkout Form'
  UNION ALL SELECT 'pjAdminOptions_pjActionShippingTax', 'Shipping & Tax'
  UNION ALL SELECT 'pjAdminOptions_pjActionNotifications', 'Notifications'
  UNION ALL SELECT 'pjAdminOptions_pjActionTerm', 'Terms'
  UNION ALL SELECT 'pjAdminOptions_pjActionInvoiceTemplate', 'Invoice template'
  UNION ALL SELECT 'pjAdminOptions_pjActionPreview', 'Preview'
  UNION ALL SELECT 'pjAdminOptions_pjActionInstall', 'Install Menu'
  UNION ALL SELECT 'pjAdminOptions_pjActionGrid', 'Product Display'
  UNION ALL SELECT 'pjAdminOptions_pjActionSaveThemeColors', 'Save theme colors'
  UNION ALL SELECT 'pjAdminCategories', 'Categories Menu'
  UNION ALL SELECT 'pjAdminCategories_pjActionIndex', 'Categories List'
  UNION ALL SELECT 'pjAdminCategories_pjActionCreateForm', 'Add categories'
  UNION ALL SELECT 'pjAdminCategories_pjActionUpdateForm', 'Edit categories'
  UNION ALL SELECT 'pjAdminCategories_pjActionDeleteCategory', 'Delete single category'
  UNION ALL SELECT 'pjAdminCategories_pjActionDeleteCategoryBulk', 'Delete multiple categories'
  UNION ALL SELECT 'pjAdminProducts', 'Products Menu'
  UNION ALL SELECT 'pjAdminProducts_pjActionIndex', 'Products List'
  UNION ALL SELECT 'pjAdminProducts_pjActionCreate', 'Add products'
  UNION ALL SELECT 'pjAdminProducts_pjActionUpdate', 'Edit products'
  UNION ALL SELECT 'pjAdminProducts_pjActionDeleteProduct', 'Delete single product'
  UNION ALL SELECT 'pjAdminProducts_pjActionDeleteProductBulk', 'Delete multiple products'
  UNION ALL SELECT 'pjAdminProducts_pjActionStock', 'Stock'
  UNION ALL SELECT 'pjAdminVouchers', 'Vouchers Menu'
  UNION ALL SELECT 'pjAdminVouchers_pjActionIndex', 'Vouchers List'
  UNION ALL SELECT 'pjAdminVouchers_pjActionCreate', 'Add vouchers'
  UNION ALL SELECT 'pjAdminVouchers_pjActionUpdate', 'Edit vouchers'
  UNION ALL SELECT 'pjAdminVouchers_pjActionDeleteVoucher', 'Delete single voucher'
  UNION ALL SELECT 'pjAdminVouchers_pjActionDeleteVoucherBulk', 'Delete multiple vouchers'
  UNION ALL SELECT 'pjAdminOrders', 'Orders Menu'
  UNION ALL SELECT 'pjAdminOrders_pjActionIndex', 'Orders List'
  UNION ALL SELECT 'pjAdminOrders_pjActionUpdate', 'Edit orders'
  UNION ALL SELECT 'pjAdminOrders_pjActionDeleteOrder', 'Delete single order'
  UNION ALL SELECT 'pjAdminOrders_pjActionDeleteOrderBulk', 'Delete multiple orders'
  UNION ALL SELECT 'pjAdminClients', 'Clients Menu'
  UNION ALL SELECT 'pjAdminClients_pjActionIndex', 'Clients List'
  UNION ALL SELECT 'pjAdminClients_pjActionCreate', 'Add clients'
  UNION ALL SELECT 'pjAdminClients_pjActionUpdate', 'Edit clients'
  UNION ALL SELECT 'pjAdminClients_pjActionDeleteClient', 'Delete single client'
  UNION ALL SELECT 'pjAdminClients_pjActionDeleteClientBulk', 'Delete multiple clients'
  UNION ALL SELECT 'pjAdminClients_pjActionExportClient', 'Export clients'
  UNION ALL SELECT 'pjAdminReports_pjActionIndex', 'Reports Menu'
  UNION ALL SELECT 'pjAdmin_pjActionIndex', 'Dashboard'
  UNION ALL SELECT 'pjAdminSetup', 'Setup wizard'
  UNION ALL SELECT 'pjAdminSetup_pjActionWelcome', 'Setup wizard'
  UNION ALL SELECT 'pjAdminSetup_pjActionLoadDemo', 'Load demo data'
  UNION ALL SELECT 'pjAdminSetup_pjActionSkipDemo', 'Skip demo data'
  UNION ALL SELECT 'pjAdminSetup_pjActionRemoveDemo', 'Remove demo data'
  UNION ALL SELECT 'pjAdminSetup_pjActionHasDemoData', 'Check demo data'
  UNION ALL SELECT 'pjBaseUsers_pjActionProfile', 'My account'
  UNION ALL SELECT 'pjBasePermissions_pjActionAjaxSet', 'Save permission changes'
  UNION ALL SELECT 'pjBaseOptions_pjActionImages', 'Images'
  UNION ALL SELECT 'pjBaseOptions_pjActionUploadSidebarLogo', 'Upload sidebar logo'
  UNION ALL SELECT 'pjBaseOptions_pjActionRemoveSidebarLogo', 'Remove sidebar logo'
  UNION ALL SELECT 'pjInvoice', 'Invoices Menu'
  UNION ALL SELECT 'pjInvoice_pjActionIndex', 'Invoice settings'
  UNION ALL SELECT 'pjInvoice_pjActionInvoices', 'Invoices List'
  UNION ALL SELECT 'pjInvoice_pjActionGetInvoices', 'Load invoices'
  UNION ALL SELECT 'pjInvoice_pjActionView', 'View invoice'
  UNION ALL SELECT 'pjInvoice_pjActionSend', 'Send invoice'
  UNION ALL SELECT 'pjInvoice_pjActionPrint', 'Print invoice'
  UNION ALL SELECT 'pjInvoice_pjActionCreate', 'Create invoice'
  UNION ALL SELECT 'pjInvoice_pjActionDelete', 'Delete invoice'
  UNION ALL SELECT 'pjInvoice_pjActionGeneratePdf', 'Download PDF'
  UNION ALL SELECT 'pjInvoice_generatePdf', 'Generate PDF'
  UNION ALL SELECT 'pjInvoice_pjActionDownloadBulk', 'Download invoice PDFs'
  UNION ALL SELECT 'pjInvoice_pjActionDeleteBulk', 'Delete multiple invoices'
  UNION ALL SELECT 'pjInvoice_pjActionSaveInvoice', 'Edit invoice status'
  UNION ALL SELECT 'pjAdminProfile', 'Company profile'
  UNION ALL SELECT 'pjAdminProfile_pjActionIndex', 'Edit company profile'
  UNION ALL SELECT 'pjAdminProfile_pjActionDeleteLogo', 'Delete company logo'
  UNION ALL SELECT 'pjAdminProfile_pjActionDeleteEmailLogo', 'Delete email logo'
) AS labels ON labels.`key` = p.`key`
WHERE NOT EXISTS (
  SELECT 1
  FROM `plugin_base_multi_lang` ml
  WHERE ml.foreign_id = p.id
    AND ml.model = 'pjAuthPermission'
    AND ml.locale = l.id
    AND ml.field = 'title'
);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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