From f916509b86ec9b31fb31ee8b9d5800bb6bbb7398 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 1 Oct 2024 11:30:34 +0200 Subject: [PATCH] fix(StockBought): revert bad merge master to test --- .../vn/procedures/stockBought_calculate.sql | 62 ++++++++++++++++ .../11115-turquoiseRose/00-firstScript.sql | 30 ++++++++ .../methods/stock-bought/getStockBought.js | 58 +++++++++++++++ .../stock-bought/getStockBoughtDetail.js | 74 +++++++++++++++++++ modules/entry/back/model-config.json | 5 +- modules/entry/back/models/stock-bought.js | 10 +++ modules/entry/back/models/stock-bought.json | 34 +++++++++ 7 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 db/routines/vn/procedures/stockBought_calculate.sql create mode 100644 db/versions/11115-turquoiseRose/00-firstScript.sql create mode 100644 modules/entry/back/methods/stock-bought/getStockBought.js create mode 100644 modules/entry/back/methods/stock-bought/getStockBoughtDetail.js create mode 100644 modules/entry/back/models/stock-bought.js create mode 100644 modules/entry/back/models/stock-bought.json diff --git a/db/routines/vn/procedures/stockBought_calculate.sql b/db/routines/vn/procedures/stockBought_calculate.sql new file mode 100644 index 000000000..8b2a32e5d --- /dev/null +++ b/db/routines/vn/procedures/stockBought_calculate.sql @@ -0,0 +1,62 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`stockBought_calculate`( + vDated DATE +) +proc: BEGIN +/** + * Calculate the stock of the auction warehouse from the inventory date to vDated + * without taking into account the outputs of the same day vDated + * + * @param vDated Date to calculate the stock. + */ + IF vDated < util.VN_CURDATE() THEN + LEAVE proc; + END IF; + + CREATE OR REPLACE TEMPORARY TABLE tStockBought + SELECT workerFk, reserve + FROM stockBought + WHERE dated = vDated + AND reserve; + + DELETE FROM stockBought WHERE dated = vDated; + + CALL item_calculateStock(vDated); + + INSERT INTO stockBought(workerFk, bought, dated) + SELECT it.workerFk, + ROUND(SUM( + (ti.quantity / b.packing) * + buy_getVolume(b.id) + ) / vc.palletM3 / 1000000, 1) bought, + vDated + FROM itemType it + JOIN item i ON i.typeFk = it.id + LEFT JOIN tmp.item ti ON ti.itemFk = i.id + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN warehouse wh ON wh.code = 'VNH' + JOIN tmp.buyUltimate bu ON bu.itemFk = i.id + AND bu.warehouseFk = wh.id + JOIN buy b ON b.id = bu.buyFk + JOIN volumeConfig vc + WHERE ic.display + GROUP BY it.workerFk + HAVING bought; + + UPDATE stockBought s + JOIN tStockBought ts ON ts.workerFk = s.workerFk + SET s.reserve = ts.reserve + WHERE s.dated = vDated; + + INSERT INTO stockBought (workerFk, reserve, dated) + SELECT ts.workerFk, ts.reserve, vDated + FROM tStockBought ts + WHERE ts.workerFk NOT IN ( + SELECT workerFk + FROM stockBought + WHERE dated = vDated + ); + + DROP TEMPORARY TABLE tStockBought, tmp.item, tmp.buyUltimate; +END$$ +DELIMITER ; diff --git a/db/versions/11115-turquoiseRose/00-firstScript.sql b/db/versions/11115-turquoiseRose/00-firstScript.sql new file mode 100644 index 000000000..3982936fc --- /dev/null +++ b/db/versions/11115-turquoiseRose/00-firstScript.sql @@ -0,0 +1,30 @@ +-- Place your SQL code here +-- vn.stockBought definition + +CREATE TABLE IF NOT EXISTS vn.stockBought ( + id INT UNSIGNED auto_increment NOT NULL, + workerFk int(10) unsigned NOT NULL, + bought decimal(10,2) NOT NULL COMMENT 'purchase volume in m3 for the day', + reserve decimal(10,2) NULL COMMENT 'reserved volume in m3 for the day', + dated DATE NOT NULL DEFAULT current_timestamp(), + CONSTRAINT stockBought_pk PRIMARY KEY (id), + CONSTRAINT stockBought_unique UNIQUE KEY (workerFk,dated), + CONSTRAINT stockBought_worker_FK FOREIGN KEY (workerFk) REFERENCES vn.worker(id) +) +ENGINE=InnoDB +DEFAULT CHARSET=utf8mb3 +COLLATE=utf8mb3_unicode_ci; + + +INSERT IGNORE vn.stockBought (workerFk, bought, reserve, dated) + SELECT userFk, SUM(buyed), SUM(IFNULL(reserved,0)), dated + FROM vn.stockBuyed + WHERE userFk IS NOT NULL + AND buyed IS NOT NULL + GROUP BY userFk, dated; + +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('StockBought','*','READ','ALLOW','ROLE','buyer'), + ('StockBought','*','WRITE','ALLOW','ROLE','buyer'), + ('Buyer','*','READ','ALLOW','ROLE','buyer'); + diff --git a/modules/entry/back/methods/stock-bought/getStockBought.js b/modules/entry/back/methods/stock-bought/getStockBought.js new file mode 100644 index 000000000..c1f99c496 --- /dev/null +++ b/modules/entry/back/methods/stock-bought/getStockBought.js @@ -0,0 +1,58 @@ +module.exports = Self => { + Self.remoteMethod('getStockBought', { + description: 'Returns the stock bought for a given date', + accessType: 'READ', + accepts: [{ + arg: 'workerFk', + type: 'number', + description: 'The id for a buyer', + }, + { + arg: 'dated', + type: 'date', + description: 'The date to filter', + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/getStockBought`, + verb: 'GET' + } + }); + + Self.getStockBought = async(workerFk, dated = Date.vnNew()) => { + const models = Self.app.models; + const today = Date.vnNew(); + dated.setHours(0, 0, 0, 0); + today.setHours(0, 0, 0, 0); + + await models.StockBought.rawSql(`CALL vn.stockBought_calculate(?)`, [dated]); + + const filter = { + where: {dated}, + include: [ + { + fields: ['workerFk', 'reserve', 'bought'], + relation: 'worker', + scope: { + include: [ + { + relation: 'user', + scope: { + fields: ['id', 'name'] + } + } + ] + } + } + ] + }; + + if (workerFk) filter.where.workerFk = workerFk; + + return models.StockBought.find(filter); + }; +}; diff --git a/modules/entry/back/methods/stock-bought/getStockBoughtDetail.js b/modules/entry/back/methods/stock-bought/getStockBoughtDetail.js new file mode 100644 index 000000000..3e040d0d3 --- /dev/null +++ b/modules/entry/back/methods/stock-bought/getStockBoughtDetail.js @@ -0,0 +1,74 @@ +module.exports = Self => { + Self.remoteMethod('getStockBoughtDetail', { + description: 'Returns the detail of stock bought for a given date and a worker', + accessType: 'READ', + accepts: [{ + arg: 'workerFk', + type: 'number', + description: 'The worker to filter', + required: true, + }, { + arg: 'dated', + type: 'string', + description: 'The date to filter', + required: true, + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/getStockBoughtDetail`, + verb: 'GET' + } + }); + + Self.getStockBoughtDetail = async(workerFk, dated) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + let result; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + await models.StockBought.rawSql(`CALL vn.item_calculateStock(?)`, [dated], myOptions); + result = await Self.rawSql( + `SELECT b.entryFk entryFk, + i.id itemFk, + i.name itemName, + ti.quantity, + (ac.conversionCoefficient * (ti.quantity / b.packing) * buy_getVolume(b.id)) + / (vc.trolleyM3 * 1000000) volume, + b.packagingFk packagingFk, + b.packing + FROM tmp.item ti + JOIN item i ON i.id = ti.itemFk + JOIN itemType it ON i.typeFk = it.id + JOIN itemCategory ic ON ic.id = it.categoryFk + JOIN worker w ON w.id = it.workerFk + JOIN auctionConfig ac + JOIN tmp.buyUltimate bu ON bu.itemFk = i.id + AND bu.warehouseFk = ac.warehouseFk + JOIN buy b ON b.id = bu.buyFk + JOIN volumeConfig vc + WHERE ic.display + AND w.id = ?`, + [workerFk], myOptions + ); + await Self.rawSql(`DROP TEMPORARY TABLE tmp.item, tmp.buyUltimate;`, [], myOptions); + if (tx) await tx.commit(); + return result; + } catch (e) { + await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/entry/back/model-config.json b/modules/entry/back/model-config.json index dc7fd86be..5c45b6e07 100644 --- a/modules/entry/back/model-config.json +++ b/modules/entry/back/model-config.json @@ -25,5 +25,8 @@ }, "EntryType": { "dataSource": "vn" + }, + "StockBought": { + "dataSource": "vn" } -} \ No newline at end of file +} diff --git a/modules/entry/back/models/stock-bought.js b/modules/entry/back/models/stock-bought.js new file mode 100644 index 000000000..ae52e7654 --- /dev/null +++ b/modules/entry/back/models/stock-bought.js @@ -0,0 +1,10 @@ +const UserError = require('vn-loopback/util/user-error'); +module.exports = Self => { + require('../methods/stock-bought/getStockBought')(Self); + require('../methods/stock-bought/getStockBoughtDetail')(Self); + Self.rewriteDbError(function(err) { + if (err.code === 'ER_DUP_ENTRY') + return new UserError(`This buyer has already made a reservation for this date`); + return err; + }); +}; diff --git a/modules/entry/back/models/stock-bought.json b/modules/entry/back/models/stock-bought.json new file mode 100644 index 000000000..18c9f0347 --- /dev/null +++ b/modules/entry/back/models/stock-bought.json @@ -0,0 +1,34 @@ +{ + "name": "StockBought", + "base": "VnModel", + "options": { + "mysql": { + "table": "stockBought" + } + }, + "properties": { + "id": { + "type": "number", + "id": true + }, + "workerFk": { + "type": "number" + }, + "bought": { + "type": "number" + }, + "reserve": { + "type": "number" + }, + "dated": { + "type": "date" + } + }, + "relations": { + "worker": { + "type": "belongsTo", + "model": "Worker", + "foreignKey": "workerFk" + } + } +}