bb_sign/app/Controllers/StaffController.php
2024-07-08 11:51:54 +05:30

465 lines
16 KiB
PHP

<?php
namespace App\Controllers;
use App\Helpers\MailHelper;
use App\Models\StaffModel;
use App\Models\ClientModel;
use App\Models\ClientBranchModel;
use App\Models\ServiceModel;
use App\Models\BusinessModel;
use App\Models\BranchModel;
use App\Models\TemplateModel;
use CodeIgniter\API\ResponseTrait;
class StaffController extends BaseController
{
public $session;
protected $StaffModel;
protected $ClientModel;
protected $ClientBranchModel;
protected $ServiceModel;
protected $BusinessModel;
protected $BranchModel;
protected $TemplateModel;
use ResponseTrait;
public function __construct()
{
$this->session = session();
$this->StaffModel = new StaffModel();
$this->ClientModel = new ClientModel();
$this->ClientBranchModel = new ClientBranchModel();
$this->ServiceModel = new ServiceModel();
$this->BusinessModel = new BusinessModel();
$this->BranchModel = new BranchModel();
$this->TemplateModel = new TemplateModel();
}
public function set_business_in_session($business_id){
$this->session->set('logged_in_staff_business_id', $business_id);
return json_encode(true);
}
public function login()
{
if($this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff_list'); }
if($this->request->getmethod() == 'POST')
{
$email = $this->request->getVar('email');
$password = $this->request->getVar('password');
$staffdata = $this->StaffModel->verifyEmail($email);
if($staffdata)
{
if($staffdata['is_active'] == 0)
{
$this->session->setTempdata('error','Staff Deactivated',3);
return redirect()->to(base_url()."staff");
}
if (password_verify($password, $staffdata['password']))
{
$this->session->set('is_staff_logged_in',$staffdata['staff_id']);
$this->session->set('logged_in_staff_role',$staffdata['role']);
$this->session->set('logged_in_staff_name',$staffdata['name']);
// $this->session->set('logged_in_staff_branch_id',$staffdata['branch_id']);
if($staffdata['role'] == 'Admin'){
$this->session->set('logged_in_staff_business_id', 0);
}else{
$this->session->set('logged_in_staff_business_id',json_decode($staffdata['business_id'])[0]);
}
return redirect()->to(base_url().'shared_docs_list');
}
else{
$this->session->setTempdata('error','Email or Password is invalid',3);
return redirect()->to(base_url()."staff");
}
}else{
$this->session->setTempdata('error','Email or Password is invalid',3);
return redirect()->to(base_url()."staff");
}
}
//get method
return view('staff_login');
}
public function staff_logout()
{
session()->remove('is_staff_logged_in');
session()->remove('logged_in_staff_branch_id');
session()->destroy();
return redirect()->to(base_url()."staff");
}
// staff crud
public function staff_list()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$data['staffList'] = $this->StaffModel->getStaffWithBusinessAndBranch(session()->get('is_staff_logged_in'), $this->session->get('logged_in_staff_branch_id'));
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
$data['business_list'] = $this->BusinessModel->findAll();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('staff_list',$data);
echo view('layout/footer');
}
public function staff_create()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
$email = $this->request->getVar('email');
$staff = $this->StaffModel->where('email', $email)->first();
if($staff){
return json_encode('mail_exists');
}else{
$staffData = $this->request->getVar();
$staffData['created_by'] = $this->session->get('is_staff_logged_in');
$staffData['business_id'] = json_encode($this->request->getVar('business_id'));
$insert = $this->StaffModel->save($staffData);
if($insert)
{
return json_encode('staff_created');
}
}
}else{
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
$data['business'] = $this->BusinessModel->where('isactive', 1)->findAll();
$data['page_heading'] = "Create Staff";
$data['business_list'] = $this->BusinessModel->findAll();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('staff_form', $data);
echo view('layout/footer');
}
}
public function staff_edit($staff_id = null)
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
$staffData = $this->request->getVar();
$staffData['updated_by'] = $this->session->get('is_staff_logged_in');
$staff_id = $this->request->getVar('id');
$staffData['business_id'] = json_encode($this->request->getVar('business_id'));
$this->StaffModel->where('staff_id', $staff_id )->set($staffData)->update();
return json_encode('edit_success');
}else{
$data['staffData'] = $this->StaffModel->where('md5(staff.staff_id)', $this->request->getVar('staff_id') )->get()->getRow();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
$data['business'] = $this->BusinessModel->where('isactive', 1)->findAll();
$data['page_heading'] = "Edit Staff";
$data['business_list'] = $this->BusinessModel->findAll();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('staff_form',$data);
echo view('layout/footer');
}
}
public function business_view($staff_id)
{
$staffdata = $this->StaffModel->where('staff_id',$staff_id)->first();
$businessData =[];
$businessIds = json_decode($staffdata['business_id']);
if (isset($businessIds) && is_array($businessIds)) {
foreach ($businessIds as $key => $value) {
$businessData[] = $this->BusinessModel->where('business_id', $value)->first();
}
}else{
$businessData[] = $this->BusinessModel->where('business_id', $businessIds)->first();
}
return json_encode($businessData);
}
// client crud
public function client_list()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$business_id = $this->session->get('logged_in_staff_business_id');
$data['clientListForList'] = $this->ClientModel->findAll();
$data['clientList'] = $this->ClientModel->select('client.*, service_group.business_id')
->join('client_branch', 'client_branch.client_id = client.client_id')
->join('client_docs', 'client_docs.client_branch_id = client_branch.id')
->join('template', 'template.template_id = client_docs.template_id')
->join('service_group', 'service_group.service_group_id = template.service_group_id')
->findAll();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))
->get()
->getRow();
$data['business_list'] = $this->BusinessModel->findAll();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('client_list',$data);
echo view('layout/footer');
}
public function client_edit($client_idd)
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
//update staff details
$clientData = $this->request->getVar();
$clientData['updated_by'] = $this->session->get('is_staff_logged_in');
$client_id = $this->request->getVar('client_id');
$client_update =$this->ClientModel->where('client_id', $client_id )->set($clientData)->update();
if($client_update){
return json_encode(true);
}else{
return ;
}
}else{
// $data['clientData'] = $this->ClientModel->where('md5(client.client_id)', $this->request->getVar('client_id') )->get()->getRow();
$data['clientData'] = $this->ClientModel->where('client_id', $client_idd )->get()->getRow();
// print_r($data);die;
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
return json_encode($data['clientData']);
// echo view('layout/header', ['Data'=>$staffdata]);
// echo view('client_form',$data);
// echo view('layout/footer');
}
}
public function client_create()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
$clientData = $this->request->getVar();
$clientData['created_by'] = $this->session->get('is_staff_logged_in');
$clientData['business_id'] = $this->session->get('logged_in_staff_business_id');
// $business= $this->BranchModel->select('*')
// ->join('business' , 'branches.business_id = business.business_id', 'left')
// ->where('branches.branch_id ',$clientData['branch_id'])
// ->first();
// $business= $this->BusinessModel->select('*')
// ->where('business_id ',$clientData['business_id'])
// ->first();
// $clientData['business_id'] = $business['business_id'];
// $insert = $this->ClientModel->save($clientData);
$insert = $this->ClientModel->insert($clientData);
if($insert)
{
$data['client_id'] = $insert;
$data['email'] = $clientData['branch_email'];
$data['country'] = $clientData['country'];
$data['name'] = $clientData['branch_name'];
$data['address'] = $clientData['address'];
$data['city'] = $clientData['city'];
$data['state'] = $clientData['state'];
$data['pin_code'] = $clientData['pin_code'];
$data['mobile_no'] = $clientData['branch_mobile_no'];
$data['gst_no'] = $clientData['gst_no'];
$data['sms'] = $clientData['sms'];
$data['whatsapp'] = $clientData['whatsapp'];
$branch_insert = $this->ClientBranchModel->insert($data);
if($branch_insert){
return json_encode(true);
}else{
return ;
}
}
}else{
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('client_form');
echo view('layout/footer');
}
}
public function staff_profile()
{
if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
if($this->request->getmethod() == 'POST')
{
//update staff profile
$staffData = $this->request->getVar();
// print_r($staffData);
$staffData['updated_by'] = $this->session->get('is_staff_logged_in');
$staff_id = $this->request->getVar('staff_id');
$this->StaffModel->where('staff_id', $staff_id )->set($staffData)->update();
echo true;
}else{
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
$business_list = [];
foreach (json_decode($staffdata->business_id) as $key => $value) {
$business = $this->BusinessModel->where('business_id', $value)->first();
if ($business) {
$business_list[] = $business;
}
}
echo view('layout/header', ['Data'=>$staffdata]);
echo view('staff_profile',['staff'=>$staffdata, 'business_list'=> $business_list]);
echo view('layout/footer');
}
}
public function change_staff_password($staff_id)
{
$old_password =$this->request->getVar('old_password');
$new_password =$this->request->getVar('new_password');
$confirm_password =$this->request->getVar('confirm_password');
$staff = $this->StaffModel->where('staff_id',$staff_id)->first();
$data_old_password = $staff['password'];
if (!password_verify($old_password, $data_old_password)) {
$response = array(
'status' => 'error',
'message' => 'Old Password Not Match...'
);
return json_encode($response);
}
if($new_password != $confirm_password){
$response = array(
'status' => 'error',
'message' => 'New Password And Confirm Password Not Match...'
);
return json_encode($response);
}
//$hashedPassword = password_hash($new_password, PASSWORD_DEFAULT);
$data['password'] = $new_password;
if ($this->StaffModel->update($staff_id, $data)) {
$response = array(
'status' => 'success',
'message' => 'Password Change Succuessfully'
);
return json_encode($response);
}
}
public function forgot_password()
{
if($this->request->getmethod() == 'POST')
{
$email = $this->request->getVar('email');
$staff_data = $this->StaffModel->where('email', $email)->find();
if ($staff_data)
{
$otp = rand(1000 , 9999);
$MailHelper = new MailHelper();
$MailHelper->send_otp_email($this->request->getVar('email') , $otp );
$data['status'] = 'success';
$data['otp'] = $otp;
echo json_encode($data);
}
else
{
$data['status'] = 'failed';
echo json_encode($data);
}
}else{
return view('forgot_password.php');
}
}
public function verify_otp()
{
if($this->request->getmethod() == 'POST')
{
$otp = $this->request->getVar('otp');
$verify_otp = $this->request->getVar('verify_otp');
if ($otp == $verify_otp)
{
$email = $this->request->getVar('email');
$data = $this->StaffModel->where('email', $email)->find();
if ($data)
{
$this->session->setTempdata('staff_id',$data[0]['staff_id'],3);
$this->session->setTempdata('email',$data[0]['email'],3);
$this->session->setTempdata('mail_success', "OTP verified sucessfully",3);
return view('reset_password.php');
}
}
else
{
$this->session->setTempdata('error', "You entered wrong OTP.Please retry",3);
return view('forgot_password.php');
}
}
}
public function change_password()
{
if($this->request->getmethod() == 'POST')
{
$staffData = $this->request->getVar();
// print_r($staffData);die;
$staff_id = $this->request->getVar('staff_id');
$this->StaffModel->where('staff_id', $staff_id )->set($staffData)->update();
$this->session->setTempdata('mail_success', "Password set sucessfully",3);
return redirect()->to(base_url()."staff");
}
}
public function staff_password_update()
{
if($this->request->getmethod() == 'POST')
{
$staffData = $this->request->getVar();
$staff_id = $this->request->getVar('id');
$this->StaffModel->where('staff_id', $staff_id )->set($staffData)->update();
$this->session->setTempdata('mail_success', "Password set sucessfully",3);
return redirect()->to(base_url()."staff_list");
}
}
public function status_staff()
{
try {
$staff_id = $this->request->getPost('staff_id');
$is_active = $this->request->getPost('is_active');
$data = [
'is_active' => $is_active
];
$updated = $this->StaffModel->update($staff_id, $data);
if ($updated) {
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);
}
}
}