fix(server): wirte GL entries only when publish transaction

This commit is contained in:
Ahmed Bouhuolia
2023-08-17 21:49:07 +02:00
parent 5b2be2ac19
commit 0fcee0eaa7
13 changed files with 65 additions and 43 deletions

View File

@@ -16,16 +16,16 @@ import BaseCreditNotes from './CreditNotes';
@Service() @Service()
export default class CreateCreditNote extends BaseCreditNotes { export default class CreateCreditNote extends BaseCreditNotes {
@Inject() @Inject()
uow: UnitOfWork; private uow: UnitOfWork;
@Inject() @Inject()
itemsEntriesService: ItemsEntriesService; private itemsEntriesService: ItemsEntriesService;
@Inject() @Inject()
tenancy: HasTenancyService; private tenancy: HasTenancyService;
@Inject() @Inject()
eventPublisher: EventPublisher; private eventPublisher: EventPublisher;
/** /**
* Creates a new credit note. * Creates a new credit note.

View File

@@ -1,5 +1,4 @@
import { Service, Inject } from 'typedi'; import { Service, Inject } from 'typedi';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
import { import {
IApplyCreditToInvoicesCreatedPayload, IApplyCreditToInvoicesCreatedPayload,
@@ -10,15 +9,12 @@ import CreditNoteApplySyncInvoicesCreditedAmount from './CreditNoteApplySyncInvo
@Service() @Service()
export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber { export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber {
@Inject() @Inject()
tenancy: HasTenancyService; private syncInvoicesWithCreditNote: CreditNoteApplySyncInvoicesCreditedAmount;
@Inject()
syncInvoicesWithCreditNote: CreditNoteApplySyncInvoicesCreditedAmount;
/** /**
* Attaches events with handlers. * Attaches events with handlers.
*/ */
attach(bus) { public attach(bus) {
bus.subscribe( bus.subscribe(
events.creditNote.onApplyToInvoicesCreated, events.creditNote.onApplyToInvoicesCreated,
this.incrementAppliedInvoicesOnceCreditCreated this.incrementAppliedInvoicesOnceCreditCreated

View File

@@ -15,7 +15,7 @@ export default class CreditNoteInventoryTransactionsSubscriber {
/** /**
* Attaches events with publisher. * Attaches events with publisher.
*/ */
attach(bus) { public attach(bus) {
bus.subscribe( bus.subscribe(
events.creditNote.onCreated, events.creditNote.onCreated,
this.writeInventoryTranscationsOnceCreated this.writeInventoryTranscationsOnceCreated
@@ -37,6 +37,7 @@ export default class CreditNoteInventoryTransactionsSubscriber {
/** /**
* Writes inventory transactions once credit note created. * Writes inventory transactions once credit note created.
* @param {ICreditNoteCreatedPayload} payload - * @param {ICreditNoteCreatedPayload} payload -
* @returns {Promise<void>}
*/ */
public writeInventoryTranscationsOnceCreated = async ({ public writeInventoryTranscationsOnceCreated = async ({
tenantId, tenantId,
@@ -44,9 +45,8 @@ export default class CreditNoteInventoryTransactionsSubscriber {
trx, trx,
}: ICreditNoteCreatedPayload) => { }: ICreditNoteCreatedPayload) => {
// Can't continue if the credit note is open yet. // Can't continue if the credit note is open yet.
if (!creditNote.isOpen) { if (!creditNote.isOpen) return;
return;
}
await this.inventoryTransactions.createInventoryTransactions( await this.inventoryTransactions.createInventoryTransactions(
tenantId, tenantId,
creditNote, creditNote,
@@ -57,6 +57,7 @@ export default class CreditNoteInventoryTransactionsSubscriber {
/** /**
* Rewrites inventory transactions once credit note edited. * Rewrites inventory transactions once credit note edited.
* @param {ICreditNoteEditedPayload} payload - * @param {ICreditNoteEditedPayload} payload -
* @returns {Promise<void>}
*/ */
public rewriteInventoryTransactionsOnceEdited = async ({ public rewriteInventoryTransactionsOnceEdited = async ({
tenantId, tenantId,
@@ -65,9 +66,8 @@ export default class CreditNoteInventoryTransactionsSubscriber {
trx, trx,
}: ICreditNoteEditedPayload) => { }: ICreditNoteEditedPayload) => {
// Can't continue if the credit note is open yet. // Can't continue if the credit note is open yet.
if (!creditNote.isOpen) { if (!creditNote.isOpen) return;
return;
}
await this.inventoryTransactions.editInventoryTransactions( await this.inventoryTransactions.editInventoryTransactions(
tenantId, tenantId,
creditNoteId, creditNoteId,
@@ -87,9 +87,8 @@ export default class CreditNoteInventoryTransactionsSubscriber {
trx, trx,
}: ICreditNoteDeletedPayload) => { }: ICreditNoteDeletedPayload) => {
// Can't continue if the credit note is open yet. // Can't continue if the credit note is open yet.
if (!oldCreditNote.isOpen) { if (!oldCreditNote.isOpen) return;
return;
}
await this.inventoryTransactions.deleteInventoryTransactions( await this.inventoryTransactions.deleteInventoryTransactions(
tenantId, tenantId,
creditNoteId, creditNoteId,

View File

@@ -21,7 +21,7 @@ export class ExpensesWriteGLSubscriber {
* Attaches events with handlers. * Attaches events with handlers.
* @param bus * @param bus
*/ */
attach(bus) { public attach(bus) {
bus.subscribe( bus.subscribe(
events.expenses.onCreated, events.expenses.onCreated,
this.handleWriteGLEntriesOnceCreated this.handleWriteGLEntriesOnceCreated

View File

@@ -48,6 +48,7 @@ export class ManualJournalWriteGLSubscriber {
/** /**
* Handle manual journal created event. * Handle manual journal created event.
* @param {IManualJournalEventCreatedPayload} payload - * @param {IManualJournalEventCreatedPayload} payload -
* @returns {Promise<void>}
*/ */
private handleWriteJournalEntriesOnCreated = async ({ private handleWriteJournalEntriesOnCreated = async ({
tenantId, tenantId,
@@ -55,18 +56,19 @@ export class ManualJournalWriteGLSubscriber {
trx, trx,
}: IManualJournalEventCreatedPayload) => { }: IManualJournalEventCreatedPayload) => {
// Ingore writing manual journal journal entries in case was not published. // Ingore writing manual journal journal entries in case was not published.
if (manualJournal.publishedAt) { if (!manualJournal.publishedAt) return;
await this.manualJournalGLEntries.createManualJournalGLEntries(
tenantId, await this.manualJournalGLEntries.createManualJournalGLEntries(
manualJournal.id, tenantId,
trx manualJournal.id,
); trx
} );
}; };
/** /**
* Handles the manual journal next number increment once the journal be created. * Handles the manual journal next number increment once the journal be created.
* @param {IManualJournalEventCreatedPayload} payload - * @param {IManualJournalEventCreatedPayload} payload -
* @return {Promise<void>}
*/ */
private handleJournalNumberIncrement = async ({ private handleJournalNumberIncrement = async ({
tenantId, tenantId,
@@ -77,6 +79,7 @@ export class ManualJournalWriteGLSubscriber {
/** /**
* Handle manual journal edited event. * Handle manual journal edited event.
* @param {IManualJournalEventEditedPayload} * @param {IManualJournalEventEditedPayload}
* @return {Promise<void>}
*/ */
private handleRewriteJournalEntriesOnEdited = async ({ private handleRewriteJournalEntriesOnEdited = async ({
tenantId, tenantId,
@@ -96,6 +99,7 @@ export class ManualJournalWriteGLSubscriber {
/** /**
* Handles writing journal entries once the manula journal publish. * Handles writing journal entries once the manula journal publish.
* @param {IManualJournalEventPublishedPayload} payload - * @param {IManualJournalEventPublishedPayload} payload -
* @return {Promise<void>}
*/ */
private handleWriteJournalEntriesOnPublished = async ({ private handleWriteJournalEntriesOnPublished = async ({
tenantId, tenantId,

View File

@@ -4,6 +4,7 @@ import {
IBillCreatedPayload, IBillCreatedPayload,
IBillEditedPayload, IBillEditedPayload,
IBIllEventDeletedPayload, IBIllEventDeletedPayload,
IBillOpenedPayload,
} from '@/interfaces'; } from '@/interfaces';
import { BillGLEntries } from './BillGLEntries'; import { BillGLEntries } from './BillGLEntries';
@@ -37,13 +38,12 @@ export class BillGLEntriesSubscriber {
*/ */
private handlerWriteJournalEntriesOnCreate = async ({ private handlerWriteJournalEntriesOnCreate = async ({
tenantId, tenantId,
billId,
bill, bill,
trx, trx,
}: IBillCreatedPayload) => { }: IBillCreatedPayload | IBillOpenedPayload) => {
if (!bill.openedAt) return null; if (!bill.openedAt) return null;
await this.billGLEntries.writeBillGLEntries(tenantId, billId, trx); await this.billGLEntries.writeBillGLEntries(tenantId, bill.id, trx);
}; };
/** /**

View File

@@ -50,10 +50,13 @@ export class OpenBill {
oldBill, oldBill,
} as IBillOpeningPayload); } as IBillOpeningPayload);
// Record the bill opened at on the storage. // Save the bill opened at on the storage.
const bill = await Bill.query(trx).patchAndFetchById(billId, { const bill = await Bill.query(trx)
openedAt: moment().toMySqlDateTime(), .patchAndFetchById(billId, {
}); openedAt: moment().toMySqlDateTime(),
})
.withGraphFetched('entries');
// Triggers `onBillCreating` event. // Triggers `onBillCreating` event.
await this.eventPublisher.emitAsync(events.bill.onOpened, { await this.eventPublisher.emitAsync(events.bill.onOpened, {
trx, trx,

View File

@@ -10,7 +10,7 @@ import VendorCreditInventoryTransactions from './VendorCreditInventoryTransactio
@Service() @Service()
export default class VendorCreditInventoryTransactionsSubscriber { export default class VendorCreditInventoryTransactionsSubscriber {
@Inject() @Inject()
inventoryTransactions: VendorCreditInventoryTransactions; private inventoryTransactions: VendorCreditInventoryTransactions;
/** /**
* Attaches events with handlers. * Attaches events with handlers.
@@ -21,6 +21,10 @@ export default class VendorCreditInventoryTransactionsSubscriber {
events.vendorCredit.onCreated, events.vendorCredit.onCreated,
this.writeInventoryTransactionsOnceCreated this.writeInventoryTransactionsOnceCreated
); );
bus.subscribe(
events.vendorCredit.onOpened,
this.writeInventoryTransactionsOnceCreated
);
bus.subscribe( bus.subscribe(
events.vendorCredit.onEdited, events.vendorCredit.onEdited,
this.rewriteInventroyTransactionsOnceEdited this.rewriteInventroyTransactionsOnceEdited
@@ -40,6 +44,9 @@ export default class VendorCreditInventoryTransactionsSubscriber {
vendorCredit, vendorCredit,
trx, trx,
}: IVendorCreditCreatedPayload) => { }: IVendorCreditCreatedPayload) => {
// Can't continue if vendor credit is not opened.
if (!vendorCredit.openedAt) return null;
await this.inventoryTransactions.createInventoryTransactions( await this.inventoryTransactions.createInventoryTransactions(
tenantId, tenantId,
vendorCredit, vendorCredit,
@@ -57,6 +64,9 @@ export default class VendorCreditInventoryTransactionsSubscriber {
vendorCredit, vendorCredit,
trx, trx,
}: IVendorCreditEditedPayload) => { }: IVendorCreditEditedPayload) => {
// Can't continue if vendor credit is not opened.
if (!vendorCredit.openedAt) return null;
await this.inventoryTransactions.editInventoryTransactions( await this.inventoryTransactions.editInventoryTransactions(
tenantId, tenantId,
vendorCreditId, vendorCreditId,

View File

@@ -4,6 +4,7 @@ import {
IBillCreatedPayload, IBillCreatedPayload,
IBillEditedPayload, IBillEditedPayload,
IBIllEventDeletedPayload, IBIllEventDeletedPayload,
IBillOpenedPayload,
} from '@/interfaces'; } from '@/interfaces';
import { BillInventoryTransactions } from '@/services/Purchases/Bills/BillInventoryTransactions'; import { BillInventoryTransactions } from '@/services/Purchases/Bills/BillInventoryTransactions';
@@ -20,6 +21,10 @@ export default class BillWriteInventoryTransactionsSubscriber {
events.bill.onCreated, events.bill.onCreated,
this.handleWritingInventoryTransactions this.handleWritingInventoryTransactions
); );
bus.subscribe(
events.bill.onOpened,
this.handleWritingInventoryTransactions
);
bus.subscribe( bus.subscribe(
events.bill.onEdited, events.bill.onEdited,
this.handleOverwritingInventoryTransactions this.handleOverwritingInventoryTransactions
@@ -32,19 +37,19 @@ export default class BillWriteInventoryTransactionsSubscriber {
/** /**
* Handles writing the inventory transactions once bill created. * Handles writing the inventory transactions once bill created.
* @param {IBillCreatedPayload | IBillOpenedPayload} payload -
*/ */
private handleWritingInventoryTransactions = async ({ private handleWritingInventoryTransactions = async ({
tenantId, tenantId,
billId,
bill, bill,
trx, trx,
}: IBillCreatedPayload) => { }: IBillCreatedPayload | IBillOpenedPayload) => {
// Can't continue if the bill is not opened yet. // Can't continue if the bill is not opened yet.
if (!bill.openedAt) return null; if (!bill.openedAt) return null;
await this.billsInventory.recordInventoryTransactions( await this.billsInventory.recordInventoryTransactions(
tenantId, tenantId,
billId, bill.id,
false, false,
trx trx
); );
@@ -52,6 +57,7 @@ export default class BillWriteInventoryTransactionsSubscriber {
/** /**
* Handles the overwriting the inventory transactions once bill edited. * Handles the overwriting the inventory transactions once bill edited.
* @param {IBillEditedPayload} payload -
*/ */
private handleOverwritingInventoryTransactions = async ({ private handleOverwritingInventoryTransactions = async ({
tenantId, tenantId,
@@ -72,6 +78,7 @@ export default class BillWriteInventoryTransactionsSubscriber {
/** /**
* Handles the reverting the inventory transactions once the bill deleted. * Handles the reverting the inventory transactions once the bill deleted.
* @param {IBIllEventDeletedPayload} payload -
*/ */
private handleRevertInventoryTransactions = async ({ private handleRevertInventoryTransactions = async ({
tenantId, tenantId,

View File

@@ -19,7 +19,7 @@ export default class BillWriteGLEntriesSubscriber {
/** /**
* Attachs events with handles. * Attachs events with handles.
*/ */
attach(bus) { public attach(bus) {
bus.subscribe( bus.subscribe(
events.bill.onCreated, events.bill.onCreated,
this.handlerWriteJournalEntriesOnCreate this.handlerWriteJournalEntriesOnCreate

View File

@@ -37,7 +37,6 @@ export default class PaymentReceiveSyncInvoicesSubscriber {
*/ */
private handleInvoiceIncrementPaymentOnceCreated = async ({ private handleInvoiceIncrementPaymentOnceCreated = async ({
tenantId, tenantId,
paymentReceiveId,
paymentReceive, paymentReceive,
trx, trx,
}: IPaymentReceiveCreatedPayload) => { }: IPaymentReceiveCreatedPayload) => {

View File

@@ -36,10 +36,8 @@ export default class PaymentReceivesWriteGLEntriesSubscriber {
private handleWriteJournalEntriesOnceCreated = async ({ private handleWriteJournalEntriesOnceCreated = async ({
tenantId, tenantId,
paymentReceiveId, paymentReceiveId,
paymentReceive,
trx, trx,
}: IPaymentReceiveCreatedPayload) => { }: IPaymentReceiveCreatedPayload) => {
if (paymentReceive)
await this.paymentReceiveGLEntries.writePaymentGLEntries( await this.paymentReceiveGLEntries.writePaymentGLEntries(
tenantId, tenantId,
paymentReceiveId, paymentReceiveId,

View File

@@ -36,6 +36,8 @@ export default class SaleInvoiceWriteGLEntriesSubscriber {
/** /**
* Records journal entries of the non-inventory invoice. * Records journal entries of the non-inventory invoice.
* @param {ISaleInvoiceCreatedPayload} payload -
* @returns {Promise<void>}
*/ */
private handleWriteJournalEntriesOnInvoiceCreated = async ({ private handleWriteJournalEntriesOnInvoiceCreated = async ({
tenantId, tenantId,
@@ -55,6 +57,8 @@ export default class SaleInvoiceWriteGLEntriesSubscriber {
/** /**
* Records journal entries of the non-inventory invoice. * Records journal entries of the non-inventory invoice.
* @param {ISaleInvoiceEditedPayload} payload -
* @returns {Promise<void>}
*/ */
private handleRewriteJournalEntriesOnceInvoiceEdit = async ({ private handleRewriteJournalEntriesOnceInvoiceEdit = async ({
tenantId, tenantId,
@@ -73,6 +77,8 @@ export default class SaleInvoiceWriteGLEntriesSubscriber {
/** /**
* Handle reverting journal entries once sale invoice delete. * Handle reverting journal entries once sale invoice delete.
* @param {ISaleInvoiceDeletePayload} payload -
* @returns {Promise<void>}
*/ */
private handleRevertingInvoiceJournalEntriesOnDelete = async ({ private handleRevertingInvoiceJournalEntriesOnDelete = async ({
tenantId, tenantId,