279 lines
9.4 KiB
PHP
279 lines
9.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Enums\RawMaterialUnit;
|
|
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\RawMaterialRequest;
|
|
use App\Models\RawMaterial;
|
|
use App\Models\RawMaterialPrice;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class RawMaterialController extends Controller
|
|
{
|
|
use ParsesDataTableQuery;
|
|
|
|
public function index(Request $request): Response
|
|
{
|
|
$tableQuery = $this->parseDataTableQuery($request);
|
|
$search = $tableQuery['search'];
|
|
$sort = $tableQuery['sort'];
|
|
$direction = $tableQuery['direction'];
|
|
$isActive = $request->string('is_active')->toString();
|
|
|
|
$query = RawMaterial::query()
|
|
->with([
|
|
'prices' => fn ($query) => $query->orderBy('created_at'),
|
|
])
|
|
->when($search !== '', function (Builder $query) use ($search): void {
|
|
$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')
|
|
);
|
|
|
|
$this->applySorting($query, $sort, $direction);
|
|
|
|
$rawMaterials = $query
|
|
->paginate(10)
|
|
->withQueryString();
|
|
|
|
$rows = $rawMaterials->getCollection()
|
|
->flatMap(fn (RawMaterial $rawMaterial, int $index) => $this->flattenRawMaterialForTable(
|
|
$rawMaterial,
|
|
$index,
|
|
$rawMaterials->firstItem() ?? 1,
|
|
))
|
|
->values()
|
|
->all();
|
|
|
|
return Inertia::render('admin/raw-materials/Index', [
|
|
'rows' => $rows,
|
|
'pagination' => [
|
|
'current_page' => $rawMaterials->currentPage(),
|
|
'last_page' => $rawMaterials->lastPage(),
|
|
'per_page' => $rawMaterials->perPage(),
|
|
'total' => $rawMaterials->total(),
|
|
'links' => $rawMaterials->linkCollection()->toArray(),
|
|
],
|
|
'filters' => $this->dataTableFilters($tableQuery, [
|
|
'is_active' => $isActive,
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function create(): Response
|
|
{
|
|
return Inertia::render('admin/raw-materials/Create', [
|
|
'units' => RawMaterialUnit::selectOptions(),
|
|
]);
|
|
}
|
|
|
|
public function store(RawMaterialRequest $request): RedirectResponse
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
DB::transaction(function () use ($validated): void {
|
|
$rawMaterial = RawMaterial::create([
|
|
'name' => $validated['name'],
|
|
'unit' => $validated['unit'],
|
|
]);
|
|
|
|
foreach ($validated['prices'] as $priceData) {
|
|
$this->createPrice($rawMaterial, $priceData);
|
|
}
|
|
});
|
|
|
|
Inertia::flash('success', 'Bahan baku berhasil ditambahkan.');
|
|
|
|
return redirect()->route('admin.master.raw-materials.index');
|
|
}
|
|
|
|
public function edit(RawMaterial $rawMaterial): Response
|
|
{
|
|
$rawMaterial->load([
|
|
'prices' => fn ($query) => $query->orderBy('created_at'),
|
|
]);
|
|
|
|
return Inertia::render('admin/raw-materials/Edit', [
|
|
'units' => RawMaterialUnit::selectOptions(),
|
|
'rawMaterial' => $this->transformRawMaterialForForm($rawMaterial),
|
|
]);
|
|
}
|
|
|
|
public function update(RawMaterialRequest $request, RawMaterial $rawMaterial): RedirectResponse
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
DB::transaction(function () use ($validated, $rawMaterial): void {
|
|
$rawMaterial->name = $validated['name'];
|
|
$rawMaterial->unit = $validated['unit'];
|
|
$rawMaterial->save();
|
|
|
|
$submittedPriceIds = collect($validated['prices'])
|
|
->pluck('id')
|
|
->filter()
|
|
->map(fn ($id) => (int) $id)
|
|
->all();
|
|
|
|
$rawMaterial->prices()
|
|
->whereNotIn('id', $submittedPriceIds)
|
|
->get()
|
|
->each(fn (RawMaterialPrice $price) => $price->delete());
|
|
|
|
foreach ($validated['prices'] as $priceData) {
|
|
if (! empty($priceData['id'])) {
|
|
$price = $rawMaterial->prices()->findOrFail($priceData['id']);
|
|
$price->variant = $priceData['variant'];
|
|
$price->price = $priceData['price'];
|
|
$price->stock = $priceData['stock'];
|
|
$price->save();
|
|
|
|
continue;
|
|
}
|
|
|
|
$this->createPrice($rawMaterial, $priceData);
|
|
}
|
|
});
|
|
|
|
Inertia::flash('success', 'Bahan baku berhasil diperbarui.');
|
|
|
|
return redirect()->route('admin.master.raw-materials.index');
|
|
}
|
|
|
|
public function toggleStatus(Request $request, RawMaterial $rawMaterial): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'is_active' => ['required', 'boolean'],
|
|
]);
|
|
|
|
$rawMaterial->is_active = $validated['is_active'];
|
|
$rawMaterial->save();
|
|
|
|
Inertia::flash('success', 'Status bahan baku berhasil diperbarui.');
|
|
|
|
return back();
|
|
}
|
|
|
|
public function destroy(RawMaterial $rawMaterial): RedirectResponse
|
|
{
|
|
DB::transaction(function () use ($rawMaterial): void {
|
|
$rawMaterial->prices()->delete();
|
|
$rawMaterial->delete();
|
|
});
|
|
|
|
Inertia::flash('success', 'Bahan baku berhasil dihapus.');
|
|
|
|
return redirect()->route('admin.master.raw-materials.index');
|
|
}
|
|
|
|
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): RawMaterialPrice
|
|
{
|
|
return $rawMaterial->prices()->create([
|
|
'variant' => $priceData['variant'],
|
|
'price' => $priceData['price'],
|
|
'stock' => $priceData['stock'],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
private function flattenRawMaterialForTable(RawMaterial $rawMaterial, int $materialIndex, int $firstItem): array
|
|
{
|
|
$prices = $rawMaterial->prices->isNotEmpty()
|
|
? $rawMaterial->prices
|
|
: collect([null]);
|
|
|
|
$variantCount = $prices->count();
|
|
|
|
return $prices
|
|
->values()
|
|
->map(function ($price, int $variantIndex) use ($rawMaterial, $materialIndex, $firstItem, $variantCount) {
|
|
return [
|
|
'row_id' => $price
|
|
? "{$rawMaterial->id}-{$price->id}"
|
|
: "{$rawMaterial->id}-empty",
|
|
'raw_material_id' => $rawMaterial->id,
|
|
'raw_material_name' => $rawMaterial->name,
|
|
'unit' => $rawMaterial->unit->value,
|
|
'unit_label' => $rawMaterial->unit->label(),
|
|
'unit_abbreviation' => $rawMaterial->unit->abbreviation(),
|
|
'is_active' => $rawMaterial->is_active,
|
|
'is_first_variant' => $variantIndex === 0,
|
|
'variant_count' => $variantCount,
|
|
'raw_material_row_number' => $firstItem + $materialIndex,
|
|
'price_id' => $price?->id,
|
|
'variant' => $price?->variant,
|
|
'stock' => $price?->stock,
|
|
'stock_formatted' => $price ? $this->formatStock($price->stock, $rawMaterial->unit) : null,
|
|
'price' => $price?->price,
|
|
'price_formatted' => $price ? $this->formatPrice($price->price) : null,
|
|
];
|
|
})
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function transformRawMaterialForForm(RawMaterial $rawMaterial): array
|
|
{
|
|
return [
|
|
'id' => $rawMaterial->id,
|
|
'name' => $rawMaterial->name,
|
|
'unit' => $rawMaterial->unit->value,
|
|
'prices' => $rawMaterial->prices
|
|
->map(fn (RawMaterialPrice $price) => [
|
|
'id' => $price->id,
|
|
'variant' => $price->variant,
|
|
'price' => (string) (int) $price->price,
|
|
'stock' => $this->formatStockInput($price->stock),
|
|
])
|
|
->values()
|
|
->all(),
|
|
];
|
|
}
|
|
|
|
private function formatPrice(float|string $price): string
|
|
{
|
|
return 'Rp '.number_format((float) $price, 0, ',', '.');
|
|
}
|
|
|
|
private function formatStock(float|string $stock, RawMaterialUnit $unit): string
|
|
{
|
|
$formatted = rtrim(rtrim(number_format((float) $stock, 4, ',', '.'), '0'), ',');
|
|
|
|
return "{$formatted} {$unit->abbreviation()}";
|
|
}
|
|
|
|
private function formatStockInput(float|string $stock): string
|
|
{
|
|
return rtrim(rtrim(number_format((float) $stock, 4, '.', ''), '0'), '.');
|
|
}
|
|
}
|