feat: refs #7984 add currency in order sections
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2025-01-15 14:59:17 +01:00
parent 479d81d046
commit 4e1ab2652c
19 changed files with 184 additions and 38 deletions

View File

@ -4048,7 +4048,8 @@ INSERT INTO vn.workerIrpf (workerFk,spouseNif, geographicMobilityDate)
INSERT INTO vn.referenceRate (currencyFk, dated, value) INSERT INTO vn.referenceRate (currencyFk, dated, value)
VALUES (2, '2000-12-01', 1.0495), VALUES (2, '2000-12-01', 1.0495),
(2, '2001-01-01', 1.0531), (2, '2001-01-01', 1.0531),
(2, '2001-02-01', 7.6347); (2, '2001-02-01', 7.6347),
(2, '2000-12-31', 7.6347);
INSERT IGNORE INTO vn.osrmConfig (id,url,tolerance) INSERT IGNORE INTO vn.osrmConfig (id,url,tolerance)
VALUES (1,'https://router.project-osrm.org', 0.002); VALUES (1,'https://router.project-osrm.org', 0.002);

View File

@ -20,7 +20,7 @@ BEGIN
CALL order_getTotal; CALL order_getTotal;
SELECT total INTO vTotal FROM tmp.orderTotal; SELECT IFNULL(foreignTotal, total) INTO vTotal FROM tmp.orderTotal;
DROP TEMPORARY TABLE DROP TEMPORARY TABLE
tmp.`order`, tmp.`order`,

View File

@ -29,7 +29,8 @@ BEGIN
oro.amount * oro.price total, oro.amount * oro.price total,
s.countryFk, s.countryFk,
ata.areaFk, ata.areaFk,
itc.taxClassFk itc.taxClassFk,
o.currencyFk
FROM hedera.orderRow oro FROM hedera.orderRow oro
JOIN tmp.order tor ON tor.orderFk = oro.orderFk JOIN tmp.order tor ON tor.orderFk = oro.orderFk
JOIN hedera.`order` o ON o.id = tor.orderFk JOIN hedera.`order` o ON o.id = tor.orderFk
@ -46,7 +47,7 @@ BEGIN
tc.code, tc.code,
SUM(o.total) taxableBase, SUM(o.total) taxableBase,
pgc.rate, pgc.rate,
currency_getRate(o.currencyFk, NULL) * vn.currency_getRate(o.currencyFk, NULL) *
SUM(o.total) foreignTaxableBase, SUM(o.total) foreignTaxableBase,
o.currencyFk o.currencyFk
FROM orders o FROM orders o
@ -70,7 +71,7 @@ BEGIN
SUM(CAST(taxableBase * rate / 100 AS DECIMAL(10, 2))) tax, SUM(CAST(taxableBase * rate / 100 AS DECIMAL(10, 2))) tax,
currencyFk, currencyFk,
foreignTaxableBase, foreignTaxableBase,
currency_getRate(o.currencyFk, NULL) * vn.currency_getRate(currencyFk, NULL) *
SUM(CAST(taxableBase * rate / 100 AS DECIMAL(10, 2))) foreignTax SUM(CAST(taxableBase * rate / 100 AS DECIMAL(10, 2))) foreignTax
FROM tmp.orderTax FROM tmp.orderTax
GROUP BY orderFk, `code`; GROUP BY orderFk, `code`;

View File

@ -37,7 +37,7 @@ proc: BEGIN
AND p.rate = r.rate AND p.rate = r.rate
LEFT JOIN tmp.zoneGetShipped t ON t.warehouseFk = r.warehouseFk LEFT JOIN tmp.zoneGetShipped t ON t.warehouseFk = r.warehouseFk
SET r.price = p.price, SET r.price = p.price,
r.foreignPrice = currency_getRate(o.currencyFk, NULL) * p.price, r.foreignPrice = vn.currency_getRate(o.currencyFk, NULL) * p.price,
r.amount = IF(p.itemFk IS NOT NULL, r.amount = IF(p.itemFk IS NOT NULL,
r.amount + IF(@m := MOD(r.amount, p.`grouping`), p.`grouping` - @m, 0) r.amount + IF(@m := MOD(r.amount, p.`grouping`), p.`grouping` - @m, 0)
, 0), , 0),

