Skip to content

Commit 523afc6

Browse files
committed
Add VAT rate retrieval logic and enhance test coverage
Introduces a method to fetch the current active VAT rate based on date and activity status, improving the functionality of VAT rate management. Enhances test coverage by adding unit tests for the new method and updating existing tests to ensure robustness. Refines handling of relation data for better consistency. Allows configuration to support unsupported PHP versions for php-cs-fixer, improving compatibility in development environments.
1 parent 2b4f4bb commit 523afc6

File tree

5 files changed

+125
-25
lines changed

5 files changed

+125
-25
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
'php_unit_test_case_static_method_calls' => false,
9090
]));
9191

92-
$config = Factory::fromRuleSet($ruleSet);
92+
$config = Factory::fromRuleSet($ruleSet)->setUnsupportedPhpVersionAllowed(true);
9393

9494
$config->getFinder()
9595
->append([

src/AbraFlexi/RO.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,10 @@ public function fixRecordTypes(array $record, $evidence = null)
10951095
\array_key_exists($subject.'@ref', $value) ? $record[$subject.'@ref'] : $value['id'],
10961096
\array_key_exists($subject.'@showAs', $value) ? $value[$subject.'@showAs'] : null,
10971097
);
1098+
1099+
foreach ($value as $a => $b) {
1100+
$record[$column][$a] = $b;
1101+
}
10981102
} else {
10991103
$record[$column] = new Relation(
11001104
$value,

src/AbraFlexi/SazbaDph.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,41 @@
2020
class SazbaDph extends RW
2121
{
2222
public ?string $evidence = 'sazba-dph';
23+
24+
/**
25+
* Returns the current active VAT rate for today.
26+
*
27+
* This method queries the 'sazba-dph' evidence for an active rate
28+
* where the current date is between 'platnostOd' and 'platnostDo'.
29+
*
30+
* @throws \RuntimeException if the evidence query fails
31+
*
32+
* @return null|float the current VAT rate percentage, or null if not found
33+
*/
34+
public function getCurrent(): ?float
35+
{
36+
$today = date('Y-m-d');
37+
$queryParams = [
38+
'platnostOd:lte' => $today,
39+
'platnostDo:gte' => $today,
40+
'aktivni' => true,
41+
'limit' => 1,
42+
];
43+
44+
try {
45+
$result = $this->getColumns(['procento'], $queryParams);
46+
47+
if (isset($result[0]['procento'])) {
48+
return (float) $result[0]['procento'];
49+
}
50+
} catch (\Exception $exception) {
51+
throw new \RuntimeException(
52+
_('Failed to fetch current VAT rate: ').$exception->getMessage(),
53+
0,
54+
$exception,
55+
);
56+
}
57+
58+
return null;
59+
}
2360
}

tests/src/AbraFlexi/RelationTest.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ class RelationTest extends \PHPUnit\Framework\TestCase
2323
{
2424
protected Relation $object;
2525

26+
/**
27+
* Sets up the fixture, for example, opens a network connection.
28+
* This method is called before a test is executed.
29+
*/
2630
/**
2731
* Sets up the fixture, for example, opens a network connection.
2832
* This method is called before a test is executed.
2933
*/
3034
protected function setUp(): void
3135
{
32-
$this->object = new Relation();
36+
// Create a Relation object with required arguments for testing.
37+
$this->object = new Relation('TEST_CODE', 'faktura-vydana');
3338
}
3439

3540
/**
@@ -40,27 +45,27 @@ protected function tearDown(): void
4045
{
4146
}
4247

43-
// /**
44-
// * @covers \AbraFlexi\Relation::__toString
45-
// *
46-
// * @todo Implement test__toString().
47-
// */
48-
// public function testToString(): void
49-
// {
50-
// $this->assertEquals('', $this->object->__toString());
51-
// // Remove the following lines when you implement this test.
52-
// $this->markTestIncomplete('This test has not been implemented yet.');
53-
// }
54-
//
55-
// /**
56-
// * @covers \AbraFlexi\Relation::getRelationTarget
57-
// *
58-
// * @todo Implement testgetRelationTarget().
59-
// */
60-
// public function testgetRelationTarget(): void
61-
// {
62-
// $this->assertEquals('', $this->object->getRelationTarget());
63-
// // Remove the following lines when you implement this test.
64-
// $this->markTestIncomplete('This test has not been implemented yet.');
65-
// }
48+
/**
49+
* @covers \AbraFlexi\Relation::__toString
50+
*/
51+
public function testToString(): void
52+
{
53+
$relation = new Relation('CODE123', 'faktura-vydana');
54+
$this->assertSame('CODE123', (string) $relation);
55+
56+
$relationArr = new Relation(['kod' => 'KOD456'], 'faktura-vydana');
57+
$this->assertSame('KOD456', (string) $relationArr);
58+
}
59+
60+
/**
61+
* @covers \AbraFlexi\Relation::getRelationTarget
62+
*/
63+
public function testgetRelationTarget(): void
64+
{
65+
$relation = new Relation('CODE123', 'faktura-vydana');
66+
$target = $relation->getRelationTarget();
67+
$this->assertInstanceOf(\AbraFlexi\RO::class, $target);
68+
// Assert that the record code matches the expected value.
69+
$this->assertEquals('CODE123', $target->getRecordCode());
70+
}
6671
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the SpojeNet\AbraFlexi package.
7+
*
8+
* (c) 2019-2024 SpojeNet s.r.o. <http://spoje.net/>
9+
* (c) 2025 SpojeNetIT s.r.o. <http://spojenet.cz/>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Test\AbraFlexi;
16+
17+
use AbraFlexi\SazbaDph;
18+
19+
/**
20+
* Generated by PHPUnit_SkeletonGenerator on 2025-09-10 at 20:21:14.
21+
*/
22+
class SazbaDphTest extends \PHPUnit\Framework\TestCase
23+
{
24+
protected SazbaDph $object;
25+
26+
/**
27+
* Sets up the fixture, for example, opens a network connection.
28+
* This method is called before a test is executed.
29+
*/
30+
protected function setUp(): void
31+
{
32+
$this->object = new SazbaDph();
33+
}
34+
35+
/**
36+
* Tears down the fixture, for example, closes a network connection.
37+
* This method is called after a test is executed.
38+
*/
39+
protected function tearDown(): void
40+
{
41+
}
42+
43+
/**
44+
* @covers \AbraFlexi\SazbaDph::getCurrent
45+
*/
46+
public function testgetCurrent(): void
47+
{
48+
$result = $this->object->getCurrent();
49+
$this->assertTrue(
50+
\is_float($result) || null === $result,
51+
'getCurrent() should return float (VAT rate) or null if not found.',
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)