198 lines
6.2 KiB
PHP
198 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\BusinessModel;
|
|
|
|
class Business extends BaseController
|
|
{
|
|
|
|
## For Business Listing
|
|
public function index()
|
|
{
|
|
helper('session');
|
|
if (is_session_active()) {
|
|
$session_role = get_user_role();
|
|
if (!empty($session_role) && $session_role !== "sadmin") {
|
|
$this->logger->info("Buiness: Listing In admin role .");
|
|
$where = ['business_id' => (int)get_business_id(), 'isactive' => 1];
|
|
} else {
|
|
$this->logger->info("Buiness: Listing In Super-admin role .");
|
|
$where = ['isactive !=' => NULL];
|
|
}
|
|
$BusinessModel = new BusinessModel();
|
|
$data['page_name'] = 'Organization Details';
|
|
$data['businesses'] = $BusinessModel->where($where)->where('business_id !=', 0)->where('isactive', 1)->findAll();
|
|
// $data['lastQuery'] = $BusinessModel->getLastQuery();
|
|
// print_r($data);die;
|
|
$this->render_page('business_list', $data);
|
|
} else {
|
|
return redirect()->to('login');
|
|
}
|
|
}
|
|
|
|
## To Load Business Form (Add/Update)
|
|
public function new_org($id)
|
|
{
|
|
helper('session');
|
|
$session_role = get_user_role();
|
|
|
|
if ($id === '0') {
|
|
$data['page_name'] = 'Add Organization';
|
|
$data['loged_user'] = $session_role;
|
|
$data['businesses'] = [];
|
|
$data['branches'] = [];
|
|
} else if ($id !== '0') {
|
|
$data['page_name'] = 'Edit Organization';
|
|
|
|
$model = new BusinessModel();
|
|
$model->setTable('business');
|
|
|
|
// Fetch business details
|
|
$edit_user_details = $model->where(['business_id ' => $id])->first();
|
|
$data['businesses'] = $edit_user_details;
|
|
|
|
// Fetch branch details
|
|
$branchData = $model->getBranchesByBusinessId($id);
|
|
$data['branches'] = $branchData;
|
|
}
|
|
|
|
$this->render_page('business_form', $data);
|
|
}
|
|
|
|
|
|
## For inserting/updating details of business
|
|
public function insert_org()
|
|
{
|
|
helper('session');
|
|
$session_uid = get_logged_user_id();
|
|
$session_role = get_user_role();
|
|
|
|
$img = $this->request->getFile('bfile');
|
|
$filePath = 'public/uploads/' . $this->request->getPost('bfile');
|
|
$fileName = $img->getName();
|
|
|
|
if ($fileName !== "") {
|
|
if ($img->isValid() && !$img->hasMoved()) {
|
|
$img->move(ROOTPATH . 'public/uploads', $fileName);
|
|
}
|
|
}
|
|
|
|
$sign = $this->request->getFile('signature');
|
|
$signFileName = $sign->getName();
|
|
|
|
if ($signFileName !== "") {
|
|
if ($sign->isValid() && !$sign->hasMoved()) {
|
|
$sign->move(ROOTPATH . 'public/uploads', $signFileName);
|
|
}
|
|
}
|
|
|
|
$BusinessModel = new BusinessModel();
|
|
$data = [
|
|
'title' => $this->request->getPost('bname'),
|
|
'email' => $this->request->getPost('bmail'),
|
|
'mobile_no' => $this->request->getPost('bmobile'),
|
|
'address' => $this->request->getPost('baddress'),
|
|
'city' => $this->request->getPost('bcity'),
|
|
'state' => $this->request->getPost('bstate'),
|
|
'postal_code' => $this->request->getPost('bzip'),
|
|
'pan_no' => $this->request->getPost('pan_no'),
|
|
'org_reg_no' => $this->request->getPost('org_reg_no'),
|
|
'terms' => $this->request->getPost('bterms'),
|
|
'80G' => $this->request->getPost('80G'),
|
|
];
|
|
|
|
if ($fileName !== "") {
|
|
$data['business_logo'] = $fileName;
|
|
}
|
|
if ($signFileName !== "") {
|
|
$data['signature'] = $signFileName;
|
|
}
|
|
|
|
$business_id = $this->request->getPost('business_id'); // Get the business ID for update
|
|
|
|
if (empty($business_id)) {
|
|
// It's an insert operation
|
|
$data['isactive'] = 1;
|
|
$data['created_by'] = $session_uid;
|
|
$businessId = $BusinessModel->insert($data);
|
|
// $this->insert_branch($businessId);
|
|
} else {
|
|
// It's an update operation
|
|
if ($session_role === 'sadmin') {
|
|
$isactive = $this->request->getPost('bcheckbox');
|
|
$data['isactive'] = ($isactive == 'on') ? 1 : 0;
|
|
}
|
|
$data['updated_by'] = $session_uid;
|
|
$BusinessModel->update($business_id, $data);
|
|
$businessId = $business_id;
|
|
}
|
|
|
|
// ... existing code ...
|
|
|
|
if (!empty($_POST['branchname'])) {
|
|
$branchNames = $_POST['branchname'];
|
|
$branchMobile = $_POST['mobile_no'];
|
|
$branchMail = $_POST['email'];
|
|
$branchAddress = $_POST['branchaddress'];
|
|
$branchIds = isset($_POST['id']) ? $_POST['id'] : [];
|
|
|
|
foreach ($branchNames as $key => $branchName) {
|
|
$branchId = isset($branchIds[$key]) ? $branchIds[$key] : null;
|
|
|
|
$branchData = [
|
|
'business_id' => $businessId,
|
|
'branch_name' => $branchName,
|
|
'mobile_no' => $branchMobile[$key],
|
|
'email' => $branchMail[$key],
|
|
'address' => $branchAddress[$key],
|
|
];
|
|
|
|
if (!empty($branchId)) {
|
|
// Update the existing branch
|
|
$db = \Config\Database::connect();
|
|
$db->table('business_branches')
|
|
->where('id', $branchId)
|
|
->update($branchData);
|
|
} else {
|
|
// Insert new branch
|
|
$db = \Config\Database::connect();
|
|
$db->table('business_branches')->insert($branchData);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// ... exis
|
|
// ... existing code ...
|
|
|
|
// Redirect based on user role
|
|
if ($session_role === 'sadmin') {
|
|
return redirect()->route('org_list');
|
|
} else {
|
|
return redirect()->route('dashboard');
|
|
}
|
|
}
|
|
|
|
## For delete the business details (Which means inactive the details)
|
|
public function delete_org($id)
|
|
{
|
|
helper('session');
|
|
$session_uid = get_logged_user_id();
|
|
$BusinessModel = new BusinessModel();
|
|
|
|
// Check if the business ID exists
|
|
// $existingBusiness = $BusinessModel->find($id);
|
|
// if (!$existingBusiness) {
|
|
// return redirect()->route('org_list');
|
|
// }
|
|
|
|
// Delete the business record
|
|
$data['isactive'] = 0;
|
|
$data['updated_by'] = $session_uid;
|
|
$BusinessModel->update($id, $data);
|
|
|
|
return redirect()->route('org_list');
|
|
}
|
|
}
|