diff --git a/credits.php b/credits.php
index 49e3ea4e4..0f0cd6130 100644
--- a/credits.php
+++ b/credits.php
@@ -110,6 +110,10 @@
Reference
|
+ Origin
+ |
+
Actions |
@@ -127,14 +131,41 @@
$credit_account_id = $row['credit_account_id'];
// Get client name from DB
- $clientQuery = mysqli_query($mysqli, "SELECT * FROM clients WHERE client_id = $credit_client_id LIMIT 1");
+ $clientQuery = mysqli_query($mysqli, "SELECT * FROM clients WHERE client_id = $credit_client_id");
$client = mysqli_fetch_array($clientQuery);
$client_name = $client['client_name'];
// Get account name from DB
- $accountQuery = mysqli_query($mysqli, "SELECT * FROM accounts WHERE account_id = $credit_account_id LIMIT 1");
- $account = mysqli_fetch_array($accountQuery);
- $account_name = $account['account_name'];
+ if($credit_account_id != NULL) {
+ $accountQuery = mysqli_query($mysqli, "SELECT * FROM accounts WHERE account_id = $credit_account_id");
+ $account = mysqli_fetch_array($accountQuery);
+ $account_name = $account['account_name'];
+ } else {
+ $account_name = "Unassigned";
+ }
+
+ // Get payment invoice and reference from DB
+ if($credit_payment_id != NULL) {
+ $paymentQuery = mysqli_query($mysqli, "SELECT * FROM payments WHERE payment_id = $credit_payment_id");
+ $payment = mysqli_fetch_array($paymentQuery);
+ $payment_invoice = $payment['payment_invoice_id'];
+ $payment_reference = $payment['payment_reference'];
+ } else {
+ $payment_invoice = "Unassigned";
+ $payment_reference = "Unassigned";
+ }
+
+ // Get invoice prefix and number from DB
+ if($payment_invoice != NULL) {
+ $invoiceQuery = mysqli_query($mysqli, "SELECT * FROM invoices WHERE invoice_id = $payment_invoice");
+ $invoice = mysqli_fetch_array($invoiceQuery);
+ $invoice_prefix = $invoice['invoice_prefix'];
+ $invoice_number = $invoice['invoice_number'];
+ $payment_invoice_display = "Payment for: " . $invoice_prefix . $invoice_number;
+ } else {
+ $invoice_prefix = "Unassigned";
+ $invoice_number = "Unassigned";
+ }
$credit_display_amount = numfmt_format_currency($currency_format, $credit_amount, $credit_currency_code);
@@ -150,6 +181,7 @@
|
|
+ |
diff --git a/database_updates.php b/database_updates.php
index ad500c0eb..869e85d4e 100644
--- a/database_updates.php
+++ b/database_updates.php
@@ -1615,12 +1615,20 @@
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.0.7'");
}
- // if (CURRENT_DATABASE_VERSION == '1.0.7') {
+ if (CURRENT_DATABASE_VERSION == '1.0.7') {
// // Insert queries here required to update to DB version 1.0.8
+ mysqli_query($mysqli, "CREATE TABLE `credits ` (`credit_id` int(11) NOT NULL AUTO_INCREMENT,`credit_amount` decimal(15,2) NOT NULL,`credit_currency_code` varchar(200) NOT NULL,`credit_date` date NOT NULL,`credit_reference` text 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,`credit_payment_id` int(11) NOT NULL,`credit_account_id` int(11) NOT NULL, PRIMARY KEY (`credit_id`))");
// // Then, update the database to the next sequential version
- // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.0.8'");
+ mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.0.8'");
+ }
+
+ // if (CURRENT_DATABASE_VERSION == '1.0.8') {
+ // // Insert queries here required to update to DB version 1.0.9
+ // // Then, update the database to the next sequential version
+ // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.0.9'");
// }
+
} else {
// Up-to-date
}
diff --git a/database_version.php b/database_version.php
index c1dcafe27..eb2233d5a 100644
--- a/database_version.php
+++ b/database_version.php
@@ -5,5 +5,5 @@
* It is used in conjunction with database_updates.php
*/
-DEFINE("LATEST_DATABASE_VERSION", "1.0.7");
+DEFINE("LATEST_DATABASE_VERSION", "1.0.8");
diff --git a/post/invoice.php b/post/invoice.php
index e2dfa7f17..95b6d773a 100644
--- a/post/invoice.php
+++ b/post/invoice.php
@@ -780,7 +780,7 @@
$date = sanitizeInput($_POST['date']);
$bulk_payment_amount = floatval($_POST['amount']);
$bulk_payment_amount_static = floatval($_POST['amount']);
- $total_account_balance = floatval($_POST['balance']);
+ $total_client_balance = floatval($_POST['balance']);
$account = intval($_POST['account']);
$currency_code = sanitizeInput($_POST['currency_code']);
$payment_method = sanitizeInput($_POST['payment_method']);
@@ -788,11 +788,15 @@
$email_receipt = intval($_POST['email_receipt']);
// Check if bulk_payment_amount exceeds total_account_balance
- if ($bulk_payment_amount > $total_account_balance) {
- $_SESSION['alert_type'] = "error";
- $_SESSION['alert_message'] = "Payment exceeds Client Balance.";
- header("Location: " . $_SERVER["HTTP_REFERER"]);
- exit;
+ if ($bulk_payment_amount > $total_client_balance) {
+ // Create new credit for the overpayment
+ $credit_amount = $bulk_payment_amount - $total_client_balance;
+ $bulk_payment_amount = $total_client_balance;
+
+ // Add Credit
+ $credit_query = "INSERT INTO credits SET credit_amount = $credit_amount, credit_currency_code = '$currency_code', credit_date = '$date', credit_reference = 'Overpayment: $reference', credit_client_id = $client_id, credit_account_id = $account";
+ mysqli_query($mysqli, $credit_query);
+ $credit_id = mysqli_insert_id($mysqli);
}
// Get Invoices
@@ -1439,8 +1443,8 @@
header("Location: post.php?add_ticket_to_invoice=$invoice_id");
}
-if (isset($_GET['apply_credit_id'])) {
- $credit_id = intval($_GET['apply_credit_id']);
+if (isset($_GET['apply_credit'])) {
+ $credit_id = intval($_GET['apply_credit']);
$credit_sql = mysqli_query($mysqli,"SELECT * FROM credits WHERE credit_id = $credit_id");
$credit_row = mysqli_fetch_array($credit_sql);
@@ -1456,7 +1460,9 @@
$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);
- }
+ }
+ // Delete the original credit
+ mysqli_query($mysqli,"DELETE FROM credits WHERE credit_id = $credit_id");
// Apply payments similar to add bulk payment
@@ -1468,6 +1474,7 @@
AND invoice_client_id = $client_id
ORDER BY invoice_number ASC";
$result_invoices = mysqli_query($mysqli, $sql_invoices);
+ $invoice_applied_count = 0;
// Loop Through Each Invoice
while ($row = mysqli_fetch_array($result_invoices)) {
@@ -1482,8 +1489,13 @@
$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
+ break; // Exit the loop if no credit amount is left
+ }
+
+ if ($invoice_balance <= 0) {
+ continue; // Skip the invoice if it's already paid
}
if ($credit_amount >= $invoice_balance) {
@@ -1493,15 +1505,17 @@
$payment_amount = $credit_amount;
$invoice_status = "Partial";
}
+
+ $invoice_applied_count++;
- // Subtract the payment amount from the bulk payment amount
+ // Subtract the payment amount from the credit 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})";
+ $payment_query = "INSERT INTO payments SET payment_date = CURDATE(), payment_amount = $payment_amount, payment_invoice_id = $invoice_id, payment_account_id = 1, payment_currency_code = '{$credit_row['credit_currency_code']}', payment_reference = 'Credit Applied'";
mysqli_query($mysqli, $payment_query);
$payment_id = mysqli_insert_id($mysqli);
@@ -1520,6 +1534,8 @@
} // End Invoice Loop
+
+
// Send Email
if ($email_receipt == 1) {
@@ -1564,7 +1580,7 @@
// 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";
+ $_SESSION['alert_message'] .= "Credit applied to $credit_applied_count invoices";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
|