partial update close modal on all page and disable create transaction with no stock
This commit is contained in:
@@ -7,12 +7,14 @@ use App\Models\Work;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\StockLog;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Exception;
|
||||
|
||||
class StockService
|
||||
{
|
||||
/**
|
||||
* Check if dealer has sufficient stock for work
|
||||
* Modified to always return available = true (allow negative stock)
|
||||
*
|
||||
* @param int $workId
|
||||
* @param int $dealerId
|
||||
@@ -25,36 +27,30 @@ class StockService
|
||||
|
||||
if (!$work) {
|
||||
return [
|
||||
'available' => false,
|
||||
'message' => 'Pekerjaan tidak ditemukan',
|
||||
'available' => true,
|
||||
'message' => 'Pekerjaan tidak ditemukan, tapi transaksi diizinkan',
|
||||
'details' => []
|
||||
];
|
||||
}
|
||||
|
||||
$stockDetails = [];
|
||||
$allAvailable = true;
|
||||
|
||||
foreach ($work->products as $product) {
|
||||
$requiredQuantity = $product->pivot->quantity_required * $workQuantity;
|
||||
$availableStock = $product->getStockByDealer($dealerId);
|
||||
|
||||
$isAvailable = $availableStock >= $requiredQuantity;
|
||||
if (!$isAvailable) {
|
||||
$allAvailable = false;
|
||||
}
|
||||
|
||||
$stockDetails[] = [
|
||||
'product_id' => $product->id,
|
||||
'product_name' => $product->name,
|
||||
'required_quantity' => $requiredQuantity,
|
||||
'available_stock' => $availableStock,
|
||||
'is_available' => $isAvailable
|
||||
'is_available' => true // Always true - allow negative stock
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'available' => $allAvailable,
|
||||
'message' => $allAvailable ? 'Stock tersedia' : 'Stock tidak mencukupi',
|
||||
'available' => true, // Always return true - allow negative stock
|
||||
'message' => 'Stock tersedia (negative stock allowed)',
|
||||
'details' => $stockDetails
|
||||
];
|
||||
}
|
||||
@@ -68,11 +64,13 @@ class StockService
|
||||
*/
|
||||
public function reduceStockForTransaction(Transaction $transaction)
|
||||
{
|
||||
try {
|
||||
return DB::transaction(function () use ($transaction) {
|
||||
$work = $transaction->work;
|
||||
|
||||
if (!$work) {
|
||||
throw new Exception('Work not found for transaction');
|
||||
// If work not found, just return true to allow transaction to proceed
|
||||
return true;
|
||||
}
|
||||
|
||||
$work->load('products');
|
||||
@@ -84,30 +82,123 @@ class StockService
|
||||
|
||||
foreach ($work->products as $product) {
|
||||
$requiredQuantity = $product->pivot->quantity_required * $transaction->qty;
|
||||
|
||||
Log::info('Processing stock reduction', [
|
||||
'transaction_id' => $transaction->id,
|
||||
'product_id' => $product->id,
|
||||
'product_name' => $product->name,
|
||||
'dealer_id' => $transaction->dealer_id,
|
||||
'required_quantity' => $requiredQuantity,
|
||||
'transaction_qty' => $transaction->qty
|
||||
]);
|
||||
|
||||
$stock = Stock::where('product_id', $product->id)
|
||||
->where('dealer_id', $transaction->dealer_id)
|
||||
->first();
|
||||
|
||||
if (!$stock) {
|
||||
throw new Exception("Stock not found for product {$product->name} at dealer");
|
||||
Log::info('Stock not found, creating new stock record', [
|
||||
'product_id' => $product->id,
|
||||
'dealer_id' => $transaction->dealer_id
|
||||
]);
|
||||
|
||||
try {
|
||||
// Create new stock record with 0 quantity if doesn't exist
|
||||
$stock = Stock::create([
|
||||
'product_id' => $product->id,
|
||||
'dealer_id' => $transaction->dealer_id,
|
||||
'quantity' => 0
|
||||
]);
|
||||
|
||||
Log::info('New stock record created', [
|
||||
'stock_id' => $stock->id,
|
||||
'initial_quantity' => $stock->quantity
|
||||
]);
|
||||
} catch (\Exception $createException) {
|
||||
Log::warning('Failed to create stock, using firstOrCreate', [
|
||||
'error' => $createException->getMessage()
|
||||
]);
|
||||
|
||||
// If creating stock fails, try to use firstOrCreate instead
|
||||
$stock = Stock::firstOrCreate([
|
||||
'product_id' => $product->id,
|
||||
'dealer_id' => $transaction->dealer_id
|
||||
], [
|
||||
'quantity' => 0
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
Log::info('Existing stock found', [
|
||||
'stock_id' => $stock->id,
|
||||
'current_quantity' => $stock->quantity
|
||||
]);
|
||||
}
|
||||
|
||||
if ($stock->quantity < $requiredQuantity) {
|
||||
throw new Exception("Insufficient stock for product {$product->name}. Required: {$requiredQuantity}, Available: {$stock->quantity}");
|
||||
}
|
||||
|
||||
// Reduce stock
|
||||
// Allow negative stock - reduce regardless of current quantity
|
||||
$newQuantity = $stock->quantity - $requiredQuantity;
|
||||
|
||||
Log::info('Updating stock quantity', [
|
||||
'stock_id' => $stock->id,
|
||||
'previous_quantity' => $stock->quantity,
|
||||
'required_quantity' => $requiredQuantity,
|
||||
'new_quantity' => $newQuantity
|
||||
]);
|
||||
|
||||
try {
|
||||
$stock->updateStock(
|
||||
$newQuantity,
|
||||
$transaction,
|
||||
"Stock reduced for work: {$work->name} (Transaction #{$transaction->id})"
|
||||
"Stock reduced for work: {$work->name} (Transaction #{$transaction->id}) - Allow negative stock"
|
||||
);
|
||||
|
||||
Log::info('Stock update successful via updateStock method');
|
||||
} catch (\Exception $updateException) {
|
||||
Log::warning('updateStock method failed, using fallback', [
|
||||
'error' => $updateException->getMessage()
|
||||
]);
|
||||
// If updateStock fails, try direct update but still create stock log
|
||||
$previousQuantity = $stock->quantity;
|
||||
$stock->quantity = $newQuantity;
|
||||
$stock->save();
|
||||
|
||||
// Manually create stock log since updateStock failed
|
||||
try {
|
||||
$stockLog = \App\Models\StockLog::create([
|
||||
'stock_id' => $stock->id,
|
||||
'source_type' => get_class($transaction),
|
||||
'source_id' => $transaction->id,
|
||||
'previous_quantity' => $previousQuantity,
|
||||
'new_quantity' => $newQuantity,
|
||||
'quantity_change' => $newQuantity - $previousQuantity,
|
||||
'description' => "Stock reduced for work: {$work->name} (Transaction #{$transaction->id}) - Allow negative stock (manual log)",
|
||||
'user_id' => auth()->id()
|
||||
]);
|
||||
|
||||
Log::info('Manual stock log created successfully', [
|
||||
'stock_log_id' => $stockLog->id,
|
||||
'previous_quantity' => $previousQuantity,
|
||||
'new_quantity' => $newQuantity
|
||||
]);
|
||||
} catch (\Exception $logException) {
|
||||
// Log the error but don't fail the transaction
|
||||
Log::warning('Failed to create stock log: ' . $logException->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
} catch (\Exception $e) {
|
||||
// Log the error but don't throw it - allow transaction to proceed
|
||||
Log::error('StockService::reduceStockForTransaction error: ' . $e->getMessage(), [
|
||||
'transaction_id' => $transaction->id,
|
||||
'work_id' => $transaction->work_id,
|
||||
'dealer_id' => $transaction->dealer_id
|
||||
]);
|
||||
|
||||
// Return true to allow transaction to proceed even if stock reduction fails
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user