Your IP : 216.73.216.91


Current Path : /var/www/html/tax/public/http/
Upload File :
Current File : /var/www/html/tax/public/http/payment.js

document.write('<script type="text/javascript" src="http/config.js" ></script>');
$(document).ready(()=>{
	var businesses = [];
	var books = [];
	var paymentMethods = [];
	var banks = [];
	var payments = [];
	function validate(){
		var validateFlag = true;
		if($('#pay_book_id').val() == -1){
            $('#pay_book_id').css('border','1px solid red');
            validateFlag = false;
		}
		if($('#pay_business_id').val() == -1){
            $('#pay_business_id').css('border','1px solid red');
            validateFlag = false;
		}
		if($('#pay_method_id').val() == -1){
            $('#pay_method_id').css('border','1px solid red');
            validateFlag = false;
		}
		
		if($('#pay_bank_id').val() == -1){
            $('#pay_bank_id').css('border','1px solid red');
            validateFlag = false;
		}
		return validateFlag;
	}
	$('#pay_book_id').change(()=>{
		$('#pay_book_id').css('border','');
	});
	$('#pay_business_id').change(()=>{
		$('#pay_business_id').css('border','');
	});
	$('#pay_method_id').change(()=>{
		$('#pay_method_id').css('border','');
	});
	$('#pay_bank_id').change(()=>{
		$('#pay_bank_id').css('border','');
	});

	function getBooks(){
		var paymentBookTypes = getConfig("paymentBookTypes");
		var url = getConfig("baseUrl")+"resource=book&action=getBooks&source=payment";
		$.post( url, function( data ) {
			var options = "";
			// console.log(res.result);
			options += "<option value='-1'>Select Book</option>";
			var res = JSON.parse(data);
			if(res.rowsAffected > 0){
				res.result.forEach((r)=>{
					books.push(r);
					if(paymentBookTypes.indexOf(r.idBookType) > -1){
						options += "<option value='"+r.idBook+"'>"+r.name+"</option>";
					}
				});
			}
			$('#pay_book_id').html(options);
		});
	}
	getBooks();

	function getBusinesses(){
		var url = getConfig("baseUrl")+"resource=business&action=getBusiness";
		$.post( url, function( data ) {
			var res = JSON.parse(data);
			var options = "";
			// console.log(res.result);
			options += "<option value='-1'>Select Business</option>";
			if(res.rowsAffected > 0){
				res.result.forEach((r)=>{
					businesses.push(r);
					options += "<option value='"+r.idBusiness+"'>"+r.name+"</option>";
				});
			}
			$('#pay_business_id').html(options);
		});
	}
	getBusinesses();

	function getBanks(){
		var url = getConfig("baseUrl")+"resource=payment&action=getBanks";
		$.post( url, function( data ) {
			var res = JSON.parse(data);
			var options = "";
			// console.log(res.result);
			options += "<option value='-1'>Select Bank</option>";
			if(res.rowsAffected > 0){
				res.result.forEach((r)=>{
					banks.push(r);
					options += "<option value='"+r.idBank+"'>"+r.name+"</option>";
				});
			}
			$('#pay_bank_id').html(options);
		});
	}
	getBanks();

	function getPaymentMethods(){
		var url = getConfig("baseUrl")+"resource=payment&action=getPaymentMethods";
		$.post( url, function( data ) {
			var res = JSON.parse(data);
			var options = "";
			// console.log(res.result);
			options += "<option value='-1'>Select Payment Method</option>";
			if(res.rowsAffected > 0){
				res.result.forEach((r)=>{
					paymentMethods.push(r);
					options += "<option value='"+r.idPaymentMethod+"'>"+r.name+"</option>";
				});
			}
			$('#pay_method_id').html(options);
		});
	}
	getPaymentMethods();

	function getPayments(){
		var url = getConfig("baseUrl")+"resource=payment&action=getPayments";
		$.post( url, function( data ) {
			var res = JSON.parse(data);
			var table = [];
			var totalAmount = 0;
			if(res.rowsAffected > 0){
				res.result.forEach((r)=>{
					payments.push(r);
					var tempTable = [];
					tempTable.push(r.idPayment);
					tempTable.push(r.date);
					tempTable.push(r.receiverName);
					tempTable.push(r.billRef);
					tempTable.push(r.businessName);
					tempTable.push(r.bookName);
					tempTable.push(r.serial);
					tempTable.push(r.amount);
					tempTable.push(totalAmount += parseInt(r.amount));
					tempTable.push(r.balance);
					tempTable.push(r.paymentMethod);
					tempTable.push(r.paymentRef);
					tempTable.push(r.payeeBankName);
					tempTable.push(r.remarks);
					tempTable.push(r.town);
					tempTable.push(r.status);				
					table.push(tempTable);
				});
			}	
			var myTable = $('#paymentTable').DataTable( {
				bDestroy: true,
		        data: table,
		        columns: [
		            { title: "ID" },
		            { title: "Date" },
		            { title: "Receiver" },
		            { title: "Bill Ref" },
		            { title: "Business" },
		            { title: "Book" },
		            { title: "Serial" },
		            { title: "Amount" },
		            { title: "Total Amount" },
		            { title: "Balance" },
		            { title: "Payment Method" },
		            { title: "Payment Ref" },
		            { title: "Payee Bank" },
		            { title: "Remarks" },
		            { title: "Town" },
		            { title: "Status" }
		        ]
		    } );

		    $('#paymentTable tfoot th').each( function () {
		        var title = $(this).text();
		        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
		    } );
		    // Apply the search
		    myTable.columns().every( function () {
		        var that = this;
		 
		        $( 'input', this.footer() ).on( 'keyup change clear', function () {
		            if ( that.search() !== this.value ) {
		                that
		                    .search( this.value )
		                    .draw();
		            }
		        } );
		    } );
			// $('#paymentdata').html(rows);
			// $(".container").hide();
			// $("#paymentList").fadeIn();
		});
	}
	getPayments();

	

	$( "#paymentForm" ).submit(function( event ) {
		var update = false;
		var msg = "";
		if(validate()){
			var url = "";
			if($("#pay_id").val() == "-1"){
				url = getConfig("baseUrl")+"resource=payment&action=createPayment";
			}else if(parseInt($("#bs_id").val()) > 0){
				url = getConfig("baseUrl")+"resource=payment&action=updatePayment";
				update = true;
			}
			var formData = $( this ).serialize();
			// console.log(formData);
			$.post( url, formData, (res)=>{
				$("#submit").attr("disabled", true);
				// console.log(res);
				json = JSON.parse(res);
				if(json.status == "success"){
					if(update){
						msg = "Payment has been updated successfully";
					}else{
						msg = "Payment has been created successfully";
					}
					$.toast({
					    heading: 'Success',
					    text: msg,
					    showHideTransition: 'slide',
					    icon: 'success',
					    hideAfter: getConfig("toastHideAfter")
					})
					// getPayments();
					$('#paymentForm').trigger("reset");
				}else if(json.status == "info" && json.code == "book_closed"){
					$.toast({
					    heading: 'Information',
					    text: json.msg,
					    showHideTransition: 'slide',
					    icon: 'info',
					    hideAfter: getConfig("toastHideAfter")
					})
				}else{
					$.toast({
					    heading: 'Error',
					    text: 'Some thing went wrong',
					    showHideTransition: 'fade',
					    icon: 'error',
					    hideAfter: getConfig("toastHideAfter")
					})
				}
			});
			setTimeout(()=>{
				$("#submit").attr("disabled", false);
			},2000);
			event.preventDefault();
		}else{
			$.toast({
			    heading: 'Missing Information',
			    text: 'Please fill out all the mendatory fields',
			    showHideTransition: 'fade',
			    icon: 'error',
				hideAfter: getConfig("toastHideAfter")
			})
		}
	});
	$("#viewPayments").click(()=>{
		getPayments();
		$("#paymentList").fadeIn();
		$(".container").hide();
	});
	$("#back").click(()=>{
		$("#paymentList").hide();
		$(".container").fadeIn();
	});
	$("#businessesTable").on("click", "label", function(){
		// console.log($(this).parent().attr('id'));
		// console.log($(this).attr('class'));
		getBusinesses();
		if($(this).attr('class') == "edit"){
			var businessId = $(this).parent().attr('id');
			var myBusiness;
			businesses.forEach((b)=>{
				if(b.idBusiness == businessId){
					myBusiness = b;
					return;
				}
			});
			var hasParent = (myBusiness.parentIdBusiness == -1) ? 0 : 1;
			$("#bs_id").val(myBusiness.idBusiness);
			$("#bs_name").val(myBusiness.name);
			$("#bs_label").val(myBusiness.label);
			$("#bs_address").val(myBusiness.address);
			$("#bs_type").val(myBusiness.idBusinessType);
			$("#bs_sponsor").val(myBusiness.sponsorIdBusiness);
			$("#businessesList").hide();
			$(".container").fadeIn();
			// console.log(myBusiness);
		}else if($(this).attr('class') == "delete"){
			var businessId = $(this).parent().attr('id');
			var url = getConfig("baseUrl")+"resource=business&action=deleteBusiness";
			// console.log(formData);
			$.post( url, {bs_id: businessId}, (res)=>{
				json = JSON.parse(res);
				if(json.status == "success" && json.rowsAffected == 1){
					$.toast({
					    heading: 'Success',
					    text: 'Business deleted successfully',
					    showHideTransition: 'slide',
					    icon: 'success',
					    hideAfter: getConfig("toastHideAfter")
					});
					
				}
				$(this).parents().eq(1).remove();
			});

		}
	})
});