Warenwirtschaft: AP-09 bis AP-13 (Produktbestand, Set-Produkte, Ausschuss, Konzepte)

- AP-09 Produktbestand inkl. Bewegungshistorie (product_stock_movements, ProductStockService)
- AP-10 Rohstoffbestand-Ansicht je Lager (RawMaterialStockController)
- AP-11 Bestandsschwellen / Out-of-Stock-Handling fuer Produkte und Shop
- AP-12 Ausgang/Ausschuss (stock_disposals, StockDisposalController, InventoryService)
- Set-Produkte (product_set_items) inkl. Aufloesung
- Produktentwicklung & Hinweise-Verwaltung (Notices)
- AP-13 Entwicklungskonzept Shop-Bestandsabzug im Plan dokumentiert
- Feature-Tests fuer neue Module + aktualisierter Entwicklungsplan

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Kevin Adametz 2026-06-03 11:04:22 +00:00
parent 78679e0c55
commit 3ee2d756e9
63 changed files with 5968 additions and 901 deletions

View file

@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Illuminate\View\View;
class NoticeController extends Controller
{
public function index(): View
{
$path = resource_path('docs/hinweise.md');
$markdown = File::exists($path) ? File::get($path) : __('Noch keine Hinweise hinterlegt.');
return view('admin.inventory.notices.index', [
'content' => Str::markdown($markdown),
]);
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use Illuminate\View\View;
class ProductDevelopmentController extends Controller
{
public function index(): View
{
return view('admin.inventory.product-development.index');
}
}

View file

@ -0,0 +1,142 @@
<?php
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use App\Http\Requests\Inventory\StoreProductStockMovementRequest;
use App\Models\Product;
use App\Models\ProductStockMovement;
use App\Services\ProductStockService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class ProductStockController extends Controller
{
public function __construct(
protected ProductStockService $productStockService,
) {}
public function index(): View
{
$products = Product::query()
->where('active', true)
->where('is_set', false)
->whereNull('main_product_id')
->with('images')
->orderBy('pos')
->orderBy('name')
->get();
$stock = $this->productStockService->currentStockByProduct($products->pluck('id')->all());
$rows = $products->map(function (Product $product) use ($stock) {
$current = $stock[$product->id] ?? 0;
return [
'product' => $product,
'stock' => $current,
'status' => $this->productStockService->productStatus(
$current,
$product->min_product_stock,
$product->critical_product_stock,
),
];
});
return view('admin.inventory.product-stock.index', [
'rows' => $rows,
'reasons' => $this->manualReasons(),
]);
}
public function storeMovement(StoreProductStockMovementRequest $request, Product $product): RedirectResponse
{
$data = $request->validated();
$this->productStockService->recordMovement(
$product,
$data['direction'],
(int) $data['quantity'],
$data['reason'],
'manual',
$data['note'] ?? null,
(int) $request->user()->id,
);
\Session::flash('alert-save', '1');
return redirect()->route('admin.inventory.product-stock.index');
}
public function history(Request $request): View
{
$query = ProductStockMovement::query()
->with(['product', 'user.account'])
->latest('created_at')
->latest('id');
if (($productId = (int) $request->query('product_id')) > 0) {
$query->where('product_id', $productId);
}
if (in_array($request->query('direction'), ['in', 'out'], true)) {
$query->where('direction', $request->query('direction'));
}
if (($reason = trim((string) $request->query('reason'))) !== '') {
$query->where('reason', $reason);
}
$month = (int) $request->query('month');
$year = (int) $request->query('year');
if ($year > 0) {
$query->whereYear('created_at', $year);
if ($month >= 1 && $month <= 12) {
$query->whereMonth('created_at', $month);
}
}
$movements = $query->limit(500)->get();
return view('admin.inventory.product-stock.history', [
'movements' => $movements,
'products' => Product::query()->orderBy('name')->get(['id', 'name']),
'reasonOptions' => ProductStockMovement::query()->distinct()->orderBy('reason')->pluck('reason')->filter()->values(),
'filters' => [
'product_id' => (int) $request->query('product_id'),
'direction' => $request->query('direction'),
'reason' => $request->query('reason'),
'month' => $month,
'year' => $year,
],
'years' => $this->yearOptions(),
]);
}
/**
* @return array<int, string>
*/
protected function manualReasons(): array
{
return [
__('Initialbestand'),
__('Korrektur'),
__('Inventur'),
__('Retoure'),
__('Testervergabe'),
__('Verlust / Bruch'),
__('Sonstiges'),
];
}
/**
* @return array<int, int>
*/
protected function yearOptions(): array
{
$current = (int) now()->year;
return range($current, $current - 5);
}
}

View file

@ -29,15 +29,18 @@ class ProductionController extends Controller
]);
}
public function create(): View
public function create(Request $request): View
{
$defaultLocationId = Location::query()->where('name', 'like', '%öln%')->value('id')
?? Location::query()->where('active', true)->first()?->id;
$defaultProductId = (int) $request->query('product_id') ?: null;
return view('admin.inventory.productions.create', [
'products' => Product::query()->where('active', 1)->orderBy('name')->get(['id', 'name']),
'products' => Product::query()->where('active', 1)->where('is_set', false)->orderBy('name')->get(['id', 'name']),
'locations' => Location::query()->where('active', true)->orderBy('name')->get(),
'defaultLocationId' => $defaultLocationId,
'defaultProductId' => $defaultProductId,
'model' => null,
]);
}
@ -99,7 +102,8 @@ class ProductionController extends Controller
return view('admin.inventory.productions.edit', [
'model' => $production,
'products' => Product::query()->where('active', 1)
'products' => Product::query()
->where(fn ($q) => $q->where('active', 1)->where('is_set', false))
->orWhere('id', $production->product_id)
->orderBy('name')->get(['id', 'name']),
'locations' => Location::query()->where('active', true)->orderBy('name')->get(),
@ -148,7 +152,7 @@ class ProductionController extends Controller
return view('admin.inventory.productions.create', [
'model' => $production,
'products' => Product::query()->where('active', 1)->orderBy('name')->get(['id', 'name']),
'products' => Product::query()->where('active', 1)->where('is_set', false)->orderBy('name')->get(['id', 'name']),
'locations' => Location::query()->where('active', true)->orderBy('name')->get(),
'defaultLocationId' => $defaultLocationId,
]);
@ -158,12 +162,13 @@ class ProductionController extends Controller
{
$locationId = (int) $request->query('location_id', 0);
$quantity = (int) $request->query('quantity', 1);
$excludeProductionId = (int) $request->query('exclude_production', 0) ?: null;
if ($locationId < 1) {
return response()->json(['message' => __('location_id erforderlich')], 422);
}
return response()->json(
$this->productionService->buildRecipePayload($product, $locationId, max(1, $quantity))
$this->productionService->buildRecipePayload($product, $locationId, max(1, $quantity), $excludeProductionId)
);
}
}

View file

@ -0,0 +1,173 @@
<?php
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use App\Models\Ingredient;
use App\Models\Location;
use App\Models\StockEntry;
use App\Models\TaxRate;
use App\Services\InventoryService;
use App\Services\ProductionService;
use App\Services\ProductStockService;
use Illuminate\View\View;
class RawMaterialStockController extends Controller
{
public function __construct(
protected InventoryService $inventoryService,
protected ProductionService $productionService,
protected ProductStockService $productStockService,
) {}
public function index(): View
{
$ingredients = Ingredient::query()
->where('active', true)
->with('materialQuality')
->orderBy('pos')
->orderBy('name')
->get();
$ids = $ingredients->pluck('id')->all();
$remaining = $this->inventoryService->remainingByIngredient($ids);
$daily = $this->inventoryService->dailyConsumptionByIngredient($ids);
$openOrders = $this->inventoryService->openOrderQuantityByIngredient($ids);
$rows = $ingredients->map(function (Ingredient $ingredient) use ($remaining, $daily, $openOrders) {
$rem = $remaining[$ingredient->id] ?? 0.0;
$perDay = $daily[$ingredient->id] ?? null;
$open = $openOrders[$ingredient->id] ?? 0.0;
$minAlert = $ingredient->min_stock_alert !== null ? (float) $ingredient->min_stock_alert : null;
$leadDays = $ingredient->delivery_time_days !== null ? (int) $ingredient->delivery_time_days : null;
return [
'ingredient' => $ingredient,
'remaining' => $rem,
'daily' => $perDay,
'open_order' => $open,
'days_until_empty' => $this->inventoryService->daysUntilEmpty($rem, $perDay),
'expected_empty' => $this->inventoryService->expectedEmptyDate($rem, $perDay),
'status' => $this->inventoryService->stockStatus($minAlert, $rem, $perDay, $leadDays, $open > 0),
];
});
return view('admin.inventory.raw-material-stock.index', [
'rows' => $rows,
'criticalCount' => $rows->where('status', 'critical')->count(),
'horizonOptions' => $this->horizonOptions(),
'defaultHorizon' => 90,
]);
}
public function show(Ingredient $ingredient): View
{
$ingredient->load([
'materialQuality',
'taxRate',
'suppliers' => fn ($q) => $q->orderByPivot('preferred', 'desc')->orderBy('name'),
'products' => fn ($q) => $q->where('active', true),
]);
$entries = StockEntry::query()
->with(['supplier', 'location'])
->where('status', 'received')
->where('entry_type', 'ingredient')
->where('ingredient_id', $ingredient->id)
->orderByRaw('best_before is null, best_before asc')
->orderBy('id')
->get();
$consumed = $this->productionService->consumedByStockEntry($entries->pluck('id')->all());
$charges = $entries->map(function (StockEntry $entry) use ($consumed) {
$received = $entry->received_quantity !== null ? (float) $entry->received_quantity : 0.0;
$entry->setAttribute('remaining_quantity', round($received - ($consumed[(int) $entry->id] ?? 0.0), 2));
return $entry;
})->filter(fn (StockEntry $entry) => (float) $entry->getAttribute('remaining_quantity') > 0.0)
->values();
$remaining = array_sum(array_map(
fn (StockEntry $entry) => (float) $entry->getAttribute('remaining_quantity'),
$charges->all()
));
$remainingByLocation = $this->inventoryService->remainingByLocationForIngredient($ingredient->id);
$openOrders = StockEntry::query()
->with(['supplier', 'location'])
->where('status', 'pending')
->where('entry_type', 'ingredient')
->where('ingredient_id', $ingredient->id)
->orderBy('ordered_at')
->orderBy('id')
->get();
$openTotal = round((float) $openOrders->sum('ordered_quantity'), 2);
$daily = ($this->inventoryService->dailyConsumptionByIngredient([$ingredient->id]))[$ingredient->id] ?? null;
$minAlert = $ingredient->min_stock_alert !== null ? (float) $ingredient->min_stock_alert : null;
$leadDays = $ingredient->delivery_time_days !== null ? (int) $ingredient->delivery_time_days : null;
$lastPriceBySupplier = $this->lastNetPricePerSupplier($ingredient->id);
$productStock = $this->productStockService->currentStockByProduct($ingredient->products->pluck('id')->all());
return view('admin.inventory.raw-material-stock.show', [
'ingredient' => $ingredient,
'productStock' => $productStock,
'charges' => $charges,
'remaining' => round($remaining, 2),
'remainingByLocation' => $remainingByLocation,
'locations' => Location::query()->where('active', true)->orderBy('name')->get(),
'openOrders' => $openOrders,
'openTotal' => $openTotal,
'daily' => $daily,
'daysUntilEmpty' => $this->inventoryService->daysUntilEmpty($remaining, $daily),
'expectedEmpty' => $this->inventoryService->expectedEmptyDate($remaining, $daily),
'status' => $this->inventoryService->stockStatus($minAlert, $remaining, $daily, $leadDays, $openTotal > 0),
'lastPriceBySupplier' => $lastPriceBySupplier,
'taxRates' => TaxRate::query()->active()->orderBy('pos')->orderBy('name')->get(),
]);
}
/**
* Letzter Netto-kg-Preis je Lieferant für diesen Rohstoff (für die Bestell-/Lieferantenliste).
*
* @return array<int, float> [supplier_id => price_per_kg_net]
*/
protected function lastNetPricePerSupplier(int $ingredientId): array
{
$entries = StockEntry::query()
->where('entry_type', 'ingredient')
->where('ingredient_id', $ingredientId)
->whereNotNull('supplier_id')
->whereNotNull('price_per_kg')
->orderBy('ordered_at', 'desc')
->orderBy('id', 'desc')
->get(['supplier_id', 'price_per_kg']);
$result = [];
foreach ($entries as $entry) {
$supplierId = (int) $entry->supplier_id;
if (! isset($result[$supplierId])) {
$result[$supplierId] = (float) $entry->price_per_kg;
}
}
return $result;
}
/**
* @return array<int, string> [days => label]
*/
protected function horizonOptions(): array
{
return [
30 => __('Verbrauch (nächster Monat)'),
90 => __('Verbrauch (nächste 3 Monate)'),
180 => __('Verbrauch (nächste 6 Monate)'),
365 => __('Verbrauch (nächste 12 Monate)'),
];
}
}

View file

@ -0,0 +1,125 @@
<?php
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use App\Http\Requests\Inventory\StoreStockDisposalRequest;
use App\Models\Ingredient;
use App\Models\Location;
use App\Models\StockDisposal;
use App\Models\StockEntry;
use App\Services\InventoryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class StockDisposalController extends Controller
{
public function __construct(
protected InventoryService $inventoryService,
) {}
public function index(Request $request): View
{
$query = StockDisposal::query()
->with(['ingredient', 'packagingItem', 'location', 'stockEntry', 'user'])
->latest('disposed_at')
->latest('id');
if (in_array($request->query('type'), ['ingredient', 'packaging'], true)) {
$query->where('disposal_type', $request->query('type'));
}
return view('admin.inventory.stock-disposals.index', [
'values' => $query->limit(500)->get(),
'typeFilter' => $request->query('type'),
]);
}
public function create(Request $request): View|RedirectResponse
{
if (! auth()->user()->isAdmin()) {
return redirect()->route('home');
}
$prefill = [
'disposal_type' => 'ingredient',
'ingredient_id' => null,
'ingredient_label' => null,
];
$ingredientId = (int) $request->query('ingredient_id');
if ($ingredientId > 0 && ($ingredient = Ingredient::query()->find($ingredientId))) {
$prefill['ingredient_id'] = $ingredient->id;
$prefill['ingredient_label'] = $ingredient->inci ? $ingredient->name.' ('.$ingredient->inci.')' : $ingredient->name;
}
return view('admin.inventory.stock-disposals.create', [
'locations' => Location::query()->where('active', true)->orderBy('name')->get(),
'reasons' => $this->reasons(),
'prefill' => $prefill,
]);
}
public function store(StoreStockDisposalRequest $request): RedirectResponse
{
$data = $request->validatedPayload();
$data['user_id'] = (int) $request->user()->id;
StockDisposal::query()->create($data);
\Session::flash('alert-save', '1');
return redirect()->route('admin.inventory.stock-disposals.index');
}
public function ingredientCharges(Ingredient $ingredient): JsonResponse
{
$remainingByLocation = $this->inventoryService->remainingByLocationForIngredient($ingredient->id);
$charges = StockEntry::query()
->where('status', 'received')
->where('entry_type', 'ingredient')
->where('ingredient_id', $ingredient->id)
->with('location')
->orderBy('best_before')
->get(['id', 'location_id', 'batch_number', 'best_before', 'received_quantity']);
$results = $charges->map(function (StockEntry $charge) {
$label = $charge->batch_number ? __('Charge').' '.$charge->batch_number : __('Charge #:id', ['id' => $charge->id]);
if ($charge->location) {
$label .= ' · '.$charge->location->name;
}
if ($charge->best_before) {
$label .= ' · MHD '.$charge->best_before->format('d.m.Y');
}
return [
'id' => $charge->id,
'location_id' => $charge->location_id,
'text' => $label,
];
})->values()->all();
return response()->json([
'charges' => $results,
'remaining_by_location' => $remainingByLocation,
]);
}
/**
* @return array<int, string>
*/
protected function reasons(): array
{
return [
__('Bruch / Beschädigung'),
__('Verfall / MHD überschritten'),
__('Qualitätsmangel'),
__('Schwund / Inventurdifferenz'),
__('Muster / Testverbrauch'),
__('Sonstiges'),
];
}
}

View file

@ -32,17 +32,29 @@ class StockEntryController extends Controller
]));
}
public function create(): View|RedirectResponse
public function create(Request $request): View|RedirectResponse
{
if (! auth()->user()->isAdmin()) {
return redirect()->route('home');
}
$model = new StockEntry([
'ordered_at' => now()->toDateString(),
'entry_type' => 'ingredient',
]);
$ingredientId = (int) $request->query('ingredient_id');
if ($ingredientId > 0) {
$ingredient = Ingredient::query()->find($ingredientId);
if ($ingredient) {
$model->entry_type = 'ingredient';
$model->ingredient_id = $ingredient->id;
$model->setRelation('ingredient', $ingredient);
}
}
return view('admin.inventory.stock-entries.create', array_merge($this->formSharedData(), [
'model' => new StockEntry([
'ordered_at' => now()->toDateString(),
'entry_type' => 'ingredient',
]),
'model' => $model,
]));
}

