-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from rojtjo/feature/vat-code-detail
Adding ability to fetch VAT code details
- Loading branch information
Showing
8 changed files
with
614 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.