Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): shouldn't write GL entries when save transaction as draft. #221

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions packages/server/src/interfaces/Bill.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -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;
}
3 changes: 1 addition & 2 deletions packages/server/src/interfaces/PaymentReceive.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
1 change: 1 addition & 0 deletions packages/server/src/interfaces/SaleInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export interface ISaleInvoiceEventDeliveredPayload {
tenantId: number;
saleInvoiceId: number;
saleInvoice: ISaleInvoice;
trx: Knex.Transaction;
}

export interface ISaleInvoiceDeliveringPayload {
Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/services/CreditNotes/CreateCreditNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Inject } from 'typedi';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import events from '@/subscribers/events';
import {
IApplyCreditToInvoicesCreatedPayload,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class CreditNoteInventoryTransactionsSubscriber {
/**
* Attaches events with publisher.
*/
attach(bus) {
public attach(bus) {
bus.subscribe(
events.creditNote.onCreated,
this.writeInventoryTranscationsOnceCreated
Expand All @@ -37,16 +37,16 @@ export default class CreditNoteInventoryTransactionsSubscriber {
/**
* Writes inventory transactions once credit note created.
* @param {ICreditNoteCreatedPayload} payload -
* @returns {Promise<void>}
*/
public writeInventoryTranscationsOnceCreated = async ({
tenantId,
creditNote,
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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,27 @@ export class ManualJournalWriteGLSubscriber {
/**
* Handle manual journal created event.
* @param {IManualJournalEventCreatedPayload} payload -
* @returns {Promise<void>}
*/
private handleWriteJournalEntriesOnCreated = async ({
tenantId,
manualJournal,
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,
Expand All @@ -77,6 +79,7 @@ export class ManualJournalWriteGLSubscriber {
/**
* Handle manual journal edited event.
* @param {IManualJournalEventEditedPayload}
* @return {Promise<void>}
*/
private handleRewriteJournalEntriesOnEdited = async ({
tenantId,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
IBillCreatedPayload,
IBillEditedPayload,
IBIllEventDeletedPayload,
IBillOpenedPayload,
} from '@/interfaces';
import { BillGLEntries } from './BillGLEntries';

Expand All @@ -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
Expand All @@ -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);
};

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

Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/services/Purchases/Bills/EditBill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
31 changes: 27 additions & 4 deletions packages/server/src/services/Purchases/Bills/OpenBill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,6 +20,9 @@ export class OpenBill {
@Inject()
private validators: BillsValidators;

@Inject()
private eventPublisher: EventPublisher;

/**
* Mark the bill as open.
* @param {number} tenantId
Expand All @@ -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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import VendorCreditInventoryTransactions from './VendorCreditInventoryTransactio
@Service()
export default class VendorCreditInventoryTransactionsSubscriber {
@Inject()
inventoryTransactions: VendorCreditInventoryTransactions;
private inventoryTransactions: VendorCreditInventoryTransactions;

/**
* Attaches events with handlers.
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Loading
Loading