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

53 lines
1.7 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}
}