172 lines
5.5 KiB
PHP
Executable File
172 lines
5.5 KiB
PHP
Executable File
<?php
|
|
|
|
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 $session;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->expense_model = new Expense_model();
|
|
$this->supplier_model = new Supplier_model();
|
|
$this->session = session();
|
|
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';
|
|
$this->loadViews("expense_list", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function addExpense()
|
|
{
|
|
$file = $this->request->getFile('transporterfile');
|
|
$fileName = '';
|
|
if ($file->isValid() && !$file->hasMoved()) {
|
|
$fileName = $file->getRandomName();
|
|
$file->move(WRITEPATH . 'uploads', $fileName);
|
|
}
|
|
|
|
$status = $this->request->getPost('status');
|
|
$paymentMethod = $status === 'Paid' ? $this->request->getPost('payment_method') : 0;
|
|
|
|
$data = [
|
|
'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'),
|
|
];
|
|
|
|
$insert_id = $this->expense_model->insertExpense($data);
|
|
if ($insert_id) {
|
|
return json_encode('true');
|
|
} else {
|
|
return json_encode('false');
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
$status = $this->request->getPost('status');
|
|
$paymentMethod = $status === 'Paid' ? $this->request->getPost('payment_method') : 0;
|
|
|
|
$data = [
|
|
'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'),
|
|
];
|
|
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.']);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|