Skip to content

Commit

Permalink
feat(class): Add a class to manage the financial writting on the journal
Browse files Browse the repository at this point in the history
  • Loading branch information
DedrickEnc committed Sep 5, 2017
1 parent 57a5abf commit 3a91d1d
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/controllers/inventory/inventory/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function getIds() {
* @return {Promise} Returns a database query promise
*/
function getItemsMetadata(params) {
db.convert(params, ['inventory_uuids']);
const filters = new FilterParser(params, { tableAlias : 'inventory' });

const sql =
Expand All @@ -123,6 +124,7 @@ function getItemsMetadata(params) {
inventory.unit_id = iu.id`;

filters.fullText('text', 'text', 'inventory');
filters.custom('inventory_uuids', 'inventory.uuid IN (?)', params.inventory_uuids);
filters.setOrder('ORDER BY inventory.code ASC');
const query = filters.applyQuery(sql);
const parameters = filters.parameters();
Expand Down
7 changes: 7 additions & 0 deletions server/controllers/stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

const uuid = require('node-uuid');
const moment = require('moment');

const db = require('../../lib/db');
const core = require('./core');

const StockFinanceWriter = require('./stockFinanceWriter');

// expose to the API
exports.createStock = createStock;
exports.createMovement = createMovement;
Expand Down Expand Up @@ -47,6 +50,8 @@ function createStock(req, res, next) {
let createMovementObject;
let date;

const stockFinanceWriter = new StockFinanceWriter('lot');

const transaction = db.transaction();

const document = {
Expand Down Expand Up @@ -95,6 +100,8 @@ function createStock(req, res, next) {
transaction.addQuery(createMovementQuery, [createMovementObject]);
});

stockFinanceWriter.writePurchase(document, params.lots);

transaction.execute()
.then(() => {
res.status(201).json({ uuid : document.uuid });
Expand Down
131 changes: 131 additions & 0 deletions server/controllers/stock/stockFinanceWriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* @class StockFinanceWriter
* @description
*
* This class has as purpose to provide all necessary information to let
* the main stock controller write any financial writting related to the
* stock management.
*
* Managing stock includes many operation financially, the very common thing is to
* write on the posting journal to make stock account reflect the physical quantity
* of the stock.
*
* The constructor of the class will receive a token of insertion to the table
* something like INSERT INTO table content of the insertion are provided by appropriated
* methods.
*
* The writting will increase or decrease the stock account as it is a entry or an exit
*
* @requires ../inventory/inventory/core
**/

const inventoryCore = require('../inventory/inventory/core');
class StockFinanceWriter {

/**
* @constructs
*
* During the object creation, the entity is needed to distinguish
* which table is concerned by the operation and the value of the entity
* variable is provided by the component which requires the StockFinanceWriter
* object
**/
constructor(entity) {
this.entity = entity;
}

/**
* @method writePurchase
*
* @description
*
* This method is fired when a stock must be entered from a purchase order
* at this step, we consider the purchase order is already confirmed.
*
* This method will debit the 3 account (in theory) or the stock account (in reality)
* and will credit the variation account which is the expense account commonly
* BHIMA is so flexible, the user can choose the stock account and expense account as he neede
*
* This operation will be repeat for each lot item in the list, so if we have n item in the list
* of lot then we will have 2n writting in the journal. so the method will send an array of 2n elements
* back to the main controller.
*
* @param lotList {Array} : an array of object representing lot.
* @param metadata {Object} : should contains the :
* - uuid : uuid of the docuement
* - date : date of confirmation
* - user : Id of the user who performs the operation
**/
writePurchase(metadata, lotList) {
// making sure we are about to work on an array
const lots = [].concat(lotList);
const lines = [];

var inventory_uuids = lotList.map(function (item) {
return item.inventory_uuid;
});

this.inventoriesDetails(inventory_uuids, true)
.then(function (detailsList) {
//building object to make persistent

lots.forEach(function (lot) {
var obj = {
uuid,
account_id,
debit,
credit,
debit_equiv,
credit_equiv,

project_id,
fiscal_year_id,
period_id,
trans_id,
trans_date,
record_uuid,
description,
currency_id,
entity,
reference_uuid,
comment,
origin_id,
user_id,
}

lines.push(obj);
});


console.log('resultat', res);
});
}

/**
* @method inventoriesDetails
*
* @description
*
* This method a list of inventries and their details
*
* @param inventory_uuids {Array} list of inventries uuids
*
**/
inventoriesDetails(inventory_uuids, asObject) {
return inventoryCore.getItemsMetadata({ inventory_uuids })
.then(function (data) {
if (asObject) {
return data.reduce(function (obj, item) {
obj[item.uuid] = item;
return obj;
}, {});
}
return data;
})
.catch(function (err) {
console.log(err);
});
}
}

module.exports = StockFinanceWriter;
9 changes: 9 additions & 0 deletions server/lib/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ class DatabaseConnector {
if (data[key] && typeof data[key] === 'string') {
data[key] = this.bid(data[key]);
}

if(data[key] && Array.isArray(data[key])) {
var that = this;
data[key] = data[key].map(function (item){
return that.bid(item);
});
}
});

return data;
Expand All @@ -214,6 +221,8 @@ class DatabaseConnector {
escape(key) {
return mysql.escape(key);
}


}

module.exports = new DatabaseConnector();

0 comments on commit 3a91d1d

Please sign in to comment.