Skip to content

Commit

Permalink
fix: added logic for declarations endpiont
Browse files Browse the repository at this point in the history
  • Loading branch information
rensreinders committed May 28, 2024
1 parent 86a4217 commit 75b91e0
Show file tree
Hide file tree
Showing 13 changed files with 465 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/ApiConnectors/BaseApiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpTwinfield\Response\MappedResponseCollection;
use PhpTwinfield\Response\Response;
use PhpTwinfield\Secure\AuthenticatedConnection;
use PhpTwinfield\Services\DeclarationService;
use PhpTwinfield\Services\FinderService;
use PhpTwinfield\Services\ProcessXmlService;
use PhpTwinfield\Services\SelectOfficeService;
Expand Down Expand Up @@ -182,4 +183,15 @@ protected function getSessionService(): SessionService
{
return $this->connection->getAuthenticatedClient(Services::SESSION());
}


public function getDeclarationService(): DeclarationService
{
return $this->connection->getAuthenticatedClient(Services::DECLARATION());
}

public function getConnection(): AuthenticatedConnection
{
return $this->connection;
}
}
102 changes: 102 additions & 0 deletions src/ApiConnectors/DeclarationsApiConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace PhpTwinfield\ApiConnectors;

use PhpTwinfield\Mappers\DocumentMapper;

class DeclarationsApiConnector extends BaseApiConnector
{
public function setOffice($officeCode): void
{
$headers = $this->getConnection()->getSoapHeaders();

$this->getDeclarationService()->setSoapHeadersFromArray([
...$headers->data,

Check failure on line 14 in src/ApiConnectors/DeclarationsApiConnector.php

View workflow job for this annotation

GitHub Actions / PHP 7.3

syntax error, unexpected '...' (T_ELLIPSIS), expecting ']'
'CompanyCode' => $officeCode,
]);
}

public function declarationCount($officeCode): int
{
$response = $this->getDeclarationService()->declarationCount($officeCode);

return $response->numberOfVatReturns ?? 0;
}

public function vatReturnXbrl($documentId): string|null

Check failure on line 26 in src/ApiConnectors/DeclarationsApiConnector.php

View workflow job for this annotation

GitHub Actions / PHP 7.4

syntax error, unexpected '|', expecting ';' or '{'
{
$response = $this->getDeclarationService()->vatReturnXbrl($documentId);

return $response->vatReturn->any ?? null;
}

public function icpReturnXBrl($documentId): string|null
{
$response = $this->getDeclarationService()->icpReturnXbrl($documentId);

return $response->vatReturn->any ?? null;
}

public function summaries($officeCode): array
{
// this seems redundant, but it is necessary to set the office code
$this->setOffice($officeCode);

$response = $this->getDeclarationService()->summaries($officeCode);

if (!isset($response->vatReturn->DeclarationSummary)) {
return [];
}

$declarations = $response->vatReturn->DeclarationSummary;

// If there are multiple declarations, map them all
if (is_array($declarations)) {
return array_map(function ($declaration) {
return DocumentMapper::map($declaration);
}, $declarations);
}

// With XML, if there is only one declaration, it is not an array
return [DocumentMapper::map($declarations)];
}

public function setApproved($officeCode, $documentId): bool
{
$this->setOffice($officeCode);

$result = $this->getDeclarationService()->setApproved($documentId);

var_dump($result);

// @todo implement proper error / success handling

return false;
}

public function setRejected($officeCode, $documentId, $reason): bool
{
$this->setOffice($officeCode);

$result = $this->getDeclarationService()->setRejected($documentId, $reason);

var_dump($result);

// @todo implement proper error / success handling

return false;
}

public function setSent($officeCode, $documentId): bool
{
$this->setOffice($officeCode);

$result = $this->getDeclarationService()->setSent($documentId);

var_dump($result);

// @todo implement proper error / success handling

return false;
}
}
69 changes: 69 additions & 0 deletions src/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace PhpTwinfield;

