| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Modules\Reporting; |
| 4 |
|
| 5 |
/** |
| 6 |
* ReportingHelperTrait is act as helper of report |
| 7 |
* This will help to get frequency, several date format, get advanced or backdated date and other |
| 8 |
* @package FluentSupport\App\Modules\Reporting |
| 9 |
* |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
trait ReportingHelperTrait |
| 14 |
{ |
| 15 |
protected static $daily = 'P1D'; |
| 16 |
protected static $weekly = 'P1W'; |
| 17 |
protected static $monthly = 'P1M'; |
| 18 |
|
| 19 |
protected function db() { |
| 20 |
return \FluentSupport\App\App::getInstance('db'); |
| 21 |
} |
| 22 |
|
| 23 |
protected function makeFromDate($from) |
| 24 |
{ |
| 25 |
$from = $from ?: '-30 days'; |
| 26 |
|
| 27 |
return new \DateTime($from); |
| 28 |
} |
| 29 |
|
| 30 |
protected function makeToDate($to = false) |
| 31 |
{ |
| 32 |
$to = $to ?: '+1 days'; |
| 33 |
|
| 34 |
return new \DateTime($to); |
| 35 |
} |
| 36 |
|
| 37 |
protected function makeDatePeriod($from, $to, $interval = null) |
| 38 |
{ |
| 39 |
$interval = $interval ?: static::$daily; |
| 40 |
|
| 41 |
return new \DatePeriod($from, new \DateInterval($interval), $to); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* getFrequency method will return the frequency |
| 47 |
* this function get date from and date to as parameter and find the frequency and return |
| 48 |
* @param $from |
| 49 |
* @param $to |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
protected function getFrequency($from, $to) |
| 53 |
{ |
| 54 |
$numDays = $to->diff($from)->format("%a"); |
| 55 |
//If number of days in the range is greater than 2 month and less than or equal to 3 months |
| 56 |
if ($numDays > 62 && $numDays <= 181) { |
| 57 |
return static::$weekly; |
| 58 |
} else if ($numDays > 181) { |
| 59 |
//Greater than 3 months |
| 60 |
return static::$monthly; |
| 61 |
} |
| 62 |
|
| 63 |
return static::$daily; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* prepareSelect method will prepare the field to select |
| 68 |
* @param $frequency |
| 69 |
* @param string $dateField |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
protected function prepareSelect($frequency, $dateField = 'created_at') |
| 73 |
{ |
| 74 |
$select = [ |
| 75 |
$this->db()->raw('COUNT(id) AS count'), |
| 76 |
$this->db()->raw('DATE('.$dateField.') AS date') |
| 77 |
]; |
| 78 |
|
| 79 |
if ($frequency == static::$weekly) { |
| 80 |
$select[] = $this->db()->raw('WEEK(created_at) week'); |
| 81 |
} else if ($frequency == static::$monthly) { |
| 82 |
$select[] = $this->db()->raw('MONTH(created_at) month'); |
| 83 |
} |
| 84 |
|
| 85 |
return $select; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* getGroupAndOrder method will return order by and group by value based on frequency |
| 90 |
* @param $frequency |
| 91 |
* @return string[] |
| 92 |
*/ |
| 93 |
protected function getGroupAndOrder($frequency) |
| 94 |
{ |
| 95 |
$orderBy = $groupBy = 'date'; |
| 96 |
|
| 97 |
if ($frequency == static::$weekly) { |
| 98 |
$orderBy = $groupBy = 'week'; |
| 99 |
} else if ($frequency == static::$monthly) { |
| 100 |
$orderBy = $groupBy = 'month'; |
| 101 |
} |
| 102 |
|
| 103 |
return [$groupBy, $orderBy]; |
| 104 |
} |
| 105 |
|
| 106 |
protected function getDateRangeArray($period) |
| 107 |
{ |
| 108 |
$range = []; |
| 109 |
|
| 110 |
$formatter = 'basicFormatter'; |
| 111 |
|
| 112 |
if ($this->isMonthly($period)) { |
| 113 |
$formatter = 'monYearFormatter'; |
| 114 |
} |
| 115 |
|
| 116 |
foreach ($period as $date) { |
| 117 |
$date = $this->{$formatter}($date); |
| 118 |
$range[$date] = 0; |
| 119 |
} |
| 120 |
|
| 121 |
return $range; |
| 122 |
} |
| 123 |
|
| 124 |
protected function getResult($period, $items) |
| 125 |
{ |
| 126 |
$range = $this->getDateRangeArray($period); |
| 127 |
|
| 128 |
$formatter = 'basicFormatter'; |
| 129 |
|
| 130 |
if ($this->isMonthly($period)) { |
| 131 |
$formatter = 'monYearFormatter'; |
| 132 |
} |
| 133 |
|
| 134 |
foreach ($items as $item) { |
| 135 |
$date = $this->{$formatter}($item->date); |
| 136 |
$range[$date] = (int) $item->count; |
| 137 |
} |
| 138 |
|
| 139 |
return $range; |
| 140 |
} |
| 141 |
|
| 142 |
protected function isMonthly($period) |
| 143 |
{ |
| 144 |
return !!$period->getDateInterval()->m; |
| 145 |
} |
| 146 |
|
| 147 |
protected function basicFormatter($date) |
| 148 |
{ |
| 149 |
if (is_string($date)) { |
| 150 |
$date = new \DateTime($date); |
| 151 |
} |
| 152 |
|
| 153 |
return $date->format('Y-m-d'); |
| 154 |
} |
| 155 |
|
| 156 |
protected function monYearFormatter($date) |
| 157 |
{ |
| 158 |
if (is_string($date)) { |
| 159 |
$date = new \DateTime($date); |
| 160 |
} |
| 161 |
|
| 162 |
return $date->format('M Y'); |
| 163 |
} |
| 164 |
} |
| 165 |
|