Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 22
CRAP
0.00% covered (danger)
0.00%
0 / 566
EodService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 22
12656.00
0.00% covered (danger)
0.00%
0 / 566
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 getEodNotificationSettings
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 18
 createEodNotificationSettings
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 30
 updateEodNotificationSettings
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 16
 getEodNotificationStatusByCreatedDate
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getEodNotificationStatusByType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 createEodNotificationStatus
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 14
 updateEodNotificationStatus
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 15
 triggerEodNotification
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 12
 triggerTutorEODNotification
0.00% covered (danger)
0.00%
0 / 1
90.00
0.00% covered (danger)
0.00%
0 / 24
 triggerHODEODNotification
0.00% covered (danger)
0.00%
0 / 1
462.00
0.00% covered (danger)
0.00%
0 / 50
 sendTutorNotification
0.00% covered (danger)
0.00%
0 / 1
132.00
0.00% covered (danger)
0.00%
0 / 79
 sendHodNotification
0.00% covered (danger)
0.00%
0 / 1
306.00
0.00% covered (danger)
0.00%
0 / 133
 getEodNotMarkedTutorsListByDate
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 16
 checkEodMarked
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 13
 triggerMarkEOD
0.00% covered (danger)
0.00%
0 / 1
72.00
0.00% covered (danger)
0.00%
0 / 34
 checkReadyToMarkEOD
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 49
 markEod
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 15
 addTutorEodSendSmsStatus
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getCountOfEodSmsByStaffId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\dto\EodNotificationStatus;
use com\linways\core\ams\professional\mapper\EodServiceMapper;
use com\linways\core\ams\professional\request\CheckReadyToMarkEODRequest;
use com\linways\core\ams\professional\request\MarkEODRequest;
use com\linways\core\ams\professional\request\SearchStaffRequest;
use com\linways\core\ams\professional\dto\EodNotificationSettings;
use com\linways\base\util\StringUtil;
use com\linways\base\dto\Email;
use com\linways\base\dto\EmailFrom;
use com\linways\base\dto\EmailTo;
use com\linways\base\service\EmailService;
use com\linways\core\ams\professional\request\TriggerMarkEODRequest;
use Prophecy\Exception\Prophecy\MethodProphecyException;
use com\linways\core\ams\professional\dto\TutorEodSendSmsStatus;
class EodService extends BaseService
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
        $this->mapper = EodServiceMapper::getInstance()->getMapper();
    }
    // Prevent any oustide instantiation of this class
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone()
    {
    }
    // Prevent any copy of this object
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance()
    {
        if (!is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
            self::$_instance = new self();
        return self::$_instance;
    }
    /**
     * get eod notification Settings
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function getEodNotificationSettings()
    {
        $eodSettings = NULL;
        $sql = "SELECT 
                enableNotification, 
                facultyNotificationTime, 
                hodNotificationTime, 
                notifyUsers, 
                facultyMsg, 
                hodMsg
                 FROM 
                eodNotificationSettings;";
        try {
            $eodSettings = $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $eodSettings;
    }
    /**
     * create EOD notification settings
     * @param EodNotificationSettings $eodSettings
     * @throws ProfessionalException
     * @return object|NULL|\com\linways\base\util\$objectList[]
     */
    public function createEodNotificationSettings($eodSettings)
    {
        $sql = "INSERT INTO eodNotificationSettings(
                enableNotification, 
                facultyNotificationTime, 
                hodNotificationTime, 
                notifyUsers,
                facultyMsg, 
                hodMsg, 
                createdBy, 
                createdDate,
                updatedBy,
                updatedDate) 
                VALUES (
                $eodSettings->enableNotification
                '$eodSettings->facultyNotificationTime', 
                '$eodSettings->hodNotificationTime', 
                '$eodSettings->notifyUsers',
                '$eodSettings->facultyMsg',
                '$eodSettings->hodMsg',
                $eodSettings->createdBy,
                utc_timestamp(),
                $eodSettings->updatedBy,
                utc_timestamp
                )";
        try {
            return $this->executeQueryForObject($sql, true);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * update EOD notification settings
     *
     * @param EodNotificationSettings $eodSettings
     */
    public function updateEodNotificationSettings($eodSettings)
    {
        $sql = "UPDATE eodNotificationSettings SET
        enableNotification=$eodSettings->enableNotification,
        facultyNotificationTime='$eodSettings->facultyNotificationTime',
        hodNotificationTime='$eodSettings->hodNotificationTime',
        notifyUsers='$eodSettings->notifyUsers',
        facultyMsg='$eodSettings->facultyMsg',
        hodMsg='$eodSettings->hodMsg',
        updatedBy=$eodSettings->updatedBy,
        updatedDate=utc_timestamp()";
        try {
            return $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * get eod notification status
     *
     * @param string $createdDate
     * @return array
     *
     */
    public function getEodNotificationStatusByCreatedDate($createdDate)
    {
        $eodStatusList = [];
        $sql = "SELECT * FROM eodNotificationStatus WHERE createdDate='$createdDate' AND (isEmailSend=0 || isSmsSend=0)";
        try {
            $eodStatusList = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $eodStatusList;
    }
    /**
     * get eod notification status by type and date
     *
     * @param string $createdDate
     */
    public function getEodNotificationStatusByType($createdDate, $type)
    {
        $eodStatusList = [];
        $sql = "SELECT * FROM eodNotificationStatus WHERE createdDate='$createdDate' AND (isEmailSent=0 || isSmsSent=0) AND type='$type'";
        try {
            $eodStatusList = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $eodStatusList;
    }
    /**
     * insert into eodNotificationStatus
     *
     * @param EodNotificationStatus $eodStatusList
     */
    public function createEodNotificationStatus($eodStatusList)
    {
        $values = [];
        foreach ($eodStatusList as $eodStatus) {
            $values[] = "('$eodStatus->type',
                $eodStatus->staffID,
                '$eodStatus->createdDate')";
        }
        $sql = "INSERT INTO eodNotificationStatus(type, staffID, createdDate) VALUES " . implode(',', $values);
        try {
            return $this->executeQueryForObject($sql, true);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * update eodNotificationStatus
     *
     * @param EodNotificationStatus $eodStatusList
     */
    public function updateEodNotificationStatus($eodStatusList)
    {
        $sql = "UPDATE eodNotificationStatus SET
                sendDate='$eodStatusList->sendDate',
                isEmailSent=$eodStatusList->isEmailSent,
                isSmsSent=$eodStatusList->isSmsSent,
                emailError='$eodStatusList->emailError',
                smsError='$eodStatusList->smsError'
                WHERE id=$eodStatusList->id
                ";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * trigger cronjob for send eod notification
     */
    public function triggerEodNotification()
    {
        $eodNotificationSettings = $this->getEodNotificationSettings();
        if ($eodNotificationSettings->enableNotification) {
            $currentTime = strtotime(date('H:i'));
            if (strtotime($eodNotificationSettings->facultyNotificationTime) == $currentTime) {
                $this->triggerTutorEODNotification($eodNotificationSettings);
            }
            if (strtotime($eodNotificationSettings->hodNotificationTime) == $currentTime) {
                $this->triggerHODEODNotification($eodNotificationSettings);
            }
        }
    }
    /**
     * trigger cronjob and send sms and email to tutors
     *
     * @param EodNotificationStatus $eodNotificationSettings
     */
    private function triggerTutorEODNotification($eodNotificationSettings)
    {
        $date = date('Y-m-d');
        $eodDate = date('Y-m-d', strtotime(' -1 day'));
        // $notifiyUsers = explode(',', $eodNotificationSettings->notifyUsers);
        $notificationStatus = $this->getEodNotificationStatusByType($date, 'TUTOR');
        if ($notificationStatus == NULL || count($notificationStatus) <= 0) {
            $tutorList = $this->getEodNotMarkedTutorsListByDate($eodDate);
            if ($tutorList != NULL && count($tutorList) > 0) {
                $eodStatusList = [];
                foreach ($tutorList as $tutors) {
                    foreach ($tutors as $tutor) {
                        $eodStatus = new EodNotificationStatus();
                        $eodStatus->type = 'TUTOR';
                        $eodStatus->staffID = $tutor->staffID;
                        $eodStatus->createdDate = $date;
                        $eodStatusList[] = $eodStatus;
                    }
                }
                if ($eodStatusList != NULL && count($eodStatusList) > 0) {
                    $this->createEodNotificationStatus($eodStatusList);
                }
            }
        }
        $this->sendTutorNotification($eodNotificationSettings);
    }
    /**
     * trigger cronjob and send sms and email to hod and principal
     *
     * @param EodNotificationStatus $eodNotificationSettings
     */
    private function triggerHODEODNotification($eodNotificationSettings)
    {
        $date = date('Y-m-d');
        $eodDate = date('Y-m-d', strtotime(' -1 day'));
        $notMarkedTutors = [];
        $notifiyUsers = explode(',', $eodNotificationSettings->notifyUsers);
        $notificationStatusHod = $this->getEodNotificationStatusByType($date, 'HOD');
        $notificationStatusPrincipal = $this->getEodNotificationStatusByType($date, 'PRINCIPAL');
        if (($notificationStatusHod == NULL || count($notificationStatusHod) <= 0) || ($notificationStatusPrincipal == NULL || count($notificationStatusPrincipal) <= 0)) {
            $tutorList = $this->getEodNotMarkedTutorsListByDate($eodDate);
            if ($tutorList != NULL && count($tutorList) > 0) {
                $eodStatusList = [];
                $deptID = [];
                $hodList = [];
                foreach ($tutorList as $tutors) {
                    foreach ($tutors as $tutor) {
                        $deptID[] = $tutor->deptID;
                        $notMarkedTutors[$tutor->deptID] = $notMarkedTutors[$tutor->deptID] == NULL ? $tutor->staffName : $notMarkedTutors[$tutor->deptID] . "," . $tutor->staffName;
                    }
                }
                if ($notificationStatusHod == NULL && count($notificationStatusHod) <= 0) {
                    if (count($deptID) > 0 && in_array("HOD", $notifiyUsers)) {
                        $hodList = StaffService::getInstance()->getHodByDeptId(array_unique($deptID));
                    }
                    if ($hodList != NULL) {
                        foreach ($hodList as $hod) {
                            $eodStatus = new EodNotificationStatus();
                            $eodStatus->type = 'HOD';
                            $eodStatus->staffID = $hod->staffID;
                            $eodStatus->createdDate = $date;
                            $eodStatusList[] = $eodStatus;
                        }
                    }
                }
                if ($notificationStatusPrincipal == NULL && count($notificationStatusPrincipal) <= 0) {
                    if (in_array("PRINCIPAL", $notifiyUsers)) {
                        $principal = StaffService::getInstance()->getPrincipal();
                        $eodStatus = new EodNotificationStatus();
                        $eodStatus->type = 'PRINCIPAL';
                        $eodStatus->staffID = $principal->staffID;
                        $eodStatus->createdDate = $date;
                        $eodStatusList[] = $eodStatus;
                    }
                }
                if ($eodStatusList != NULL && count($eodStatusList) > 0) {
                    $this->createEodNotificationStatus($eodStatusList);
                }
            }
        }
        $this->sendHodNotification($eodNotificationSettings, $notMarkedTutors);
    }
    /**
     * send notification to tutors
     *
     * @param EodNotificationSettings $eodNotificationSettings
     */
    private function sendTutorNotification($eodNotificationSettings)
    {
        global $college_MailFromID;
        global $MailFromName;
        $date = date('Y-m-d');
        $eodDate = date('Y-m-d', strtotime(' -1 day'));
        $notificationStatus = $this->getEodNotificationStatusByType($date, 'TUTOR');
        $request = new SearchStaffRequest();
        if ($notificationStatus != NULL && count($notificationStatus) > 0) {
            foreach ($notificationStatus as $notification) {
                $request->staffID = $notification->staffID;
                $content = StringUtil::replace_tags($eodNotificationSettings->facultyMsg, array(
                    'date' => $eodDate
                ));
                $staffDetails = StaffService::getInstance()->searchStaff($request);
                if ($notification->isEmailSent == 0) {
                    if (CommonService::getInstance()->checkEmail($staffDetails->staffEmail)) {
                        $emailBody = $content;
                        $emailSubject = 'EOD Notification';
                        $emailObj = new Email();
                        $emailFrom = new EmailFrom();
                        $emailFrom->email = $college_MailFromID;
                        $emailFrom->name = $MailFromName;
                        $emailTo = new EmailTo();
                        $emailTo->email = $staffDetails->staffEmail;
                        $emailTo->name = '';
                        $emailObj->subject = $emailSubject;
                        $emailObj->body = $emailBody;
                        $emailObj->from = $emailFrom;
                        $emailObj->to = $emailTo;
                        try {
                            EmailService::getInstance()->sendEmail($emailObj);
                            $isEmailSend = 1;
                            $emailStatus = '';
                        } catch (\Exception $e) {
                            $status = 0;
                            $isEmailSend = 0;
                            $emailStatus = 'FAILED';
                            $success = FALSE;
                        }
                    } else {
                        $isEmailSend = 0;
                        $emailStatus = 'INVALID_EMAIL';
                        $success = FALSE;
                    }
                } else {
                    $isEmailSend = $notification->isEmailSent;
                    $emailStatus = $notification->emailError;
                }
                if ($notification->isSmsSent == 0) {
                    $smsSendStatus = '';
                    if (CommonService::getInstance()->checkPhoneNo($staffDetails->staffPhone)) {
                        $smscontent = $content;
                        $isSmsSend = sendSms($staffDetails->staffPhone, $smscontent, $SMS_TRANS);
                        if ($isSmsSend == 0) {
                            $smsSendStatus = 'FAILED';
                            $success = FALSE;
                        }
                    } else {
                        $isSmsSend = 0;
                        $smsSendStatus = 'INVALID_PHONE';
                        $success = FALSE;
                    }
                } else {
                    $isSmsSend = $notification->isSmsSent;
                    $smsSendStatus = $notification->smsError;
                }
                $eodStatus = new EodNotificationStatus();
                $eodStatus->emailError = $emailStatus;
                $eodStatus->id = $notification->id;
                $eodStatus->isEmailSent = $isEmailSend;
                $eodStatus->isSmsSent = $isSmsSend;
                $eodStatus->smsError = $smsSendStatus;
                $eodStatus->sendDate = date('Y-m-d H:i:s');
                if ($eodStatus) {
                    $this->updateEodNotificationStatus($eodStatus);
                }
            }
        }
    }
    /**
     * send notification to hod and principal
     *
     * @param EodNotificationSettings $eodNotificationSettings
     */
    private function sendHodNotification($eodNotificationSettings, $notMarkedTutors)
    {
        global $college_MailFromID;
        global $MailFromName;
        $date = date('Y-m-d');
        $eodDate = date('Y-m-d', strtotime(' -1 day'));
        $staffNames = [];
        $notificationStatus = $this->getEodNotificationStatusByType($date, 'HOD');
        $request = new SearchStaffRequest();
        if ($notificationStatus != NULL && count($notificationStatus) > 0) {
            foreach ($notificationStatus as $notification) {
                $request->staffID = $notification->staffID;
                $staffDetails = StaffService::getInstance()->searchStaff($request);
                $content = StringUtil::replace_tags($eodNotificationSettings->hodMsg, array(
                    'date' => $eodDate,
                    'staffNames' => $notMarkedTutors[$staffDetails->deptID]
                ));
                if ($notification->isEmailSent == 0) {
                    if (CommonService::getInstance()->checkEmail($staffDetails->staffEmail)) {
                        $emailBody = $content;
                        $emailSubject = 'EOD Notification';
                        $emailObj = new Email();
                        $emailFrom = new EmailFrom();
                        $emailFrom->email = $college_MailFromID;
                        $emailFrom->name = $MailFromName;
                        $emailTo = new EmailTo();
                        $emailTo->email = $staffDetails->staffEmail;
                        $emailTo->name = '';
                        $emailObj->subject = $emailSubject;
                        $emailObj->body = $emailBody;
                        $emailObj->from = $emailFrom;
                        $emailObj->to = $emailTo;
                        try {
                            EmailService::getInstance()->sendEmail($emailObj);
                            $isEmailSend = 1;
                            $emailStatus = '';
                        } catch (\Exception $e) {
                            $isEmailSend = 0;
                            $emailStatus = 'FAILED';
                        }
                    } else {
                        $isEmailSend = 0;
                        $emailStatus = 'INVALID_EMAIL';
                    }
                } else {
                    $isEmailSend = $notification->isEmailSent;
                    $emailStatus = $notification->emailError;
                }
                $smsSendStatus = '';
                if ($notification->isSmsSent == 0) {
                    if (CommonService::getInstance()->checkPhoneNo($staffDetails->staffPhone)) {
                        $smscontent = $content;
                        $isSmsSend = sendSms($staffDetails->staffPhone, $smscontent, $SMS_TRANS);
                        if ($isSmsSend == 0) {
                            $smsSendStatus = 'FAILED';
                            $success = FALSE;
                        }
                    } else {
                        $isSmsSend = 0;
                        $smsSendStatus = 'INVALID_PHONE';
                        $success = FALSE;
                    }
                } else {
                    $isSmsSend = $notification->isSmsSent;
                    $smsSendStatus = $notification->smsError;
                }
                $eodStatus = new EodNotificationStatus();
                $eodStatus->emailError = $emailStatus;
                $eodStatus->id = $notification->id;
                $eodStatus->isEmailSent = $isEmailSend;
                $eodStatus->isSmsSent = $isSmsSend;
                $eodStatus->smsError = $smsSendStatus;
                $eodStatus->sendDate = date('Y-m-d H:i:s');
                $this->updateEodNotificationStatus($eodStatus);
            }
        }
        $notificationStatus = $this->getEodNotificationStatusByType($date, 'PRINCIPAL');
        $request = new SearchStaffRequest();
        if ($notificationStatus != NULL && count($notificationStatus) > 0) {
            foreach ($notificationStatus as $notification) {
                $request->staffID = $notification->staffID;
                $staffDetails = StaffService::getInstance()->searchStaff($request);
                if ($notification->isEmailSent == 0) {
                    foreach ($notMarkedTutors as $deptID => $staffName) {
                        $deptDetails = DepartmentService::getInstance()->getDepartmentById($deptID);
                        $staffNames[] = $deptDetails->name . "[" . $staffName . "]";
                    }
                    if (CommonService::getInstance()->checkEmail($staffDetails->staffEmail)) {
                        $content = StringUtil::replace_tags($eodNotificationSettings->hodMsg, array(
                            'date' => $eodDate,
                            'staffNames' => implode(',', $staffNames)
                        ));
                        $emailBody = $content;
                        $emailSubject = 'EOD Notification';
                        $emailObj = new Email();
                        $emailFrom = new EmailFrom();
                        $emailFrom->email = $college_MailFromID;
                        $emailFrom->name = $MailFromName;
                        $emailTo = new EmailTo();
                        $emailTo->email = $staffDetails->staffEmail;
                        $emailTo->name = '';
                        $emailObj->subject = $emailSubject;
                        $emailObj->body = $emailBody;
                        $emailObj->from = $emailFrom;
                        $emailObj->to = $emailTo;
                        try {
                            EmailService::getInstance()->sendEmail($emailObj);
                            $isEmailSend = 1;
                            $emailStatus = '';
                        } catch (\Exception $e) {
                            $status = 0;
                            $emailStatus = 'FAILED';
                            $success = FALSE;
                        }
                    } else {
                        $isEmailSend = 0;
                        $emailStatus = 'INVALID_EMAIL';
                        $success = FALSE;
                    }
                } else {
                    $isEmailSend = $notification->isEmailSent;
                    $emailStatus = $notification->emailError;
                }
                $eodStatus = new EodNotificationStatus();
                $eodStatus->emailError = $emailStatus;
                $eodStatus->id = $notification->id;
                $eodStatus->isEmailSent = $isEmailSend;
                $eodStatus->isSmsSent = $isSmsSend;
                $eodStatus->smsError = $smsSendStatus;
                $eodStatus->sendDate = date('Y-m-d H:i:s');
                $this->updateEodNotificationStatus($eodStatus);
            }
        }
    }
    /**
     * get eod not marked turors by date
     * @param string $date
     * @return object[]|array[]|\com\linways\base\util\$objectList[][]
     */
    public function getEodNotMarkedTutorsListByDate($date)
    {
        $tutorList = [];
        $sql = "SELECT DISTINCT(bt.batchID),b.semID FROM batch_timetable bt 
                INNER JOIN batches b on b.batchId = bt.batchId
                WHERE bt.batchId NOT IN(SELECT av.batchId FROM attendance_verifyeod av WHERE av.eodDate = '$date') 
                 AND bt.batchId NOT IN(SELECT batchIDs FROM eod_settings)
                 AND bt.timetableDate = '$date';";
        $batchDetailsList = $this->executeQueryForList($sql);
        if ($batchDetailsList != NULL && count($batchDetailsList) > 0) {
            foreach ($batchDetailsList as $batchDetails) {
                $sql_tutor = "SELECT t2.staffID, t2.staffName, t2.deptID FROM batch_tutor t1, staffaccounts t2 WHERE t1.staffID = t2.staffID AND t1.batchID =$batchDetails->batchID AND t1.semID =$batchDetails->semID";
                $tutorList[] = $this->executeQueryForList($sql_tutor);
            }
        }
        return $tutorList;
    }
    /**
     * check batch eod marked or not
     * @param string $eodDate
     * @param int $batchID
     * @throws ProfessionalException
     * @return boolean
     */
    public function checkEodMarked($eodDate, $batchID)
    {
        $flag = FALSE;
        $sql = "SELECT count(batchID) as flag FROM attendance_verifyeod WHERE eodDate='$eodDate' AND batchID='$batchID'";
        try {
            $eodFlag = $this->executeQueryForObject($sql)->flag;
            if ($eodFlag) {
                $flag = TRUE;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $flag;
    }
    /**
     * This function will mark EOD if all staffs marked attendance
     * @param TriggerMarkEODRequest $request
     * @return bool
     * @throws ProfessionalException
     */
    public function triggerMarkEOD(TriggerMarkEODRequest $request)
    {
        $request = $this->realEscapeObject($request);
        if (empty($request->batchId))
            throw new ProfessionalException(ProfessionalException::INVALID_BATCH_ID, "Invalid batch given");
        if (empty($request->semId))
            throw new ProfessionalException(ProfessionalException::INVALID_SEMESTER_ID, "Invalid semester given");
        if (empty($request->date))
            throw new ProfessionalException(ProfessionalException::INVALID_DATE, "Invalid date given");
        if (empty($request->deptId))
            throw new ProfessionalException(ProfessionalException::INVALID_DEPARTMENT_ID, "Invalid department given");
        $checkReadyToMarkEODRequest = new CheckReadyToMarkEODRequest();
        $checkReadyToMarkEODRequest->batchId = $request->batchId;
        $checkReadyToMarkEODRequest->semId = $request->semId;
        $checkReadyToMarkEODRequest->date = $request->date;
        $checkReadyToMarkEODRequest->deptId = $request->deptId;
        $checkReadyToMarkEODRequest->isExcludeLastHour = $request->isExcludeLastHour;
        $isEODMarked = $this->checkEodMarked($request->date, $request->batchId);
        if ($isEODMarked)
            return false;
        try {
            $isReadyToMark = $this->checkReadyToMarkEOD($checkReadyToMarkEODRequest);
            if ($isReadyToMark) {
                $markEODRequest = new MarkEODRequest();
                $markEODRequest->batchId = $request->batchId;
                $markEODRequest->date = $request->date;
                $markEODRequest->isAutoMarked = 1;
                /**
                 * Marking EOD
                 */
                $this->markEod($markEODRequest);
                return true;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return false;
    }
    /**
     * Return true if all staff marked attendance else return false
     * if marked attendance count is greater than or equal to sum of count of students in time table assigned hours
     * by batchId, semId, hourId and date
     * @param CheckReadyToMarkEODRequest $request
     * @return bool
     * @throws ProfessionalException
     */
    public function checkReadyToMarkEOD(CheckReadyToMarkEODRequest $request)
    {
        global $ATTENDANCE_HOURS;
        $request = $this->realEscapeObject($request);
        $batchId = $request->batchId;
        $semId = $request->semId;
        $deptId = $request->deptId;
        $date = date('Y-m-d', strtotime($request->date));
        $excludeLastHour = 0;
        if ($request->isExcludeLastHour) {
            $excludeLastHour = 1;
        }
        /**
         * Checking given date is holiday
         */
        $isHoliday = CalendarService::getInstance()->isHoliday($deptId, $batchId, $date);
        if ($isHoliday)
            return false;
        if (empty($ATTENDANCE_HOURS))
            throw new ProfessionalException(ProfessionalException::ATTENDANCE_HOURS_NOT_CONFIGURED, "Configure attendance hours");
        $totalStudentsCount = -1;
        $attendanceCount = -1;
        try {
            /**
             * Fetching students count from attendance table by batch, sem, hour and date.
             */
            $sql = "SELECT count(studentID) AS attendanceCount FROM attendance 
                    WHERE batchID= $batchId AND semID = $semId AND attendanceDate = '" . $date . "'";
            $attendanceCount = $this->executeQueryForObject($sql)->attendanceCount;
            $attendanceCount = (int)$attendanceCount;
            $totalStudentsCount = 0;
            for ($i = 1; $i <= $ATTENDANCE_HOURS - $excludeLastHour; $i++) {
                /**
                 * This query returns how much students in time table assigned hour.
                 * If time table assigned for a subbatch, then this will be return count of students in subbatch
                 * else return strength of all batch
                 */
                $sql = "SELECT CASE 
                                WHEN count(sbss.subbatchID) >0 AND sh.hour IS NULL THEN count(DISTINCT sbst.studentID) 
                                WHEN count(sbss.subbatchID) =0 AND sh.hour IS NULL THEN count(DISTINCT sa.studentID)
                                ELSE 0
                                END AS studentCount
                        FROM batch_timetable btt 
                        INNER JOIN batches b ON b.batchID=btt.batchID AND b.semID = btt.semID
                        INNER JOIN studentaccount sa ON sa.batchID = b.batchID 
                        LEFT JOIN suspended_hours sh ON sh.batchID = btt.batchID and sh.semID = btt.semID 
                              AND sh.suspendedDate = btt.timetableDate AND sh.hour =btt.hourID
                        LEFT JOIN sbs_relation sbsr ON sbsr.sbsID = btt.sbsID 
                        LEFT JOIN subbatch_sbs sbss ON IF (btt.subbatchID, sbss.sbsID = sbsr.sbsID AND sbss.subbatchID = btt.subbatchID,sbss.sbsID = sbsr.sbsID)
                        LEFT JOIN subbatch_student sbst ON sbst.subbatchID = sbss.subbatchID  AND sbst.studentID=sa.studentID
                        WHERE btt.batchID= $batchId AND btt.semID = $semId AND btt.timetableDate = '" . $date . "
                        AND btt.hourID =$i";
                $studentCount = $this->executeQueryForObject($sql)->studentCount;
                $totalStudentsCount += $studentCount;
            }
            $totalStudentsCount = (int)$totalStudentsCount;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $attendanceCount >= $totalStudentsCount && $totalStudentsCount > 0;
    }
    /**
     * This will mark the EOD by batch id, date and tutor id
     * @param MarkEODRequest $request
     * @throws ProfessionalException
     */
    public function markEod(MarkEODRequest $request)
    {
        if (empty($request->batchId))
            throw new ProfessionalException(ProfessionalException::INVALID_BATCH_ID, "Couldn't mark EOD without batch");
        if (empty($request->date))
            throw new ProfessionalException(ProfessionalException::INVALID_DATE, "Invalid date given");
        if (empty($request->isAutoMarked))
            $request->isAutoMarked = 'NULL';
        $sql = "INSERT INTO attendance_verifyeod (eodDate, batchID, tutorID,is_auto_marked) 
                VALUES ('" . $request->date . "', '" . $request->batchId . "', '" . $request->tutotId . "',$request->isAutoMarked)";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    
    /**
     * add tutor eod sms send status
     * @param TutorEodSendSmsStatus $tutorEodSendSmsStatus
     * @throws ProfessionalException
     * @return \com\linways\base\dto\MySqlResult
     */
    public function addTutorEodSendSmsStatus($tutorEodSendSmsStatus)
    {
        $tutorEodSendSmsStatus = $this->realEscapeObject($tutorEodSendSmsStatus);
        
        $sql="INSERT INTO tutor_eod_send_sms_status (staffId, attendanceDate, hour, batchId, semId, createdBy, createdDate, updatedBy, updatedDate) VALUES ($tutorEodSendSmsStatus->staffId, '$tutorEodSendSmsStatus->attendanceDate', $tutorEodSendSmsStatus->hour$tutorEodSendSmsStatus->batchId$tutorEodSendSmsStatus->semId$tutorEodSendSmsStatus->createdBy, utc_timestamp(), $tutorEodSendSmsStatus->updatedBy, utc_timestamp())
";
        try {
            return $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    
    /**
     * sms sent count of a staff
     * @param TutorEodSendSmsStatus $tutorEodSendSmsStatus
     * @throws ProfessionalException
     * @return \com\linways\base\dto\MySqlResult
     */
    public function getCountOfEodSmsByStaffId($tutorEodSendSmsStatus)
    {
        $sql="SELECT count(id) as totalSmsSend FROM tutor_eod_send_sms_status WHERE staffId=$tutorEodSendSmsStatus->staffId AND attendanceDate='$tutorEodSendSmsStatus->attendanceDate' AND batchId=$tutorEodSendSmsStatus->batchId AND semId=$tutorEodSendSmsStatus->semId AND hour=$tutorEodSendSmsStatus->hour";
        try {
            return $this->executeQueryForObject($sql)->totalSmsSend;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}