1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/tests/Unit/RecurringDatesTest.php

616 lines
22 KiB
PHP
Raw Normal View History

2020-09-13 12:20:11 +02:00
<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
2020-09-14 13:11:46 +02:00
*/
2020-09-13 12:20:11 +02:00
namespace Tests\Unit;
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;
2020-09-13 12:20:11 +02:00
use App\Models\RecurringInvoice;
use App\DataMapper\CompanySettings;
use App\DataMapper\DefaultSettings;
use App\Factory\RecurringInvoiceFactory;
use App\Factory\InvoiceToRecurringInvoiceFactory;
2020-09-13 12:20:11 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
2020-09-24 02:57:44 +02:00
* @covers \App\Models\RecurringInvoice
2020-09-13 12:20:11 +02:00
*/
class RecurringDatesTest extends TestCase
{
use MakesHash;
2020-09-13 12:20:11 +02:00
use MockAccountData;
use DatabaseTransactions;
protected function setUp() :void
2020-09-13 12:20:11 +02:00
{
parent::setUp();
$this->makeTestData();
}
2023-03-29 09:14:28 +02:00
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()
{
$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();
$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'));
}
2020-09-13 12:20:11 +02:00
public function testRecurringDatesDraftInvoice()
{
$recurring_invoice = RecurringInvoiceFactory::create($this->company->id, $this->user->id);
$recurring_invoice->line_items = $this->buildLineItems();
$recurring_invoice->client_id = $this->client->id;
$recurring_invoice->save();
$recurring_invoice->calc()->getInvoice();
$this->assertEquals(0, count($recurring_invoice->recurringDates()));
}
public function testRecurringDatesPendingInvoice()
{
$recurring_invoice = RecurringInvoiceFactory::create($this->company->id, $this->user->id);
$recurring_invoice->line_items = $this->buildLineItems();
$recurring_invoice->client_id = $this->client->id;
$recurring_invoice->status_id = RecurringInvoice::STATUS_PENDING;
$recurring_invoice->frequency_id = RecurringInvoice::FREQUENCY_MONTHLY;
$recurring_invoice->remaining_cycles = 5;
2020-09-14 13:11:46 +02:00
$recurring_invoice->due_date_days = '5';
2020-09-13 12:20:11 +02:00
$recurring_invoice->next_send_date = now();
$recurring_invoice->save();
$recurring_invoice->calc()->getInvoice();
$this->assertEquals(5, count($recurring_invoice->recurringDates()));
}
public function testRecurringDatesPendingInvoiceWithNoDueDate()
{
$recurring_invoice = RecurringInvoiceFactory::create($this->company->id, $this->user->id);
$recurring_invoice->line_items = $this->buildLineItems();
$recurring_invoice->client_id = $this->client->id;
$recurring_invoice->status_id = RecurringInvoice::STATUS_PENDING;
$recurring_invoice->frequency_id = RecurringInvoice::FREQUENCY_MONTHLY;
$recurring_invoice->remaining_cycles = 5;
$recurring_invoice->due_date_days = null;
$recurring_invoice->next_send_date = now();
2020-09-13 12:20:11 +02:00
$recurring_invoice->save();
$recurring_invoice->calc()->getInvoice();
$this->assertEquals(5, count($recurring_invoice->recurringDates()));
}
2020-09-24 02:57:44 +02:00
public function testCompareDatesLogic()
{
$date = now()->startOfDay()->format('Y-m-d');
$this->assertTrue(Carbon::parse($date)->lte(now()->startOfDay()));
}
2020-09-13 12:20:11 +02:00
}