akaunting/app/Traits/DateTime.php

203 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Traits;
2021-01-23 12:16:58 +03:00
use App\Traits\SearchString;
2023-02-16 13:08:02 +03:00
use App\Utilities\Date;
2021-02-08 12:07:22 +03:00
use Carbon\CarbonPeriod;
2017-09-14 22:21:00 +03:00
trait DateTime
{
2021-01-23 12:16:58 +03:00
use SearchString;
2017-09-14 22:21:00 +03:00
/*
* Get the date format based on company settings.
* getDateFormat method is used by Eloquent
*
* @return string
*/
public function getCompanyDateFormat()
{
2019-11-16 10:21:14 +03:00
$default = 'd M Y';
// Make sure it's installed
2022-06-13 10:01:46 +03:00
if (! config('app.installed') && ! env_is_testing()) {
2019-11-16 10:21:14 +03:00
return $default;
}
// Make user is logged in
if (!user()) {
return $default;
}
2017-09-14 22:21:00 +03:00
$chars = ['dash' => '-', 'slash' => '/', 'dot' => '.', 'comma' => ',', 'space' => ' '];
$date_format = (setting('localisation.date_format', $default)) ?? $default;
$char = (setting('localisation.date_separator', 'space')) ?? 'space';
$date_separator = $chars[$char];
2017-09-14 22:21:00 +03:00
return str_replace(' ', $date_separator, $date_format);
}
2017-11-07 05:11:03 +03:00
public function scopeMonthsOfYear($query, $field)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
$now = Date::now();
2021-01-23 12:16:58 +03:00
$year = $this->getSearchStringValue('year', $now->year);
2017-09-14 22:21:00 +03:00
2021-01-23 12:16:58 +03:00
$financial_start = $this->getFinancialStart($year);
2018-12-14 20:03:18 +05:30
2019-11-16 10:21:14 +03:00
// Check if FS has been customized
if ($now->startOfYear()->format('Y-m-d') === $financial_start->format('Y-m-d')) {
$start = Date::parse($year . '-01-01')->startOfDay()->format('Y-m-d H:i:s');
$end = Date::parse($year . '-12-31')->endOfDay()->format('Y-m-d H:i:s');
} else {
$start = $financial_start->startOfDay()->format('Y-m-d H:i:s');
$end = $financial_start->addYear(1)->subDays(1)->endOfDay()->format('Y-m-d H:i:s');
2018-12-14 20:03:18 +05:30
}
2017-09-14 22:21:00 +03:00
return $query->whereBetween($field, [$start, $end]);
2017-09-14 22:21:00 +03:00
}
public function getTimezones()
{
// The list of available timezone groups to use.
2023-02-16 13:08:02 +03:00
$use_zones = ['Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'];
2017-09-14 22:21:00 +03:00
// Get the list of time zones from the server.
$zones = \DateTimeZone::listIdentifiers();
2023-02-16 13:08:02 +03:00
return collect($zones)
->mapWithKeys(function ($timezone) use ($use_zones) {
if (! in_array(str($timezone)->before('/'), $use_zones)) {
return [];
}
return [$timezone => str($timezone)->after('/')->value()];
})
->groupBy(function ($item, $key) {
return str($key)->before('/')->value();
}, preserveKeys: true)
->toArray();
2017-09-14 22:21:00 +03:00
}
2018-12-31 14:41:17 +03:00
2021-01-23 12:16:58 +03:00
public function getFinancialStart($year = null)
2018-12-31 14:41:17 +03:00
{
2019-11-16 10:21:14 +03:00
$now = Date::now();
$start = Date::now()->startOfYear();
$setting = explode('-', setting('localisation.financial_start'));
2018-12-31 14:41:17 +03:00
2019-11-16 10:21:14 +03:00
$day = !empty($setting[0]) ? $setting[0] : $start->day;
$month = !empty($setting[1]) ? $setting[1] : $start->month;
2021-01-23 12:16:58 +03:00
$year = $year ?? $this->getSearchStringValue('year', $now->year);
2018-12-31 14:41:17 +03:00
2019-11-16 10:21:14 +03:00
$financial_start = Date::create($year, $month, $day);
2021-02-12 23:05:50 +03:00
if ((setting('localisation.financial_denote') == 'ends') && ($financial_start->dayOfYear != 1)) {
2019-11-16 10:21:14 +03:00
$financial_start->subYear();
}
return $financial_start;
}
public function getFinancialYear($year = null)
{
$start = $this->getFinancialStart($year);
return CarbonPeriod::create($start, $start->copy()->addYear()->subDay()->endOfDay());
}
2021-02-08 12:08:24 +03:00
public function getFinancialQuarters($year = null)
2021-02-08 12:07:22 +03:00
{
$quarters = [];
$start = $this->getFinancialStart($year);
for ($i = 0; $i < 4; $i++) {
$quarters[] = CarbonPeriod::create($start->copy()->addQuarters($i), $start->copy()->addQuarters($i + 1)->subDay()->endOfDay());
2021-02-08 12:07:22 +03:00
}
return $quarters;
}
public function getDatePickerShortcuts()
{
$today = new Date();
$financial_year = $this->getFinancialYear();
$financial_quarters = $this->getFinancialQuarters();
foreach ($financial_quarters as $quarter) {
if ($today->lessThan($quarter->getStartDate()) || $today->greaterThan($quarter->getEndDate())) {
$previous_quarter = $quarter;
continue;
}
$this_quarter = $quarter;
break;
}
2023-02-22 10:20:41 +03:00
if (! isset($this_quarter)) {
$this_quarter = $financial_quarters[0];
}
$date_picker_shortcuts = [
trans('reports.this_year') => [
'start' => $financial_year->getStartDate()->format('Y-m-d'),
'end' => $financial_year->getEndDate()->format('Y-m-d'),
],
trans('reports.previous_year') => [
'start' => $financial_year->getStartDate()->copy()->subYear()->format('Y-m-d'),
'end' => $financial_year->getEndDate()->copy()->subYear()->format('Y-m-d'),
],
trans('reports.this_quarter') => [
'start' => $this_quarter->getStartDate()->format('Y-m-d'),
'end' => $this_quarter->getEndDate()->format('Y-m-d'),
],
trans('reports.last_12_months') => [
'start' => $today->copy()->subYear()->startOfDay()->format('Y-m-d'),
'end' => $today->copy()->subDay()->endOfDay()->format('Y-m-d'),
],
];
2023-02-22 10:20:41 +03:00
if (isset($previous_quarter)) {
$date_picker_shortcuts[trans('reports.previous_quarter')] = [
'start' => $previous_quarter->getStartDate()->format('Y-m-d'),
'end' => $previous_quarter->getEndDate()->format('Y-m-d'),
];
}
return $date_picker_shortcuts;
}
2022-08-04 12:09:54 +03:00
public function getDailyDateFormat($year = null)
{
$format = 'd M Y';
return $format;
}
2021-01-23 12:16:58 +03:00
public function getMonthlyDateFormat($year = null)
2019-11-16 10:21:14 +03:00
{
$format = 'M Y';
2019-11-16 10:21:14 +03:00
return $format;
}
2021-01-23 12:16:58 +03:00
public function getQuarterlyDateFormat($year = null)
2019-11-16 10:21:14 +03:00
{
$format = 'M Y';
2019-11-16 10:21:14 +03:00
return $format;
}
public function getYearlyDateFormat()
{
$format = 'Y';
2018-12-31 14:41:17 +03:00
2019-11-16 10:21:14 +03:00
return $format;
2018-12-31 14:41:17 +03:00
}
2018-12-14 20:03:18 +05:30
}