Files
portal-api/app/Http/Helpers/function_helper.php
T

107 lines
2.5 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 Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
if (!function_exists('setting')) {
function setting(string $key = '')
{
$repo = app()->make(\App\Repositories\Setting\SettingCachedRepository::class);
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_num')) {
/**
* 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_num(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' : '';
}