mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
refactor: GL entries
This commit is contained in:
@@ -12,8 +12,10 @@ import {
|
||||
IExpenseCreateDTO,
|
||||
IExpenseEditDTO,
|
||||
} from './interfaces/Expenses.interface';
|
||||
import { PublicRoute } from '../Auth/Jwt.guard';
|
||||
|
||||
@Controller('expenses')
|
||||
@PublicRoute()
|
||||
export class ExpensesController {
|
||||
constructor(private readonly expensesApplication: ExpensesApplication) {}
|
||||
|
||||
|
||||
@@ -12,9 +12,12 @@ import { TenancyContext } from '../Tenancy/TenancyContext.service';
|
||||
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
|
||||
import { ExpensesWriteGLSubscriber } from './subscribers/ExpenseGLEntries.subscriber';
|
||||
import { ExpenseGLEntriesStorageService } from './subscribers/ExpenseGLEntriesStorage.sevice';
|
||||
import { ExpenseGLEntriesService } from './subscribers/ExpenseGLEntries.service';
|
||||
import { LedgerModule } from '../Ledger/Ledger.module';
|
||||
import { BranchesModule } from '../Branches/Branches.module';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
imports: [LedgerModule, BranchesModule],
|
||||
controllers: [ExpensesController],
|
||||
providers: [
|
||||
CreateExpense,
|
||||
@@ -29,6 +32,7 @@ import { ExpenseGLEntriesStorageService } from './subscribers/ExpenseGLEntriesSt
|
||||
TransformerInjectable,
|
||||
ExpensesWriteGLSubscriber,
|
||||
ExpenseGLEntriesStorageService,
|
||||
ExpenseGLEntriesService
|
||||
],
|
||||
})
|
||||
export class ExpensesModule {}
|
||||
|
||||
@@ -2,10 +2,6 @@ import { Knex } from 'knex';
|
||||
import { Expense } from './models/Expense.model';
|
||||
import { SystemUser } from '../System/models/SystemUser';
|
||||
import { IFilterRole } from '../DynamicListing/DynamicFilter/DynamicFilter.types';
|
||||
// import { ISystemUser } from './User';
|
||||
// import { IFilterRole } from './DynamicFilter';
|
||||
// import { IAccount } from './Account';
|
||||
// import { AttachmentLinkDTO } from './Attachments';
|
||||
|
||||
export interface IPaginationMeta {
|
||||
total: number;
|
||||
@@ -23,55 +19,6 @@ export interface IExpensesFilter {
|
||||
filterQuery?: (query: any) => void;
|
||||
}
|
||||
|
||||
// export interface IExpense {
|
||||
// id: number;
|
||||
// totalAmount: number;
|
||||
// localAmount?: number;
|
||||
// currencyCode: string;
|
||||
// exchangeRate: number;
|
||||
// description?: string;
|
||||
// paymentAccountId: number;
|
||||
// peyeeId?: number;
|
||||
// referenceNo?: string;
|
||||
// publishedAt: Date | null;
|
||||
// userId: number;
|
||||
// paymentDate: Date;
|
||||
// payeeId: number;
|
||||
// landedCostAmount: number;
|
||||
// allocatedCostAmount: number;
|
||||
// unallocatedCostAmount: number;
|
||||
// categories?: IExpenseCategory[];
|
||||
// isPublished: boolean;
|
||||
|
||||
// localLandedCostAmount?: number;
|
||||
// localAllocatedCostAmount?: number;
|
||||
// localUnallocatedCostAmount?: number;
|
||||
|
||||
// billableAmount: number;
|
||||
// invoicedAmount: number;
|
||||
|
||||
// branchId?: number;
|
||||
|
||||
// createdAt?: Date;
|
||||
// }
|
||||
|
||||
// export interface IExpenseCategory {
|
||||
// id?: number;
|
||||
// expenseAccountId: number;
|
||||
// index: number;
|
||||
// description: string;
|
||||
// expenseId: number;
|
||||
// amount: number;
|
||||
|
||||
// projectId?: number;
|
||||
|
||||
// allocatedCostAmount: number;
|
||||
// unallocatedCostAmount: number;
|
||||
// landedCost: boolean;
|
||||
|
||||
// expenseAccount?: IAccount;
|
||||
// }
|
||||
|
||||
export interface IExpenseCommonDTO {
|
||||
currencyCode: string;
|
||||
exchangeRate?: number;
|
||||
@@ -103,68 +50,25 @@ export interface IExpenseCategoryDTO {
|
||||
projectId?: number;
|
||||
}
|
||||
|
||||
// export interface IExpensesService {
|
||||
// newExpense(
|
||||
// tenantid: number,
|
||||
// expenseDTO: IExpenseDTO,
|
||||
// authorizedUser: ISystemUser
|
||||
// ): Promise<IExpense>;
|
||||
|
||||
// editExpense(
|
||||
// tenantid: number,
|
||||
// expenseId: number,
|
||||
// expenseDTO: IExpenseDTO,
|
||||
// authorizedUser: ISystemUser
|
||||
// ): void;
|
||||
|
||||
// publishExpense(
|
||||
// tenantId: number,
|
||||
// expenseId: number,
|
||||
// authorizedUser: ISystemUser
|
||||
// ): Promise<void>;
|
||||
|
||||
// deleteExpense(
|
||||
// tenantId: number,
|
||||
// expenseId: number,
|
||||
// authorizedUser: ISystemUser
|
||||
// ): Promise<void>;
|
||||
|
||||
// getExpensesList(
|
||||
// tenantId: number,
|
||||
// expensesFilter: IExpensesFilter
|
||||
// ): Promise<{
|
||||
// expenses: IExpense[];
|
||||
// pagination: IPaginationMeta;
|
||||
// filterMeta: IFilterMeta;
|
||||
// }>;
|
||||
|
||||
// getExpense(tenantId: number, expenseId: number): Promise<IExpense>;
|
||||
// }
|
||||
|
||||
export interface IExpenseCreatingPayload {
|
||||
trx: Knex.Transaction;
|
||||
// tenantId: number;
|
||||
expenseDTO: IExpenseCreateDTO;
|
||||
}
|
||||
|
||||
export interface IExpenseEventEditingPayload {
|
||||
// tenantId: number;
|
||||
oldExpense: Expense;
|
||||
expenseDTO: IExpenseEditDTO;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IExpenseCreatedPayload {
|
||||
// tenantId: number;
|
||||
expenseId: number;
|
||||
// authorizedUser: ISystemUser;
|
||||
expense: Expense;
|
||||
expenseDTO: IExpenseCreateDTO;
|
||||
trx?: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IExpenseEventEditPayload {
|
||||
// tenantId: number;
|
||||
expenseId: number;
|
||||
expense: Expense;
|
||||
expenseDTO: IExpenseEditDTO;
|
||||
@@ -174,7 +78,6 @@ export interface IExpenseEventEditPayload {
|
||||
}
|
||||
|
||||
export interface IExpenseEventDeletePayload {
|
||||
// tenantId: number;
|
||||
expenseId: number;
|
||||
authorizedUser: SystemUser;
|
||||
oldExpense: Expense;
|
||||
@@ -183,11 +86,9 @@ export interface IExpenseEventDeletePayload {
|
||||
|
||||
export interface IExpenseDeletingPayload {
|
||||
trx: Knex.Transaction;
|
||||
// tenantId: number;
|
||||
oldExpense: Expense;
|
||||
}
|
||||
export interface IExpenseEventPublishedPayload {
|
||||
// tenantId: number;
|
||||
expenseId: number;
|
||||
oldExpense: Expense;
|
||||
expense: Expense;
|
||||
@@ -198,7 +99,6 @@ export interface IExpenseEventPublishedPayload {
|
||||
export interface IExpensePublishingPayload {
|
||||
trx: Knex.Transaction;
|
||||
oldExpense: Expense;
|
||||
// tenantId: number;
|
||||
}
|
||||
export enum ExpenseAction {
|
||||
Create = 'Create',
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { omit, sumBy } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import * as moment from 'moment';
|
||||
import * as R from 'ramda';
|
||||
import {
|
||||
IExpenseCreateDTO,
|
||||
IExpenseCommonDTO,
|
||||
IExpenseEditDTO,
|
||||
} from '../interfaces/Expenses.interface';
|
||||
// import { BranchTransactionDTOTransform } from '@/services/Branches/Integrations/BranchTransactionDTOTransform';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { BranchTransactionDTOTransformer } from '@/modules/Branches/integrations/BranchTransactionDTOTransform';
|
||||
import { Expense } from '../models/Expense.model';
|
||||
import { assocItemEntriesDefaultIndex } from '@/utils/associate-item-entries-index';
|
||||
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
|
||||
|
||||
@Injectable()
|
||||
export class ExpenseDTOTransformer {
|
||||
/**
|
||||
* @param {BranchTransactionDTOTransformer} branchDTOTransform - Branch transaction DTO transformer.
|
||||
* @param {TenancyContext} tenancyContext - Tenancy context.
|
||||
*/
|
||||
constructor(
|
||||
// private readonly branchDTOTransform: BranchTransactionDTOTransform;
|
||||
private readonly branchDTOTransform: BranchTransactionDTOTransformer,
|
||||
private readonly tenancyContext: TenancyContext,
|
||||
) {}
|
||||
|
||||
@@ -50,7 +53,6 @@ export class ExpenseDTOTransformer {
|
||||
*/
|
||||
private expenseDTOToModel(
|
||||
expenseDTO: IExpenseCreateDTO | IExpenseEditDTO,
|
||||
// user?: ISystemUser
|
||||
): Expense {
|
||||
const landedCostAmount = this.getExpenseLandedCostAmount(expenseDTO);
|
||||
const totalAmount = this.getExpenseCategoriesTotal(expenseDTO.categories);
|
||||
@@ -72,10 +74,9 @@ export class ExpenseDTOTransformer {
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
return initialDTO;
|
||||
// return R.compose(this.branchDTOTransform.transformDTO<IExpense>(tenantId))(
|
||||
// initialDTO
|
||||
// );
|
||||
return R.compose(this.branchDTOTransform.transformDTO<Expense>)(
|
||||
initialDTO,
|
||||
) as Expense;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -203,21 +203,28 @@ export class Expense extends BaseModel {
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
// const Account = require('models/Account');
|
||||
const { Account } = require('../../Accounts/models/Account.model');
|
||||
const { ExpenseCategory } = require('./ExpenseCategory.model');
|
||||
// const Document = require('models/Document');
|
||||
// const Branch = require('models/Branch');
|
||||
const { Document } = require('../../ChromiumlyTenancy/models/Document');
|
||||
const { Branch } = require('../../Branches/models/Branch.model');
|
||||
// const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
|
||||
|
||||
return {
|
||||
// paymentAccount: {
|
||||
// relation: Model.BelongsToOneRelation,
|
||||
// modelClass: Account.default,
|
||||
// join: {
|
||||
// from: 'expenses_transactions.paymentAccountId',
|
||||
// to: 'accounts.id',
|
||||
// },
|
||||
// },
|
||||
/**
|
||||
* Expense transaction may belongs to a payment account.
|
||||
*/
|
||||
paymentAccount: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Account,
|
||||
join: {
|
||||
from: 'expenses_transactions.paymentAccountId',
|
||||
to: 'accounts.id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Expense transaction may has many expense categories.
|
||||
*/
|
||||
categories: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: ExpenseCategory,
|
||||
@@ -233,33 +240,33 @@ export class Expense extends BaseModel {
|
||||
/**
|
||||
* Expense transction may belongs to a branch.
|
||||
*/
|
||||
// branch: {
|
||||
// relation: Model.BelongsToOneRelation,
|
||||
// modelClass: Branch.default,
|
||||
// join: {
|
||||
// from: 'expenses_transactions.branchId',
|
||||
// to: 'branches.id',
|
||||
// },
|
||||
// },
|
||||
branch: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Branch,
|
||||
join: {
|
||||
from: 'expenses_transactions.branchId',
|
||||
to: 'branches.id',
|
||||
},
|
||||
},
|
||||
|
||||
// /**
|
||||
// * Expense transaction may has many attached attachments.
|
||||
// */
|
||||
// attachments: {
|
||||
// relation: Model.ManyToManyRelation,
|
||||
// modelClass: Document.default,
|
||||
// join: {
|
||||
// from: 'expenses_transactions.id',
|
||||
// through: {
|
||||
// from: 'document_links.modelId',
|
||||
// to: 'document_links.documentId',
|
||||
// },
|
||||
// to: 'documents.id',
|
||||
// },
|
||||
// filter(query) {
|
||||
// query.where('model_ref', 'Expense');
|
||||
// },
|
||||
// },
|
||||
/**
|
||||
* Expense transaction may has many attached attachments.
|
||||
*/
|
||||
attachments: {
|
||||
relation: Model.ManyToManyRelation,
|
||||
modelClass: Document,
|
||||
join: {
|
||||
from: 'expenses_transactions.id',
|
||||
through: {
|
||||
from: 'document_links.modelId',
|
||||
to: 'document_links.documentId',
|
||||
},
|
||||
to: 'documents.id',
|
||||
},
|
||||
filter(query) {
|
||||
query.where('model_ref', 'Expense');
|
||||
},
|
||||
},
|
||||
|
||||
// /**
|
||||
// * Expense may belongs to matched bank transaction.
|
||||
|
||||
@@ -77,7 +77,7 @@ export class ExpenseGL {
|
||||
index: index + 2,
|
||||
projectId: category.projectId,
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -88,8 +88,9 @@ export class ExpenseGL {
|
||||
const getCategoryEntry = this.getExpenseGLCategoryEntry();
|
||||
|
||||
const paymentEntry = this.getExpenseGLPaymentEntry();
|
||||
const categoryEntries = this.expense.categories.map(getCategoryEntry);
|
||||
|
||||
const categoryEntries = this.expense.categories.map((category, index) =>
|
||||
getCategoryEntry(category, index),
|
||||
);
|
||||
return [paymentEntry, ...categoryEntries];
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user