1138 lines
48 KiB
PHP
1138 lines
48 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Requests\LambdaRequest;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use DB;
|
||
use Log;
|
||
use Maatwebsite\Excel\Facades\Excel;
|
||
use App\Exports\InvoicesExport;
|
||
use Illuminate\Support\Str;
|
||
use Carbon\Carbon;
|
||
|
||
class MilestoneController extends Controller
|
||
{
|
||
public function __construct()
|
||
{
|
||
$this->middleware('auth')->except(['publicTeamReport', 'exportTeamReport']);
|
||
$this->_service = 'formreminders';
|
||
$this->_module = 'milestones';
|
||
$this->_viewname = 'milestone';
|
||
$this->_title = 'Milestone View';
|
||
$this->_base_url = '/milestone';
|
||
}
|
||
|
||
public function list(Request $request)
|
||
{
|
||
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module . '/get_all');
|
||
// echo '<pre>'; print_r($response); exit;
|
||
|
||
|
||
$fields = $this->get_list_fields();
|
||
return $this->view('list')->with([
|
||
'title' => $this->_title,
|
||
'fields' => $fields,
|
||
'base_url' => $this->_base_url,
|
||
'items' => $response->data->items,
|
||
'buttons' => $this->get_list_buttons()
|
||
]);
|
||
}
|
||
|
||
protected function get_list_fields()
|
||
{
|
||
return [
|
||
'actions' => ['title' => 'Actions', 'buttons' => ['edit', 'delete']],
|
||
'title' => ['title' => 'Name', 'type' => 'text'],
|
||
'status' => ['title' => 'Status', 'type' => 'select'],
|
||
];
|
||
}
|
||
|
||
protected function get_list_buttons()
|
||
{
|
||
return [
|
||
['title' => 'Add New', 'link' => $this->_base_url . '/addnew'],
|
||
];
|
||
}
|
||
|
||
protected function get_form_fields($type, $values)
|
||
{
|
||
$metrics = $this->getlambda('get', 'metric', 'metric', 'get_all');
|
||
$metrics_dd = [];
|
||
foreach ($metrics->data->items as $metric) {
|
||
$metrics_dd[$metric->id] = $metric->name;
|
||
}
|
||
|
||
$badge = $this->getlambda('get', 'recognition', 'badge_list', 'get_all');
|
||
$badge_dd = [];
|
||
foreach ($badge->data->items as $bd) {
|
||
$badge_dd[$bd->id] = $bd->title;
|
||
}
|
||
|
||
$apprcat = $this->getlambda('get', 'recognition', 'appreciation_category', 'get_all');
|
||
$apprcat_dd = [];
|
||
foreach ($apprcat->data->items as $bd) {
|
||
$apprcat_dd[$bd->id] = $bd->title;
|
||
}
|
||
// echo '<pre>'; print_r($apprcat_dd); exit;
|
||
|
||
// $domain = LambdaRequest::signedrequest('get', 'ums', 'domain/get_one');
|
||
// //echo '<pre>'; print_r($domain); exit;
|
||
// $designations_dd = [];
|
||
// foreach($domain->data->item->designations as $designation){
|
||
// $designations_dd[$designation] = $designation;
|
||
// }
|
||
//echo '<pre>'; print_r($values); exit;
|
||
|
||
$roles = $this->getlambda('get', 'ums', 'roles', 'dropdown');
|
||
$roles_dd = [];
|
||
foreach ($roles->data->items as $role) {
|
||
$roles_dd[$role->id] = $role->name;
|
||
}
|
||
|
||
$coattr = $this->getlambda('get', 'contact', 'contactattribute', 'get_all');
|
||
$coattr_dd = [];
|
||
foreach ($coattr->data->items as $coatt) {
|
||
$coattr_dd[$coatt->code] = $coatt->attribute;
|
||
}
|
||
|
||
$apprimg = $this->getlambda('get', 'recognition', 'appreciation_image', 'get_all');
|
||
$apprimg_dd = [];
|
||
foreach ($apprimg->data->items as $appr) {
|
||
$apprimg_dd[$appr->id] = [
|
||
'title' => $appr->title,
|
||
'image' => $appr->image
|
||
];
|
||
}
|
||
// echo '<pre>'; print_r($apprimg_dd); exit;
|
||
|
||
//echo '<pre>'; print_r($coattr_dd); exit;
|
||
|
||
$userattr_dd = $this->getUserAttributeOptions();
|
||
|
||
$dates_dd = ['L' => 'Last day of month'];
|
||
$date = 1;
|
||
while($date <= 31) {
|
||
$dates_dd[$date] = $date;
|
||
$date++;
|
||
}
|
||
|
||
$fields = [
|
||
'title' => ['title' => 'Title', 'type' => 'text', 'required' => true],
|
||
'interval_start_date' => ['title' => 'Interval Start Date', 'type' => 'date', 'required' => true],
|
||
'interval_end_date' => ['title' => 'Interval End Date', 'type' => 'date', 'required' => true],
|
||
// 'is_recurring' => ['title' => 'Is Recurring', 'type' => 'select', 'required' => true, 'values' => ['Yes' => 'Yes', 'No' => 'No']],
|
||
'period' => ['title' => 'Period', 'type' => 'select', 'required' => true, 'values' => ['daily' => 'Daily', 'weekly' => 'Weekly', 'monthly'=> 'Monthly', 'yearly' => 'Yearly', 'overall' => 'Overall', 'custom' => 'Custom'] ],
|
||
'custom_start_date' => ['title' => 'Custom Start Date', 'type' => 'date', 'required' => false],
|
||
'custom_end_date' => ['title' => 'Custom End Date', 'type' => 'date', 'required' => false],
|
||
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled']],
|
||
'show_positions' => ['title' => 'Show Positions', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No']],
|
||
'show_positions_type' => ['title' => 'Show Positions Type', 'type' => 'select', 'required' => true, 'values' => ['all' => 'All', 'loggedin' => 'Logged In']],
|
||
'show_units_export' => ['title' => 'Show Units Export', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No'], 'default' => 'yes'],
|
||
'show_user_attributes' => ['title' => 'Show User Attributes', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No'], 'default' => 'no'],
|
||
'user_attributes' => ['title' => 'Include User Attributes', 'type' => 'multiselect', 'values' => $userattr_dd, 'default' => []],
|
||
'show_normal_metrics' => ['title' => 'Show Normal Metrics', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No'], 'default' => 'yes'],
|
||
'show_combined_metrics' => ['title' => 'Show Combined Metrics', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No'], 'default' => 'yes'],
|
||
'manager_details' => ['title' => 'Manager Details', 'type' => 'multiselect', 'values' => ['l1manager' => 'L1 Manager', 'l2manager' => 'L2 Manager', 'l3manager' => 'L3 Manager', 'l4manager' => 'L4 Manager'], 'default' => ['l1manager']],
|
||
'total_units_name' => ['title' => 'Total Units Name', 'type' => 'text', 'required' => false, 'default' => ''],
|
||
'send_report' => ['title' => 'Send Report', 'type' => 'select', 'required' => true, 'values' => ['yes' => 'Yes', 'no' => 'No']],
|
||
'performance_period' => ['title' => 'Performance Period', 'type' => 'select', 'required' => false, 'values' => ['daily' => 'Daily', 'weekly' => 'Weekly', 'monthly' => 'Monthly']],
|
||
'days_or_dates' => ['title' => 'Days or Dates', 'type' => 'radio', 'required' => false, 'fullwidth' => true, 'values' => ['days' => 'Days', 'dates' => 'Dates']],
|
||
'days' => ['title' => 'Days', 'type' => 'checkbox', 'required' => false, 'fullwidth' => true, 'values' => ['MON' => 'Monday', 'TUE' => 'Tuesday', 'WED' => 'Wednesday', 'THU' => 'Thursday', 'FRI' => 'Friday', 'SAT' => 'Saturday', 'SUN' => 'Sunday']],
|
||
'dates' => ['title' => 'Dates', 'type' => 'multiselect', 'required' => false, 'values' => $dates_dd],
|
||
'send_time' => ['title' => 'Send Time', 'type' => 'text', 'required' => false],
|
||
'send_report_roles' => ['title' => 'Send Report Roles', 'type' => 'multiselect', 'required' => false, 'values' => $roles_dd],
|
||
'report_roles' => ['title' => 'Report Roles', 'type' => 'multiselect', 'required' => false, 'values' => $roles_dd],
|
||
'send_user_performance' => ['title' => 'Send User Performance', 'type' => 'select', 'required' => false, 'values' => ['yes' => 'Yes', 'no' => 'No']],
|
||
'ai_action_items' => ['title' => 'AI Action Items', 'type' => 'select', 'required' => false, 'values' => ['yes' => 'Yes', 'no' => 'No']],
|
||
'send_pending_users' => ['title' => 'Send Pending Users', 'type' => 'select', 'required' => false, 'values' => ['yes' => 'Yes', 'no' => 'No']],
|
||
'show_target_average' => ['title' => 'Show Target Average', 'type' => 'select', 'required' => false, 'values' => ['yes' => 'Yes', 'no' => 'No']],
|
||
'show_overall_average' => ['title' => 'Show Overall Average', 'type' => 'select', 'required' => false, 'values' => ['yes' => 'Yes', 'no' => 'No']],
|
||
'subject' => ['title' => 'Report Subject', 'type' => 'text', 'required' => false],
|
||
'body' => ['title' => 'Report Body', 'type' => 'textarea', 'required' => false],
|
||
|
||
];
|
||
|
||
$fields['metrics'] = ['title' => 'Metric Mapping', 'type' => 'mapping', 'required' => true, 'metrics' => $metrics_dd, 'types' => ['unit' => 'Units', 'count' => 'Count'], 'fullwidth' => true];
|
||
|
||
$fields['milestones'] = ['title' => 'Milestone', 'type' => 'milestonemapping', 'required' => true, 'roles' => $roles_dd, 'badges' => $badge_dd, 'appreciation_categories' => $apprcat_dd, 'appreciation_image' => $apprimg_dd, 'types' => ['unit' => 'Units', 'count' => 'Count'], 'fullwidth' => true];
|
||
|
||
// $fields = array_merge(
|
||
// $fields,
|
||
// [
|
||
// 'operation' => ['title' => 'Operation', 'type' => 'select', 'required' => true, 'values' => ['sum' => 'Sum', 'avg' => 'Avg', 'value_count' => 'Count']],
|
||
// 'agg_type' => ['title' => 'Aggregation Type', 'type' => 'select', 'required' => true, 'values' => ['individual' => 'Individual', 'team' => 'Team']],
|
||
|
||
// 'graphical_views' => ['title' => 'Graphical views', 'type' => 'multiselect', 'values' => ['progress' => 'Progress', 'percentile' => 'Percentile', 'pie' => 'Pie chart', 'bar' => 'Bar chart', 'line' => 'Line chart']],
|
||
// 'details_views' => ['title' => 'Details views', 'type' => 'multiselect', 'required' => true, 'values' => ['tabular' => 'Tabular', 'contacts' => 'Doctor View', 'incremental' => 'Incremental View']],
|
||
// 'trend_views' => ['title' => 'Trend views', 'type' => 'multiselect', 'values' => ['bar' => 'Bar chart', 'line' => 'Line chart', 'tabular' => 'Tabular']],
|
||
// 'leaderboard_views' => ['title' => 'Leaderboard views', 'type' => 'multiselect', 'values' => ['leaderboard' => 'Leaderboard View']],
|
||
// 'contacts_views' => ['title' => 'Doctor views', 'type' => 'multiselect', 'values' => ['contacts' => 'Doctor View']],
|
||
// 'doctor_view_name' => ['title' => 'Enter Doctor View Name', 'type' => 'text', 'required' => false],
|
||
|
||
// 'intervals' => ['title' => 'Intervals', 'type' => 'multiselect', 'required' => true, 'values' => ['yearlycy' => 'Calender Year', 'yearlyfy' => 'Financial Year', 'monthly' => 'Monthly', 'weekly' => 'Weekly', 'daily' => 'Daily']],
|
||
|
||
// 'default_interval' => ['title' => 'Default Interval', 'type' => 'select', 'required' => true, 'values' => ['month-0' => 'This Month', 'month-1' => 'Last Month', 'month-3' => 'Last 3 Months', 'month-6' => 'Last 6 Months', 'month-12' => 'Last 12 Months', 'day-0' => 'Today', 'day-1' => 'Yesterday', 'day-7' => 'Last 7 Days', 'day-30' => 'Last 30 Days', 'week-0' => 'This week', 'week-1' => 'Last week', 'week-2' => '-2 week', 'week-3' => '-3 week', 'week-4' => '-4 week', 'yearfy-0' => 'This Financial Year', 'yearfy-1' => 'Last Financial Year', 'yearcy-0' => 'This Calender Year', 'yearcy-1' => 'Last Calender Year']],
|
||
// 'widget_sort_order' => ['title' => 'Widget Sort Order', 'type' => 'number', 'required' => true],
|
||
// 'doctor_type' => ['title' => 'Doctor Type', 'type' => 'text', 'required' => false],
|
||
// 'combined_metrics' => ['title' => 'Combined Metrics (if any)', 'type' => 'combined_metrics', 'fullwidth' => true],
|
||
|
||
// 'report_roles' => ['title' => 'Data Report from Roles', 'type' => 'multiselect', 'values' => $roles_dd, 'required' => true],
|
||
|
||
// 'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled']],
|
||
// 'show_positions' => ['title' => 'Show Position in Reports', 'type' => 'select', 'required' => true, 'values' => ['Yes' => 'Yes', 'No' => 'No']],
|
||
|
||
// 'contact_attributes' => ['title' => 'Include Doctor Attributes', 'type' => 'multiselect', 'values' => $coattr_dd],
|
||
// ]
|
||
// );
|
||
|
||
return $fields;
|
||
}
|
||
|
||
protected function _beforesave($type, $request)
|
||
{
|
||
$data = $request->except('_token');
|
||
// echo '<pre>';
|
||
// print_r($data);
|
||
// exit;
|
||
$milestones = [];
|
||
$count = count($data['target'] ?? []);
|
||
|
||
$start = Carbon::createFromFormat('m/d/Y', $data['interval_start_date'])->format('Y-m-d');
|
||
$end = Carbon::createFromFormat('m/d/Y', $data['interval_end_date'])->format('Y-m-d');
|
||
|
||
// if (!empty($data['custom_start_date'])) {
|
||
// $data['custom_start_date'] = Carbon::createFromFormat('m/d/Y', $data['custom_start_date'])->format('Y-m-d');
|
||
// }
|
||
|
||
// if (!empty($data['custom_end_date'])) {
|
||
// $data['custom_end_date'] = Carbon::createFromFormat('m/d/Y', $data['custom_end_date'])->format('Y-m-d');
|
||
// }
|
||
|
||
$data['custom_start_date'] = !empty($data['custom_start_date'])
|
||
? Carbon::createFromFormat('m/d/Y', $data['custom_start_date'])->format('Y-m-d')
|
||
: '';
|
||
|
||
$data['custom_end_date'] = !empty($data['custom_end_date'])
|
||
? Carbon::createFromFormat('m/d/Y', $data['custom_end_date'])->format('Y-m-d')
|
||
: '';
|
||
|
||
|
||
$data['interval'] = $start . '_' . $end;
|
||
|
||
for ($i = 0; $i < $count; $i++) {
|
||
|
||
$milestones[] = [
|
||
|
||
"milestone_id" => $data['milestone_id'][$i] ?? Str::uuid()->toString(),
|
||
|
||
"target" => [
|
||
"value" => (int) ($data['target'][$i] ?? 0),
|
||
"target_title" => $data['target_title'][$i] ?? '',
|
||
],
|
||
|
||
"repeat_target" => ($data['repeat_target'][$i] ?? '0') === '1',
|
||
|
||
"roles" => $data['roles'][$i] ?? [],
|
||
|
||
"reporting_roles" => $data['reporting_roles'][$i] ?? [],
|
||
|
||
"actions" => [
|
||
|
||
"user_notification" => isset($data['user_notification'][$i]),
|
||
|
||
"user_email" => isset($data['user_email'][$i]),
|
||
|
||
"manager_notification" => $data['manager_notification'][$i] ?? [],
|
||
|
||
"manager_email" => $data['manager_email'][$i] ?? [],
|
||
|
||
"send_badge" => array_values(array_filter((array)($data['send_badge'][$i] ?? []))),
|
||
|
||
"send_appreciation" => [
|
||
[
|
||
"category_id" => $data['appreciation_category'][$i] ?? null,
|
||
"image_id" => $data['appreciation_image'][$i] ?? null,
|
||
"message" => $data['appreciation_message'][$i] ?? null
|
||
]
|
||
]
|
||
|
||
]
|
||
|
||
];
|
||
}
|
||
|
||
$data['milestones'] = $milestones;
|
||
unset($data['milestone_id'], $data['target'], $data['target_title'], $data['repeat_target'], $data['roles'], $data['reporting_roles'], $data['user_notification'], $data['user_email'], $data['manager_notification'], $data['manager_email'], $data['send_badge'], $data['appreciation_category'], $data['appreciation_image'], $data['appreciation_message']);
|
||
|
||
// echo "<pre>";
|
||
// print_r($data);
|
||
// exit;
|
||
|
||
|
||
|
||
$data['metrics'] = [];
|
||
|
||
foreach ($data['metric'] ?? [] as $k => $metric) {
|
||
$target = $data['metric_target'][$k] ?? '';
|
||
$data['metrics'][] = [
|
||
'metric' => $metric,
|
||
'display' => $data['display_unit'][$k] ?? '',
|
||
'weight' => isset($data['weight'][$k]) && $data['weight'][$k] !== '' ? (string) $data['weight'][$k] : '1',
|
||
'target' => $target !== '' && $target !== null ? (string) $target : '',
|
||
];
|
||
}
|
||
|
||
$daysOrDates = $data['days_or_dates'] ?? 'days';
|
||
$data['send_report'] = $this->toBool($data['send_report'] ?? 'no');
|
||
$data['report_config'] = [
|
||
'days_or_dates' => $daysOrDates,
|
||
'days' => $daysOrDates === 'days' ? $this->toStringList($data['days'] ?? []) : [],
|
||
'dates' => $daysOrDates === 'dates' ? $this->toStringList($data['dates'] ?? []) : [],
|
||
'send_time' => $data['send_time'] ?? '13:00',
|
||
'performance_period' => $data['performance_period'] ?? 'monthly',
|
||
'send_report_roles' => $this->toStringList($data['send_report_roles'] ?? []),
|
||
'report_roles' => $this->toStringList($data['report_roles'] ?? []),
|
||
'send_user_performance' => $this->toBool($data['send_user_performance'] ?? 'no'),
|
||
'ai_action_items' => $this->toBool($data['ai_action_items'] ?? 'no'),
|
||
'send_pending_users' => $this->toBool($data['send_pending_users'] ?? 'no'),
|
||
'show_target_average' => $this->toBool($data['show_target_average'] ?? 'no'),
|
||
'show_overall_average' => $this->toBool($data['show_overall_average'] ?? 'no'),
|
||
'subject' => trim((string) ($data['subject'] ?? '')),
|
||
'body' => trim((string) ($data['body'] ?? '')),
|
||
];
|
||
|
||
// $data['is_recurring'] = isset($data['is_recurring']) ? (strtolower($data['is_recurring']) === 'yes') : false;
|
||
|
||
if (!isset($data['manager_details']) || !is_array($data['manager_details']) || $data['manager_details'] === []) {
|
||
$data['manager_details'] = ['l1manager'];
|
||
}
|
||
foreach (['show_units_export', 'show_normal_metrics', 'show_combined_metrics'] as $flag) {
|
||
if (!isset($data[$flag]) || $data[$flag] === '') {
|
||
$data[$flag] = 'yes';
|
||
}
|
||
}
|
||
$data['show_user_attributes'] = ($data['show_user_attributes'] ?? 'no') === 'yes' ? 'yes' : 'no';
|
||
$data['user_attributes'] = $this->normalizeAttributeCodes($data['user_attributes'] ?? []);
|
||
$data['total_units_name'] = trim((string) ($data['total_units_name'] ?? ''));
|
||
|
||
unset($data['interval_start_date']);
|
||
unset($data['interval_end_date']);
|
||
unset($data['metric']);
|
||
unset($data['display_unit']);
|
||
unset($data['weight']);
|
||
unset($data['metric_target']);
|
||
unset(
|
||
$data['days_or_dates'],
|
||
$data['days'],
|
||
$data['dates'],
|
||
$data['send_time'],
|
||
$data['performance_period'],
|
||
$data['send_report_roles'],
|
||
$data['report_roles'],
|
||
$data['send_user_performance'],
|
||
$data['ai_action_items'],
|
||
$data['send_pending_users'],
|
||
$data['show_target_average'],
|
||
$data['show_overall_average'],
|
||
$data['subject'],
|
||
$data['body']
|
||
);
|
||
// echo '<pre>';
|
||
// print_r($data);
|
||
// exit;
|
||
return $data;
|
||
}
|
||
|
||
protected function _beforeform($type, $values)
|
||
{
|
||
if ($type == 'edit') {
|
||
if (!isset($values->manager_details) || !is_array($values->manager_details) || $values->manager_details === []) {
|
||
$values->manager_details = ['l1manager'];
|
||
}
|
||
foreach (['show_units_export', 'show_normal_metrics', 'show_combined_metrics'] as $flag) {
|
||
if (!isset($values->{$flag}) || $values->{$flag} === '') {
|
||
$values->{$flag} = 'yes';
|
||
}
|
||
}
|
||
$values->show_user_attributes = (($values->show_user_attributes ?? 'no') === 'yes') ? 'yes' : 'no';
|
||
$values->user_attributes = $this->normalizeAttributeCodes($values->user_attributes ?? []);
|
||
if (!isset($values->total_units_name) || $values->total_units_name === null) {
|
||
$values->total_units_name = '';
|
||
}
|
||
|
||
// split interval
|
||
if (!empty($values->interval)) {
|
||
[$start, $end] = explode('_', $values->interval);
|
||
|
||
$values->interval_start_date = date('m/d/Y', strtotime($start));
|
||
$values->interval_end_date = date('m/d/Y', strtotime($end));
|
||
}
|
||
|
||
// boolean
|
||
// $values->is_recurring = isset($values->is_recurring) ? (strtolower($values->is_recurring) === 'yes') : false;
|
||
|
||
|
||
// $values->is_recurring = isset($values->is_recurring)
|
||
// ? (
|
||
// (is_string($values->is_recurring) && strtolower($values->is_recurring) === 'yes')
|
||
// || $values->is_recurring === 1
|
||
// || $values->is_recurring === true
|
||
// )
|
||
// ? 'Yes'
|
||
// : 'No'
|
||
// : '';
|
||
|
||
// prepare milestone arrays for blade
|
||
$values->target = [];
|
||
$values->target_title = [];
|
||
$values->repeat_target = [];
|
||
$values->roles = [];
|
||
$values->reporting_roles = [];
|
||
$values->user_notification = [];
|
||
$values->user_email = [];
|
||
$values->manager_notification = [];
|
||
$values->manager_email = [];
|
||
$values->send_badge = [];
|
||
$values->appreciation_category = [];
|
||
$values->appreciation_image = [];
|
||
$values->appreciation_message = [];
|
||
$values->milestone_id = [];
|
||
|
||
if (!empty($values->milestones)) {
|
||
|
||
foreach ($values->milestones as $i => $milestone) {
|
||
|
||
$values->milestone_id[$i] = $milestone->milestone_id ?? null;
|
||
|
||
if (is_object($milestone->target ?? null)) {
|
||
$values->target[$i] = $milestone->target->value ?? 0;
|
||
$values->target_title[$i] = $milestone->target->target_title ?? '';
|
||
} else {
|
||
$values->target[$i] = $milestone->target ?? 0;
|
||
$values->target_title[$i] = '';
|
||
}
|
||
|
||
$values->repeat_target[$i] = !empty($milestone->repeat_target) ? '1' : '0';
|
||
|
||
$values->roles[$i] = $milestone->roles ?? [];
|
||
|
||
$values->reporting_roles[$i] = $milestone->reporting_roles ?? [];
|
||
|
||
$values->user_notification[$i] =
|
||
!empty($milestone->actions->user_notification)
|
||
? $milestone->actions->user_notification
|
||
: [];
|
||
|
||
$values->user_email[$i] =
|
||
!empty($milestone->actions->user_email)
|
||
? $milestone->actions->user_email
|
||
: [];
|
||
|
||
$values->manager_notification[$i] =
|
||
$milestone->actions->manager_notification ?? [];
|
||
|
||
$values->manager_email[$i] =
|
||
$milestone->actions->manager_email ?? [];
|
||
|
||
$values->send_badge[$i] =
|
||
$milestone->actions->send_badge ?? [];
|
||
|
||
$app = $milestone->actions->send_appreciation[0] ?? null;
|
||
|
||
$values->appreciation_category[$i] = $app->category_id ?? null;
|
||
$values->appreciation_image[$i] = $app->image_id ?? null;
|
||
$values->appreciation_message[$i] = $app->message ?? null;
|
||
}
|
||
}
|
||
|
||
// metrics conversion
|
||
if (!empty($values->metrics)) {
|
||
|
||
$values->metric = [];
|
||
$values->display_unit = [];
|
||
$values->sort_order = [];
|
||
$values->list_view = [];
|
||
$values->weight = [];
|
||
$values->metric_target = [];
|
||
|
||
foreach ($values->metrics as $k => $metric) {
|
||
$metric = is_object($metric) ? $metric : (object) $metric;
|
||
$values->metric[$k] = $metric->metric ?? '';
|
||
$values->display_unit[$k] = $metric->display ?? '';
|
||
$values->weight[$k] = $metric->weight ?? 1;
|
||
$values->metric_target[$k] = $metric->target ?? '';
|
||
}
|
||
}
|
||
|
||
$reportConfig = $values->report_config ?? null;
|
||
if (is_array($reportConfig)) {
|
||
$reportConfig = (object) $reportConfig;
|
||
}
|
||
if (is_object($reportConfig)) {
|
||
$values->days_or_dates = $reportConfig->days_or_dates ?? 'days';
|
||
$values->days = $this->toStringList($reportConfig->days ?? []);
|
||
$values->dates = $this->toStringList($reportConfig->dates ?? []);
|
||
$values->send_time = $reportConfig->send_time ?? '';
|
||
$values->performance_period = $reportConfig->performance_period ?? '';
|
||
$values->send_report_roles = $this->toStringList($reportConfig->send_report_roles ?? []);
|
||
$values->report_roles = $this->toStringList($reportConfig->report_roles ?? []);
|
||
$values->send_user_performance = $this->fromBool($reportConfig->send_user_performance ?? false);
|
||
$values->ai_action_items = $this->fromBool($reportConfig->ai_action_items ?? false);
|
||
$values->send_pending_users = $this->fromBool($reportConfig->send_pending_users ?? false);
|
||
$values->show_target_average = $this->fromBool($reportConfig->show_target_average ?? false);
|
||
$values->show_overall_average = $this->fromBool($reportConfig->show_overall_average ?? false);
|
||
$values->subject = $reportConfig->subject ?? '';
|
||
$values->body = $reportConfig->body ?? '';
|
||
} else {
|
||
$values->days_or_dates = 'days';
|
||
$values->days = [];
|
||
$values->dates = [];
|
||
$values->send_time = '';
|
||
$values->performance_period = '';
|
||
$values->send_report_roles = [];
|
||
$values->report_roles = [];
|
||
$values->send_user_performance = 'no';
|
||
$values->ai_action_items = 'no';
|
||
$values->send_pending_users = 'no';
|
||
$values->show_target_average = 'no';
|
||
$values->show_overall_average = 'no';
|
||
$values->subject = '';
|
||
$values->body = '';
|
||
}
|
||
$values->send_report = $this->fromBool($values->send_report ?? false);
|
||
}
|
||
// echo '<pre>';
|
||
// print_r($values);
|
||
// exit;
|
||
return $values;
|
||
}
|
||
|
||
public function form($type, $id = null)
|
||
{
|
||
$values = [];
|
||
if ($type == 'edit') {
|
||
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module . '/get_one', ['id' => $id]);
|
||
$values = $response->data->item;
|
||
// echo '<pre>'; print_r($values); exit;
|
||
}
|
||
//echo '<pre>'; print_r($values); exit;
|
||
$data = $this->_beforeform($type, $values);
|
||
$fields = $this->get_form_fields($type, $values);
|
||
// echo '<pre>'; print_r($data); exit;
|
||
return $this->view('form')->with([
|
||
'title' => $this->_title,
|
||
'mode' => $type,
|
||
'fields' => $fields,
|
||
'base_url' => $this->_base_url,
|
||
'item' => $data,
|
||
]);
|
||
}
|
||
|
||
public function dashboardLeaderboards(Request $request)
|
||
{
|
||
$response = LambdaRequest::signedrequest('get', 'reports', 'roles_leaderboard/list', [], 120);
|
||
if (!is_object($response) || ($response->statusCode ?? 0) != 200) {
|
||
return view('widgets.milestone_team_leaderboard_cards', ['items' => []]);
|
||
}
|
||
|
||
$leaderboards = $response->data ?? [];
|
||
if (is_object($leaderboards) && isset($leaderboards->items)) {
|
||
$leaderboards = $leaderboards->items;
|
||
}
|
||
if (is_object($leaderboards)) {
|
||
$leaderboards = array_values((array) $leaderboards);
|
||
}
|
||
if (!is_array($leaderboards)) {
|
||
$leaderboards = [];
|
||
}
|
||
|
||
$items = [];
|
||
foreach ($leaderboards as $board) {
|
||
$board = $this->asObject($board);
|
||
$rankRoles = $board->rank_roles ?? [];
|
||
if (is_object($rankRoles)) {
|
||
$rankRoles = array_values((array) $rankRoles);
|
||
}
|
||
if (!is_array($rankRoles)) {
|
||
$rankRoles = [];
|
||
}
|
||
|
||
$start = $board->validity_start ?? '';
|
||
$end = $board->validity_end ?? '';
|
||
$detailsUrl = url('/leaderboard/dashboard') . '?' . http_build_query([
|
||
'id' => $board->id ?? '',
|
||
]);
|
||
|
||
if ($this->isTeamLeaderboardView($board->team_view ?? false)) {
|
||
$items[] = [
|
||
'id' => $board->id ?? '',
|
||
'title' => $board->name ?? 'Leaderboard',
|
||
'interval_label' => trim($start . ($start && $end ? ' – ' : '') . $end),
|
||
'details_url' => $detailsUrl,
|
||
'podium_users' => $this->podiumUsersFromTeams($rankRoles),
|
||
'pills' => [],
|
||
];
|
||
continue;
|
||
}
|
||
|
||
$pills = [];
|
||
foreach ($rankRoles as $index => $rankRole) {
|
||
$rankRole = $this->asObject($rankRole);
|
||
$role = $this->asObject($rankRole->role ?? null);
|
||
$roleId = $role->id ?? '';
|
||
$limit = (int) ($rankRole->limit ?? 10);
|
||
if ($limit < 1) {
|
||
$limit = 10;
|
||
}
|
||
$pills[] = [
|
||
'label' => $this->rankRolePillLabel($rankRole->title ?? '', $limit, $role->name ?? ''),
|
||
'url' => $detailsUrl . '&' . http_build_query([
|
||
'role' => $roleId,
|
||
]),
|
||
'podium_users' => $this->podiumUsersFromRankRole($rankRole),
|
||
'active' => $index === 0,
|
||
];
|
||
}
|
||
|
||
$items[] = [
|
||
'id' => $board->id ?? '',
|
||
'title' => $board->name ?? 'Leaderboard',
|
||
'interval_label' => trim($start . ($start && $end ? ' – ' : '') . $end),
|
||
'details_url' => $pills[0]['url'] ?? $detailsUrl,
|
||
'podium_users' => $pills[0]['podium_users'] ?? [],
|
||
'pills' => $pills,
|
||
];
|
||
}
|
||
|
||
return view('widgets.milestone_team_leaderboard_cards', [
|
||
'items' => $items,
|
||
]);
|
||
}
|
||
|
||
protected function rankRolePillLabel($title, $limit, $roleName): string
|
||
{
|
||
$title = trim((string) $title);
|
||
if ($title === '') {
|
||
$title = trim((string) $roleName) !== '' ? $roleName : 'Role';
|
||
}
|
||
if (preg_match('/^\s*top\s+\d+/i', $title)) {
|
||
return $title;
|
||
}
|
||
return 'Top ' . (int) $limit . ' ' . $title;
|
||
}
|
||
|
||
protected function podiumUsersFromRankRole($rankRole): array
|
||
{
|
||
$rows = $rankRole->users ?? [];
|
||
if (is_object($rows)) {
|
||
$rows = array_values((array) $rows);
|
||
}
|
||
if (!is_array($rows)) {
|
||
return [];
|
||
}
|
||
|
||
$users = [];
|
||
foreach (array_slice($rows, 0, 3) as $row) {
|
||
$row = $this->asObject($row);
|
||
$info = $this->asObject($row->user ?? null);
|
||
$name = trim((string) ($info->name ?? ''));
|
||
$users[] = [
|
||
'name' => $name,
|
||
'initials' => $this->userInitials($name),
|
||
'avatar' => $info->avatar ?? '',
|
||
'score' => is_numeric($row->score ?? null) ? (0 + $row->score) : 0,
|
||
];
|
||
}
|
||
return $users;
|
||
}
|
||
|
||
protected function isTeamLeaderboardView($flag): bool
|
||
{
|
||
return $flag === true || $flag === 1 || $flag === '1' || $flag === 'true' || $flag === 'yes';
|
||
}
|
||
|
||
protected function podiumUsersFromTeams(array $rows): array
|
||
{
|
||
usort($rows, function ($a, $b) {
|
||
$a = $this->asObject($a);
|
||
$b = $this->asObject($b);
|
||
return ((int) ($a->rank ?? 0)) <=> ((int) ($b->rank ?? 0));
|
||
});
|
||
|
||
$users = [];
|
||
foreach (array_slice($rows, 0, 3) as $row) {
|
||
$row = $this->asObject($row);
|
||
$team = $this->asObject($row->team ?? null);
|
||
$name = trim((string) ($team->name ?? ''));
|
||
if ($name === '') {
|
||
$name = 'Team';
|
||
}
|
||
$users[] = [
|
||
'name' => $name,
|
||
'initials' => $this->userInitials($name),
|
||
'avatar' => $team->icon ?? '',
|
||
'score' => is_numeric($row->score ?? null) ? (0 + $row->score) : 0,
|
||
];
|
||
}
|
||
|
||
return $users;
|
||
}
|
||
|
||
public function dashboardLeaderboardPodium(Request $request)
|
||
{
|
||
$data = $this->teamReportViewData($request);
|
||
if (!empty($data['error'])) {
|
||
return response()->json([
|
||
'status' => 'error',
|
||
'message' => $data['error'],
|
||
'users' => [],
|
||
'groups' => [],
|
||
]);
|
||
}
|
||
|
||
$users = $this->rankedReportUsers($data['report'] ?? null);
|
||
$groups = $this->leaderboardGroups($users);
|
||
|
||
$startLabel = $this->formatReportDate($data['start_date'] ?? '');
|
||
$endLabel = $this->formatReportDate($data['end_date'] ?? '');
|
||
$intervalLabel = trim($startLabel . ($startLabel && $endLabel ? ' – ' : '') . $endLabel);
|
||
|
||
return response()->json([
|
||
'status' => 'success',
|
||
'title' => $data['title'] ?? 'Leaderboard',
|
||
'interval_label' => $intervalLabel,
|
||
'users' => $users,
|
||
'groups' => $groups,
|
||
]);
|
||
}
|
||
|
||
public function publicTeamReport(Request $request)
|
||
{
|
||
$data = $this->teamReportViewData($request);
|
||
$data['exportUrl'] = url('/milestones/exportTeamReport') . '?' . http_build_query($request->query());
|
||
return view('milestone.public_team_report', $data);
|
||
}
|
||
|
||
public function viewTeamReport(Request $request)
|
||
{
|
||
$data = $this->teamReportViewData($request);
|
||
$data['exportUrl'] = url('/milestones/exportTeamReport') . '?' . http_build_query($request->query());
|
||
return view('milestone.team_report', $data);
|
||
}
|
||
|
||
public function exportTeamReport(Request $request)
|
||
{
|
||
$data = $this->teamReportViewData($request);
|
||
if (!empty($data['error'])) {
|
||
$fallback = Auth::check() ? '/milestones/viewTeamReport' : '/milestones/publicTeamReport';
|
||
return redirect($fallback . '?' . http_build_query($request->query()))
|
||
->withError($data['error']);
|
||
}
|
||
|
||
$report = $data['report'];
|
||
$users = [];
|
||
if ($report && isset($report->users)) {
|
||
$users = is_array($report->users) ? $report->users : array_values((array) $report->users);
|
||
}
|
||
|
||
$metrics = [];
|
||
if (count($users)) {
|
||
$first = is_object($users[0]) ? $users[0] : (object) $users[0];
|
||
if (isset($first->metrics)) {
|
||
$metrics = is_array($first->metrics) ? $first->metrics : array_values((array) $first->metrics);
|
||
}
|
||
}
|
||
|
||
$metricHasTarget = [];
|
||
foreach ($metrics as $index => $metricCol) {
|
||
$metricCol = is_object($metricCol) ? $metricCol : (object) $metricCol;
|
||
$hasTarget = false;
|
||
foreach ($users as $user) {
|
||
$cell = $this->teamReportMetricCell($user, $metricCol);
|
||
if ($this->teamReportMetricHasTarget($cell)) {
|
||
$hasTarget = true;
|
||
break;
|
||
}
|
||
}
|
||
$metricHasTarget[$index] = $hasTarget;
|
||
}
|
||
|
||
$headers = ['Rank', 'User', 'Employee Code', 'Designation', 'Position', 'Score'];
|
||
foreach ($metrics as $index => $metric) {
|
||
$metric = is_object($metric) ? $metric : (object) $metric;
|
||
$name = $metric->name ?? ($metric->metric ?? 'Metric');
|
||
$headers[] = $name . ' Value';
|
||
$headers[] = $name . ' Average';
|
||
if (!empty($metricHasTarget[$index])) {
|
||
$headers[] = $name . ' Target';
|
||
$headers[] = $name . ' Status';
|
||
}
|
||
}
|
||
|
||
$exceldata = [$headers];
|
||
foreach ($users as $index => $user) {
|
||
$user = is_object($user) ? $user : (object) $user;
|
||
$row = [
|
||
$index + 1,
|
||
$user->name ?? '',
|
||
$user->employee_code ?? '',
|
||
$user->designation ?? '',
|
||
$user->position_name ?? '',
|
||
$user->score ?? 0,
|
||
];
|
||
|
||
foreach ($metrics as $metricIndex => $metricCol) {
|
||
$metricCol = is_object($metricCol) ? $metricCol : (object) $metricCol;
|
||
$cell = $this->teamReportMetricCell($user, $metricCol);
|
||
if ($cell) {
|
||
$row[] = $cell->value ?? 0;
|
||
$row[] = $cell->previous_average ?? 0;
|
||
if (!empty($metricHasTarget[$metricIndex])) {
|
||
$row[] = $this->teamReportMetricHasTarget($cell) ? ($cell->target ?? '') : '';
|
||
$row[] = $this->teamReportMetricHasTarget($cell)
|
||
? (!empty($cell->completed) ? 'Completed' : 'Not completed')
|
||
: '';
|
||
}
|
||
} else {
|
||
$row[] = '';
|
||
$row[] = '';
|
||
if (!empty($metricHasTarget[$metricIndex])) {
|
||
$row[] = '';
|
||
$row[] = '';
|
||
}
|
||
}
|
||
}
|
||
|
||
$exceldata[] = $row;
|
||
}
|
||
|
||
$filename = preg_replace('/[^A-Za-z0-9_\-]+/', '-', (string) ($data['title'] ?: 'leaderboard'));
|
||
$filename = trim($filename, '-') ?: 'leaderboard';
|
||
|
||
return Excel::download(new InvoicesExport($exceldata), $filename . '-team-report.xlsx');
|
||
}
|
||
|
||
public function getTeamReport(Request $request)
|
||
{
|
||
$data = $this->teamReportViewData($request);
|
||
if (!empty($data['error'])) {
|
||
return response('<p class="text-danger mb-0">' . e($data['error']) . '</p>');
|
||
}
|
||
|
||
return view('widgets.milestone_team_leaderboard_table', $data);
|
||
}
|
||
|
||
protected function teamReportViewData(Request $request): array
|
||
{
|
||
$user = Auth::user();
|
||
$params = [
|
||
'report_id' => trim((string) $request->query('report_id', '')),
|
||
'partition' => trim((string) $request->query('partition', '')),
|
||
];
|
||
|
||
if ($params['partition'] === '' && $user) {
|
||
$params['partition'] = $this->currentPartition();
|
||
}
|
||
|
||
if ($params['report_id'] === '' || $params['partition'] === '') {
|
||
return [
|
||
'error' => 'Unable to load this leaderboard for the current user.',
|
||
'title' => 'Leaderboard',
|
||
'performance_period' => '',
|
||
'start_date' => '',
|
||
'end_date' => '',
|
||
'report' => null,
|
||
];
|
||
}
|
||
|
||
$response = Auth::check()
|
||
? LambdaRequest::signedrequest('get', $this->_service, $this->_module . '/getTeamReport', $params, 120)
|
||
: LambdaRequest::getrequest($this->_service, $this->_module . '/getTeamReport', $params, '', 120);
|
||
|
||
if (!is_object($response) || ($response->statusCode ?? 0) != 200 || !isset($response->data)) {
|
||
$message = is_object($response) ? ($response->message ?? 'Unable to load leaderboard.') : 'Unable to load leaderboard.';
|
||
return [
|
||
'error' => $message,
|
||
'title' => 'Leaderboard',
|
||
'performance_period' => '',
|
||
'start_date' => '',
|
||
'end_date' => '',
|
||
'report' => null,
|
||
];
|
||
}
|
||
|
||
$data = $response->data;
|
||
$reports = $data->reports ?? [];
|
||
if (is_object($reports)) {
|
||
$reports = (array) $reports;
|
||
}
|
||
|
||
return [
|
||
'error' => '',
|
||
'title' => $data->title ?? 'Leaderboard',
|
||
'performance_period' => $data->performance_period ?? '',
|
||
'start_date' => $data->start_date ?? '',
|
||
'end_date' => $data->end_date ?? '',
|
||
'report' => (is_array($reports) && count($reports)) ? $reports[0] : null,
|
||
];
|
||
}
|
||
|
||
protected function teamReportMetricCell($user, $metricCol)
|
||
{
|
||
$user = is_object($user) ? $user : (object) $user;
|
||
$metricCol = is_object($metricCol) ? $metricCol : (object) $metricCol;
|
||
$userMetrics = [];
|
||
if (isset($user->metrics)) {
|
||
$userMetrics = is_array($user->metrics) ? $user->metrics : array_values((array) $user->metrics);
|
||
}
|
||
foreach ($userMetrics as $userMetric) {
|
||
$userMetric = is_object($userMetric) ? $userMetric : (object) $userMetric;
|
||
if (($userMetric->metric ?? '') === ($metricCol->metric ?? '')) {
|
||
return $userMetric;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
protected function teamReportMetricHasTarget($cell): bool
|
||
{
|
||
if (!$cell) {
|
||
return false;
|
||
}
|
||
$target = $cell->target ?? null;
|
||
return $target !== null && $target !== '' && (float) $target > 0;
|
||
}
|
||
|
||
protected function toBool($value): bool
|
||
{
|
||
if (is_bool($value)) {
|
||
return $value;
|
||
}
|
||
return in_array(strtolower((string) $value), ['yes', '1', 'true'], true);
|
||
}
|
||
|
||
protected function fromBool($value): string
|
||
{
|
||
return $this->toBool($value) ? 'yes' : 'no';
|
||
}
|
||
|
||
protected function toStringList($value): array
|
||
{
|
||
if ($value === null || $value === '') {
|
||
return [];
|
||
}
|
||
if (is_object($value)) {
|
||
$value = (array) $value;
|
||
}
|
||
if (!is_array($value)) {
|
||
return [(string) $value];
|
||
}
|
||
return array_values(array_map('strval', $value));
|
||
}
|
||
|
||
protected function currentPartition(): string
|
||
{
|
||
$domain = Auth::user()->domain ?? '';
|
||
return $domain !== '' ? 'rapport-' . $domain : '';
|
||
}
|
||
|
||
protected function currentUserRoleIds(): array
|
||
{
|
||
$roles = Auth::user()->roles ?? [];
|
||
$decoded = is_string($roles) ? json_decode($roles, true) : $roles;
|
||
if (is_array($decoded)) {
|
||
return array_values(array_filter(array_map('strval', $decoded)));
|
||
}
|
||
return $this->toStringList(is_string($roles) ? explode('#', $roles) : $roles);
|
||
}
|
||
|
||
protected function asObject($value): object
|
||
{
|
||
if (is_object($value)) {
|
||
return $value;
|
||
}
|
||
if (is_array($value)) {
|
||
return (object) $value;
|
||
}
|
||
return (object) [];
|
||
}
|
||
|
||
protected function intervalLabel($interval): string
|
||
{
|
||
if (!is_string($interval) || $interval === '') {
|
||
return '';
|
||
}
|
||
$parts = explode('_', $interval);
|
||
if (count($parts) !== 2) {
|
||
return $interval;
|
||
}
|
||
$start = strtotime($parts[0]);
|
||
$end = strtotime($parts[1]);
|
||
return ($start ? date('d M Y', $start) : $parts[0]) . ' – ' . ($end ? date('d M Y', $end) : $parts[1]);
|
||
}
|
||
|
||
protected function milestoneVisibleOnDashboard($milestone, array $userRoles): bool
|
||
{
|
||
$status = strtolower((string) ($milestone->status ?? ''));
|
||
if (!empty($milestone->is_deleted) || in_array($status, ['deleted', 'disabled'], true)) {
|
||
return false;
|
||
}
|
||
if ($status !== '' && $status !== 'active') {
|
||
return false;
|
||
}
|
||
|
||
$reportConfig = $this->asObject($milestone->report_config ?? null);
|
||
$sendRoles = $this->toStringList($reportConfig->send_report_roles ?? []);
|
||
$reportRoles = $this->toStringList($reportConfig->report_roles ?? []);
|
||
$hasReport = $this->toBool($milestone->send_report ?? false) || count($reportRoles) > 0;
|
||
if (!$hasReport) {
|
||
return false;
|
||
}
|
||
|
||
$allowed = array_merge($sendRoles, $reportRoles);
|
||
$rows = $milestone->milestones ?? [];
|
||
if (is_object($rows)) {
|
||
$rows = (array) $rows;
|
||
}
|
||
foreach ($rows as $row) {
|
||
$row = $this->asObject($row);
|
||
$allowed = array_merge(
|
||
$allowed,
|
||
$this->toStringList($row->roles ?? []),
|
||
$this->toStringList($row->reporting_roles ?? [])
|
||
);
|
||
}
|
||
$allowed = array_values(array_unique(array_filter($allowed)));
|
||
if (!count($allowed)) {
|
||
return true;
|
||
}
|
||
|
||
return count(array_intersect($userRoles, $allowed)) > 0;
|
||
}
|
||
|
||
protected function rankedReportUsers($report): array
|
||
{
|
||
$users = [];
|
||
if ($report && isset($report->users)) {
|
||
$users = is_array($report->users) ? $report->users : array_values((array) $report->users);
|
||
}
|
||
|
||
$ranked = [];
|
||
foreach ($users as $index => $user) {
|
||
$user = is_object($user) ? $user : (object) $user;
|
||
$name = trim((string) ($user->name ?? ''));
|
||
$ranked[] = [
|
||
'rank' => $index + 1,
|
||
'name' => $name,
|
||
'initials' => $this->userInitials($name),
|
||
'avatar' => $user->avatar ?? '',
|
||
'score' => is_numeric($user->score ?? null) ? (0 + $user->score) : 0,
|
||
'designation' => trim((string) ($user->designation ?? '')),
|
||
'position_name' => trim((string) ($user->position_name ?? '')),
|
||
];
|
||
}
|
||
|
||
return $ranked;
|
||
}
|
||
|
||
protected function leaderboardGroups(array $users): array
|
||
{
|
||
$grouped = [];
|
||
foreach ($users as $user) {
|
||
$key = $user['designation'] !== '' ? $user['designation'] : 'Team';
|
||
if (!isset($grouped[$key])) {
|
||
$grouped[$key] = 0;
|
||
}
|
||
$grouped[$key]++;
|
||
}
|
||
|
||
$groups = [];
|
||
foreach ($grouped as $key => $count) {
|
||
$labelName = $key;
|
||
if ($count !== 1 && !preg_match('/s$/i', $labelName)) {
|
||
$labelName .= 's';
|
||
}
|
||
$groups[] = [
|
||
'key' => $key,
|
||
'label' => 'Top ' . $count . ' ' . $labelName,
|
||
'count' => $count,
|
||
];
|
||
}
|
||
|
||
usort($groups, function ($a, $b) {
|
||
return $b['count'] <=> $a['count'];
|
||
});
|
||
|
||
return $groups;
|
||
}
|
||
|
||
protected function userInitials($name): string
|
||
{
|
||
$parts = preg_split('/\s+/', trim((string) $name));
|
||
$parts = array_values(array_filter($parts));
|
||
if (!count($parts)) {
|
||
return '?';
|
||
}
|
||
if (count($parts) === 1) {
|
||
return strtoupper(substr($parts[0], 0, 2));
|
||
}
|
||
return strtoupper(substr($parts[0], 0, 1) . substr($parts[count($parts) - 1], 0, 1));
|
||
}
|
||
|
||
protected function formatReportDate($value): string
|
||
{
|
||
if ($value === null || $value === '') {
|
||
return '';
|
||
}
|
||
if (is_numeric($value)) {
|
||
return date('d M Y', (int) $value);
|
||
}
|
||
$ts = strtotime((string) $value);
|
||
return $ts ? date('d M Y', $ts) : '';
|
||
}
|
||
|
||
protected function getUserAttributeOptions(): array
|
||
{
|
||
$options = [];
|
||
$userAttrs = $this->getlambda('get', 'ums', 'userattribute', 'get_all');
|
||
foreach (($userAttrs->data->items ?? []) as $attr) {
|
||
if (($attr->status ?? 'active') === 'disabled') {
|
||
continue;
|
||
}
|
||
$code = $attr->code ?? '';
|
||
if ($code === '') {
|
||
continue;
|
||
}
|
||
$options[$code] = $attr->attribute ?? $code;
|
||
}
|
||
|
||
return $options;
|
||
}
|
||
|
||
protected function normalizeAttributeCodes($raw): array
|
||
{
|
||
if (is_string($raw) && $raw !== '') {
|
||
$decoded = json_decode($raw, true);
|
||
$raw = is_array($decoded) ? $decoded : explode('#', $raw);
|
||
}
|
||
if (!is_array($raw)) {
|
||
return [];
|
||
}
|
||
|
||
$codes = [];
|
||
foreach ($raw as $code) {
|
||
if (is_object($code)) {
|
||
$code = $code->code ?? '';
|
||
}
|
||
$code = trim((string) $code);
|
||
if ($code !== '') {
|
||
$codes[] = $code;
|
||
}
|
||
}
|
||
|
||
return array_values(array_unique($codes));
|
||
}
|
||
}
|