From 727b170b664cc256a0cc858f015a9c2c5e5ef12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Andr=C3=A9s?= Date: Wed, 27 Nov 2024 18:20:20 +0100 Subject: [PATCH] feat: refs #7984 add currency option --- db/routines/vn/functions/currency_getRate.sql | 29 ++++++++++ .../11307-blackAralia/00-firstScript.sql | 54 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 db/routines/vn/functions/currency_getRate.sql create mode 100644 db/versions/11307-blackAralia/00-firstScript.sql diff --git a/db/routines/vn/functions/currency_getRate.sql b/db/routines/vn/functions/currency_getRate.sql new file mode 100644 index 000000000..d3cbeceaa --- /dev/null +++ b/db/routines/vn/functions/currency_getRate.sql @@ -0,0 +1,29 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`currency_getRate`( + vSelf VARCHAR(3), + vDated DATE +) + RETURNS DECIMAL(10,2) + NOT DETERMINISTIC + READS SQL DATA +BEGIN +/** + * Returns the rate exchange based on the specified currency + * and yesterday's exchange rate. + * + * @param vSelf The currency code + * @param vDated The date of exchangue + * @return vForeignRate The rate e of currency exchange + */ + DECLARE vForeignRate DECIMAL(10,2); + + SELECT value INTO vForeignRate + FROM referenceRate + WHERE dated = IFNULL(vDated, util.yesterday()) + AND id = vSelf + ORDER BY dated DESC + LIMIT 1; + + RETURN vForeignRate; +END$$ +DELIMITER ; diff --git a/db/versions/11307-blackAralia/00-firstScript.sql b/db/versions/11307-blackAralia/00-firstScript.sql new file mode 100644 index 000000000..3b6a20ce6 --- /dev/null +++ b/db/versions/11307-blackAralia/00-firstScript.sql @@ -0,0 +1,54 @@ +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`; + + +