widget_finance.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. class widget_finance extends TaskbarWidget {
  3. /**
  4. * Caching object placeholder
  5. *
  6. * @var object
  7. */
  8. protected $cache = '';
  9. /**
  10. * Caching timeout in seconds
  11. *
  12. * @var int
  13. */
  14. protected $timeout = 3600; //hour
  15. /**
  16. * Initalizes system cache object for further usage
  17. *
  18. * @return void
  19. */
  20. protected function initCache() {
  21. $this->cache = new UbillingCache();
  22. }
  23. /**
  24. * Returns array of payments performed in current month
  25. *
  26. * @return array
  27. */
  28. protected function getMonthPayments() {
  29. $result = array();
  30. $curmonth = curmonth();
  31. $query = "SELECT * from `payments` WHERE `date` LIKE '" . $curmonth . "-%' AND `summ`>0";
  32. $all = simple_queryall($query);
  33. if (!empty($all)) {
  34. foreach ($all as $io => $each) {
  35. $result[$each['id']] = $each;
  36. }
  37. }
  38. return ($result);
  39. }
  40. /**
  41. * Direcly renders chart by current month payments
  42. *
  43. * @return string
  44. */
  45. public function renderChart() {
  46. $result = '';
  47. $allPayments = $this->getMonthPayments();
  48. $chartData = array();
  49. $tmpArr = array();
  50. if (!empty($allPayments)) {
  51. $chartData[] = array(__('Day'), __('Cash'));
  52. foreach ($allPayments as $io => $each) {
  53. $paymentDate = strtotime($each['date']);
  54. $paymentDate = date("d", $paymentDate);
  55. if (isset($tmpArr[$paymentDate])) {
  56. $tmpArr[$paymentDate]+=$each['summ'];
  57. } else {
  58. $tmpArr[$paymentDate] = $each['summ'];
  59. }
  60. }
  61. if (!empty($tmpArr)) {
  62. foreach ($tmpArr as $day => $cash) {
  63. $chartData[] = array($day, $cash);
  64. }
  65. }
  66. $result = $this->widgetContainer(wf_gchartsLine($chartData, __('Month payments'), '500px', '256px', ''));
  67. }
  68. return ($result);
  69. }
  70. /**
  71. * Renders widget code
  72. *
  73. * @return string
  74. */
  75. public function render() {
  76. $result = '';
  77. $this->initCache();
  78. $obj = $this;
  79. $result = $this->cache->getCallback('WIDGET_FINANCE', function() use ($obj) {
  80. return ($obj->renderChart());
  81. }, $this->timeout);
  82. return ($result);
  83. }
  84. }
  85. ?>