112 lines
3.6 KiB
PHP
112 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\AssetExchange\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Modules\AssetExchange\Models\PriceInquiry;
|
|
use Modules\AssetExchange\Models\PriceInquiryRecipient;
|
|
use Modules\AssetExchange\Services\PriceInquiryService;
|
|
use Modules\AssetExchange\Utils\AssetExchangeUtil;
|
|
use Modules\Portal\Http\Controllers\BasePortalController;
|
|
|
|
class PortalPriceInquiryController extends BasePortalController
|
|
{
|
|
public function __construct(
|
|
protected AssetExchangeUtil $aexUtil,
|
|
protected PriceInquiryService $inquiryService
|
|
) {
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->ensurePortalAccess();
|
|
|
|
$business_id = $this->businessId();
|
|
$contact_id = $this->contactId();
|
|
|
|
$inquiries = PriceInquiry::where('business_id', $business_id)
|
|
->whereHas('recipients', function ($q) use ($contact_id) {
|
|
$q->where('recipient_type', 'supplier')->where('contact_id', $contact_id);
|
|
})
|
|
->with(['listing', 'recipients' => function ($q) use ($contact_id) {
|
|
$q->where('recipient_type', 'supplier')->where('contact_id', $contact_id);
|
|
}])
|
|
->orderByDesc('created_at')
|
|
->paginate(20);
|
|
|
|
return view('assetexchange::portal.inquiries.index', [
|
|
'inquiries' => $inquiries,
|
|
'aexUtil' => $this->aexUtil,
|
|
]);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->ensurePortalAccess();
|
|
|
|
$business_id = $this->businessId();
|
|
$contact_id = $this->contactId();
|
|
|
|
$inquiry = PriceInquiry::where('business_id', $business_id)
|
|
->whereHas('recipients', function ($q) use ($contact_id) {
|
|
$q->where('recipient_type', 'supplier')->where('contact_id', $contact_id);
|
|
})
|
|
->with(['listing', 'lines', 'recipients' => function ($q) use ($contact_id) {
|
|
$q->where('recipient_type', 'supplier')->where('contact_id', $contact_id);
|
|
}])
|
|
->findOrFail($id);
|
|
|
|
$recipient = $inquiry->recipients->first();
|
|
if ($recipient) {
|
|
$this->inquiryService->markRecipientViewed($recipient);
|
|
}
|
|
|
|
return view('assetexchange::portal.inquiries.show', [
|
|
'inquiry' => $inquiry,
|
|
'aexUtil' => $this->aexUtil,
|
|
]);
|
|
}
|
|
|
|
public function quote(Request $request, $id)
|
|
{
|
|
$this->ensurePortalAccess();
|
|
|
|
$business_id = $this->businessId();
|
|
$contact_id = $this->contactId();
|
|
|
|
$recipient = PriceInquiryRecipient::where('business_id', $business_id)
|
|
->where('id', $id)
|
|
->where('recipient_type', 'supplier')
|
|
->where('contact_id', $contact_id)
|
|
->firstOrFail();
|
|
|
|
$data = $request->validate([
|
|
'quoted_price' => 'required|integer|min:0',
|
|
'currency' => 'nullable|string|max:10',
|
|
'quote_notes' => 'nullable|string',
|
|
]);
|
|
|
|
$this->inquiryService->recordQuote($recipient, $data, auth()->user());
|
|
|
|
return redirect()->back()->with('status', __('assetexchange::lang.quote_recorded'));
|
|
}
|
|
|
|
public function unreadCount()
|
|
{
|
|
$this->ensurePortalAccess();
|
|
|
|
$business_id = $this->businessId();
|
|
$contact_id = $this->contactId();
|
|
|
|
$count = PriceInquiryRecipient::where('business_id', $business_id)
|
|
->where('recipient_type', 'supplier')
|
|
->where('contact_id', $contact_id)
|
|
->whereNotNull('sent_at')
|
|
->whereNull('viewed_at')
|
|
->count();
|
|
|
|
return response()->json(['unread_count' => $count]);
|
|
}
|
|
}
|
|
|