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

View File

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

View File

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