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

Tests for calculating recurring next send dates

This commit is contained in:
David Bomba 2023-03-29 17:53:49 +11:00
parent e85b0c5964
commit f5cdf07d78
3 changed files with 355 additions and 5 deletions

View File

@ -418,6 +418,8 @@ 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

@ -56,6 +56,8 @@ class RecurringInvoiceTest extends TestCase
$this->makeTestData();
}
public function testBulkIncreasePriceWithJob()
{
@ -561,4 +563,5 @@ class RecurringInvoiceTest extends TestCase
$this->assertEquals(null, $invoice->subscription_id);
}
}

View File

@ -11,12 +11,21 @@
namespace Tests\Unit;
use App\Factory\RecurringInvoiceFactory;
use App\Models\RecurringInvoice;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;
use Tests\MockAccountData;
use Tests\TestCase;
use App\Models\User;
use App\Models\Client;
use App\Models\Account;
use App\Models\Company;
use Tests\MockAccountData;
use App\Models\ClientContact;
use Illuminate\Support\Carbon;
use App\Utils\Traits\MakesHash;
use App\Models\RecurringInvoice;
use App\DataMapper\CompanySettings;
use App\DataMapper\DefaultSettings;
use App\Factory\RecurringInvoiceFactory;
use App\Factory\InvoiceToRecurringInvoiceFactory;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -24,6 +33,7 @@ use Tests\TestCase;
*/
class RecurringDatesTest extends TestCase
{
use MakesHash;
use MockAccountData;
use DatabaseTransactions;
@ -34,6 +44,341 @@ class RecurringDatesTest extends TestCase
$this->makeTestData();
}
public function testDailyFrequencyCalc4()
{
$account = Account::factory()->create();
$settings = CompanySettings::defaults();
$settings->entity_send_time = '6';
$settings->timezone_id = '1';
$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('6', $client->getSetting('entity_send_time'));
$this->assertEquals('1', $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 testDailyFrequencyCalc3()
{
$account = Account::factory()->create();
$settings = CompanySettings::defaults();
$settings->entity_send_time = '1';
$settings->timezone_id = '1';
$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,
]);
$this->assertEquals('1', $client->getSetting('entity_send_time'));
$this->assertEquals('1', $client->getSetting('timezone_id'));
$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(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 testDailyFrequencyCalc2()
{
$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,
]);
$this->assertEquals('23', $client->getSetting('entity_send_time'));
$this->assertEquals('113', $client->getSetting('timezone_id'));
$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(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 testDailyFrequencyCalc()
{
$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,
]);
$this->assertEquals('1', $client->getSetting('entity_send_time'));
$this->assertEquals('113', $client->getSetting('timezone_id'));
$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(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();
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'));
}
public function testRecurringDatesDraftInvoice()
{
$recurring_invoice = RecurringInvoiceFactory::create($this->company->id, $this->user->id);