Merge branch 'test' of https://gitea.verdnatura.es/verdnatura/salix into dev
gitea/salix/dev This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-02-10 14:54:59 +01:00
commit f2fdce11cd
11 changed files with 133 additions and 53 deletions

View File

@ -0,0 +1,19 @@
ALTER TABLE `vn`.`ticket`
ADD COLUMN `zonePrice` DECIMAL(10,2) NULL DEFAULT NULL AFTER `collectionFk`,
ADD COLUMN `zoneBonus` DECIMAL(10,2) NULL DEFAULT NULL AFTER `zonePrice`,
ADD COLUMN `zoneClosure` TIME NULL AFTER `zoneBonus`;
CREATE TABLE vn.`zoneCalcTicket` (
`zoneFk` int(11) NOT NULL PRIMARY KEY,
CONSTRAINT `zoneCalcTicketfk_1` FOREIGN KEY (`zoneFk`) REFERENCES `zone` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DROP EVENT IF EXISTS vn.`zone_doCalc`;
CREATE DEFINER=`root`@`%` EVENT vn.`zone_doCalc`
ON SCHEDULE EVERY 15 SECOND STARTS '2020-01-31 11:32:30'
ON COMPLETION PRESERVE ENABLE
DO CALL util.procNoOverlap('vn.zone_doCalc');
DROP TABLE `vn`.`zoneConfig`;
DROP procedure IF EXISTS vn.`zoneClosure_recalc`;

View File

@ -0,0 +1,56 @@
USE `vn`;
DROP procedure IF EXISTS `zone_doCalc`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `zone_doCalc`()
proc: BEGIN
/**
* Updates ticket fields related with zone
*/
DECLARE vDone BOOL;
DECLARE vTicketFk INT;
DECLARE vShipped DATE;
DECLARE vZoneFk INT;
DECLARE cCur CURSOR FOR
SELECT t.id, t.shipped, t.zoneFk
FROM zoneCalcTicket zct
JOIN ticket t ON t.zoneFk = zct.zoneFk
WHERE shipped >= CURDATE();
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET vDone = TRUE;
OPEN cCur;
myLoop: LOOP
SET vDone = FALSE;
FETCH cCur INTO vTicketFk, vShipped, vZoneFk;
IF vDone THEN
LEAVE myLoop;
END IF;
DROP TEMPORARY TABLE IF EXISTS tmp.zone;
CREATE TEMPORARY TABLE tmp.zone
(INDEX (id))
ENGINE = MEMORY
SELECT vZoneFk id;
CALL zone_getOptionsForShipment(vShipped, TRUE);
UPDATE ticket t
LEFT JOIN tmp.zoneOption zo ON TRUE
SET zonePrice = zo.price, zoneBonus = zo.bonus, zoneClosure = zo.`hour`
WHERE t.id = vTicketFk;
END LOOP;
CLOSE cCur;
DELETE FROM zoneCalcTicket;
END$$
DELIMITER ;

View File

@ -0,0 +1 @@
USE `vn`;

View File

@ -0,0 +1,30 @@
USE `util`;
DROP procedure IF EXISTS `procNoOverlap`;
DELIMITER $$
USE `util`$$
CREATE PROCEDURE `procNoOverlap` (procName VARCHAR(255))
SQL SECURITY INVOKER
proc: BEGIN
/**
* call procedure without overlap
*/
DECLARE vIsChanged BOOL;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
DO RELEASE_LOCK(procName);
RESIGNAL;
END;
IF !GET_LOCK(procName, 0) THEN
LEAVE proc;
END IF;
CALL exec(CONCAT('CALL ', procName));
DO RELEASE_LOCK(procName);
END$$
DELIMITER ;

View File

@ -11,7 +11,7 @@
"Zone": {
"dataSource": "vn"
},
"ZoneClosure": {
"ZoneCalcTicket": {
"dataSource": "vn"
},
"ZoneEvent": {

View File

@ -0,0 +1,24 @@
const app = require('vn-loopback/server/server');
module.exports = Self => {
app.on('started', function() {
let models = ['Zone', 'ZoneEvent', 'ZoneExclusion'];
for (let modelName of models) {
let Model = app.models[modelName];
Model.observe('after save', doCalc);
Model.observe('after delete', doCalc);
}
async function doCalc(ctx) {
try {
await Self.create({zoneFk: ctx.instance.zoneFk || ctx.instance.id});
} catch (err) {
if (err.code != 'ER_DUP_ENTRY')
throw err;
}
}
});
};

View File

@ -1,23 +1,15 @@
{
"name": "ZoneClosure",
"name": "ZoneCalcTicket",
"base": "VnModel",
"options": {
"mysql": {
"table": "zoneClosure"
"table": "zoneCalcTicket"
}
},
"properties": {
"zoneFk": {
"id": true,
"type": "Number"
},
"dated": {
"type": "Date",
"required": true
},
"hour": {
"type": "date",
"required": true
}
},
"relations": {

View File

@ -1,13 +0,0 @@
module.exports = Self => {
Self.doRecalc = async function() {
try {
await Self.rawSql(`
CREATE EVENT zoneClosure_doRecalc
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 15 SECOND
DO CALL zoneClosure_recalc;
`);
} catch (err) {
if (err.code != 'ER_EVENT_ALREADY_EXISTS') throw err;
}
};
};

View File

@ -34,12 +34,4 @@ module.exports = Self => {
}, {
message: `You should mark at least one week day`
});
Self.observe('after save', async function() {
await app.models.ZoneClosure.doRecalc();
});
Self.observe('after delete', async function() {
await app.models.ZoneClosure.doRecalc();
});
};

View File

@ -1,11 +0,0 @@
const app = require('vn-loopback/server/server');
module.exports = Self => {
Self.observe('after save', async function() {
await app.models.ZoneClosure.doRecalc();
});
Self.observe('after delete', async function() {
await app.models.ZoneClosure.doRecalc();
});
};

View File

@ -1,5 +1,3 @@
const app = require('vn-loopback/server/server');
module.exports = Self => {
require('../methods/zone/clone')(Self);
require('../methods/zone/getLeaves')(Self);
@ -9,12 +7,4 @@ module.exports = Self => {
Self.validatesPresenceOf('agencyModeFk', {
message: `Agency cannot be blank`
});
Self.observe('after save', async function() {
await app.models.ZoneClosure.doRecalc();
});
Self.observe('after delete', async function() {
await app.models.ZoneClosure.doRecalc();
});
};