Skip to content

Commit

Permalink
Merge pull request #198 from rojtjo/feature/vat-code-detail
Browse files Browse the repository at this point in the history
Adding ability to fetch VAT code details
  • Loading branch information
Willem Stuursma-Ruwen authored Aug 25, 2021
2 parents f66edaf + 5c34043 commit 99a9a8c
Show file tree
Hide file tree
Showing 8 changed files with 614 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace PhpTwinfield;

use PhpTwinfield\Enums\LineType;

final class Account
{
private $id;
private $dim1;
private $groupCountry;
private $group;
private $percentage;
private $lineType;

public function getId(): string
{
return $this->id;
}

public function setId($id): self
{
$this->id = $id;
return $this;
}

public function getDim1(): string
{
return $this->dim1;
}

public function setDim1(string $dim1): self
{
$this->dim1 = $dim1;
return $this;
}

public function getGroupCountry(): string
{
return $this->groupCountry;
}

public function setGroupCountry(string $groupCountry): self
{
$this->groupCountry = $groupCountry;
return $this;
}

public function getGroup(): string
{
return $this->group;
}

public function setGroup(string $group): self
{
$this->group = $group;
return $this;
}

public function getPercentage(): float
{
return $this->percentage;
}

public function setPercentage(float $percentage): self
{
$this->percentage = $percentage;
return $this;
}

public function getLineType(): LineType
{
return $this->lineType;
}

public function setLineType(LineType $lineType): self
{
$this->lineType = $lineType;
return $this;
}
}
13 changes: 13 additions & 0 deletions src/ApiConnectors/VatCodeApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace PhpTwinfield\ApiConnectors;

use PhpTwinfield\Mappers\VatMapper;
use PhpTwinfield\Office;
use PhpTwinfield\Services\FinderService;
use PhpTwinfield\VatCodeDetail;
use PhpTwinfield\VatCode;

/**
Expand Down Expand Up @@ -54,4 +57,14 @@ public function listAll(

return $vatCodes;
}

public function get(string $code, Office $office): VatCodeDetail
{
$response = $this->sendXmlDocument(new \PhpTwinfield\Request\Read\VatCode(
$office,
$code,
));

return VatMapper::map($response);
}
}
85 changes: 85 additions & 0 deletions src/Mappers/VatMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace PhpTwinfield\Mappers;

use PhpTwinfield\Account;
use PhpTwinfield\Enums\LineType;
use PhpTwinfield\Response\Response;
use PhpTwinfield\VatCodeDetail;
use PhpTwinfield\VatCodePercentage;

final class VatMapper extends BaseMapper
{
/**
* Maps a Response object to a clean Vat entity.
*
* @access public
* @param \PhpTwinfield\Response\Response $response
* @return VatCodeDetail
*/
public static function map(Response $response): VatCodeDetail
{
$vatCodeDetail = new VatCodeDetail();

// Gets the raw DOMDocument response.
$responseDOM = $response->getResponseDocument();

// Set the status attribute
$vatElement = $responseDOM->getElementsByTagName('vat')->item(0);
$vatCodeDetail->setStatus($vatElement->getAttribute('status'));

// Vat elements and their methods
$vatTags = [
'code' => 'setCode',
'name' => 'setName',
'shortname' => 'setShortName',
'uid' => 'setUID',
'created' => 'setCreatedFromString',
'modified' => 'setModifiedFromString',
'touched' => 'setTouched',
'type' => 'setType',
'user' => 'setUser',
];

// Loop through all the tags
foreach ($vatTags as $tag => $method) {
self::setFromTagValue($responseDOM, $tag, [$vatCodeDetail, $method]);
}

$xpath = new \DOMXPath($responseDOM);
$percentages = [];
foreach ($xpath->query('/vat/percentages/percentage', $vatElement) as $percentageNode) {
$percentages[] = $percentage = new VatCodePercentage();

$percentage
->setStatus($percentageNode->getAttribute('status'))
->setInUse($percentageNode->getAttribute('inuse') === 'true')
->setDateFromString(self::getField($percentageNode, 'date'))
->setPercentage((float) self::getField($percentageNode, 'percentage'))
->setCreatedFromString(self::getField($percentageNode, 'created'))
->setName(self::getField($percentageNode, 'name'))
->setShortname(self::getField($percentageNode, 'shortname'))
->setUser(self::getField($percentageNode, 'user'));

$accounts = [];
foreach ($xpath->query('accounts/account', $percentageNode) as $accountNode) {
$accounts[] = $account = new Account();

$account
->setId($accountNode->getAttribute('id'))
->setDim1(self::getField($accountNode, 'dim1'))
->setGroupCountry(self::getField($accountNode, 'groupcountry'))
->setGroup(self::getField($accountNode, 'group'))
->setPercentage((float) self::getField($accountNode, 'percentage'))
->setLineType(new LineType(self::getField($accountNode, 'linetype')));
}

$percentage->setAccounts($accounts);
}
$vatCodeDetail->setPercentages($percentages);

return $vatCodeDetail;
}
}
18 changes: 18 additions & 0 deletions src/Request/Read/VatCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace PhpTwinfield\Request\Read;

final class VatCode extends Read
{
public function __construct(string $office, string $code)
{
parent::__construct();

$this->add('type', 'vat');

$this->add('office', $office);
$this->add('code', $code);
}
}
151 changes: 151 additions & 0 deletions src/VatCodeDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);

namespace PhpTwinfield;

use PhpTwinfield\Transactions\TransactionFields\OfficeField;
use Webmozart\Assert\Assert;

final class VatCodeDetail
{
use OfficeField;

private $status;
private $code;
private $name;
private $shortName;
private $uid;
private $created;
private $modified;
private $touched;
private $type;
private $user;
private $percentages = [];

public function getStatus(): string
{
return $this->status;
}

public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}

public function getCode(): string
{
return $this->code;
}

public function setCode(string $code): self
{
$this->code = $code;
return $this;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;
return $this;
}

public function getShortName(): string
{
return $this->shortName;
}

public function setShortName(string $shortName): self
{
$this->shortName = $shortName;
return $this;
}

public function getUID(): string
{
return $this->uid;
}

public function setUID(string $uid): self
{
$this->uid = $uid;
return $this;
}

public function getCreated(): \DateTimeInterface
{
return $this->created;
}

public function setCreatedFromString(string $dateStr): self
{
$this->created = Util::parseDateTime($dateStr);
return $this;
}

public function getModified(): \DateTimeInterface
{
return $this->created;
}

public function setModifiedFromString(string $dateStr): self
{
$this->modified = Util::parseDateTime($dateStr);
return $this;
}

public function getTouched(): int
{
return $this->touched;
}

public function setTouched(int $touched): self
{
$this->touched = $touched;
return $this;
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): self
{
$this->type = $type;
return $this;
}

public function getUser(): string
{
return $this->user;
}

public function setUser(string $user): self
{
$this->user = $user;
return $this;
}

/**
* @return VatCodePercentage[]
*/
public function getPercentages(): array
{
return $this->percentages;
}

public function setPercentages(array $percentages): self
{
Assert::allIsInstanceOf($percentages, VatCodePercentage::class);

$this->percentages = $percentages;
return $this;
}
}
Loading

0 comments on commit 99a9a8c

Please sign in to comment.