From 129fbd300818648e28a6fb5bd24009e5bbe66505 Mon Sep 17 00:00:00 2001 From: o-psi Date: Tue, 27 Feb 2024 17:29:05 +0000 Subject: [PATCH] Credits --- credits.php | 169 +++++++++++++++++++++ db.sql | 23 +++ functions.php | 23 +++ inc_all_client.php | 14 +- payments.php | 197 +++++++++++++++--------- post/invoice.php | 364 ++++++++++++++++++++++++++++++++------------- 6 files changed, 602 insertions(+), 188 deletions(-) create mode 100644 credits.php diff --git a/credits.php b/credits.php new file mode 100644 index 000000000..1da307d0c --- /dev/null +++ b/credits.php @@ -0,0 +1,169 @@ + + +
+
+

Credits

+
+ +
+
+
+
+
+ +
+ + +
+
+
+
+
" + id="advancedFilter"> +
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+
+
+
+ + "> + + + + + + + + + + + + + + + + + + + + + + + + +
Client NameAccount + Name"> + Amount + Date + Reference + Actions
"> + + + + +
+
+
+
+ + \ No newline at end of file diff --git a/db.sql b/db.sql index 16b440e23..595809781 100644 --- a/db.sql +++ b/db.sql @@ -416,6 +416,29 @@ CREATE TABLE `contacts` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `credits` +-- + +DROP TABLE IF EXISTS `credits`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `credits` ( + `credit_id` int(11) NOT NULL AUTO_INCREMENT, + `credit_amount` decimal(15,2) NOT NULL DEFAULT 0.00, + `credit_currency_code` varchar(200) NOT NULL, + `credit_date` date NOT NULL, + `credit_reference` varchar(200) DEFAULT NULL, + `credit_payment_method` varchar(200) DEFAULT NULL, + `credit_created_at` datetime NOT NULL DEFAULT current_timestamp(), + `credit_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(), + `credit_archived_at` datetime DEFAULT NULL, + `credit_client_id` int(11) NOT NULL DEFAULT 0, + `credit_payment_id` int(11) NOT NULL DEFAULT 0, + `credit_account_id` int(11) DEFAULT NULL, + PRIMARY KEY (`credit_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + -- -- Table structure for table `custom_fields` -- diff --git a/functions.php b/functions.php index bf46ea319..ace0aec9c 100644 --- a/functions.php +++ b/functions.php @@ -1133,4 +1133,27 @@ function createiCalStrCancel($originaliCalStr) { // Return the modified iCal string return $cal_event->export(); } + +function getClientBalance($mysqli, $client_id, $credits = false) { + //Add up all the payments for the invoice and get the total amount paid to the invoice + $sql_invoice_amounts = mysqli_query($mysqli, "SELECT SUM(invoice_amount) AS invoice_amounts FROM invoices WHERE invoice_client_id = $client_id AND invoice_status NOT LIKE 'Draft' AND invoice_status NOT LIKE 'Cancelled'"); + $row = mysqli_fetch_array($sql_invoice_amounts); + + $invoice_amounts = floatval($row['invoice_amounts']); + + $sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments, invoices WHERE payment_invoice_id = invoice_id AND invoice_client_id = $client_id"); + $row = mysqli_fetch_array($sql_amount_paid); + + $amount_paid = floatval($row['amount_paid']); + + if ($credits) { + $sql_credits = mysqli_query($mysqli, "SELECT SUM(credit_amount) AS credit_amounts FROM credits WHERE credit_client_id = $client_id"); + $row = mysqli_fetch_array($sql_credits); + $credit_amounts = floatval($row['credit_amounts']); + + return $invoice_amounts - ($amount_paid + $credit_amounts); + } else { + return $invoice_amounts - $amount_paid; + } +} diff --git a/inc_all_client.php b/inc_all_client.php index 8e26cfd05..e5d693be4 100644 --- a/inc_all_client.php +++ b/inc_all_client.php @@ -86,18 +86,8 @@ } $client_tags_display = implode('', $client_tag_name_display_array); - //Add up all the payments for the invoice and get the total amount paid to the invoice - $sql_invoice_amounts = mysqli_query($mysqli, "SELECT SUM(invoice_amount) AS invoice_amounts FROM invoices WHERE invoice_client_id = $client_id AND invoice_status NOT LIKE 'Draft' AND invoice_status NOT LIKE 'Cancelled'"); - $row = mysqli_fetch_array($sql_invoice_amounts); - - $invoice_amounts = floatval($row['invoice_amounts']); - - $sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments, invoices WHERE payment_invoice_id = invoice_id AND invoice_client_id = $client_id"); - $row = mysqli_fetch_array($sql_amount_paid); - - $amount_paid = floatval($row['amount_paid']); - - $balance = $invoice_amounts - $amount_paid; + // Get Balance + $balance = getClientBalance($mysqli, $client_id, $credits = true); //Get Monthly Recurring Total $sql_recurring_monthly_total = mysqli_query($mysqli, "SELECT SUM(recurring_amount) AS recurring_monthly_total FROM recurring WHERE recurring_status = 1 AND recurring_frequency = 'month' AND recurring_client_id = $client_id"); diff --git a/payments.php b/payments.php index 96f36c6ef..2d2392ebf 100644 --- a/payments.php +++ b/payments.php @@ -23,75 +23,122 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); +// Credits SQL +$sql_credits = mysqli_query( + $mysqli, + "SELECT * FROM credits + WHERE credit_archived_at IS NULL" +); + +$credits_num_rows = mysqli_num_rows($sql_credits); ?> -
-
-

Payments

-
+
+
+

Payments

+
-
-
-
-
-
- -
- - -
+
+ +
+
+
+ +
+ +
-
" id="advancedFilter"> -
-
-
- - -
+ +
+
" + id="advancedFilter"> +
+
+
+ +
-
-
- - -
+
+
+
+ +
-
-
- - -
+
+
+
+ +
- -
-
- - "> + + +
+
+
+ "> - - - - - - - - + + + + + + + + - - + + - - - - - - - - - - + + + + + + + + + + - -
Payment DateInvoice DateInvoiceClientAmountPayment MethodReferenceAccountPayment + DateInvoice + DateInvoice + Client + Amount + Payment + MethodReference + Account +
+ + + +
-
- + +
+
+
+ ?> \ No newline at end of file diff --git a/post/invoice.php b/post/invoice.php index 0ffedaf03..69647197d 100644 --- a/post/invoice.php +++ b/post/invoice.php @@ -609,155 +609,171 @@ //Check to see if amount entered is greater than the balance of the invoice if ($amount > $balance) { - $_SESSION['alert_message'] = "Payment is more than the balance"; - header("Location: " . $_SERVER["HTTP_REFERER"]); + $payment_is_credit = true; + + // Calculate the overpayment amount + $credit_amount = $amount - $balance; + + // Set the payment amount to the invoice balance + $amount = $balance; } else { - mysqli_query($mysqli,"INSERT INTO payments SET payment_date = '$date', payment_amount = $amount, payment_currency_code = '$currency_code', payment_account_id = $account, payment_method = '$payment_method', payment_reference = '$reference', payment_invoice_id = $invoice_id"); + $payment_is_credit = false; + } - // Get Payment ID for reference - $payment_id = mysqli_insert_id($mysqli); - //Add up all the payments for the invoice and get the total amount paid to the invoice - $sql_total_payments_amount = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payments_amount FROM payments WHERE payment_invoice_id = $invoice_id"); - $row = mysqli_fetch_array($sql_total_payments_amount); - $total_payments_amount = floatval($row['payments_amount']); + mysqli_query($mysqli,"INSERT INTO payments SET payment_date = '$date', payment_amount = $amount, payment_currency_code = '$currency_code', payment_account_id = $account, payment_method = '$payment_method', payment_reference = '$reference', payment_invoice_id = $invoice_id"); - //Get the invoice total - $sql = mysqli_query($mysqli,"SELECT * FROM invoices - LEFT JOIN clients ON invoice_client_id = client_id - LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id AND contact_primary = 1 - WHERE invoice_id = $invoice_id" - ); + // Get payment ID for reference + $payment_id = mysqli_insert_id($mysqli); - $row = mysqli_fetch_array($sql); - $invoice_amount = floatval($row['invoice_amount']); - $invoice_prefix = sanitizeInput($row['invoice_prefix']); - $invoice_number = intval($row['invoice_number']); - $invoice_url_key = sanitizeInput($row['invoice_url_key']); - $invoice_currency_code = sanitizeInput($row['invoice_currency_code']); - $client_id = intval($row['client_id']); - $client_name = sanitizeInput($row['client_name']); - $contact_name = sanitizeInput($row['contact_name']); - $contact_email = sanitizeInput($row['contact_email']); - $contact_phone = sanitizeInput(formatPhoneNumber($row['contact_phone'])); - $contact_extension = preg_replace("/[^0-9]/", '',$row['contact_extension']); - $contact_mobile = sanitizeInput(formatPhoneNumber($row['contact_mobile'])); + if($payment_is_credit) { + //Create a credit for the overpayment + mysqli_query($mysqli,"INSERT INTO credits SET credit_amount = $credit_amount, credit_currency_code = '$currency_code', credit_date = '$date', credit_reference = 'Overpayment: $reference', credit_client_id = (SELECT invoice_client_id FROM invoices WHERE invoice_id = $invoice_id), credit_payment_id = $payment_id, credit_account_id = $account"); + // Get credit ID for reference + $credit_id = mysqli_insert_id($mysqli); + } - $sql = mysqli_query($mysqli,"SELECT * FROM companies WHERE company_id = 1"); - $row = mysqli_fetch_array($sql); + //Add up all the payments for the invoice and get the total amount paid to the invoice + $sql_total_payments_amount = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payments_amount FROM payments WHERE payment_invoice_id = $invoice_id"); + $row = mysqli_fetch_array($sql_total_payments_amount); + $total_payments_amount = floatval($row['payments_amount']); - $company_name = sanitizeInput($row['company_name']); - $company_country = sanitizeInput($row['company_country']); - $company_address = sanitizeInput($row['company_address']); - $company_city = sanitizeInput($row['company_city']); - $company_state = sanitizeInput($row['company_state']); - $company_zip = sanitizeInput($row['company_zip']); - $company_phone = sanitizeInput(formatPhoneNumber($row['company_phone'])); - $company_email = sanitizeInput($row['company_email']); - $company_website = sanitizeInput($row['company_website']); - $company_logo = sanitizeInput($row['company_logo']); + //Get the invoice total + $sql = mysqli_query($mysqli,"SELECT * FROM invoices + LEFT JOIN clients ON invoice_client_id = client_id + LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id AND contact_primary = 1 + WHERE invoice_id = $invoice_id" + ); - // Sanitize Config vars from get_settings.php - $config_invoice_from_name = sanitizeInput($config_invoice_from_name); - $config_invoice_from_email = sanitizeInput($config_invoice_from_email); + $row = mysqli_fetch_array($sql); + $invoice_amount = floatval($row['invoice_amount']); + $invoice_prefix = sanitizeInput($row['invoice_prefix']); + $invoice_number = intval($row['invoice_number']); + $invoice_url_key = sanitizeInput($row['invoice_url_key']); + $invoice_currency_code = sanitizeInput($row['invoice_currency_code']); + $client_id = intval($row['client_id']); + $client_name = sanitizeInput($row['client_name']); + $contact_name = sanitizeInput($row['contact_name']); + $contact_email = sanitizeInput($row['contact_email']); + $contact_phone = sanitizeInput(formatPhoneNumber($row['contact_phone'])); + $contact_extension = preg_replace("/[^0-9]/", '',$row['contact_extension']); + $contact_mobile = sanitizeInput(formatPhoneNumber($row['contact_mobile'])); - //Calculate the Invoice balance - $invoice_balance = $invoice_amount - $total_payments_amount; + $sql = mysqli_query($mysqli,"SELECT * FROM companies WHERE company_id = 1"); + $row = mysqli_fetch_array($sql); - $email_data = []; + $company_name = sanitizeInput($row['company_name']); + $company_country = sanitizeInput($row['company_country']); + $company_address = sanitizeInput($row['company_address']); + $company_city = sanitizeInput($row['company_city']); + $company_state = sanitizeInput($row['company_state']); + $company_zip = sanitizeInput($row['company_zip']); + $company_phone = sanitizeInput(formatPhoneNumber($row['company_phone'])); + $company_email = sanitizeInput($row['company_email']); + $company_website = sanitizeInput($row['company_website']); + $company_logo = sanitizeInput($row['company_logo']); - //Determine if invoice has been paid then set the status accordingly - if ($invoice_balance == 0) { + // Sanitize Config vars from get_settings.php + $config_invoice_from_name = sanitizeInput($config_invoice_from_name); + $config_invoice_from_email = sanitizeInput($config_invoice_from_email); - $invoice_status = "Paid"; + //Calculate the Invoice balance + $invoice_balance = $invoice_amount - $total_payments_amount; - if ($email_receipt == 1) { + $email_data = []; - $subject = "Payment Received - Invoice $invoice_prefix$invoice_number"; - $body = "Hello $contact_name,

We have received your payment in the amount of " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . " for invoice $invoice_prefix$invoice_number. Please keep this email as a receipt for your records.

Amount: " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . "
Balance: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "

Thank you for your business!


--
$company_name - Billing Department
$config_invoice_from_email
$company_phone"; + //Determine if invoice has been paid then set the status accordingly + if ($invoice_balance == 0) { - // Queue Mail - $email = [ - 'from' => $config_invoice_from_email, - 'from_name' => $config_invoice_from_name, - 'recipient' => $contact_email, - 'recipient_name' => $contact_name, - 'subject' => $subject, - 'body' => $body - ]; + $invoice_status = "Paid"; - $email_data[] = $email; + if ($email_receipt == 1) { - // Get Email ID for reference - $email_id = mysqli_insert_id($mysqli); + $subject = "Payment Received - Invoice $invoice_prefix$invoice_number"; + $body = "Hello $contact_name,

We have received your payment in the amount of " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . " for invoice $invoice_prefix$invoice_number. Please keep this email as a receipt for your records.

Amount: " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . "
Balance: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "

Thank you for your business!


--
$company_name - Billing Department
$config_invoice_from_email
$company_phone"; - // Email Logging + // Queue Mail + $email = [ + 'from' => $config_invoice_from_email, + 'from_name' => $config_invoice_from_name, + 'recipient' => $contact_email, + 'recipient_name' => $contact_name, + 'subject' => $subject, + 'body' => $body + ]; - $_SESSION['alert_message'] = "Email receipt sent "; + $email_data[] = $email; - mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Emailed Receipt!', history_invoice_id = $invoice_id"); + // Get Email ID for reference + $email_id = mysqli_insert_id($mysqli); - } + // Email Logging - } else { + $_SESSION['alert_message'] = "Email receipt sent "; - $invoice_status = "Partial"; + mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Emailed Receipt!', history_invoice_id = $invoice_id"); - if ($email_receipt == 1) { + } + + } else { + $invoice_status = "Partial"; - $subject = "Partial Payment Recieved - Invoice $invoice_prefix$invoice_number"; - $body = "Hello $contact_name,

We have recieved partial payment in the amount of " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . " and it has been applied to invoice $invoice_prefix$invoice_number. Please keep this email as a receipt for your records.

Amount: " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . "
Balance: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "

Thank you for your business!


~
$company_name - Billing
$config_invoice_from_email
$company_phone"; + if ($email_receipt == 1) { - // Queue Mail - $email = [ - 'from' => $config_invoice_from_email, - 'from_name' => $config_invoice_from_name, - 'recipient' => $contact_email, - 'recipient_name' => $contact_name, - 'subject' => $subject, - 'body' => $body - ]; - $email_data[] = $email; + $subject = "Partial Payment Recieved - Invoice $invoice_prefix$invoice_number"; + $body = "Hello $contact_name,

We have recieved partial payment in the amount of " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . " and it has been applied to invoice $invoice_prefix$invoice_number. Please keep this email as a receipt for your records.

Amount: " . numfmt_format_currency($currency_format, $amount, $invoice_currency_code) . "
Balance: " . numfmt_format_currency($currency_format, $invoice_balance, $invoice_currency_code) . "

Thank you for your business!


~
$company_name - Billing
$config_invoice_from_email
$company_phone"; - // Get Email ID for reference - $email_id = mysqli_insert_id($mysqli); + // Queue Mail + $email = [ + 'from' => $config_invoice_from_email, + 'from_name' => $config_invoice_from_name, + 'recipient' => $contact_email, + 'recipient_name' => $contact_name, + 'subject' => $subject, + 'body' => $body + ]; - // Email Logging + $email_data[] = $email; - $_SESSION['alert_message'] .= "Email receipt sent "; + // Get Email ID for reference + $email_id = mysqli_insert_id($mysqli); - mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Payment Receipt sent to mail queue ID: $email_id!', history_invoice_id = $invoice_id"); + // Email Logging - } + $_SESSION['alert_message'] .= "Email receipt sent "; - } + mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Sent', history_description = 'Payment Receipt sent to mail queue ID: $email_id!', history_invoice_id = $invoice_id"); - // Add emails to queue - if (!empty($email)) { - addToMailQueue($mysqli, $email_data); } - //Update Invoice Status - mysqli_query($mysqli,"UPDATE invoices SET invoice_status = '$invoice_status' WHERE invoice_id = $invoice_id"); + } - //Add Payment to History - mysqli_query($mysqli,"INSERT INTO history SET history_status = '$invoice_status', history_description = 'Payment added', history_invoice_id = $invoice_id"); + // Add emails to queue + if (!empty($email)) { + addToMailQueue($mysqli, $email_data); + } - //Logging - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = '$payment_amount', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id"); + //Update Invoice Status + mysqli_query($mysqli,"UPDATE invoices SET invoice_status = '$invoice_status' WHERE invoice_id = $invoice_id"); - if ($email_receipt == 1) { - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Email', log_description = 'Payment receipt for invoice $invoice_prefix$invoice_number queued to $contact_email Email ID: $email_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id"); - } + //Add Payment to History + mysqli_query($mysqli,"INSERT INTO history SET history_status = '$invoice_status', history_description = 'Payment added', history_invoice_id = $invoice_id"); - $_SESSION['alert_message'] .= "Payment added"; + //Logging + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = '$payment_amount', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id"); - header("Location: " . $_SERVER["HTTP_REFERER"]); + if ($email_receipt == 1) { + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Email', log_description = 'Payment receipt for invoice $invoice_prefix$invoice_number queued to $contact_email Email ID: $email_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $payment_id"); } + + $_SESSION['alert_message'] .= "Payment added"; + + header("Location: " . $_SERVER["HTTP_REFERER"]); } + if (isset($_POST['add_bulk_payment'])) { $client_id = intval($_POST['client_id']); @@ -1422,3 +1438,143 @@ header("Location: post.php?add_ticket_to_invoice=$invoice_id"); } + +if (isset($_POST['apply_credit_id'])) { + + $credit_id = intval($_POST['apply_credit_id']); + + $credit_sql = mysqli_query($mysqli,"SELECT * FROM credits WHERE credit_id = $credit_id"); + $credit_row = mysqli_fetch_array($credit_sql); + + $client_id = intval($credit_row['credit_client_id']); + $credit_amount = floatval($credit_row['credit_amount']); + + $client_balance = getClientBalance($mysqli, $client_id); + + if ($client_balance < $credit_amount) { + //create a new credit for the remaining amount + $new_credit_amount = $credit_amount - $client_balance; + $new_credit_query = "INSERT INTO credits (credit_date, credit_amount, credit_currency_code, credit_client_id) VALUES (CURDATE(), $new_credit_amount, '{$credit_row['credit_currency_code']}', $client_id)"; + mysqli_query($mysqli, $new_credit_query); + $new_credit_id = mysqli_insert_id($mysqli); + } + + // Apply payments similar to add bulk payment + + // Get Invoices + $sql_invoices = "SELECT * FROM invoices + WHERE invoice_status != 'Draft' + AND invoice_status != 'Paid' + AND invoice_status != 'Cancelled' + AND invoice_client_id = $client_id + ORDER BY invoice_number ASC"; + $result_invoices = mysqli_query($mysqli, $sql_invoices); + + // Loop Through Each Invoice + while ($row = mysqli_fetch_array($result_invoices)) { + $invoice_id = intval($row['invoice_id']); + $invoice_prefix = sanitizeInput($row['invoice_prefix']); + $invoice_number = intval($row['invoice_number']); + $invoice_amount = floatval($row['invoice_amount']); + $invoice_url_key = sanitizeInput($row['invoice_url_key']); + $invoice_balance_query = "SELECT SUM(payment_amount) AS amount_paid FROM payments WHERE payment_invoice_id = $invoice_id"; + $result_amount_paid = mysqli_query($mysqli, $invoice_balance_query); + $row_amount_paid = mysqli_fetch_array($result_amount_paid); + $amount_paid = floatval($row_amount_paid['amount_paid']); + $invoice_balance = $invoice_amount - $amount_paid; + + if ($credit_amount <= 0) { + break; // Exit the loop if no payment amount is left + } + + if ($credit_amount >= $invoice_balance) { + $payment_amount = $invoice_balance; + $invoice_status = "Paid"; + } else { + $payment_amount = $credit_amount; + $invoice_status = "Partial"; + } + + // Subtract the payment amount from the bulk payment amount + $credit_amount -= $payment_amount; + + // Get Invoice Remain Balance + $remaining_invoice_balance = $invoice_balance - $payment_amount; + + // Add Payment + $payment_query = "INSERT INTO payments (payment_date, payment_amount, payment_currency_code, payment_account_id, payment_method, payment_reference, payment_invoice_id) VALUES ('{$date}', {$payment_amount}, '{$currency_code}', {$account}, '{$payment_method}', '{$reference}', {$invoice_id})"; + mysqli_query($mysqli, $payment_query); + $payment_id = mysqli_insert_id($mysqli); + + // Update Invoice Status + $update_invoice_query = "UPDATE invoices SET invoice_status = '{$invoice_status}' WHERE invoice_id = {$invoice_id}"; + mysqli_query($mysqli, $update_invoice_query); + + // Add Payment to History + $history_description = "Payment added"; + $add_history_query = "INSERT INTO history (history_status, history_description, history_invoice_id) VALUES ('{$invoice_status}', '{$history_description}', {$invoice_id})"; + mysqli_query($mysqli, $add_history_query); + + // Add to Email Body Invoice Portion + + $email_body_invoices .= "
Invoice $invoice_prefix$invoice_number - Outstanding Amount: " . numfmt_format_currency($currency_format, $invoice_balance, $currency_code) . " - Payment Applied: " . numfmt_format_currency($currency_format, $payment_amount, $currency_code) . " - New Balance: " . numfmt_format_currency($currency_format, $remaining_invoice_balance, $currency_code); + + } // End Invoice Loop + + // Send Email + if ($email_receipt == 1) { + + // Get Client / Contact Info + $sql_client = mysqli_query($mysqli,"SELECT * FROM clients + LEFT JOIN contacts ON clients.client_id = contacts.contact_client_id + AND contact_primary = 1 + WHERE client_id = $client_id" + ); + + $row = mysqli_fetch_array($sql_client); + $client_name = sanitizeInput($row['client_name']); + $contact_name = sanitizeInput($row['contact_name']); + $contact_email = sanitizeInput($row['contact_email']); + + $sql_company = mysqli_query($mysqli,"SELECT company_name, company_phone FROM companies WHERE company_id = 1"); + $row = mysqli_fetch_array($sql_company); + + $company_name = sanitizeInput($row['company_name']); + $company_phone = sanitizeInput(formatPhoneNumber($row['company_phone'])); + + // Sanitize Config vars from get_settings.php + $config_invoice_from_name = sanitizeInput($config_invoice_from_name); + $config_invoice_from_email = sanitizeInput($config_invoice_from_email); + + $subject = "Payment Received - Multiple Invoices"; + $body = "Hello $contact_name,

