import React, { useMemo } from 'react'; import { FormGroup, InputGroup, Position, ControlGroup, Button, } from '@blueprintjs/core'; import { DateInput } from '@blueprintjs/datetime'; import { FormattedMessage as T, If } from 'components'; import { FastField, Field, useFormikContext, ErrorMessage } from 'formik'; import { useAutofocus } from 'hooks'; import { CLASSES } from 'common/classes'; import classNames from 'classnames'; import styled from 'styled-components'; import { compose, safeSumBy, momentFormatter, tansformDateValue, handleDateChange, inputIntent, } from 'utils'; import { AccountsSelectList, CustomerSelectField, FieldRequiredHint, Icon, InputPrependButton, MoneyInputGroup, InputPrependText, CustomerDrawerLink, Hint, Money, } from 'components'; import { usePaymentReceiveFormContext } from './PaymentReceiveFormProvider'; import { ACCOUNT_TYPE } from 'common/accountTypes'; import { PaymentReceiveExchangeRateInputField } from './components'; import withDialogActions from 'containers/Dialog/withDialogActions'; import withSettings from 'containers/Settings/withSettings'; import withCurrentOrganization from 'containers/Organization/withCurrentOrganization'; import { useObservePaymentNoSettings, amountPaymentEntries, fullAmountPaymentEntries, customersFieldShouldUpdate, accountsFieldShouldUpdate, } from './utils'; import { toSafeInteger } from 'lodash'; /** * Payment receive header fields. */ function PaymentReceiveHeaderFields({ // #withCurrentOrganization organization: { base_currency }, // #withDialogActions openDialog, // #withSettings paymentReceiveAutoIncrement, paymentReceiveNumberPrefix, paymentReceiveNextNumber, }) { // Payment receive form context. const { customers, accounts, isNewMode } = usePaymentReceiveFormContext(); // Formik form context. const { values: { entries }, setFieldValue, } = useFormikContext(); const customerFieldRef = useAutofocus(); // Calculates the full-amount received. const totalDueAmount = useMemo( () => safeSumBy(entries, 'due_amount'), [entries], ); // Handle receive full-amount link 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); }; // Handle click open payment receive number dialog. const handleClickOpenDialog = () => { openDialog('payment-receive-number-form'); }; // Handle payment number field blur. const handlePaymentNoBlur = (form, field) => (event) => { const newValue = event.target.value; if (field.value !== newValue && paymentReceiveAutoIncrement) { openDialog('payment-receive-number-form', { initialFormValues: { manualTransactionNo: newValue, incrementMode: 'manual-transaction', }, }); } }; // Syncs payment receive number from settings to the form. useObservePaymentNoSettings( paymentReceiveNumberPrefix, paymentReceiveNextNumber, ); return (
{/* ------------- Customer name ------------- */} {({ form, field: { value }, meta: { error, touched } }) => ( } inline={true} className={classNames('form-group--select-list', CLASSES.FILL)} labelInfo={} intent={inputIntent({ error, touched })} helperText={} > } onContactSelected={(customer) => { form.setFieldValue('customer_id', customer.id); form.setFieldValue('full_amount', ''); form.setFieldValue('currency_code', customer?.currency_code); }} popoverFill={true} disabled={!isNewMode} buttonProps={{ elementRef: (ref) => (customerFieldRef.current = ref), }} 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: { setFieldValue, values: { currency_code }, }, field: { value, onChange }, 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 receive no. ------------ */} {({ form, field, meta: { error, touched } }) => ( } inline={true} labelInfo={} className={('form-group--payment_receive_no', CLASSES.FILL)} intent={inputIntent({ error, touched })} helperText={} > , }} tooltip={true} tooltipProps={{ content: ( ), position: Position.BOTTOM_LEFT, }} /> )} {/* ------------ Deposit account ------------ */} {({ form, field: { value }, meta: { error, touched } }) => ( } className={classNames( 'form-group--deposit_account_id', 'form-group--select-list', CLASSES.FILL, )} inline={true} labelInfo={} intent={inputIntent({ error, touched })} helperText={} > } onAccountSelected={(account) => { form.setFieldValue('deposit_account_id', account.id); }} defaultSelectText={} selectedAccountId={value} filterByTypes={[ ACCOUNT_TYPE.CASH, ACCOUNT_TYPE.BANK, ACCOUNT_TYPE.OTHER_CURRENT_ASSET, ]} /> )} {/* ------------ Reference No. ------------ */} {({ form, field, meta: { error, touched } }) => ( } inline={true} className={classNames('form-group--reference', CLASSES.FILL)} intent={inputIntent({ error, touched })} helperText={} > )}
); } export default compose( withSettings(({ paymentReceiveSettings }) => ({ paymentReceiveNextNumber: paymentReceiveSettings?.nextNumber, paymentReceiveNumberPrefix: paymentReceiveSettings?.numberPrefix, paymentReceiveAutoIncrement: paymentReceiveSettings?.autoIncrement, })), withDialogActions, withCurrentOrganization(), )(PaymentReceiveHeaderFields); const CustomerButtonLink = styled(CustomerDrawerLink)` font-size: 11px; margin-top: 6px; `;