Fixes for credit query

This commit is contained in:
David Bomba 2021-07-02 16:36:14 +10:00
parent 3995674dfb
commit e8a1df16cd
7 changed files with 80 additions and 7 deletions

View File

@ -476,8 +476,10 @@ class CompanyController extends BaseController
{ {
$company_count = $company->account->companies->count(); $company_count = $company->account->companies->count();
$account = $company->account; $account = $company->account;
$account_key = $account->key;
if ($company_count == 1) { if ($company_count == 1) {
$company->company_users->each(function ($company_user) { $company->company_users->each(function ($company_user) {
$company_user->user->forceDelete(); $company_user->user->forceDelete();
$company_user->forceDelete(); $company_user->forceDelete();
@ -485,9 +487,13 @@ class CompanyController extends BaseController
$account->delete(); $account->delete();
if(Ninja::isHosted())
\Modules\Admin\Jobs\Account\NinjaDeletedAccount::dispatch($account_key);
LightLogs::create(new AccountDeleted()) LightLogs::create(new AccountDeleted())
->increment() ->increment()
->batch(); ->batch();
} else { } else {
$company_id = $company->id; $company_id = $company->id;

View File

@ -38,6 +38,7 @@ class CreditsTable extends Component
->where('client_id', auth('contact')->user()->client->id) ->where('client_id', auth('contact')->user()->client->id)
->where('status_id', '<>', Credit::STATUS_DRAFT) ->where('status_id', '<>', Credit::STATUS_DRAFT)
->whereDate('due_date', '<=', now()) ->whereDate('due_date', '<=', now())
->orWhere('due_date', NULL)
->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc') ->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc')
->paginate($this->per_page); ->paginate($this->per_page);

View File

@ -215,6 +215,11 @@ class Client extends BaseModel implements HasLocalePreference
return $this->hasMany(Invoice::class)->withTrashed(); return $this->hasMany(Invoice::class)->withTrashed();
} }
public function recurring_invoices()
{
return $this->hasMany(RecurringInvoice::class)->withTrashed();
}
public function shipping_country() public function shipping_country()
{ {
return $this->belongsTo(Country::class, 'shipping_country_id', 'id'); return $this->belongsTo(Country::class, 'shipping_country_id', 'id');

View File

@ -48,21 +48,24 @@ class ClientService
public function getCreditBalance() :float public function getCreditBalance() :float
{ {
$credits = $this->client->credits $credits = $this->client->credits()
->where('is_deleted', false) ->where('is_deleted', false)
->where('balance', '>', 0) ->where('balance', '>', 0)
->where('due_date', '<=', now()) ->whereDate('due_date', '<=', now()->format('Y-m-d'))
->sortBy('created_at'); ->orWhere('due_date', NULL)
->orderBy('created_at','ASC');
return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision); return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision);
} }
public function getCredits() :Collection public function getCredits() :Collection
{ {
return $this->client->credits return $this->client->credits()
->where('is_deleted', false) ->where('is_deleted', false)
->where('balance', '>', 0) ->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) public function getPaymentMethods(float $amount)

View File

@ -12,7 +12,6 @@
namespace Tests\ClientPortal; namespace Tests\ClientPortal;
use App\Http\Livewire\CreditsTable; use App\Http\Livewire\CreditsTable;
use App\Models\Account; use App\Models\Account;
use App\Models\Client; use App\Models\Client;
@ -36,7 +35,7 @@ class CreditsTest extends TestCase
$this->faker = Factory::create(); $this->faker = Factory::create();
} }
public function testShowingOnlyQuotesWithDueDateLessOrEqualToNow() public function testShowingOnlyCreditsWithDueDateLessOrEqualToNow()
{ {
$account = Account::factory()->create(); $account = Account::factory()->create();
@ -88,4 +87,57 @@ class CreditsTest extends TestCase
->assertSee('testing-number-02') ->assertSee('testing-number-02')
->assertDontSee('testing-number-03'); ->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');
}
} }

View File

@ -8,8 +8,10 @@
* *
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace Tests\Feature; namespace Tests\Feature;
use App\Models\CompanyGateway;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\URL;
use Tests\MockAccountData; use Tests\MockAccountData;
@ -45,6 +47,8 @@ class ClientModelTest extends TestCase
$payment_methods = $this->client->service()->getPaymentMethods(40); $payment_methods = $this->client->service()->getPaymentMethods(40);
$this->assertGreaterThan(0, CompanyGateway::count());
$this->assertEquals(1, count($payment_methods)); $this->assertEquals(1, count($payment_methods));
} }

View File

@ -396,6 +396,8 @@ trait MockAccountData
$this->credit->line_items = $this->buildLineItems(); $this->credit->line_items = $this->buildLineItems();
$this->credit->amount = 10; $this->credit->amount = 10;
$this->credit->balance = 10; $this->credit->balance = 10;
// $this->credit->due_date = now()->addDays(200);
$this->credit->tax_name1 = ''; $this->credit->tax_name1 = '';
$this->credit->tax_name2 = ''; $this->credit->tax_name2 = '';