100 lines
3.5 KiB
PHP
100 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\AssetExchange\Services;
|
|
|
|
use App\Business;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\AssetExchange\Models\Listing;
|
|
use Modules\AssetExchange\Utils\AssetExchangeUtil;
|
|
|
|
class IntakeService
|
|
{
|
|
public function __construct(
|
|
protected ListingService $listingService,
|
|
protected MediaService $mediaService,
|
|
protected CatalogMatchService $catalogMatchService,
|
|
protected ResearchCaseService $researchCaseService,
|
|
protected AssetExchangeCrmBridgeService $crmBridge,
|
|
protected AssetExchangeUtil $aexUtil,
|
|
protected PipelineService $pipelineService
|
|
) {
|
|
}
|
|
|
|
public function createFromPortal(Business $business, array $data, $request, ?User $user = null, string $source = 'portal_customer'): Listing
|
|
{
|
|
return DB::transaction(function () use ($business, $data, $request, $user, $source) {
|
|
$specs = $this->buildSpecsJson($data);
|
|
$data['specs_json'] = $specs;
|
|
$data['intake_source'] = $source;
|
|
$data['visibility'] = $data['visibility'] ?? 'portal';
|
|
|
|
$listing = $this->listingService->create($business, $data, $user);
|
|
|
|
if ($request && method_exists($request, 'hasFile')) {
|
|
$this->mediaService->uploadForListing($business, $listing, $request, $user);
|
|
}
|
|
|
|
$listing = $this->listingService->submit($business, $listing->fresh(['media']), $user);
|
|
|
|
try {
|
|
$this->catalogMatchService->discoverMatches($business, $listing->fresh(), true);
|
|
} catch (\Throwable) {
|
|
}
|
|
|
|
try {
|
|
$this->crmBridge->createLeadFromListing($business, $listing->fresh());
|
|
} catch (\Throwable) {
|
|
}
|
|
|
|
$settings = $this->aexUtil->getAexSettings($business->id);
|
|
if ($settings['auto_start_research_on_intake'] ?? true) {
|
|
try {
|
|
$this->researchCaseService->openCase($business, $listing->fresh(), [
|
|
'title' => 'تحقیق اولیه — '.$listing->title,
|
|
'spawn_templates' => true,
|
|
'lead_researcher_id' => $listing->assigned_broker_user_id,
|
|
], $user);
|
|
} catch (\Throwable) {
|
|
}
|
|
}
|
|
|
|
$this->pipelineService->logEvent(
|
|
$business,
|
|
Listing::class,
|
|
$listing->id,
|
|
'portal_intake_received',
|
|
'دریافت عرضه از پورتال با مشخصات و رسانه',
|
|
$user,
|
|
['source' => $source, 'media_count' => $listing->media()->count()]
|
|
);
|
|
|
|
return $listing->fresh(['media', 'seller']);
|
|
});
|
|
}
|
|
|
|
protected function buildSpecsJson(array $data): array
|
|
{
|
|
$keys = [
|
|
'power_kw', 'voltage', 'dimensions', 'weight_kg', 'capacity',
|
|
'control_system', 'automation_level', 'included_accessories',
|
|
'known_defects', 'maintenance_history', 'operating_hours_note',
|
|
'custom_fields',
|
|
];
|
|
|
|
$specs = [];
|
|
foreach ($keys as $key) {
|
|
if (! empty($data[$key])) {
|
|
$specs[$key] = $data[$key];
|
|
}
|
|
}
|
|
|
|
if (! empty($data['specs_json']) && is_array($data['specs_json'])) {
|
|
$specs = array_merge($specs, $data['specs_json']);
|
|
}
|
|
|
|
return $specs;
|
|
}
|
|
}
|