Merge pull request 'added catalog calculate join to show only available item types' (#708) from 2954-order_catalog_types into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #708
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Joan Sanchez 2021-08-06 11:26:54 +00:00
commit d5ba0ced0b
1 changed files with 39 additions and 12 deletions

View File

@ -28,30 +28,57 @@ module.exports = Self => {
});
Self.getItemTypeAvailable = async(orderId, itemCategoryId) => {
let stmts = [];
const stmts = [];
let stmt;
let order = await app.models.Order.findById(orderId);
const order = await app.models.Order.findById(orderId);
stmt = new ParameterizedSQL('call vn.available_calc(?, ?, ?)', [
order.landed,
order.addressFk,
order.agencyModeFk
]);
stmts.push(stmt);
stmt = new ParameterizedSQL(`
SELECT it.id, it.name, ic.name categoryName
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.item');
stmt = new ParameterizedSQL(
`CREATE TEMPORARY TABLE tmp.item
(PRIMARY KEY (itemFk)) ENGINE = MEMORY
SELECT DISTINCT
i.id AS itemFk,
it.id AS typeFk,
it.name,
ic.name AS categoryName
FROM tmp.availableCalc ac
JOIN cache.available a ON a.calc_id = ac.calcFk
JOIN item i ON i.id = a.item_id
JOIN itemType it ON it.id = i.typeFk
JOIN itemCategory ic ON ic.id = it.categoryFk
WHERE it.categoryFk = ?
GROUP BY it.id`, [itemCategoryId]
JOIN vn.item i ON i.id = a.item_id
JOIN vn.itemType it ON it.id = i.typeFk
JOIN vn.itemCategory ic ON ic.id = it.categoryFk
WHERE it.categoryFk = ?`, [itemCategoryId]
);
let categoriesIndex = stmts.push(stmt) - 1;
stmts.push(stmt);
let sql = ParameterizedSQL.join(stmts, ';');
let result = await Self.rawStmt(sql);
stmt = new ParameterizedSQL(
'CALL vn.catalog_calculate(?, ?, ?)', [
order.landed,
order.addressFk,
order.agencyModeFk,
]
);
stmts.push(stmt);
stmt = new ParameterizedSQL(`
SELECT i.typeFk AS id, i.name, i.categoryName
FROM tmp.item i
JOIN tmp.ticketCalculateItem tci ON tci.itemFk = i.itemFk
GROUP BY i.typeFk`
);
const categoriesIndex = stmts.push(stmt) - 1;
stmts.push('DROP TEMPORARY TABLE tmp.item');
const sql = ParameterizedSQL.join(stmts, ';');
const result = await Self.rawStmt(sql);
return result[categoriesIndex];
};