mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
WIP Financial accounting module.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import express from 'express';
|
||||
import { check, validationResult, oneOf } from 'express-validator';
|
||||
import { difference } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import asyncMiddleware from '../middleware/asyncMiddleware';
|
||||
import jwtAuth from '@/http/middleware/jwtAuth';
|
||||
import Account from '@/models/Account';
|
||||
import JournalPoster from '@/services/Accounting/JournalPoster';
|
||||
import JournalEntry from '@/services/Accounting/JournalEntry';
|
||||
import ManualJournal from '@/models/ManualJournal';
|
||||
|
||||
export default {
|
||||
/**
|
||||
@@ -13,6 +16,8 @@ export default {
|
||||
router() {
|
||||
const router = express.Router();
|
||||
|
||||
router.use(jwtAuth);
|
||||
|
||||
router.post('/',
|
||||
this.openingBalnace.validation,
|
||||
asyncMiddleware(this.openingBalnace.handler));
|
||||
@@ -27,11 +32,14 @@ export default {
|
||||
*/
|
||||
openingBalnace: {
|
||||
validation: [
|
||||
check('date').optional(),
|
||||
check('note').optional().trim().escape(),
|
||||
check('balance_adjustment_account').exists().isNumeric().toInt(),
|
||||
check('accounts').isArray({ min: 1 }),
|
||||
check('accounts.*.id').exists().isInt(),
|
||||
oneOf([
|
||||
check('accounts.*.debit').isNumeric().toFloat(),
|
||||
check('accounts.*.credit').isNumeric().toFloat(),
|
||||
check('accounts.*.debit').exists().isNumeric().toFloat(),
|
||||
check('accounts.*.credit').exists().isNumeric().toFloat(),
|
||||
]),
|
||||
],
|
||||
async handler(req, res) {
|
||||
@@ -44,13 +52,19 @@ export default {
|
||||
}
|
||||
|
||||
const { accounts } = req.body;
|
||||
const { user } = req;
|
||||
const form = { ...req.body };
|
||||
const date = moment(form.date).format('YYYY-MM-DD');
|
||||
|
||||
const accountsIds = accounts.map((account) => account.id);
|
||||
const accountsCollection = await Account.query()
|
||||
.select(['id'])
|
||||
.whereIn('id', accountsIds);
|
||||
const storedAccounts = await Account.query()
|
||||
.select(['id']).whereIn('id', accountsIds)
|
||||
.withGraphFetched('type');
|
||||
|
||||
const accountsCollection = new Map(storedAccounts.map(i => [i.id, i]));
|
||||
|
||||
// Get the stored accounts Ids and difference with submit accounts.
|
||||
const accountsStoredIds = accountsCollection.map((account) => account.id);
|
||||
const accountsStoredIds = storedAccounts.map((account) => account.id);
|
||||
const notFoundAccountsIds = difference(accountsIds, accountsStoredIds);
|
||||
const errorReasons = [];
|
||||
|
||||
@@ -58,36 +72,76 @@ export default {
|
||||
const ids = notFoundAccountsIds.map((a) => parseInt(a, 10));
|
||||
errorReasons.push({ type: 'NOT_FOUND_ACCOUNT', code: 100, ids });
|
||||
}
|
||||
if (form.balance_adjustment_account) {
|
||||
const account = await Account.query().findById(form.balance_adjustment_account);
|
||||
|
||||
if (!account) {
|
||||
errorReasons.push({ type: 'BALANCE.ADJUSTMENT.ACCOUNT.NOT.EXIST', code: 300 });
|
||||
}
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.boom.badData(null, { errors: errorReasons });
|
||||
}
|
||||
|
||||
const sharedJournalDetails = new JournalEntry({
|
||||
referenceType: 'OpeningBalance',
|
||||
referenceId: 1,
|
||||
});
|
||||
const journalEntries = new JournalPoster(sharedJournalDetails);
|
||||
const journalEntries = new JournalPoster();
|
||||
|
||||
accounts.forEach((account) => {
|
||||
const entry = new JournalEntry({
|
||||
account: account.id,
|
||||
accountNormal: account.type.normal,
|
||||
});
|
||||
const storedAccount = accountsCollection.get(account.id);
|
||||
|
||||
// Can't continue in case the stored account was not found.
|
||||
if (!storedAccount) { return; }
|
||||
|
||||
const entryModel = new JournalEntry({
|
||||
referenceType: 'OpeningBalance',
|
||||
account: account.id,
|
||||
accountNormal: storedAccount.type.normal,
|
||||
userId: user.id,
|
||||
});
|
||||
if (account.credit) {
|
||||
entry.credit = account.credit;
|
||||
journalEntries.credit(entry);
|
||||
entryModel.entry.credit = account.credit;
|
||||
journalEntries.credit(entryModel);
|
||||
} else if (account.debit) {
|
||||
entry.debit = account.debit;
|
||||
journalEntries.debit(entry);
|
||||
entryModel.entry.debit = account.debit;
|
||||
journalEntries.debit(entryModel);
|
||||
}
|
||||
});
|
||||
// Calculates the credit and debit balance of stacked entries.
|
||||
const trial = journalEntries.getTrialBalance();
|
||||
|
||||
if (trial.credit !== trial.debit) {
|
||||
const entryModel = new JournalEntry({
|
||||
referenceType: 'OpeningBalance',
|
||||
account: form.balance_adjustment_account,
|
||||
accountNormal: 'credit',
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
if (trial.credit > trial.debit) {
|
||||
entryModel.entry.credit = Math.abs(trial.credit);
|
||||
journalEntries.credit(entryModel);
|
||||
|
||||
} else if (trial.credit < trial.debit) {
|
||||
entryModel.entry.debit = Math.abs(trial.debit);
|
||||
journalEntries.debit(entryModel);
|
||||
}
|
||||
}
|
||||
const manualJournal = await ManualJournal.query().insert({
|
||||
amount: Math.max(trial.credit, trial.debit),
|
||||
transaction_type: 'OpeningBalance',
|
||||
date,
|
||||
note: form.note,
|
||||
user_id: user.id,
|
||||
});
|
||||
|
||||
journalEntries.entries = journalEntries.entries.map((entry) => ({
|
||||
...entry,
|
||||
referenceId: manualJournal.id,
|
||||
}));
|
||||
await Promise.all([
|
||||
journalEntries.saveEntries(),
|
||||
journalEntries.saveBalance(),
|
||||
]);
|
||||
return res.status(200).send();
|
||||
return res.status(200).send({ id: manualJournal.id });
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user