501 lines
22 KiB
PHP
501 lines
22 KiB
PHP
<!-- Include jQuery first -->
|
||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||
|
||
<!-- Include jQuery Validation plugin -->
|
||
<script src="https://cdn.jsdelivr.net/jquery.validation/1.19.3/jquery.validate.min.js"></script>
|
||
|
||
<style>
|
||
.form-group {
|
||
padding-bottom: 0%;
|
||
}
|
||
.Date-filter-form {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
/* Adjust the gap as needed */
|
||
}
|
||
|
||
.Date-filter-form input,
|
||
.Date-filter-form button {
|
||
padding: 5px;
|
||
font-size: 14px;
|
||
}
|
||
|
||
.Date-filter-form button {
|
||
background-color: #007bff;
|
||
color: white;
|
||
border: none;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.Date-filter-form button:hover {
|
||
background-color: #0056b3;
|
||
}
|
||
|
||
.icon-button {
|
||
background: none;
|
||
border: none;
|
||
cursor: pointer;
|
||
color: #007bff;
|
||
font-size: 18px;
|
||
}
|
||
|
||
.icon-button:hover {
|
||
color: #0056b3;
|
||
}
|
||
.delete_button{
|
||
border: none;
|
||
background-color: white;
|
||
color: red;
|
||
}
|
||
</style>
|
||
<div class="content-page">
|
||
<div class="content">
|
||
<!-- Start Content-->
|
||
<div class="container-fluid">
|
||
<!-- start page title -->
|
||
<div>
|
||
<div class="row">
|
||
<div class="col-6">
|
||
<div class="page-title-box page-title-box-alt">
|
||
<h4 class="page-title">Expense List</h4>
|
||
</div>
|
||
</div>
|
||
<div class="col-6 text-right">
|
||
<a id="addExpense" class="btn btn-success" data-toggle="modal" href="#expenseModal">Add New Expense</a>
|
||
</div>
|
||
</div>
|
||
<div class="row">
|
||
<div class="col-12">
|
||
<div class="card">
|
||
<form class="Date-filter-form" id="DateRangeFilter" style="margin-top: 14px;margin-bottom: -11px;margin-left: 33px;">
|
||
<input type="hidden" name="table" value="requisition">
|
||
<label for="fromDate">From:</label>
|
||
<input class="form-control date_range" type="date" id="fromDate" name="fromDate" placeholder="Select From Date" autocomplete="off" required>
|
||
|
||
<label for="toDate">To:</label>
|
||
<input class="form-control date_range" type="date" id="toDate" name="toDate" placeholder="Select To Date" autocomplete="off" required>
|
||
|
||
<button type="submit" class="range_search_button"><i class="fe-search" aria-hidden="true" class="icon-button" title="Search"></i></button>
|
||
<i class="fe-rotate-cw range_reset_button" aria-hidden="true" class="icon-button" id="resetButton" title="Reset"></i>
|
||
<div class="error" id="error"></div>
|
||
</form>
|
||
<div class="card-body">
|
||
<table class="table table-bordered table-hover" id="expense_list_table" style="background-color:#fff;font-size:12px;">
|
||
<thead style="background-color: #ddd;">
|
||
<tr>
|
||
<th align="right">Created Date</th>
|
||
<!-- <th align="right">Expense Id</th> -->
|
||
<th align="right">Supplier Name</th>
|
||
<th align="right">GST</th>
|
||
<th align="right">Cost</th>
|
||
<th align="right">Total</th>
|
||
<th align="right">Status</th>
|
||
<th align="right">Payment Method</th>
|
||
<th>Action</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php
|
||
if (!empty($expense_list)) {
|
||
foreach ($expense_list as $record) {
|
||
$fileExists = 0;
|
||
$fileUrl = '';
|
||
if ($record['transporter_file']) {
|
||
$fileExists = 1;
|
||
$fileUrl = base_url('uploads/') . $record['transporter_file'];
|
||
}
|
||
// Create download link and delete button
|
||
?>
|
||
<tr>
|
||
<?php if (date('d-m-Y', strtotime($record['created_on'])) == "30-11--0001") {
|
||
$cdate = '00-00-0000';
|
||
} else {
|
||
$cdate = date('d-m-Y', strtotime($record['created_on']));
|
||
}
|
||
?>
|
||
<td><?php echo $cdate; ?></td>
|
||
<!-- <td><?php echo $record['id']; ?></td> -->
|
||
<td><?php echo $record['SupplierName']; ?></td>
|
||
<td><?php echo $record['gst']; ?></td>
|
||
<td><?php echo $record['cost']; ?></td>
|
||
<td><?php echo $record['total']; ?></td>
|
||
<td><?php echo $record['status']; ?></td>
|
||
<td><?php echo $record['payment_method']; ?></td>
|
||
<td>
|
||
<?php if($fileExists == 1){ ?>
|
||
<button title="Download FIle" class="download_button" onclick="downloadFile('<?php echo $record['id']; ?>', '<?php echo $record['transporter_file']; ?>')" style="border: none;background-color: white;color: #02a8b5;">
|
||
<i class="fas fa-file-download"></i>
|
||
</button>
|
||
<button title="Delete File" id="deleteFile" class="delete_button" data-value="<?php echo $record['id'] ?>" data-id="<?php echo $record['id'] ?>" data-file="<?php echo $record['transporter_file'] ?>"><i class="fas fa-trash"></i></button>
|
||
|
||
<?php } ?>
|
||
<button title="Edit" class="edit-button" data-toggle="modal" data-target="#expenseModal" data-id="<?php echo $record['id']; ?>" style="border: none;background-color: white;color: #02a8b5;" >
|
||
<i class="fas fa-pencil-alt"></i>
|
||
</button>
|
||
</td>
|
||
</tr>
|
||
<?php
|
||
}
|
||
}
|
||
?>
|
||
</tbody>
|
||
</table>
|
||
</div> <!-- end card body-->
|
||
</div> <!-- end card -->
|
||
</div><!-- end col-->
|
||
</div>
|
||
</div>
|
||
</div> <!-- container -->
|
||
</div> <!-- content -->
|
||
</div>
|
||
|
||
|
||
<div id="expenseModal" class="modal fade" tabindex="-1" role="dialog"
|
||
aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
|
||
<div class="modal-dialog modal-lg">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h4 class="modal-title">Add Expense</h4>
|
||
<button type="button" class="close" data-dismiss="modal"
|
||
aria-hidden="true">×</button>
|
||
</div>
|
||
<div class="modal-body p-4">
|
||
<form id="expenseForm" class="form-horizontal" role="form">
|
||
<input type="hidden" name="id" id="id">
|
||
<div class="form-group row">
|
||
<div class="col-md-5">
|
||
<label for="transporterfile" class="col-form-label">Transporter File:</label>
|
||
<input type="file" name="transporterfile" id="transporterfile" class="form-control">
|
||
<input type="hidden" name="transporterfile_name" id="transporterfile_name">
|
||
<small id="fileHelp" class="form-text text-muted">
|
||
Current file: <span id="currentFileName"></span>
|
||
</small>
|
||
</div>
|
||
<div class="col-md-1">
|
||
</div>
|
||
<div class="col-md-5">
|
||
<label for="supplierid" class="col-form-label">Supplier :</label>
|
||
<select name="supplierid" id="supplierid" class="form-control" required>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="form-group row">
|
||
<div class="col-md-5">
|
||
<label for="gst" class="col-form-label">Tax:</label>
|
||
<input type="number" name="gst" id="gst" class="form-control" >
|
||
</div>
|
||
<div class="col-md-1">
|
||
</div>
|
||
<div class="col-md-5">
|
||
<label for="cost" class="col-form-label">Cost:</label>
|
||
<input type="number" name="cost" id="cost" class="form-control">
|
||
</div>
|
||
|
||
|
||
</div>
|
||
<div class="form-group row">
|
||
<div class="col-md-5">
|
||
<label for="status" class="col-form-label">Status:</label>
|
||
<select name="status" id="status_id" class="form-control" required>
|
||
<option>Select Status</option>
|
||
<option value="Payment Pending">Payment Pending</option>
|
||
<option value="Paid">Paid</option>
|
||
</select>
|
||
</div>
|
||
<div class="col-md-1">
|
||
</div>
|
||
<div class="col-md-5">
|
||
<label for="payment_method" class="col-form-label">Payment Method:</label>
|
||
<select name="payment_method" id="payment_method" class="form-control" required>
|
||
<option>Select Payment Method</option>
|
||
<option value="Cash">Cash</option>
|
||
<option value="Online">Online</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
<div class="form-group row">
|
||
<div class="col-md-12">
|
||
<label for="remarks" class="col-form-label">Remarks:</label>
|
||
<!-- <input type="text" name="remarks" id="remarks" class="form-control"> -->
|
||
<textarea name="remarks" id="remarks" class="form-control"></textarea>
|
||
</div>
|
||
|
||
</div>
|
||
</form>
|
||
</div>
|
||
|
||
<div class="modal-footer">
|
||
<button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Cancel</button>
|
||
<button type="button" class="btn btn-info waves-effect waves-light tempClick" id="tempClick">Submit</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div><!-- /.modal -->
|
||
|
||
<script>
|
||
function downloadFile(fileId, fileName) {
|
||
window.location.href = 'downloadFile/' + fileName;
|
||
}
|
||
$(document).ready(function() {
|
||
// Function to toggle payment method based on status
|
||
function togglePaymentMethod() {
|
||
var status = $('#status_id').val();
|
||
console.log(status);
|
||
|
||
if (status === 'Paid') {
|
||
$('#payment_method').prop('disabled', false); // Enable payment method
|
||
} else {
|
||
$('#payment_method').prop('disabled', true).val(''); // Disable payment method and clear value
|
||
}
|
||
}
|
||
|
||
// Trigger the toggle function on page load
|
||
togglePaymentMethod();
|
||
|
||
// Trigger the toggle function whenever the status changes
|
||
$('#status_id').on('change', function() {
|
||
togglePaymentMethod();
|
||
});
|
||
});
|
||
|
||
$(document).ready(function() {
|
||
$('#supplierid').select2();
|
||
var formActionUrl = '';
|
||
|
||
// Event handler for file input change
|
||
$('#transporterfile').on('change', function() {
|
||
var fileName = $(this).val().split('\\').pop(); // Get the file name
|
||
$('#file-name-display').text(fileName); // Display the file name
|
||
});
|
||
function togglePaymentMethodEdit() {
|
||
var status = $('#status_id').val();
|
||
console.log(status);
|
||
|
||
if (status === 'Paid') {
|
||
$('#payment_method').prop('disabled', false); // Enable payment method
|
||
} else {
|
||
$('#payment_method').prop('disabled', true).val(''); // Disable payment method and clear value
|
||
}
|
||
}
|
||
$(document).on('click', '.edit-button', function() {
|
||
|
||
$('.modal-title').html('Edit Expense');
|
||
var expenseId = $(this).data('id');
|
||
var supplier_list = [<?php echo $supplier_list; ?>];
|
||
$('#supplierid').empty(); // Clear the supplier dropdown
|
||
$('#supplierid').append('<option>Select Supplier</option>');
|
||
supplier_list[0].forEach(function(supplier) {
|
||
$('#supplierid').append('<option value="' + supplier.SupplierID + '">' + supplier.SupplierName + '</option>');
|
||
});
|
||
formActionUrl = '<?php echo base_url().'editExpense' ?>'; // Set action URL for adding
|
||
|
||
$.ajax({
|
||
url: '<?php echo base_url().'getExpenseDetails'; ?>',
|
||
type: 'POST',
|
||
data: { id: expenseId },
|
||
success: function(response) {
|
||
var expense = JSON.parse(response);
|
||
$('#id').val(expense.id);
|
||
$('#transporterfile').val(''); // Clear the file input
|
||
$('#currentFileName').text(''); // Clear the file name display
|
||
$('#supplierid').val(expense.supplier_id).trigger('change');
|
||
$('#remarks').val(expense.remarks);
|
||
$('#cost').val(expense.cost);
|
||
$('#gst').val(expense.gst);
|
||
$('#total').val(expense.total);
|
||
$('#status_id').val(expense.status);
|
||
$('#payment_method').val(expense.payment_method);
|
||
|
||
// Clear any existing file link and delete button
|
||
$('#transporterfile').next('.download_button').remove();
|
||
$('#transporterfile').next('.delete_button').remove();
|
||
|
||
if (expense.transporter_file) {
|
||
$('#transporterfile_name').val(expense.transporter_file); // Set the hidden input value
|
||
$('#currentFileName').text(expense.transporter_file); // Display the existing file name
|
||
|
||
// Construct the full URL to the file
|
||
var fileUrl = '<?php echo base_url('uploads/'); ?>' + expense.transporter_file;
|
||
|
||
// Create download link and delete button
|
||
var fileLink = '<a class="download_button" href="' + fileUrl + '" target="_blank" download="' + expense.transporter_file + '"><i class=" fas fa-file-download"></i></a>';
|
||
var deleteButton = '<button id="deleteFile" class="delete_button" data-value="' + expense.id +'" data-id="' + expense.id + '" data-file="' + expense.transporter_file + '"><i class="fas fa-trash"></i></button>';
|
||
|
||
// Add the file link and delete button
|
||
$('#transporterfile').after(fileLink + ' ' + deleteButton);
|
||
} else {
|
||
$('#transporterfile_name').val(''); // Clear hidden input if no file
|
||
}
|
||
togglePaymentMethodEdit();
|
||
}
|
||
|
||
});
|
||
});
|
||
|
||
|
||
$(document).on('click', '#addExpense', function () {
|
||
$('.download_button').remove();
|
||
$('.delete_button').remove();
|
||
$('.modal-title').html('Add Expense');
|
||
$('#transporterfile').val(''); // Clear the file input
|
||
$('#currentFileName').text(''); // Clear the file name display
|
||
$('#supplierid').val('').trigger('change');
|
||
$('#remarks').val('');
|
||
$('#cost').val('');
|
||
$('#gst').val('');
|
||
$('#total').val('');
|
||
$('#status_id').val('');
|
||
$('#payment_method').val('');
|
||
var supplier_list = [<?php echo $supplier_list; ?>];
|
||
$('#supplierid').empty(); // Clear the supplier dropdown
|
||
$('#supplierid').append('<option>Select Supplier</option>');
|
||
|
||
supplier_list[0].forEach(function(supplier) {
|
||
$('#supplierid').append('<option value="' + supplier.SupplierID + '">' + supplier.SupplierName + '</option>');
|
||
});
|
||
formActionUrl = '<?php echo base_url().'addExpense' ?>'; // Set action URL for adding
|
||
togglePaymentMethodEdit();
|
||
});
|
||
|
||
// Handle form submission
|
||
$('#tempClick').on('click', function(e) {
|
||
e.preventDefault();
|
||
|
||
// Native HTML5 form validation
|
||
if ($('#expenseForm')[0].checkValidity()) {
|
||
var formData = new FormData($('#expenseForm')[0]); // Use FormData to handle file upload
|
||
console.log(formActionUrl);
|
||
|
||
$.ajax({
|
||
url: formActionUrl,
|
||
type: 'POST',
|
||
data: formData,
|
||
processData: false,
|
||
contentType: false,
|
||
success: function(response) {
|
||
console.log(response);
|
||
|
||
if (response) {
|
||
$('#expenseModal').modal('hide');
|
||
$('#expenseForm')[0].reset();
|
||
$('#file-name-display').text(''); // Clear the file name display
|
||
window.location.reload();
|
||
} else {
|
||
console.log(response);
|
||
}
|
||
},
|
||
error: function(xhr, status, error) {
|
||
alert('An error occurred: ' + error);
|
||
}
|
||
});
|
||
} else {
|
||
// If form is invalid, show native validation messages
|
||
$('#expenseForm')[0].reportValidity();
|
||
}
|
||
});
|
||
|
||
|
||
// DataTable initialization and date range filter logic (unchanged)
|
||
var table = $('#expense_list_table').DataTable({
|
||
dom: 'Blfrtip',
|
||
buttons: [
|
||
'copy', 'csv', 'excel', 'pdf', 'print'
|
||
],
|
||
pageLength: 10,
|
||
lengthMenu: [ [10, 20, 30, 50, -1], [10, 20, 30, 50, "All"] ],
|
||
responsive: true,
|
||
order: [[0, 'desc']],
|
||
language: {
|
||
paginate: {
|
||
next: '<i class="fas fa-angle-right"></i>',
|
||
previous: '<i class="fas fa-angle-left"></i>'
|
||
}
|
||
}
|
||
});
|
||
|
||
$.fn.dataTable.ext.search.push(
|
||
function(settings, data, dataIndex) {
|
||
var fromDate = $('#fromDate').val();
|
||
var toDate = $('#toDate').val();
|
||
|
||
if (!fromDate || !toDate) {
|
||
return true; // If no dates selected, don't filter
|
||
}
|
||
|
||
var dateStr = data[0];
|
||
var dateParts = dateStr.split("-");
|
||
var date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
|
||
|
||
var startDate = new Date(fromDate);
|
||
var endDate = new Date(toDate);
|
||
|
||
startDate.setDate(startDate.getDate() - 1);
|
||
endDate.setHours(23, 59, 59, 999);
|
||
|
||
return date >= startDate && date <= endDate;
|
||
}
|
||
);
|
||
|
||
$("#DateRangeFilter").submit(function(e) {
|
||
e.preventDefault();
|
||
table.draw();
|
||
});
|
||
|
||
$("#resetButton").click(function() {
|
||
$("#fromDate").val('');
|
||
$("#toDate").val('');
|
||
table.draw();
|
||
});
|
||
|
||
document.getElementById('fromDate').addEventListener('change', function() {
|
||
var fromDate = this.value;
|
||
var toDateInput = document.getElementById('toDate');
|
||
toDateInput.min = fromDate;
|
||
if (toDateInput.value < fromDate) {
|
||
toDateInput.value = fromDate;
|
||
}
|
||
});
|
||
});
|
||
|
||
$(document).on('click', '#deleteFile', function() {
|
||
var fileName = $(this).data('file');
|
||
var expenseId = $(this).data('value');
|
||
|
||
$.ajax({
|
||
url: '<?php echo base_url('deleteFile'); ?>',
|
||
type: 'POST',
|
||
data: { file_name: fileName, expense_id: expenseId },
|
||
success: function(response) {
|
||
console.log(response);
|
||
|
||
var result = JSON.parse(response);
|
||
if (result.success) {
|
||
alert('File deleted successfully.');
|
||
$('#currentFileName').text('');
|
||
$('#transporterfile_name').val('');
|
||
$('#deleteFile').remove(); // Remove the delete button
|
||
$('#transporterfile').next('a').remove(); // Remove the download link
|
||
} else {
|
||
alert('Failed to delete the file: ' + result.message);
|
||
}
|
||
},
|
||
error: function() {
|
||
alert('Error deleting the file.');
|
||
}
|
||
});
|
||
});
|
||
|
||
|
||
|
||
</script>
|
||
|
||
|
||
<script>
|
||
document.getElementById('transporterfile').addEventListener('change', function() {
|
||
var fileName = this.files[0].name;
|
||
document.getElementById('currentFileName').textContent = fileName;
|
||
});
|
||
</script>
|