CHANGE_EXPENSE_FULL_CODE
This commit is contained in:
parent
e1e524646d
commit
d9816e20cc
@ -266,7 +266,10 @@ $routes->post('inwardgateregister/edituploadfile', 'Inwardgateregister::edituplo
|
|||||||
// Non IGR / Expense
|
// Non IGR / Expense
|
||||||
$routes->get('ListIGR', 'Expense::index');
|
$routes->get('ListIGR', 'Expense::index');
|
||||||
$routes->post('addExpense', 'Expense::addExpense');
|
$routes->post('addExpense', 'Expense::addExpense');
|
||||||
$routes->get('getExpenseDetails', 'Expense::getExpenseDetails');
|
$routes->post('editExpense', 'Expense::editExpense');
|
||||||
|
$routes->post('getExpenseDetails', 'Expense::getExpenseDetails');
|
||||||
|
$routes->get('uploads/(:any)', 'Expense::downloads/$1');
|
||||||
|
$routes->post('deleteFile', 'Expense::deleteFile');
|
||||||
|
|
||||||
|
|
||||||
// Application OGR Page
|
// Application OGR Page
|
||||||
|
|||||||
@ -3,46 +3,44 @@
|
|||||||
namespace App\Controllers;
|
namespace App\Controllers;
|
||||||
|
|
||||||
use App\Controllers\BaseController;
|
use App\Controllers\BaseController;
|
||||||
|
|
||||||
use App\Models\Expense_model;
|
use App\Models\Expense_model;
|
||||||
use App\Models\Supplier_model;
|
use App\Models\Supplier_model;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Expense extends BaseController
|
class Expense extends BaseController
|
||||||
{
|
{
|
||||||
protected $expense_model;
|
protected $expense_model;
|
||||||
protected $supplier_model;
|
protected $supplier_model;
|
||||||
protected $session;
|
protected $session;
|
||||||
|
|
||||||
/**
|
|
||||||
* This is default constructor of the class
|
|
||||||
*/
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->expense_model = new Expense_model();
|
$this->expense_model = new Expense_model();
|
||||||
$this->supplier_model = new Supplier_model();
|
$this->supplier_model = new Supplier_model();
|
||||||
|
|
||||||
$this->session = session();
|
$this->session = session();
|
||||||
helper('form'); //$this->load->library('form_validation');
|
helper('form');
|
||||||
|
|
||||||
$this->isLoggedIn();
|
$this->isLoggedIn();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$data['expense_list'] = $this->expense_model->getAllExpense();
|
$data['expense_list'] = $this->expense_model->getAllExpense();
|
||||||
$data['supplier_list'] = json_encode($this->supplier_model->supplierlisting());
|
$data['supplier_list'] = json_encode($this->supplier_model->supplierlisting());
|
||||||
$this->global['pageTitle'] = 'Non IGR / Expense List';
|
$this->global['pageTitle'] = 'Non IGR / Expense List';
|
||||||
// echo "<pre>";
|
|
||||||
// print_r($data);die;
|
|
||||||
$this->loadViews("expense_list", $this->global, $data, NULL);
|
$this->loadViews("expense_list", $this->global, $data, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addExpense()
|
public function addExpense()
|
||||||
{
|
{
|
||||||
$data = array(
|
$file = $this->request->getFile('transporterfile');
|
||||||
'transporter_file' => $this->request->getPost('transporterfile'),
|
$fileName = '';
|
||||||
|
if ($file->isValid() && !$file->hasMoved()) {
|
||||||
|
$fileName = $file->getRandomName();
|
||||||
|
$file->move(WRITEPATH . 'uploads', $fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'transporter_file' => $fileName,
|
||||||
'supplier_id' => $this->request->getPost('supplierid'),
|
'supplier_id' => $this->request->getPost('supplierid'),
|
||||||
'remarks' => $this->request->getPost('remarks'),
|
'remarks' => $this->request->getPost('remarks'),
|
||||||
'cost' => $this->request->getPost('cost'),
|
'cost' => $this->request->getPost('cost'),
|
||||||
@ -50,26 +48,20 @@ class Expense extends BaseController
|
|||||||
'total' => $this->request->getPost('total'),
|
'total' => $this->request->getPost('total'),
|
||||||
'status' => $this->request->getPost('status'),
|
'status' => $this->request->getPost('status'),
|
||||||
'payment_method' => $this->request->getPost('payment_method')
|
'payment_method' => $this->request->getPost('payment_method')
|
||||||
);
|
];
|
||||||
|
|
||||||
$insert_id = $this->expense_model->insertExpense($data);
|
$insert_id = $this->expense_model->insertExpense($data);
|
||||||
|
|
||||||
if ($insert_id) {
|
if ($insert_id) {
|
||||||
// Return success response or redirect
|
|
||||||
// $this->session->set_flashdata('success', 'Expense added successfully.');
|
|
||||||
// redirect('your_controller/your_method'); // Replace with your redirect path
|
|
||||||
return json_encode('true');
|
return json_encode('true');
|
||||||
} else {
|
} else {
|
||||||
// Return error response
|
|
||||||
// $this->session->set_flashdata('error', 'Failed to add expense.');
|
|
||||||
// redirect('your_controller/your_method'); // Replace with your redirect path
|
|
||||||
return json_encode('false');
|
return json_encode('false');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getExpenseDetails() {
|
public function getExpenseDetails()
|
||||||
|
{
|
||||||
$id = $this->request->getPost('id');
|
$id = $this->request->getPost('id');
|
||||||
|
|
||||||
if ($id) {
|
if ($id) {
|
||||||
$expense = $this->expense_model->getExpenseById($id);
|
$expense = $this->expense_model->getExpenseById($id);
|
||||||
echo json_encode($expense);
|
echo json_encode($expense);
|
||||||
@ -78,28 +70,93 @@ class Expense extends BaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateExpense() {
|
public function editExpense()
|
||||||
// $id = $this->input->post('id');
|
{
|
||||||
// $data = [
|
$id = $this->request->getPost('id');
|
||||||
// 'transporterfile' => $this->input->post('transporterfile'),
|
$file = $this->request->getFile('transporterfile');
|
||||||
// 'supplierid' => $this->input->post('supplierid'),
|
$fileName = '';
|
||||||
// 'remarks' => $this->input->post('remarks'),
|
|
||||||
// 'cost' => $this->input->post('cost'),
|
|
||||||
// 'gst' => $this->input->post('gst'),
|
|
||||||
// 'total' => $this->input->post('total'),
|
|
||||||
// 'status' => $this->input->post('status'),
|
|
||||||
// 'payment_method' => $this->input->post('payment_method')
|
|
||||||
// ];
|
|
||||||
|
|
||||||
// if ($id) {
|
if ($file->isValid() && !$file->hasMoved()) {
|
||||||
// $result = $this->Expense_model->updateExpense($id, $data);
|
$fileName = $file->getRandomName();
|
||||||
// if ($result) {
|
$file->move(WRITEPATH . 'uploads', $fileName);
|
||||||
// echo json_encode(['success' => 'Expense updated successfully']);
|
} else {
|
||||||
// } else {
|
$fileName = $this->request->getPost('existing_file'); // Use existing file if no new file is uploaded
|
||||||
// echo json_encode(['error' => 'Failed to update expense']);
|
}
|
||||||
// }
|
|
||||||
// } else {
|
$data = [
|
||||||
// echo json_encode(['error' => 'Invalid ID']);
|
'supplier_id' => $this->request->getPost('supplierid'),
|
||||||
// }
|
'remarks' => $this->request->getPost('remarks'),
|
||||||
|
'cost' => $this->request->getPost('cost'),
|
||||||
|
'gst' => $this->request->getPost('gst'),
|
||||||
|
'total' => $this->request->getPost('total'),
|
||||||
|
'status' => $this->request->getPost('status'),
|
||||||
|
'payment_method' => $this->request->getPost('payment_method')
|
||||||
|
];
|
||||||
|
if(!empty($fileName)){
|
||||||
|
$data['transporter_file'] = $fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($id) {
|
||||||
|
$result = $this->expense_model->updateExpense($id, $data);
|
||||||
|
if ($result) {
|
||||||
|
echo json_encode(['success' => 'Expense updated successfully']);
|
||||||
|
} else {
|
||||||
|
echo json_encode(['error' => 'Failed to update expense']);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo json_encode(['error' => 'Invalid ID']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function downloads($filename)
|
||||||
|
{
|
||||||
|
$path = WRITEPATH . 'uploads/' . $filename;
|
||||||
|
if (file_exists($path)) {
|
||||||
|
return $this->response->download($path, null);
|
||||||
|
} else {
|
||||||
|
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('File not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function deleteFile()
|
||||||
|
{
|
||||||
|
$fileName = $this->request->getPost('file_name');
|
||||||
|
$expenseId = $this->request->getPost('expense_id');
|
||||||
|
|
||||||
|
if ($fileName && $expenseId) {
|
||||||
|
// Construct the file path
|
||||||
|
$path = WRITEPATH . 'uploads/' . $fileName;
|
||||||
|
|
||||||
|
// Begin transaction
|
||||||
|
$db = \Config\Database::connect();
|
||||||
|
$db->transBegin();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Delete the file from the folder
|
||||||
|
if (file_exists($path)) {
|
||||||
|
if (!unlink($path)) {
|
||||||
|
throw new \Exception('Unable to delete file from the folder.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the database to set transporter_file to null
|
||||||
|
$this->expense_model->deleteFile($expenseId);
|
||||||
|
|
||||||
|
// Commit transaction
|
||||||
|
$db->transCommit();
|
||||||
|
echo json_encode(['success' => true]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Rollback transaction
|
||||||
|
$db->transRollback();
|
||||||
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Invalid parameters.']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ class Expense_model extends Model
|
|||||||
protected $primaryKey = 'id'; // Primary key
|
protected $primaryKey = 'id'; // Primary key
|
||||||
|
|
||||||
protected $allowedFields = [
|
protected $allowedFields = [
|
||||||
'transporterfile', 'supplier_id', 'remarks', 'cost', 'gst', 'total', 'status', 'payment_method'
|
'transporter_file', 'supplier_id', 'remarks', 'cost', 'gst', 'total', 'status', 'payment_method'
|
||||||
];
|
];
|
||||||
|
|
||||||
// Retrieve all active expenses
|
// Retrieve all active expenses
|
||||||
@ -34,5 +34,12 @@ class Expense_model extends Model
|
|||||||
public function updateExpense($id, $data) {
|
public function updateExpense($id, $data) {
|
||||||
return $this->update($id, $data); // Use primary key
|
return $this->update($id, $data); // Use primary key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function deleteFile($expenseId)
|
||||||
|
{
|
||||||
|
// Update the database to set transporter_file to null
|
||||||
|
return $this->update($expenseId, ['transporter_file' => null]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@ -37,6 +37,11 @@
|
|||||||
.icon-button:hover {
|
.icon-button:hover {
|
||||||
color: #0056b3;
|
color: #0056b3;
|
||||||
}
|
}
|
||||||
|
.delete_button{
|
||||||
|
border: none;
|
||||||
|
background-color: white;
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
<div class="content-page">
|
<div class="content-page">
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@ -74,13 +79,13 @@
|
|||||||
<thead style="background-color: #ddd;">
|
<thead style="background-color: #ddd;">
|
||||||
<tr>
|
<tr>
|
||||||
<th align="right">Created Date</th>
|
<th align="right">Created Date</th>
|
||||||
|
<th align="right">Expense Id</th>
|
||||||
<th align="right">Supplier Name</th>
|
<th align="right">Supplier Name</th>
|
||||||
<th align="right">Cost</th>
|
<th align="right">Cost</th>
|
||||||
<th align="right">GST</th>
|
<th align="right">GST</th>
|
||||||
<th align="right">Total</th>
|
<th align="right">Total</th>
|
||||||
<th align="right">Status</th>
|
<th align="right">Status</th>
|
||||||
<th align="right">payment_method</th>
|
<th align="right">Payment Method</th>
|
||||||
<th align="right">Action</th> <!-- New column for actions -->
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@ -95,17 +100,13 @@
|
|||||||
$cdate = date('d-m-Y', strtotime($record['created_on']));
|
$cdate = date('d-m-Y', strtotime($record['created_on']));
|
||||||
} ?>
|
} ?>
|
||||||
<td><?php echo $cdate; ?></td>
|
<td><?php echo $cdate; ?></td>
|
||||||
|
<td class="edit-button" data-id="<?php echo $record['id']; ?>"><a href="#" data-toggle="modal" data-target="#expenseModal"><?php echo $record['id']; ?></a></td>
|
||||||
<td><?php echo $record['SupplierName']; ?></td>
|
<td><?php echo $record['SupplierName']; ?></td>
|
||||||
<td><?php echo $record['cost']; ?></td>
|
<td><?php echo $record['cost']; ?></td>
|
||||||
<td><?php echo $record['gst']; ?></td>
|
<td><?php echo $record['gst']; ?></td>
|
||||||
<td><?php echo $record['total']; ?></td>
|
<td><?php echo $record['total']; ?></td>
|
||||||
<td><?php echo $record['status']; ?></td>
|
<td><?php echo $record['status']; ?></td>
|
||||||
<td><?php echo $record['payment_method']; ?></td>
|
<td><?php echo $record['payment_method']; ?></td>
|
||||||
<td>
|
|
||||||
<button class="btn btn-primary edit-button" data-id="<?php echo $record['id']; ?>" data-toggle="modal" data-target="#expenseModal">
|
|
||||||
Edit
|
|
||||||
</button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
@ -134,15 +135,20 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="modal-body p-4">
|
<div class="modal-body p-4">
|
||||||
<form id="expenseForm" class="form-horizontal" role="form">
|
<form id="expenseForm" class="form-horizontal" role="form">
|
||||||
|
<input type="hidden" name="id" id="id">
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<div class="col-md-5">
|
<div class="col-md-5">
|
||||||
<label for="transporterfile" class="col-form-label">Transporter File:</label>
|
<label for="transporterfile" class="col-form-label">Transporter File:</label>
|
||||||
<input type="text" name="transporterfile" id="transporterfile" class="form-control">
|
<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>
|
||||||
<div class="col-md-1">
|
<div class="col-md-1">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-5">
|
<div class="col-md-5">
|
||||||
<label for="supplierid" class="col-form-label">Supplier ID:</label>
|
<label for="supplierid" class="col-form-label">Supplier :</label>
|
||||||
<select name="supplierid" id="supplierid" class="form-control">
|
<select name="supplierid" id="supplierid" class="form-control">
|
||||||
<option value="">Select Supplier</option>
|
<option value="">Select Supplier</option>
|
||||||
</select>
|
</select>
|
||||||
@ -175,13 +181,21 @@
|
|||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<div class="col-md-5">
|
<div class="col-md-5">
|
||||||
<label for="status" class="col-form-label">Status:</label>
|
<label for="status" class="col-form-label">Status:</label>
|
||||||
<input type="text" name="status" id="status" class="form-control">
|
<select name="status" id="status_id" class="form-control" required>
|
||||||
|
<option >Select Status</option>
|
||||||
|
<option value="1">Pending</option>
|
||||||
|
<option value="2">Complete</option>
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-1">
|
<div class="col-md-1">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-5">
|
<div class="col-md-5">
|
||||||
<label for="payment_method" class="col-form-label">Payment Method:</label>
|
<label for="payment_method" class="col-form-label">Payment Method:</label>
|
||||||
<input type="text" name="payment_method" id="payment_method" class="form-control">
|
<select name="payment_method" id="payment_method" class="form-control" required>
|
||||||
|
<option >Select Payment Method</option>
|
||||||
|
<option value="1">Cash</option>
|
||||||
|
<option value="2">Online</option>
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
@ -195,94 +209,145 @@
|
|||||||
</div>
|
</div>
|
||||||
</div><!-- /.modal -->
|
</div><!-- /.modal -->
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
$(document).on('click', '.edit-button', function() {
|
var formActionUrl = '';
|
||||||
var expenseId = $(this).data('id');
|
|
||||||
|
|
||||||
$.ajax({
|
// Event handler for file input change
|
||||||
url: '<?php echo base_url().'getExpenseDetails' ?>', // Your PHP script to get expense details
|
$('#transporterfile').on('change', function() {
|
||||||
type: 'POST',
|
var fileName = $(this).val().split('\\').pop(); // Get the file name
|
||||||
data: { id: expenseId },
|
$('#file-name-display').text(fileName); // Display the file name
|
||||||
success: function(response) {
|
});
|
||||||
var expense = JSON.parse(response);
|
|
||||||
|
|
||||||
// Populate the form with existing data
|
$(document).on('click', '.edit-button', function() {
|
||||||
$('#transporterfile').val(expense.transporterfile);
|
$('.modal-title').html('Edit Expense');
|
||||||
$('#supplierid').val(expense.supplierid);
|
var expenseId = $(this).data('id');
|
||||||
$('#remarks').val(expense.remarks);
|
var supplier_list = [<?php echo $supplier_list; ?>];
|
||||||
$('#cost').val(expense.cost);
|
$('#supplierid').empty(); // Clear the supplier dropdown
|
||||||
$('#gst').val(expense.gst);
|
$('#supplierid').append('<option >Select Supplier</option>');
|
||||||
$('#total').val(expense.total);
|
supplier_list[0].forEach(function(supplier) {
|
||||||
$('#status').val(expense.status);
|
$('#supplierid').append('<option value="' + supplier.SupplierID + '">' + supplier.SupplierName + '</option>');
|
||||||
$('#payment_method').val(expense.payment_method);
|
});
|
||||||
$('#expenseForm').data('id', expenseId); // Store the ID in the form for later use
|
formActionUrl = '<?php echo base_url().'editExpense' ?>'; // Set action URL for adding
|
||||||
},
|
|
||||||
error: function(xhr, status, error) {
|
$.ajax({
|
||||||
alert('An error occurred: ' + error);
|
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);
|
||||||
|
$('#remarks').val(expense.remarks);
|
||||||
|
$('#cost').val(expense.cost);
|
||||||
|
$('#gst').val(expense.gst);
|
||||||
|
$('#total').val(expense.total);
|
||||||
|
if(expense.status == 1){
|
||||||
|
$('#status_id').val("Pending");
|
||||||
|
}else if(expense.status == 2){
|
||||||
|
$('#status_id').val("Completed");
|
||||||
|
}
|
||||||
|
if(expense.payment_method == 1){
|
||||||
|
$('#payment_method').val("Cash");
|
||||||
|
}else if(expense.payment_method == 2){
|
||||||
|
$('#payment_method').val("Online");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 + '">Download Current File</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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$(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('');
|
||||||
|
$('#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>');
|
||||||
});
|
});
|
||||||
$(document).on('click', '#addExpense', function () {
|
formActionUrl = '<?php echo base_url().'addExpense' ?>'; // Set action URL for adding
|
||||||
console.log("1111111111111111111");
|
});
|
||||||
|
|
||||||
var supplier_list = [<?php echo $supplier_list; ?>];
|
|
||||||
supplier_list[0].forEach(function(supplier) {
|
|
||||||
// Append each supplier to the dropdown
|
|
||||||
$('#supplierid').append('<option value="' + supplier.SupplierID + '">' + supplier.SupplierName + '</option>');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
$('#tempClick').on('click', function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
var formData = $('#expenseForm').serialize(); // Serialize form data
|
|
||||||
|
|
||||||
$.ajax({
|
// Handle form submission
|
||||||
url: '<?php echo base_url().'addExpense' ?>', // Your PHP script to save data
|
$('#tempClick').on('click', function(e) {
|
||||||
type: 'POST',
|
e.preventDefault();
|
||||||
data: formData,
|
var formData = new FormData($('#expenseForm')[0]); // Use FormData to handle file upload
|
||||||
success: function(response) {
|
console.log(formActionUrl);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: formActionUrl, // Use the correct URL based on the button clicked
|
||||||
|
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);
|
console.log(response);
|
||||||
|
|
||||||
if (response) {
|
|
||||||
// Close the modal
|
|
||||||
$('#expenseModal').modal('hide');
|
|
||||||
|
|
||||||
// Clear the form
|
|
||||||
$('#expenseForm')[0].reset();
|
|
||||||
|
|
||||||
// Refresh the DataTable
|
|
||||||
window.location.reload();
|
|
||||||
} else {
|
|
||||||
// Handle errors here
|
|
||||||
console.log(response);
|
|
||||||
|
|
||||||
// alert(response.message);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function(xhr, status, error) {
|
|
||||||
// Handle errors here
|
|
||||||
alert('An error occurred: ' + error);
|
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
error: function(xhr, status, error) {
|
||||||
|
alert('An error occurred: ' + error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// DataTable initialization and date range filter logic (unchanged)
|
||||||
|
|
||||||
var table = $('#expense_list_table').DataTable({
|
var table = $('#expense_list_table').DataTable({
|
||||||
dom: 'Blfrtip', // Buttons, length menu, filter, table, information, pagination
|
dom: 'Blfrtip',
|
||||||
buttons: [
|
buttons: [
|
||||||
'copy', 'csv', 'excel', 'pdf', 'print' // Export buttons
|
'copy', 'csv', 'excel', 'pdf', 'print'
|
||||||
],
|
],
|
||||||
pageLength: 10, // Default rows per page
|
pageLength: 10,
|
||||||
lengthMenu: [ [10, 20, 30, 50, -1], [10, 20, 30, 50, "All"] ], // Rows per page options
|
lengthMenu: [ [10, 20, 30, 50, -1], [10, 20, 30, 50, "All"] ],
|
||||||
responsive: true, // Responsive table
|
responsive: true,
|
||||||
order: [[0, 'desc']], // Default ordering (column index 0, descending)
|
order: [[0, 'desc']],
|
||||||
language: {
|
language: {
|
||||||
paginate: {
|
paginate: {
|
||||||
next: '<i class="fas fa-angle-right"></i>', // Next button icon
|
next: '<i class="fas fa-angle-right"></i>',
|
||||||
previous: '<i class="fas fa-angle-left"></i>' // Previous button icon
|
previous: '<i class="fas fa-angle-left"></i>'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -296,50 +361,77 @@
|
|||||||
return true; // If no dates selected, don't filter
|
return true; // If no dates selected, don't filter
|
||||||
}
|
}
|
||||||
|
|
||||||
var dateStr = data[0]; // Assuming the date is in the first column
|
var dateStr = data[0];
|
||||||
var dateParts = dateStr.split("-");
|
var dateParts = dateStr.split("-");
|
||||||
var date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]); // Convert dd-mm-yyyy to Date object
|
var date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
|
||||||
|
|
||||||
var startDate = new Date(fromDate);
|
var startDate = new Date(fromDate);
|
||||||
var endDate = new Date(toDate);
|
var endDate = new Date(toDate);
|
||||||
|
|
||||||
// Adjust startDate to include the day before the selected fromDate
|
|
||||||
startDate.setDate(startDate.getDate() - 1);
|
startDate.setDate(startDate.getDate() - 1);
|
||||||
|
|
||||||
// Ensure endDate includes the entire end date by setting time to the end of the day
|
|
||||||
endDate.setHours(23, 59, 59, 999);
|
endDate.setHours(23, 59, 59, 999);
|
||||||
|
|
||||||
// Return true if the date is within the range
|
|
||||||
return date >= startDate && date <= endDate;
|
return date >= startDate && date <= endDate;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Handle form submission for the date range filter
|
|
||||||
$("#DateRangeFilter").submit(function(e) {
|
$("#DateRangeFilter").submit(function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
table.draw(); // Redraw the table to apply the filter
|
table.draw();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reset button functionality
|
|
||||||
$("#resetButton").click(function() {
|
$("#resetButton").click(function() {
|
||||||
$("#fromDate").val('');
|
$("#fromDate").val('');
|
||||||
$("#toDate").val('');
|
$("#toDate").val('');
|
||||||
table.draw(); // Redraw the table to clear the filter
|
table.draw();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Automatically focus and open the To Date picker when From Date is selected
|
|
||||||
document.getElementById('fromDate').addEventListener('change', function() {
|
document.getElementById('fromDate').addEventListener('change', function() {
|
||||||
var fromDate = this.value;
|
var fromDate = this.value;
|
||||||
var toDateInput = document.getElementById('toDate');
|
var toDateInput = document.getElementById('toDate');
|
||||||
|
|
||||||
// Set the min attribute of the To Date input to the selected From Date
|
|
||||||
toDateInput.min = fromDate;
|
toDateInput.min = fromDate;
|
||||||
|
|
||||||
// Optionally reset the To Date input if the current value is before the new min date
|
|
||||||
if (toDateInput.value < fromDate) {
|
if (toDateInput.value < fromDate) {
|
||||||
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>
|
||||||
|
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.getElementById('transporterfile').addEventListener('change', function() {
|
||||||
|
var fileName = this.files[0].name;
|
||||||
|
document.getElementById('currentFileName').textContent = fileName;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user