Skip to content

Commit

Permalink
Zip CSV and PDF files
Browse files Browse the repository at this point in the history
  • Loading branch information
mbayopanda committed Feb 3, 2022
1 parent 74aadf3 commit 5e7438f
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 141 deletions.
4 changes: 3 additions & 1 deletion client/src/i18n/en/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"CONFIRM_CANCEL": "Confirm Cancellation",
"CONFIRM": "Confirm",
"CONNECTION": "Log in",
"DOWNLOAD": "Download",
"DELETE": "Delete",
"DELETE_RECORD": "Delete Record",
"DEPOT_MANAGEMENT": "Depots Management",
Expand Down Expand Up @@ -1086,7 +1087,8 @@
"PAGE_NOT_FOUND": "The page you requested could not be found",
"PERCENTAGE_BREACKDOWN": "Percentage breakdown is not possible when selecting transactions that belong to different expense centers",
"REMAINS_DISTRIBUTION": "It remains {{ value }}",
"NO_CHANGES": "You haven't changed any form values. Nothing will be submitted to the server."
"NO_CHANGES": "You haven't changed any form values. Nothing will be submitted to the server.",
"ONLY_500_TAG_NUMBERS_CAN_BE_GENERATED": "Only a max of 500 labels can be generated at a time"
}
}
}
4 changes: 3 additions & 1 deletion client/src/i18n/fr/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"CONFIRM_CANCEL": "Confirmer Annulation",
"CONFIRM": "Confirmer",
"CONNECTION": "Se connecter",
"DOWNLOAD": "Télécharger",
"DELETE_RECORD": "Supprimer Enregistrement",
"DELETE": "Supprimer",
"DEPOT_MANAGEMENT": "Gestion des dépôts",
Expand Down Expand Up @@ -1087,7 +1088,8 @@
"PAGE_NOT_FOUND": "La page demandée n'a pas été trouvée",
"PERCENTAGE_BREACKDOWN": "La ventilation par pourcentage n'est pas possible lorsqu'on selectionne des transactions qui appartienent à des centres des frais différents",
"REMAINS_DISTRIBUTION": "Il reste {{ value }}",
"NO_CHANGES": "Vous n'avez pas changé les données dans le formulaire. Rien a été submis au server."
"NO_CHANGES": "Vous n'avez pas changé les données dans le formulaire. Rien a été submis au server.",
"ONLY_500_TAG_NUMBERS_CAN_BE_GENERATED": "Seulement 500 étiquettes maximum peuvent être générées à la fois"
}
}
}
98 changes: 0 additions & 98 deletions client/src/js/services/DownloadLink.js

This file was deleted.

5 changes: 2 additions & 3 deletions client/src/js/services/LotService.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ function LotService(Api, $http, util) {
.then(util.unwrapHttpResponse);
};

