mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Tests for Generates Counter
This commit is contained in:
parent
494504a2d0
commit
b2dcd5cbcb
@ -31,15 +31,30 @@ trait GeneratesCounter
|
||||
//Reset counters if enabled
|
||||
$this->resetCounters($client);
|
||||
|
||||
$counter = $this->getNextInvoiceCounter($client);
|
||||
$is_client_counter = false;
|
||||
|
||||
$pattern = $client->company->settings->invoice_number_pattern;
|
||||
|
||||
if(strpos($pattern, 'client_counter') === false)
|
||||
{
|
||||
$counter = $client->company->settings->invoice_number_counter;
|
||||
}
|
||||
else
|
||||
{
|
||||
$counter = $client->settings->invoice_number_counter;
|
||||
$is_client_counter = true;
|
||||
}
|
||||
|
||||
$counter = $this->checkEntityNumber($client, $counter, $client->company->settings->counter_padding, $client->company->settings->invoice_number_prefix);
|
||||
|
||||
$client_counter = $this->getNextInvoiceClientCounter($client);
|
||||
$client_counter = $this->checkEntityNumber($client, $client_counter, $client->company->settings->counter_padding, $client->company->settings->invoice_number_prefix);
|
||||
|
||||
//build number pattern
|
||||
$invoice_number = $this->applyNumberPattern($client, $counter, $client_counter, $client->company->settings->invoice_number_pattern);
|
||||
$invoice_number = $this->applyNumberPattern($client, $counter, $client->company->settings->invoice_number_pattern);
|
||||
|
||||
if($is_client_counter)
|
||||
$this->incrementCounter($client, 'invoice_number_counter');
|
||||
else
|
||||
$this->incrementCounter($client->company, 'invoice_number_counter');
|
||||
|
||||
return $invoice_number;
|
||||
}
|
||||
|
||||
@ -76,6 +91,12 @@ trait GeneratesCounter
|
||||
return $client_number;
|
||||
}
|
||||
|
||||
public function hasSharedCounter($client)
|
||||
{
|
||||
|
||||
return $client->getSettingsByKey('shared_invoice_quote_counter') === TRUE;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the number has not already been used
|
||||
@ -104,7 +125,6 @@ trait GeneratesCounter
|
||||
|
||||
} while ($check);
|
||||
|
||||
$this->incrementCounter($entity, 'invoice_number_counter');
|
||||
|
||||
return $number;
|
||||
}
|
||||
@ -118,15 +138,12 @@ trait GeneratesCounter
|
||||
*/
|
||||
private function incrementCounter($entity, string $counter_name) :void
|
||||
{
|
||||
$company_settings = $entity->company->settings;
|
||||
$company_settings->$counter_name = $company_settings->$counter_name + 1;
|
||||
$entity->company->settings = $company_settings;
|
||||
$entity->company->save();
|
||||
|
||||
$client_settings = $entity->settings;
|
||||
$client_settings->$counter_name = $client_settings->$counter_name + 1;
|
||||
$entity->settings = $client_settings;
|
||||
$settings = $entity->settings;
|
||||
$settings->$counter_name = $settings->$counter_name + 1;
|
||||
$entity->settings = $settings;
|
||||
$entity->save();
|
||||
|
||||
}
|
||||
|
||||
private function prefixCounter($counter, $prefix) : string
|
||||
@ -153,31 +170,6 @@ trait GeneratesCounter
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next invoice counter.
|
||||
*
|
||||
* Determine whether we need to harvest the
|
||||
* Client specific invoice increment OR
|
||||
* the Company wide invoice increment
|
||||
*
|
||||
* @param string $number_pattern The number pattern
|
||||
* @param \App\Models\Client $client The client
|
||||
*
|
||||
* @return string The next invoice counter.
|
||||
*/
|
||||
private function getNextInvoiceCounter(Client $client) : string
|
||||
{
|
||||
|
||||
return $client->company->settings->invoice_number_counter;
|
||||
|
||||
}
|
||||
|
||||
private function getNextInvoiceClientCounter(Client $client) : string
|
||||
{
|
||||
|
||||
return $client->settings->invoice_number_counter;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* If we are using counter reset,
|
||||
@ -191,11 +183,17 @@ trait GeneratesCounter
|
||||
|
||||
$timezone = $client->company->timezone()->name;
|
||||
|
||||
Log::error('timezone = '.$timezone);
|
||||
|
||||
$reset_date = Carbon::parse($client->company->settings->reset_counter_date, $timezone);
|
||||
|
||||
if (! $reset_date->isToday())
|
||||
Log::error('reset date = '. $reset_date->format('Y-m-d'));
|
||||
|
||||
if (! $reset_date->isToday() || ! $client->company->settings->reset_counter_date)
|
||||
return false;
|
||||
|
||||
Log::error('we are resetting here!!');
|
||||
|
||||
switch ($client->company->reset_counter_frequency_id) {
|
||||
case RecurringInvoice::FREQUENCY_WEEKLY:
|
||||
$reset_date->addWeek();
|
||||
@ -239,19 +237,20 @@ trait GeneratesCounter
|
||||
$client->company->save();
|
||||
}
|
||||
|
||||
private function applyNumberPattern($client, $counter, $client_counter, $pattern)
|
||||
private function applyNumberPattern($client, $counter, $pattern)
|
||||
{
|
||||
if(!$pattern)
|
||||
return $counter;
|
||||
|
||||
|
||||
$search = ['{$year}'];
|
||||
$replace = [date('Y')];
|
||||
|
||||
$search[] = '{$counter}';
|
||||
$replace[] = [$counter];
|
||||
$replace[] = $counter;
|
||||
|
||||
$search[] = '{$client_counter';
|
||||
$replace[] = [$client_counter];
|
||||
$search[] = '{$client_counter}';
|
||||
$replace[] = $counter;
|
||||
|
||||
if (strstr($pattern, '{$user_id}')) {
|
||||
$user_id = auth()->check() ? auth()->user()->id : 0;
|
||||
@ -271,20 +270,22 @@ trait GeneratesCounter
|
||||
}
|
||||
|
||||
$search[] = '{$custom1}';
|
||||
$replace[] = [$client->custom_value1];
|
||||
$replace[] = $client->custom_value1;
|
||||
|
||||
$search[] = '{$custom2}';
|
||||
$replace[] = [$client->custom_value1];
|
||||
$replace[] = $client->custom_value1;
|
||||
|
||||
$search[] = '{$custom3}';
|
||||
$replace[] = [$client->custom_value1];
|
||||
$replace[] = $client->custom_value1;
|
||||
|
||||
$search[] = '{$custom4}';
|
||||
$replace[] = [$client->custom_value1];
|
||||
$replace[] = $client->custom_value1;
|
||||
|
||||
$search[] = '{$id_number}';
|
||||
$replace[] = [$client->id_number];
|
||||
|
||||
$replace[] = $client->id_number;
|
||||
Log::error($search);
|
||||
Log::error($replace);
|
||||
Log::error($pattern);
|
||||
return str_replace($search, $replace, $pattern);
|
||||
|
||||
}
|
||||
|
@ -9,10 +9,12 @@ use App\Models\Credit;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\RecurringInvoice;
|
||||
use App\Utils\Traits\GeneratesCounter;
|
||||
use App\Utils\Traits\GeneratesNumberCounter;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
@ -21,23 +23,202 @@ use Tests\TestCase;
|
||||
*/
|
||||
class GeneratesCounterTest extends TestCase
|
||||
{
|
||||
|
||||
use GeneratesCounter;
|
||||
use DatabaseTransactions;
|
||||
use MockAccountData;
|
||||
use MakesHash;
|
||||
//use MockAccountData;
|
||||
|
||||
public function setUp() :void
|
||||
{
|
||||
|
||||
parent::setUp();
|
||||
|
||||
$this->makeTestData();
|
||||
|
||||
Session::start();
|
||||
$this->faker = \Faker\Factory::create();
|
||||
Model::reguard();
|
||||
$account = factory(\App\Models\Account::class)->create();
|
||||
$company = factory(\App\Models\Company::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$account->default_company_id = $company->id;
|
||||
$account->save();
|
||||
$user = factory(\App\Models\User::class)->create([
|
||||
// 'account_id' => $account->id,
|
||||
'confirmation_code' => $this->createDbHash(config('database.default'))
|
||||
]);
|
||||
$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,
|
||||
'permissions' => $userPermissions->toJson(),
|
||||
'settings' => json_encode($userSettings),
|
||||
'is_locked' => 0,
|
||||
]);
|
||||
factory(\App\Models\Client::class)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
|
||||
factory(\App\Models\ClientContact::class,1)->create([
|
||||
'user_id' => $user->id,
|
||||
'client_id' => $c->id,
|
||||
'company_id' => $company->id,
|
||||
'is_primary' => 1
|
||||
]);
|
||||
factory(\App\Models\ClientContact::class,2)->create([
|
||||
'user_id' => $user->id,
|
||||
'client_id' => $c->id,
|
||||
'company_id' => $company->id
|
||||
]);
|
||||
});
|
||||
$this->client = Client::whereUserId($user->id)->whereCompanyId($company->id)->first();
|
||||
}
|
||||
|
||||
public function testGeneric()
|
||||
|
||||
public function testHasSharedCounter()
|
||||
{
|
||||
$this->assertFalse($this->hasSharedCounter($this->client));
|
||||
}
|
||||
|
||||
public function testInvoiceNumberValue()
|
||||
{
|
||||
|
||||
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
||||
|
||||
$this->assertEquals($invoice_number, 1);
|
||||
|
||||
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
||||
|
||||
$this->assertEquals($invoice_number, 2);
|
||||
|
||||
|
||||
// Log::error(print_r($client->company->settings,1));
|
||||
// Log::error(print_r($client->settings,1));
|
||||
}
|
||||
|
||||
public function testInvoiceNumberPattern()
|
||||
{
|
||||
$settings = $this->client->company->settings;
|
||||
|
||||
$settings->invoice_number_pattern = '{$year}-{$counter}';
|
||||
$this->client->company->settings = $settings;
|
||||
$this->client->company->save();
|
||||
|
||||
//Log::error(print_r($this->client->company->settings,1));
|
||||
|
||||
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
||||
$invoice_number2 = $this->getNextInvoiceNumber($this->client);
|
||||
|
||||
$this->assertEquals($invoice_number, '2019-1');
|
||||
$this->assertEquals($invoice_number2, '2019-2');
|
||||
$this->assertEquals($this->client->company->settings->invoice_number_counter,3);
|
||||
|
||||
}
|
||||
|
||||
public function testInvoiceClientNumberPattern()
|
||||
{
|
||||
$settings = $this->client->company->settings;
|
||||
|
||||
$settings->invoice_number_pattern = '{$year}-{$client_counter}';
|
||||
$this->client->company->settings = $settings;
|
||||
$this->client->company->save();
|
||||
|
||||
$settings = $this->client->settings;
|
||||
$settings->invoice_number_counter = 10;
|
||||
$this->client->settings = $settings;
|
||||
$this->client->save();
|
||||
|
||||
$this->assertEquals($this->client->settings->invoice_number_counter,10);
|
||||
|
||||
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
||||
|
||||
$this->assertEquals($invoice_number, '2019-10');
|
||||
|
||||
$invoice_number = $this->getNextInvoiceNumber($this->client);
|
||||
$this->assertEquals($invoice_number, '2019-11');
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
public function testPrefixOnlyInvoiceNumber()
|
||||
{
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
public function testClientCounterValue()
|
||||
{
|
||||
$this->assertEquals($this->getCounter($this->client), 1);
|
||||
}
|
||||
public function testClientNextNumber()
|
||||
{
|
||||
$this->assertEquals($this->getNextNumber($this->client),1);
|
||||
}
|
||||
public function testRecurringInvoiceNumberPrefix()
|
||||
{
|
||||
//$this->assertEquals($this->getNextNumber(RecurringInvoice::class), 'R1');
|
||||
$this->assertEquals($this->getCounter($this->client), 1);
|
||||
|
||||
}
|
||||
public function testClientIncrementer()
|
||||
{
|
||||
$this->incrementCounter($this->client);
|
||||
$this->assertEquals($this->getCounter($this->client), 2);
|
||||
}
|
||||
/*
|
||||
public function testCounterValues()
|
||||
{
|
||||
$this->assertEquals($this->getCounter(Invoice::class), 1);
|
||||
$this->assertEquals($this->getCounter(RecurringInvoice::class), 1);
|
||||
$this->assertEquals($this->getCounter(Credit::class), 1);
|
||||
}
|
||||
public function testClassIncrementers()
|
||||
{
|
||||
$this->client->incrementCounter(Invoice::class);
|
||||
$this->client->incrementCounter(RecurringInvoice::class);
|
||||
$this->client->incrementCounter(Credit::class);
|
||||
$this->assertEquals($this->getCounter(Invoice::class), 3);
|
||||
$this->assertEquals($this->getCounter(RecurringInvoice::class), 3);
|
||||
$this->assertEquals($this->getCounter(Credit::class), 2);
|
||||
}
|
||||
|
||||
public function testClientNumberPattern()
|
||||
{
|
||||
$settings = $this->client->getSettingsByKey('client_number_pattern');
|
||||
$settings->client_number_pattern = '{$year}-{$counter}';
|
||||
$this->client->setSettingsByEntity(Client::class, $settings);
|
||||
$company = Company::find($this->client->company_id);
|
||||
$this->assertEquals($company->settings->client_number_counter,1);
|
||||
$this->assertEquals($this->getNextNumber($this->client), '2019-1');
|
||||
$this->assertEquals($this->getNextNumber($this->client), '2019-2');
|
||||
|
||||
$company = Company::find($this->client->company_id);
|
||||
$this->assertEquals($company->settings->client_number_counter,2);
|
||||
$this->assertEquals($this->client->settings->client_number_counter,1);
|
||||
}
|
||||
public function testClientNumberPatternWithDate()
|
||||
{
|
||||
date_default_timezone_set('US/Eastern');
|
||||
$settings = $this->client->getSettingsByKey('client_number_pattern');
|
||||
$settings->client_number_pattern = '{$date:j}-{$counter}';
|
||||
$this->client->setSettingsByEntity(Client::class, $settings);
|
||||
|
||||
$this->assertEquals($this->getNextNumber($this->client), date('j') . '-1');
|
||||
}
|
||||
public function testClientNumberPatternWithDate2()
|
||||
{
|
||||
date_default_timezone_set('US/Eastern');
|
||||
$settings = $this->client->getSettingsByKey('client_number_pattern');
|
||||
$settings->client_number_pattern = '{$date:d M Y}-{$counter}';
|
||||
$this->client->setSettingsByEntity(Client::class, $settings);
|
||||
|
||||
$this->assertEquals($this->getNextNumber($this->client), date('d M Y') . '-1');
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user