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,6 +1,6 @@
import { Container, Service, Inject } from 'typedi';
import { map } from 'lodash';
import JournalPoster from 'services/Accounting/JournalPoster';
import JournalEntry from 'services/Accounting/JournalEntry';
import InventoryService from 'services/Inventory/Inventory';
import TenancyService from 'services/Tenancy/TenancyService';
import { ISaleInvoice, IItemEntry, IInventoryLotCost, IItem } from 'interfaces';
@@ -21,10 +21,10 @@ export default class SaleInvoicesCost {
* @param {Date} startingDate - Starting compute cost date.
* @return {Promise<Agenda>}
*/
async scheduleComputeItemsCost(
async scheduleComputeCostByItemsIds(
tenantId: number,
inventoryItemsIds: number[],
startingDate: Date
startingDate: Date,
) {
const asyncOpers: Promise<[]>[] = [];
@@ -39,6 +39,86 @@ export default class SaleInvoicesCost {
return Promise.all([...asyncOpers]);
}
/**
* Schedules compute sale invoice items cost based on each item
* cost method.
* @param {number} tenantId - Tenant id.
* @param {ISaleInvoice} saleInvoiceId - Sale invoice id.
* @param {boolean} override - Allow to override old computes in edit mode.
* @return {Promise}
*/
async scheduleComputeCostByInvoiceId(
tenantId: number,
saleInvoiceId: number,
) {
const { SaleInvoice } = this.tenancy.models(tenantId);
// Retrieve the sale invoice with associated entries.
const saleInvoice: ISaleInvoice = await SaleInvoice.query()
.findById(saleInvoiceId)
.withGraphFetched('entries');
// Schedule compute inventory items cost by the given invoice model object.
return this.scheduleComputeCostByEntries(
tenantId,
saleInvoice.entries,
saleInvoice.invoiceDate,
);
}
/**
* Schedules the compute inventory items cost by the given bill id.
* @param {number} tenantId - Tenant id.
* @param {number} billId - Bill id.
* @return {Promise<void>}
*/
async scheduleComputeCostByBillId(
tenantId: number,
billId: number
): Promise<void> {
const { Bill } = this.tenancy.models(tenantId);
// Retrieve the bill with associated entries.
const bill = await Bill.query()
.findById(billId)
.withGraphFetched('entries');
return this.scheduleComputeCostByEntries(
tenantId,
bill.entries,
bill.billDate,
);
}
/**
* Schedules the compute inventory items by the given invoice.
* @param {number} tenantId
* @param {ISaleInvoice & { entries: IItemEntry[] }} saleInvoice
* @param {boolean} override
*/
async scheduleComputeCostByEntries(
tenantId: number,
entries: IItemEntry[],
startingDate: Date,
) {
const { Item } = this.tenancy.models(tenantId);
// Retrieve the inventory items that associated to the sale invoice entries.
const inventoryItems = await Item.query()
.whereIn('id', map(entries, 'itemId'))
.where('type', 'inventory');
const inventoryItemsIds = map(inventoryItems, 'id');
if (inventoryItemsIds.length > 0) {
await this.scheduleComputeCostByItemsIds(
tenantId,
inventoryItemsIds,
startingDate
);
}
}
/**
* Schedule writing journal entries.
* @param {Date} startingDate