diff --git a/modules/ticket/back/methods/sale-tracking/delete.js b/modules/ticket/back/methods/sale-tracking/delete.js index 000c52621..30956228b 100644 --- a/modules/ticket/back/methods/sale-tracking/delete.js +++ b/modules/ticket/back/methods/sale-tracking/delete.js @@ -25,6 +25,7 @@ module.exports = Self => { }); Self.delete = async(saleFk, stateCode, options) => { + const models = Self.app.models; const myOptions = {}; let tx; @@ -37,15 +38,27 @@ module.exports = Self => { } try { - const result = await Self.rawSql(`CALL vn.saleTracking_del(?, ?)`, - [ - saleFk, - stateCode, - ], myOptions); + const itemShelvingSales = await models.ItemShelvingSale.find({where: {saleFk: saleFk}}, myOptions); + for (let itemShelvingSale of itemShelvingSales) + await itemShelvingSale.destroy(myOptions); + + const state = await models.State.findOne({ + where: {code: stateCode} + }, myOptions); + + const filter = { + where: { + saleFk: saleFk, + stateFk: state.id + } + }; + const saleTrackings = await models.SaleTracking.find(filter, myOptions); + for (let saleTracking of saleTrackings) + await saleTracking.destroy(myOptions); if (tx) await tx.commit(); - return result; + return true; } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/modules/ticket/back/methods/sale-tracking/new.js b/modules/ticket/back/methods/sale-tracking/new.js index ec5951d7d..2be5bd86e 100644 --- a/modules/ticket/back/methods/sale-tracking/new.js +++ b/modules/ticket/back/methods/sale-tracking/new.js @@ -33,6 +33,7 @@ module.exports = Self => { }); Self.new = async(ctx, saleFk, isChecked, quantity, stateCode, options) => { + const models = Self.app.models; const userId = ctx.req.accessToken.userId; const myOptions = {}; let tx; @@ -45,19 +46,42 @@ module.exports = Self => { myOptions.transaction = tx; } try { - const result = await Self.rawSql(`CALL vn.saleTracking_new(?, ?, ?, ?, ?, ?)`, - [ - saleFk, - isChecked, - quantity, - userId, - stateCode, - null - ], myOptions); + const state = await models.State.findOne({ + where: {code: stateCode} + }, myOptions); + + const saleTracking = await models.SaleTracking.findOne({ + where: { + saleFk: saleFk, + stateFk: state.id, + workerFk: userId + } + }, myOptions); + + let newSaleTracking; + if (saleTracking) { + newSaleTracking = await saleTracking.updateAttributes({ + saleFk: saleFk, + stateFk: state.id, + workerFk: userId, + isChecked: isChecked, + originalQuantity: quantity, + isScanned: null + }, myOptions); + } else { + newSaleTracking = await models.SaleTracking.create({ + saleFk: saleFk, + stateFk: state.id, + workerFk: userId, + isChecked: isChecked, + originalQuantity: quantity, + isScanned: null + }); + } if (tx) await tx.commit(); - return result; + return newSaleTracking; } catch (e) { if (tx) await tx.rollback(); throw e;