diff --git a/app/Controllers/DashboardController.php b/app/Controllers/DashboardController.php index 08a2b82a..3ff685d7 100755 --- a/app/Controllers/DashboardController.php +++ b/app/Controllers/DashboardController.php @@ -1015,34 +1015,27 @@ class DashboardController extends AdminController $db2 = \Config\Database::connect('preDB'); - $builder = $db2->table('employees'); - $total_employee_draft_count = $builder->join('employee_polices', 'employees.id = employee_polices.employee_id') - ->select('employee_polices.status, count(*) as count') + $total_employee_draft_count = $db2->table('employee_polices') + ->join('employees', 'employees.id = employee_polices.employee_id') ->where('DATE(employee_polices.created_at)', $today) ->where('employee_polices.is_active', 1) ->where('employees.is_active', 1) - ->whereIn('employee_polices.status', ['draft']) - ->groupBy('employee_polices.status') + ->where('employee_polices.status', 'draft') ->countAllResults(); - - $builder1 = $db2->table('employees'); - $total_employee_enrolled_count = $builder1->join('employee_polices', 'employees.id = employee_polices.employee_id') - ->select('employee_polices.status, count(*) as count') + $total_employee_enrolled_count = $db2->table('employee_polices') + ->join('employees', 'employees.id = employee_polices.employee_id') ->where('DATE(employee_polices.created_at)', $today) ->where('employee_polices.is_active', 1) ->where('employees.is_active', 1) - ->whereIn('employee_polices.status', ['enrolled']) - ->groupBy('employee_polices.status') + ->where('employee_polices.status', 'enrolled') ->countAllResults(); - - $builder2 = $db2->table('files'); - $total_open_for_enrollemnt_policy_count = $builder2 - ->select('COUNT(*) as count') - ->where('enrollment_open_date <=', $today) - ->where('enrollment_close_date >=', $today) + + $total_open_for_enrollemnt_policy_count = $db2->table('client_policy') + ->where('open_date <=', $today) + ->where('close_date >=', $today) ->where('is_active', 1) - ->where('status', 'success') + ->where('policy_status', 1) ->countAllResults(); $data = [ diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 3066ada1..fcf7c9f6 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -2266,6 +2266,7 @@ class EmployeeRestController extends AdminController client_policy.insurer_id as insurer_id, DATE_FORMAT(client_policy.policy_start_date, '%d-%m-%Y') AS policy_start_date, DATE_FORMAT(client_policy.policy_end_date, '%d-%m-%Y') AS policy_expiry_date, + clients.client_logo as client_logo, ( select COALESCE(ROUND(SUM(rata_premimum + gst)), 0) from employee_polices @@ -2275,6 +2276,7 @@ class EmployeeRestController extends AdminController ) as total_premium, CASE WHEN policy_type.allocg IN ('Non-EB', 'Marine') THEN 'Non-EB' ELSE 'EB' END as allocg ", false) + ->join('clients', 'clients.id = client_policy.client_id', 'left') ->join('policy_type', 'policy_type.id = client_policy.policy_type_id', 'left') ->where('md5(client_policy.client_id)', $this->request->getGet('client_id')) ->where('client_policy.client_branch_id', $this->request->getGet('client_branch_id')) @@ -2293,6 +2295,7 @@ class EmployeeRestController extends AdminController $value['policy_name'] = $policyTypeData->long_name; $value['insurer_name'] = $insurerData->name; $value['insurer_short_name'] = $insurerData->short_name; + $value['client_logo'] = ! empty($value['client_logo']) ? base_url() . 'public/uploads/logo/' . $value['client_logo'] : ''; $employeeDetails = $this->employeePolicyModel->getEmployeePolicy(client_id: $value['client_id'], policy_id: $value['client_policy_id'], status: 0, branch_id: $this->request->getGet('client_branch_id'), status_type: 'inactive'); // dd($employeeDetails); $activeCount = 0; diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 26da53b2..a8d1ab90 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -39,6 +39,7 @@ use App\Helpers\ExcelSanitizeHelper; use App\Models\NhanceBranchModel; use App\Models\BDSDumpModel; use App\Models\VehicleModel; +use CodeIgniter\CLI\CLI; use Exception; class PolicyTransactionController extends BaseController @@ -5324,21 +5325,18 @@ class PolicyTransactionController extends BaseController $bdsInstallmentData = []; try { - - $bdsInstallmentData = $this->BdsPlacementModel->getClientInstallmentDetails(); + $bdsInstallmentData = $this->BdsPlacementModel->getLeadInstallmentDetails(); } catch (Exception $e) { - $this->myLogger->logme('error', 'Exception: ' . $e->getMessage() . 'Page: ' . $e->getFile() . ' Line: ' . $e->getLine()); } $this->myLogger->logme("error", "Data for BDS Installment " . json_encode($bdsInstallmentData)); - // dd($bdsInstallmentData); + // print_r($bdsInstallmentData);die(); if (empty($bdsInstallmentData)) { - $this->myLogger->logme("error", "No Client Installment is Due in the 15th Day"); - - return false; + CLI::write("No Client Installment is Due in the 15th Day"); + return ['status' => false, 'message' => 'No data found', 'response' => []]; } foreach ($bdsInstallmentData as $installmentData) { @@ -5358,13 +5356,13 @@ class PolicyTransactionController extends BaseController $this->myLogger->logme('error', 'res: ' . json_encode($res)); if ($res->status == 'success') { - return true; + return ['status' => true, 'message' => 'Mail sent successfully', 'response' => $res]; } else { - return false; + return ['status' => false, 'message' => 'Mail sent failed', 'response' => $res]; } } - } catch (Exception $e) { + } catch (Exception $e) { $this->myLogger->logme('error', 'Exception: ' . $e->getMessage() . 'Page: ' . $e->getFile() . ' Line: ' . $e->getLine()); } } diff --git a/app/Controllers/SalesController.php b/app/Controllers/SalesController.php index edf64242..cd55d3a3 100644 --- a/app/Controllers/SalesController.php +++ b/app/Controllers/SalesController.php @@ -10,6 +10,7 @@ use App\Models\SalesLeadNoteModel; use App\Models\SalesTargetModel; use App\Models\UserModel; use App\Models\ClientModel; +use App\Helpers\MailHelper; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\API\ResponseTrait; @@ -330,7 +331,10 @@ class SalesController extends BaseController return $this->fail($this->leadModel->errors()); } - return $this->respondCreated(['status' => 'success', 'id' => $this->leadModel->getInsertID()]); + $leadId = $this->leadModel->getInsertID(); + $this->sendLeadCreateMail($leadId, $data); + + return $this->respondCreated(['status' => 'success', 'id' => $leadId]); } catch (\Exception $e) { return $this->failServerError($e->getMessage()); } @@ -2595,4 +2599,132 @@ public function salesManagerLevelDashboard($userId, $current_fin_year = null, $f ]; } } + + /** + * Send lead-create notification email when the logged-in user's branch is Chennai. + */ + private function sendLeadCreateMail(int $leadId, array $leadData): void + { + try { + if (!$this->isLoggedInUserChennaiBranch()) { + return; + } + + $toMail = trim((string) getenv('LEAD_CREATE_MAIL')); + if ($toMail === '') { + log_message('error', 'sendLeadCreateMail: LEAD_CREATE_MAIL is not configured.'); + return; + } + + $recipientEmails = array_filter(array_map('trim', explode(',', $toMail))); + if (empty($recipientEmails)) { + log_message('error', 'sendLeadCreateMail: No valid recipients in LEAD_CREATE_MAIL.'); + return; + } + + $creator = $this->getLeadCreateMailCreatorDetails(); + $assignedUser = $this->userModel->where('id', $leadData['assigned_to'] ?? 0)->first() ?? []; + + $companyName = $leadData['company_name'] ?? ''; + $subject = 'New Lead Created - ' . $companyName; + $message = $this->buildLeadCreateMailMessage( + $companyName, + $leadData['email'] ?? '', + $leadData['phone'] ?? '', + $leadData['status'] ?? '', + $creator['name'] ?? '', + $creator['email'] ?? '', + $creator['branch'] ?? '', + $assignedUser['first_name'] ?? '', + $assignedUser['email'] ?? '', + date('d/m/Y h:i A') + ); + + $res = MailHelper::send_email([ + 'mail' => $recipientEmails, + 'subject' => $subject, + 'message' => $message, + 'common' => ['module' => 'sales', 'pk' => $leadId, 'mail_type' => 'lead_create'], + ]); + + $resDecoded = is_string($res) ? json_decode($res, true) : $res; + if (!isset($resDecoded['status']) || $resDecoded['status'] !== 'success') { + log_message('error', 'sendLeadCreateMail: Email send failed - ' . json_encode($resDecoded)); + } + } catch (\Throwable $e) { + log_message('error', 'sendLeadCreateMail: ' . $e->getMessage()); + } + } + + private function isLoggedInUserChennaiBranch(): bool + { + $userId = get_session_userid(); + if (empty($userId)) { + return false; + } + + $row = $this->userModel + ->select('id') + ->where('id', $userId) + ->where('nhance_branch_id', 1) + ->first(); + + return !empty($row); + } + + private function getLeadCreateMailCreatorDetails(): array + { + $userId = get_session_userid(); + $db = \Config\Database::connect(); + + $row = $db->table('user_profiles up') + ->select('up.first_name, up.email, nb.branch_name') + ->join('nhance_branch nb', 'nb.id = up.nhance_branch_id', 'left') + ->where('up.id', $userId) + ->get() + ->getRowArray(); + + if (!$row) { + return ['name' => '', 'email' => '', 'branch' => '']; + } + + return [ + 'name' => $row['first_name'] ?? '', + 'email' => $row['email'] ?? '', + 'branch' => $row['branch_name'] ?? '', + ]; + } + + private function buildLeadCreateMailMessage( + string $companyName, + string $leadEmail, + string $leadMobile, + string $leadStatus, + string $createdByName, + string $createdByEmail, + string $createdByBranch, + string $assignedUserName, + string $assignedUserEmail, + string $createdDateTime + ): string { + $e = static fn (?string $value): string => htmlspecialchars((string) ($value ?? ''), ENT_QUOTES, 'UTF-8'); + + return '
Hello Team,
' + . 'A new lead has been created
' + . 'Company Name : ' . $e($companyName) . '
' + . 'Email : ' . $e($leadEmail) . '
' + . 'Mobile : ' . $e($leadMobile) . '
' + . 'Status : ' . $e($leadStatus) . '
' + . 'Name : ' . $e($createdByName) . '
' + . 'Email : ' . $e($createdByEmail) . '
' + . 'Branch : ' . $e($createdByBranch) . '
' + . 'Name : ' . $e($assignedUserName) . '
' + . 'Email : ' . $e($assignedUserEmail) . '
' + . 'Date & Time : ' . $e($createdDateTime) . '
' + . 'Regards,
NHANCE INDIA INSURANCE BROKING PRIVATE LIMITED