/**
* @description :
* @author : srinu
* @group :
* @last modified on : 09-22-2022
* @last modified by : srinu
**/
public without sharing class PPL_FullCalCtlrClone {
@AuraEnabled(cacheable=true)
public static List<Map<String, String>> getContactList(Integer monthDays,
Integer selectedMonth, Integer selectedYear) {
//List of Employees from TRH
Set<User> contactSet = new Set<User>();
//New Date to search Projects
Date filterDate = Date.newInstance(selectedYear,selectedMonth,1);
Date startDate = filterDate.toStartOfMonth();
Date lastDate = startDate.addMonths(1).addDays(-1);
//List of Projects that Employees are in, on that month and year
Map<Id, Set<PPL_Project__c>> projectsPerContact = new Map<Id,
Set<PPL_Project__c>>();
//List that will save project/absence/vacation days by row
List<Map<String, String>> rowData = new List<Map<String, String>>();
//Check for data
//List of Different Type of Days
Map<String, PPL_Day__c> absenceDays = new Map<String, PPL_Day__c>();
Map<String, PPL_Day__c> vacationDays = new Map<String, PPL_Day__c>();
Map<String, PPL_Day__c> projectDays = new Map<String, PPL_Day__c>();
//Email and profile of current user to check his permissions (up to debate)
List<Profile> profile = [SELECT Id, Name FROM Profile WHERE
Id=:userinfo.getProfileId() LIMIT 1];
String thisProfile = profile[0].Name;
String currentUserEmail = UserInfo.getUserEmail();
String userId = UserInfo.getUserId();
List<PPL_Day__c> queryDay = new List<PPL_Day__c>();
if(thisProfile == 'System Administrator' || thisProfile == 'TRH Manager'){
queryDay = [SELECT Id,PPL_Hour__c, PPL_Type__c, PPL_ThisDay__c,
PPL_Date__c,
PPL_Employee__c, PPL_Employee__r.Id,
PPL_Employee__r.Email, PPL_Employee__r.Name,
PPL_Project_Days_Aggregated__r.PPL_Allocation__c,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.PPL_Motive__c,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.Id,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.PPL_Description__c,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.RecordType.Name
FROM PPL_Day__c
WHERE PPL_Employee__r.IsActive = true
//AND
(((PPL_Project__r.PPL_StartDate_Contract__c >= :startDate AND
PPL_Project__r.PPL_StartDate_Contract__c <= :lastDate) OR
(PPL_Project__r.PPL_EndDate__c >= :startDate AND PPL_Project__r.PPL_EndDate__c
<= :lastDate))
//OR (PPL_Project__r.PPL_StartDate_Contract__c
<= :startDate AND PPL_Project__r.PPL_EndDate__c >= :lastDate))
AND PPL_Date__c >= :startDate AND PPL_Date__c
<= :lastDate
ORDER BY PPL_ThisDay__c];
} else{
queryDay = [SELECT Id,PPL_Hour__c, PPL_Type__c, PPL_ThisDay__c,
PPL_Date__c,
PPL_Employee__c, PPL_Employee__r.Id,
PPL_Employee__r.Email, PPL_Employee__r.Name,
PPL_Project_Days_Aggregated__r.PPL_Allocation__c,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.PPL_Motive__c,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.Id,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.PPL_Description__c,
PPL_Project_Days_Aggregated__r.PPL_Allocation__r.RecordType.Name
FROM PPL_Day__c
WHERE PPL_Employee__r.IsActive = true AND
PPL_Employee__r.Id = :userId
//AND
(((PPL_Project__r.PPL_StartDate_Contract__c >= :startDate AND
PPL_Project__r.PPL_StartDate_Contract__c <= :lastDate) OR
(PPL_Project__r.PPL_EndDate__c >= :startDate AND PPL_Project__r.PPL_EndDate__c
<= :lastDate))
//OR (PPL_Project__r.PPL_StartDate_Contract__c
<= :startDate AND PPL_Project__r.PPL_EndDate__c >= :lastDate))
AND PPL_Date__c >= :startDate AND PPL_Date__c
<= :lastDate
ORDER BY PPL_ThisDay__c];
}
for(PPL_Day__c day : queryDay){
if(day.PPL_Project_Days_Aggregated__r.PPL_Allocation__r.RecordType.Name
== 'Project'){
if(day.PPL_Type__c == 'Working Hours'){
projectDays.put( day.PPL_Employee__c+String.valueOf(day.PPL_Project_Days_Aggregated
__r.PPL_Allocation__c)+String.valueOf(day.PPL_ThisDay__c), day);
}
if(!projectsPerContact.containsKey(day.PPL_Employee__c)){
projectsPerContact.put(day.PPL_Employee__c, new
Set<PPL_Project__c>());
}
projectsPerContact.get(day.PPL_Employee__c).add(day.PPL_Project_Days_Aggregated__r.
PPL_Allocation__r);
}else
if(day.PPL_Project_Days_Aggregated__r.PPL_Allocation__r.RecordType.Name ==
'Absences' && day.PPL_Type__c == 'Absence' &&
day.PPL_Project_Days_Aggregated__r.PPL_Allocation__r.PPL_Motive__c != 'Vacation'){
absenceDays.put( day.PPL_Employee__c+String.valueOf(day.PPL_ThisDay__c),
day);
}else
if(day.PPL_Project_Days_Aggregated__r.PPL_Allocation__r.RecordType.Name ==
'Absences' && day.PPL_Type__c == 'Absence' &&
day.PPL_Project_Days_Aggregated__r.PPL_Allocation__r.PPL_Motive__c == 'Vacation'){
vacationDays.put( day.PPL_Employee__c+String.valueOf(day.PPL_ThisDay__c),
day);
}
contactSet.add(day.PPL_Employee__r);
}
for (User thisContact : contactSet) {
if (projectsPerContact.containsKey(thisContact.Id)) {
// Iterate through projects of the contact
for (PPL_Project__c project : projectsPerContact.get(thisContact.Id)) {
Map<String, String> row = new Map<String, String>();
String projName = '';
if (thisContact.Email != currentUserEmail) {
projName = thisContact.Name + ' | ' + project.Name;
} else {
projName = project.Name;
}
row.put('Project_Name', projName);
Integer currentDay = 1;
// Iterate through each day for the project
while (currentDay <= monthDays) {
Integer hours = 0;
String key = thisContact.Id + String.valueOf(project.Id) +
String.valueOf(currentDay);
// Retrieve the day information based on your new objects
if (projectDays.containsKey(key)) {
hours = (Integer) projectDays.get(key).PPL_Hour__c;
}
row.put('day' + currentDay, String.valueOf(hours));
currentDay++;
}
rowData.add(row);
}
}
}
for(User thisContact : contactSet){
if(projectsPerContact.containsKey(thisContact.Id)){
//Is Contact in a Project. if yes add String with project name
for(PPL_Project__c project :
projectsPerContact.get(thisContact.Id)){
Map<String, String> row = new Map<String, String>();
String projName = '';
if(thisContact.Email != currentUserEmail){
projName = thisContact.Name + ' | ' +
project.PPL_Description__c;
} else{
projName = project.PPL_Description__c;
}
row.put('Project_Name', projName);
Integer currentDay = 1;
//Checks each day for a record, if yes saves the number of
hours if not saves a 0
while(currentDay <= monthDays){
Integer hours = 0;
String key = thisContact.Id+String.valueOf(project.Id)
+String.valueOf(currentDay);
if(projectDays.containsKey(key)){
hours = (Integer)projectDays.get(key).PPL_Hour__c;
}
row.put('day'+currentDay, String.valueOf(hours));
currentDay++;
}
rowData.add(row);
}
}
// Row regarding Project
// Row regarding Absences
// switch abName to absenceName and abRow to absenceRow
String abName = '';
Integer totalAbsenceHoursCount = 0;
Map<String, String> abRow = new Map<String, String>();
Integer test = 40;
Integer currentDay = 1;
while(currentDay <= monthDays){
Integer hours = 0;
String key = thisContact.Id+String.valueOf(currentDay);
if(absenceDays.containsKey(key)){
hours = (Integer)absenceDays.get(key).PPL_Hour__c;
totalAbsenceHoursCount = totalAbsenceHoursCount + hours;
}
abRow.put('day'+currentDay, String.valueOf(hours));
currentDay++;
}
rowData.add(abRow);
if(thisContact.Email != currentUserEmail){
abName = thisContact.Name + ' | Absences - Total (' +
totalAbsenceHoursCount + ')';
} else{
abName = 'Absences - Total (' + totalAbsenceHoursCount + ')';
}
abRow.put('Project_Name', abName);
//Row regarding Vacations
//change vaName to vacationName and vaRow to vacationRow
String vaName = '';
Integer totalVacationHoursCount = 0;
Map<String, String> vaRow = new Map<String, String>();
currentDay = 1;
while(currentDay <= monthDays){
Integer hours = 0;
String key = thisContact.Id+String.valueOf(currentDay);
if(vacationDays.containsKey(key)){
hours = (Integer)vacationDays.get(key).PPL_Hour__c;
totalVacationHoursCount = totalVacationHoursCount + hours;
}
vaRow.put('day'+currentDay, String.valueOf(hours));
currentDay++;
}
rowData.add(vaRow);
if(thisContact.Email != currentUserEmail){
vaName = thisContact.Name + ' | Vacations - Total (' +
totalVacationHoursCount + ')';
} else{
vaName = 'Vacations - Total (' + totalVacationHoursCount + ')';
}
vaRow.put('Project_Name', vaName);
}
//System.debug(rowData);
if (rowData.isEmpty()) {
Map<String, String> defaultRow = new Map<String, String>();
defaultRow.put('Project_Name', 'No data available');
rowData.add(defaultRow);
}
return rowData;
}
}