nhance/app/Views/employee_upload.php
2024-03-04 09:24:49 +05:30

397 lines
17 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php //echo fancy_date_time_format('2024-01-01 15:40:32');echo '<br>';?>
<?php //echo fancy_date_time_format('2024-02-01 15:40:32');echo '<br>';?>
<!-- <?php echo fancy_date_time_format('2024-02-10 15:40:32');echo '<br>';?> -->
<!-- <?php echo fancy_date_time_format('2024-02-13 14:40:32');echo '<br>';?> -->
<!-- <?php echo fancy_date_time_format('2024-02-13 14:58:00');echo '<br>';?> -->
<br>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<!-- <div class="text-center"> -->
<form id="emp-upload-form" action="<?php echo base_url().'employee/upload'?>" method="post">
<input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" id="csrf_token">
<div class="row">
<div class="col-md-4 col-xl-3">
<div class="form-group mb-3">
<label>Client</label> <br/>
<select name="client_id" class="form-control" id="client_id" required>
<option value="">Select</option>
</select>
</div>
</div>
<div class="col-md-4 col-xl-3">
<div class="form-group mb-3">
<label>Policy</label> <br/>
<select name="policy_id" class="form-control" id="policy_id">
<option value="">Select</option>
</select>
</div>
</div>
<div class="col-md-4 col-xl-3">
<div class="form-group mb-3">
<label>Action</label> <br/>
<select name="upload-action-type" class="form-control" id="upload-action-type" required>
<option value="">Select</option>
<?php
if(isset($actions) && count($actions))
{
foreach($actions as $key => $action)
{
echo "<option value=".$key.">".$action."</option>";
}
}
?>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xl-3">
<div class="form-group mb-3">
<label>Choose file</label> <br/>
<input type="file" name="emplist" id="emplist" accept=" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel,application/vnd.oasis.opendocument.spreadsheet" required>
</div></div>
<div class="col-8" style="text-align: right;">
<button id="emp_form_submit_button" type="submit" class="btn btn-primary waves-effect waves-light">Submit</button>
</div>
</div>
</form>
<!-- </div> -->
</div>
</div>
</div>
</div>
</div>
<!-- start page title -->
<div class="row">
<div class="col-12">
<div class="page-title-box page-title-box-alt">
<h4 class="page-title"></h4>
<div class="page-title-right">
</div>
</div>
</div>
</div>
<!-- end page title -->
<div class="row" id="file_list">
<?php include('file_list.php');?>
</div>
<!-- end row -->
<!-- Center modal content -->
<div class="modal fade" id="file-err-modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myCenterModalLabel">File Rejected Reason</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
</div>
</div>
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- <div id="loader" class="loader" style="display:none;">SPINNER</div> -->
<script>
// Declare a global variable to store API response data
var clientPolicies = [];
$( document ).ready(function() {
console.log( "document loaded" );
//fetchClientPolicies();
//for modal pop up
$('#file-err-modal').on('show.bs.modal', function (event) {
// console.log(event.relatedTarget);
var myVal = $(event.relatedTarget).data('err');
console.log(myVal);
var loader = '<div style="text-align: center;"><img src="<?php echo base_url()?>public/assets/images/simple_loader.gif" height="50px" width="50px" ></div>';
$('#file-err-modal').find(".modal-body").html(loader);
var model_data = fetchFileError(myVal);
// console.log('data received.');
// console.log(model_data);
});
//for form submit
$("#emp-upload-form").submit(function(event) {
event.preventDefault(); // Prevent default form submission
console.log('submit called');
var action_item = $('#upload-action-type').val();
var policy_id = $('#policy_id').val();
console.log('action_item - '+ action_item);
if(action_item != 'correction' && policy_id == "")
{
// $('#policy_id').attr('required', true);
// $('#policy_id').prop('title', 'plz choose policy');
// $('#policy_id').mouseover();
console.log('required');
alert('please choose policy for this uploaing event ');
return false;
}
else{
$('#policy_id').attr('required', false);
console.log('not required');
}
// Create FormData object
var formData = new FormData($(this)[0]);
for (var pair of formData.entries()) {
console.log(pair[0]+ ', ' + pair[1]); }
console.log(formData);
$('#emp_form_submit_button').prop('disabled',true);
$('#emp_form_submit_button').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Loading...');
// return false;
$.ajax({
url: $(this).attr("action"),
type: "POST",
data: formData,
processData: false, // Prevent jQuery from automatically processing the data
contentType: false, // Let jQuery handle the content type
headers: {
// "Content-Type":"multipart/form-data",
"X-Requested-With": "XMLHttpRequest"
},
success: function(response) {
// Request successful, handle response
$('#policy_id').attr('required', false);
console.log(response);
if (response.code === 200 && response.dataStatus === true && response.data !== "") {
toastr.success('File upload successs, Data validation is in-progress', 'success');
window.location.reload(true);
} else if(response.code === 404 && response.dataStatus === false){
console.error('no data found', response);
// alert(response.message);
toastr.error(response.message, 'Failed');
window.location.reload(true);
}
else
{
console.error('Something went wrong!');
// alert('Something went wrong! Try later');
toastr.error('Something went wrong! Try later', 'Error');
window.location.reload(true);
}
},
error: function(xhr, status, error) {
// Request failed, handle error
console.error("Request failed:", status, error);
toastr.error('Something went wrong! Try later', 'Error');
window.location.reload(true);
}
});
});//end
});
$( window ).on( "load", function() {
console.log( "window loaded" );
fetchClientPolicies();
});
function fetchClientPolicies() {
$('#loader').show();
var apiURL = '<?php echo base_url();?>' + 'util/clients-with-policies';
console.log('fetchClientPolicies');
console.log(apiURL);
$.ajax({
url: apiURL,
method: 'GET',
headers: {
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest"
},
success: function(response) {
// console.log(response.code);
// console.log(response.dataStatus);
// console.log(response.data);
if (response.code === 200 && response.dataStatus === true && response.data !== "") {
try {
clientPolicies = (response.data);
// console.log(clientPolicies);
appendClients(clientPolicies);
} catch (error)
{
console.error('Error parsing API response data:', error);
}
} else if(response.code === 404 && response.dataStatus === false){
console.error('no data found', response);
}
else
{
console.error('Something went wrong!');
}
},
error: function(xhr, status, error) {
console.error('Error fetching data from API:', error);
}
});
$('#loader').hide();
}
function fetchFileError(file_id) {
$('#loader').show();
var apiURL = '<?php echo base_url();?>' + 'util/get-file-error/' + file_id;
console.log('fetchFileError');
console.log(apiURL);
$.ajax({
url: apiURL,
method: 'GET',
headers: {
"X-Requested-With": "XMLHttpRequest"
},
success: function(response) {
// console.log(response.code);
// console.log(response.dataStatus);
// console.log(response.data);
$(this).find(".modal-body").html("");
if (response.code === 200 && response.dataStatus === true && response.data !== "") {
try {
console.log((JSON.parse(response.data)));
var file_error_data = (JSON.parse(response.data));
var file_error_html = "";
for (var key in file_error_data['error_summary']) {
console.log(key);
var err_id = key;
var err_count = file_error_data['error_summary'][key];
// for (var ckey in col)
// {
file_error_html += (err_id == 1 ? "<strong>Mandatory values missing</strong> <span class='badge badge-danger float-right'>"+err_count+"</span>" : (err_id == 2 ? "<strong>Values not in expected format</strong> <span class='badge badge-danger float-right'>"+err_count+"</span>" : (err_id == 3 ? "<strong>Field contains not allowed values</strong> <span class='badge badge-danger float-right'>"+err_count+"</span>" : (err_id == 4 ? "<strong>Rule Conflict</strong> <span class='badge badge-danger float-right'>"+err_count+"</span>" : (err_id == 5 ? "<strong>"+file_error_data['error_data']+"</strong>" : (err_id == 6 ? "<strong>"+file_error_data['error_data']+"</strong>" : ""))))));
file_error_html += '<br/>';
// }
}
if(err_id != 5 && err_id != 6)
{
file_error_html += (file_error_html != "" ? "<a href ='<?php echo base_url()?>"+ "employee/excel_error/" + file_id + "' target=_blank>click here to more details...</a>":"");
}
console.log(file_error_html);
$('#file-err-modal').find(".modal-body").html(file_error_html);
return file_error_html;
} catch (error)
{
console.error('Error parsing API response data:', error);
}
} else if(response.code === 404 && response.dataStatus === false){
console.error('no data found', response);
}
else
{
console.error('Something went wrong!');
}
},
error: function(xhr, status, error) {
console.error('Error fetching data from API:', error);
}
});
$('#loader').hide();
}
function appendClients(data) {
$.each(data, function(index, item) {
$('#client_id').append($('<option>', {
value: item.id,
text: item.client_name
}));
});
}
function appendPolicies(data) {
$('#policy_id').empty();
$('#policy_id').append($('<option>', { value: '',text: 'Select'}));
$.each(data, function(index, item) {
$('#policy_id').append($('<option>', {
value: item.id,
text: item.name
}));
});
}
$('#client_id').on('change', function() {
var selectedClient = $(this).val();
console.log(selectedClient);
// $('#selectedOptionInfo').text('Selected option: ' + selectedOption);
// Check if the selected option exists in apiData
var foundPolicies = clientPolicies.find(function(item) {
return item.id === selectedClient;
});
console.log(foundPolicies.policies);
appendPolicies(foundPolicies.policies);
});
// Function to convert object to query parameters
function objectToQueryString(obj) {
return Object.keys(obj).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`).join('&');
}
function fetchEmpolyeeList(event) {
event.preventDefault(); // Prevent default action
var client_id = $('#client_id').val();
var policy_id = $('#policy_id').val();
console.log(client_id+'-'+policy_id);
if (client_id == '0' || policy_id == '0' ) {
alert('Please select values in both dropdowns.');
return;
}
var queryParams = {
client_id: client_id,
policy_id: policy_id
};
const queryString = objectToQueryString(queryParams);
const apiURL = $('#get-emp-list').attr('href')+"?" + queryString;
console.log(apiURL);
window.location.href = apiURL;
// var apiURL2 = $('#get-emp-list').attr('href'); // Get href attribute value
// console.log(apiURL);
// $.ajax({
// url: apiURL,
// method: 'GET',
// data: queryParams,
// success: function(response) {
// // Assuming response is a JSON array
// // displayDataTables(response);
// console.log(response);
// },
// error: function(xhr, status, error) {
// console.error('Error:', error);
// }
// });
}
</script>