From b813a79fe8cc10a4aa4bcab2087b7d1155d890b5 Mon Sep 17 00:00:00 2001 From: Suvodip Date: Sat, 8 Mar 2025 17:01:20 +0530 Subject: [PATCH] s11 --- customers/.hta_slug/additional-payment.php | 1 - customers/.hta_slug/billing-details.php | 148 +++++++++++++++------ customers/.hta_slug/edit-invoice.php | 96 ++++++------- customers/.hta_slug/generate-invoice.php | 39 +++--- customers/.hta_slug/list.php | 12 +- customers/.hta_slug/print-invoice.php | 51 +++++-- my-account/.hta_slug/print-invoice.php | 8 +- 7 files changed, 229 insertions(+), 126 deletions(-) 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 @@ - $ +
@@ -223,12 +275,12 @@
+ function addAutoAddRemarks() { + const emiAmount = ; + const addPayAmount = parseFloat(document.getElementById('adPaymentAmount').value) || 0; + const extraAmount = addPayAmount - emiAmount; + + document.getElementById('adPaymentRemarks').value = + `EMI Amount: ${emiAmount}. Extra ${extraAmount > 0 ? extraAmount.toFixed(2) : "0.00"} Settled in last EMI `; + } + + + + \ No newline at end of file diff --git a/customers/.hta_slug/edit-invoice.php b/customers/.hta_slug/edit-invoice.php index 15475d5..3ae919f 100644 --- a/customers/.hta_slug/edit-invoice.php +++ b/customers/.hta_slug/edit-invoice.php @@ -22,8 +22,8 @@ $description = $_POST['description']; $quantity = $_POST['quantity']; $rate = $_POST['rate']; - $tax = $_POST['tax']; - $taxAmount = $_POST['taxAmount']; + $discount = $_POST['discount']; + $discountAmount = $_POST['discountAmount']; @@ -32,31 +32,31 @@ $outstandingAmount = $totalAmount; $emiDate = new DateTime($bookingDate); // Set initial date - $stmt = $db->prepare("INSERT INTO emi (customerId, emiNumber, emiAmount, emiDate, totalAmount, outstanding, tenure, frequency, bookingDate, invoiceId) VALUES (:customerId, :emiNumber, :emiAmount, :emiDate, :totalAmount, :outstanding, :tenure, :frequency, :bookingDate, :invoiceId)"); - for ($i = 1; $i <= $tenure; $i++) { - $outstandingAmount -= $emiAmount; - $emiDateFormatted = $emiDate->format('Y-m-d'); - $stmt->bindParam(':customerId', $_GET['customerId']); - $stmt->bindParam(':emiNumber', $i); - $stmt->bindParam(':emiAmount', $emiAmount); - $stmt->bindParam(':emiDate', $emiDateFormatted); - $stmt->bindParam(':totalAmount', $totalAmount); - $stmt->bindParam(':outstanding', $outstandingAmount); - $stmt->bindParam(':tenure', $tenure); - $stmt->bindParam(':frequency', $frequency); - $stmt->bindParam(':bookingDate', $bookingDate); - $stmt->bindParam(':invoiceId', $invoiceId); - $stmt->execute(); - // Move to the next EMI date - if (trim(strtolower($frequency)) === 'weekly') { - $emiDate->modify('+1 week'); - } elseif (trim(strtolower($frequency)) === 'monthly') { - $emiDate->modify('+1 month'); - } - } + // $stmt = $db->prepare("INSERT INTO emi (customerId, emiNumber, emiAmount, emiDate, totalAmount, outstanding, tenure, frequency, bookingDate, invoiceId) VALUES (:customerId, :emiNumber, :emiAmount, :emiDate, :totalAmount, :outstanding, :tenure, :frequency, :bookingDate, :invoiceId)"); + // for ($i = 1; $i <= $tenure; $i++) { + // $outstandingAmount -= $emiAmount; + // $emiDateFormatted = $emiDate->format('Y-m-d'); + // $stmt->bindParam(':customerId', $_GET['customerId']); + // $stmt->bindParam(':emiNumber', $i); + // $stmt->bindParam(':emiAmount', $emiAmount); + // $stmt->bindParam(':emiDate', $emiDateFormatted); + // $stmt->bindParam(':totalAmount', $totalAmount); + // $stmt->bindParam(':outstanding', $outstandingAmount); + // $stmt->bindParam(':tenure', $tenure); + // $stmt->bindParam(':frequency', $frequency); + // $stmt->bindParam(':bookingDate', $bookingDate); + // $stmt->bindParam(':invoiceId', $invoiceId); + // $stmt->execute(); + // // Move to the next EMI date + // if (trim(strtolower($frequency)) === 'weekly') { + // $emiDate->modify('+1 week'); + // } elseif (trim(strtolower($frequency)) === 'monthly') { + // $emiDate->modify('+1 month'); + // } + // } // Insert into invoice table - $stmt2 = $db->prepare("UPDATE invoice SET customerName = :customerName, address = :address, frequency = :frequency, invoiceDate = :invoiceDate, paymentMode = :paymentMode, salesAgent = :salesAgent, marketingAgent = :marketingAgent, tenure = :tenure, item = :item, description = :description, qty = :qty, rate = :rate, tax = :tax, totalAmount = :totalAmount, adminNote = :adminNote, taxAmount = :taxAmount WHERE customerId = :customerId AND invoiceId = :invoiceId"); + $stmt2 = $db->prepare("UPDATE invoice SET customerName = :customerName, address = :address, frequency = :frequency, invoiceDate = :invoiceDate, paymentMode = :paymentMode, salesAgent = :salesAgent, marketingAgent = :marketingAgent, tenure = :tenure, item = :item, description = :description, qty = :qty, rate = :rate, discount = :discount, totalAmount = :totalAmount, adminNote = :adminNote, discount = :discount WHERE customerId = :customerId AND invoiceId = :invoiceId"); $stmt2->bindParam(':customerId', $_GET['customerId']); $stmt2->bindParam(':invoiceId', $invoiceId); $stmt2->bindParam(':customerName', $customerName); @@ -71,13 +71,11 @@ $stmt2->bindParam(':description', $description); $stmt2->bindParam(':qty', $quantity); $stmt2->bindParam(':rate', $rate); - $stmt2->bindParam(':tax', $tax); + $stmt2->bindParam(':discount', $discount); $stmt2->bindParam(':totalAmount', $totalAmount); $stmt2->bindParam(':adminNote', $adminNote); - $stmt2->bindParam(':taxAmount', $taxAmount); + $stmt2->bindParam(':discountAmount', $discountAmount); $stmt2->execute(); - - echo '
New EMI Plan Saved Successfully!
'; echo ' \ No newline at end of file + + + + \ No newline at end of file diff --git a/customers/.hta_slug/generate-invoice.php b/customers/.hta_slug/generate-invoice.php index b9abbf8..ad93e76 100644 --- a/customers/.hta_slug/generate-invoice.php +++ b/customers/.hta_slug/generate-invoice.php @@ -22,10 +22,8 @@ $description = $_POST['description']; $quantity = $_POST['quantity']; $rate = $_POST['rate']; - $tax = $_POST['tax']; - $taxAmount = $_POST['taxAmount']; - - + $discount = $_POST['discount']; + $discountAmount = $_POST['discountAmount']; // EMI Calculation $emiAmount = round($totalAmount / $tenure, 2); @@ -56,7 +54,7 @@ } // Insert into invoice table - $stmt2 = $db->prepare("INSERT INTO invoice (customerId, invoiceId, customerName, address, frequency, invoiceDate, paymentMode, salesAgent, marketingAgent, tenure, item, description, qty, rate, tax, totalAmount, adminNote, taxAmount) VALUES (:customerId, :invoiceId, :customerName, :address, :frequency, :invoiceDate, :paymentMode, :salesAgent, :marketingAgent, :tenure, :item, :description, :qty, :rate, :tax, :totalAmount, :adminNote, :taxAmount)"); + $stmt2 = $db->prepare("INSERT INTO invoice (customerId, invoiceId, customerName, address, frequency, invoiceDate, paymentMode, salesAgent, marketingAgent, tenure, item, description, qty, rate, discount, totalAmount, adminNote, discountAmount) VALUES (:customerId, :invoiceId, :customerName, :address, :frequency, :invoiceDate, :paymentMode, :salesAgent, :marketingAgent, :tenure, :item, :description, :qty, :rate, :discount, :totalAmount, :adminNote, :discountAmount)"); $stmt2->bindParam(':customerId', $_GET['customerId']); $stmt2->bindParam(':invoiceId', $invoiceId); $stmt2->bindParam(':customerName', $customerName); @@ -71,10 +69,10 @@ $stmt2->bindParam(':description', $description); $stmt2->bindParam(':qty', $quantity); $stmt2->bindParam(':rate', $rate); - $stmt2->bindParam(':tax', $tax); + $stmt2->bindParam(':discount', $discount); $stmt2->bindParam(':totalAmount', $totalAmount); $stmt2->bindParam(':adminNote', $adminNote); - $stmt2->bindParam(':taxAmount', $taxAmount); + $stmt2->bindParam(':discountAmount', $discountAmount); $stmt2->execute(); echo '
New EMI Plan Saved Successfully!
'; @@ -156,13 +154,14 @@
- +
@@ -213,8 +212,8 @@ Description Qty Rate - Tax % - Tax Amount + Discount % + Discount Amount Amount @@ -233,8 +232,8 @@ - + @@ -242,7 +241,7 @@ - + @@ -297,8 +296,8 @@ document.addEventListener("DOMContentLoaded", function () { const quantityInput = document.getElementById("quantity"); const rateInput = document.getElementById("rate"); - const taxSelect = document.getElementById("tax"); - const taxAmountInput = document.getElementById("taxAmount"); + const taxSelect = document.getElementById("discount"); + const taxAmountInput = document.getElementById("discountAmount"); const totalAmountInput = document.getElementById("totalAmount"); function calculateTotal() { @@ -308,7 +307,7 @@ let subtotal = quantity * rate; let taxAmount = (subtotal * tax) / 100; - let grandTotal = subtotal + taxAmount; + let grandTotal = subtotal - taxAmount; taxAmountInput.value = taxAmount.toFixed(2); // Format Tax Amount totalAmountInput.value = grandTotal.toFixed(2); // Format Total Amount diff --git a/customers/.hta_slug/list.php b/customers/.hta_slug/list.php index 93ac870..eb82fdb 100644 --- a/customers/.hta_slug/list.php +++ b/customers/.hta_slug/list.php @@ -4,7 +4,7 @@

Customer List


- +
@@ -15,7 +15,7 @@ - + @@ -84,9 +84,9 @@ 1) { - - + + @@ -54,8 +54,8 @@ if ($invoiceInfo['tenure'] > 1) { - - + + @@ -63,7 +63,7 @@ if ($invoiceInfo['tenure'] > 1) { 1): ?> -

EMI Payment Plan

+
EMI Payment Plan
Sl NoMonthly Payment Date Monthly Payment Amount Total AmountActionInvoice
$ - New Invoice - Edit Invoice - Payment + New + Edit + Print
No EMI or Invoice Data - Create Invoice + New
Description Qty RateTax(%)TaxDiscount(%)Discount Total
%$%$ $
@@ -75,7 +75,7 @@ if ($invoiceInfo['tenure'] > 1) { - + @@ -85,18 +85,49 @@ if ($invoiceInfo['tenure'] > 1) { Paid' : 'Pending' ?> - +
+
Additional Payment Details:
+ + + + + + + + + + + + 0) { + ?> + + + + + + + + + +
Payment AmountPayment DatePayment SourceTransaction IDRemarks
+

Admin Note:

- - +

Invoice Generate Date:

- +