347 lines
14 KiB
PHP
347 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\BusinessModel;
|
|
use CodeIgniter\Database\Exceptions\DatabaseException;
|
|
|
|
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 = ['business_id !=' => 0,'isactive !=' => NULL];
|
|
}
|
|
$BusinessModel = new BusinessModel();
|
|
$data['page_name'] = 'Organization Details';
|
|
$data['organzations'] = $BusinessModel->where($where)->findAll();
|
|
// $data['lastQuery'] = $BusinessModel->getLastQuery();
|
|
// print_r($data);die;
|
|
$this->render_page('business_list', $data);
|
|
} else {
|
|
return redirect()->to('login');
|
|
}
|
|
}
|
|
|
|
## For Organization 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['organzations'] = [];
|
|
$data['branches'] = [];
|
|
$data['donation']=[];
|
|
} else if ($id !== '0') {
|
|
$data['page_name'] = 'Edit Organization';
|
|
$data['loged_user'] = $session_role;
|
|
$model = new BusinessModel();
|
|
$model->setTable('business');
|
|
|
|
## Fetch business details (particular business)
|
|
$edit_organzation_details = $model->where(['business_id ' => $id])->first();
|
|
$data['organzations'] = $edit_organzation_details;
|
|
|
|
## Fetch branch details
|
|
$branch_details = $model->getBranchesByBusinessId($id);
|
|
$data['branches'] = $branch_details;
|
|
|
|
## Fetch donation details
|
|
if ($session_role !== 'sadmin') {
|
|
$donation_accepted=$model->getDonationBusinessData($id);
|
|
$data['donation']=$donation_accepted;
|
|
}
|
|
|
|
}
|
|
|
|
$data['master_organzation_details']=$this->organzation_details();
|
|
$data['session_bid']=$session_bid;
|
|
$data['country_details'] = $this->get_country_details();
|
|
// print_r($data['donation']);die;
|
|
$this->render_page('business_form', $data);
|
|
}
|
|
|
|
public function organzation_details()
|
|
{
|
|
helper('session');
|
|
$session_role = get_user_role();
|
|
$session_bid = get_business_id();
|
|
|
|
$model = new BusinessModel();
|
|
$model->setTable('business');
|
|
if($session_role === 'sadmin'){
|
|
|
|
$organzation_details = $model->where('business_id !=', 0)->orderBy('business_id', 'DESC')->findAll();
|
|
}
|
|
else{
|
|
$organzation_details = $model->where('business_id', $session_bid)->findAll();
|
|
|
|
}
|
|
return $organzation_details;
|
|
}
|
|
|
|
|
|
## For inserting/updating details of business
|
|
public function insert_org()
|
|
{
|
|
helper('session');
|
|
$session_uid = get_logged_user_id();
|
|
$session_role = get_user_role();
|
|
$panNo = $this->request->getPost('pan_no');
|
|
$BusinessModel = new BusinessModel();
|
|
$businessId = $this->request->getPost('business_id');
|
|
|
|
$existingBusiness = $BusinessModel->where('pan_no', $panNo)->first();
|
|
|
|
if ($existingBusiness && $existingBusiness['business_id'] != $businessId) {
|
|
// If it exists and it's not the same record being updated, show an error message
|
|
session()->setFlashdata('error', 'The PAN number already exists.');
|
|
return redirect()->back()->withInput(); // Redirect back with input data
|
|
}
|
|
|
|
try {
|
|
## logo
|
|
$img = $this->request->getFile('business_logo');
|
|
$filePath = 'public/uploads/' . $this->request->getPost('business_logo');
|
|
$fileName = $img->getName();
|
|
if ($fileName !== "") {
|
|
if ($img->isValid() && !$img->hasMoved()) {
|
|
$img->move(ROOTPATH . 'public/uploads', $fileName);
|
|
}
|
|
}
|
|
|
|
## favicon
|
|
$favicon = $this->request->getFile('favicon');
|
|
$faviconFileName = $favicon->getName();
|
|
if ($faviconFileName !== "") {
|
|
if ($favicon->isValid() && !$favicon->hasMoved()) {
|
|
$favicon->move(ROOTPATH . 'public/uploads', $faviconFileName);
|
|
}
|
|
}
|
|
|
|
## signature
|
|
$sign = $this->request->getFile('signature');
|
|
$signFileName = $sign->getName();
|
|
if ($signFileName !== "") {
|
|
if ($sign->isValid() && !$sign->hasMoved()) {
|
|
$sign->move(ROOTPATH . 'public/uploads', $signFileName);
|
|
}
|
|
}
|
|
|
|
## Business Data
|
|
// print_r( $this->request->getPost('currency'));
|
|
|
|
$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'),
|
|
'12AA' => $this->request->getPost('12AA'),
|
|
'80G_vaildupto' => $this->request->getPost('80G_vaildupto'),
|
|
'12AA_vaildupto' => $this->request->getPost('12AA_vaildupto'),
|
|
'site_name' => $this->request->getPost('site_name'),
|
|
'site_title' => $this->request->getPost('site_title'),
|
|
'admin_email' => $this->request->getPost('admin_email'),
|
|
'site_mobile'=> $this->request->getPost('site_mobile'),
|
|
'copyright'=> $this->request->getPost('copyright'),
|
|
'currency'=> serialize($this->request->getPost('currency')),
|
|
'country'=> $this->request->getPost('country'),
|
|
'start_no'=> $this->request->getPost('start_no'),
|
|
'left_pad'=> $this->request->getPost('left_pad'),
|
|
'prefix_format'=> $this->request->getPost('prefix_format'),
|
|
];
|
|
if ($fileName !== "") {
|
|
$data['business_logo'] = $fileName;
|
|
}
|
|
if ($faviconFileName !== "") {
|
|
$data['favicon'] = $faviconFileName;
|
|
}
|
|
if ($signFileName !== "") {
|
|
$data['signature'] = $signFileName;
|
|
}
|
|
|
|
$business_id = $this->request->getPost('business_id'); // Get the business ID for update
|
|
// print_r($data);die;
|
|
if (empty($business_id)) {
|
|
// It's an insert operation
|
|
$data['created_by'] = $session_uid;
|
|
$businessId = $BusinessModel->insert($data);
|
|
session()->setFlashdata('success', 'Organization details have been updated successfully.');
|
|
} else {
|
|
// It's an update operation
|
|
$businessId = $business_id;
|
|
$data['updated_by'] = $session_uid;
|
|
$BusinessModel->update($business_id, $data);
|
|
session()->setFlashdata('success', 'Organization details have been updated successfully.');
|
|
}
|
|
|
|
## Donation Business Data (Note: not a super-admin means just updated only)
|
|
if ($session_role !== 'sadmin') {
|
|
$selectedDonations = $this->request->getPost('name');
|
|
$BusinessModel->updateDonationData($businessId, $selectedDonations);
|
|
}else if ($session_role === 'sadmin' && $businessId) {
|
|
$BusinessModel->insertDonationBusiness($businessId);
|
|
}
|
|
|
|
## Buiness Branch Data
|
|
if(!empty($_POST['branch_name'])){
|
|
$requestData = $this->request->getPost();
|
|
$bb_result = $this->save_business_branch($requestData, $businessId);
|
|
$this->logger->info("business branch: final message = " .implode(" ",$bb_result));
|
|
}
|
|
|
|
if ($session_role === 'sadmin') {
|
|
return redirect()->route('org_list');
|
|
} else {
|
|
return redirect()->to(base_url("new_org/{$business_id}"));
|
|
}
|
|
} catch (DatabaseException $e) {
|
|
session()->setFlashdata('error', $e->getMessage());
|
|
return redirect()->back()->withInput(); // Redirect back with input data
|
|
}
|
|
}
|
|
|
|
public function save_business_branch($requestData, $bid)
|
|
{
|
|
try {
|
|
|
|
$getBuinessBranchdetails = $this->get_business_branch($bid);
|
|
|
|
|
|
$BBprimaryid = $requestData['id'] ; // Available Buiness Branch Primary IDS in Form Fields.
|
|
|
|
|
|
$model = new BusinessModel();
|
|
|
|
## IF Any Missing value Means that values are Inactive here....
|
|
if(!empty($BBprimaryid)){
|
|
$this->logger->info("BuinessBranch: Primary ID = ".implode(",",$BBprimaryid));
|
|
$filteringIds = [];
|
|
for ($y = 0; $y < count($getBuinessBranchdetails); $y++) {
|
|
$filteringIds[$y] = $getBuinessBranchdetails[$y]['id'];
|
|
}
|
|
|
|
|
|
if (!empty($filteringIds)) {
|
|
$A = $filteringIds;
|
|
$B = $BBprimaryid;
|
|
$missingValues = array_diff($A, $B);
|
|
if (!empty($missingValues)) {
|
|
$where = ['isactive' => 1, 'business_id'=>$bid];
|
|
$model->inactiveMissingBuinessBranchDetails($where, $missingValues);
|
|
$this->logger->info("BuinessBranch: Inactived Missing Count = " . count($missingValues)." That Primary ID = ".implode(",",$BBprimaryid));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$count = count($BBprimaryid);
|
|
|
|
$branchData = [];
|
|
if ($count > 0) {
|
|
for ($x = 0; $x < $count; $x++) {
|
|
$branchData[$x]['id'] = $requestData['id'][$x];
|
|
$branchData[$x]['branch_name'] = $requestData['branch_name'][$x];
|
|
$branchData[$x]['business_id'] = $bid;
|
|
$branchData[$x]['email'] = $requestData['email'][$x];
|
|
$branchData[$x]['mobile_no'] = $requestData['mobile_no'][$x];
|
|
$branchData[$x]['address'] = $requestData['branchaddress'][$x];
|
|
}
|
|
|
|
$statement = !empty($branchData) ? $model->saveBuinessBranchDetails($branchData) : ["No data found For buisness branch"];
|
|
$this->logger->info("BuinessBranch: Final message = " . implode(",",$statement));
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("BuinessBranch: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine());
|
|
$statement = ['Message: ' . $e->getMessage()];
|
|
}
|
|
return $statement;
|
|
}
|
|
|
|
public function get_business_branch($bid){
|
|
$model = new BusinessModel();
|
|
$model->setTable('business_branches');
|
|
$where = ['business_id'=> (int)$bid , 'isactive' => 1];
|
|
$details = $model->where($where)->findAll();
|
|
return $details;
|
|
}
|
|
|
|
## 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 activate_org($id){
|
|
helper('session');
|
|
$session_uid = get_logged_user_id();
|
|
$BusinessModel = new BusinessModel();
|
|
|
|
$data['isactive'] = 1;
|
|
$data['updated_by'] = $session_uid;
|
|
$BusinessModel->update($id, $data);
|
|
|
|
return redirect()->route('org_list');
|
|
}
|
|
public function insertDonationBusiness($businessId)
|
|
{
|
|
$db = \Config\Database::connect();
|
|
$donationData = $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
|
|
$db->table('donation_business')->insertBatch($donationData);
|
|
}
|
|
}
|
|
|
|
public function get_country_details()
|
|
{
|
|
$model = new BusinessModel();
|
|
$model->setTable('countries');
|
|
$country_details = $model->orderBy('country_id', 'ASC')->findAll();
|
|
return $country_details;
|
|
}
|
|
}
|