JezK
Edit File: products-paymentdetails.php
<?php include "sidemenu-start.php"; include dirname(__DIR__) . '/admin/app/adminObject.php'; $productId = base64_decode($_GET['product_id']); ?> <div class="container-fluid"> <h1>Payment Details</h1> <div class="row mt-5"> <div class="col-md-3 mx-3"> <form id="getUserDetails"> <div class="mb-3 position-relative"> <label for="userMobile" class="form-label">User Mobile</label> <input type="number" class="form-control" id="userMobile" name="userMobile"> <button type="submit" class="no-btn pr-icon" onclick="return validatePaymentsUserId()"> <img src="./images/arrow.png"></button> </div> <p class="error userMobile_error"></p> </form> <div class="mb-3"> <label for="name" class="form-label">User Id</label> <input type="text" class="form-control" id="detailingId" readonly> </div> <div class="mb-3"> <label for="name" class="form-label">Name</label> <input type="text" class="form-control" id="name" readonly> </div> <div class="mb-3"> <label for="email" class="form-label">Email</label> <input type="email" class="form-control" id="email" readonly> </div> <div class="mb-3"> <label for="address" class="form-label">Address</label> <textarea class="form-control" id="address" rows="3" readonly></textarea> </div> <div class="mb-3"> <input type="hidden" class="form-control" id="checkUserId" name="checkUserId"> </div> </div> <div class="col-md-4 mx-3"> <div class="payments-card"> <div id="itemNamesAndCost"> </div> <div class="hr-line"> <form id="couponForm"> <div class="p-row "> <div class="d-flex flex-column position-relative"> <input type="text" id="coupon" placeholder="Enter Coupon" class="coupon afterPayment" name="coupon"> <p class="afterPaymentCoupon d-none">Offer Amount</p> <button class="no-btn coupon-icon" type="submit" onclick="return validatePaymentCoupon()" id="couponButton"> <img src="./images/arrow.png"></button> <p class="error coupon_error" style="font-size:13px"></p> </div> <span class="rednote"></span> <input type="button" value="Remove Coupon" id="rcoupon" class="de-btn-small d-block m-auto"> </div> </form> <input type="hidden" id="couponId" value=0> <div class="mt-3 d-flex justify-content-between d-none" id="loyaltyPointsDiv"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="addLoyalPoints" name="addLoyalPoints" onchange="calculateLoyaltyPoints()"> <label class="form-check-label" for="addLoyalPoints"> Add Loyal Points </label> </div> <span id="loyaltyPoints"></span> </div> <div class="mt-3 d-flex justify-content-between d-none" id="referralPointsDiv"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="addReferralPoints" name="addReferralPoints" onchange="calculateReferralPoints()"> <label class="form-check-label" for="addReferralPoints"> Add Referral Points </label> </div> <span id="referralPoints"></span> </div> </div> <div class="p-row hr-line"> <h5>SubTotal</h5> <h5>Rs <span id="totalCost"></span></h5> </div> <div class="p-row "> <h5>Tax Rate</h5> <h5>18% </h5> </div> <div class="p-row "> <h5>Tax</h5> <h5>Rs <span id="tax"></span></h5> </div> <div class="p-row d-none" id="couponDisplay"> <h5>Coupon Points</h5> <h5>- Rs. <span id="offAmount">0</span></p> </div> <div class="p-row d-none" id="loyaltyDisplay"> <h5>Loyalty Points</h5> <h5>- Rs. <span id="loyalty">0</span></p> </div> <div class="p-row d-none" id="referralDisplay"> <h5>Referral Points</h5> <h5>- Rs. <span id="referral">0</span></p> </div> <div class="p-row "> <h5>Grand Total</h5> <h5>Rs <span id="grandTotal"></span></h5> </div> <form id="productsPaymentForm"> <button type="submit" id="paymentButton" class="de-btn-small d-block m-auto" onclick="return validateProductPaymentFrom()">PAYMENT</button> <p class="text-center error payment_error"></p> </form> <button type="button" id="disablePayment" class="de-btns de-grey-btn d-block m-auto d-none">PAYMENT</button> <button type="button" id="invoice" class="no-btn d-block m-auto d-none"><a href="" id="downloadInvoice">Invoice </a><img src="./images/download.png" /> </button> </div> </div> </form> </div> <h3 style="font-weight:600;margin-left:1rem;">Products</h3> <div class="row products-list"> <div class="row" id="appendSelectedProducts"> </div> </div> <h3 style="font-weight:600;margin-left:1rem;">Services</h3> <!-- Seleted Services are Showing --> <div class="row products-list"> <div class="row" id="appendData"> <div class="col-lg-2 col-4 d-flex align-items-center"> <div class="card card-plus"> <div class="card-body"> <button data-bs-toggle="modal" data-bs-target="#addserivices" class="no-btn plus">+</button> </div> </div> </div> </div> </div> </div> <!-- Product Modal --> <div class="modal fade" id="addproduct" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog modal-xl modal-dialog-centered"> <div class="modal-content de-modal-content"> <div class="modal-header de-mh"> <h5 class="modal-title" id="exampleModalLabel">Add Product</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <div class="row" id="productsModal"> </div> </div> </div> </div> </div> <!-- Services Modal --> <div class="modal fade" id="addserivices" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog modal-xl modal-dialog-centered"> <div class="modal-content de-modal-content"> <div class="modal-header de-mh"> <h5 class="modal-title" id="exampleModalLabel">Add Services</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <div class="row" id="servicesModal"> </div> </div> </div> </div> </div> <?php include "sidemenu-end.php"; ?> <script> let baseUrl = "<?Php echo $baseUrl ?>"; let productId = "<?php echo $productId ?>"; let selectedProductsIds = []; let selectedProductsNames = []; let selectedProductQuantity = []; selectedProductsIds.push(productId); selectedProductQuantity.push(1); let selectedServices = []; let servicesNames = []; let servicesCosts = []; let serviceCarTypes = [];updateSelectedServices $(document).ready(function() { $.ajax({ type: 'POST', url: baseUrl + 'admin/app/productsPayments.php', data: { getproduct: productId }, success: function(response) { data = JSON.parse(response); let selectedItem = data.selectedItem; let services = data.services let products = data.products let name = data.selectedProductDetails.name; let amount = data.selectedProductDetails.amount; let count = 1; // Product Count let grandTotal = parseInt(amount) + ((amount * 18) / 100) grandTotal = grandTotal.toFixed(2) $('#servicesModal').html(services); // Services Modal $('#productsModal').html(products) // Products Modal $('#appendSelectedProducts').html(selectedItem); // selected Item $('#itemNamesAndCost').html('<div class="p-row"><h5>' + name + '(' + parseInt(count) + ')' + '</h5>' + '<h5 class="service-cost">' + amount + ' Rs' + '</h5><div>') $('#totalCost').html(amount); $('#tax').html((amount * 18) / 100); $('#grandTotal').html(grandTotal); } }) }) $('#getUserDetails').submit(function(event) { event.preventDefault(); let userMobile = $('#userMobile').val(); $.ajax({ type: 'POST', url: baseUrl + 'admin/app/editUser.php', data: { userMobile: userMobile }, success: function(data) { userDetails = JSON.parse(data); if (userDetails.status == 0) { toastr.warning('Invalid User') } else if (userDetails.status == 2) { toastr.warning('User Account Deleted') } else { $('#name').val(userDetails.user_name) $('#detailingId').val(userDetails.user_id) $('#email').val(userDetails.email) $('#address').val(userDetails.address) $('#checkUserId').val(userDetails.id) if(userDetails.loyal_user && userDetails.loyalty_points > 0 ){ $('#loyaltyPointsDiv').removeClass('d-none'); $('#loyaltyPoints').html(userDetails.loyalty_points) } if(userDetails.referral_points > 0){ $('#referralPointsDiv').removeClass('d-none'); $('#referralPoints').html(userDetails.referral_points) } } } }) }); // In Modal when click on Selecte or selected updating selected Products Array function updateSelectedProducts(Id, type) { productId = Id.match(/\d+/)[0]; // Selected Items if (type == 1) { selectedProductsIds.push(productId) // Updating ProductId in Products Array // Adding Quantity for selected productId in the index of productId of selectedProductQuantity; let IndexOfproductId = selectedProductsIds.indexOf(productId) selectedProductQuantity[IndexOfproductId] = 1; } else if (type == 2) { // DeSelected index = selectedProductsIds.indexOf(productId); if (index > -1) { selectedProductsIds.splice(index, 1); selectedProductQuantity.splice(index, 1); } } updateProductModal(selectedProductsIds) } // For Updating Modal when click on select function updateProductModal(selectedProductsIds) { let selectedServicesIds = JSON.stringify(selectedServices); let selectedProducts = JSON.stringify(selectedProductsIds); let productQuantites = JSON.stringify(selectedProductQuantity); let selectedServiceCarTypes = JSON.stringify(serviceCarTypes); $.ajax({ type: 'POST', url: baseUrl + 'admin/app/productsPayments.php', data: { ServicesIds: selectedServicesIds, ProductsIds: selectedProducts, productQuantity: productQuantites, selectedServiceCarTypes: selectedServiceCarTypes }, success: function(response) { let data = JSON.parse(response); let appendNameAndCost = '' let totalCost = data.totalCost $('#productsModal').html(data.modal) $('#appendSelectedProducts').html(data.selectedItem) $('#totalCost').html(totalCost); // Checking Offer Amount Before Appending Grand Total let offerAmount = $('#offAmount').html() let loyaltyPoints = $('#loyalty').html(); if (parseInt(offerAmount) > 0 || parseInt(loyaltyPoints) > 0) { grandTotal = totalCost - offerAmount; if(loyaltyPoints > 0){ grandTotal = grandTotal - loyaltyPoints; } finalTotal = grandTotal + ((grandTotal*18)/100); grandTotal = grandTotal.toFixed(2) $('#tax').html((grandTotal * 18) / 100); $('#grandTotal').html(finalTotal); } else { grandTotal = totalCost + ((totalCost * 18) / 100) grandTotal = grandTotal.toFixed(2) $('#tax').html((totalCost * 18) / 100); $('#grandTotal').html(grandTotal); } $('#itemNamesAndCost').html(''); // Showing Service Names And Costs If Any Service Selected if (selectedServices.length > 0) { let row; servicesNames = data.servicesNames; // Assgning Services Names As Array servicesCosts = data.servicesCosts; // Assgning Services Costs As Array for (let i = 0; i < servicesNames.length; i++) { row = '<div class="p-row"><h5>' + servicesNames[i] + '</h5>' + '<h5>' + servicesCosts[i] + 'Rs' + '</h5></div>'; appendNameAndCost = appendNameAndCost + row; } } // Showing Products Names And Costs If Any Service Selected if (selectedProductsIds.length > 0) { let row; productsNames = data.productsNames; // Assgning Products Names As Array productsCosts = data.productsCosts; // Assgning Products Costs As Array productsCount = data.productsCount // Assgning Products Counr As Array for (let i = 0; i < productsNames.length; i++) { row = '<div class="p-row"><h5>' + productsNames[i] + '(' + parseInt(productsCount[i]) + ')' + '</h5>' + '<h5>' + productsCosts[i] + ' Rs' + '</h5></div>'; appendNameAndCost = appendNameAndCost + row; } } $('#itemNamesAndCost').append(appendNameAndCost); } }) } // When Pressing X button in Selected Services function removeProduct(productId) { let index = selectedProductsIds.indexOf(productId.toString()); if (index > -1) { selectedProductsIds.splice(index, 1); selectedProductQuantity.splice(index, 1); } updateProductModal(selectedProductsIds) } $(document).on('click', '.increase', function() { productId = this.value; type = "increase"; updateQuantity(productId, type) }); $(document).on('click', '.decrease', function() { productId = this.value; type = "decrease"; updateQuantity(productId, type) }); function updateQuantity(id, type) { productId = '#quanity' + id; let quantityValue = parseInt($(productId).val()); if (type == "increase") { quantityValue = quantityValue + 1; // Finding Index to update in selectedProductQuantity Array IndexOfproductId = selectedProductsIds.indexOf(id) selectedProductQuantity[IndexOfproductId] = quantityValue } else if (type == "decrease") { if (quantityValue >= 2) { quantityValue = quantityValue - 1; } IndexOfproductId = selectedProductsIds.indexOf(id) selectedProductQuantity[IndexOfproductId] = quantityValue } $(productId).val(quantityValue) updateProductModal(selectedProductsIds) } // In Modal when click on Selecte or selected updating selectedServices Array function updateSelectedServices(Id, type, carType) { serviceId = Id.match(/\d+/)[0]; // Selected Items if (type == 1) { selectedServices.push(serviceId) serviceCarTypes.push(carType) } else if (type == 2) { // DeSelected index = selectedServices.indexOf(serviceId); if (index > -1) { selectedServices.splice(index, 1); serviceCarTypes.splice(index, 1); } } updateModalServices(selectedServices) } // For Updating Modal when click on select and Unselect function updateModalServices(selectedServices) { let selectedServicesIds = JSON.stringify(selectedServices); let selectedServiceCarTypes = JSON.stringify(serviceCarTypes); let selectedProducts = JSON.stringify(selectedProductsIds); let productQuantites = JSON.stringify(selectedProductQuantity); $.ajax({ type: 'POST', url: baseUrl + 'admin/app/productsPayments.php', data: { selectedServicesIds: selectedServicesIds, selectedProductsIds: selectedProducts, productQuantity: productQuantites, selectedServiceCarTypes: selectedServiceCarTypes }, success: function(response) { let data = JSON.parse(response) let appendNameAndCost = ''; let totalCost = data.totalCost let offerAmount; let loyaltyPoints; $('#servicesModal').html(data.modal) $('#appendData').html(data.selectedItem); $('#totalCost').html(totalCost); // Checking Offer Amount Before Appending Grand Total offerAmount = $('#offAmount').html() loyaltyPoints = $('#loyalty').html() if (parseInt(offerAmount) > 0 || parseInt(loyaltyPoints) > 0) { grandTotal = totalCost - offerAmount; if(loyaltyPoints > 0){ grandTotal = grandTotal - loyaltyPoints; } finalTotal = grandTotal +((grandTotal*18)/100) grandTotal = grandTotal.toFixed(2) $('#tax').html((grandTotal * 18) / 100); $('#grandTotal').html(finalTotal); } else { grandTotal = totalCost + ((totalCost * 18) / 100) grandTotal = grandTotal.toFixed(2) $('#tax').html((totalCost * 18) / 100); $('#grandTotal').html(grandTotal); } $('#itemNamesAndCost').html(''); // Showing Service Names And Costs If Any Service Selected if (selectedServices.length > 0) { let row; servicesNames = data.servicesNames; // Assgning Services Names As Array servicesCosts = data.servicesCosts; // Assgning Services Costs As Array for (let i = 0; i < servicesNames.length; i++) { row = '<div class="p-row"><h5>' + servicesNames[i] + '</h5>' + '<h5>' + servicesCosts[i] + 'Rs' + '</h5></div>'; appendNameAndCost = appendNameAndCost + row; } } // Showing Products Names And Costs If Any Service Selected if (selectedProductsIds.length > 0) { let row; productsNames = data.productsNames; // Assgning Products Names As Array productsCosts = data.productsCosts; // Assgning Products Costs As Array productsCount = data.productsCount // Assgning Products Counr As Array for (let i = 0; i < productsNames.length; i++) { row = '<div class="p-row"><h5>' + productsNames[i] + '(' + parseInt(productsCount[i]) + ')' + '</h5>' + '<h5>' + productsCosts[i] + ' Rs' + '</h5></div>'; appendNameAndCost = appendNameAndCost + row; } } $('#itemNamesAndCost').append(appendNameAndCost); } }) } // When Pressing X button in Selected Services Removing Service Id function removeService(serviceId) { let index = selectedServices.indexOf(serviceId.toString()); if (index > -1) { selectedServices.splice(index, 1); serviceCarTypes.splice(index, 1); } updateModalServices(selectedServices) } $('#couponForm').submit(function(event) { event.preventDefault(); let grandTotal = $('#grandTotal').html(); if (parseInt(grandTotal) > 0) { let coupon = $('#coupon').val(); $.ajax({ type: 'POST', url: baseUrl + 'admin/app/productsPayments.php', data: { coupon: coupon }, success: function(response) { data = JSON.parse(response); let checkTotalCost = $('#totalCost').html(); if (checkTotalCost > 0) { let usersLeft = parseInt(data.usercount) - parseInt(data.usedusers); let couponMinBill = data.min_bill; let couponType = data.cpn_type; let offerAmount; if(couponType == 1) { offerAmount = parseInt(data.amount) } else { offerAmount = (checkTotalCost * (data.percent/100)).toFixed(2); } if (usersLeft > 0 && offerAmount < checkTotalCost && couponMinBill <= checkTotalCost) { $('#couponDisplay').removeClass('d-none'); $('#offAmount').html(offerAmount) $('.coupon_error').html(usersLeft + 'Users Left') $('#couponId').val(data.coupon_id) priceDetails(checkTotalCost); } else if (data.status == 0) { $('#couponDisplay').addClass('d-none'); $('.coupon_error').html('Invalid Coupon') $('#offAmount').html('0') $('#couponId').val(0); priceDetails(checkTotalCost); } else { if (offerAmount > checkTotalCost) { $('.coupon_error').html('Offer Amount Greather Than Bill Amount'); $('#offAmount').html('0') } else if (couponMinBill > checkTotalCost) { $('.coupon_error').html('Coupon Minimum Balance: ' + couponMinBill); $('#offAmount').html('0') } else { $('.coupon_error').html(usersLeft + ' Users Left') $('#offAmount').html('0') } $('#couponId').val(0) priceDetails(checkTotalCost); } } else { $('.coupon_error').html('Select Items') } } }) } else { $('.coupon_error').html('Select Items') } }) $('#productsPaymentForm').submit(function(event) { event.preventDefault(); $('#paymentButton').addClass('d-none'); let grandTotal = $('#grandTotal').html(); if (parseInt(grandTotal) >= 0) { let selectedServicesIds = JSON.stringify(selectedServices); let selectedServiceCarTypes = JSON.stringify(serviceCarTypes); let productIds = selectedProductsIds.map(Number); productIds = JSON.stringify(selectedProductsIds); let productQuantity = JSON.stringify(selectedProductQuantity); let userId = $('#checkUserId').val(); let couponId = $('#couponId').val(); let loyalty = $('#loyalty').html(); let referral = $('#referral').html(); $.ajax({ type: 'POST', url: baseUrl + 'admin/app/productsPayments.php', data: { productPayment: 'payment', ServicesIds: selectedServicesIds, ServiceCarTypes: selectedServiceCarTypes, productIds: productIds, productQuantity: productQuantity, userId: userId, couponId: couponId, loyalty: loyalty, referral: referral }, success: function(response) { result = JSON.parse(response) if (result.insert == 1) { $('#paymentButton').addClass('d-none'); $('#disablePayment').removeClass('d-none'); $('#invoice').removeClass('d-none'); $('.afterPayment').addClass('d-none'); $('.afterPaymentCoupon').removeClass('d-none'); $('#rcoupon').addClass('d-none'); $('#couponButton').addClass('d-none'); let url = baseUrl + 'App/userInvoicepdf.php?allotmentId=' + result.insertedId $('#downloadInvoice').attr("href", url) $('#userMobile').prop('readonly', true) toastr.success('Service Allocated') setTimeout(()=>{ window.location.href = 'edituser.php?userid='+result.encryptedUserId; },1500) } else { toastr.warning('Unable to Allot') $('#paymentButton').removeClass('d-none'); } } }) } else { $('.payment_error').html('Select Items') } }) $('#rcoupon').click(function() { let checkTotalCost = document.getElementById('totalCost'); checkTotalCost = checkTotalCost.textContent $('#coupon').val(''); $('.coupon_error').html('') $('#offAmount').html('0') $('#couponId').val(0) $('#couponDisplay').addClass('d-none'); priceDetails(checkTotalCost); }) function convertToUpperCase() { var inputField = document.getElementById("vehiclenumber"); inputField.value = inputField.value.toUpperCase(); } $(document).on("change", "#carTypeFilter", function () { let selectedCarType = $(this).val(); let serviceBox = $(this).closest(".service-box"); let serviceId = serviceBox.find(".selectbtn").attr("data-service-id"); let serviceIdElement = serviceBox.find(".selectbtn").attr("id"); let priceElement = serviceBox.find(".product-price"); let buyButton = serviceBox.find(".selectbtn"); if (selectedCarType == 0) { priceElement.text("Please select a car type"); buyButton.attr("disabled",true); return; } // Send AJAX request to get updated price $.ajax({ url: baseUrl + "admin/app/productsPayments.php", // This backend script fetches the price type: "POST", data: { carType: selectedCarType, serviceId: serviceId, action: "getServicePrice" }, dataType: "json", success: function (response) { if (response.status == "success") { buyButton.attr("disabled",false); priceElement.html("Rs." + response.amount); // Update the "Request To Buy" button with the correct amount and carType buyButton.attr("onclick", "updateSelectedServices('" + serviceIdElement + "', 1, " + selectedCarType + ")"); } else { priceElement.text("Price not available"); buyButton.attr("onclick", "updateSelectedServices(" + serviceIdElement + ", 1, " + selectedCarType + ")"); } }, error: function () { priceElement.text("Error fetching price"); } }); }); //loyaty points function calculateLoyaltyPoints() { const checkbox = document.getElementById('addLoyalPoints'); const loyaltyPoints = parseFloat(document.getElementById('loyaltyPoints').textContent); let amount = parseFloat($('#totalCost').html()); let couponId = $('#couponId').val(); let couponAmount = parseFloat($('#offAmount').html()); let referralAmount = $('#referral').html(); if (checkbox.checked) { // Calculate how much we can actually deduct (can't deduct more than total amount) let finalAmount = amount; const pointsValue = loyaltyPoints; // Assuming 1 point = 1 currency unit if(referralAmount > 0){ finalAmount = finalAmount - referralAmount; } if(couponAmount > 0) { finalAmount = finalAmount - couponAmount; } const maxDeductible = Math.min(pointsValue, finalAmount); $('#loyaltyDisplay').removeClass('d-none'); $('#loyalty').html(maxDeductible); priceDetails(amount); } else { // If unchecked, restore the original values $('#loyaltyDisplay').addClass('d-none'); $('#loyalty').html(0); priceDetails(amount); } } function calculateReferralPoints() { const checkbox = document.getElementById('addReferralPoints'); const referralPoints = parseFloat(document.getElementById('referralPoints').textContent); let amount = parseFloat($('#totalCost').html()); let couponId = $('#couponId').val(); let couponAmount = parseFloat($('#offAmount').html()); let loyaltyAmount = $('#loyalty').html(); if (checkbox.checked) { // Calculate how much we can actually deduct (can't deduct more than total amount) let finalAmount = amount; const pointsValue = referralPoints; // Assuming 1 point = 1 currency unit if(loyaltyAmount > 0){ finalAmount = finalAmount - loyaltyAmount; } if(couponAmount > 0) { finalAmount = finalAmount - couponAmount; } const maxDeductible = Math.min(pointsValue, finalAmount); $('#referralDisplay').removeClass('d-none'); $('#referral').html(maxDeductible); priceDetails(amount); } else { // If unchecked, restore the original values $('#referralDisplay').addClass('d-none'); $('#referral').html(0); priceDetails(amount); } } //price details function priceDetails(amount) { let finalAmount = parseFloat(amount); let cpnId = parseInt($('#couponId').val()); let cpnAmount = $('#offAmount').html(); let loyaltyPoints = $('#loyalty').html(); let referralPoints = $('#referral').html(); if(cpnId > 0){ $('#couponDisplay').removeClass('d-none'); finalAmount = finalAmount - cpnAmount; } if(loyaltyPoints > 0){ finalAmount = finalAmount - loyaltyPoints; } if(referralPoints > 0){ finalAmount = finalAmount - referralPoints; } //calculate tax based on amount let tax = (finalAmount * 18) / 100; let grandTotal = finalAmount + tax; $('#subTotal').html(amount); $('#tax').html(tax); $('#grandTotal').html(grandTotal); } </script>