1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Utils/Traits/MakesDates.php

198 lines
8.0 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Utils\Traits;
2023-10-26 04:57:44 +02:00
use App\DataMapper\Schedule\EmailStatement;
use App\Models\Company;
use Carbon\Carbon;
2020-10-28 11:10:49 +01:00
use DateTime;
use DateTimeZone;
2019-08-28 02:58:13 +02:00
/**
* Class MakesDates.
*/
2019-04-23 00:41:32 +02:00
trait MakesDates
{
/**
* Converts from UTC to client timezone.
* @param datetime object $utc_date
* @param string $timezone ie Australia/Sydney
* @return Carbon Carbon object
*/
public function createClientDate($utc_date, $timezone)
{
if (is_string($utc_date)) {
$utc_date = $this->convertToDateObject($utc_date);
}
2020-10-28 11:10:49 +01:00
return $utc_date->setTimezone(new DateTimeZone($timezone));
}
/**
* Converts from client timezone to UTC.
2020-10-28 11:10:49 +01:00
* @param datetime object $utc_date
* @return Carbon Carbon object
*/
public function createUtcDate($client_date)
{
if (is_string($client_date)) {
$client_date = $this->convertToDateObject($client_date);
}
2020-10-28 11:10:49 +01:00
return $client_date->setTimezone(new DateTimeZone('GMT'));
}
/**
* Formats a date.
* @param Carbon|string $date Carbon object or date string
* @param string $format The date display format
* @return string The formatted date
*/
2024-01-14 05:05:00 +01:00
public function formatDate($date, string $format): string
{
if (! isset($date)) {
return '';
2020-11-25 15:19:52 +01:00
}
if (is_string($date)) {
$date = $this->convertToDateObject($date);
}
return $date->format($format);
}
/**
* Formats a datedate.
2022-07-18 02:04:17 +02:00
* @param $date Carbon object or date string
* @param string $format The date display format
* @return string The formatted date
*/
2024-01-14 05:05:00 +01:00
public function formatDatetime($date, string $format): string
{
2024-06-25 06:58:18 +02:00
return Carbon::createFromTimestamp((int) ($date ?? 0))->format($format.' g:i a');
}
/**
* Formats a date.
* @param Carbon/String $date Carbon object or date string
* @param string $format The date display format
* @return string The formatted date
*/
2024-01-14 05:05:00 +01:00
public function formatDateTimestamp($timestamp, string $format): string
{
return Carbon::createFromTimestamp((int)$timestamp)->format($format);
}
private function convertToDateObject($date)
{
2021-01-12 08:40:18 +01:00
$dt = new DateTime($date);
2021-01-12 08:38:16 +01:00
$dt->setTimezone(new DateTimeZone('UTC'));
2021-01-12 08:38:16 +01:00
return $dt;
}
2021-02-23 11:04:39 +01:00
public function translateDate($date, $format, $locale)
{
if (empty($date)) {
return '';
}
2021-02-23 11:04:39 +01:00
Carbon::setLocale($locale);
try {
return Carbon::parse($date)->translatedFormat($format);
} catch (\Exception $e) {
return 'Invalid date!';
}
2021-02-23 11:04:39 +01:00
}
2023-04-13 05:31:19 +02:00
/**
* Start and end date of the statement
*
* @return array [$start_date, $end_date];
*/
public function calculateStartAndEndDates(array $data, ?Company $company = null): array
2023-04-13 05:31:19 +02:00
{
//override for financial years
if($data['date_range'] == 'this_year') {
$first_month_of_year = $company ? $company?->first_month_of_year : 1;
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
2023-10-26 04:57:44 +02:00
if(now()->lt($fin_year_start)) {
$fin_year_start->subYearNoOverflow();
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
}
//override for financial years
if($data['date_range'] == 'last_year') {
$first_month_of_year = $company ? $company?->first_month_of_year : 1;
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
$fin_year_start->subYearNoOverflow();
if(now()->subYear()->lt($fin_year_start)) {
$fin_year_start->subYearNoOverflow();
}
2024-01-14 05:05:00 +01:00
}
2023-06-26 07:14:58 +02:00
return match ($data['date_range']) {
2023-04-13 05:31:19 +02:00
EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
EmailStatement::LAST30 => [now()->startOfDay()->subDays(30)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
EmailStatement::LAST365 => [now()->startOfDay()->subDays(365)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
EmailStatement::THIS_MONTH => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
EmailStatement::LAST_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')],
2024-05-09 02:15:46 +02:00
EmailStatement::THIS_QUARTER => [now()->startOfDay()->startOfQuarter()->format('Y-m-d'), now()->startOfDay()->endOfQuarter()->format('Y-m-d')],
EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->startOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->endOfQuarter()->format('Y-m-d')],
EmailStatement::THIS_YEAR => [$fin_year_start->format('Y-m-d'), $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d')],
EmailStatement::LAST_YEAR => [$fin_year_start->format('Y-m-d'), $fin_year_start->copy()->addYear()->subDay()->format('Y-m-d')],
2023-06-26 07:14:58 +02:00
EmailStatement::CUSTOM_RANGE => [$data['start_date'], $data['end_date']],
2023-04-13 05:31:19 +02:00
default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
};
}
2024-06-28 23:42:45 +02:00
public function calculatePreviousPeriodStartAndEndDates(array $data, ?Company $company = null): array
{
//override for financial years
if($data['date_range'] == 'this_year') {
$first_month_of_year = $company ? $company?->first_month_of_year : 1;
$fin_year_start = now()->createFromDate(now()->year, $first_month_of_year, 1);
$fin_year_start->subYearNoOverflow();
if(now()->subYear()->lt($fin_year_start)) {
$fin_year_start->subYearNoOverflow();
}
}
2024-08-22 08:45:06 +02:00
2024-06-28 23:42:45 +02:00
return match ($data['date_range']) {
EmailStatement::LAST7 => [now()->startOfDay()->subDays(14)->format('Y-m-d'), now()->subDays(7)->startOfDay()->format('Y-m-d')],
EmailStatement::LAST30 => [now()->startOfDay()->subDays(60)->format('Y-m-d'), now()->subDays(30)->startOfDay()->format('Y-m-d')],
EmailStatement::LAST365 => [now()->startOfDay()->subDays(739)->format('Y-m-d'), now()->subDays(365)->startOfDay()->format('Y-m-d')],
EmailStatement::THIS_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')],
EmailStatement::LAST_MONTH => [now()->startOfDay()->subMonthsNoOverflow(2)->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')],
EmailStatement::THIS_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->startOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->endOfQuarter()->format('Y-m-d')],
EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuartersNoOverflow(2)->startOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuartersNoOverflow(2)->endOfQuarter()->format('Y-m-d')],
EmailStatement::THIS_YEAR => [$fin_year_start->subYear()->format('Y-m-d'), $fin_year_start->copy()->subDay()->format('Y-m-d')],
EmailStatement::LAST_YEAR => [$fin_year_start->subYear(2)->format('Y-m-d'), $fin_year_start->copy()->subYear()->subDay()->format('Y-m-d')],
EmailStatement::CUSTOM_RANGE => [$data['start_date'], $data['end_date']],
default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
};
}
2023-10-26 04:57:44 +02:00
}