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