1405/02/14-add giftCards

This commit is contained in:
2026-05-04 16:22:10 +03:30
parent 4f1b5f5e85
commit 9c5d153fc0
6 changed files with 878 additions and 0 deletions
+412
View File
@@ -0,0 +1,412 @@
<template>
<div>
<BasicMainCard>
<div class="flex w-full h-full">
<tbl
ref="table"
class="w-full h-full"
@info="info"
@edit="edit"
@delete="deleteItem"
@add="add"
@giftcardcodes="showGiftCardCodes"
/>
<!-- <BasicMenu :menu="menu" width="13rem" @onChange="tabSelected" />
<component :is="selectedComponent" class="mr-5" />-->
<!-- <BasicShowContext title="پروژه ها"> <Search /> </BasicShowContext>-->
</div>
</BasicMainCard>
<BasicModal
v-model:visible="addGiftCardVisible"
width="30rem"
:title="`${editId ? 'ویرایش' : 'افزودن'} کارت هدیه`"
@ok="ok"
:loading="loading"
@close="addGiftCardVisible = false"
>
<a-form :model="form" ref="formRef" autocomplete="off">
<div class="grid md:grid-cols-1 gap-4">
<a-form-item label="شماره موبایل مشتری" name="mobile" :rules="inputLenRules(11)">
<BasicInput
v-model:value="form.mobile"
type="tel"
placeholder="09123456789"
/>
</a-form-item>
<a-form-item label="نام مشتری" name="name" :rules="inputRules()">
<BasicInput
v-model:value="form.name"
placeholder="نام و نام خانوادگی مشتری"
/>
</a-form-item>
<a-form-item label="نوع کارت هدیه" name="unit" :rules="inputRules()">
<a-radio-group
v-model:value="form.unit"
button-style="solid"
>
<a-radio-button value="1">ریالی</a-radio-button>
<a-radio-button value="2">طلایی</a-radio-button>
</a-radio-group>
</a-form-item>
<a-form-item label="تعداد کدها" name="count" :rules="inputRulesNumber()">
<BasicInput
v-model:value="form.count"
type="tel"
placeholder="تعداد کدهای کارت هدیه"
/>
</a-form-item>
<a-form-item
:label="`مقدار ${form.unit === '2' ? '(گرم)' : '(ریال)'}`"
name="amount"
:rules="inputRulesNumber()"
>
<BasicInput
v-model:value="form.amount"
:addon-before="form.unit === '2' ? 'گرم' : 'ریال'"
type="tel"
placeholder="مقدار کارت هدیه"
/>
</a-form-item>
<a-form-item label="تاریخ انقضا" name="expiry_date" :rules="inputRules()">
<BaseDatePicker v-model:value="form.expiry_date" format="YYYY-MM-DD" />
</a-form-item>
<a-form-item label="توضیحات" name="description">
<BasicTextArea
v-model:value="form.description"
place-holder="توضیحات اختیاری"
/>
</a-form-item>
</div>
</a-form>
</BasicModal>
<BasicInfoModal
v-model:visible="userInfoVisible"
@close="userInfoVisible = false"
:title="infoParams.title"
:date="infoParams.date"
:time="infoParams.time"
:type="infoParams.type"
:name="infoParams.name"
:items="infoParams.items"
/>
<BasicModal
v-model:visible="giftCardCodesVisible"
width="80%"
title="لیست کدهای کارت هدیه"
custom-footer
@close="giftCardCodesVisible = false"
>
<template #footer>
<a-button @click="giftCardCodesVisible = false">بستن</a-button>
</template>
<a-table
:columns="giftCardCodesColumns"
:data-source="selectedGiftCardCodes"
:pagination="{ pageSize: 10 }"
row-key="id"
>
<template #bodyCell="{ column, record, index }">
<template v-if="column.key === 'index'">
{{ index + 1 }}
</template>
<template v-else-if="column.key === 'sts'">
<CustomTag
v-if="record.sts === 1"
textColor="#ca8a04"
bgColor="#fef3c7"
>
فعال
</CustomTag>
<CustomTag
v-else-if="record.sts === 2"
textColor="#25855A"
bgColor="#E3F4E9"
>
استفاده شده
</CustomTag>
<CustomTag
v-else
textColor="#C53030"
bgColor="#FECACA"
>
غیرفعال
</CustomTag>
</template>
<template v-else-if="column.key === 'unit'">
<CustomTag
v-if="record.unit === 2"
textColor="#C53030"
bgColor="#FECACA"
>
طلایی
</CustomTag>
<CustomTag
v-else
textColor="#25855A"
bgColor="#E3F4E9"
>
ریالی
</CustomTag>
</template>
<template v-else-if="column.key === 'amount'">
{{ record.amount }} {{ record.unit === 2 ? 'گرم' : 'ریال' }}
</template>
<template v-else-if="column.key === 'expiry_date'">
{{ record.expiry_date ? toJalali(record.expiry_date) : '-' }}
</template>
<template v-else-if="column.key === 'used_at'">
{{ record.used_at ? toJalali(record.used_at) : '-' }}
</template>
<template v-else-if="column.key === 'assigned_to_name'">
{{ record.assigned_to?.name ?? '-' }}
</template>
<template v-else-if="column.key === 'assigned_to_mobile'">
{{ record.assigned_to?.mobile ?? '-' }}
</template>
</template>
</a-table>
</BasicModal>
</div>
</template>
<script setup lang="ts">
// import BasicInfoModal from '@/components/BasicInfoModal'
import { inputLenRules, inputRules, inputRulesNumber } from '@/utils/inputRules'
import { useStaticApiListsStore } from '@/store/staticApisStore'
import { ref } from 'vue'
import tbl from './components/table.vue'
import type { FormInstance } from 'ant-design-vue'
import BasicInfoModal from '@/components/BasicInfoModal/index.vue'
import { message } from 'ant-design-vue/es'
import type { Info } from '@/components/BasicInfoModal/model/Info'
import { toJalaliNoTime, toTime } from '@core/utils/useDataTime'
import { confirmDeleteItem } from '@/utils/confirmModal'
import { cloneDeep } from 'lodash'
import type { Item } from '@/model/Item'
import type { Customer } from '@/model/customer'
import { createCustomer, deleteCustomer, getCustomersList, updateCustomer } from '@/api/customer'
import { usePrice } from '@core/utils/usePrice'
import type { Giftcard, Giftcardcode, GiftcardInput } from '@/model/giftCard'
import CustomTag from '@/components/CustomTag.vue'
import { toJalali } from '@core/utils/useDataTime'
import { createGiftCard } from '@/api/giftCard'
import dayjs from 'dayjs'
//loading
const loading = ref<boolean>(false)
//define store
const store = useStaticApiListsStore()
//define tbl
const table = ref(null)
const refreshTable = () => table?.value?.fetch()
//form
const form = ref<GiftcardInput>({
mobile: '',
name: '',
count: undefined,
amount: undefined,
unit: '1',
expiry_date: '',
description: ''
})
const formRef = ref<FormInstance>()
//info
let infoParams = reactive<Info>({})
const giftCardCodesVisible = ref<boolean>(false)
const selectedGiftCardCodes = ref<Giftcardcode[]>([])
const giftCardCodesColumns = [
{
title: 'ردیف',
key: 'index',
dataIndex: 'index'
},
{
title: 'کد',
dataIndex: 'code',
key: 'code'
},
{
title: 'نوع',
dataIndex: 'unit',
key: 'unit'
},
{
title: 'مقدار',
dataIndex: 'amount',
key: 'amount'
},
{
title: 'وضعیت',
dataIndex: 'sts',
key: 'sts'
},
{
title: 'تاریخ انقضا',
dataIndex: 'expiry_date',
key: 'expiry_date'
},
{
title: 'تاریخ استفاده',
dataIndex: 'used_at',
key: 'used_at'
},
{
title: 'نام کاربر اختصاص داده شده',
dataIndex: 'assigned_to.name',
key: 'assigned_to_name'
},
{
title: 'شماره موبایل کاربر اختصاص داده شده',
dataIndex: 'assigned_to.mobile',
key: 'assigned_to_mobile'
}
]
function info(val: Giftcard) {
// نمایش اطلاعات اصلی در BasicInfoModal
infoParams.title = 'اطلاعات کارت هدیه'
infoParams.name = val.customer?.name ?? '-'
infoParams.type = val.customer?.mobile ?? '-'
infoParams.date = toJalaliNoTime(val.created_at)
infoParams.time = toTime(val.created_at)
infoParams.itemsTitle = 'اطلاعات کارت هدیه'
infoParams.items = [
{
key: 'کد',
value: val.id ?? '-'
},
{
key: 'نوع کارت هدیه',
value: val.unit === 2 ? 'طلایی' : 'ریالی'
},
{
key: 'تعداد کدها',
value: val.count ?? '-'
},
{
key: 'مقدار (گرم)',
value: val.amount ?? '-'
},
{
key: 'مبلغ کل (ریال)',
value: usePrice(val.total ?? 0)
},
{
key: 'کد یکتا',
value: val.unique_code ?? '-'
},
{
key: 'تعداد استفاده شده',
value: val.used_count ?? '-'
},
{
key: 'وضعیت',
value: val.sts_str ?? '-'
},
{
key: 'تاریخ ثبت',
value: toJalaliNoTime(val.created_at)
},
{
key: 'تاریخ انقضا',
value: toJalaliNoTime(val.expiry_date)
},
{
key: 'توضیحات',
value: val.description ?? '-'
}
]
userInfoVisible.value = true
}
function showGiftCardCodes(val: Giftcard) {
// نمایش کدهای کارت هدیه در مودال جداگانه
if (val.giftcardcodes && val.giftcardcodes.length > 0) {
selectedGiftCardCodes.value = val.giftcardcodes
giftCardCodesVisible.value = true
} else {
message.info('کد کارت هدیه‌ای برای این کارت وجود ندارد')
}
}
//edit
let editId: number | undefined = undefined
async function edit(record: Giftcard) {
addGiftCardVisible.value = true
editId = record.id
form.value = {
mobile: record.customer?.mobile ?? '',
name: record.customer?.name ?? '',
count: record.count,
amount: record.amount,
unit: record.unit.toString(),
expiry_date: record.expiry_date,
description: record.description ?? ''
}
}
//add
async function add() {
editId = undefined
resetData()
addGiftCardVisible.value = true
}
//reset data
const resetData = () => {
formRef.value?.resetFields()
form.value = {
mobile: '',
name: '',
count: undefined,
amount: undefined,
unit: '1',
expiry_date: '',
description: ''
}
}
//delete
const deleteItem = (id: number) => confirmDeleteItem('کارت هدیه', id, deleteCustomer, refreshTable)
//add project modal
const addGiftCardVisible = ref<boolean>(false)
const userInfoVisible = ref<boolean>(false)
//submit gift card
async function ok() {
formRef.value?.validateFields().then(async () => {
loading.value = true
try {
// تنظیم تاریخ انقضا به آخر همان روز (23:59:59) و تبدیل به میلادی
const formData = { ...form.value }
if (formData.expiry_date) {
// تبدیل به میلادی و تنظیم به آخر روز
formData.expiry_date = dayjs(formData.expiry_date)
.calendar('gregory')
.endOf('day')
.format('YYYY-MM-DD HH:mm:ss')
}
await createGiftCard(formData)
message.success('کارت هدیه با موفقیت ثبت شد')
addGiftCardVisible.value = false
resetData()
refreshTable()
} catch (e) {
console.error(e)
message.error('خطا در ثبت کارت هدیه')
} finally {
loading.value = false
}
})
}
</script>
<style scoped></style>