diff --git a/.env b/.env index 7e3bd14..b281b3e 100644 --- a/.env +++ b/.env @@ -69,3 +69,9 @@ database.default.port = 3306 #-------------------------------------------------------------------- # logger.threshold = 4 + + + +WAAI_INSTANCE = '666FEE6738696' +WAAI_TOKEN = '666fd862df687' +SEND_WAAI_URL = SEND_WAAI_URL \ No newline at end of file diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 7f52cce..68797ea 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -67,6 +67,7 @@ use CodeIgniter\Router\RouteCollection; $routes->match(['get','post'],'/client_docs_preview','MasterController::client_docs_preview'); $routes->get('client_docs_histroy/(:any)','MasterController::client_docs_histroy/$1'); $routes->match(['get','post'],'/change_doc_status','MasterController::change_doc_status'); + $routes->match(['get','post'],'/rememeber_notification','MasterController::rememeber_notification'); $routes->match(['get','post'],'/test','MasterController::test'); $routes->post('doc_generate_otp','MasterController::doc_generate_otp'); $routes->post('doc_verify_otp','MasterController::doc_verify_otp'); @@ -74,7 +75,7 @@ use CodeIgniter\Router\RouteCollection; $routes->match(['get','post'],'shared_document_edit','MasterController::shared_document_edit'); - + $routes->get('business_list','BusinessController::business_list'); $routes->get('new_business/(:any)','BusinessController::new_business/$1'); $routes->post('add_business','BusinessController::add_business'); diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 2448925..4924a84 100644 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -12,6 +12,11 @@ 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 { @@ -161,29 +166,47 @@ class ClientController extends BaseController try { $gst_no = $this->request->getPost('gst_no'); - $client_branch_data = $this->ClientBranchModel->where('gst_no', $gst_no )->first(); - - if($client_branch_data){ - - $id = $client_branch_data['id']; - - $data = [ - 'otp' => '123456' - ]; - $update = $this->ClientBranchModel->update($id , $data); - - if($update){ - return json_encode(true); - } - } else { - return false; + 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; + + try { + $notificationHelper->sendWhatsAppMessage($number, $message); + return $this->respond(['success' => true, 'otp' => $otp]); + } catch (\Exception $e) { + return $this->respond(['Error' => $e->getMessage()]); + } + // Return success response with OTP + } catch (\Exception $e) { - // Handle any exceptions that occur during execution + // 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() diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 6e36ee9..83bae19 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -21,8 +21,8 @@ class Home extends BaseController $file_name = $_FILES['file']['tmp_name']; $spreadsheet = IOFactory::load($file_name); $data = $spreadsheet->getActiveSheet()->toArray(); - echo "
";
- print_r($data);die;
+ // echo "";
+ // print_r($data);die;
foreach ($data as $row) {
$name = $row[0];
diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php
index af1b32e..2805ff2 100644
--- a/app/Controllers/MasterController.php
+++ b/app/Controllers/MasterController.php
@@ -16,6 +16,7 @@ use App\Models\DocsStatusModel;
use \Mpdf\Mpdf;
+use App\Helpers\NotificationHelper;
use CodeIgniter\API\ResponseTrait;
@@ -294,6 +295,8 @@ class MasterController extends BaseController
$data['serviceGroupData'] = $this->ServiceGroupModel->findAll();
$staffdata = $this->StaffModel->where('staff_id',$this->session->get('is_staff_logged_in'))->get()->getRow();
+ // echo "";
+ // print_r($data['doc_list']);die;
echo view('layout/header', ['Data'=>$staffdata]);
echo view('share_docs',$data);
echo view('layout/footer');
@@ -530,20 +533,33 @@ class MasterController extends BaseController
$MailHelper = new MailHelper();
- $clientData = $this->ClientDocsModel->select('client.name , client_branch.email')
+ $clientData = $this->ClientDocsModel->select('client.name , client_branch.email, client_branch.mobile_no')
->join('client_branch', 'client_docs.client_branch_id = client_branch.id', 'left')
->join('client', 'client_branch.client_id = client.client_id', 'left')
->where('client_docs.id', $doc_id)->get()->getRow();
- $url = 'Click here';
+ $documentUrl = base_url();
+ $url = 'Preview and Approve Document';
$businessData = $this->BranchModel->select('branches.* , business.business_name ')
->join('business', 'branches.business_id = business.business_id', 'left')
->where('branches.branch_id',$this->session->get('logged_in_staff_branch_id'))
->get()->getRow();
-
- $MailHelper->send_url_email($clientData->email , $url , $clientData->name , $businessData->business_name, $businessData->email);
+ $subject = 'BB-Sign Verify Document';
+ $message = "Dear " . $clientData->name . ",
" . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve.
" . $url;
+ $mail_send = $MailHelper->send_url_email($clientData->email , $businessData->email, $subject, $message);
+
+ $notificationHelper = new NotificationHelper();
+ $number = '91'.$clientData->mobile_no; // Replace with dynamic recipient number
+
+ try {
+ $whatsApp_message = "Dear " . $clientData->name . "," . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve." ;
+ $notificationHelper->sendWhatsAppMessage($number, $whatsApp_message, $url);
+ // return $this->respond(['success' => true, 'otp' => $otp]);
+ } catch (\Exception $e) {
+ return $this->respond(['Error' => $e->getMessage()]);
+ }
$statusData['client_docs_id'] = $doc_id;
$statusData['is_staff'] = 1;
@@ -570,6 +586,45 @@ class MasterController extends BaseController
return $this->response->setJSON(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
+
+
+ public function rememeber_notification()
+ {
+ try {
+ $doc_id = $this->request->getPost('doc_id');
+
+ $MailHelper = new MailHelper();
+
+ $clientData = $this->ClientDocsModel->select('client.name , client_branch.email, client_branch.mobile_no')
+ ->join('client_branch', 'client_docs.client_branch_id = client_branch.id', 'left')
+ ->join('client', 'client_branch.client_id = client.client_id', 'left')
+ ->where('client_docs.id', $doc_id)->get()->getRow();
+
+ $url = 'Click here';
+
+ $businessData = $this->BranchModel->select('branches.* , business.business_name ')
+ ->join('business', 'branches.business_id = business.business_id', 'left')
+ ->where('branches.branch_id',$this->session->get('logged_in_staff_branch_id'))
+ ->get()->getRow();
+ $subject = 'BB-Sign Verify Document';
+ $message = "Dear " . $clientData->name . ",
" . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve.
" . $url;
+
+ $MailHelper->send_url_email($clientData->email , $businessData->email, $subject, $message);
+
+ $notificationHelper = new NotificationHelper();
+ $number = '91'.$clientData->mobile_no; // Replace with dynamic recipient number
+
+ try {
+ $whatsApp_message = "Dear " . $clientData->name . "," . $businessData->business_name . " has requested your approval for a document. Please click the link below to preview and approve." ;
+ $notificationHelper->sendWhatsAppMessage($number, $whatsApp_message, $url);
+ return $this->respond(['success' => true, 'data' => 'Remember Send Successfully']);
+ } catch (\Exception $e) {
+ return $this->respond(['Error' => $e->getMessage()]);
+ }
+ } catch (\Exception $exception) {
+ return $this->response->setJSON(['status' => 'failed','code' => 500,'data' => $exception],500);
+ }
+ }
public function doc_generate_otp()
@@ -583,13 +638,21 @@ class MasterController extends BaseController
if($client_data){
$id = $this->request->getPost('doc_id');
- $data = [
- 'otp' => '123456',
- ];
+ $otp = mt_rand(100000, 999999);
+ $data = ['otp' => $otp];
$update = $this->ClientDocsModel->update($id, $data);
if($update){
- return json_encode(true);
+ $notificationHelper = new NotificationHelper();
+ $number = '91'.$mobile_no; // Replace with dynamic recipient number
+ $message = 'Document Verification OTP : ' . $otp;
+
+ try {
+ $notificationHelper->sendWhatsAppMessage($number, $message);
+ return $this->respond(['success' => true, 'otp' => $otp]);
+ } catch (\Exception $e) {
+ return $this->respond(['Error' => $e->getMessage()]);
+ }
}
} else {
return false;
diff --git a/app/Controllers/StaffController.php b/app/Controllers/StaffController.php
index 01475a5..a04dcdb 100644
--- a/app/Controllers/StaffController.php
+++ b/app/Controllers/StaffController.php
@@ -105,8 +105,6 @@ class StaffController extends BaseController
$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();
echo view('layout/header', ['Data'=>$staffdata]);
echo view('staff_list',$data);
@@ -118,15 +116,22 @@ class StaffController extends BaseController
if($this->request->getmethod() == 'POST')
{
+ $email = $this->request->getVar('email');
+ $staff = $this->StaffModel->where('email', $email)->first();
+
+ if($staff){
+ return;
+ }else{
$staffData = $this->request->getVar();
// print_r($staffData);die;
- $staffData['created_by'] = $this->session->get('is_staff_logged_in');
- // $staffData['branch_id'] = $this->session->get('logged_in_staff_branch_id');
+ $staffData['created_by'] = $this->session->get('is_staff_logged_in');
+ // $staffData['branch_id'] = $this->session->get('logged_in_staff_branch_id');
$insert = $this->StaffModel->save($staffData);
if($insert)
{
return redirect()->to(base_url()."staff_list");
}
+ }
}else{
@@ -246,7 +251,7 @@ class StaffController extends BaseController
{
//update staff profile
$staffData = $this->request->getVar();
- print_r($staffData);
+ // 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();
@@ -362,7 +367,7 @@ class StaffController extends BaseController
if($this->request->getmethod() == 'POST')
{
$staffData = $this->request->getVar();
- print_r($staffData);die;
+ // 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);
diff --git a/app/Helpers/MailHelper.php b/app/Helpers/MailHelper.php
index a5c7426..c448ae5 100644
--- a/app/Helpers/MailHelper.php
+++ b/app/Helpers/MailHelper.php
@@ -2,91 +2,58 @@
namespace App\Helpers;
-
-
class MailHelper
{
- protected static $logger;
+ public static function send_otp_email($email_id, $otp)
+ {
+ $subject = 'BB-Sign Verify Document';
+ $message = "Hai,
+
+ We have a requested forgot password OTP: " . $otp;
- public static function initLogger() {
- if (self::$logger === null) {
- self::$logger = \Config\Services::logger();
+ try {
+ $email = \Config\Services::email();
+ $email->setMailType('html');
+ $email->setFrom('bbone@venbait.in', 'BB-Sign');
+ $email->setTo($email_id);
+ $email->setSubject($subject);
+ $email->setMessage($message);
+
+ if ($email->send()) {
+ return ['status' => 'success', 'code' => 200, 'message' => 'Email Sent Successfully...', 'data' => $email_id];
+ } else {
+ return ['status' => 'failed', 'code' => 404, 'message' => 'Email Sent Failed...', 'data' => $email_id];
+ }
+ } catch (\Exception $e) {
+ return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage(), 'data' => $email_id];
+ }
+ }
+
+ public static function send_url_email($email_id, $branch_email, $subject, $message)
+ {
+ try {
+
+
+
+ $email = \Config\Services::email();
+
+ $email->setMailType('html');
+ $email->setFrom('bbone@venbait.in', 'BB-Sign');
+ $email->setTo($email_id);
+ $email->setSubject($subject);
+ $email->setMessage($message);
+
+ if ($email->send()) {
+ return ['status' => 'success', 'code' => 200, 'message' => 'Email Sent Successfully...', 'data' => $email_id];
+ } else {
+ log_message('error', 'Email sending failed: ' . $email->printDebugger(['headers']));
+ return ['status' => 'failed', 'code' => 404, 'message' => 'Email Sent Failed...', 'data' => $email_id];
+ }
+ } catch (\Exception $e) {
+ log_message('error', 'Exception occurred while sending email: ' . $e->getMessage());
+ return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage(), 'data' => $email_id];
}
}
- public static function send_otp_email($email_id,$otp)
- {
-
-
-
- $subject = 'BB-Sign Verify Document';
- $message = "Hai ,
-
- We have a requested forgot password OTP :".$otp;
-
-
-
- try {
-
- $email = \Config\Services::email();
- $email->setMailType('html');
- $email->setFrom('bbone@venbait.in', 'BB-Sign');
-
- $email->setTo($email_id);
-
- $email->setSubject($subject);
- $email->setMessage($message);
-
- if ($email->send()) {
- return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...','data' => $email_id,],200);
- } else {
- return json_encode(['status' => 'failed','code' => 404,'message'=>'Email Sent Failed...','data' => $email_id ],404);
- }
-
- } catch (\Exception $e) {
- return json_encode(['status' => 'failed','code' => 500,'data' => $email_id],500);
- }
- }
-
- public static function send_url_email($email_id,$url,$client_name,$business_name,$branch_email)
- {
-
-
- self::initLogger();
-
-
- $subject = 'BB-Sign Verify Document';
-
- $message = "Dear " . $client_name . ",
" . $business_name . " has requested your approval for a document. Please click the link below to preview and approve.
" . $url;
-
-
- try {
-
- $email = \Config\Services::email();
- $email->setMailType('html');
- $email->setFrom('bbone@venbait.in', 'BB-Sign');
-
- $email->setTo($email_id);
-
- $email->setSubject($subject);
- $email->setMessage($message);
-
- if ($email->send()) {
- self::$logger->info('Email Sent Successfully');
- return json_encode(['status' => 'success','code' => 200, 'message'=>'Email Sent Successfully...','data' => $email_id,],200);
- } else {
- self::$logger->error('Email Sent Failed');
- return json_encode(['status' => 'failed','code' => 404,'message'=>'Email Sent Failed...','data' => $email_id ],404);
- }
-
- } catch (\Exception $e) {
- return json_encode(['status' => 'failed','code' => 500,'data' => $email_id],500);
- }
- }
-
-
-
-
-
}
-?>
\ No newline at end of file
+?>
diff --git a/app/Helpers/NotificationHelper.php b/app/Helpers/NotificationHelper.php
new file mode 100644
index 0000000..d496d12
--- /dev/null
+++ b/app/Helpers/NotificationHelper.php
@@ -0,0 +1,84 @@
+email = service('email');
+ $this->logger = service('logger');
+ }
+
+
+ public function sendWhatsAppMessage($number, $message, $client_login_url = '')
+ {
+ // Extract plain text message
+ $plainTextMessage = strip_tags($message);
+
+ // Extract URL from message
+ preg_match('/]*?\s+)?href="([^"]*)"/i', $client_login_url, $match);
+ $client_login_url = isset($match[1]) ? $match[1] : '';
+ // Prepare the data to send
+ $instance_id = $_ENV['WAAI_INSTANCE'];
+ $access_token = $_ENV['WAAI_TOKEN'];
+ $url = 'https://waai.in/api/send';
+
+ // Prepare the text for WhatsApp
+ if($client_login_url){
+ $whatsappMessage = $plainTextMessage . "\n\nClick here - " . $client_login_url;
+ }else{
+ $whatsappMessage = $plainTextMessage ;
+ }
+ // Prepare JSON payload
+ $postdata = json_encode([
+ 'number' => $number,
+ 'type' => 'text',
+ 'message' => $whatsappMessage,
+ 'instance_id' => $instance_id,
+ 'access_token' => $access_token
+ ]);
+
+ // Initialize cURL session
+ $ch = curl_init($url);
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, [
+ 'Content-Type: application/json',
+ 'Content-Length: ' . strlen($postdata)
+ ]);
+
+ // Execute cURL session
+ $response = curl_exec($ch);
+
+ // Check for cURL errors
+ if ($response === false) {
+ $this->logger->error('Failed to communicate with WhatsApp API: ' . curl_error($ch));
+ curl_close($ch);
+ throw new \Exception('Failed to communicate with WhatsApp API');
+ }
+
+ // Get HTTP response code
+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ curl_close($ch);
+
+ // Check HTTP response code for success
+ if ($httpCode !== 200) {
+ $this->logger->error('Failed to send WhatsApp message. HTTP Code: ' . $httpCode);
+ throw new \Exception('Failed to send WhatsApp message. HTTP Code: ' . $httpCode);
+ }
+
+ // Log success or handle further
+ $this->logger->info('WhatsApp message sent successfully to ' . $number);
+ return true;
+ }
+
+}
+
+?>
diff --git a/app/Views/client_login.php b/app/Views/client_login.php
index 1cb71d3..f5aa422 100644
--- a/app/Views/client_login.php
+++ b/app/Views/client_login.php
@@ -159,7 +159,7 @@
gst_no : $('#gst_no').val()
},
success: function(response) {
- if(response){
+ if(response.success){
gst_error.html('');
$('#generate_otp_id').hide();
$('#otp_hide').show();
@@ -218,7 +218,8 @@
})
$('#resend_otp').click(function (params) {
-
+ var gst_error = $('#gst_error')
+
event.preventDefault(); // Prevent form submission
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
@@ -229,15 +230,15 @@
gst_no : $('#gst_no').val()
},
success: function(response) {
- if(response){
+ if(response.success){
$('#gst_error').html('');
$('#generate_otp_id').hide();
$('#otp_hide').show();
$('#verify_otp_hide').show();
$('#otp_sent_success').html('OTP Sent Successfully').css('color','green');
}else{
-
-
+ gst_error.html('GST number mismatch. Please try again.');
+ gst_error.css('color','red');
}
$('.loader').fadeOut();
$('.loader-mask').fadeOut('slow');
diff --git a/app/Views/share_docs.php b/app/Views/share_docs.php
index 1a15165..eefab18 100644
--- a/app/Views/share_docs.php
+++ b/app/Views/share_docs.php
@@ -105,7 +105,12 @@
Share
+
+
+ Remember
+
+
@@ -390,6 +395,7 @@
data: formData,
dataType: 'json',
success: function(response) {
+ // console.log(response);
if (response.data) {
window.location.reload();
}
@@ -403,8 +409,32 @@
}
+ function rememberNotification(params) {
+ var doc_id = params.getAttribute('data-id');
+
+ var formData = {
+ doc_id: doc_id,
+ };
+ $.ajax({
+ type: 'POST',
+ url: '',
+ data: formData,
+ dataType: 'json',
+ success: function(response) {
+ if (response.data) {
+ window.location.reload();
+ }
+ },
+ error: function(xhr, status, error) {
+ // Handle error response here
+ console.error(xhr.responseText);
+ }
+ });
+ }
+
+
diff --git a/app/Views/staff_form.php b/app/Views/staff_form.php
index 363b577..d05b5cb 100644
--- a/app/Views/staff_form.php
+++ b/app/Views/staff_form.php
@@ -38,8 +38,7 @@
Personal Info