import React, { useMemo } from 'react';
import {
FormGroup,
InputGroup,
Position,
Classes,
ControlGroup,
Button,
} from '@blueprintjs/core';
import { DateInput } from '@blueprintjs/datetime';
import { FastField, Field, useFormikContext, ErrorMessage } from 'formik';
import { FormattedMessage as T } from 'components';
import { toSafeInteger } from 'lodash';
import classNames from 'classnames';
import { CLASSES } from 'common/classes';
import styled from 'styled-components';
import {
FFormGroup,
AccountsSelectList,
VendorSelectField,
FieldRequiredHint,
InputPrependText,
Money,
Hint,
Icon,
VendorDrawerLink,
MoneyInputGroup,
} from 'components';
import withCurrentOrganization from 'containers/Organization/withCurrentOrganization';
import { usePaymentMadeFormContext } from './PaymentMadeFormProvider';
import { ACCOUNT_TYPE } from 'common/accountTypes';
import { PaymentMadeExchangeRateInputField } from './components';
import {
momentFormatter,
tansformDateValue,
handleDateChange,
inputIntent,
compose,
safeSumBy,
fullAmountPaymentEntries,
amountPaymentEntries,
} from 'utils';
import { accountsFieldShouldUpdate, vendorsFieldShouldUpdate } from './utils';
/**
* Payment made form header fields.
*/
function PaymentMadeFormHeaderFields({ organization: { base_currency } }) {
// Formik form context.
const {
values: { entries },
setFieldValue,
} = useFormikContext();
// Payment made form context.
const {
vendors,
accounts,
isNewMode,
setPaymentVendorId,
isForeignVendor,
baseCurrency,
selectVendor,
} = usePaymentMadeFormContext();
// Sumation of payable full-amount.
const payableFullAmount = useMemo(
() => safeSumBy(entries, 'due_amount'),
[entries],
);
// Handle receive full-amount click.
const handleReceiveFullAmountClick = () => {
const newEntries = fullAmountPaymentEntries(entries);
const fullAmount = safeSumBy(newEntries, 'payment_amount');
setFieldValue('entries', newEntries);
setFieldValue('full_amount', fullAmount);
};
// Handles the full-amount field blur.
const onFullAmountBlur = (value) => {
const newEntries = amountPaymentEntries(toSafeInteger(value), entries);
setFieldValue('entries', newEntries);
};
return (
{/* ------------ Vendor name ------------ */}
{({ form, field: { value }, meta: { error, touched } }) => (
}
inline={true}
className={classNames('form-group--select-list', Classes.FILL)}
labelInfo={}
>
}
onContactSelected={(contact) => {
form.setFieldValue('vendor_id', contact.id);
form.setFieldValue('currency_code', contact?.currency_code);
setPaymentVendorId(contact.id);
}}
disabled={!isNewMode}
popoverFill={true}
allowCreate={true}
/>
{value && (
)}
)}
{/* ----------- Exchange rate ----------- */}
{/* ------------ Payment date ------------ */}
{({ form, field: { value }, meta: { error, touched } }) => (
}
inline={true}
labelInfo={}
className={classNames('form-group--select-list', Classes.FILL)}
intent={inputIntent({ error, touched })}
helperText={}
>
{
form.setFieldValue('payment_date', formattedDate);
})}
popoverProps={{ position: Position.BOTTOM, minimal: true }}
inputProps={{
leftIcon: ,
}}
/>
)}
{/* ------------ Full amount ------------ */}
{({
form: {
values: { currency_code },
},
field: { value },
meta: { error, touched },
}) => (
}
inline={true}
className={('form-group--full-amount', Classes.FILL)}
intent={inputIntent({ error, touched })}
labelInfo={}
helperText={}
>
{
setFieldValue('full_amount', value);
}}
onBlurValue={onFullAmountBlur}
/>
)}
{/* ------------ Payment number ------------ */}
{({ form, field, meta: { error, touched } }) => (
}
inline={true}
className={('form-group--payment_number', Classes.FILL)}
intent={inputIntent({ error, touched })}
helperText={}
>
)}
{/* ------------ Payment account ------------ */}
{({ form, field: { value }, meta: { error, touched } }) => (
}
className={classNames(
'form-group--payment_account_id',
'form-group--select-list',
Classes.FILL,
)}
inline={true}
labelInfo={}
intent={inputIntent({ error, touched })}
helperText={}
>
}
onAccountSelected={(account) => {
form.setFieldValue('payment_account_id', account.id);
}}
defaultSelectText={}
selectedAccountId={value}
filterByTypes={[
ACCOUNT_TYPE.CASH,
ACCOUNT_TYPE.BANK,
ACCOUNT_TYPE.OTHER_CURRENT_ASSET,
]}
/>
)}
{/* ------------ Reference ------------ */}
{({ form, field, meta: { error, touched } }) => (
}
inline={true}
className={classNames('form-group--reference', Classes.FILL)}
intent={inputIntent({ error, touched })}
helperText={}
>
)}
);
}
export default compose(withCurrentOrganization())(PaymentMadeFormHeaderFields);
const ControlVendorGroup = styled(ControlGroup)`
display: flex;
align-items: center;
transform: none;
`;
const VendorButtonLink = styled(VendorDrawerLink)`
font-size: 11px;
margin-top: 6px;
`;