mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
fix(server): shouldn't write GL entries when save transaction as draft.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -156,6 +156,7 @@ export interface ISaleInvoiceEventDeliveredPayload {
|
||||
tenantId: number;
|
||||
saleInvoiceId: number;
|
||||
saleInvoice: ISaleInvoice;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface ISaleInvoiceDeliveringPayload {
|
||||
|
||||
@@ -20,6 +20,10 @@ export class BillGLEntriesSubscriber {
|
||||
events.bill.onCreated,
|
||||
this.handlerWriteJournalEntriesOnCreate
|
||||
);
|
||||
bus.subscribe(
|
||||
events.bill.onOpened,
|
||||
this.handlerWriteJournalEntriesOnCreate
|
||||
);
|
||||
bus.subscribe(
|
||||
events.bill.onEdited,
|
||||
this.handleOverwriteJournalEntriesOnEdit
|
||||
@@ -34,8 +38,11 @@ export class BillGLEntriesSubscriber {
|
||||
private handlerWriteJournalEntriesOnCreate = async ({
|
||||
tenantId,
|
||||
billId,
|
||||
bill,
|
||||
trx,
|
||||
}: IBillCreatedPayload) => {
|
||||
if (!bill.openedAt) return null;
|
||||
|
||||
await this.billGLEntries.writeBillGLEntries(tenantId, billId, 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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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,24 @@ export class OpenBill {
|
||||
throw new ServiceError(ERRORS.BILL_ALREADY_OPEN);
|
||||
}
|
||||
return this.uow.withTransaction(tenantId, async (trx) => {
|
||||
// Triggers `onBillCreating` event.
|
||||
await this.eventPublisher.emitAsync(events.bill.onOpening, {
|
||||
trx,
|
||||
tenantId,
|
||||
oldBill,
|
||||
} as IBillOpeningPayload);
|
||||
|
||||
// Record the bill opened at on the storage.
|
||||
await Bill.query(trx).findById(billId).patch({
|
||||
const bill = await Bill.query(trx).patchAndFetchById(billId, {
|
||||
openedAt: moment().toMySqlDateTime(),
|
||||
});
|
||||
// Triggers `onBillCreating` event.
|
||||
await this.eventPublisher.emitAsync(events.bill.onOpened, {
|
||||
trx,
|
||||
bill,
|
||||
oldBill,
|
||||
tenantId,
|
||||
} as IBillOpenedPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -36,8 +36,12 @@ export default class BillWriteInventoryTransactionsSubscriber {
|
||||
private handleWritingInventoryTransactions = async ({
|
||||
tenantId,
|
||||
billId,
|
||||
bill,
|
||||
trx,
|
||||
}: IBillCreatedPayload) => {
|
||||
// Can't continue if the bill is not opened yet.
|
||||
if (!bill.openedAt) return null;
|
||||
|
||||
await this.billsInventory.recordInventoryTransactions(
|
||||
tenantId,
|
||||
billId,
|
||||
@@ -52,8 +56,12 @@ export default class BillWriteInventoryTransactionsSubscriber {
|
||||
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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -36,8 +36,10 @@ export default class PaymentReceivesWriteGLEntriesSubscriber {
|
||||
private handleWriteJournalEntriesOnceCreated = async ({
|
||||
tenantId,
|
||||
paymentReceiveId,
|
||||
paymentReceive,
|
||||
trx,
|
||||
}: IPaymentReceiveCreatedPayload) => {
|
||||
if (paymentReceive)
|
||||
await this.paymentReceiveGLEntries.writePaymentGLEntries(
|
||||
tenantId,
|
||||
paymentReceiveId,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
@@ -36,8 +40,12 @@ export default class SaleInvoiceWriteGLEntriesSubscriber {
|
||||
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,
|
||||
@@ -53,6 +61,9 @@ export default class SaleInvoiceWriteGLEntriesSubscriber {
|
||||
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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -220,6 +220,9 @@ export default {
|
||||
|
||||
onPublishing: 'onBillPublishing',
|
||||
onPublished: 'onBillPublished',
|
||||
|
||||
onOpening: 'onBillOpening',
|
||||
onOpened: 'onBillOpened',
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user