mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { Inject, Service } from 'typedi';
|
|
import events from '@/subscribers/events';
|
|
import {
|
|
ISaleInvoiceWriteoffCreatePayload,
|
|
ISaleInvoiceWrittenOffCanceledPayload,
|
|
} from '@/interfaces';
|
|
import { SaleInvoiceWriteoffGLStorage } from './SaleInvoiceWriteoffGLStorage';
|
|
|
|
@Service()
|
|
export default class SaleInvoiceWriteoffSubscriber {
|
|
@Inject()
|
|
writeGLStorage: SaleInvoiceWriteoffGLStorage;
|
|
|
|
/**
|
|
* Attaches events.
|
|
*/
|
|
public attach(bus) {
|
|
bus.subscribe(
|
|
events.saleInvoice.onWrittenoff,
|
|
this.writeJournalEntriesOnceWriteoffCreate
|
|
);
|
|
bus.subscribe(
|
|
events.saleInvoice.onWrittenoffCanceled,
|
|
this.revertJournalEntriesOnce
|
|
);
|
|
}
|
|
/**
|
|
* Write the written-off sale invoice journal entries.
|
|
* @param {ISaleInvoiceWriteoffCreatePayload}
|
|
*/
|
|
private writeJournalEntriesOnceWriteoffCreate = async ({
|
|
tenantId,
|
|
saleInvoice,
|
|
trx,
|
|
}: ISaleInvoiceWriteoffCreatePayload) => {
|
|
await this.writeGLStorage.writeInvoiceWriteoffEntries(
|
|
tenantId,
|
|
saleInvoice.id,
|
|
trx
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Reverts the written-of sale invoice jounral entries.
|
|
* @param {ISaleInvoiceWrittenOffCanceledPayload}
|
|
*/
|
|
private revertJournalEntriesOnce = async ({
|
|
tenantId,
|
|
saleInvoice,
|
|
trx,
|
|
}: ISaleInvoiceWrittenOffCanceledPayload) => {
|
|
await this.writeGLStorage.revertInvoiceWriteoffEntries(
|
|
tenantId,
|
|
saleInvoice.id,
|
|
trx
|
|
);
|
|
};
|
|
}
|