View File

@ -0,0 +1,23 @@
-- DELIMITER $$
-- CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION vn.client_getCompany(vSelf INT)
-- RETURNS INT(3)
-- DETERMINISTIC
-- BEGIN
-- /**
-- * Devuelve la moneda por defecto del cliente dado
-- *
-- * @param vSelf id del cliente
-- * @return devuelve id de la compañia por defecto
-- */
-- DECLARE vCompanyFk INT;
-- SELECT co.currencyFk INTO vCompanyFk
-- FROM client c
-- JOIN vn.province p ON p.id = c.provinceFk
-- JOIN vn.country co ON co.id = p.countryFk
-- WHERE c.id = vSelf
-- LIMIT 1;
-- RETURN vCompanyFk;
-- END$$
-- DELIMITER ;

View File

@ -0,0 +1,23 @@
-- DELIMITER $$
-- CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION vn.client_getCurrency(vSelf INT)
-- RETURNS INT(3)
-- DETERMINISTIC
-- BEGIN
-- /**
-- * Devuelve la moneda por defecto del cliente dado
-- *
-- * @param vSelf id del cliente
-- * @return devuelve id del tipo de moneda
-- */
-- DECLARE vCurrencyFk INT;
-- SELECT co.currencyFk INTO vCurrencyFk
-- FROM client c
-- JOIN vn.province p ON p.id = c.provinceFk
-- JOIN vn.country co ON co.id = p.countryFk
-- WHERE c.id = vSelf
-- LIMIT 1;
-- RETURN vCurrencyFk;
-- END$$
-- DELIMITER ;

View File

@ -20,7 +20,7 @@ BEGIN
SELECT value INTO vForeignRate SELECT value INTO vForeignRate
FROM referenceRate FROM referenceRate
WHERE dated = IFNULL(vDated, util.yesterday()) WHERE dated = IFNULL(vDated, util.yesterday())
AND id = vSelf AND currencyFk = vSelf
ORDER BY dated DESC ORDER BY dated DESC
LIMIT 1; LIMIT 1;

View File

@ -3,6 +3,8 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`orderCreate`(
vLanded DATE, vLanded DATE,
vAgencyMode INT, vAgencyMode INT,
vAddress INT, vAddress INT,
vCompany INT,
vCurrency INT,
vSourceApp VARCHAR(10), vSourceApp VARCHAR(10),
OUT vOrderId INT) OUT vOrderId INT)
BEGIN BEGIN
@ -18,8 +20,8 @@ BEGIN
FROM vn.address FROM vn.address
WHERE id = vAddress; WHERE id = vAddress;
INSERT INTO hedera.order(date_send,customer_id,delivery_method_id,agency_id,address_id,source_app) INSERT INTO hedera.order(date_send,customer_id,delivery_method_id,agency_id,address_id,company_id,currencyFk,source_app)
VALUES( vLanded,vClient ,vDeliveryMethod,vAgencyMode ,vAddress ,vSourceApp); VALUES( vLanded,vClient ,vDeliveryMethod,vAgencyMode,vAddress,vCompany,vCurrency,vSourceApp);
SET vOrderId = LAST_INSERT_ID(); SET vOrderId = LAST_INSERT_ID();

View File

@ -3,11 +3,13 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`orderListCreate`(
vLanded DATE, vLanded DATE,
vAgencyMode INT, vAgencyMode INT,
vAddress INT, vAddress INT,
vCompany INT,
vCurrency INT,
vSourceApp VARCHAR(10)) vSourceApp VARCHAR(10))
BEGIN BEGIN
DECLARE vOrderId INT; DECLARE vOrderId INT;
CALL vn.orderCreate(vLanded,vAgencyMode,vAddress,vSourceApp,vOrderId); CALL vn.orderCreate(vLanded,vAgencyMode,vAddress,vCompany,vCurrency,vSourceApp,vOrderId);
SELECT vOrderId; SELECT vOrderId;
END$$ END$$

