| Current Path : /var/www/html/tax/public/http/ |
| Current File : /var/www/html/tax/public/http/business.js |
document.write('<script type="text/javascript" src="http/config.js" ></script>');
$(document).ready(()=>{
var businesses = [];
function validate(){
var validateFlag = true;
if($('#bs_type').val() == -1){
$('#bs_type').css('border','1px solid red');
validateFlag = false;
}
if($('#bs_type').val() == -1){
$('#bs_sponsor').css('border','1px solid red');
$('#bs_type').css('border','1px solid red');
validateFlag = false;
}else if($('#bs_type').val() == "2" && $('#bs_sponsor').val() != -1){
$('#bs_sponsor').css('border','1px solid red');
validateFlag = false;
}
$('#bs_type').change(()=>{
$('#bs_type').css('border','');
});
$('#bs_sponsor').change(()=>{
$('#bs_sponsor').css('border','');
$('#bs_sponsor_id').css('border','');
});
return validateFlag;
}
var url = getConfig("baseUrl")+"resource=business&action=getBusinessType";
$.post( url, function( data ) {
var res = JSON.parse(data);
var options = "";
options += "<option value='-1'>Business Type</option>";
// console.log(res.result);
if(res.rowsAffected > 0){
res.result.forEach((r)=>{
// console.log(r);
options += "<option value='"+r.idBusinessType+"'>"+r.type+"</option>";
});
}
$('#bs_type').append(options);
});
$("#bs_type").change(()=>{
getBusinesses();
if($("#bs_type").val() == 2){
$('#bs_sponsor').attr('disabled', true);
}else{
$('#bs_sponsor').attr('disabled', false);
}
});
function getBusinesses(){
var deferred = new $.Deferred();
var url = getConfig("baseUrl")+"resource=business&action=getBusiness";
$.post( url, function( data ) {
var res = JSON.parse(data);
var options = "";
var rows = "";
// console.log(res.result);
options += "<option value='-1'>Select Sponsor</option>";
if(res.rowsAffected > 0){
res.result.forEach((r)=>{
businesses.push(r);
if(r.idBusinessType == $("#bs_type").val() || $("#bs_type").val() == "-1"){
options += "<option value='"+r.idBusiness+"'>"+r.name+"</option>";
}
rows += "<tr id='row_"+ r.idBusiness +"'>";
rows += "<td>"+ r.idBusiness +"</td>";
rows += "<td>"+ r.name +"</td>";
rows += "<td>"+ r.label +"</td>";
rows += "<td>"+ r.address +"</td>";
rows += "<td>"+ r.businessType +"</td>";
rows += "<td>"+ r.sponsorBusinessName +"</td>";
rows += "<td class='businessAction' id='"+ r.idBusiness +"'>";
rows += "<label class='edit'><img src='asset/images/edit.png' alt='Edit' width='18' title='Click to edit'></label>";
rows += " ";
rows += "<label class='delete'><img src='asset/images/delete.png' alt='Delete' width='18' title='Click to delete'></label>";
rows += "</td>";
rows += "<tr>";
});
}
$('#bs_sponsor').html(options);
$('#businessdata').html(rows);
deferred.resolve(true);
});
return deferred.promise();
}
getBusinesses();
$( "#businessForm" ).submit(function( event ) {
var update = false;
var msg = "";
if(validate()){
var url = "";
if($("#bs_id").val() == "-1"){
url = getConfig("baseUrl")+"resource=business&action=createBusiness";
}else if(parseInt($("#bs_id").val()) > 0){
url = getConfig("baseUrl")+"resource=business&action=updateBusiness";
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 = "Business has been updated successfully";
}else{
msg = "Business has been created successfully";
}
$.toast({
heading: 'Success',
text: msg,
showHideTransition: 'slide',
icon: 'success',
hideAfter: getConfig("toastHideAfter")
})
getBusinesses();
$('#businessForm').trigger("reset");
}else if(json.status == "info"){
$.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")
})
}
});
$("#viewBusinesses").click(()=>{
getBusinesses();
$("#businessesList").fadeIn();
$(".container").hide();
});
$("#back").click(()=>{
$("#businessesList").hide();
$(".container").fadeIn();
});
$("#businessesTable").on("click", "label", function(){
// console.log($(this).parent().attr('id'));
// console.log($(this).attr('class'));
if($(this).attr('class') == "edit"){
if (confirm('Are you sure you want to make changes?')) {
getBusinesses().then(()=>{
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 {
// alert('Why did you press cancel? You should have confirmed');
$.toast({
heading: 'Error',
text: "Please always think before you modify anything in system",
showHideTransition: 'fade',
icon: 'error',
hideAfter: getConfig("toastHideAfter")
})
}
}else if($(this).attr('class') == "delete"){
if (confirm('Are you sure you want to 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();
}else{
$.toast({
heading: 'Error',
text: "Business can't be deleted, transactions have been generated against it",
showHideTransition: 'fade',
icon: 'error',
hideAfter: getConfig("toastHideAfter")
})
}
});
} else {
// alert('Why did you press cancel? You should have confirmed');
$.toast({
heading: 'Error',
text: "Please always think before you click delete",
showHideTransition: 'fade',
icon: 'error',
hideAfter: getConfig("toastHideAfter")
})
}
}
})
});