api.reportstreets.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * Streets report base class
  4. */
  5. class ReportStreets {
  6. /**
  7. * Contains cities as id=>name
  8. *
  9. * @var array
  10. */
  11. protected $cities = array();
  12. /**
  13. * Contains streets as id=>streetdata
  14. *
  15. * @var array
  16. */
  17. protected $streets = array();
  18. /**
  19. * Contains builds as id=>builddata
  20. *
  21. * @var array
  22. */
  23. protected $builds = array();
  24. /**
  25. * contains apt related shit
  26. *
  27. * @var array
  28. */
  29. protected $apts = array();
  30. /**
  31. * Payments abstraction layer placeholder
  32. *
  33. * @var object
  34. */
  35. protected $payments = '';
  36. /**
  37. * Total user counter
  38. *
  39. * @var int
  40. */
  41. protected $totalusercount = 0;
  42. /**
  43. * Contains payments search year
  44. *
  45. * @var int
  46. */
  47. protected $year = '';
  48. /**
  49. * Contains payments search month with lezding zero
  50. *
  51. * @var string
  52. */
  53. protected $month = '';
  54. /**
  55. * Contains all preprocessed assigns for some agents as fullstreet=>agentid
  56. *
  57. * @var array
  58. */
  59. protected $allAssigns = array();
  60. /**
  61. * Contains available agents as id=>name
  62. *
  63. * @var array
  64. */
  65. protected $agents = array();
  66. /**
  67. * Build passports object placeholder
  68. *
  69. * @var object
  70. */
  71. protected $buildPassport = '';
  72. /**
  73. * Contains build passports enabling flag
  74. *
  75. * @var bool
  76. */
  77. protected $passportsFlag = false;
  78. public function __construct() {
  79. $this->setDates();
  80. $this->initPayments();
  81. $this->loadCities();
  82. $this->loadStreets();
  83. $this->initBuildPassports();
  84. $this->loadBuilds();
  85. $this->loadApts();
  86. $this->countApts();
  87. $this->countBuilds();
  88. $this->loadAllAssigns();
  89. $this->loadAgents();
  90. }
  91. /**
  92. * Internal dates setter
  93. *
  94. * @return void
  95. */
  96. protected function setDates() {
  97. if (ubRouting::checkPost(array('showyear', 'showmonth'))) {
  98. $this->year = ubRouting::post('showyear', 'int');
  99. $this->month = ubRouting::post('showmonth', 'int');
  100. } else {
  101. $this->year = curyear();
  102. $this->month = date("m");
  103. }
  104. }
  105. /**
  106. * Inits builds passports instance if enabled for further usage
  107. *
  108. * @global object $ubillingConfig
  109. *
  110. * @return void
  111. */
  112. protected function initBuildPassports() {
  113. global $ubillingConfig;
  114. if ($ubillingConfig->getAlterParam('BUILD_EXTENDED')) {
  115. $this->passportsFlag = true;
  116. $this->buildPassport = new BuildPassport();
  117. }
  118. }
  119. /**
  120. * Inits payments abstraction layer
  121. *
  122. * @return void
  123. */
  124. protected function initPayments() {
  125. $this->payments = new NyanORM('payments');
  126. if (!empty($this->year) AND ! empty($this->month)) {
  127. $dateFilter = $this->year . '-' . $this->month . '-%';
  128. $this->payments->where('date', 'LIKE', $dateFilter);
  129. $this->payments->where('summ', '>', 0);
  130. }
  131. }
  132. /**
  133. * loads available cities from database into private data property
  134. *
  135. * @return void
  136. */
  137. protected function loadCities() {
  138. $query = "SELECT * from `city`";
  139. $all = simple_queryall($query);
  140. if (!empty($all)) {
  141. foreach ($all as $io => $each) {
  142. $this->cities[$each['id']] = $each['cityname'];
  143. }
  144. }
  145. }
  146. /**
  147. * loads available assigns from database into private prop
  148. *
  149. * @return void
  150. */
  151. protected function loadAllAssigns() {
  152. $assignsTmp = zb_AgentAssignGetAllData();
  153. if (!empty($assignsTmp)) {
  154. foreach ($assignsTmp as $io => $each) {
  155. $this->allAssigns[$each['streetname']] = $each['ahenid'];
  156. }
  157. }
  158. }
  159. /**
  160. * loads contragent data into protected prop
  161. *
  162. * @return void
  163. */
  164. protected function loadAgents() {
  165. $tmpArr = array();
  166. $tmpArr = zb_ContrAhentGetAllData();
  167. if (!empty($tmpArr)) {
  168. foreach ($tmpArr as $io => $each) {
  169. $this->agents[$each['id']] = $each['contrname'];
  170. }
  171. }
  172. }
  173. /**
  174. * loads available streets from database into private data property
  175. *
  176. * @return void
  177. */
  178. protected function loadStreets() {
  179. $query = "SELECT * from `street`";
  180. $all = simple_queryall($query);
  181. if (!empty($all)) {
  182. foreach ($all as $io => $each) {
  183. $this->streets[$each['id']]['streetname'] = $each['streetname'];
  184. $this->streets[$each['id']]['cityid'] = $each['cityid'];
  185. $this->streets[$each['id']]['buildcount'] = 0;
  186. $this->streets[$each['id']]['usercount'] = 0;
  187. $this->streets[$each['id']]['aptstotal'] = 0;
  188. $this->streets[$each['id']]['anthills'] = 0;
  189. $this->streets[$each['id']]['anthillusers'] = 0;
  190. }
  191. }
  192. }
  193. /**
  194. * loads available builds from database into private data property
  195. *
  196. * @return void
  197. */
  198. protected function loadBuilds() {
  199. $query = "SELECT * from `build`";
  200. $all = simple_queryall($query);
  201. if (!empty($all)) {
  202. foreach ($all as $io => $each) {
  203. $this->builds[$each['id']]['buildnum'] = $each['buildnum'];
  204. $this->builds[$each['id']]['streetid'] = $each['streetid'];
  205. $this->builds[$each['id']]['aptcount'] = 0;
  206. $aptsTotal = 0;
  207. $antHill = 0;
  208. if ($this->passportsFlag) {
  209. $eachPassport = $this->buildPassport->getPassportData($each['id']);
  210. if (@$eachPassport['anthill']) {
  211. $aptsTotal = $eachPassport['apts'];
  212. $antHill = 1;
  213. }
  214. }
  215. $this->builds[$each['id']]['aptstotal'] = $aptsTotal;
  216. $this->builds[$each['id']]['anthill'] = $antHill;
  217. $this->builds[$each['id']]['anthillusers'] = 0;
  218. }
  219. }
  220. }
  221. /**
  222. * loads available apts from database into private data property
  223. *
  224. * @return void
  225. */
  226. protected function loadApts() {
  227. $query = "SELECT * from `apt`";
  228. $all = simple_queryall($query);
  229. if (!empty($all)) {
  230. foreach ($all as $io => $each) {
  231. $this->apts[$each['id']]['apt'] = $each['apt'];
  232. $this->apts[$each['id']]['buildid'] = $each['buildid'];
  233. }
  234. }
  235. }
  236. /**
  237. * prepares builds data for render report
  238. *
  239. * @return void
  240. */
  241. protected function countApts() {
  242. if (!empty($this->builds)) {
  243. if (!empty($this->apts)) {
  244. foreach ($this->apts as $io => $eachapt) {
  245. if (isset($this->builds[$eachapt['buildid']])) {
  246. $this->builds[$eachapt['buildid']]['aptcount'] ++;
  247. if ($this->builds[$eachapt['buildid']]['anthill']) {
  248. $this->builds[$eachapt['buildid']]['anthillusers'] ++;
  249. }
  250. $this->totalusercount++;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. /**
  257. * prepares streets data for render report
  258. *
  259. * @return void
  260. */
  261. protected function countBuilds() {
  262. if (!empty($this->streets)) {
  263. if (!empty($this->builds)) {
  264. foreach ($this->builds as $io => $eachbuild) {
  265. if (isset($this->streets[$eachbuild['streetid']])) {
  266. $this->streets[$eachbuild['streetid']]['buildcount'] ++;
  267. $this->streets[$eachbuild['streetid']]['usercount'] = $this->streets[$eachbuild['streetid']]['usercount'] + $eachbuild['aptcount'];
  268. if ($eachbuild['anthill']) {
  269. $this->streets[$eachbuild['streetid']]['aptstotal'] += $eachbuild['aptstotal'];
  270. $this->streets[$eachbuild['streetid']]['anthillusers'] += $eachbuild['anthillusers'];
  271. $this->streets[$eachbuild['streetid']]['anthills'] ++;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. /**
  279. * returns colorized register level for street
  280. *
  281. * @param int $usercount Registered apts (users) count on the street
  282. * @param int $buildcount Builds count on the street
  283. *
  284. * @return string
  285. */
  286. protected function getLevel($usercount, $buildcount) {
  287. if (($usercount != 0) AND ( $buildcount != 0)) {
  288. $level = $usercount / $buildcount;
  289. } else {
  290. $level = 0;
  291. }
  292. $level = round($level, 2);
  293. $color = 'black';
  294. if ($level < 2) {
  295. $color = 'red';
  296. }
  297. if ($level >= 3) {
  298. $color = 'green';
  299. }
  300. $result = wf_tag('font', false, '', 'color="' . $color . '"') . $level . wf_tag('font', true);
  301. return ($result);
  302. }
  303. /**
  304. * renders report by prepeared data
  305. *
  306. * @return string
  307. */
  308. public function render() {
  309. $addrPayments = array(); //city + street => payments total
  310. $allPayments = $this->payments->getAll();
  311. $totalPaymentsSumm = 0;
  312. if (!empty($allPayments)) {
  313. $allUsers = zb_UserGetAllDataCache();
  314. foreach ($allPayments as $io => $each) {
  315. if (isset($allUsers[$each['login']])) {
  316. $userData = $allUsers[$each['login']];
  317. $userCity = $userData['cityname'];
  318. $userStreet = $userData['streetname'];
  319. $userAddr = $userCity . ' ' . $userStreet;
  320. if (isset($addrPayments[$userAddr])) {
  321. $addrPayments[$userAddr] += $each['summ'];
  322. } else {
  323. $addrPayments[$userAddr] = $each['summ'];
  324. }
  325. }
  326. }
  327. }
  328. if (!empty($this->streets)) {
  329. $cells = wf_TableCell(__('ID'));
  330. $cells .= wf_TableCell(__('City'));
  331. $cells .= wf_TableCell(__('Street'));
  332. $cells .= wf_TableCell(__('Contrahent name'));
  333. $cells .= wf_TableCell(__('Builds'));
  334. if ($this->passportsFlag) {
  335. $cells .= wf_TableCell(__('Anthill'));
  336. $cells .= wf_TableCell(__('Apartments'));
  337. }
  338. $cells .= wf_TableCell(__('Users'));
  339. if ($this->passportsFlag) {
  340. $cells .= wf_TableCell(wf_img_sized('skins/ymaps/build.png', __('Users') . ': ' . __('Apartment house'), 12));
  341. $cells .= wf_TableCell(wf_img_sized('skins/ymaps/coverage.png', __('Coverage'), 12));
  342. }
  343. $cells .= wf_TableCell(__('Visual'));
  344. $cells .= wf_TableCell(__('Level'));
  345. $cells .= wf_TableCell(__('Money'));
  346. $rows = wf_TableRow($cells, 'row1');
  347. foreach ($this->streets as $streetid => $each) {
  348. $streetAgentId = 0;
  349. $addrString = @$this->cities[$each['cityid']] . ' ' . $each['streetname'];
  350. if (!empty($this->allAssigns)) {
  351. foreach ($this->allAssigns as $streetAssign => $agentId) {
  352. if (ispos($addrString, $streetAssign)) {
  353. $streetAgentId = $agentId; //gotcha motherfucker!
  354. }
  355. }
  356. }
  357. if ($streetAgentId != 0) {
  358. $streetAgentName = $this->agents[$streetAgentId];
  359. $cellsClass = 'todaysig';
  360. } else {
  361. $streetAgentName = '';
  362. $cellsClass = 'row3';
  363. }
  364. $cells = wf_TableCell($streetid, '', $cellsClass);
  365. $cells .= wf_TableCell(@$this->cities[$each['cityid']]);
  366. $cells .= wf_TableCell($each['streetname']);
  367. $cells .= wf_TableCell($streetAgentName);
  368. $cells .= wf_TableCell($each['buildcount']);
  369. if ($this->passportsFlag) {
  370. $cells .= wf_TableCell($each['anthills']);
  371. $cells .= wf_TableCell($each['aptstotal']);
  372. }
  373. $usersCount = $each['usercount'];
  374. $cells .= wf_TableCell($usersCount);
  375. if ($this->passportsFlag) {
  376. $anthillUsers = $each['anthillusers'];
  377. $cells .= wf_TableCell($anthillUsers);
  378. if ($anthillUsers > 0) {
  379. $coveragePercent = zb_PercentValue($each['aptstotal'], $anthillUsers) . '%';
  380. } else {
  381. $coveragePercent = '';
  382. }
  383. $cells .= wf_TableCell($coveragePercent);
  384. }
  385. $cells .= wf_TableCell(web_bar($each['usercount'], $this->totalusercount), '15%', '', 'sorttable_customkey="' . $each['usercount'] . '"');
  386. $cells .= wf_TableCell($this->getLevel($each['usercount'], $each['buildcount']));
  387. $paymentsSumm = (isset($addrPayments[$addrString])) ? $addrPayments[$addrString] : '0';
  388. $totalPaymentsSumm += $paymentsSumm;
  389. $cells .= wf_TableCell($paymentsSumm);
  390. $rows .= wf_TableRow($cells, 'row5');
  391. }
  392. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  393. $result .= __('Users') . ': ' . $this->totalusercount;
  394. $result .= wf_tag('br');
  395. $result .= __('Payments') . ': ' . $totalPaymentsSumm;
  396. } else {
  397. $messages = new UbillingMessageHelper();
  398. $result = $messages->getStyledMessage(__('Nothing found'), 'warning');
  399. }
  400. return ($result);
  401. }
  402. /**
  403. * Renders payments date selection form
  404. *
  405. * @return string
  406. */
  407. public function renderDateForm() {
  408. $result = '';
  409. $inputs = wf_YearSelectorPreset('showyear', __('Year'), false, $this->year) . ' ';
  410. $inputs .= wf_MonthSelector('showmonth', __('Month'), $this->month, false) . ' ';
  411. $inputs .= wf_Submit(__('Search') . ' ' . __('payments'));
  412. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  413. return($result);
  414. }
  415. }