49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Quote;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class QuoteRepository extends BaseRepository implements QuoteRepositoryInterface
|
|
{
|
|
public function __construct(
|
|
Quote $model,
|
|
protected QuoteLineRepositoryInterface $quoteLineRepository
|
|
) {
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function create(array $data): Quote
|
|
{
|
|
return DB::transaction(function () use ($data) {
|
|
try {
|
|
// Create the quote
|
|
$quote = parent::create($data);
|
|
|
|
// Create the quote lines
|
|
if (isset($data['lines']) && is_array($data['lines'])) {
|
|
foreach ($data['lines'] as $lineData) {
|
|
$lineData['quote_id'] = $quote->id;
|
|
$this->quoteLineRepository->create($lineData);
|
|
}
|
|
}
|
|
|
|
return $quote;
|
|
} catch (\Exception $e) {
|
|
// Log the error
|
|
Log::error('Error creating quote with lines: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'data' => $data,
|
|
]);
|
|
|
|
// Re-throw to trigger rollback
|
|
throw $e;
|
|
}
|
|
});
|
|
}
|
|
}
|