185 lines
6.1 KiB
JavaScript
185 lines
6.1 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('createManualInvoice', {
|
|
description: 'Make a manual invoice',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'clientFk',
|
|
type: 'any',
|
|
description: 'The invoiceable client id'
|
|
},
|
|
{
|
|
arg: 'ticketFk',
|
|
type: 'any',
|
|
description: 'The invoiceable ticket id'
|
|
},
|
|
{
|
|
arg: 'maxShipped',
|
|
type: 'date',
|
|
description: 'The maximum shipped date'
|
|
},
|
|
{
|
|
arg: 'serial',
|
|
type: 'string',
|
|
description: 'The invoice serial'
|
|
},
|
|
{
|
|
arg: 'taxArea',
|
|
type: 'string',
|
|
description: 'The invoice tax area'
|
|
},
|
|
{
|
|
arg: 'reference',
|
|
type: 'string',
|
|
description: 'The invoice reference'
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: '/createManualInvoice',
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.createManualInvoice = async(ctx, options) => {
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
const ticketId = args.ticketFk;
|
|
let clientId = args.clientFk;
|
|
let maxShipped = args.maxShipped;
|
|
let companyId;
|
|
let newInvoice;
|
|
let query;
|
|
try {
|
|
if (ticketId) {
|
|
const ticket = await models.Ticket.findById(ticketId, null, myOptions);
|
|
const company = await models.Company.findById(ticket.companyFk, null, myOptions);
|
|
|
|
clientId = ticket.clientFk;
|
|
maxShipped = ticket.shipped;
|
|
companyId = ticket.companyFk;
|
|
|
|
// Validates invoiced ticket
|
|
if (ticket.refFk)
|
|
throw new UserError('This ticket is already invoiced');
|
|
|
|
// Validates ticket amount
|
|
if (ticket.totalWithVat == 0)
|
|
throw new UserError(`A ticket with an amount of zero can't be invoiced`);
|
|
|
|
// Validates ticket nagative base
|
|
const hasNegativeBase = await getNegativeBase(ticketId, myOptions);
|
|
if (hasNegativeBase && company.code == 'VNL')
|
|
throw new UserError(`A ticket with a negative base can't be invoiced`);
|
|
} else {
|
|
if (!maxShipped)
|
|
throw new UserError(`Max shipped required`);
|
|
|
|
const company = await models.Ticket.findOne({
|
|
fields: ['companyFk'],
|
|
where: {
|
|
clientFk: clientId,
|
|
shipped: {lte: maxShipped}
|
|
}
|
|
}, myOptions);
|
|
companyId = company.companyFk;
|
|
}
|
|
|
|
// Validate invoiceable client
|
|
const isClientInvoiceable = await isInvoiceable(clientId, myOptions);
|
|
if (!isClientInvoiceable)
|
|
throw new UserError(`This client is not invoiceable`);
|
|
|
|
// Can't invoice tickets into future
|
|
const tomorrow = new Date();
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
if (maxShipped >= tomorrow)
|
|
throw new UserError(`Can't invoice to future`);
|
|
|
|
const maxInvoiceDate = await getMaxIssued(args.serial, companyId, myOptions);
|
|
if (new Date() < maxInvoiceDate)
|
|
throw new UserError(`Can't invoice to past`);
|
|
|
|
if (ticketId) {
|
|
query = `CALL invoiceOut_newFromTicket(?, ?, ?, ?, @newInvoiceId)`;
|
|
await Self.rawSql(query, [
|
|
ticketId,
|
|
args.serial,
|
|
args.taxArea,
|
|
args.reference
|
|
], myOptions);
|
|
} else {
|
|
query = `CALL invoiceOut_newFromClient(?, ?, ?, ?, ?, ?, @newInvoiceId)`;
|
|
await Self.rawSql(query, [
|
|
clientId,
|
|
args.serial,
|
|
maxShipped,
|
|
companyId,
|
|
args.taxArea,
|
|
args.reference
|
|
], myOptions);
|
|
}
|
|
|
|
[newInvoice] = await Self.rawSql(`SELECT @newInvoiceId id`, null, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
|
|
if (newInvoice.id)
|
|
await Self.createPdf(ctx, newInvoice.id);
|
|
|
|
return newInvoice;
|
|
};
|
|
|
|
async function isInvoiceable(clientId, options) {
|
|
const models = Self.app.models;
|
|
const query = `SELECT (hasToInvoice AND isTaxDataChecked) AS invoiceable
|
|
FROM client
|
|
WHERE id = ?`;
|
|
const [result] = await models.InvoiceOut.rawSql(query, [clientId], options);
|
|
|
|
return result.invoiceable;
|
|
}
|
|
|
|
async function getNegativeBase(ticketId, options) {
|
|
const models = Self.app.models;
|
|
const query = 'SELECT vn.hasSomeNegativeBase(?) AS base';
|
|
const [result] = await models.InvoiceOut.rawSql(query, [ticketId], options);
|
|
|
|
return result.base;
|
|
}
|
|
|
|
async function getMaxIssued(serial, companyId, options) {
|
|
const models = Self.app.models;
|
|
const query = `SELECT MAX(issued) AS issued
|
|
FROM invoiceOut
|
|
WHERE serial = ? AND companyFk = ?`;
|
|
const [maxIssued] = await models.InvoiceOut.rawSql(query,
|
|
[serial, companyId], options);
|
|
const maxInvoiceDate = maxIssued && maxIssued.issued || new Date();
|
|
|
|
return maxInvoiceDate;
|
|
}
|
|
};
|