PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / trunk
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution vtrunk
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / app / Services / ReportingHelperTrait.php

ReportingHelperTrait.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution trunk, at app/Services/ReportingHelperTrait.php

136 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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