View file

@ -9,6 +9,7 @@ use App\Models\Product;
use App\Models\ProductImage;
use App\Models\ProductIngredient;
use App\Repositories\ProductRepository;
use Illuminate\Database\Eloquent\Collection;
use Request;
use Validator;
@ -47,7 +48,7 @@ class ProductController extends Controller
$model->active = true;
} else {
$model = Product::findOrFail($id);
$model->load(['packagings.packagingMaterial']);
$model->load(['packagings.packagingMaterial', 'setItems']);
}
$country_for_prices = Country::where('own_eur', '=', true)->orWhere('currency', '=', true)->get();
@ -56,11 +57,27 @@ class ProductController extends Controller
'country_for_prices' => $country_for_prices,
'ingredient_catalog' => Ingredient::query()->where('active', true)->with('materialQuality')->orderBy('name')->get(['id', 'name', 'inci', 'effect', 'default_factor', 'material_quality_id']),
'packaging_catalog' => PackagingItem::query()->where('active', true)->with('packagingMaterial')->orderBy('name')->get(),
'set_product_catalog' => $this->setProductCatalog($model),
];
return view('admin.product.edit', $data);
}
/**
* Auswählbare Set-Bestandteile: aktive Einzelprodukte (keine Sets, nicht das Produkt selbst).
*
* @return Collection<int, Product>
*/
protected function setProductCatalog(Product $model)
{
return Product::query()
->where('active', true)
->singleProducts()
->when($model->id, fn ($q) => $q->where('id', '!=', $model->id))
->orderBy('name')
->get(['id', 'name', 'number']);
}
public function store()
{
@ -87,15 +104,18 @@ class ProductController extends Controller
$model = new Product;
} else {
$model = Product::findOrFail($data['id']);
$model->load(['packagings.packagingMaterial']);
$model->load(['packagings.packagingMaterial', 'setItems']);
}
$country_for_prices = Country::where('own_eur', '=', true)->orWhere('currency', '=', true)->get();
$this->validateSetItems($validator, Request::all(), $model);
$data = [
'product' => $model,
'country_for_prices' => $country_for_prices,
'ingredient_catalog' => Ingredient::query()->where('active', true)->with('materialQuality')->orderBy('name')->get(['id', 'name', 'inci', 'effect', 'default_factor', 'material_quality_id']),
'packaging_catalog' => PackagingItem::query()->where('active', true)->with('packagingMaterial')->orderBy('name')->get(),
'set_product_catalog' => $this->setProductCatalog($model),
];
if ($validator->fails()) {
@ -110,6 +130,43 @@ class ProductController extends Controller
}
}
/**
* Set-Validierung: Set braucht mindestens ein Einzelprodukt als Bestandteil
* (keine Sets, nicht das Produkt selbst).
*
* @param array<string, mixed> $data
*/
protected function validateSetItems($validator, array $data, Product $model): void
{
if (! isset($data['is_set'])) {
return;
}
$validator->after(function ($validator) use ($data, $model) {
$componentIds = collect($data['set_component_id'] ?? [])
->map(fn ($id) => (int) $id)
->filter(fn (int $id) => $id > 0 && $id !== (int) $model->id)
->unique()
->values();
if ($componentIds->isEmpty()) {
$validator->errors()->add('set_component_id', __('Ein Set benötigt mindestens ein Einzelprodukt als Bestandteil.'));
return;
}
$components = Product::query()->whereIn('id', $componentIds)->get(['id', 'is_set']);
if ($components->count() !== $componentIds->count()) {
$validator->errors()->add('set_component_id', __('Mindestens ein gewählter Bestandteil existiert nicht.'));
}
if ($components->where('is_set', true)->isNotEmpty()) {
$validator->errors()->add('set_component_id', __('Set-Bestandteile dürfen selbst keine Sets sein.'));
}
});
}
public function copy($id)
{
$model = Product::findOrFail($id);

View file

@ -1,54 +1,54 @@
<?php
namespace App\Http\Controllers\User;
use Auth;
use Yard;
use Request;
use App\User;
use Validator;
use App\Services\Shop;
use App\Services\Util;
use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\UserShop;
use App\Services\Payment;
use App\Models\ProductBuy;
use App\Models\UserHistory;
use App\Models\ShoppingUser;
use App\Models\ShoppingOrder;
use App\Services\UserService;
use App\Models\ProductCategory;
use App\Models\Setting;
use App\Models\ShippingCountry;
use App\Models\ShoppingInstance;
use App\Http\Controllers\Controller;
use App\Models\UserHistory;
use App\Services\MyLog;
use App\Services\Payment;
use App\Services\Shop;
use App\Services\UserService;
use App\Services\Util;
use App\User;
use Auth;
use Request;
use Validator;
use Yard;
class OrderController extends Controller
{
public function __construct()
{
$this->middleware('active.account');
}
public function delivery($for, $id=null)
public function delivery($for, $id = null)
{
$user = User::find(\Auth::user()->id);
$user = User::find(Auth::user()->id);
$shopping_user = null;
$delivery_id = null;
if(strpos($for, 'ot') !== false){
if (strpos($for, 'ot') !== false) {
$shopping_user = Shop::checkShoppingUser($id, $user);
$delivery_id = $shopping_user->id;
if(!Shop::checkShoppingCountry($for, $delivery_id) && !\Session()->has('custom-error')){
if (! Shop::checkShoppingCountry($for, $delivery_id) && ! \Session()->has('custom-error')) {
\Session()->flash('custom-error', __('validation.custom.shipping_not_found'));
return redirect(route('user_order_my_delivery', [$for, $delivery_id]));
}
}
if(Request::get('action') === 'next'){
if (Request::get('action') === 'next') {
Yard::instance('shopping')->destroy();
if(strpos(Request::get('switchers-radio-is-for'), 'ot') !== false){
if (strpos(Request::get('switchers-radio-is-for'), 'ot') !== false) {
$delivery_id = $id;
}
return redirect(route('user_order_my_list', [Request::get('switchers-radio-is-for'), $delivery_id]));
}
$data = [
@ -58,39 +58,41 @@ class OrderController extends Controller
'for' => $for,
'delivery_id' => $delivery_id,
];
return view('user.order.delivery', $data);
}
public function list($for, $id=null)
public function list($for, $id = null)
{
$user = User::find(\Auth::user()->id);
$user = User::find(Auth::user()->id);
$shopping_user = null;
$delivery_id = null;
if(strpos($for, 'ot') !== false){
if (strpos($for, 'ot') !== false) {
$shopping_user = Shop::checkShoppingUser($id, $user);
$delivery_id = $shopping_user->id;
}
if($for === 'ot-customer'){ //noch nicht implementiert
//Liederung an ot-customer (Kunden) Zahlung und Rechnung geht an Kunden
if ($for === 'ot-customer') { // noch nicht implementiert
// Liederung an ot-customer (Kunden) Zahlung und Rechnung geht an Kunden
UserService::initCustomerYard($shopping_user, $for);
}else{
//Lieferung an user oder ot-member (Kunden) rechnung geht an User
//lieferland und rechnungsland prüfen
$shipping_country_id = Shop::checkShoppingCountry($for, $id);
if(!$shipping_country_id){
} else {
// Lieferung an user oder ot-member (Kunden) rechnung geht an User
// lieferland und rechnungsland prüfen
$shipping_country_id = Shop::checkShoppingCountry($for, $id);
if (! $shipping_country_id) {
\Session()->flash('custom-error', __('validation.custom.shipping_not_found'));
return redirect(route('user_order_my_delivery', [$for, $delivery_id]));
}
UserService::initUserYard($user, $shipping_country_id, $for);
}
if($for === 'cr'){
if ($for === 'cr') {
Yard::instance('shopping')->setGlobalTaxRate(0);
Yard::instance('shopping')->setShoppingUser($user, false);
}else{
} else {
Yard::instance('shopping')->setShoppingUser($user, true);
}
$data = [
@ -102,15 +104,17 @@ class OrderController extends Controller
'delivery_id' => $delivery_id,
'comp_products' => $this->getCompProducts($for),
];
return view('user.order.list', $data);
}
public function payment($for, $id=null){
public function payment($for, $id = null)
{
$data = Request::all();
$user = User::find(Auth::user()->id);
$rules = array(
$rules = [
'shipping_salutation' => 'required',
'shipping_firstname' => 'required',
'shipping_lastname' => 'required',
@ -118,25 +122,25 @@ class OrderController extends Controller
'shipping_zipcode' => 'required',
'shipping_city' => 'required',
'shipping_state' => 'required',
);
];
$validator = Validator::make(Request::all(), $rules);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput(Request::all());
}
//hier prüfen, ob versand etc richtig berechnet wurde
$this->checkSendYardForPayment($data, $id);
// hier prüfen, ob versand etc richtig berechnet wurde
$this->checkSendYardForPayment($data, $id);
if(Yard::instance('shopping')->getNumComp() > 0){
if(!isset($data['switchers-comp-product'])){
if (Yard::instance('shopping')->getNumComp() > 0) {
if (! isset($data['switchers-comp-product'])) {
$validator->errors()->add('switchers-comp-product', __('Bitte wähle ein Kompensationsprodukt aus'));
}else{
if(!is_array($data['switchers-comp-product'])){
} else {
if (! is_array($data['switchers-comp-product'])) {
$validator->errors()->add('switchers-comp-product', __('Bitte wähle ein Kompensationsprodukt aus'));
}else{
if(count($data['switchers-comp-product']) !== Yard::instance('shopping')->getNumComp()){
$validator->errors()->add('switchers-comp-product', __('Bitte wähle :count Kompensationsprodukte aus', ['count'=>Yard::instance('shopping')->getNumComp()]));
} else {
if (count($data['switchers-comp-product']) !== Yard::instance('shopping')->getNumComp()) {
$validator->errors()->add('switchers-comp-product', __('Bitte wähle :count Kompensationsprodukte aus', ['count' => Yard::instance('shopping')->getNumComp()]));
}
}
}
@ -144,7 +148,7 @@ class OrderController extends Controller
return back()->withErrors($validator)->withInput(Request::all());
}
}
/*
do {
$identifier = Util::getToken();
@ -173,118 +177,119 @@ class OrderController extends Controller
]);
Yard::instance('shopping')->store($identifier);
*/
//add to DB
//$path = route('checkout.checkout_card', ['identifier'=>$identifier]);
UserHistory::create(['user_id' => $user->id, 'action'=>'user_order_payment', 'status'=>1, 'product_id'=>null, 'identifier'=>$identifier]);
//$path = str_replace('http', 'https', $path);
//return redirect()->secure($path);
// add to DB
// $path = route('checkout.checkout_card', ['identifier'=>$identifier]);
UserHistory::create(['user_id' => $user->id, 'action' => 'user_order_payment', 'status' => 1, 'product_id' => null, 'identifier' => $identifier]);
// $path = str_replace('http', 'https', $path);
// return redirect()->secure($path);
return redirect(route('user_checkout', [$identifier]));
}
private function checkSendYardForPayment($data, $id){
private function checkSendYardForPayment($data, $id)
{
$user = User::find(\Auth::user()->id);
$user = User::find(Auth::user()->id);
$shopping_user = null;
if(strpos($data['shipping_is_for'], 'ot') !== false){
if (strpos($data['shipping_is_for'], 'ot') !== false) {
$shopping_user = Shop::checkShoppingUser($id, $user);
}
$shipping_country_id = Shop::checkShoppingCountry($data['shipping_is_for'], $id);
if(!$shipping_country_id){
if (! $shipping_country_id) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'no shipping_country_id found | Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'no shipping_country_id found | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shipping_country_was_not_found'));
}
//must be the same shipping country
if($shipping_country_id != Yard::instance('shopping')->getShippingCountryId()){
// must be the same shipping country
if ($shipping_country_id != Yard::instance('shopping')->getShippingCountryId()) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'shipping_country_id is not the same from Yard | Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'shipping_country_id is not the same from Yard | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shipping_country_was_not_correctly'));
}
if($data['shipping_is_for'] !== 'ot-customer'){
if(Yard::instance('shopping')->shipping_free){
if ($data['shipping_is_for'] !== 'ot-customer') {
if (Yard::instance('shopping')->shipping_free) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'Yard can by not shipping_free | Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'Yard can by not shipping_free | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shopping_cart_was_shipping_free'));
}
}
if($data['shipping_is_for'] === 'ot-customer'){
if(!$user->shop){
if ($data['shipping_is_for'] === 'ot-customer') {
if (! $user->shop) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'User has no Shop for an User to Customer order| Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'User has no Shop for an User to Customer order| Yard identifier: '.$identifier, $data);
abort(403, __('msg.shopping_cart_was_not_user_shop'));
}
}
$shipping_price = Shop::getShippingPriceByShippingCountryId($shipping_country_id, Yard::instance('shopping')->weight());
//for other and has weight - check
if(strpos($data['shipping_is_for'], 'ot') !== false && $data['shipping_is_for'] !== 'ot-customer' && Yard::instance('shopping')->weight() > 0){
if(!Yard::instance('shopping')->getShippingPrice() || Yard::instance('shopping')->getShippingPrice() == 0){
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'Yard OT shipping_price is 0 or | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shipping_cost_cannot_be_0'));
}
if(Yard::instance('shopping')->getShippingPrice() != $shipping_price->price){
// for other and has weight - check
if (strpos($data['shipping_is_for'], 'ot') !== false && $data['shipping_is_for'] !== 'ot-customer' && Yard::instance('shopping')->weight() > 0) {
if (! Yard::instance('shopping')->getShippingPrice() || Yard::instance('shopping')->getShippingPrice() == 0) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'Yard OT shipping_price is not the same from shipping_price | Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'Yard OT shipping_price is 0 or | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shipping_cost_cannot_be_0'));
}
if (Yard::instance('shopping')->getShippingPrice() != $shipping_price->price) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
MyLog::writeLog('payment', 'error', 'Yard OT shipping_price is not the same from shipping_price | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shipping_costs_were_not_calculated_correctly'));
}
}
if($data['shipping_is_for'] == 'me' && Yard::instance('shopping')->weight() > 0){
if(!Yard::instance('shopping')->getShippingPrice() || Yard::instance('shopping')->getShippingPrice() == 0){
if ($data['shipping_is_for'] == 'me' && Yard::instance('shopping')->weight() > 0) {
if (! Yard::instance('shopping')->getShippingPrice() || Yard::instance('shopping')->getShippingPrice() == 0) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'Yard ME shipping_price is 0 or | Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'Yard ME shipping_price is 0 or | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shipping_cost_cannot_be_0'));
}
if(Yard::instance('shopping')->getShippingPrice() != $shipping_price->price_comp){
if (Yard::instance('shopping')->getShippingPrice() != $shipping_price->price_comp) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'Yard ME shipping_price is not the same from shipping_price | Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'Yard ME shipping_price is not the same from shipping_price | Yard identifier: '.$identifier, $data);
abort(403, __('msg.shipping_costs_were_not_calculated_correctly'));
}
if(Yard::instance('shopping')->getNumComp() != $shipping_price->num_comp){
if (Yard::instance('shopping')->getNumComp() != $shipping_price->num_comp) {
$identifier = 'error-'.time().mt_rand(1000000, 9999999);
Yard::instance('shopping')->store($identifier);
$data['user_id'] = Auth::user()->id;
$data['shopping_user_id'] = $id;
\App\Services\MyLog::writeLog('payment', 'error', 'Yard num_comp is 0 | Yard identifier: '.$identifier, $data);
MyLog::writeLog('payment', 'error', 'Yard num_comp is 0 | Yard identifier: '.$identifier, $data);
abort(403, __('msg.compensation_products_cannot_be_0'));
}
}
}
public function datatable(){
public function datatable()
{
$not_show_pids = ProductBuy::getNotShowProductIDs(Auth::user()->id);
@ -294,7 +299,7 @@ class OrderController extends Controller
break;
case 'mp':
$query = Product::select('products.*')->where('active', true)->whereJsonContains('show_on', '2');
break;
break;
case 'cr':
$query = Product::select('products.*')->where('active', true)->whereJsonContains('show_on', '6');
break;
@ -302,9 +307,8 @@ class OrderController extends Controller
$query = Product::select('products.*')->where('active', true)->whereJsonContains('show_on', '1');
break;
}
foreach($not_show_pids as $not_show_pid){
foreach ($not_show_pids as $not_show_pid) {
$query->where('id', '!=', $not_show_pid);
}
@ -314,7 +318,11 @@ class OrderController extends Controller
$cartItem = Yard::instance('shopping')->getCartItemByProduct($product->id);
$qty = isset($cartItem->qty) ? $cartItem->qty : 0;
$rowId = isset($cartItem->rowId) ? $cartItem->rowId : '';
return '<strong>'.$product->name.'</strong><br><div class="no-line-break input-group-min-w">
if ($product->isOutOfStock()) {
$controls = '<div class="product-stock-hint">'.e($product->outOfStockNotice()).'</div>';
} else {
$controls = '<div class="no-line-break input-group-min-w">
<div class="input-group d-inline-flex w-auto">
<span class="input-group-prepend">
<button type="button" class="btn btn-secondary icon-btn md-btn-extra remove-product-basket" data-row-id="'.$rowId.'" data-product-id="'.$product->id.'">-</button>
@ -325,6 +333,9 @@ class OrderController extends Controller
</span>
</div>
</div>';
}
return '<strong>'.$product->name.'</strong><br>'.$controls;
})
/*
->addColumn('add_card', function (Product $product) {
@ -349,32 +360,33 @@ class OrderController extends Controller
</div>';
})*/
->addColumn('category', function (Product $product) {
$ret = "";
foreach($product->categories as $category){
$ret = '';
foreach ($product->categories as $category) {
$ret .= '<span><div style="white-space: nowrap">'.$category->category->name.'</div></span>';
}
return $ret;
})
->addColumn('picture', function (Product $product) {
if(count($product->images)){
if (count($product->images)) {
return '<a href="" class="" data-modal="modal-lg" data-toggle="modal" data-target="#modals-load-content" data-id="'.$product->id.'" data-route="'.route('modal_load').'"
data-action="user-order-show-product" data-view="customer">
<img class="img-fluid img-extra" alt="" src="'.route('product_image', [$product->images->first()->slug]).'">
<div class="text-center"><i class="ion ion-md-eye"></i></div></a>';
}
return "";
return '';
})
->addColumn('price_net', function (Product $product) {
return $product->getFormattedPriceWith(true, false). "";
return $product->getFormattedPriceWith(true, false).'€';
})
->addColumn('price_gross', function (Product $product) {
return $product->getFormattedPriceWith(false, false). "";
return $product->getFormattedPriceWith(false, false).'€';
})
->addColumn('price_vk_gross', function (Product $product) {
return $product->getFormattedPriceWith(false, false). "";
return $product->getFormattedPriceWith(false, false).'€';
})
->addColumn('single_commission', function (Product $product) {
return $product->single_commission ? '<span class="badge badge-warning">Handelspanne: '.$product->getFormattedValueCommission().' %</span>' : '<span class="badge badge-primary">Staffelprovision</span> <button class="btn btn-default btn-xs icon-btn md-btn-flat product-tooltip" title="details" data-modal="modal-lg"
@ -386,8 +398,8 @@ class OrderController extends Controller
data-toggle="modal" data-target="#modals-load-content" data-id="'.$product->id.'" data-route="'.route('modal_load').'"
data-action="user-order-show-product" data-view="customer"><i class="ion ion-md-eye"></i></button>';
})
->filterColumn('product', function($query, $keyword) {
if($keyword != ""){
->filterColumn('product', function ($query, $keyword) {
if ($keyword != '') {
$query->where('name', 'LIKE', '%'.$keyword.'%');
}
})
@ -414,32 +426,32 @@ class OrderController extends Controller
, $order);
})*/
->rawColumns(['add_card', 'category', 'product', 'quantity', 'picture', 'action', 'single_commission'])
->make(true);
}
public function performRequest(){
public function performRequest()
{
if(Request::ajax()) {
if (Request::ajax()) {
$data = Request::all();
$is_for = isset($data['shipping_is_for']) ? $data['shipping_is_for'] : 'ot';
$data['comp_products'] = $this->getCompProducts($is_for);
if($data['action'] === 'updateCart' && isset($data['product_id'])){
if($product = Product::find($data['product_id'])){
$image = "";
if($product->images->count()){
if ($data['action'] === 'updateCart' && isset($data['product_id'])) {
if ($product = Product::find($data['product_id'])) {
$image = '';
if ($product->images->count()) {
$image = $product->images->first()->slug;
}
//get the card item
// get the card item
// Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->price, $product->tax, ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight]);
//Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->price, $product->tax, ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight]);
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->price, $product->tax,
//$product->getPriceWith(Yard::instance('shopping')->getUserTaxFree(), true, Yard::instance('shopping')->getUserCountry()), $product->getTaxWith(Yard::instance('shopping')->getUserCountry()), //$product->tax, true?
// $product->getPriceWith(Yard::instance('shopping')->getUserTaxFree(), true, Yard::instance('shopping')->getUserCountry()), $product->getTaxWith(Yard::instance('shopping')->getUserCountry()), //$product->tax, true?
[
'image' => $image,
'slug' => $product->slug,
@ -450,140 +462,144 @@ class OrderController extends Controller
'partner_commission' => $product->partner_commission,
]);
if(Yard::instance('shopping')->getUserTaxFree()){
//Yard::setTax($cartItem->rowId, 0);
Yard::instance('shopping')->setGlobalTaxRate(0);
}else{
//Yard::setTax($cartItem->rowId, $product->getTaxWith(Yard::instance('shopping')->getUserCountry()));
}
if (Yard::instance('shopping')->getUserTaxFree()) {
// Yard::setTax($cartItem->rowId, 0);
Yard::instance('shopping')->setGlobalTaxRate(0);
} else {
// Yard::setTax($cartItem->rowId, $product->getTaxWith(Yard::instance('shopping')->getUserCountry()));
}
if(isset($data['qty']) && $data['qty'] > 0){
if (isset($data['qty']) && $data['qty'] > 0) {
Yard::instance('shopping')->update($cartItem->rowId, $data['qty']);
}else{
//if 0 get the item by qty:1 and remove it
} else {
// if 0 get the item by qty:1 and remove it
Yard::instance('shopping')->remove($cartItem->rowId);
}
//
Yard::instance('shopping')->reCalculate();
$this->checkCompProduct(Yard::instance('shopping')->getNumComp());
$html_card = view("user.order.yard_view_form", $data)->render();
$html_comp = view("user.order.comp_product", $data)->render();
$html_card = view('user.order.yard_view_form', $data)->render();
$html_comp = view('user.order.comp_product', $data)->render();
return response()->json(['response' => true, 'data'=>$data, 'html_card'=>$html_card, 'html_comp'=>$html_comp]);
return response()->json(['response' => true, 'data' => $data, 'html_card' => $html_card, 'html_comp' => $html_comp]);
}
}
if($data['action'] === 'reCalculateCart') {
//set use_payment_credit
$data['reduce_payment_credit'] = $data['reduce_payment_credit'] == 'true' ? true: false;
if ($data['action'] === 'reCalculateCart') {
// set use_payment_credit
$data['reduce_payment_credit'] = $data['reduce_payment_credit'] == 'true' ? true : false;
Yard::instance('shopping')->setReducePaymentCredit($data['reduce_payment_credit']);
Yard::instance('shopping')->reCalculate();
$html_card = view("user.order.yard_view_form", $data)->render();
$html_comp = view("user.order.comp_product", $data)->render();
return response()->json(['response' => true, 'data'=>$data, 'html_card'=>$html_card, 'html_comp'=>$html_comp]);
$html_card = view('user.order.yard_view_form', $data)->render();
$html_comp = view('user.order.comp_product', $data)->render();
return response()->json(['response' => true, 'data' => $data, 'html_card' => $html_card, 'html_comp' => $html_comp]);
}
if($data['action'] === 'clearCart') {
if ($data['action'] === 'clearCart') {
Yard::instance('shopping')->destroy();
return response()->json(['response' => true, 'data'=>Yard::instance('shopping')->count(), 'html_card'=>'', 'html_comp'=>'']);
return response()->json(['response' => true, 'data' => Yard::instance('shopping')->count(), 'html_card' => '', 'html_comp' => '']);
}
if($data['action'] === 'updateShippingCountry') {
if(isset($data['shipping_country_id'])){
if($shipping_country = ShippingCountry::find($data['shipping_country_id'])){
Yard::instance('shopping')->setShippingCountryWithPrice($shipping_country->id, $is_for); //$is_for == 'ot' or 'me'
if ($data['action'] === 'updateShippingCountry') {
if (isset($data['shipping_country_id'])) {
if ($shipping_country = ShippingCountry::find($data['shipping_country_id'])) {
Yard::instance('shopping')->setShippingCountryWithPrice($shipping_country->id, $is_for); // $is_for == 'ot' or 'me'
$this->checkCompProduct(Yard::instance('shopping')->getNumComp());
}
}
$html_card = view("user.order.yard_view_form", $data)->render();
$html_comp = view("user.order.comp_product", $data)->render();
return response()->json(['response' => true, 'data'=>$data, 'html_card'=>$html_card, 'html_comp'=>$html_comp]);
$html_card = view('user.order.yard_view_form', $data)->render();
$html_comp = view('user.order.comp_product', $data)->render();
return response()->json(['response' => true, 'data' => $data, 'html_card' => $html_card, 'html_comp' => $html_comp]);
}
if($data['action'] === 'updateCompProduct'){
if ($data['action'] === 'updateCompProduct') {
// $data['comp_product_id']
// $data['comp_num']
//count_comp_products
$this->updateCompProduct($data);
Yard::instance('shopping')->reCalculateShippingPrice();
$html_card = view("user.order.yard_view_form", $data)->render();
$html_comp = view("user.order.comp_product", $data)->render();
// count_comp_products
$this->updateCompProduct($data);
Yard::instance('shopping')->reCalculateShippingPrice();
$html_card = view('user.order.yard_view_form', $data)->render();
$html_comp = view('user.order.comp_product', $data)->render();
return response()->json(['response' => true, 'data'=>$data, 'html_card'=>$html_card, 'html_comp'=>$html_comp]);
return response()->json(['response' => true, 'data' => $data, 'html_card' => $html_card, 'html_comp' => $html_comp]);
}
return response()->json(['response' => false, 'data'=>$data]);
}
return response()->json(['response' => false, 'data' => $data]);
}
}
private function checkCompProduct($count_comp_products){
private function checkCompProduct($count_comp_products)
{
foreach (Yard::instance('shopping')->content() as $row) {
//wenn gleich löschen, da neue Versandkosten
if($row->options->comp > $count_comp_products) {
// wenn gleich löschen, da neue Versandkosten
if ($row->options->comp > $count_comp_products) {
Yard::instance('shopping')->remove($row->rowId);
}
}
}
private function updateCompProduct($data){
//clear old
foreach (Yard::instance('shopping')->content() as $row) {
//wenn kleiner wurde ein produkt entfernt aufgrund der Anzahl
//wenn gleich löschen, da neue Versandkosten
if($row->options->comp === $data['comp_num'] || $row->options->comp > $data['count_comp_products']) {
private function updateCompProduct($data)
{
// clear old
foreach (Yard::instance('shopping')->content() as $row) {
// wenn kleiner wurde ein produkt entfernt aufgrund der Anzahl
// wenn gleich löschen, da neue Versandkosten
if ($row->options->comp === $data['comp_num'] || $row->options->comp > $data['count_comp_products']) {
Yard::instance('shopping')->remove($row->rowId);
}
}
}
}
if(isset($data['comp_product_id'])) {
if (isset($data['comp_product_id'])) {
if ($product = Product::find($data['comp_product_id'])) {
$image = "";
$image = '';
if ($product->images->count()) {
$image = $product->images->first()->slug;
}
$cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, 0, 0,
[
'image' => $image,
'slug' => $product->slug,
'weight' => 0,
'single_commission' => 0,
'amount_commission' => 0,
'value_commission' => 0,
'partner_commission' => 0,
'comp' => $data['comp_num'],
'product_id' => $product->id
]);
[
'image' => $image,
'slug' => $product->slug,
'weight' => 0,
'single_commission' => 0,
'amount_commission' => 0,
'value_commission' => 0,
'partner_commission' => 0,
'comp' => $data['comp_num'],
'product_id' => $product->id,
]);
Yard::setTax($cartItem->rowId, 0);
}
}
}
private function getCompProducts($for) {
if($for === 'me' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_me')) {
private function getCompProducts($for)
{
if ($for === 'me' && Setting::getContentBySlug('order_partner_is_comp_me')) {
return Product::whereActive(true)
->where(function($query) {
$query->whereRaw("JSON_CONTAINS(show_on, '\"2\"')")
->orWhereRaw("JSON_CONTAINS(show_on, '\"11\"')");
})
->where('shipping_addon', true)
->orderBy('pos', 'DESC')
->get();
->where(function ($query) {
$query->whereRaw("JSON_CONTAINS(show_on, '\"2\"')")
->orWhereRaw("JSON_CONTAINS(show_on, '\"11\"')");
})
->where('shipping_addon', true)
->orderBy('pos', 'DESC')
->get();
}
if($for === 'ot' && \App\Models\Setting::getContentBySlug('order_partner_is_comp_ot')) {
if ($for === 'ot' && Setting::getContentBySlug('order_partner_is_comp_ot')) {
return Product::whereActive(true)
->where(function($query) {
$query->whereRaw("JSON_CONTAINS(show_on, '\"1\"')")
->orWhereRaw("JSON_CONTAINS(show_on, '\"11\"')");
})
->where('shipping_addon', true)
->orderBy('pos', 'DESC')
->get();
->where(function ($query) {
$query->whereRaw("JSON_CONTAINS(show_on, '\"1\"')")
->orWhereRaw("JSON_CONTAINS(show_on, '\"11\"')");
})
->where('shipping_addon', true)
->orderBy('pos', 'DESC')
->get();
}
return null;
}
}
}

View file

@ -0,0 +1,39 @@
<?php
namespace App\Http\Requests\Inventory;
use Illuminate\Foundation\Http\FormRequest;
class StoreProductStockMovementRequest extends FormRequest
{
public function authorize(): bool
{
return (bool) $this->user()?->isAdmin();
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'direction' => ['required', 'in:in,out'],
'quantity' => ['required', 'integer', 'min:1'],
'reason' => ['required', 'string', 'max:100'],
'note' => ['nullable', 'string', 'max:255'],
];
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'direction.required' => __('Bitte Eingang oder Ausgang wählen.'),
'quantity.required' => __('Bitte eine Stückzahl angeben.'),
'quantity.min' => __('Die Stückzahl muss mindestens 1 sein.'),
'reason.required' => __('Bitte einen Grund angeben.'),
];
}
}

View file

@ -2,6 +2,7 @@
namespace App\Http\Requests\Inventory;
use App\Models\Product;
use Illuminate\Foundation\Http\FormRequest;
class StoreProductionRequest extends FormRequest
@ -16,19 +17,28 @@ class StoreProductionRequest extends FormRequest
*/
public function rules(): array
{
$recipeRequired = $this->recipeRequired();
return [
'product_id' => ['required', 'integer', 'exists:products,id'],
'location_id' => ['required', 'integer', 'exists:locations,id'],
'produced_at' => ['required', 'date'],
'quantity' => ['required', 'integer', 'min:1'],
'notes' => ['nullable', 'string', 'max:2000'],
'ingredient_lines' => ['required', 'array', 'min:1'],
'ingredient_lines' => [$recipeRequired ? 'required' : 'nullable', 'array', $recipeRequired ? 'min:1' : 'min:0'],
'ingredient_lines.*.ingredient_id' => ['required', 'integer', 'exists:ingredients,id'],
'ingredient_lines.*.stock_entry_id' => ['required', 'integer', 'exists:stock_entries,id'],
'ingredient_lines.*.quantity_used' => ['required', 'string'],
];
}
private function recipeRequired(): bool
{
$product = Product::query()->find($this->input('product_id'));
return $product === null ? true : ! (bool) $product->no_recipe_required;
}
/**
* @return array<string, mixed>
*/
@ -48,7 +58,7 @@ class StoreProductionRequest extends FormRequest
'stock_entry_id' => (int) $line['stock_entry_id'],
'quantity_used' => $line['quantity_used'],
];
}, $data['ingredient_lines']),
}, $data['ingredient_lines'] ?? []),
];
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace App\Http\Requests\Inventory;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class StoreStockDisposalRequest extends FormRequest
{
public function authorize(): bool
{
return (bool) $this->user()?->isAdmin();
}
/**
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [
'disposal_type' => ['required', Rule::in(['ingredient', 'packaging'])],
'ingredient_id' => ['nullable', 'integer', 'exists:ingredients,id'],
'packaging_item_id' => ['nullable', 'integer', 'exists:packaging_items,id'],
'stock_entry_id' => ['nullable', 'integer', 'exists:stock_entries,id'],
'location_id' => ['required', 'integer', 'exists:locations,id'],
'quantity' => ['required', 'numeric', 'min:0.01'],
'reason' => ['required', 'string', 'max:100'],
'note' => ['nullable', 'string', 'max:255'],
'disposed_at' => ['required', 'date'],
];
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'location_id.required' => __('Bitte einen Lagerort wählen.'),
'quantity.required' => __('Bitte eine Menge angeben.'),
'quantity.min' => __('Die Menge muss größer als 0 sein.'),
'reason.required' => __('Bitte einen Grund angeben.'),
'disposed_at.required' => __('Bitte ein Datum angeben.'),
];
}
protected function prepareForValidation(): void
{
$disposedAt = $this->input('disposed_at');
if (is_string($disposedAt) && preg_match('/^\d{2}\.\d{2}\.\d{4}$/', trim($disposedAt))) {
$disposedAt = Carbon::createFromFormat('d.m.Y', trim($disposedAt))->format('Y-m-d');
}
$this->merge([
'quantity' => reFormatNumber($this->input('quantity')),
'disposed_at' => $disposedAt,
]);
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
if ($this->input('disposal_type') === 'ingredient') {
if (empty($this->input('ingredient_id'))) {
$validator->errors()->add('ingredient_id', __('Bitte einen Rohstoff wählen.'));
}
} elseif ($this->input('disposal_type') === 'packaging') {
if (empty($this->input('packaging_item_id'))) {
$validator->errors()->add('packaging_item_id', __('Bitte einen Verpackungsartikel wählen.'));
}
}
});
}
/**
* @return array<string, mixed>
*/
public function validatedPayload(): array
{
$data = $this->validated();
if (($data['disposal_type'] ?? '') === 'ingredient') {
$data['packaging_item_id'] = null;
$data['unit'] = 'gram';
} else {
$data['ingredient_id'] = null;
$data['stock_entry_id'] = null;
$data['unit'] = 'piece';
}
return $data;
}
}

View file

@ -5,8 +5,10 @@ namespace App\Models;
use App\Services\Type;
use App\Services\Util;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
@ -200,6 +202,14 @@ class Product extends Model
'max_buy' => 'bool',
'max_buy_num' => 'int',
'whitelabel' => 'bool',
'no_recipe_required' => 'bool',
'is_set' => 'bool',
'main_product_id' => 'int',
'main_product_quantity' => 'int',
'out_of_stock_until' => 'date',
'out_of_stock_indefinite' => 'bool',
'min_product_stock' => 'int',
'critical_product_stock' => 'int',
];
use Sluggable;
@ -247,6 +257,14 @@ class Product extends Model
'max_buy_num',
'shelf_life_type',
'shelf_life_months',
'no_recipe_required',
'is_set',
'main_product_id',
'main_product_quantity',
'out_of_stock_until',
'out_of_stock_indefinite',
'min_product_stock',
'critical_product_stock',
];
@ -369,6 +387,94 @@ class Product extends Model
return $this->hasMany(Production::class);
}
/**
* Produktbestands-Bewegungen (Eingang/Ausgang).
*
* @return HasMany<ProductStockMovement, $this>
*/
public function stockMovements(): HasMany
{
return $this->hasMany(ProductStockMovement::class);
}
/**
* Set-Bestandteile (Einzelprodukte) dieses Sets mit Menge und Reihenfolge.
*
* @return BelongsToMany<Product, $this>
*/
public function setItems(): BelongsToMany
{
return $this->belongsToMany(Product::class, 'product_set_items', 'set_product_id', 'component_product_id')
->withPivot('quantity', 'pos')
->withTimestamps()
->orderByPivot('pos');
}
/**
* Sets, in denen dieses Produkt als Bestandteil enthalten ist.
*
* @return BelongsToMany<Product, $this>
*/
public function partOfSets(): BelongsToMany
{
return $this->belongsToMany(Product::class, 'product_set_items', 'component_product_id', 'set_product_id')
->withPivot('quantity', 'pos')
->withTimestamps();
}
/**
* Übergeordnetes Hauptprodukt (z. B. „50 × 15 ml").
*
* @return BelongsTo<Product, $this>
*/
public function mainProduct(): BelongsTo
{
return $this->belongsTo(Product::class, 'main_product_id');
}
/**
* Untergeordnete Varianten, die auf dieses Produkt als Hauptprodukt zeigen.
*
* @return HasMany<Product, $this>
*/
public function variants(): HasMany
{
return $this->hasMany(Product::class, 'main_product_id');
}
/**
* Nur Einzelprodukte (keine Sets).
*
* @param Builder<Product> $query
* @return Builder<Product>
*/
public function scopeSingleProducts(Builder $query): Builder
{
return $query->where('is_set', false);
}
/**
* Sets.
*
* @param Builder<Product> $query
* @return Builder<Product>
*/
public function scopeSets(Builder $query): Builder
{
return $query->where('is_set', true);
}
/**
* Haupt-/Einzelprodukte, die keinem übergeordneten Hauptprodukt zugeordnet sind.
*
* @param Builder<Product> $query
* @return Builder<Product>
*/
public function scopeMainProducts(Builder $query): Builder
{
return $query->whereNull('main_product_id');
}
public function getShortCopy($clean = false, $len = false)
{
$ret = $this->short_copy ? $this->short_copy : $this->description;
@ -437,6 +543,53 @@ class Product extends Model
return isset($this->attributes['price']) ? Util::formatNumber($this->attributes['price']) : '';
}
public function isOutOfStock(): bool
{
if ($this->out_of_stock_indefinite) {
return true;
}
return $this->out_of_stock_until !== null && $this->out_of_stock_until->endOfDay()->isFuture();
}
public function outOfStockRemainingDays(): ?int
{
if ($this->out_of_stock_indefinite || $this->out_of_stock_until === null) {
return null;
}
if (! $this->out_of_stock_until->endOfDay()->isFuture()) {
return null;
}
$diffSeconds = $this->out_of_stock_until->copy()->startOfDay()->getTimestamp() - now()->startOfDay()->getTimestamp();
return (int) max(0, (int) round($diffSeconds / 86400));
}
public function outOfStockNotice(): ?string
{
if ($this->out_of_stock_indefinite) {
return __('Zur Zeit nicht vorrätig');
}
$days = $this->outOfStockRemainingDays();
if ($days === null) {
return null;
}
if ($days <= 0) {
return __('In Kürze wieder verfügbar');
}
if ($days === 1) {
return __('In ca. 1 Tag wieder da!');
}
return __('In ca. :days Tagen wieder da!', ['days' => $days]);
}
public function getFormattedPriceEk()
{
return isset($this->attributes['price_ek']) ? Util::formatNumber($this->attributes['price_ek']) : '';

View file

@ -0,0 +1,70 @@
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class ProductStockMovement extends Model
{
protected $fillable = [
'product_id',
'direction',
'quantity',
'reason',
'source',
'note',
'user_id',
'reference_type',
'reference_id',
];
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'quantity' => 'integer',
];
}
/**
* @return BelongsTo<Product, $this>
*/
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
/**
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @return MorphTo<Model, $this>
*/
public function reference(): MorphTo
{
return $this->morphTo();
}
public function isIn(): bool
{
return $this->direction === 'in';
}
/**
* Vorzeichenbehaftete Menge (Eingang positiv, Ausgang negativ).
*/
public function signedQuantity(): int
{
return $this->isIn() ? (int) $this->quantity : -(int) $this->quantity;
}
}

View file

@ -0,0 +1,87 @@
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class StockDisposal extends Model
{
protected $fillable = [
'disposal_type',
'ingredient_id',
'packaging_item_id',
'stock_entry_id',
'location_id',
'quantity',
'unit',
'reason',
'note',
'user_id',
'disposed_at',
];
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'disposed_at' => 'date',
'quantity' => 'decimal:2',
];
}
/**
* @return BelongsTo<Ingredient, $this>
*/
public function ingredient(): BelongsTo
{
return $this->belongsTo(Ingredient::class);
}
/**
* @return BelongsTo<PackagingItem, $this>
*/
public function packagingItem(): BelongsTo
{
return $this->belongsTo(PackagingItem::class);
}
/**
* @return BelongsTo<StockEntry, $this>
*/
public function stockEntry(): BelongsTo
{
return $this->belongsTo(StockEntry::class);
}
/**
* @return BelongsTo<Location, $this>
*/
public function location(): BelongsTo
{
return $this->belongsTo(Location::class);
}
/**
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function isIngredient(): bool
{
return $this->disposal_type === 'ingredient';
}
public function articleName(): string
{
return $this->isIngredient()
? ($this->ingredient?->name ?? '—')
: ($this->packagingItem?->name ?? '—');
}
}

View file

@ -2,9 +2,12 @@
namespace App\Providers;
use App\Services\InventoryService;
use App\Services\ProductStockService;
use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@ -18,6 +21,18 @@ class AppServiceProvider extends ServiceProvider
{
Schema::defaultStringLength(191);
URL::forceScheme('https');
View::composer('layouts.includes.layout-sidenav', function ($view): void {
$criticalIngredients = 0;
$criticalProducts = 0;
$user = auth()->user();
if ($user && $user->isCopyReader()) {
$criticalIngredients = app(InventoryService::class)->criticalIngredientCount();
$criticalProducts = app(ProductStockService::class)->criticalProductCount();
}
$view->with('criticalIngredientCount', $criticalIngredients);
$view->with('criticalProductCount', $criticalProducts);
});
}
/**

View file

@ -33,8 +33,39 @@ class ProductRepository extends BaseRepository
$data['whitelabel'] = isset($data['whitelabel']) ? 1 : 0;
$data['shipping_addon'] = isset($data['shipping_addon']) ? 1 : 0;
$data['max_buy'] = isset($data['max_buy']) ? 1 : 0;
$data['no_recipe_required'] = isset($data['no_recipe_required']) ? 1 : 0;
$data['is_set'] = isset($data['is_set']) ? 1 : 0;
$data['show_on'] = isset($data['show_on']) ? $data['show_on'] : null;
$data['main_product_quantity'] = isset($data['main_product_quantity']) && $data['main_product_quantity'] !== ''
? (int) $data['main_product_quantity']
: null;
// Ein Set ist selbst keine Variante eines Hauptprodukts.
$data['main_product_id'] = ! $data['is_set'] && isset($data['main_product_id']) && $data['main_product_id'] !== ''
? (int) $data['main_product_id']
: null;
// AP-11: Produktbestand-Schwellwerte (leer => null).
$data['min_product_stock'] = isset($data['min_product_stock']) && $data['min_product_stock'] !== ''
? max(0, (int) $data['min_product_stock'])
: null;
$data['critical_product_stock'] = isset($data['critical_product_stock']) && $data['critical_product_stock'] !== ''
? max(0, (int) $data['critical_product_stock'])
: null;
// AP-03: „Nicht vorrätig"-Status. „Unbestimmt" hat Vorrang vor der Tagesangabe.
$data['out_of_stock_indefinite'] = isset($data['out_of_stock_indefinite']) ? 1 : 0;
if ($data['out_of_stock_indefinite']) {
$data['out_of_stock_until'] = null;
} elseif (isset($data['out_of_stock_active']) && isset($data['out_of_stock_days']) && $data['out_of_stock_days'] !== '') {
$days = max(0, (int) $data['out_of_stock_days']);
$data['out_of_stock_until'] = now()->addDays($days)->startOfDay();
} else {
$data['out_of_stock_until'] = null;
}
if (array_key_exists('shelf_life_type', $data)) {
if ($data['shelf_life_type'] === '' || $data['shelf_life_type'] === null) {
$data['shelf_life_type'] = null;
@ -60,14 +91,54 @@ class ProductRepository extends BaseRepository
$this->updateWLVariants(isset($data['whitelabel_variants']) ? $data['whitelabel_variants'] : []);
$this->updateWLImageAttributs(isset($data['image_wl_attributes']) ? $data['image_wl_attributes'] : [], isset($data['whitelabel_variants']) ? $data['whitelabel_variants'] : []);
$this->updateIngredients($data);
$this->updateManufacturerIngredients($data);
$this->updatePackagings($data);
if ($this->model->is_set) {
// Sets haben keine eigene Rezeptur/Verpackung und werden nicht produziert.
ProductIngredient::where('product_id', $this->model->id)->delete();
$this->model->packagings()->detach();
$this->updateSetItems($data);
} else {
$this->model->setItems()->detach();
$this->updateIngredients($data);
$this->updateManufacturerIngredients($data);
$this->updatePackagings($data);
}
$this->updateCountryPrices($data);
return $this->model;
}
public function updateSetItems(array $data = []): bool
{
if (! isset($data['set_component_id']) || ! is_array($data['set_component_id'])) {
$this->model->setItems()->detach();
return true;
}
$ids = $data['set_component_id'];
$quantities = $data['set_quantity'] ?? [];
$syncData = [];
foreach ($ids as $index => $componentId) {
$cid = (int) $componentId;
if ($cid <= 0 || $cid === (int) $this->model->id) {
continue;
}
$qty = (int) ($quantities[$index] ?? 1);
if ($qty < 1) {
$qty = 1;
}
$syncData[$cid] = [
'quantity' => $qty,
'pos' => (int) $index,
];
}
$this->model->setItems()->sync($syncData);
return true;
}
public function updatePackagings(array $data = []): bool
{
if (! isset($data['pp_packaging_item_id']) || ! is_array($data['pp_packaging_item_id'])) {
@ -400,6 +471,17 @@ class ProductRepository extends BaseRepository
}
$this->model->packagings()->sync($packSync);
$setSync = [];
foreach ($model->setItems()->get() as $component) {
$setSync[$component->id] = [
'quantity' => $component->pivot->quantity !== null ? (int) $component->pivot->quantity : 1,
'pos' => (int) ($component->pivot->pos ?? 0),
];
}
if ($setSync !== []) {
$this->model->setItems()->sync($setSync);
}
foreach ($model->country_prices as $cp) {
CountryPrice::create([
'country_id' => $cp->country_id,

View file

@ -0,0 +1,309 @@
<?php
namespace App\Services;
use App\Models\Ingredient;
use App\Models\ProductionIngredient;
use App\Models\ProductionPackaging;
use App\Models\StockDisposal;
use App\Models\StockEntry;
use Carbon\Carbon;
class InventoryService
{
/**
* Lookback-Fenster (Tage) für die Berechnung des durchschnittlichen Tagesverbrauchs
* aus der Produktionshistorie.
*/
public const CONSUMPTION_WINDOW_DAYS = 90;
/**
* Standard-Lieferzeit (Tage), falls weder am Rohstoff noch am Lieferanten ein Wert gepflegt ist.
*/
public const DEFAULT_LEAD_DAYS = 14;
/**
* Gesamter Restbestand je Rohstoff in Gramm (nur eingegangene Chargen, abzüglich Produktionsverbrauch).
*
* @param array<int, int>|null $ingredientIds Optionaler Filter; null = alle.
* @return array<int, float> [ingredient_id => grams]
*/
public function remainingByIngredient(?array $ingredientIds = null): array
{
$received = StockEntry::query()
->where('status', 'received')
->where('entry_type', 'ingredient')
->whereNotNull('ingredient_id')
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
->selectRaw('ingredient_id, SUM(received_quantity) as qty')
->groupBy('ingredient_id')
->pluck('qty', 'ingredient_id');
$consumed = ProductionIngredient::query()
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
->selectRaw('ingredient_id, SUM(quantity_used) as qty')
->groupBy('ingredient_id')
->pluck('qty', 'ingredient_id');
$disposed = $this->disposedByIngredient($ingredientIds);
$result = [];
foreach ($received as $id => $qty) {
$result[(int) $id] = round((float) $qty - (float) ($consumed[$id] ?? 0) - (float) ($disposed[(int) $id] ?? 0), 2);
}
return $result;
}
/**
* Per Ausgang/Ausschuss entnommene Menge je Rohstoff in Gramm.
*
* @param array<int, int>|null $ingredientIds
* @return array<int, float> [ingredient_id => grams]
*/
public function disposedByIngredient(?array $ingredientIds = null): array
{
$rows = StockDisposal::query()
->where('disposal_type', 'ingredient')
->whereNotNull('ingredient_id')
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
->selectRaw('ingredient_id, SUM(quantity) as qty')
->groupBy('ingredient_id')
->pluck('qty', 'ingredient_id');
$result = [];
foreach ($rows as $id => $qty) {
$result[(int) $id] = round((float) $qty, 2);
}
return $result;
}
/**
* Restbestand eines Rohstoffs je Lagerort.
*
* @return array<int, float> [location_id => grams]
*/
public function remainingByLocationForIngredient(int $ingredientId): array
{
$received = StockEntry::query()
->where('status', 'received')
->where('entry_type', 'ingredient')
->where('ingredient_id', $ingredientId)
->selectRaw('location_id, SUM(received_quantity) as qty')
->groupBy('location_id')
->pluck('qty', 'location_id');
$consumed = ProductionIngredient::query()
->join('stock_entries', 'stock_entries.id', '=', 'production_ingredients.stock_entry_id')
->where('production_ingredients.ingredient_id', $ingredientId)
->selectRaw('stock_entries.location_id as location_id, SUM(production_ingredients.quantity_used) as qty')
->groupBy('stock_entries.location_id')
->pluck('qty', 'location_id');
$disposed = StockDisposal::query()
->where('disposal_type', 'ingredient')
->where('ingredient_id', $ingredientId)
->selectRaw('location_id, SUM(quantity) as qty')
->groupBy('location_id')
->pluck('qty', 'location_id');
$result = [];
foreach ($received as $locationId => $qty) {
$result[(int) $locationId] = round((float) $qty - (float) ($consumed[$locationId] ?? 0) - (float) ($disposed[$locationId] ?? 0), 2);
}
return $result;
}
/**
* Restbestand je Verpackungsartikel in Stück (Wareneingang Produktionsverbrauch Ausschuss).
*
* @param array<int, int>|null $packagingItemIds
* @return array<int, float> [packaging_item_id => pieces]
*/
public function remainingByPackagingItem(?array $packagingItemIds = null): array
{
$received = StockEntry::query()
->where('status', 'received')
->whereIn('entry_type', ['packaging', 'shipping'])
->whereNotNull('packaging_item_id')
->when($packagingItemIds !== null, fn ($q) => $q->whereIn('packaging_item_id', $packagingItemIds))
->selectRaw('packaging_item_id, SUM(received_quantity) as qty')
->groupBy('packaging_item_id')
->pluck('qty', 'packaging_item_id');
$consumed = ProductionPackaging::query()
->when($packagingItemIds !== null, fn ($q) => $q->whereIn('packaging_item_id', $packagingItemIds))
->selectRaw('packaging_item_id, SUM(quantity_used) as qty')
->groupBy('packaging_item_id')
->pluck('qty', 'packaging_item_id');
$disposed = $this->disposedByPackagingItem($packagingItemIds);
$result = [];
foreach ($received as $id => $qty) {
$result[(int) $id] = round((float) $qty - (float) ($consumed[$id] ?? 0) - (float) ($disposed[(int) $id] ?? 0), 2);
}
return $result;
}
/**
* Per Ausgang/Ausschuss entnommene Menge je Verpackungsartikel in Stück.
*
* @param array<int, int>|null $packagingItemIds
* @return array<int, float> [packaging_item_id => pieces]
*/
public function disposedByPackagingItem(?array $packagingItemIds = null): array
{
$rows = StockDisposal::query()
->where('disposal_type', 'packaging')
->whereNotNull('packaging_item_id')
->when($packagingItemIds !== null, fn ($q) => $q->whereIn('packaging_item_id', $packagingItemIds))
->selectRaw('packaging_item_id, SUM(quantity) as qty')
->groupBy('packaging_item_id')
->pluck('qty', 'packaging_item_id');
$result = [];
foreach ($rows as $id => $qty) {
$result[(int) $id] = round((float) $qty, 2);
}
return $result;
}
/**
* Offene (bestellte, noch nicht eingegangene) Menge je Rohstoff in Gramm.
*
* @param array<int, int>|null $ingredientIds
* @return array<int, float> [ingredient_id => grams]
*/
public function openOrderQuantityByIngredient(?array $ingredientIds = null): array
{
$rows = StockEntry::query()
->where('status', 'pending')
->where('entry_type', 'ingredient')
->whereNotNull('ingredient_id')
->when($ingredientIds !== null, fn ($q) => $q->whereIn('ingredient_id', $ingredientIds))
->selectRaw('ingredient_id, SUM(ordered_quantity) as qty')
->groupBy('ingredient_id')
->pluck('qty', 'ingredient_id');
$result = [];
foreach ($rows as $id => $qty) {
$result[(int) $id] = round((float) $qty, 2);
}
return $result;
}
/**
* Durchschnittlicher Tagesverbrauch je Rohstoff (Gramm/Tag) aus der Produktionshistorie.
*
* @param array<int, int>|null $ingredientIds
* @return array<int, float> [ingredient_id => grams_per_day]
*/
public function dailyConsumptionByIngredient(?array $ingredientIds = null, int $windowDays = self::CONSUMPTION_WINDOW_DAYS): array
{
$windowDays = max(1, $windowDays);
$since = Carbon::now()->subDays($windowDays)->startOfDay();
$rows = ProductionIngredient::query()
->join('productions', 'productions.id', '=', 'production_ingredients.production_id')
->where('productions.produced_at', '>=', $since)
->when($ingredientIds !== null, fn ($q) => $q->whereIn('production_ingredients.ingredient_id', $ingredientIds))
->selectRaw('production_ingredients.ingredient_id as ingredient_id, SUM(production_ingredients.quantity_used) as qty')
->groupBy('production_ingredients.ingredient_id')
->pluck('qty', 'ingredient_id');
$result = [];
foreach ($rows as $id => $qty) {
$result[(int) $id] = round((float) $qty / $windowDays, 4);
}
return $result;
}
/**
* Tage bis der Bestand bei gleichbleibendem Verbrauch aufgebraucht ist (null = kein Verbrauch).
*/
public function daysUntilEmpty(float $remaining, ?float $dailyConsumption): ?int
{
if ($dailyConsumption === null || $dailyConsumption <= 0) {
return null;
}
if ($remaining <= 0) {
return 0;
}
return (int) floor($remaining / $dailyConsumption);
}
/**
* Voraussichtliches Datum, an dem der Bestand auf null fällt (null = kein Verbrauch).
*/
public function expectedEmptyDate(float $remaining, ?float $dailyConsumption): ?Carbon
{
$days = $this->daysUntilEmpty($remaining, $dailyConsumption);
if ($days === null) {
return null;
}
return Carbon::now()->startOfDay()->addDays($days);
}
/**
* Bestands-Status eines Rohstoffs:
* - "critical": Meldebestand gepflegt und unterschritten (rot, Badge-relevant).
* - "critical_ordered": kritisch, aber es existiert bereits eine offene Bestellung (entschärft).
* - "warning": Bestand reicht voraussichtlich nicht mehr bis zur nächsten Lieferung.
* - "ok": ausreichend.
*/
public function stockStatus(?float $minStockAlert, float $remaining, ?float $dailyConsumption, ?int $leadDays, bool $hasOpenOrder = false): string
{
if ($minStockAlert !== null && $minStockAlert > 0 && $remaining <= $minStockAlert) {
return $hasOpenOrder ? 'critical_ordered' : 'critical';
}
$days = $this->daysUntilEmpty($remaining, $dailyConsumption);
if ($days !== null) {
$lead = $leadDays !== null && $leadDays > 0 ? $leadDays : self::DEFAULT_LEAD_DAYS;
if ($days <= $lead) {
return 'warning';
}
}
return 'ok';
}
/**
* Anzahl der kritischen (Meldebestand unterschritten) aktiven Rohstoffe für das Sidenav-Badge.
*/
public function criticalIngredientCount(): int
{
$ingredients = Ingredient::query()
->where('active', true)
->whereNotNull('min_stock_alert')
->where('min_stock_alert', '>', 0)
->get(['id', 'min_stock_alert']);
if ($ingredients->isEmpty()) {
return 0;
}
$ids = $ingredients->pluck('id')->all();
$remaining = $this->remainingByIngredient($ids);
$openOrders = $this->openOrderQuantityByIngredient($ids);
return $ingredients->filter(function (Ingredient $ingredient) use ($remaining, $openOrders) {
$rem = $remaining[$ingredient->id] ?? 0.0;
if ($rem > (float) $ingredient->min_stock_alert) {
return false;
}
// Kritisch, aber bereits nachbestellt => entschärft, nicht im Badge zählen.
return ($openOrders[$ingredient->id] ?? 0.0) <= 0;
})->count();
}
}

View file

@ -0,0 +1,146 @@
<?php
namespace App\Services;
use App\Models\Product;
use App\Models\Production;
use App\Models\ProductStockMovement;
use Illuminate\Database\Eloquent\Model;
class ProductStockService
{
/**
* Aktueller Bestand je Produkt (Summe Eingänge Summe Ausgänge).
*
* @param array<int, int>|null $productIds
* @return array<int, int> [product_id => stock]
*/
public function currentStockByProduct(?array $productIds = null): array
{
$rows = ProductStockMovement::query()
->when($productIds !== null, fn ($q) => $q->whereIn('product_id', $productIds))
->selectRaw("product_id, SUM(CASE WHEN direction = 'in' THEN quantity ELSE -quantity END) as stock")
->groupBy('product_id')
->pluck('stock', 'product_id');
$result = [];
foreach ($rows as $id => $stock) {
$result[(int) $id] = (int) $stock;
}
return $result;
}
public function currentStock(int $productId): int
{
return $this->currentStockByProduct([$productId])[$productId] ?? 0;
}
/**
* Bucht eine Bestandsbewegung. Menge wird immer positiv gespeichert, die Richtung steuert das Vorzeichen.
*
* @param array{type: string, id: int}|Model|null $reference
*/
public function recordMovement(
Product $product,
string $direction,
int $quantity,
string $reason,
string $source = 'manual',
?string $note = null,
?int $userId = null,
Model|array|null $reference = null,
): ProductStockMovement {
$attributes = [
'product_id' => $product->id,
'direction' => $direction === 'out' ? 'out' : 'in',
'quantity' => max(0, $quantity),
'reason' => $reason,
'source' => $source,
'note' => $note,
'user_id' => $userId,
];
if ($reference instanceof Model) {
$attributes['reference_type'] = $reference->getMorphClass();
$attributes['reference_id'] = $reference->getKey();
} elseif (is_array($reference)) {
$attributes['reference_type'] = $reference['type'];
$attributes['reference_id'] = $reference['id'];
}
return ProductStockMovement::query()->create($attributes);
}
/**
* Verbucht den Produktbestand einer Produktion idempotent: gleicht die bereits gebuchte
* Menge dieser Produktion mit der Soll-Stückzahl ab und bucht nur die Differenz.
*/
public function recordProductionStock(Production $production, ?int $userId = null): void
{
$production->loadMissing('product');
$alreadyBooked = (int) ProductStockMovement::query()
->where('source', 'production')
->where('reference_type', $production->getMorphClass())
->where('reference_id', $production->getKey())
->selectRaw("COALESCE(SUM(CASE WHEN direction = 'in' THEN quantity ELSE -quantity END), 0) as net")
->value('net');
$target = (int) $production->quantity;
$delta = $target - $alreadyBooked;
if ($delta === 0) {
return;
}
$this->recordMovement(
$production->product,
$delta > 0 ? 'in' : 'out',
abs($delta),
$alreadyBooked === 0 ? __('Produktion') : __('Produktionskorrektur'),
'production',
null,
$userId ?? $production->produced_by,
$production,
);
}
/**
* Bestands-Status: "critical" (rot) kritischer Schwellwert, "warning" (gelb) Meldebestand, sonst "ok".
*/
public function productStatus(int $stock, ?int $minStock, ?int $criticalStock): string
{
if ($criticalStock !== null && $stock <= $criticalStock) {
return 'critical';
}
if ($minStock !== null && $stock <= $minStock) {
return 'warning';
}
return 'ok';
}
/**
* Anzahl kritischer Hauptprodukte (Bestand kritischer Schwellwert) für das Sidenav-Badge.
*/
public function criticalProductCount(): int
{
$products = Product::query()
->where('active', true)
->where('is_set', false)
->whereNull('main_product_id')
->whereNotNull('critical_product_stock')
->get(['id', 'critical_product_stock']);
if ($products->isEmpty()) {
return 0;
}
$stock = $this->currentStockByProduct($products->pluck('id')->all());
return $products->filter(function (Product $product) use ($stock) {
return ($stock[$product->id] ?? 0) <= (int) $product->critical_product_stock;
})->count();
}
}

View file

@ -4,6 +4,7 @@ namespace App\Services;
use App\Models\Product;
use App\Models\Production;
use App\Models\ProductionIngredient;
use App\Models\StockEntry;
use Carbon\Carbon;
use Illuminate\Support\Collection;
@ -12,6 +13,10 @@ use Illuminate\Validation\ValidationException;
class ProductionService
{
public function __construct(
protected ProductStockService $productStockService,
) {}
/**
* @param array<string, mixed> $data
* @param array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used: float|int|string}> $ingredientLines
@ -20,7 +25,7 @@ class ProductionService
{
return DB::transaction(function () use ($data, $ingredientLines, $userId) {
$product = Product::query()
->with(['p_ingredients', 'packagings'])
->with(['manufacturer_ingredients', 'packagings'])
->findOrFail($data['product_id']);
$locationId = (int) $data['location_id'];
@ -29,44 +34,22 @@ class ProductionService
throw ValidationException::withMessages(['quantity' => __('Die Stückzahl muss mindestens 1 sein.')]);
}
if ($product->p_ingredients->isEmpty()) {
throw ValidationException::withMessages(['product_id' => __('Das Produkt hat keine Rezeptur (Inhaltsstoffe).')]);
}
$this->assertNotASet($product);
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
if ($product->no_recipe_required) {
$ingredientLines = [];
} else {
$this->assertManufacturerRecipe($product);
$sums = [];
foreach ($ingredientLines as $line) {
$iid = (int) $line['ingredient_id'];
$used = $this->parseQuantity($line['quantity_used'] ?? null);
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
}
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
foreach ($requiredGrams as $iid => $req) {
$sum = $sums[$iid] ?? 0;
if (abs($sum - $req) > 0.02) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
'id' => $iid,
'req' => number_format($req, 2, ',', '.'),
'sum' => number_format($sum, 2, ',', '.'),
]),
]);
$this->assertLinesMatchRecipe($requiredGrams, $ingredientLines);
foreach ($ingredientLines as $line) {
$this->assertStockEntryMatchesLine($line, $locationId);
}
}
foreach ($sums as $iid => $_sum) {
if (! isset($requiredGrams[$iid])) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Unerwarteter Inhaltsstoff in den Chargen-Zeilen.'),
]);
}
}
foreach ($ingredientLines as $line) {
$this->assertStockEntryMatchesLine($line, $locationId);
}
$mhdWarning = $this->computeMhdWarning($product, $data['produced_at'], $ingredientLines);
$production = Production::query()->create([
@ -87,17 +70,10 @@ class ProductionService
]);
}
foreach ($product->packagings as $bom) {
$perUnit = (float) ($bom->pivot->quantity ?? 1);
$pieces = (int) round($perUnit * $producedQty);
if ($pieces < 1) {
$pieces = 1;
}
$production->productionPackagings()->create([
'packaging_item_id' => $bom->id,
'quantity_used' => $pieces,
]);
}
$this->syncPackagingSnapshot($production, $product, $producedQty);
$production->setRelation('product', $product);
$this->productStockService->recordProductionStock($production, $userId);
return $production->fresh(['product', 'location', 'productionIngredients', 'productionPackagings']);
});
@ -109,9 +85,9 @@ class ProductionService
*/
public function updateProduction(Production $production, array $data, array $ingredientLines, int $userId): Production
{
return DB::transaction(function () use ($production, $data, $ingredientLines) {
return DB::transaction(function () use ($production, $data, $ingredientLines, $userId) {
$product = Product::query()
->with(['p_ingredients', 'packagings'])
->with(['manufacturer_ingredients', 'packagings'])
->findOrFail($data['product_id']);
$locationId = (int) $data['location_id'];
@ -120,36 +96,22 @@ class ProductionService
throw ValidationException::withMessages(['quantity' => __('Die Stückzahl muss mindestens 1 sein.')]);
}
if ($product->p_ingredients->isEmpty()) {
throw ValidationException::withMessages(['product_id' => __('Das Produkt hat keine Rezeptur (Inhaltsstoffe).')]);
}
$this->assertNotASet($product);
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
if ($product->no_recipe_required) {
$ingredientLines = [];
} else {
$this->assertManufacturerRecipe($product);
$sums = [];
foreach ($ingredientLines as $line) {
$iid = (int) $line['ingredient_id'];
$used = $this->parseQuantity($line['quantity_used'] ?? null);
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
}
$requiredGrams = $this->requiredGramsByIngredient($product, $producedQty);
foreach ($requiredGrams as $iid => $req) {
$sum = $sums[$iid] ?? 0;
if (abs($sum - $req) > 0.02) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
'id' => $iid,
'req' => number_format($req, 2, ',', '.'),
'sum' => number_format($sum, 2, ',', '.'),
]),
]);
$this->assertLinesMatchRecipe($requiredGrams, $ingredientLines);
foreach ($ingredientLines as $line) {
$this->assertStockEntryMatchesLine($line, $locationId);
}
}
foreach ($ingredientLines as $line) {
$this->assertStockEntryMatchesLine($line, $locationId);
}
$mhdWarning = $this->computeMhdWarning($product, $data['produced_at'], $ingredientLines);
$production->update([
@ -171,33 +133,102 @@ class ProductionService
}
$production->productionPackagings()->delete();
foreach ($product->packagings as $bom) {
$perUnit = (float) ($bom->pivot->quantity ?? 1);
$pieces = (int) round($perUnit * $producedQty);
if ($pieces < 1) {
$pieces = 1;
}
$production->productionPackagings()->create([
'packaging_item_id' => $bom->id,
'quantity_used' => $pieces,
]);
}
$this->syncPackagingSnapshot($production, $product, $producedQty);
$production->setRelation('product', $product);
$this->productStockService->recordProductionStock($production, $userId);
return $production->fresh(['product', 'location', 'productionIngredients', 'productionPackagings']);
});
}
/**
* Sets sind Bündel aus Einzelprodukten und werden nicht produziert.
*/
public function assertNotASet(Product $product): void
{
if ($product->is_set) {
throw ValidationException::withMessages([
'product_id' => __('Sets können nicht produziert werden. Bitte ein Einzelprodukt wählen.'),
]);
}
}
/**
* Produktion basiert ausschließlich auf der Hersteller-Rezeptur (kein Fallback auf die Produkt-Rezeptur).
*/
public function assertManufacturerRecipe(Product $product): void
{
$product->loadMissing('manufacturer_ingredients');
if ($product->manufacturer_ingredients->isEmpty()) {
throw ValidationException::withMessages([
'product_id' => __('Für dieses Produkt ist keine Hersteller-Rezeptur gepflegt. Eine Produktion ist erst möglich, wenn eine Hersteller-Rezeptur hinterlegt wurde.'),
]);
}
}
/**
* @param array<int, float> $requiredGrams
* @param array<int, array{ingredient_id: int, stock_entry_id: int, quantity_used?: mixed}> $ingredientLines
*/
private function assertLinesMatchRecipe(array $requiredGrams, array $ingredientLines): void
{
$sums = [];
foreach ($ingredientLines as $line) {
$iid = (int) $line['ingredient_id'];
$used = $this->parseQuantity($line['quantity_used'] ?? null);
$sums[$iid] = ($sums[$iid] ?? 0) + $used;
}
foreach ($requiredGrams as $iid => $req) {
$sum = $sums[$iid] ?? 0;
if (abs($sum - $req) > 0.02) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Summe der Chargen-Mengen pro INCI muss dem Soll-Verbrauch entsprechen (INCI :id: Soll :req g, Ist :sum g).', [
'id' => $iid,
'req' => number_format($req, 2, ',', '.'),
'sum' => number_format($sum, 2, ',', '.'),
]),
]);
}
}
foreach ($sums as $iid => $_sum) {
if (! isset($requiredGrams[$iid])) {
throw ValidationException::withMessages([
'ingredient_lines' => __('Unerwarteter Inhaltsstoff in den Chargen-Zeilen.'),
]);
}
}
}
private function syncPackagingSnapshot(Production $production, Product $product, int $producedQty): void
{
foreach ($product->packagings as $bom) {
$perUnit = (float) ($bom->pivot->quantity ?? 1);
$pieces = (int) round($perUnit * $producedQty);
if ($pieces < 1) {
$pieces = 1;
}
$production->productionPackagings()->create([
'packaging_item_id' => $bom->id,
'quantity_used' => $pieces,
]);
}
}
/**
* @return array<int, float>
*/
public function requiredGramsByIngredient(Product $product, int $producedQuantity): array
{
$required = [];
foreach ($product->p_ingredients as $ing) {
foreach ($product->manufacturer_ingredients as $ing) {
$gram = $ing->pivot->gram;
if ($gram === null || $gram === '') {
throw ValidationException::withMessages([
'product_id' => __('Für „:name“ fehlt der Anteil (%) in der Rezeptur.', ['name' => $ing->name]),
'product_id' => __('Für „:name“ fehlt der Anteil (%) in der Hersteller-Rezeptur.', ['name' => $ing->name]),
]);
}
$factor = (float) ($ing->pivot->factor ?? 1.1);
@ -251,52 +282,107 @@ class ProductionService
}
/**
* Letzte empfangene Wareneingänge pro Inhaltsstoff am Standort (max. 3).
* Verbrauchte Menge je Wareneingang (über alle Produktionen), optional ohne eine bestimmte Produktion.
*
* @param array<int, int> $stockEntryIds
* @return array<int, float>
*/
public function consumedByStockEntry(array $stockEntryIds, ?int $excludeProductionId = null): array
{
if ($stockEntryIds === []) {
return [];
}
$rows = ProductionIngredient::query()
->selectRaw('stock_entry_id, SUM(quantity_used) as used')
->whereIn('stock_entry_id', $stockEntryIds)
->when($excludeProductionId !== null, fn ($q) => $q->where('production_id', '!=', $excludeProductionId))
->groupBy('stock_entry_id')
->pluck('used', 'stock_entry_id');
$result = [];
foreach ($rows as $stockEntryId => $used) {
$result[(int) $stockEntryId] = (float) $used;
}
return $result;
}
/**
* Wareneingänge eines Inhaltsstoffs am Standort mit Restbestand > 0 (FEFO-Reihenfolge).
*
* @return Collection<int, StockEntry>
*/
public function latestStockEntriesForIngredient(int $ingredientId, int $locationId, int $limit = 3)
public function availableStockEntriesForIngredient(int $ingredientId, int $locationId, ?int $excludeProductionId = null): Collection
{
return StockEntry::query()
$entries = StockEntry::query()
->with('supplier')
->where('status', 'received')
->where('entry_type', 'ingredient')
->where('ingredient_id', $ingredientId)
->where('location_id', $locationId)
->orderByDesc('received_at')
->orderByDesc('id')
->limit($limit)
->orderByRaw('best_before is null, best_before asc')
->orderBy('id')
->get();
$consumed = $this->consumedByStockEntry($entries->pluck('id')->all(), $excludeProductionId);
return $entries
->map(function (StockEntry $entry) use ($consumed) {
$received = $entry->received_quantity !== null ? (float) $entry->received_quantity : 0.0;
$entry->setAttribute('remaining_quantity', round($received - ($consumed[(int) $entry->id] ?? 0.0), 2));
return $entry;
})
->filter(fn (StockEntry $entry) => (float) $entry->getAttribute('remaining_quantity') > 0.0)
->values();
}
public function stockEntryLabel(StockEntry $entry): string
{
$parts = [];
$parts[] = $entry->supplier?->name ?: __('Lieferant unbekannt');
$parts[] = $entry->batch_number ?: ('#'.$entry->id);
if ($entry->best_before) {
$parts[] = $entry->best_before->format('d.m.Y');
}
return implode(' - ', $parts);
}
/**
* @return array<string, mixed>
*/
public function buildRecipePayload(Product $product, int $locationId, int $productionQuantity): array
public function buildRecipePayload(Product $product, int $locationId, int $productionQuantity, ?int $excludeProductionId = null): array
{
$product->loadMissing(['p_ingredients', 'packagings.packagingMaterial']);
$product->loadMissing(['manufacturer_ingredients', 'packagings.packagingMaterial']);
$qty = max(1, $productionQuantity);
$recipeRequired = ! (bool) $product->no_recipe_required;
$hasRecipe = $product->manufacturer_ingredients->isNotEmpty();
$ingredients = [];
foreach ($product->p_ingredients as $ing) {
foreach ($product->manufacturer_ingredients as $ing) {
$gram = $ing->pivot->gram;
$factor = (float) ($ing->pivot->factor ?? 1.1);
$req = ($gram !== null && $gram !== '') ? (float) $gram * $factor * $qty : null;
$hasGram = $gram !== null && $gram !== '';
$req = $hasGram ? (float) $gram * $factor * $qty : null;
$ingredients[] = [
'id' => $ing->id,
'name' => $ing->name,
'gram' => $gram !== null && $gram !== '' ? (float) $gram : null,
'gram' => $hasGram ? (float) $gram : null,
'factor' => $factor,
'required_grams_total' => $req,
'stock_entries' => $this->latestStockEntriesForIngredient((int) $ing->id, $locationId)->map(function (StockEntry $se) {
return [
'id' => $se->id,
'batch_number' => $se->batch_number,
'best_before' => $se->best_before?->format('Y-m-d'),
'received_at' => $se->received_at?->format('Y-m-d'),
'received_quantity' => $se->received_quantity !== null ? (float) $se->received_quantity : null,
];
})->values()->all(),
'stock_entries' => $this->availableStockEntriesForIngredient((int) $ing->id, $locationId, $excludeProductionId)
->map(function (StockEntry $se) {
return [
'id' => $se->id,
'label' => $this->stockEntryLabel($se),
'batch_number' => $se->batch_number,
'best_before' => $se->best_before?->format('Y-m-d'),
'remaining' => (float) $se->getAttribute('remaining_quantity'),
];
})->values()->all(),
];
}
@ -307,7 +393,7 @@ class ProductionService
'id' => $pk->id,
'name' => $pk->name,
'quantity_per_product' => $perUnit,
'total_pieces' => (int) round($perUnit * max(1, $productionQuantity)),
'total_pieces' => (int) round($perUnit * $qty),
'weight_grams' => $pk->weight_grams !== null ? (float) $pk->weight_grams : null,
'material_name' => $pk->packagingMaterial?->name,
];
@ -320,8 +406,10 @@ class ProductionService
'shelf_life_type' => $product->shelf_life_type,
'shelf_life_months' => $product->shelf_life_months,
],
'recipe_required' => $recipeRequired,
'has_recipe' => $hasRecipe,
'location_id' => $locationId,
'production_quantity' => $productionQuantity,
'production_quantity' => $qty,
'ingredients' => $ingredients,
'packagings' => $packagings,
];