diff --git a/app/Factory/SchedulerFactory.php b/app/Factory/SchedulerFactory.php index c6603e148d4f..5f60fa6dd7ec 100644 --- a/app/Factory/SchedulerFactory.php +++ b/app/Factory/SchedulerFactory.php @@ -26,7 +26,9 @@ class SchedulerFactory $scheduler->is_paused = false; $scheduler->is_deleted = false; $scheduler->template = ''; - + $scheduler->next_run = now()->format('Y-m-d'); + $scheduler->next_run_client = now()->format('Y-m-d'); + return $scheduler; } } diff --git a/app/Repositories/SchedulerRepository.php b/app/Repositories/SchedulerRepository.php index b9c1d5a49039..5c9b9ad4e19c 100644 --- a/app/Repositories/SchedulerRepository.php +++ b/app/Repositories/SchedulerRepository.php @@ -27,8 +27,6 @@ class SchedulerRepository extends BaseRepository public function save(array $data, Scheduler $scheduler): Scheduler { -nlog($data); - $scheduler->fill($data); $scheduler->save(); diff --git a/app/Services/Scheduler/SchedulerService.php b/app/Services/Scheduler/SchedulerService.php index 1c10f32fabf5..6074572bf5a4 100644 --- a/app/Services/Scheduler/SchedulerService.php +++ b/app/Services/Scheduler/SchedulerService.php @@ -53,9 +53,10 @@ class SchedulerService ->each(function ($_client){ $this->client = $_client; - $statement_properties = $this->calculateStatementProperties(); //work out the date range + $statement_properties = $this->calculateStatementProperties(); + $pdf = $_client->service()->statement($statement_properties,true); }); @@ -65,7 +66,12 @@ class SchedulerService } - private function calculateStatementProperties() + /** + * Hydrates the array needed to generate the statement + * + * @return array The statement options array + */ + private function calculateStatementProperties(): array { $start_end = $this->calculateStartAndEndDates(); @@ -79,7 +85,12 @@ class SchedulerService } - private function calculateStartAndEndDates() + /** + * Start and end date of the statement + * + * @return array [$start_date, $end_date]; + */ + private function calculateStartAndEndDates(): array { return match ($this->scheduler->parameters['date_range']) { 'this_month' => [now()->firstOfMonth()->format('Y-m-d'), now()->lastOfMonth()->format('Y-m-d')], @@ -94,7 +105,11 @@ class SchedulerService } - public function calculateNextRun() :?Carbon + /** + * Sets the next run date of the scheduled task + * + */ + private function calculateNextRun() { if (! $this->scheduler->next_run) { return null; diff --git a/database/factories/SchedulerFactory.php b/database/factories/SchedulerFactory.php index 92c86d8ad8ce..e60a53211da3 100644 --- a/database/factories/SchedulerFactory.php +++ b/database/factories/SchedulerFactory.php @@ -31,6 +31,7 @@ class SchedulerFactory extends Factory 'parameters' => [], 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, 'next_run' => now()->addSeconds(rand(86400,8640000)), + 'next_run_client' => now()->addSeconds(rand(86400,8640000)), 'template' => 'statement_task', ]; } diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index 9c9832af76db..ffd2099c6a10 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -12,8 +12,10 @@ namespace Tests\Feature\Scheduler; use App\Export\CSV\ClientExport; +use App\Factory\SchedulerFactory; use App\Models\RecurringInvoice; use App\Models\Scheduler; +use App\Services\Scheduler\SchedulerService; use App\Utils\Traits\MakesHash; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; @@ -51,6 +53,88 @@ class SchedulerTest extends TestCase $this->withoutExceptionHandling(); } + public function testCalculateStartAndEndDates() + { + + $scheduler = SchedulerFactory::create($this->company->id, $this->user->id); + + $data = [ + 'name' => 'A test statement scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => "2023-01-01", + 'template' => 'client_statement', + 'parameters' => [ + 'date_range' => 'previous_month', + 'show_payments_table' => true, + 'show_aging_table' => true, + 'status' => 'paid', + 'clients' => [], + ], + ]; + + $scheduler->fill($data); + $scheduler->save(); + + $service_object = new SchedulerService($scheduler); + + // $reflection = new \ReflectionClass(get_class($service_object)); + // $method = $reflection->getMethod('calculateStatementProperties'); + // $method->setAccessible(true); + // $method->invokeArgs($service_object, []); + + $reflectionMethod = new \ReflectionMethod(SchedulerService::class, 'calculateStartAndEndDates'); + $reflectionMethod->setAccessible(true); + $method = $reflectionMethod->invoke(new SchedulerService($scheduler)); // 'baz' + + $this->assertIsArray($method); + + $this->assertEquals('previous_month', $scheduler->parameters['date_range']); + + $this->assertEqualsCanonicalizing(['2022-12-01','2022-12-31'], $method); + + + // $this->assertEquals('paid', $method['status']); + + } + + public function testCalculateStatementProperties() + { + + $scheduler = SchedulerFactory::create($this->company->id, $this->user->id); + + $data = [ + 'name' => 'A test statement scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => now()->format('Y-m-d'), + 'template' => 'client_statement', + 'parameters' => [ + 'date_range' => 'previous_month', + 'show_payments_table' => true, + 'show_aging_table' => true, + 'status' => 'paid', + 'clients' => [], + ], + ]; + + $scheduler->fill($data); + $scheduler->save(); + + $service_object = new SchedulerService($scheduler); + + // $reflection = new \ReflectionClass(get_class($service_object)); + // $method = $reflection->getMethod('calculateStatementProperties'); + // $method->setAccessible(true); + // $method->invokeArgs($service_object, []); + + $reflectionMethod = new \ReflectionMethod(SchedulerService::class, 'calculateStatementProperties'); + $reflectionMethod->setAccessible(true); + $method = $reflectionMethod->invoke(new SchedulerService($scheduler)); // 'baz' + + $this->assertIsArray($method); + + $this->assertEquals('paid', $method['status']); + + } public function testGetThisMonthRange() { @@ -82,24 +166,15 @@ class SchedulerTest extends TestCase }; } - /** - * 'name' => ['bail', 'required', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)], - 'is_paused' => 'bail|sometimes|boolean', - 'frequency_id' => 'bail|required|integer|digits_between:1,12', - 'next_run' => 'bail|required|date:Y-m-d', - 'template' => 'bail|required|string', - 'parameters' => 'bail|array', - */ - public function testClientStatementGeneration() { $data = [ 'name' => 'A test statement scheduler', 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, - 'next_run' => '2023-01-14', + 'next_run' => now()->format('Y-m-d'), 'template' => 'client_statement', 'parameters' => [ - 'date_range' => 'last_month', + 'date_range' => 'previous_month', 'show_payments_table' => true, 'show_aging_table' => true, 'status' => 'paid',