diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 842fb03abd3b..b85793ffe940 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -476,8 +476,10 @@ class CompanyController extends BaseController { $company_count = $company->account->companies->count(); $account = $company->account; + $account_key = $account->key; if ($company_count == 1) { + $company->company_users->each(function ($company_user) { $company_user->user->forceDelete(); $company_user->forceDelete(); @@ -485,9 +487,13 @@ class CompanyController extends BaseController $account->delete(); + if(Ninja::isHosted()) + \Modules\Admin\Jobs\Account\NinjaDeletedAccount::dispatch($account_key); + LightLogs::create(new AccountDeleted()) ->increment() ->batch(); + } else { $company_id = $company->id; diff --git a/app/Http/Livewire/CreditsTable.php b/app/Http/Livewire/CreditsTable.php index 97ac843fa8f4..7b69eb33c995 100644 --- a/app/Http/Livewire/CreditsTable.php +++ b/app/Http/Livewire/CreditsTable.php @@ -38,6 +38,7 @@ class CreditsTable extends Component ->where('client_id', auth('contact')->user()->client->id) ->where('status_id', '<>', Credit::STATUS_DRAFT) ->whereDate('due_date', '<=', now()) + ->orWhere('due_date', NULL) ->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc') ->paginate($this->per_page); diff --git a/app/Models/Client.php b/app/Models/Client.php index dbc42b4e6e97..46b538a8f1ff 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -215,6 +215,11 @@ class Client extends BaseModel implements HasLocalePreference return $this->hasMany(Invoice::class)->withTrashed(); } + public function recurring_invoices() + { + return $this->hasMany(RecurringInvoice::class)->withTrashed(); + } + public function shipping_country() { return $this->belongsTo(Country::class, 'shipping_country_id', 'id'); diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 9289a53604f1..a1cf2b316e2a 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -48,21 +48,24 @@ class ClientService public function getCreditBalance() :float { - $credits = $this->client->credits + $credits = $this->client->credits() ->where('is_deleted', false) ->where('balance', '>', 0) - ->where('due_date', '<=', now()) - ->sortBy('created_at'); + ->whereDate('due_date', '<=', now()->format('Y-m-d')) + ->orWhere('due_date', NULL) + ->orderBy('created_at','ASC'); return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision); } public function getCredits() :Collection { - return $this->client->credits + return $this->client->credits() ->where('is_deleted', false) ->where('balance', '>', 0) - ->sortBy('created_at'); + ->whereDate('due_date', '<=', now()->format('Y-m-d')) + ->orWhere('due_date', NULL) + ->orderBy('created_at','ASC'); } public function getPaymentMethods(float $amount) diff --git a/tests/ClientPortal/CreditsTest.php b/tests/ClientPortal/CreditsTest.php index 16ab0923a8b2..129c811bc34c 100644 --- a/tests/ClientPortal/CreditsTest.php +++ b/tests/ClientPortal/CreditsTest.php @@ -12,7 +12,6 @@ namespace Tests\ClientPortal; - use App\Http\Livewire\CreditsTable; use App\Models\Account; use App\Models\Client; @@ -36,7 +35,7 @@ class CreditsTest extends TestCase $this->faker = Factory::create(); } - public function testShowingOnlyQuotesWithDueDateLessOrEqualToNow() + public function testShowingOnlyCreditsWithDueDateLessOrEqualToNow() { $account = Account::factory()->create(); @@ -88,4 +87,57 @@ class CreditsTest extends TestCase ->assertSee('testing-number-02') ->assertDontSee('testing-number-03'); } + + public function testShowingCreditsWithNullDueDate() + { + $account = Account::factory()->create(); + + $user = User::factory()->create( + ['account_id' => $account->id, 'email' => $this->faker->safeEmail] + ); + + $company = Company::factory()->create(['account_id' => $account->id]); + + $client = Client::factory()->create(['company_id' => $company->id, 'user_id' => $user->id]); + + ClientContact::factory()->count(2)->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + ]); + + Credit::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'number' => 'testing-number-01', + 'status_id' => Credit::STATUS_SENT, + ]); + + Credit::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'number' => 'testing-number-02', + 'due_date' => now(), + 'status_id' => Credit::STATUS_SENT, + ]); + + Credit::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'number' => 'testing-number-03', + 'due_date' => now()->addDays(5), + 'status_id' => Credit::STATUS_SENT, + ]); + + $this->actingAs($client->contacts->first(), 'contact'); + + Livewire::test(CreditsTable::class, ['company' => $company]) + ->assertSee('testing-number-01') + ->assertSee('testing-number-02') + ->assertDontSee('testing-number-03'); + } + } diff --git a/tests/Feature/ClientModelTest.php b/tests/Feature/ClientModelTest.php index a1c36a608d08..7fe728ae69f7 100644 --- a/tests/Feature/ClientModelTest.php +++ b/tests/Feature/ClientModelTest.php @@ -8,8 +8,10 @@ * * @license https://opensource.org/licenses/AAL */ + namespace Tests\Feature; +use App\Models\CompanyGateway; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\URL; use Tests\MockAccountData; @@ -45,6 +47,8 @@ class ClientModelTest extends TestCase $payment_methods = $this->client->service()->getPaymentMethods(40); + $this->assertGreaterThan(0, CompanyGateway::count()); + $this->assertEquals(1, count($payment_methods)); } diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 9cfec53b1c4f..f4dae156a0c1 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -396,6 +396,8 @@ trait MockAccountData $this->credit->line_items = $this->buildLineItems(); $this->credit->amount = 10; $this->credit->balance = 10; + + // $this->credit->due_date = now()->addDays(200); $this->credit->tax_name1 = ''; $this->credit->tax_name2 = '';