diff --git a/modules/order/back/methods/order/getItemTypeAvailable.js b/modules/order/back/methods/order/getItemTypeAvailable.js index 56f6a8c0e..906095f41 100644 --- a/modules/order/back/methods/order/getItemTypeAvailable.js +++ b/modules/order/back/methods/order/getItemTypeAvailable.js @@ -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]; };