ALTER TABLE hedera.order ADD `currencyFk`   tinyint(3) UNSIGNED  DEFAULT 1 NOT NULL AFTER total;
ALTER TABLE hedera.order ADD `foreignTaxableBase` decimal(10,2) NULL AFTER currencyFk;
ALTER TABLE hedera.order ADD `foreignTax` decimal(10,2) NULL AFTER foreignTaxableBase;
ALTER TABLE hedera.order ADD `foreignTotal` decimal(10,2) NULL AFTER foreignTax;


ALTER TABLE hedera.order DROP FOREIGN KEY IF EXISTS order_currency_FK;
ALTER TABLE hedera.order ADD CONSTRAINT order_currency_FK FOREIGN KEY (currencyFk)
	REFERENCES vn.currency(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE hedera.orderRow ADD `foreignPrice` decimal(10,2) NULL AFTER price;

ALTER TABLE vn.company ADD `currencyFk`  tinyint(3) UNSIGNED  DEFAULT 1 NOT NULL;
ALTER TABLE vn.company DROP FOREIGN KEY IF EXISTS company_currency_FK;
ALTER TABLE vn.company ADD CONSTRAINT company_currency_FK FOREIGN KEY (currencyFk)
	REFERENCES vn.currency(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE vn.ticket ADD `currencyFk`  tinyint(3) UNSIGNED DEFAULT 1 NOT NULL AFTER totalWithoutVat;
ALTER TABLE vn.ticket ADD `foreignTotalWithVat` decimal(10,2) NULL AFTER currencyFk;
ALTER TABLE vn.ticket ADD `foreignTotalWithoutVat` decimal(10,2) NULL AFTER foreignTotalWithVat;

ALTER TABLE vn.ticket DROP FOREIGN KEY IF EXISTS ticket_currency_FK;
ALTER TABLE vn.ticket ADD CONSTRAINT ticket_currency_FK FOREIGN KEY (currencyFk)
	REFERENCES vn.currency(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE vn.sale ADD `foreignPrice` decimal(10,2) NULL AFTER price;
ALTER TABLE vn.sale ADD `foreignTotal` decimal(10,2) NULL AFTER total;

ALTER TABLE vn.ticketService ADD `foreignPrice` decimal(10,2) NULL AFTER price;

ALTER TABLE vn.invoiceOut ADD `foreignAmount` decimal(10,2) NULL AFTER amount;
ALTER TABLE vn.invoiceOutExpense ADD `foreignAmount` DECIMAL(10,2) DEFAULT NULL AFTER amount;
ALTER TABLE vn.invoiceOutTax ADD `foreignTaxableBase` DECIMAL(10,2) DEFAULT NULL AFTER vat;
ALTER TABLE vn.invoiceOutTax ADD `foreignVat` DECIMAL(10,2) DEFAULT NULL AFTER foreignTaxableBase;


CREATE OR REPLACE DEFINER=`vn`@`localhost`
	SQL SECURITY DEFINER
	VIEW `hedera`.`order_row`
AS SELECT `t`.`id` AS `id`,
	`t`.`orderFk` AS `order_id`,
	`t`.`itemFk` AS `item_id`,
	`t`.`warehouseFk` AS `warehouse_id`,
	`t`.`shipment` AS `shipment`,
	`t`.`amount` AS `amount`,
	`t`.`price` AS `price`,
	`t`.`foreignPrice` AS `foreignPrice`,
	`t`.`rate` AS `rate`,
	`t`.`created` AS `created`,
	`t`.`saleFk` AS `Id_Movimiento`
FROM `hedera`.`orderRow` `t`;



ALTER TABLE vn.country ADD IF NOT EXISTS `companyFk` int(10) UNSIGNED DEFAULT 442 NOT NULL;
ALTER TABLE vn.country DROP FOREIGN KEY IF EXISTS country_defaultCompany_FK;
ALTER TABLE vn.country ADD CONSTRAINT country_defaultCompany_FK FOREIGN KEY (companyFk)
	REFERENCES vn.company(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE vn.client ADD IF NOT EXISTS `defaultCompanyFk` int(10) UNSIGNED DEFAULT 442 NOT NULL;
ALTER TABLE vn.client ADD IF NOT EXISTS `defaultCurrencyFk` tinyint(3) UNSIGNED DEFAULT 1 NOT NULL;


ALTER TABLE vn.client DROP FOREIGN KEY IF EXISTS client_defaultCompany_FK;
ALTER TABLE vn.client DROP FOREIGN KEY IF EXISTS client_defaultCurrency_FK;

ALTER TABLE vn.client ADD CONSTRAINT client_defaultCompany_FK FOREIGN KEY (defaultCompanyFk)
	REFERENCES vn.company(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE vn.client ADD CONSTRAINT client_defaultCurrency_FK FOREIGN KEY (defaultCurrencyFk)
	REFERENCES vn.currency(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

UPDATE vn.client c
		JOIN vn.province p ON p.id = c.provinceFk
		JOIN vn.country co ON co.id = p.countryFk
	SET c.defaultCompanyFk = co.companyFk,
		c.defaultCurrencyFk = co.currencyFk;





