// @ts-nocheck
import React from 'react';
import classNames from 'classnames';
import { FormGroup, Position, Classes, ControlGroup } from '@blueprintjs/core';
import { FastField, ErrorMessage, useFormikContext } from 'formik';
import { Features } from '@/constants';
import {
FFormGroup,
FormattedMessage as T,
InputPrependText,
CurrencySelectList,
BranchSelect,
BranchSelectButton,
FeatureCan,
Row,
Col,
FMoneyInputGroup,
ExchangeRateInputGroup,
FDateInput,
} from '@/components';
import { useCustomerFormContext } from './CustomerFormProvider';
import {
openingBalanceFieldShouldUpdate,
useIsCustomerForeignCurrency,
useSetPrimaryBranchToForm,
} from './utils';
import { useCurrentOrganization } from '@/hooks/state';
/**
* Customer financial panel.
*/
export default function CustomerFinancialPanel() {
const { currencies, customerId, branches } = useCustomerFormContext();
// Sets the primary branch to form.
useSetPrimaryBranchToForm();
return (
{/*------------ Currency -----------*/}
{({ form, field: { value }, meta: { error, touched } }) => (
}
className={classNames(
'form-group--select-list',
'form-group--balance-currency',
Classes.FILL,
)}
inline={true}
>
{
form.setFieldValue('currency_code', currency.currency_code);
}}
disabled={customerId}
/>
)}
{/*------------ Opening balance -----------*/}
{/*------ Opening Balance Exchange Rate -----*/}
{/*------------ Opening balance at -----------*/}
{/*------------ Opening branch -----------*/}
}
name={'opening_balance_branch_id'}
inline={true}
className={classNames('form-group--select-list', Classes.FILL)}
>
);
}
/**
* Customer opening balance at date field.
* @returns {JSX.Element}
*/
function CustomerOpeningBalanceAtField() {
const { customerId } = useCustomerFormContext();
// Cannot continue if the customer id is defined.
if (customerId) return null;
return (
}
inline={true}
helperText={}
>
date.toLocaleDateString()}
parseDate={(str) => new Date(str)}
fill={true}
/>
);
}
/**
* Customer opening balance field.
* @returns {JSX.Element}
*/
function CustomerOpeningBalanceField() {
const { customerId } = useCustomerFormContext();
const { values } = useFormikContext();
// Cannot continue if the customer id is defined.
if (customerId) return null;
return (
}
name={'opening_balance'}
inline={true}
shouldUpdate={openingBalanceFieldShouldUpdate}
shouldUpdateDeps={{ currencyCode: values.currency_code }}
fastField={true}
>
);
}
/**
* Customer opening balance exchange rate field if the customer has foreign
* currency.
* @returns {JSX.Element}
*/
function CustomerOpeningBalanceExchangeRateField() {
const { values } = useFormikContext();
const { customerId } = useCustomerFormContext();
const currentOrganization = useCurrentOrganization();
const isForeignJouranl = useIsCustomerForeignCurrency();
// Can't continue if the customer is not foreign.
if (!isForeignJouranl || customerId) {
return null;
}
return (
);
}