Skip to content

Commit

Permalink
calling cmm data calculating for new stock movement
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremielodi committed Sep 1, 2020
1 parent 1a3e12f commit 4a314a5
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 103 deletions.
190 changes: 114 additions & 76 deletions server/controllers/stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,85 +52,94 @@ exports.getStockTransfers = getStockTransfers;
* POST /stock/lots
* Create a new stock lots entry
*/
function createStock(req, res, next) {
const params = req.body;
const documentUuid = uuid();
async function createStock(req, res, next) {

Fiscal.lookupFiscalYearByDate(params.date)
.then(result => {
const periodId = result.id;

const transaction = db.transaction();
const document = {
uuid : documentUuid,
date : new Date(params.date),
user : req.session.user.id,
depot_uuid : params.depot_uuid,
try {
const params = req.body;
const documentUuid = uuid();

const period = await Fiscal.lookupFiscalYearByDate(params.date);
const periodId = period.id;

const transaction = db.transaction();
const transaction2 = db.transaction();

const document = {
uuid : documentUuid,
date : new Date(params.date),
user : req.session.user.id,
depot_uuid : params.depot_uuid,
flux_id : params.flux_id,
description : params.description,
};

// prepare lot insertion query
const createLotQuery = 'INSERT INTO lot SET ?';

// prepare movement insertion query
const createMovementQuery = 'INSERT INTO stock_movement SET ?';

// aggregate data for CMM calculation
const recomputeCMMDataQuery = `CALL computeStockQuantityByLotUuid(?, ?, ?);`;

params.lots.forEach(lot => {
// parse the expiration date
const date = new Date(lot.expiration_date);

// the lot object to insert
const createLotObject = {
uuid : db.bid(uuid()),
label : lot.label,
initial_quantity : lot.quantity,
quantity : lot.quantity,
unit_cost : lot.unit_cost,
expiration_date : date,
inventory_uuid : db.bid(lot.inventory_uuid),
origin_uuid : db.bid(lot.origin_uuid),
delay : 0,
};

// the movement object to insert
const createMovementObject = {
uuid : db.bid(uuid()),
lot_uuid : createLotObject.uuid,
depot_uuid : db.bid(document.depot_uuid),
document_uuid : db.bid(documentUuid),
flux_id : params.flux_id,
description : params.description,
date : document.date,
quantity : lot.quantity,
unit_cost : lot.unit_cost,
is_exit : 0,
user_id : document.user,
description : document.description,
period_id : periodId,
};

// prepare lot insertion query
const createLotQuery = 'INSERT INTO lot SET ?';

// prepare movement insertion query
const createMovementQuery = 'INSERT INTO stock_movement SET ?';

params.lots.forEach((lot) => {
// parse the expiration date
const date = new Date(lot.expiration_date);

// the lot object to insert
const createLotObject = {
uuid : db.bid(uuid()),
label : lot.label,
initial_quantity : lot.quantity,
quantity : lot.quantity,
unit_cost : lot.unit_cost,
expiration_date : date,
inventory_uuid : db.bid(lot.inventory_uuid),
origin_uuid : db.bid(lot.origin_uuid),
delay : 0,
};

// the movement object to insert
const createMovementObject = {
uuid : db.bid(uuid()),
lot_uuid : createLotObject.uuid,
depot_uuid : db.bid(document.depot_uuid),
document_uuid : db.bid(documentUuid),
flux_id : params.flux_id,
date : document.date,
quantity : lot.quantity,
unit_cost : lot.unit_cost,
is_exit : 0,
user_id : document.user,
description : document.description,
period_id : periodId,
};

// adding a lot insertion query into the transaction
transaction.addQuery(createLotQuery, [createLotObject]);

// adding a movement insertion query into the transaction
transaction.addQuery(createMovementQuery, [createMovementObject]);
});
// adding a lot insertion query into the transaction
transaction.addQuery(createLotQuery, [createLotObject]);

const isExit = 0;
const postingParams = [db.bid(documentUuid), isExit, req.session.project.id, req.session.enterprise.currency_id];
// adding a movement insertion query into the transaction
transaction.addQuery(createMovementQuery, [createMovementObject]);

if (req.session.enterprise.settings.enable_auto_stock_accounting) {
transaction.addQuery('CALL PostStockMovement(?)', [postingParams]);
}
transaction2.addQuery(recomputeCMMDataQuery, [
document.date,
createLotObject.uuid,
db.bid(document.depot_uuid)]);
});
const isExit = 0;
const postingParams = [db.bid(documentUuid), isExit, req.session.project.id, req.session.enterprise.currency_id];

// execute all operations as one transaction
return transaction.execute();
})
.then(() => {
res.status(201).json({ uuid : documentUuid });
})
.catch(next)
.done();
if (req.session.enterprise.settings.enable_auto_stock_accounting) {
transaction.addQuery('CALL PostStockMovement(?)', [postingParams]);
}

// execute all operations as one transaction
await transaction.execute();
await transaction2.execute();
res.status(201).json({ uuid : documentUuid });
} catch (ex) {
next(ex);
}
}

/**
Expand All @@ -141,6 +150,7 @@ function createStock(req, res, next) {
*/
async function insertNewStock(session, params, originTable = 'integration') {
const transaction = db.transaction();
const transaction2 = db.transaction();
const identifier = uuid();
const documentUuid = uuid();

Expand All @@ -157,6 +167,8 @@ async function insertNewStock(session, params, originTable = 'integration') {

transaction.addQuery(sql, [integration]);

const recomputeCMMDataQuery = `CALL computeStockQuantityByLotUuid(?, ?, ?);`;

params.lots.forEach((lot) => {
const lotUuid = uuid();

Expand Down Expand Up @@ -188,6 +200,12 @@ async function insertNewStock(session, params, originTable = 'integration') {
description : params.movement.description,
period_id : periodId,
});

// aggregate data for CMM calculation
transaction2.addQuery(recomputeCMMDataQuery, [
new Date(params.movement.date),
db.bid(lotUuid),
db.bid(params.movement.depot_uuid)]);
});

const postingParams = [
Expand All @@ -199,6 +217,7 @@ async function insertNewStock(session, params, originTable = 'integration') {
}

await transaction.execute();
await transaction2.execute();
return documentUuid;
}

Expand Down Expand Up @@ -368,11 +387,12 @@ function createMovement(req, res, next) {
* @function normalMovement
* @description there are only lines for IN or OUT
*/
function normalMovement(document, params, metadata) {
async function normalMovement(document, params, metadata) {
let createMovementQuery;
let createMovementObject;

const transaction = db.transaction();
const transaction2 = db.transaction();
const parameters = params;

const isDistributable = !!(
Expand All @@ -382,6 +402,9 @@ function normalMovement(document, params, metadata) {
parameters.entity_uuid = parameters.entity_uuid ? db.bid(parameters.entity_uuid) : null;
parameters.invoice_uuid = parameters.invoice_uuid ? db.bid(parameters.invoice_uuid) : null;

// aggregate data for CMM calculation
const recomputeCMMDataQuery = `CALL computeStockQuantityByLotUuid(?, ?, ?);`;

parameters.lots.forEach((lot) => {
createMovementQuery = 'INSERT INTO stock_movement SET ?';
createMovementObject = {
Expand All @@ -404,6 +427,10 @@ function normalMovement(document, params, metadata) {
// transaction - add movement
transaction.addQuery(createMovementQuery, [createMovementObject]);

transaction2.addQuery(recomputeCMMDataQuery, [
document.date,
db.bid(lot.uuid),
db.bid(parameters.depot_uuid)]);
// track distribution to patient and service
if (isDistributable) {
const consumptionParams = [
Expand All @@ -421,17 +448,20 @@ function normalMovement(document, params, metadata) {
transaction.addQuery('CALL PostStockMovement(?, ?, ?, ?);', postStockParameters);
}

return transaction.execute();
const result = await transaction.execute();
await transaction2.execute();
return result;
}

/**
* @function depotMovement
* @description movement between depots
*/
function depotMovement(document, params) {
async function depotMovement(document, params) {

let isWarehouse;
const transaction = db.transaction();
const transaction2 = db.transaction();
const parameters = params;
const isExit = parameters.isExit ? 1 : 0;

Expand All @@ -443,6 +473,7 @@ function depotMovement(document, params) {
const entityUuid = isExit ? db.bid(parameters.to_depot) : db.bid(parameters.from_depot);
const fluxId = isExit ? core.flux.TO_OTHER_DEPOT : core.flux.FROM_OTHER_DEPOT;

const recomputeCMMDataQuery = `CALL computeStockQuantityByLotUuid(?, ?, ?);`;
parameters.lots.forEach((lot) => {
record = {
depot_uuid : depotUuid,
Expand All @@ -464,6 +495,11 @@ function depotMovement(document, params) {

isWarehouse = !!(parameters.from_depot_is_warehouse);

transaction2.addQuery(recomputeCMMDataQuery, [
document.date,
db.bid(lot.uuid),
depotUuid]);

// track distribution to other depot from a warehouse
if (record.is_exit && isWarehouse) {
const consumptionParams = [
Expand All @@ -473,7 +509,9 @@ function depotMovement(document, params) {
}
});

return transaction.execute();
const result = await transaction.execute();
await transaction2.execute();
return result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion server/models/admin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ CALL zComputeAllInventoryStockQuantities(startDate, depotUuid)
DESCRIPTION
Recomputes the quantity in stock for all inventory items for a given depot.
If _depot_uuid is NULL, Recomputation will be done for every depots
*/
DROP PROCEDURE IF EXISTS zComputeAllInventoryStockQuantities $$
CREATE PROCEDURE zComputeAllInventoryStockQuantities (
Expand Down
22 changes: 22 additions & 0 deletions server/models/migrations/next/migrate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,25 @@ CREATE TABLE `lot_tag` (
FOREIGN KEY (`lot_uuid`) REFERENCES `lot` (`uuid`),
FOREIGN KEY (`tag_uuid`) REFERENCES `tags` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;


/*
* @author: jeremielodi
on @jniles PR https://github.com/IMA-WorldHealth/bhima/pull/4866
* @date: 2020-08-31
*/
DROP TABLE IF EXISTS `stock_movement_status`;
CREATE TABLE `stock_movement_status` (
`uuid` BINARY(16),
`start_date` DATE,
`end_date` DATE,
`quantity` DECIMAL(19,4),
`in_quantity` DECIMAL(19,4),
`out_quantity` DECIMAL(19,4),
`inventory_uuid` BINARY(16),
`depot_uuid` BINARY(16),
PRIMARY KEY(`uuid`),
KEY `depot_uuid` (`depot_uuid`),
KEY `inventory_uuid` (`inventory_uuid`)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;

Loading

0 comments on commit 4a314a5

Please sign in to comment.