diff --git a/src/pages/receipts/Detail.tsx b/src/pages/receipts/Detail.tsx index 2422231..9b38620 100644 --- a/src/pages/receipts/Detail.tsx +++ b/src/pages/receipts/Detail.tsx @@ -1,562 +1,258 @@ -import { ChangeEvent, FC, Fragment, useState, useEffect } from 'react' +import { FC } from 'react' import { useTranslation } from 'react-i18next' -import Button from '../../components/Button' -import { Add, TickCircle, TickSquare, Trash } from 'iconsax-react' -import Select from '../../components/Select' -import Input from '../../components/Input' -import { CreateReceiptType, ReceiptCreateItemsType } from './types/ReceiptTypes' -import { useFormik } from 'formik' -import * as Yup from 'yup' -import { toast } from 'react-toastify' -import { useGetCustomers } from '../customer/hooks/useCustomerData' -import { CustomerItemType } from '../customer/types/CustomerTypes' -import PageLoading from '../../components/PageLoading' -import { useGetInvoiceById, useUpdateInvoice } from './hooks/useReceiptData' -import { useNavigate, useParams } from 'react-router-dom' -import { Pages } from '../../config/Pages' -import { ErrorType } from '../../helpers/types' -import { RecurringPeriodEnum } from './enum/ReceipEnum' -import SwitchComponent from '../../components/Switch' +import { Edit } from 'iconsax-react' +import StatusCircle from '../../components/StatusCircle' +import SamanImage from '../../assets/images/saman.png' -// Add interface for invoice item from API -interface InvoiceItemType { - id: string; - name: string; - count: number; - unitPrice: number; - discount?: number; - totalPrice: number; - createdAt: string; - updatedAt: string; - subscriptionPlan: null | string; -} +const ReceiptsDetail: FC = () => { -const CreateReceipt: FC = () => { - - const navigate = useNavigate() - const { id } = useParams() const { t } = useTranslation('global') - const { data: invoice, isPending: isLoading } = useGetInvoiceById(id as string) - const updateInvoice = useUpdateInvoice() - const [items, setItems] = useState([]) - const [customer, setCustomer] = useState() - const [isRecurring, setIsRecurring] = useState(false) - const [type, setType] = useState(RecurringPeriodEnum.WEEKLY) - const [maxRecurringCycles, setMaxRecurringCycles] = useState(0) - const getCustomers = useGetCustomers(1, true) - - const formik = useFormik({ - initialValues: { - name: '', - count: '', - unitPrice: '', - discount: '' - }, - validationSchema: Yup.object({ - name: Yup.string().required(t('errors.required')), - count: Yup.string().required(t('errors.required')), - unitPrice: Yup.string().required(t('errors.required')), - }), - onSubmit: (values) => { - setItems((prev) => [...prev, { - count: +values.count, - discount: +values.discount, - name: values.name, - unitPrice: +values.unitPrice - }]) - formik.resetForm() - } - }) - - const handleChangeCustomer = (e: ChangeEvent) => { - const value = e.target.value - const customer = getCustomers.data?.data?.customers?.find((item: CustomerItemType) => item.id === value) - setCustomer(customer) - } - - const handleSubmit = () => { - if (customer) { - const params: CreateReceiptType = { - userId: customer?.id, - items: items, - isRecurring: isRecurring, - recurringPeriod: isRecurring ? +type : undefined, - maxRecurringCycles: isRecurring ? maxRecurringCycles : undefined - } - - updateInvoice.mutate({ id: id as string, params }, { - onSuccess: () => { - toast.success(t('success')) - navigate(Pages.receipts.index) - }, - onError(error: ErrorType) { - toast.error(error.response?.data?.error.message[0]) - }, - }) - } - } - - const handleRemove = (index: number) => { - setItems((prev) => prev.filter((_, i) => i !== index)) - } - - // Prefill form when editing an invoice - useEffect(() => { - - // Check if we need to access invoice.data or directly invoice - const invoiceData = invoice?.data?.invoice || invoice; - - if (id && invoiceData) { - console.log("Invoice data to use:", invoiceData); - - // 1. If there's a user, find matching customer - if (invoiceData.user && getCustomers.data?.data?.customers) { - const foundCustomer = getCustomers.data.data.customers.find( - (item: CustomerItemType) => item.id === invoiceData.user.id - ); - setCustomer(foundCustomer); - console.log("Found customer:", foundCustomer); - setCustomer(foundCustomer); - } - - // 2. Set items from invoice - directly use the structure from API response - if (invoiceData.items && invoiceData.items.length > 0) { - console.log("Setting items:", invoiceData.items); - // Map API items to the format expected by our component - setItems(invoiceData.items.map((item: InvoiceItemType) => ({ - name: item.name, - count: item.count, - unitPrice: item.unitPrice, - discount: item.discount || 0 - }))); - - // 3. Set formik values from first item - - } - - // 4. Set recurring settings - setIsRecurring(Boolean(invoiceData.isRecurring)); - if (typeof invoiceData.recurringPeriod === 'number') { - setType(invoiceData.recurringPeriod.toString()); - } - setMaxRecurringCycles(invoiceData.maxRecurringCycles || 0); - } - }, [id, invoice, getCustomers.data]); - - // Add this function to programmatically select the customer in the dropdown - const selectCustomerInDropdown = () => { - if (customer && invoice?.user?.id) { - // Trigger the onChange manually with a synthetic event - const event = { - target: { - value: invoice.user.id - } - } as ChangeEvent; - - handleChangeCustomer(event); - } - }; - - // Call this when customer or invoice changes - useEffect(() => { - if (customer && invoice?.user?.id) { - selectCustomerInDropdown(); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [customer, invoice]); return (
- { - getCustomers.isPending || isLoading ? - - : - +
+ {t('receip.detail_account')} + ۱۲۳۱۲ +
+ +
+
+
- {t('receip.add_receip')} + {t('receip.personal_information')} +
+
+ +
+ {t('edit')} +
+
+
+ +
+
+
+ {t('receip.type_person')} +
+
+ {t('receip.company')} +
+
+ +
+
+ {t('receip.representativeـname')} +
+
+ مهرداد مظفری +
+
+ +
+
+ {t('receip.company_name')} +
+
+ داناک +
+
+
+
+ {t('receip.registration_number')} +
+
+ ۱۲۳۴ +
+
+
+ +
+
+
+ {t('receip.tel_company')} +
+
+ ۰۰۱۲۳۴۵۶۷۸ +
+
+ +
+
+ {t('receip.national_code')} +
+
+ ۰۰۱۲۳۴۵۶۷۸ +
+
+ +
+
+ {t('receip.postal_code')} +
+
+ ۳۸۲۱۳۵۸۸۹۲ +
+
+ +
+ +
+
+
+ {t('receip.state')} +
+
+ مرکزی +
+
+
+
+ {t('receip.state')} +
+
+ مرکزی +
+
+
+
+ {t('receip.city')} +
+
+ اراک +
+
+
+
+ {t('receip.address')} +
+
+ اراک -خیابان شریعتی-خیابان جنت +
+
+
+ +
+ +
+
+
+ {t('receip.detail_receip')} +
+
+ +
+ {t('receip.not_paid')} +
+
+
+ +
+
+
+ {t('receip.description')} +
+
+ {t('receip.price')} +
+
+
+
+ اکانت یکساله با دسترسی +
+
+ ۱۰,۰۰۰,۰۰۰ ریال +
+
+
+
+ ۱۰٪ ارزش افزوده +
+
+ ۱۰,۰۰۰,۰۰۰ ریال +
+
+
+
+ جمع کل +
+
+ ۱۰,۰۰۰,۰۰۰ ریال +
+
+
+
+
+ +
+
+
+ {t('receip.receip_information')} +
+
+ +
پرداخت نشده
+
+
+ +
+
+
+ {t('receip.date_factor')}
- + ۱۴۰۲/۱۰/۰۴
- -
-
-
- {t('receip.customer_information')} -
- -
-
- setType(e.target.value as unknown as RecurringPeriodEnum)} - /> - - setMaxRecurringCycles(+e.target.value)} - /> -
-
- } - -
-
{t('receip.receipt_information')}
-
- -
-
-
- {t('receip.number')} -
-
- {items.length + 1} -
-
- -
-
- {t('receip.product_name')} -
- -
- -
-
- {t('receip.count')} -
- -
- -
-
- {t('receip.unit_amount')} -
- formik.setFieldValue('unitPrice', e.target.value)} - seprator - /> -
- -
-
- {t('receip.discount')} -
- -
- -
-
- {t('receip.total_amount')} -
- -
- -
{ - if (formik.errors.name || formik.errors.count || formik.errors.unitPrice || formik.errors.discount) { - toast.error(t('receip.error_empty')) - } else { - formik.handleSubmit() - } - }} className='size-10 border border-border rounded-xl flex justify-center items-center'> - -
- - -
- - { - items.map((item, index: number) => { - return ( -
-
- { - index === 0 && -
- {t('receip.number')} -
- } -
- {index + 1} -
-
-
- { - index === 0 && -
- {t('receip.product_name')} -
- } - -
- -
- { - index === 0 && -
- {t('receip.count')} -
- } - -
- -
- { - index === 0 && -
- {t('receip.unit_amount')} -
- } - -
- -
- { - index === 0 && -
- {t('receip.discount')} -
- } - -
- -
- { - index === 0 && -
- {t('receip.total_amount')} -
- } - -
- -
handleRemove(index)} className='size-10 border border-border rounded-xl flex justify-center items-center'> - -
-
- ) - }) - } - - - -
-
-
- {t('receip.tax')} -
-
- {(items.reduce((acc, item) => acc + (Number(item.unitPrice) || 0) * (Number(item.count) || 0) - (Number(item.unitPrice) || 0) * (Number(item.count) || 0) * (Number(item.discount) / 100 || 0), 0) * 0.10).toFixed(1)} -
-
-
-
- {t('receip.total')} -
-
- {( - items.reduce((acc, item) => acc + (Number(item.unitPrice) || 0) * (Number(item.count) || 0) - (Number(item.unitPrice) || 0) * (Number(item.count) || 0) * (Number(item.discount) / 100 || 0), 0) * 1.10 - ).toFixed(1)} -
-
-
- +
+
+ {t('receip.due_date')}
- -
-
- {t('ticket.title_hint')} -
- -
-
-
- -
-
- سوالات - مشکلاتی که به یک موضوع مربوط میشوند را در یک درخواست پشتیبانی پیگیر باشید و چند درخواست برای یک موضوع باز نکنید. -
-
-
-
- -
-
- لطفاً برای بررسی و رفع مشکلات احتمالی صبور باشید بررسی و رفع مشکلات در برخی موارد زمان گیر است. -
-
-
-
- -
-
- پاسخگویی 24 ساعته تلفنی را تنها از میهن وب هاست می توانید انتظار داشته باشید .بخش پشتیبانی در هر ساعتی حتی در روز های تعطیل آماده پیگیری سریع مشکلات کاربران است. -
-
-
+
+ ۱۴۰۲/۱۰/۰۴
- - } +
+
+ {t('receip.factor_number')} +
+
+ ۱۲۳۱ +
+
+
+
+ {t('receip.status_factor')} +
+
+ تایید شده +
+
+
+ +
+
+ saman +
+ بانک سامان +
+
+
+ saman +
+ بانک سامان +
+
+
+
+
+
) } -export default CreateReceipt +export default ReceiptsDetail \ No newline at end of file