88 lines
2.7 KiB
PHP
88 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\GoodsReceipt;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class GoodsReceiptRepository extends BaseRepository implements GoodsReceiptRepositoryInterface
|
|
{
|
|
public function __construct(GoodsReceipt $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function all(array $columns = ['*']): Collection
|
|
{
|
|
return $this->model->with(['purchaseOrder', 'warehouse', 'lines.product', 'lines.packaging', 'lines.tvaRate'])->get($columns);
|
|
}
|
|
|
|
public function find(int|string $id, array $columns = ['*']): ?GoodsReceipt
|
|
{
|
|
return $this->model->with(['purchaseOrder', 'warehouse', 'lines.product', 'lines.packaging', 'lines.tvaRate'])->find($id, $columns);
|
|
}
|
|
|
|
public function create(array $attributes): GoodsReceipt
|
|
{
|
|
return DB::transaction(function () use ($attributes) {
|
|
try {
|
|
$lines = $attributes['lines'] ?? [];
|
|
unset($attributes['lines']);
|
|
|
|
$goodsReceipt = parent::create($attributes);
|
|
|
|
if (!empty($lines)) {
|
|
foreach ($lines as $line) {
|
|
$goodsReceipt->lines()->create($line);
|
|
}
|
|
}
|
|
|
|
return $goodsReceipt->load('lines.product', 'lines.packaging', 'lines.tvaRate');
|
|
} catch (\Exception $e) {
|
|
Log::error('Error creating GoodsReceipt with lines: ' . $e->getMessage(), [
|
|
'attributes' => $attributes,
|
|
'exception' => $e
|
|
]);
|
|
throw $e;
|
|
}
|
|
});
|
|
}
|
|
|
|
public function update(int|string $id, array $attributes): bool
|
|
{
|
|
return DB::transaction(function () use ($id, $attributes) {
|
|
try {
|
|
$goodsReceipt = $this->find($id);
|
|
if (!$goodsReceipt) {
|
|
return false;
|
|
}
|
|
|
|
$lines = $attributes['lines'] ?? null;
|
|
unset($attributes['lines']);
|
|
|
|
$updated = parent::update($id, $attributes);
|
|
|
|
if ($lines !== null && $updated) {
|
|
$goodsReceipt->lines()->delete();
|
|
foreach ($lines as $line) {
|
|
$goodsReceipt->lines()->create($line);
|
|
}
|
|
}
|
|
|
|
return $updated;
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating GoodsReceipt with lines: ' . $e->getMessage(), [
|
|
'id' => $id,
|
|
'attributes' => $attributes,
|
|
'exception' => $e
|
|
]);
|
|
throw $e;
|
|
}
|
|
});
|
|
}
|
|
}
|