98 lines
2.6 KiB
PHP
98 lines
2.6 KiB
PHP
<?php
|
|
|
|
use App\Enums\ActivityType;
|
|
use App\Models\LogUserActivity;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
if (!function_exists('getFormattedValue')) {
|
|
function getFormattedValue($value, $decNum = 8, $hasTrail = false)
|
|
{
|
|
if ($value !== null && $value !== '' && is_numeric($value)) {
|
|
// Convert to float and format with maximum decimal places
|
|
$value = number_format((float) $value, $decNum, ',', '.');
|
|
|
|
// Remove trailing zeros and the decimal separator if there are no decimals left
|
|
if ($hasTrail == false) {
|
|
$value = rtrim(rtrim($value, '0'), ',');
|
|
}
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('getOriginalValue')) {
|
|
function getOriginalValue($value)
|
|
{
|
|
if ($value) {
|
|
$value = str_replace('.', '', $value); // Remove periods
|
|
$value = str_replace(',', '.', $value); // Replace commas with periods
|
|
|
|
if (is_numeric($value)) {
|
|
return (float) $value;
|
|
}
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|
|
|
|
|
|
if (!function_exists('formatActivityCode')) {
|
|
function formatActivityCode($code)
|
|
{
|
|
if (strpos($code, '_') === false && strlen($code) <= 3) {
|
|
return strtoupper($code);
|
|
} else {
|
|
$words = explode('_', $code);
|
|
$formattedWords = array_map('ucwords', $words);
|
|
return implode(' ', $formattedWords);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('logUserActivity')) {
|
|
function logUserActivity(ActivityType $activityType, ?string $formId = null)
|
|
{
|
|
$user = Auth::user();
|
|
$uuid = (string) Str::uuid();
|
|
|
|
if (!$user) {
|
|
return;
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
LogUserActivity::create([
|
|
'id' => $uuid,
|
|
'user_id' => $user->id,
|
|
'activity_type' => $activityType->value,
|
|
'ip_address' => Request::ip(),
|
|
'form_id' => $formId,
|
|
'created_by' => $user->name,
|
|
'updated_by' => $user->name,
|
|
]);
|
|
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('activityYearRange')) {
|
|
function activityYearRange(int $inventoryYear, $isDesc = true, int $range = 15): array
|
|
{
|
|
if ($isDesc == true) {
|
|
return range($inventoryYear - 1, $inventoryYear - $range);
|
|
} else {
|
|
return range($inventoryYear - $range, $inventoryYear - 1);
|
|
}
|
|
}
|
|
}
|