ultimatepos/Modules/SupplyChain/Utils/SupplyChainUtil.php

80 lines
2.1 KiB
PHP

<?php
namespace Modules\SupplyChain\Utils;
use App\Business;
use App\Contact;
use App\Product;
use App\Utils\ModuleUtil;
use App\Utils\Util;
class SupplyChainUtil
{
public function __construct(
protected Util $commonUtil,
protected ModuleUtil $moduleUtil
) {
}
public function getBusinessId(): int
{
return (int) request()->session()->get('user.business_id');
}
public function getBusiness(): ?Business
{
$businessId = $this->getBusinessId();
return $businessId ? Business::find($businessId) : null;
}
public function getSuppliersDropdown(?int $businessId = null): array
{
$businessId = $businessId ?? $this->getBusinessId();
return Contact::suppliersDropdown($businessId, false);
}
public function getProductsDropdown(?int $businessId = null): array
{
$businessId = $businessId ?? $this->getBusinessId();
return Product::where('business_id', $businessId)
->where('is_inactive', 0)
->orderBy('name')
->pluck('name', 'id')
->toArray();
}
public function jsonOrRedirect($request, bool $success, string $message, $redirect = null)
{
if ($request->ajax() || $request->wantsJson()) {
return response()->json(['success' => $success, 'msg' => $message]);
}
return redirect($redirect ?? url()->previous())
->with('status', ['success' => $success, 'msg' => $message]);
}
public function riskBadgeClass(string $riskLevel): string
{
return match ($riskLevel) {
'low' => 'label-success',
'medium' => 'label-warning',
'high' => 'label-danger',
default => 'label-default',
};
}
public function rfqStatusBadgeClass(string $status): string
{
return match ($status) {
'open' => 'label-info',
'awarded' => 'label-success',
'closed' => 'label-default',
'cancelled' => 'label-danger',
default => 'label-warning',
};
}
}