mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 05:40:31 +00:00
WIP Make journal entries page.
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
|
||||
|
||||
import React, {useState, useEffect} from 'react';
|
||||
import React, {useMemo, useEffect} from 'react';
|
||||
import * as Yup from 'yup';
|
||||
import MakeJournalEntriesHeader from './MakeJournalEntriesHeader';
|
||||
import MakeJournalEntriesFooter from './MakeJournalEntriesFooter';
|
||||
import MakeJournalEntriesTable from './MakeJournalEntriesTable';
|
||||
import {Formik, useFormik} from "formik";
|
||||
import {useFormik} from "formik";
|
||||
import MakeJournalEntriesConnect from 'connectors/MakeJournalEntries.connect';
|
||||
import AccountsConnect from 'connectors/Accounts.connector';
|
||||
import DashboardConnect from 'connectors/Dashboard.connector';
|
||||
@@ -30,6 +30,7 @@ function MakeJournalEntriesForm({
|
||||
});
|
||||
|
||||
const validationSchema = Yup.object().shape({
|
||||
journal_number: Yup.string().required(),
|
||||
date: Yup.date().required(),
|
||||
reference: Yup.string(),
|
||||
description: Yup.string(),
|
||||
@@ -37,42 +38,66 @@ function MakeJournalEntriesForm({
|
||||
Yup.object().shape({
|
||||
credit: Yup.number().nullable(),
|
||||
debit: Yup.number().nullable(),
|
||||
account_id: Yup.number().nullable().when(['credit', 'debit'], {
|
||||
is: (credit, debit) => credit || debit,
|
||||
then: Yup.number().required(),
|
||||
}),
|
||||
note: Yup.string().nullable(),
|
||||
}),
|
||||
)
|
||||
});
|
||||
|
||||
const defaultEntry = {
|
||||
const defaultEntry = useMemo(() => ({
|
||||
account_id: null,
|
||||
credit: null,
|
||||
debit: null,
|
||||
note: '',
|
||||
};
|
||||
note: '',
|
||||
}), []);
|
||||
|
||||
const formik = useFormik({
|
||||
enableReinitialize: true,
|
||||
validationSchema,
|
||||
initialValues: {
|
||||
reference: '',
|
||||
journal_number: '',
|
||||
date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
description: '',
|
||||
reference: '',
|
||||
entries: [
|
||||
defaultEntry,
|
||||
defaultEntry,
|
||||
defaultEntry,
|
||||
defaultEntry,
|
||||
defaultEntry,
|
||||
defaultEntry,
|
||||
],
|
||||
},
|
||||
onSubmit: (values) => {
|
||||
const form = values.entries.filter((entry) => (
|
||||
(entry.credit || entry.debit)
|
||||
));
|
||||
makeJournalEntries(values).then((response) => {
|
||||
AppToaster.show({
|
||||
message: 'the_account_has_been_submit',
|
||||
});
|
||||
}).catch((error) => {
|
||||
const getTotal = (type = 'credit') => {
|
||||
return form.reduce((total, item) => {
|
||||
return item[type] ? item[type] + total : total;
|
||||
}, 0);
|
||||
}
|
||||
const totalCredit = getTotal('credit');
|
||||
const totalDebit = getTotal('debit');
|
||||
|
||||
});
|
||||
if (totalCredit !== totalDebit) {
|
||||
AppToaster.show({
|
||||
message: 'credit_and_debit_not_equal',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
makeJournalEntries({ ...values, entries: form })
|
||||
.then((response) => {
|
||||
AppToaster.show({
|
||||
message: 'manual_journal_has_been_submit',
|
||||
});
|
||||
}).catch((error) => {
|
||||
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user