mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
fix: Filter financial reports by items, customers or vendors.
This commit is contained in:
@@ -23,6 +23,7 @@ export default class CustomerBalanceSummaryReportController extends BaseFinancia
|
||||
router.get(
|
||||
'/',
|
||||
this.validationSchema,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.customerBalanceSummary.bind(this))
|
||||
);
|
||||
return router;
|
||||
@@ -34,7 +35,13 @@ export default class CustomerBalanceSummaryReportController extends BaseFinancia
|
||||
get validationSchema() {
|
||||
return [
|
||||
...this.sheetNumberFormatValidationSchema,
|
||||
|
||||
// As date.
|
||||
query('as_date').optional().isISO8601(),
|
||||
|
||||
// Customers ids.
|
||||
query('customers_ids').optional().isArray({ min: 1 }),
|
||||
query('customers_ids.*').exists().isInt().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -53,8 +53,12 @@ export default class InventoryDetailsController extends BaseController {
|
||||
.escape(),
|
||||
query('from_date').optional(),
|
||||
query('to_date').optional(),
|
||||
|
||||
query('none_zero').optional().isBoolean().toBoolean(),
|
||||
query('none_transactions').optional().isBoolean().toBoolean(),
|
||||
|
||||
query('items_ids').optional().isArray(),
|
||||
query('items_ids.*').optional().isInt().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,10 @@ export default class InventoryValuationReportController extends BaseFinancialRep
|
||||
return [
|
||||
query('from_date').optional().isISO8601(),
|
||||
query('to_date').optional().isISO8601(),
|
||||
|
||||
query('items_ids').optional().isArray(),
|
||||
query('items_ids.*').optional().isInt().toInt(),
|
||||
|
||||
query('number_format.no_cents').optional().isBoolean().toBoolean(),
|
||||
query('number_format.divide_1000').optional().isBoolean().toBoolean(),
|
||||
query('none_transactions').default(true).isBoolean().toBoolean(),
|
||||
|
||||
@@ -28,14 +28,20 @@ export default class PurchasesByItemReportController extends BaseFinancialReport
|
||||
|
||||
/**
|
||||
* Validation schema.
|
||||
* @return {ValidationChain[]}
|
||||
*/
|
||||
get validationSchema(): ValidationChain[] {
|
||||
return [
|
||||
query('from_date').optional().isISO8601(),
|
||||
query('to_date').optional().isISO8601(),
|
||||
|
||||
query('number_format.no_cents').optional().isBoolean().toBoolean(),
|
||||
query('number_format.divide_1000').optional().isBoolean().toBoolean(),
|
||||
query('none_transactions').default(true).isBoolean().toBoolean(),
|
||||
|
||||
query('items_ids').optional().isArray(),
|
||||
query('items_ids.*').optional().isInt().toInt(),
|
||||
|
||||
query('orderBy').optional().isIn(['created_at', 'name', 'code']),
|
||||
query('order').optional().isIn(['desc', 'asc']),
|
||||
];
|
||||
|
||||
@@ -33,6 +33,10 @@ export default class SalesByItemsReportController extends BaseFinancialReportCon
|
||||
return [
|
||||
query('from_date').optional().isISO8601(),
|
||||
query('to_date').optional().isISO8601(),
|
||||
|
||||
query('items_ids').optional().isArray(),
|
||||
query('items_ids.*').optional().isInt().toInt(),
|
||||
|
||||
query('number_format.no_cents').optional().isBoolean().toBoolean(),
|
||||
query('number_format.divide_1000').optional().isBoolean().toBoolean(),
|
||||
query('none_transactions').default(true).isBoolean().toBoolean(),
|
||||
|
||||
@@ -23,6 +23,7 @@ export default class TransactionsByCustomersReportController extends BaseFinanci
|
||||
router.get(
|
||||
'/',
|
||||
this.validationSchema,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.transactionsByCustomers.bind(this))
|
||||
);
|
||||
return router;
|
||||
@@ -31,13 +32,18 @@ export default class TransactionsByCustomersReportController extends BaseFinanci
|
||||
/**
|
||||
* Validation schema.
|
||||
*/
|
||||
get validationSchema() {
|
||||
private get validationSchema() {
|
||||
return [
|
||||
...this.sheetNumberFormatValidationSchema,
|
||||
query('from_date').optional().isISO8601(),
|
||||
query('to_date').optional().isISO8601(),
|
||||
|
||||
query('none_zero').optional().isBoolean().toBoolean(),
|
||||
query('none_transactions').optional().isBoolean().toBoolean(),
|
||||
|
||||
// Customers ids.
|
||||
query('customers_ids').optional().isArray({ min: 1 }),
|
||||
query('customers_ids.*').exists().isInt().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -45,7 +51,9 @@ export default class TransactionsByCustomersReportController extends BaseFinanci
|
||||
* Transformes the statement to table rows response.
|
||||
* @param {ITransactionsByCustomersStatement} statement -
|
||||
*/
|
||||
transformToTableResponse({ data }: ITransactionsByCustomersStatement) {
|
||||
private transformToTableResponse({
|
||||
data,
|
||||
}: ITransactionsByCustomersStatement) {
|
||||
return {
|
||||
table: {
|
||||
rows: this.transactionsByCustomersTableRows.tableRows(data),
|
||||
@@ -57,7 +65,7 @@ export default class TransactionsByCustomersReportController extends BaseFinanci
|
||||
* Transformes the statement to json response.
|
||||
* @param {ITransactionsByCustomersStatement} statement -
|
||||
*/
|
||||
transfromToJsonResponse({
|
||||
private transfromToJsonResponse({
|
||||
data,
|
||||
columns,
|
||||
}: ITransactionsByCustomersStatement) {
|
||||
@@ -83,10 +91,11 @@ export default class TransactionsByCustomersReportController extends BaseFinanci
|
||||
const filter = this.matchedQueryData(req);
|
||||
|
||||
try {
|
||||
const transactionsByCustomers = await this.transactionsByCustomersService.transactionsByCustomers(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
const transactionsByCustomers =
|
||||
await this.transactionsByCustomersService.transactionsByCustomers(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
const accept = this.accepts(req);
|
||||
const acceptType = accept.types(['json', 'application/json+table']);
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ export default class TransactionsByVendorsReportController extends BaseFinancial
|
||||
router.get(
|
||||
'/',
|
||||
this.validationSchema,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.transactionsByVendors.bind(this))
|
||||
);
|
||||
return router;
|
||||
@@ -34,10 +35,16 @@ export default class TransactionsByVendorsReportController extends BaseFinancial
|
||||
get validationSchema(): ValidationChain[] {
|
||||
return [
|
||||
...this.sheetNumberFormatValidationSchema,
|
||||
|
||||
query('from_date').optional().isISO8601(),
|
||||
query('to_date').optional().isISO8601(),
|
||||
|
||||
query('none_zero').optional().isBoolean().toBoolean(),
|
||||
query('none_transactions').optional().isBoolean().toBoolean(),
|
||||
|
||||
// Vendors ids.
|
||||
query('vendors_ids').optional().isArray({ min: 1 }),
|
||||
query('vendors_ids.*').exists().isInt().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -80,10 +87,11 @@ export default class TransactionsByVendorsReportController extends BaseFinancial
|
||||
const filter = this.matchedQueryData(req);
|
||||
|
||||
try {
|
||||
const transactionsByVendors = await this.transactionsByVendorsService.transactionsByVendors(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
const transactionsByVendors =
|
||||
await this.transactionsByVendorsService.transactionsByVendors(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
const accept = this.accepts(req);
|
||||
const acceptType = accept.types(['json', 'application/json+table']);
|
||||
|
||||
|
||||
@@ -34,6 +34,10 @@ export default class VendorBalanceSummaryReportController extends BaseFinancialR
|
||||
return [
|
||||
...this.sheetNumberFormatValidationSchema,
|
||||
query('as_date').optional().isISO8601(),
|
||||
|
||||
// Vendors ids.
|
||||
query('vendors_ids').optional().isArray({ min: 1 }),
|
||||
query('vendors_ids.*').exists().isInt().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface IInventoryValuationReportQuery {
|
||||
asDate: Date | string;
|
||||
numberFormat: INumberFormatQuery;
|
||||
noneTransactions: boolean;
|
||||
itemsIds: number[],
|
||||
};
|
||||
|
||||
export interface IInventoryValuationSheetMeta {
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface IInventoryDetailsQuery {
|
||||
toDate: Date | string;
|
||||
numberFormat: INumberFormatQuery;
|
||||
noneTransactions: boolean;
|
||||
itemsIds: number[]
|
||||
}
|
||||
|
||||
export interface IInventoryDetailsNumber {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
export interface ISalesByItemsReportQuery {
|
||||
fromDate: Date | string;
|
||||
toDate: Date | string;
|
||||
itemsIds: number[],
|
||||
numberFormat: INumberFormatQuery;
|
||||
noneTransactions: boolean;
|
||||
};
|
||||
|
||||
@@ -18,7 +18,9 @@ export interface ITransactionsByCustomersCustomer {
|
||||
}
|
||||
|
||||
export interface ITransactionsByCustomersFilter
|
||||
extends ITransactionsByContactsFilter {}
|
||||
extends ITransactionsByContactsFilter {
|
||||
customersIds: number[];
|
||||
}
|
||||
|
||||
export type ITransactionsByCustomersData = ITransactionsByCustomersCustomer[];
|
||||
|
||||
|
||||
@@ -18,7 +18,9 @@ export interface ITransactionsByVendorsVendor {
|
||||
}
|
||||
|
||||
export interface ITransactionsByVendorsFilter
|
||||
extends ITransactionsByContactsFilter {}
|
||||
extends ITransactionsByContactsFilter {
|
||||
vendorsIds: number[];
|
||||
}
|
||||
|
||||
export type ITransactionsByVendorsData = ITransactionsByVendorsVendor[];
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ export default class ARAgingSummaryService {
|
||||
});
|
||||
// Retrieve all customers from the storage.
|
||||
const customers =
|
||||
filter.customersIds.length > 0
|
||||
(filter.customersIds.length > 0)
|
||||
? await customerRepository.findWhereIn('id', filter.customersIds)
|
||||
: await customerRepository.all();
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Inject } from 'typedi';
|
||||
import { raw } from 'objection';
|
||||
import { isEmpty } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import {
|
||||
IItem,
|
||||
@@ -17,10 +18,16 @@ export default class InventoryDetailsRepository {
|
||||
* @param {number} tenantId -
|
||||
* @returns {Promise<IItem>}
|
||||
*/
|
||||
public getInventoryItems(tenantId: number): Promise<IItem[]> {
|
||||
public getInventoryItems(tenantId: number, itemsIds?: number[]): Promise<IItem[]> {
|
||||
const { Item } = this.tenancy.models(tenantId);
|
||||
|
||||
return Item.query().where('type', 'inventory');
|
||||
return Item.query().onBuild((q) => {
|
||||
q.where('type', 'inventory');
|
||||
|
||||
if (!isEmpty(itemsIds)) {
|
||||
q.whereIn('id', itemsIds);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -31,6 +31,7 @@ export default class InventoryDetailsService extends FinancialSheet {
|
||||
return {
|
||||
fromDate: moment().startOf('year').format('YYYY-MM-DD'),
|
||||
toDate: moment().endOf('year').format('YYYY-MM-DD'),
|
||||
itemsIds: [],
|
||||
numberFormat: {
|
||||
precision: 2,
|
||||
divideOn1000: false,
|
||||
@@ -91,8 +92,10 @@ export default class InventoryDetailsService extends FinancialSheet {
|
||||
...query,
|
||||
};
|
||||
// Retrieves the items.
|
||||
const items = await this.reportRepo.getInventoryItems(tenantId);
|
||||
|
||||
const items = await this.reportRepo.getInventoryItems(
|
||||
tenantId,
|
||||
filter.itemsIds
|
||||
);
|
||||
// Opening balance transactions.
|
||||
const openingBalanceTransactions =
|
||||
await this.reportRepo.openingBalanceTransactions(tenantId, filter);
|
||||
|
||||
@@ -26,6 +26,7 @@ export default class InventoryValuationSheetService {
|
||||
get defaultQuery(): IInventoryValuationReportQuery {
|
||||
return {
|
||||
asDate: moment().endOf('year').format('YYYY-MM-DD'),
|
||||
itemsIds: [],
|
||||
numberFormat: {
|
||||
precision: 2,
|
||||
divideOn1000: false,
|
||||
@@ -75,9 +76,6 @@ export default class InventoryValuationSheetService {
|
||||
) {
|
||||
const { Item, InventoryCostLotTracker } = this.tenancy.models(tenantId);
|
||||
|
||||
const inventoryItems = await Item.query().where('type', 'inventory');
|
||||
const inventoryItemsIds = inventoryItems.map((item) => item.id);
|
||||
|
||||
// Settings tenant service.
|
||||
const settings = this.tenancy.settings(tenantId);
|
||||
const baseCurrency = settings.get({
|
||||
@@ -89,6 +87,15 @@ export default class InventoryValuationSheetService {
|
||||
...this.defaultQuery,
|
||||
...query,
|
||||
};
|
||||
const inventoryItems = await Item.query().onBuild(q => {
|
||||
q.where('type', 'inventory');
|
||||
|
||||
if (filter.itemsIds.length > 0) {
|
||||
q.whereIn('id', filter.itemsIds);
|
||||
}
|
||||
});
|
||||
const inventoryItemsIds = inventoryItems.map((item) => item.id);
|
||||
|
||||
const commonQuery = (builder) => {
|
||||
builder.whereIn('item_id', inventoryItemsIds);
|
||||
builder.sum('rate as rate');
|
||||
|
||||
@@ -24,6 +24,7 @@ export default class InventoryValuationReportService {
|
||||
return {
|
||||
fromDate: moment().startOf('year').format('YYYY-MM-DD'),
|
||||
toDate: moment().endOf('year').format('YYYY-MM-DD'),
|
||||
itemsIds: [],
|
||||
numberFormat: {
|
||||
precision: 2,
|
||||
divideOn1000: false,
|
||||
@@ -91,7 +92,13 @@ export default class InventoryValuationReportService {
|
||||
filter,
|
||||
tenantId,
|
||||
});
|
||||
const inventoryItems = await Item.query().where('type', 'inventory');
|
||||
const inventoryItems = await Item.query().onBuild(q => {
|
||||
q.where('type', 'inventory');
|
||||
|
||||
if (filter.itemsIds.length > 0) {
|
||||
q.whereIn('id', filter.itemsIds);
|
||||
}
|
||||
});
|
||||
const inventoryItemsIds = inventoryItems.map((item) => item.id);
|
||||
|
||||
// Calculates the total inventory total quantity and rate `IN` transactions.
|
||||
|
||||
@@ -24,6 +24,7 @@ export default class SalesByItemsReportService {
|
||||
return {
|
||||
fromDate: moment().startOf('year').format('YYYY-MM-DD'),
|
||||
toDate: moment().endOf('year').format('YYYY-MM-DD'),
|
||||
itemsIds: [],
|
||||
numberFormat: {
|
||||
precision: 2,
|
||||
divideOn1000: false,
|
||||
@@ -91,7 +92,14 @@ export default class SalesByItemsReportService {
|
||||
filter,
|
||||
tenantId,
|
||||
});
|
||||
const inventoryItems = await Item.query().where('type', 'inventory');
|
||||
// Inventory items for sales report.
|
||||
const inventoryItems = await Item.query().onBuild((q) => {
|
||||
q.where('type', 'inventory');
|
||||
|
||||
if (filter.itemsIds.length > 0) {
|
||||
q.whereIn('id', filter.itemsIds);
|
||||
}
|
||||
});
|
||||
const inventoryItemsIds = inventoryItems.map((item) => item.id);
|
||||
|
||||
// Calculates the total inventory total quantity and rate `IN` transactions.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { map } from 'lodash';
|
||||
import { isEmpty, map } from 'lodash';
|
||||
import { IAccount, IAccountTransaction } from 'interfaces';
|
||||
import { ACCOUNT_TYPE } from 'data/AccountTypes';
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
@@ -13,10 +13,16 @@ export default class TransactionsByCustomersRepository {
|
||||
* @param {number} tenantId
|
||||
* @returns {Promise<ICustomer[]>}
|
||||
*/
|
||||
public async getCustomers(tenantId: number) {
|
||||
public async getCustomers(tenantId: number, customersIds?: number[]) {
|
||||
const { Customer } = this.tenancy.models(tenantId);
|
||||
|
||||
return Customer.query().orderBy('displayName');
|
||||
return Customer.query().onBuild((q) => {
|
||||
q.orderBy('displayName');
|
||||
|
||||
if (!isEmpty(customersIds)) {
|
||||
q.whereIn('id', customersIds);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,6 +44,8 @@ export default class TransactionsByCustomersService
|
||||
},
|
||||
noneZero: false,
|
||||
noneTransactions: false,
|
||||
|
||||
customersIds: [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -125,7 +127,7 @@ export default class TransactionsByCustomersService
|
||||
const accountsGraph = await accountRepository.getDependencyGraph();
|
||||
|
||||
// Retrieve the report customers.
|
||||
const customers = await this.reportRepository.getCustomers(tenantId);
|
||||
const customers = await this.reportRepository.getCustomers(tenantId, filter.customersIds);
|
||||
|
||||
const openingBalanceDate = moment(filter.fromDate)
|
||||
.subtract(1, 'days')
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { map } from 'lodash';
|
||||
import { isEmpty, map } from 'lodash';
|
||||
import { IVendor, IAccount, IAccountTransaction } from 'interfaces';
|
||||
import HasTenancyService from 'services/Tenancy/TenancyService';
|
||||
import { ACCOUNT_TYPE } from 'data/AccountTypes';
|
||||
@@ -14,10 +14,19 @@ export default class TransactionsByVendorRepository {
|
||||
* @param {number} tenantId
|
||||
* @returns {Promise<IVendor[]>}
|
||||
*/
|
||||
public getVendors(tenantId: number): Promise<IVendor[]> {
|
||||
public getVendors(
|
||||
tenantId: number,
|
||||
vendorsIds?: number[]
|
||||
): Promise<IVendor[]> {
|
||||
const { Vendor } = this.tenancy.models(tenantId);
|
||||
|
||||
return Vendor.query().orderBy('displayName');
|
||||
return Vendor.query().onBuild((q) => {
|
||||
q.orderBy('displayName');
|
||||
|
||||
if (!isEmpty(vendorsIds)) {
|
||||
q.whereIn('id', vendorsIds);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +76,7 @@ export default class TransactionsByVendorRepository {
|
||||
* @param {Date|string} openingDate
|
||||
* @param {number[]} customersIds
|
||||
*/
|
||||
public async getVendorsPeriodTransactions(
|
||||
public async getVendorsPeriodTransactions(
|
||||
tenantId: number,
|
||||
fromDate: Date,
|
||||
toDate: Date
|
||||
|
||||
@@ -45,6 +45,8 @@ export default class TransactionsByVendorsService
|
||||
},
|
||||
noneZero: false,
|
||||
noneTransactions: false,
|
||||
|
||||
vendorsIds: [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -139,12 +141,13 @@ export default class TransactionsByVendorsService
|
||||
group: 'organization',
|
||||
key: 'base_currency',
|
||||
});
|
||||
|
||||
const filter = { ...this.defaultQuery, ...query };
|
||||
|
||||
// Retrieve the report vendors.
|
||||
const vendors = await this.reportRepository.getVendors(tenantId);
|
||||
|
||||
const vendors = await this.reportRepository.getVendors(
|
||||
tenantId,
|
||||
filter.vendorsIds
|
||||
);
|
||||
// Retrieve the accounts graph.
|
||||
const accountsGraph = await accountRepository.getDependencyGraph();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user