mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 23:00:34 +00:00
fix: expense writing journal entries.
fix: delete expense from the storage.
This commit is contained in:
@@ -36,7 +36,7 @@ export default class ExpenseRepository extends TenantRepository {
|
|||||||
*/
|
*/
|
||||||
async create(expenseInput: IExpense): Promise<void> {
|
async create(expenseInput: IExpense): Promise<void> {
|
||||||
const { Expense } = this.models;
|
const { Expense } = this.models;
|
||||||
const expense = await Expense.query().insert({ ...expenseInput });
|
const expense = await Expense.query().insertGraph({ ...expenseInput });
|
||||||
this.flushCache();
|
this.flushCache();
|
||||||
|
|
||||||
return expense;
|
return expense;
|
||||||
@@ -72,10 +72,10 @@ export default class ExpenseRepository extends TenantRepository {
|
|||||||
* @param {number} expenseId
|
* @param {number} expenseId
|
||||||
*/
|
*/
|
||||||
async delete(expenseId: number): Promise<void> {
|
async delete(expenseId: number): Promise<void> {
|
||||||
const { Expense } = this.models;
|
const { Expense, ExpenseCategory } = this.models;
|
||||||
|
|
||||||
|
await ExpenseCategory.query().where('expense_id', expenseId).delete();
|
||||||
await Expense.query().where('id', expenseId).delete();
|
await Expense.query().where('id', expenseId).delete();
|
||||||
await Expense.query().where('expense_id', expenseId).delete();
|
|
||||||
|
|
||||||
this.flushCache();
|
this.flushCache();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,16 +138,18 @@ export default class JournalCommands{
|
|||||||
const paymentJournalEntry = new JournalEntry({
|
const paymentJournalEntry = new JournalEntry({
|
||||||
credit: expense.totalAmount,
|
credit: expense.totalAmount,
|
||||||
account: expense.paymentAccountId,
|
account: expense.paymentAccountId,
|
||||||
|
index: 1,
|
||||||
...mixinEntry,
|
...mixinEntry,
|
||||||
});
|
});
|
||||||
this.journal.credit(paymentJournalEntry);
|
this.journal.credit(paymentJournalEntry);
|
||||||
|
|
||||||
expense.categories.forEach((category: IExpenseCategory) => {
|
expense.categories.forEach((category: IExpenseCategory, index) => {
|
||||||
const expenseJournalEntry = new JournalEntry({
|
const expenseJournalEntry = new JournalEntry({
|
||||||
account: category.expenseAccountId,
|
account: category.expenseAccountId,
|
||||||
debit: category.amount,
|
debit: category.amount,
|
||||||
note: category.description,
|
note: category.description,
|
||||||
...mixinEntry,
|
...mixinEntry,
|
||||||
|
index: index + 2,
|
||||||
});
|
});
|
||||||
this.journal.debit(expenseJournalEntry);
|
this.journal.debit(expenseJournalEntry);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -143,16 +143,15 @@ export default class ExpensesService implements IExpensesService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async revertJournalEntries(
|
public async revertJournalEntries(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
expenseId: number|number[],
|
expenseId: number|number[],
|
||||||
) {
|
) {
|
||||||
const journal = new JournalPoster(tenantId);
|
const journal = new JournalPoster(tenantId);
|
||||||
const journalCommands = new JournalCommands(journal);
|
const journalCommands = new JournalCommands(journal);
|
||||||
|
|
||||||
if (revertOld) {
|
await journalCommands.revertJournalEntries(expenseId, 'Expense');
|
||||||
await journalCommands.revertJournalEntries(expenseId, 'Expense');
|
|
||||||
}
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
journal.saveBalance(),
|
journal.saveBalance(),
|
||||||
journal.deleteEntries(),
|
journal.deleteEntries(),
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export default class JournalPosterService {
|
|||||||
* @param {string} referenceType - The transaction reference type.
|
* @param {string} referenceType - The transaction reference type.
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
async deleteJournalTransactions(
|
async revertJournalTransactions(
|
||||||
tenantId: number,
|
tenantId: number,
|
||||||
referenceId: number,
|
referenceId: number,
|
||||||
referenceType: string
|
referenceType: string
|
||||||
|
|||||||
@@ -3,61 +3,84 @@ import { EventSubscriber, On } from 'event-dispatch';
|
|||||||
import events from 'subscribers/events';
|
import events from 'subscribers/events';
|
||||||
import ExpensesService from 'services/Expenses/ExpensesService';
|
import ExpensesService from 'services/Expenses/ExpensesService';
|
||||||
import TenancyService from 'services/Tenancy/TenancyService';
|
import TenancyService from 'services/Tenancy/TenancyService';
|
||||||
import {
|
import ExpenseRepository from 'repositories/ExpenseRepository';
|
||||||
EventDispatcher,
|
|
||||||
EventDispatcherInterface,
|
|
||||||
} from 'decorators/eventDispatcher';
|
|
||||||
|
|
||||||
@Service()
|
@EventSubscriber()
|
||||||
export default class ExpensesSubscriber {
|
export default class ExpensesSubscriber {
|
||||||
|
tenancy: TenancyService;
|
||||||
constructor(
|
expensesService: ExpensesService;
|
||||||
@Inject()
|
|
||||||
tenancy: TenancyService,
|
|
||||||
|
|
||||||
@EventDispatcher()
|
constructor() {
|
||||||
eventDispatcher: EventDispatcherInterface,
|
this.tenancy = Container.get(TenancyService);
|
||||||
) {
|
this.expensesService = Container.get(ExpensesService);
|
||||||
console.log(this, 'XXXX');
|
|
||||||
// this.eventDispatcher.on(events.expenses.onCreated, this.onExpenseCreated);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public onExpenseCreated({ expenseId, tenantId }) {
|
/**
|
||||||
console.log(this)
|
* On expense created.
|
||||||
|
*/
|
||||||
|
@On(events.expenses.onCreated)
|
||||||
|
public async onExpenseCreated({ expenseId, tenantId }) {
|
||||||
|
const { expenseRepository } = this.tenancy.repositories(tenantId);
|
||||||
|
const expense = await expenseRepository.getById(expenseId);
|
||||||
|
|
||||||
// // 7. In case expense published, write journal entries.
|
// In case expense published, write journal entries.
|
||||||
// if (expenseObj.publishedAt) {
|
if (expense.publishedAt) {
|
||||||
// await this.writeJournalEntries(tenantId, expenseModel, false);
|
await this.expensesService.writeJournalEntries(tenantId, expense, false);
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On expense edited.
|
||||||
|
*/
|
||||||
|
@On(events.expenses.onEdited)
|
||||||
|
public async onExpenseEdited({ expenseId, tenantId }) {
|
||||||
|
const { expenseRepository } = this.tenancy.repositories(tenantId);
|
||||||
|
const expense = await expenseRepository.getById(expenseId);
|
||||||
|
|
||||||
|
// In case expense published, write journal entries.
|
||||||
|
if (expense.publishedAt) {
|
||||||
|
await this.expensesService.writeJournalEntries(tenantId, expense, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @On(events.expenses.onEdited)
|
/**
|
||||||
public onExpenseEdited({ expenseId, tenantId }) {
|
*
|
||||||
// - In case expense published, write journal entries.
|
* @param param0
|
||||||
// if (expenseObj.publishedAt) {
|
*/
|
||||||
// await this.writeJournalEntries(tenantId, expenseModel, true, authorizedUser);
|
@On(events.expenses.onDeleted)
|
||||||
// }
|
public async onExpenseDeleted({ expenseId, tenantId }) {
|
||||||
|
await this.expensesService.revertJournalEntries(tenantId, expenseId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @On(events.expenses.onDeleted)
|
/**
|
||||||
public onExpenseDeleted({ expenseId, tenantId }) {
|
*
|
||||||
// if (expense.published) {
|
* @param param0
|
||||||
// await this.revertJournalEntries(tenantId, expenseId);
|
*/
|
||||||
// }
|
@On(events.expenses.onPublished)
|
||||||
|
public async onExpensePublished({ expenseId, tenantId }) {
|
||||||
|
const { expenseRepository } = this.tenancy.repositories(tenantId);
|
||||||
|
const expense = await expenseRepository.getById(expenseId);
|
||||||
|
|
||||||
|
// In case expense published, write journal entries.
|
||||||
|
if (expense.publishedAt) {
|
||||||
|
await this.expensesService.writeJournalEntries(tenantId, expense, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @On(events.expenses.onPublished)
|
/**
|
||||||
public onExpensePublished({ expenseId, tenantId }) {
|
*
|
||||||
|
* @param param0
|
||||||
}
|
*/
|
||||||
|
@On(events.expenses.onBulkDeleted)
|
||||||
// @On(events.expenses.onBulkDeleted)
|
|
||||||
public onExpenseBulkDeleted({ expensesIds, tenantId }) {
|
public onExpenseBulkDeleted({ expensesIds, tenantId }) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @On(events.expenses.onBulkPublished)
|
/**
|
||||||
|
*
|
||||||
|
* @param param0
|
||||||
|
*/
|
||||||
|
@On(events.expenses.onBulkPublished)
|
||||||
public onExpenseBulkPublished({ expensesIds, tenantId }) {
|
public onExpenseBulkPublished({ expensesIds, tenantId }) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user