1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Use travel to move timezones

This commit is contained in:
David Bomba 2023-03-29 18:14:28 +11:00
parent f5cdf07d78
commit ebdf25e1a8
2 changed files with 180 additions and 5 deletions

View File

@ -418,8 +418,6 @@ class RecurringInvoice extends BaseModel
$offset = $this->client->timezone_offset();
nlog("offset = {$offset}");
/* If this setting is enabled, the recurring invoice may be set in the past */
if ($this->company->stop_on_unpaid_recurring) {

View File

@ -45,6 +45,186 @@ class RecurringDatesTest extends TestCase
}
public function testDailyFrequencyCalc6()
{
$this->travelTo(now()->subHours(8));
$account = Account::factory()->create();
$settings = CompanySettings::defaults();
$settings->entity_send_time = '1';
$settings->timezone_id = '113';
$company = Company::factory()->create([
'account_id' => $account->id,
'settings' => $settings,
]);
$account->default_company_id = $company->id;
$account->save();
$user = User::factory()->create([
'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default')),
'email' => 'whiz@gmail.com',
]);
$userPermissions = collect([
'view_invoice',
'view_client',
'edit_client',
'edit_invoice',
'create_invoice',
'create_client',
]);
$userSettings = DefaultSettings::userSettings();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => $userPermissions->toJson(),
'settings' => json_encode($userSettings),
'is_locked' => 0,
]);
$client = Client::factory()->create(['user_id' => $user->id, 'company_id' => $company->id]);
ClientContact::factory()->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id,
'is_primary' => 1,
]);
$recurring_invoice = RecurringInvoice::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'frequency_id' => RecurringInvoice::FREQUENCY_DAILY,
'next_send_date' => now()->format('Y-m-d'),
'next_send_date_client' => now()->format('Y-m-d'),
'date' => now()->format('Y-m-d'),
'remaining_cycles' => -1,
'status_id' => 1,
]);
$recurring_invoice->service()->start()->save();
$this->assertEquals('1', $client->getSetting('entity_send_time'));
$this->assertEquals('113', $client->getSetting('timezone_id'));
$this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date)->format('Y-m-d'));
$this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d'));
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
$recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient();
$recurring_invoice->save();
$this->assertEquals(now()->startOfDay()->addDay()->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date));
$this->assertEquals(now()->addDay()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d'));
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
$recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient();
$recurring_invoice->save();
$this->assertEquals(now()->startOfDay()->addDays(2)->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date));
$this->assertEquals(now()->addDays(2)->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d'));
$this->travelBack();
}
public function testDailyFrequencyCalc5()
{
$account = Account::factory()->create();
$settings = CompanySettings::defaults();
$settings->entity_send_time = '23';
$settings->timezone_id = '113';
$company = Company::factory()->create([
'account_id' => $account->id,
'settings' => $settings,
]);
$account->default_company_id = $company->id;
$account->save();
$user = User::factory()->create([
'account_id' => $account->id,
'confirmation_code' => $this->createDbHash(config('database.default')),
'email' => 'whiz@gmail.com',
]);
$userPermissions = collect([
'view_invoice',
'view_client',
'edit_client',
'edit_invoice',
'create_invoice',
'create_client',
]);
$userSettings = DefaultSettings::userSettings();
$user->companies()->attach($company->id, [
'account_id' => $account->id,
'is_owner' => 1,
'is_admin' => 1,
'notifications' => CompanySettings::notificationDefaults(),
'permissions' => $userPermissions->toJson(),
'settings' => json_encode($userSettings),
'is_locked' => 0,
]);
$client = Client::factory()->create(['user_id' => $user->id, 'company_id' => $company->id]);
ClientContact::factory()->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id,
'is_primary' => 1,
]);
$recurring_invoice = RecurringInvoice::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'frequency_id' => RecurringInvoice::FREQUENCY_DAILY,
'next_send_date' => now()->format('Y-m-d'),
'next_send_date_client' => now()->format('Y-m-d'),
'date' => now()->format('Y-m-d'),
'remaining_cycles' => -1,
'status_id' => 1,
]);
$recurring_invoice->service()->start()->save();
$this->assertEquals('23', $client->getSetting('entity_send_time'));
$this->assertEquals('113', $client->getSetting('timezone_id'));
$this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date)->format('Y-m-d'));
$this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d'));
$recurring_invoice->next_send_date = $recurring_invoice->nextSendDate();
$recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient();
$recurring_invoice->save();
$this->assertEquals(now()->startOfDay()->addDay()->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date));
$this->assertEquals(now()->addDay()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d'));
}
public function testDailyFrequencyCalc4()
{
@ -369,9 +549,6 @@ class RecurringDatesTest extends TestCase
$recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient();
$recurring_invoice->save();
nlog($recurring_invoice->next_send_date);
nlog($recurring_invoice->next_send_date_client);
$this->assertEquals(now()->startOfDay()->addDay()->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date));
$this->assertEquals(now()->addDay()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d'));