api.tasksduplicates.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Implements basic tasks per-address dupicates report
  4. */
  5. class TasksDuplicates {
  6. /**
  7. * Contains default from-date filter
  8. *
  9. * @var string
  10. */
  11. protected $dateFrom = '';
  12. /**
  13. * Contains default to-date filter
  14. *
  15. * @var string
  16. */
  17. protected $dateTo = '';
  18. /**
  19. * Contains optional job type to filter
  20. *
  21. * @var int
  22. */
  23. protected $jobTypeId = '';
  24. /**
  25. * Contains optional secondary job type filter
  26. *
  27. * @var int
  28. */
  29. protected $secondaryJobType = '';
  30. /**
  31. * Contains available jobtypes as id=>jobtypename
  32. *
  33. * @var array
  34. */
  35. protected $allJobtypes = array();
  36. /**
  37. * Contains array of available employee as id=>name
  38. *
  39. * @var array
  40. */
  41. protected $allEmployee = array();
  42. /**
  43. * System messages helper placeholder
  44. *
  45. * @var object
  46. */
  47. protected $messages = '';
  48. /**
  49. * Tasks datasource database abstraction layer placeholder
  50. *
  51. * @var object
  52. */
  53. protected $tasksDB = '';
  54. /**
  55. * Some predefined URLs, routes etc
  56. */
  57. const URL_ME = '?module=report_taskduplicates';
  58. const URL_SHOWTASK = '?module=taskman&edittask=';
  59. const PROUTE_DATEFROM = 'datefromfilter';
  60. const PROUTE_DATETO = 'datetofilter';
  61. const PROUTE_JOBTYPE = 'jobtypeidfilter';
  62. const PROUTE_SECJOBTYPE = 'secondaryjobtypeidfilter';
  63. const PROUTE_SHOWREPORT = 'renderthisreport';
  64. const TABLE_DATASOURCE = 'taskman';
  65. /**
  66. * Creates new report instance
  67. */
  68. public function __construct() {
  69. $this->initMessages();
  70. $this->setDates();
  71. $this->setJobtype();
  72. $this->loadJobTypes();
  73. $this->loadEmployee();
  74. $this->initTasksDB();
  75. }
  76. /**
  77. * Sets actual date filters properties
  78. *
  79. * @return void
  80. */
  81. protected function setDates() {
  82. if (ubRouting::checkPost(array(self::PROUTE_DATEFROM, self::PROUTE_DATETO))) {
  83. $this->dateFrom = ubRouting::post(self::PROUTE_DATEFROM, 'mres');
  84. $this->dateTo = ubRouting::post(self::PROUTE_DATETO, 'mres');
  85. } else {
  86. $this->dateFrom = date("Y-m-01");
  87. $this->dateTo = curdate();
  88. }
  89. }
  90. /**
  91. * Sets optional jobtype filter property
  92. *
  93. * @return void
  94. */
  95. protected function setJobtype() {
  96. if (ubRouting::checkPost(self::PROUTE_JOBTYPE)) {
  97. $this->jobTypeId = ubRouting::post(self::PROUTE_JOBTYPE, 'int');
  98. }
  99. if (ubRouting::checkPost(self::PROUTE_SECJOBTYPE)) {
  100. $this->secondaryJobType = ubRouting::post(self::PROUTE_SECJOBTYPE, 'int');
  101. }
  102. }
  103. /**
  104. * Inits system messages helper
  105. *
  106. * @return void
  107. */
  108. protected function initMessages() {
  109. $this->messages = new UbillingMessageHelper();
  110. }
  111. /**
  112. * Inits tasks abstraction database layer
  113. *
  114. * @return void
  115. */
  116. protected function initTasksDB() {
  117. $this->tasksDB = new NyanORM(self::TABLE_DATASOURCE);
  118. }
  119. /**
  120. * Loads available jobtypes from database
  121. *
  122. * @return void
  123. */
  124. protected function loadJobTypes() {
  125. $this->allJobtypes = ts_GetAllJobtypes();
  126. }
  127. /**
  128. * Loads existing employee data from database
  129. *
  130. * @return void
  131. */
  132. protected function loadEmployee() {
  133. $this->allEmployee = ts_GetAllEmployee();
  134. }
  135. /**
  136. * Renders module search form
  137. *
  138. * @return string
  139. */
  140. public function renderSearchForm() {
  141. $result = '';
  142. $inputs = wf_HiddenInput(self::PROUTE_SHOWREPORT, 'true');
  143. $inputs .= wf_DatePickerPreset(self::PROUTE_DATEFROM, $this->dateFrom) . ' ' . __('Date from') . ' ';
  144. $inputs .= wf_DatePickerPreset(self::PROUTE_DATETO, $this->dateTo) . ' ' . __('Date to') . ' ';
  145. $jobTypesArr = array(0 => __('Any'));
  146. $jobTypesArr += $this->allJobtypes;
  147. $inputs .= wf_Selector(self::PROUTE_JOBTYPE, $jobTypesArr, __('Job type'), $this->jobTypeId, false) . ' ';
  148. if ($this->jobTypeId) {
  149. $inputs .= wf_Selector(self::PROUTE_SECJOBTYPE, $jobTypesArr, __('Or'), $this->secondaryJobType, false) . ' ';
  150. }
  151. $inputs .= wf_Submit(__('Search'));
  152. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  153. return($result);
  154. }
  155. /**
  156. * Preloads and process report data from database
  157. *
  158. * @return array
  159. */
  160. protected function getDuplicatesTasks() {
  161. $result = array();
  162. $addrTmp = array();
  163. $this->tasksDB->where('startdate', 'BETWEEN', $this->dateFrom . "' AND '" . $this->dateTo);
  164. $this->tasksDB->orderBy('startdate', 'DESC');
  165. $allTasks = $this->tasksDB->getAll('id');
  166. if (!empty($allTasks)) {
  167. foreach ($allTasks as $io => $each) {
  168. if ($this->jobTypeFilter($each)) {
  169. $addrTmp[$each['address']][] = $each;
  170. }
  171. }
  172. if (!empty($addrTmp)) {
  173. foreach ($addrTmp as $io => $each) {
  174. $tasksCount = sizeof($each);
  175. if ($tasksCount > 1) {
  176. $result[$io] = $each;
  177. }
  178. }
  179. }
  180. }
  181. return($result);
  182. }
  183. /**
  184. * Check is some job pass some jobtype filters if its applied?
  185. *
  186. * @param array $jobData
  187. *
  188. * @return bool
  189. */
  190. protected function jobTypeFilter($taskData) {
  191. $result = false;
  192. if (!empty($this->jobTypeId)) {
  193. if ($taskData['jobtype'] == $this->jobTypeId) {
  194. $result = true;
  195. } else {
  196. //additional jobtype filter may be?
  197. if ($this->secondaryJobType) {
  198. if ($taskData['jobtype'] == $this->secondaryJobType) {
  199. $result = true;
  200. }
  201. }
  202. }
  203. } else {
  204. $result = true;
  205. }
  206. return($result);
  207. }
  208. /**
  209. * Renders report itself.
  210. *
  211. * @return string
  212. */
  213. public function renderReport() {
  214. $result = '';
  215. $allTasks = $this->getDuplicatesTasks();
  216. if (!empty($allTasks)) {
  217. foreach ($allTasks as $eachAddress => $tasksArr) {
  218. $dupCount = sizeof($tasksArr);
  219. $result .= wf_tag('strong') . $eachAddress . ', ' . __('Tasks') . ': ' . $dupCount . wf_tag('strong', true);
  220. $cells = wf_TableCell(__('ID'), '5%');
  221. $cells .= wf_TableCell(__('Address'), '25%');
  222. $cells .= wf_TableCell(__('Job type'), '10%');
  223. $cells .= wf_TableCell(__('Phone'), '20%');
  224. $cells .= wf_TableCell(__('Employee'), '20%');
  225. $cells .= wf_TableCell(__('Target date'), '10%');
  226. $cells .= wf_TableCell(__('Done'), '5%');
  227. $cells .= wf_TableCell(__('Actions'), '5%');
  228. $rows = wf_TableRow($cells, 'row1');
  229. foreach ($tasksArr as $index => $taskData) {
  230. $cells = wf_TableCell($taskData['id']);
  231. $cells .= wf_TableCell($taskData['address']);
  232. $cells .= wf_TableCell(@$this->allJobtypes[$taskData['jobtype']]);
  233. $cells .= wf_TableCell($taskData['phone']);
  234. $cells .= wf_TableCell(@$this->allEmployee[$taskData['employee']]);
  235. $cells .= wf_TableCell($taskData['startdate']);
  236. $cells .= wf_TableCell(web_bool_led($taskData['status']));
  237. $taskActs = wf_Link(self::URL_SHOWTASK . $taskData['id'], web_edit_icon());
  238. $cells .= wf_TableCell($taskActs);
  239. $rows .= wf_TableRow($cells, 'row5');
  240. }
  241. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  242. }
  243. } else {
  244. $result .= $this->messages->getStyledMessage(__('Nothing found'), 'success');
  245. }
  246. return($result);
  247. }
  248. /**
  249. * Renders advice of the day
  250. *
  251. * @return string
  252. */
  253. public function renderAdviceOfTheDay() {
  254. $result = '';
  255. $fga = new FGA();
  256. $adviceLabel = __('Advice of the day') . ': ' . $fga->getAdviceOfTheDay();
  257. $result .= $this->messages->getStyledMessage($adviceLabel, 'info');
  258. return($result);
  259. }
  260. }