From bc065e21322a12e4b1416e7b8461047197d08d8a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 17 Feb 2023 08:59:19 +1100 Subject: [PATCH] Update scheduler date ranges --- app/DataMapper/Schedule/ClientStatement.php | 18 +++--- app/Services/Scheduler/SchedulerService.php | 26 ++++---- tests/Feature/Scheduler/SchedulerTest.php | 69 +++++++++++---------- 3 files changed, 63 insertions(+), 50 deletions(-) diff --git a/app/DataMapper/Schedule/ClientStatement.php b/app/DataMapper/Schedule/ClientStatement.php index a51cc15154fd..f6fd6e4ff734 100644 --- a/app/DataMapper/Schedule/ClientStatement.php +++ b/app/DataMapper/Schedule/ClientStatement.php @@ -32,13 +32,17 @@ class ClientStatement /** * The consts to be used to define the date_range variable of the statement */ - public const THIS_MONTH = 'this_month'; - public const THIS_QUARTER = 'this_quarter'; - public const THIS_YEAR = 'this_year'; - public const PREVIOUS_MONTH = 'previous_month'; - public const PREVIOUS_QUARTER = 'previous_quarter'; - public const PREVIOUS_YEAR = 'previous_year'; - public const CUSTOM_RANGE = "custom_range"; + public const LAST7 = "last7_days"; + public const LAST30 = "last30_days"; + public const LAST365 = "last365_days"; + public const THIS_MONTH = "this_month"; + public const LAST_MONTH = "last_month"; + public const THIS_QUARTER = "this_quarter"; + public const LAST_QUARTER = "last_quarter"; + public const THIS_YEAR = "this_year"; + public const LAST_YEAR = "last_year"; + public const CUSTOM_RANGE = "custom"; + /** * The date range the statement should include diff --git a/app/Services/Scheduler/SchedulerService.php b/app/Services/Scheduler/SchedulerService.php index 854ff8efce4c..23275c303e2c 100644 --- a/app/Services/Scheduler/SchedulerService.php +++ b/app/Services/Scheduler/SchedulerService.php @@ -12,10 +12,12 @@ namespace App\Services\Scheduler; use App\Models\Client; -use App\Models\RecurringInvoice; use App\Models\Scheduler; -use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesHash; +use App\Models\RecurringInvoice; +use App\Utils\Traits\MakesDates; +use App\DataMapper\Schedule\ClientStatement; +use Symfony\Component\HttpClient\Internal\ClientState; class SchedulerService { @@ -82,7 +84,7 @@ class SchedulerService 'status' => $this->scheduler->parameters['status'] ]; } - + /** * Start and end date of the statement * @@ -91,18 +93,20 @@ class SchedulerService private function calculateStartAndEndDates(): array { return match ($this->scheduler->parameters['date_range']) { - 'this_month' => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], - 'this_quarter' => [now()->startOfDay()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->lastOfQuarter()->format('Y-m-d')], - 'this_year' => [now()->startOfDay()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->lastOfYear()->format('Y-m-d')], - 'previous_month' => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')], - 'previous_quarter' => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')], - 'previous_year' => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')], - 'custom_range' => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']], + ClientStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + ClientStatement::LAST30 => [now()->startOfDay()->subDays(30)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + ClientStatement::LAST365 => [now()->startOfDay()->subDays(365)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + ClientStatement::THIS_MONTH => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], + ClientStatement::LAST_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')], + ClientStatement::THIS_QUARTER => [now()->startOfDay()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->lastOfQuarter()->format('Y-m-d')], + ClientStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')], + ClientStatement::THIS_YEAR => [now()->startOfDay()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->lastOfYear()->format('Y-m-d')], + ClientStatement::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')], + ClientStatement::CUSTOM_RANGE => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']], default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], }; } - /** * Sets the next run date of the scheduled task * diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index d2ec6eee0931..6fba294959f6 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -11,20 +11,21 @@ namespace Tests\Feature\Scheduler; -use App\Factory\SchedulerFactory; -use App\Models\Client; -use App\Models\RecurringInvoice; -use App\Models\Scheduler; -use App\Services\Scheduler\SchedulerService; -use App\Utils\Traits\MakesHash; use Carbon\Carbon; +use Tests\TestCase; +use App\Models\Client; +use App\Models\Scheduler; +use Tests\MockAccountData; +use App\Utils\Traits\MakesHash; +use App\Models\RecurringInvoice; +use App\Factory\SchedulerFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Session; +use App\DataMapper\Schedule\ClientStatement; +use App\Services\Scheduler\SchedulerService; +use Illuminate\Validation\ValidationException; use Illuminate\Foundation\Testing\WithoutEvents; use Illuminate\Routing\Middleware\ThrottleRequests; -use Illuminate\Support\Facades\Session; -use Illuminate\Validation\ValidationException; -use Tests\MockAccountData; -use Tests\TestCase; /** * @test @@ -75,7 +76,7 @@ class SchedulerTest extends TestCase 'next_run' => now()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid', @@ -138,7 +139,7 @@ class SchedulerTest extends TestCase 'next_run' => now()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid', @@ -170,7 +171,7 @@ class SchedulerTest extends TestCase 'next_run' => now()->addDay()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid', @@ -194,7 +195,7 @@ class SchedulerTest extends TestCase 'next_run' => now()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid', @@ -223,7 +224,7 @@ class SchedulerTest extends TestCase 'next_run' => now()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid', @@ -258,7 +259,7 @@ class SchedulerTest extends TestCase 'next_run' => "2023-01-01", 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid', @@ -277,7 +278,7 @@ class SchedulerTest extends TestCase $this->assertIsArray($method); - $this->assertEquals('previous_month', $scheduler->parameters['date_range']); + $this->assertEquals(ClientStatement::LAST_MONTH, $scheduler->parameters['date_range']); $this->assertEqualsCanonicalizing(['2022-12-01','2022-12-31'], $method); } @@ -292,7 +293,7 @@ class SchedulerTest extends TestCase 'next_run' => now()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid', @@ -323,13 +324,13 @@ class SchedulerTest extends TestCase { $this->travelTo(Carbon::parse('2023-01-14')); - $this->assertEqualsCanonicalizing(['2023-01-01','2023-01-31'], $this->getDateRange('this_month')); - $this->assertEqualsCanonicalizing(['2023-01-01','2023-03-31'], $this->getDateRange('this_quarter')); - $this->assertEqualsCanonicalizing(['2023-01-01','2023-12-31'], $this->getDateRange('this_year')); + $this->assertEqualsCanonicalizing(['2023-01-01','2023-01-31'], $this->getDateRange(ClientStatement::THIS_MONTH)); + $this->assertEqualsCanonicalizing(['2023-01-01','2023-03-31'], $this->getDateRange(ClientStatement::THIS_QUARTER)); + $this->assertEqualsCanonicalizing(['2023-01-01','2023-12-31'], $this->getDateRange(ClientStatement::THIS_YEAR)); - $this->assertEqualsCanonicalizing(['2022-12-01','2022-12-31'], $this->getDateRange('previous_month')); - $this->assertEqualsCanonicalizing(['2022-10-01','2022-12-31'], $this->getDateRange('previous_quarter')); - $this->assertEqualsCanonicalizing(['2022-01-01','2022-12-31'], $this->getDateRange('previous_year')); + $this->assertEqualsCanonicalizing(['2022-12-01','2022-12-31'], $this->getDateRange(ClientStatement::LAST_MONTH)); + $this->assertEqualsCanonicalizing(['2022-10-01','2022-12-31'], $this->getDateRange(ClientStatement::LAST_QUARTER)); + $this->assertEqualsCanonicalizing(['2022-01-01','2022-12-31'], $this->getDateRange(ClientStatement::LAST_YEAR)); $this->travelBack(); } @@ -337,13 +338,17 @@ class SchedulerTest extends TestCase private function getDateRange($range) { return match ($range) { - 'this_month' => [now()->firstOfMonth()->format('Y-m-d'), now()->lastOfMonth()->format('Y-m-d')], - 'this_quarter' => [now()->firstOfQuarter()->format('Y-m-d'), now()->lastOfQuarter()->format('Y-m-d')], - 'this_year' => [now()->firstOfYear()->format('Y-m-d'), now()->lastOfYear()->format('Y-m-d')], - 'previous_month' => [now()->subMonth()->firstOfMonth()->format('Y-m-d'), now()->subMonth()->lastOfMonth()->format('Y-m-d')], - 'previous_quarter' => [now()->subQuarter()->firstOfQuarter()->format('Y-m-d'), now()->subQuarter()->lastOfQuarter()->format('Y-m-d')], - 'previous_year' => [now()->subYear()->firstOfYear()->format('Y-m-d'), now()->subYear()->lastOfYear()->format('Y-m-d')], - 'custom_range' => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']] + ClientStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + ClientStatement::LAST30 => [now()->startOfDay()->subDays(30)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + ClientStatement::LAST365 => [now()->startOfDay()->subDays(365)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + ClientStatement::THIS_MONTH => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], + ClientStatement::LAST_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')], + ClientStatement::THIS_QUARTER => [now()->startOfDay()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->lastOfQuarter()->format('Y-m-d')], + ClientStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')], + ClientStatement::THIS_YEAR => [now()->startOfDay()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->lastOfYear()->format('Y-m-d')], + ClientStatement::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')], + ClientStatement::CUSTOM_RANGE => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']], + default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], }; } @@ -355,7 +360,7 @@ class SchedulerTest extends TestCase 'next_run' => now()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'previous_month', + 'date_range' => ClientStatement::LAST_MONTH, 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid',