nhance/app/Views/client_branch.php

697 lines
28 KiB
PHP

<div class="tab-pane fade" id="branch-tab">
<input type="hidden" id="client_id_for_client_branch" value="<?= isset($client['id']) ? $client['id'] : '' ?>" />
<div class="row float-right" style="padding-bottom: 10px;position: relative;right: 13px;">
<button type="button" id="btnBranchAdd" class="btn btn-primary waves-effect waves-light btn-sm"><span
class="fa fa-plus-square" aria-hidden="true" style="padding: 5px 10px;"></span>Add Branch</button>
</div>
<div class="table-responsive" id="branch_table">
<table class="table table-borderless table-nowrap mb-0">
<thead class="thead-light">
<tr>
<th>Branch Name</th>
<th>Branch Code</th>
<th>Action</th>
</tr>
</thead>
<tbody id="branch_list">
</tbody>
</table>
</div>
<div class="row" id="add_branch">
<div class="col-12">
<div class="card-body">
<div class="row float-right" style="position: relative; bottom: 20px; right: 13px;">
<button type="button" id="btnBranchBack"
class="btn btn-primary waves-effect waves-light btn-sm btnBack"><span class="fa fa-list"
aria-hidden="true" style="padding: 5px 10px;"></span>Back To List</button>
</div>
<hr>
<form role="form" class="parsley-examples" method="post" id="branch_form" enctype="multipart/form-data">
<input type="hidden" class="form-control" value="<?= csrf_hash() ?>" name="<?= csrf_token() ?>" />
<input type="hidden" name="client_id" id="client_id_branch"
value="<?= isset($client['id']) ? $client['id'] : '' ?>" />
<input type="hidden" name="branch_id_primarykey" id="branch_id_primarykey" />
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label for="branch_name">Branch Name<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_name" placeholder="Enter Branch Name"
name="branch_name" required>
</div>
<div class="form-group col-md-6">
<label for="branch_code">Branch Code<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_code" placeholder="Enter Branch Code"
name="branch_code" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="address1">Address Line 1<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="address1" name="address1" placeholder="Enter Address 1" required>
</div>
<div class="form-group col-md-6">
<label for="address2">Address Line 2<span class="text-danger"></span></label>
<input type="text" class="form-control" id="address2" placeholder="Enter Address 2" name="address2">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-3">
<label for="state">State<span class="text-danger">*</span></label>
<select class="form-control" id="state" name="state" required>
<option value="">Select State</option>
<?php foreach($state as $value) { ?>
<option value="<?= $value['id'] ?>"><?= $value['state'] ?></option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-3">
<label for="district">District<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="district" placeholder="Enter District"
name="district" required>
</div>
<div class="form-group col-md-3">
<label for="city">City<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_city" placeholder="Enter City"
name="city" required>
</div>
<div class="form-group col-md-3">
<label for="gst">Pincode<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="pincode" placeholder="Enter Pincode" name="pincode" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="gst">GST<span class="text-danger">*</span></label>
<input type="text" name="gst" class="form-control" id="gst" placeholder="Enter GST Number" data-parsley-error-message="Invalid GST Number. Example: 12ABCDE1234F5Z6" data-parsley-trigger="change" data-parsley-pattern="^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}$" required>
</div>
<div class="form-group col-md-4">
<label class="switch" style="position: relative;top: 43px;left: 20px;">
<input id="sez" type="checkbox" name="sez">
<span class="slider round" style="height: 27px;"></span>
</label>
<label for="inception_type" style="position: relative;bottom: 5px;left: 85px;">Enable SEZ</label>
</div>
</div>
<hr>
<h6 class="header-title">Contact 1</h6>
<br>
<div class="form-row">
<div class="form-group col-md-6">
<label for="first_name">Name<span class="text-danger">*</span></label>
<input value="" type="text" class="form-control" placeholder="Enter Contact Name"
name="name[]" id="name" required>
</div>
<div class="form-group col-md-6">
<label for="designation">Designation<span class="text-danger">*</span></label>
<input value="" type="text" class="form-control" placeholder="Enter Designation Name"
name="designation[]" id="designation" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="last_name">Email<span class="text-danger">*</span></label>
<input value="" type="text" class="form-control" placeholder="Enter Contact Email"
name="email[]" id="email" data-parsley-trigger="change" data-parsley-type="email" required>
</div>
<div class="form-group col-md-6">
<label for="mobile">Mobile<span class="text-danger">*</span></label>
<input value="" type="text" class="form-control" placeholder="Enter Contact Mobile"
name="mobile[]" id="mobile" onchange="checkMobileNumber(this)"
onkeypress="return onlyNumbers(event)" maxlength="10" minlength="10"
data-parsley-type-message="Please enter a valid 10-digit mobile number."
data-parsley-required-message="Please enter a valid 10-digit mobile number."
required>
</div>
</div>
<div class="form-group" style="display: flex;">
<button style="margin-right: 10px;" type="button" class="btn btn-primary btn-sm ac"
onclick="appendContactHtml()" id="add">Add Contact</button>
<button type="button" class="btn btn-danger btn-sm" onclick="removeContact(this)"
id="remove_btn">Remove</button>
</div>
<div id="container"></div>
</div>
<div class="form-group text-right m-b-0">
<button type="submit" class="btn btn-primary waves-effect waves-light mr-1"
id="btnSubmit">Submit</button>
<button type="button" class="btn btn-secondary waves-effect btnBack"
id="btnBack">Cancel</button>
</div>
</form>
</div>
</div>
</div> <!-- end col-->
</div>
<!-- end -->
<script>
var branch_form_action = '';
var contactCount = 1;
var branch_PrimaryKey = $('#client_id_for_client_branch').val();
$(document).ready(function() {
branch_PrimaryKey = $('#client_id_branch').val();
$('#add_branch').hide();
$('.btnBack').hide();
if (branch_PrimaryKey !== '') {
var branchTable = '';
var data = <?= isset($client_branch) ? json_encode($client_branch) : '[]' ?>;
var role = data.role
delete data.role;
console.log(role)
$.each(data, function(index, item) {
if(role != 3 && role != 4){
branchTable += `
<tr>
<td>${item.branch_name}</td>
<td>${item.branch_code}</td>
<td>
<a class="mdi mdi-lead-pencil btnBranchEdit" href="#" data-id="${item.id}" style="font-size:18px;"></a>
<a style="color:#02a8b5" data-id="${item.id}" onclick="removeClientBranch(this)"><i data-id="${item.id}" class="mdi mdi-delete" style="font-size:18px;"></i></a>
</td>
</tr>
`;
}else{
branchTable += `
<tr>
<td>${item.branch_name}</td>
<td>${item.branch_code}</td>
<td>
<a class="mdi mdi-lead-pencil btnBranchEdit" href="#" data-id="${item.id}" style="font-size:18px;"></a>
</td>
</tr>
`;
}
});
$('#branch_list').append(branchTable);
}
});
$('#btnBranchAdd').click(function() {
branch_form_action = '<?= base_url("client/branch/create"); ?>';
$('#add_branch').show();
$('#branch_table').hide();
$('.btnBack').show();
$('#btnBranchAdd').hide();
$('#branch_name').val('');
$('#branch_code').val('');
$('#state').val('');
$('#district').val('');
$('#branch_city').val('');
$('#branch_form')[0].reset();
$('.ac').css('display', 'block');
contactCount = 1
var addButton = document.getElementById('add');
if (addButton.style.display === 'none') {
addButton.style.display = 'block';
}
var buttonIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
for (var i = 0; i < buttonIds.length; i++) {
var firstElement = buttonIds[i].split('_')[0];
console.log(firstElement);
// Find the element by its ID
var elementToRemove = document.getElementById(firstElement);
console.log(elementToRemove);
if (elementToRemove) {
console.log(elementToRemove);
elementToRemove.parentNode.removeChild(elementToRemove);
}
localStorage.removeItem('buttonIds')
}
})
$('.btnBack').click(function() {
$('#add_branch').hide();
$('#branch_table').show();
$('.btnBack').hide();
$('#btnBranchAdd').show();
$('#branch_name').val('');
$('#branch_code').val('');
$('#state').val('');
$('#district').val('');
$('#branch_city').val('');
$('#branch_form')[0].reset();
$('.ac').css('display', 'block');
$('.parsley-errors-list').remove();
$('#branch_form').parsley().reset();
contactCount = 1
var buttonIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
for (var i = 0; i < buttonIds.length; i++) {
var firstElement = buttonIds[i].split('_')[0];
console.log(firstElement);
// Find the element by its ID
var elementToRemove = document.getElementById(firstElement);
console.log(elementToRemove);
if (elementToRemove) {
console.log(elementToRemove);
elementToRemove.parentNode.removeChild(elementToRemove);
}
}
})
$("#branch_form").submit(function(event) {
event.preventDefault();
branch_PrimaryKey = $('#client_id_branch').val();
console.log('branch_PrimaryKey', branch_PrimaryKey)
if (branch_PrimaryKey === '') {
toastr.error('Client is required', 'Error');
$('#insurer').val('');
$('#tpa').val('');
$('#policy').html('<option value="" selected>Select Policy</option>');
return;
}
var isValid = $('#branch_form').parsley().validate();
if (isValid) {
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
var formData = new FormData($('#branch_form')[0]);
$.ajax({
data: formData,
url: branch_form_action,
type: "POST",
dataType: 'json',
processData: false,
contentType: false,
success: function(res) {
if (res) {
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
$('#add_branch').hide();
$('#branch_table').show();
$('.btnBack').hide();
$('#btnBranchAdd').show();
var message = (branch_PrimaryKey === '') ?
'Client Branch Created successfully' :
'Client Branch Updated successfully';
toastr.success(message, 'Success');
}, 1000);
}
$('#branch_list tr').remove();
var branchTableInsert = ''
var role = res.data.role
delete res.data.role;
console.log(role)
$.each(res.data, function(index, item) {
console.log('client_branch_data', item)
if(role != 3 && role != 4){
branchTableInsert += `
<tr>
<td>${item.branch_name}</td>
<td>${item.branch_code}</td>
<td>
<a class="mdi mdi-lead-pencil btnBranchEdit" href="#" data-id="${item.id}" style="font-size:18px;"></a>
<a style="color:#02a8b5" data-id="${item.id}" onclick="removeClientBranch(this)"><i data-id="${item.id}" class="mdi mdi-delete" style="font-size:18px;"></i></a>
</td>
</tr>
`;
}else{
branchTableInsert += `
<tr>
<td>${item.branch_name}</td>
<td>${item.branch_code}</td>
<td>
<a class="mdi mdi-lead-pencil btnBranchEdit" href="#" data-id="${item.id}" style="font-size:18px;"></a>
</td>
</tr>
`;
}
});
$('#branch_list').append(branchTableInsert);
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
toastr.warning('Something Wrong!', 'warning');
}, 1000);
}
});
}
contactCount = 1
});
$('body').on('click', '.btnBranchEdit', function() {
var buttonIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
for (var i = 0; i < buttonIds.length; i++) {
var firstElement = buttonIds[i].split('_')[0];
console.log(firstElement);
// Find the element by its ID
var elementToRemove = document.getElementById(firstElement);
console.log(elementToRemove);
if (elementToRemove) {
console.log(elementToRemove);
elementToRemove.parentNode.removeChild(elementToRemove);
}
localStorage.removeItem('buttonIds')
}
contactCount = 1
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
var branch_id = $(this).attr('data-id');
$('#branch_id_primarykey').val(branch_id);
branch_form_action = '<?= base_url("client/branch/edit"); ?>';
$.ajax({
url: '<?php echo base_url('client/branch/list/');?>' + branch_id,
type: "GET",
dataType: 'json',
success: function(res) {
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}, 500);
// console.log('branch contact : ',res.contact)
$('#add_branch').show();
$('#branch_table').hide();
$('.btnBack').show();
$('#btnBranchAdd').hide();
$('#branch_name').val(res.data.branch_name);
$('#branch_code').val(res.data.branch_code);
$('#pincode').val(res.data.pincode);
$('#address1').val(res.data.address1);
$('#address2').val(res.data.address2);
$('#gst').val(res.data.gst);
$('#state option[value="' + res.data.state + '"]').prop('selected', true);
$('#district').val(res.data.district);
$('#branch_city').val(res.data.city);
$('#branch_PrimaryKey').val(res.data.id);
if(res.data.sez == 1){
$('#sez').prop('checked', true);
}else{
$('#sez').prop('checked', false);
}
$('#name').val(res.contact[0].name);
$('#email').val(res.contact[0].email);
$('#mobile').val(res.contact[0].mobile);
$('#designation').val(res.contact[0].designation);
res.contact.shift();
// console.log(res.contact.length)
for (let index = 0; index < res.contact.length; index++) {
const contact = res.contact[index];
if (contact !== undefined) {
appendContactHtml(contact);
}
}
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
toastr.warning('Something Wrong!', 'warning');
}, 1000);
}
});
console.log(branch_form_action);
});
$("#remove_btn").click(function() {
$("#name").val('');
$("#email").val('');
$("#mobile").val('');
$("#designation").val('');
})
// Initialize the contact count
function appendContactHtml(contact = false, reset = false) {
console.log('appendContactHtml function called ')
contactCount++;
var container = document.getElementById('container');
var uniqueId = Date.now().toString();
var html = `
<div id="${uniqueId}">
<hr>
<h6 class="header-title">Contact ${contactCount}</h6>
<br>
<div class="form-row">
<div class="form-group col-md-6">
<label for="${uniqueId}_first_name">Name<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.name : ''}" type="text" class="form-control" placeholder="Enter Contact Name" name="name[]" id="${uniqueId}_name" required>
</div>
<div class="form-group col-md-6">
<label for="${uniqueId}_designation">Designation<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.designation : ''}" type="text" class="form-control" placeholder="Enter Designation Name" name="designation[]" id="${uniqueId}_designation" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="${uniqueId}_last_name">Email<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.email : ''}" type="text" class="form-control" placeholder="Enter Contact Email" name="email[]" id="${uniqueId}_email" data-parsley-trigger="change" data-parsley-type="email" required>
</div>
<div class="form-group col-md-6">
<label for="${uniqueId}_mobile">Mobile<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.mobile : ''}" type="text" class="form-control" placeholder="Enter Contact Mobile" name="mobile[]" id="${uniqueId}_mobile" onchange="checkMobileNumber(this)" onkeypress = "return onlyNumbers(event)" maxlength="10" min="10" data-parsley-type-message="Please enter a valid 10-digit mobile number." data-parsley-required-message="Please enter a valid 10-digit mobile number." required>
</div>
</div>
<div class="form-group" style="display: flex;">
<button style="margin-right: 10px;" type="button" class="btn btn-primary btn-sm ac" onclick="appendContactHtml()" id="${uniqueId}_add">Add Contact</button>
<button type="button" class="btn btn-danger btn-sm" onclick="removeContact(this)" id="${uniqueId}_remove">Remove</button>
</div>
</div>
`;
if (reset == false) {
console.log(reset)
container.insertAdjacentHTML('beforeend', html);
storeButtonId(uniqueId + '_add');
if (contactCount >= 3) {
hideStoredAddButtons();
}
}
}
function removeContact(button) {
var uniqueId = button.id.split("_")[0];
var contactSection = document.getElementById(uniqueId);
if (contactSection) {
contactSection.parentNode.removeChild(contactSection);
contactCount--;
if (contactCount < 3) {
var addButton = document.querySelector('.ac');
if (addButton) {
addButton.style.display = 'block';
}
}
}
}
function storeButtonId(id) {
var buttonIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
buttonIds.push(id);
localStorage.setItem('buttonIds', JSON.stringify(buttonIds));
}
function hideStoredAddButtons() {
var storedIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
storedIds.forEach(function(id) {
var addButton = document.getElementById(id);
var first_addButton = document.querySelector('.ac')
if (addButton) {
addButton.style.display = 'none';
first_addButton.style.display = 'none';
}
});
}
function showNextAddButton() {
var storedIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
var addButtonShown = false;
// Iterate through the stored IDs to find the next "Add" button to show
storedIds.forEach(function(id) {
if (!addButtonShown) {
var addButton = document.getElementById(id);
if (addButton && addButton.style.display === 'none') {
addButton.style.display = 'block';
addButtonShown = true; // Set to true once an "Add" button is shown
}
}
});
}
function validateForm() {
var isValid = true;
$('#branch_form input, #branch_form select').each(function() {
if ($(this).is('input[type="text"]') || $(this).is('select')) {
if ($.trim($(this).val()) == '') {
isValid = false;
return false;
}
}
});
return isValid;
}
function onlyNumbers(event) {
var charcode;
charcode = event.which || event.keyCode;
if (charcode >= 48 && charcode <= 57) return true;
return false;
}
function removeClientBranch(element) {
Swal.fire({
title: "Are you sure?",
text: "You need to remove this client branch.",
icon: "info",
showCancelButton: true,
confirmButtonColor: "#3085d6",
confirmButtonText: "Yes",
}).then((result) => {
if (result.isConfirmed) {
var id = element.getAttribute('data-id');
var form_action = '<?= base_url("client/branch/remove/") ?>' + id;
$.ajax({
url: form_action,
type: "GET",
dataType: 'json',
processData: false,
contentType: false,
success: function(res) {
// console.log(res.status == true);
if (res) {
if (res.status == true) {
toastr.success('Client branch removed successfully', 'success');
location.reload();
} else {
toastr.warning('Failed to remove client branch', 'warning');
}
}
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
console.log('Something Wrong!', 'warning');
}, 1000);
}
});
}
});
}
function checkMobileNumber(input) {
console.log('function called')
console.log(input);
console.log('Input Value', input.value);
var mobileNumber = input.value;
var url = '<?php echo base_url('util/checkHRNumber/') ?>' + mobileNumber;
$.get(url, function(response) {
console.log(response)
console.log(response.data)
if (response.data > 0) {
toastr.warning('The Mobile Number Already Exist.', 'Warning');
input.value = "";
return;
}
}).fail(function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
});
}
</script>