:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/crm/quatation/**
:: Editing File: edit_inward.php
<?php // Your includes remain the same include_once("session.php"); include("header.php"); include("db.php"); // Assumed to contain $conn for mysqli connection // --- 1. GET INWARD ID & CHECK --- if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { $_SESSION['error'] = "Invalid Inward ID provided."; header("Location: inward_list.php"); exit(); } $inward_id = (int)$_GET['id']; // --- 2. FETCH MASTER INWARD DATA --- $sql_master = "SELECT * FROM inward WHERE id = ?"; $stmt_master = mysqli_prepare($conn, $sql_master); mysqli_stmt_bind_param($stmt_master, "i", $inward_id); mysqli_stmt_execute($stmt_master); $result_master = mysqli_stmt_get_result($stmt_master); $inward_data = mysqli_fetch_assoc($result_master); mysqli_stmt_close($stmt_master); if (!$inward_data) { $_SESSION['error'] = "Inward Bill not found."; header("Location: inward_list.php"); exit(); } // --- 3. FETCH BOX BREAKDOWN DATA (One-to-One) --- $sql_box = "SELECT * FROM inward_box WHERE inward_id = ?"; $stmt_box = mysqli_prepare($conn, $sql_box); mysqli_stmt_bind_param($stmt_box, "i", $inward_id); mysqli_stmt_execute($stmt_box); $result_box = mysqli_stmt_get_result($stmt_box); $box_data = mysqli_fetch_assoc($result_box); mysqli_stmt_close($stmt_box); // --- 4. FETCH LINE ITEMS DATA (One-to-Many) --- $sql_items = "SELECT * FROM inward_items WHERE inward_id = ?"; $stmt_items = mysqli_prepare($conn, $sql_items); mysqli_stmt_bind_param($stmt_items, "i", $inward_id); mysqli_stmt_execute($stmt_items); $result_items = mysqli_stmt_get_result($stmt_items); $inward_items = mysqli_fetch_all($result_items, MYSQLI_ASSOC); mysqli_stmt_close($stmt_items); // --- HELPER FUNCTION: Get Safe Value --- function get_val($array, $key, $default = '') { return htmlspecialchars($array[$key] ?? $default); } ?> <main> <div class="container-fluid"> <div class="row m-1"> <div class="col-12"> <h4 class="main-title">Edit Inward Bill #<?php echo $inward_data['id']; ?></h4> <ul class="app-line-breadcrumbs mb-3"> <li><a class="f-s-14 f-w-500" href="dashboard.php"><i class="ph-duotone ph-bank f-s-16"></i> Home</a></li> <li><a class="f-s-14 f-w-500" href="inward_list.php"><i class="ph-duotone ph-table f-s-16"></i> Inward List</a></li> <li class="active"><a class="f-s-14 f-w-500" href="#">Edit Inward Bill</a></li> </ul> </div> </div> <div class="row"> <div class="col-12"> <div class="card"> <div class="card-body"> <?php // Session messages for success/error if (isset($_SESSION['success'])) { echo '<div class="alert alert-success">' . $_SESSION['success'] . '</div>'; unset($_SESSION['success']); } if (isset($_SESSION['error'])) { echo '<div class="alert alert-danger">' . $_SESSION['error'] . '</div>'; unset($_SESSION['error']); } ?> <form class="row g-3 app-form" method="post" action="update_inward.php" enctype="multipart/form-data"> <input type="hidden" name="inward_id" value="<?php echo $inward_id; ?>"> <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']; ?>"> <h5 class="mt-3">Inward/Bill Details</h5> <div class="col-md-6"> <label class="form-label">Inward Date</label> <input class="form-control" type="date" name="inward_date" value="<?php echo get_val($inward_data, 'inward_date'); ?>" required> </div> <div class="col-md-6"> <label class="form-label">Inward Challan No (BARJEEL)</label> <input class="form-control" name="inward_no" placeholder="Challan No" value="<?php echo get_val($inward_data, 'inward_no'); ?>"> </div> <div class="col-md-6"> <label class="form-label">Bill No (PAN EXPORTS)</label> <input class="form-control" name="billno" placeholder="Bill No" value="<?php echo get_val($inward_data, 'billno'); ?>"> </div> <h5 class="mt-3">Party Supplier Information</h5> <div class="col-md-6"> <label class="form-label">Party Name (Supplier)</label> <select class="form-control form-select" id="vendor_id" name="vendor_id" required> <option value="">Select Party</option> <?php $query = "SELECT id, name FROM party ORDER BY name ASC"; $result = mysqli_query($conn, $query); while ($row = mysqli_fetch_assoc($result)) { $selected = ($row['id'] == $inward_data['vendor_id']) ? 'selected' : ''; echo "<option value='" . $row['id'] . "' " . $selected . ">" . htmlspecialchars($row['name']) . "</option>"; } ?> </select> </div> <!--<div class="col-md-4"> <label class="form-label">Vendor Code</label> <input type="text" class="form-control" id="vendor_code" name="vendor_code" value="" readonly> </div> --> <div class="col-md-6"> <label class="form-label">Product Name</label> <input type="text" class="form-control" id="product_name" name="product_name" value="<?php echo get_val($inward_data, 'product_name'); ?>"> </div> <h5 class="mt-3"><br>Brand & Box Counts</h5> <div class="col-md-12"> <label class="form-label">Brand Description</label> <input type="text" class="form-control" name="brand_desc" placeholder="e.g., B.G Banana" value="<?php echo get_val($inward_data, 'brand_desc'); ?>"> </div> <?php $query = "SELECT category_name FROM box_category WHERE status = 1 ORDER BY id ASC"; $result = mysqli_query($conn, $query); if ($result && mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { $category_name = htmlspecialchars($row['category_name']); $input_key = 'box_' . str_replace([' ', '/', '-'], '', $category_name); $box_value = get_val($box_data, $input_key, 0); echo ' <div class="col-md-2 mb-3"> <label class="form-label">'.$category_name.'</label> <input type="number" class="form-control box-count-input" id="'.$input_key.'" name="'.$input_key.'" value="'.$box_value.'" min="0" oninput="updateTotalBox()"> </div>'; } } ?> <div class="col-md-2"> <label class="form-label">Total Box (All Types)</label> <input type="number" class="form-control" id="total_box_count" name="total_box_count" value="<?php echo get_val($inward_data, 'total_box_count', 0); ?>" readonly> </div> <!-- <h5 class="mt-3"><br>Items/Material Received</h5> <div id="items_container" class="col-12"> <div class="row g-2 mb-1 fw-bold d-none d-md-flex"> <div class="col-md-3">Description/S.No</div> <div class="col-md-2">Type of Good</div> <div class="col-md-2">Total Box (Item)</div> <div class="col-md-2">Wastage KG</div> <div class="col-md-2">Actual Wastage KG</div> <div class="col-md-1"></div> </div> <?php if (!empty($inward_items)): ?> <?php foreach ($inward_items as $item): ?> <div class="row g-2 mb-2 item-row"> <input type="hidden" name="item_id[]" value="<?php echo $item['id']; ?>"> <div class="col-md-3 col-6"><input class="form-control form-control-sm" name="item_desc[]" placeholder="Description/S.No" required value="<?php echo get_val($item, 'description'); ?>"></div> <div class="col-md-2 col-6"><input class="form-control form-control-sm" name="item_good[]" placeholder="Type of Good" required value="<?php echo get_val($item, 'type_of_good'); ?>"></div> <div class="col-md-2 col-4"><input type="number" step="0.01" class="form-control form-control-sm item_box" name="item_total_box[]" placeholder="Total Box" value="<?php echo get_val($item, 'total_box', 0); ?>" min="0"></div> <div class="col-md-2 col-4"><input type="number" step="0.01" class="form-control form-control-sm item_wastage" name="item_wastage_kg[]" placeholder="Wastage KG" value="<?php echo get_val($item, 'wastage_kg', 0); ?>" min="0"></div> <div class="col-md-2 col-3"><input type="number" step="0.01" class="form-control form-control-sm item_actual_wastage" name="item_actual_wastage_kg[]" placeholder="Actual Wastage KG" value="<?php echo get_val($item, 'actual_wastage_kg', 0); ?>" min="0"></div> <div class="col-md-1 col-1"><button type="button" class="btn btn-danger btn-sm remove-item">X</button></div> </div> <?php endforeach; ?> <?php endif; ?> </div> <div class="col-12"> <button type="button" class="btn btn-sm btn-outline-primary" onclick="addItem()">+ Add Item</button><br><br> </div> <div class="row"> <div class="col-md-4 "> <label class="form-label fw-bold">Grand Total Box (from Items)</label> <input type="text" class="form-control fw-bold" id="grand_total_item_box" name="grand_total_item_box" readonly> </div> <div class="col-md-4 "> <label class="form-label fw-bold">Grand Total Wastage KG</label> <input type="text" class="form-control fw-bold" id="grand_wastage_kg" name="grand_wastage_kg" readonly> </div> <div class="col-md-4 "> <label class="form-label fw-bold">Grand Total Actual Wastage KG</label> <input type="text" class="form-control fw-bold" id="grand_actual_wastage_kg" name="grand_actual_wastage_kg" readonly> </div> </div> --> <h5 class="mt-3">Logistics & Cold Storage Information</h5> <div class="col-md-4"> <label class="form-label">Name Of Supervisior</label> <input class="form-control" name="supervisior_name" placeholder="Name Of Supervisior" value="<?php echo get_val($inward_data, 'supervisior_name'); ?>"> </div> <div class="col-md-4"> <label class="form-label">Vehicle Number</label> <input class="form-control" name="vehicle_number" placeholder="Vehicle Number" value="<?php echo get_val($inward_data, 'vehicle_number'); ?>"> </div> <div class="col-md-4"> <label class="form-label">Driver Mobile</label> <input class="form-control" name="driver_number" placeholder="Driver Mobile" value="<?php echo get_val($inward_data, 'driver_number'); ?>"> </div> <div class="col-md-6"> <label class="form-label">Dispatch Time</label> <?php $dispatch_time = $inward_data['dispatch_time'] ? date('Y-m-d\TH:i', strtotime($inward_data['dispatch_time'])) : ''; ?> <input class="form-control" type="datetime-local" name="dispatch_time" value="<?php echo $dispatch_time; ?>" > </div> <div class="col-md-6"> <label class="form-label">Cold Storage Name</label> <select class="form-control form-select" name="cold_storage_id" required> <option value="">Select Cold Storage Name</option> <?php $query = "SELECT id, company_name FROM cold_storage ORDER BY company_name ASC"; $result = mysqli_query($conn, $query); while ($row = mysqli_fetch_assoc($result)) { $selected = ($row['id'] == $inward_data['cold_storage_id']) ? 'selected' : ''; echo "<option value='" . $row['id'] . "' " . $selected . ">" . htmlspecialchars($row['company_name']) . "</option>"; } ?> </select> </div> <div class="col-md-12"> <label class="form-label">Inward Receipt/Challan Image (Current: <?php if (!empty($inward_data['inward_receipt'])): ?> <a href="<?php echo get_val($inward_data, 'inward_receipt'); ?>" target="_blank" style="color:#ff0000">View File</a> <?php else: ?> No File Uploaded <?php endif; ?> )</label> <input type="file" name="inward_receipt" id="inward_receipt" class="form-control"> <small class="text-muted">Upload a new file to replace the existing one. Leave blank to keep current file.</small> </div> <div class="col-12 mt-4"> <button type="submit" class="btn btn-primary">Update Inward Bill</button> </div> </form> </div> </div> </div> </div> </div> </main> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> // Attach listeners and run initial calculations on page load document.addEventListener('DOMContentLoaded', function() { // 1. Attach change listener to all box count inputs document.querySelectorAll('.box-count-input').forEach(function(input) { input.addEventListener('input', updateTotalBox); }); // 2. Attach change listener to all item detail inputs (existing rows) document.querySelectorAll('.item-row input[type="number"]').forEach(function(input) { input.addEventListener('input', calculateGrandTotals); }); // 3. Attach remove handler to existing item rows document.querySelectorAll('.item-row .remove-item').forEach(function(button) { button.addEventListener('click', function () { this.closest('.item-row').remove(); calculateGrandTotals(); }); }); // 4. Run initial calculations updateTotalBox(); calculateGrandTotals(); // 5. Trigger vendor AJAX to populate vendor code on load $('#vendor_id').trigger('change'); }); // --- BOX COUNT LOGIC (Same as add_inward.php) --- function updateTotalBox() { let total = 0; document.querySelectorAll('.box-count-input').forEach(function(input) { const v = input.value.trim(); if (v !== '' && !isNaN(v)) total += parseFloat(v); }); const totalEl = document.getElementById('total_box_count'); if (totalEl) totalEl.value = total; } // --- ITEM DETAILS LOGIC (Same as add_inward.php) --- function addItem() { const itemsContainer = document.getElementById('items_container'); const itemRow = document.createElement('div'); itemRow.classList.add('row', 'g-2', 'mb-2', 'item-row'); itemRow.innerHTML = ` <input type="hidden" name="item_id[]" value=""> <div class="col-md-3 col-6"><input class="form-control form-control-sm" name="item_desc[]" placeholder="Description/S.No" required></div> <div class="col-md-2 col-6"><input class="form-control form-control-sm" name="item_good[]" placeholder="Type of Good" required></div> <div class="col-md-2 col-4"><input type="number" step="0.01" class="form-control form-control-sm item_box" name="item_total_box[]" placeholder="Total Box" value="0" min="0"></div> <div class="col-md-2 col-4"><input type="number" step="0.01" class="form-control form-control-sm item_wastage" name="item_wastage_kg[]" placeholder="Wastage KG" value="0" min="0"></div> <div class="col-md-2 col-3"><input type="number" step="0.01" class="form-control form-control-sm item_actual_wastage" name="item_actual_wastage_kg[]" placeholder="Actual Wastage KG" value="0" min="0"></div> <div class="col-md-1 col-1"><button type="button" class="btn btn-danger btn-sm remove-item">X</button></div> `; itemsContainer.appendChild(itemRow); // Recalculate whenever user types in new row itemRow.querySelectorAll('.item_box, .item_wastage, .item_actual_wastage').forEach(function(input) { input.addEventListener('input', calculateGrandTotals); }); // Remove row and recalc itemRow.querySelector('.remove-item').addEventListener('click', function () { itemRow.remove(); calculateGrandTotals(); }); } function calculateGrandTotals() { let totalItemBox = 0; let totalWastage = 0; let totalActualWastage = 0; // Sum all item box values document.querySelectorAll('.item-row .item_box').forEach(function (el) { const val = parseFloat(el.value); if (!isNaN(val)) totalItemBox += val; }); // Sum all wastage KG values document.querySelectorAll('.item-row .item_wastage').forEach(function (el) { const val = parseFloat(el.value); if (!isNaN(val)) totalWastage += val; }); // Sum all actual wastage KG values document.querySelectorAll('.item-row .item_actual_wastage').forEach(function (el) { const val = parseFloat(el.value); if (!isNaN(val)) totalActualWastage += val; }); // Update the grand total fields document.getElementById('grand_total_item_box').value = totalItemBox.toFixed(2); document.getElementById('grand_wastage_kg').value = totalWastage.toFixed(2); document.getElementById('grand_actual_wastage_kg').value = totalActualWastage.toFixed(2); } // --- JQUERY AJAX (Same as add_inward.php) --- $(document).ready(function() { // AJAX to get Vendor details $("#vendor_id").change(function() { var vendorId = $(this).val(); if (vendorId) { $.ajax({ url: "get_vendor_details.php", type: "POST", data: { vendor_id: vendorId }, dataType: "json", success: function(data) { $("#vendor_code").val(data.vendor_code); }, error: function() { $("#vendor_code").val("Error fetching code"); } }); } else { $("#vendor_code").val(""); } }); }); </script> <?php include("footer.php"); ?>