:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/crm/quatation/**
:: Editing File: edit_vendor_invoice.php
<?php include_once("session.php"); include("db.php"); // $conn (mysqli) include("header.php"); // ---------- 1️⃣ Validate Invoice ID ---------- if (!isset($_GET['id']) || empty($_GET['id'])) { die("Error: Invoice ID is missing."); } $invoice_id = (int)$_GET['id']; // ---------- 2️⃣ Fetch Master Invoice Record (prepared) ---------- $invoice = null; $stmt = mysqli_prepare($conn, "SELECT * FROM vendor_invoice WHERE id = ? LIMIT 1"); if ($stmt) { mysqli_stmt_bind_param($stmt, "i", $invoice_id); mysqli_stmt_execute($stmt); $res = mysqli_stmt_get_result($stmt); $invoice = $res ? mysqli_fetch_assoc($res) : null; mysqli_stmt_close($stmt); } if (!$invoice) { die("Error: Invoice not found."); } // ---------- 3️⃣ Fetch Invoice Item Rows (prepared) ---------- $items = []; $stmt2 = mysqli_prepare($conn, "SELECT * FROM vendor_invoice_items WHERE vendor_invoice_id = ?"); if ($stmt2) { mysqli_stmt_bind_param($stmt2, "i", $invoice_id); mysqli_stmt_execute($stmt2); $res2 = mysqli_stmt_get_result($stmt2); while ($row = mysqli_fetch_assoc($res2)) { $items[] = $row; } mysqli_stmt_close($stmt2); } // ---------- 4️⃣ Fetch Vendor List (from party table where type='Vendor') ---------- $vendors = []; $vstmt = mysqli_prepare($conn, "SELECT id, name FROM party WHERE type = 'Vendor' ORDER BY name ASC"); if ($vstmt) { mysqli_stmt_execute($vstmt); $vres = mysqli_stmt_get_result($vstmt); while ($vrow = mysqli_fetch_assoc($vres)) $vendors[] = $vrow; mysqli_stmt_close($vstmt); } else { // fallback: query directly $vres = mysqli_query($conn, "SELECT id, name FROM party WHERE type='Vendor' ORDER BY name ASC"); while ($vrow = mysqli_fetch_assoc($vres)) $vendors[] = $vrow; } // ---------- 5️⃣ Fetch tax options (CGST / SGST) ---------- $cgst_options = []; $sgst_options = []; $tax_q = mysqli_query($conn, "SELECT id, tax_name, tax_percentage FROM taxes WHERE status='active' ORDER BY tax_percentage ASC"); if ($tax_q) { while ($t = mysqli_fetch_assoc($tax_q)) { $name_up = strtoupper($t['tax_name']); $opt_text = htmlspecialchars($t['tax_name']) . " (" . htmlspecialchars($t['tax_percentage']) . " %)"; $opt_val = (int)$t['id']; if (strpos($name_up, 'CGST') !== false) $cgst_options[] = ['id'=>$opt_val,'text'=>$opt_text,'perc'=>$t['tax_percentage']]; elseif (strpos($name_up, 'SGST') !== false) $sgst_options[] = ['id'=>$opt_val,'text'=>$opt_text,'perc'=>$t['tax_percentage']]; } } ?> <main> <div class="container-fluid"> <div class="row m-1"> <div class="col-12"> <h4 class="main-title">Edit Vendor Invoice</h4> <ul class="app-line-breadcrumbs mb-3"> <li><a href="dashboard.php">Home</a></li> <li><a href="vendor_invoice_list.php">Vendor Invoice List</a></li> <li class="active">Edit Invoice</li> </ul> </div> </div> <form class="row g-3 app-form" method="post" action="update_vendor_invoice.php" enctype="multipart/form-data"> <input type="hidden" name="vendor_invoice_id" value="<?php echo (int)$invoice['id']; ?>"> <input type="hidden" name="user_id" value="<?php echo (int)($_SESSION['user_id'] ?? 0); ?>"> <h5 class="mt-3">Invoice Details</h5> <div class="col-md-6"> <label class="form-label">Invoice Date</label> <input class="form-control" type="date" name="invoice_date" value="<?php echo htmlspecialchars($invoice['invoice_date']); ?>" required> </div> <div class="col-md-6"> <label class="form-label">Invoice No</label> <input class="form-control" name="invoice_no" value="<?php echo htmlspecialchars($invoice['invoice_no']); ?>" required> </div> <h5 class="mt-3">Vendor Information</h5> <div class="col-md-6"> <label class="form-label">Vendor Name</label> <select class="form-control" name="vendor_id" id="vendor_id" required> <option value="">Select Vendor</option> <?php foreach ($vendors as $v): $sel = ($v['id'] == $invoice['vendor_id']) ? "selected" : ""; ?> <option value="<?php echo (int)$v['id']; ?>" <?php echo $sel; ?>> <?php echo htmlspecialchars($v['name']); ?> </option> <?php endforeach; ?> </select> </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 htmlspecialchars($invoice['product_name']); ?>"> </div> <h5 class="mt-3">Vehicle & Delivery Information</h5> <div class="col-md-6"> <label class="form-label">Vehicle Number</label> <input type="text" class="form-control" name="vehicle_number" value="<?php echo htmlspecialchars($invoice['vehicle_number']); ?>"> </div> <div class="col-md-6"> <label class="form-label">Terms of Delivery</label> <input type="text" class="form-control" name="terms_of_delivery" value="<?php echo htmlspecialchars($invoice['terms_of_delivery']); ?>"> </div> <h5 class="mt-3">Invoice Items</h5> <div class="col-12"> <div class="row g-2 mb-1 fw-bold d-none d-md-flex text-center"> <div class="col-md-3">Description</div> <div class="col-md-1">Pack Type</div> <div class="col-md-1">Qty</div> <div class="col-md-2">Net Wt.</div> <div class="col-md-1">Unit</div> <div class="col-md-1">Rate</div> <div class="col-md-2">Amount</div> <div class="col-md-1"></div> </div> </div> <div id="items_container" class="col-12"> <?php if (count($items) > 0): ?> <?php foreach ($items as $it): ?> <div class="row g-2 mb-2 item-row"> <input type="hidden" name="item_id[]" value="<?php echo (int)$it['id']; ?>"> <div class="col-md-3"><input class="form-control" name="description[]" value="<?php echo htmlspecialchars($it['description']); ?>"></div> <div class="col-md-1"><input class="form-control" name="pack_type[]" value="<?php echo htmlspecialchars($it['pack_type']); ?>"></div> <div class="col-md-1"><input type="number" step="0.01" class="form-control item-qty" name="qty[]" value="<?php echo htmlspecialchars($it['qty']); ?>"></div> <div class="col-md-2"><input type="number" step="0.01" class="form-control item-weight" name="net_weight[]" value="<?php echo htmlspecialchars($it['net_weight']); ?>"></div> <div class="col-md-1"><input class="form-control" name="unit[]" value="<?php echo htmlspecialchars($it['unit']); ?>"></div> <div class="col-md-1"><input type="number" step="0.01" class="form-control item-rate" name="rate[]" value="<?php echo htmlspecialchars($it['rate']); ?>"></div> <div class="col-md-2"><input type="number" step="0.01" class="form-control item-amount" name="amount[]" value="<?php echo htmlspecialchars($it['amount']); ?>" readonly></div> <div class="col-md-1"><button type="button" class="btn btn-danger btn-sm remove-item">X</button></div> </div> <?php endforeach; ?> <?php else: ?> <!-- If no items, show one blank row --> <div class="row g-2 mb-2 item-row"> <input type="hidden" name="item_id[]" value="0"> <div class="col-md-3"><input class="form-control" name="description[]" placeholder="Description"></div> <div class="col-md-1"><input class="form-control" name="pack_type[]" placeholder="Pack"></div> <div class="col-md-1"><input type="number" step="0.01" class="form-control item-qty" name="qty[]" value="0"></div> <div class="col-md-2"><input type="number" step="0.01" class="form-control item-weight" name="net_weight[]" value="0"></div> <div class="col-md-1"><input class="form-control" name="unit[]" value="Kg"></div> <div class="col-md-1"><input type="number" step="0.01" class="form-control item-rate" name="rate[]" value="0"></div> <div class="col-md-2"><input type="number" step="0.01" class="form-control item-amount" name="amount[]" value="0.00" readonly></div> <div class="col-md-1"><button type="button" class="btn btn-danger btn-sm remove-item">X</button></div> </div> <?php endif; ?> </div> <div class="col-12"> <button type="button" class="btn btn-sm btn-outline-primary" id="add_item_btn">+ Add Item</button> </div> <hr class="my-4"> <div class="row g-2"> <div class="col-md-3"> <label class="form-label fw-bold">Sub Total</label> <input type="text" class="form-control fw-bold" id="sub_total" name="sub_total" value="<?php echo htmlspecialchars($invoice['sub_total'] ?? '0.00'); ?>" readonly> </div> <div class="col-md-3"> <label class="form-label fw-bold">CGST</label> <select name="tax_cgst" id="tax_cgst" class="form-control form-select"> <option value="">Select CGST</option> <?php foreach ($cgst_options as $o): $sel = ((int)$invoice['tax_cgst_id'] === $o['id']) ? 'selected' : ''; ?> <option value="<?php echo $o['id']; ?>" data-perc="<?php echo htmlspecialchars($o['perc']); ?>" <?php echo $sel; ?>><?php echo $o['text']; ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label fw-bold">SGST</label> <select name="tax_sgst" id="tax_sgst" class="form-control form-select"> <option value="">Select SGST</option> <?php foreach ($sgst_options as $o): $sel = ((int)$invoice['tax_sgst_id'] === $o['id']) ? 'selected' : ''; ?> <option value="<?php echo $o['id']; ?>" data-perc="<?php echo htmlspecialchars($o['perc']); ?>" <?php echo $sel; ?>><?php echo $o['text']; ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label fw-bold">Grand Total</label> <input type="text" class="form-control fw-bold" id="grand_total" name="grand_total" value="<?php echo htmlspecialchars($invoice['grand_total'] ?? '0.00'); ?>" readonly> </div> </div> <div class="row g-2 mt-2"> <div class="col-md-3"> <label class="form-label">CGST Amount</label> <input type="text" class="form-control" id="cgst_amount" name="cgst_amount" value="<?php echo htmlspecialchars($invoice['cgst_amount'] ?? '0.00'); ?>" readonly> </div> <div class="col-md-3"> <label class="form-label">SGST Amount</label> <input type="text" class="form-control" id="sgst_amount" name="sgst_amount" value="<?php echo htmlspecialchars($invoice['sgst_amount'] ?? '0.00'); ?>" readonly> </div> </div> <div class="col-md-12 mt-3"> <label class="form-label">Vendor Receipt</label> <input type="file" name="vendor_receipt" class="form-control"> <?php if (!empty($invoice['vendor_receipt'])): ?> <p class="mt-1">Current: <a href="<?php echo htmlspecialchars('uploads/vendor_receipts/' . $invoice['vendor_receipt']); ?>" target="_blank">View Receipt</a></p> <?php endif; ?> </div> <div class="col-12 mt-4"> <button type="submit" class="btn btn-success">Update Invoice</button> </div> </form> </div> </main> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> function addItemRow(prefill = null) { // prefill: optional object with fields (description, pack_type, qty, net_weight, unit, rate) const desc = prefill?.description ?? ''; const pack = prefill?.pack_type ?? ''; const qty = prefill?.qty ?? 0; const wt = prefill?.net_weight ?? 0; const unit = prefill?.unit ?? 'Kg'; const rate = prefill?.rate ?? 0; const amt = prefill?.amount ?? 0; const row = ` <div class="row g-2 mb-2 item-row"> <input type="hidden" name="item_id[]" value="0"> <div class="col-md-3"><input class="form-control" name="description[]" value="${escapeHtml(desc)}" placeholder="Description"></div> <div class="col-md-1"><input class="form-control" name="pack_type[]" value="${escapeHtml(pack)}" placeholder="Pack"></div> <div class="col-md-1"><input type="number" step="0.01" class="form-control item-qty" name="qty[]" value="${qty}"></div> <div class="col-md-2"><input type="number" step="0.01" class="form-control item-weight" name="net_weight[]" value="${wt}"></div> <div class="col-md-1"><input class="form-control" name="unit[]" value="${escapeHtml(unit)}"></div> <div class="col-md-1"><input type="number" step="0.01" class="form-control item-rate" name="rate[]" value="${rate}"></div> <div class="col-md-2"><input type="number" step="0.01" class="form-control item-amount" name="amount[]" value="${parseFloat(amt).toFixed(2)}" readonly></div> <div class="col-md-1"><button type="button" class="btn btn-danger btn-sm remove-item">X</button></div> </div>`; $('#items_container').append(row); } // Utility to escape content inserted into HTML strings function escapeHtml(text) { if (!text) return ''; return String(text) .replace(/&/g, "&") .replace(/"/g, """) .replace(/'/g, "'") .replace(/</g, "<") .replace(/>/g, ">"); } function calculateTotals() { let subTotal = 0; $('.item-row').each(function() { const row = $(this); const netWeight = parseFloat(row.find('.item-weight').val()) || 0; const rate = parseFloat(row.find('.item-rate').val()) || 0; // Amount formula: net_weight * rate (matches server) const amount = netWeight * rate; row.find('.item-amount').val(amount.toFixed(2)); subTotal += amount; }); $('#sub_total').val(subTotal.toFixed(2)); // Get selected tax percentages from data-perc attribute const cgstPerc = parseFloat($('#tax_cgst option:selected').data('perc')) || 0; const sgstPerc = parseFloat($('#tax_sgst option:selected').data('perc')) || 0; const cgstAmount = (subTotal * cgstPerc) / 100; const sgstAmount = (subTotal * sgstPerc) / 100; $('#cgst_amount').val(cgstAmount.toFixed(2)); $('#sgst_amount').val(sgstAmount.toFixed(2)); const grandTotal = subTotal + cgstAmount + sgstAmount; $('#grand_total').val(grandTotal.toFixed(2)); } $(document).ready(function() { // Add new item row (blank) $('#add_item_btn').on('click', function() { addItemRow(); }); // Remove item row $(document).on('click', '.remove-item', function() { $(this).closest('.item-row').remove(); calculateTotals(); }); // Recalculate on net_weight or rate change $(document).on('input', '.item-weight, .item-rate', function() { calculateTotals(); }); // Recalculate when taxes change $(document).on('change', '#tax_cgst, #tax_sgst', function() { calculateTotals(); }); // Initial calculate (use existing rows) calculateTotals(); // AJAX to get Vendor details (vendor_code) - ensures endpoint exists $("#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(''); } }); } else { $("#vendor_code").val(""); } }).trigger('change'); // fetch on load for preselected vendor }); </script> <?php include("footer.php"); ?>