expense_model = new Expense_model(); $this->supplier_model = new Supplier_model(); $this->session = session(); $this->session = session(); helper('form'); $this->isLoggedIn(); } public function index() { $isActiveFilter = 1 ; if ($this->request->getMethod() === 'GET') { $toDate = date('Y-m-d'); $fromDate = date('Y-m-d', strtotime('-90 days', strtotime($toDate))); } else if ($this->request->getMethod() === 'POST') { $fromDate = $this->request->getPost('fromDate'); $toDate = $this->request->getPost('toDate'); $fromDate = date('Y-m-d', strtotime($fromDate)); $toDate = date('Y-m-d', strtotime($toDate)); } $data['expense_list'] = $this->expense_model->getAllExpense($fromDate,$toDate); $data['supplier_list'] = json_encode($this->supplier_model->supplierlisting($isActiveFilter)); $this->global['pageTitle'] = 'Non IGR / Expense List'; $data['fromDate'] = date('d-m-Y', strtotime($fromDate)); $data['toDate'] = date('d-m-Y', strtotime($toDate)); $this->loadViews("expense_list", $this->global, $data, NULL); } public function addExpense() { helper('numberseries'); try{ $file = $this->request->getFile('transporterfile'); $fileName = ''; if ($file->isValid() && !$file->hasMoved()) { $fileName = $file->getRandomName(); $file->move(WRITEPATH . 'uploads', $fileName); } // Fetch the last serial number for the financial year $lastSerial = $this->expense_model->getLastSerialNumber(); // Generate the next serial number $newSerialNumber = generate_non_igr_number($lastSerial); $sesn_uid = $this->session->get('userId'); $data = [ 'created_by' => $sesn_uid, 'transporter_file' => $fileName, 'supplier_id' => $this->request->getPost('supplierid'), 'remarks' => $this->request->getPost('remarks'), 'item_description' => $this->request->getPost('item_description'), 'cost' => $this->request->getPost('cost'), 'cgst' => $this->request->getPost('cgst'), 'sgst' => $this->request->getPost('sgst'), 'igst' => $this->request->getPost('igst'), 'total' => $this->request->getPost('total'), 'bill_no' => $this->request->getPost('bill_no'), 'bill_date' => $this->request->getPost('bill_date'), 'status' => $this->request->getPost('status'), 'serial_number' => $newSerialNumber, ]; $insert_id = $this->expense_model->insertExpense($data); if ($insert_id) { return json_encode('true'); } else { return json_encode('false'); } } catch (\Exception $e) { // Handle the exception echo "Error: " . $e->getMessage(); } } public function getExpenseDetails() { $id = $this->request->getPost('id'); if ($id) { $expense = $this->expense_model->getExpenseById($id); echo json_encode($expense); } else { echo json_encode(['error' => 'Invalid ID']); } } public function editExpense() { $id = $this->request->getPost('id'); $file = $this->request->getFile('transporterfile'); $fileName = ''; 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 } $sesn_uid = $this->session->get('userId'); $data = [ 'updated_by' =>$sesn_uid, 'supplier_id' => $this->request->getPost('supplierid'), 'remarks' => $this->request->getPost('remarks'), 'item_description' => $this->request->getPost('item_description'), 'cost' => $this->request->getPost('cost'), 'cgst' => $this->request->getPost('cgst'), 'sgst' => $this->request->getPost('sgst'), 'igst' => $this->request->getPost('igst'), 'total' => $this->request->getPost('total'), 'bill_no' => $this->request->getPost('bill_no'), 'bill_date' => $this->request->getPost('bill_date'), 'status' => $this->request->getPost('status') ]; 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.']); } } }