Skip to content

Commit

Permalink
crud tags on lot
Browse files Browse the repository at this point in the history
  • Loading branch information
mbayopanda committed Aug 23, 2020
1 parent 18b9172 commit c9246b9
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 15 deletions.
1 change: 1 addition & 0 deletions client/src/i18n/en/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@
"QUANTITY": "Enter a quantity"
},
"SELECT": {
"TAGS":"Select tags to join",
"PATIENT_TO_CONSIDER":"Select the patient to consider",
"ACCOUNT_REFERENCE" : "Select an Account Reference",
"ACCOUNT_REFERENCE_TYPE" : "Select an Account Reference Type",
Expand Down
1 change: 1 addition & 0 deletions client/src/i18n/fr/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@
"QUANTITY": "Entrer le Quantité"
},
"SELECT": {
"TAGS":"Sélectionner les étiquettes à joindre",
"PATIENT_TO_CONSIDER":"Sélectionner le patient à considérer",
"ACCOUNT_REFERENCE" : "Sélectionner la référence des comptes",
"ACCOUNT_REFERENCE_TYPE" : "Sélectionner le type de référence des comptes",
Expand Down
5 changes: 3 additions & 2 deletions client/src/js/components/bhTagSelect/bhTagSelect.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
ng-required="$ctrl.required">

<ui-select-match placeholder="{{ 'FORM.SELECT.TAGS' | translate }}">
<span>{{$item.display_name}}</span>
<i class="fa fa-circle" ng-style="$ctrl.getTagColor($item)"></i>
<span>{{$item.name}}</span>
</ui-select-match>

<ui-select-choices repeat="tag.uuid as tag in $ctrl.tags | filter: { 'name': $select.search }">
<strong ng-bind-html="tag.name | highlight:$select.search"></strong>
<span><i class="fa fa-circle" ng-style="$ctrl.getTagColor($item)"></i> <span translate>{{ tag.name }}</span></span>
</ui-select-choices>
</ui-select>

Expand Down
6 changes: 5 additions & 1 deletion client/src/js/components/bhTagSelect/bhTagSelect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('bhima.components')
.component('bhTagSelect', {
templateUrl : 'js/components/bhTagSelect/bhTagSelect.tmpl.html',
templateUrl : 'js/components/bhTagSelect/bhTagSelect.html',
controller : TagSelectController,
transclude : true,
bindings : {
Expand Down Expand Up @@ -31,6 +31,10 @@ function TagSelectController(Tags, Notify) {
loadTags();
};

$ctrl.getTagColor = t => {
return t ? { color : t.color, 'font-size' : '14px' } : null;
};

function loadTags() {
Tags.read(null)
.then(tags => {
Expand Down
5 changes: 5 additions & 0 deletions client/src/modules/stock/lots/modals/edit.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
currency-id="$ctrl.enterprise.currency_id"
model="$ctrl.model.unit_cost">
</bh-currency-input>

<bh-tag-select
tag-uuids="$ctrl.model.tags"
on-select-callback="$ctrl.onSelectTags(tags)">
</bh-tag-select>
</div>

<div class="modal-footer">
Expand Down
5 changes: 5 additions & 0 deletions client/src/modules/stock/lots/modals/edit.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function EditLotModalController(Data, Session, Lots, Inventory, Notify, Instance
vm.model = {};
vm.enterprise = Session.enterprise;
vm.onDateChange = onDateChange;
vm.onSelectTags = onSelectTags;
vm.cancel = Instance.dismiss;
vm.submit = submit;

Expand All @@ -31,6 +32,10 @@ function EditLotModalController(Data, Session, Lots, Inventory, Notify, Instance
vm.model.expiration_date = date;
}

function onSelectTags(tags) {
vm.model.tags = tags;
}

function submit(form) {
if (form.$invalid) { return; }
Lots.update(Data.uuid, vm.model)
Expand Down
1 change: 1 addition & 0 deletions client/src/modules/stock/lots/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ function StockLotsController(
lots.sort(LotsRegistry.orderByDepot);

vm.gridOptions.data = lots;
console.log('>>> lots : ', lots);

vm.grouping.unfoldAllGroups();
vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
Expand Down
17 changes: 14 additions & 3 deletions server/controllers/stock/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function getLotFilters(parameters) {
'service_uuid',
'invoice_uuid',
'purchase_uuid',
'tag_uuid',
]);

const filters = new FilterParser(params);
Expand All @@ -104,6 +105,7 @@ function getLotFilters(parameters) {
filters.equals('service_uuid', 'uuid', 'serv');
filters.equals('invoice_uuid', 'invoice_uuid', 'm');
filters.equals('purchase_uuid', 'origin_uuid', 'l');
filters.equals('tag_uuid', 'tags', 't');

// NOTE(@jniles)
// is_expired is based off the server time, not off the client time.
Expand Down Expand Up @@ -158,7 +160,8 @@ function getLots(sqlQuery, parameters, finalClause = '', orderBy) {
l.expiration_date, BUID(l.inventory_uuid) AS inventory_uuid, i.delay, l.entry_date,
i.code, i.text, BUID(m.depot_uuid) AS depot_uuid, d.text AS depot_text, iu.text AS unit_type,
BUID(ig.uuid) AS group_uuid, ig.name AS group_name,
dm.text AS documentReference, ser.name AS service_name
dm.text AS documentReference, ser.name AS service_name,
t.name AS tag_name, t.color
FROM lot l
JOIN inventory i ON i.uuid = l.inventory_uuid
JOIN inventory_unit iu ON iu.id = i.unit_id
Expand All @@ -167,6 +170,8 @@ function getLots(sqlQuery, parameters, finalClause = '', orderBy) {
LEFT JOIN document_map dm ON dm.uuid = m.document_uuid
LEFT JOIN service AS ser ON ser.uuid = m.entity_uuid
JOIN depot d ON d.uuid = m.depot_uuid
LEFT JOIN lot_tag lt ON lt.lot_uuid = l.uuid
LEFT JOIN tags t ON t.uuid = lt.tag_uuid
`;

const filters = getLotFilters(parameters);
Expand Down Expand Up @@ -227,14 +232,17 @@ function getLotsDepot(depotUuid, params, finalClause) {
i.avg_consumption, i.purchase_interval, i.delay,
iu.text AS unit_type,
ig.name AS group_name, ig.tracking_expiration, ig.tracking_consumption,
dm.text AS documentReference
dm.text AS documentReference,
t.name AS tag_name, t.color
FROM stock_movement m
JOIN lot l ON l.uuid = m.lot_uuid
JOIN inventory i ON i.uuid = l.inventory_uuid
JOIN inventory_unit iu ON iu.id = i.unit_id
JOIN inventory_group ig ON ig.uuid = i.group_uuid
JOIN depot d ON d.uuid = m.depot_uuid
LEFT JOIN document_map dm ON dm.uuid = m.document_uuid
LEFT JOIN lot_tag lt ON lt.lot_uuid = l.uuid
LEFT JOIN tags t ON t.uuid = lt.tag_uuid
`;

const groupByClause = finalClause || ` GROUP BY l.uuid, m.depot_uuid ${excludeToken} ORDER BY i.code, l.label `;
Expand Down Expand Up @@ -288,7 +296,8 @@ async function getLotsMovements(depotUuid, params) {
BUID(m.depot_uuid) AS depot_uuid, m.is_exit, m.date, BUID(m.document_uuid) AS document_uuid,
m.flux_id, BUID(m.entity_uuid) AS entity_uuid, m.unit_cost,
f.label AS flux_label, i.delay, BUID(m.invoice_uuid) AS invoice_uuid, idm.text AS invoice_reference,
iu.text AS unit_type, dm.text AS documentReference
iu.text AS unit_type, dm.text AS documentReference,
t.name AS tag_name, t.color
FROM stock_movement m
JOIN lot l ON l.uuid = m.lot_uuid
JOIN inventory i ON i.uuid = l.inventory_uuid
Expand All @@ -298,6 +307,8 @@ async function getLotsMovements(depotUuid, params) {
LEFT JOIN document_map dm ON dm.uuid = m.document_uuid
LEFT JOIN document_map idm ON idm.uuid = m.invoice_uuid
LEFT JOIN service AS serv ON serv.uuid = m.entity_uuid
LEFT JOIN lot_tag lt ON lt.lot_uuid = l.uuid
LEFT JOIN tags t ON t.uuid = lt.tag_uuid
`;

const orderBy = 'ORDER BY m.date, dm.text, l.label';
Expand Down
42 changes: 33 additions & 9 deletions server/controllers/stock/lots.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @module lots/
*
Expand All @@ -24,6 +23,7 @@ exports.assignments = assignments;
*/
function details(req, res, next) {
const bid = db.bid(req.params.uuid);
let info = {};
const query = `
SELECT
BUID(l.uuid) AS uuid, l.label, l.quantity, l.unit_cost,
Expand All @@ -36,7 +36,18 @@ function details(req, res, next) {

db.one(query, [bid])
.then(row => {
res.status(200).json(row);
info = row;
const queryTags = `
SELECT BUID(t.uuid) uuid, t.name, t.color
FROM tags t
JOIN lot_tag lt ON lt.tag_uuid = t.uuid
WHERE lt.lot_uuid = ?
`;
return db.exec(queryTags, [bid]);
})
.then(tags => {
info.tags = tags;
res.status(200).json(info);
})
.catch(next)
.done();
Expand All @@ -46,21 +57,34 @@ function details(req, res, next) {
* PUT /stock/lots/:uuid
* Edit a stock lot
*/
function update(req, res, next) {
async function update(req, res, next) {
const bid = db.bid(req.params.uuid);
const allowedToEdit = ['label', 'expiration_date', 'unit_cost'];
const params = _.pick(req.body, allowedToEdit);
const { tags } = req.body;

if (params.expiration_date) {
params.expiration_date = moment(params.expiration_date).format('YYYY-MM-DD');
}

db.exec('UPDATE lot SET ? WHERE uuid = ?', [params, bid])
.then(() => {
res.sendStatus(200);
})
.catch(next)
.done();
try {
await db.exec('UPDATE lot SET ? WHERE uuid = ?', [params, bid]);

if (tags) {
// update tags
const transaction = db.transaction();
transaction.addQuery('DELETE FROM lot_tag WHERE lot_uuid = ?', [bid]);
tags.forEach(uuid => {
const binaryTagUuid = db.bid(uuid);
transaction.addQuery('INSERT INTO lot_tag(lot_uuid, tag_uuid) VALUES (?, ?);', [bid, binaryTagUuid]);
});
await transaction.execute();
}

res.sendStatus(200);
} catch (error) {
next(error);
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions server/models/migrations/next/migrate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,11 @@ ALTER TABLE enterprise ADD COLUMN `address` VARCHAR(200) DEFAULT NULL;
* @date: 2020-07-20
*/
ALTER TABLE `tags` ADD COLUMN `color` VARCHAR(50) NULL;

DROP TABLE IF EXISTS `lot_tag`;
CREATE TABLE `lot_tag` (
`lot_uuid` BINARY(16) NOT NULL,
`tag_uuid` BINARY(16) NOT NULL,
FOREIGN KEY (`lot_uuid`) REFERENCES `lot` (`uuid`),
FOREIGN KEY (`tag_uuid`) REFERENCES `tags` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;
8 changes: 8 additions & 0 deletions server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,14 @@ CREATE TABLE `lot` (
CONSTRAINT `lot__inventory` FOREIGN KEY (`inventory_uuid`) REFERENCES `inventory` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `lot_tag`;
CREATE TABLE `lot_tag` (
`lot_uuid` BINARY(16) NOT NULL,
`tag_uuid` BINARY(16) NOT NULL,
FOREIGN KEY (`lot_uuid`) REFERENCES `lot` (`uuid`),
FOREIGN KEY (`tag_uuid`) REFERENCES `tags` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;

DROP TABLE IF EXISTS `stock_assign`;
CREATE TABLE `stock_assign` (
`uuid` BINARY(16) NOT NULL,
Expand Down

0 comments on commit c9246b9

Please sign in to comment.