Skip to content

Commit

Permalink
handle deletion and job stop
Browse files Browse the repository at this point in the history
  • Loading branch information
mbayopanda committed Jul 1, 2019
1 parent b9cec59 commit 2101165
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 26 deletions.
4 changes: 3 additions & 1 deletion client/src/i18n/en/enterprise.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"ENABLE_BARCODES_LABEL" : "Enables barcodes throughout the application",
"ENABLE_BARCODES_HELP_TEXT" : "Enable to place barcodes on most printed records and enable options to scan barcodes for document inputs.",
"ENABLE_AUTO_STOCK_ACCOUNTING_LABEL" : "Enables realtime stock accounting",
"ENABLE_AUTO_STOCK_ACCOUNTING_HELP_TEXT" : "Enabling this feature will write stock movement transactions into the posting journal in real time. It requires all inventory accounts to be correctly configured."
"ENABLE_AUTO_STOCK_ACCOUNTING_HELP_TEXT" : "Enabling this feature will write stock movement transactions into the posting journal in real time. It requires all inventory accounts to be correctly configured.",
"ENABLE_AUTO_EMAIL_REPORT_LABEL" : "Enable sending automatic reports by email",
"ENABLE_AUTO_EMAIL_REPORT_HELP_TEXT" : "Enabling this option gives access to the reporting configuration for automatic emails"
}
}
}
4 changes: 3 additions & 1 deletion client/src/i18n/fr/enterprise.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"ENABLE_BARCODES_LABEL" : "Activer les codes à barres dans l'application",
"ENABLE_BARCODES_HELP_TEXT" : "L'activation de cette fonction placera des codes à barres sur la plupart des enregistrements imprimés et activera les options permettant de numériser des codes à barres pour les entrées de document.",
"ENABLE_AUTO_STOCK_ACCOUNTING_LABEL" : "Activer la comptabilisation de stock en temps réel",
"ENABLE_AUTO_STOCK_ACCOUNTING_HELP_TEXT" : "L'activation de cette option va écrire automatiquement et en temps réel dans le journal toutes les transactions liées aux mouvements de stock. Les comptes des inventaires et groupes d'inventaire doivent être bien configurés au préalable"
"ENABLE_AUTO_STOCK_ACCOUNTING_HELP_TEXT" : "L'activation de cette option va écrire automatiquement et en temps réel dans le journal toutes les transactions liées aux mouvements de stock. Les comptes des inventaires et groupes d'inventaire doivent être bien configurés au préalable",
"ENABLE_AUTO_EMAIL_REPORT_LABEL" : "Activer l'envoie des rapports automatiques par email",
"ENABLE_AUTO_EMAIL_REPORT_HELP_TEXT" : "L'activation de cette option donne accès à la configuration des rapports pour les emails automatiques"
}
}
}
30 changes: 21 additions & 9 deletions client/src/js/components/bhCronEmailReport/bhCronEmailReport.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section>
<section ng-if="$ctrl.isFeatureEnabled">
<!-- form -->
<div class="panel panel-info">
<div class="panel-heading">
Expand All @@ -8,24 +8,33 @@
<div class="panel-body">
<form name="CronForm" bh-submit="$ctrl.submit(CronForm, $ctrl.reportForm)" novalidate>
<!-- label -->
<div class="form-group">
<!-- <div class="form-group">
<label for="cronReportLabel" translate>FORM.LABELS.LABEL</label>
<input type="text" class="form-control" id="cronReportLabel" ng-model="$ctrl.cron.label">
</div>
<input type="text" class="form-control" id="cronReportLabel" ng-model="$ctrl.cron.label" required>
</div> -->
<bh-input-text
key="label"
text-value="$ctrl.cron.label"
label="FORM.LABELS.LABEL"
autocomplete="off"
required="true">
</bh-input-text>

<!-- group -->
<div class="form-group">
<bh-entity-group-select
uuid="$ctrl.cron.entity_group_uuid"
on-select-callback="$ctrl.onSelectEntityGroup(entityGroup)"
></bh-entity-group-select>
required="true">
</bh-entity-group-select>
</div>

<!-- frequency -->
<div class="form-group">
<bh-cron-select
id="$ctrl.cron.cron_id"
on-select-callback="$ctrl.onSelectCron(cron)">
on-select-callback="$ctrl.onSelectCron(cron)"
required="true">
</bh-cron-select>
</div>

Expand All @@ -37,7 +46,7 @@
</bh-yes-no-radios>

