PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/AppliedSolutions/AWPAnalytics/Modules/web_services/Personal/module.php

https://github.com/sergiygladkyy/OEF
PHP | 213 lines | 144 code | 42 blank | 27 comment | 12 complexity | 8bb694c863317fb587668284b2856197 MD5 | raw file
  1. <?php
  2. /**
  3. * Web-service action "getEmployeeHours"
  4. *
  5. * @param array $attributes
  6. * @return array
  7. */
  8. function getEmployeeHours(array $attributes)
  9. {
  10. $period = empty($attributes['Period']) ? 'This Month' : $attributes['Period'];
  11. $employee = MEmployees::retrieveCurrentEmployee();
  12. $result = array(
  13. 'Hours' => array(
  14. 'actual' => 0,
  15. 'max' => 0
  16. ),
  17. 'Overtime' => array(
  18. 'actual' => 0,
  19. 'max' => 0
  20. ),
  21. 'Extra' => array(
  22. 'actual' => 0,
  23. 'max' => 0
  24. )
  25. );
  26. if (null === ($period = MGlobal::parseDatePeriodString($period)))
  27. {
  28. throw new Exception('Invalid period');
  29. }
  30. $container = Container::getInstance();
  31. // Retrieve actual
  32. $cmodel = $container->getCModel('AccumulationRegisters', 'EmployeeHoursReported');
  33. $totals = $cmodel->getTotals($period, array('criteria' => array('Employee' => $employee)));
  34. foreach ($totals as $row)
  35. {
  36. $result['Hours']['actual'] += $row['Hours'];
  37. $result['Overtime']['actual'] += $row['OvertimeHours'];
  38. $result['Extra']['actual'] += $row['ExtraHours'];
  39. }
  40. // Calculate max
  41. $schedules = MEmployees::retrieveSchedulesByPeriod($employee, $period['from'], $period['to']);
  42. if (empty($schedules)) throw new Exception('Unknow schedules');
  43. $vacations = MVacation::getScheduleVarianceDays($employee, $period['from'], $period['to']);
  44. foreach ($schedules as $schedID => $periods)
  45. {
  46. foreach ($periods as $dates)
  47. {
  48. $schedule = MSchedules::getSchedule($schedID, $dates['from'], $dates['to']);
  49. if (empty($schedule)) throw new Exception('Unknow schedule');
  50. foreach ($schedule as $date => $hours)
  51. {
  52. if (isset($vacations[$date])) continue;
  53. if ($hours > 0)
  54. {
  55. $result['Hours']['max'] += $hours;
  56. $result['Overtime']['max'] += 24 - $hours;
  57. }
  58. else
  59. {
  60. $result['Extra']['max'] += 24;
  61. }
  62. }
  63. }
  64. }
  65. return $result;
  66. }
  67. /**
  68. * Web-service action "getEmployeeVacationDays"
  69. *
  70. * @param array $attributes
  71. * @return array
  72. */
  73. function getEmployeeVacationDays(array $attributes)
  74. {
  75. $result = array(
  76. 'daysEligible' => 0,
  77. 'daysAccounted' => 0,
  78. 'daysSpent' => 0,
  79. 'nextMondayVacationEnds' => ''
  80. );
  81. if (0 === ($employee = MEmployees::retrieveCurrentEmployee()))
  82. {
  83. throw new Exception('Unknow employee');
  84. }
  85. $ts = time();
  86. $date = date('Y-m-d', $ts);
  87. // Retrieve altogether
  88. $hist = MEmployees::getLastHiringRecord($employee, $date);
  89. if (empty($hist)) throw new Exception('Employee not hiring');
  90. $result['daysEligible'] = $hist['YearlyVacationDays'];
  91. // Retrieve actual
  92. $container = Container::getInstance();
  93. $cmodel = $container->getCModel('AccumulationRegisters', 'EmployeeVacationDays');
  94. $prev = $cmodel->getTotals(date('Y', $ts).'-01-01', array('criteria' => array('Employee' => $employee)));
  95. $total = $cmodel->getTotals($date, array('criteria' => array('Employee' => $employee)));
  96. $accum = $cmodel->getTotals(array(date('Y', $ts).'-01-01', $date), array('operation' => '+', 'criteria' => array('Employee' => $employee)));
  97. $pvd = isset($prev[0]['VacationDays']) ? $prev[0]['VacationDays'] : 0;
  98. $avd = isset($accum[0]['VacationDays']) ? $accum[0]['VacationDays'] : 0;
  99. $tvd = isset($total[0]['VacationDays']) ? $total[0]['VacationDays'] : 0;
  100. $result['daysAccounted'] = $pvd + $avd;
  101. $result['daysSpent'] = $result['daysAccounted'] - $tvd;
  102. // Retrieve endDate
  103. $day = date('w', $ts);
  104. $day = $day == 0 ? 6 : $day - 1;
  105. $start = mktime(0,0,0, date('m', $ts), date('d', $ts) + 7 - $day, date('Y'));
  106. $end = MVacation::getEndDate($hist['Schedule'], date('Y-m-d', $start), $total[0]['VacationDays']);
  107. $result['nextMondayVacationEnds'] = date('Y-m-d', $end);
  108. return $result;
  109. }
  110. /**
  111. * Web-service action "getEmployeeProjects"
  112. *
  113. * @param array $attributes
  114. * @return array
  115. */
  116. function getEmployeeProjects(array $attributes)
  117. {
  118. // Check attributes
  119. $date = empty($attributes['Date']) ? date('Y-m-d') : $attributes['Date'];
  120. $employee = MEmployees::retrieveCurrentEmployee();
  121. $result = array(
  122. 'list' => array(),
  123. 'links' => array(),
  124. 'fields' => array('Project', 'Nearest Ms', 'Hrs Allocated/Spent')
  125. );
  126. if (empty($employee)) return $result;
  127. $projects = MProjects::getEmployeeProjects($employee, $date, $date, true, array('key' => 'Project'));
  128. if (empty($projects)) return $result;
  129. $proIDS = array_keys($projects);
  130. $container = Container::getInstance();
  131. $odb = $container->getODBManager();
  132. $query = "SELECT `Project`, MIN(`MileStoneDeadline`) AS `MileStoneDeadline` ".
  133. "FROM information_registry.MilestoneRecords ".
  134. "WHERE `Project` IN (".implode(',', $proIDS).") AND `MileStoneDeadline` >= '".$date."'".
  135. "GROUP BY `Project`";
  136. if (null === ($ms = $odb->loadAssocList($query, array('key' => 'Project'))))
  137. {
  138. throw new Exception('Database error');
  139. }
  140. // Allocated Hours
  141. $aHours = MEmployees::getHoursAllocated($employee, $proIDS);
  142. // Hours SPENT
  143. $model = $container->getCModel('AccumulationRegisters', 'EmployeeHoursReported');
  144. $sRows = $model->getTotals(array(date('Y-m', strtotime($date)).'-01 00:00:00', $date), array('criteria' => array('Project' => $proIDS, 'Employee' => $employee)));
  145. $spent = array();
  146. foreach ($sRows as $row)
  147. {
  148. if (isset($spent[$row['Project']]))
  149. {
  150. $spent[$row['Project']] += $row['Hours'];
  151. }
  152. else
  153. {
  154. $spent[$row['Project']] = $row['Hours'];
  155. }
  156. }
  157. // Result
  158. foreach ($proIDS as $project)
  159. {
  160. $al = isset($aHours[$project]) ? $aHours[$project]['HoursAllocated'] : 0;
  161. $sp = isset($spent[$project]) ? $spent[$project] : 0;
  162. $result['list'][] = array(
  163. 0 => $project,
  164. 1 => (isset($ms[$project]) ? $ms[$project]['MileStoneDeadline'] : ''),
  165. 2 => $al.'/'.$sp
  166. );
  167. }
  168. $result['links']['Project'] = $container->getCModel('catalogs', 'Projects')->retrieveLinkData($proIDS);
  169. return $result;
  170. }
  171. ?>