WIP: refs #5914 fix: transferInvoice #1868

Closed
alexm wants to merge 1 commits from 5914-warmFix-transferInvoice into test
11 changed files with 126 additions and 99 deletions
Showing only changes of commit 37f8369849 - Show all commits

View File

@ -24,7 +24,7 @@ describe('InvoiceOut tranferInvoice()', () => {
const options = {transaction: tx};
const args = {
id: '1',
ref: 'T4444444',
refFk: 'T4444444',
newClientFk: 1,
cplusRectificationId: 1,
siiTypeInvoiceOutId: 1,
@ -49,7 +49,7 @@ describe('InvoiceOut tranferInvoice()', () => {
const options = {transaction: tx};
const args = {
id: '1',
ref: 'T1111111',
refFk: 'T1111111',
newClientFk: 1101,
cplusRectificationId: 1,
siiTypeInvoiceOutId: 1,

View File

@ -12,7 +12,7 @@ module.exports = Self => {
description: 'Issued invoice id'
},
{
arg: 'ref',
arg: 'refFk',
type: 'string',
required: true
},
@ -22,17 +22,17 @@ module.exports = Self => {
required: true
},
{
arg: 'cplusRectificationId',
arg: 'cplusRectificationFk',
type: 'number',
required: true
},
{
arg: 'siiTypeInvoiceOutId',
arg: 'siiTypeInvoiceOutFk',
type: 'number',
required: true
},
{
arg: 'invoiceCorrectionTypeId',
arg: 'invoiceCorrectionTypeFk',
type: 'number',
required: true
},
@ -50,14 +50,14 @@ module.exports = Self => {
Self.transferInvoice = async(ctx, options) => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
const args = ctx.args;
const {id, refFk, newClientFk, cplusRectificationFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk} = ctx.args;
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
const {clientFk} = await models.InvoiceOut.findById(args.id);
const {clientFk} = await models.InvoiceOut.findById(id);
if (clientFk == args.newClientFk)
if (clientFk == newClientFk)
throw new UserError(`Select a different client`);
if (!myOptions.transaction) {
@ -65,10 +65,10 @@ module.exports = Self => {
myOptions.transaction = tx;
}
try {
const filterRef = {where: {refFk: args.ref}};
const filterRef = {where: {refFk: refFk}};
const tickets = await models.Ticket.find(filterRef, myOptions);
const ticketsIds = tickets.map(ticket => ticket.id);
await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
const refundTickets = await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
const filterTicket = {where: {ticketFk: {inq: ticketsIds}}};
@ -82,20 +82,16 @@ module.exports = Self => {
const clonedTicketIds = [];
for (const clonedTicket of clonedTickets) {
await clonedTicket.updateAttribute('clientFk', args.newClientFk, myOptions);
await clonedTicket.updateAttribute('clientFk', newClientFk, myOptions);
clonedTicketIds.push(clonedTicket.id);
}
const invoiceIds = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions);
const [invoiceId] = invoiceIds;
const invoiceCorrection =
{correctedFk: id, cplusRectificationFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk};
const refundTicketIds = refundTickets.map(ticket => ticket.id);
await models.InvoiceCorrection.create({
correctingFk: invoiceId,
correctedFk: args.id,
cplusRectificationTypeFk: args.cplusRectificationId,
siiTypeInvoiceOutFk: args.siiTypeInvoiceOutId,
invoiceCorrectionTypeFk: args.invoiceCorrectionTypeId
}, myOptions);
await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions);
const [[invoiceId]] = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions);
if (tx) {
await tx.commit();

View File

@ -16,13 +16,16 @@
"type": "number"
},
"cplusRectificationTypeFk": {
"type": "number"
"type": "number",
"required": true
},
"siiTypeInvoiceOutFk": {
"type": "number"
"type": "number",
"required": true
},
"invoiceCorrectionTypeFk": {
"type": "number"
"type": "number",
"required": true
}
}
}

View File

@ -12,6 +12,9 @@
"type": "number",
"description": "Identifier"
},
"code": {
"type": "string"
},
"description": {
"type": "string"
}

View File

@ -7,7 +7,8 @@
<vn-crud-model
auto-load="true"
url="SiiTypeInvoiceOuts"
data="siiTypeInvoiceOut">
data="siiTypeInvoiceOuts"
where="{code: {like: 'R%'}}">
</vn-crud-model>
<vn-crud-model
auto-load="true"
@ -185,64 +186,69 @@
vn-id="transferInvoice"
title="transferInvoice"
on-accept="$ctrl.transferInvoice()">
<tpl-title translate>
transferInvoice
</tpl-title>
<tpl-body>
<section class="transferInvoice">
<vn-horizontal>
<vn-autocomplete
vn-one
vn-id="client"
required="true"
url="Clients"
label="Client"
show-field="name"
value-field="id"
search-function="{or: [{id: $search}, {name: {like: '%'+ $search +'%'}}]}"
ng-model="$ctrl.invoiceOut.client.id"
initial-data="$ctrl.invoiceOut.client.id"
order="id">
<tpl-item>
#{{id}} - {{::name}}
</tpl-item>
</vn-autocomplete>
<vn-autocomplete
vn-one
vn-id="cplusRectificationType"
required="true"
data="cplusRectificationTypes"
show-field="description"
value-field="id"
ng-model="$ctrl.cplusRectificationType"
search-function="{or: [{id: $search}, {description: {like: '%'+ $search +'%'}}]}"
label="Cplus Type">
<tpl-item>
{{::description}}
</tpl-item>
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete
vn-one
vn-id="cplusInvoiceType"
data="siiTypeInvoiceOut"
show-field="description"
value-field="id"
required="true"
ng-model="$ctrl.siiTypeInvoiceOut"
search-function="{or: [{id: $search}, {description: {like: '%'+ $search +'%'}}]}"
label="Class">
</vn-autocomplete>
<vn-autocomplete
vn-one
vn-id="invoiceCorrectionType"
data="invoiceCorrectionTypes"
ng-model="$ctrl.invoiceCorrectionType"
show-field="description"
value-field="id"
required="true"
label="Type">
</vn-autocomplete>
</vn-horizontal>
</section>
<vn-horizontal>
<vn-autocomplete
vn-one
vn-id="client"
required="true"
url="Clients"
label="Client"
show-field="name"
value-field="id"
search-function="{or: [{id: $search}, {name: {like: '%'+ $search +'%'}}]}"
ng-model="$ctrl.clientId"
order="id">
<tpl-item>
#{{id}} - {{::name}}
</tpl-item>
</vn-autocomplete>
<vn-autocomplete
vn-one
vn-id="cplusRectificationType"
required="true"
data="cplusRectificationTypes"
show-field="description"
value-field="id"
ng-model="$ctrl.cplusRectificationType"
search-function="{or: [{id: $search}, {description: {like: '%'+ $search +'%'}}]}"
label="Rectificative type">
<tpl-item>
{{::description}}
</tpl-item>
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete
vn-one
vn-id="siiTypeInvoiceOut"
data="siiTypeInvoiceOuts"
show-field="code"
value-field="id"
fields="['id','code','description']"
required="true"
ng-model="$ctrl.siiTypeInvoiceOut"
label="Class">
<tpl-item>
{{::code}} - {{::description}}
</tpl-item>
</vn-autocomplete>
<vn-autocomplete
vn-one
vn-id="invoiceCorrectionType"
data="invoiceCorrectionTypes"
ng-model="$ctrl.invoiceCorrectionType"
show-field="description"
value-field="id"
required="true"
label="Type">
</vn-autocomplete>
</vn-horizontal>
</section>
</tpl-body>
<tpl-buttons>
<button response="accept" translate>Transfer client</button>

View File

@ -129,11 +129,11 @@ class Controller extends Section {
transferInvoice() {
const params = {
id: this.invoiceOut.id,
ref: this.invoiceOut.ref,
newClientFk: this.invoiceOut.client.id,
cplusRectificationId: this.cplusRectificationType,
siiTypeInvoiceOutId: this.siiTypeInvoiceOut,
invoiceCorrectionTypeId: this.invoiceCorrectionType
refFk: this.invoiceOut.ref,
newClientFk: this.clientId,
cplusRectificationFk: this.cplusRectificationType,
siiTypeInvoiceOutFk: this.siiTypeInvoiceOut,
invoiceCorrectionTypeFk: this.invoiceCorrectionType
};
this.$http.post(`InvoiceOuts/transferInvoice`, params).then(res => {
const invoiceId = res.data;

View File

@ -22,4 +22,4 @@ The email can't be empty: El correo no puede estar vacío
The following refund tickets have been created: "Se han creado los siguientes tickets de abono: {{ticketIds}}"
Refund...: Abono...
Transfer invoice to...: Transferir factura a...
Cplus Type: Cplus Tipo
Rectificative type: Tipo rectificativa

View File

@ -19,7 +19,7 @@ module.exports = Self => {
}
],
returns: {
type: ['number'],
type: ['object'],
root: true
},
http: {
@ -54,7 +54,7 @@ module.exports = Self => {
if (tx) await tx.commit();
return refundsTicket[0];
return refundsTicket;
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -10,7 +10,13 @@ module.exports = function(Self) {
description: 'The tickets id',
type: ['number'],
required: true
},
{
arg: 'invoiceCorrection',
description: 'The invoice correction',
type: 'object',
}
],
returns: {
type: ['object'],
@ -22,7 +28,7 @@ module.exports = function(Self) {
}
});
Self.invoiceTickets = async(ctx, ticketsIds, options) => {
Self.invoiceTickets = async(ctx, ticketsIds, invoiceCorrection, options) => {
const models = Self.app.models;
const date = Date.vnNew();
date.setHours(0, 0, 0, 0);
@ -68,9 +74,9 @@ module.exports = function(Self) {
const addressIds = result.map(address => address.addressFk);
for (const address of addressIds)
await createInvoice(ctx, companyId, ticketsIds, address, invoicesIds, myOptions);
await createInvoice(ctx, companyId, ticketsIds, address, invoicesIds, invoiceCorrection, myOptions);
} else
await createInvoice(ctx, companyId, ticketsIds, null, invoicesIds, myOptions);
await createInvoice(ctx, companyId, ticketsIds, null, invoicesIds, invoiceCorrection, myOptions);
if (tx) await tx.commit();
} catch (e) {
@ -85,9 +91,9 @@ module.exports = function(Self) {
return invoicesIds;
};
async function createInvoice(ctx, companyId, ticketsIds, address, invoicesIds, myOptions) {
async function createInvoice(ctx, companyId, ticketsIds, address, invoicesIds, invoiceCorrection, myOptions) {
const models = Self.app.models;
console.log(ticketsIds, address);
await models.Ticket.rawSql(`
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketToInvoice
(PRIMARY KEY (id))
@ -98,7 +104,8 @@ module.exports = function(Self) {
${address ? `AND addressFk = ${address}` : ''}
`, [ticketsIds], myOptions);
const invoiceId = await models.Ticket.makeInvoice(ctx, 'R', companyId, Date.vnNew(), myOptions);
const invoiceId =
await models.Ticket.makeInvoice(ctx, 'R', companyId, Date.vnNew(), invoiceCorrection, myOptions);
invoicesIds.push(invoiceId);
}
};

View File

@ -22,6 +22,11 @@ module.exports = function(Self) {
description: 'The invoice date',
type: 'date',
required: true
},
{
arg: 'invoiceCorrection',
description: 'The invoice correction',
type: 'object',
}
],
returns: {
@ -34,7 +39,7 @@ module.exports = function(Self) {
}
});
Self.makeInvoice = async(ctx, invoiceType, companyFk, invoiceDate, options) => {
Self.makeInvoice = async(ctx, invoiceType, companyFk, invoiceDate, invoiceCorrection, options) => {
const models = Self.app.models;
invoiceDate.setHours(0, 0, 0, 0);
@ -66,8 +71,8 @@ module.exports = function(Self) {
const [firstTicket] = tickets;
const clientId = firstTicket.clientFk;
const clientCanBeInvoiced = await models.Client.canBeInvoiced(clientId, companyFk, myOptions);
if (!clientCanBeInvoiced)
const clientCanBeInvoiced = await models.Client.canBeInvoiced(clientId, companyFk, invoiceCorrection, myOptions);
if (!clientCanBeInvoiced && !invoiceCorrection)
throw new UserError(`This client can't be invoiced`);
const query = `SELECT vn.invoiceSerial(?, ?, ?) AS serial`;
@ -90,6 +95,13 @@ module.exports = function(Self) {
if (!resultInvoice)
throw new UserError('No tickets to invoice', 'notInvoiced');
if (invoiceCorrection) {
await models.InvoiceCorrection.create(
Object.assign(invoiceCorrection, {correctingFk: resultInvoice.id}),
myOptions
);
}
if (serial != 'R' && resultInvoice.id)
await Self.rawSql('CALL invoiceOutBooking(?)', [resultInvoice.id], myOptions);

View File

@ -15,7 +15,7 @@ module.exports = Self => {
}
],
returns: {
type: ['number'],
type: ['object'],
root: true
},
http: {