115 lines
4.1 KiB
PHP
115 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\AssetExchange\Services;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\AssetExchange\Models\Appraisal;
|
|
use Modules\AssetExchange\Models\AppraisalLine;
|
|
use Modules\AssetExchange\Models\Listing;
|
|
|
|
class AssetExchangeIeBridgeService
|
|
{
|
|
public function importBomToAppraisalLines(Appraisal $appraisal, ?int $bomId = null, bool $replaceExisting = false): int
|
|
{
|
|
if (! class_exists(\Modules\IndustrialEngineering\Models\Bom::class)) {
|
|
throw new \RuntimeException('ماژول مهندسی صنعتی نصب نیست.');
|
|
}
|
|
|
|
if (! Schema::hasTable('ie_boms') || ! Schema::hasTable('aex_appraisal_lines')) {
|
|
throw new \RuntimeException('جداول BOM یا خطوط کارشناسی موجود نیست.');
|
|
}
|
|
|
|
$appraisal->loadMissing('listing');
|
|
$listing = $appraisal->listing;
|
|
$resolvedBomId = $bomId ?: $listing?->ie_bom_id;
|
|
|
|
if (! $resolvedBomId) {
|
|
throw new \RuntimeException('شناسه BOM برای واردسازی مشخص نشده است.');
|
|
}
|
|
|
|
$bom = \Modules\IndustrialEngineering\Models\Bom::where('business_id', $appraisal->business_id)
|
|
->with(['lines.itemMaster'])
|
|
->find($resolvedBomId);
|
|
|
|
if (! $bom) {
|
|
throw new \RuntimeException('BOM مورد نظر یافت نشد.');
|
|
}
|
|
|
|
$bomService = app(\Modules\IndustrialEngineering\Services\BomService::class);
|
|
$costingService = class_exists(\Modules\IndustrialEngineering\Services\LiveBomCostingService::class)
|
|
? app(\Modules\IndustrialEngineering\Services\LiveBomCostingService::class)
|
|
: null;
|
|
|
|
$liveCosts = [];
|
|
if ($costingService) {
|
|
$analysis = $costingService->analyze($bom, 1.0);
|
|
foreach ($analysis['lines'] ?? [] as $row) {
|
|
$liveCosts[$row['bom_line_id']] = (int) round($row['live_extended'] ?? $row['standard_extended'] ?? 0);
|
|
}
|
|
}
|
|
|
|
$flat = $bomService->explode($bom, 1.0);
|
|
$lineMap = [];
|
|
$sort = 0;
|
|
$created = 0;
|
|
|
|
if ($replaceExisting) {
|
|
AppraisalLine::where('appraisal_id', $appraisal->id)->delete();
|
|
}
|
|
|
|
foreach ($flat as $row) {
|
|
$parentLineId = isset($row['parent_line_id'], $lineMap[$row['parent_line_id']])
|
|
? $lineMap[$row['parent_line_id']]
|
|
: null;
|
|
|
|
$replacementCost = $liveCosts[$row['bom_line_id']]
|
|
?? (int) round($row['extended_cost'] ?? 0);
|
|
|
|
$line = AppraisalLine::create([
|
|
'business_id' => $appraisal->business_id,
|
|
'appraisal_id' => $appraisal->id,
|
|
'parent_line_id' => $parentLineId,
|
|
'line_kind' => 'part',
|
|
'name' => $row['item_name'] ?? ('قطعه #'.$row['bom_line_id']),
|
|
'ie_item_master_id' => $row['item_master_id'] ?? null,
|
|
'ie_bom_line_id' => $row['bom_line_id'],
|
|
'replacement_cost' => $replacementCost,
|
|
'health_percentage' => 100,
|
|
'renovation_need' => 'none',
|
|
'keep_in_line' => true,
|
|
'sort_order' => $sort++,
|
|
]);
|
|
|
|
$lineMap[$row['bom_line_id']] = $line->id;
|
|
$created++;
|
|
}
|
|
|
|
if ($listing && ! $listing->ie_bom_id) {
|
|
$listing->update(['ie_bom_id' => $bom->id]);
|
|
}
|
|
|
|
return $created;
|
|
}
|
|
|
|
public function resolveBomForListing(Listing $listing): ?object
|
|
{
|
|
if (! class_exists(\Modules\IndustrialEngineering\Models\Bom::class)) {
|
|
return null;
|
|
}
|
|
|
|
if ($listing->ie_bom_id) {
|
|
return \Modules\IndustrialEngineering\Models\Bom::find($listing->ie_bom_id);
|
|
}
|
|
|
|
if ($listing->ie_line_revision_id && Schema::hasTable('ie_boms')) {
|
|
return \Modules\IndustrialEngineering\Models\Bom::where('business_id', $listing->business_id)
|
|
->where('line_revision_id', $listing->ie_line_revision_id)
|
|
->where('status', 'released')
|
|
->orderByDesc('id')
|
|
->first();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|