- fix: Schedule write journal entries after item compute cost.

- fix: active vouchers query.
- fix: remove babel loader in server-side.
This commit is contained in:
Ahmed Bouhuolia
2020-08-29 22:11:42 +02:00
parent e4270dc039
commit 74321a2886
12 changed files with 217 additions and 104 deletions

View File

@@ -1,7 +1,26 @@
import { Container } from 'typedi';
import moment from 'moment';
import InventoryService from '@/services/Inventory/Inventory';
import SalesInvoicesCost from '@/services/Sales/SalesInvoicesCost';
export default class ComputeItemCostJob {
depends: number;
agenda: any;
startingDate: Date;
constructor(agenda) {
this.agenda = agenda;
this.depends = 0;
this.startingDate = null;
this.agenda.on('complete:compute-item-cost', this.onJobFinished.bind(this));
this.agenda.on('start:compute-item-cost', this.onJobStart.bind(this));
}
/**
* The job handler.
* @param {} -
*/
public async handler(job, done: Function): Promise<void> {
const Logger = Container.get('logger');
const { startingDate, itemId, costMethod = 'FIFO' } = job.attrs.data;
@@ -17,6 +36,38 @@ export default class ComputeItemCostJob {
Logger.error(`Compute item cost: ${job.attrs.data}, error: ${e}`);
done(e);
}
}
/**
* Handle the job started.
* @param {Job} job - .
*/
async onJobStart(job) {
const { startingDate } = job.attrs.data;
this.depends += 1;
if (!this.startingDate || moment(this.startingDate).isBefore(startingDate)) {
this.startingDate = startingDate;
}
}
/**
* Handle job complete items cost finished.
* @param {Job} job -
*/
async onJobFinished() {
const agenda = Container.get('agenda');
const startingDate = this.startingDate;
this.depends = Math.max(this.depends - 1, 0);
console.log(startingDate);
if (this.depends === 0) {
this.startingDate = null;
await agenda.now('rewrite-invoices-journal-entries', {
startingDate,
});
}
}
}