228 lines
7.7 KiB
PHP
Executable File
228 lines
7.7 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()
|
|
{
|
|
|
|
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());
|
|
$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);
|
|
}
|
|
|
|
private function generateSerialNumber($financialYear, $lastSerial)
|
|
{
|
|
// If no last serial number exists, start with 00001
|
|
$lastNumber = $lastSerial ? (int)explode('/', $lastSerial)[2] : 0;
|
|
$newNumber = str_pad($lastNumber + 1, 3, '0', STR_PAD_LEFT);
|
|
return "NonIGR/$financialYear/$newNumber";
|
|
}
|
|
|
|
public function addExpense()
|
|
{
|
|
try{
|
|
$file = $this->request->getFile('transporterfile');
|
|
$fileName = '';
|
|
if ($file->isValid() && !$file->hasMoved()) {
|
|
$fileName = $file->getRandomName();
|
|
$file->move(WRITEPATH . 'uploads', $fileName);
|
|
}
|
|
|
|
|
|
|
|
// Fetch the current month
|
|
$currentMonth = date('n'); // 'n' gives the month without leading zeros (1-12)
|
|
|
|
// Calculate the current financial year
|
|
if ($currentMonth >= 4) { // If it's April or later
|
|
$startYear = date('Y'); // Current year
|
|
$endYear = $startYear + 1; // Next year
|
|
} else { // If it's January, February, or March
|
|
$endYear = date('Y'); // Current year
|
|
$startYear = $endYear - 1; // Previous year
|
|
}
|
|
|
|
// Format the financial year as "yy-yy"
|
|
$financialYear = substr($startYear, -2) . '-' . substr($endYear, -2);
|
|
|
|
// Fetch the last serial number for the financial year
|
|
$lastSerial = $this->expense_model->getLastSerialNumber($financialYear);
|
|
|
|
// Generate the next serial number
|
|
$newSerialNumber = $this->generateSerialNumber($financialYear, $lastSerial);
|
|
|
|
|
|
$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'),
|
|
'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
|
|
}
|
|
|
|
|
|
|
|
$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'),
|
|
'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.']);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|