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

feat(cost center): add link to edit for bases #5976

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
2 changes: 1 addition & 1 deletion client/src/i18n/en/cost_center.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
"ALLOCATION_BASIS_NUM_LABOR_HOURS": "Num. labor hours",
"ALLOCATION_BASIS_NUM_LABOR_HOURS_DESCRIPTION": "Number of labor hours by the service",
"ALLOCATION_BASIS_NUM_LABOR_HOURS_UNITS": "hours"
}
}
43 changes: 12 additions & 31 deletions client/src/modules/cost_center/allocation_bases/allocation_bases.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ function CostCenterAllocationBasesController(
}];

// Build other columns for ui-grid
const otherColumns = rows.costCenterIndexes.map((item, idx) => {
const otherColumns = rows.costCenterIndexes.map((item) => {
return {
field : `idx${idx}`,
field : item.index,
displayName : item.index,
type : 'number',
headerCellFilter : 'translate',
Expand All @@ -92,35 +92,16 @@ function CostCenterAllocationBasesController(
* 1. Get all cost centers by allocation basis -> return an array of arrays
* 2. Format this array of array into an array of objects which fit to ui-grid data format
*/
const line = [];
const data = [];

// First step: Get all cost centers by allocation basis -> return an array of arrays
for (let i = 0; i < rows.costCenterIndexes.length; i++) {
const base = rows.costCenterIndexes[i];
line[i] = [];
for (let j = 0; j < base.distribution.length; j++) {
const item = base.distribution[j];
const value = { id : item.id, label : item.cost_center_label };
value[`idx${i}`] = item.value;
line[i].push(value);
}
}

// Second step: Format this array of arrays into an array of objects which fit to ui-grid data format
for (let i = 0; i < rows.costCenterList.length; i++) {
let value = {};
for (let j = 0; j < line.length; j++) {
/**
* Pick each i element from each array (line[j])
* to build an object based on only i th element of each array
*/
const item = line[j];
value = { ...value, ...item[i] };
}
// Combine all these object for having an array (which fit to ui-grid data format)
data.push(value);
}
const data = rows.costCenterList.map(label => ({ label }));

// populate the data matrix with the cost center indexes
rows.costCenterIndexes.forEach(base => {
base.distribution.forEach(item => {
const row = data.find(cc => cc.label === item.cost_center_label);
row[base.index] = item.value;
row.id = item.id;
});
});

const actionColumn = {
field : 'action',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</div>

<div class="modal-body">

<bh-cost-center-select
cost-center-id="CCAllocationBasisModalCtrl.costCenterId"
on-select-callback="CCAllocationBasisModalCtrl.onSelectCostCenter(costCenter)"
Expand All @@ -28,11 +27,10 @@
<div ng-messages-include="modules/templates/messages.tmpl.html"></div>
</div>
</div>

</div>

<div class="modal-footer">
<button data-method="cancel" type="button" class="btn btn-default" ui-sref="cost_center_allocation_bases">
<button data-method="cancel" type="button" class="btn btn-default" ng-click="CCAllocationBasisModalCtrl.cancel()">
<span translate>FORM.BUTTONS.CANCEL</span>
</button>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ angular.module('bhima.controllers')

CCAllocationBasisModalController.$inject = [
'$state', 'AllocationBasisService', 'AllocationBasisQuantityService',
'NotifyService', 'params', 'appcache',
'NotifyService', 'parameters', 'appcache',
];

function CCAllocationBasisModalController(
$state, AllocationBasis, AllocationBasisQuantity,
Notify, params, AppCache,
Notify, parameters, AppCache,
) {
const vm = this;

const cache = AppCache('CostCenterModal');
const { params, fromState } = parameters;

const cache = AppCache('CostCenterAllocationBasisModal');

if (params.isCreateState || params.id) {
cache.stateParams = params;
Expand All @@ -23,31 +25,32 @@ function CCAllocationBasisModalController(

vm.costCenterId = params.id;
vm.costCenter = {};

vm.isCreateState = vm.stateParams.isCreateState;
vm.ccAllocation = {};

vm.onSelectCostCenter = onSelectCostCenter;
vm.submit = submit;

vm.cancel = () => { $state.go(fromState || '^'); };

function onSelectCostCenter(cc) {
vm.costCenter = cc;
vm.costCenterId = cc.id;
}

function load() {
if (!vm.isCreateState) {
loadAllocationBasis()
.then(loadAllocationBasisQuantity)
.catch(Notify.handleError);
} else {
loadAllocationBasis()
.catch(Notify.handleError);
}
loadAllocationBasis()
.then(() => {
if (!vm.isCreateState) { loadAllocationBasisQuantity(); }
})
.catch(Notify.handleError);
}

function loadAllocationBasis() {
return AllocationBasis.read(null, { cost_center_id : vm.costCenterId })
.then(result => {
vm.allocationBases = result;
vm.allocationBases = result || {};
});
}

Expand All @@ -66,14 +69,14 @@ function CCAllocationBasisModalController(
current[item.name] = item.id;
return current;
}, {});
const insert = Object.keys(vm.ccAllocation).map((key) => {
const record = {

const insert = Object.keys(vm.ccAllocation)
.map((key) => ({
cost_center_id : vm.costCenterId,
basis_id : baseMap[key],
quantity : vm.ccAllocation[key],
};
return record;
});
}));

return insert;
}

Expand All @@ -88,7 +91,7 @@ function CCAllocationBasisModalController(
request
.then(() => {
Notify.success(vm.isCreateState ? 'FORM.INFO.CREATE_SUCCESS' : 'FORM.INFO.UPDATE_SUCCESS');
$state.go('cost_center_allocation_bases', null, { reload : true });
$state.go(fromState, null, { reload : true });
})
.catch(Notify.handleError);
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/modules/cost_center/cost_center.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function CCAllocationBasesModal($modal, $transition) {
$modal.open({
templateUrl : 'modules/cost_center/allocation_bases/modals/action.modal.html',
controller : 'CCAllocationBasisModalController as CCAllocationBasisModalCtrl',
resolve : { params : () => $transition.params('to') },
resolve : { parameters : () => ({ params : $transition.params('to'), fromState : $transition.from().name }) },
}).result.catch(angular.noop);
}

Expand Down
7 changes: 6 additions & 1 deletion client/src/modules/cost_center/templates/action.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
<span class="caret"></span>
</a>

<ul data-action="{{rowRenderIndex}}" class="dropdown-menu-right" bh-dropdown-menu-auto-dropup uib-dropdown-menu>
<ul class="dropdown-menu-right" bh-dropdown-menu-auto-dropup uib-dropdown-menu>
<li class="bh-dropdown-header">{{row.entity.label}}</li>
<li>
<a data-method="edit-record" ui-sref="cost_center.edit({id : row.entity.id})" href>
<span class="fa fa-edit"></span> <span translate>TABLE.COLUMNS.EDIT</span>
</a>
</li>
<li>
<a data-method="edit-allocation-bases" ui-sref="cost_center_allocation_bases.edit({id : row.entity.id})" href>
<span class="fa fa-edit"></span> <span translate>COST_CENTER.EDIT_ALLOCATION_BASIS</span>
</a>
</li>
<li>
<a data-method="delete-record" ng-click="grid.appScope.deleteCostCenter(row.entity)" href>
<span class="text-danger">
Expand Down
43 changes: 27 additions & 16 deletions server/controllers/finance/cost_center_allocation_bases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
*
* This controller exposes an API to the client for reading allocation keys
*/

const _ = require('lodash');
const db = require('../../lib/db');

async function fetch() {
const queryCostCenterIndexesList = `
SELECT
fc.id,
cc.id,
ccb.name AS cost_center_allocation_basis_label,
ccbv.quantity, fc.label AS cost_center_label,
fc.step_order
FROM cost_center fc
JOIN cost_center_allocation_basis_value ccbv ON ccbv.cost_center_id = fc.id
JOIN cost_center_allocation_basis ccb ON ccb.id = ccbv.basis_id
ORDER BY fc.step_order ASC;
ccbv.quantity,
cc.label AS cost_center_label,
cc.step_order
FROM cost_center cc
JOIN cost_center_allocation_basis_value ccbv ON ccbv.cost_center_id = cc.id
JOIN cost_center_allocation_basis ccb ON ccb.id = ccbv.basis_id
ORDER BY cc.step_order ASC;
`;
const costCenterIndexesList = await db.exec(queryCostCenterIndexesList);

const indexes = _.groupBy(costCenterIndexesList, 'cost_center_allocation_basis_label');
const costCenterList = [];
const costCenterIndexes = _.keys(indexes).map((index, i) => {
const fcIndex = _.sortBy(indexes[index], 'step_order');
const line = { index, distribution : [] };
fcIndex.forEach((item) => {
if (i === 0) { costCenterList.push(item.cost_center_label); }
line.distribution.push({ id : item.id, cost_center_label : item.cost_center_label, value : item.quantity });

const costCenterList = costCenterIndexesList
.map(row => row.cost_center_label)
.filter((row, index, array) => (array.indexOf(row) === index));

const costCenterIndexes = _.keys(indexes)
.map((index) => {
const ccIndex = _.sortBy(indexes[index], 'step_order');

const line = { index, distribution : [] };

ccIndex.forEach((item) => {
line.distribution.push({ id : item.id, cost_center_label : item.cost_center_label, value : item.quantity });
});

return line;
});
return line;
});

return { costCenterList, costCenterIndexes };
}

Expand Down
4 changes: 3 additions & 1 deletion server/controllers/finance/stepDownCostAllocationQuantity.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ function bulkUpdate(req, res, next) {
const tx = db.transaction();

tx.addQuery('DELETE FROM cost_center_allocation_basis_value WHERE cost_center_id = ?;', [ccId]);

data.forEach(item => {
tx.addQuery(sql, item);
tx.addQuery(sql, { cost_center_id : ccId, ...item });
});

tx.execute()
Expand Down Expand Up @@ -129,6 +130,7 @@ function list(req, res, next) {
filters.equals('cost_center_id', 'cost_center_id', 'abv');
const query = filters.applyQuery(sql);
const params = filters.parameters();

return db.exec(query, params)
.then((rows) => {
res.status(200).json(rows);
Expand Down