fixes for endless recursion

This commit is contained in:
David Bomba 2021-04-10 12:01:36 +10:00
parent 5279888d6d
commit 1696c63ba4
2 changed files with 54 additions and 2 deletions

View File

@ -46,7 +46,7 @@ trait GeneratesCounter
$counter_string = $this->getEntityCounter($entity, $client); $counter_string = $this->getEntityCounter($entity, $client);
$pattern = $this->getNumberPattern($entity, $client); $pattern = $this->getNumberPattern($entity, $client);
if (strpos($pattern, 'clientCounter') || strpos($pattern, 'client_counter')) { if ((strpos($pattern, 'clientCounter') !== false) || (strpos($pattern, 'client_counter') !==false) ) {
if (property_exists($client->settings, $counter_string)) { if (property_exists($client->settings, $counter_string)) {
$counter = $client->settings->{$counter_string}; $counter = $client->settings->{$counter_string};
@ -55,7 +55,7 @@ trait GeneratesCounter
} }
$counter_entity = $client; $counter_entity = $client;
} elseif (strpos($pattern, 'groupCounter') || strpos($pattern, 'group_counter')) { } elseif ((strpos($pattern, 'groupCounter') !== false) || (strpos($pattern, 'group_counter') !== false)) {
if (property_exists($client->group_settings, $counter_string)) { if (property_exists($client->group_settings, $counter_string)) {
$counter = $client->group_settings->{$counter_string}; $counter = $client->group_settings->{$counter_string};
@ -73,6 +73,10 @@ trait GeneratesCounter
//If it is a quote - we need to //If it is a quote - we need to
$pattern = $this->getNumberPattern($entity, $client); $pattern = $this->getNumberPattern($entity, $client);
if(strlen($pattern) > 1 && (stripos($pattern, 'counter') === false)){
$pattern = $pattern.'{$counter}';
}
$padding = $client->getSetting('counter_padding'); $padding = $client->getSetting('counter_padding');
if($is_recurring) if($is_recurring)
@ -333,6 +337,7 @@ trait GeneratesCounter
*/ */
private function checkEntityNumber($class, $entity, $counter, $padding, $pattern, $prefix = '') private function checkEntityNumber($class, $entity, $counter, $padding, $pattern, $prefix = '')
{ {
$check = false; $check = false;
do { do {
@ -347,6 +352,11 @@ trait GeneratesCounter
$counter++; $counter++;
} while ($check); } while ($check);
nlog($counter);
nlog($pattern);
nlog($number);
return $number; return $number;
} }
@ -482,6 +492,9 @@ trait GeneratesCounter
} }
switch ($company->reset_counter_frequency_id) { switch ($company->reset_counter_frequency_id) {
case RecurringInvoice::FREQUENCY_DAILY:
$reset_date->addDay();
break;
case RecurringInvoice::FREQUENCY_WEEKLY: case RecurringInvoice::FREQUENCY_WEEKLY:
$reset_date->addWeek(); $reset_date->addWeek();
break; break;

View File

@ -73,6 +73,45 @@ class GeneratesCounterTest extends TestCase
$this->assertTrue($this->hasSharedCounter($this->client)); $this->assertTrue($this->hasSharedCounter($this->client));
} }
public function testNoCounterBeingSpecifiedInCounterStringStub()
{
$settings = $this->client->company->settings;
$settings->invoice_number_counter = 1;
$settings->invoice_number_pattern = 'test-{$counter}';
$settings->shared_invoice_quote_counter = 1;
$this->client->company->settings = $settings;
$this->client->company->save();
$this->client->settings = $settings;
$this->client->save();
$this->client->fresh();
$invoice_number = $this->getNextInvoiceNumber($this->client, $this->invoice);
$this->assertEquals('test-0001', $invoice_number);
}
public function testNoCounterBeingSpecifiedInCounterStringWithFix()
{
$settings = $this->client->company->settings;
$settings->invoice_number_counter = 100;
$settings->invoice_number_pattern = 'test-';
$settings->shared_invoice_quote_counter = 100;
$this->client->company->settings = $settings;
$this->client->company->save();
$this->client->settings = $settings;
$this->client->save();
$this->client->fresh();
$invoice_number = $this->getNextInvoiceNumber($this->client, $this->invoice);
$this->assertEquals('test-0100', $invoice_number);
}
public function testInvoiceNumberValue() public function testInvoiceNumberValue()
{ {
$invoice_number = $this->getNextInvoiceNumber($this->client, $this->invoice); $invoice_number = $this->getNextInvoiceNumber($this->client, $this->invoice);