541 lines
22 KiB
PHP
541 lines
22 KiB
PHP
<?php
|
|
use App\Controllers\PlanController;
|
|
use App\Models\OrganizationModel;
|
|
use App\Models\UserModel;
|
|
use App\Models\GroupModel;
|
|
use App\Models\PolicyModel;
|
|
use App\Models\PolicyDetailsModel;
|
|
use App\Models\ServiceModel;
|
|
use App\Models\PlanStatusModel;
|
|
use App\Models\PlanModel;
|
|
use App\Models\MailTemplateModel;
|
|
|
|
|
|
|
|
if (!function_exists('sendPlanCreationMail')) {
|
|
function sendPlanCreationMail($planId , $mailOn = 'send_mail_to_traveller_and_approver')
|
|
{
|
|
$planModel = new PlanModel();
|
|
$userModel = new UserModel();
|
|
$organizationModel = new OrganizationModel();
|
|
$planStatusModel = new PlanStatusModel();
|
|
|
|
// Fetch plan data
|
|
$planData = $planModel->where('plan_id', $planId)->first();
|
|
if (!$planData) { return false; }
|
|
$userId = isset($planData['traveller_id']) && !empty($planData['traveller_id']) ? $planData['created_by'] : $planData['user_id'];
|
|
|
|
|
|
// Fetch user data
|
|
$userData = $userModel->where('user_id', $userId)->first();
|
|
if (!$userData) { return false; }
|
|
|
|
// send mail to traveller
|
|
if($mailOn == 'send_mail_to_traveller_and_approver')
|
|
send_email($planData['org_id'] , $userData['email'], 'plan_creation_notification_to_traveller' , $userData , $planData);
|
|
|
|
// send mail to approver
|
|
$action = getPlanApproverAction($planId);
|
|
|
|
foreach ($action['approver_data'] as $key => $value) {
|
|
|
|
// Skip if not active
|
|
if ($value['status'] !== 'active') { continue; }
|
|
// Skip if already completed
|
|
if ($value['is_action_done'] == 1) { continue; }
|
|
// Skip if action is None
|
|
if ($value['action'] === 'None') { continue; }
|
|
|
|
if($value['action'] == 'Approval'){ $template = 'plan_creation_approval_notification_to_approver'; }else if($value['action'] == 'Notification'){ $template = 'plan_creation_notification_to_approver'; }
|
|
|
|
$userData = $userModel->where('user_id', $value['user_id'])->first();
|
|
|
|
$url = urlButton($planId, $value['user_id']);
|
|
|
|
send_email($userData['org_id'] , $userData['email'], $template , $userData , $planData, $url);
|
|
|
|
// if action is notification update the action status as 1 (done)
|
|
$parallelProcessValue = $action['parallel_process_from'];
|
|
if($value['action'] == 'Notification'){
|
|
|
|
$planStatusModel->set([ $value['action_done_key'] => 1 ])
|
|
->where('plan_id', $planId)
|
|
->where($value['action_key'], 'Notification')
|
|
->where('is_active', 1)
|
|
->update();
|
|
|
|
if($parallelProcessValue == 2 && $value['action_key'] == 'a1_action')
|
|
{
|
|
sendPlanCreationMail($planId , 'send_mail_to_only_approver');
|
|
}
|
|
if($parallelProcessValue == 3)
|
|
{
|
|
sendPlanCreationMail($planId , 'send_mail_to_only_approver');
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//delegation part
|
|
$delegationUser = $userModel->where('user_id', $value['user_id'])
|
|
->where('is_active', 1)
|
|
->where('delegation_start_date <=', date('Y-m-d'))
|
|
->where('delegation_end_date >=', date('Y-m-d'))
|
|
->first();
|
|
if($delegationUser){
|
|
$delegatedToUserId = $delegationUser['delegated_to_user_id'];
|
|
|
|
$delegatedUserData = $userModel->where('user_id', $delegatedToUserId)->first();
|
|
|
|
$url = urlButton($planId, $value['user_id'] , $delegatedToUserId);
|
|
|
|
send_email($delegatedUserData['org_id'] , $delegatedUserData['email'], $template , $delegatedUserData , $planData, $url);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
if (!function_exists('sendPlanApprovalOrRejectMail')) {
|
|
function sendPlanApprovalOrRejectMail($planId,$action)
|
|
{
|
|
$planModel = new PlanModel();
|
|
$userModel = new UserModel();
|
|
$organizationModel = new OrganizationModel();
|
|
|
|
// Fetch plan data
|
|
$planData = $planModel->where('plan_id', $planId)->first();
|
|
if (!$planData) { return false; }
|
|
$userId = isset($planData['traveller_id']) && !empty($planData['traveller_id']) ? $planData['created_by'] : $planData['user_id'];
|
|
|
|
// Fetch user data
|
|
$userData = $userModel->where('user_id', $userId)->first();
|
|
if (!$userData) { return false; }
|
|
$groupId = $userData['group_id'];
|
|
$a1Id = $userData['first_approver'];
|
|
$a2Id = $userData['second_approver'];
|
|
$a3Id = $userData['third_approver'];
|
|
|
|
// Fetch Organization data
|
|
$orgData = $organizationModel->where('org_id', $planData['org_id'])->first();
|
|
|
|
//current plan status
|
|
$status = getCurrentPlanStatus($planId);
|
|
|
|
$statusHtml = '<h4>Trip Status:</h4>';
|
|
$statusHtml .= '<ul>';
|
|
foreach ($status as $key => $value) {
|
|
|
|
if (isset($value['a1_status'])) {
|
|
$statusHtml .= '<li style="margin-bottom: 5px;"><strong>Approver 1:</strong> ' . $value['a1_status'] . '</li>';
|
|
}
|
|
|
|
if (isset($value['a2_status'])) {
|
|
$statusHtml .= '<li style="margin-bottom: 5px;"><strong>Approver 2:</strong> ' . $value['a2_status'] . '</li>';
|
|
}
|
|
|
|
if (isset($value['a3_status'])) {
|
|
$statusHtml .= '<li style="margin-bottom: 5px;"><strong>Approver 3:</strong> ' . $value['a3_status'] . '</li>';
|
|
}
|
|
}
|
|
$statusHtml .= '</ul>';
|
|
$userData['status_details'] = $statusHtml;
|
|
|
|
if($action == 'A'){ $template = 'plan_approval_notification_to_traveller'; }else{ $template = 'plan_rejection_notification_to_traveller'; }
|
|
|
|
|
|
// send mail to traveller
|
|
send_email($orgData['org_id'] , $userData['email'], $template , $userData , $planData);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
if (!function_exists('sendPlanUpdationMail')) {
|
|
function sendPlanUpdationMail($planId)
|
|
{
|
|
$planModel = new PlanModel();
|
|
$userModel = new UserModel();
|
|
$organizationModel = new OrganizationModel();
|
|
$planStatusModel = new PlanStatusModel();
|
|
|
|
// Fetch plan data
|
|
$planData = $planModel->where('plan_id', $planId)->first();
|
|
if (!$planData) { return false; }
|
|
$userId = isset($planData['traveller_id']) && !empty($planData['traveller_id']) ? $planData['created_by'] : $planData['user_id'];
|
|
|
|
$currentStatus = $planData['status'];
|
|
if($currentStatus != 1){
|
|
$planModel->set(['status'=>1])->where('plan_id', $planId)->update();
|
|
$planStatusModel->set(['is_a1_action_done'=>0, 'a1_reject_reason'=>null, 'is_a2_action_done'=>0, 'a2_reject_reason'=>null, 'is_a3_action_done'=>0, 'a3_reject_reason'=>null,])->where('plan_id', $planId)->update();
|
|
}
|
|
|
|
// Fetch user data
|
|
$userData = $userModel->where('user_id', $userId)->first();
|
|
|
|
|
|
// send mail to traveller
|
|
send_email($planData['org_id'] , $userData['email'], 'plan_updation_notification_to_traveller' , $userData , $planData);
|
|
|
|
// send mail to approver
|
|
$action = getPlanApproverAction($planId);
|
|
|
|
foreach ($action['approver_data'] as $key => $value) {
|
|
|
|
// Skip if not active
|
|
if ($value['status'] !== 'active') { continue; }
|
|
// Skip if already completed
|
|
if ($value['is_action_done'] == 1) { continue; }
|
|
// Skip if action is None
|
|
if ($value['action'] === 'None') { continue; }
|
|
|
|
if($value['action'] == 'Approval'){ $template = 'plan_updation_approval_notification_to_approver'; }else if($value['action'] == 'Notification'){ $template = 'plan_updation_notification_to_approver'; }
|
|
|
|
$userData = $userModel->where('user_id', $value['user_id'])->first();
|
|
|
|
$url = urlButton($planId, $value['user_id']);
|
|
|
|
send_email($userData['org_id'] , $userData['email'], $template , $userData , $planData, $url);
|
|
|
|
// if action is notification update the action status as 1 (done)
|
|
$parallelProcessValue = $action['parallel_process_from'];
|
|
if($value['action'] == 'Notification'){
|
|
|
|
$planStatusModel->set([ $value['action_done_key'] => 1 ])
|
|
->where('plan_id', $planId)
|
|
->where($value['action_key'], 'Notification')
|
|
->where('is_active', 1)
|
|
->update();
|
|
|
|
if($parallelProcessValue == 2 && $value['action_key'] == 'a1_action')
|
|
{
|
|
sendPlanCreationMail($planId , 'send_mail_to_only_approver');
|
|
}
|
|
if($parallelProcessValue == 3)
|
|
{
|
|
sendPlanCreationMail($planId , 'send_mail_to_only_approver');
|
|
}
|
|
|
|
}
|
|
|
|
//delegation part
|
|
$delegationUser = $userModel->where('user_id', $value['user_id'])
|
|
->where('is_active', 1)
|
|
->where('delegation_start_date <=', date('Y-m-d'))
|
|
->where('delegation_end_date >=', date('Y-m-d'))
|
|
->first();
|
|
if($delegationUser){
|
|
$delegatedToUserId = $delegationUser['delegated_to_user_id'];
|
|
|
|
$delegatedUserData = $userModel->where('user_id', $delegatedToUserId)->first();
|
|
|
|
$url = urlButton($planId, $value['user_id'] , $delegatedToUserId);
|
|
|
|
send_email($delegatedUserData['org_id'] , $delegatedUserData['email'], $template , $delegatedUserData , $planData, $url);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
if (!function_exists('sendUserCreationMail')) {
|
|
function sendUserCreationMail($mailData)
|
|
{
|
|
|
|
// Fetch Organization data
|
|
$organizationModel = new OrganizationModel();
|
|
$orgData = $organizationModel->where('org_id', $mailData['org_id'] )->first();
|
|
|
|
// Fetch Template data
|
|
$mailTemplateModel = new MailTemplateModel();
|
|
$templateData = $mailTemplateModel->where('org_id', $mailData['org_id'] )->where('template_name', $mailData['template'] )->first();
|
|
|
|
//replace the placeholder
|
|
foreach ($mailData as $key => $value) {
|
|
$placeHolder = '%'.$key.'%';
|
|
$templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
|
|
}
|
|
|
|
$subject = $templateData['subject'];
|
|
$html = $templateData['body_html'];
|
|
$toEmailId = $mailData['email'];
|
|
|
|
$email = \Config\Services::email();
|
|
$email->initialize([
|
|
'protocol' => 'smtp',
|
|
'SMTPHost' => $orgData['mail_host'],
|
|
'SMTPUser' => $orgData['mail_user_name'],
|
|
'SMTPPass' => $orgData['mail_password'],
|
|
'SMTPPort' => (int)$orgData['mail_port'],
|
|
'mailType' => 'html'
|
|
]);
|
|
$email->setFrom($orgData['sender_email'], $orgData['name']);
|
|
$email->setTo($toEmailId);
|
|
$email->setSubject($subject);
|
|
$email->setMessage($html);
|
|
|
|
|
|
|
|
if ($email->send()) {
|
|
|
|
log_message('debug', 'Email Success to ' . $toEmailId);
|
|
return ['status' => 'success', 'code' => 200, 'message' => 'Email Sent Successfully...', 'data' => $toEmailId];
|
|
} else {
|
|
log_message('debug', 'Email sending failed: ' . $email->printDebugger(['headers']));
|
|
return ['status' => 'failed', 'code' => 404, 'message' => 'Email Sent Failed...', 'data' => $toEmailId];
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
if (!function_exists('send_email')) {
|
|
function send_email($org_id , $toEmailId, $template , $userData , $planData = null, $url = null)
|
|
{
|
|
try {
|
|
|
|
// Fetch Organization data
|
|
$organizationModel = new OrganizationModel();
|
|
$orgData = $organizationModel->where('org_id', $org_id )->first();
|
|
// Fetch Template data
|
|
$mailTemplateModel = new MailTemplateModel();
|
|
$templateData = $mailTemplateModel->where('org_id', $org_id )->where('template_name', $template )->first();
|
|
log_message('info', "Template : ". $template ." , ToMail : ". $toEmailId);
|
|
|
|
//replace the placeholder
|
|
foreach ($userData as $key => $value) {
|
|
$placeHolder = '%'.$key.'%';
|
|
$templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
|
|
}
|
|
|
|
if(!empty($planData)){
|
|
foreach ($planData as $key => $value) {
|
|
$placeHolder = '%'.$key.'%';
|
|
$templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
|
|
}
|
|
|
|
if($planData['exceptional_plan_reason'] != null && $planData['exceptional_plan_reason'] != '')
|
|
{
|
|
$reasonHtmlString = '<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
|
|
<tr>
|
|
<td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom:1px solid #c65e56;">
|
|
Reason for travel policy exception
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;border-bottom-left-radius:5px;border-bottom-right-radius:5px;">
|
|
'.$planData['exceptional_plan_reason'].'
|
|
</td>
|
|
</tr>
|
|
</table>';
|
|
|
|
$templateData['body_html'] = $templateData['body_html'].''.$reasonHtmlString;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if($url != null){
|
|
|
|
$placeHolder = '%trip_review_link%';
|
|
$templateData['body_html'] = str_replace($placeHolder, $url , $templateData['body_html']);
|
|
|
|
}
|
|
|
|
$subject = $templateData['subject'];
|
|
$html = $templateData['body_html'];
|
|
|
|
$email = \Config\Services::email(true);
|
|
$email->initialize([
|
|
'protocol' => 'smtp',
|
|
'SMTPHost' => $orgData['mail_host'],
|
|
'SMTPUser' => $orgData['mail_user_name'],
|
|
'SMTPPass' => $orgData['mail_password'],
|
|
'SMTPPort' => (int)$orgData['mail_port'],
|
|
'mailType' => 'html'
|
|
]);
|
|
$email->setFrom($orgData['sender_email'], $orgData['name']);
|
|
$email->setTo($toEmailId);
|
|
$email->setSubject($subject);
|
|
$email->setMessage($html);
|
|
|
|
if(!empty($planData)){
|
|
$email->attach($planData['pdf_file_path']);
|
|
}
|
|
|
|
|
|
|
|
// Try sending email
|
|
if ($email->send()) {
|
|
log_message('info', "[EMAIL SENT] To approver/traveller : {$toEmailId}, Template: {$template}");
|
|
return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
|
|
} else {
|
|
// Email send failed, log clean debug info
|
|
$debugInfo = $email->printDebugger(['headers', 'subject', 'body']);
|
|
log_message('error', "[EMAIL FAILED] To approver/traveller : {$toEmailId}, Template: {$template}\nError:\n" . strip_tags($debugInfo));
|
|
|
|
return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'data' => $toEmailId];
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
log_message('critical', "[EMAIL EXCEPTION] To approver/traveller : {$toEmailId}, Error: " . $e->getMessage().$e->getLine());
|
|
return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage(), 'data' => $toEmailId];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('send_email_agent')) {
|
|
function send_email_agent($org_id , $plan_id)
|
|
{
|
|
try {
|
|
|
|
// Fetch Organization data
|
|
$organizationModel = new OrganizationModel();
|
|
$orgData = $organizationModel->where('org_id', $org_id )->first();
|
|
// Fetch Template data
|
|
$mailTemplateModel = new MailTemplateModel();
|
|
$templateData = $mailTemplateModel->where('org_id', $org_id )->where('template_name', 'plan_notification_to_agent' )->first();
|
|
|
|
//Fetch plan data
|
|
$planController = new PlanController();
|
|
$planData = $planController->find($plan_id, 'internal');
|
|
if (!$planData) {
|
|
log_message('error', "No plan data found for plan_id: {$plan_id}");
|
|
return false;
|
|
}
|
|
|
|
$planServices = [];
|
|
if (count($planData['flight'])) { array_push($planServices, 1); }
|
|
if (count($planData['train'])) { array_push($planServices, 2); }
|
|
if (count($planData['bus'])) { array_push($planServices, 3); }
|
|
if (count($planData['taxi'])) { array_push($planServices, 4); }
|
|
if (count($planData['accomodation'])) { array_push($planServices, 5); }
|
|
if (count($planData['forex'])) { array_push($planServices, 6); }
|
|
if (count($planData['insurance'])) { array_push($planServices, 7); }
|
|
if (count($planData['visa'])) { array_push($planServices, 8); }
|
|
if (count($planData['miscellaneous'])) { array_push($planServices, 9); }
|
|
|
|
// dd($planServices);
|
|
|
|
|
|
|
|
//Fetch Agent Data
|
|
$userModel = new UserModel();
|
|
$userData = $userModel->where('role_id', 5)->where('is_active', 1)->findAll();
|
|
if (!$userData) {
|
|
log_message('error', "User data not found for agent");
|
|
return false;
|
|
}
|
|
|
|
$email = \Config\Services::email();
|
|
$email->initialize([
|
|
'protocol' => 'smtp',
|
|
'SMTPHost' => $orgData['mail_host'],
|
|
'SMTPUser' => $orgData['mail_user_name'],
|
|
'SMTPPass' => $orgData['mail_password'],
|
|
'SMTPPort' => (int)$orgData['mail_port'],
|
|
'mailType' => 'html'
|
|
]);
|
|
|
|
foreach ($userData as $key => $value) {
|
|
$jsonString = $value['agent_supported_service_ids'];
|
|
|
|
// Decode JSON string into PHP array
|
|
$agentService = json_decode($jsonString, true);
|
|
$agentServiceArray = [];
|
|
if (is_array($agentService)) {
|
|
foreach ($agentService as $service) {
|
|
array_push($agentServiceArray,$service['service_id']);
|
|
}
|
|
}
|
|
|
|
$common = array_intersect($planServices, $agentServiceArray);
|
|
|
|
if (!empty($common)) {
|
|
|
|
//send mail to agent
|
|
$placeHolderData['agent_name'] = $value['first_name'].' '.$value['last_name'];
|
|
|
|
foreach ($placeHolderData as $placeHolderkey => $placeHoldervalue) {
|
|
$placeHolder = '%'.$placeHolderkey.'%';
|
|
$templateData['body_html'] = str_replace($placeHolder, $placeHoldervalue ?? '', $templateData['body_html']);
|
|
}
|
|
|
|
$subject = $templateData['subject'];
|
|
$html = $templateData['body_html'];
|
|
|
|
//trigger_email_to agent
|
|
$email->setFrom($orgData['sender_email'], $orgData['name']);
|
|
$email->setTo($value['email']);
|
|
$email->setSubject($subject);
|
|
$email->setMessage($html);
|
|
|
|
if(!empty($planData)){
|
|
$email->attach($planData['pdf_file_path']);
|
|
}
|
|
|
|
if ($email->send()) {
|
|
|
|
log_message('debug', 'Email Success to agent plan_id = ' .$plan_id.'email = ' . $value['email']);
|
|
|
|
} else {
|
|
log_message('debug', 'Email sending failed: agent plan_id = ' .$plan_id.'email = ' . $value['email'].'error = '.$email->printDebugger(['headers']));
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
log_message('debug', 'Exception occurred while sending email to agent: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('check_mail_config')) {
|
|
function check_mail_config($to_email, $mail_config = [])
|
|
{
|
|
if (empty($mail_config['SMTPHost']) ||
|
|
empty($mail_config['SMTPUser']) ||
|
|
empty($mail_config['SMTPPass']) ||
|
|
empty($mail_config['SMTPPort']) ||
|
|
empty($mail_config['sender_email']) ) {
|
|
return json_encode(['status' => 400, 'message' => 'Incomplete Mail Configuration']);
|
|
}
|
|
|
|
|
|
$email = \Config\Services::email();
|
|
$email->initialize([
|
|
'protocol' => 'smtp',
|
|
'mailType' => 'html',
|
|
'SMTPHost' => $mail_config['SMTPHost'],
|
|
'SMTPUser' => $mail_config['SMTPUser'],
|
|
'SMTPPass' => $mail_config['SMTPPass'],
|
|
'SMTPPort' => (int)$mail_config['SMTPPort']
|
|
]);
|
|
|
|
$html = '<html><p style="text-align: center;">Test Mail Configuration </p></html>';
|
|
$email->setFrom($mail_config['sender_email'], '');
|
|
$email->setTo($to_email);
|
|
$email->setSubject("Testing Mail Configuration");
|
|
$email->setMessage($html);
|
|
|
|
if ($email->send()) {
|
|
return json_encode(['status' => 200, 'message' => 'Email Sent Successfully...']);
|
|
} else {
|
|
log_message('error', 'Email sending failed: ' . $email->printDebugger(['headers']));
|
|
return json_encode(['status' => 404, 'message' => 'Email Sent Failed...']);
|
|
}
|
|
}
|
|
} |