| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Report\Concerns; |
| 4 |
|
| 5 |
use FluentCart\App\Services\DateTime\DateTime; |
| 6 |
use FluentCart\App\Services\Report\ReportService; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
|
| 9 |
trait HasRange |
| 10 |
{ |
| 11 |
protected string $rangeKey = 'created_at'; |
| 12 |
protected ?\DateTimeZone $originalTimeZone = null; |
| 13 |
|
| 14 |
public function getRangeKey(): string |
| 15 |
{ |
| 16 |
return $this->rangeKey; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @param string $rangeKey |
| 21 |
* @return ReportService |
| 22 |
*/ |
| 23 |
public function setRangeKey(string $rangeKey) |
| 24 |
{ |
| 25 |
$this->rangeKey = $rangeKey; |
| 26 |
return $this; |
| 27 |
} |
| 28 |
|
| 29 |
public function getRange(): array |
| 30 |
{ |
| 31 |
return [ |
| 32 |
'start_date' => $this->startDate, |
| 33 |
'end_date' => $this->endDate, |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param $startDate |
| 39 |
* @param $endDate |
| 40 |
*/ |
| 41 |
public function setRange($startDate, $endDate, bool $handleDayClosingTime = true) |
| 42 |
{ |
| 43 |
if ($handleDayClosingTime) { |
| 44 |
$endDate = DateTime::anyTimeToGmt($endDate)->endOfDay(); |
| 45 |
} |
| 46 |
return $this->setStartDate($startDate)->setEndDate($endDate); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param $year |
| 51 |
*/ |
| 52 |
public function setYearlyRange($selectedYear) |
| 53 |
{ |
| 54 |
|
| 55 |
$startDate = DateTime::parse("{$selectedYear}-01-01")->startOfYear(); |
| 56 |
$endDate = DateTime::parse("{$selectedYear}-01-01")->endOfYear(); |
| 57 |
|
| 58 |
return $this->setStartDate($startDate)->setEndDate($endDate); |
| 59 |
} |
| 60 |
|
| 61 |
protected ?DateTime $startDate = null; |
| 62 |
|
| 63 |
/** |
| 64 |
* @return mixed |
| 65 |
*/ |
| 66 |
public function getStartDate(): ?DateTime |
| 67 |
{ |
| 68 |
return $this->startDate; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @param mixed $startDate |
| 73 |
*/ |
| 74 |
public function setStartDate($startDate) |
| 75 |
{ |
| 76 |
$this->startDate = $this->validate($startDate); |
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
protected ?DateTime $endDate = null; |
| 81 |
|
| 82 |
/** |
| 83 |
* @return mixed |
| 84 |
*/ |
| 85 |
public function getEndDate(): ?DateTime |
| 86 |
{ |
| 87 |
return $this->startDate; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @param null $endDate |
| 92 |
*/ |
| 93 |
public function setEndDate($endDate) |
| 94 |
{ |
| 95 |
$this->endDate = $this->validate($endDate); |
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
protected function validate($time) |
| 100 |
{ |
| 101 |
if ($this->originalTimeZone === null) { |
| 102 |
$this->originalTimeZone = DateTime::extractTimezone($time); |
| 103 |
} |
| 104 |
return DateTime::anyTimeToGmt($time); |
| 105 |
} |
| 106 |
|
| 107 |
protected function getFilters(): array |
| 108 |
{ |
| 109 |
|
| 110 |
$filters = $this->filters; |
| 111 |
|
| 112 |
$filterRangeValues = Arr::get($filters, $this->getRangeKey() . '.value'); |
| 113 |
|
| 114 |
if (!is_array($filterRangeValues)) { |
| 115 |
$filterRangeValues = []; |
| 116 |
} |
| 117 |
|
| 118 |
if (!empty($this->startDate) && !empty($this->endDate)) { |
| 119 |
$filterRangeValues[0] = $this->startDate; |
| 120 |
$filterRangeValues[1] = $this->endDate; |
| 121 |
} |
| 122 |
|
| 123 |
if (!empty($this->startDate) && !empty($this->endDate)) { |
| 124 |
$filters[$this->getRangeKey()] = |
| 125 |
[ |
| 126 |
"column" => $this->getRangeKey(), |
| 127 |
"operator" => "between", |
| 128 |
"value" => $filterRangeValues |
| 129 |
]; |
| 130 |
|
| 131 |
} else if (!empty($this->startDate)) { |
| 132 |
$filters[] = [ |
| 133 |
"column" => $this->getRangeKey(), |
| 134 |
"operator" => ">=", |
| 135 |
"value" => $this->startDate |
| 136 |
]; |
| 137 |
} else if (!empty($this->endDate)) { |
| 138 |
$filters[] = [ |
| 139 |
"column" => $this->getRangeKey(), |
| 140 |
"operator" => "<=", |
| 141 |
"value" => $this->endDate |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
return $filters; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
public function getRangeDiffInMonth(): int |
| 150 |
{ |
| 151 |
if (!empty($this->startDate) && !empty($this->endDate)) { |
| 152 |
(new DateTime($this->startDate))->diffInMonths( |
| 153 |
new DateTime($this->endDate) |
| 154 |
); |
| 155 |
} |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
public function getRangeDiffInYear(): int |
| 160 |
{ |
| 161 |
if (!empty($this->startDate) && !empty($this->endDate)) { |
| 162 |
(new DateTime($this->startDate))->diffInYears( |
| 163 |
new DateTime($this->endDate) |
| 164 |
); |
| 165 |
} |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
public function test() |
| 170 |
{ |
| 171 |
return 6; |
| 172 |
} |
| 173 |
|
| 174 |
} |