feat: Inventory item details report.

feat: Cash flow statement report.
This commit is contained in:
a.bouhuolia
2021-05-31 13:17:02 +02:00
parent 256d915f06
commit d47633b8ea
80 changed files with 5474 additions and 376 deletions

View File

@@ -0,0 +1,92 @@
import { map } from 'lodash';
import { IAccount, IAccountTransaction } from 'interfaces';
import { ACCOUNT_TYPE } from 'data/AccountTypes';
import HasTenancyService from 'services/Tenancy/TenancyService';
import { Inject } from 'typedi';
export default class TransactionsByCustomersRepository {
@Inject()
tenancy: HasTenancyService;
/**
* Retrieve the report customers.
* @param {number} tenantId
* @returns {Promise<ICustomer[]>}
*/
public async getCustomers(tenantId: number) {
const { Customer } = this.tenancy.models(tenantId);
return Customer.query().orderBy('displayName');
}
/**
* Retrieve the accounts receivable.
* @param {number} tenantId
* @returns {Promise<IAccount[]>}
*/
public async getReceivableAccounts(tenantId: number): Promise<IAccount[]> {
const { Account } = this.tenancy.models(tenantId);
const accounts = await Account.query().where(
'accountType',
ACCOUNT_TYPE.ACCOUNTS_RECEIVABLE
);
return accounts;
}
/**
* Retrieve the customers opening balance transactions.
* @param {number} tenantId - Tenant id.
* @param {number} openingDate - Opening date.
* @param {number} customersIds - Customers ids.
* @returns {Promise<IAccountTransaction[]>}
*/
public async getCustomersOpeningBalanceTransactions(
tenantId: number,
openingDate: Date,
customersIds?: number[]
): Promise<IAccountTransaction[]> {
const { AccountTransaction } = this.tenancy.models(tenantId);
const receivableAccounts = await this.getReceivableAccounts(tenantId);
const receivableAccountsIds = map(receivableAccounts, 'id');
const openingTransactions = await AccountTransaction.query().modify(
'contactsOpeningBalance',
openingDate,
receivableAccountsIds,
customersIds
);
return openingTransactions;
}
/**
* Retrieve the customers periods transactions.
* @param {number} tenantId - Tenant id.
* @param {Date|string} openingDate - Opening date.
* @param {number[]} customersIds - Customers ids.
* @return {Promise<IAccountTransaction[]>}
*/
public async getCustomersPeriodTransactions(
tenantId: number,
fromDate: Date,
toDate: Date
): Promise<IAccountTransaction[]> {
const { AccountTransaction } = this.tenancy.models(tenantId);
const receivableAccounts = await this.getReceivableAccounts(tenantId);
const receivableAccountsIds = map(receivableAccounts, 'id');
const transactions = await AccountTransaction.query().onBuild((query) => {
// Filter by date.
query.modify('filterDateRange', fromDate, toDate);
// Filter by customers.
query.whereNot('contactId', null);
// Filter by accounts.
query.whereIn('accountId', receivableAccountsIds);
});
return transactions;
}
}