Skip to content

Commit

Permalink
fix(review): Fix the code based oon the review
Browse files Browse the repository at this point in the history
  • Loading branch information
DedrickEnc committed Sep 6, 2017
1 parent c3879fc commit 4dd397a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 114 deletions.
121 changes: 52 additions & 69 deletions server/controllers/stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,72 @@ exports.getStockConsumptionAverage = getStockConsumptionAverage;
function createStock(req, res, next) {
const params = req.body;
const transaction = db.transaction();
const lotResult = processLots(params.lots);
const document = {
uuid : uuid.v4(),
date : new Date(params.date),
user : req.session.user.id,
depot_uuid : params.depot_uuid,
flux_id : params.flux_id
};
const movements = processMovements(document, lotResult.processedLots);

let createLotQuery;
let createMovementQuery;
let createLotObject;
let createMovementObject;
let commonInfos;
let date;

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

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

// the lot object to insert
createLotObject = {
uuid : db.bid(uuid.v4()),
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,
};

// writting all lots in the table lot, this is a transactionnal task
lotResult.mappedLots.forEach((item) => {
transaction.addQuery('CALL CreateLot(?)', [item]);
});
// prepare movement insertion query
createMovementQuery = 'INSERT INTO stock_movement SET ?';

// writting all necessary movement based to the lot entry operation, this is a transcationnal task
movements.forEach((item) => {
transaction.addQuery('CALL CreateStockMovement(?)', [item]);
});
// the movement object to insert
createMovementObject = {
uuid : db.bid(uuid.v4()),
lot_uuid : createLotObject.uuid,
depot_uuid : db.bid(document.depot_uuid),
document_uuid : db.bid(document.uuid),
flux_id : params.flux_id,
date : document.date,
quantity : lot.quantity,
unit_cost : lot.unit_cost,
is_exit : 0,
user_id : document.user,
};

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

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

// An arry of common info, to send to the store procedure in order to insert to the posting journal
const commonInfos = [ db.bid(document.uuid), document.date, req.session.enterprise.id, req.session.project.id, req.session.enterprise.currency_id, req.session.user.id ];
// An arry of common info, to send to the store procedure in order to insert to the posting journal
commonInfos = [ db.bid(document.uuid), document.date, req.session.enterprise.id, req.session.project.id, req.session.enterprise.currency_id, document.user ];

// writting all records relative to the movement in the posting journal table
lotResult.processedLots.forEach(() => {
// writting all records relative to the movement in the posting journal table
transaction.addQuery('CALL PostPurchase(?)', [commonInfos]);
});

// execute all 3 operations as one transaction
// execute all operations as one transaction
transaction.execute()
.then(() => {
res.status(201).json({ uuid : document.uuid });
Expand All @@ -82,60 +119,6 @@ function createStock(req, res, next) {
.done();
}

function processMovements (document, lots) {

const items = lots || [];
const movements = [];

items.forEach((item) => {

const movement = {
uuid : db.bid(uuid.v4()),
lot_uuid : item.uuid,
depot_uuid : db.bid(document.depot_uuid),
document_uuid : db.bid(document.uuid),
flux_id : document.flux_id,
date : document.date,
quantity : item.quantity,
unit_cost : item.unit_cost,
is_exit : 0,
user_id : document.user ,
entity_uuid : null,
description : null
};

movements.push(movement);
});

const filter =
util.take('uuid', 'document_uuid', 'depot_uuid', 'lot_uuid', 'entity_uuid', 'description', 'flux_id', 'date', 'quantity', 'unit_cost', 'is_exit', 'user_id');

// prepare movement items for insertion into database
return _.map(movements, filter);
}

// this function process lot list in order to insert them to the database
function processLots (lots) {
const items = lots || [];

// make sure that lot items have their uuids
items.forEach((item) => {
item.uuid = db.bid(item.uuid || uuid.v4());
item.inventory_uuid = db.bid(item.inventory_uuid);
item.expiration_date = new Date(item.expiration_date);
item.origin_uuid = db.bid(item.origin_uuid);
item.delay = 0;
});

// create a filter to align lot item columns to the SQL columns
const filter =
util.take(
'uuid', 'label', 'initial_quantity', 'unit_cost', 'expiration_date', 'inventory_uuid', 'origin_uuid', 'delay'
);

return {mappedLots : _.map(items, filter), processedLots : items};
}

/**
* POST /stock/movement
* Create a new stock movement
Expand Down
2 changes: 0 additions & 2 deletions server/lib/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ class DatabaseConnector {

// the key exists on the object and value is an array
if(data[key] && Array.isArray(data[key])) {
var that = this;

// Every item should be converted to binary
data[key] = data[key].map(this.bid);
}
Expand Down
50 changes: 7 additions & 43 deletions server/models/procedures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1334,47 +1334,6 @@ BEGIN
ON DUPLICATE KEY UPDATE `quantity` = `quantity` + movementQuantity;
END $$

-- This stored procedure, insert a lot record into the table
CREATE PROCEDURE CreateLot (
IN uuid BINARY(16),
IN label VARCHAR(255),
IN initial_quantity INT(11),
IN unit_cost DECIMAL(19,4) UNSIGNED,
IN expiration_date DATE,
IN inventory_uuid BINARY(16),
IN origin_uuid BINARY(16),
IN delay INT(11)
)
BEGIN
INSERT INTO
`lot` (`uuid`, `label`, `initial_quantity`, `unit_cost`, `expiration_date`, `inventory_uuid`, `origin_uuid`, `delay`)
VALUES
(uuid, label, initial_quantity, unit_cost, expiration_date, inventory_uuid, origin_uuid, delay);
END $$


-- This stored procedure, insert a record into the table stock movement
CREATE PROCEDURE CreateStockMovement (
IN uuid BINARY(16),
IN document_uuid BINARY(16),
IN depot_uuid BINARY(16),
IN lot_uuid BINARY(16),
IN entity_uuid BINARY(16),
IN description TEXT,
IN flux_id INT(11),
IN date DATETIME,
IN quantity INT(11),
IN unit_cost DECIMAL(19, 4) UNSIGNED,
IN is_exit TINYINT(1),
IN user_id SMALLINT(5) UNSIGNED
)
BEGIN
INSERT INTO
`stock_movement` (`uuid`, `document_uuid`, `depot_uuid`, `lot_uuid`, `entity_uuid`, `description`, `flux_id`, `date`, `quantity`, `unit_cost`, `is_exit`, `user_id`)
VALUES
(uuid, document_uuid, depot_uuid, lot_uuid, entity_uuid, description, flux_id, date, quantity, unit_cost, is_exit, user_id);
END $$

-- This processus inserts records relative to the stock movement in the posting journal
CREATE PROCEDURE PostPurchase (
IN document_uuid BINARY(16),
Expand All @@ -1391,6 +1350,11 @@ BEGIN
DECLARE current_exchange_rate DECIMAL(19, 4) UNSIGNED;
DECLARE transaction_id VARCHAR(100);
DECLARE verify_invalid_accounts SMALLINT(5);
DECLARE PURCHASE_TRANSACTION_TYPE TINYINT(3) UNSIGNED;

-- should not be reassigned during the execution, to know why 9 please see the transaction_type table
SET PURCHASE_TRANSACTION_TYPE = 9;


-- getting the curent fiscal year
SET current_fiscal_year_id = (
Expand Down Expand Up @@ -1440,7 +1404,7 @@ BEGIN
SELECT
HUID(UUID()), project_id, current_fiscal_year_id, current_period_id, transaction_id,
date, p.uuid, sm.description, ig.stock_account, pi.total, 0, pi.total, 0, currency_id,
9, user_id
PURCHASE_TRANSACTION_TYPE, user_id
FROM
stock_movement As sm
JOIN
Expand All @@ -1466,7 +1430,7 @@ BEGIN
SELECT
HUID(UUID()), project_id, current_fiscal_year_id, current_period_id, transaction_id,
date, p.uuid, sm.description, ig.cogs_account, 0, pi.total, 0, pi.total, currency_id,
9, user_id
PURCHASE_TRANSACTION_TYPE, user_id
FROM
stock_movement As sm
JOIN
Expand Down

0 comments on commit 4dd397a

Please sign in to comment.