1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/tests/Unit/GeneratesConvertedQuoteCounterTest.php

157 lines
4.4 KiB
PHP
Raw Normal View History

2022-03-25 07:12:49 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
2022-03-25 07:12:49 +01:00
*/
2022-03-25 07:12:49 +01:00
namespace Tests\Unit;
2024-10-17 23:54:38 +02:00
use Tests\TestCase;
use App\Models\User;
use App\Models\Quote;
2022-03-25 07:12:49 +01:00
use App\Models\Client;
2024-10-17 23:54:38 +02:00
use App\Models\Account;
2022-03-25 07:12:49 +01:00
use App\Models\Company;
2024-10-17 23:54:38 +02:00
use App\Models\Country;
2022-03-25 07:12:49 +01:00
use App\Models\Invoice;
2024-10-17 23:54:38 +02:00
use App\Models\ClientContact;
2022-03-25 07:12:49 +01:00
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
2024-10-17 23:54:38 +02:00
use App\Utils\Traits\GeneratesConvertedQuoteCounter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-03-25 07:12:49 +01:00
/**
2024-09-16 13:13:55 +02:00
*
* App\Utils\Traits\GeneratesConvertedQuoteCounter
2022-03-25 07:12:49 +01:00
*/
class GeneratesConvertedQuoteCounterTest extends TestCase
{
use GeneratesConvertedQuoteCounter;
use DatabaseTransactions;
use MakesHash;
2024-01-01 07:58:59 +01:00
protected $account;
protected $faker;
protected $client;
protected $company;
2024-08-22 08:57:52 +02:00
protected function setUp(): void
2022-03-25 07:12:49 +01:00
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
2024-10-17 23:54:38 +02:00
if (\App\Models\Country::count() == 0) {
\Illuminate\Support\Facades\Artisan::call('db:seed', ['--force' => true]);
}
2022-03-25 07:12:49 +01:00
}
public function testCounterExtraction()
{
$this->account = Account::factory()->create([
'hosted_client_count' => 1000,
'hosted_company_count' => 1000,
2022-03-25 07:12:49 +01:00
]);
2022-03-25 07:12:49 +01:00
$this->account->num_users = 3;
$this->account->save();
2022-03-25 07:57:53 +01:00
$fake_email = $this->faker->email();
$user = User::whereEmail($fake_email)->first();
2022-03-25 07:57:53 +01:00
if (! $user) {
$user = User::factory()->create([
'account_id' => $this->account->id,
'confirmation_code' => $this->createDbHash(config('database.default')),
'email' => $fake_email,
2022-03-25 07:57:53 +01:00
]);
}
$user_id = $user->id;
2022-03-25 07:12:49 +01:00
$this->company = Company::factory()->create([
'account_id' => $this->account->id,
]);
2022-03-25 07:12:49 +01:00
$this->client = Client::factory()->create([
'user_id' => $user_id,
'company_id' => $this->company->id,
2022-03-25 07:12:49 +01:00
]);
$contact = ClientContact::factory()->create([
'user_id' => $user_id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'is_primary' => 1,
'send_email' => true,
2022-03-25 07:12:49 +01:00
]);
$settings = $this->client->getMergedSettings();
$settings->invoice_number_counter = 1;
$settings->invoice_number_pattern = '{$year}-I{$counter}';
$settings->quote_number_pattern = '{$year}-Q{$counter}';
$settings->shared_invoice_quote_counter = 1;
2024-01-01 07:58:59 +01:00
$settings->timezone_id = '31';
2022-03-25 07:12:49 +01:00
$this->company->settings = $settings;
$this->company->save();
$this->client->settings = $settings;
$this->client->save();
$quote = Quote::factory()->create([
'user_id' => $this->client->user_id,
'company_id' => $this->client->company_id,
'client_id' => $this->client->id,
2022-03-25 07:12:49 +01:00
]);
$quote = $quote->service()->markSent()->convert()->save();
$invoice = Invoice::find($quote->invoice_id);
$this->assertNotNull($invoice);
2023-01-02 07:55:57 +01:00
$this->assertEquals(now()->format('Y'). '-Q0001', $quote->number);
$this->assertEquals(now()->format('Y'). '-I0001', $invoice->number);
2022-03-25 07:12:49 +01:00
2022-03-25 07:16:26 +01:00
$settings = $this->client->getMergedSettings();
$settings->invoice_number_counter = 100;
$settings->invoice_number_pattern = 'I{$counter}';
$settings->quote_number_pattern = 'Q{$counter}';
$settings->shared_invoice_quote_counter = 1;
2024-01-01 07:58:59 +01:00
$settings->timezone_id = '31';
2022-03-25 07:16:26 +01:00
$this->company->settings = $settings;
2022-03-25 07:12:49 +01:00
2022-03-25 07:16:26 +01:00
$this->company->save();
2022-03-25 07:12:49 +01:00
2022-03-25 07:16:26 +01:00
$this->client->settings = $settings;
$this->client->save();
2022-03-25 07:12:49 +01:00
2022-03-25 07:16:26 +01:00
$quote = Quote::factory()->create([
'user_id' => $this->client->user_id,
'company_id' => $this->client->company_id,
'client_id' => $this->client->id,
2022-03-25 07:16:26 +01:00
]);
2022-03-25 07:12:49 +01:00
2022-03-25 07:16:26 +01:00
$quote = $quote->service()->markSent()->convert()->save();
2022-03-25 07:12:49 +01:00
2022-03-25 07:16:26 +01:00
$invoice = Invoice::find($quote->invoice_id);
$this->assertNotNull($invoice);
2022-03-25 07:12:49 +01:00
2022-03-25 07:16:26 +01:00
$this->assertEquals('Q0100', $quote->number);
$this->assertEquals('I0100', $invoice->number);
}
2022-03-25 07:12:49 +01:00
}