From cf9ffb05d5b65e3a0b19b45cfec85ec467a80c9c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 14 Jan 2023 18:47:14 +1100 Subject: [PATCH] Tests for calculating date ranges --- app/DataMapper/Schedule/ClientStatement.php | 2 +- app/Services/Scheduler/SchedulerService.php | 69 ++++++++++----------- tests/Feature/Scheduler/SchedulerTest.php | 34 +++++++++- tests/Unit/RecurringDateTest.php | 1 + 4 files changed, 67 insertions(+), 39 deletions(-) diff --git a/app/DataMapper/Schedule/ClientStatement.php b/app/DataMapper/Schedule/ClientStatement.php index e5f51a0051e4..37ba76d081cb 100644 --- a/app/DataMapper/Schedule/ClientStatement.php +++ b/app/DataMapper/Schedule/ClientStatement.php @@ -59,7 +59,6 @@ class ClientStatement */ public string $start_date = ''; - /** * If a custom range is select for the date range then * the end_date should be supplied in Y-m-d format @@ -93,4 +92,5 @@ class ClientStatement */ public string $status = 'paid'; // paid | unpaid + } \ No newline at end of file diff --git a/app/Services/Scheduler/SchedulerService.php b/app/Services/Scheduler/SchedulerService.php index bcaf4c95d7f8..684fe282c442 100644 --- a/app/Services/Scheduler/SchedulerService.php +++ b/app/Services/Scheduler/SchedulerService.php @@ -42,51 +42,48 @@ class SchedulerService //Email only the selected clients if(count($this->scheduler->parameters['clients']) >= 1) $query->where('id', $this->transformKeys($this->scheduler->parameters['clients'])); - + + $statement_properties = $this->calculateStatementProperties(); + $query->cursor() - ->each(function ($client){ + ->each(function ($client) use($statement_properties){ //work out the date range + $pdf = $client->service()->statement($statement_properties); }); + } - // public function scheduleStatement() - // { - - // //Is it for one client - // //Is it for all clients - // //Is it for all clients excluding these clients - - // //Frequency - - // //show aging - // //show payments - // //paid/unpaid - - // //When to send? 1st of month - // //End of month - // //This date - - // } + private function calculateStatementProperties() + { + $start_end = $this->calculateStartAndEndDates(); - // public function scheduleReport() - // { - // //Report type - // //same schema as ScheduleStatement - // } + return [ + 'start_date' =>$start_end[0], + 'end_date' =>$start_end[1], + 'show_payments_table' => $this->scheduler->parameters['show_payments_table'], + 'show_aging_table' => $this->scheduler->parameters['show_aging_table'], + 'status' => $this->scheduler->status + ]; - // public function scheduleEntitySend() - // { - // //Entity - // //Entity Id - // //When - // } + } - // public function projectStatus() - // { - // //Project ID - // //Tasks - task statuses - // } + private function calculateStartAndEndDates() + { + return match ($this->scheduler->parameters['date_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()->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()->format('Y-m-d')], + 'custom_range' => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']] + }; + } + private function thisMonth() + { + + } } \ No newline at end of file diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index 13c2c84c0f29..3e67bd79ea8e 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -31,7 +31,6 @@ class SchedulerTest extends TestCase use MakesHash; use MockAccountData; use WithoutEvents; - // use RefreshDatabase; protected function setUp(): void { @@ -52,6 +51,37 @@ class SchedulerTest extends TestCase $this->withoutExceptionHandling(); } + + public function testGetThisMonthRange() + { + + $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(['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->travelBack(); + + } + + 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']] + }; + } + /** * 'name' => ['bail', 'required', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)], 'is_paused' => 'bail|sometimes|boolean', @@ -66,7 +96,7 @@ class SchedulerTest extends TestCase $data = [ 'name' => 'A test statement scheduler', 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, - 'next_run' => '2023-01-31', + 'next_run' => '2023-01-14', 'template' => 'client_statement', 'clients' => [], 'parameters' => [ diff --git a/tests/Unit/RecurringDateTest.php b/tests/Unit/RecurringDateTest.php index 8276f9a17902..e8224badb29d 100644 --- a/tests/Unit/RecurringDateTest.php +++ b/tests/Unit/RecurringDateTest.php @@ -42,4 +42,5 @@ class RecurringDateTest extends TestCase $this->assertequals($trial_ends->format('Y-m-d'), '2021-12-03'); } + }