diff --git a/.env b/.env index d5340ee..2948e68 100644 --- a/.env +++ b/.env @@ -6,7 +6,7 @@ VITE_BASE_URL = 'https://api-dzone.danakcorp.com' VITE_DANAK_BASE_URL ='https://api.danakcorp.com' VITE_SERVICE_ID = '7e3c2f08-b7e7-4402-ae5f-fea99cff56bd' VITE_WORKSPACE_ID = 'workspace_id' -# VITE_LOGIN_URL = 'https://console.danakcorp.com/auth/login' -VITE_LOGIN_URL = 'http://localhost:5174/auth/login' +VITE_LOGIN_URL = 'https://console.danakcorp.com/auth/login' +# VITE_LOGIN_URL = 'http://localhost:5174/auth/login' VITE_DZONE_URL = 'https://dzone.danakcorp.com' diff --git a/src/pages/receipts/Create.tsx b/src/pages/receipts/Create.tsx index 5830a0b..aa3b629 100644 --- a/src/pages/receipts/Create.tsx +++ b/src/pages/receipts/Create.tsx @@ -8,26 +8,25 @@ 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 { useCreateInvoice } from './hooks/useReceiptData' import { useNavigate } from 'react-router-dom' import { Pages } from '../../config/Pages' import { ErrorType } from '../../helpers/types' -import { ReceiptTypeEnum } from './enum/ReceipEnum' +import { RecurringPeriodEnum } from './enum/ReceipEnum' import SwitchComponent from '../../components/Switch' -import { useGetCompanies } from '../company/hooks/useCompanyData' -import { CompanyItemType } from '../company/types/CompanyTypes' -import moment from 'moment-jalaali' const CreateReceipt: FC = () => { const navigate = useNavigate() const { t } = useTranslation('global') const [items, setItems] = useState([]) - const [customer, setCustomer] = useState() + const [customer, setCustomer] = useState() const [isRecurring, setIsRecurring] = useState(false) - const [type, setType] = useState(ReceiptTypeEnum.DAILY) + const [type, setType] = useState(RecurringPeriodEnum.WEEKLY) const [maxRecurringCycles, setMaxRecurringCycles] = useState(0) - const getCustomers = useGetCompanies(1) + const getCustomers = useGetCustomers(1, true) const createInvoice = useCreateInvoice() const formik = useFormik({ @@ -55,14 +54,14 @@ const CreateReceipt: FC = () => { const handleChangeCustomer = (e: ChangeEvent) => { const value = e.target.value - const customer = getCustomers.data?.data?.companies?.find((item: CompanyItemType) => item.id === value) + const customer = getCustomers.data?.data?.customers?.find((item: CustomerItemType) => item.id === value) setCustomer(customer) } const handleSubmit = () => { if (customer) { const params: CreateReceiptType = { - companyId: customer?.id, + userId: customer?.id, items: items, isRecurring: isRecurring, recurringPeriod: isRecurring ? +type : undefined, @@ -115,18 +114,18 @@ const CreateReceipt: FC = () => {
- {/*
+
{t('receip.customer_information')} -
*/} +
{ - return { - label: t(`receip.${item}`), - value: item - } - })} - onChange={(e) => setType(e.target.value as ReceiptTypeEnum)} + items={[ + { label: t('receip.WEEKLY'), value: RecurringPeriodEnum.WEEKLY.toString() }, + { label: t('receip.MONTHLY'), value: RecurringPeriodEnum.MONTHLY.toString() }, + { label: t('receip.QUARTERLY'), value: RecurringPeriodEnum.QUARTERLY.toString() }, + { label: t('receip.SEMIANNUALLY'), value: RecurringPeriodEnum.SEMIANNUALLY.toString() }, + { label: t('receip.ANNUALLY'), value: RecurringPeriodEnum.ANNUALLY.toString() } + ]} + onChange={(e) => setType(e.target.value as unknown as RecurringPeriodEnum)} /> { +// 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 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 (
-
- {t('receip.detail_account')} - ۱۲۳۱۲ -
- -
-
-
+ { + getCustomers.isPending || isLoading ? + + : +
- {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.add_receip')}
- ۱۴۰۲/۱۰/۰۴ +
-
-
- {t('receip.due_date')} -
-
- ۱۴۰۲/۱۰/۰۴ -
-
-
-
- {t('receip.factor_number')} -
-
- ۱۲۳۱ -
-
-
-
- {t('receip.status_factor')} -
-
- تایید شده -
-
-
-
-
- saman -
- بانک سامان -
-
-
- saman -
- بانک سامان -
-
-
-
-
+
+
+
+ {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('ticket.title_hint')} +
+ +
+
+
+ +
+
+ سوالات - مشکلاتی که به یک موضوع مربوط میشوند را در یک درخواست پشتیبانی پیگیر باشید و چند درخواست برای یک موضوع باز نکنید. +
+
+
+
+ +
+
+ لطفاً برای بررسی و رفع مشکلات احتمالی صبور باشید بررسی و رفع مشکلات در برخی موارد زمان گیر است. +
+
+
+
+ +
+
+ پاسخگویی 24 ساعته تلفنی را تنها از میهن وب هاست می توانید انتظار داشته باشید .بخش پشتیبانی در هر ساعتی حتی در روز های تعطیل آماده پیگیری سریع مشکلات کاربران است. +
+
+
+
+
+ + }
) } -export default ReceiptsDetail \ No newline at end of file +export default CreateReceipt diff --git a/src/pages/receipts/List.tsx b/src/pages/receipts/List.tsx index 78f50c6..ab446a0 100644 --- a/src/pages/receipts/List.tsx +++ b/src/pages/receipts/List.tsx @@ -1,20 +1,19 @@ import { FC, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Tabs from '../../components/Tabs' -import { FolderOpen, WalletCheck, WalletRemove, WalletSearch, WalletMinus } from 'iconsax-react' +import { Edit, FolderOpen, WalletCheck, WalletMinus, WalletRemove, WalletSearch } from 'iconsax-react' import Td from '../../components/Td' import { useGetInvoices } from './hooks/useReceiptData' import PageLoading from '../../components/PageLoading' -import { InvoiceStatus, ReceiptItemType } from './types/ReceiptTypes' +import { InvoiceStatus, InvoiceUserItemType, ReceiptItemType } from './types/ReceiptTypes' import { NumberFormat } from '../../config/func' import Select from '../../components/Select' import Input from '../../components/Input' import DatePickerComponent from '../../components/DatePicker' -import { useSearchParams } from 'react-router-dom' +import { Link, useSearchParams } from 'react-router-dom' import moment from 'moment-jalaali' -import { CompanyItemType } from '../company/types/CompanyTypes' import Pagination from '../../components/Pagination' - +import { Pages } from '../../config/Pages' const ReceiptsList: FC = () => { @@ -26,7 +25,7 @@ const ReceiptsList: FC = () => { const [search, setSearch] = useState('') const [date, setDate] = useState('') const [endDate, setEndDate] = useState('') - const getInvoices = useGetInvoices(activeTab, customerId, search, date, endDate) + const getInvoices = useGetInvoices(activeTab, customerId, search, date, endDate, page) useEffect(() => { const urlCustomerId = searchParams.get('user') @@ -46,11 +45,11 @@ const ReceiptsList: FC = () => {