2025-08-07 23:48:31 +07:00
|
|
|
import { useQuery } from '@tanstack/react-query'
|
2025-08-08 17:59:39 +07:00
|
|
|
import { DashboardReport, PaymentReport, ProductSalesReport, ProfitLossReport, SalesReport } from '../../types/services/analytic'
|
2025-08-07 23:48:31 +07:00
|
|
|
import { api } from '../api'
|
|
|
|
|
import { formatDateDDMMYYYY } from '../../utils/transform'
|
|
|
|
|
|
|
|
|
|
interface AnalyticQueryParams {
|
|
|
|
|
date_from?: string
|
|
|
|
|
date_to?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useSalesAnalytics(params: AnalyticQueryParams = {}) {
|
|
|
|
|
const today = new Date()
|
2025-08-08 17:59:39 +07:00
|
|
|
const monthAgo = new Date()
|
|
|
|
|
monthAgo.setDate(today.getDate() - 30)
|
2025-08-07 23:48:31 +07:00
|
|
|
|
|
|
|
|
const defaultDateTo = formatDateDDMMYYYY(today)
|
2025-08-08 17:59:39 +07:00
|
|
|
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
2025-08-07 23:48:31 +07:00
|
|
|
|
|
|
|
|
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
|
|
|
|
|
|
|
|
|
return useQuery<SalesReport>({
|
|
|
|
|
queryKey: ['analytics-sales', { date_from, date_to, ...filters }],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const queryParams = new URLSearchParams()
|
|
|
|
|
|
|
|
|
|
queryParams.append('date_from', date_from)
|
|
|
|
|
queryParams.append('date_to', date_to)
|
|
|
|
|
|
|
|
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
|
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
|
|
|
queryParams.append(key, value.toString())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await api.get(`/analytics/sales?${queryParams.toString()}`)
|
|
|
|
|
return res.data.data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useProductSalesAnalytics(params: AnalyticQueryParams = {}) {
|
|
|
|
|
const today = new Date()
|
2025-08-08 17:59:39 +07:00
|
|
|
const monthAgo = new Date()
|
|
|
|
|
monthAgo.setDate(today.getDate() - 30)
|
2025-08-07 23:48:31 +07:00
|
|
|
|
|
|
|
|
const defaultDateTo = formatDateDDMMYYYY(today)
|
2025-08-08 17:59:39 +07:00
|
|
|
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
2025-08-07 23:48:31 +07:00
|
|
|
|
|
|
|
|
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
|
|
|
|
|
|
|
|
|
return useQuery<ProductSalesReport>({
|
|
|
|
|
queryKey: ['analytics-products', { date_from, date_to, ...filters }],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const queryParams = new URLSearchParams()
|
|
|
|
|
|
|
|
|
|
queryParams.append('date_from', date_from)
|
|
|
|
|
queryParams.append('date_to', date_to)
|
|
|
|
|
|
|
|
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
|
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
|
|
|
queryParams.append(key, value.toString())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await api.get(`/analytics/products?${queryParams.toString()}`)
|
|
|
|
|
return res.data.data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-08-08 17:59:39 +07:00
|
|
|
|
|
|
|
|
export function usePaymentAnalytics(params: AnalyticQueryParams = {}) {
|
|
|
|
|
const today = new Date()
|
|
|
|
|
const monthAgo = new Date()
|
|
|
|
|
monthAgo.setDate(today.getDate() - 30)
|
|
|
|
|
|
|
|
|
|
const defaultDateTo = formatDateDDMMYYYY(today)
|
|
|
|
|
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
|
|
|
|
|
|
|
|
|
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
|
|
|
|
|
|
|
|
|
return useQuery<PaymentReport>({
|
|
|
|
|
queryKey: ['analytics-payment-methods', { date_from, date_to, ...filters }],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const queryParams = new URLSearchParams()
|
|
|
|
|
|
|
|
|
|
queryParams.append('date_from', date_from)
|
|
|
|
|
queryParams.append('date_to', date_to)
|
|
|
|
|
|
|
|
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
|
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
|
|
|
queryParams.append(key, value.toString())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await api.get(`/analytics/payment-methods?${queryParams.toString()}`)
|
|
|
|
|
return res.data.data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDashboardAnalytics(params: AnalyticQueryParams = {}) {
|
|
|
|
|
const today = new Date()
|
|
|
|
|
const monthAgo = new Date()
|
|
|
|
|
monthAgo.setDate(today.getDate() - 30)
|
|
|
|
|
|
|
|
|
|
const defaultDateTo = formatDateDDMMYYYY(today)
|
|
|
|
|
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
|
|
|
|
|
|
|
|
|
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
|
|
|
|
|
|
|
|
|
return useQuery<DashboardReport>({
|
|
|
|
|
queryKey: ['analytics-dashboard', { date_from, date_to, ...filters }],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const queryParams = new URLSearchParams()
|
|
|
|
|
|
|
|
|
|
queryParams.append('date_from', date_from)
|
|
|
|
|
queryParams.append('date_to', date_to)
|
|
|
|
|
|
|
|
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
|
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
|
|
|
queryParams.append(key, value.toString())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await api.get(`/analytics/dashboard?${queryParams.toString()}`)
|
|
|
|
|
return res.data.data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useProfitLossAnalytics(params: AnalyticQueryParams = {}) {
|
|
|
|
|
const today = new Date()
|
|
|
|
|
const monthAgo = new Date()
|
|
|
|
|
monthAgo.setDate(today.getDate() - 30)
|
|
|
|
|
|
|
|
|
|
const defaultDateTo = formatDateDDMMYYYY(today)
|
|
|
|
|
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
|
|
|
|
|
|
|
|
|
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
|
|
|
|
|
|
|
|
|
return useQuery<ProfitLossReport>({
|
|
|
|
|
queryKey: ['analytics-profit-loss', { date_from, date_to, ...filters }],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const queryParams = new URLSearchParams()
|
|
|
|
|
|
|
|
|
|
queryParams.append('date_from', date_from)
|
|
|
|
|
queryParams.append('date_to', date_to)
|
|
|
|
|
|
|
|
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
|
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
|
|
|
queryParams.append(key, value.toString())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const res = await api.get(`/analytics/profit-loss?${queryParams.toString()}`)
|
|
|
|
|
return res.data.data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|