87 lines
2.1 KiB
PHP
87 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Repos\Order\Loan;
|
|
|
|
use App\Models\Branch\Branch;
|
|
use App\Nova\Resources\Order\Loan\LoanOrder as NovaLoanOrder;
|
|
use App\Repos\Order\OrderRepo;
|
|
use Closure;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
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,
|
|
Model $model,
|
|
string $message,
|
|
string $type = 'info',
|
|
string $icon = 'eye',
|
|
): void {
|
|
/** @var \App\Models\Order\Loan\LoanOrder */
|
|
$loanOrder = $model;
|
|
|
|
$id = $loanOrder->id;
|
|
$nova_path = config('nova.path');
|
|
$loan_order_path = 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)
|
|
);
|
|
}
|
|
}
|