| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules\Reporting; |
| 4 |
|
| 5 |
trait ReportingHelperTrait |
| 6 |
{ |
| 7 |
protected static $daily = 'P1D'; |
| 8 |
protected static $weekly = 'P1W'; |
| 9 |
protected static $monthly = 'P1M'; |
| 10 |
|
| 11 |
protected function db() { |
| 12 |
return \FluentSupport\App\App::getInstance('db'); |
| 13 |
} |
| 14 |
|
| 15 |
protected function makeFromDate($from) |
| 16 |
{ |
| 17 |
$from = $from ?: '-30 days'; |
| 18 |
|
| 19 |
return new \DateTime($from); |
| 20 |
} |
| 21 |
|
| 22 |
protected function makeToDate($to = false) |
| 23 |
{ |
| 24 |
$to = $to ?: '+1 days'; |
| 25 |
|
| 26 |
return new \DateTime($to); |
| 27 |
} |
| 28 |
|
| 29 |
protected function makeDatePeriod($from, $to, $interval = null) |
| 30 |
{ |
| 31 |
$interval = $interval ?: static::$daily; |
| 32 |
|
| 33 |
return new \DatePeriod($from, new \DateInterval($interval), $to); |
| 34 |
} |
| 35 |
|
| 36 |
protected function getFrequency($from, $to) |
| 37 |
{ |
| 38 |
$numDays = $to->diff($from)->format("%a"); |
| 39 |
|
| 40 |
if ($numDays > 62 && $numDays <= 181) { |
| 41 |
return static::$weekly; |
| 42 |
} else if ($numDays > 181) { |
| 43 |
return static::$monthly; |
| 44 |
} |
| 45 |
|
| 46 |
return static::$daily; |
| 47 |
} |
| 48 |
|
| 49 |
protected function prepareSelect($frequency, $dateField = 'created_at') |
| 50 |
{ |
| 51 |
$select = [ |
| 52 |
$this->db()->raw('COUNT(id) AS count'), |
| 53 |
$this->db()->raw('DATE('.$dateField.') AS date') |
| 54 |
]; |
| 55 |
|
| 56 |
if ($frequency == static::$weekly) { |
| 57 |
$select[] = $this->db()->raw('WEEK(created_at) week'); |
| 58 |
} else if ($frequency == static::$monthly) { |
| 59 |
$select[] = $this->db()->raw('MONTH(created_at) month'); |
| 60 |
} |
| 61 |
|
| 62 |
return $select; |
| 63 |
} |
| 64 |
|
| 65 |
protected function getGroupAndOrder($frequency) |
| 66 |
{ |
| 67 |
$orderBy = $groupBy = 'date'; |
| 68 |
|
| 69 |
if ($frequency == static::$weekly) { |
| 70 |
$orderBy = $groupBy = 'week'; |
| 71 |
} else if ($frequency == static::$monthly) { |
| 72 |
$orderBy = $groupBy = 'month'; |
| 73 |
} |
| 74 |
|
| 75 |
return [$groupBy, $orderBy]; |
| 76 |
} |
| 77 |
|
| 78 |
protected function getDateRangeArray($period) |
| 79 |
{ |
| 80 |
$range = []; |
| 81 |
|
| 82 |
$formatter = 'basicFormatter'; |
| 83 |
|
| 84 |
if ($this->isMonthly($period)) { |
| 85 |
$formatter = 'monYearFormatter'; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ($period as $date) { |
| 89 |
$date = $this->{$formatter}($date); |
| 90 |
$range[$date] = 0; |
| 91 |
} |
| 92 |
|
| 93 |
return $range; |
| 94 |
} |
| 95 |
|
| 96 |
protected function getResult($period, $items) |
| 97 |
{ |
| 98 |
$range = $this->getDateRangeArray($period); |
| 99 |
|
| 100 |
$formatter = 'basicFormatter'; |
| 101 |
|
| 102 |
if ($this->isMonthly($period)) { |
| 103 |
$formatter = 'monYearFormatter'; |
| 104 |
} |
| 105 |
|
| 106 |
foreach ($items as $item) { |
| 107 |
$date = $this->{$formatter}($item->date); |
| 108 |
$range[$date] = (int) $item->count; |
| 109 |
} |
| 110 |
|
| 111 |
return $range; |
| 112 |
} |
| 113 |
|
| 114 |
protected function isMonthly($period) |
| 115 |
{ |
| 116 |
return !!$period->getDateInterval()->m; |
| 117 |
} |
| 118 |
|
| 119 |
protected function basicFormatter($date) |
| 120 |
{ |
| 121 |
if (is_string($date)) { |
| 122 |
$date = new \DateTime($date); |
| 123 |
} |
| 124 |
|
| 125 |
return $date->format('Y-m-d'); |
| 126 |
} |
| 127 |
|
| 128 |
protected function monYearFormatter($date) |
| 129 |
{ |
| 130 |
if (is_string($date)) { |
| 131 |
$date = new \DateTime($date); |
| 132 |
} |
| 133 |
|
| 134 |
return $date->format('M Y'); |
| 135 |
} |
| 136 |
} |
| 137 |
|