ALTER TABLE `PageTranslation`
  ADD CONSTRAINT `FK1_PageTranslation_Lang_langId` FOREIGN KEY (`langId`) REFERENCES `Lang` (`id`)
  ON UPDATE NO ACTION
  ON DELETE NO ACTION,
  ADD CONSTRAINT `FK2_PageTranslation_Page_pageId` FOREIGN KEY (`pageId`) REFERENCES `Page` (`id`)
  ON UPDATE NO ACTION
  ON DELETE NO ACTION;

INSERT INTO Page (id, slug, url, icon, permission)
VALUES (108, 'product_slim_list', '/blueseal/prodotti/slim', 'fa-list', '/admin/product/list');
INSERT INTO PageTranslation (pageId, langId, title, description, keywords)
VALUES (108, 1, 'Lista Prodotti Slim', NULL, NULL);

CREATE OR REPLACE VIEW vBluesealProductSlimList as
SELECT
  `p`.`id`                                       AS `id`,
  `p`.`productVariantId`                         AS `productVariantId`,
  concat(`p`.`id`, '-', `p`.`productVariantId`)  AS `code`,
  `pb`.`name`                                    AS `brand`,
  concat(`p`.`itemno`, ' # ', `pv`.`name`)       AS `cpf`,
  `shp`.`extId`                                  AS `externalId`,
  concat(`ps`.`name`, ' ', `ps`.`year`)          AS `season`,
  `s`.`id`                                       AS `shopId`,
  `s`.`title`                                    AS `shop`,
  if((`p`.`qty` > 0), 'disponibile', 'mancante') AS `stock`,
  p.creationDate,
  pss.name as status
FROM `Product` `p`
  JOIN `ProductVariant` `pv` JOIN `ProductBrand` `pb` JOIN ProductStatus pss JOIN
  `ProductSeason` `ps` JOIN `ShopHasProduct` `shp` JOIN `Shop` `s`
WHERE (
  (`p`.`productVariantId` = `pv`.`id`) AND (`p`.`productBrandId` = `pb`.`id`) AND (`p`.`id` = `shp`.`productId`) AND
  (`p`.`productVariantId` = `shp`.`productVariantId`) AND (`s`.`id` = `shp`.`shopId`) AND
  (`p`.`productSeasonId` = `ps`.`id`) and pss.id = p.productStatusId) and pss.id not in (7,8)
GROUP BY `p`.`id`, `p`.`productVariantId`, `s`.`id`
ORDER BY `p`.`creationDate` DESC;

INSERT INTO Page (id,slug, url, icon, permission) VALUES (110,'barcode_print', '/blueseal/prodotti/barcode/print', 'fa-default', '/admin/product/list');
INSERT INTO PageTranslation (pageId, langId, title, description, keywords) VALUES (110, 1, 'Stampa Barcode', null, null);

alter table StorehouseOperation drop FOREIGN KEY fk_StorehouseOperation_Storehouse1;
alter table StorehouseOperation add CONSTRAINT `fk_StorehouseOperation_Storehouse1` FOREIGN KEY (`storehouseId`,`shopId`) REFERENCES `Storehouse` (`id`, `shopId`) ON UPDATE NO ACTION ON DELETE NO ACTION;

drop trigger StorehouseOperation_BEFORE_INSERT;
CREATE TRIGGER `StorehouseOperation_BEFORE_INSERT` BEFORE INSERT ON `StorehouseOperation` FOR EACH ROW BEGIN
  SELECT COALESCE(MAX(`id`) + 1, 1) INTO @v FROM StorehouseOperation WHERE `shopId` = NEW.`shopId` and storehouseId = NEW.storehouseId;
  SET NEW.`id` = @v;
END;

CREATE TRIGGER `Storehouse_BEFORE_INSERT` BEFORE INSERT ON `Storehouse` FOR EACH ROW BEGIN
  SELECT COALESCE(MAX(`id`) + 1, 1) INTO @v FROM Storehouse WHERE `shopId` = NEW.`shopId`;
  SET NEW.`id` = @v;
END;