200 lines
6.9 KiB
PHP
200 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Master;
|
|
|
|
use App\Enums\RawMaterialUnit;
|
|
use App\Models\RawMaterial;
|
|
use App\Models\RawMaterialPrice;
|
|
use App\Services\Media\MediaService;
|
|
use App\Support\Media\MediaPresenter;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class RawMaterialService
|
|
{
|
|
private const MAX_VARIANT_IMAGES = 5;
|
|
|
|
public function __construct(
|
|
private readonly MediaService $mediaService,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{search: string, sort: string, direction: 'asc'|'desc'} $tableQuery
|
|
*/
|
|
public function paginateForIndex(array $tableQuery, string $isActive, string $stockStatus = ''): LengthAwarePaginator
|
|
{
|
|
$query = RawMaterial::query()
|
|
->with([
|
|
'prices' => fn ($query) => $query
|
|
->orderBy('created_at')
|
|
->with(['rawMaterial:id,unit', 'media']),
|
|
])
|
|
->when($tableQuery['search'] !== '', function (Builder $query) use ($tableQuery): void {
|
|
$search = $tableQuery['search'];
|
|
$query->where(function (Builder $query) use ($search): void {
|
|
$query->where('name', 'like', "%{$search}%")
|
|
->orWhereHas('prices', fn (Builder $query) => $query->where('variant', 'like', "%{$search}%"));
|
|
});
|
|
})
|
|
->when($isActive !== '', fn (Builder $query) => $query->where('is_active', $isActive === '1'))
|
|
->when($stockStatus === 'out_of_stock', function (Builder $query): void {
|
|
$query->whereHas('prices', fn (Builder $priceQuery) => $priceQuery->where('stock', '<=', 0));
|
|
})
|
|
->when($stockStatus === 'low_stock', function (Builder $query): void {
|
|
$minStock = [
|
|
RawMaterialUnit::YARD => 20,
|
|
RawMaterialUnit::METER => 10,
|
|
RawMaterialUnit::KILOGRAM => 5,
|
|
];
|
|
|
|
$query->whereHas('prices', function (Builder $priceQuery) use ($minStock): void {
|
|
$priceQuery->where('stock', '>', 0)->where(function (Builder $priceQuery) use ($minStock): void {
|
|
foreach (RawMaterialUnit::cases() as $unit) {
|
|
$priceQuery->orWhere(function (Builder $priceQuery) use ($unit, $minStock): void {
|
|
$priceQuery->whereHas('rawMaterial', fn (Builder $rawMaterialQuery) => $rawMaterialQuery->where('unit', $unit))
|
|
->where('stock', '<', $minStock[$unit]);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
$this->applySorting($query, $tableQuery['sort'], $tableQuery['direction']);
|
|
|
|
return $query
|
|
->paginate(10)
|
|
->withQueryString()
|
|
->through(function (RawMaterial $rawMaterial) {
|
|
$rawMaterial->prices->each(function (RawMaterialPrice $price): void {
|
|
$price->setAttribute(
|
|
'images',
|
|
MediaPresenter::collection($price, 'images'),
|
|
);
|
|
});
|
|
|
|
return $rawMaterial;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
*/
|
|
public function create(array $validated): void
|
|
{
|
|
DB::transaction(function () use ($validated): void {
|
|
$rawMaterial = RawMaterial::create([
|
|
'name' => $validated['name'],
|
|
'unit' => $validated['unit'],
|
|
]);
|
|
|
|
foreach ($validated['prices'] as $index => $priceData) {
|
|
$this->createPrice($rawMaterial, $priceData, $index);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
*/
|
|
public function update(RawMaterial $rawMaterial, array $validated): void
|
|
{
|
|
DB::transaction(function () use ($validated, $rawMaterial): void {
|
|
$rawMaterial->update([
|
|
'name' => $validated['name'],
|
|
]);
|
|
|
|
$submittedPriceIds = collect($validated['prices'])
|
|
->pluck('id')
|
|
->filter()
|
|
->map(fn ($id) => (int) $id)
|
|
->all();
|
|
|
|
$rawMaterial->prices()
|
|
->whereNotIn('id', $submittedPriceIds)
|
|
->get()
|
|
->each(function (RawMaterialPrice $price): void {
|
|
$price->clearMediaCollection('images');
|
|
$price->delete();
|
|
});
|
|
|
|
foreach ($validated['prices'] as $index => $priceData) {
|
|
if (! empty($priceData['id'])) {
|
|
$price = $rawMaterial->prices()->findOrFail($priceData['id']);
|
|
$price->update([
|
|
'variant' => $priceData['variant'],
|
|
'price' => $priceData['price'],
|
|
'stock' => $priceData['stock'],
|
|
]);
|
|
$this->syncPriceImages($price, $priceData, $index);
|
|
|
|
continue;
|
|
}
|
|
|
|
$this->createPrice($rawMaterial, $priceData, $index);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function toggleStatus(RawMaterial $rawMaterial, array $validated): void
|
|
{
|
|
$rawMaterial->update([
|
|
'is_active' => $validated['is_active'],
|
|
]);
|
|
}
|
|
|
|
public function delete(RawMaterial $rawMaterial): void
|
|
{
|
|
DB::transaction(function () use ($rawMaterial): void {
|
|
$rawMaterial->prices()->each(function (RawMaterialPrice $price): void {
|
|
$price->clearMediaCollection('images');
|
|
});
|
|
$rawMaterial->prices()->delete();
|
|
$rawMaterial->delete();
|
|
});
|
|
}
|
|
|
|
private function applySorting(Builder $query, string $sort, string $direction): void
|
|
{
|
|
if (in_array($sort, ['name', 'unit', 'is_active'], true)) {
|
|
$query->orderBy($sort, $direction);
|
|
|
|
return;
|
|
}
|
|
|
|
$query->latest();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $priceData
|
|
*/
|
|
private function createPrice(RawMaterial $rawMaterial, array $priceData, int $index): RawMaterialPrice
|
|
{
|
|
$price = $rawMaterial->prices()->create([
|
|
'variant' => $priceData['variant'],
|
|
'price' => $priceData['price'],
|
|
'stock' => $priceData['stock'],
|
|
]);
|
|
|
|
$this->syncPriceImages($price, $priceData, $index);
|
|
|
|
return $price;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $priceData
|
|
*/
|
|
private function syncPriceImages(RawMaterialPrice $price, array $priceData, int $index): void
|
|
{
|
|
$this->mediaService->syncCollection(
|
|
$price,
|
|
'images',
|
|
$priceData['images'] ?? null,
|
|
$priceData['remove_media_ids'] ?? null,
|
|
self::MAX_VARIANT_IMAGES,
|
|
required: true,
|
|
errorKey: "prices.{$index}.images",
|
|
);
|
|
}
|
|
}
|