// @ts-nocheck
import React, { useMemo } from 'react';
import classNames from 'classnames';
import styled from 'styled-components';
import {
InputGroup,
Position,
Classes,
ControlGroup,
Button,
} from '@blueprintjs/core';
import { isEmpty, toSafeInteger } from 'lodash';
import { useFormikContext } from 'formik';
import { css } from '@emotion/css';
import { Theme, useTheme } from '@emotion/react';
import {
FeatureCan,
CustomersSelect,
FormattedMessage as T,
FMoneyInputGroup,
Stack,
FDateInput,
} from '@/components';
import { safeSumBy } from '@/utils';
import {
FFormGroup,
AccountsSelect,
FieldRequiredHint,
Icon,
InputPrependText,
CustomerDrawerLink,
Hint,
Money,
} from '@/components';
import { usePaymentReceiveFormContext } from './PaymentReceiveFormProvider';
import { ACCOUNT_TYPE } from '@/constants/accountTypes';
import { ProjectsSelect } from '@/containers/Projects/components';
import {
PaymentReceiveExchangeRateInputField,
PaymentReceiveProjectSelectButton,
} from './components';
import {
amountPaymentEntries,
fullAmountPaymentEntries,
customersFieldShouldUpdate,
accountsFieldShouldUpdate,
} from './utils';
import { Features } from '@/constants';
import { PaymentReceivePaymentNoField } from './PaymentReceivePaymentNoField';
const getHeaderFieldsStyle = (theme: Theme) => css`
.${theme.bpPrefix}-form-group {
margin-bottom: 0;
&.${theme.bpPrefix}-inline {
max-width: 470px;
}
.${theme.bpPrefix}-label {
min-width: 160px;
}
.${theme.bpPrefix}-form-content {
width: 100%;
}
}
`;
/**
* Payment receive header fields.
*/
export default function PaymentReceiveHeaderFields() {
const theme = useTheme();
const styleClassName = getHeaderFieldsStyle(theme);
// Payment receive form context.
const { accounts, projects } = usePaymentReceiveFormContext();
// Formik form context.
const {
values: { entries, currency_code },
setFieldValue,
} = useFormikContext();
// 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('amount', fullAmount);
};
// Handles the full-amount field blur.
const onFullAmountBlur = (value) => {
const newEntries = amountPaymentEntries(toSafeInteger(value), entries);
setFieldValue('entries', newEntries);
};
return (
{/* ------------- Customer name ------------- */}
{/* ----------- Exchange rate ----------- */}
{/* ------------- Payment date ------------- */}
}
labelInfo={}
inline
fastField
>
date.toLocaleDateString()}
parseDate={(str) => new Date(str)}
popoverProps={{ position: Position.BOTTOM_LEFT, minimal: true }}
inputProps={{
leftIcon: ,
fill: true,
}}
fill
fastField
/>
{/* ------------ Full amount ------------ */}
}
inline={true}
labelInfo={}
fastField
>
{!isEmpty(entries) && (
)}
{/* ------------ Payment receive no. ------------ */}
{/* ------------ Deposit account ------------ */}
}
inline={true}
labelInfo={}
items={accounts}
shouldUpdate={accountsFieldShouldUpdate}
fastField={true}
>
}
placeholder={}
filterByTypes={[
ACCOUNT_TYPE.CASH,
ACCOUNT_TYPE.BANK,
ACCOUNT_TYPE.OTHER_CURRENT_ASSET,
]}
shouldUpdate={accountsFieldShouldUpdate}
fastField={true}
fill={true}
/>
{/* ------------ Reference No. ------------ */}
}
inline
fastField
>
{/*------------ Project name -----------*/}
}
inline={true}
className={classNames('form-group--select-list', Classes.FILL)}
>
);
}
const CustomerButtonLink = styled(CustomerDrawerLink)`
font-size: 11px;
margin-top: 6px;
`;
/**
* Customer select field of payment receive form.
* @returns {React.ReactNode}
*/
function PaymentReceiveCustomerSelect() {
// Payment receive form context.
const { customers, isNewMode } = usePaymentReceiveFormContext();
// Formik form context.
const { values, setFieldValue } = useFormikContext();
return (
}
inline={true}
labelInfo={}
name={'customer_id'}
fastField={true}
shouldUpdate={customersFieldShouldUpdate}
shouldUpdateDeps={{ items: customers }}
>
}
onItemChange={(customer) => {
setFieldValue('customer_id', customer.id);
setFieldValue('full_amount', '');
setFieldValue('currency_code', customer?.currency_code);
}}
popoverFill={true}
disabled={!isNewMode}
allowCreate={true}
fastField={true}
shouldUpdate={customersFieldShouldUpdate}
shouldUpdateDeps={{ items: customers }}
/>
{values.customer_id && (
)}
);
}