FEAT_CREATE_LEAD_MAIL_SEND_FIX_EXPORT_ISSUE_IN_CD

This commit is contained in:
VENKATESHWARAN 2026-06-04 14:13:29 +05:30
parent 9f3bd4162f
commit 5291609040
2 changed files with 167 additions and 5 deletions

View File

@ -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 '<p>Hello Team,</p>'
. '<p>A new lead has been created</p>'
. '<h3>Lead Details</h3>'
. '<p><strong>Company Name :</strong> ' . $e($companyName) . '</p>'
. '<p><strong>Email :</strong> ' . $e($leadEmail) . '</p>'
. '<p><strong>Mobile :</strong> ' . $e($leadMobile) . '</p>'
. '<p><strong>Status :</strong> ' . $e($leadStatus) . '</p>'
. '<h3>Created By</h3>'
. '<p><strong>Name :</strong> ' . $e($createdByName) . '</p>'
. '<p><strong>Email :</strong> ' . $e($createdByEmail) . '</p>'
. '<p><strong>Branch :</strong> ' . $e($createdByBranch) . '</p>'
. '<h3>Assigned To</h3>'
. '<p><strong>Name :</strong> ' . $e($assignedUserName) . '</p>'
. '<p><strong>Email :</strong> ' . $e($assignedUserEmail) . '</p>'
. '<h3>Created On</h3>'
. '<p><strong>Date &amp; Time :</strong> ' . $e($createdDateTime) . '</p>'
. '<p>Regards,<br>NHANCE INDIA INSURANCE BROKING PRIVATE LIMITED</p>';
}
}

View File

@ -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 $('<div>').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: '<i class="mdi mdi-file-delimited " ></i><span class=" btn-custom"> CSV </span>',
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: '<i class="mdi mdi-file-excel " ></i><span class=" btn-custom"> EXCEL </span>',
title: 'CD TransactionDetails',
sheetName: 'CD TransactionDetails',
exportOptions: {
orthogonal: 'sort'
}
exportOptions: $.extend(true, { orthogonal: 'sort' }, depositExportOptions)
}
]
}