Merge pull request #221 from bigcapitalhq/abouhuolia/big-56-should-not-write-gl-entries-when-save-transaction-as-draft

fix(server): shouldn't write GL entries when save transaction as draft.
This commit is contained in:
Ahmed Bouhuolia
2023-08-20 23:04:31 +02:00
committed by GitHub
22 changed files with 184 additions and 58 deletions

View File

@@ -99,7 +99,7 @@ export interface IBillCreatedPayload {
trx: Knex.Transaction; trx: Knex.Transaction;
} }
export interface IBillCreatingPayload{ export interface IBillCreatingPayload {
tenantId: number; tenantId: number;
billDTO: IBillDTO; billDTO: IBillDTO;
trx: Knex.Transaction; trx: Knex.Transaction;
@@ -138,3 +138,16 @@ export enum BillAction {
View = 'View', View = 'View',
NotifyBySms = 'NotifyBySms', NotifyBySms = 'NotifyBySms',
} }
export interface IBillOpeningPayload {
trx: Knex.Transaction;
tenantId: number;
oldBill: IBill;
}
export interface IBillOpenedPayload {
trx: Knex.Transaction;
bill: IBill;
oldBill: IBill;
tenantId: number;
}

View File

@@ -1,6 +1,5 @@
import { ISystemUser } from '@/interfaces';
import { Knex } from 'knex'; import { Knex } from 'knex';
import { pick } from 'lodash'; import { ISystemUser } from '@/interfaces';
import { ILedgerEntry } from './Ledger'; import { ILedgerEntry } from './Ledger';
import { ISaleInvoice } from './SaleInvoice'; import { ISaleInvoice } from './SaleInvoice';

View File

@@ -156,6 +156,7 @@ export interface ISaleInvoiceEventDeliveredPayload {
tenantId: number; tenantId: number;
saleInvoiceId: number; saleInvoiceId: number;
saleInvoice: ISaleInvoice; saleInvoice: ISaleInvoice;
trx: Knex.Transaction;
} }
export interface ISaleInvoiceDeliveringPayload { export interface ISaleInvoiceDeliveringPayload {

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( await this.manualJournalGLEntries.createManualJournalGLEntries(
tenantId, tenantId,
manualJournal.id, manualJournal.id,
trx 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';
@@ -20,6 +21,10 @@ export class BillGLEntriesSubscriber {
events.bill.onCreated, events.bill.onCreated,
this.handlerWriteJournalEntriesOnCreate this.handlerWriteJournalEntriesOnCreate
); );
bus.subscribe(
events.bill.onOpened,
this.handlerWriteJournalEntriesOnCreate
);
bus.subscribe( bus.subscribe(
events.bill.onEdited, events.bill.onEdited,
this.handleOverwriteJournalEntriesOnEdit this.handleOverwriteJournalEntriesOnEdit
@@ -33,10 +38,12 @@ export class BillGLEntriesSubscriber {
*/ */
private handlerWriteJournalEntriesOnCreate = async ({ private handlerWriteJournalEntriesOnCreate = async ({
tenantId, tenantId,
billId, bill,
trx, trx,
}: IBillCreatedPayload) => { }: IBillCreatedPayload | IBillOpenedPayload) => {
await this.billGLEntries.writeBillGLEntries(tenantId, billId, trx); if (!bill.openedAt) return null;
await this.billGLEntries.writeBillGLEntries(tenantId, bill.id, trx);
}; };
/** /**
@@ -46,8 +53,11 @@ export class BillGLEntriesSubscriber {
private handleOverwriteJournalEntriesOnEdit = async ({ private handleOverwriteJournalEntriesOnEdit = async ({
tenantId, tenantId,
billId, billId,
bill,
trx, trx,
}: IBillEditedPayload) => { }: IBillEditedPayload) => {
if (!bill.openedAt) return null;
await this.billGLEntries.rewriteBillGLEntries(tenantId, billId, trx); await this.billGLEntries.rewriteBillGLEntries(tenantId, billId, trx);
}; };

View File

@@ -132,7 +132,7 @@ export class EditBill {
} as IBillEditingPayload); } as IBillEditingPayload);
// Update the bill transaction. // Update the bill transaction.
const bill = await Bill.query(trx).upsertGraph({ const bill = await Bill.query(trx).upsertGraphAndFetch({
id: billId, id: billId,
...billObj, ...billObj,
}); });

View File

@@ -5,6 +5,9 @@ import { ERRORS } from './constants';
import HasTenancyService from '@/services/Tenancy/TenancyService'; import HasTenancyService from '@/services/Tenancy/TenancyService';
import UnitOfWork from '@/services/UnitOfWork'; import UnitOfWork from '@/services/UnitOfWork';
import { BillsValidators } from './BillsValidators'; import { BillsValidators } from './BillsValidators';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { IBillOpenedPayload, IBillOpeningPayload } from '@/interfaces';
@Service() @Service()
export class OpenBill { export class OpenBill {
@@ -17,6 +20,9 @@ export class OpenBill {
@Inject() @Inject()
private validators: BillsValidators; private validators: BillsValidators;
@Inject()
private eventPublisher: EventPublisher;
/** /**
* Mark the bill as open. * Mark the bill as open.
* @param {number} tenantId * @param {number} tenantId
@@ -37,10 +43,27 @@ export class OpenBill {
throw new ServiceError(ERRORS.BILL_ALREADY_OPEN); throw new ServiceError(ERRORS.BILL_ALREADY_OPEN);
} }
return this.uow.withTransaction(tenantId, async (trx) => { return this.uow.withTransaction(tenantId, async (trx) => {
// Record the bill opened at on the storage. // Triggers `onBillCreating` event.
await Bill.query(trx).findById(billId).patch({ await this.eventPublisher.emitAsync(events.bill.onOpening, {
trx,
tenantId,
oldBill,
} as IBillOpeningPayload);
// Save the bill opened at on the storage.
const bill = await Bill.query(trx)
.patchAndFetchById(billId, {
openedAt: moment().toMySqlDateTime(), openedAt: moment().toMySqlDateTime(),
}); })
.withGraphFetched('entries');
// Triggers `onBillCreating` event.
await this.eventPublisher.emitAsync(events.bill.onOpened, {
trx,
bill,
oldBill,
tenantId,
} as IBillOpenedPayload);
}); });
} }
} }

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

@@ -63,14 +63,17 @@ export class DeliverSaleInvoice {
// Record the delivered at on the storage. // Record the delivered at on the storage.
const saleInvoice = await SaleInvoice.query(trx) const saleInvoice = await SaleInvoice.query(trx)
.where({ id: saleInvoiceId }) .patchAndFetchById(saleInvoiceId, {
.update({ deliveredAt: moment().toMySqlDateTime() }); deliveredAt: moment().toMySqlDateTime(),
})
.withGraphFetched('entries');
// Triggers `onSaleInvoiceDelivered` event. // Triggers `onSaleInvoiceDelivered` event.
await this.eventPublisher.emitAsync(events.saleInvoice.onDelivered, { await this.eventPublisher.emitAsync(events.saleInvoice.onDelivered, {
tenantId, tenantId,
saleInvoiceId, saleInvoiceId,
saleInvoice, saleInvoice,
trx,
} as ISaleInvoiceEventDeliveredPayload); } as ISaleInvoiceEventDeliveredPayload);
}); });
} }

View File

@@ -58,12 +58,12 @@ export class CloseSaleReceipt {
} as ISaleReceiptEventClosingPayload); } as ISaleReceiptEventClosingPayload);
// Mark the sale receipt as closed on the storage. // Mark the sale receipt as closed on the storage.
const saleReceipt = await SaleReceipt.query(trx) const saleReceipt = await SaleReceipt.query(trx).patchAndFetchById(
.findById(saleReceiptId) saleReceiptId,
.patch({ {
closedAt: moment().toMySqlDateTime(), closedAt: moment().toMySqlDateTime(),
}); }
);
// Triggers `onSaleReceiptClosed` event. // Triggers `onSaleReceiptClosed` event.
await this.eventPublisher.emitAsync(events.saleReceipt.onClosed, { await this.eventPublisher.emitAsync(events.saleReceipt.onClosed, {
saleReceiptId, saleReceiptId,

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,15 +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,
trx, trx,
}: IBillCreatedPayload) => { }: IBillCreatedPayload | IBillOpenedPayload) => {
// Can't continue if the bill is not opened yet.
if (!bill.openedAt) return null;
await this.billsInventory.recordInventoryTransactions( await this.billsInventory.recordInventoryTransactions(
tenantId, tenantId,
billId, bill.id,
false, false,
trx trx
); );
@@ -48,12 +57,17 @@ 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,
billId, billId,
bill,
trx, trx,
}: IBillEditedPayload) => { }: IBillEditedPayload) => {
// Can't continue if the bill is not opened yet.
if (!bill.openedAt) return null;
await this.billsInventory.recordInventoryTransactions( await this.billsInventory.recordInventoryTransactions(
tenantId, tenantId,
billId, billId,
@@ -64,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
@@ -38,8 +38,12 @@ export default class BillWriteGLEntriesSubscriber {
private handlerWriteJournalEntriesOnCreate = async ({ private handlerWriteJournalEntriesOnCreate = async ({
tenantId, tenantId,
billId, billId,
bill,
trx, trx,
}: IBillCreatedPayload) => { }: IBillCreatedPayload) => {
// Can't continue if the bill is not opened yet.
if (!bill.openedAt) return null;
await this.billsService.recordJournalTransactions( await this.billsService.recordJournalTransactions(
tenantId, tenantId,
billId, billId,
@@ -55,8 +59,12 @@ export default class BillWriteGLEntriesSubscriber {
private handleOverwriteJournalEntriesOnEdit = async ({ private handleOverwriteJournalEntriesOnEdit = async ({
tenantId, tenantId,
billId, billId,
bill,
trx, trx,
}: IBillEditedPayload) => { }: IBillEditedPayload) => {
// Can't continue if the bill is not opened yet.
if (!bill.openedAt) return null;
await this.billsService.recordJournalTransactions( await this.billsService.recordJournalTransactions(
tenantId, tenantId,
billId, billId,

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

@@ -4,6 +4,7 @@ import {
ISaleInvoiceCreatedPayload, ISaleInvoiceCreatedPayload,
ISaleInvoiceDeletedPayload, ISaleInvoiceDeletedPayload,
ISaleInvoiceEditedPayload, ISaleInvoiceEditedPayload,
ISaleInvoiceEventDeliveredPayload,
} from '@/interfaces'; } from '@/interfaces';
import { InvoiceInventoryTransactions } from '@/services/Sales/Invoices/InvoiceInventoryTransactions'; import { InvoiceInventoryTransactions } from '@/services/Sales/Invoices/InvoiceInventoryTransactions';
@@ -20,6 +21,10 @@ export default class WriteInventoryTransactions {
events.saleInvoice.onCreated, events.saleInvoice.onCreated,
this.handleWritingInventoryTransactions this.handleWritingInventoryTransactions
); );
bus.subscribe(
events.saleInvoice.onDelivered,
this.handleWritingInventoryTransactions
);
bus.subscribe( bus.subscribe(
events.saleInvoice.onEdited, events.saleInvoice.onEdited,
this.handleRewritingInventoryTransactions this.handleRewritingInventoryTransactions
@@ -38,7 +43,10 @@ export default class WriteInventoryTransactions {
tenantId, tenantId,
saleInvoice, saleInvoice,
trx, trx,
}: ISaleInvoiceCreatedPayload) => { }: ISaleInvoiceCreatedPayload | ISaleInvoiceEventDeliveredPayload) => {
// Can't continue if the sale invoice is not delivered yet.
if (!saleInvoice.deliveredAt) return null;
await this.saleInvoiceInventory.recordInventoryTranscactions( await this.saleInvoiceInventory.recordInventoryTranscactions(
tenantId, tenantId,
saleInvoice, saleInvoice,

View File

@@ -15,11 +15,15 @@ export default class SaleInvoiceWriteGLEntriesSubscriber {
/** /**
* Constructor method. * Constructor method.
*/ */
attach(bus) { public attach(bus) {
bus.subscribe( bus.subscribe(
events.saleInvoice.onCreated, events.saleInvoice.onCreated,
this.handleWriteJournalEntriesOnInvoiceCreated this.handleWriteJournalEntriesOnInvoiceCreated
); );
bus.subscribe(
events.saleInvoice.onDelivered,
this.handleWriteJournalEntriesOnInvoiceCreated
);
bus.subscribe( bus.subscribe(
events.saleInvoice.onEdited, events.saleInvoice.onEdited,
this.handleRewriteJournalEntriesOnceInvoiceEdit this.handleRewriteJournalEntriesOnceInvoiceEdit
@@ -32,12 +36,18 @@ 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,
saleInvoiceId, saleInvoiceId,
saleInvoice,
trx, trx,
}: ISaleInvoiceCreatedPayload) => { }: ISaleInvoiceCreatedPayload) => {
// Can't continue if the sale invoice is not delivered yet.
if (!saleInvoice.deliveredAt) return null;
await this.saleInvoiceGLEntries.writeInvoiceGLEntries( await this.saleInvoiceGLEntries.writeInvoiceGLEntries(
tenantId, tenantId,
saleInvoiceId, saleInvoiceId,
@@ -47,12 +57,17 @@ 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,
saleInvoice, saleInvoice,
trx, trx,
}: ISaleInvoiceEditedPayload) => { }: ISaleInvoiceEditedPayload) => {
// Can't continue if the sale invoice is not delivered yet.
if (!saleInvoice.deliveredAt) return null;
await this.saleInvoiceGLEntries.rewritesInvoiceGLEntries( await this.saleInvoiceGLEntries.rewritesInvoiceGLEntries(
tenantId, tenantId,
saleInvoice.id, saleInvoice.id,
@@ -62,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,

View File

@@ -40,6 +40,9 @@ export default class SaleReceiptInventoryTransactionsSubscriber {
saleReceipt, saleReceipt,
trx, trx,
}: ISaleReceiptCreatedPayload) => { }: ISaleReceiptCreatedPayload) => {
// Can't continue if the sale receipt is not closed yet.
if (!saleReceipt.closedAt) return null;
await this.saleReceiptInventory.recordInventoryTransactions( await this.saleReceiptInventory.recordInventoryTransactions(
tenantId, tenantId,
saleReceipt, saleReceipt,
@@ -57,6 +60,9 @@ export default class SaleReceiptInventoryTransactionsSubscriber {
saleReceipt, saleReceipt,
trx, trx,
}: ISaleReceiptEditedPayload) => { }: ISaleReceiptEditedPayload) => {
// Can't continue if the sale receipt is not closed yet.
if (!saleReceipt.closedAt) return null;
await this.saleReceiptInventory.recordInventoryTransactions( await this.saleReceiptInventory.recordInventoryTransactions(
tenantId, tenantId,
saleReceipt, saleReceipt,

View File

@@ -21,6 +21,10 @@ export default class SaleReceiptWriteGLEntriesSubscriber {
events.saleReceipt.onCreated, events.saleReceipt.onCreated,
this.handleWriteReceiptIncomeJournalEntrieOnCreate this.handleWriteReceiptIncomeJournalEntrieOnCreate
); );
bus.subscribe(
events.saleReceipt.onClosed,
this.handleWriteReceiptIncomeJournalEntrieOnCreate
);
bus.subscribe( bus.subscribe(
events.saleReceipt.onEdited, events.saleReceipt.onEdited,
this.handleWriteReceiptIncomeJournalEntrieOnEdited this.handleWriteReceiptIncomeJournalEntrieOnEdited
@@ -38,8 +42,12 @@ export default class SaleReceiptWriteGLEntriesSubscriber {
public handleWriteReceiptIncomeJournalEntrieOnCreate = async ({ public handleWriteReceiptIncomeJournalEntrieOnCreate = async ({
tenantId, tenantId,
saleReceiptId, saleReceiptId,
saleReceipt,
trx, trx,
}: ISaleReceiptCreatedPayload) => { }: ISaleReceiptCreatedPayload) => {
// Can't continue if the sale receipt is not closed yet.
if (!saleReceipt.closedAt) return null;
// Writes the sale receipt income journal entries. // Writes the sale receipt income journal entries.
await this.saleReceiptGLEntries.writeIncomeGLEntries( await this.saleReceiptGLEntries.writeIncomeGLEntries(
tenantId, tenantId,
@@ -71,8 +79,12 @@ export default class SaleReceiptWriteGLEntriesSubscriber {
private handleWriteReceiptIncomeJournalEntrieOnEdited = async ({ private handleWriteReceiptIncomeJournalEntrieOnEdited = async ({
tenantId, tenantId,
saleReceiptId, saleReceiptId,
saleReceipt,
trx, trx,
}: ISaleReceiptEditedPayload) => { }: ISaleReceiptEditedPayload) => {
// Can't continue if the sale receipt is not closed yet.
if (!saleReceipt.closedAt) return null;
// Writes the sale receipt income journal entries. // Writes the sale receipt income journal entries.
await this.saleReceiptGLEntries.rewriteReceiptGLEntries( await this.saleReceiptGLEntries.rewriteReceiptGLEntries(
tenantId, tenantId,

View File

@@ -220,6 +220,9 @@ export default {
onPublishing: 'onBillPublishing', onPublishing: 'onBillPublishing',
onPublished: 'onBillPublished', onPublished: 'onBillPublished',
onOpening: 'onBillOpening',
onOpened: 'onBillOpened',
}, },
/** /**