USE `vn`;

ALTER TABLE `vn`.`ticketRequest` 
DROP FOREIGN KEY `fgnAtender`;
ALTER TABLE `vn`.`ticketRequest` 
CHANGE COLUMN `atenderFk` `attenderFk` INT(11) NULL DEFAULT NULL ;
ALTER TABLE `vn`.`ticketRequest` 
ADD CONSTRAINT `fgnAtender`
  FOREIGN KEY (`attenderFk`)
  REFERENCES `vn`.`worker` (`id`)
  ON UPDATE CASCADE;

USE `vn2008`;
CREATE
     OR REPLACE ALGORITHM = UNDEFINED
    DEFINER = `root`@`%`
    SQL SECURITY DEFINER
VIEW `vn2008`.`Ordenes` AS
    SELECT
        `tr`.`id` AS `Id_ORDEN`,
        `tr`.`description` AS `ORDEN`,
        `tr`.`requesterFk` AS `requesterFk`,
        `tr`.`attenderFk` AS `attenderFk`,
        `tr`.`quantity` AS `CANTIDAD`,
        `tr`.`itemFk` AS `Id_ARTICLE`,
        `tr`.`price` AS `PRECIOMAX`,
        `tr`.`isOk` AS `isOk`,
        `tr`.`saleFk` AS `Id_Movimiento`,
        `tr`.`ticketFk` AS `ticketFk`,
        `tr`.`response` AS `COMENTARIO`,
        `tr`.`created` AS `odbc_date`,
        `tr`.`ordered` AS `datORDEN`,
        `tr`.`shipped` AS `datTICKET`,
        `tr`.`salesPersonCode` AS `CodVENDEDOR`,
        `tr`.`buyerCode` AS `CodCOMPRADOR`,
        `tr`.`price__` AS `PREU`,
        `tr`.`clientFk` AS `Id_CLIENTE`,
        `tr`.`ok__` AS `OK`,
        `tr`.`total` AS `TOTAL`,
        `tr`.`buyed` AS `datCOMPRA`,
        `tr`.`ko__` AS `KO`
    FROM
        `vn`.`ticketRequest` `tr`;

USE `vn`;

DROP TRIGGER IF EXISTS `vn`.`ticketRequest_beforeInsert`;

DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` TRIGGER `vn`.`ticketRequest_beforeInsert` BEFORE INSERT ON `ticketRequest` FOR EACH ROW
BEGIN
    IF NEW.ticketFk IS NULL THEN
        SET NEW.ticketFk = (SELECT s.ticketFk FROM sale s WHERE s.id = NEW.saleFk);
    END IF;

    IF NEW.requesterFk IS NULL THEN
        SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode);
    END IF;

    IF NEW.attenderFk IS NULL THEN
        SET NEW.attenderFk = (SELECT w.id FROM worker w WHERE w.code = NEW.buyerCode);
    END IF;
END$$
DELIMITER ; 


DROP TRIGGER IF EXISTS `vn`.`ticketRequest_beforeUpdate`;

DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` TRIGGER `vn`.`ticketRequest_beforeUpdate` BEFORE UPDATE ON `ticketRequest` FOR EACH ROW
BEGIN
    IF NEW.saleFk <> OLD.saleFk THEN
        SET NEW.ticketFk = (SELECT s.ticketFk FROM sale s WHERE s.id = NEW.saleFk);
    END IF;

    IF NEW.salesPersonCode <> OLD.salesPersonCode THEN
        SET NEW.requesterFk = (SELECT w.id FROM worker w WHERE w.code = NEW.salesPersonCode);
    END IF;

    IF NEW.buyerCode <> OLD.buyerCode THEN
        SET NEW.attenderFk = (SELECT w.id FROM worker w WHERE w.code = NEW.buyerCode);
    END IF;
END$$
DELIMITER ;

USE `vn`;
CREATE 
     OR REPLACE ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
    SQL SECURITY DEFINER
VIEW `vn`.`ticketRequest__` AS
    SELECT 
        `t`.`Id_ORDEN` AS `id`,
        `t`.`ORDEN` AS `description`,
        `t`.`requesterFk` AS `requesterFk`,
        `t`.`attenderFk` AS `attenderFk`,
        `t`.`CANTIDAD` AS `quantity`,
        `t`.`Id_ARTICLE` AS `itemFk`,
        `t`.`PRECIOMAX` AS `price`,
        `t`.`isOk` AS `isOk`,
        `t`.`Id_Movimiento` AS `saleFk`,
        `t`.`ticketFk` AS `ticketFk`,
        `t`.`COMENTARIO` AS `response`,
        `t`.`odbc_date` AS `created`
    FROM
        `vn2008`.`Ordenes` `t`;
