Files
2025-05-12 19:37:09 +05:00

135 lines
4.1 KiB
PHP

<?php
namespace App\Repos\Order\Loan;
use App\Models\Branch\Branch;
use App\Models\Order\Loan\LoanOrder;
use App\Nova\Resources\Order\Loan\LoanOrder as NovaLoanOrder;
use App\Nova\Resources\Order\Loan\LoanOrderMobile;
use App\Repos\Order\OrderRepo;
use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Notifications\NovaNotification;
use Laravel\Nova\URL;
class LoanOrderRepo
{
/**
* Satisfiable values
*
* @return array<string|null, string>
*/
public static function satisfiableValues(): array
{
return [
null => '-',
'satisfiable' => __('Satisfiable'),
'insufficient' => __('Insufficient'),
'unknown' => __('Unknown'),
];
}
/**
* When model is being created
*/
public static function creating(): Closure
{
return function (mixed $model) {
$model->status = $model->status ?: OrderRepo::defaultStatus();
};
}
/**
* When model is created
*/
public static function created(): Closure
{
return function ($model) {
$model->update(['unique_id' => static::generateUniqueId($model)]);
};
}
/**
* Fill unique id
*/
public static function generateUniqueId(mixed $model): string
{
return sprintf(
'TB%s-%s',
Branch::find($model->branch_id)->unique_code ?? uniqid(),
$model->id,
);
}
public static function notifyUser(
NovaRequest $request,
LoanOrder $loanOrder,
string $message,
string $type = 'info',
string $icon = 'eye',
bool $mobile = false,
): void {
$id = $loanOrder->id;
$nova_path = config('nova.path');
$loan_order_path = $mobile ? LoanOrderMobile::uriKey() : NovaLoanOrder::uriKey();
$url = url($nova_path.'/resources/'.$loan_order_path.'/'.$id);
$request->user()->notifyNow(
NovaNotification::make()
->message($message.': '.$loanOrder->unique_id)
->action(__('See'), URL::remote($url))
->icon($icon)
->type($type)
);
}
public static function notifyOperators(
LoanOrder $loanOrder,
string $message,
string $type = 'info',
string $icon = 'eye',
bool $mobile = false
): void {
$id = $loanOrder->id;
$nova_path = config('nova.path');
$loan_order_path = $mobile ? LoanOrderMobile::uriKey() : NovaLoanOrder::uriKey();
$url = URL::remote(url($nova_path.'/resources/'.$loan_order_path.'/'.$id));
// Get users related with branch...
$branch_users = DB::table('branch_user')->where('branch_id', $loanOrder->branch_id)->pluck('user_id');
// Check if they have permission for viewing loan order
$loanOrderOperators = DB::table('model_has_permissions')
->where('permission_id', view_loan_order_permission_id())
->whereIntegerInRaw('model_id', $branch_users)
->pluck('model_id');
$loanOrderOperators->each(function ($branch_user) use ($icon, $message, $url, $type) {
DB::table('nova_notifications')->insert([
'id' => Str::uuid(),
'type' => 'Laravel\Nova\Notifications\NovaNotification',
'notifiable_type' => 'App\Models\User',
'notifiable_id' => $branch_user,
'data' => json_encode([
'component' => 'message-notification',
'icon' => $icon,
'message' => $message,
'actionText' => 'Görmek',
'actionUrl' => [
'url' => $url,
'remote' => true,
],
'openInNewTab' => false,
'type' => $type,
'iconClass' => 'text-sky-500',
]),
'created_at' => now(),
'updated_at' => now(),
]);
});
}
}