Files
kulakpos_web/public/restaurant/app/Helper/IngredientDamageHelper.php

53 lines
1.7 KiB
PHP
Raw Normal View History

2026-03-15 17:08:23 +07:00
<?php
namespace App\Helper;
use Illuminate\Support\Facades\DB;
use Modules\Restaurant\Models\Ingredient;
use Modules\Restaurant\Models\IngredientDamage;
use Modules\Restaurant\Models\Stock;
class IngredientDamageHelper
{
public static function createDamage(array $data)
{
DB::beginTransaction();
try {
// 1⃣ Create ingredient damage record
$damage = IngredientDamage::create($data);
// 2⃣ Reduce ingredient stock
$ingredient = Ingredient::find($data['ingredient_id']);
if (! $ingredient) {
throw new \Exception('Ingredient not found.');
}
$ingredient->decrement('stock_quantity', $data['quantity']);
// 3⃣ Log stock movement
Stock::create([
'restaurant_id' => $data['restaurant_id'],
'ingredient_id' => $data['ingredient_id'],
'type' => 'damage',
'quantity' => $data['quantity'],
'unit_cost' => $data['unit_cost'] ?? null,
'total_cost' => isset($data['unit_cost']) ? $data['unit_cost'] * $data['quantity'] : null,
'reference_type' => 'IngredientDamage',
'purchase_id' => $data['purchase_id'],
'batch_no' => $data['batch_no'] ?? null,
'expiry_date' => null,
'added_by' => $data['reported_by'] ?? auth()->id(),
'remarks' => $data['reason'] ?? 'Ingredient damage',
]);
DB::commit();
return $damage;
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
}