1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Merge pull request #9103 from turbo124/v5-develop

Reset counters at client and group levels
This commit is contained in:
David Bomba 2024-01-05 18:43:11 +11:00 committed by GitHub
commit b7d6cf8c34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 157 additions and 34 deletions

View File

@ -28,7 +28,8 @@ class CloneQuoteToInvoiceFactory
unset($quote_array['invoice_id']);
unset($quote_array['id']);
unset($quote_array['invitations']);
unset($quote_array['user']);
//preserve terms if they exist on Quotes
//if(array_key_exists('terms', $quote_array) && strlen($quote_array['terms']) < 2)
if (! $quote->company->use_quote_terms_on_conversion) {
@ -38,7 +39,6 @@ class CloneQuoteToInvoiceFactory
// unset($quote_array['public_notes']);
unset($quote_array['footer']);
unset($quote_array['design_id']);
unset($quote_array['user']);
foreach ($quote_array as $key => $value) {
$invoice->{$key} = $value;
@ -59,6 +59,7 @@ class CloneQuoteToInvoiceFactory
$invoice->last_sent_date = null;
$invoice->last_viewed = null;
return $invoice;
}
}

View File

@ -115,7 +115,7 @@ class SwissQrGenerator
} else {
$tempInvoiceNumber = $this->invoice->number;
$tempInvoiceNumber = preg_replace('/[^A-Za-z0-9]/', '', $tempInvoiceNumber);
$tempInvoiceNumber = substr($tempInvoiceNumber, 1);
// $tempInvoiceNumber = substr($tempInvoiceNumber, 1);
$calcInvoiceNumber = "";
$array = str_split($tempInvoiceNumber);

View File

@ -97,11 +97,7 @@ class BaseModel extends Model
public function dateMutator($value)
{
if (! empty($value)) {
return (new Carbon($value))->format('Y-m-d');
}
return $value;
return (new Carbon($value))->format('Y-m-d');
}
// public function __call($method, $params)

View File

@ -545,14 +545,17 @@ class Client extends BaseModel implements HasLocalePreference
$cg = CompanyGateway::query()->find($pm['company_gateway_id']);
if ($cg && ! property_exists($cg->fees_and_limits, strval(GatewayType::CREDIT_CARD))) {
if($cg->gateway_key == '80af24a6a691230bbec33e930ab40666') //ensure we don't attempt to authorize paypal platform - yet.
continue;
if ($cg && is_object($cg->fees_and_limits) && ! property_exists($cg->fees_and_limits, strval(GatewayType::CREDIT_CARD))) {
$fees_and_limits = $cg->fees_and_limits;
$fees_and_limits->{GatewayType::CREDIT_CARD} = new FeesAndLimits;
$cg->fees_and_limits = $fees_and_limits;
$cg->save();
}
if ($cg && $cg->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
if ($cg && is_object($cg->fees_and_limits)&& $cg->fees_and_limits->{GatewayType::CREDIT_CARD}->is_enabled) {
return $cg;
}
}

View File

@ -240,12 +240,12 @@ class Invoice extends BaseModel
public function getDueDateAttribute($value)
{
return $this->dateMutator($value);
return $value ? $this->dateMutator($value) : null;
}
public function getPartialDueDateAttribute($value)
{
return $this->dateMutator($value);
return $value ? $this->dateMutator($value) : null;
}
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo

View File

@ -462,7 +462,7 @@ class PdfMock
'$viewLink' => '<a class="button" href="http://ninja.test:8000/client/invoice/UAUY8vIPuno72igmXbbpldwo5BDDKIqs">View Invoice</a>',
'$autoBill' => 'This invoice will automatically be billed to your credit card on file on the due date.',
'$view_url' => 'http://ninja.test:8000/client/invoice/UAUY8vIPuno72igmXbbpldwo5BDDKIqs',
'$font_url' => 'https://fonts.googleapis.com/css2?family=Roboto&display=swap',
'$font_url' => isset($this->settings?->primary_font) ? \App\Utils\Helpers::resolveFont($this->settings->primary_font)['url'] : 'https://fonts.googleapis.com/css2?family=Roboto&display=swap',
'$details' => '',
'$balance' => '$40.00',
'$partial' => '$30.00',

View File

@ -132,7 +132,7 @@ class InvoiceTransformer extends EntityTransformer
'is_amount_discount' => (bool) ($invoice->is_amount_discount ?: false),
'footer' => $invoice->footer ?: '',
'partial' => (float) ($invoice->partial ?: 0.0),
'partial_due_date' => $invoice->partial_due_date ?: '',
'partial_due_date' => ($invoice->partial_due_date && $invoice->partial_due_date != "-0001-11-30") ?: '',
'custom_value1' => (string) $invoice->custom_value1 ?: '',
'custom_value2' => (string) $invoice->custom_value2 ?: '',
'custom_value3' => (string) $invoice->custom_value3 ?: '',

View File

@ -510,13 +510,17 @@ trait GeneratesCounter
private function resetCounters(Client $client)
{
$reset_counter_frequency = (int) $client->getSetting('reset_counter_frequency_id');
$settings_entity = $client->getSettingEntity('reset_counter_frequency_id');
$settings = $settings_entity->settings;
if ($reset_counter_frequency == 0) {
if ($client->getSetting('reset_counter_date')) {
$settings = $client->company->settings;
// $settings = $client->company->settings;
$settings->reset_counter_date = "";
$client->company->settings = $settings;
$client->company->save();
$settings_entity->settings = $settings;
$settings_entity->saveQuietly();
// $client->company->settings = $settings;
// $client->company->save();
}
return;
@ -570,7 +574,7 @@ trait GeneratesCounter
break;
}
$settings = $client->company->settings;
// $settings = $client->company->settings;
$settings->reset_counter_date = $new_reset_date->format('Y-m-d');
$settings->invoice_number_counter = 1;
$settings->quote_number_counter = 1;
@ -583,8 +587,10 @@ trait GeneratesCounter
$settings->recurring_expense_number_counter = 1;
$settings->purchase_order_number_counter = 1;
$client->company->settings = $settings;
$client->company->save();
// $client->company->settings = $settings;
// $client->company->save();
$settings_entity->settings = $settings;
$settings_entity->saveQuietly();
}
private function resetCompanyCounters($company)

View File

@ -397,6 +397,7 @@ class EventTest extends TestCase
];
$quote = Quote::find($this->decodePrimaryKey($arr['data']['id']));
$quote->due_date = now()->addYear();
$quote->status_id = Quote::STATUS_SENT;
$quote->save();

View File

@ -11,23 +11,24 @@
namespace Tests\Unit;
use App\DataMapper\ClientSettings;
use Tests\TestCase;
use App\Models\Quote;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Timezone;
use Tests\MockAccountData;
use App\Models\GroupSetting;
use App\Factory\ClientFactory;
use App\Factory\VendorFactory;
use App\Models\Client;
use App\Models\Company;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Models\Timezone;
use App\Utils\Traits\GeneratesCounter;
use App\Utils\Traits\MakesHash;
use App\Models\RecurringInvoice;
use App\DataMapper\ClientSettings;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -53,6 +54,121 @@ class GeneratesCounterTest extends TestCase
$this->makeTestData();
}
public function testResetCounterGroup()
{
$timezone = Timezone::find(1);
$date_formatted = now($timezone->name)->format('Ymd');
$gs = new GroupSetting;
$gs->name = 'Test';
$gs->company_id = $this->client->company_id;
$gs->settings = ClientSettings::buildClientSettings($this->company->settings, $this->client->settings);
$gs->save();
$this->client->group_settings_id = $gs->id;
$this->client->save();
$settings = $gs->settings;
// $settings = $this->client->settings;
$settings->invoice_number_pattern = '{$date:Ymd}-{$group_counter}';
$settings->timezone_id = 1;
$gs->settings = $settings;
$gs->save();
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0001', $invoice_number);
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0002', $invoice_number);
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0003', $invoice_number);
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0004', $invoice_number);
$settings->reset_counter_date = now($timezone->name)->format('Y-m-d');
$settings->reset_counter_frequency_id = RecurringInvoice::FREQUENCY_DAILY;
$gs->settings = $settings;
$gs->save();
$this->travel(5)->days();
$date_formatted = now($timezone->name)->format('Ymd');
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0001', $invoice_number);
$this->invoice->number = $invoice_number;
$this->invoice->save();
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0002', $invoice_number);
$settings->reset_counter_date = now($timezone->name)->format('Y-m-d');
$settings->reset_counter_frequency_id = RecurringInvoice::FREQUENCY_DAILY;
$gs->settings = $settings;
$gs->save();
$this->travel(5)->days();
$date_formatted = now($timezone->name)->format('Ymd');
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0001', $invoice_number);
$this->travelBack();
}
public function testResetCounterClient()
{
$timezone = Timezone::find(1);
$date_formatted = now($timezone->name)->format('Ymd');
$settings = $this->client->settings;
$settings->invoice_number_pattern = '{$date:Ymd}-{$client_counter}';
$settings->timezone_id = 1;
$this->client->settings = $settings;
$this->client->save();
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0001', $invoice_number);
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0002', $invoice_number);
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0003', $invoice_number);
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0004', $invoice_number);
$settings->reset_counter_date = now($timezone->name)->format('Y-m-d');
$settings->reset_counter_frequency_id = RecurringInvoice::FREQUENCY_DAILY;
$this->client->settings = $settings;
$this->client->save();
$this->travel(5)->days();
$date_formatted = now($timezone->name)->format('Ymd');
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0001', $invoice_number);
$this->invoice->number = $invoice_number;
$this->invoice->save();
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0002', $invoice_number);
$settings->reset_counter_date = now($timezone->name)->format('Y-m-d');
$settings->reset_counter_frequency_id = RecurringInvoice::FREQUENCY_DAILY;
$this->client->settings = $settings;
$this->client->save();
$this->travel(5)->days();
$date_formatted = now($timezone->name)->format('Ymd');
$invoice_number = $this->getNextInvoiceNumber($this->client->fresh(), $this->invoice->fresh());
$this->assertEquals($date_formatted.'-0001', $invoice_number);
$this->travelBack();
}
public function testResetCounter()
{
$timezone = Timezone::find(1);
@ -82,8 +198,8 @@ class GeneratesCounterTest extends TestCase
$this->company->settings = $settings;
$this->company->save();
$this->client->settings = $settings;
$this->client->save();
// $this->client->settings = $settings;
// $this->client->save();
$this->travel(5)->days();
$date_formatted = now($timezone->name)->format('Ymd');