mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
fix: refactoring invoice calc cost service.
This commit is contained in:
82
server/src/subscribers/SaleInvoices/SyncCustomersBalance.ts
Normal file
82
server/src/subscribers/SaleInvoices/SyncCustomersBalance.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { Container } from 'typedi';
|
||||
import { On, EventSubscriber } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class SaleInvoiceSubscriber {
|
||||
logger: any;
|
||||
tenancy: TenancyService;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.logger = Container.get('logger');
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles customer balance increment once sale invoice created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleCustomerBalanceIncrement({
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
saleInvoiceId,
|
||||
}) {
|
||||
const { customerRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
this.logger.info('[sale_invoice] trying to increment customer balance.', {
|
||||
tenantId,
|
||||
});
|
||||
await customerRepository.changeBalance(
|
||||
saleInvoice.customerId,
|
||||
saleInvoice.balance
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles customer balance diff balnace change once sale invoice edited.
|
||||
*/
|
||||
@On(events.saleInvoice.onEdited)
|
||||
public async onSaleInvoiceEdited({
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
oldSaleInvoice,
|
||||
saleInvoiceId,
|
||||
}) {
|
||||
const { customerRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
this.logger.info('[sale_invoice] trying to change diff customer balance.', {
|
||||
tenantId,
|
||||
});
|
||||
await customerRepository.changeDiffBalance(
|
||||
saleInvoice.customerId,
|
||||
saleInvoice.balance,
|
||||
oldSaleInvoice.balance,
|
||||
oldSaleInvoice.customerId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles customer balance decrement once sale invoice deleted.
|
||||
*/
|
||||
@On(events.saleInvoice.onDeleted)
|
||||
public async handleCustomerBalanceDecrement({
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
oldSaleInvoice,
|
||||
}) {
|
||||
const { customerRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
this.logger.info('[sale_invoice] trying to decrement customer balance.', {
|
||||
tenantId,
|
||||
});
|
||||
await customerRepository.changeBalance(
|
||||
oldSaleInvoice.customerId,
|
||||
oldSaleInvoice.balance * -1
|
||||
);
|
||||
}
|
||||
}
|
||||
71
server/src/subscribers/SaleInvoices/SyncItemsQuantity.ts
Normal file
71
server/src/subscribers/SaleInvoices/SyncItemsQuantity.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { Container } from 'typedi';
|
||||
import { On, EventSubscriber } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import ItemsEntriesService from 'services/Items/ItemsEntriesService';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class SyncItemsQuantityWithInvoices {
|
||||
logger: any;
|
||||
tenancy: TenancyService;
|
||||
itemsEntriesService: ItemsEntriesService;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.logger = Container.get('logger');
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
this.itemsEntriesService = Container.get(ItemsEntriesService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the sale invoice items once the invoice created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleDecrementSaleInvoiceItemsQuantity({
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
}) {
|
||||
await this.itemsEntriesService.decrementItemsQuantity(
|
||||
tenantId,
|
||||
saleInvoice.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the sale invoice items once the invoice deleted.
|
||||
*/
|
||||
@On(events.saleInvoice.onDeleted)
|
||||
public async handleIncrementSaleInvoiceItemsQuantity({
|
||||
tenantId,
|
||||
oldSaleInvoice,
|
||||
}) {
|
||||
await this.itemsEntriesService.incrementItemsEntries(
|
||||
tenantId,
|
||||
oldSaleInvoice.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle increment/decrement the different items quantity once the sale invoice be edited.
|
||||
*/
|
||||
@On(events.saleInvoice.onEdited)
|
||||
public async handleChangeSaleInvoiceItemsQuantityOnEdit({
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
oldSaleInvoice,
|
||||
}) {
|
||||
await this.itemsEntriesService.changeItemsQuantity(
|
||||
tenantId,
|
||||
saleInvoice.entries.map((entry) => ({
|
||||
...entry,
|
||||
quantity: entry.quantity * -1,
|
||||
})),
|
||||
oldSaleInvoice.entries.map((entry) => ({
|
||||
...entry,
|
||||
quantity: entry.quantity * -1,
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { Container } from 'typedi';
|
||||
import { On, EventSubscriber } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import SaleInvoicesService from 'services/Sales/SalesInvoices';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class WriteInventoryTransactions {
|
||||
logger: any;
|
||||
tenancy: TenancyService;
|
||||
saleInvoicesService: SaleInvoicesService;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.logger = Container.get('logger');
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
this.saleInvoicesService = Container.get(SaleInvoicesService);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewriting the inventory transactions once the sale invoice be edited.
|
||||
*/
|
||||
@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,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles deleting the inventory transactions once the invoice deleted.
|
||||
*/
|
||||
@On(events.saleInvoice.onDeleted)
|
||||
public async handleDeletingInventoryTransactions({
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
oldSaleInvoice,
|
||||
}) {
|
||||
this.logger.info(
|
||||
'[sale_invoice] trying to revert inventory transactions.',
|
||||
{
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
}
|
||||
);
|
||||
await this.saleInvoicesService.revertInventoryTransactions(
|
||||
tenantId,
|
||||
saleInvoiceId
|
||||
);
|
||||
}
|
||||
}
|
||||
80
server/src/subscribers/SaleInvoices/WriteJournalEntries.ts
Normal file
80
server/src/subscribers/SaleInvoices/WriteJournalEntries.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { Container } from 'typedi';
|
||||
import { On, EventSubscriber } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import SaleInvoicesService from 'services/Sales/SalesInvoices';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class SaleInvoiceSubscriber {
|
||||
logger: any;
|
||||
tenancy: TenancyService;
|
||||
saleInvoicesService: SaleInvoicesService;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.logger = Container.get('logger');
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
this.saleInvoicesService = Container.get(SaleInvoicesService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Records journal entries of the non-inventory invoice.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleWriteJournalEntriesOnInvoiceCreated({
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
saleInvoice,
|
||||
authorizedUser,
|
||||
}) {
|
||||
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
const saleInvoiceWithItems = await saleInvoiceRepository.findOneById(
|
||||
saleInvoiceId,
|
||||
'entries.item'
|
||||
);
|
||||
await this.saleInvoicesService.writesIncomeJournalEntries(
|
||||
tenantId,
|
||||
saleInvoiceWithItems
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Records journal entries of the non-inventory invoice.
|
||||
*/
|
||||
@On(events.saleInvoice.onEdited)
|
||||
public async handleRewriteJournalEntriesOnceInvoiceEdit({
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
saleInvoice,
|
||||
authorizedUser,
|
||||
}) {
|
||||
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
const saleInvoiceWithItems = await saleInvoiceRepository.findOneById(
|
||||
saleInvoiceId,
|
||||
'entries.item'
|
||||
);
|
||||
await this.saleInvoicesService.writesIncomeJournalEntries(
|
||||
tenantId,
|
||||
saleInvoiceWithItems,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle reverting journal entries once sale invoice delete.
|
||||
*/
|
||||
@On(events.saleInvoice.onDeleted)
|
||||
public async handleRevertingInvoiceJournalEntriesOnDelete({
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
}) {
|
||||
await this.saleInvoicesService.revertInvoiceJournalEntries(
|
||||
tenantId,
|
||||
saleInvoiceId,
|
||||
);
|
||||
}
|
||||
}
|
||||
57
server/src/subscribers/SaleInvoices/index.ts
Normal file
57
server/src/subscribers/SaleInvoices/index.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Container } from 'typedi';
|
||||
import { On, EventSubscriber } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import SettingsService from 'services/Settings/SettingsService';
|
||||
import SaleEstimateService from 'services/Sales/SalesEstimate';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class SaleInvoiceSubscriber {
|
||||
logger: any;
|
||||
tenancy: TenancyService;
|
||||
settingsService: SettingsService;
|
||||
saleEstimatesService: SaleEstimateService;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.logger = Container.get('logger');
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
this.settingsService = Container.get(SettingsService);
|
||||
this.saleEstimatesService = Container.get(SaleEstimateService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the sale estimate as converted from the sale invoice once created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleMarkEstimateConvert({
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
saleInvoiceId,
|
||||
}) {
|
||||
if (saleInvoice.fromEstimateId) {
|
||||
this.saleEstimatesService.convertEstimateToInvoice(
|
||||
tenantId,
|
||||
saleInvoice.fromEstiamteId,
|
||||
saleInvoiceId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user