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

109 lines
3.7 KiB
PHP

<?php
namespace Modules\AssetExchange\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\AssetExchange\Http\Controllers\Concerns\AuthorizesAssetExchange;
use Modules\AssetExchange\Models\CatalogMatch;
use Modules\AssetExchange\Models\Listing;
use Modules\AssetExchange\Services\CatalogMatchService;
use Modules\AssetExchange\Utils\AssetExchangeUtil;
class CatalogMatchController extends Controller
{
use AuthorizesAssetExchange;
public function __construct(
protected AssetExchangeUtil $aexUtil,
protected CatalogMatchService $catalogMatchService
) {
}
public function search(Request $request)
{
$this->authorizeAexAccess('aex.listings.view');
$business = $this->aexUtil->getBusiness();
if (! $business) {
abort(404);
}
$term = $request->get('term', '');
if (strlen($term) < 2) {
return response()->json([]);
}
return response()->json(
$this->catalogMatchService->searchCatalog($business, $term, $request->get('type'))
);
}
public function discover($listingId)
{
$this->authorizeAexAccess('aex.listings.update');
$business = $this->aexUtil->getBusiness();
$listing = Listing::where('business_id', $business->id)->findOrFail($listingId);
try {
$matches = $this->catalogMatchService->discoverMatches($business, $listing, true);
} catch (\Throwable $e) {
return redirect()->back()->with('status', $e->getMessage());
}
return redirect()
->action([ListingController::class, 'show'], [$listing->id])
->with('status', __('assetexchange::lang.catalog_discovered', ['count' => $matches->count()]));
}
public function confirm($matchId)
{
$this->authorizeAexAccess('aex.listings.update');
$business = $this->aexUtil->getBusiness();
$match = CatalogMatch::where('business_id', $business->id)->findOrFail($matchId);
try {
$this->catalogMatchService->confirmMatch($business, $match, auth()->user());
} catch (\Throwable $e) {
return redirect()->back()->with('status', $e->getMessage());
}
return redirect()
->action([ListingController::class, 'show'], [$match->listing_id])
->with('status', __('assetexchange::lang.catalog_match_confirmed'));
}
public function manualLink(Request $request, $listingId)
{
$this->authorizeAexAccess('aex.listings.update');
$business = $this->aexUtil->getBusiness();
$listing = Listing::where('business_id', $business->id)->findOrFail($listingId);
$data = $request->validate([
'catalog_type' => 'required|in:ie_line_template,ie_item_master,ie_installed_asset,maintenance_equipment',
'catalog_id' => 'required|integer|min:1',
'catalog_label' => 'nullable|string|max:255',
]);
$match = CatalogMatch::create([
'business_id' => $business->id,
'listing_id' => $listing->id,
'catalog_type' => $data['catalog_type'],
'catalog_id' => $data['catalog_id'],
'catalog_label' => $data['catalog_label'] ?? ('#'.$data['catalog_id']),
'match_score' => 100,
'match_reason' => 'انتخاب دستی توسط کارشناس',
'status' => 'suggested',
]);
$this->catalogMatchService->confirmMatch($business, $match, auth()->user());
return redirect()
->action([ListingController::class, 'show'], [$listing->id])
->with('status', __('assetexchange::lang.catalog_linked'));
}
}