2891 - Get ticket problems for every sale #629

Merged
carlosjr merged 7 commits from 2891-ticket_sale_problems into dev 2021-05-27 09:49:44 +00:00
3 changed files with 269 additions and 21 deletions
Showing only changes of commit a103c8d7fc - Show all commits

View File

@ -0,0 +1,180 @@
DROP PROCEDURE IF EXISTS `vn`.`ticket_getProblems`;
DELIMITER $$
$$
CREATE
DEFINER = root@`%` PROCEDURE `vn`.`ticket_getProblems`(IN vTicketFk INT, IN vIsTodayRelative TINYINT(1))
BEGIN
DECLARE vWarehouse INT;
DECLARE vDate DATE;
DECLARE vAvailableCache INT;
DECLARE vDone INT DEFAULT 0;
DECLARE vCursor CURSOR FOR
SELECT DISTINCT tt.warehouseFk, IF(vIsTodayRelative, CURDATE(), date(tt.shipped))
FROM tmp.ticket_getProblems tt
WHERE DATE(tt.shipped) BETWEEN CURDATE()
AND TIMESTAMPADD(DAY, IF(vIsTodayRelative, 9.9, 1.9), CURDATE());
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = 1;
IF NOT ISNULL(vTicketFk) THEN
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_getProblems;
CREATE TEMPORARY TABLE tmp.ticket_getProblems
(INDEX (ticketFk))
ENGINE = MEMORY
SELECT t.id ticketFk, t.clientFk, t.warehouseFk, t.shipped
FROM ticket t
WHERE t.id = vTicketFk;
END IF;
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_problems;
CREATE TEMPORARY TABLE tmp.ticket_problems (
ticketFk INT(11) PRIMARY KEY,
saleFk INT(11),
isFreezed INTEGER(1) DEFAULT 0,
risk DECIMAL(10,2) DEFAULT 0,
hasTicketRequest INTEGER(1) DEFAULT 0,
isAvailable INTEGER(1) DEFAULT 1,
itemShortage VARCHAR(250),
isTaxDataChecked INTEGER(1) DEFAULT 1,
itemDelay VARCHAR(250)
) ENGINE = MEMORY;
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_list;
CREATE TEMPORARY TABLE tmp.ticket_list
(PRIMARY KEY (ticketFk))
ENGINE = MEMORY
SELECT tp.ticketFk, c.id clientFk
FROM tmp.ticket_getProblems tp
JOIN vn.client c ON c.id = tp.clientFk;
INSERT INTO tmp.ticket_problems(ticketFk, isFreezed)
SELECT DISTINCT tl.ticketFk, 1
FROM tmp.ticket_list tl
JOIN vn.client c ON c.id = tl.clientFk
WHERE c.isFreezed;
DROP TEMPORARY TABLE IF EXISTS tmp.clientGetDebt;
CREATE TEMPORARY TABLE tmp.clientGetDebt
(PRIMARY KEY (clientFk))
ENGINE = MEMORY
SELECT DISTINCT clientFk
FROM tmp.ticket_list;
CALL clientGetDebt(CURDATE());
INSERT INTO tmp.ticket_problems(ticketFk, risk)
SELECT DISTINCT tl.ticketFk, r.risk
FROM tmp.ticket_list tl
JOIN vn.ticket t ON t.id = tl.ticketFk
JOIN vn.agencyMode a ON t.agencyModeFk = a.id
JOIN tmp.risk r ON r.clientFk = t.clientFk
JOIN vn.client c ON c.id = t.clientFk
JOIN vn.clientConfig cc
WHERE r.risk - cc.riskTolerance > c.credit + 10
AND a.isRiskFree = FALSE
ON DUPLICATE KEY UPDATE
risk = r.risk;
INSERT INTO tmp.ticket_problems(ticketFk, hasTicketRequest, saleFk)
SELECT DISTINCT tl.ticketFk, 1, tr.saleFk
FROM tmp.ticket_list tl
JOIN vn.ticketRequest tr ON tr.ticketFk = tl.ticketFk
WHERE tr.isOK IS NULL AND tr.saleFk IS NOT NULL
ON DUPLICATE KEY UPDATE
hasTicketRequest = 1, saleFk = tr.saleFk;
OPEN vCursor;
WHILE NOT vDone
DO
FETCH vCursor INTO vWarehouse, vDate;
CALL cache.available_refresh(vAvailableCache, FALSE, vWarehouse, vDate);
INSERT INTO tmp.ticket_problems(ticketFk, isAvailable, saleFk)
SELECT tl.ticketFk, 0, s.id
FROM tmp.ticket_list tl
JOIN vn.ticket t ON t.id = tl.ticketFk
JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn.item i ON i.id = s.itemFk
JOIN vn.itemType it on it.id = i.typeFk
LEFT JOIN cache.available av ON av.item_id = i.id
AND av.calc_id = vAvailableCache
WHERE date(t.shipped) = vDate
AND it.categoryFk != 6
AND IFNULL(av.available, 0) < 0
AND s.isPicked = FALSE
AND NOT i.generic
AND vWarehouse = t.warehouseFk
GROUP BY tl.ticketFk
ON DUPLICATE KEY UPDATE
isAvailable = 0, saleFk = s.id;
INSERT INTO tmp.ticket_problems(ticketFk, itemShortage, saleFk)
SELECT ticketFk, problem, saleFk
FROM (
SELECT tl.ticketFk, CONCAT('F: ',GROUP_CONCAT(i.id, ' ', i.longName, ' ')) problem, s.id AS saleFk
FROM tmp.ticket_list tl
JOIN vn.ticket t ON t.id = tl.ticketFk
JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn.item i ON i.id = s.itemFk
JOIN vn.itemType it on it.id = i.typeFk
LEFT JOIN vn.itemShelvingStock_byWarehouse issw ON issw.itemFk = i.id AND issw.warehouseFk = t.warehouseFk
LEFT JOIN cache.available av ON av.item_id = i.id AND av.calc_id = vAvailableCache
WHERE IFNULL(av.available, 0) < 0
AND s.quantity > IFNULL(issw.visible, 0)
AND s.quantity > 0
AND s.isPicked = FALSE
AND s.reserved = FALSE
AND it.categoryFk != 6
AND IF(vIsTodayRelative, TRUE, date(t.shipped) = vDate)
AND NOT i.generic
AND CURDATE() = vDate
AND t.warehouseFk = vWarehouse
GROUP BY tl.ticketFk) sub
ON DUPLICATE KEY UPDATE
itemShortage = sub.problem, saleFk = sub.saleFk;
INSERT INTO tmp.ticket_problems(ticketFk, itemDelay, saleFk)
SELECT ticketFk, problem, saleFk
FROM (
SELECT tl.ticketFk, GROUP_CONCAT('I: ',i.id, ' ', i.longName, ' ') problem, s.id AS saleFk
FROM tmp.ticket_list tl
JOIN vn.ticket t ON t.id = tl.ticketFk
JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn.item i ON i.id = s.itemFk
JOIN vn.itemType it on it.id = i.typeFk
LEFT JOIN vn.itemShelvingStock_byWarehouse issw ON issw.itemFk = i.id AND issw.warehouseFk = t.warehouseFk
WHERE s.quantity > IFNULL(issw.visible, 0)
AND s.quantity > 0
AND s.isPicked = FALSE
AND s.reserved = FALSE
AND it.categoryFk != 6
AND IF(vIsTodayRelative, TRUE, date(t.shipped) = vDate)
AND NOT i.generic
AND CURDATE() = vDate
AND t.warehouseFk = vWarehouse
GROUP BY tl.ticketFk) sub
ON DUPLICATE KEY UPDATE
itemDelay = sub.problem, saleFk = sub.saleFk;
END WHILE;
CLOSE vCursor;
INSERT INTO tmp.ticket_problems(ticketFk, isTaxDataChecked)
SELECT DISTINCT tl.ticketFk, FALSE
FROM tmp.ticket_list tl
JOIN vn.client c ON c.id = tl.clientFk
WHERE c.isTaxDataChecked= FALSE
ON DUPLICATE KEY UPDATE
isTaxDataChecked = FALSE;
DROP TEMPORARY TABLE
tmp.clientGetDebt,
tmp.ticket_list;
SELECT * FROM tmp.ticket_problems;
END;;$$
DELIMITER ;

