Skip to content

Commit

Permalink
fix(vouchers): tools populate entity/records
Browse files Browse the repository at this point in the history
Vouchers tools can now set the entity and records for each of the items
in the voucher grid.
  • Loading branch information
jniles committed Feb 12, 2024
1 parent 315e004 commit d22b10a
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 35 deletions.
23 changes: 11 additions & 12 deletions client/src/js/components/bhRecordTypeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ angular.module('bhima.components')
});

bhRecordTypeaheadController.$inject = [
'FindReferenceService', 'NotifyService',
'FindReferenceService', 'NotifyService', '$q',
];

function bhRecordTypeaheadController(FindReferences, Notify) {
function bhRecordTypeaheadController(FindReferences, Notify, $q) {
const $ctrl = this;
let timer = $q.defer();

$ctrl.$onInit = () => {
if ($ctrl.recordUuid) {
Expand All @@ -36,19 +37,17 @@ function bhRecordTypeaheadController(FindReferences, Notify) {
};

$ctrl.lookupRecords = (text) => {
cancelInProgressRequests();
if (text.length < 3) { return null; }
return FindReferences.read(null, { text, limit : 10 })
.then(records => {
records.forEach(record => {
record.hrLabel = `[${record.text}] ${record.description}`;
});

console.log('records:', records);

return records;
});
return FindReferences.read(null, { text, limit : 3 }, { timeout : timer.promise });
};

// cancels all pending requests
function cancelInProgressRequests() {
timer.resolve();
timer = $q.defer();
}

$ctrl.onSelectRecord = record => {
$ctrl.onSelectCallback({ record });
};
Expand Down
6 changes: 4 additions & 2 deletions client/src/js/services/PrototypeApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ function PrototypeApiService($http, util) {
* // data is typically an array here
* });
*/
function read(id, parameters = {}) {
function read(id, parameters = {}, httpParameters = {}) {
// append the id to the target
const target = this.url.concat(id || '');

const options = angular.extend(httpParameters, { params : parameters });

// send the GET request
return $http.get(target, { params : parameters })
return $http.get(target, options)
.then(util.unwrapHttpResponse);
}

Expand Down
1 change: 1 addition & 0 deletions client/src/modules/templates/bhEntityTypeahead.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class="form-control"
placeholder="{{ ::'FORM.PLACEHOLDERS.ENTITY' | translate }}"
ng-model="$ctrl.entity"
ng-attr-title="{{$ctrl.entity.hrLabel }}"
ng-disabled="$ctrl.disabled"
uib-typeahead="entity as entity.text for entity in $ctrl.lookupEntities($viewValue)"
typeahead-template-url="bhEntityTypeaheadItem.html"
Expand Down
10 changes: 9 additions & 1 deletion client/src/modules/templates/bhRecordTypeahead.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@
class="form-control"
placeholder="{{ ::'FORM.PLACEHOLDERS.RECORD' | translate }}"
ng-model="$ctrl.record"
ng-attr-title="{{$ctrl.record.description}}"
ng-disabled="$ctrl.disabled"
uib-typeahead="record as record.hrLabel for record in $ctrl.lookupRecords($viewValue)"
uib-typeahead="record as record.text for record in $ctrl.lookupRecords($viewValue)"
typeahead-select-on-blur="true"
typeahead-on-select="$ctrl.onSelectRecord($item)"
typeahead-template-url="bhRecordTypeaheadItem.html"
typeahead-popup-template-url="bhRecordTypeaheadPopupTemplate"
typeahead-append-to-body="true"
autocomplete="off">
</div>

<script type="text/ng-template" id="bhRecordTypeaheadItem.html">
<a href>
[<span ng-bind-html="match.model.text | uibTypeaheadHighlight:query"></span>] {{ match.model.description }}
</a>
</script>

<script id="bhRecordTypeaheadPopupTemplate" type="text/ng-template">
<ul class="dropdown-menu dropdown-menu-right" ng-show="isOpen() && !moveInProgress" ng-style="{top: position().top+'px' }" role="listbox" aria-hidden="{{!isOpen()}}">
<li class="uib-typeahead-match" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index, $event)" role="option" id="{{::match.id}}" style="overflow-x:hidden">
Expand Down
16 changes: 6 additions & 10 deletions client/src/modules/vouchers/complex-voucher.ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ ComplexJournalVoucherController.$inject = [
* specifying two or more lines of transactions and all relative document references
*
* @constructor
*
* TODO/FIXME - this error notification system needs serious refactor.
*/
function ComplexJournalVoucherController(
Vouchers, Currencies, Session, FindEntity, FindReference, Notify, Toolkit,
Expand Down Expand Up @@ -57,7 +55,6 @@ function ComplexJournalVoucherController(
// ui-grid options
vm.gridOptions = {
appScopeProvider : vm,
fastWatch : true,
flatEntityAccess : true,
enableSorting : false,
enableColumnMenus : false,
Expand Down Expand Up @@ -268,26 +265,25 @@ function ComplexJournalVoucherController(
}

const voucher = vm.Voucher.details;

voucher.items = vm.Voucher.store.data;

console.log('voucher:', voucher);

return Vouchers.create(voucher)
.then((result) => {
.then(result => {
Receipts.voucher(result.uuid, true);
vm.Voucher.clear();
form.$setPristine();
})
.catch(Notify.handleError);
}

vm.onSelectEntity = function onSelectEntity(row, entity) {
console.log('row', row);
console.log('entity:', entity);
vm.onSelectEntity = (row, entity) => {
row.entity.entity_uuid = entity.uuid;
};

vm.onSelectRecord = (row, record) => {
console.log('row', row);
console.log('record:', record);
row.entity.document_uuid = record.uuid;
};

startup();
Expand Down
2 changes: 1 addition & 1 deletion client/src/modules/vouchers/find-entity.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ angular.module('bhima.services')
FindEntityService.$inject = ['$uibModal', 'PrototypeApiService'];

function FindEntityService(Modal, Api) {
const service = new Api('/finance/entities');
const service = new Api('/finance/entities/');

service.openModal = openModal;

Expand Down
26 changes: 25 additions & 1 deletion client/src/modules/vouchers/find-reference.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,34 @@ angular.module('bhima.services')
FindReferenceService.$inject = ['$uibModal', 'PrototypeApiService'];

function FindReferenceService(Modal, Api) {
const service = new Api('/finance/records');
const service = new Api('/finance/records/');

service.openModal = openModal;

function label(record) {
record.hrLabel = `[${record.text}] ${record.description}`;
}

/**
* @method read
* @description
* Custom read() method makes a human readable label
*/
service.read = (...args) => {
return Api.read.apply(service, args)
.then(records => {

// label array whether it is multiple records or a single JSON
if (Array.isArray(records)) {
records.forEach(label);
} else {
label(records);
}

return records;
});
};

/**
* @function openModal
* @description open the modal for references (patient invoices, cash payment and vuocher)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="ui-grid-cell-contents">
<bh-record-typeahead
record-uuid="row.record.record_uuid"
on-select-record="grid.appScope.onSelectRecord(record)"
record-uuid="row.entity.document_uuid"
on-select-callback="grid.appScope.onSelectRecord(row, record)"
disabled="!row.entity._initialized">
</bh-record-typeahead>
</div>
10 changes: 6 additions & 4 deletions client/src/modules/vouchers/voucher-item.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ function VoucherItemService(uuid, Constants, util) {
* the ui-select.
*/
VoucherItem.prototype.configure = function configure(item) {
console.log('item:', item);

if (item.account_id) {
this.account_id = item.account_id;
}
Expand All @@ -142,11 +140,15 @@ function VoucherItemService(uuid, Constants, util) {
}

if (angular.isDefined(item.entity)) {
this.entity = item.entity;
this.entity_uuid = item.entity.uuid;
}

if (angular.isDefined(item.entity_uuid)) {
this.entity_uuid = item.entity_uuid;
}

if (angular.isDefined(item.document)) {
this.document = item.document;
this.document_uuid = item.document.uuid;
}

if (angular.isDefined(item.document_uuid)) {
Expand Down
2 changes: 2 additions & 0 deletions server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,9 @@ exports.configure = function configure(app) {
app.get('/lots/generate_barcodes/:number', lots.generateBarcodes);

app.get('/finance/entities', financeShared.lookupFinancialEntity);
app.get('/finance/entities/:uuid', financeShared.lookupFinancialEntityByUuid);
app.get('/finance/records', financeShared.lookupFinancialRecord);
app.get('/finance/records/:uuid', financeShared.lookupFinancialRecordByUuid);

// lots API
app.get('/lots/:uuid', lots.details);
Expand Down
48 changes: 46 additions & 2 deletions server/controllers/finance/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,50 @@ exports.getRecordUuidByTextBulk = getRecordUuidByTextBulk;
exports.getEntityUuidByText = getEntityUuidByText;
exports.getEntityUuidByTextBulk = getEntityUuidByTextBulk;

exports.lookupFinancialEntityByUuid = (req, res, next) => {
const uuid = db.bid(req.params.uuid);

const debtorSQL = `
SELECT em.uuid, em.text, d.text as hrLabel FROM entity_map em JOIN debtor d ON em.uuid = d.uuid
WHERE em.uuid = ?
`;

const creditorSQL = `
SELECT em.uuid, em.text, c.text as hrLabel FROM entity_map em JOIN creditor c ON em.uuid = c.uuid
WHERE em.uuid = ?
`;

const combinedSQL = `
SELECT uuid, text, hrLabel FROM (
${debtorSQL} UNION ${creditorSQL}
)z ORDER BY text LIMITT 1;
`;

db.one(combinedSQL, [uuid, uuid])
.then(record => res.status(200).json(record))
.catch(next);
};

exports.lookupFinancialRecordByUuid = (req, res, next) => {
const uuid = db.bid(req.params.uuid);

const vouchers = getQueryForTable('voucher', { uuid });
const invoices = getQueryForTable('invoice', { uuid });
const cash = getQueryForTable('cash', { uuid });

return Promise.all([
db.exec(vouchers.query, vouchers.parameters),
db.exec(invoices.query, invoices.parameters),
db.exec(cash.query, cash.parameters),
])
.then(records => {
const [record] = _.flatten(records);
res.status(200).json(record);
})
.catch(next);

};

/**
* @function lookupFinancialEntity
*
Expand All @@ -51,7 +95,7 @@ exports.lookupFinancialEntity = (req, res, next) => {
SELECT em.uuid, em.text, c.text as hrLabel FROM entity_map em JOIN creditor c ON em.uuid = c.uuid
`;

filters.equals('uuid');
filters.equals('uuid', 'uuid', 'em');
filters.fullText('text', 'text', 'em');

const debtorQuery = filters.applyQuery(debtorSQL);
Expand All @@ -78,7 +122,7 @@ function getQueryForTable(table, options) {
FROM document_map dm JOIN ${table} t ON dm.uuid = t.uuid
`;

filters.equals('uuid');
filters.equals('uuid', 'uuid', 't');
filters.fullText('text');
filters.setOrder('ORDER BY t.date DESC');

Expand Down

0 comments on commit d22b10a

Please sign in to comment.