Files
boursecu_admin/src/pages/menuPages/copperConvert/copperConvertTable.ts
T
safavimohadeseh78andCursor 3df3164e0b feat(convert): show convert requests under hesab ketab
Admins review pending cathode-to-wire requests in the shared table, and on mobile they see cards.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-01 13:13:09 +03:30

171 lines
5.4 KiB
TypeScript

import { ref } from 'vue'
import { Modal, message } from 'ant-design-vue'
import type { Filter, TableInput, TableOutput } from '@core/type/baseTable'
import { toJalali } from '@core/utils/useDataTime'
import {
approveCopperConvertRequest,
getCopperConvertRequests,
rejectCopperConvertRequest,
type CopperConvertRequestRow
} from '@/api/copperConvert'
export type CopperConvertTableRow = CopperConvertRequestRow & {
source_label: string
status_label: string
created_label: string
}
const numericKeys = new Set(['id', 'wire15_rings', 'wire25_rings', 'consumed_amount', 'leftover_amount'])
export function sourceLabel(source: string) {
if (source === 'katod') return 'کاتد'
if (source === 'maftoul') return 'مفتول'
if (source === 'shamsh') return 'شمش'
return source
}
export function statusLabel(status: string) {
if (status === 'pending') return 'در انتظار'
if (status === 'approved') return 'تأیید شده'
if (status === 'rejected') return 'رد شده'
return status
}
export function statusColors(status: string) {
if (status === 'approved') return { text: '#25855A', bg: '#E3F4E9' }
if (status === 'rejected') return { text: '#C53030', bg: '#FECACA' }
return { text: '#B45309', bg: '#FEF3C7' }
}
function decorate(row: CopperConvertRequestRow): CopperConvertTableRow {
return {
...row,
source_label: sourceLabel(row.source),
status_label: statusLabel(row.status),
created_label: row.created_at ? toJalali(row.created_at) : '-'
}
}
function asText(value: unknown) {
return value == null ? '' : String(value).toLowerCase()
}
function matchFilter(row: Record<string, unknown>, filter: Filter) {
const raw = row[filter.field]
const op = filter.op
const value = filter.value
if (op === 'null') return raw == null || raw === ''
if (op === 'nn') return raw != null && raw !== ''
if (value === '' || value == null) return true
if (op === 'bet' && Array.isArray(value)) {
const n = Number(raw)
return n >= Number(value[0]) && n <= Number(value[1])
}
if (op === 'in' && Array.isArray(value)) {
return value.map(String).includes(String(raw ?? ''))
}
const text = asText(raw)
const needle = asText(value)
if (op === 'eq') return text === needle
if (op === 'not') return text !== needle
if (op === 'sw') return text.startsWith(needle)
if (op === 'ew') return text.endsWith(needle)
if (op === 'has' || op == null) return text.includes(needle)
const left = Number(raw)
const right = Number(value)
if (!Number.isFinite(left) || !Number.isFinite(right)) return false
if (op === 'gt') return left > right
if (op === 'lt') return left < right
if (op === 'gte') return left >= right
if (op === 'lte') return left <= right
return true
}
function queryRows(rows: CopperConvertTableRow[], params: TableInput): TableOutput<CopperConvertTableRow> {
const search = (params.search ?? '').trim().toLowerCase()
let list = rows.filter((row) => {
if (!search) return true
return [row.customer_name, row.customer_mobile, row.source_label, row.status_label, row.asset_unit, row.created_label]
.join(' ')
.toLowerCase()
.includes(search)
})
for (const filter of params.filters ?? []) {
if (!filter.field) continue
list = list.filter((row) => matchFilter(row as unknown as Record<string, unknown>, filter))
}
const sortKey = params.sort?.key
if (sortKey) {
const dir = params.sort?.order === 'desc' ? -1 : 1
list = [...list].sort((a, b) => {
const av = (a as Record<string, unknown>)[sortKey]
const bv = (b as Record<string, unknown>)[sortKey]
if (numericKeys.has(sortKey)) return (Number(av) - Number(bv)) * dir
return String(av ?? '').localeCompare(String(bv ?? ''), 'fa') * dir
})
}
const page = params.page || 1
const limit = params.limit || 10
return {
rows: list.slice((page - 1) * limit, page * limit),
query: {
...params,
page,
limit,
total: list.length,
search: params.search ?? null,
filters: params.filters ?? []
}
}
}
export async function fetchCopperConvertTable(params: TableInput) {
const data = await getCopperConvertRequests()
const rows = (Array.isArray(data) ? data : []).map(decorate)
return queryRows(rows, params)
}
export function useCopperConvertActions(reload: () => unknown) {
const busyId = ref(0)
function approve(row: CopperConvertRequestRow) {
Modal.confirm({
title: 'تأیید تبدیل',
content: 'دارایی مشتری فروخته و حلقه‌های سیم برای او خریداری شود؟',
okText: 'تأیید',
cancelText: 'انصراف',
async onOk() {
busyId.value = row.id
try {
await approveCopperConvertRequest(row.id)
message.success('تبدیل ثبت شد')
await reload()
} finally {
busyId.value = 0
}
}
})
}
function reject(row: CopperConvertRequestRow) {
Modal.confirm({
title: 'رد درخواست',
content: 'این درخواست تبدیل رد شود؟',
okText: 'رد',
cancelText: 'انصراف',
async onOk() {
busyId.value = row.id
try {
await rejectCopperConvertRequest(row.id)
message.success('درخواست رد شد')
await reload()
} finally {
busyId.value = 0
}
}
})
}
return { busyId, approve, reject }
}