ultimatepos/Modules/AssetExchange/Http/Controllers/PortalListingController.php

114 lines
3.9 KiB
PHP

<?php
namespace Modules\AssetExchange\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\AssetExchange\Models\Listing;
use Modules\AssetExchange\Services\IntakeService;
use Modules\AssetExchange\Utils\AssetExchangeUtil;
use Modules\Portal\Http\Controllers\BasePortalController;
class PortalListingController extends BasePortalController
{
public function __construct(
protected AssetExchangeUtil $aexUtil,
protected IntakeService $intakeService
) {
}
public function index()
{
$this->ensurePortalAccess();
$business_id = $this->businessId();
$contact_id = $this->contactId();
$settings = $this->aexUtil->getAexSettings($business_id);
if (empty($settings['portal_listings_enabled'])) {
abort(404);
}
$listings = Listing::where('business_id', $business_id)
->when($contact_id, fn ($q) => $q->where('seller_contact_id', $contact_id))
->withCount('media')
->orderByDesc('created_at')
->paginate(20);
return view('assetexchange::portal.listings.index', compact('listings'));
}
public function create()
{
$this->ensurePortalAccess();
$business_id = $this->businessId();
$settings = $this->aexUtil->getAexSettings($business_id);
if (empty($settings['portal_listings_enabled'])) {
abort(404);
}
return view('assetexchange::portal.listings.create', [
'listing_types' => $this->aexUtil->listingTypeLabels(),
]);
}
public function store(Request $request)
{
$this->ensurePortalAccess();
$business = $this->aexUtil->getBusiness();
if (! $business) {
abort(404);
}
$settings = $this->aexUtil->getAexSettings($business->id);
if (empty($settings['portal_listings_enabled'])) {
abort(404);
}
$data = $request->validate([
'title' => 'required|string|max:255',
'listing_type' => 'required|in:machine_new,machine_used,production_line_full,production_line_partial,subsystem,spare_lot',
'manufacturer' => 'nullable|string|max:120',
'model' => 'nullable|string|max:120',
'serial_number' => 'nullable|string|max:120',
'description' => 'nullable|string',
'location_text' => 'nullable|string|max:255',
'year_manufactured' => 'nullable|integer|min:1900|max:2100',
'working_hours' => 'nullable|integer|min:0',
'asking_price' => 'nullable|integer|min:0',
'condition_grade' => 'nullable|in:A,B,C,D',
'power_kw' => 'nullable|string|max:50',
'voltage' => 'nullable|string|max:50',
'dimensions' => 'nullable|string|max:120',
'weight_kg' => 'nullable|string|max:50',
'control_system' => 'nullable|string|max:120',
'known_defects' => 'nullable|string',
'maintenance_history' => 'nullable|string',
'included_accessories' => 'nullable|string',
'photos' => 'nullable|array',
'photos.*' => 'image|max:10240',
'documents' => 'nullable|array',
'documents.*' => 'file|max:20480',
]);
$data['seller_contact_id'] = $this->contactId();
$data['visibility'] = 'portal';
$data['price_negotiable'] = true;
$source = request()->segment(1) === 'supplier' ? 'portal_supplier' : 'portal_customer';
try {
$listing = $this->intakeService->createFromPortal($business, $data, $request, auth()->user(), $source);
} catch (\Throwable $e) {
return redirect()->back()->withInput()->with('status', $e->getMessage());
}
return redirect()
->action([self::class, 'index'])
->with('status', __('assetexchange::lang.portal_listing_submitted'));
}
}