#6878 hotfix fix add manual entries #2074

Merged
jorgep merged 2 commits from 6878-hotfix-addManualTimeEntries into master 2024-02-23 07:37:12 +00:00
4 changed files with 63 additions and 86 deletions
Showing only changes of commit f51df43d17 - Show all commits

View File

@ -16,7 +16,7 @@ BEGIN
LEFT JOIN vn.supplier s LEFT JOIN vn.supplier s
ON s.id = bp.supplierFk ON s.id = bp.supplierFk
LEFT JOIN vn.bank b LEFT JOIN vn.bank b
ON b.id = bp.bankFk ON b.id = bp.accountingFk
WHERE bp.insuranceExpired = util.VN_CURDATE(); WHERE bp.insuranceExpired = util.VN_CURDATE();
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -48,7 +48,7 @@ BEGIN
IF vCounter > 0 OR vASIEN > 0 THEN IF vCounter > 0 OR vASIEN > 0 THEN
UPDATE vn2008.XDiario x UPDATE XDiario x
JOIN ledgerConfig lc ON lc.lastBookEntry = x.ASIEN JOIN ledgerConfig lc ON lc.lastBookEntry = x.ASIEN
SET x.ASIEN = vASIEN; SET x.ASIEN = vASIEN;

View File

@ -1,14 +0,0 @@
CREATE OR REPLACE DEFINER=`root`@`localhost`
SQL SECURITY DEFINER
VIEW `vn`.`doc`
AS SELECT `g`.`id` AS `id`,
`g`.`sref` AS `sref`,
`g`.`brief` AS `brief`,
`g`.`emp_id` AS `companyFk`,
`g`.`orden` AS `order`,
`g`.`file` AS `file`,
`g`.`original` AS `original`,
`g`.`trabajador_id` AS `workerFk`,
`g`.`odbc_date` AS `created`,
`g`.`warehouse_id` AS `warehouseFk`
FROM `vn2008`.`gestdoc` `g`

View File

@ -33,8 +33,8 @@ module.exports = Self => {
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;
let ticket; let dms;
let externalTickets = []; let gestDocCreated = false;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -44,11 +44,6 @@ module.exports = Self => {
myOptions.transaction = tx; myOptions.transaction = tx;
} }
const dmsTypeTicket = await models.DmsType.findOne({
where: {code: 'ticket'},
fields: ['id']
});
async function setLocation(ticketId) { async function setLocation(ticketId) {
await models.Delivery.create({ await models.Delivery.create({
ticketFk: ticketId, ticketFk: ticketId,
@ -58,106 +53,102 @@ module.exports = Self => {
}, myOptions); }, myOptions);
} }
async function hasSignDms(ticketId) { async function gestDocExists(ticketId) {
const ticketDms = await models.TicketDms.findOne({ const ticketDms = await models.TicketDms.findOne({
where: {ticketFk: ticketId}, where: {ticketFk: ticketId},
include: [ fields: ['dmsFk']
{ }, myOptions);
relation: 'dms',
fields: ['id'], if (!ticketDms) return false;
scope: {
where: {dmsTypeFk: dmsTypeTicket.id} const ticket = await models.Ticket.findById(ticketId, {fields: ['isSigned']}, myOptions);
} if (ticket.isSigned == true)
} return true;
] else
}); await models.Dms.destroyAll({where: {reference: ticketId}}, myOptions);
if (ticketDms?.dms()?.id) return true;
return false;
} }
async function createGestDoc() { async function createGestDoc(id) {
const ticket = await models.Ticket.findById(id,
{
include: [
{
relation: 'warehouse',
scope: {
fields: ['id']
}
}, {
relation: 'client',
scope: {
fields: ['name']
}
}, {
relation: 'route',
scope: {
fields: ['id']
}
}
]
}, myOptions);
const dmsType = await models.DmsType.findOne({where: {code: 'Ticket'}, fields: ['id']}, myOptions);
const ctxUploadFile = Object.assign({}, ctx); const ctxUploadFile = Object.assign({}, ctx);
if (ticket.route() === null)
throw new UserError('Ticket without route');
ctxUploadFile.args = { ctxUploadFile.args = {
warehouseId: ticket.warehouseFk, warehouseId: ticket.warehouseFk,
companyId: ticket.companyFk, companyId: ticket.companyFk,
dmsTypeId: dmsTypeTicket.id, dmsTypeId: dmsType.id,
reference: ticket.id, reference: '',
description: `Firma del cliente - Ruta ${ticket.route().id}`, description: `Firma del cliente - Ruta ${ticket.route().id}`,
contentType: 'image/png', hasFile: false
hasFile: true
}; };
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions); gestDocCreated = true;
} }
try { try {
for (const ticketId of tickets) { for (const ticketId of tickets) {
ticket = await models.Ticket.findById(ticketId, { const ticketState = await models.TicketState.findOne(
include: [{ {where: {ticketFk: ticketId},
relation: 'address', fields: ['alertLevel']
scope: { }, myOptions);
include: {
relation: 'province',
scope: {
include: {
relation: 'country',
scope: {
fields: ['code']
}
}
}
}
}
}, {
relation: 'route',
scope: {
fields: ['id']
}
}]
});
const ticketState = await models.TicketState.findOne({ const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'},
where: {ticketFk: ticketId},
fields: ['alertLevel']
});
const packedAlertLevel = await models.AlertLevel.findOne({
where: {code: 'PACKED'},
fields: ['id'] fields: ['id']
}); }, myOptions);
if (!ticketState) if (!ticketState)
throw new UserError('Ticket does not exist'); throw new UserError('Ticket does not exist');
if (!ticket.route())
throw new UserError('Ticket without route');
if (ticketState.alertLevel < packedAlertLevel.id) if (ticketState.alertLevel < packedAlertLevel.id)
throw new UserError('This ticket cannot be signed because it has not been boxed'); throw new UserError('This ticket cannot be signed because it has not been boxed');
if (await ticket.isSigned) if (await gestDocExists(ticketId))
throw new UserError('Ticket is already signed'); throw new UserError('Ticket is already signed');
if (location) await setLocation(ticketId); if (location) await setLocation(ticketId);
if (!await hasSignDms(ticketId)) if (!gestDocCreated) await createGestDoc(ticketId);
await createGestDoc(ticketId); await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions);
const ticket = await models.Ticket.findById(ticketId, null, myOptions);
await ticket.updateAttribute('isSigned', true, myOptions); await ticket.updateAttribute('isSigned', true, myOptions);
const deliveryState = await models.State.findOne({ const deliveryState = await models.State.findOne({
where: {code: 'DELIVERED'} where: {
}); code: 'DELIVERED'
}
}, myOptions);
await models.Ticket.state(ctx, { await models.Ticket.state(ctx, {
ticketFk: ticketId, ticketFk: ticketId,
stateFk: deliveryState.id stateFk: deliveryState.id
}, myOptions); }, myOptions);
if (ticket?.address()?.province()?.country()?.code != 'ES') {
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
externalTickets.push(ticketId);
}
} }
if (tx) await tx.commit(); if (tx) await tx.commit();
return;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;
} }
await models.Route.cmrEmail(ctx, externalTickets);
}; };
}; };