WIP Manual journals.

This commit is contained in:
Ahmed Bouhuolia
2020-04-07 22:38:57 +02:00
parent 05cc76b4f1
commit 8ba96e7343
6 changed files with 650 additions and 14 deletions

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback, useState, useEffect } from 'react';
import MoneyInputGroup from 'components/MoneyInputGroup';
// Input form cell renderer.
@@ -23,6 +23,10 @@ const MoneyFieldCellRenderer = ({
payload.updateData(index, id, updateValue);
};
useEffect(() => {
setValue(initialValue);
}, [initialValue])
return (<MoneyInputGroup
value={value}
prefix={'$'}

View File

@@ -8,7 +8,6 @@ import { FormattedList } from 'react-intl';
export default function MakeJournalEntriesFooter({
formik,
}) {
return (
<div>
<div class="form__floating-footer">

View File

@@ -1,4 +1,4 @@
import { check, query, oneOf, validationResult } from 'express-validator';
import { check, query, oneOf, validationResult, param } from 'express-validator';
import express from 'express';
import { difference } from 'lodash';
import moment from 'moment';
@@ -8,6 +8,7 @@ import JWTAuth from '@/http/middleware/jwtAuth';
import JournalPoster from '@/services/Accounting/JournalPoster';
import JournalEntry from '@/services/Accounting/JournalEntry';
import ManualJournal from '@/models/JournalEntry';
import AccountTransaction from '@/models/AccountTransaction';
import Resource from '@/models/Resource';
import View from '@/models/View';
import {
@@ -32,6 +33,14 @@ export default {
this.makeJournalEntries.validation,
asyncMiddleware(this.makeJournalEntries.handler));
router.post('/manual-journal/:id',
this.editManualJournal.validation,
asyncMiddleware(this.editManualJournal.handler));
router.delete('/manual-journals/:id',
this.deleteManualJournal.validation,
asyncMiddleware(this.deleteManualJournal.handler));
router.post('/recurring-journal-entries',
this.recurringJournalEntries.validation,
asyncMiddleware(this.recurringJournalEntries.handler));
@@ -125,7 +134,7 @@ export default {
*/
makeJournalEntries: {
validation: [
check('date').isISO8601(),
check('date').exists().isISO8601(),
check('journal_number').exists().trim().escape(),
check('transaction_type').optional({ nullable: true }).trim().escape(),
check('reference').optional({ nullable: true }),
@@ -261,7 +270,189 @@ export default {
code: 'validation_error', ...validationErrors,
});
}
},
},
editManualJournal: {
validation: [
param('id').exists().isNumeric().toInt(),
check('date').exists().isISO8601(),
check('journal_number').exists().trim().escape(),
check('transaction_type').optional({ nullable: true }).trim().escape(),
check('reference').optional({ nullable: true }),
check('description').optional().trim().escape(),
check('entries').isArray({ min: 2 }),
check('entries.*.credit').optional({ nullable: true }).isNumeric().toInt(),
check('entries.*.debit').optional({ nullable: true }).isNumeric().toInt(),
check('entries.*.account_id').isNumeric().toInt(),
check('entries.*.note').optional(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const form = {
date: new Date(),
transaction_type: 'journal',
reference: '',
...req.body,
};
const { id } = req.params;
const manualJournal = await ManualJournal.query().where('id', id).first();
if (!manualJournal) {
return res.status(4040).send({
errors: [{ type: 'MANUAL.JOURNAL.NOT.FOUND', code: 100 }],
});
}
let totalCredit = 0;
let totalDebit = 0;
const { user } = req;
const errorReasons = [];
const entries = form.entries.filter((entry) => (entry.credit || entry.debit));
const formattedDate = moment(form.date).format('YYYY-MM-DD');
entries.forEach((entry) => {
if (entry.credit > 0) {
totalCredit += entry.credit;
}
if (entry.debit > 0) {
totalDebit += entry.debit;
}
});
if (totalCredit <= 0 || totalDebit <= 0) {
errorReasons.push({
type: 'CREDIT.DEBIT.SUMATION.SHOULD.NOT.EQUAL.ZERO',
code: 400,
});
}
if (totalCredit !== totalDebit) {
errorReasons.push({ type: 'CREDIT.DEBIT.NOT.EQUALS', code: 100 });
}
const journalNumber = await ManualJournal.query()
.where('journal_number', form.journal_number)
.whereNot('id', id)
.first();
if (journalNumber) {
errorReasons.push({ type: 'JOURNAL.NUMBER.ALREADY.EXISTS', code: 300 });
}
const accountsIds = entries.map((entry) => entry.account_id);
const accounts = await Account.query().whereIn('id', accountsIds)
.withGraphFetched('type');
const storedAccountsIds = accounts.map((account) => account.id);
if (difference(accountsIds, storedAccountsIds).length > 0) {
errorReasons.push({ type: 'ACCOUNTS.IDS.NOT.FOUND', code: 200 });
}
if (errorReasons.length > 0) {
return res.status(400).send({ errors: errorReasons });
}
await ManualJournal.query()
.where('id', manualJournal.id)
.update({
reference: form.reference,
transaction_type: 'Journal',
journalNumber: form.journal_number,
amount: totalCredit,
date: formattedDate,
description: form.description,
});
const transactions = await AccountTransaction.query()
.whereIn('reference_type', ['Journal'])
.where('reference_id', manualJournal.id)
.withGraphFetched('account.type');
const journal = new JournalPoster();
journal.loadEntries(transactions);
journal.removeEntries();
await Promise.all([
journal.deleteEntries(),
journal.saveEntries(),
journal.saveBalance(),
]);
return res.status(200).send({});
},
},
getManualJournal: {
validation: [
param('id').exists().isNumeric().toInt(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const { id } = req.params;
const manualJournal = await ManualJournal.query()
.where('id', id).first();
if (!manualJournal) {
return res.status(404).send({
errors: [{ type: 'MANUAL.JOURNAL.NOT.FOUND', code: 100 }],
});
}
},
},
/**
* Deletes manual journal transactions and associated
* accounts transactions.
*/
deleteManualJournal: {
validation: [
param('id').exists().isNumeric().toInt(),
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
});
}
const { id } = req.params;
const manualJournal = await ManualJournal.query()
.where('id', id).first();
if (!manualJournal) {
return res.status(404).send({
errors: [{ type: 'MANUAL.JOURNAL.NOT.FOUND', code: 100 }],
});
}
const transactions = await AccountTransaction.query()
.whereIn('reference_type', ['Journal', 'ManualJournal'])
.where('reference_id', manualJournal.id)
.withGraphFetched('account.type');
const journal = new JournalPoster();
journal.loadEntries(transactions);
journal.removeEntries();
await ManualJournal.query()
.where('id', manualJournal.id)
.delete();
await Promise.all([
journal.deleteEntries(),
journal.saveBalance(),
]);
return res.status(200).send({ id });
},
},

View File

@@ -12,6 +12,7 @@ export default class JournalPoster {
constructor() {
this.entries = [];
this.balancesChange = {};
this.deletedEntriesIds = [];
}
/**
@@ -155,14 +156,32 @@ export default class JournalPoster {
}
/**
* Delete the given or all stacked entries.
*
* @param {Array} ids -
*/
async deleteEntries(ids) {
const entriesIds = ids || this.entries.map((e) => e.id);
removeEntries(ids = []) {
const targetIds = (ids.length <= 0) ? this.entries.map(e => e.id) : ids;
const removeEntries = this.entries.filter((e) => targetIds.indexOf(e.id) !== -1);
if (entriesIds.length > 0) {
await AccountTransaction.query().whereIn('id', entriesIds).delete();
this.entries = this.entries
.filter(e => targetIds.indexOf(e.id) === -1)
removeEntries.forEach((entry) => {
entry.credit = -1 * entry.credit;
entry.debit = -1 * entry.debit;
this.setAccountBalanceChange(entry, entry.accountNormal);
});
this.deletedEntriesIds.push(
...removeEntries.map(entry => entry.id),
);
}
async deleteEntries() {
if (this.deletedEntriesIds.length > 0) {
await AccountTransaction.query()
.whereIn('id', this.deletedEntriesIds)
.delete();
}
}

View File

@@ -7,6 +7,7 @@ import {
import moment from 'moment';
import ManualJournal from '@/models/ManualJournal';
import AccountTransaction from '@/models/AccountTransaction';
import AccountBalance from '@/models/AccountBalance';
let loginRes;
@@ -18,7 +19,7 @@ describe('routes: `/accounting`', () => {
loginRes = null;
});
describe.only('route: `/accounting/make-journal-entries`', async () => {
describe('route: `/accounting/make-journal-entries`', async () => {
it('Should sumation of credit or debit does not equal zero.', async () => {
const account = await create('account');
const res = await request()
@@ -167,7 +168,7 @@ describe('routes: `/accounting`', () => {
});
});
it.only('Should store manual journal transaction to the storage.', async () => {
it('Should store manual journal transaction to the storage.', async () => {
const account1 = await create('account');
const account2 = await create('account');
@@ -249,6 +250,334 @@ describe('routes: `/accounting`', () => {
});
});
describe('route: POST: `/accounting/manual-journal/:id`', () => {
it('Should response not found in case manual journal transaction was not exists.', async () => {
const res = await request()
.post('/api/manual-journal/1000')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(404);
});
it('Should sumation of credit or debit be equal zero.', async () => {
const manualJournal = await create('manual_journal');
const res = await request()
.post(`/api/accounting/manual-journal/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send({
date: new Date().toISOString(),
journal_number: '123',
reference: 'ASC',
entries: [
{
credit: 0,
debit: 0,
account_id: 2000,
},
{
credit: 0,
debit: 0,
account_id: 2000,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'CREDIT.DEBIT.SUMATION.SHOULD.NOT.EQUAL.ZERO',
code: 400,
});
});
it('Should all credit and debit sumation be equal.', async () => {
const manualJournal = await create('manual_journal');
const res = await request()
.post(`/api/accounting/manual-journal/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send({
date: new Date().toISOString(),
journal_number: '123',
reference: 'ASC',
entries: [
{
credit: 0,
debit: 2000,
account_id: 2000,
},
{
credit: 1000,
debit: 0,
account_id: 2000,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'CREDIT.DEBIT.NOT.EQUALS', code: 100,
});
});
it('Should response journal number already exists in case another one on the storage.', async () => {
const manualJournal = await create('manual_journal');
const manualJournal2 = await create('manual_journal');
const jsonBody = {
date: new Date().toISOString(),
reference: 'ASC',
entries: [
{
credit: 0,
debit: 2000,
account_id: 2000,
},
{
credit: 1000,
debit: 0,
account_id: 2000,
},
],
};
const res = await request()
.post(`/api/accounting/manual-journal/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send({
...jsonBody,
journal_number: manualJournal2.journalNumber,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'JOURNAL.NUMBER.ALREADY.EXISTS', code: 300,
});
});
it('Should not response journal number exists in case was unique number.', async () => {
const manualJournal = await create('manual_journal');
const manualJournal2 = await create('manual_journal');
const jsonBody = {
date: new Date().toISOString(),
reference: 'ASC',
entries: [
{
credit: 0,
debit: 2000,
account_id: 2000,
},
{
credit: 1000,
debit: 0,
account_id: 2000,
},
],
};
const res = await request()
.post(`/api/accounting/manual-journal/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send({
...jsonBody,
journal_number: manualJournal.journalNumber,
});
expect(res.status).equals(400);
expect(res.body.errors).not.include.something.that.deep.equal({
type: 'JOURNAL.NUMBER.ALREADY.EXISTS', code: 300,
});
})
it('Should response error in case account id not exists in one of the given entries.', async () => {
const manualJournal = await create('manual_journal');
const manualJournal2 = await create('manual_journal');
const jsonBody = {
date: new Date().toISOString(),
reference: 'ASC',
entries: [
{
credit: 0,
debit: 1000,
account_id: 2000,
},
{
credit: 1000,
debit: 0,
account_id: 2000,
},
],
};
const res = await request()
.post(`/api/accounting/manual-journal/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send({
...jsonBody,
journal_number: manualJournal.journalNumber,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'ACCOUNTS.IDS.NOT.FOUND', code: 200,
});
});
it('Should update the given manual journal transaction in the storage.', async () => {
const manualJournal = await create('manual_journal');
const account1 = await create('account');
const account2 = await create('account');
const res = await request()
.post(`/api/accounting/manual-journal/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send({
journal_number: '123',
date: new Date().toISOString(),
reference: 'ABC',
description: 'hello world',
entries: [
{
credit: 0,
debit: 1000,
account_id: account1.id,
},
{
credit: 1000,
debit: 0,
account_id: account2.id,
},
],
});
const foundManualJournal = await ManualJournal.query()
.where('id', manualJournal.id);
expect(foundManualJournal.length).equals(1);
expect(foundManualJournal[0].journalNumber).equals('123');
expect(foundManualJournal[0].reference).equals('ABC');
expect(foundManualJournal[0].description).equals('hello world');
});
it('Should update account transactions that associated to the manual journal transaction.', async () => {
const manualJournal = await create('manual_journal');
const account1 = await create('account');
const account2 = await create('account');
const transaction = await create('account_transaction', {
reference_type: 'Journal',
reference_id: manualJournal.id,
});
const transaction2 = await create('account_transaction', {
reference_type: 'Journal',
reference_id: manualJournal.id,
});
const res = await request()
.post(`/api/accounting/manual-journal/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send({
journal_number: '123',
date: new Date().toISOString(),
reference: 'ABC',
description: 'hello world',
entries: [
{
credit: 0,
debit: 1000,
account_id: account1.id,
note: 'hello 1',
},
{
credit: 1000,
debit: 0,
account_id: account2.id,
note: 'hello 2',
},
],
});
const foundTransactions = await AccountTransaction.query();
expect(foundTransactions.length).equals(2);
expect(foundTransactions[0].credit).equals(0);
expect(foundTransactions[0].debit).equals(1000);
expect(foundTransactions[0].accountId).equals(account1.id);
expect(foundTransactions[0].note).equals('hello 1');
expect(foundTransactions[1].credit).equals(1000);
expect(foundTransactions[1].debit).equals(0);
expect(foundTransactions[1].accountId).equals(account2.id);
expect(foundTransactions[1].note).equals('hello 2');
});
});
describe('route: DELETE `accounting/manual-journals/:id`', () => {
it('Should response not found in case the manual journal transaction was not found.', async() => {
const res = await request()
.delete('/api/accounting/manual-journals/1000')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equal({
type: 'MANUAL.JOURNAL.NOT.FOUND', code: 100,
});
});
it('Should delete manual journal transactions from storage.', async () => {
const manualJournal = await create('manual_journal');
const res = await request()
.delete(`/api/accounting/manual-journals/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send();
const foundManualTransaction = await ManualJournal.query()
.where('id', manualJournal.id).first();
expect(foundManualTransaction).equals(undefined);
});
it('Should delete associated transactions of journal transaction.', async () => {
const manualJournal = await create('manual_journal');
const transaction1 = await create('account_transaction', {
reference_type: 'Journal', reference_id: manualJournal.id,
});
const transaction2 = await create('account_transaction', {
reference_type: 'Journal', reference_id: manualJournal.id,
});
const res = await request()
.delete(`/api/accounting/manual-journals/${manualJournal.id}`)
.set('x-access-token', loginRes.body.token)
.send();
const foundTransactions = await AccountTransaction.query();
expect(foundTransactions.length).equals(0);
});
it('Should revert accounts balance after delete account transactions.', () => {
});
});
describe.only('route: GET `accounting/manual-journals/:id`', () => {
it('Should response not found in case manual transaction id was not exists.', async () => {
const res = await request()
.delete('/api/accounting/manual-journals/100')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'MANUAL.JOURNAL.NOT.FOUND', code: 100,
});
});
it('Should response manual transaction and transactions metadata.', async () => {
});
});
describe('route: `accounting/manual-journals`', async () => {
@@ -263,7 +592,7 @@ describe('routes: `/accounting`', () => {
expect(res.body.errors[0].code).equals(200);
});
it.only('Should retrieve all manual journals with pagination meta.', async () => {
it('Should retrieve all manual journals with pagination meta.', async () => {
const resource = await create('resource', { name: 'manual_journals' });
const manualJournal1 = await create('manual_journal');
const manualJournal2 = await create('manual_journal');
@@ -275,7 +604,7 @@ describe('routes: `/accounting`', () => {
expect(res.status).equals(200);
expect(res.body.manualJournals).to.be.a('array');
expect(res.body.manualJournals.length).equals(2);
expect(res.body.manualJournals.length).equals(2);
});
});

View File

@@ -220,8 +220,102 @@ describe('JournalPoster', () => {
});
describe('deleteEntries', () => {
describe.only('removeEntries', () => {
it('Should remove all entries in the collection.', () => {
const journalPoster = new JournalPoster();
const journalEntry1 = new JournalEntry({
id: 1,
credit: 1000,
account: 1,
accountNormal: 'credit',
});
const journalEntry2 = new JournalEntry({
id: 2,
debit: 1000,
account: 2,
accountNormal: 'debit',
});
journalPoster.credit(journalEntry1);
journalPoster.debit(journalEntry2);
journalPoster.removeEntries();
expect(journalPoster.entries.length).equals(0);
});
it('Should remove the given entries ids from the collection.', () => {
const journalPoster = new JournalPoster();
const journalEntry1 = new JournalEntry({
id: 1,
credit: 1000,
account: 1,
accountNormal: 'credit',
});
const journalEntry2 = new JournalEntry({
id: 2,
debit: 1000,
account: 2,
accountNormal: 'debit',
});
journalPoster.credit(journalEntry1);
journalPoster.debit(journalEntry2);
journalPoster.removeEntries([1]);
expect(journalPoster.entries.length).equals(1);
});
it('Should the removed entries ids be stacked to deleted entries ids.', () => {
const journalPoster = new JournalPoster();
const journalEntry1 = new JournalEntry({
id: 1,
credit: 1000,
account: 1,
accountNormal: 'credit',
});
const journalEntry2 = new JournalEntry({
id: 2,
debit: 1000,
account: 2,
accountNormal: 'debit',
});
journalPoster.credit(journalEntry1);
journalPoster.debit(journalEntry2);
journalPoster.removeEntries();
expect(journalPoster.deletedEntriesIds.length).equals(2);
expect(journalPoster.deletedEntriesIds[0]).equals(1);
expect(journalPoster.deletedEntriesIds[1]).equals(2);
});
it.only('Should revert the account balance after remove the entries.', () => {
const journalPoster = new JournalPoster();
const journalEntry1 = new JournalEntry({
id: 1,
credit: 1000,
account: 1,
accountNormal: 'credit',
});
const journalEntry2 = new JournalEntry({
id: 2,
debit: 1000,
account: 2,
accountNormal: 'debit',
});
journalPoster.credit(journalEntry1);
journalPoster.debit(journalEntry2);
journalPoster.removeEntries([1]);
expect(journalPoster.balancesChange['1']).equals(0);
expect(journalPoster.balancesChange['2']).equals(1000);
})
});
describe('deleteEntries', () => {
it('Should delete all entries from the storage based on the stacked deleted entries ids.', () => {
});
});
describe('reverseEntries()', () => {