Skip to content

Commit

Permalink
(stock entry) find donation modal
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremielodi committed Aug 24, 2020
1 parent f220c39 commit 2f29aca
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 11 deletions.
15 changes: 15 additions & 0 deletions client/src/modules/donor/donation.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
angular.module('bhima.services')
.service('DonationService', DonationService);

DonationService.$inject = ['PrototypeApiService'];

/**
* Role Service
*
* A service wrapper for the /donors HTTP endpoint.
*
*/
function DonationService(Api) {
const service = new Api('/donations/');
return service;
}
25 changes: 14 additions & 11 deletions client/src/modules/stock/entry/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function StockEntryController(
* @description
* set movement information according the selected entity and populate the grid
*/
function handleSelectedEntity(_entities, _type) {
function handleSelectedEntity(_entities, _type, noPopulate = false) {
if (!_entities || !_entities.length) {
resetSelectedEntity();
return;
Expand All @@ -276,7 +276,10 @@ function StockEntryController(
setDescription(vm.movement.entity);

// populate the grid
populate(_entities);
if (!noPopulate) {
populate(_entities);
}

}

/**
Expand Down Expand Up @@ -348,9 +351,17 @@ function StockEntryController(
function handleDonationSelection() {
const description = $translate.instant('STOCK.RECEPTION_DONATION');
initSelectedEntity(description);

if (vm.gridOptions.data.length === 0) {
vm.addItems(1);
}

StockModal.openFindDonation()
.then((donation) => {
handleSelectedEntity(donation, 'donation', true);
setSelectedEntity(vm.movement.entity.instance);
})
.catch(Notify.handleError);
}

/**
Expand Down Expand Up @@ -505,7 +516,6 @@ function StockEntryController(
.catch(Notify.handleError);
}


/**
* @method submitIntegration
* @description prepare the stock movement and send data to the server as new stock integration
Expand Down Expand Up @@ -547,14 +557,7 @@ function StockEntryController(
user_id : vm.stockForm.details.user_id,
};

/*
the origin_uuid of lots is set on the client
because donation table depends on donor, and donor management
is not yet implemented in the application
TODO: add a donor management module
*/
movement.lots = Stock.processLotsFromStore(vm.stockForm.store.data, Uuid());
movement.lots = Stock.processLotsFromStore(vm.stockForm.store.data, vm.movement.entity.uuid);

return Stock.stocks.create(movement)
.then((document) => {
Expand Down
54 changes: 54 additions & 0 deletions client/src/modules/stock/entry/modals/findDonation.modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<form
name="FindForm"
bh-submit="$ctrl.submit(FindForm)"
novalidate>

<div class="modal-header space-between">
<div class="headercrumb">
<i class="fa fa-search"></i>
<span translate>FORM.LABELS.SEARCH</span>
<span translate>STOCK.ENTRY_DONATION</span>
</div>

<div class="toolbar text-right">
<div class="toolbar-item">
<button type="button"
ng-click="$ctrl.toggleFilter()"
class="btn btn-sm btn-default"
ng-class="{ 'btn-info' : $ctrl.filterEnabled }">
<span class="fa fa-filter"></span>
</button>
</div>
</div>
</div>

<div class="modal-body">
<div class="row">
<div class="col-xs-12">
<div
id="PurchaseGrid"
ui-grid="$ctrl.gridOptions"
ui-grid-auto-resize
ui-grid-resize-columns
ui-grid-selection>
<bh-grid-loading-indicator
loading-state="$ctrl.loading"
empty-state="$ctrl.gridOptions.data.length === 0"
error-state="$ctrl.hasError">
</bh-grid-loading-indicator>
</div>
</div>
</div>
</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="$ctrl.cancel()" data-method="cancel" translate>
FORM.BUTTONS.CLOSE
</button>

<bh-loading-button loading-state="FindForm.$loading">
<span translate>FORM.BUTTONS.SUBMIT</span>
</bh-loading-button>
</div>

</form>
117 changes: 117 additions & 0 deletions client/src/modules/stock/entry/modals/findDonation.modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
angular.module('bhima.controllers')
.controller('StockFindDonationModalController', StockFindDonationModalController);

StockFindDonationModalController.$inject = [
'$uibModalInstance', 'DonationService', 'NotifyService',
'uiGridConstants', 'GridFilteringService', 'ReceiptModal',
'bhConstants',
];

function StockFindDonationModalController(
Instance, Donation, Notify,
uiGridConstants, Filtering, Receipts, bhConstants,
) {
const vm = this;

// global
vm.selectedRow = {};

/* ======================= Grid configurations ============================ */
vm.filterEnabled = false;
vm.gridOptions = { appScopeProvider : vm };

const filtering = new Filtering(vm.gridOptions);

const columns = [
{
field : 'reference',
displayName : 'TABLE.COLUMNS.REFERENCE',
headerCellFilter : 'translate',
cellTemplate : 'modules/stock/entry/modals/templates/purchase_reference.tmpl.html',
},

{
field : 'date',
cellFilter : 'date:"'.concat(bhConstants.dates.format, '"'),
filter : { condition : filtering.filterByDate },
displayName : 'TABLE.COLUMNS.DATE',
headerCellFilter : 'translate',
sort : { priority : 0, direction : 'desc' },
},

{
field : 'display_name',
displayName : 'TREE.DONOR',
headerCellFilter : 'translate',
},
{
field : 'project_name',
displayName : 'FORM.LABELS.PROJECT',
headerCellFilter : 'translate',
},
{
field : 'description',
displayName : 'FORM.LABELS.DESCRIPTION',
headerCellFilter : 'translate',
},
];

vm.gridOptions.columnDefs = columns;
vm.gridOptions.multiSelect = false;
vm.gridOptions.enableFiltering = vm.filterEnabled;
vm.gridOptions.onRegisterApi = onRegisterApi;
vm.toggleFilter = toggleFilter;

// bind methods
vm.submit = submit;
vm.cancel = cancel;
vm.showReceipt = showReceipt;

function onRegisterApi(gridApi) {
vm.gridApi = gridApi;
vm.gridApi.selection.on.rowSelectionChanged(null, rowSelectionCallback);
}

function rowSelectionCallback(row) {
vm.selectedRow = row.entity;
}

/** toggle filter */
function toggleFilter() {
vm.filterEnabled = !vm.filterEnabled;
vm.gridOptions.enableFiltering = vm.filterEnabled;
vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
}

/** get purchase document */
function showReceipt(uuid) {
Receipts.purchase(uuid);
}

/* ======================= End Grid ======================================== */
function load() {
vm.loading = true;
Donation.read()
.then(donations => {
vm.gridOptions.data = donations;
})
.catch(() => {
vm.hasError = true;
})
.finally(() => {
vm.loading = false;
});
}

// submit
function submit() {
if (!vm.selectedRow || (vm.selectedRow && !vm.selectedRow.uuid)) { return null; }
return Instance.close([].concat(vm.selectedRow));
}
// cancel
function cancel() {
Instance.close();
}

load();
}
14 changes: 14 additions & 0 deletions client/src/modules/stock/stock.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function StockModalService(Modal) {
service.openFindService = openFindService;
service.openFindDepot = openFindDepot;
service.openFindPurchase = openFindPurchase;
service.openFindDonation = openFindDonation;
service.openDefineLots = openDefineLots;
service.openFindTansfer = openFindTansfer;
service.openActionStockAssign = openActionStockAssign;
Expand Down Expand Up @@ -226,6 +227,19 @@ function StockModalService(Modal) {
return instance.result;
}

/** search donation */
function openFindDonation(request) {
const params = angular.extend(modalParameters, {
templateUrl : 'modules/stock/entry/modals/findDonation.modal.html',
controller : 'StockFindDonationModalController',
controllerAs : '$ctrl',
resolve : { data : () => request },
});

const instance = Modal.open(params);
return instance.result;
}

/** search transfer */
function openFindTansfer(request) {
const params = angular.extend(modalParameters, {
Expand Down
9 changes: 9 additions & 0 deletions server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const install = require('../controllers/install');
const rolesCtrl = require('../controllers/admin/roles');
const users = require('../controllers/admin/users');
const donor = require('../controllers/admin/donor');
const donation = require('../controllers/admin/donation');
const projects = require('../controllers/admin/projects');
const enterprises = require('../controllers/admin/enterprises');
const services = require('../controllers/admin/services');
Expand Down Expand Up @@ -571,6 +572,14 @@ exports.configure = function configure(app) {
app.get('/donors/:id', donor.detail);
app.put('/donors/:id', donor.update);
app.delete('/donors/:id', donor.remove);

// donor controller
app.get('/donations', donation.read);
app.post('/donations', donation.create);
app.get('/donations/:uuid', donation.detail);
app.put('/donations/:uuid', donation.update);
app.delete('/donations/:uuid', donation.remove);

// projects controller
app.get('/projects/:id', projects.detail);
app.put('/projects/:id', projects.update);
Expand Down
60 changes: 60 additions & 0 deletions server/controllers/admin/donation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const db = require('../../lib/db');

module.exports = {
create,
update,
read,
remove,
detail,
};

function create(req, res, next) {
const data = req.body;
data.uuid = data.uuid ? db.bid(data.uuid) : db.uuid();

db.exec(`INSERT INTO donation SET ?`, data).then(() => {
res.sendStatus(201);
}).catch(next);
}

function update(req, res, next) {
const data = req.body;
const { uuid } = req.params;
db.exec(`UPDATE donation SET ? WHERE uuid=?`, [data, db.bid(uuid)]).then(() => {
res.sendStatus(200);
}).catch(next);
}

function read(req, res, next) {
const sql = `
SELECT BUID(dt.uuid) as uuid, dt.reference, dt.project_id,
dt.description,
dt.date,
dt.donor_id, d.display_name, p.name as project_name
FROM donation dt
JOIN project p ON p.id= dt.project_id
JOIN donor d ON d.id= dt.donor_id
`;

db.exec(sql).then(donations => {
res.status(200).json(donations);
}).catch(next);
}

function detail(req, res, next) {
const sql = `
SELECT BUID(uuid) as uuid, reference, project_id, description, date, donor_id
FROM donation
WHERE uuid=?`;
const { uuid } = req.params;
db.one(sql, db.bid(uuid)).then(donation => {
res.status(200).json(donation);
}).catch(next);
}

function remove(req, res, next) {
const { uuid } = req.params;
db.exec(`DELETE FROM donation WHERE uuid=?`, db.bid(uuid)).then(() => {
res.sendStatus(200);
}).catch(next);
}

0 comments on commit 2f29aca

Please sign in to comment.