8062-testToMaster_2 #3059
|
@ -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 ;
|
|
@ -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');
|
||||||
|
|
|
@ -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);
|
||||||
|
};
|
||||||
|
};
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
|
@ -25,5 +25,8 @@
|
||||||
},
|
},
|
||||||
"EntryType": {
|
"EntryType": {
|
||||||
"dataSource": "vn"
|
"dataSource": "vn"
|
||||||
|
},
|
||||||
|
"StockBought": {
|
||||||
|
"dataSource": "vn"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
});
|
||||||
|
};
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue