📁 File Manager
Editing:
/home/wvl7mof7402e/www/domain/billing/create_invoice.php
<?php include '../db.php'; function generateInvoiceNumber($conn) { $last = $conn->query("SELECT invoice_number FROM invoices ORDER BY id DESC LIMIT 1")->fetchColumn(); if (!$last) return 'INV-0001'; $num = intval(substr($last, 4)) + 1; return 'INV-' . str_pad($num, 4, '0', STR_PAD_LEFT); } $clients = $conn->query("SELECT id, name FROM clients")->fetchAll(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $client_id = $_POST['client_id']; $services = $_POST['services']; // array of ['type'=>'domain', 'id'=>..., 'description'=>..., 'amount'=>...] $gst = 18.0; $total = 0; foreach ($services as $s) { $total += floatval($s['amount']); } $gst_amount = $total * ($gst / 100); $net_amount = $total + $gst_amount; $invoice_number = generateInvoiceNumber($conn); // Insert invoice $stmt = $conn->prepare("INSERT INTO invoices (client_id, invoice_number, gst_percentage, total_amount, gst_amount, net_amount) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$client_id, $invoice_number, $gst, $total, $gst_amount, $net_amount]); $invoice_id = $conn->lastInsertId(); // Insert invoice items $stmt_item = $conn->prepare("INSERT INTO invoice_items (invoice_id, service_type, service_id, description, amount) VALUES (?, ?, ?, ?, ?)"); foreach ($services as $s) { $stmt_item->execute([$invoice_id, $s['type'], $s['id'], $s['description'], $s['amount']]); } header("Location: view_invoice.php?id=$invoice_id"); exit; } ?> <h2>Create Invoice</h2> <form method="post" id="invoiceForm"> <label>Client:</label> <select name="client_id" required> <?php foreach ($clients as $c) echo "<option value='{$c['id']}'>{$c['name']}</option>"; ?> </select><br><br> <h3>Services</h3> <div id="servicesContainer"> <!-- Service entries will be added here dynamically --> </div> <button type="button" onclick="addService()">Add Service</button><br><br> <strong>Total: </strong><span id="totalAmount">0</span><br> <strong>GST (18%): </strong><span id="gstAmount">0</span><br> <strong>Net Amount: </strong><span id="netAmount">0</span><br> <input type="hidden" name="services" id="servicesInput"> <button type="submit">Generate Invoice</button> </form> <script> let services = []; function addService() { let type = prompt('Service Type (domain, hosting, email):'); if (!['domain','hosting','email'].includes(type)) { alert('Invalid service type'); return; } let id = prompt('Service ID (numeric):'); let description = prompt('Description:'); let amount = parseFloat(prompt('Amount:')); if (isNaN(amount) || amount <= 0) { alert('Invalid amount'); return; } services.push({type, id, description, amount}); renderServices(); updateTotals(); } function renderServices() { let container = document.getElementById('servicesContainer'); container.innerHTML = ''; services.forEach((s, i) => { container.innerHTML += `<div> ${s.type} | ${s.description} | ?${s.amount.toFixed(2)} <button type="button" onclick="removeService(${i})">Remove</button> </div>`; }); document.getElementById('servicesInput').value = JSON.stringify(services); } function removeService(index) { services.splice(index,1); renderServices(); updateTotals(); } function updateTotals() { let total = services.reduce((acc, s) => acc + s.amount, 0); let gst = total * 0.18; let net = total + gst; document.getElementById('totalAmount').innerText = '?' + total.toFixed(2); document.getElementById('gstAmount').innerText = '?' + gst.toFixed(2); document.getElementById('netAmount').innerText = '?' + net.toFixed(2); } </script>
Save
Cancel