feat(server): wip sale invoice tax rates

This commit is contained in:
Ahmed Bouhuolia
2023-08-29 19:12:19 +02:00
parent 09d73db20f
commit 6535424d0f
15 changed files with 219 additions and 49 deletions

View File

@@ -1,14 +1,18 @@
import { ItemEntry } from "@/models";
import { sumBy } from "lodash";
import { Service } from "typedi";
import { Inject, Service } from 'typedi';
import { keyBy, sumBy } from 'lodash';
import * as R from 'ramda';
import { ItemEntry } from '@/models';
import HasTenancyService from '../Tenancy/TenancyService';
@Service()
export class ItemEntriesTaxTransactions {
@Inject()
private tenancy: HasTenancyService;
/**
*
* @param model
* @returns
* Associates tax amount withheld to the model.
* @param model
* @returns
*/
public assocTaxAmountWithheldFromEntries(model: any) {
const entries = model.entries.map((entry) => ItemEntry.fromJson(entry));
@@ -19,4 +23,27 @@ export class ItemEntriesTaxTransactions {
}
return model;
}
}
/**
* Associates tax rate id from tax code to entries.
* @param {number} tenantId
* @param {} model
*/
public assocTaxRateIdFromCodeToEntries =
(tenantId: number) => async (entries: any) => {
const entriesWithCode = entries.filter((entry) => entry.taxCode);
const taxCodes = entriesWithCode.map((entry) => entry.taxCode);
const { TaxRate } = this.tenancy.models(tenantId);
const foundTaxCodes = await TaxRate.query().whereIn('code', taxCodes);
const taxCodesMap = keyBy(foundTaxCodes, 'code');
return entries.map((entry) => {
if (entry.taxCode) {
entry.taxRateId = taxCodesMap[entry.taxCode]?.id;
}
return entry;
});
};
}