Here's a complete single HTML file with embedded JavaScript for a simple invoicing app with payments management. The data is stored in JSON format using localStorage for persistence:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Invoicing App</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.container {
display: flex;
gap: 20px;
}
.section {
flex: 1;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 8px;
border: 1px solid #ddd;
text-align: left;
}
th {
background-color: #f5f5f5;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input, select {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
button {
padding: 8px 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.status-paid {
color: green;
font-weight: bold;
}
.status-pending {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Simple Invoicing App</h1>
<div class="container">
<div class="section">
<h2>Create Invoice</h2>
<div class="form-group">
<label for="clientName">Client Name:</label>
<input type="text" id="clientName" required>
</div>
<div class="form-group">
<label for="amount">Amount:</label>
<input type="number" id="amount" step="0.01" required>
</div>
<div class="form-group">
<label for="dueDate">Due Date:</label>
<input type="date" id="dueDate" required>
</div>
<button onclick="createInvoice()">Create Invoice</button>
</div>
<div class="section">
<h2>Record Payment</h2>
<div class="form-group">
<label for="paymentInvoiceId">Invoice:</label>
<select id="paymentInvoiceId"></select>
</div>
<div class="form-group">
<label for="paymentAmount">Payment Amount:</label>
<input type="number" id="paymentAmount" step="0.01" required>
</div>
<div class="form-group">
<label for="paymentDate">Payment Date:</label>
<input type="date" id="paymentDate" required>
</div>
<button onclick="recordPayment()">Record Payment</button>
</div>
</div>
<div class="section">
<h2>Invoices</h2>
<table id="invoicesTable">
<thead>
<tr>
<th>ID</th>
<th>Client</th>
<th>Amount</th>
<th>Paid</th>
<th>Due Date</th>
<th>Status</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="section">
<h2>Payments</h2>
<table id="paymentsTable">
<thead>
<tr>
<th>Invoice ID</th>
<th>Amount</th>
<th>Date</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
// Data storage
let data = {
invoices: [],
payments: []
};
// Load data from localStorage on page load
function loadData() {
const storedData = localStorage.getItem('invoicingData');
if (storedData) {
data = JSON.parse(storedData);
}
updateUI();
}
// Save data to localStorage
function saveData() {
localStorage.setItem('invoicingData', JSON.stringify(data));
}
// Create new invoice
function createInvoice() {
const clientName = document.getElementById('clientName').value;
const amount = parseFloat(document.getElementById('amount').value);
const dueDate = document.getElementById('dueDate').value;
if (!clientName || !amount || !dueDate) {
alert('Please fill in all fields');
return;
}
const invoice = {
id: Date.now(),
clientName,
amount,
dueDate,
createdDate: new Date().toISOString()
};
data.invoices.push(invoice);
saveData();
updateUI();
// Clear form
document.getElementById('clientName').value = '';
document.getElementById('amount').value = '';
document.getElementById('dueDate').value = '';
}
// Record payment
function recordPayment() {
const invoiceId = parseInt(document.getElementById('paymentInvoiceId').value);
const amount = parseFloat(document.getElementById('paymentAmount').value);
const paymentDate = document.getElementById('paymentDate').value;
if (!invoiceId || !amount || !paymentDate) {
alert('Please fill in all fields');
return;
}
const payment = {
id: Date.now(),
invoiceId,
amount,
date: paymentDate
};
data.payments.push(payment);
saveData();
updateUI();
// Clear form
document.getElementById('paymentAmount').value = '';
document.getElementById('paymentDate').value = '';
}
// Update UI
function updateUI() {
updateInvoicesTable();
updatePaymentsTable();
updatePaymentInvoiceSelect();
}
// Update invoices table
function updateInvoicesTable() {
const tbody = document.querySelector('#invoicesTable tbody');
tbody.innerHTML = '';
data.invoices.forEach(invoice => {
const paidAmount = calculatePaidAmount(invoice.id);
const status = paidAmount >= invoice.amount ? 'Paid' : 'Pending';
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${invoice.id}</td>
<td>${invoice.clientName}</td>
<td>$${invoice.amount.toFixed(2)}</td>
<td>$${paidAmount.toFixed(2)}</td>
<td>${invoice.dueDate}</td>
<td class="status-${status.toLowerCase()}">${status}</td>
`;
tbody.appendChild(tr);
});
}
// Update payments table
function updatePaymentsTable() {
const tbody = document.querySelector('#paymentsTable tbody');
tbody.innerHTML = '';
data.payments.forEach(payment => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${payment.invoiceId}</td>
<td>$${payment.amount.toFixed(2)}</td>
<td>${payment.date}</td>
`;
tbody.appendChild(tr);
});
}
// Update payment invoice select dropdown
function updatePaymentInvoiceSelect() {
const select = document.getElementById('paymentInvoiceId');
select.innerHTML = '<option value="">Select Invoice</option>';
data.invoices.forEach(invoice => {
const paidAmount = calculatePaidAmount(invoice.id);
if (paidAmount < invoice.amount) { // Only show unpaid/partially paid invoices
const option = document.createElement('option');
option.value = invoice.id;
option.textContent = `#${invoice.id} - ${invoice.clientName} (Remaining: $${(invoice.amount - paidAmount).toFixed(2)})`;
select.appendChild(option);
}
});
}
// Calculate total paid amount for an invoice
function calculatePaidAmount(invoiceId) {
return data.payments
.filter(payment => payment.invoiceId === invoiceId)
.reduce((sum, payment) => sum + payment.amount, 0);
}
// Initialize app
window.onload = loadData;
</script>
</body>
</html>
This invoicing app includes the following features:
Invoice Creation :
Create new invoices with client name, amount, and due date
Invoices are assigned a unique ID based on timestamp
Payment Management :
Record payments against existing invoices
Only shows unpaid or partially paid invoices in the payment dropdown
Tracks payment amount and date
Data Display :
Invoices table showing all invoices with their status (Paid/Pending)
Payments table showing all recorded payments
Shows total amount paid per invoice
Color-coded status indicators (green for paid, red for pending)
Data Persistence :
Uses localStorage to store data in JSON format
Data persists between page refreshes
User Interface :
Clean, responsive layout
Form validation
Clear visual feedback
No external dependencies
To use the app:
Save this code as an HTML file (e.g., invoicing.html)
Open it in a web browser
Start creating invoices and recording payments
The data will be stored in your browser's localStorage, so it will persist until you clear your browser data. The app uses pure JavaScript and CSS, with no external dependencies or third-party libraries.
Note: This is a basic implementation suitable for learning or small-scale use. For production use, you might want to add more features like:
Data validation
Invoice deletion
Payment deletion
Data export/import
Better error handling
Security measures
Server-side storage