/
home
/
sjslayjy
/
public_html
/
ccbfsoution
/
resources
/
views
/
admin
/
Upload File
HOME
@extends('layouts.app') @section('title', 'Fertilizer Management') @section('content') @php $fields = [ 'fertilizer_name' => 'Fertilizer Name', 'fertilizer_type' => 'Fertilizer Type', 'brand_name' => 'Brand Name', 'supplier_name' => 'Supplier Name', 'uom' => 'UOM', ]; // Calculate totals $totalStock = $fertilizers->sum('stock_kg'); $totalAmount = $fertilizers->sum('calculated_amount'); @endphp <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> <style> .btn-add { color: white; background-color: #28a745; padding: 8px 14px; font-weight: 600; border-radius: 8px; font-size: 14px; border: none; } .modal-content { border-radius: 12px; padding: 20px; } table thead { background-color: #28a745; color: white; } table tbody tr:nth-child(even) { background-color: #f8f9fa; } .form-label { font-weight: 500; } .filter-section { background-color: #f8f9fa; padding: 15px; border-radius: 8px; margin-bottom: 20px; } </style> @if(session('success')) <div class="alert alert-success alert-dismissible fade show mt-3" role="alert"> {{ session('success') }} <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> @endif @if(Auth::check() && Auth::user()->role == 1) <div class="filter-section"> <form method="GET" action="{{ route('fertilizer.index') }}"> <div class="row align-items-end"> <div class="col-md-4"> <label class="form-label">Filter by Site</label> <select name="site_id" class="form-select"> <option value="">-- All Sites --</option> @foreach($sites as $site) <option value="{{ $site->id }}" {{ request('site_id') == $site->id ? 'selected' : '' }}> {{ $site->site_name }} </option> @endforeach </select> </div> <div class="col-md-4"> <input type="text" name="search" class="form-control" placeholder="Search..." value="{{ request('search') }}"> </div> <div class="col-md-4"> <button class="btn btn-primary" type="submit"> <i class="fas fa-filter"></i> Filter </button> <a href="{{ route('fertilizer.index') }}" class="btn btn-secondary"> <i class="fas fa-refresh"></i> Reset </a> </div> </div> </form> </div> @endif <div class="d-flex justify-content-between align-items-center mt-4"> @if(Auth::check() && Auth::user()->role != 1) <button class="btn-add" data-bs-toggle="modal" data-bs-target="#addFertilizerModal"> <i class="fas fa-plus"></i> Add Fertilizer </button> @endif @if(Auth::check() && Auth::user()->role != 1) <form method="GET" action="{{ route('fertilizer.index') }}" class="input-group shadow-sm rounded-pill w-25"> <input type="text" name="search" class="form-control border-0" placeholder="Search..." value="{{ request('search') }}"> <button class="btn btn-light border-0" type="submit"> <i class="fas fa-search text-secondary"></i> </button> </form> @endif </div> <div class="table-responsive mt-4"> <table class="table table-bordered text-center align-middle"> <thead> <tr> <th>Sr.No.</th> @if(Auth::check() && Auth::user()->role == 1) <th>Location</th> @endif <th>Fertilizer Name</th> <th>Type</th> <th>Brand</th> <th>Stock</th> <th>UOM</th> <th>Purchase Date</th> <th>Expiry</th> <th>Supplier</th> <th>Fertilizer Price/Kg (Rs)</th> <th> Amount (Rs)</th> @if(Auth::check() && Auth::user()->role != 1) <th>Actions</th> @endif </tr> </thead> <tbody> @forelse ($fertilizers as $index => $fertilizer) <tr> <td>{{ $index + 1 }}</td> @if(Auth::check() && Auth::user()->role == 1) <td>{{ $fertilizer->site_name }}</td> @endif <td>{{ $fertilizer->fertilizer_name }}</td> <td>{{ $fertilizer->fertilizer_type }}</td> <td>{{ $fertilizer->brand_name }}</td> <td>{{ $fertilizer->stock_kg }}</td> <td>{{ $fertilizer->uom }}</td> <td>{{ \Carbon\Carbon::parse($fertilizer->purchase_date)->format('d-m-Y') }}</td> <td>{{ \Carbon\Carbon::parse($fertilizer->expiry_date)->format('d-m-Y') }}</td> <td>{{ $fertilizer->supplier_name }}</td> <td>{{ $fertilizer->rate }}</td> <td>{{ number_format($fertilizer->calculated_amount, 2) }}</td> @if(Auth::check() && Auth::user()->role != 1) <td class="d-flex justify-content-center gap-2"> <button class="btn btn-sm" style="background-color: #FFD700; color: black;" data-bs-toggle="modal" data-bs-target="#editFertilizerModal{{ $fertilizer->id }}"> <i class="fas fa-edit"></i> </button> <button class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#addStockModal{{ $fertilizer->id }}"> <i class="fas fa-plus"></i> </button> <button class="btn btn-info btn-sm view-history-btn" data-fertilizer-id="{{ $fertilizer->id }}" data-bs-toggle="modal" data-bs-target="#historyModal"> <i class="fas fa-history"></i> View History </button> <form action="{{ route('fertilizer.de', $fertilizer->id) }}" method="POST"> @csrf @method('DELETE') <button onclick="return confirm('Are you sure?')" class="btn btn-sm btn-danger"> <i class="fas fa-trash-alt"></i> </button> </form> </td> @endif </tr> @if(Auth::check() && Auth::user()->role != 1) <div class="modal fade" id="editFertilizerModal{{ $fertilizer->id }}" tabindex="-1" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <form action="{{ route('fertilizer.update', $fertilizer->id) }}" method="POST"> @csrf @method('PUT') <div class="modal-header"> <h5 class="modal-title">Edit Fertilizer</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <div class="row g-3"> @if(Auth::user()->role == 1) <div class="col-md-4"> <label class="form-label">Location</label> <select name="site_id" class="form-select" required> <option value="">-- Select Site --</option> @foreach($sites as $site) <option value="{{ $site->id }}" {{ $fertilizer->site_id == $site->id ? 'selected' : '' }}>{{ $site->site_name }}</option> @endforeach </select> </div> @else <div class="col-md-4"> <label class="form-label">Location</label> @php $userSite = $sites->where('id', Auth::user()->site_id)->first(); @endphp <input type="text" class="form-control" value="{{ $userSite ? $userSite->site_name : 'N/A' }}" readonly> <input type="hidden" name="site_id" value="{{ Auth::user()->site_id }}"> </div> @endif @foreach($fields as $field => $label) <div class="col-md-4"> <label class="form-label">{{ $label }}</label> <input type="text" class="form-control" name="{{ $field }}" value="{{ $fertilizer->$field }}" required> </div> @endforeach <div class="col-md-6"> <label class="form-label">Purchase Date</label> <input type="date" class="form-control" name="purchase_date" value="{{ $fertilizer->purchase_date }}" required> </div> <div class="col-md-6"> <label class="form-label">Expiry Date</label> <input type="date" class="form-control" name="expiry_date" value="{{ $fertilizer->expiry_date }}" required> </div> <div class="col-md-6"> <label class="form-label">Rate (Rs)</label> <input type="number" class="form-control" name="rate" value="{{ $fertilizer->rate }}" required> </div> <div class="col-md-6"> <label class="form-label">Current Stock (kg)</label> <input type="number" class="form-control" name="stock_kg" value="{{ $fertilizer->stock_kg }}" readonly> <small class="text-muted">Managed separately</small> </div> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-success w-100">Update Fertilizer</button> </div> </form> </div> </div> </div> @endif <div class="modal fade" id="addStockModal{{ $fertilizer->id }}" tabindex="-1" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <form action="{{ route('fertilizers.addStock', $fertilizer->id) }}" method="POST"> @csrf <div class="modal-header"> <h5 class="modal-title">Add Stock for {{ $fertilizer->fertilizer_name }}</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <div class="mb-3"> <label class="form-label">Current Stock (kg)</label> <input type="number" class="form-control" value="{{ $fertilizer->stock_kg }}" readonly> <small class="text-muted">This is the current stock before adding.</small> </div> <div class="mb-3"> <label class="form-label">Added Quantity (kg)</label> <input type="number" step="0.01" name="quantity_to_add" class="form-control" required> </div> <div class="mb-3"> <label class="form-label">New Price (Rs)</label> <input type="number" step="0.01" name="new_rate" class="form-control"> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-success w-100">Add Stock</button> </div> </form> </div> </div> </div> @empty <tr> <td colspan="{{ Auth::check() && Auth::user()->role == 1 ? '12' : '11' }}">No fertilizers found.</td> </tr> @endforelse </tbody> @if ($fertilizers->count() > 0) <tfoot> <tr class="fw-bold"> <td colspan="{{ Auth::check() && Auth::user()->role == 1 ? '5' : '4' }}">Total</td> <td>{{ number_format($totalStock, 2) }}</td> <td colspan="5"></td> <td>{{ number_format($totalAmount, 2) }}</td> @if(Auth::check() && Auth::user()->role != 1) <td></td> @endif </tr> </tfoot> @endif </table> </div> @if(Auth::check() && Auth::user()->role != 1) <div class="modal fade" id="addFertilizerModal" tabindex="-1" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <form action="{{ route('fertilizer.store') }}" method="POST"> @csrf <div class="modal-header"> <h5 class="modal-title">Add Fertilizer</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <div class="row g-3"> @if(Auth::user()->role == 1) <div class="col-md-4"> <label class="form-label">Location</label> <select name="site_id" class="form-select" required> <option value="">-- Select Site --</option> @foreach($sites as $site) <option value="{{ $site->id }}">{{ $site->site_name }}</option> @endforeach </select> </div> @else <div class="col-md-4"> <label class="form-label">Location</label> @php $userSite = $sites->where('id', Auth::user()->site_id)->first(); @endphp <input type="text" class="form-control" value="{{ $userSite ? $userSite->site_name : 'N/A' }}" readonly> <input type="hidden" name="site_id" value="{{ Auth::user()->site_id }}"> </div> @endif @foreach($fields as $field => $label) <div class="col-md-4"> <label class="form-label">{{ $label }}</label> <input type="text" class="form-control" name="{{ $field }}" required> </div> @endforeach <div class="col-md-6"> <label class="form-label">Purchase Date</label> <input type="date" class="form-control" name="purchase_date" required> </div> <div class="col-md-6"> <label class="form-label">Expiry Date</label> <input type="date" class="form-control" name="expiry_date" required> </div> <div class="col-md-6"> <label class="form-label">Rate (Rs)</label> <input type="number" class="form-control" name="rate" required> </div> <div class="col-md-6"> <label class="form-label">Initial Stock (kg)</label> <input type="number" class="form-control" name="stock_kg" step="0.01" required> </div> </div> </div> <div class="modal-footer"> <button type="submit" class="btn-add">Add Fertilizer</button> </div> </form> </div> </div> </div> @endif <div class="modal fade" id="historyModal" tabindex="-1" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header bg-primary text-white"> <h5 class="modal-title" id="historyModalTitle">Fertilizer Stock History</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <div class="card p-3 mb-3"> <h6 id="historyFertilizerName"><strong>Fertilizer Name:</strong> Loading...</h6> <p id="historyFertilizerDetails"><strong>Type:</strong> Loading... | <strong>Brand:</strong> Loading...</p> <p><strong>Current Stock:</strong> <span class="text-success fw-bold" id="historyCurrentStock">Loading...</span></p> </div> <h6 class="text-primary">Stock History</h6> <div id="loadingSpinner" class="text-center my-4 d-none"> <div class="spinner-border text-primary" role="status"> <span class="visually-hidden">Loading...</span> </div> <p class="mt-2">Loading history...</p> </div> <div id="historyMessage" class="alert alert-info text-center d-none"></div> <table class="table table-bordered table-striped"> <thead class="bg-success text-white"> <tr> <th>Date</th> <th>Previous fertilizer Stock </th> <th>Added Quantity (kg)</th> <th>New Price (Rs)</th> </tr> </thead> <tbody id="historyTableBody"> </tbody> </table> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script> <script> document.addEventListener('DOMContentLoaded', function () { const historyModal = new bootstrap.Modal(document.getElementById('historyModal')); const viewHistoryButtons = document.querySelectorAll('.view-history-btn'); const historyModalTitle = document.getElementById('historyModalTitle'); const historyFertilizerName = document.getElementById('historyFertilizerName'); const historyFertilizerDetails = document.getElementById('historyFertilizerDetails'); const historyCurrentStock = document.getElementById('historyCurrentStock'); const historyTableBody = document.getElementById('historyTableBody'); const loadingSpinner = document.getElementById('loadingSpinner'); const historyMessage = document.getElementById('historyMessage'); // ✅ Function to format date as DD-MM-YY function formatDateDDMMYY(dateStr) { const dateObj = new Date(dateStr); const day = String(dateObj.getDate()).padStart(2, '0'); const month = String(dateObj.getMonth() + 1).padStart(2, '0'); const year = String(dateObj.getFullYear()).slice(-2); return `${day}-${month}-${year}`; } viewHistoryButtons.forEach(button => { button.addEventListener('click', async function () { const fertilizerId = encodeURIComponent(this.dataset.fertilizerId); // Clear previous data and show loading spinner historyTableBody.innerHTML = ''; historyMessage.classList.add('d-none'); loadingSpinner.classList.remove('d-none'); // Reset header info historyModalTitle.textContent = 'Fertilizer Stock History'; historyFertilizerName.innerHTML = '<strong>Fertilizer Name:</strong> Loading...'; historyFertilizerDetails.innerHTML = '<strong>Type:</strong> Loading... | <strong>Brand:</strong> Loading...'; historyCurrentStock.textContent = 'Loading...'; try { // Fetch fertilizer details const fertilizerResponse = await fetch(`/api/fertilizers/${fertilizerId}`); if (!fertilizerResponse.ok) throw new Error(`HTTP error! status: ${fertilizerResponse.status}`); const fertilizerData = await fertilizerResponse.json(); // Populate fertilizer details historyModalTitle.textContent = `Fertilizer Stock History for ${fertilizerData.fertilizer_name}`; historyFertilizerName.innerHTML = `<strong>Fertilizer Name:</strong> ${fertilizerData.fertilizer_name}`; historyFertilizerDetails.innerHTML = `<strong>Type:</strong> ${fertilizerData.fertilizer_type} | <strong>Brand:</strong> ${fertilizerData.brand_name}`; historyCurrentStock.textContent = `${fertilizerData.stock_kg} kg`; // Fetch history records const historyResponse = await fetch(`/api/fertilizers/${fertilizerId}/history`); if (!historyResponse.ok) throw new Error(`HTTP error! status: ${historyResponse.status}`); const historyRecords = await historyResponse.json(); loadingSpinner.classList.add('d-none'); if (historyRecords.length > 0) { historyRecords.forEach(record => { const row = document.createElement('tr'); row.innerHTML = ` <td>${formatDateDDMMYY(record.created_at)}</td> <td>${record.stock_before_addition ?? 'N/A'}</td> <td>${record.added_quantity ?? 'N/A'}</td> <td>₹${record.rate ?? 'N/A'}</td> `; historyTableBody.appendChild(row); }); } else { historyMessage.textContent = 'No history found for this fertilizer.'; historyMessage.classList.remove('d-none'); } } catch (error) { console.error('Error fetching fertilizer history:', error); loadingSpinner.classList.add('d-none'); historyMessage.textContent = 'Failed to load history. Please try again.'; historyMessage.classList.remove('d-none'); } finally { historyModal.show(); } }); }); }); </script> @endsection