7984-addCurrency #3316

Open
carlosap wants to merge 14 commits from 7984-addCurrency into dev
4 changed files with 53 additions and 15 deletions
Showing only changes of commit 6c2b57060c - Show all commits

View File

@ -24,7 +24,9 @@ ALTER TABLE vn.ticket ADD CONSTRAINT ticket_currency_FK FOREIGN KEY (currencyFk)
REFERENCES vn.currency(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; 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 `foreignPrice` decimal(10,2) NULL AFTER price;
ALTER TABLE vn.sale ADD `foreignTotal` decimal(10,2) NULL AFTER total; ALTER TABLE vn.sale
ADD COLUMN foreignTotal DECIMAL(10,2)
AS (foreignPrice * quantity * (100 - discount) / 100) VIRTUAL AFTER total;
ALTER TABLE vn.ticketService ADD `foreignPrice` decimal(10,2) NULL AFTER price; ALTER TABLE vn.ticketService ADD `foreignPrice` decimal(10,2) NULL AFTER price;

View File

@ -14,6 +14,10 @@ module.exports = Self => {
description: 'The new price', description: 'The new price',
type: 'number', type: 'number',
required: true required: true
}, {
arg: 'isForeign',
type: 'boolean',
description: 'If should be calculated with the foreign price'
} }
], ],
returns: { returns: {
@ -26,7 +30,7 @@ module.exports = Self => {
} }
}); });
Self.updatePrice = async(ctx, id, newPrice, options) => { Self.updatePrice = async(ctx, id, newPrice, isForeign, options) => {
const $t = ctx.req.__; // $translate const $t = ctx.req.__; // $translate
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
@ -57,15 +61,28 @@ module.exports = Self => {
} }
} }
}, },
fields: ['id', 'clientFk'] fields: ['id', 'clientFk', 'currencyFk']
} }
} }
}; };
const sale = await models.Sale.findById(id, filter, myOptions); const sale = await models.Sale.findById(id, filter, myOptions);
await models.Sale.canEdit(ctx, [id], myOptions); await models.Sale.canEdit(ctx, [id], myOptions);
let newForeignPrice;
const isForeignCurrency = sale.foreignPrice || sale.foreignPrice === 0;
if (isForeignCurrency) {
const [{rate}] = await Self.rawSql(
'SELECT ticket_getCurrencyRate(?, NULL) rate',
[sale.ticketFk],
myOptions
);
if (isForeign) {
newForeignPrice = newPrice;
newPrice = newPrice / rate;
} else
newForeignPrice = newPrice * rate;
}
const oldPrice = sale.price; const oldPrice = sale.price;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
@ -104,6 +121,7 @@ module.exports = Self => {
await sale.updateAttributes({ await sale.updateAttributes({
price: newPrice, price: newPrice,
foreignPrice: newForeignPrice,
priceFixed: priceFixed.value priceFixed: priceFixed.value
}, myOptions); }, myOptions);
@ -120,6 +138,7 @@ module.exports = Self => {
quantity: sale.quantity, quantity: sale.quantity,
oldPrice: oldPrice, oldPrice: oldPrice,
newPrice: newPrice, newPrice: newPrice,
foreignPrice: newForeignPrice,
ticketUrl: `${url}ticket/${sale.ticket().id}/sale`, ticketUrl: `${url}ticket/${sale.ticket().id}/sale`,
itemUrl: `${url}item/${sale.itemFk}/summary` itemUrl: `${url}item/${sale.itemFk}/summary`
}); });
@ -128,7 +147,7 @@ module.exports = Self => {
if (tx) await tx.commit(); if (tx) await tx.commit();
return sale; return await models.Sale.findById(id, filter); // for virutal fields
} catch (error) { } catch (error) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw error; throw error;

View File

@ -2,13 +2,19 @@ module.exports = Self => {
Self.remoteMethod('getSalesPersonMana', { Self.remoteMethod('getSalesPersonMana', {
description: 'Returns the mana amount of a salesperson for a ticket', description: 'Returns the mana amount of a salesperson for a ticket',
accessType: 'READ', accessType: 'READ',
accepts: [{ accepts: [
{
arg: 'id', arg: 'id',
type: 'number', type: 'number',
required: true, required: true,
description: 'The ticket id', description: 'The ticket id',
http: {source: 'path'} http: {source: 'path'}
}], }, {
arg: 'isForeign',
type: 'boolean',
description: 'If need show with foreign currency',
},
],
returns: { returns: {
root: true root: true
}, },
@ -18,7 +24,7 @@ module.exports = Self => {
} }
}); });
Self.getSalesPersonMana = async(ticketId, options) => { Self.getSalesPersonMana = async(ticketId, isForeign, options) => {
const myOptions = {}; const myOptions = {};
const models = Self.app.models; const models = Self.app.models;
@ -37,13 +43,18 @@ module.exports = Self => {
if (!ticket) return 0; if (!ticket) return 0;
const mana = await models.WorkerMana.findOne({ let {amount} = await models.WorkerMana.findOne({
where: { where: {
workerFk: ticket.client().salesPersonFk workerFk: ticket.client().salesPersonFk
}, },
fields: 'amount' fields: 'amount'
}, myOptions); }, myOptions);
return mana ? mana.amount : 0; if (isForeign) {
const [{rate}] = await models.Ticket.rawSql('SELECT ticket_getCurrencyRate(?, NULL) rate', [ticketId]);
amount = amount * rate;
}
return amount ?? 0;
}; };
}; };

View File

@ -45,6 +45,12 @@
}, },
"originalQuantity":{ "originalQuantity":{
"type": "number" "type": "number"
},
"total":{
"type": "number"
},
"foreignTotal":{
"type": "number"
} }
}, },
"relations": { "relations": {