feat: trigger compute items cost once the sale invoice and bill be edited or deleted.

This commit is contained in:
a.bouhuolia
2020-12-23 18:57:17 +02:00
parent 26452d9c05
commit b07bb2df53
9 changed files with 228 additions and 69 deletions

View File

@@ -1,5 +1,6 @@
import { Container } from 'typedi';
import { EventSubscriber, On } from 'event-dispatch';
import { map, head } from 'lodash';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
import BillsService from 'services/Purchases/Bills';
@@ -163,7 +164,27 @@ export default class BillSubscriber {
tenantId,
billId,
});
await this.billsService.scheduleComputeBillItemsCost(tenantId, billId);
await this.billsService.scheduleComputeCostByBillId(tenantId, billId);
}
/**
* Handles computes items costs once the inventory transactions deleted.
*/
@On(events.bill.onInventoryTransactionsDeleted)
public async handleComputeCostsOnInventoryTransactionsDeleted({
tenantId,
billId,
oldInventoryTransactions,
}) {
const inventoryItemsIds = map(oldInventoryTransactions, 'itemId');
const startingDates = map(oldInventoryTransactions, 'date');
const startingDate = head(startingDates);
await this.billsService.scheduleComputeCostByItemsIds(
tenantId,
inventoryItemsIds,
startingDate
);
}
/**

View File

@@ -79,6 +79,7 @@ export default {
saleInvoice: {
onCreated: 'onSaleInvoiceCreated',
onEdited: 'onSaleInvoiceEdited',
onDelete: 'onSaleInvoiceDelete',
onDeleted: 'onSaleInvoiceDeleted',
onBulkDelete: 'onSaleInvoiceBulkDeleted',
onPublished: 'onSaleInvoicePublished',
@@ -127,7 +128,8 @@ export default {
onDeleted: 'onBillDeleted',
onBulkDeleted: 'onBillBulkDeleted',
onPublished: 'onBillPublished',
onInventoryTransactionsCreated: 'onBillInventoryTransactionsCreated'
onInventoryTransactionsCreated: 'onBillInventoryTransactionsCreated',
onInventoryTransactionsDeleted: 'onBillInventoryTransactionsDeleted'
},
/**
@@ -163,6 +165,9 @@ export default {
onOpeningBalanceChanged: 'onOpeingBalanceChanged',
},
/**
* Items service.
*/
items: {
onCreated: 'onItemCreated',
onEdited: 'onItemEdited',

View File

@@ -1,4 +1,5 @@
import { Container } from 'typedi';
import { head, map } from 'lodash';
import { On, EventSubscriber } from 'event-dispatch';
import events from 'subscribers/events';
import TenancyService from 'services/Tenancy/TenancyService';
@@ -89,7 +90,7 @@ export default class SaleInvoiceSubscriber {
await this.saleInvoicesService.recordInventoryTranscactions(
tenantId,
saleInvoice.id,
saleInvoice.invoiceDate,
saleInvoice.invoiceDate
);
}
@@ -101,12 +102,12 @@ export default class SaleInvoiceSubscriber {
public async handleWritingNonInventoryEntries({ tenantId, saleInvoice }) {
await this.saleInvoicesService.recordNonInventoryJournalEntries(
tenantId,
saleInvoice.id,
saleInvoice.id
);
}
/**
*
* Rewriting the inventory transactions once the sale invoice be edited.
*/
@On(events.saleInvoice.onEdited)
public async handleRewritingInventoryTransactions({ tenantId, saleInvoice }) {
@@ -117,7 +118,7 @@ export default class SaleInvoiceSubscriber {
tenantId,
saleInvoice.id,
saleInvoice.invoiceDate,
true,
true
);
}
@@ -167,14 +168,22 @@ export default class SaleInvoiceSubscriber {
/**
* Handles deleting the inventory transactions once the invoice deleted.
*/
@On(events.saleInvoice.onDeleted)
public async handleDeletingInventoryTransactions({ tenantId, saleInvoiceId }) {
this.logger.info('[sale_invoice] trying to revert inventory transactions.', {
tenantId, saleInvoiceId,
});
@On(events.saleInvoice.onDelete)
public async handleDeletingInventoryTransactions({
tenantId,
saleInvoiceId,
oldSaleInvoice,
}) {
this.logger.info(
'[sale_invoice] trying to revert inventory transactions.',
{
tenantId,
saleInvoiceId,
}
);
await this.saleInvoicesService.revertInventoryTransactions(
tenantId,
saleInvoiceId,
saleInvoiceId
);
}
@@ -182,24 +191,54 @@ export default class SaleInvoiceSubscriber {
* Schedules compute invoice items cost job.
*/
@On(events.saleInvoice.onInventoryTransactionsCreated)
public async handleComputeItemsCosts({ tenantId, saleInvoiceId }) {
this.logger.info('[sale_invoice] trying to compute the invoice items cost.', {
tenantId, saleInvoiceId,
});
await this.saleInvoicesService.scheduleComputeInvoiceItemsCost(
public async handleComputeCostsOnInventoryTransactionsCreated({
tenantId,
saleInvoiceId,
}) {
this.logger.info(
'[sale_invoice] trying to compute the invoice items cost.',
{
tenantId,
saleInvoiceId,
}
);
await this.saleInvoicesService.scheduleComputeCostByInvoiceId(
tenantId,
saleInvoiceId,
saleInvoiceId
);
}
/**
* Increments the sale invoice items once the invoice created.
* Schedules compute items cost once the inventory transactions deleted.
*/
@On(events.saleInvoice.onInventoryTransactionsDeleted)
public async handleComputeCostsOnInventoryTransactionsDeleted({
tenantId,
saleInvoiceId,
oldInventoryTransactions,
}) {
const inventoryItemsIds = map(oldInventoryTransactions, 'itemId');
const startingDates = map(oldInventoryTransactions, 'date');
const startingDate = head(startingDates);
await this.saleInvoicesService.scheduleComputeCostByItemsIds(
tenantId,
inventoryItemsIds,
startingDate
);
}
/**
* Increments the sale invoice items once the invoice created.
*/
@On(events.saleInvoice.onCreated)
public async handleDecrementSaleInvoiceItemsQuantity({ tenantId, saleInvoice }) {
public async handleDecrementSaleInvoiceItemsQuantity({
tenantId,
saleInvoice,
}) {
await this.itemsEntriesService.decrementItemsQuantity(
tenantId,
saleInvoice.entries,
saleInvoice.entries
);
}
@@ -207,10 +246,13 @@ export default class SaleInvoiceSubscriber {
* Decrements the sale invoice items once the invoice deleted.
*/
@On(events.saleInvoice.onDeleted)
public async handleIncrementSaleInvoiceItemsQuantity({ tenantId, oldSaleInvoice }) {
public async handleIncrementSaleInvoiceItemsQuantity({
tenantId,
oldSaleInvoice,
}) {
await this.itemsEntriesService.incrementItemsEntries(
tenantId,
oldSaleInvoice.entries,
oldSaleInvoice.entries
);
}
@@ -218,7 +260,11 @@ export default class SaleInvoiceSubscriber {
* Handle increment/decrement the different items quantity once the sale invoice be edited.
*/
@On(events.saleInvoice.onEdited)
public async handleChangeSaleInvoiceItemsQuantityOnEdit({ tenantId, saleInvoice, oldSaleInvoice }) {
public async handleChangeSaleInvoiceItemsQuantityOnEdit({
tenantId,
saleInvoice,
oldSaleInvoice,
}) {
await this.itemsEntriesService.changeItemsQuantity(
tenantId,
saleInvoice.entries.map((entry) => ({
@@ -228,7 +274,7 @@ export default class SaleInvoiceSubscriber {
oldSaleInvoice.entries.map((entry) => ({
...entry,
quantity: entry.quantity * -1,
})),
}))
);
}
}