diff --git a/customers/.hta_slug/additional-payment.php b/customers/.hta_slug/additional-payment.php
index 35e38a8..69c2260 100644
--- a/customers/.hta_slug/additional-payment.php
+++ b/customers/.hta_slug/additional-payment.php
@@ -24,7 +24,6 @@
} catch(PDOException $e){
echo '
Error: ' . $e->getMessage() . '
';
}
-
}
try {
$stmt = $db->prepare("SELECT * FROM invoice WHERE customerId = :customerId");
diff --git a/customers/.hta_slug/billing-details.php b/customers/.hta_slug/billing-details.php
index 1176940..3dd60b3 100644
--- a/customers/.hta_slug/billing-details.php
+++ b/customers/.hta_slug/billing-details.php
@@ -3,32 +3,85 @@
$db = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-
- if($_SERVER['REQUEST_METHOD'] === 'POST'){
- 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', $_GET['customerId']);
- $stmt3->bindParam(':invoiceId', $_GET['invoiceId']);
- $stmt3->bindParam(':adPaymentAmount', $_POST['adPaymentAmount']);
- $stmt3->bindParam(':adPaymentDate', $_POST['adPaymentDate']);
- $stmt3->bindParam(':adPaymentSource', $_POST['adPaymentSource']);
- $stmt3->bindParam(':adPaymentTran', $_POST['adPaymentTran']);
- $stmt3->bindParam(':adPaymentRemarks', $_POST['adPaymentRemarks']);
- if($stmt3->execute()){
- echo 'Additional Payment ' . htmlspecialchars($_POST['adPaymentAmount']) . ' Paid Successfully.
';
- }else{
- echo 'Additional Payment ' . htmlspecialchars($_POST['adPaymentAmount']) . ' Payment Faild.
';
- }
- } catch(PDOException $e){
- echo 'Error: ' . $e->getMessage() . '
';
- }
- }
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
- $stmt = $db->prepare("SELECT * FROM invoice WHERE customerId = :customerId");
+ // Get customer EMI details
+ $customerId = $_GET['customerId'];
+ $invoiceId = $_GET['invoiceId'];
+ $adPaymentAmount = floatval($_POST['adPaymentAmount']);
+ $adPaymentDate = $_POST['adPaymentDate'];
+ $adPaymentSource = $_POST['adPaymentSource'];
+ $adPaymentTran = $_POST['adPaymentTran'];
+ $adPaymentRemarks = $_POST['adPaymentRemarks'];
+
+ // 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 'No EMI records found.
';
+ 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();
+ }
+
+ // Insert additional payment record
+ $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 'Additional Payment ' . htmlspecialchars($_POST['adPaymentAmount']) . ' applied successfully.
';
+ } else {
+ echo 'Failed to record the additional payment.
';
+ }
+ } catch (PDOException $e) {
+ echo 'Error: ' . $e->getMessage() . '
';
+ }
+ }
+
+
+
+
+ try {
+ $stmt = $db->prepare("SELECT * FROM invoice WHERE customerId = :customerId AND invoiceId = :invoiceId");
+ $stmt->bindParam(':invoiceId', $_GET['invoiceId']);
$stmt->bindParam(':customerId', $_GET['customerId']);
$stmt->execute();
$invoiceData = $stmt->fetch(PDO::FETCH_ASSOC);
-
+
$stmt2 = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId");
$stmt2->bindParam(':customerId', $_GET['customerId']);
$stmt2->execute();
@@ -48,11 +101,16 @@
$stmt->bindParam(':payStatus', $_POST['payStatus'], PDO::PARAM_INT);
$stmt->bindParam(':emiId', $_POST['emiId'], PDO::PARAM_INT);
$stmt->execute();
+
+ // Ensure no previous output before JSON
echo json_encode(['status' => 'success']);
+ exit; // Terminate script after sending JSON
} catch (PDOException $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
+ exit;
}
}
+
try {
// Fetch EMI data
$stmt = $db->prepare("SELECT * FROM emi WHERE customerId = :customerId AND invoiceId = :invoiceId ORDER BY emiDate ASC");
@@ -113,7 +171,7 @@
Total Amount: $
Outstanding: $
Tenure:
- Frequency:
+ Frequency:
@@ -126,7 +184,6 @@
EMI Amount |
EMI Date |
Payment Status |
- Outstanding |
Action |
@@ -141,7 +198,6 @@
= $emi['payStatus'] == 0 ? 'Unpaid' : 'Paid'; ?>
- $= number_format($emi['outstanding'], 2); ?> |
|