mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
feat: average rate cost method.
This commit is contained in:
@@ -117,7 +117,8 @@ export default class BillSubscriber {
|
||||
this.logger.info('[bill] writing the inventory transactions', { tenantId });
|
||||
this.billsService.recordInventoryTransactions(
|
||||
tenantId,
|
||||
bill,
|
||||
bill.id,
|
||||
bill.billDate,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -129,7 +130,8 @@ export default class BillSubscriber {
|
||||
this.logger.info('[bill] overwriting the inventory transactions.', { tenantId });
|
||||
this.billsService.recordInventoryTransactions(
|
||||
tenantId,
|
||||
bill,
|
||||
bill.id,
|
||||
bill.billDate,
|
||||
true,
|
||||
);
|
||||
}
|
||||
@@ -145,4 +147,19 @@ export default class BillSubscriber {
|
||||
billId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules items cost compute jobs once the inventory transactions created
|
||||
* of the bill transaction.
|
||||
*/
|
||||
@On(events.bill.onInventoryTransactionsCreated)
|
||||
public async handleComputeItemsCosts({ tenantId, billId }) {
|
||||
this.logger.info('[bill] trying to compute the bill items cost.', {
|
||||
tenantId, billId,
|
||||
});
|
||||
await this.billsService.scheduleComputeBillItemsCost(
|
||||
tenantId,
|
||||
billId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +82,8 @@ export default {
|
||||
onDeleted: 'onSaleInvoiceDeleted',
|
||||
onBulkDelete: 'onSaleInvoiceBulkDeleted',
|
||||
onPublished: 'onSaleInvoicePublished',
|
||||
onInventoryTransactionsCreated: 'onInvoiceInventoryTransactionsCreated',
|
||||
onInventoryTransactionsDeleted: 'onInvoiceInventoryTransactionsDeleted',
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -125,6 +127,7 @@ export default {
|
||||
onDeleted: 'onBillDeleted',
|
||||
onBulkDeleted: 'onBillBulkDeleted',
|
||||
onPublished: 'onBillPublished',
|
||||
onInventoryTransactionsCreated: 'onBillInventoryTransactionsCreated'
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -165,5 +168,14 @@ export default {
|
||||
onEdited: 'onItemEdited',
|
||||
onDeleted: 'onItemDeleted',
|
||||
onBulkDeleted: 'onItemBulkDeleted',
|
||||
},
|
||||
|
||||
/**
|
||||
* Inventory service.
|
||||
*/
|
||||
inventory: {
|
||||
onComputeItemCostJobScheduled: 'onComputeItemCostJobScheduled',
|
||||
onComputeItemCostJobStarted: 'onComputeItemCostJobStarted',
|
||||
onComputeItemCostJobCompleted: 'onComputeItemCostJobCompleted'
|
||||
}
|
||||
}
|
||||
|
||||
34
server/src/subscribers/inventory.ts
Normal file
34
server/src/subscribers/inventory.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Container } from 'typedi';
|
||||
import { EventSubscriber, On } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import SaleInvoicesCost from 'services/Sales/SalesInvoicesCost';
|
||||
|
||||
@EventSubscriber()
|
||||
export class InventorySubscriber {
|
||||
depends: number = 0;
|
||||
startingDate: Date;
|
||||
|
||||
/**
|
||||
* Handle run writing the journal entries once the compute items jobs completed.
|
||||
*/
|
||||
@On(events.inventory.onComputeItemCostJobCompleted)
|
||||
async onComputeItemCostJobFinished({ itemId, tenantId, startingDate }) {
|
||||
const saleInvoicesCost = Container.get(SaleInvoicesCost);
|
||||
const agenda = Container.get('agenda');
|
||||
|
||||
const dependsComputeJobs = await agenda.jobs({
|
||||
name: 'compute-item-cost',
|
||||
nextRunAt: { $ne: null },
|
||||
'data.tenantId': tenantId,
|
||||
});
|
||||
// There is no scheduled compute jobs waiting.
|
||||
if (dependsComputeJobs.length === 0) {
|
||||
this.startingDate = null;
|
||||
|
||||
await saleInvoicesCost.scheduleWriteJournalEntries(
|
||||
tenantId,
|
||||
startingDate
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,64 @@ export default class SaleInvoiceSubscriber {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles sale invoice next number increment once invoice created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleInvoiceNextNumberIncrement({
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
saleInvoice,
|
||||
}) {
|
||||
await this.settingsService.incrementNextNumber(tenantId, {
|
||||
key: 'next_number',
|
||||
group: 'sales_invoices',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the writing inventory transactions once the invoice created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleWritingInventoryTransactions({ tenantId, saleInvoice }) {
|
||||
this.logger.info('[sale_invoice] trying to write inventory transactions.', {
|
||||
tenantId,
|
||||
});
|
||||
await this.saleInvoicesService.recordInventoryTranscactions(
|
||||
tenantId,
|
||||
saleInvoice.id,
|
||||
saleInvoice.invoiceDate,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Records journal entries of the non-inventory invoice.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
@On(events.saleInvoice.onEdited)
|
||||
public async handleWritingNonInventoryEntries({ tenantId, saleInvoice }) {
|
||||
await this.saleInvoicesService.recordNonInventoryJournalEntries(
|
||||
tenantId,
|
||||
saleInvoice.id,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@On(events.saleInvoice.onEdited)
|
||||
public async handleRewritingInventoryTransactions({ tenantId, saleInvoice }) {
|
||||
this.logger.info('[sale_invoice] trying to write inventory transactions.', {
|
||||
tenantId,
|
||||
});
|
||||
await this.saleInvoicesService.recordInventoryTranscactions(
|
||||
tenantId,
|
||||
saleInvoice.id,
|
||||
saleInvoice.invoiceDate,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles customer balance diff balnace change once sale invoice edited.
|
||||
*/
|
||||
@@ -103,35 +161,6 @@ export default class SaleInvoiceSubscriber {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles sale invoice next number increment once invoice created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleInvoiceNextNumberIncrement({
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
saleInvoice,
|
||||
}) {
|
||||
await this.settingsService.incrementNextNumber(tenantId, {
|
||||
key: 'next_number',
|
||||
group: 'sales_invoices',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the writing inventory transactions once the invoice created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleWritingInventoryTransactions({ tenantId, saleInvoice }) {
|
||||
this.logger.info('[sale_invoice] trying to write inventory transactions.', {
|
||||
tenantId,
|
||||
});
|
||||
await this.saleInvoicesService.recordInventoryTranscactions(
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles deleting the inventory transactions once the invoice deleted.
|
||||
*/
|
||||
@@ -145,4 +174,18 @@ export default class SaleInvoiceSubscriber {
|
||||
saleInvoiceId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user