Merge branch 'test' into dev
This commit is contained in:
commit
554f6c4695
|
@ -38,7 +38,8 @@ BEGIN
|
|||
DELETE FROM saleTracking WHERE created < vOneYearAgo;
|
||||
DELETE FROM ticketTracking WHERE created < v18Month;
|
||||
DELETE tobs FROM ticketObservation tobs
|
||||
JOIN ticket t ON tobs.ticketFk = t.id WHERE t.shipped < TIMESTAMPADD(YEAR,-2,util.VN_CURDATE());
|
||||
JOIN ticket t ON tobs.ticketFk = t.id
|
||||
WHERE t.shipped < v5Years;
|
||||
DELETE sc.* FROM saleCloned sc JOIN sale s ON s.id = sc.saleClonedFk JOIN ticket t ON t.id = s.ticketFk WHERE t.shipped < vOneYearAgo;
|
||||
DELETE FROM sharingCart where ended < vDateShort;
|
||||
DELETE FROM sharingClient where ended < vDateShort;
|
||||
|
|
|
@ -25,11 +25,6 @@ proc: BEGIN
|
|||
DELETE FROM Movimientos_mark WHERE odbc_date < vDate;
|
||||
DELETE FROM Splits WHERE Fecha < vDate18;
|
||||
|
||||
DELETE tobs
|
||||
FROM ticket_observation tobs
|
||||
JOIN Tickets t ON tobs.Id_Ticket = t.Id_Ticket
|
||||
WHERE t.Fecha < vDate;
|
||||
|
||||
DELETE tobs
|
||||
FROM movement_label tobs
|
||||
JOIN Movimientos m ON tobs.Id_Movimiento = m.Id_Movimiento
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
DELETE FROM vn.entryObservation
|
||||
WHERE observationTypeFk IS NULL;
|
||||
|
||||
ALTER TABLE vn.entryObservation
|
||||
MODIFY COLUMN observationTypeFk tinyint(3) unsigned NOT NULL;
|
|
@ -48,23 +48,18 @@ module.exports = Self => {
|
|||
if (!recipient)
|
||||
throw new UserError('There is no assigned email for this client');
|
||||
|
||||
const dms = await models.TicketDms.findOne({
|
||||
where: {ticketFk: ticketId},
|
||||
include: [{
|
||||
relation: 'dms',
|
||||
fields: ['id'],
|
||||
scope: {
|
||||
relation: 'dmsType',
|
||||
scope: {
|
||||
where: {code: 'cmr'}
|
||||
}
|
||||
}
|
||||
}]
|
||||
}, myOptions);
|
||||
const dms = await Self.rawSql(`
|
||||
SELECT d.id
|
||||
FROM ticketDms td
|
||||
JOIN dms d ON d.id = td.dmsFk
|
||||
JOIN dmsType dt ON dt.id = d.dmsTypeFk
|
||||
WHERE td.ticketFk = ?
|
||||
AND dt.code = 'cmr'
|
||||
`, [ticketId]);
|
||||
|
||||
if (!dms) throw new UserError('Cmr file does not exist');
|
||||
if (!dms.length) throw new UserError('Cmr file does not exist');
|
||||
|
||||
const response = await models.Dms.downloadFile(ctx, dms.id);
|
||||
const response = await models.Dms.downloadFile(ctx, dms[0].id);
|
||||
|
||||
const email = new Email('cmr', {
|
||||
ticketId,
|
||||
|
|
|
@ -42,18 +42,15 @@ module.exports = Self => {
|
|||
const ticket = await models.Ticket.findById(ticketId, myOptions);
|
||||
|
||||
if (ticket.cmrFk) {
|
||||
const hasDmsCmr = await models.TicketDms.findOne({
|
||||
where: {ticketFk: ticketId},
|
||||
include: {
|
||||
relation: 'dms',
|
||||
fields: ['dmsFk'],
|
||||
scope: {
|
||||
where: {dmsTypeFk: dmsTypeCmr.id}
|
||||
}
|
||||
}
|
||||
}, myOptions);
|
||||
const hasDmsCmr = await Self.rawSql(`
|
||||
SELECT d.id
|
||||
FROM ticketDms td
|
||||
JOIN dms d ON d.id = td.dmsFk
|
||||
WHERE td.ticketFk = ?
|
||||
AND d.dmsTypeFk = ?
|
||||
`, [ticketId, dmsTypeCmr.id]);
|
||||
|
||||
if (hasDmsCmr?.dms())
|
||||
if (hasDmsCmr.length)
|
||||
throw new UserError('This ticket already has a cmr saved');
|
||||
|
||||
ctx.args.id = ticket.cmrFk;
|
||||
|
|
|
@ -33,8 +33,10 @@ module.exports = Self => {
|
|||
const models = Self.app.models;
|
||||
const myOptions = {userId: ctx.req.accessToken.userId};
|
||||
let tx;
|
||||
let ticket;
|
||||
let dms;
|
||||
let gestDocCreated = false;
|
||||
let isSignUploaded;
|
||||
let externalTickets = [];
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
@ -44,6 +46,11 @@ 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,
|
||||
|
@ -53,102 +60,105 @@ module.exports = Self => {
|
|||
}, myOptions);
|
||||
}
|
||||
|
||||
async function gestDocExists(ticketId) {
|
||||
const ticketDms = await models.TicketDms.findOne({
|
||||
where: {ticketFk: ticketId},
|
||||
fields: ['dmsFk']
|
||||
}, myOptions);
|
||||
async function hasSignDms(ticketId) {
|
||||
const hasTicketDms = await Self.rawSql(`
|
||||
SELECT d.id
|
||||
FROM ticketDms td
|
||||
JOIN dms d ON d.id = td.dmsFk
|
||||
WHERE td.ticketFk = ?
|
||||
AND d.dmsTypeFk = ?
|
||||
`, [ticketId, dmsTypeTicket.id], 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;
|
||||
if (hasTicketDms.length) return true;
|
||||
}
|
||||
|
||||
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);
|
||||
async function createGestDoc() {
|
||||
const ctxUploadFile = Object.assign({}, ctx);
|
||||
if (ticket.route() === null)
|
||||
throw new UserError('Ticket without route');
|
||||
ctxUploadFile.args = {
|
||||
warehouseId: ticket.warehouseFk,
|
||||
companyId: ticket.companyFk,
|
||||
dmsTypeId: dmsType.id,
|
||||
reference: '',
|
||||
dmsTypeId: dmsTypeTicket.id,
|
||||
reference: ticket.id,
|
||||
description: `Firma del cliente - Ruta ${ticket.route().id}`,
|
||||
hasFile: false
|
||||
hasFile: true
|
||||
};
|
||||
dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
|
||||
gestDocCreated = true;
|
||||
// Si se ha subido ya la firma, no se vuelve a subir, ya que si no
|
||||
// da un error de deadlock en la db
|
||||
isSignUploaded = true;
|
||||
}
|
||||
|
||||
try {
|
||||
for (const ticketId of tickets) {
|
||||
const ticketState = await models.TicketState.findOne(
|
||||
{where: {ticketFk: ticketId},
|
||||
fields: ['alertLevel']
|
||||
}, myOptions);
|
||||
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']
|
||||
}
|
||||
}]
|
||||
}, myOptions);
|
||||
|
||||
const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'},
|
||||
const ticketState = await models.TicketState.findOne({
|
||||
where: {ticketFk: ticketId},
|
||||
fields: ['alertLevel']
|
||||
}, myOptions);
|
||||
|
||||
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 gestDocExists(ticketId))
|
||||
if (await ticket.isSigned)
|
||||
throw new UserError('Ticket is already signed');
|
||||
|
||||
if (location) await setLocation(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);
|
||||
if (!await hasSignDms(ticketId) && !isSignUploaded)
|
||||
await createGestDoc(ticketId);
|
||||
if (isSignUploaded)
|
||||
await models.TicketDms.create({ticketFk: ticket.id, dmsFk: dms[0].id}, 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' && ticket.cmrFk) {
|
||||
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);
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue