diff --git a/packages/server/src/services/CreditNotes/CreateCreditNote.ts b/packages/server/src/services/CreditNotes/CreateCreditNote.ts index 2c4c0d58f..641d1034e 100644 --- a/packages/server/src/services/CreditNotes/CreateCreditNote.ts +++ b/packages/server/src/services/CreditNotes/CreateCreditNote.ts @@ -16,16 +16,16 @@ import BaseCreditNotes from './CreditNotes'; @Service() export default class CreateCreditNote extends BaseCreditNotes { @Inject() - uow: UnitOfWork; + private uow: UnitOfWork; @Inject() - itemsEntriesService: ItemsEntriesService; + private itemsEntriesService: ItemsEntriesService; @Inject() - tenancy: HasTenancyService; + private tenancy: HasTenancyService; @Inject() - eventPublisher: EventPublisher; + private eventPublisher: EventPublisher; /** * Creates a new credit note. diff --git a/packages/server/src/services/CreditNotes/CreditNoteApplySyncInvoicesSubscriber.ts b/packages/server/src/services/CreditNotes/CreditNoteApplySyncInvoicesSubscriber.ts index 3a4e7f59b..f22a6c03e 100644 --- a/packages/server/src/services/CreditNotes/CreditNoteApplySyncInvoicesSubscriber.ts +++ b/packages/server/src/services/CreditNotes/CreditNoteApplySyncInvoicesSubscriber.ts @@ -1,5 +1,4 @@ import { Service, Inject } from 'typedi'; -import HasTenancyService from '@/services/Tenancy/TenancyService'; import events from '@/subscribers/events'; import { IApplyCreditToInvoicesCreatedPayload, @@ -10,15 +9,12 @@ import CreditNoteApplySyncInvoicesCreditedAmount from './CreditNoteApplySyncInvo @Service() export default class CreditNoteApplySyncInvoicesCreditedAmountSubscriber { @Inject() - tenancy: HasTenancyService; - - @Inject() - syncInvoicesWithCreditNote: CreditNoteApplySyncInvoicesCreditedAmount; + private syncInvoicesWithCreditNote: CreditNoteApplySyncInvoicesCreditedAmount; /** * Attaches events with handlers. */ - attach(bus) { + public attach(bus) { bus.subscribe( events.creditNote.onApplyToInvoicesCreated, this.incrementAppliedInvoicesOnceCreditCreated diff --git a/packages/server/src/services/CreditNotes/CreditNoteInventoryTransactionsSubscriber.ts b/packages/server/src/services/CreditNotes/CreditNoteInventoryTransactionsSubscriber.ts index 7b23d079d..54b702bea 100644 --- a/packages/server/src/services/CreditNotes/CreditNoteInventoryTransactionsSubscriber.ts +++ b/packages/server/src/services/CreditNotes/CreditNoteInventoryTransactionsSubscriber.ts @@ -15,7 +15,7 @@ export default class CreditNoteInventoryTransactionsSubscriber { /** * Attaches events with publisher. */ - attach(bus) { + public attach(bus) { bus.subscribe( events.creditNote.onCreated, this.writeInventoryTranscationsOnceCreated @@ -37,6 +37,7 @@ export default class CreditNoteInventoryTransactionsSubscriber { /** * Writes inventory transactions once credit note created. * @param {ICreditNoteCreatedPayload} payload - + * @returns {Promise} */ public writeInventoryTranscationsOnceCreated = async ({ tenantId, @@ -44,9 +45,8 @@ export default class CreditNoteInventoryTransactionsSubscriber { trx, }: ICreditNoteCreatedPayload) => { // Can't continue if the credit note is open yet. - if (!creditNote.isOpen) { - return; - } + if (!creditNote.isOpen) return; + await this.inventoryTransactions.createInventoryTransactions( tenantId, creditNote, @@ -57,6 +57,7 @@ export default class CreditNoteInventoryTransactionsSubscriber { /** * Rewrites inventory transactions once credit note edited. * @param {ICreditNoteEditedPayload} payload - + * @returns {Promise} */ public rewriteInventoryTransactionsOnceEdited = async ({ tenantId, @@ -65,9 +66,8 @@ export default class CreditNoteInventoryTransactionsSubscriber { trx, }: ICreditNoteEditedPayload) => { // Can't continue if the credit note is open yet. - if (!creditNote.isOpen) { - return; - } + if (!creditNote.isOpen) return; + await this.inventoryTransactions.editInventoryTransactions( tenantId, creditNoteId, @@ -87,9 +87,8 @@ export default class CreditNoteInventoryTransactionsSubscriber { trx, }: ICreditNoteDeletedPayload) => { // Can't continue if the credit note is open yet. - if (!oldCreditNote.isOpen) { - return; - } + if (!oldCreditNote.isOpen) return; + await this.inventoryTransactions.deleteInventoryTransactions( tenantId, creditNoteId, diff --git a/packages/server/src/services/Expenses/ExpenseGLEntriesSubscriber.ts b/packages/server/src/services/Expenses/ExpenseGLEntriesSubscriber.ts index 56d37a7db..1a177876e 100644 --- a/packages/server/src/services/Expenses/ExpenseGLEntriesSubscriber.ts +++ b/packages/server/src/services/Expenses/ExpenseGLEntriesSubscriber.ts @@ -21,7 +21,7 @@ export class ExpensesWriteGLSubscriber { * Attaches events with handlers. * @param bus */ - attach(bus) { + public attach(bus) { bus.subscribe( events.expenses.onCreated, this.handleWriteGLEntriesOnceCreated diff --git a/packages/server/src/services/ManualJournals/ManualJournalGLEntriesSubscriber.ts b/packages/server/src/services/ManualJournals/ManualJournalGLEntriesSubscriber.ts index c5efa8f12..21f921d22 100644 --- a/packages/server/src/services/ManualJournals/ManualJournalGLEntriesSubscriber.ts +++ b/packages/server/src/services/ManualJournals/ManualJournalGLEntriesSubscriber.ts @@ -48,6 +48,7 @@ export class ManualJournalWriteGLSubscriber { /** * Handle manual journal created event. * @param {IManualJournalEventCreatedPayload} payload - + * @returns {Promise} */ private handleWriteJournalEntriesOnCreated = async ({ tenantId, @@ -55,18 +56,19 @@ export class ManualJournalWriteGLSubscriber { trx, }: IManualJournalEventCreatedPayload) => { // Ingore writing manual journal journal entries in case was not published. - if (manualJournal.publishedAt) { - await this.manualJournalGLEntries.createManualJournalGLEntries( - tenantId, - manualJournal.id, - trx - ); - } + if (!manualJournal.publishedAt) return; + + await this.manualJournalGLEntries.createManualJournalGLEntries( + tenantId, + manualJournal.id, + trx + ); }; /** * Handles the manual journal next number increment once the journal be created. * @param {IManualJournalEventCreatedPayload} payload - + * @return {Promise} */ private handleJournalNumberIncrement = async ({ tenantId, @@ -77,6 +79,7 @@ export class ManualJournalWriteGLSubscriber { /** * Handle manual journal edited event. * @param {IManualJournalEventEditedPayload} + * @return {Promise} */ private handleRewriteJournalEntriesOnEdited = async ({ tenantId, @@ -96,6 +99,7 @@ export class ManualJournalWriteGLSubscriber { /** * Handles writing journal entries once the manula journal publish. * @param {IManualJournalEventPublishedPayload} payload - + * @return {Promise} */ private handleWriteJournalEntriesOnPublished = async ({ tenantId, diff --git a/packages/server/src/services/Purchases/Bills/BillGLEntriesSubscriber.ts b/packages/server/src/services/Purchases/Bills/BillGLEntriesSubscriber.ts index ea7ed5ab4..9b31cf463 100644 --- a/packages/server/src/services/Purchases/Bills/BillGLEntriesSubscriber.ts +++ b/packages/server/src/services/Purchases/Bills/BillGLEntriesSubscriber.ts @@ -4,6 +4,7 @@ import { IBillCreatedPayload, IBillEditedPayload, IBIllEventDeletedPayload, + IBillOpenedPayload, } from '@/interfaces'; import { BillGLEntries } from './BillGLEntries'; @@ -37,13 +38,12 @@ export class BillGLEntriesSubscriber { */ private handlerWriteJournalEntriesOnCreate = async ({ tenantId, - billId, bill, trx, - }: IBillCreatedPayload) => { + }: IBillCreatedPayload | IBillOpenedPayload) => { if (!bill.openedAt) return null; - await this.billGLEntries.writeBillGLEntries(tenantId, billId, trx); + await this.billGLEntries.writeBillGLEntries(tenantId, bill.id, trx); }; /** diff --git a/packages/server/src/services/Purchases/Bills/OpenBill.ts b/packages/server/src/services/Purchases/Bills/OpenBill.ts index bcd1e397e..25a0da22e 100644 --- a/packages/server/src/services/Purchases/Bills/OpenBill.ts +++ b/packages/server/src/services/Purchases/Bills/OpenBill.ts @@ -50,10 +50,13 @@ export class OpenBill { oldBill, } as IBillOpeningPayload); - // Record the bill opened at on the storage. - const bill = await Bill.query(trx).patchAndFetchById(billId, { - openedAt: moment().toMySqlDateTime(), - }); + // Save the bill opened at on the storage. + const bill = await Bill.query(trx) + .patchAndFetchById(billId, { + openedAt: moment().toMySqlDateTime(), + }) + .withGraphFetched('entries'); + // Triggers `onBillCreating` event. await this.eventPublisher.emitAsync(events.bill.onOpened, { trx, diff --git a/packages/server/src/services/Purchases/VendorCredits/VendorCreditInventoryTransactionsSusbcriber.ts b/packages/server/src/services/Purchases/VendorCredits/VendorCreditInventoryTransactionsSusbcriber.ts index c9b24a4f7..bae3adc23 100644 --- a/packages/server/src/services/Purchases/VendorCredits/VendorCreditInventoryTransactionsSusbcriber.ts +++ b/packages/server/src/services/Purchases/VendorCredits/VendorCreditInventoryTransactionsSusbcriber.ts @@ -10,7 +10,7 @@ import VendorCreditInventoryTransactions from './VendorCreditInventoryTransactio @Service() export default class VendorCreditInventoryTransactionsSubscriber { @Inject() - inventoryTransactions: VendorCreditInventoryTransactions; + private inventoryTransactions: VendorCreditInventoryTransactions; /** * Attaches events with handlers. @@ -21,6 +21,10 @@ export default class VendorCreditInventoryTransactionsSubscriber { events.vendorCredit.onCreated, this.writeInventoryTransactionsOnceCreated ); + bus.subscribe( + events.vendorCredit.onOpened, + this.writeInventoryTransactionsOnceCreated + ); bus.subscribe( events.vendorCredit.onEdited, this.rewriteInventroyTransactionsOnceEdited @@ -40,6 +44,9 @@ export default class VendorCreditInventoryTransactionsSubscriber { vendorCredit, trx, }: IVendorCreditCreatedPayload) => { + // Can't continue if vendor credit is not opened. + if (!vendorCredit.openedAt) return null; + await this.inventoryTransactions.createInventoryTransactions( tenantId, vendorCredit, @@ -57,6 +64,9 @@ export default class VendorCreditInventoryTransactionsSubscriber { vendorCredit, trx, }: IVendorCreditEditedPayload) => { + // Can't continue if vendor credit is not opened. + if (!vendorCredit.openedAt) return null; + await this.inventoryTransactions.editInventoryTransactions( tenantId, vendorCreditId, diff --git a/packages/server/src/subscribers/Bills/WriteInventoryTransactions.ts b/packages/server/src/subscribers/Bills/WriteInventoryTransactions.ts index 04f6039d6..6af4e8551 100644 --- a/packages/server/src/subscribers/Bills/WriteInventoryTransactions.ts +++ b/packages/server/src/subscribers/Bills/WriteInventoryTransactions.ts @@ -4,6 +4,7 @@ import { IBillCreatedPayload, IBillEditedPayload, IBIllEventDeletedPayload, + IBillOpenedPayload, } from '@/interfaces'; import { BillInventoryTransactions } from '@/services/Purchases/Bills/BillInventoryTransactions'; @@ -20,6 +21,10 @@ export default class BillWriteInventoryTransactionsSubscriber { events.bill.onCreated, this.handleWritingInventoryTransactions ); + bus.subscribe( + events.bill.onOpened, + this.handleWritingInventoryTransactions + ); bus.subscribe( events.bill.onEdited, this.handleOverwritingInventoryTransactions @@ -32,19 +37,19 @@ export default class BillWriteInventoryTransactionsSubscriber { /** * Handles writing the inventory transactions once bill created. + * @param {IBillCreatedPayload | IBillOpenedPayload} payload - */ private handleWritingInventoryTransactions = async ({ tenantId, - billId, bill, trx, - }: IBillCreatedPayload) => { + }: IBillCreatedPayload | IBillOpenedPayload) => { // Can't continue if the bill is not opened yet. if (!bill.openedAt) return null; await this.billsInventory.recordInventoryTransactions( tenantId, - billId, + bill.id, false, trx ); @@ -52,6 +57,7 @@ export default class BillWriteInventoryTransactionsSubscriber { /** * Handles the overwriting the inventory transactions once bill edited. + * @param {IBillEditedPayload} payload - */ private handleOverwritingInventoryTransactions = async ({ tenantId, @@ -72,6 +78,7 @@ export default class BillWriteInventoryTransactionsSubscriber { /** * Handles the reverting the inventory transactions once the bill deleted. + * @param {IBIllEventDeletedPayload} payload - */ private handleRevertInventoryTransactions = async ({ tenantId, diff --git a/packages/server/src/subscribers/Bills/WriteJournalEntries.ts b/packages/server/src/subscribers/Bills/WriteJournalEntries.ts index 23f68c6ad..f30694753 100644 --- a/packages/server/src/subscribers/Bills/WriteJournalEntries.ts +++ b/packages/server/src/subscribers/Bills/WriteJournalEntries.ts @@ -19,7 +19,7 @@ export default class BillWriteGLEntriesSubscriber { /** * Attachs events with handles. */ - attach(bus) { + public attach(bus) { bus.subscribe( events.bill.onCreated, this.handlerWriteJournalEntriesOnCreate diff --git a/packages/server/src/subscribers/PaymentReceive/PaymentReceiveSyncInvoices.ts b/packages/server/src/subscribers/PaymentReceive/PaymentReceiveSyncInvoices.ts index 26ac16e14..d257de18c 100644 --- a/packages/server/src/subscribers/PaymentReceive/PaymentReceiveSyncInvoices.ts +++ b/packages/server/src/subscribers/PaymentReceive/PaymentReceiveSyncInvoices.ts @@ -37,7 +37,6 @@ export default class PaymentReceiveSyncInvoicesSubscriber { */ private handleInvoiceIncrementPaymentOnceCreated = async ({ tenantId, - paymentReceiveId, paymentReceive, trx, }: IPaymentReceiveCreatedPayload) => { diff --git a/packages/server/src/subscribers/PaymentReceive/WriteGLEntries.ts b/packages/server/src/subscribers/PaymentReceive/WriteGLEntries.ts index d9ae3b643..bffe4260f 100644 --- a/packages/server/src/subscribers/PaymentReceive/WriteGLEntries.ts +++ b/packages/server/src/subscribers/PaymentReceive/WriteGLEntries.ts @@ -36,10 +36,8 @@ export default class PaymentReceivesWriteGLEntriesSubscriber { private handleWriteJournalEntriesOnceCreated = async ({ tenantId, paymentReceiveId, - paymentReceive, trx, }: IPaymentReceiveCreatedPayload) => { - if (paymentReceive) await this.paymentReceiveGLEntries.writePaymentGLEntries( tenantId, paymentReceiveId, diff --git a/packages/server/src/subscribers/SaleInvoices/WriteJournalEntries.ts b/packages/server/src/subscribers/SaleInvoices/WriteJournalEntries.ts index 15ba5562b..4b1eab698 100644 --- a/packages/server/src/subscribers/SaleInvoices/WriteJournalEntries.ts +++ b/packages/server/src/subscribers/SaleInvoices/WriteJournalEntries.ts @@ -36,6 +36,8 @@ export default class SaleInvoiceWriteGLEntriesSubscriber { /** * Records journal entries of the non-inventory invoice. + * @param {ISaleInvoiceCreatedPayload} payload - + * @returns {Promise} */ private handleWriteJournalEntriesOnInvoiceCreated = async ({ tenantId, @@ -55,6 +57,8 @@ export default class SaleInvoiceWriteGLEntriesSubscriber { /** * Records journal entries of the non-inventory invoice. + * @param {ISaleInvoiceEditedPayload} payload - + * @returns {Promise} */ private handleRewriteJournalEntriesOnceInvoiceEdit = async ({ tenantId, @@ -73,6 +77,8 @@ export default class SaleInvoiceWriteGLEntriesSubscriber { /** * Handle reverting journal entries once sale invoice delete. + * @param {ISaleInvoiceDeletePayload} payload - + * @returns {Promise} */ private handleRevertingInvoiceJournalEntriesOnDelete = async ({ tenantId,