mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: remove Webpack and depend on nodemon. feat: refactoring expenses. feat: optimize system users with caching. feat: architecture tenant optimize.
100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
import { Model } from 'objection';
|
|
import moment from 'moment';
|
|
import TenantModel from 'models/TenantModel';
|
|
|
|
export default class AccountTransaction extends TenantModel {
|
|
/**
|
|
* Table name
|
|
*/
|
|
static get tableName() {
|
|
return 'accounts_transactions';
|
|
}
|
|
|
|
/**
|
|
* Timestamps columns.
|
|
*/
|
|
get timestamps() {
|
|
return ['createdAt'];
|
|
}
|
|
|
|
/**
|
|
* Model modifiers.
|
|
*/
|
|
static get modifiers() {
|
|
return {
|
|
/**
|
|
* Filters accounts by the given ids.
|
|
* @param {Query} query
|
|
* @param {number[]} accountsIds
|
|
*/
|
|
filterAccounts(query, accountsIds) {
|
|
if (accountsIds.length > 0) {
|
|
query.whereIn('account_id', accountsIds);
|
|
}
|
|
},
|
|
filterTransactionTypes(query, types) {
|
|
if (Array.isArray(types) && types.length > 0) {
|
|
query.whereIn('reference_type', types);
|
|
} else if (typeof types === 'string') {
|
|
query.where('reference_type', types);
|
|
}
|
|
},
|
|
filterDateRange(query, startDate, endDate, type = 'day') {
|
|
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
|
|
const fromDate = moment(startDate).startOf(type).format(dateFormat);
|
|
const toDate = moment(endDate).endOf(type).format(dateFormat);
|
|
|
|
if (startDate) {
|
|
query.where('date', '>=', fromDate);
|
|
}
|
|
if (endDate) {
|
|
query.where('date', '<=', toDate);
|
|
}
|
|
},
|
|
filterAmountRange(query, fromAmount, toAmount) {
|
|
if (fromAmount) {
|
|
query.andWhere((q) => {
|
|
q.where('credit', '>=', fromAmount);
|
|
q.orWhere('debit', '>=', fromAmount);
|
|
});
|
|
}
|
|
if (toAmount) {
|
|
query.andWhere((q) => {
|
|
q.where('credit', '<=', toAmount);
|
|
q.orWhere('debit', '<=', toAmount);
|
|
});
|
|
}
|
|
},
|
|
sumationCreditDebit(query) {
|
|
query.sum('credit as credit');
|
|
query.sum('debit as debit');
|
|
query.groupBy('account_id');
|
|
},
|
|
filterContactType(query, contactType) {
|
|
query.where('contact_type', contactType);
|
|
},
|
|
filterContactIds(query, contactIds) {
|
|
query.whereIn('contact_id', contactIds);
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Relationship mapping.
|
|
*/
|
|
static get relationMappings() {
|
|
const Account = require('models/Account');
|
|
|
|
return {
|
|
account: {
|
|
relation: Model.BelongsToOneRelation,
|
|
modelClass: this.relationBindKnex(Account.default),
|
|
join: {
|
|
from: 'accounts_transactions.accountId',
|
|
to: 'accounts.id',
|
|
},
|
|
},
|
|
};
|
|
}
|
|
}
|