Remove ticket from route. Added warehouse to procedure. #326

This commit is contained in:
Joan Sanchez 2018-05-30 10:55:54 +02:00
parent fc67fdd6af
commit d6f0be2209
13 changed files with 307 additions and 11 deletions

View File

@ -33,7 +33,8 @@ class Controller {
let data = {
landed: toJsonDate(this.ticket.landed),
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk
};
return this.$http.post(query, data).then(res => {
@ -42,7 +43,6 @@ class Controller {
return true;
}, res => {
console.log(res);
if (res.data.error.message === 'NO_AGENCY_AVAILABLE')
this.vnApp.showError(
this.$translate.instant(`There's no available agency for this landing date`)

View File

@ -25,6 +25,7 @@ describe('ticket', () => {
addressFk: 121,
agencyModeFk: 1,
companyFk: 442,
warehouseFk: 1,
shipped: new Date(),
landed: new Date()
};
@ -42,6 +43,7 @@ describe('ticket', () => {
addressFk: 121,
agencyModeFk: 1,
companyFk: 442,
warehouseFk: 1,
shipped: new Date(),
landed: new Date()
};
@ -49,6 +51,7 @@ describe('ticket', () => {
let data = {
addressFk: 121,
agencyModeFk: 1,
warehouseFk: 1,
landed: new Date()
};
let response = {data: {error: new Error('NO_AGENCY_AVAILABLE')}};

View File

@ -11,5 +11,12 @@
initial-data="$ctrl.ticket.option">
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-check
vn-one label="Remove from route"
field="$ctrl.ticket.hasToBeUnrouted"
vn-acl="delivery">
</vn-check>
</vn-horizontal>
</vn-card>
</form>

View File

@ -15,6 +15,7 @@ class Controller {
$onChanges() {
this.ticket.option = 1;
this.ticket.hasToBeUnrouted = true;
}
onStepChange(state) {
@ -29,12 +30,14 @@ class Controller {
let query = `/ticket/api/tickets/${this.ticket.id}/componentUpdate`;
let data = {
clientFk: this.ticket.clientFk,
agencyModeFk: this.ticket.agencyModeFk,
addressFk: this.ticket.addressFk,
warehouseFk: this.ticket.warehouseFk,
shipped: this.ticket.shipped,
landed: this.ticket.landed,
isDeleted: this.ticket.isDeleted,
hasToBeUnrouted: this.ticket.hasToBeUnrouted,
option: this.ticket.option
};

View File

@ -1,3 +1,4 @@
Charge: Cargo
Choose an option: Selecciona una opción
Charge difference to: Diferencia a cargo de
Charge difference to: Diferencia a cargo de
Remove from route: Sacar de la ruta

View File

@ -0,0 +1,54 @@
USE `vn`;
DROP procedure IF EXISTS `ticketComponentMakeUpdate`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ticketComponentMakeUpdate`(
vTicketFk INT,
vClientFk INT,
vAgencyModeFk INT,
vAddressFk INT,
vWarehouseFk INT,
vShipped DATETIME,
vLanded DATE,
vIsDeleted BOOLEAN,
vHasToBeUnrouted BOOLEAN,
vOption INT)
BEGIN
/**
* Calcula los componentes de un ticket
* y los actualiza con los nuevos datos.
*
* @param vTicketFk Id del ticket
* @param vClientFk Id del cliente
* @param vAgencyModeFk Id del tipo de agencia
* @param vAddressFk Id del consignatario
* @param vWarehouseFk Id del almacén
* @param vShipped Fecha de salida
* @param vLanded Fecha de llegada
* @param vIsDeleted Marcado como eliminado
* @param vHasToBeUnrouted Marcado para sacar de ruta
* @param vOption Id de la acción ticketUpdateAction
*/
CALL vn.ticketComponentPreview (vTicketFk, vLanded, vAddressFk, vAgencyModeFk, vWarehouseFk);
CALL vn.ticketComponentUpdate (
vTicketFk,
vClientFk,
vAgencyModeFk,
vAddressFk,
vWarehouseFk,
vShipped,
vLanded,
vIsDeleted,
vHasToBeUnrouted,
vOption
);
DROP TEMPORARY TABLE
tmp.ticketComponent,
tmp.ticketComponentPrice;
END$$
DELIMITER ;

View File

@ -0,0 +1,74 @@
USE `vn`;
DROP procedure IF EXISTS `ticketComponentPreview`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `ticketComponentPreview`(
vTicketFk INT,
vDate DATE,
vAddressFk INT,
vAgencyModeFk INT,
vWarehouseFk SMALLINT)
BEGIN
/**
* Devuelve un listado previo de
* componentes para un ticket
*
* @param vTicketFk Id del ticket
* @param vDate Fecha de envío
* @param vAddressFk Id del consignatario
* @param vAgencyModeFk Id del modo de agencia
* @param vWarehouseFk Id del almacén
*/
DECLARE vAgencyFk INT;
DECLARE vShipped DATE;
DECLARE vBuyOrderItem INT DEFAULT 100;
SELECT agencyFk INTO vAgencyFk
FROM agencyMode
WHERE id = vAgencyModeFk;
CALL agencyHourOffer(vDate, vAddressFk, vAgencyFk);
SELECT shipped INTO vShipped
FROM tmp.agencyHourOffer
WHERE warehouseFk = vWarehouseFK;
CALL buyUltimate(vWarehouseFK, vShipped);
DROP TEMPORARY TABLE IF EXISTS tmp.ticketLot;
CREATE TEMPORARY TABLE tmp.ticketLot ENGINE = MEMORY (
SELECT
vWarehouseFK AS warehouseFk,
NULL AS available,
s.itemFk,
bu.buyFk
FROM sale s
LEFT JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk
WHERE s.ticketFk = vTicketFk
AND s.itemFk != vBuyOrderItem
GROUP BY bu.warehouseFk, bu.itemFk);
CALL ticketComponentCalculate(vAddressFk, vAgencyModeFk);
REPLACE INTO tmp.ticketComponent (warehouseFk, itemFk, componentFk, cost)
SELECT t.warehouseFk, s.itemFk, sc.componentFk, sc.value
FROM saleComponent sc
JOIN sale s ON s.id = sc.saleFk
JOIN ticket t ON t.id = s.ticketFk
JOIN componentRate cr ON cr.id = sc.componentFk
WHERE s.ticketFk = vTicketFk AND NOT cr.isRenewable;
SET @shipped = vShipped;
DROP TEMPORARY TABLE
tmp.agencyHourOffer,
tmp.buyUltimate,
tmp.ticketLot;
IF IFNULL(vShipped, CURDATE() - 1) < CURDATE() THEN
CALL util.throw('NO_AGENCY_AVAILABLE');
END IF;
END$$
DELIMITER ;

View File

@ -0,0 +1,55 @@
USE `vn`;
DROP procedure IF EXISTS `ticketComponentPriceDifference`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `ticketComponentPriceDifference`(
vTicketFk INT,
vDate DATE,
vAddressFk INT,
vAgencyModeFk INT,
vWarehouseFk INT)
BEGIN
/**
* Devuelve las diferencias de precio
* de los movimientos de un ticket.
*
* @param vTicketFk Id del ticket
* @param vDate Fecha de envío
* @param vAddressFk Id del consignatario
* @param vAgencyModeFk Id del modo de agencia
* @param vWarehouseFk Id del almacén
*/
CALL vn.ticketComponentPreview(vTicketFk, vDate, vAddressFk, vAgencyModeFk, vWarehouseFk);
SELECT
s.itemFk,
i.name,
i.size,
i.category,
IFNULL(s.quantity, 0) AS quantity,
IFNULL(s.price, 0) AS price,
ROUND(SUM(tc.cost), 4) AS newPrice,
s.quantity * (s.price - ROUND(SUM(cost), 4)) difference,
s.id AS saleFk
FROM sale s
JOIN item i ON i.id = s.itemFk
JOIN ticket t ON t.id = s.ticketFk
LEFT JOIN tmp.ticketComponent tc ON tc.itemFk = s.itemFk
AND tc.warehouseFk = t.warehouseFk
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
AND sc.componentFk = tc.componentFk
LEFT JOIN componentRate cr ON cr.id = tc.componentFk
WHERE
t.id = vTicketFk
AND IF(sc.componentFk IS NULL
AND cr.classRate IS NOT NULL, FALSE, TRUE)
GROUP BY s.id ORDER BY s.id;
DROP TEMPORARY TABLE
tmp.ticketComponent,
tmp.ticketComponentPrice;
END$$
DELIMITER ;

View File

@ -0,0 +1,74 @@
USE `vn`;
DROP procedure IF EXISTS `ticketComponentUpdate`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `ticketComponentUpdate`(
vTicketFk INT,
vClientFk INT,
vAgencyModeFk INT,
vAddressFk INT,
vWarehouseFk INT,
vShipped DATETIME,
vLanded DATE,
vIsDeleted BOOLEAN,
vHasToBeUnrouted BOOLEAN,
vOption INT)
BEGIN
/**
* Actualiza un ticket y sus componentes
* con los nuevos datos.
*
* @param vTicketFk Id del ticket
* @param vClientFk Id del cliente
* @param vAgencyModeFk Id del tipo de agencia
* @param vAddressFk Id del consignatario
* @param vWarehouseFk Id del almacén
* @param vShipped Fecha de salida
* @param vLanded Fecha de llegada
* @param vIsDeleted Marcado como eliminado
* @param vHasToBeUnrouted Marcado para sacar de ruta
* @param vOption Id de la acción ticketUpdateAction
*/
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
UPDATE ticket t
SET
t.clientFk = vClientFk,
t.agencyModeFk = vAgencyModeFk,
t.addressFk = vAddressFk,
t.warehouseFk = vWarehouseFk,
t.landed = vLanded,
t.shipped = vShipped,
t.isDeleted = vIsDeleted
WHERE
t.id = vTicketFk;
IF vHasToBeUnrouted THEN
UPDATE ticket t SET t.routeFk = NULL
WHERE t.id = vTicketFk;
END IF;
IF vOption <> 8 THEN
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
CREATE TEMPORARY TABLE tmp.sale
(PRIMARY KEY (saleFk))
ENGINE = MEMORY
SELECT id AS saleFk, vWarehouseFk warehouseFk
FROM sale s WHERE s.ticketFk = vTicketFk;
CALL ticketComponentUpdateSale (vOption);
DROP TEMPORARY TABLE tmp.sale;
END IF;
COMMIT;
END$$
DELIMITER ;

View File

@ -57,8 +57,14 @@ module.exports = Self => {
salesObj.totalNewPrice = 0.00;
salesObj.totalDifference = 0.00;
let query = `CALL vn.ticketComponentPriceDifference(?, ?, ?, ?)`;
let [differences] = await Self.rawSql(query, [data.landed, data.addressFk, data.agencyModeFk, ticketFk]);
let query = `CALL vn.ticketComponentPriceDifference(?, ?, ?, ?, ?)`;
let [differences] = await Self.rawSql(query, [
ticketFk,
data.landed,
data.addressFk,
data.agencyModeFk,
data.warehouseFk
]);
salesObj.items.forEach(sale => {
differences.forEach(difference => {

View File

@ -5,7 +5,8 @@ describe('sale priceDifference()', () => {
let data = {
landed: Date.now(),
addressFk: 121,
agencyModeFk: 1
agencyModeFk: 1,
warehouseFk: 1
};
app.models.Sale.priceDifference(1, data)
.catch(response => {

View File

@ -8,12 +8,20 @@ module.exports = Self => {
required: true,
description: 'ticket id',
http: {source: 'path'}
}, {
},
{
arg: 'data',
type: 'Object',
required: true,
description: 'landed, addressFk, agencyModeFk',
description: 'landed, addressFk, agencyModeFk, warehouseFk',
http: {source: 'body'}
},
{
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
}],
returns: {
type: ['Object'],
@ -25,16 +33,24 @@ module.exports = Self => {
}
});
Self.componentUpdate = async (ticketFk, data) => {
let query = 'CALL vn.ticketComponentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?)';
Self.componentUpdate = async (ticketFk, data, ctx) => {
let userId = ctx.req.accessToken.userId;
let hasDeliveryRole = await Self.app.models.Account.hasRole(userId, 'delivery');
if (!hasDeliveryRole)
data.hasToBeUnrouted = true;
let query = 'CALL vn.ticketComponentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
let res = await Self.rawSql(query, [
ticketFk,
data.clientFk,
data.agencyModeFk,
data.addressFk,
data.warehouseFk,
data.shipped,
data.landed,
data.isDeleted,
data.hasToBeUnrouted,
data.option
]);
return res;

View File

@ -8,9 +8,11 @@ describe('ticket componentUpdate()', () => {
warehouseFk: 1,
shipped: Date.now(),
landed: Date.now(),
hasToBeUnrouted: true,
option: 1
};
app.models.Ticket.componentUpdate(1, data)
let ctx = {req: {accessToken: {userId: 101}}};
app.models.Ticket.componentUpdate(1, data, ctx)
.catch(response => {
expect(response).toEqual(new Error('ER_SIGNAL_EXCEPTION: NO_AGENCY_AVAILABLE'));
done();