Merge branch 'master' into 6878-hotfix-addManualTimeEntries
gitea/salix/pipeline/pr-master This commit looks good Details

This commit is contained in:
Alex Moreno 2024-02-23 07:32:18 +00:00
commit f51df43d17
4 changed files with 63 additions and 86 deletions

View File

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

View File

@ -48,7 +48,7 @@ BEGIN
IF vCounter > 0 OR vASIEN > 0 THEN
UPDATE vn2008.XDiario x
UPDATE XDiario x
JOIN ledgerConfig lc ON lc.lastBookEntry = x.ASIEN
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 myOptions = {userId: ctx.req.accessToken.userId};
let tx;
let ticket;
let externalTickets = [];
let dms;
let gestDocCreated = false;
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -44,11 +44,6 @@ module.exports = Self => {
myOptions.transaction = tx;
}
const dmsTypeTicket = await models.DmsType.findOne({
where: {code: 'ticket'},
fields: ['id']
});
async function setLocation(ticketId) {
await models.Delivery.create({
ticketFk: ticketId,
@ -58,106 +53,102 @@ module.exports = Self => {
}, myOptions);
}
async function hasSignDms(ticketId) {
async function gestDocExists(ticketId) {
const ticketDms = await models.TicketDms.findOne({
where: {ticketFk: ticketId},
include: [
{
relation: 'dms',
fields: ['id'],
scope: {
where: {dmsTypeFk: dmsTypeTicket.id}
}
}
]
});
if (ticketDms?.dms()?.id) return true;
fields: ['dmsFk']
}, myOptions);
if (!ticketDms) return false;
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);
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);
if (ticket.route() === null)
throw new UserError('Ticket without route');
ctxUploadFile.args = {
warehouseId: ticket.warehouseFk,
companyId: ticket.companyFk,
dmsTypeId: dmsTypeTicket.id,
reference: ticket.id,
dmsTypeId: dmsType.id,
reference: '',
description: `Firma del cliente - Ruta ${ticket.route().id}`,
contentType: 'image/png',
hasFile: true
hasFile: false
};
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, myOptions);
dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
gestDocCreated = true;
}
try {
for (const ticketId of tickets) {
ticket = await models.Ticket.findById(ticketId, {
include: [{
relation: 'address',
scope: {
include: {
relation: 'province',
scope: {
include: {
relation: 'country',
scope: {
fields: ['code']
}
}
}
}
}
}, {
relation: 'route',
scope: {
fields: ['id']
}
}]
});
const ticketState = await models.TicketState.findOne(
{where: {ticketFk: ticketId},
fields: ['alertLevel']
}, myOptions);
const ticketState = await models.TicketState.findOne({
where: {ticketFk: ticketId},
fields: ['alertLevel']
});
const packedAlertLevel = await models.AlertLevel.findOne({
where: {code: 'PACKED'},
const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'},
fields: ['id']
});
}, myOptions);
if (!ticketState)
throw new UserError('Ticket does not exist');
if (!ticket.route())
throw new UserError('Ticket without route');
if (ticketState.alertLevel < packedAlertLevel.id)
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');
if (location) await setLocation(ticketId);
if (!await hasSignDms(ticketId))
await createGestDoc(ticketId);
if (!gestDocCreated) 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);
const deliveryState = await models.State.findOne({
where: {code: 'DELIVERED'}
});
where: {
code: 'DELIVERED'
}
}, myOptions);
await models.Ticket.state(ctx, {
ticketFk: ticketId,
stateFk: deliveryState.id
}, myOptions);
if (ticket?.address()?.province()?.country()?.code != 'ES') {
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
externalTickets.push(ticketId);
}
}
if (tx) await tx.commit();
return;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
await models.Route.cmrEmail(ctx, externalTickets);
};
};