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

471 lines
19 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\ClientModel;
use App\Models\ClientBranchModel;
use App\Models\BranchModel;
use App\Models\BusinessModel;
use App\Models\StaffModel;
use App\Models\ClientDocsModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use App\Models\DocsStatusModel;
use CodeIgniter\API\ResponseTrait;
use App\Helpers\NotificationHelper;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class ClientController extends BaseController
{
public $session;
protected $ClientModel;
protected $ClientBranchModel;
protected $BranchModel;
protected $BusinessModel;
protected $StaffModel;
protected $ClientDocsModel;
protected $DocsStatusModel;
use ResponseTrait;
public function __construct()
{
$this->session = session();
$this->ClientModel = new ClientModel();
$this->ClientBranchModel = new ClientBranchModel();
$this->BranchModel = new BranchModel();
$this->BusinessModel = new BusinessModel();
$this->StaffModel = new StaffModel();
$this->ClientDocsModel = new ClientDocsModel();
$this->DocsStatusModel = new DocsStatusModel();
}
public function login()
{
if($this->session->has('is_client_logged_in')){ return redirect()->to(base_url().'client_home'); }
if($this->request->getmethod() == 'POST')
{
$gst_no = $this->request->getPost('gst_no');
$client_branch_data = $this->ClientBranchModel->where('gst_no', $gst_no)->first();
if($client_branch_data)
{
if($client_branch_data['is_active'] == 0)
{
$this->session->setTempdata('error','Client Deactivated',3);
return redirect()->to(base_url());
}
$this->session->set('is_client_logged_in',$client_branch_data['id']);
// $this->session->set('logged_in_client_name',$clientdata['org_name']);
return redirect()->to(base_url().'client_home');
}else{
$this->session->setTempdata('error','Email or Password is invalid',3);
return redirect()->to(base_url());
}
}
return view('client_login');
}
public function client_logout()
{
session()->remove('is_client_logged_in');
session()->destroy();
return redirect()->to(base_url());
}
public function client_home(){
$client_id =$this->session->get('is_client_logged_in');
$data['doc_list'] = $this->ClientDocsModel->getTemplate($client_id);
$clientdata = $this->ClientBranchModel->where('id', $client_id)->get()->getRow();
if($clientdata){
echo view('layout/header', ['Data'=>$clientdata]);
echo view('client_home',$data);
echo view('layout/footer');
}
}
public function client_profile()
{
if(!$this->session->has('is_client_logged_in')){ return redirect()->to(base_url()); }
if($this->request->getmethod() == 'POST')
{
//update client profile
$clientData = $this->request->getVar();
$clientData['updated_by'] = $this->session->get('is_client_logged_in');
$client_id = $this->request->getVar('client_id');
$this->ClientModel->where('client_id', $client_id )->set($clientData)->update();
echo true;
}else{
$clientdata = $this->ClientModel->where('client_id',$this->session->get('is_client_logged_in'))->get()->getRow();
echo view('layout/header', ['Data'=>$clientdata]);
echo view('client_profile',['client'=>$clientdata]);
echo view('layout/footer');
}
}
public function change_client_password($client_id)
{
$old_password =$this->request->getVar('old_password');
$new_password =$this->request->getVar('new_password');
$confirm_password =$this->request->getVar('confirm_password');
$client = $this->ClientModel->where('client_id',$client_id)->first();
$data_old_password = $client['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);
}
$data['password'] = $new_password;
if ($this->ClientModel->update($client_id, $data)) {
$response = array(
'status' => 'success',
'message' => 'Password Change Succuessfully'
);
return json_encode($response);
}
}
public function generate_otp()
{
try {
$gst_no = $this->request->getPost('gst_no');
if (empty($gst_no)) {
throw new \Exception('GST number is required');
}
$client_branch_data = $this->ClientBranchModel->where('gst_no', $gst_no)->first();
if (!$client_branch_data) {
throw new \Exception('Client branch not found for GST number: ' . $gst_no);
}
$id = $client_branch_data['id'];
$otp = mt_rand(100000, 999999);
$data = ['otp' => $otp];
$update = $this->ClientBranchModel->update($id, $data);
if (!$update) {
throw new \Exception('Failed to update OTP in database');
}
$notificationHelper = new NotificationHelper();
$number = '91'. $client_branch_data['mobile_no']; // Replace with dynamic recipient number
$message = 'Login OTP is: ' . $otp;
if($client_branch_data['whatsapp'] == 1){
try {
$notificationHelper->sendWhatsAppMessage($number, $message);
// return $this->respond(['success' => true, 'otp' => $otp]);
} catch (\Exception $e) {
return $this->respond(['Error' => $e->getMessage()]);
}
}
if($client_branch_data['sms'] == 1){
try {
$notificationHelper->sendSms('91'. $client_branch_data['mobile_no'], $client_branch_data['name'], $otp, '', 'login_otp','');
// return $this->respond(['success' => true, 'otp' => $otp]);
} catch (\Exception $e) {
return $this->respond(['Error' => $e->getMessage()]);
}
}
return $this->respond(['success' => true, 'otp' => '']);
// Return success response with OTP
} catch (\Exception $e) {
// Log the exception
error_log('Error generating OTP: ' . $e->getMessage());
// Return error response
return $this->respond(['status' => 'error', 'code' => 500, 'message' => $e->getMessage()], 500);
}
}
public function verify_otp()
{
try {
$gst_no = $this->request->getPost('gst_no');
$otp = $this->request->getPost('otp');
$client_branch_data = $this->ClientBranchModel->where('gst_no', $gst_no )->where('otp', $otp)->first();
$data = ['otp' =>''];
if($client_branch_data){
$id = $client_branch_data['id'];
$update = $this->ClientBranchModel->update($id , $data);
if($update){
return json_encode(true);
}
} else {
return ;
}
} catch (\Exception $e) {
return $this->respond(['status' => 'error', 'code' => 500, 'message' => $e->getMessage()], 500);
}
}
public function status_client()
{
try {
$client_id = $this->request->getPost('client_id');
$is_active = $this->request->getPost('is_active');
$data = [
'is_active' => $is_active
];
$updated = $this->ClientModel->update($client_id, $data);
if ($updated) {
$client_branch_list = $this->ClientBranchModel->where('client_id', $client_id)->findAll();
foreach($client_branch_list as $key=> $value){
$this->ClientBranchModel->update($value['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);
}
}
public function doc_rejection()
{
$id = $this->request->getPost('doc_id');
$data=[
'rejection_reason' => $this->request->getPost('rejection_reason'),
'is_approved' => $this->request->getPost('is_approved')
];
if($id){
$update =$this->ClientDocsModel->update($id, $data);
if($this->session->has('is_client_logged_in')){
$logged_user = $this->session->get('is_client_logged_in');
$statusData['is_staff'] = 0;
}else{
$logged_user = $this->session->get('is_staff_logged_in');
$statusData['is_staff'] = 1;
}
$statusData['client_docs_id'] = $id;
$statusData['created_by'] = $logged_user;
$statusData['status'] = 'Rejected';
$statusData['note'] = $this->request->getPost('rejection_reason');
$this->DocsStatusModel->insert($statusData);
if ($update) {
return json_encode(true);
}
}
return ;
}
public function client_branch_list()
{
// if(!$this->session->has('is_staff_logged_in')){ return redirect()->to(base_url().'staff'); }
$data['clientBranchList'] = $this->ClientBranchModel->select('client_branch.*, client.name as client_name, service_group.business_id')
->join('client', 'client.client_id = client_branch.client_id', 'left')
->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') // ->where('client.branch_id',$this->session->get('logged_in_staff_branch_id'))
// ->groupBy('client_branch.client_id')
->findAll();
$data['client_list'] = $this->ClientModel->select('client_id, name')->where('is_active', 1)->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_branch_list',$data);
echo view('layout/footer');
}
public function add_client_branch()
{
$gst = $this->request->getPost('gst_no');
$client_branch = $this->ClientBranchModel->where('gst_no', $gst)->first();
if($client_branch){
return json_encode(false);
}else{
$data = $this->request->getPost();
$this->ClientBranchModel->insert($data);
return json_encode(true);
}
}
public function edit_client_branch()
{
$id = $this->request->getPost('id');
$data = $this->request->getPost();
$this->ClientBranchModel->update($id, $data);
return json_encode('update');
}
public function get_client_branch($branch_id)
{
if ($branch_id) {
$branch =$this->ClientBranchModel->where('id',$branch_id)->first();
return json_encode($branch);
}
}
public function status_client_branch()
{
try {
$client_branch_id = $this->request->getPost('client_branch_id');
$is_active = $this->request->getPost('is_active');
$data = [
'is_active' => $is_active
];
$updated = $this->ClientBranchModel->update($client_branch_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);
}
}
public function client_upload()
{
if ($_FILES['file']['name']) {
$allowed_extension = ['xls', 'xlsx'];
$file_array = explode(".", $_FILES['file']['name']);
$file_extension = end($file_array);
if (in_array($file_extension, $allowed_extension)) {
$file_name = $_FILES['file']['tmp_name'];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_name);
$data = $spreadsheet->getActiveSheet()->toArray();
// Extract headers and data
$headers = array_shift($data);
$nameIndex = array_search('Name', $headers);
$panNoIndex = array_search('PAN No', $headers);
$emailIndex = array_search('Email', $headers);
$mobileNoIndex = array_search('Mobile No', $headers);
if ($nameIndex === false || $panNoIndex === false || $emailIndex === false || $mobileNoIndex === false) {
echo "Required columns (Name, PAN No, Email, Mobile No) not found in the spreadsheet.";
return;
}
$groupedData = [];
// Group data by the "Name" field
foreach ($data as $key => $row) {
$name = $row[$nameIndex];
$panNo = $row[$panNoIndex];
$email = $row[$emailIndex];
$mobileNo = $row[$mobileNoIndex];
if (!isset($groupedData[$name])) {
$groupedData[$name] = [
'name' => $name,
'pan_no' => $panNo,
'email' => $email,
'mobile_no' => $mobileNo,
'branches' => []
];
}
$branch = [];
foreach ($headers as $index => $header) {
// print_r($header);die;
switch ($header) {
case 'B Name':
$branch['b_name'] = $row[$index];
break;
case 'GST No':
$branch['gst_no'] = $row[$index];
break;
case 'B Mobile No':
$branch['b_mobile_no'] = $row[$index];
break;
case 'B Email':
$branch['b_email'] = $row[$index];
break;
case 'Pin Code':
$branch['pin_code'] = $row[$index];
break;
case 'Country':
$branch['country'] = $row[$index];
break;
case 'State':
$branch['state'] = $row[$index];
break;
case 'City':
$branch['city'] = $row[$index];
break;
case 'Address':
$branch['address'] = $row[$index];
break;
// default:
// $branch[$header] = $row[$index];
// break;
}
}
$groupedData[$name]['branches'][] = $branch;
}
foreach ($groupedData as $key => $value) {
$created_by = $this->session->get('is_staff_logged_in');
$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 ',$branch_id)
// ->first();
// $business_id = $business['business_id'];
$insertData = [
"name" => $value['name'],
"pan_no" => $value['pan_no'],
"email" => $value['email'],
"mobile_no" => $value['mobile_no'],
"created_by" => $created_by,
"business_id" => $business_id,
];
$client_pan_find = $this->ClientModel->where('pan_no', $value['pan_no'])->first();
if($client_pan_find){
}else{
$client = $this->ClientModel->insert($insertData);
if($client){
if($value['branches']){
foreach ($value['branches'] as $key => $branch) {
$insertBranchData = [
"name" => $branch['b_name'],
"gst_no" => $branch['gst_no'],
"email" => $branch['b_email'],
"mobile_no" => $branch['b_mobile_no'],
"created_by" => $created_by,
"client_id" => $client,
"country" => $branch['country'],
"state" => $branch['state'],
"city" => $branch['city'],
"pin_code" => $branch['pin_code'],
"address" => $branch['address'],
];
if($this->ClientBranchModel->where('gst_no', $branch['gst_no'])->first()){
}else{
$clientBranch = $this->ClientBranchModel->insert($insertBranchData);
}
}
}
}
}
}
return json_encode(true);
} else {
echo "Invalid file extension.";
}
} else {
echo "No file uploaded.";
}
}
}