107 lines
2.7 KiB
PHP
107 lines
2.7 KiB
PHP
<?php
|
||
|
||
use App\Repositories\Setting\SettingCachedRepository;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Illuminate\Support\Str;
|
||
|
||
if (!function_exists('setting')) {
|
||
function setting(string $key = '')
|
||
{
|
||
// Cách gọi setting('setting') để lấy toàn bộ group json
|
||
$repo = app()->make(\App\Repositories\Setting\SettingCachedRepository::class);
|
||
|
||
// Nếu có dấu chấm => lấy theo group
|
||
if (Str::contains($key, '.')) {
|
||
[$group, $subKey] = explode('.', $key, 2);
|
||
$groupData = $repo->getByKey($group);
|
||
|
||
if (is_string($groupData)) {
|
||
$groupData = json_decode($groupData, true);
|
||
}
|
||
|
||
return $groupData[$subKey] ?? null;
|
||
}
|
||
|
||
// Ngược lại là setting đơn
|
||
return $repo->getByKey($key);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('format_price')) {
|
||
function format_price($price, $suffix = '₫')
|
||
{
|
||
if ($price === null)
|
||
return '';
|
||
return number_format($price, 0, ',', '.') . ' ' . $suffix;
|
||
}
|
||
}
|
||
if (!function_exists('format_point')) {
|
||
function format_point($point, $suffix = '𝑝')
|
||
{
|
||
if ($point === null)
|
||
return '';
|
||
return number_format($point, 0, ',', '.') . ' ' . $suffix;
|
||
}
|
||
}
|
||
|
||
|
||
if (!function_exists('format_day')) {
|
||
function format_day($date, $format = 'd/m/Y')
|
||
{
|
||
if (empty($date))
|
||
return '';
|
||
try {
|
||
return \Carbon\Carbon::parse($date)->format($format);
|
||
} catch (\Exception $e) {
|
||
return '';
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('customer')) {
|
||
function customer()
|
||
{
|
||
return Auth::guard('client')->user();
|
||
}
|
||
}
|
||
|
||
if (!function_exists('user')) {
|
||
function user()
|
||
{
|
||
return Auth::guard('client')->user();
|
||
}
|
||
}
|
||
|
||
if (!function_exists('generate_order_number')) {
|
||
/**
|
||
* Generate a unique order number with given prefix
|
||
*
|
||
* @param string $prefix Default prefix 'TGS'
|
||
* @param int $length Length of random number (default 6)
|
||
* @return string Unique order number
|
||
*/
|
||
function generate_order_number(string $prefix = 'TGS', int $length = 6): string
|
||
{
|
||
do {
|
||
$randomNumber = random_int(10 ** ($length - 1), (10 ** $length) - 1);
|
||
$orderNum = $prefix . $randomNumber;
|
||
} while (\App\Models\Order::where('order_num', $orderNum)->exists());
|
||
|
||
return $orderNum;
|
||
}
|
||
}
|
||
|
||
function active_class($path, $active = 'active')
|
||
{
|
||
return request()->is(...(array) $path) ? $active : '';
|
||
}
|
||
|
||
function is_active_route($path)
|
||
{
|
||
return request()->is(...(array) $path) ? 'true' : 'false';
|
||
}
|
||
|
||
function show_class($path)
|
||
{
|
||
return request()->is(...(array) $path) ? 'show' : '';
|
||
} |