Fix MemberPaymentsBlock

This commit is contained in:
Philipp Lang 2023-12-17 01:49:12 +01:00
parent 0f80844d20
commit bf8f6c87a8
7 changed files with 61 additions and 50 deletions

View File

@ -4,10 +4,10 @@ namespace App\Dashboard;
use App\Dashboard\Blocks\Block;
use App\Efz\EfzPendingBlock;
use App\Invoice\MemberPaymentBlock;
use App\Member\PsPendingBlock;
use App\Membership\AgeGroupCountBlock;
use App\Membership\TestersBlock;
use App\Payment\MemberPaymentBlock;
class DashboardFactory
{

View File

@ -1,8 +1,9 @@
<?php
namespace App\Payment;
namespace App\Invoice;
use App\Dashboard\Blocks\Block;
use App\Invoice\Models\InvoicePosition;
use App\Member\Member;
class MemberPaymentBlock extends Block
@ -12,17 +13,15 @@ class MemberPaymentBlock extends Block
*/
public function data(): array
{
$amount = Payment::whereNeedsPayment()
->selectRaw('sum(subscription_children.amount) AS nr')
->join('subscriptions', 'subscriptions.id', 'payments.subscription_id')
->join('subscription_children', 'subscriptions.id', 'subscription_children.parent_id')
$amount = InvoicePosition::whereHas('invoice', fn ($query) => $query->whereNeedsPayment())
->selectRaw('sum(price) AS price')
->first();
$members = Member::whereHasPendingPayment()->count();
return [
'members' => $members,
'total_members' => Member::count(),
'amount' => number_format((int) $amount->nr / 100, 2, ',', '.').' €',
'amount' => number_format((int) $amount->price / 100, 2, ',', '.') . ' €',
];
}

View File

@ -5,6 +5,7 @@ namespace App\Invoice\Models;
use App\Invoice\BillKind;
use App\Invoice\Enums\InvoiceStatus;
use App\Member\Member;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -55,4 +56,14 @@ class Invoice extends Model
$model->positions()->delete();
});
}
/**
* @param Builder<self> $query
*
* @return Builder<self>
*/
public function scopeWhereNeedsPayment(Builder $query): Builder
{
return $query->whereIn('status', [InvoiceStatus::NEW->value, InvoiceStatus::SENT->value]);
}
}

View File

@ -342,9 +342,7 @@ class Member extends Model implements Geolocatable
*/
public function scopeWhereHasPendingPayment(Builder $query): Builder
{
return $query->whereHas('payments', function (Builder $q): void {
$q->whereNeedsPayment();
});
return $query->whereHas('invoicePositions', fn ($q) => $q->whereHas('invoice', fn ($q) => $q->whereNeedsPayment()));
}
/**

View File

@ -22,7 +22,6 @@ class InvoicePositionFactory extends Factory
{
return [
'description' => $this->faker->words(4, true),
'member_id' => Member::factory()->defaults()->create()->id,
'price' => $this->faker->numberBetween(1000, 2000),
];
}
@ -31,4 +30,9 @@ class InvoicePositionFactory extends Factory
{
return $this->state(['price' => $price]);
}
public function withMember(): self
{
return $this->state(['member_id' => Member::factory()->defaults()->create()->id]);
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Tests\Feature\Payment;
use App\Invoice\Enums\InvoiceStatus;
use App\Invoice\MemberPaymentBlock;
use App\Invoice\Models\Invoice;
use App\Invoice\Models\InvoicePosition;
use App\Member\Member;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class MemberPaymentBlockTest extends TestCase
{
use DatabaseTransactions;
public function testItHasData(): void
{
$this->login()->loginNami();
$member = Member::factory()->defaults()->create();
Member::factory()->defaults()->create();
Invoice::factory()
->has(InvoicePosition::factory()->price(3500)->for($member), 'positions')
->has(InvoicePosition::factory()->price(1000)->for($member), 'positions')
->status(InvoiceStatus::SENT)->create();
Invoice::factory()->has(InvoicePosition::factory()->price(600)->for($member), 'positions')->status(InvoiceStatus::NEW)->create();
Invoice::factory()->has(InvoicePosition::factory()->price(1000)->for($member), 'positions')->status(InvoiceStatus::PAID)->create();
$data = app(MemberPaymentBlock::class)->render()['data'];
$this->assertEquals([
'amount' => '51,00 €',
'members' => 1,
'total_members' => 2,
], $data);
}
}

View File

@ -1,39 +0,0 @@
<?php
namespace Tests\Feature\Payment;
use App\Member\Member;
use App\Payment\MemberPaymentBlock;
use App\Payment\Payment;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\RequestFactories\Child;
use Tests\TestCase;
class MemberPaymentsBlockTest extends TestCase
{
use DatabaseTransactions;
public function testItHasData(): void
{
$this->login()->loginNami();
Member::factory()
->defaults()
->has(Payment::factory()->notPaid()->subscription('example', [
new Child('gg', 3400),
new Child('gg', 100),
]))
->create();
Member::factory()
->defaults()
->create();
$data = app(MemberPaymentBlock::class)->render()['data'];
$this->assertEquals([
'amount' => '35,00 €',
'members' => 1,
'total_members' => 2,
], $data);
}
}