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()
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.

View File

@@ -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

View File

@@ -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<void>}
*/
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<void>}
*/
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,

View File

@@ -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

View File

@@ -48,6 +48,7 @@ export class ManualJournalWriteGLSubscriber {
/**
* Handle manual journal created event.
* @param {IManualJournalEventCreatedPayload} payload -
* @returns {Promise<void>}
*/
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<void>}
*/
private handleJournalNumberIncrement = async ({
tenantId,
@@ -77,6 +79,7 @@ export class ManualJournalWriteGLSubscriber {
/**
* Handle manual journal edited event.
* @param {IManualJournalEventEditedPayload}
* @return {Promise<void>}
*/
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<void>}
*/
private handleWriteJournalEntriesOnPublished = async ({
tenantId,

View File

@@ -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);
};
/**

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

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

View File

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

View File

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

View File

@@ -36,6 +36,8 @@ export default class SaleInvoiceWriteGLEntriesSubscriber {
/**
* Records journal entries of the non-inventory invoice.
* @param {ISaleInvoiceCreatedPayload} payload -
* @returns {Promise<void>}
*/
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<void>}
*/
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<void>}
*/
private handleRevertingInvoiceJournalEntriesOnDelete = async ({
tenantId,