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 (