ALTER TABLE `vn`.`agencyTerm` ADD `supplierFk` INT NULL;
ALTER TABLE `vn`.`agencyTerm` CHANGE `supplierFk` `supplierFk` INT NULL AFTER `agencyFk`;

UPDATE `vn`.`agencyTerm` `at`
	JOIN `vn`.`agency` `a` ON `a`.`id` = `at`.`agencyFk`
SET `at`.`supplierFk` = `a`.`supplierFk`;

ALTER TABLE `vn`.`agencyTerm` ADD CONSTRAINT `agencyTerm_FK` FOREIGN KEY (`agencyFk`) REFERENCES `vn`.`agency`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `vn`.`agencyTerm` ADD CONSTRAINT `agencyTerm_FK_1` FOREIGN KEY (`supplierFk`) REFERENCES `vn`.`supplier`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

RENAME TABLE `vn`.`agencyTerm` TO `vn`.`supplierAgencyTerm`;

CREATE OR REPLACE
ALGORITHM = UNDEFINED 
DEFINER=`root`@`localhost`  
VIEW `vn`.`agencyTerm` AS
SELECT
    `sat`.`agencyFk` AS `agencyFk`,
    `sat`.`minimumPackages` AS `minimumPackages`,
    `sat`.`kmPrice` AS `kmPrice`,
    `sat`.`packagePrice` AS `packagePrice`,
    `sat`.`routePrice` AS `routePrice`,
    `sat`.`minimumKm` AS `minimumKm`,
    `sat`.`minimumM3` AS `minimumM3`,
    `sat`.`m3Price` AS `m3Price`
FROM
    `vn`.`supplierAgencyTerm` `sat`;

ALTER TABLE `vn`.`agency` DROP FOREIGN KEY `agency_ibfk_4`;
ALTER TABLE `vn`.`agency` CHANGE `supplierFk` `supplierFk__` int(11) DEFAULT NULL NULL;

CREATE OR REPLACE
ALGORITHM = UNDEFINED 
DEFINER=`root`@`localhost`  
VIEW `vn2008`.`agency` AS
SELECT
    `a`.`id` AS `agency_id`,
    `a`.`name` AS `name`,
    `a`.`warehouseFk` AS `warehouse_id`,
    `a`.`isVolumetric` AS `por_volumen`,
    `a`.`bankFk` AS `Id_Banco`,
    `a`.`warehouseAliasFk` AS `warehouse_alias_id`,
    `a`.`isOwn` AS `propios`,
    `a`.`labelZone` AS `zone_label`,
    `a`.`workCenterFk` AS `workCenterFk`,
    `a`.`supplierFk__` AS `supplierFk__`
FROM
    `vn`.`agency` `a`;