arif_grafin/CONTENT/ROOT_URI/exe/deduct-from-recurring/index.php

95 lines
4.4 KiB
PHP

<?php
if ($_SESSION['type'] === 'admin' && isset($_GET["no"]) && isset($_GET["type"]) && $_GET["type"] == "Recurring") {
if (
$_SERVER['REQUEST_METHOD'] === 'POST'
&& isset($_POST['PAY_LOAN_EMI_FROM_RECURRING'], $_POST['PAY_LOAN_EMI_FROM_RECURRING_ID'], $_POST['DEDUCT_LOAN_AMOUNT'])
&& $_POST['PAY_LOAN_EMI_FROM_RECURRING_ID'] === 'axakassaoxnnxsaoij34866'
) {
$loanEMIAmount = floatval($_POST['DEDUCT_LOAN_AMOUNT']);
$deductAmount = -$loanEMIAmount; // negative for recurring deduction
$paidToLoanAccountNumber = $_POST['LOAN_AC_NUMBER'];
$accountId = $_GET['no'];
if (!$accountId) {
echo "Account number missing";
exit;
}
// ✅ DB connection
$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// ✅ Check recurring balance first
$sql = "SELECT AA_BAL FROM `" . $GLOBALS['arif_ac'] . "` WHERE `AA_ACNO` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $accountId);
$stmt->execute();
$stmt->bind_result($CURRENT_RECURRING_BALANCE);
$stmt->fetch();
$stmt->close();
if ($loanEMIAmount > $CURRENT_RECURRING_BALANCE) {
echo "<div class='container' style=' background-color: #f8d7da; color: #721c24; padding: 12px 20px; border: 1px solid #f5c6cb; border-radius: 5px; font-family: Arial, sans-serif; font-size: 15px; margin: 10px auto; position: relative;'>
<strong>Error!</strong> Insufficient balance in Recurring account.
<span style=\" position: absolute; top: 8px; right: 12px; color: #721c24; font-weight: bold; cursor: pointer; \" onclick=\"this.parentElement.style.display='none';\">&times;</span>
</div>";
exit;
}
// ✅ Begin transaction for atomicity
$conn->begin_transaction();
try {
$table = $GLOBALS['arif_tran'] ?? 'arif_tran';
$userType = 'admin';
// Entry 1: Deduction from recurring
$remarksText1 = "$loanEMIAmount deducted from Recurring for Loan A/c $paidToLoanAccountNumber EMI";
$stmt = $conn->prepare("INSERT INTO `$table` (AT_ADMIN, AT_ACID, AT_AMOUNT, REMARKS) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssds", $userType, $accountId, $deductAmount, $remarksText1);
$stmt->execute();
$stmt->close();
// Entry 2: Credit to loan account
$remarksText2 = "$loanEMIAmount credited to Loan A/c $paidToLoanAccountNumber EMI (from Recurring $accountId)";
$stmt = $conn->prepare("INSERT INTO `$table` (AT_ADMIN, AT_ACID, AT_AMOUNT, REMARKS) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssds", $userType, $paidToLoanAccountNumber, $loanEMIAmount, $remarksText2);
$stmt->execute();
$stmt->close();
// Update Loan Account
$ins_no = 1;
$sql = "UPDATE `" . $GLOBALS['arif_ac'] . "` SET `AA_BAL` = `AA_BAL` + ?, `AA_NO_OF_PAYPAID` = `AA_NO_OF_PAYPAID` + ? WHERE `AA_ACNO` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("dis", $loanEMIAmount, $ins_no, $paidToLoanAccountNumber);
$stmt->execute();
$stmt->close();
// Update Recurring Account
$sql = "UPDATE `" . $GLOBALS['arif_ac'] . "` SET `AA_BAL` = `AA_BAL` - ? WHERE `AA_ACNO` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ds", $loanEMIAmount, $accountId);
$stmt->execute();
$stmt->close();
// ✅ Commit if everything ok
$conn->commit();
echo "<div class='container' style=' background-color: #d4edda; color: #155724; padding: 12px 20px; border: 1px solid #c3e6cb; border-radius: 5px; font-family: Arial, sans-serif; font-size: 15px; margin: 10px auto; position: relative;'>
<strong>Success!</strong> Loan EMI paid successfully!
<span style=\" position: absolute; top: 8px; right: 12px; color: #155724; font-weight: bold; cursor: pointer; \" onclick=\"this.parentElement.style.display='none';\">&times;</span>
</div>";
} catch (Exception $e) {
$conn->rollback();
echo "Error processing EMI payment: " . $e->getMessage();
}
$conn->close();
}
}
?>