feat: invoicing by (clientFk, addressFk)
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2022-10-11 12:24:06 +02:00
parent 53f78a98d2
commit 0d785695fe
3 changed files with 66 additions and 19 deletions

View File

@ -53,7 +53,6 @@ module.exports = Self => {
myOptions.transaction = tx;
}
const clientToInvoideIds = [];
let query;
try {
query = `
@ -97,14 +96,21 @@ module.exports = Self => {
if (!invoiceableClients.length) return;
const clientToInvoiceIds = invoiceableClients.map(invoiceableClient => invoiceableClient.id);
const dataArr = new Set(clientToInvoiceIds);
const clientNotRepeatedIds = [...dataArr];
const clientAndAddress = invoiceableClients.map(
invoiceableClient => [invoiceableClient.id, invoiceableClient.addressFk]
);
if (tx) await tx.commit();
console.log(invoiceableClients, clientToInvoiceIds, clientNotRepeatedIds);
return clientNotRepeatedIds;
return {
clientAndAddressIds: clientAndAddress,
invoiceDate: args.invoiceDate,
maxShipped: args.maxShipped,
fromClientId: args.fromClientId,
toClientId: args.toClientId,
companyFk: args.companyFk,
minShipped: minShipped
};
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -7,6 +7,43 @@ module.exports = Self => {
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'
},
{
arg: 'maxShipped',
type: 'date',
description: 'The maximum shipped date'
},
{
arg: 'fromClientId',
type: 'number',
description: 'The minimum client id'
},
{
arg: 'toClientId',
type: 'number',
description: 'The maximum client id',
required: false
},
{
arg: 'companyFk',
type: 'number',
description: 'The company id to invoice'
},
{
arg: 'minShipped',
type: 'date',
description: 'The company id to invoice'
}],
returns: {
type: 'object',
@ -18,7 +55,8 @@ module.exports = Self => {
}
});
Self.globalInvoicing = async(ctx, clientId, options) => {
Self.globalInvoicing = async(ctx, options) => {
const args = ctx.args;
const models = Self.app.models;
const myOptions = {};
let tx;
@ -34,15 +72,16 @@ module.exports = Self => {
const invoicesIds = [];
const failedClients = [];
try {
const client = await models.Client.findById(clientId, {
fields: ['id', 'hasToInvoiceByAddress', 'addressFk']
// const addresses = await models.Address.find({where: {clientFk: args.clientId}}, myOptions);
const client = await models.Client.findById(args.clientId, {
fields: ['id', 'hasToInvoiceByAddress']
}, myOptions);
try {
if (client.hasToInvoiceByAddress) {
await Self.rawSql('CALL ticketToInvoiceByAddress(?, ?, ?, ?)', [
minShipped,
args.minShipped,
args.maxShipped,
client.addressFk,
args.addressId,
args.companyFk
], myOptions);
} else {
@ -59,7 +98,7 @@ module.exports = Self => {
// Validates ticket nagative base
const hasAnyNegativeBase = await getNegativeBase(myOptions);
if (hasAnyNegativeBase && isSpanishCompany)
return notifyFailures(ctx, failedClients, myOptions); // continue
return tx.rollback(); // continue
query = `SELECT invoiceSerial(?, ?, ?) AS serial`;
const [invoiceSerial] = await Self.rawSql(query, [
@ -89,9 +128,8 @@ module.exports = Self => {
id: client.id,
stacktrace: e
});
return notifyFailures(ctx, failedClients, myOptions); // continue
await notifyFailures(ctx, failedClients, myOptions); // continue
}
// }
if (tx) await tx.commit();
} catch (e) {

View File

@ -59,12 +59,15 @@ class Controller extends Dialog {
return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice)
.then(async res => {
this.lastClientId = res.data[res.data.length - 1];
const clientAndAddressIds = res.data.clientAndAddressIds;
if (!clientAndAddressIds) return super.responseHandler(response);
this.lastClientId = clientAndAddressIds[clientAndAddressIds.length - 1][0];
this.isInvoicing = true;
for (let clientId of res.data) {
this.currentClientId = clientId;
const params = {clientId: clientId};
await this.$http.post(`InvoiceOuts/globalInvoicing`, params);
for (let clientAndAddresId of clientAndAddressIds) {
this.currentClientId = clientAndAddresId[0];
res.data.clientId = clientAndAddresId[0];
res.data.addressId = clientAndAddresId[1];
await this.$http.post(`InvoiceOuts/globalInvoicing`, res.data);
}
})
.then(() => super.responseHandler(response))