226 lines
6.8 KiB
PHP
226 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\BusinessModel;
|
|
use App\Models\BranchModel;
|
|
use App\Models\StaffModel;
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
|
|
|
class BusinessController extends BaseController
|
|
{
|
|
public $session;
|
|
protected $BusinessModel;
|
|
protected $BranchModel;
|
|
protected $StaffModel;
|
|
|
|
use ResponseTrait;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->session = session();
|
|
$this->BusinessModel = new BusinessModel();
|
|
$this->BranchModel = new BranchModel();
|
|
$this->StaffModel = new StaffModel();
|
|
|
|
}
|
|
|
|
public function business_list()
|
|
{
|
|
$data['business'] = $this->BusinessModel->findAll();
|
|
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
|
|
|
|
echo view('layout/header', ['Data'=>$staffdata]);
|
|
echo view('business_list',$data);
|
|
echo view('layout/footer');
|
|
|
|
}
|
|
|
|
|
|
public function new_business($business_id)
|
|
{
|
|
|
|
|
|
if ($business_id === '0') {
|
|
$data['business'] = [];
|
|
$data['page_name']="Create Business";
|
|
} else if ($business_id !== '0') {
|
|
|
|
$BusinessModel = new BusinessModel();
|
|
$business=$BusinessModel->getBusinessById($business_id);
|
|
$data['business']=$business;
|
|
$data['page_name']="Edit Business" ;
|
|
}
|
|
$data['business_id'] = $business_id;
|
|
|
|
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
|
|
|
|
echo view('layout/header', ['Data'=>$staffdata]);
|
|
echo view('business_form',$data);
|
|
echo view('layout/footer');
|
|
|
|
}
|
|
public function new_branch($business_id)
|
|
{
|
|
|
|
|
|
if ($business_id === '0') {
|
|
|
|
$data['business'] = [];
|
|
$data['page_name']="Add Branch";
|
|
} else if ($business_id !== '0') {
|
|
|
|
$BusinessModel = new BusinessModel();
|
|
$business=$BusinessModel->getBusinessById($business_id);
|
|
$data['business']=$business;
|
|
$data['page_name']="Edit Branch" ;
|
|
}
|
|
$data['business_id'] = $business_id;
|
|
|
|
return view('branch_form',$data);
|
|
}
|
|
|
|
|
|
public function add_business()
|
|
{
|
|
$BusinessModel = new BusinessModel();
|
|
|
|
|
|
$business_id = $this->request->getPost('business_id');
|
|
$data = $this->request->getPost();
|
|
|
|
|
|
if ($business_id != '') {
|
|
|
|
$BusinessModel->update($business_id, $data);
|
|
return $this->response->setJSON(['success' => true ]);
|
|
|
|
} else {
|
|
|
|
$BusinessModel->insert($data);
|
|
return $this->response->setJSON(['success' => true ]);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public function delete_business($business_id)
|
|
{
|
|
$model = new BusinessModel();
|
|
$where = ['business_id' => $business_id, 'isactive' => 1]; // Assuming 'isactive' is a column in your database
|
|
$existingBusiness = $model->where($where)->first();
|
|
|
|
if ($existingBusiness) {
|
|
$data['isactive'] = 0;
|
|
|
|
if ($model->update($business_id, $data)) {
|
|
session()->setFlashdata('success', 'Deleted successfully.');
|
|
$this->logger->info("Users: has been Inactivated successfully. Inactivated ID = ".$business_id);
|
|
}
|
|
}
|
|
|
|
|
|
return redirect()->to('business_list');
|
|
}
|
|
|
|
public function get_business($business_id)
|
|
{
|
|
if ($business_id) {
|
|
$BusinessModel = new BusinessModel();
|
|
$business =$BusinessModel->where('business_id',$business_id)->first();
|
|
|
|
return json_encode($business);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function business()
|
|
{
|
|
$branch_id =session()->get("logged_in_staff_branch_id");
|
|
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
|
|
if ($branch_id) {
|
|
// $BusinessModel = new BusinessModel();
|
|
$BranchModel = new BranchModel();
|
|
$data['branch'] =$BranchModel->select('branches.*, business.*')
|
|
->join('business', 'business.business_id = branches.business_id')
|
|
->where('branches.branch_id',$branch_id)
|
|
->first();
|
|
|
|
echo view('layout/header', ['Data'=>$staffdata]);
|
|
echo view('business',$data);
|
|
echo view('layout/footer');
|
|
}
|
|
}
|
|
|
|
|
|
public function business_edit()
|
|
{
|
|
$uploadFilePath = ROOTPATH . 'public/uploads/';
|
|
$file_name = $this->file_Upload($this->request->getFile('business_logo'), $uploadFilePath);
|
|
$data = $this->request->getPost();
|
|
$data['logo'] = $file_name;
|
|
|
|
$business_id = $this->request->getPost('business_id');
|
|
|
|
if($business_id){
|
|
$update = $this->BusinessModel->update($business_id, $data);
|
|
}else{
|
|
$insert = $this->BusinessModel->insert($data);
|
|
}
|
|
|
|
return json_encode(true);
|
|
|
|
// if($insert){
|
|
// $client_data = $this->clientModel->where(['id' => $insert, 'is_active' => 1])->first();
|
|
// return $this->respond(['status' => true,'code' => 200,'data' => $client_data], 200);
|
|
|
|
// }else{
|
|
// return $this->respond(['status' => false,'code' => 404,'message' => 'no data found'], 200);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
public function file_Upload($fileToUpload, $filepath)
|
|
{
|
|
if ($fileToUpload !== null && $fileToUpload->isValid() && !$fileToUpload->hasMoved()) {
|
|
$fileName = $fileToUpload->getName();
|
|
$fileToUpload->move($filepath, $fileName);
|
|
return $fileName;
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
|
|
public function status_business()
|
|
{
|
|
try {
|
|
$business_id = $this->request->getPost('business_id');
|
|
$isactive = $this->request->getPost('isactive');
|
|
$data = ['isactive' => $isactive];
|
|
|
|
$updated = $this->BusinessModel->update($business_id, $data);
|
|
if ($updated) {
|
|
$BranchList = $this->BranchModel->where('business_id', $business_id)->findAll();
|
|
foreach ($BranchList as $key => $value) {
|
|
$this->BranchModel->update($value['branch_id'], $data);
|
|
}
|
|
return $this->respond(['status' => 'success','code' => 200,'data' => true],200);
|
|
} else {
|
|
$result = "No Match's";
|
|
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
|
|
}
|
|
} catch (\Exception $exception) {
|
|
return $this->respond(['status' => 'failed','code' => 500,'data' => $exception],500);
|
|
}
|
|
}
|
|
|
|
}
|