Skip to content

Added Currency and Currency rate support #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,21 @@ $connector = new BrowseDataApiConnector(
Not all resources from the Twinfield API are currently implemented. Feel free to create a pull request when you need
support for another resource.

| Component | get() | listAll() | send() | delete() | Mapper |
| --------------------------------------------------------------------------------------------------------------- | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: |
| [Articles](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Articles) | :white_check_mark: | | :white_check_mark: | | :white_check_mark: |
| [BankTransaction](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Transactions/BankTransactions)| | | :white_check_mark: | :white_check_mark: | |
| [Customer](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Customers) | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: |
| [Electronic Bank Statements](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Transactions/BankStatements)| | | :white_check_mark: | | |
| [Sales Invoices](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/SalesInvoices) | :white_check_mark: | | :white_check_mark: | | :white_check_mark: |
| [Matching](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Miscellaneous/Matching) | | | :white_check_mark: | | :white_check_mark: | |
| [Offices](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Offices) | | :white_check_mark: | | | :white_check_mark: |
| [Suppliers](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Suppliers) | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: |
| Component | get() | listAll() | send() | delete() | Mapper |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| :----------------: | :----------------: | :----------------: | :----------------: | :----------------: |
| [Articles](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Articles) | :white_check_mark: | | :white_check_mark: | | :white_check_mark: |
| [BankTransaction](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Transactions/BankTransactions) | | | :white_check_mark: | :white_check_mark: | |
| [Currency](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Currencies) | | | :white_check_mark: | | :white_check_mark: |
| [Customer](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Customers) | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: |
| [Electronic Bank Statements](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Transactions/BankStatements) | | | :white_check_mark: | | |
| [Sales Invoices](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/SalesInvoices) | :white_check_mark: | | :white_check_mark: | | :white_check_mark: |
| [Matching](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Miscellaneous/Matching) | | | :white_check_mark: | | :white_check_mark: | |
| [Offices](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Offices) | | :white_check_mark: | | | :white_check_mark: |
| [Suppliers](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Suppliers) | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: |
| Transactions:<br> [Purchase](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/PurchaseTransactions), [Sale](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/SalesTransactions), [Journal](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Transactions/JournalTransactions), [Cash](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Transactions/CashTransactions) | :white_check_mark: | | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| [Users](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Users) | | :white_check_mark: | | | |
| [Vat types](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/VAT) | | :white_check_mark: | | | |
| [Browse Data](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Request/BrowseData) | :white_check_mark: | | | | :white_check_mark: |
| [Users](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Users) | | :white_check_mark: | | | |
| [Vat types](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/VAT) | | :white_check_mark: | | | |
| [Browse Data](https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Request/BrowseData) | :white_check_mark: | | | | :white_check_mark: |

## Links

Expand Down
65 changes: 65 additions & 0 deletions src/ApiConnectors/CurrencyApiConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace PhpTwinfield\ApiConnectors;

use PhpTwinfield\Currency;
use PhpTwinfield\DomDocuments\CurrenciesDocument;
use PhpTwinfield\Exception;
use PhpTwinfield\Mappers\CurrencyMapper;
use PhpTwinfield\Response\MappedResponseCollection;
use PhpTwinfield\Response\Response;
use Webmozart\Assert\Assert;

/**
* A facade to make interaction with the the Twinfield service easier when trying to retrieve or send information about
* Currencies.
*
* If you require more complex interactions or a heavier amount of control over the requests to/from then look inside
* the methods or see the advanced guide detailing the required usages.
*
* This class is created by copying and changing the ArticleApiConnector class
*
* @author Marc van de Geijn <[email protected]>
*/
class CurrencyApiConnector extends BaseApiConnector
{
/**
* Sends an Currency instance to Twinfield to update or add.
*
* @param Currency $currency
* @return Currency
* @throws Exception
*/
public function send(Currency $currency): Currency
{
return $this->unwrapSingleResponse($this->sendAll([$currency]));
}

/**
* @param Currency[] $currencies
* @return MappedResponseCollection
* @throws Exception
*/
public function sendAll(array $currencies): MappedResponseCollection
{
Assert::allIsInstanceOf($currencies, Currency::class);

/** @var Response[] $responses */
$responses = [];

foreach ($this->getProcessXmlService()->chunk($currencies) as $chunk) {

$currenciesDocument = new CurrenciesDocument();

foreach ($chunk as $currency) {
$currenciesDocument->addCurrency($currency);
}

$responses[] = $this->sendXmlDocument($currenciesDocument);
}

return $this->getProcessXmlService()->mapAll($responses, "currency", function(Response $response): Currency {
return CurrencyMapper::map($response);
});
}
}
89 changes: 89 additions & 0 deletions src/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace PhpTwinfield;

use PhpTwinfield\Transactions\TransactionFields\OfficeField;

/**
* @see https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Currencies
* @todo Add documentation and typehints to all properties.
*/
class Currency
{
use OfficeField;

private $code;
private $name;
private $shortName;
private $rates = [];

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

public function setOfficeByCode(string $code): self
{
$office = new Office();
$office->setCode($code);

$this->setOffice( $office );
return $this;
}

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

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

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

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

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

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

public function getRates()
{
return $this->rates;
}

public function addRate(CurrencyRate $rate)
{
$this->rates[] = $rate;
return $this;
}

public function removeRate(CurrencyRate $rate)
{
$index = array_search($rate, $this->rates);

if ($index !== false) {
unset($this->rates[$index]);
return true;
} else {
return false;
}
}
}
47 changes: 47 additions & 0 deletions src/CurrencyRate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace PhpTwinfield;

/**
* @see https://accounting.twinfield.com/webservices/documentation/#/ApiReference/Masters/Currencies
* @todo Add documentation and typehints to all properties.
*/
class CurrencyRate
{
private $status;
private $startdate;
private $rate;

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

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

public function getStartdate()
{
return $this->startdate;
}

public function setStartdate($startdate)
{
$this->startdate = $startdate;
return $this;
}

public function getRate()
{
return $this->rate;
}

public function setRate($rate)
{
$this->rate = $rate;
return $this;
}
}
Loading
Loading