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

@@ -1,7 +1,7 @@
import { Knex } from 'knex';
import { IDynamicListFilterDTO } from './DynamicFilter';
import { IItemEntry, IItemEntryDTO } from './ItemEntry';
import { IBillLandedCost } from './LandedCost';
import { IBillLandedCost } from './LandedCost';
export interface IBillDTO {
vendorId: number;
billNumber: string;
@@ -99,17 +99,17 @@ export interface IBillCreatedPayload {
trx: Knex.Transaction;
}
export interface IBillCreatingPayload{
export interface IBillCreatingPayload {
tenantId: number;
billDTO: IBillDTO;
trx: Knex.Transaction;
trx: Knex.Transaction;
}
export interface IBillEditingPayload {
tenantId: number;
oldBill: IBill;
billDTO: IBillEditDTO;
trx: Knex.Transaction;
trx: Knex.Transaction;
}
export interface IBillEditedPayload {
tenantId: number;
@@ -129,7 +129,7 @@ export interface IBIllEventDeletedPayload {
export interface IBillEventDeletingPayload {
tenantId: number;
oldBill: IBill;
trx: Knex.Transaction;
trx: Knex.Transaction;
}
export enum BillAction {
Create = 'Create',
@@ -138,3 +138,16 @@ export enum BillAction {
View = 'View',
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 { pick } from 'lodash';
import { ISystemUser } from '@/interfaces';
import { ILedgerEntry } from './Ledger';
import { ISaleInvoice } from './SaleInvoice';

View File

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

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

View File

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

View File

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

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

View File

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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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