fix(server): tax tracking on sale invoices

This commit is contained in:
Ahmed Bouhuolia
2023-09-23 14:19:42 +02:00
parent 92ac0dbd25
commit 1148fef9ad
6 changed files with 48 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ import { Inject, Service } from 'typedi';
import { keyBy, sumBy } from 'lodash';
import { ItemEntry } from '@/models';
import HasTenancyService from '../Tenancy/TenancyService';
import { IItem, IItemEntry, IItemEntryDTO } from '@/interfaces';
@Service()
export class ItemEntriesTaxTransactions {
@@ -45,4 +46,27 @@ export class ItemEntriesTaxTransactions {
return entry;
});
};
/**
* Associates tax rate from tax id to entries.
* @param {number} tenantId
* @returns {Promise<IItemEntry[]>}
*/
public assocTaxRateFromTaxIdToEntries =
(tenantId: number) => async (entries: IItemEntry[]) => {
const entriesWithId = entries.filter((e) => e.taxRateId);
const taxRateIds = entriesWithId.map((e) => e.taxRateId);
const { TaxRate } = this.tenancy.models(tenantId);
const foundTaxes = await TaxRate.query().whereIn('id', taxRateIds);
const taxRatesMap = keyBy(foundTaxes, 'id');
return entries.map((entry) => {
if (entry.taxRateId) {
entry.taxRate = taxRatesMap[entry.taxRateId]?.rate;
}
return entry;
});
};
}