class Document {
private $id;
private $documentCode;
private $name;
private $documentTimeFrame;
private $status;
private $assignee;
private $company;

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

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

public function getDocumentCode() {
return $this->documentCode;
}

public function setDocumentCode($documentCode) {
$this->documentCode = $documentCode;
}

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

public function setName($name) {
$this->name = $name;
}

public function getDocumentTimeFrame() {
return $this->documentTimeFrame;
}

public function setDocumentTimeFrame($documentTimeFrame) {
$this->documentTimeFrame = $documentTimeFrame;
}

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

public function setStatus($status) {
$this->status = $status;
}

public function getAssignee() {
return $this->assignee;
}

public function setAssignee($assignee) {
$this->assignee = $assignee;
}

public function getCompany() {
return $this->company;
}

public function setCompany($company) {
$this->company = $company;
}
}
24 changes: 24 additions & 0 deletions src/DocumentAssignee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PhpTwinfield;

class DocumentAssignee {
private $code;
private $name;

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

public function setCode($code) {
$this->code = $code;
}

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

public function setName($name) {
$this->name = $name;
}
}
24 changes: 24 additions & 0 deletions src/DocumentCompany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PhpTwinfield;

class DocumentCompany {
private $code;
private $name;

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

public function setCode($code) {
$this->code = $code;
}

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

public function setName($name) {
$this->name = $name;
}
}
43 changes: 43 additions & 0 deletions src/DocumentStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpTwinfield;

class DocumentStatus {
private $description;
private $stepIndex;
private $extraInformation;


public function getDescription() {
// 1 = Created
// 2 = Modified
// 3 = Sending
// 4 = Error while sending
// 5 = Sent
// 6 = Rejected
// 7 = Approved
// 8 = Authorized

return $this->description;
}

public function setDescription($description) {
$this->description = $description;
}

public function getStepIndex() {
return $this->stepIndex;
}

public function setStepIndex($stepIndex) {
$this->stepIndex = $stepIndex;
}

public function getExtraInformation() {
return $this->extraInformation;
}

public function setExtraInformation($extraInformation) {
$this->extraInformation = $extraInformation;
}
}
24 changes: 24 additions & 0 deletions src/DocumentTimeFrame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PhpTwinfield;

class DocumentTimeFrame {
private $year;
private $period;

public function getYear() {
return $this->year;
}

public function setYear($year) {
$this->year = $year;
}

public function getPeriod() {
return $this->period;
}

public function setPeriod($period) {
$this->period = $period;
}
}
8 changes: 8 additions & 0 deletions src/Enums/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpTwinfield\Enums;

use PhpTwinfield\Services\DeclarationService;
use PhpTwinfield\Services\FinderService;
use PhpTwinfield\Services\ProcessXmlService;
use PhpTwinfield\Services\SessionService;
Expand All @@ -17,6 +18,8 @@
*
* @method static self FINDER()
* @method static self PROCESSXML()
* @method static self SESSION()
* @method static self DECLARATION()
*/
class Services extends \MyCLabs\Enum\Enum
{
Expand All @@ -34,4 +37,9 @@ class Services extends \MyCLabs\Enum\Enum
* The service that selects the current office in Twinfield
*/
protected const SESSION = SessionService::class;

/**
* The service that enables interaction with the command query API.
*/
protected const DECLARATION = DeclarationService::class;
}
43 changes: 43 additions & 0 deletions src/Mappers/DocumentMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpTwinfield\Mappers;

use PhpTwinfield\Document;
use PhpTwinfield\DocumentAssignee;
use PhpTwinfield\DocumentCompany;
use PhpTwinfield\DocumentStatus;
use PhpTwinfield\DocumentTimeFrame;
use stdClass;

class DocumentMapper {
public static function map(stdClass $data) {
$document = new Document();

$document->setId($data->Id);
$document->setDocumentCode($data->DocumentCode);
$document->setName($data->Name);

$documentTimeFrame = new DocumentTimeFrame();
$documentTimeFrame->setYear($data->DocumentTimeFrame->Year);
$documentTimeFrame->setPeriod($data->DocumentTimeFrame->Period);
$document->setDocumentTimeFrame($documentTimeFrame);

$status = new DocumentStatus();
$status->setDescription($data->Status->Description);
$status->setStepIndex($data->Status->StepIndex);
$status->setExtraInformation($data->Status->ExtraInformation);
$document->setStatus($status);

$assignee = new DocumentAssignee();
$assignee->setCode($data->Assignee->Code);
$assignee->setName($data->Assignee->Name);
$document->setAssignee($assignee);

$company = new DocumentCompany();
$company->setCode($data->Company->Code);
$company->setName($data->Company->Name);
$document->setCompany($company);

return $document;
}
}
Loading

0 comments on commit 75b91e0

Please sign in to comment.