fix: accounts types with new design.

This commit is contained in:
a.bouhuolia
2021-01-26 12:51:13 +02:00
parent a871aea84d
commit d789228038
24 changed files with 101 additions and 100 deletions

View File

@@ -1,4 +1,7 @@
import { IBalanceSheetStructureSection } from 'interfaces';
import {
ACCOUNT_TYPE
} from 'data/AccountTypes';
const balanceSheetStructure: IBalanceSheetStructureSection[] = [
{
@@ -14,22 +17,22 @@ const balanceSheetStructure: IBalanceSheetStructureSection[] = [
{
name: 'Cash and cash equivalents',
type: 'accounts_section',
accountsTypes: ['cash', 'bank'],
accountsTypes: [ACCOUNT_TYPE.CASH, ACCOUNT_TYPE.BANK],
},
{
name: 'Accounts Receivable',
type: 'accounts_section',
accountsTypes: ['accounts_receivable'],
accountsTypes: [ACCOUNT_TYPE.ACCOUNTS_RECEIVABLE],
},
{
name: 'Inventories',
type: 'accounts_section',
accountsTypes: ['inventory'],
accountsTypes: [ACCOUNT_TYPE.INVENTORY],
},
{
name: 'Other current assets',
type: 'accounts_section',
accountsTypes: ['other_current_asset'],
accountsTypes: [ACCOUNT_TYPE.OTHER_CURRENT_ASSET],
},
],
alwaysShow: true,
@@ -37,12 +40,12 @@ const balanceSheetStructure: IBalanceSheetStructureSection[] = [
{
name: 'Fixed Asset',
type: 'accounts_section',
accountsTypes: ['fixed_asset'],
accountsTypes: [ACCOUNT_TYPE.FIXED_ASSET],
},
{
name: 'Non-Current Assets',
type: 'accounts_section',
accountsTypes: ['non_current_asset'],
accountsTypes: [ACCOUNT_TYPE.NON_CURRENT_ASSET],
}
],
alwaysShow: true,
@@ -61,21 +64,21 @@ const balanceSheetStructure: IBalanceSheetStructureSection[] = [
name: 'Current Liabilties',
type: 'accounts_section',
accountsTypes: [
'accounts_payable',
'tax_payable',
'credit_card',
'other_current_liability'
ACCOUNT_TYPE.ACCOUNTS_PAYABLE,
ACCOUNT_TYPE.TAX_PAYABLE,
ACCOUNT_TYPE.CREDIT_CARD,
ACCOUNT_TYPE.OTHER_CURRENT_LIABILITY,
],
},
{
name: 'Long-Term Liabilities',
type: 'accounts_section',
accountsTypes: ['long_term_liability'],
accountsTypes: [ACCOUNT_TYPE.LOGN_TERM_LIABILITY],
},
{
name: 'Non-Current Liabilities',
type: 'accounts_section',
accountsTypes: ['non_current_liability'],
accountsTypes: [ACCOUNT_TYPE.NON_CURRENT_LIABILITY],
}
],
},
@@ -83,7 +86,7 @@ const balanceSheetStructure: IBalanceSheetStructureSection[] = [
name: 'Equity',
sectionType: 'equity',
type: 'accounts_section',
accountsTypes: ['equity'],
accountsTypes: [ACCOUNT_TYPE.EQUITY],
},
],
alwaysShow: true,

View File

