102 lines
3.0 KiB
JavaScript
102 lines
3.0 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('invoiceClient', {
|
|
description: 'Make a invoice of a client',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'clientId',
|
|
type: 'number',
|
|
description: 'The client id to invoice',
|
|
required: true
|
|
}, {
|
|
arg: 'addressId',
|
|
type: 'number',
|
|
description: 'The address id to invoice',
|
|
required: true
|
|
}, {
|
|
arg: 'invoiceDate',
|
|
type: 'date',
|
|
description: 'The invoice date',
|
|
required: true
|
|
}, {
|
|
arg: 'maxShipped',
|
|
type: 'date',
|
|
description: 'The maximum shipped date',
|
|
required: true
|
|
}, {
|
|
arg: 'companyFk',
|
|
type: 'number',
|
|
description: 'The company id to invoice',
|
|
required: true
|
|
}, {
|
|
arg: 'serialType',
|
|
type: 'string',
|
|
description: 'Invoice serial number type (see vn.invoiceOutSerial.type enum)',
|
|
required: true
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'number',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: '/invoiceClient',
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
Self.invoiceClient = async(ctx, options) => {
|
|
const args = ctx.args;
|
|
const models = Self.app.models;
|
|
let tx;
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
const minShipped = Date.vnNew();
|
|
minShipped.setFullYear(args.maxShipped.getFullYear() - 1);
|
|
|
|
try {
|
|
const client = await models.Client.findById(args.clientId, {
|
|
fields: ['id', 'hasToInvoiceByAddress']
|
|
}, myOptions);
|
|
|
|
if (client.hasToInvoiceByAddress) {
|
|
await Self.rawSql('CALL ticketToInvoiceByAddress(?, ?, ?, ?)', [
|
|
minShipped,
|
|
args.maxShipped,
|
|
args.addressId,
|
|
args.companyFk
|
|
], myOptions);
|
|
} else {
|
|
await Self.rawSql('CALL invoiceFromClient(?, ?, ?)', [
|
|
args.maxShipped,
|
|
client.id,
|
|
args.companyFk
|
|
], myOptions);
|
|
}
|
|
|
|
const invoiceId = await models.Ticket.makeInvoice(
|
|
ctx,
|
|
args.serialType,
|
|
args.companyFk,
|
|
args.invoiceDate,
|
|
null,
|
|
myOptions
|
|
);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return invoiceId;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|