diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 8a1d236e..cf3212bb 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -266,7 +266,10 @@ $routes->post('inwardgateregister/edituploadfile', 'Inwardgateregister::edituplo // Non IGR / Expense $routes->get('ListIGR', 'Expense::index'); $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 diff --git a/app/Controllers/Expense.php b/app/Controllers/Expense.php index efc417dc..7465b323 100644 --- a/app/Controllers/Expense.php +++ b/app/Controllers/Expense.php @@ -3,46 +3,44 @@ namespace App\Controllers; use App\Controllers\BaseController; - use App\Models\Expense_model; use App\Models\Supplier_model; - - class Expense extends BaseController { - protected $expense_model; - protected $supplier_model; + protected $expense_model; + protected $supplier_model; protected $session; - /** - * This is default constructor of the class - */ public function __construct() { parent::__construct(); $this->expense_model = new Expense_model(); $this->supplier_model = new Supplier_model(); - $this->session = session(); - helper('form'); //$this->load->library('form_validation'); - + helper('form'); $this->isLoggedIn(); } + public function index() { $data['expense_list'] = $this->expense_model->getAllExpense(); $data['supplier_list'] = json_encode($this->supplier_model->supplierlisting()); $this->global['pageTitle'] = 'Non IGR / Expense List'; - // echo "
";
-        // print_r($data);die;
         $this->loadViews("expense_list", $this->global, $data, NULL);
     }
 
-    function addExpense()
+    public function addExpense()
     {
-        $data = array(
-            'transporter_file' => $this->request->getPost('transporterfile'),
+        $file = $this->request->getFile('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'),
             'remarks' => $this->request->getPost('remarks'),
             'cost' => $this->request->getPost('cost'),
@@ -50,26 +48,20 @@ class Expense extends BaseController
             'total' => $this->request->getPost('total'),
             'status' => $this->request->getPost('status'),
             'payment_method' => $this->request->getPost('payment_method')
-        );
-
+        ];
         $insert_id = $this->expense_model->insertExpense($data);
 
         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');
         } 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');
         }
     }
 
-    public function getExpenseDetails() {
+    public function getExpenseDetails()
+    {
         $id = $this->request->getPost('id');
-        
+
         if ($id) {
             $expense = $this->expense_model->getExpenseById($id);
             echo json_encode($expense);
@@ -78,28 +70,93 @@ class Expense extends BaseController
         }
     }
 
-    public function updateExpense() {
-        // $id = $this->input->post('id');
-        // $data = [
-        //     'transporterfile' => $this->input->post('transporterfile'),
-        //     'supplierid'      => $this->input->post('supplierid'),
-        //     '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')
-        // ];
+    public function editExpense()
+    {
+        $id = $this->request->getPost('id');
+        $file = $this->request->getFile('transporterfile');
+        $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']);
-        // }
+        if ($file->isValid() && !$file->hasMoved()) {
+            $fileName = $file->getRandomName();
+            $file->move(WRITEPATH . 'uploads', $fileName);
+        } else {
+            $fileName = $this->request->getPost('existing_file'); // Use existing file if no new file is uploaded
+        }
+
+        $data = [
+            '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.']);
+        }
+    }
+    
+
+    
 }
diff --git a/app/Models/Expense_model.php b/app/Models/Expense_model.php
index c3727ee1..24c0a807 100644
--- a/app/Models/Expense_model.php
+++ b/app/Models/Expense_model.php
@@ -9,7 +9,7 @@ class Expense_model extends Model
     protected $primaryKey = 'id'; // Primary key
 
     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
@@ -34,5 +34,12 @@ class Expense_model extends Model
     public function updateExpense($id, $data) {
         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]);
+    }
 }
 ?>
diff --git a/app/Views/assetListing.php b/app/Views/assetListing.php
index 60c106aa..53194795 100644
--- a/app/Views/assetListing.php
+++ b/app/Views/assetListing.php
@@ -69,14 +69,7 @@
                                     
- - - - - - - -
+ diff --git a/app/Views/expense_list.php b/app/Views/expense_list.php index 2c21c86e..ffa2da6d 100644 --- a/app/Views/expense_list.php +++ b/app/Views/expense_list.php @@ -37,6 +37,11 @@ .icon-button:hover { color: #0056b3; } + .delete_button{ + border: none; + background-color: white; + color: red; + }
@@ -74,13 +79,13 @@
+ - - + @@ -95,17 +100,13 @@ $cdate = date('d-m-Y', strtotime($record['created_on'])); } ?> + -
Created DateExpense Id Supplier Name Cost GST Total Statuspayment_methodAction Payment Method
- -