fix: Filter financial reports by items, customers or vendors.

This commit is contained in:
a.bouhuolia
2021-07-25 03:59:02 +02:00
parent 504b380da6
commit 3a7f8a4512
71 changed files with 1021 additions and 350 deletions

View File

@@ -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();

View File

@@ -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);
}
})
}
/**

View File

@@ -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);

View File

@@ -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');

View File

@@ -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.

View File

@@ -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.

View File

@@ -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);
}
});
}
/**

View File

@@ -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')

View File

@@ -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

View File

@@ -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();