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

' + . '

Lead Details

' + . '

Company Name : ' . $e($companyName) . '

' + . '

Email : ' . $e($leadEmail) . '

' + . '

Mobile : ' . $e($leadMobile) . '

' + . '

Status : ' . $e($leadStatus) . '

' + . '

Created By

' + . '

Name : ' . $e($createdByName) . '

' + . '

Email : ' . $e($createdByEmail) . '

' + . '

Branch : ' . $e($createdByBranch) . '

' + . '

Assigned To

' + . '

Name : ' . $e($assignedUserName) . '

' + . '

Email : ' . $e($assignedUserEmail) . '

' + . '

Created On

' + . '

Date & Time : ' . $e($createdDateTime) . '

' + . '

Regards,
NHANCE INDIA INSURANCE BROKING PRIVATE LIMITED

'; + } } \ No newline at end of file diff --git a/app/Views/view_deposit.php b/app/Views/view_deposit.php index 5bd008a9..29d833ee 100755 --- a/app/Views/view_deposit.php +++ b/app/Views/view_deposit.php @@ -668,6 +668,38 @@ $vd_col_width_px = nhance_dt_column_widths_px($vd_header_labels, $vd_col_max_len }); nhanceListDataTableBeforeInit(); + function depositExportFormatBody(data, row, column, node) { + if (column === 6 || column === 7) { + var $input = $(node).find('.deposit-inline-input'); + if ($input.length) { + var val = ($input.val() || '').trim(); + return val !== '' ? val : '-'; + } + var $tr = $(node).closest('tr'); + var credit = parseFloat($tr.attr('data-credit')) || 0; + var debit = parseFloat($tr.attr('data-debit')) || 0; + if (column === 6) { + return credit > 0 ? String(credit) : '-'; + } + return debit > 0 ? String(debit) : '-'; + } + if (column === 8) { + var $balanceSpan = $(node).find('.deposit-balance-wrap > span').first(); + if ($balanceSpan.length) { + return ($balanceSpan.text() || '').trim(); + } + return ($(node).text() || '').trim(); + } + if (typeof data === 'string' && data.indexOf('<') !== -1) { + return $('
').html(data).text().trim(); + } + return data; + } + var depositExportOptions = { + format: { + body: depositExportFormatBody + } + }; var depositPageStorageKey = 'view_deposit_datatable_page_' + window.location.pathname + window.location.search; function getStoredDepositPage() { var page = parseInt(sessionStorage.getItem(depositPageStorageKey), 10); @@ -780,7 +812,7 @@ $vd_col_width_px = nhance_dt_column_widths_px($vd_header_labels, $vd_col_max_len extend: 'csv', text: ' CSV ', title: 'CD TransactionDetails', - exportOptions: { columns: '' }, + exportOptions: $.extend(true, { columns: '' }, depositExportOptions), className: 'app-btn-primary ', }, { @@ -788,9 +820,7 @@ $vd_col_width_px = nhance_dt_column_widths_px($vd_header_labels, $vd_col_max_len text: ' EXCEL ', title: 'CD TransactionDetails', sheetName: 'CD TransactionDetails', - exportOptions: { - orthogonal: 'sort' - } + exportOptions: $.extend(true, { orthogonal: 'sort' }, depositExportOptions) } ] }