feat: Financial statements dependency graph calculate.

This commit is contained in:
Ahmed Bouhuolia
2020-05-23 06:47:00 +02:00
parent b2c9ac54f4
commit 17c1b6ad51
26 changed files with 2041 additions and 900 deletions

View File

@@ -9,7 +9,8 @@ import {
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
import CachableModel from '@/lib/Cachable/CachableModel';
import DateSession from '@/models/DateSession';
import { flatToNestedArray } from '@/utils';
import DependencyGraph from '@/lib/DependencyGraph';
export default class Account extends mixin(TenantModel, [CachableModel, DateSession]) {
/**
@@ -26,6 +27,25 @@ export default class Account extends mixin(TenantModel, [CachableModel, DateSess
return CachableQueryBuilder;
}
static query(...args) {
return super.query(...args).runAfter((result) => {
if (Array.isArray(result)) {
return this.isDepGraph ?
Account.toDependencyGraph(result) :
this.collection.from(result);
}
return result;
});
}
/**
* Convert the array result to dependency graph.
*/
static depGraph() {
this.isDepGraph = true;
return this;
}
/**
* Model modifiers.
*/
@@ -106,4 +126,19 @@ export default class Account extends mixin(TenantModel, [CachableModel, DateSess
accountNormal: account.type.normal,
}))));
}
/**
* Converts flatten accounts list to nested array.
* @param {Array} accounts
* @param {Object} options
*/
static toNestedArray(accounts, options = { children: 'children' }) {
return flatToNestedArray(accounts, { id: 'id', parentId: 'parentAccountId' })
}
static toDependencyGraph(accounts) {
return DependencyGraph.fromArray(
accounts, { itemId: 'id', parentItemId: 'parentAccountId' }
);
}
}

View File

@@ -1,10 +1,12 @@
import { Model } from 'objection';
import { Model, mixin } from 'objection';
import moment from 'moment';
import TenantModel from '@/models/TenantModel';
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
import CachableModel from '@/lib/Cachable/CachableModel';
import DateSession from '@/models/DateSession';
export default class AccountTransaction extends TenantModel {
export default class AccountTransaction extends mixin(TenantModel, [CachableModel, DateSession]) {
/**
* Table name
*/

View File

@@ -32,6 +32,11 @@ export default class Option extends TenantModel {
return MetableCollection;
}
/**
* Validates the given options is defined or either not.
* @param {Array} options
* @return {Boolean}
*/
static validateDefined(options) {
const notDefined = [];