<div class="text-right">
<button type="button" ng-click="$ctrl.submit(CronForm, $ctrl.reportForm)" class="btn btn-primary" translate>
<button type="submit" ng-click="$ctrl.submit(CronForm, $ctrl.reportForm)" class="btn btn-primary" translate>
CRON.SAVE
</button>
</div>
Expand All @@ -54,11 +63,14 @@
<table class="table table-condensed">
<tbody>
<tr ng-repeat="item in $ctrl.list track by item.id">
<td><span class="text-action" ng-click="$ctrl.details(item.id)">{{ item.label}}</span></td>
<td>
<span class="text-action" ng-click="$ctrl.details(item.id)">
<i class="fa fa-send text-info"></i> {{ item.label}}
</span>
</td>
<td><a ui-sref="entityGroup">{{ item.entity_group_label }}</a> <i>(<span translate>{{ item.cron_label }}</span>)</i></td>
<td><span ng-if="item.last_send"><span translate>CRON.LAST</span>: {{ item.last_send | date:'dd MMMM yyyy, hh:mm' }}</span></td>
<td><span ng-if="item.next_send"><span translate>CRON.NEXT</span>: {{ item.next_send | date:'dd MMMM yyyy, hh:mm' }}</span></td>
<td><a href ng-click="$ctrl.send(item.id)"><i class="fa fa-send text-success"></i></a></td>
<td><a href ng-click="$ctrl.remove(item.id)"><i class="fa fa-trash text-danger"></i></a></td>
</tr>
</tbody>
Expand Down
14 changes: 11 additions & 3 deletions client/src/js/components/bhCronEmailReport/bhCronEmailReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ angular.module('bhima.components')
});

bhCronEmailReportController.$inject = [
'CronEmailReportService', 'NotifyService',
'CronEmailReportService', 'NotifyService', 'SessionService',
];

function bhCronEmailReportController(CronEmailReports, Notify) {
function bhCronEmailReportController(CronEmailReports, Notify, Session) {
const $ctrl = this;

$ctrl.submit = submit;
Expand All @@ -42,6 +42,8 @@ function bhCronEmailReportController(CronEmailReports, Notify) {
has_dynamic_dates : 0,
};

$ctrl.isFeatureEnabled = Session.enterprise.settings.enable_auto_email_report;

load();
};

Expand Down Expand Up @@ -76,7 +78,6 @@ function bhCronEmailReportController(CronEmailReports, Notify) {
}

if (cronForm.$invalid) {
Notify.warn('CRON.PLEASE_FILL_CRON_FORM');
return;
}

Expand All @@ -86,7 +87,14 @@ function bhCronEmailReportController(CronEmailReports, Notify) {
};

CronEmailReports.create(params)
.then(() => reset())
.then(() => load())
.catch(Notify.handleError);
}

