api.metabolism.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * User signup and payments extended stats
  4. */
  5. class Metabolism {
  6. /**
  7. * Payments database abstraction layer placeholder
  8. *
  9. * @var object
  10. */
  11. protected $payments = '';
  12. /**
  13. * Signups database abstraction layer placeholder
  14. *
  15. * @var object
  16. */
  17. protected $signups = '';
  18. /**
  19. * System message helper placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. /**
  25. * Contains default year to display
  26. *
  27. * @var int
  28. */
  29. protected $year = '';
  30. /**
  31. * Contains default month to dislay with leading zero
  32. *
  33. * @var string
  34. */
  35. protected $month = '';
  36. /**
  37. * Routes etc.
  38. */
  39. const URL_ME = '?module=metabolism';
  40. const URL_BACK = '?module=report_finance';
  41. /**
  42. * Creates new metabolism instance
  43. */
  44. public function __construct() {
  45. $this->initMessages();
  46. $this->setDate();
  47. $this->initPayments();
  48. $this->initSignups();
  49. }
  50. /**
  51. * Catches year/month selector data and sets internal props for further usage
  52. *
  53. * @return void
  54. */
  55. protected function setDate() {
  56. if (ubRouting::checkPost(array('showyear', 'showmonth'))) {
  57. $this->year = ubRouting::post('showyear', 'int');
  58. $this->month = ubRouting::post('showmonth', 'int');
  59. } else {
  60. $this->year = curyear();
  61. $this->month = date("m");
  62. }
  63. }
  64. /**
  65. * Inits payments database layer instance
  66. *
  67. * @return void
  68. */
  69. protected function initPayments() {
  70. $this->payments = new NyanORM('payments');
  71. }
  72. /**
  73. * Inits signups database layer instance
  74. *
  75. * @return void
  76. */
  77. protected function initSignups() {
  78. $this->signups = new NyanORM('userreg');
  79. }
  80. /**
  81. * Inits system message helper instance
  82. *
  83. * @return void
  84. */
  85. protected function initMessages() {
  86. $this->messages = new UbillingMessageHelper();
  87. }
  88. /**
  89. * Renders default module controls panel
  90. *
  91. * @return string
  92. */
  93. public function renderPanel() {
  94. $result = '';
  95. $result .= wf_BackLink(self::URL_BACK);
  96. $result .= wf_Link(self::URL_ME, wf_img_sized('skins/icon_dollar.gif', '', '16', '16') . ' ' . __('Payments'), false, 'ubButton') . ' ';
  97. $result .= wf_Link(self::URL_ME . '&signups=true', web_icon_charts() . ' ' . __('Signups'), false, 'ubButton') . ' ';
  98. $result .= wf_CleanDiv();
  99. $result .= wf_tag('br');
  100. $result .= $this->renderDateForm();
  101. return($result);
  102. }
  103. /**
  104. * Returns year-month selection form
  105. *
  106. * @return string
  107. */
  108. protected function renderDateForm() {
  109. $result = '';
  110. $inputs = wf_YearSelectorPreset('showyear', __('Year'), false, $this->year) . ' ';
  111. $inputs .= wf_MonthSelector('showmonth', __('Month'), $this->month, false, true) . ' ';
  112. $inputs .= wf_Submit(__('Show'));
  113. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  114. return($result);
  115. }
  116. /**
  117. * Returns default chart options
  118. *
  119. * @return string
  120. */
  121. protected function getChartOptions() {
  122. $result = "'focusTarget': 'category',
  123. 'hAxis': {
  124. 'color': 'none',
  125. 'baselineColor': 'none',
  126. },
  127. 'vAxis': {
  128. 'color': 'none',
  129. 'baselineColor': 'none',
  130. },
  131. 'curveType': 'function',
  132. 'pointSize': 5,
  133. 'crosshair': {
  134. trigger: 'none'
  135. },
  136. ";
  137. return($result);
  138. }
  139. /**
  140. * Renders payments metabolism report
  141. *
  142. * @return string
  143. */
  144. public function renderPayments() {
  145. $result = '';
  146. $tmpArr = array();
  147. if (!empty($this->year) AND ! empty($this->month)) {
  148. if ($this->month == '1488') {
  149. //all time
  150. $dateFilter = $this->year . '-%';
  151. } else {
  152. //normal case
  153. $dateFilter = $this->year . '-' . $this->month . '-%';
  154. }
  155. //setting db props
  156. $this->payments->orderBy('date', 'asc');
  157. $this->payments->where('summ', '>', '0');
  158. $this->payments->where('date', 'LIKE', $dateFilter);
  159. $allPayments = $this->payments->getAll();
  160. if (!empty($allPayments)) {
  161. $chartsData[] = array(__('Date'), __('Count'), __('Money'));
  162. foreach ($allPayments as $io => $each) {
  163. $timeStamp = strtotime($each['date']); //need to be transformed to Y-m-d
  164. $date = date("Y-m-d", $timeStamp);
  165. if (isset($tmpArr[$date])) {
  166. $tmpArr[$date]['count'] ++;
  167. $tmpArr[$date]['summ'] += $each['summ'];
  168. } else {
  169. $tmpArr[$date]['count'] = 1;
  170. $tmpArr[$date]['summ'] = $each['summ'];
  171. }
  172. }
  173. if (!empty($tmpArr)) {
  174. foreach ($tmpArr as $date => $each) {
  175. $chartsData[] = array($date, $each['count'], $each['summ']);
  176. }
  177. $result .= wf_gchartsLine($chartsData, __('Cash'), '100%', '400px;', $this->getChartOptions());
  178. }
  179. } else {
  180. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  181. }
  182. } else {
  183. $result .= $this->messages->getStyledMessage(__('Something went wrong'), 'error');
  184. }
  185. return($result);
  186. }
  187. /**
  188. * Renders payments metabolism report
  189. *
  190. * @return string
  191. */
  192. public function renderSignups() {
  193. $result = '';
  194. $tmpArr = array();
  195. if (!empty($this->year) AND ! empty($this->month)) {
  196. if ($this->month == '1488') {
  197. //all time
  198. $dateFilter = $this->year . '-%';
  199. } else {
  200. //normal case
  201. $dateFilter = $this->year . '-' . $this->month . '-%';
  202. }
  203. //setting db props
  204. $this->signups->orderBy('date', 'asc');
  205. $this->signups->where('date', 'LIKE', $dateFilter);
  206. $allSignups = $this->signups->getAll();
  207. if (!empty($allSignups)) {
  208. //prefill tmparr with zero day values
  209. if ($this->month != '1488') {
  210. $showMonth = strtotime($this->year . '-' . $this->month);
  211. $maxDay = date("t", $showMonth);
  212. for ($zeroDay = 1; $zeroDay <= $maxDay; $zeroDay++) {
  213. if ($zeroDay < 10) {
  214. $tmpArr[$this->year . '-' . $this->month . '-0' . $zeroDay]['count'] = 0;
  215. } else {
  216. $tmpArr[$this->year . '-' . $this->month . '-' . $zeroDay]['count'] = 0;
  217. }
  218. }
  219. }
  220. $chartsData[] = array(__('Date'), __('Count'));
  221. foreach ($allSignups as $io => $each) {
  222. $timeStamp = strtotime($each['date']); //need to be transformed to Y-m-d
  223. $date = date("Y-m-d", $timeStamp);
  224. if (isset($tmpArr[$date])) {
  225. $tmpArr[$date]['count'] ++;
  226. } else {
  227. $tmpArr[$date]['count'] = 1;
  228. }
  229. }
  230. if (!empty($tmpArr)) {
  231. foreach ($tmpArr as $date => $each) {
  232. $chartsData[] = array($date, $each['count']);
  233. }
  234. $result .= wf_gchartsLine($chartsData, __('Signups'), '100%', '400px;', $this->getChartOptions());
  235. }
  236. } else {
  237. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  238. }
  239. } else {
  240. $result .= $this->messages->getStyledMessage(__('Something went wrong'), 'error');
  241. }
  242. return($result);
  243. }
  244. }