Skip to content

Commit

Permalink
Credits DB updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
o-psi committed Feb 27, 2024
1 parent 02ebe84 commit e2e3601
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
40 changes: 36 additions & 4 deletions credits.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<th><a class="text-dark"
href="?<?php echo $url_query_strings_sort; ?>&sort=credit_reference&order=<?php echo $disp; ?>">Reference</a>
</th>
<th><a class="text-dark"
href="?<?php echo $url_query_strings_sort; ?>&sort=credit_payment&order=<?php echo $disp; ?>">Origin</a>
</th>

<th>Actions</th>

</tr>
Expand All @@ -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);

Expand All @@ -150,6 +181,7 @@
</td>
<td><?php echo $credit_date; ?></td>
<td><?php echo $credit_reference; ?></td>
<td><a href="client_payments.php?client_id=<?php echo $credit_client_id; ?>"><?php echo $payment_invoice_display; ?></a></td>
<td>
<a href="post.php?apply_credit=<?php echo $credit_id; ?>" class="btn btn-sm btn-primary"
title="Apply"><i class="fas fa-credit-card"></i></a>
Expand Down
12 changes: 10 additions & 2 deletions database_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion database_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

42 changes: 29 additions & 13 deletions post/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,19 +780,23 @@
$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']);
$reference = sanitizeInput($_POST['reference']);
$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
Expand Down Expand Up @@ -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);
Expand All @@ -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

Expand All @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -1520,6 +1534,8 @@

} // End Invoice Loop



// Send Email
if ($email_receipt == 1) {

Expand Down Expand Up @@ -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"]);
}
Expand Down

0 comments on commit e2e3601

Please sign in to comment.