-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice_create.php
More file actions
347 lines (309 loc) · 16.1 KB
/
Copy pathinvoice_create.php
File metadata and controls
347 lines (309 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_login();
// Get customer if ID provided
$customer = null;
if (isset($_GET['customer_id']) && is_numeric($_GET['customer_id'])) {
try {
$pdo = new PDO(
"mysql:host=" . DB_HOST . ";dbname=" . DB_NAME,
DB_USER,
DB_PASS
);
$stmt = $pdo->prepare("SELECT * FROM customers WHERE id = ?");
$stmt->execute([$_GET['customer_id']]);
$customer = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$_SESSION['error'] = "Error loading customer details";
}
}
// Generate next invoice number (format: INV-YYYYMM-XXXX)
function generate_invoice_number()
{
global $pdo;
$prefix = 'INV-' . date('Ym') . '-';
$stmt = $pdo->query("SELECT MAX(invoice_number) as max_number FROM invoices WHERE invoice_number LIKE '$prefix%'");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result['max_number']) {
$number = intval(substr($result['max_number'], -4)) + 1;
} else {
$number = 1;
}
return $prefix . str_pad($number, 4, '0', STR_PAD_LEFT);
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
$pdo->beginTransaction();
// Insert invoice
$sql = "INSERT INTO invoices (
customer_id, invoice_number, invoice_date, due_date,
subtotal, tax_rate, tax_amount, total, notes, status
) VALUES (
:customer_id, :invoice_number, :invoice_date, :due_date,
:subtotal, :tax_rate, :tax_amount, :total, :notes, :status
)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'customer_id' => $_POST['customer_id'],
'invoice_number' => $_POST['invoice_number'],
'invoice_date' => $_POST['invoice_date'],
'due_date' => $_POST['due_date'],
'subtotal' => $_POST['subtotal'],
'tax_rate' => $_POST['tax_rate'],
'tax_amount' => $_POST['tax_amount'],
'total' => $_POST['total'],
'notes' => $_POST['notes'],
'status' => $_POST['send_now'] ? 'sent' : 'draft'
]);
$invoice_id = $pdo->lastInsertId();
// Insert invoice items
$items = json_decode($_POST['invoice_items'], true);
$sql = "INSERT INTO invoice_items (invoice_id, description, quantity, unit_price, amount)
VALUES (:invoice_id, :description, :quantity, :unit_price, :amount)";
$stmt = $pdo->prepare($sql);
foreach ($items as $item) {
$stmt->execute([
'invoice_id' => $invoice_id,
'description' => $item['description'],
'quantity' => $item['quantity'],
'unit_price' => $item['unit_price'],
'amount' => $item['amount']
]);
}
$pdo->commit();
// Handle email sending if requested
if ($_POST['send_now']) {
// We'll implement email sending later
$_SESSION['message'] = 'Invoice created and marked as sent. Email functionality coming soon.';
} else {
$_SESSION['message'] = 'Invoice created successfully';
}
header('Location: invoice_view.php?id=' . $invoice_id);
exit();
} catch (Exception $e) {
$pdo->rollBack();
$error = "Error creating invoice: " . $e->getMessage();
}
}
$page_title = 'Create Invoice';
require_once 'includes/header.php';
require_once 'includes/topbar.php';
require_once 'includes/sidebar.php';
?>
<div class="page-wrapper">
<div class="page-content">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="page-title-box d-md-flex justify-content-md-between align-items-center">
<h4 class="page-title">Create New Invoice</h4>
<div class="">
<ol class="breadcrumb mb-0">
<li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a>
</li><!--end nav-item-->
<li class="breadcrumb-item active">Create Invoice</li>
</ol>
</div>
</div><!--end page-title-box-->
<form id="invoiceForm" method="post" action="">
<div class="row">
<!-- Left Column -->
<div class="col-md-8">
<!-- Customer Selection -->
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Customer Details</h5>
<?php if ($customer): ?>
<input type="hidden" name="customer_id" value="<?php echo $customer['id']; ?>">
<div class="row">
<div class="col-md-6">
<p class="mb-1"><strong><?php echo htmlspecialchars($customer['company_name']); ?></strong></p>
<p class="mb-1"><?php echo htmlspecialchars($customer['contact_name']); ?></p>
<p class="mb-1"><?php echo nl2br(htmlspecialchars($customer['address'])); ?></p>
<p class="mb-1"><?php echo htmlspecialchars($customer['city']); ?></p>
<p class="mb-1"><?php echo htmlspecialchars($customer['postcode']); ?></p>
</div>
<div class="col-md-6 text-md-end">
<a href="customer_select.php" class="btn btn-outline-primary">Change Customer</a>
</div>
</div>
<?php else: ?>
<div class="text-center py-3">
<p>No customer selected</p>
<a href="customer_select.php" class="btn btn-primary">Select Customer</a>
</div>
<?php endif; ?>
</div>
</div>
<!-- Invoice Items -->
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Invoice Items</h5>
<table class="table" id="itemsTable">
<thead>
<tr>
<th>Description</th>
<th width="100">Quantity</th>
<th width="150">Unit Price</th>
<th width="150">Amount</th>
<th width="50"></th>
</tr>
</thead>
<tbody>
<!-- Items will be added here dynamically -->
</tbody>
</table>
<button type="button" class="btn btn-outline-primary" id="addItem">Add Item</button>
</div>
</div>
<!-- Notes -->
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Notes</h5>
<textarea name="notes" class="form-control" rows="3"
placeholder="Enter any additional notes..."></textarea>
</div>
</div>
</div>
<!-- Right Column -->
<div class="col-md-4">
<!-- Invoice Details -->
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Invoice Details</h5>
<div class="mb-3">
<label class="form-label">Invoice Number</label>
<input type="text" class="form-control" name="invoice_number"
value="<?php echo generate_invoice_number(); ?>" readonly>
</div>
<div class="mb-3">
<label class="form-label">Invoice Date</label>
<input type="date" class="form-control" name="invoice_date"
value="<?php echo date('Y-m-d'); ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Due Date</label>
<input type="date" class="form-control" name="due_date"
value="<?php echo date('Y-m-d', strtotime('+30 days')); ?>" required>
</div>
</div>
</div>
<!-- Totals -->
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Summary</h5>
<div class="mb-3">
<label class="form-label">Subtotal</label>
<input type="text" class="form-control" name="subtotal" readonly>
</div>
<div class="mb-3">
<label class="form-label">Tax Rate (%)</label>
<input type="number" class="form-control" name="tax_rate" value="20.00">
</div>
<div class="mb-3">
<label class="form-label">Tax Amount</label>
<input type="text" class="form-control" name="tax_amount" readonly>
</div>
<div class="mb-3">
<label class="form-label">Total</label>
<input type="text" class="form-control" name="total" readonly>
</div>
</div>
</div>
<!-- Actions -->
<div class="card">
<div class="card-body">
<div class="mb-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="send_now" id="send_now">
<label class="form-check-label" for="send_now">
Send invoice to customer immediately
</label>
</div>
</div>
<input type="hidden" name="invoice_items" id="invoice_items">
<button type="submit" class="btn btn-primary w-100">Create Invoice</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
// Invoice items handling
let items = [];
function updateTotals() {
let subtotal = 0;
items.forEach(item => {
subtotal += parseFloat(item.amount);
});
const taxRate = parseFloat(document.querySelector('[name="tax_rate"]').value) || 0;
const taxAmount = subtotal * (taxRate / 100);
const total = subtotal + taxAmount;
document.querySelector('[name="subtotal"]').value = subtotal.toFixed(2);
document.querySelector('[name="tax_amount"]').value = taxAmount.toFixed(2);
document.querySelector('[name="total"]').value = total.toFixed(2);
document.getElementById('invoice_items').value = JSON.stringify(items);
}
function addItem() {
items.push({
description: '',
quantity: 1,
unit_price: 0,
amount: 0
});
renderItems();
}
function removeItem(index) {
items.splice(index, 1);
renderItems();
updateTotals();
}
function updateItem(index, field, value) {
items[index][field] = value;
if (field === 'quantity' || field === 'unit_price') {
items[index].amount = (items[index].quantity * items[index].unit_price).toFixed(2);
}
renderItems();
updateTotals();
}
function renderItems() {
const tbody = document.querySelector('#itemsTable tbody');
tbody.innerHTML = '';
items.forEach((item, index) => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>
<input type="text" class="form-control" value="${item.description}"
onchange="updateItem(${index}, 'description', this.value)">
</td>
<td>
<input type="number" class="form-control" value="${item.quantity}" min="1" step="1"
onchange="updateItem(${index}, 'quantity', this.value)">
</td>
<td>
<input type="number" class="form-control" value="${item.unit_price}" min="0" step="0.01"
onchange="updateItem(${index}, 'unit_price', this.value)">
</td>
<td>
<input type="text" class="form-control" value="${item.amount}" readonly>
</td>
<td>
<button type="button" class="btn btn-sm btn-danger" onclick="removeItem(${index})">×</button>
</td>
`;
tbody.appendChild(tr);
});
}
document.getElementById('addItem').addEventListener('click', addItem);
document.querySelector('[name="tax_rate"]').addEventListener('change', updateTotals);
// Add first item automatically
addItem();
</script>
<?php require_once 'includes/footer.php'; ?>