mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
refactoring: payment receive form.
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import moment from 'moment';
|
||||
import { transformToForm } from 'utils';
|
||||
import { transformToForm, safeSumBy } from 'utils';
|
||||
|
||||
// Default payment receive entry.
|
||||
export const defaultPaymentReceiveEntry = {
|
||||
id: '',
|
||||
payment_amount: '',
|
||||
invoice_id: '',
|
||||
invoice_no: '',
|
||||
due_amount: '',
|
||||
date: '',
|
||||
amount: '',
|
||||
};
|
||||
|
||||
// Form initial values.
|
||||
@@ -21,13 +23,61 @@ export const defaultPaymentReceive = {
|
||||
entries: [],
|
||||
};
|
||||
|
||||
export const transformToEditForm = (paymentReceive, paymentReceiveEntries) => {
|
||||
return {
|
||||
...transformToForm(paymentReceive, defaultPaymentReceive),
|
||||
entries: [
|
||||
...paymentReceiveEntries.map((paymentReceiveEntry) => ({
|
||||
...transformToForm(paymentReceiveEntry, defaultPaymentReceiveEntry),
|
||||
})),
|
||||
],
|
||||
};
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export const transformToEditForm = (paymentReceive, paymentReceiveEntries) => ({
|
||||
...transformToForm(paymentReceive, defaultPaymentReceive),
|
||||
full_amount: safeSumBy(paymentReceiveEntries, 'payment_amount'),
|
||||
entries: [
|
||||
...paymentReceiveEntries.map((paymentReceiveEntry) => ({
|
||||
...transformToForm(paymentReceiveEntry, defaultPaymentReceiveEntry),
|
||||
})),
|
||||
],
|
||||
});
|
||||
|
||||
/**
|
||||
* Transformes the given invoices to the new page receivable entries.
|
||||
*/
|
||||
export const transformInvoicesNewPageEntries = (invoices) => [
|
||||
...invoices.map((invoice, index) => ({
|
||||
index: index + 1,
|
||||
invoice_id: invoice.id,
|
||||
entry_type: 'invoice',
|
||||
due_amount: invoice.due_amount,
|
||||
date: invoice.invoice_date,
|
||||
amount: invoice.balance,
|
||||
payment_amount: 0,
|
||||
invoice_no: invoice.invoice_no,
|
||||
total_payment_amount: invoice.payment_amount,
|
||||
})),
|
||||
];
|
||||
|
||||
export const transformEntriesToEditForm = (receivableEntries) => [
|
||||
...transformInvoicesNewPageEntries([...(receivableEntries || [])]),
|
||||
];
|
||||
|
||||
export const clearAllPaymentEntries = (entries) => [
|
||||
...entries.map((entry) => ({ ...entry, payment_amount: 0 })),
|
||||
];
|
||||
|
||||
export const amountPaymentEntries = (amount, entries) => {
|
||||
let total = amount;
|
||||
|
||||
return entries.map((item) => {
|
||||
const diff = Math.min(item.due_amount, total);
|
||||
total -= Math.max(diff, 0);
|
||||
|
||||
return {
|
||||
...item,
|
||||
payment_amount: diff,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export const fullAmountPaymentEntries = (entries) => {
|
||||
return entries.map((item) => ({
|
||||
...item,
|
||||
payment_amount: item.due_amount,
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user