refs #5914 WIP clone method created
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2023-07-31 10:33:51 +02:00
parent e63efa4f0e
commit 22f76ec6a9
10 changed files with 129 additions and 151 deletions

View File

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

View File

@ -34,15 +34,17 @@ module.exports = Self => {
myOptions.transaction = tx;
}
try {
const {ref, newClientFk, cplusRectificationId, cplusCorrectingTypeId, invoiceCorrectionTypeId} = params;
if (!ref || !newClientFk || !cplusRectificationId || !cplusCorrectingTypeId || !invoiceCorrectionTypeId)
const {id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477FkId, invoiceCorrectionTypeId} = params;
if (!id || !ref || !newClientFk || !cplusRectificationId || !cplusInvoiceType477FkId || !invoiceCorrectionTypeId)
throw new UserError(`There are missing fields.`);
// Refund tickets and group
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
await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
// Clone tickets
const refundAgencyMode = await models.AgencyMode.findOne({
include: {
relation: 'zones',
@ -66,11 +68,28 @@ module.exports = Self => {
}
};
const sales = await models.Sale.find(salesFilter, myOptions);
// Actualizar cliente
const isRefund = false;
const clonedTicketIds = await models.Sale.clone(sales, refundAgencyMode, refoundZoneId, servicesIds, null, isRefund, myOptions);
// Invoice Ticket - Factura rápida ??
// Update client
for (const clonedTicketId of clonedTicketIds) {
const ticket = await models.Ticket.findById(clonedTicketId, myOptions);
await ticket.updateAttributes({clientFk: newClientFk});
}
// Quick invoice
const invoiceIds = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions);
// Insert InvoiceCorrection
for (const invoiceId of invoiceIds) {
await models.invoiceCorrection.create({
correctingFk: invoiceId,
correctedFk: id,
cplusRectificationTypeFk: cplusRectificationId,
cplusInvoiceType477Fk: cplusInvoiceType477FkId,
invoiceCorrectionType: invoiceCorrectionTypeId
});
}
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();

View File

@ -35,13 +35,13 @@
"CplusRectificationType": {
"dataSource": "vn"
},
"CplusCorrectingType": {
"dataSource": "vn"
},
"InvoiceCorrectionType": {
"dataSource": "vn"
},
"InvoiceCorrection": {
"dataSource": "vn"
},
"CplusInvoiceType477": {
"dataSource": "vn"
}
}

View File

@ -1,9 +1,9 @@
{
"name": "CplusCorrectingType",
"name": "CplusInvoiceType477",
"base": "VnModel",
"options": {
"mysql": {
"table": "cplusCorrectingType"
"table": "cplusInvoiceType477"
}
},
"properties": {

View File

@ -6,8 +6,8 @@
</vn-crud-model>
<vn-crud-model
auto-load="true"
url="CplusCorrectingTypes"
data="cplusCorrectingTypes">
url="CplusInvoiceType477s"
data="cplusInvoiceType477">
</vn-crud-model>
<vn-crud-model
auto-load="true"
@ -222,12 +222,12 @@
<vn-horizontal>
<vn-autocomplete
vn-one
vn-id="cplusCorrectingType"
data="cplusCorrectingTypes"
vn-id="cplusInvoiceType477"
data="cplusInvoiceType477"
show-field="description"
value-field="id"
required="true"
ng-model="$ctrl.cplusCorrectingType"
ng-model="$ctrl.cplusInvoiceType477"
label="Class">
</vn-autocomplete>
<vn-autocomplete

View File

@ -129,10 +129,11 @@ class Controller extends Section {
transferInvoice() {
const params = {
data: {
id: this.invoiceOut.id,
ref: this.invoiceOut.ref,
newClientFk: this.invoiceOut.client.id,
cplusRectificationId: this.cplusRectificationType,
cplusCorrectingTypeId: this.cplusCorrectingType,
cplusInvoiceType477Id: this.cplusInvoiceType477,
invoiceCorrectionTypeId: this.invoiceCorrectionType
}
};

View File

@ -1,76 +1,67 @@
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;
module.exports = Self => {
Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, isRefund, myOptions) => {
const models = Self.app.models;
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const now = Date.vnNew();
let refundTickets = [];
let refundTicket;
const now = Date.vnNew();
let updatedTickets = [];
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
);
}
const filter = {include: {relation: 'address'}};
const ticket = await models.Ticket.findById(ticketId, filter, 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,
const ticketUpdated = 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);
updatedTickets.push(ticketUpdated);
}
}
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;
}
for (const sale of sales) {
const createdSale = await models.Sale.create({
ticketFk: 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: service.ticketFk,
ticketServiceTypeFk: service.ticketServiceTypeFk,
}, myOptions);
}
}
const query = `CALL vn.ticket_recalc(?, NULL)`;
for (const updatedTicket of updatedTickets)
await Self.rawSql(query, [updatedTicket.id], myOptions);
return updatedTickets.map(updatedTicket => updatedTicket.id);
};
};

View File

@ -1,25 +0,0 @@
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

@ -158,10 +158,11 @@ module.exports = Self => {
const group = true;
const isRefund = true;
const refundTicket = await clone(
const refundTicket = await cloneAndGroup(
sales,
refundAgencyMode,
refoundZoneId, servicesIds,
refoundZoneId,
servicesIds,
withWarehouse,
group,
isRefund,
@ -176,41 +177,37 @@ module.exports = Self => {
}
};
async function clone(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions) {
const models = Self.app.models;
async function cloneAndGroup(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions) {
if (!group) {
const tickets = await models.Sale.clone(sales,
refundAgencyMode,
refoundZoneId, servicesIds,
withWarehouse,
isRefund,
myOptions);
return tickets;
}
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const [firstTicketId] = ticketsIds;
const filter = {include: {relation: 'address'}};
const ticket = await models.Ticket.findById(firstTicketId, filter, myOptions);
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
);
}
const ticketUpdated = 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);
for (const sale of sales) {
const createdSale = await models.Sale.create({
ticketFk: (group) ? refundTicket.id : sale.ticketFk,
ticketFk: ticketUpdated.id,
itemFk: sale.itemFk,
quantity: (isRefund) ? - sale.quantity : sale.quantity,
concept: sale.concept,
@ -236,21 +233,15 @@ module.exports = Self => {
quantity: (isRefund) ? - service.quantity : service.quantity,
price: service.price,
taxClassFk: service.taxClassFk,
ticketFk: (group) ? refundTicket.id : service.ticketFk,
ticketFk: ticketUpdated.id,
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;
}
await Self.rawSql(query, [refundTicket.id], myOptions);
return ticketUpdated;
}
async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) {

View File

@ -10,6 +10,7 @@ module.exports = Self => {
require('../methods/sale/refund')(Self);
require('../methods/sale/canEdit')(Self);
require('../methods/sale/usesMana')(Self);
require('../methods/sale/clone')(Self);
Self.validatesPresenceOf('concept', {
message: `Concept cannot be blank`