api.taskslabortime.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Implements try of tasks execution time planning and fair per-employee allocation
  4. */
  5. class TasksLaborTime {
  6. /**
  7. * Salary object placeholder
  8. *
  9. * @var object
  10. */
  11. protected $salary = '';
  12. /**
  13. * Contains all jobtypes expected labor times as jobtypeid=>time in minutes
  14. *
  15. * @var array
  16. */
  17. protected $allJobTimes = array();
  18. /**
  19. * Contains all tasks filtered by date
  20. *
  21. * @var array
  22. */
  23. protected $allTasksFiltered = array();
  24. /**
  25. * System message helper instance
  26. *
  27. * @var object
  28. */
  29. protected $messages = '';
  30. /**
  31. * Contains date for rendering basic report
  32. *
  33. * @var string
  34. */
  35. protected $showDate = '';
  36. /**
  37. * Contains all employee as id=>name
  38. *
  39. * @var array
  40. */
  41. protected $allEmployee = array();
  42. //predefined URLS, routes, etc..
  43. const URL_ME = '?module=report_taskslabortime';
  44. const PROUTE_DATE = 'tasklabortimedatefilter';
  45. public function __construct() {
  46. $this->setDateFilter();
  47. $this->initMessages();
  48. $this->initSalary();
  49. $this->loadJobTimes();
  50. $this->loadTasks();
  51. $this->loadEmployee();
  52. }
  53. /**
  54. * Sets date to render report based on search controls state
  55. *
  56. * @return void
  57. */
  58. protected function setDateFilter() {
  59. if (ubRouting::checkPost(self::PROUTE_DATE)) {
  60. $this->showDate = ubRouting::post(self::PROUTE_DATE, 'mres');
  61. } else {
  62. $this->showDate = curdate();
  63. }
  64. }
  65. /**
  66. * Inits system messages helper
  67. *
  68. * @return void
  69. */
  70. protected function initMessages() {
  71. $this->messages = new UbillingMessageHelper();
  72. }
  73. /**
  74. * Inits salary instance for further usage
  75. *
  76. * @rerutn void
  77. */
  78. protected function initSalary() {
  79. $this->salary = new Salary();
  80. }
  81. /**
  82. * Loads expected jobtype labor times into protected property
  83. *
  84. * @return void
  85. */
  86. protected function loadJobTimes() {
  87. $this->allJobTimes = $this->salary->getAllJobTimes();
  88. }
  89. /**
  90. * Loads all tasks by some date from database
  91. *
  92. * @return void
  93. */
  94. protected function loadTasks() {
  95. $this->allTasksFiltered = ts_getAllTasksByDate($this->showDate);
  96. }
  97. /**
  98. * Loads all employee data from database
  99. *
  100. * @return void
  101. */
  102. protected function loadEmployee() {
  103. $this->allEmployee = ts_GetAllEmployee();
  104. }
  105. /**
  106. * Renders default module search form with some controls
  107. *
  108. * @return string
  109. */
  110. public function renderSearchForm() {
  111. $result = '';
  112. $inputs = wf_DatePickerPreset(self::PROUTE_DATE, $this->showDate, true) . ' ';
  113. $inputs .= wf_Submit(__('Show'));
  114. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  115. return($result);
  116. }
  117. /**
  118. * Returns job type timing from salary directory
  119. *
  120. * @param int $jobTypeId
  121. *
  122. * @return int
  123. */
  124. protected function getJobtypeTiming($jobTypeId) {
  125. $result = 0;
  126. if (isset($this->allJobTimes[$jobTypeId])) {
  127. $result = $this->allJobTimes[$jobTypeId];
  128. }
  129. return($result);
  130. }
  131. /**
  132. * Returns current instance date filter value
  133. *
  134. * @return string
  135. */
  136. public function getDateFilter() {
  137. return($this->showDate);
  138. }
  139. /**
  140. * Renders report in human-viewable format
  141. *
  142. * @return string
  143. */
  144. protected function renderTimeline($timelineData) {
  145. $result = '';
  146. $containerId = 'timeline' . wf_InputId();
  147. $timelineCode = "['Activity', 'Start Time', 'End Time'],";
  148. if (!empty($timelineData)) {
  149. foreach ($timelineData as $key => $eachTime) {
  150. if ($eachTime['time'] != 0) {
  151. $startTime = $this->showDate . ' 08:00:00';
  152. $startTime = strtotime($startTime);
  153. $startYear = date("Y", $startTime);
  154. $startMonth = date("n", $startTime) - 1;
  155. $startDay = date("d", $startTime);
  156. $startHour = date("H", $startTime);
  157. $startMinute = date("i", $startTime);
  158. $endTime = $startTime + ($eachTime['time'] * 60);
  159. $endYear = date("Y", $endTime);
  160. $endMonth = date("n", $endTime) - 1;
  161. $endDay = date("d", $endTime);
  162. $endHour = date("H", $endTime);
  163. $endMinute = date("i", $endTime);
  164. $timelineCode .= "
  165. ['" . $key . "',
  166. new Date(" . $startYear . ", " . $startMonth . ", " . $startDay . ", " . $startHour . ", " . $startMinute . "),
  167. new Date(" . $endYear . ", " . $endMonth . ", " . $endDay . ", " . $endHour . ", " . $endMinute . ")],";
  168. }
  169. }
  170. $timelineCode = zb_CutEnd($timelineCode);
  171. }
  172. $result .= wf_tag('div', false, '', 'id="' . $containerId . '"') . wf_tag('div', true);
  173. $result .= wf_tag('script', false, '', 'type="text/javascript" src="https://www.gstatic.com/charts/loader.js"') . wf_tag('script', true);
  174. $result .= wf_tag('script', false);
  175. $result .= "google.charts.load('current', {'packages':['timeline']});";
  176. $result .= "google.charts.setOnLoadCallback(drawChart);";
  177. $result .= " function drawChart() {
  178. var data = google.visualization.arrayToDataTable([
  179. " . $timelineCode . "
  180. ]);
  181. var paddingHeight = 50;
  182. var rowHeight = data.getNumberOfRows() * 41;
  183. var chartHeight = rowHeight + paddingHeight;
  184. var options = {
  185. height: chartHeight,
  186. hAxis: {
  187. format: 'HH:mm'
  188. }
  189. };
  190. var chart = new google.visualization.Timeline(document.getElementById('" . $containerId . "'));
  191. chart.draw(data, options);
  192. }
  193. ";
  194. $result .= wf_tag('script', true);
  195. return($result);
  196. }
  197. /**
  198. * Renders basic report
  199. *
  200. * @return string
  201. */
  202. public function renderReport() {
  203. $result = '';
  204. $timelineData = array();
  205. $totalTasksTime = 0;
  206. if (!empty($this->allTasksFiltered)) {
  207. foreach ($this->allTasksFiltered as $io => $eachTask) {
  208. $taskEmployeeName = $this->allEmployee[$eachTask['employee']];
  209. $jobTiming = $this->getJobtypeTiming($eachTask['jobtype']);
  210. if (!empty($taskEmployeeName)) {
  211. if (isset($timelineData[$taskEmployeeName])) {
  212. $timelineData[$taskEmployeeName]['time'] += $jobTiming;
  213. $timelineData[$taskEmployeeName]['taskscount'] ++;
  214. } else {
  215. $timelineData[$taskEmployeeName]['time'] = $jobTiming;
  216. $timelineData[$taskEmployeeName]['taskscount'] = 1;
  217. }
  218. $totalTasksTime += $jobTiming;
  219. }
  220. }
  221. if (!empty($timelineData)) {
  222. $cells = wf_TableCell(__('Employee'));
  223. $cells .= wf_TableCell(__('Time'));
  224. $cells .= wf_TableCell(__('Tasks'));
  225. $cells .= wf_TableCell(__('Percent'), '50%');
  226. $rows = wf_TableRow($cells, 'row1');
  227. foreach ($timelineData as $employeeName => $tasksTiming) {
  228. $cells = wf_TableCell($employeeName);
  229. $cells .= wf_TableCell(zb_formatTime(($tasksTiming['time'] * 60)), '', '', 'sorttable_customkey="' . $tasksTiming['time'] . '"');
  230. $cells .= wf_TableCell($tasksTiming['taskscount']);
  231. $cells .= wf_TableCell(web_bar($tasksTiming['time'], $totalTasksTime));
  232. $rows .= wf_TableRow($cells, 'row5');
  233. }
  234. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  235. $result .= wf_delimiter(0);
  236. $result .= $this->renderTimeline($timelineData);
  237. }
  238. } else {
  239. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  240. }
  241. return($result);
  242. }
  243. }