Skip to content

Commit 9116022

Browse files
committed
Fix vatNotice tests, refactor and extend revenue tests
1 parent faea6d7 commit 9116022

4 files changed

Lines changed: 91 additions & 35 deletions

File tree

database/factories/RevenueFactory.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,53 @@
22

33
namespace Database\Factories;
44

5+
use App\Models\Revenue;
56
use Illuminate\Database\Eloquent\Factories\Factory;
67

78
/**
89
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Revenue>
910
*/
1011
class RevenueFactory extends Factory
1112
{
13+
protected $model = Revenue::class;
14+
1215
/**
1316
* Define the model's default state.
1417
*
1518
* @return array<string, mixed>
1619
*/
1720
public function definition(): array
1821
{
19-
$net = $this->faker->randomFloat(2, 120, 3000);
20-
$tax = (int) $net * env('DEFAULT_TAX_RATE') / 100;
21-
$gross = $net + $tax;
22-
23-
$billigDate = $this->faker->dateTimeThisYear();
24-
2522
return [
26-
'billing_date' => $billigDate,
27-
'payment_date' => $this->getPaymentDateFromBillingDate($billigDate),
23+
'billing_date' => $this->faker->dateTimeThisYear(),
2824
'company_name' => $this->faker->company,
2925
'invoice_number' => $this->faker->randomNumber(8),
30-
'net' => $net,
31-
'tax' => $tax,
32-
'gross' => $gross,
26+
'net' => $this->faker->randomFloat(2, 120, 3000),
3327
];
3428
}
3529

36-
public function yearsBack($yearsBack)
30+
public function configure()
3731
{
38-
return $this->state(function (array $attributes) use ($yearsBack) {
39-
$revenue = $this->definition();
40-
$revenue['billing_date'] = $this->faker->dateTimeBetween('-'.$yearsBack.' years', '-'.$yearsBack.' years');
41-
$revenue['payment_date'] = $this->getPaymentDateFromBillingDate($revenue['billing_date']);
42-
43-
return $revenue;
32+
return $this->afterMaking(function (Revenue $revenue) {
33+
$rate = config('app.default_tax_rate', env('DEFAULT_TAX_RATE', 19));
34+
35+
$revenue->tax = (int) $revenue->net * $rate / 100;
36+
$revenue->gross = $revenue->net + $revenue->tax;
37+
38+
$revenue->payment_date ??= $this->faker->dateTimeBetween(
39+
$revenue->billing_date,
40+
min(
41+
(clone $revenue->billing_date)->modify('+14 days'),
42+
(clone $revenue->billing_date)->setDate((int) $revenue->billing_date->format('Y'), 12, 31)
43+
)
44+
);
4445
});
4546
}
4647

47-
private function getPaymentDateFromBillingDate($billingDate)
48+
public function yearsBack(int $years): static
4849
{
49-
$maxPaymentDate = (clone $billingDate)->modify('+14 days');
50-
$endOfYear = (clone $billingDate)->setDate($billingDate->format('Y'), 12, 31);
51-
52-
return $this->faker->dateTimeBetween($billingDate, min($maxPaymentDate, $endOfYear));
50+
return $this->state(fn () => [
51+
'billing_date' => $this->faker->dateTimeBetween("-{$years} years", "-{$years} years"),
52+
]);
5353
}
5454
}

resources/views/vat-notice/index.blade.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414
</div>
1515
<div class="p-6 text-gray-900 dark:text-gray-100">
1616
<h1>Umsatzsteuer {{$year}}</h1>
17-
<div class="total_amounts flex flex-row text-center my-4">
18-
<div>
17+
<div class="total_amounts flex flex-row flex-wrap text-center my-4">
18+
<div class="font-semibold w-1/2">
1919
<p>Zu meldende steuerpflichtige Umsätze</p>
2020
<span>{{Number::currency($remainingNetRevenue, in: 'EUR', locale: 'de')}}</span>
2121
</div>
22-
<div>
22+
<div class="opacity-30 w-1/2 mb-4">
2323
<p>Gezahlte Steuern Gesamt</p>
2424
<span>{{Number::currency($totalExpenseTax, in: 'EUR', locale: 'de')}}</span>
2525
</div>
26-
</div>
27-
<div class="total_amounts flex flex-row text-center">
28-
<div>
26+
<div class="font-semibold w-1/2">
2927
<p>Zu meldende Steuereinnahmen</p>
3028
<span>{{Number::currency($remainingRevenueTax, in: 'EUR', locale: 'de')}}</span>
3129
</div>
32-
<div>
30+
<div class="font-semibold w-1/2">
3331
<p>Zu meldende Steuerzahlungen</p>
3432
<span>{{Number::currency($remainingExpenseTax, in: 'EUR', locale: 'de')}}</span>
3533
</div>

tests/Feature/RevenueTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44

55
use App\Models\Revenue;
66
use App\Models\User;
7+
use Database\Seeders\CostTypeSeeder;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use Illuminate\Support\Number;
710
use Tests\TestCase;
811

912
class RevenueTest extends TestCase
1013
{
14+
use RefreshDatabase;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
$this->seed(CostTypeSeeder::class);
20+
}
21+
1122
public function test_create_revenue_page_is_loaded(): void
1223
{
1324
$user = User::factory()->create();
@@ -80,4 +91,28 @@ public function test_deleting_revenue_is_working(): void
8091
$user->delete();
8192
$revenue->delete();
8293
}
94+
95+
public function test_overview_page_shows_correct_total_for_multiple_revenues(): void
96+
{
97+
$user = User::factory()->createOne();
98+
99+
$revenues = collect([
100+
Revenue::factory()->create(['net' => 112.38]),
101+
Revenue::factory()->create(['net' => 238.47]),
102+
Revenue::factory()->create(['net' => 323.99]),
103+
]);
104+
105+
$response = $this->actingAs($user)
106+
->get('');
107+
108+
$totalNet = $revenues->sum('net');
109+
$totalTax = $revenues->sum('tax');
110+
$totalGross = $revenues->sum('gross');
111+
112+
$formatEuro = fn ($value) => Number::currency($value, in: 'EUR', locale: 'de');
113+
114+
$response->assertSee($formatEuro($totalNet));
115+
$response->assertSee($formatEuro($totalTax));
116+
$response->assertSee($formatEuro($totalGross));
117+
}
83118
}

tests/Feature/VatNoticeTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@
66
use App\Models\Revenue;
77
use App\Models\User;
88
use App\Models\VatNotice;
9+
use Database\Seeders\CostTypeSeeder;
10+
use Illuminate\Foundation\Testing\RefreshDatabase;
911
use Illuminate\Support\Number;
1012
use Tests\TestCase;
1113

1214
class VatNoticeTest extends TestCase
1315
{
16+
use RefreshDatabase;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
$this->seed(CostTypeSeeder::class);
22+
}
23+
1424
public function test_vat_notice_page_is_loaded(): void
1525
{
1626
$user = User::factory()->create();
1727

1828
$vatNoticePage = $this->actingAs($user)
19-
->get('/vat-notice');
29+
->get('/vat-notice/');
2030

2131
$vatNoticePage->assertSeeInOrder([
2232
'Zu meldende steuerpflichtige Umsätze',
@@ -30,18 +40,29 @@ public function test_vat_notice_page_is_loaded(): void
3040
public function test_vat_notice_page_calcs_total_tax(): void
3141
{
3242
$user = User::factory()->createOne();
33-
$rev1 = Revenue::factory()->create();
34-
$rev2 = Revenue::factory()->create();
43+
$rev1 = Revenue::factory()->create(['net' => 5200]);
44+
$rev2 = Revenue::factory()->create(['net' => 3200]);
3545
$exp1 = Expense::factory()->create();
3646
$exp2 = Expense::factory()->create();
47+
$vatNotice1 = VatNotice::factory()->create();
48+
$vatNotice2 = VatNotice::factory()->create();
49+
3750
$totalReceivedTax = $rev1->tax + $rev2->tax;
51+
$remainingRevenueTax = $totalReceivedTax - $vatNotice1->vat_received - $vatNotice2->vat_received;
52+
53+
$remainingNetRevenueRounded = round($remainingRevenueTax * 100 / 19, 0); // Elster only let you use non decimal numbers for the net revenue
54+
$remainingRevenueTaxRounded = $remainingNetRevenueRounded * 19 / 100;
55+
3856
$totalPaidTax = $exp1->tax + $exp2->tax;
57+
$paidTaxToReport = $totalPaidTax - $vatNotice1->vat_paid - $vatNotice2->vat_paid;
3958

4059
$vatNoticePage = $this->actingAs($user)
41-
->get('/vat-notice');
60+
->get('/vat-notice/');
4261

43-
$vatNoticePage->assertSee(Number::currency($totalReceivedTax, in: 'EUR', locale: 'de'));
62+
$vatNoticePage->assertSee(Number::currency($remainingNetRevenueRounded, in: 'EUR', locale: 'de'));
63+
$vatNoticePage->assertSee(Number::currency($remainingRevenueTaxRounded, in: 'EUR', locale: 'de'));
4464
$vatNoticePage->assertSee(Number::currency($totalPaidTax, in: 'EUR', locale: 'de'));
65+
$vatNoticePage->assertSee(Number::currency($paidTaxToReport, in: 'EUR', locale: 'de'));
4566
$vatNoticePage->assertStatus(200);
4667

4768
$rev1->delete();
@@ -92,6 +113,7 @@ public function test_vat_notice_page_shows_notices(): void
92113
$vatNoticePage->assertSee(Number::currency($vatNotice->vat_paid, in: 'EUR', locale: 'de'));
93114
$vatNoticePage->assertStatus(200);
94115

116+
$user->delete();
95117
$vatNotice->delete();
96118
}
97119

@@ -105,6 +127,7 @@ public function test_create_vat_notice_page_is_loaded(): void
105127
$vatNoticePage->assertSeeInOrder([
106128
'Meldedatum',
107129
]);
130+
$user->delete();
108131
$vatNoticePage->assertStatus(200);
109132
}
110133

0 commit comments

Comments
 (0)