Thank you for your payment of " . numfmt_format_currency($currency_format, $bulk_payment_amount_static, $currency_code) . " We\'ve applied your payment to the following invoices, updating their balances accordingly:

$email_body_invoices


We appreciate your continued business!

Sincerely,
$company_name - Billing
$config_invoice_from_email
$company_phone"; + + // Queue Mail + mysqli_query($mysqli, "INSERT INTO email_queue SET email_recipient = '$contact_email', email_recipient_name = '$contact_name', email_from = '$config_invoice_from_email', email_from_name = '$config_invoice_from_name', email_subject = '$subject', email_content = '$body'"); + + // Get Email ID for reference + $email_id = mysqli_insert_id($mysqli); + + // Email Logging + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Email', log_description = 'Bulk Payment receipt for multiple Invoices queued to $contact_email Email ID: $email_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session"); + + $_SESSION['alert_message'] .= "Email receipt sent and "; + + } // End Email + + // Logging + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Payment', log_action = 'Create', log_description = 'Bulk Payment of $bulk_payment_amount_static', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id"); + + $_SESSION['alert_message'] .= "Bulk Payment added"; + + header("Location: " . $_SERVER["HTTP_REFERER"]); +} + +if (isset($_POST['delete_credit_id'])) { + $credit_id = intval($_POST['delete_credit_id']); + + mysqli_query($mysqli,"DELETE FROM credits WHERE credit_id = $credit_id"); + + $_SESSION['alert_message'] = "Credit deleted"; + header("Location: " . $_SERVER["HTTP_REFERER"]); +} \ No newline at end of file