mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
102 lines
2.4 KiB
JavaScript
102 lines
2.4 KiB
JavaScript
import React from 'react';
|
|
import { useFormikContext } from 'formik';
|
|
import moment from 'moment';
|
|
import * as R from 'ramda';
|
|
import {
|
|
defaultFastFieldShouldUpdate,
|
|
transactionNumber,
|
|
repeatValue,
|
|
transformToForm,
|
|
} from 'utils';
|
|
import {
|
|
updateItemsEntriesTotal,
|
|
ensureEntriesHaveEmptyLine,
|
|
} from 'containers/Entries/utils';
|
|
|
|
export const MIN_LINES_NUMBER = 4;
|
|
|
|
export const defaultReceiptEntry = {
|
|
index: 0,
|
|
item_id: '',
|
|
rate: '',
|
|
discount: '',
|
|
quantity: '',
|
|
description: '',
|
|
amount: '',
|
|
};
|
|
|
|
export const defaultReceipt = {
|
|
customer_id: '',
|
|
deposit_account_id: '',
|
|
receipt_number: '',
|
|
receipt_date: moment(new Date()).format('YYYY-MM-DD'),
|
|
reference_no: '',
|
|
receipt_message: '',
|
|
statement: '',
|
|
closed: '',
|
|
entries: [...repeatValue(defaultReceiptEntry, MIN_LINES_NUMBER)],
|
|
};
|
|
|
|
/**
|
|
* Transform to form in edit mode.
|
|
*/
|
|
export const transformToEditForm = (receipt) => {
|
|
const initialEntries = [
|
|
...receipt.entries.map((entry) => ({
|
|
...transformToForm(entry, defaultReceiptEntry),
|
|
})),
|
|
...repeatValue(
|
|
defaultReceiptEntry,
|
|
Math.max(MIN_LINES_NUMBER - receipt.entries.length, 0),
|
|
),
|
|
];
|
|
const entries = R.compose(
|
|
ensureEntriesHaveEmptyLine(defaultReceiptEntry),
|
|
updateItemsEntriesTotal,
|
|
)(initialEntries);
|
|
|
|
return {
|
|
...transformToForm(receipt, defaultReceipt),
|
|
entries,
|
|
};
|
|
};
|
|
|
|
export const useObserveReceiptNoSettings = (prefix, nextNumber) => {
|
|
const { setFieldValue } = useFormikContext();
|
|
|
|
React.useEffect(() => {
|
|
const receiptNo = transactionNumber(prefix, nextNumber);
|
|
setFieldValue('receipt_number', receiptNo);
|
|
}, [setFieldValue, prefix, nextNumber]);
|
|
};
|
|
|
|
/**
|
|
* Detarmines entries fast field should update.
|
|
*/
|
|
export const entriesFieldShouldUpdate = (newProps, oldProps) => {
|
|
return (
|
|
newProps.items !== oldProps.items ||
|
|
defaultFastFieldShouldUpdate(newProps, oldProps)
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Detarmines accounts fast field should update.
|
|
*/
|
|
export const accountsFieldShouldUpdate = (newProps, oldProps) => {
|
|
return (
|
|
newProps.accounts !== oldProps.accounts ||
|
|
defaultFastFieldShouldUpdate(newProps, oldProps)
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Detarmines customers fast field should update.
|
|
*/
|
|
export const customersFieldShouldUpdate = (newProps, oldProps) => {
|
|
return (
|
|
newProps.customers !== oldProps.customers ||
|
|
defaultFastFieldShouldUpdate(newProps, oldProps)
|
|
);
|
|
};
|