@@ -10,6 +10,9 @@ exports.up = function(knex) {
table.integer('account_id').unsigned().index().references('id').inTable('accounts');
table.string('contact_type').nullable().index();
table.integer('contact_id').unsigned().nullable().index();
table.string('transaction_number').nullable().index();
table.string('reference_number').nullable().index();
table.integer('item_id').unsigned().nullable().index();
table.string('note');
table.integer('user_id').unsigned().index();
table.integer('index').unsigned();

View File

@@ -118,8 +118,8 @@ export default [
predefined: 0,
},
{
name:'Opening Balance Adjustments',
slug: 'opening-balance-adjustments',
name:'Opening Balance Liabilities',
slug: 'opening-balance-liabilities',
account_type: 'other-current-liability',
code: '20004',
description:'This account will hold the difference in the debits and credits entered during the opening balance..',
@@ -193,9 +193,9 @@ export default [
// Expenses
{
name:'Uncategorized Expenses',
slug: 'uncategorized-expense',
account_type: 'expense',
name:'Other Expenses',
slug: 'other-expenses',
account_type: 'other-expense',
parent_account_id: null,
code: '40001',
description: '',

View File

@@ -24,6 +24,8 @@ export interface IAccount {
currencyCode: string,
transactions?: any[],
type?: any[],
accountNormal: string,
accountParentType: string,
};
export interface IAccountsFilter extends IDynamicListFilterDTO {

View File

@@ -179,14 +179,6 @@ export default class Account extends TenantModel {
return this.isProfitLossSheet();
}
static collectJournalEntries(accounts) {
return flatten(accounts.map((account) => account.transactions.map((transaction) => ({
accountId: account.id,
...transaction,
accountNormal: account.type.normal,
}))));
}
/**
* Converts flatten accounts list to nested array.
* @param {Array} accounts
@@ -196,6 +188,10 @@ export default class Account extends TenantModel {
return flatToNestedArray(accounts, { id: 'id', parentId: 'parentAccountId' })
}
/**
* Transformes the accounts list to depenedency graph structure.
* @param {IAccount[]} accounts
*/
static toDependencyGraph(accounts) {
return DependencyGraph.fromArray(
accounts, { itemId: 'id', parentItemId: 'parentAccountId' }

View File

@@ -23,6 +23,15 @@ export default class AccountRepository extends TenantRepository {
return this.model.toDependencyGraph(accounts);
});
}
/**
* Retrieve.
* @param {string} slug
* @return {Promise<IAccount>}
*/
findBySlug(slug: string) {
return this.findOne({ slug });
}
/**
* Changes account balance.

View File

@@ -31,7 +31,7 @@ export default class AccountTransactionsRepository extends TenantRepository {
return this.model.query()
.modify('filterAccounts', filter.accountsIds)
.modify('filterDateRange', filter.fromDate, filter.toDate)
.withGraphFetched('account.type')
.withGraphFetched('account')
.onBuild((query) => {
if (filter.sumationCreditDebit) {
query.modify('sumationCreditDebit');

View File

@@ -269,7 +269,7 @@ export default class JournalCommands {
'reference_id',
Array.isArray(referenceId) ? referenceId : [referenceId]
)
.withGraphFetched('account.type');
.withGraphFetched('account');
this.journal.fromTransactions(transactions);
this.journal.removeEntries();

View File

@@ -165,7 +165,6 @@ export default class JournalPoster implements IJournalPoster {
private async convertBalanceChangesToArr(
accountsChange: IAccountsChange
) : Promise<{ account: number, change: number }[]>{
const { accountTypeRepository } = this.repositories;
const mappedList: { account: number, change: number }[] = [];
const accountsIds: number[] = Object.keys(accountsChange).map(id => parseInt(id, 10));
@@ -173,8 +172,8 @@ export default class JournalPoster implements IJournalPoster {
accountsIds.map(async (account: number) => {
const accountChange = accountsChange[account];
const accountNode = this.accountsDepGraph.getNodeData(account);
const accountTypeMeta = await accountTypeRepository.findOneById(accountNode.accountTypeId);
const { normal }: { normal: TEntryType } = accountTypeMeta;
const { normal }: { normal: TEntryType } = accountNode.accountNormal;
let change = 0;
if (accountChange.credit) {
@@ -333,7 +332,7 @@ export default class JournalPoster implements IJournalPoster {
...transaction,
referenceTypeFormatted: transaction.referenceTypeFormatted,
account: transaction.accountId,
accountNormal: get(transaction, 'account.type.normal'),
accountNormal: get(transaction, 'account.accountNormal'),
});
});
}

View File

@@ -184,7 +184,7 @@ export default class BalanceSheetStatement extends FinancialSheet {
const filteredAccounts = accounts
// Filter accounts that associated to the section accounts types.
.filter(
(account) => sectionAccountsTypes.indexOf(account.type.key) !== -1
(account) => sectionAccountsTypes.indexOf(account.accountType) !== -1
)
.map((account) => this.balanceSheetAccountMapper(account))
// Filter accounts that have no transaction when `noneTransactions` is on.

View File

@@ -70,7 +70,7 @@ export default class BalanceSheetStatementService
this.logger.info('[balance_sheet] trying to calculate the report.', { filter, tenantId });
// Retrieve all accounts on the storage.
const accounts = await accountRepository.all('type');
const accounts = await accountRepository.all();
const accountsGraph = await accountRepository.getDependencyGraph();
// Retrieve all journal transactions based on the given query.

View File

@@ -6,7 +6,6 @@ import {
IGeneralLedgerSheetAccountTransaction,
IAccount,
IJournalPoster,
IAccountType,
IJournalEntry,
IContact,
} from 'interfaces';
@@ -130,7 +129,7 @@ export default class GeneralLedgerSheet extends FinancialSheet {
* @return {IGeneralLedgerSheetAccountTransaction[]}
*/
private accountTransactionsMapper(
account: IAccount & { type: IAccountType },
account: IAccount,
openingBalance: number
): IGeneralLedgerSheetAccountTransaction[] {
const entries = this.transactions.getAccountEntries(account.id);
@@ -184,7 +183,7 @@ export default class GeneralLedgerSheet extends FinancialSheet {
* @return {IGeneralLedgerSheetAccount}
*/
private accountMapper(
account: IAccount & { type: IAccountType }
account: IAccount
): IGeneralLedgerSheetAccount {
const openingBalance = this.accountOpeningBalance(account);
const closingBalance = this.accountClosingBalance(account);
@@ -210,11 +209,11 @@ export default class GeneralLedgerSheet extends FinancialSheet {
* @return {IGeneralLedgerSheetAccount[]}
*/
private accountsWalker(
accounts: IAccount & { type: IAccountType }[]
accounts: IAccount[]
): IGeneralLedgerSheetAccount[] {
return (
accounts
.map((account: IAccount & { type: IAccountType }) =>
.map((account: IAccount) =>
this.accountMapper(account)
)
// Filter general ledger accounts that have no transactions

View File

@@ -89,7 +89,7 @@ export default class GeneralLedgerService {
key: 'base_currency',
});
// Retrieve all accounts with associated type from the storage.
const accounts = await accountRepository.all('type');
const accounts = await accountRepository.all();
const accountsGraph = await accountRepository.getDependencyGraph();
// Retrieve all contacts on the storage.

View File

@@ -12,6 +12,7 @@ import {
IProfitLossSheetTotalSection,
} from 'interfaces';
import { flatToNestedArray, dateRangeCollection } from 'utils';
import { ACCOUNT_TYPE } from 'data/AccountTypes';
export default class ProfitLossSheet extends FinancialSheet {
tenantId: number;
@@ -51,7 +52,7 @@ export default class ProfitLossSheet extends FinancialSheet {
}
get otherIncomeAccounts() {
return this.accounts.filter((a) => a.type.key === 'other_income');
return this.accounts.filter((a) => a.accountType === ACCOUNT_TYPE.OTHER_INCOME);
}
/**
@@ -59,7 +60,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}
*/
get incomeAccounts() {
return this.accounts.filter((a) => a.type.key === 'income');
return this.accounts.filter((a) => a.accountType === ACCOUNT_TYPE.INCOME);
}
/**
@@ -67,7 +68,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}
*/
get expensesAccounts() {
return this.accounts.filter((a) => a.type.key === 'expense');
return this.accounts.filter((a) => a.accountType === ACCOUNT_TYPE.EXPENSE);
}
/**
@@ -75,7 +76,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}}
*/
get otherExpensesAccounts() {
return this.accounts.filter((a) => a.type.key === 'other_expense');
return this.accounts.filter((a) => a.accountType === ACCOUNT_TYPE.OTHER_EXPENSE);
}
/**
@@ -83,7 +84,7 @@ export default class ProfitLossSheet extends FinancialSheet {
* @return {IAccount & { type: IAccountType }[]}
*/
get costOfSalesAccounts() {
return this.accounts.filter((a) => a.type.key === 'cost_of_goods_sold');
return this.accounts.filter((a) => a.accountType === ACCOUNT_TYPE.COST_OF_GOODS_SOLD);
}
/**

View File

@@ -77,7 +77,7 @@ export default class ProfitLossSheetService {
key: 'base_currency',
});
// Retrieve all accounts on the storage.
const accounts = await accountRepository.all('type');
const accounts = await accountRepository.all();
const accountsGraph = await accountRepository.getDependencyGraph();
// Retrieve all journal transactions based on the given query.

View File

@@ -65,7 +65,7 @@ export default class TrialBalanceSheet extends FinancialSheet {
parentAccountId: account.parentAccountId,
name: account.name,
code: account.code,
accountNormal: account.type.normal,
accountNormal: account.accountNormal,
hasTransactions: entries.length > 0,
credit: trial.credit,

View File

@@ -69,7 +69,7 @@ export default class TrialBalanceSheetService extends FinancialSheet {
filter,
});
// Retrieve all accounts on the storage.
const accounts = await accountRepository.all('type');
const accounts = await accountRepository.all();
const accountsGraph = await accountRepository.getDependencyGraph();
// Retrieve all journal transactions based on the given query.

View File

@@ -513,7 +513,7 @@ export default class BillPaymentsService {
const transactions = await AccountTransaction.query()
.whereIn('reference_type', ['BillPayment'])
.where('reference_id', billPayment.id)
.withGraphFetched('account.type');
.withGraphFetched('account');
journal.loadEntries(transactions);
journal.removeEntries();

View File

@@ -10,23 +10,4 @@ export default class ItemsSubscriber{
constructor() {
this.itemsService = Container.get(ItemsService);
};
/**
* Handle writing opening item inventory transaction.
*/
@On(events.item.onCreated)
handleWriteOpeningInventoryTransaction({ tenantId, item }) {
// Can't continue if the opeing cost, quantity or opening date was empty.
if (!item.openingCost || !item.openingQuantity || !item.openingDate) {
return;
}
// Records the opeing items inventory transaction once the item created.
this.itemsService.recordOpeningItemsInventoryTransaction(
tenantId,
item.id,
item.openingQuantity,
item.openingCost,
item.openingDate,
)
}
}