function reset() {
$ctrl.cron.label = undefined;
$ctrl.cron.entity_group_uuid = undefined;
$ctrl.cron.cron_id = undefined;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div ng-form="EntityGroupForm" bh-entity-group-select ng-model-options="{ updateOn: 'default' }">
<div
class="form-group"
ng-class="{ 'has-error' : EntityGroupForm.$submitted && EntityGroupForm.entity_group_uuid.$invalid }">
ng-class="{ 'has-error' : EntityGroupForm.$submitted && EntityGroupForm.uuid.$invalid }">

<label class="control-label" translate>
{{ $ctrl.label }}
Expand All @@ -21,7 +21,7 @@
</ui-select-choices>
</ui-select>

<div class="help-block" ng-messages="EntityGroupForm.entityGroup_uuid.$error" ng-show="EntityGroupForm.$submitted">
<div class="help-block" ng-messages="EntityGroupForm.uuid.$error" ng-show="EntityGroupForm.$submitted">
<div ng-messages-include="modules/templates/messages.tmpl.html"></div>
</div>
</div>
Expand Down
7 changes: 7 additions & 0 deletions client/src/modules/enterprises/enterprises.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@
help-text="ENTERPRISE.SETTINGS.ENABLE_AUTO_STOCK_ACCOUNTING_HELP_TEXT"
on-change-callback="EnterpriseCtrl.enableAutoStockAccountingSetting(value)">
</bh-yes-no-radios>

<bh-yes-no-radios
label="ENTERPRISE.SETTINGS.ENABLE_AUTO_EMAIL_REPORT_LABEL"
value="EnterpriseCtrl.enterprise.settings.enable_auto_email_report"
help-text="ENTERPRISE.SETTINGS.ENABLE_AUTO_EMAIL_REPORT_HELP_TEXT"
on-change-callback="EnterpriseCtrl.enableAutoEmailReportSetting(value)">
</bh-yes-no-radios>
</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions client/src/modules/enterprises/enterprises.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ function EnterpriseController(Enterprises, util, Notify, Projects, Modal, Scroll
vm.enableBalanceOnInvoiceReceipSetting = proxy('enable_balance_on_invoice_receipt');
vm.enableBarcodesSetting = proxy('enable_barcodes');
vm.enableAutoStockAccountingSetting = proxy('enable_auto_stock_accounting');
vm.enableAutoEmailReportSetting = proxy('enable_auto_email_report');

startup();
}
37 changes: 29 additions & 8 deletions server/controllers/admin/cronEmailReport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const mailer = require('../../../lib/mailer');
const auth = require('../../auth');
const dbReports = require('../../report.handlers');

const MAIN_EMAIL_JOBS = [];

function find(options = {}) {
const filters = new FilterParser(options, { tableAlias : 'cer' });
const sql = `
Expand Down Expand Up @@ -86,6 +88,16 @@ function remove(req, res, next) {
DELETE FROM cron_email_report WHERE id = ?;
`;
db.exec(query, [req.params.id])
.then(() => {
const [jobToStop] = MAIN_EMAIL_JOBS.filter(item => {
return item.id === parseInt(req.params.id, 10);
});

if (jobToStop) {
jobToStop.job.stop();
debug(`The job for "${jobToStop.label}" is stopped`);
}
})
.then(() => res.sendStatus(204))
.catch(next)
.done();
Expand Down Expand Up @@ -129,14 +141,22 @@ function addJob(frequency, cb) {
* @description at the startup, read all cron email reports
* in the database and create jobs for them
*/
function launchCronEmailReportJobs() {
find()
.then(rows => {
if (!rows.length) { return null; }
const jobs = rows.map(row => createEmailReportJob(row, sendEmailReportDocument.call(this, row)));
return Promise.all(jobs);
})
.then(() => debug('Reports scanned successfully'));
async function launchCronEmailReportJobs() {
try {
const session = await loadSession();
if (!session.enterprise.settings.enable_auto_email_report) { return; }

const records = await find();
if (!records.length) { return; }

const jobs = records.map(record => createEmailReportJob(record, sendEmailReportDocument.call(this, record)));
await Promise.all(jobs);

debug('Reports scanned successfully');
} catch (error) {
// NEED TO BE HANDLED FOR AVOIDING THE CRASH OF THE APPLICATION
throw error;
}
}

/**
Expand All @@ -146,6 +166,7 @@ function launchCronEmailReportJobs() {
*/
function createEmailReportJob(record, cb) {
const job = addJob(record.cron_value, cb);
MAIN_EMAIL_JOBS.push({ id : record.id, label : record.label, job });
return updateCronEmailReportJobDates(record.id, job);
}

Expand Down
3 changes: 2 additions & 1 deletion server/controllers/admin/enterprises.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.list = function list(req, res, next) {
BUID(location_id) AS location_id, logo, currency_id,
gain_account_id, loss_account_id, enable_price_lock, enable_prepayments,
enable_delete_records, enable_password_validation, enable_balance_on_invoice_receipt,
enable_barcodes, enable_auto_stock_accounting
enable_barcodes, enable_auto_stock_accounting, enable_auto_email_report
FROM enterprise LEFT JOIN enterprise_setting
ON enterprise.id = enterprise_setting.enterprise_id
;`;
Expand All @@ -48,6 +48,7 @@ exports.list = function list(req, res, next) {
'enable_balance_on_invoice_receipt',
'enable_barcodes',
'enable_auto_stock_accounting',
'enable_auto_email_report',
];

row.settings = _.pick(row, settings);
Expand Down
7 changes: 7 additions & 0 deletions server/models/migrations/next/migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,10 @@ CREATE TABLE `cron_email_report` (
`has_dynamic_dates` TINYINT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;

/*
* @author: mbayopanda
* @date: 2019-06-13
* @description: enable enterprise settings for auto email report
*/
ALTER TABLE `enterprise` ADD COLUMN `enable_auto_email_report` TINYINT(1) NOT NULL DEFAULT 0;
3 changes: 2 additions & 1 deletion server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ CREATE TABLE `enterprise_setting` (
`enable_balance_on_invoice_receipt` TINYINT(1) NOT NULL DEFAULT 0,
`enable_barcodes` TINYINT(1) NOT NULL DEFAULT 1,
`enable_auto_stock_accounting` TINYINT(1) NOT NULL DEFAULT 0,
`enable_auto_email_report` TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`enterprise_id`),
FOREIGN KEY (`enterprise_id`) REFERENCES `enterprise` (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;
Expand Down Expand Up @@ -1408,7 +1409,7 @@ CREATE TABLE `cron_email_report` (
`next_send` DATETIME NULL,
`has_dynamic_dates` TINYINT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `label` (`label`),
UNIQUE KEY `label` (`label`, `report_id`),
KEY `entity_group_uuid` (`entity_group_uuid`),
FOREIGN KEY (`entity_group_uuid`) REFERENCES `entity_group` (`uuid`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;
Expand Down

0 comments on commit 2101165

Please sign in to comment.