fix ذشزنلقخعدی
This commit is contained in:
+69
-12
@@ -1,3 +1,4 @@
|
||||
import { ref } from 'vue'
|
||||
import { useSettingsStore } from '@/store/settingsStore'
|
||||
import { lastPrice } from '@/utils/price'
|
||||
|
||||
@@ -13,7 +14,13 @@ export interface EffectiveTradingHours {
|
||||
enforced: boolean
|
||||
}
|
||||
|
||||
let productTradingHours: { start: string; end: string } | null = null
|
||||
const productTradingHours = ref<{ start: string; end: string } | null>(null)
|
||||
const productTradingHoursVersion = ref(0)
|
||||
|
||||
function normalizeTradingTimeSetting(value: unknown, fallback: string): string {
|
||||
const raw = String(value ?? '').trim()
|
||||
return raw || fallback
|
||||
}
|
||||
|
||||
function parseTimeToSeconds(time: string): number {
|
||||
const parts = time.trim().split(':').map(Number)
|
||||
@@ -22,20 +29,56 @@ function parseTimeToSeconds(time: string): number {
|
||||
}
|
||||
|
||||
export function formatTradingTimeDisplay(time: string): string {
|
||||
const parts = time.trim().split(':')
|
||||
const normalized = normalizeTradingTimeSetting(time, '')
|
||||
if (!normalized) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const parts = normalized.split(':')
|
||||
if (parts.length >= 3) {
|
||||
return `${parts[0]}:${parts[1]}:${parts[2]}`
|
||||
}
|
||||
if (parts.length >= 2) {
|
||||
return `${parts[0]}:${parts[1]}:00`
|
||||
}
|
||||
return time
|
||||
return normalized
|
||||
}
|
||||
|
||||
function hasProductTradingWindow(start?: string | null, end?: string | null): boolean {
|
||||
const open = formatTradingTimeDisplay(normalizeTradingTimeSetting(start, '00:00:00'))
|
||||
const close = formatTradingTimeDisplay(normalizeTradingTimeSetting(end, '00:00:00'))
|
||||
if (!open || !close) {
|
||||
return false
|
||||
}
|
||||
|
||||
return open !== close
|
||||
}
|
||||
|
||||
export function setProductTradingHours(start?: string | null, end?: string | null) {
|
||||
if (start && end) {
|
||||
productTradingHours = { start, end }
|
||||
if (!hasProductTradingWindow(start, end)) {
|
||||
productTradingHours.value = null
|
||||
productTradingHoursVersion.value += 1
|
||||
return
|
||||
}
|
||||
|
||||
productTradingHours.value = {
|
||||
start: normalizeTradingTimeSetting(start, '00:00'),
|
||||
end: normalizeTradingTimeSetting(end, '23:59'),
|
||||
}
|
||||
productTradingHoursVersion.value += 1
|
||||
}
|
||||
|
||||
export function syncProductTradingHoursFromProducts(
|
||||
products: Array<{ auto_open_at?: string | null; auto_close_at?: string | null }>,
|
||||
) {
|
||||
for (const product of products) {
|
||||
if (hasProductTradingWindow(product.auto_open_at, product.auto_close_at)) {
|
||||
setProductTradingHours(product.auto_open_at, product.auto_close_at)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
setProductTradingHours(null, null)
|
||||
}
|
||||
|
||||
export function getStoreTradingHours(): StoreTradingHours {
|
||||
@@ -47,13 +90,14 @@ export function getStoreTradingHours(): StoreTradingHours {
|
||||
String(closeRaw ?? 'false').trim().toLowerCase() === 'true'
|
||||
|
||||
return {
|
||||
start: String(settings.getSettingByKey('startDay') ?? '00:00'),
|
||||
end: String(settings.getSettingByKey('endDay') ?? '23:59'),
|
||||
start: normalizeTradingTimeSetting(settings.getSettingByKey('startDay'), '00:00'),
|
||||
end: normalizeTradingTimeSetting(settings.getSettingByKey('endDay'), '23:59'),
|
||||
closePriceEnabled,
|
||||
}
|
||||
}
|
||||
|
||||
export function getEffectiveTradingHours(): EffectiveTradingHours {
|
||||
void productTradingHoursVersion.value
|
||||
const settings = getStoreTradingHours()
|
||||
if (settings.closePriceEnabled) {
|
||||
return {
|
||||
@@ -63,10 +107,10 @@ export function getEffectiveTradingHours(): EffectiveTradingHours {
|
||||
}
|
||||
}
|
||||
|
||||
if (productTradingHours?.start && productTradingHours?.end) {
|
||||
if (productTradingHours.value?.start && productTradingHours.value?.end) {
|
||||
return {
|
||||
start: productTradingHours.start,
|
||||
end: productTradingHours.end,
|
||||
start: productTradingHours.value.start,
|
||||
end: productTradingHours.value.end,
|
||||
enforced: true,
|
||||
}
|
||||
}
|
||||
@@ -92,10 +136,18 @@ export function isStoreClosedBySchedule(now = new Date()): boolean {
|
||||
}
|
||||
|
||||
export function isStoreClosed(): boolean {
|
||||
if (lastPrice.value?.closed === true) {
|
||||
return true
|
||||
void productTradingHoursVersion.value
|
||||
const { closePriceEnabled } = getStoreTradingHours()
|
||||
|
||||
if (closePriceEnabled) {
|
||||
if (lastPrice.value?.closed === true) {
|
||||
return true
|
||||
}
|
||||
|
||||
return isStoreClosedBySchedule()
|
||||
}
|
||||
|
||||
// وقتی close_price خاموش است، صفر بودن قیمت مرجع به معنی بسته بودن فروشگاه نیست.
|
||||
return isStoreClosedBySchedule()
|
||||
}
|
||||
|
||||
@@ -103,5 +155,10 @@ export function getStoreClosedMessage(): string {
|
||||
const { start, end } = getEffectiveTradingHours()
|
||||
const open = formatTradingTimeDisplay(start)
|
||||
const close = formatTradingTimeDisplay(end)
|
||||
|
||||
if (!open || !close) {
|
||||
return 'فروشگاه در حال حاضر بسته است.'
|
||||
}
|
||||
|
||||
return `فروشگاه در حال حاضر بسته است. ساعت کاری از ${open} تا ${close} میباشد.`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user