View File

@ -1,12 +1,13 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('getSales', { Self.remoteMethod('getSales', {
description: 'New filter', description: 'New filter',
accessType: 'READ', accessType: 'READ',
accepts: [{ accepts: [{
arg: 'ticketFk', arg: 'id',
type: 'number', type: 'number',
required: true, required: true,
description: 'ticket id', description: 'The ticket id',
http: {source: 'path'} http: {source: 'path'}
}], }],
returns: { returns: {
@ -14,23 +15,18 @@ module.exports = Self => {
root: true root: true
}, },
http: { http: {
path: `/:ticketFk/getSales`, path: `/:id/getSales`,
verb: 'get' verb: 'get'
} }
}); });
Self.getSales = async ticketFk => { Self.getSales = async id => {
let query = `CALL vn.ticketGetVisibleAvailable(?)`; // implementar transacciones
let [lines] = await Self.rawSql(query, [ticketFk]); const models = Self.app.models;
let ids = []; const sales = await models.Sale.find({
let salesIds = []; include: {
relation: 'item',
for (line of lines) { scope: {
ids.push(line.itemFk);
salesIds.push(line.id);
}
let filter = {
fields: [ fields: [
'id', 'id',
'name', 'name',
@ -45,7 +41,66 @@ module.exports = Self => {
'tag9', 'tag9',
'value9', 'value9',
'tag10', 'tag10',
'value10'], 'value10'
]
}
},
where: {ticketFk: id}
});
// Get items available
const query = `CALL ticketGetVisibleAvailable(?)`;
const [salesAvailable] = await Self.rawSql(query, [id]);
const itemAvailable = new Map();
for (let sale of salesAvailable)
itemAvailable.set(sale.itemFk, sale.available);
// Get claimed sales
const saleIds = sales.map(sale => sale.id);
const claims = await models.ClaimBeginning.find({
fields: ['claimFk', 'saleFk'],
where: {saleFk: {inq: saleIds}},
});
const claimedSales = new Map();
for (let claim of claims)
claimedSales.set(claim.saleFk, claim);
// Get problems
const problemsQuery = `CALL ticket_getProblems(?, FALSE)`;
const [problems] = await Self.rawSql(problemsQuery, [id]);
const saleProblems = new Map();
for (let problem of problems)
saleProblems.set(problem.saleFk, problem);
console.log(problems);
for (let sale of sales) {
const problems = saleProblems.get(sale.id);
sale.available = itemAvailable.get(sale.itemFk);
sale.claim = claimedSales.get(sale.id);
if (problems) {
sale.isAvailable = problems.isAvailable;
sale.isFreezed = problems.isFreezed;
}
}
return sales;
/* let query = `CALL vn.ticketGetVisibleAvailable(?)`;
let [lines] = await Self.rawSql(query, [ticketFk]);
let ids = [];
let salesIds = [];
for (line of lines) {
ids.push(line.itemFk);
salesIds.push(line.id);
}
let filter = {
fields: ['id', 'name', 'tag5', 'value5', 'tag6', 'value6', 'tag7', 'value7', 'tag8', 'value8', 'tag9', 'value9', 'tag10', 'value10'],
where: {id: {inq: ids}} where: {id: {inq: ids}}
}; };
let items = await Self.app.models.Item.find(filter); let items = await Self.app.models.Item.find(filter);
@ -68,6 +123,6 @@ module.exports = Self => {
line.item = map[line.itemFk]; line.item = map[line.itemFk];
line.claim = claimMap[line.id]; line.claim = claimMap[line.id];
} }
return lines; return lines; */
}; };
}; };

View File

@ -91,6 +91,19 @@
icon="icon-reserve" icon="icon-reserve"
vn-tooltip="{{::$ctrl.$t('Reserved')}}"> vn-tooltip="{{::$ctrl.$t('Reserved')}}">
</vn-icon> </vn-icon>
<vn-icon
ng-show="::sale.isAvailable === 0"
translate-attr="{title: 'Not available'}"
class="bright"
vn-tooltip="Not available"
icon="icon-unavailable">
</vn-icon>
<vn-icon
ng-show="::sale.isFreezed"
translate-attr="{title: 'Client frozen'}"
class="bright"
icon="icon-frozen">
</vn-icon>
</vn-td> </vn-td>
<vn-td shrink> <vn-td shrink>
<img <img