-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
404 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict' | ||
|
||
import { controller } from '../reportController.js' | ||
import { initConfig } from '../../config/index.js' | ||
|
||
/** calculateYearTaxes */ | ||
describe('calculateYearTaxes', () => { | ||
beforeAll(async () => { | ||
await initConfig() | ||
}) | ||
it('returns year taxes', async () => { | ||
const actual = await controller.calculateYearTaxes('ckkfm160400003e5iimcnpt4s', 'ckkfmwjgo00033e5i0z3hktbj', '2021') | ||
console.log(actual) | ||
expect(actual).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict' | ||
|
||
import express from 'express' | ||
import asyncHandler from 'express-async-handler' | ||
import { DashboardDb, CategoryDb, BusinessDb } from '../db/index.js' | ||
import { c } from '@psbfinances/shared/core/index.js' | ||
import getLogger from '../core/logger.js' | ||
import { controller as tripController } from './tripController.js' | ||
import utils from './utils.js' | ||
import validator from 'validator' | ||
|
||
const logger = getLogger(import.meta.url) | ||
|
||
const controller = { | ||
/** @link module:psbf/api/reports */ | ||
/** | ||
* Gets tenant transactions by account and date range. | ||
* @property {AppRequest} req | ||
* @property {YearTaxRequest} req.query | ||
* @property {import(express).Response} res | ||
*/ | ||
getYearTaxes: async (req, res) => { | ||
const { tenantId, userId } = utils.getBasicRequestData(req) | ||
const criteria = req.query | ||
logger.info('list', { tenantId, userId, ...criteria }) | ||
const result = await controller.calculateYearTaxes(tenantId, criteria.businessId, criteria.year) | ||
res.json(result) | ||
}, | ||
|
||
/** | ||
* Maps PL items to categories. | ||
* @param {psbf.Category[]} categories | ||
* @param {string} categoryType | ||
* @param {PLItem[]} pl | ||
* @return {*} | ||
*/ | ||
mapAmountsToCategories: (categories, categoryType, pl) => { | ||
const typeCategories = categories.filter(x => x.type === categoryType && !x.isPersonal). | ||
sort((x, y) => x.name >= y.name ? 1 : 0) | ||
return typeCategories.map(x => { | ||
const plItem = pl.find(y => y.categoryId === x.id) | ||
return { | ||
categoryId: x.id, | ||
categoryName: x.name, | ||
amount: Math.abs(plItem ? plItem.amount / 100 : 0) | ||
} | ||
}) | ||
|
||
}, | ||
|
||
/** | ||
* Calculates business year taxes (Schedule C). | ||
* @param {string} tenantId | ||
* @param {string} businessId | ||
* @param {string} year | ||
* @return {Promise<YearTaxResponse>} | ||
*/ | ||
calculateYearTaxes: async (tenantId, businessId, year) => { | ||
logger.info('calculateYearTaxes', { tenantId, businessId, year }) | ||
const isEmpty = validator.isEmpty | ||
if (isEmpty(businessId) || isEmpty(tenantId) || isEmpty(year)) throw new Error('Invalid report criteria') | ||
const businessDb = new BusinessDb() | ||
const business = await businessDb.get(businessId, tenantId) | ||
if (business.length === 0) throw new Error('Invalid business') | ||
|
||
const dashboardDb = new DashboardDb() | ||
const categoriesDb = new CategoryDb() | ||
const pl = await dashboardDb.listBusinessPLCurrentYear(tenantId, businessId, year) | ||
|
||
const categories = await categoriesDb.list(tenantId) | ||
const businessCategories = categories.filter(x => !x.isPersonal).sort((x, y) => x.name >= y.name ? 1 : 0) | ||
const income = controller.mapAmountsToCategories(businessCategories, c.transactionType.INCOME, pl) | ||
const expenses = controller.mapAmountsToCategories(businessCategories, c.transactionType.EXPENSE, pl) | ||
|
||
const totalIncome = income.reduce((t, x) => t += x.amount, 0) | ||
const totalExpenses = expenses.reduce((t, x) => t += x.amount, 0) | ||
const profit = totalIncome - totalExpenses | ||
|
||
const mileage = await tripController.calcTotalByBusinessAndDates(tenantId, businessId, year) | ||
|
||
return { year, businessId, income, expenses, totalIncome, totalExpenses, profit, mileage } | ||
} | ||
} | ||
|
||
/** @type {import('express').Router} */ | ||
const router = express.Router() | ||
router.route('/year-tax').get(asyncHandler(controller.getYearTaxes)) | ||
export default router | ||
|
||
export { | ||
controller | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
import { Api } from './api.js' | ||
import queryString from 'query-string' | ||
|
||
export default class ReportApi extends Api { | ||
constructor (endpoint) { | ||
super(endpoint) | ||
} | ||
|
||
/** | ||
* @param {string} year | ||
* @param {string} businessId | ||
* @return {AxiosResponse<YearTaxResponse>} | ||
*/ | ||
async getYearTaxes (year, businessId) { | ||
const query = queryString.stringify({year, businessId}) | ||
return this.api.get(`/reports/year-tax?${query}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.