372 lines
12 KiB
PHP
372 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\BusinessModel;
|
|
use App\Models\SettingsModel;
|
|
|
|
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();
|
|
$session_bid = get_business_id();
|
|
$session_uid = get_logged_user_id();
|
|
|
|
if ($id === '0') {
|
|
$data['page_name'] = 'Add Organization';
|
|
$data['loged_user'] = $session_role;
|
|
$data['businesses'] = [];
|
|
$data['branches'] = [];
|
|
$data['donation']=[];
|
|
// -----------
|
|
$data['details'] = [];
|
|
} else if ($id !== '0') {
|
|
$data['page_name'] = 'Edit Organization';
|
|
$data['loged_user'] = $session_role;
|
|
$model = new BusinessModel();
|
|
$model->setTable('business');
|
|
|
|
// Fetch business details
|
|
$edit_user_details = $model->where(['business_id ' => $id])->first();
|
|
$data['businesses'] = $edit_user_details;
|
|
if ($session_role !== 'sadmin') {
|
|
|
|
$donationaccepted=$model->getDonationBusinessData($id);
|
|
$data['donation']=$donationaccepted;
|
|
// dd($data['donation']);die;
|
|
}
|
|
// Fetch branch details
|
|
$branchData = $model->getBranchesByBusinessId($id);
|
|
|
|
$data['branches'] = $branchData;
|
|
// ============================
|
|
$smodel = new SettingsModel();
|
|
$smodel->setTable('settings');
|
|
if($session_role === 'sadmin'){
|
|
|
|
$details = $smodel->where(['setting_id' => $id, 'isactive' => 1])->first();
|
|
}
|
|
else{
|
|
$details = $smodel->where(['business_id' => $session_bid, 'isactive' => 1])->first();
|
|
|
|
}
|
|
// echo json_decode($details);die;
|
|
$data['details'] = $details;
|
|
// echo json_encode($data);die;
|
|
|
|
}
|
|
$data['business_details']=$this->business_details();
|
|
$data['session_bid']=$session_bid;
|
|
$this->render_page('business_form', $data);
|
|
}
|
|
|
|
public function business_details()
|
|
{
|
|
helper('session');
|
|
$session_role = get_user_role();
|
|
$session_bid = get_business_id();
|
|
|
|
$model = new BusinessModel();
|
|
$model->setTable('business');
|
|
if($session_role === 'sadmin'){
|
|
|
|
$business_details = $model->where('business_id !=', 0)->orderBy('business_id', 'DESC')->findAll();
|
|
}
|
|
else{
|
|
$business_details = $model->where('business_id', $session_bid)->findAll();
|
|
|
|
}
|
|
return $business_details;
|
|
}
|
|
|
|
|
|
## 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('slogo');
|
|
$filePath = 'public/uploads/' . $this->request->getPost('slogo');
|
|
$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);
|
|
$BusinessModel->insertDonationBusiness($businessId);
|
|
// $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;
|
|
$selectedDonations = $this->request->getPost('name');
|
|
$BusinessModel->updateDonationData($businessId, $selectedDonations);
|
|
|
|
}
|
|
|
|
// ... 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
//SITE SETTINGS
|
|
helper('session');
|
|
$session_bid = get_business_id();
|
|
$session_uid = get_logged_user_id();
|
|
$session_role = get_user_role();
|
|
|
|
$img = $this->request->getFile('slogo');
|
|
// print_r($img);
|
|
$business_id = $this->request->getPost('business_id');
|
|
$app_setting_id = $this->request->getPost('app_setting_id');
|
|
$filePath = 'public/uploads/' . $this->request->getPost('slogo');
|
|
$fileName = $img->getName();
|
|
// var_dump($fileName);
|
|
// echo $fileName;
|
|
// echo $fileName !== "" && $fileName !== null ? "Yes":"No";
|
|
// die();
|
|
|
|
if($fileName !== ""){
|
|
|
|
if ($img->isValid() && !$img->hasMoved()) {
|
|
|
|
$img->move(ROOTPATH . 'public/uploads', $fileName);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
$favicon = $this->request->getFile('favicon');
|
|
$faviconName = $favicon->getName();
|
|
|
|
if($faviconName !== ""){
|
|
|
|
if ($favicon->isValid() && !$favicon->hasMoved()) {
|
|
|
|
$favicon->move(ROOTPATH . 'public/uploads', $faviconName);
|
|
}
|
|
}
|
|
|
|
$SettingsModel = new SettingsModel();
|
|
$data = [
|
|
|
|
'site_name' => $this->request->getPost('site_name'),
|
|
'site_title' => $this->request->getPost('site_title'),
|
|
'terms_service' => $this->request->getPost('terms_service'),
|
|
'footer_about' => $this->request->getPost('footer_about'),
|
|
'admin_email' => $this->request->getPost('admin_email'),
|
|
'mobile'=> $this->request->getPost('mobile'),
|
|
'copyright'=> $this->request->getPost('copyright'),
|
|
'pagination_limit'=> $this->request->getPost('pagination_limit'),
|
|
'site_info'=> $this->request->getPost('site_info'),
|
|
'about_info'=> $this->request->getPost('about_info'),
|
|
'mail_protocol'=> $this->request->getPost('mail_protocol'),
|
|
'mail_title'=> $this->request->getPost('mail_title'),
|
|
'mail_host'=> $this->request->getPost('mail_host'),
|
|
'mail_port'=> $this->request->getPost('mail_port'),
|
|
'mail_encryption'=> $this->request->getPost('mail_encryption'),
|
|
'mail_username'=> $this->request->getPost('mail_username'),
|
|
'mail_password'=> $this->request->getPost('mail_password'),
|
|
'currency'=> $this->request->getPost('currency'),
|
|
'country'=> $this->request->getPost('country'),
|
|
'business_id'=> $this->request->getPost('business_id'),
|
|
'start_no'=> $this->request->getPost('start_no'),
|
|
'left_pad'=> $this->request->getPost('left_pad'),
|
|
'prefix_format'=> $this->request->getPost('prefix_format'),
|
|
];
|
|
|
|
if($fileName !== "" && $fileName !== null)
|
|
{
|
|
$data['logo'] = $fileName;
|
|
}
|
|
|
|
if($faviconName !== "" && $faviconName !== null)
|
|
{
|
|
$data['favicon'] = $faviconName;
|
|
}
|
|
|
|
// echo '<pre>';
|
|
// print_r($data); die;
|
|
|
|
$setting_id = $this->request->getPost('setting_id'); // Get the business ID for update
|
|
|
|
if (empty($setting_id)) {
|
|
// It's an insert operation
|
|
$data['isactive'] = 1;
|
|
$data['created_by'] = $session_uid;
|
|
|
|
$SettingsModel->insert($data);
|
|
// print_r($data);die;
|
|
} else {
|
|
// It's an update operation
|
|
$isactive = $this->request->getPost('isactive');
|
|
$data['isactive'] = ($isactive == 'on') ? 1 : 0;
|
|
$data['updated_by'] = $session_uid;
|
|
$SettingsModel->update($setting_id, $data);
|
|
}
|
|
|
|
|
|
// ... exis
|
|
// ... existing code ...
|
|
|
|
// Redirect based on user role
|
|
// ... existing code ...
|
|
|
|
// Redirect based on user role
|
|
if ($session_role === 'sadmin') {
|
|
if (empty($business_id)) {
|
|
// Redirect to the organization list after inserting a new organization
|
|
return redirect()->route('org_list');
|
|
}
|
|
} else {
|
|
session()->setFlashdata('success', 'User has been updated successfully.');
|
|
// Assuming 'index' is the route name for your index page
|
|
return redirect()->to(base_url("new_org/{$business_id}"));
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
## 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');
|
|
}
|
|
public function insertDonationBusiness($businessId)
|
|
{
|
|
$donationData = $this->db->table('donation_accepted')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
if (!empty($donationData)) {
|
|
// Add business_id to each donation record
|
|
foreach ($donationData as &$donation) {
|
|
$donation['business_id'] = $businessId;
|
|
}
|
|
|
|
// Insert data into donation_business table
|
|
$this->db->table('donation_business')->insertBatch($donationData);
|
|
}
|
|
}
|
|
}
|