View File

@ -52,3 +52,31 @@ 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;

View File

@ -147,8 +147,13 @@
}, },
"hasDailyInvoice": { "hasDailyInvoice": {
"type": "boolean" "type": "boolean"
},
"defaultCompanyFk": {
"type": "number"
},
"defaultCurrencyFk": {
"type": "number"
} }
}, },
"relations": { "relations": {
"account": { "account": {
@ -256,6 +261,16 @@
"model": "Supplier", "model": "Supplier",
"foreignKey": "fi", "foreignKey": "fi",
"primaryKey": "nif" "primaryKey": "nif"
},
"currency": {
"type": "belongsTo",
"model": "Currency",
"foreignKey": "defaultCurrencyFk"
},
"company": {
"type": "belongsTo",
"model": "Comapny",
"foreignKey": "defaultCompanyFk"
} }
}, },
"scopes": { "scopes": {

View File

@ -92,11 +92,12 @@ module.exports = Self => {
// Calculate items // Calculate items
const order = await Self.findById(orderFk, null, myOptions); const order = await Self.findById(orderFk, null, myOptions);
stmts.push(new ParameterizedSQL( stmts.push(new ParameterizedSQL(
'CALL vn.catalog_calculate(?, ?, ?, ?)', [ 'CALL vn.catalog_calculate(?, ?, ?, ?, ?)', [
order.landed, order.landed,
order.address_id, order.address_id,
order.agency_id, order.agency_id,
false order.currencyFk,
false,
] ]
)); ));
@ -119,7 +120,9 @@ module.exports = Self => {
w.firstName, w.firstName,
tci.priceKg, tci.priceKg,
ink.hex, ink.hex,
tci.minQuantity tci.minQuantity,
tci.foreignPrice,
tci.foreignPriceKg
FROM tmp.ticketCalculateItem tci FROM tmp.ticketCalculateItem tci
JOIN vn.item i ON i.id = tci.itemFk JOIN vn.item i ON i.id = tci.itemFk
JOIN vn.itemType it ON it.id = i.typeFk JOIN vn.itemType it ON it.id = i.typeFk
@ -161,9 +164,11 @@ module.exports = Self => {
`SELECT tcp.itemFk, `SELECT tcp.itemFk,
tcp.grouping, tcp.grouping,
tcp.price, tcp.price,
tcp.foreignPrice,
tcp.rate, tcp.rate,
tcp.warehouseFk, tcp.warehouseFk,
tcp.priceKg, tcp.priceKg,
tcp.foreignPriceKg,
w.name warehouse w.name warehouse
FROM tmp.ticketComponentPrice tcp FROM tmp.ticketComponentPrice tcp
JOIN vn.warehouse w ON w.id = tcp.warehouseFk`) - 1; JOIN vn.warehouse w ON w.id = tcp.warehouseFk`) - 1;

View File

@ -62,11 +62,12 @@ module.exports = Self => {
stmts.push(stmt); stmts.push(stmt);
stmt = new ParameterizedSQL( stmt = new ParameterizedSQL(
'CALL vn.catalog_calculate(?, ?, ?, ?)', [ 'CALL vn.catalog_calculate(?, ?, ?, ?, ?)', [
order.landed, order.landed,
order.addressFk, order.addressFk,
order.agencyModeFk, order.agencyModeFk,
false order.currencyFk,
false,
] ]
); );
stmts.push(stmt); stmts.push(stmt);

View File

@ -10,7 +10,7 @@ module.exports = Self => {
http: {source: 'path'} http: {source: 'path'}
}], }],
returns: { returns: {
type: 'number', type: 'object',
root: true root: true
}, },
http: { http: {
@ -25,9 +25,22 @@ module.exports = Self => {
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const query = `SELECT hedera.order_getTotal(?) total;`; const query = `
const [total] = await Self.rawSql(query, [orderFk], myOptions); DROP TEMPORARY TABLE IF EXISTS tmp.order;
CREATE TEMPORARY TABLE tmp.order
ENGINE = MEMORY
SELECT ? orderFk;
return total.total; CALL hedera.order_getTotal;
SELECT total, foreignTotal FROM tmp.orderTotal;
DROP TEMPORARY TABLE
tmp.order,
tmp.orderTotal;`;
const totals = await Self.rawSql(query, [orderFk], myOptions);
const INDEX_QUERY = 3;
return totals[INDEX_QUERY][0];
}; };
}; };

View File

@ -10,7 +10,7 @@ module.exports = Self => {
http: {source: 'path'} http: {source: 'path'}
}], }],
returns: { returns: {
type: 'number', type: 'object',
root: true root: true
}, },
http: { http: {
@ -25,12 +25,19 @@ module.exports = Self => {
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
let totalTax = 0.00; let vat = 0.00;
let foreignVat = 0.00;
const taxes = await Self.app.models.Order.getTaxes(orderId, myOptions); const taxes = await Self.app.models.Order.getTaxes(orderId, myOptions);
taxes.forEach(tax => { taxes.forEach(tax => {
totalTax += tax.tax; vat += tax.tax;
foreignVat += tax.foreignTax;
}); });
return Math.round(totalTax * 100) / 100; return {vat: round(vat), foreignVat: round(foreignVat)};
}; };
function round(amount) {
return Math.round(amount * 100) / 100;
}
}; };

