-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice_send.php
More file actions
221 lines (188 loc) · 7.74 KB
/
Copy pathinvoice_send.php
File metadata and controls
221 lines (188 loc) · 7.74 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
<?php
define('INCLUDED_FROM_ANOTHER_SCRIPT', true);
require_once 'includes/config.php';
require_once 'includes/functions.php';
require 'lib/phpmailer/src/Exception.php';
require 'lib/phpmailer/src/PHPMailer.php';
require 'lib/phpmailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_login();
// Create database connection
if (!isset($pdo)) {
try {
$pdo = new PDO(
"mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4",
DB_USER,
DB_PASS,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]
);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
}
function set_flash_message($type, $message)
{
$_SESSION['flash_message'] = [
'type' => $type,
'message' => $message
];
}
// Check if invoice ID is provided
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
set_flash_message('error', 'Invalid invoice ID');
header('Location: invoices.php');
exit;
}
$invoice_id = (int) $_GET['id'];
// Get invoice details with customer and business information
$stmt = $pdo->prepare("
SELECT i.*,
c.company_name as client_company,
c.contact_name as client_contact,
c.email as client_email,
c.phone as client_phone,
c.address as client_address,
c.city as client_city,
c.postcode as client_postcode,
b.company_name as business_name,
b.company_number,
b.vat_number,
b.address_line1,
b.address_line2,
b.city as business_city,
b.county,
b.postcode as business_postcode,
b.country,
b.phone as business_phone,
b.smtp_host,
b.smtp_port,
b.smtp_username,
b.smtp_password,
b.from_email,
b.from_name
FROM invoices i
JOIN customers c ON i.customer_id = c.id
JOIN business_settings b ON b.id = 1
WHERE i.id = ?
");
$stmt->execute([$invoice_id]);
$invoice = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$invoice) {
set_flash_message('error', 'Invoice not found');
header('Location: invoices.php');
exit;
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
// Get invoice items
$stmt = $pdo->prepare("SELECT * FROM invoice_items WHERE invoice_id = ? ORDER BY id");
$stmt->execute([$invoice_id]);
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Generate PDF
require_once 'invoice_pdf.php';
$pdf_content = generate_invoice_pdf($invoice, $items, true);
$to = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Setup PHPMailer
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = $invoice['smtp_host'];
$mail->SMTPAuth = true;
$mail->Username = $invoice['smtp_username'];
$mail->Password = $invoice['smtp_password'];
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->Timeout = 60;
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
]
];
// Email content
$mail->setFrom($invoice['from_email'], $invoice['from_name']);
$mail->addAddress($to);
$mail->addReplyTo($invoice['from_email'], $invoice['from_name']);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = nl2br(htmlspecialchars($message));
$mail->AltBody = $message;
// Attach PDF
$mail->addStringAttachment(
$pdf_content,
"Invoice-" . $invoice['invoice_number'] . ".pdf",
'base64',
'application/pdf'
);
$mail->send();
// Update invoice status
$stmt = $pdo->prepare("UPDATE invoices SET updated_at = NOW(), status = 'sent' WHERE id = ?");
$stmt->execute([$invoice_id]);
set_flash_message('success', 'Invoice ' . $invoice['invoice_number'] . ' sent successfully to ' . $to);
header('Location: invoice_view.php?id=' . $invoice_id);
exit;
} catch (Exception $e) {
set_flash_message('error', 'Failed to send invoice: ' . $e->getMessage());
error_log("Invoice sending error - Invoice ID: $invoice_id - Error: " . $e->getMessage());
header('Location: invoice_send.php?id=' . $invoice_id);
exit;
}
}
$page_title = 'Send Invoice ' . $invoice['invoice_number'];
require_once 'includes/header.php';
require_once 'includes/menu.php';
?>
<div class="container py-4">
<div class="row">
<div class="col-md-8 offset-md-2">
<h1 class="mb-4">Send Invoice <?php echo htmlspecialchars($invoice['invoice_number']); ?></h1>
<div class="card">
<div class="card-body">
<form method="POST" action="invoice_send.php?id=<?php echo $invoice_id; ?>">
<input type="hidden" name="invoice_id" value="<?php echo $invoice_id; ?>">
<div class="mb-3">
<label for="from_email" class="form-label">From Email</label>
<input type="email" class="form-control" id="from_email" name="from_email"
value="<?php echo htmlspecialchars($invoice['from_email']); ?>" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Send To</label>
<input type="email" class="form-control" id="email" name="email"
value="<?php echo htmlspecialchars($invoice['client_email']); ?>" required>
</div>
<div class="mb-3">
<label for="subject" class="form-label">Subject</label>
<input type="text" class="form-control" id="subject" name="subject"
value="Invoice <?php echo htmlspecialchars($invoice['invoice_number']); ?> from <?php echo htmlspecialchars($invoice['business_name']); ?>" required>
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" id="message" name="message" rows="6" required>Dear <?php echo htmlspecialchars($invoice['client_contact']); ?>,
Please find attached invoice <?php echo htmlspecialchars($invoice['invoice_number']); ?> for <?php echo format_currency($invoice['total']); ?>.
Payment is due by: <?php echo format_date($invoice['due_date']); ?>
Thank you for your business!
Best regards,
<?php echo htmlspecialchars($invoice['business_name']); ?></textarea>
</div>
<div class="d-flex justify-content-between">
<a href="invoice_view.php?id=<?php echo $invoice_id; ?>" class="btn btn-secondary">Cancel</a>
<button type="submit" name="generate_pdf" class="btn btn-info">Preview PDF</button>
<button type="submit" class="btn btn-primary">Send Invoice</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>