diff --git a/agent/invoices.php b/agent/invoices.php index f8bebe91b..cacaf1f72 100644 --- a/agent/invoices.php +++ b/agent/invoices.php @@ -178,6 +178,13 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); data-modal-url=" $dtf, 'dtt' => $dtt]) ?>"> Export + = 2 && !empty($config_smtp_provider)) { ?> + + + Send Account Statement + + diff --git a/agent/modals/client/client_statement.php b/agent/modals/client/client_statement.php new file mode 100644 index 000000000..f292b56e7 --- /dev/null +++ b/agent/modals/client/client_statement.php @@ -0,0 +1,192 @@ + + + +
+ + + + + +
+ += '$statement_date_from'"; + $statement_period = "$statement_date_from to $statement_date_to"; + } + + $include_paid = !empty($_POST['include_paid']); + + // Recipients - same picker contract as the invoice send, so the same + // client scoping applies and a tampered contact_id matches nothing + $selected_contacts = $_POST['contacts'] ?? []; + if (!is_array($selected_contacts)) { + $selected_contacts = []; + } + $selected_contact_ids = array_filter(array_unique(array_map('intval', $selected_contacts))); + + if (empty($selected_contact_ids)) { + flashAlert("Select at least one contact to send to", 'error'); + redirect(); + } + + $selected_contact_id_list = implode(',', $selected_contact_ids); + + $sql_recipients = mysqli_query( + $mysqli, + "SELECT contact_email, contact_name FROM contacts + WHERE contact_id IN ($selected_contact_id_list) + AND contact_client_id = $client_id + AND contact_archived_at IS NULL + AND contact_email IS NOT NULL + AND contact_email != '' + ORDER BY contact_primary DESC, contact_billing DESC, contact_name ASC" + ); + + if (mysqli_num_rows($sql_recipients) == 0) { + flashAlert("None of the selected contacts have a usable email address", 'error'); + redirect(); + } + + // The statement lines. Draft / Cancelled / Non-Billable are not money the + // client owes, so they never appear - same exclusion the Outstanding + // Balances report uses. Payments are summed in a derived table rather than + // joined directly, or an invoice with two payments would be counted twice. + $sql_statement = mysqli_query( + $mysqli, + "SELECT invoice_amount, invoice_date, invoice_due, invoice_id, invoice_number, invoice_prefix, + invoice_scope, invoice_status, invoice_url_key, IFNULL(amount_paid, 0) AS amount_paid + FROM invoices + LEFT JOIN ( + SELECT payment_invoice_id, SUM(payment_amount) AS amount_paid FROM payments + WHERE payment_archived_at IS NULL + GROUP BY payment_invoice_id + ) AS invoice_payments ON payment_invoice_id = invoice_id + WHERE invoice_client_id = $client_id + AND invoice_status NOT IN ('Draft', 'Cancelled', 'Non-Billable') + AND invoice_date <= '$statement_date_to' + $statement_date_query + " . ($include_paid ? '' : "AND invoice_amount - IFNULL(invoice_payments.amount_paid, 0) > 0") . " + ORDER BY invoice_date ASC, invoice_number ASC" + ); + + if (mysqli_num_rows($sql_statement) == 0) { + flashAlert("No invoices to report for that period - nothing sent", 'error'); + redirect(); + } + + $sql = mysqli_query($mysqli, "SELECT company_name, company_phone, company_phone_country_code FROM companies WHERE company_id = 1"); + $row = mysqli_fetch_assoc($sql); + $company_name = escapeSql($row['company_name']); + $company_phone = escapeSql(formatPhoneNumber($row['company_phone'], $row['company_phone_country_code'])); + + $config_invoice_from_name = escapeSql($config_invoice_from_name); + $config_invoice_from_email = escapeSql($config_invoice_from_email); + $config_base_url = escapeSql($config_base_url); + + // Build the table once - it is identical for every recipient, only the + // greeting differs. Everything interpolated here is already escaped: + // addToMailQueue() writes the body into the queue raw. + $statement_rows = ''; + $statement_total = 0; + $statement_line_count = 0; + + while ($row = mysqli_fetch_assoc($sql_statement)) { + $invoice_id = intval($row['invoice_id']); + $invoice_prefix = escapeSql($row['invoice_prefix']); + $invoice_number = intval($row['invoice_number']); + $invoice_scope = escapeSql($row['invoice_scope']); + $invoice_status = escapeSql($row['invoice_status']); + $invoice_date = escapeSql($row['invoice_date']); + $invoice_due = escapeSql($row['invoice_due']); + $invoice_url_key = escapeSql($row['invoice_url_key']); + $invoice_amount = floatval($row['invoice_amount']); + $amount_paid = floatval($row['amount_paid']); + $invoice_balance = $invoice_amount - $amount_paid; + + $statement_total += $invoice_balance; + $statement_line_count++; + + $invoice_link = "https://$config_base_url/guest/guest_view_invoice.php?invoice_id=$invoice_id&url_key=$invoice_url_key"; + + $overdue_flag = ''; + if ($invoice_balance > 0 && !empty($invoice_due) && $invoice_due < date("Y-m-d")) { + $overdue_flag = ' (overdue)'; + } + + $statement_rows .= '' + . '' . "$invoice_prefix$invoice_number" . '' + . '' . $invoice_scope . '' + . '' . $invoice_date . '' + . '' . $invoice_due . $overdue_flag . '' + . '' . numfmt_format_currency($currency_format, $invoice_amount, $client_currency_code) . '' + . '' . numfmt_format_currency($currency_format, $amount_paid, $client_currency_code) . '' + . '' . numfmt_format_currency($currency_format, $invoice_balance, $client_currency_code) . '' + . ''; + } + + $statement_table = '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . $statement_rows + . '' + . '' + . '
InvoiceScopeDateDueAmountPaidBalance
Total Balance Due' . numfmt_format_currency($currency_format, $statement_total, $client_currency_code) . '
'; + + $subject = "Account Statement - $client_name"; + + $data = []; + $recipient_labels = []; + + while ($recipient = mysqli_fetch_assoc($sql_recipients)) { + $contact_name = escapeSql($recipient['contact_name']); + $contact_email = escapeSql($recipient['contact_email']); + + $body = "Hello $contact_name,

Please find your account statement for $statement_period below.

" + . $statement_table + . "

Click any invoice number above to view or pay it online.


--
$company_name - Billing
$config_invoice_from_email
$company_phone"; + + $data[] = [ + 'from' => $config_invoice_from_email, + 'from_name' => $config_invoice_from_name, + 'recipient' => $contact_email, + 'recipient_name' => $contact_name, + 'subject' => $subject, + 'body' => $body + ]; + + $recipient_labels[] = "$contact_name <$contact_email>"; + + logAudit("Invoice", "Statement", "$session_name emailed an account statement ($statement_period, $statement_line_count invoices) to $contact_email", $client_id); + } + + addToMailQueue($data); + + $recipient_count = count($recipient_labels); + + flashAlert("Statement queued to $recipient_count " . ($recipient_count == 1 ? "recipient" : "recipients")); + + redirect(); + +} + if (isExportRequest('export_invoices')) { validateCSRFToken();