handle additional payment deduct from last emi not middle one
parent
b8ebffc570
commit
01f4a72f15
|
@ -12,8 +12,9 @@
|
||||||
$adPaymentSource = $_POST['adPaymentSource'];
|
$adPaymentSource = $_POST['adPaymentSource'];
|
||||||
$adPaymentTran = $_POST['adPaymentTran'];
|
$adPaymentTran = $_POST['adPaymentTran'];
|
||||||
$adPaymentRemarks = $_POST['adPaymentRemarks'];
|
$adPaymentRemarks = $_POST['adPaymentRemarks'];
|
||||||
|
|
||||||
// Insert additional payment record
|
// Insert additional payment record
|
||||||
try{
|
try {
|
||||||
$stmt3 = $db->prepare("INSERT INTO emi (customerId, invoiceId, adPaymentAmount, adPaymentDate, adPaymentSource, adPaymentTran, adPaymentRemarks, payStatus) VALUES (:customerId, :invoiceId, :adPaymentAmount, :adPaymentDate, :adPaymentSource, :adPaymentTran, :adPaymentRemarks, 1)");
|
$stmt3 = $db->prepare("INSERT INTO emi (customerId, invoiceId, adPaymentAmount, adPaymentDate, adPaymentSource, adPaymentTran, adPaymentRemarks, payStatus) VALUES (:customerId, :invoiceId, :adPaymentAmount, :adPaymentDate, :adPaymentSource, :adPaymentTran, :adPaymentRemarks, 1)");
|
||||||
$stmt3->bindParam(':customerId', $customerId);
|
$stmt3->bindParam(':customerId', $customerId);
|
||||||
$stmt3->bindParam(':invoiceId', $invoiceId);
|
$stmt3->bindParam(':invoiceId', $invoiceId);
|
||||||
|
@ -22,61 +23,130 @@
|
||||||
$stmt3->bindParam(':adPaymentSource', $adPaymentSource);
|
$stmt3->bindParam(':adPaymentSource', $adPaymentSource);
|
||||||
$stmt3->bindParam(':adPaymentTran', $adPaymentTran);
|
$stmt3->bindParam(':adPaymentTran', $adPaymentTran);
|
||||||
$stmt3->bindParam(':adPaymentRemarks', $adPaymentRemarks);
|
$stmt3->bindParam(':adPaymentRemarks', $adPaymentRemarks);
|
||||||
if ($stmt3->execute()) {
|
$stmt3->execute();
|
||||||
echo '<div class="alert alert-success">Additional Payment <strong>' . htmlspecialchars($_POST['adPaymentAmount']) . '</strong> applied successfully.</div>';
|
echo '<div class="alert alert-success">Additional Payment <strong>' . htmlspecialchars($_POST['adPaymentAmount']) . '</strong> applied successfully.</div>';
|
||||||
} else {
|
} catch(PDOException $e) {
|
||||||
echo '<div class="alert alert-danger">Failed to record the additional payment.</div>';
|
|
||||||
}
|
|
||||||
}catch(PDOException $e){
|
|
||||||
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
// Apply additional payment to EMIs (starting from furthest future EMI)
|
||||||
$adPaymentAmount = floatval($_POST['adPaymentAmount']);
|
|
||||||
try {
|
try {
|
||||||
// Fetch EMIs of the customer (Assuming we have emiNumber in ascending order)
|
// Get all unpaid EMIs ordered by date DESC (furthest future first)
|
||||||
$stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId ORDER BY emiNumber DESC");
|
$stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId AND emiAmount > 0 AND payStatus = 0 ORDER BY emiDate DESC");
|
||||||
$stmt->bindParam(':customerId', $customerId);
|
$stmt->bindParam(':customerId', $customerId);
|
||||||
$stmt->bindParam(':invoiceId', $invoiceId);
|
$stmt->bindParam(':invoiceId', $invoiceId);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$emiRecords = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$emiRecords = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
if (!$emiRecords) {
|
|
||||||
echo '<div class="alert alert-danger">No EMI records found.</div>';
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
$remainingAmount = $adPaymentAmount;
|
$remainingAmount = $adPaymentAmount;
|
||||||
foreach ($emiRecords as $index => &$emi) {
|
|
||||||
|
foreach ($emiRecords as $emi) {
|
||||||
if ($remainingAmount <= 0) break;
|
if ($remainingAmount <= 0) break;
|
||||||
|
|
||||||
$emiId = $emi['id'];
|
$emiId = $emi['id'];
|
||||||
$emiAmount = floatval($emi['emiAmount']);
|
$emiAmount = floatval($emi['emiAmount']);
|
||||||
|
|
||||||
if ($remainingAmount >= $emiAmount) {
|
if ($remainingAmount >= $emiAmount) {
|
||||||
// If additional amount is greater or equal to EMI, pay it off
|
// Full payment
|
||||||
$payAmount = $emiAmount;
|
|
||||||
$remainingAmount -= $emiAmount;
|
$remainingAmount -= $emiAmount;
|
||||||
$newEmiAmount = 0; // EMI is fully paid
|
$newEmiAmount = 0;
|
||||||
|
$payStatus = 1;
|
||||||
} else {
|
} else {
|
||||||
// If additional amount is less than EMI, reduce this EMI
|
// Partial payment
|
||||||
$payAmount = $remainingAmount;
|
|
||||||
$newEmiAmount = $emiAmount - $remainingAmount;
|
$newEmiAmount = $emiAmount - $remainingAmount;
|
||||||
$remainingAmount = 0; // No more amount left to deduct
|
$remainingAmount = 0;
|
||||||
|
$payStatus = 0; // Still partially unpaid
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update EMI record with reduced amount
|
// Update EMI
|
||||||
$updateStmt = $db->prepare("UPDATE emi SET emiAmount = :newEmiAmount, payStatus = CASE WHEN emiAmount = 0 THEN 1 ELSE 0 END WHERE id = :emiId");
|
$updateStmt = $db->prepare("UPDATE emi SET emiAmount = :newEmiAmount, payStatus = :payStatus, paymentDate = CASE WHEN :payStatus = 1 THEN :paymentDate ELSE paymentDate END WHERE id = :emiId");
|
||||||
$updateStmt->bindParam(':newEmiAmount', $newEmiAmount);
|
$updateStmt->bindParam(':newEmiAmount', $newEmiAmount);
|
||||||
|
$updateStmt->bindParam(':payStatus', $payStatus);
|
||||||
|
$updateStmt->bindParam(':paymentDate', $adPaymentDate);
|
||||||
$updateStmt->bindParam(':emiId', $emiId);
|
$updateStmt->bindParam(':emiId', $emiId);
|
||||||
$updateStmt->execute();
|
$updateStmt->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there's remaining amount after processing all unpaid EMIs
|
||||||
|
if ($remainingAmount > 0) {
|
||||||
|
echo '<div class="alert alert-info">Note: $' . number_format($remainingAmount, 2) . ' was not applied (no unpaid EMIs remaining)</div>';
|
||||||
|
}
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['additional-payment'])){
|
||||||
|
// $adPaymentAmount = floatval($_POST['adPaymentAmount']);
|
||||||
|
// $adPaymentDate = $_POST['adPaymentDate'];
|
||||||
|
// $adPaymentSource = $_POST['adPaymentSource'];
|
||||||
|
// $adPaymentTran = $_POST['adPaymentTran'];
|
||||||
|
// $adPaymentRemarks = $_POST['adPaymentRemarks'];
|
||||||
|
// // Insert additional payment record
|
||||||
|
// try{
|
||||||
|
// $stmt3 = $db->prepare("INSERT INTO emi (customerId, invoiceId, adPaymentAmount, adPaymentDate, adPaymentSource, adPaymentTran, adPaymentRemarks, payStatus) VALUES (:customerId, :invoiceId, :adPaymentAmount, :adPaymentDate, :adPaymentSource, :adPaymentTran, :adPaymentRemarks, 1)");
|
||||||
|
// $stmt3->bindParam(':customerId', $customerId);
|
||||||
|
// $stmt3->bindParam(':invoiceId', $invoiceId);
|
||||||
|
// $stmt3->bindParam(':adPaymentAmount', $adPaymentAmount);
|
||||||
|
// $stmt3->bindParam(':adPaymentDate', $adPaymentDate);
|
||||||
|
// $stmt3->bindParam(':adPaymentSource', $adPaymentSource);
|
||||||
|
// $stmt3->bindParam(':adPaymentTran', $adPaymentTran);
|
||||||
|
// $stmt3->bindParam(':adPaymentRemarks', $adPaymentRemarks);
|
||||||
|
// if ($stmt3->execute()) {
|
||||||
|
// echo '<div class="alert alert-success">Additional Payment <strong>' . htmlspecialchars($_POST['adPaymentAmount']) . '</strong> applied successfully.</div>';
|
||||||
|
// } else {
|
||||||
|
// echo '<div class="alert alert-danger">Failed to record the additional payment.</div>';
|
||||||
|
// }
|
||||||
|
// }catch(PDOException $e){
|
||||||
|
// echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
// $adPaymentAmount = floatval($_POST['adPaymentAmount']);
|
||||||
|
// try {
|
||||||
|
// // Fetch EMIs of the customer (Assuming we have emiNumber in ascending order)
|
||||||
|
// $stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId ORDER BY emiNumber DESC");
|
||||||
|
// $stmt->bindParam(':customerId', $customerId);
|
||||||
|
// $stmt->bindParam(':invoiceId', $invoiceId);
|
||||||
|
// $stmt->execute();
|
||||||
|
// $emiRecords = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
// if (!$emiRecords) {
|
||||||
|
// echo '<div class="alert alert-danger">No EMI records found.</div>';
|
||||||
|
// exit;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $remainingAmount = $adPaymentAmount;
|
||||||
|
// foreach ($emiRecords as $index => &$emi) {
|
||||||
|
// if ($remainingAmount <= 0) break;
|
||||||
|
|
||||||
|
// $emiId = $emi['id'];
|
||||||
|
// $emiAmount = floatval($emi['emiAmount']);
|
||||||
|
|
||||||
|
// if ($remainingAmount >= $emiAmount) {
|
||||||
|
// // If additional amount is greater or equal to EMI, pay it off
|
||||||
|
// $payAmount = $emiAmount;
|
||||||
|
// $remainingAmount -= $emiAmount;
|
||||||
|
// $newEmiAmount = 0; // EMI is fully paid
|
||||||
|
// } else {
|
||||||
|
// // If additional amount is less than EMI, reduce this EMI
|
||||||
|
// $payAmount = $remainingAmount;
|
||||||
|
// $newEmiAmount = $emiAmount - $remainingAmount;
|
||||||
|
// $remainingAmount = 0; // No more amount left to deduct
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Update EMI record with reduced amount
|
||||||
|
// $updateStmt = $db->prepare("UPDATE emi SET emiAmount = :newEmiAmount, payStatus = CASE WHEN emiAmount = 0 THEN 1 ELSE 0 END WHERE id = :emiId");
|
||||||
|
// $updateStmt->bindParam(':newEmiAmount', $newEmiAmount);
|
||||||
|
// $updateStmt->bindParam(':emiId', $emiId);
|
||||||
|
// $updateStmt->execute();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// } catch (PDOException $e) {
|
||||||
|
// echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
<?php
|
||||||
|
require('../.hta_config/conf.php');
|
||||||
|
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
|
||||||
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
|
$customerId = $_GET['customerId'];
|
||||||
|
$invoiceId = $_GET['invoiceId'];
|
||||||
|
|
||||||
|
// Handle Additional Payment
|
||||||
|
if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['additional-payment'])){
|
||||||
|
$adPaymentAmount = floatval($_POST['adPaymentAmount']);
|
||||||
|
$adPaymentDate = $_POST['adPaymentDate'];
|
||||||
|
$adPaymentSource = $_POST['adPaymentSource'];
|
||||||
|
$adPaymentTran = $_POST['adPaymentTran'];
|
||||||
|
$adPaymentRemarks = $_POST['adPaymentRemarks'];
|
||||||
|
|
||||||
|
// Insert additional payment record
|
||||||
|
try {
|
||||||
|
$stmt3 = $db->prepare("INSERT INTO emi (customerId, invoiceId, adPaymentAmount, adPaymentDate, adPaymentSource, adPaymentTran, adPaymentRemarks, payStatus) VALUES (:customerId, :invoiceId, :adPaymentAmount, :adPaymentDate, :adPaymentSource, :adPaymentTran, :adPaymentRemarks, 1)");
|
||||||
|
$stmt3->bindParam(':customerId', $customerId);
|
||||||
|
$stmt3->bindParam(':invoiceId', $invoiceId);
|
||||||
|
$stmt3->bindParam(':adPaymentAmount', $adPaymentAmount);
|
||||||
|
$stmt3->bindParam(':adPaymentDate', $adPaymentDate);
|
||||||
|
$stmt3->bindParam(':adPaymentSource', $adPaymentSource);
|
||||||
|
$stmt3->bindParam(':adPaymentTran', $adPaymentTran);
|
||||||
|
$stmt3->bindParam(':adPaymentRemarks', $adPaymentRemarks);
|
||||||
|
$stmt3->execute();
|
||||||
|
echo '<div class="alert alert-success">Additional Payment <strong>' . htmlspecialchars($_POST['adPaymentAmount']) . '</strong> applied successfully.</div>';
|
||||||
|
} catch(PDOException $e) {
|
||||||
|
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply additional payment to EMIs (starting from furthest future EMI)
|
||||||
|
try {
|
||||||
|
// Get all unpaid EMIs ordered by date DESC (furthest future first)
|
||||||
|
$stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId AND emiAmount > 0 AND payStatus = 0 ORDER BY emiDate DESC");
|
||||||
|
$stmt->bindParam(':customerId', $customerId);
|
||||||
|
$stmt->bindParam(':invoiceId', $invoiceId);
|
||||||
|
$stmt->execute();
|
||||||
|
$emiRecords = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
$remainingAmount = $adPaymentAmount;
|
||||||
|
|
||||||
|
foreach ($emiRecords as $emi) {
|
||||||
|
if ($remainingAmount <= 0) break;
|
||||||
|
|
||||||
|
$emiId = $emi['id'];
|
||||||
|
$emiAmount = floatval($emi['emiAmount']);
|
||||||
|
|
||||||
|
if ($remainingAmount >= $emiAmount) {
|
||||||
|
// Full payment
|
||||||
|
$remainingAmount -= $emiAmount;
|
||||||
|
$newEmiAmount = 0;
|
||||||
|
$payStatus = 1;
|
||||||
|
} else {
|
||||||
|
// Partial payment
|
||||||
|
$newEmiAmount = $emiAmount - $remainingAmount;
|
||||||
|
$remainingAmount = 0;
|
||||||
|
$payStatus = 0; // Still partially unpaid
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update EMI
|
||||||
|
$updateStmt = $db->prepare("UPDATE emi SET emiAmount = :newEmiAmount, payStatus = :payStatus, paymentDate = CASE WHEN :payStatus = 1 THEN :paymentDate ELSE paymentDate END WHERE id = :emiId");
|
||||||
|
$updateStmt->bindParam(':newEmiAmount', $newEmiAmount);
|
||||||
|
$updateStmt->bindParam(':payStatus', $payStatus);
|
||||||
|
$updateStmt->bindParam(':paymentDate', $adPaymentDate);
|
||||||
|
$updateStmt->bindParam(':emiId', $emiId);
|
||||||
|
$updateStmt->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's remaining amount after processing all unpaid EMIs
|
||||||
|
if ($remainingAmount > 0) {
|
||||||
|
echo '<div class="alert alert-info">Note: $' . number_format($remainingAmount, 2) . ' was not applied (no unpaid EMIs remaining)</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [Rest of your existing code for handling EMI status updates and data fetching...]
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!-- [Rest of your HTML and JavaScript remains the same] -->
|
Loading…
Reference in New Issue