Erster Upload
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\User\Resources\ProductInventoryResource\Pages;
|
||||
|
||||
use App\Filament\User\Resources\ProductInventoryResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Filament\Notifications\Notification;
|
||||
use App\Models\Product;
|
||||
use App\Models\Spice;
|
||||
use App\Models\ProductSize;
|
||||
|
||||
class CreateProductInventory extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ProductInventoryResource::class;
|
||||
|
||||
protected function handleRecordCreation(array $data): Model
|
||||
{
|
||||
$amount = $data['amount'] ?? 1;
|
||||
unset($data['amount']);
|
||||
|
||||
$product = Product::find($data['product_id']);
|
||||
if (!$product) {
|
||||
return false;
|
||||
}
|
||||
$size = ProductSize::find($data['size_id']);
|
||||
if (!$size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$recipe = $product->recipe;
|
||||
$packing = $size->packing;
|
||||
$totalAmount = $packing->size;
|
||||
|
||||
|
||||
foreach ($recipe->spices as $spice) {
|
||||
$percentage = $spice->pivot->amount_in_percent;
|
||||
$amountNeeded = (($percentage / 100) * $totalAmount) * $amount;
|
||||
|
||||
if (!$spice || $spice->quantity < $amountNeeded) {
|
||||
Notification::make()
|
||||
->warning()
|
||||
->title('Fehler!')
|
||||
->body('Nicht genügend '.htmlentities($spice->name).' für dieses Produkt in dieser Größe lagernd (bräuchte '.$amountNeeded.' '.htmlentities($spice->unit).')')
|
||||
->send();
|
||||
$this->halt();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$records = [];
|
||||
for($i = 0; $i < $amount; $i++) {
|
||||
$records[] = static::getModel()::create($data);
|
||||
}
|
||||
|
||||
foreach ($recipe->spices as $spice) {
|
||||
$percentage = $spice->pivot->amount_in_percent;
|
||||
$amountNeeded = (($percentage / 100) * $totalAmount) * $amount;
|
||||
Spice::findOrFail($spice->id)->decrement('quantity', $amountNeeded);
|
||||
}
|
||||
|
||||
return $records[0];
|
||||
}
|
||||
|
||||
protected function getRedirectUrl(): string
|
||||
{
|
||||
return $this->previousUrl ?? $this->getResource()::getUrl('index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\User\Resources\ProductInventoryResource\Pages;
|
||||
|
||||
use App\Filament\User\Resources\ProductInventoryResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditProductInventory extends EditRecord
|
||||
{
|
||||
protected static string $resource = ProductInventoryResource::class;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\User\Resources\ProductInventoryResource\Pages;
|
||||
|
||||
use App\Filament\User\Resources\ProductInventoryResource;
|
||||
use App\Models\ProductInventory;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ListProductInventory extends ListRecords
|
||||
{
|
||||
protected static string $resource = ProductInventoryResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user