Files
portal-api/app/Http/Helpers/function_helper.php
T
2025-07-24 16:18:27 +07:00

87 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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('users')->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;
}
}