1<?php
2
3/**
4 * PluginLeaveRequest
5 *
6 * This class has been auto-generated by the Doctrine ORM Framework
7 *
8 * @package    ##PACKAGE##
9 * @subpackage ##SUBPACKAGE##
10 * @author     ##NAME## <##EMAIL##>
11 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
12 */
13abstract class PluginLeaveRequest extends BaseLeaveRequest {
14
15    private $leave = null;
16    private $leaveCount = null;
17    private $numberOfDays = null;
18    private $leaveDuration = null;
19    private $statusCounter = array();
20    private $workShiftHoursPerDay = null;
21
22    // const LEAVE_REQUEST_STATUS_APPROVED = 'Scheduled';
23    // const LEAVE_REQUEST_STATUS_CANCELLED = 'Cancelled';
24    // const LEAVE_REQUEST_STATUS_REJECTED = 'Rejected';
25    const LEAVE_REQUEST_STATUS_DIFFER = -2;
26
27    public function getNumberOfDays() {
28        $this->_fetchLeave();
29        return number_format($this->numberOfDays, 2);
30    }
31
32    private function getStatusCounter() {
33        return $this->statusCounter;
34    }
35
36    public function getLeaveDuration() {
37
38        if ($this->leaveCount == 1) {
39            $startTime = $this->leave[0]->getStartTime();
40            $endTime = $this->leave[0]->getEndTime();
41
42            if ((!empty($startTime) && !empty($endTime)) && ("{$startTime} {$endTime}" != '00:00:00 00:00:00')) {
43                return "{$startTime} to {$endTime}";
44            } else {
45                $totalDuration = $this->leave[0]->getLengthHours();
46                if (!empty($totalDuration)) {
47                    return number_format($totalDuration, 2) . ' hours';
48                } else {
49                    return number_format($this->_getWorkShiftHoursPerDay(), 2) . ' hours';
50                }
51            }
52        } else {
53            return number_format($this->leaveDuration, 2) . ' hours';
54        }
55    }
56
57    public function getLeaveBreakdown() {
58        $this->_fetchLeave();
59
60        $statusStrings = array();
61
62        foreach ($this->statusCounter as $status => $count) {
63            if (!is_null($status)) {
64                $statusStrings[] = __(ucwords(strtolower(Leave::getTextForLeaveStatus($status)))) . "(" . number_format($count, 2) . ")";
65            }
66        }
67
68        return implode(', ', $statusStrings);
69    }
70
71    public function getLeaveBalance() {
72        $balance = '';
73        $leaveEntitlementService = new LeaveEntitlementService();
74        $employeeId = $this->getEmpNumber();
75        $leaveTypeId = $this->getLeaveTypeId();
76
77
78        $leaveBalance = $leaveEntitlementService->getLeaveBalance( $employeeId ,$leaveTypeId);
79        if( $leaveBalance instanceof LeaveBalance){
80            $balance = $leaveBalance->getBalance();
81        }
82
83        return $balance;
84    }
85
86    private function _fetchLeave() {
87        if (is_null($this->leave)) {
88            $this->leave = $this->getLeave();
89            $this->_parseLeave();
90        }
91    }
92
93    public function getLeaveStatusId() {
94        $this->_fetchLeave();
95        if ($this->isStatusDiffer()) {
96            return self::LEAVE_REQUEST_STATUS_DIFFER;
97        } else {
98            reset($this->statusCounter);
99            $firstKey = key($this->statusCounter);
100            return $firstKey;
101        }
102    }
103
104    public function getLeaveDateRange() {
105
106        $this->_fetchLeave();
107        $leaveCount = count($this->leave);
108
109        if ($leaveCount == 1) {
110            return $this->leave[0]->getFormattedLeaveDateToView();
111        } else {
112            $firstDate = $this->leave[0]->getDate();
113            $lastDate = $this->leave[$leaveCount - 1]->getDate();
114
115            if (strtotime($firstDate) > strtotime($lastDate)) {
116                $startDate = $lastDate;
117                $endDate = $firstDate;
118            } else {
119                $startDate = $firstDate;
120                $endDate = $lastDate;
121            }
122            return sprintf('%s %s %s', set_datepicker_date_format($startDate), __('to'), set_datepicker_date_format($endDate));
123        }
124    }
125
126    public function getLeaveStartAndEndDate() {
127
128        $this->_fetchLeave();
129        $leaveCount = count($this->leave);
130
131
132        if ($leaveCount == 1) {
133            $startDate = $endDate = $this->leave[0]->getDate();
134        } else {
135            $firstDate = $this->leave[0]->getDate();
136            $lastDate = $this->leave[$leaveCount - 1]->getDate();
137
138            if (strtotime($firstDate) > strtotime($lastDate)) {
139                $startDate = $lastDate;
140                $endDate = $firstDate;
141            } else {
142                $startDate = $firstDate;
143                $endDate = $lastDate;
144            }
145        }
146
147        return array($startDate, $endDate);
148    }
149
150    public function getLeaveDates() {
151
152        $this->_fetchLeave();
153
154        $dates = array();
155        if (count($this->leave) > 0) {
156            foreach ($this->leave as $leave) {
157                $dates[] = $leave->getDate();
158            }
159        }
160
161        return $dates;
162    }
163
164    private function _parseLeave() {
165        $this->numberOfDays = 0.0;
166        $this->leaveDuration = 0.0;
167
168        // Counting leave
169        $this->leaveCount = $this->leave->count();
170
171        $this->statusCounter = array();
172
173        foreach ($this->leave as $leave) {
174            // Calculating number of days and duration
175            $dayLength = (float) $leave->getLengthDays();
176
177            //this got changed to fix sf-3019087,3044234 $hourLength = $dayLength * $this->_getWorkShiftHoursPerDay();
178            $hourLength = (float) $leave->getLengthHours();
179            if ($dayLength >= 1) {
180                $hourLength = $dayLength * (float) $leave->getLengthHours();
181            }
182
183            if ($hourLength == 0.0) {
184                $hourLength = (float) $leave->getLengthHours();
185            }
186
187            $this->leaveDuration += $hourLength;
188
189            //if($hourLength > 0) {
190            $this->numberOfDays += $dayLength;
191            //}
192
193            if (!$leave->isNonWorkingDay()) {
194
195                // Populating leave breakdown
196
197                $status = $leave->getStatus();
198                $statusDayLength = ($dayLength != 0) ? $dayLength : 1;
199                if ($hourLength > 0) {
200                    if (array_key_exists($status, $this->statusCounter)) {
201                        $this->statusCounter[$status]+= $statusDayLength;
202                    } else {
203                        $this->statusCounter[$status] = $statusDayLength;
204                    }
205                }
206            }
207        }
208
209        //is there any use of this block ?
210        /* if ($this->numberOfDays == 1.0) {
211          $this->numberOfDays = $this->leave[0]->getLengthDays();
212          } */
213
214    }
215
216    private function _getWorkShiftHoursPerDay() {
217
218        if (!isset($this->workShiftHoursPerDay)) {
219            $employeeWorkshift = $this->getEmployee()->getEmployeeWorkShift();
220            if ($employeeWorkshift->count() > 0) {
221                $this->workShiftHoursPerDay = $employeeWorkshift[0]->getWorkShift()->getHoursPerDay();
222            } else {
223                $this->workShiftHoursPerDay = WorkShift::DEFAULT_WORK_SHIFT_LENGTH;
224            }
225        }
226
227        return $this->workShiftHoursPerDay;
228    }
229
230    private function _AreAllTaken() {
231
232        $flag = true;
233
234        foreach ($this->leave as $leave) {
235            if ($leave->getStatus() != Leave::LEAVE_STATUS_LEAVE_TAKEN && $leave->getLengthHours() != '0.00') {
236                $flag = false;
237                break;
238            }
239        }
240
241        return $flag;
242    }
243
244    public function isStatusDiffer() {
245        $this->_fetchLeave();
246        if (count($this->getStatusCounter()) > 1) {
247            return true;
248        } else {
249            return false;
250        }
251    }
252
253    public function getLeaveItems() {
254
255        $leaveRequestDao = new LeaveRequestDao();
256        return $leaveRequestDao->fetchLeave($this->getId());
257    }
258
259    public function getLeaveTypeName() {
260        return $this->getLeaveType()->getName();
261    }
262
263
264    public function getLatestCommentAsText() {
265        $latestComment = '';
266        $leaveComments = $this->getLeaveRequestComment();
267
268        if (count($leaveComments) > 0) {
269            $lastComment = $leaveComments->getLast();
270            $latestComment = $lastComment->getComments();
271        }
272
273        return $latestComment;
274    }
275
276    public function getCommentsAsText() {
277        $leaveComments = $this->getLeaveRequestComment();
278
279        $allComments = '';
280
281        // show last comment only
282        if (count($leaveComments) > 0) {
283
284            foreach ($leaveComments as $comment) {
285                $created = new DateTime($comment->getCreated());
286                $createdAt = set_datepicker_date_format($created->format('Y-m-d')) . ' ' . $created->format('H:i');
287
288                $formatComment = "(" . $createdAt . ' - ' . $comment->getCreatedByName() . ") \n" .
289                        $comment->getComments();
290                $allComments = $formatComment . "\n\n" . $allComments;
291            }
292        }
293
294        return $allComments;
295    }
296
297}
298