64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Helper;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\Restaurant\Models\Ingredient;
|
|
use Modules\Restaurant\Models\Stock;
|
|
|
|
class StockHelper
|
|
{
|
|
/**
|
|
* Update ingredient stock and log the movement.
|
|
*
|
|
* @param string $type (purchase, usage, return, damage, transfer_in, transfer_out)
|
|
* @param float|null $unitCost (cost per unit)
|
|
* @param string|null $refType (e.g., Purchase, Order, Wastage)
|
|
*/
|
|
public static function updateStock(
|
|
int $ingredientId,
|
|
float $quantity,
|
|
string $type,
|
|
?float $unitCost = null,
|
|
?string $refType = null,
|
|
?int $refId = null,
|
|
?string $batchNo = null,
|
|
?string $expiryDate = null,
|
|
?int $restaurantId = null,
|
|
?int $addedBy = null
|
|
): void {
|
|
$ingredient = Ingredient::find($ingredientId);
|
|
if (! $ingredient) {
|
|
return;
|
|
}
|
|
|
|
// Determine signed quantity based on type
|
|
$signedQty = in_array($type, ['purchase', 'return', 'transfer_in', 'adjust_plus'])
|
|
? $quantity
|
|
: -$quantity;
|
|
|
|
// Update ingredient stock
|
|
$ingredient->increment('stock_quantity', $signedQty);
|
|
|
|
// Calculate total cost if unit cost is provided
|
|
$totalCost = $unitCost !== null ? $unitCost * $quantity : null;
|
|
|
|
// Record in stocks table
|
|
Stock::create([
|
|
'restaurant_id' => $restaurantId ?? $ingredient->restaurant_id ?? null,
|
|
'ingredient_id' => $ingredientId,
|
|
'type' => $type,
|
|
'quantity' => $quantity,
|
|
'unit_cost' => $unitCost,
|
|
'total_cost' => $totalCost,
|
|
'reference_type' => $refType,
|
|
'purchase_id' => $refId,
|
|
'batch_no' => $batchNo,
|
|
'expiry_date' => $expiryDate,
|
|
'added_by' => $addedBy ?? Auth::id(),
|
|
'remarks' => ucfirst($type).' stock movement',
|
|
]);
|
|
}
|
|
}
|