View File

@ -20,6 +20,16 @@ module.exports = Self => {
description: 'The agencyMode for the order', description: 'The agencyMode for the order',
type: 'number', type: 'number',
required: true required: true
}, {
arg: 'companyId',
description: 'The company for the order',
type: 'number',
required: true
}, {
arg: 'currencyId',
description: 'The currency for the order',
type: 'number',
required: true
} }
], ],
returns: { returns: {
@ -32,7 +42,7 @@ module.exports = Self => {
} }
}); });
Self.new = async(ctx, landed, addressId, agencyModeId, options) => { Self.new = async(ctx, landed, addressId, agencyModeId, companyId, currencyId, options) => {
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
let tx; let tx;
@ -59,11 +69,13 @@ module.exports = Self => {
throw new UserError(`You can't create an order for an inactive client`); throw new UserError(`You can't create an order for an inactive client`);
} }
query = `CALL vn.orderListCreate(?, ?, ?, ?);`; query = `CALL vn.orderListCreate(?, ?, ?, ?, ?, ?);`;
[result] = await Self.rawSql(query, [ [result] = await Self.rawSql(query, [
landed, landed,
agencyModeId, agencyModeId,
addressId, addressId,
companyId,
currencyId,
'SALIX' 'SALIX'
], myOptions); ], myOptions);

View File

@ -57,6 +57,8 @@ module.exports = Self => {
'landed', 'landed',
'agencyModeFk', 'agencyModeFk',
'note', 'note',
'companyFk',
'currencyFk',
]); ]);
if (Object.keys(updateParams).length) if (Object.keys(updateParams).length)
await order.updateAttributes(updateParams, myOptions); await order.updateAttributes(updateParams, myOptions);

View File

@ -30,6 +30,9 @@
"price": { "price": {
"type": "number" "type": "number"
}, },
"foreignPrice": {
"type": "number"
},
"rate": { "rate": {
"type": "number" "type": "number"
}, },

View File

@ -82,6 +82,9 @@
}, },
"total": { "total": {
"type": "number" "type": "number"
},
"currencyFk": {
"type": "number"
} }
}, },
"relations": { "relations": {
@ -114,6 +117,11 @@
"type": "belongsTo", "type": "belongsTo",
"model": "Company", "model": "Company",
"foreignKey": "company_id" "foreignKey": "company_id"
},
"currency": {
"type": "belongsTo",
"model": "Currency",
"foreignKey": "currencyFk"
} }
} }
} }