Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transferts API and Integration test #56

Merged
merged 5 commits into from
Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"dev": "gulp build && cd bin && NODE_ENV=development node server/app.js",
"start": "gulp build && cd bin && NODE_ENV=production node server/app.js",
"travis": "gulp build && cd bin && NODE_ENV=travis node server/app.js",
"dev_windows": "gulp build && cd bin && set NODE_ENV=development&& node server/app.js"
"app_windows": "gulp build && cd bin && set NODE_ENV=production && node server/app.js",
"dev_windows": "gulp build && cd bin && set NODE_ENV=development && node server/app.js"
},
"repository": {
"type": "git",
Expand Down
10 changes: 10 additions & 0 deletions server/config/codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ module.exports = {
httpStatus : 400,
reason : 'You sent a bad value for some parameters'
}),
'ERR_MISSING_REQUIRED_PARAMETERS' : makeError('ApiError', {
code : 'ERR_MISSING_REQUIRED_PARAMETERS',
httpStatus : 400,
reason : 'The request requires some essentials parameters which are missing'
}),
'ERR_RESOURCE_NOT_FOUND' : makeError('ApiError', {
code : 'ERR_RESOURCE_NOT_FOUND',
httpStatus : 500,
reason : 'The necessary resource is not found'
}),

/** MySQL error codes */
'ER_DISK_FULL' : makeError('DatabaseError', {
Expand Down
12 changes: 11 additions & 1 deletion server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var caution = require('../controllers/categorised/caution');
var employees = require('../controllers/categorised/employees');
var subsidies = require('../controllers/categorised/subsidies');
var units = require('../controllers/units');
var transfers = require('../controllers/finance/transfers');

// Middleware for handle uploaded file
var multipart = require('connect-multiparty');
Expand Down Expand Up @@ -424,6 +425,14 @@ exports.configure = function (app) {
app.put('/prices/:uuid', priceList.update);
app.delete('/prices/:uuid', priceList.delete);

/**
* transfers
* NOTE: The `/cash/transfers` API endpoint must be above the `/cash` API endpoint
*/
app.get('/cash/transfers', transfers.list);
app.get('/cash/transfers/:id', transfers.detail);
app.post('/cash/transfers', transfers.create);

/** cash (aux/primary) */
app.get('/cash', cash.list);
app.get('/cash/:uuid', cash.getCashDetails);
Expand All @@ -435,7 +444,7 @@ exports.configure = function (app) {
app.get('/cashflow/report/', cashflow.getReport);
//app.get('/stock/entries?', depot.getStockEntry);

// Enterprises api
// Enterprises api
app.get('/enterprises', enterprises.list);
app.get('/enterprises/:id', enterprises.single);
app.post('/enterprises', enterprises.create);
Expand Down Expand Up @@ -463,4 +472,5 @@ exports.configure = function (app) {
app.post('/discounts', discounts.create);
app.put('/discounts/:id', discounts.update);
app.delete('/discounts/:id', discounts.delete);

};
138 changes: 138 additions & 0 deletions server/controllers/finance/transfers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* The /transferts HTTP API endpoint
*
* @module finance/transferts
*
* @desc This module is responsible for handling all crud operations relatives
* to financial transferts and define all transferts api functions
*
* @required lib/db
* @required lib/guid
* @required journal/primarycash
*/

'use strict';

var db = require('../../lib/db'),
uuid = require('../../lib/guid'),
journalTransfer = require('./journal/primarycash').transfer;

/** get the list of all transferts */
exports.list = getList;

/** get the detail of a particular transfert */
exports.detail = getDetail;

/** create a new transfert record in the database */
exports.create = create;

/**
* GET /transferts
* GET /transferts?limit={number}
*
* @desc This function is responsible for getting transferts from the database
*/
function getList(req, res, next) {
var sql,
qLimit = req.query.limit;

/** Getting the transfert module id */
sql = 'SELECT pcm.id FROM primary_cash_module pcm WHERE pcm.text = "transfert" ';

db.exec(sql)
.then(function (row) {
if (!row.length) { return next(new req.codes.ERR_RESOURCE_NOT_FOUND()); }

/** Getting transfert records */
sql =
'SELECT pc.uuid, pc.project_id, pc.type, pc.date, pc.currency_id, pc.account_id, pc.cost, pc.description, ' +
'pc.cash_box_id, pc.origin_id, pc.user_id, pci.document_uuid ' +
'FROM primary_cash pc ' +
'JOIN primary_cash_item pci ON pci.primary_cash_uuid = pc.uuid ';

qLimit = Number(qLimit);

sql += 'WHERE pc.origin_id = ? ORDER BY pc.date DESC ';
sql += (qLimit) ? 'LIMIT ' + Math.floor(qLimit) : '';

return db.exec(sql, [row[0].id]);
})
.then(function (rows) {
if (!rows.length) { return next(new req.codes.ERR_NOT_FOUND()); }
res.status(200).json(rows);
})
.catch(next)
.done();

}

/**
* GET /transferts/:id
*
* @desc This function is responsible for getting a particular transfert record from the database
*/
function getDetail(req, res, next) {
var sql,
qUuid = req.params.id;

/** Getting the transfert module id */
sql = 'SELECT pcm.id FROM primary_cash_module pcm WHERE pcm.text = "transfert" ';

db.exec(sql)
.then(function (row) {
if (!row.length) { return next(new req.codes.ERR_RESOURCE_NOT_FOUND()); }

/** Getting transfert records */
sql =
'SELECT pc.uuid, pc.project_id, pc.type, pc.date, pc.currency_id, pc.account_id, pc.cost, pc.description, ' +
'pc.cash_box_id, pc.origin_id, pc.user_id, pci.document_uuid ' +
'FROM primary_cash pc ' +
'JOIN primary_cash_item pci ON pci.primary_cash_uuid = pc.uuid ';

sql += 'WHERE pc.origin_id = ? AND pc.uuid = ? ORDER BY pc.date DESC ';

return db.exec(sql, [row[0].id, qUuid]);
})
.then(function (rows) {
if (!rows.length) { return next(new req.codes.ERR_NOT_FOUND()); }
res.status(200).json(rows[0]);
})
.catch(next)
.done();
}


/**
* POST /transferts
*
* @desc This function is responsible for creating a new transfert record in the database
* and writing to the posting journal
*/
function create(req, res, next) {
var qData = req.body;

var sqlPrimaryCash = 'INSERT INTO primary_cash ' +
'(uuid, project_id, type, date, currency_id, account_id, cost, description, cash_box_id, origin_id, user_id) ' +
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
var paramPrimaryCash = [
qData.uuid, qData.project_id, qData.type, new Date(qData.date), qData.currency_id,
qData.account_id, qData.cost, qData.description, qData.cash_box_id, qData.origin_id, qData.user_id
];

var sqlPrimaryCashItem = 'INSERT INTO primary_cash_item ' +
'(uuid, primary_cash_uuid, debit, document_uuid, credit) VALUES (?, ?, ?, ?, ?)';
var paramPrimaryCashItem = [uuid(), qData.uuid, qData.cost, qData.uuid, 0];

db.exec(sqlPrimaryCash, paramPrimaryCash)
.then(function () {
return db.exec(sqlPrimaryCashItem, paramPrimaryCashItem);
})
.then(function () {
journalTransfer(qData.uuid, qData.user_id, function (err, result) {
if (err) { return next(err); }
res.status(201).json({ id : qData.uuid });
});
})
.catch(next)
.done();
}
2 changes: 1 addition & 1 deletion server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ CREATE TABLE `primary_cash` (
`uuid` char(36) NOT NULL,
`project_id` smallint(5) unsigned NOT NULL,
`type` char(1) NOT NULL,
`date` date NOT NULL,
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deb_cred_uuid` char(36) DEFAULT NULL,
`deb_cred_type` varchar(1) DEFAULT NULL,
`currency_id` tinyint(3) unsigned NOT NULL,
Expand Down
24 changes: 17 additions & 7 deletions server/models/test/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,17 @@ INSERT INTO `section_resultat` VALUES (1, 'Section Resultat 1', 1, 1);

INSERT INTO `reference_group` VALUES (1, 'AA', 'Reference Group 1', 1, 1);

INSERT INTO `reference` VALUES
INSERT INTO `reference` VALUES
(1, 0, 'AB', 'Reference bilan 1', 1, 1, NULL),
(3, 0, 'AC', 'Reference resultat 1', 1, NULL, 1),
(4, 0, 'XX', 'Deletable reference 1', 1, NULL, NULL);

INSERT INTO `cost_center` VALUES
INSERT INTO `cost_center` VALUES
(1, 1, 'cost center 1', 'cost note', 1),
(1, 2, 'cost center 2', 'cost note 2', 0);

INSERT INTO `profit_center` VALUES
(1, 1, 'profit center 1', 'profit note'),
INSERT INTO `profit_center` VALUES
(1, 1, 'profit center 1', 'profit note'),
(1, 2, 'profit center 2', 'profit note 2');

-- Services
Expand All @@ -268,12 +268,22 @@ INSERT INTO `sale` (`project_id`, `reference`, `uuid`, `cost`, `currency_id`, `d
INSERT INTO `sale_item` (`sale_uuid`, `uuid`, `inventory_uuid`, `quantity`, `inventory_price`, `transaction_price`, `debit`, `credit`) VALUES
('957e4e79-a6bb-4b4d-a8f7-c42152b2c2f6','2e1332a7-3e63-411e-827d-42ad585ff518','cf05da13-b477-11e5-b297-023919d3d5b0',3,25.0000,25.0000,0.0000,75.0000);

INSERT INTO `posting_journal`
INSERT INTO `posting_journal`
(uuid, project_id, fiscal_year_id, period_id, trans_id, trans_date, account_id, debit, credit,
debit_equiv, credit_equiv, currency_id, deb_cred_uuid, deb_cred_type, inv_po_id, origin_id, user_id, pc_id)
VALUES
debit_equiv, credit_equiv, currency_id, deb_cred_uuid, deb_cred_type, inv_po_id, origin_id, user_id, pc_id)
VALUES
("017dbe1e-c37c-11e5-a86e-843a4b0cdadc", 1, 1, 1, "HBB1", '2016-01-26', 3631, 75.0000, 0, 75.0000, 0, 2, "3be232f9-a4b9-4af6-984c-5d3f87d5c107",
"D", "957e4e79-a6bb-4b4d-a8f7-c42152b2c2f6", 2, 1, NULL),
("017dbe1e-c37c-11e5-a86e-843a4b0cdade", 1, 1, 1, "HBB1", '2016-01-26', 3638, 0, 75.0000, 0, 75.0000, 2, "cf05da13-b477-11e5-b297-023919d3d5b0",
NULL, "957e4e79-a6bb-4b4d-a8f7-c42152b2c2f6", 2, 1, 1);


INSERT INTO `primary_cash_module` VALUES
(1, "Transfert");

INSERT INTO `transaction_type` VALUES
(1, "pcash_transfert");

-- Excahange Rate for the current date
INSERT INTO `exchange_rate` (enterprise_currency_id, foreign_currency_id, rate, date) VALUES
(2, 1, 930, CURRENT_DATE());
Loading