Tests for calculating date ranges

This commit is contained in:
David Bomba 2023-01-14 18:47:14 +11:00
parent ada6210e34
commit cf9ffb05d5
4 changed files with 67 additions and 39 deletions

View File

@ -59,7 +59,6 @@ class ClientStatement
*/ */
public string $start_date = ''; public string $start_date = '';
/** /**
* If a custom range is select for the date range then * If a custom range is select for the date range then
* the end_date should be supplied in Y-m-d format * the end_date should be supplied in Y-m-d format
@ -93,4 +92,5 @@ class ClientStatement
*/ */
public string $status = 'paid'; // paid | unpaid public string $status = 'paid'; // paid | unpaid
} }

View File

@ -42,51 +42,48 @@ class SchedulerService
//Email only the selected clients //Email only the selected clients
if(count($this->scheduler->parameters['clients']) >= 1) if(count($this->scheduler->parameters['clients']) >= 1)
$query->where('id', $this->transformKeys($this->scheduler->parameters['clients'])); $query->where('id', $this->transformKeys($this->scheduler->parameters['clients']));
$statement_properties = $this->calculateStatementProperties();
$query->cursor() $query->cursor()
->each(function ($client){ ->each(function ($client) use($statement_properties){
//work out the date range //work out the date range
$pdf = $client->service()->statement($statement_properties);
}); });
} }
// public function scheduleStatement() private function calculateStatementProperties()
// { {
$start_end = $this->calculateStartAndEndDates();
// //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
// }
// public function scheduleReport() return [
// { 'start_date' =>$start_end[0],
// //Report type 'end_date' =>$start_end[1],
// //same schema as ScheduleStatement '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() private function calculateStartAndEndDates()
// { {
// //Project ID return match ($this->scheduler->parameters['date_range']) {
// //Tasks - task statuses '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()
{
}
} }

View File

@ -31,7 +31,6 @@ class SchedulerTest extends TestCase
use MakesHash; use MakesHash;
use MockAccountData; use MockAccountData;
use WithoutEvents; use WithoutEvents;
// use RefreshDatabase;
protected function setUp(): void protected function setUp(): void
{ {
@ -52,6 +51,37 @@ class SchedulerTest extends TestCase
$this->withoutExceptionHandling(); $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)], * 'name' => ['bail', 'required', Rule::unique('schedulers')->where('company_id', auth()->user()->company()->id)],
'is_paused' => 'bail|sometimes|boolean', 'is_paused' => 'bail|sometimes|boolean',
@ -66,7 +96,7 @@ class SchedulerTest extends TestCase
$data = [ $data = [
'name' => 'A test statement scheduler', 'name' => 'A test statement scheduler',
'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY,
'next_run' => '2023-01-31', 'next_run' => '2023-01-14',
'template' => 'client_statement', 'template' => 'client_statement',
'clients' => [], 'clients' => [],
'parameters' => [ 'parameters' => [

View File

@ -42,4 +42,5 @@ class RecurringDateTest extends TestCase
$this->assertequals($trial_ends->format('Y-m-d'), '2021-12-03'); $this->assertequals($trial_ends->format('Y-m-d'), '2021-12-03');
} }
} }