refs #5914 WIP transfer invoiceOut
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-07-28 15:21:43 +02:00
parent 61b17d1fa5
commit 50155cea8d
17 changed files with 543 additions and 2 deletions

View File

@ -0,0 +1,6 @@
INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId)
VALUES
('CplusRectificationType', '*', 'READ', 'ALLOW', 'ROLE', 'employee'),
('CplusCorrectingType', '*', 'READ', 'ALLOW', 'ROLE', 'employee'),
('InvoiceCorrectionType', '*', 'READ', 'ALLOW', 'ROLE', 'employee'),
('InvoiceOut', 'transferInvoiceOut', 'WRITE', 'ALLOW', 'ROLE', 'employee');

View File

@ -2958,3 +2958,16 @@ INSERT INTO `vn`.`invoiceInSerial` (`code`, `description`, `cplusTerIdNifFk`, `t
INSERT INTO `hedera`.`imageConfig` (`id`, `maxSize`, `useXsendfile`, `url`) INSERT INTO `hedera`.`imageConfig` (`id`, `maxSize`, `useXsendfile`, `url`)
VALUES VALUES
(1, 0, 0, 'marvel.com'); (1, 0, 0, 'marvel.com');
INSERT INTO `vn`.`cplusCorrectingType` (`description`)
VALUES
('Embalajes'),
('Anulación'),
('Impagado'),
('Moroso');
INSERT INTO `vn`.`invoiceCorrectionType` (`description`)
VALUES
('Error en el cálculo del IVA'),
('Error en el detalle de las ventas'),
('Error en los datos del cliente');

View File

@ -311,5 +311,6 @@
"You don't have enough privileges.": "No tienes suficientes permisos.", "You don't have enough privileges.": "No tienes suficientes permisos.",
"This ticket is locked.": "Este ticket está bloqueado.", "This ticket is locked.": "Este ticket está bloqueado.",
"This ticket is not editable.": "Este ticket no es editable.", "This ticket is not editable.": "Este ticket no es editable.",
"The ticket doesn't exist.": "No existe el ticket." "The ticket doesn't exist.": "No existe el ticket.",
"There are missing fields.": "There are missing fields."
} }

View File

@ -0,0 +1,81 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('transferInvoiceOut', {
description: 'Transfer an invoice out to another client',
accessType: 'WRITE',
accepts: [
{
arg: 'data',
type: 'Object',
required: true
}
],
returns: {
type: 'boolean',
root: true
},
http: {
path: '/transferInvoice',
verb: 'post'
}
});
Self.transferInvoiceOut = async(ctx, params, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const {ref, newClientFk, cplusRectificationId, cplusCorrectingTypeId, invoiceCorrectionTypeId} = params;
if (!ref || !newClientFk || !cplusRectificationId || !cplusCorrectingTypeId || !invoiceCorrectionTypeId)
throw new UserError(`There are missing fields.`);
const filter = {where: {refFk: ref}};
const tickets = await models.Ticket.find(filter, myOptions);
const ticketsIds = tickets.map(ticket => ticket.id);
const refundTicket = await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
// Clonar tickets
const refundAgencyMode = await models.AgencyMode.findOne({
include: {
relation: 'zones',
scope: {
limit: 1,
field: ['id', 'name']
}
},
where: {code: 'refund'}
}, myOptions);
const refoundZoneId = refundAgencyMode.zones()[0].id;
const services = await models.TicketService.find(filter, myOptions);
const servicesIds = services.map(service => service.id);
const salesFilter = {
where: {id: {inq: salesIds}},
include: {
relation: 'components',
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
}
};
const sales = await models.Sale.find(salesFilter, myOptions);
// Actualizar cliente
// Invoice Ticket - Factura rápida ??
// Insert InvoiceCorrection
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
return true;
};
};

View File

@ -31,5 +31,17 @@
}, },
"ZipConfig": { "ZipConfig": {
"dataSource": "vn" "dataSource": "vn"
},
"CplusRectificationType": {
"dataSource": "vn"
},
"CplusCorrectingType": {
"dataSource": "vn"
},
"InvoiceCorrectionType": {
"dataSource": "vn"
},
"InvoiceCorrection": {
"dataSource": "vn"
} }
} }

View File

@ -0,0 +1,19 @@
{
"name": "CplusCorrectingType",
"base": "VnModel",
"options": {
"mysql": {
"table": "cplusCorrectingType"
}
},
"properties": {
"id": {
"id": true,
"type": "number",
"description": "Identifier"
},
"description": {
"type": "string"
}
}
}

View File

@ -0,0 +1,19 @@
{
"name": "CplusRectificationType",
"base": "VnModel",
"options": {
"mysql": {
"table": "cplusRectificationType"
}
},
"properties": {
"id": {
"id": true,
"type": "number",
"description": "Identifier"
},
"description": {
"type": "string"
}
}
}

View File

@ -0,0 +1,19 @@
{
"name": "InvoiceCorrectionType",
"base": "VnModel",
"options": {
"mysql": {
"table": "invoiceCorrectionType"
}
},
"properties": {
"id": {
"id": true,
"type": "number",
"description": "Identifier"
},
"description": {
"type": "string"
}
}
}

View File

@ -23,6 +23,7 @@ module.exports = Self => {
require('../methods/invoiceOut/getInvoiceDate')(Self); require('../methods/invoiceOut/getInvoiceDate')(Self);
require('../methods/invoiceOut/negativeBases')(Self); require('../methods/invoiceOut/negativeBases')(Self);
require('../methods/invoiceOut/negativeBasesCsv')(Self); require('../methods/invoiceOut/negativeBasesCsv')(Self);
require('../methods/invoiceOut/transferInvoiceOut')(Self);
Self.filePath = async function(id, options) { Self.filePath = async function(id, options) {
const fields = ['ref', 'issued']; const fields = ['ref', 'issued'];

View File

@ -0,0 +1,28 @@
{
"name": "InvoiceCorrection",
"base": "VnModel",
"options": {
"mysql": {
"table": "invoiceCorrection"
}
},
"properties": {
"correctingFk": {
"id": true,
"type": "number",
"description": "Identifier"
},
"correctedFk": {
"type": "number"
},
"cplusRectificationTypeFk": {
"type": "number"
},
"cplusInvoiceType477Fk": {
"type": "number"
},
"invoiceCorrectionTypeFk": {
"type": "number"
}
}
}

View File

@ -1,3 +1,19 @@
<vn-crud-model
auto-load="true"
url="CplusRectificationTypes"
data="cplusRectificationTypes"
order="description">
</vn-crud-model>
<vn-crud-model
auto-load="true"
url="CplusCorrectingTypes"
data="cplusCorrectingTypes">
</vn-crud-model>
<vn-crud-model
auto-load="true"
url="InvoiceCorrectionTypes"
data="invoiceCorrectionTypes">
</vn-crud-model>
<vn-icon-button <vn-icon-button
icon="more_vert" icon="more_vert"
@ -5,6 +21,13 @@
</vn-icon-button> </vn-icon-button>
<vn-menu vn-id="menu"> <vn-menu vn-id="menu">
<vn-list> <vn-list>
<vn-item
vn-acl="administrative"
vn-acl-action="remove"
ng-click="transferInvoice.show()"
translate>
Transfer invoice to...
</vn-item>
<vn-item class="dropdown" <vn-item class="dropdown"
vn-click-stop="showInvoiceMenu.show($event, 'left')" vn-click-stop="showInvoiceMenu.show($event, 'left')"
name="showInvoicePdf" name="showInvoicePdf"
@ -157,3 +180,69 @@
<button response="accept" translate>Confirm</button> <button response="accept" translate>Confirm</button>
</tpl-buttons> </tpl-buttons>
</vn-dialog> </vn-dialog>
<vn-dialog
vn-id="transferInvoice"
title="transferInvoice"
size="sm"
on-accept="$ctrl.transferInvoice()">
<tpl-body>
<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="cplusCorrectingType"
data="cplusCorrectingTypes"
show-field="description"
value-field="id"
required="true"
ng-model="$ctrl.cplusCorrectingType"
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>
</tpl-body>
<tpl-buttons>
<button response="accept" translate>Transfer client</button>
</tpl-buttons>
</vn-dialog>

View File

@ -125,6 +125,19 @@ class Controller extends Section {
this.$state.go('ticket.card.sale', {id: refundTicket.id}); this.$state.go('ticket.card.sale', {id: refundTicket.id});
}); });
} }
transferInvoice() {
const params = {
data: {
ref: this.invoiceOut.ref,
newClientFk: this.invoiceOut.client.id,
cplusRectificationId: this.cplusRectificationType,
cplusCorrectingTypeId: this.cplusCorrectingType,
invoiceCorrectionTypeId: this.invoiceCorrectionType
}
};
this.$http.post(`InvoiceOuts/transferInvoice`, params).then(res => console.log(res.data));
}
} }
Controller.$inject = ['$element', '$scope', 'vnReport', 'vnEmail']; Controller.$inject = ['$element', '$scope', 'vnReport', 'vnEmail'];

View File

@ -1 +1,3 @@
The following refund tickets have been created: "The following refund tickets have been created: {{ticketIds}}" The following refund tickets have been created: "The following refund tickets have been created: {{ticketIds}}"
Transfer invoice to...: Transfer invoice to...
Cplus Type: Cplus Type

View File

@ -21,3 +21,5 @@ The invoice PDF document has been regenerated: El documento PDF de la factura ha
The email can't be empty: El correo no puede estar vacío 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}}" The following refund tickets have been created: "Se han creado los siguientes tickets de abono: {{ticketIds}}"
Refund...: Abono... Refund...: Abono...
Transfer invoice to...: Transferir factura a...
Cplus Type: Cplus Tipo

View File

@ -0,0 +1,76 @@
module.exports = async function clone(ctx, Self, sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions) {
const models = Self.app.models;
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const [firstTicketId] = ticketsIds;
const now = Date.vnNew();
let refundTickets = [];
let refundTicket;
if (!group) {
for (const ticketId of ticketsIds) {
refundTicket = await createTicketRefund(
ticketId,
now,
refundAgencyMode,
refoundZoneId,
null,
myOptions
);
refundTickets.push(refundTicket);
}
} else {
refundTicket = await createTicketRefund(
firstTicketId,
now,
refundAgencyMode,
refoundZoneId,
withWarehouse,
myOptions
);
}
for (const sale of sales) {
const createdSale = await models.Sale.create({
ticketFk: (group) ? refundTicket.id : sale.ticketFk,
itemFk: sale.itemFk,
quantity: (isRefund) ? - sale.quantity : sale.quantity,
concept: sale.concept,
price: sale.price,
discount: sale.discount,
}, myOptions);
const components = sale.components();
for (const component of components)
component.saleFk = createdSale.id;
await models.SaleComponent.create(components, myOptions);
}
if (servicesIds && servicesIds.length > 0) {
const servicesFilter = {
where: {id: {inq: servicesIds}}
};
const services = await models.TicketService.find(servicesFilter, myOptions);
for (const service of services) {
await models.TicketService.create({
description: service.description,
quantity: (isRefund) ? - service.quantity : service.quantity,
price: service.price,
taxClassFk: service.taxClassFk,
ticketFk: (group) ? refundTicket.id : service.ticketFk,
ticketServiceTypeFk: service.ticketServiceTypeFk,
}, myOptions);
}
}
const query = `CALL vn.ticket_recalc(?, NULL)`;
if (refundTickets.length > 0) {
for (const refundTicket of refundTickets)
await Self.rawSql(query, [refundTicket.id], myOptions);
return refundTickets.map(refundTicket => refundTicket.id);
} else {
await Self.rawSql(query, [refundTicket.id], myOptions);
return refundTicket;
}
};

View File

@ -0,0 +1,25 @@
module.exports = async function createTicketRefund(models, ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) {
// const models = Self.app.models;
const filter = {include: {relation: 'address'}};
const ticket = await models.Ticket.findById(ticketId, filter, myOptions);
const refundTicket = await models.Ticket.create({
clientFk: ticket.clientFk,
shipped: now,
addressFk: ticket.address().id,
agencyModeFk: refundAgencyMode.id,
nickname: ticket.address().nickname,
warehouseFk: withWarehouse ? ticket.warehouseFk : null,
companyFk: ticket.companyFk,
landed: now,
zoneFk: refoundZoneId
}, myOptions);
await models.TicketRefund.create({
refundTicketFk: refundTicket.id,
originalTicketFk: ticket.id,
}, myOptions);
return refundTicket;
};

View File

@ -28,7 +28,7 @@ module.exports = Self => {
} }
}); });
Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => { /* Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
let tx; let tx;
@ -111,6 +111,64 @@ module.exports = Self => {
if (tx) await tx.commit(); if (tx) await tx.commit();
return refundTicket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; */
Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const refundAgencyMode = await models.AgencyMode.findOne({
include: {
relation: 'zones',
scope: {
limit: 1,
field: ['id', 'name']
}
},
where: {code: 'refund'}
}, myOptions);
const refoundZoneId = refundAgencyMode.zones()[0].id;
const salesFilter = {
where: {id: {inq: salesIds}},
include: {
relation: 'components',
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
}
};
const sales = await models.Sale.find(salesFilter, myOptions);
const group = true;
const isRefund = true;
const refundTicket = await clone(
sales,
refundAgencyMode,
refoundZoneId, servicesIds,
withWarehouse,
group,
isRefund,
myOptions
);
if (tx) await tx.commit();
return refundTicket; return refundTicket;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
@ -118,6 +176,83 @@ module.exports = Self => {
} }
}; };
async function clone(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions) {
const models = Self.app.models;
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const [firstTicketId] = ticketsIds;
const now = Date.vnNew();
let refundTickets = [];
let refundTicket;
if (!group) {
for (const ticketId of ticketsIds) {
refundTicket = await createTicketRefund(
ticketId,
now,
refundAgencyMode,
refoundZoneId,
null,
myOptions
);
refundTickets.push(refundTicket);
}
} else {
refundTicket = await createTicketRefund(
firstTicketId,
now,
refundAgencyMode,
refoundZoneId,
withWarehouse,
myOptions
);
}
for (const sale of sales) {
const createdSale = await models.Sale.create({
ticketFk: (group) ? refundTicket.id : sale.ticketFk,
itemFk: sale.itemFk,
quantity: (isRefund) ? - sale.quantity : sale.quantity,
concept: sale.concept,
price: sale.price,
discount: sale.discount,
}, myOptions);
const components = sale.components();
for (const component of components)
component.saleFk = createdSale.id;
await models.SaleComponent.create(components, myOptions);
}
if (servicesIds && servicesIds.length > 0) {
const servicesFilter = {
where: {id: {inq: servicesIds}}
};
const services = await models.TicketService.find(servicesFilter, myOptions);
for (const service of services) {
await models.TicketService.create({
description: service.description,
quantity: (isRefund) ? - service.quantity : service.quantity,
price: service.price,
taxClassFk: service.taxClassFk,
ticketFk: (group) ? refundTicket.id : service.ticketFk,
ticketServiceTypeFk: service.ticketServiceTypeFk,
}, myOptions);
}
}
const query = `CALL vn.ticket_recalc(?, NULL)`;
if (refundTickets.length > 0) {
for (const refundTicket of refundTickets)
await Self.rawSql(query, [refundTicket.id], myOptions);
return refundTickets.map(refundTicket => refundTicket.id);
} else {
await Self.rawSql(query, [refundTicket.id], myOptions);
return refundTicket;
}
}
async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) { async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) {
const models = Self.app.models; const models = Self.app.models;