lots.generateTags = (total) => {
return $http.get(`/lots/generate_tags/${total}`)
.then(util.unwrapHttpResponse);
lots.generateTags = (number) => {
return window.open(`/lots/generate_barcodes/${number}`);
};

/**
Expand Down
9 changes: 7 additions & 2 deletions client/src/modules/stock/entry/modals/generateTags.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
name="totalTags"
type="number"
step="1"
max="500"
class="form-control"
ng-model="$ctrl.totalTags"
translate-attr="{ 'placeholder' : 'FORM.PLACEHOLDERS.TAG_NUMBER_TO_GENERATE' }">
<p class="form-text text-primary">
<i class="fa fa-info-circle"></i>
<span translate>FORM.WARNINGS.ONLY_500_TAG_NUMBERS_CAN_BE_GENERATED</span>
</p>
<p class="form-text text-muted" translate>FORM.INFO.TAG_NUMBERS_TO_GENERATE</p>
</div>
</div>
Expand All @@ -31,8 +36,8 @@
FORM.BUTTONS.CLOSE
</button>

<bh-loading-button loading-state="GenerateTagNumbersForm.$loading">
<span translate>FORM.BUTTONS.SUBMIT</span>
<bh-loading-button disabled="!$ctrl.totalTags" loading-state="GenerateTagNumbersForm.$loading">
<span translate>FORM.BUTTONS.DOWNLOAD</span>
</bh-loading-button>
</div>
</form>
23 changes: 6 additions & 17 deletions client/src/modules/stock/entry/modals/generateTags.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@ angular.module('bhima.controllers')
.controller('GenerateTagsModalController', GenerateTagsModalController);

GenerateTagsModalController.$inject = [
'$uibModalInstance', 'NotifyService',
'LotService', 'DownloadLinkService',
'$uibModalInstance', 'LotService',
];

function GenerateTagsModalController(Instance, Notify, Lots, DownloadLink) {
function GenerateTagsModalController(Instance, Lots) {
const vm = this;

vm.cancel = cancel;
vm.submit = submit;

// submit
function submit() {
if (vm.totalTags <= 0 || !vm.totalTags) { return null; }
vm.submit = () => {
if (!vm.totalTags || vm.totalTags > 500) { return null; }

return Lots.generateTags(vm.totalTags)
.then(file => {
if (!file) { return; }

DownloadLink.download(file, 'pdf', 'barcodes');

Instance.close();
})
.catch(Notify.errorHandler);
}
return Lots.generateTags(vm.totalTags);
};

// cancel
function cancel() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@types/angular": "^1.8.4",
"@uirouter/angularjs": "^1.0.29",
"accounting-js": "^1.1.1",
"adm-zip": "^0.5.9",
"angular": "^1.8.2",
"angular-animate": "^1.8.0",
"angular-dynamic-locale": "^0.1.37",
Expand Down
2 changes: 1 addition & 1 deletion server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ exports.configure = function configure(app) {
app.delete('/beds/:id', bed.delete);

// on the fly tag numbers
app.get('/lots/generate_tags/:number', lots.generateTags);
app.get('/lots/generate_barcodes/:number', lots.generateBarcodes);

// lots API
app.get('/lots/:uuid', lots.details);
Expand Down
46 changes: 28 additions & 18 deletions server/controllers/stock/lots.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @requires lib/db
* @requires lib/filter
*/
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const _ = require('lodash');
Expand All @@ -19,15 +20,14 @@ const debug = require('debug')('bhima:lots');
const moment = require('moment');

const coral = require('@ima-worldhealth/coral');
const PDFMerger = require('pdf-merger-js');
const AdmZip = require('adm-zip');

const html = require('../../lib/renderers/html');
const db = require('../../lib/db');
const barcode = require('../../lib/barcode');
const util = require('../../lib/util');
const FilterParser = require('../../lib/filter');
const identifiers = require('../../config/identifiers');
const path = require('path');

const detailsQuery = `
SELECT
Expand All @@ -50,7 +50,7 @@ exports.getAllDupes = getAllDupes;
exports.merge = merge;
exports.autoMerge = autoMerge;
exports.autoMergeZero = autoMergeZero;
exports.generateTags = generateTags;
exports.generateBarcodes = generateBarcodes;

function getLotTags(bid) {
const queryTags = `
Expand Down Expand Up @@ -461,9 +461,9 @@ function assignments(req, res, next) {
* GET /lots/generate_tags/:number
*
* @description
* Returns generated tag number
* Returns generated barcodes in a zip file
*/
async function generateTags(req, res, next) {
async function generateBarcodes(req, res, next) {

try {
const totalTags = req.params.number;
Expand All @@ -474,19 +474,18 @@ async function generateTags(req, res, next) {
tagNumbers.push({ barcode : barcode.generate(key, util.uuid()) });
}

// const data = await converter.json2csvAsync(tagNumbers, { trimHeaderFields : true, trimFieldValues : true });
// const tmpDocumentsFile = tempy.file({ name : 'barcodes.csv' });
// await fs.promises.writeFile(tmpDocumentsFile, data);

const tickets = await genPdfTickets(tagNumbers);
const f = path.join(tickets.path);
res.download(f, 'forced-name.pdf', (err) => {
if (err) {
console.log(err);
} else {
console.log('file uploaded with success : ', f);
}
});
// create the csv file of tag numbers
const data = await converter.json2csvAsync(tagNumbers, { trimHeaderFields : true, trimFieldValues : true });
const tmpCsvFile = tempy.file({ name : 'barcodes.csv' });
await fs.promises.writeFile(tmpCsvFile, data);

// create the pdf file of tag numbers
const pdfTickets = await genPdfTickets(tagNumbers);
const tmpPdfFile = path.join(pdfTickets.path);

// create a zip file for the csv and pdf files
const zipped = await zipFiles(tmpCsvFile, tmpPdfFile);
res.download(zipped);
} catch (error) {
next(error);
}
Expand All @@ -508,3 +507,14 @@ async function genPdfTickets(tagNumbers) {
const pdf = await coral(inlinedHtml.trim(), options);
return { file : pdf, path : tmpDocumentsFile };
}

async function zipFiles(...files) {
const zip = new AdmZip();
const outputFile = tempy.file({ name : `Barcodes for Tag Number in CSV+PDF.zip` });
files.forEach(file => {
zip.addLocalFile(file);
});
zip.writeZip(outputFile);
await Promise.all(files.map((file) => fse.unlink(file)));
return outputFile;
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ adm-zip@^0.4.9:
resolved "https://registry.npmjs.org/adm-zip/-/adm-zip-0.4.16.tgz#cf4c508fdffab02c269cbc7f471a875f05570365"
integrity sha512-TFi4HBKSGfIKsK5YCkKaaFG2m4PEDyViZmEwof3MTIgzimHLto6muaHVpbrljdIvIrFZzEq/p4nafOeLcYegrg==

adm-zip@^0.5.9:
version "0.5.9"
resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.5.9.tgz#b33691028333821c0cf95c31374c5462f2905a83"
integrity sha512-s+3fXLkeeLjZ2kLjCBwQufpI5fuN+kIGBxu6530nVQZGVol0d7Y/M88/xw9HGGUcJjKf8LutN3VPRUBq6N7Ajg==

agent-base@6:
version "6.0.2"
resolved "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
Expand Down

0 comments on commit 5e7438f

Please sign in to comment.