Skip to content

Commit

Permalink
Merge pull request #138 from Sysix/pursue-vouchers
Browse files Browse the repository at this point in the history
Pursue vouchers
  • Loading branch information
Sysix committed Dec 22, 2023
2 parents 4d21659 + 335f3e9 commit ff887e7
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 18 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'multiline_whitespace_before_semicolons' => true,
'native_type_declaration_casing' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
'ordered_traits' => true,
'no_alias_functions' => true,
'no_alias_language_construct_call' => true,
'no_alternative_syntax' => true,
Expand Down
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,34 @@ You need an [API Key](https://app.lexoffice.de/addons/public-api) for that.
### Basic
```php
// store keys in .env file
$apiKey = getenv('LEX_OFFICE_API_KEY');
$apiKey = getenv('LEXOFFICE_API_KEY');

// in this example we are using guzzlehttp/guzzle package, it can be any PSR-18 HTTP Client
// see: https://packagist.org/providers/psr/http-client-implementation
$httpClient = \GuzzleHttp\Client();
$api = new \Sysix\LexOffice\Api($apiKey, $httpClient);
```

#### Optimize your HTTP Client
### Optimize your HTTP Client

This library only prepares the `\Psr\Http\Message\RequestInterface` for the HTTP Client and returns its Response.
There are almost no error checks, no caching and no rate limiting. Your PSR-18 HTTP Client should come with a way to deal with it.

Here is a example with `guzzlehttp/guzzle` , `kevinrob/guzzle-cache-middleware` and `spatie/guzzle-rate-limiter-middleware`:

```php
$apiKey = getenv('LEX_OFFICE_API_KEY');
$apiKey = getenv('LEXOFFICE_API_KEY');

$stack = \GuzzleHttp\HandlerStack();
$stack->push(new \Kevinrob\GuzzleCache\CacheMiddleware\CacheMiddleware(), 'cache');
$stack->push(\Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware\RateLimiterMiddleware::perSecond(2));

$httpClient = \GuzzleHttp\Client(['handler' => $stack]);
$api = new \Sysix\LexOffice\Api($apiKey, $httpClient);

```

## Endpoints

### Contact Endpoint
```php

Expand Down Expand Up @@ -85,21 +86,22 @@ $voucherList = $api->creditNote()->getVoucherListClient(); // see VoucherlistCli
$response = $api->creditNote()->get($entityId);
$response = $api->creditNote()->create($data);
$response = $api->creditNote()->create($data, true); // finalized
$response = $api->creditNote()->pursue($precedingSalesVoucherId, $data);
$response = $api->creditNote()->pursue($precedingSalesVoucherId, $data, true); // finalized
$response = $api->creditNote()->document($entityId); // get document ID
$response = $api->creditNote()->document($entityId, true); // get file content
$response = $api->creditNote()->document($entityId, true, 'image/*'); // accept only images
$response = $api->creditNote()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
```

### Deliverys Notes Endpoint
```php
$voucherList = $api->deliveryNote()->getVoucherListClient(); // see VoucherlistClient Documentation
$response = $api->deliveryNote()->get($entityId);
$response = $api->deliveryNote()->create($data);
$response = $api->deliveryNote()->pursue($precedingSalesVoucherId, $data);
$response = $api->deliveryNote()->document($entityId); // get document ID
$response = $api->deliveryNote()->document($entityId, true); // get file content
$response = $api->deliveryNote()->document($entityId, true, 'image/*'); // accept only images
$response = $api->deliveryNote()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
```

### Down Payment Invoices Endpoint
Expand All @@ -110,7 +112,6 @@ $response = $api->downPaymentInvoice()->create($data);
$response = $api->downPaymentInvoice()->document($entityId); // get document ID
$response = $api->downPaymentInvoice()->document($entityId, true); // get file content
$response = $api->downPaymentInvoice()->document($entityId, true, 'image/*'); // accept only images
$response = $api->downPaymentInvoice()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
```

### Event Subscriptions Endpooint
Expand All @@ -135,6 +136,8 @@ $voucherList = $api->invoice()->getVoucherListClient(); // see VoucherlistClient
$response = $api->invoice()->get($entityId);
$response = $api->invoice()->create($data);
$response = $api->invoice()->create($data, true); // finalized
$response = $api->invoice()->pursue($precedingSalesVoucherId, $data);
$response = $api->invoice()->pursue($precedingSalesVoucherId, $data, true); // finalized
$response = $api->invoice()->document($entityId); // get document ID
$response = $api->invoice()->document($entityId, true); // get file content
$response = $api->invoice()->document($entityId, true, 'image/*'); // accept only images
Expand All @@ -146,10 +149,10 @@ $response = $api->invoice()->document($entityId, true, 'application/xml'); // ge
$voucherList = $api->orderConfirmation()->getVoucherListClient(); // see VoucherlistClient Documentation
$response = $api->orderConfirmation()->get($entityId);
$response = $api->orderConfirmation()->create($data);
$response = $api->orderConfirmation()->pursue($precedingSalesVoucherId, $data);
$response = $api->orderConfirmation()->document($entityId); // get document ID
$response = $api->orderConfirmation()->document($entityId, true); // get file content
$response = $api->orderConfirmation()->document($entityId, true, 'image/*'); // accept only images
$response = $api->orderConfirmation()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
```

### Payment Endpoint
Expand Down Expand Up @@ -181,7 +184,6 @@ $response = $api->quotation()->create($data, true); // finalized
$response = $api->quotation()->document($entityId); // get document ID
$response = $api->quotation()->document($entityId, true); // get file content
$response = $api->quotation()->document($entityId, true, 'image/*'); // accept only images
$response = $api->quotation()->document($entityId, true, 'application/xml'); // get XRechung XML File (if possible)
```

### Recurring Templates Endpoint
Expand Down Expand Up @@ -258,6 +260,8 @@ $client->updatedDateTo = new \DateTime('2023-12-01');
$response = $client->getPage(0);
```

## Utils

### get JSON from Success and Error Response

```php
Expand Down
6 changes: 4 additions & 2 deletions src/Clients/CreditNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use Sysix\LexOffice\Clients\Traits\CreateFinalizeTrait;
use Sysix\LexOffice\Clients\Traits\DocumentClientTrait;
use Sysix\LexOffice\Clients\Traits\GetTrait;
use Sysix\LexOffice\Clients\Traits\PursueFinalizeTrait;
use Sysix\LexOffice\Clients\Traits\VoucherListTrait;

class CreditNote extends BaseClient
{
use GetTrait;
use CreateFinalizeTrait;
use VoucherListTrait;
use DocumentClientTrait;
use GetTrait;
use PursueFinalizeTrait;
use VoucherListTrait;

protected string $resource = 'credit-notes';

Expand Down
6 changes: 4 additions & 2 deletions src/Clients/DeliveryNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use Sysix\LexOffice\Clients\Traits\CreateTrait;
use Sysix\LexOffice\Clients\Traits\DocumentClientTrait;
use Sysix\LexOffice\Clients\Traits\GetTrait;
use Sysix\LexOffice\Clients\Traits\PursueTrait;
use Sysix\LexOffice\Clients\Traits\VoucherListTrait;

class DeliveryNote extends BaseClient
{
use GetTrait;
use CreateTrait;
use VoucherListTrait;
use DocumentClientTrait;
use GetTrait;
use PursueTrait;
use VoucherListTrait;

protected string $resource = 'delivery-notes';

Expand Down
2 changes: 1 addition & 1 deletion src/Clients/DownPaymentInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

class DownPaymentInvoice extends BaseClient
{
use DocumentClientTrait;
use GetTrait;
use VoucherListTrait;
use DocumentClientTrait;

protected string $resource = 'down-payment-invoices';

Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
class Event extends BaseClient
{
use CreateTrait;
use GetTrait;
use DeleteTrait;
use GetTrait;

protected string $resource = 'event-subscriptions';

Expand Down
4 changes: 3 additions & 1 deletion src/Clients/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use Sysix\LexOffice\Clients\Traits\CreateFinalizeTrait;
use Sysix\LexOffice\Clients\Traits\DocumentClientTrait;
use Sysix\LexOffice\Clients\Traits\GetTrait;
use Sysix\LexOffice\Clients\Traits\PursueFinalizeTrait;
use Sysix\LexOffice\Clients\Traits\VoucherListTrait;

class Invoice extends BaseClient
{
use CreateFinalizeTrait;
use DocumentClientTrait;
use GetTrait;
use PursueFinalizeTrait;
use VoucherListTrait;
use DocumentClientTrait;

protected string $resource = 'invoices';

Expand Down
4 changes: 3 additions & 1 deletion src/Clients/OrderConfirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use Sysix\LexOffice\Clients\Traits\CreateTrait;
use Sysix\LexOffice\Clients\Traits\DocumentClientTrait;
use Sysix\LexOffice\Clients\Traits\GetTrait;
use Sysix\LexOffice\Clients\Traits\PursueTrait;
use Sysix\LexOffice\Clients\Traits\VoucherListTrait;

class OrderConfirmation extends BaseClient
{
use CreateTrait;
use DocumentClientTrait;
use GetTrait;
use PursueTrait;
use VoucherListTrait;
use DocumentClientTrait;

protected string $resource = 'order-confirmations';

Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Quotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
class Quotation extends BaseClient
{
use CreateFinalizeTrait;
use DocumentClientTrait;
use GetTrait;
use VoucherListTrait;
use DocumentClientTrait;

protected string $resource = 'quotations';

Expand Down
23 changes: 23 additions & 0 deletions src/Clients/Traits/PursueFinalizeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sysix\LexOffice\Clients\Traits;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Utils;

trait PursueFinalizeTrait
{
/**
* @param mixed[] $data
*/
public function pursue(string $precedingSalesVoucherId, array $data, bool $finalized = false): ResponseInterface
{
$api = $this->api->newRequest('POST', $this->resource . '?precedingSalesVoucherId=' . rawurlencode($precedingSalesVoucherId) . ($finalized ? '&finalize=true' : ''));

$api->setRequest($api->getRequest()->withBody(Utils::createStream($data)));

return $api->getResponse();
}
}
23 changes: 23 additions & 0 deletions src/Clients/Traits/PursueTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sysix\LexOffice\Clients\Traits;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Utils;

trait PursueTrait
{
/**
* @param mixed[] $data
*/
public function pursue(string $precedingSalesVoucherId, array $data): ResponseInterface
{
$api = $this->api->newRequest('POST', $this->resource . '?precedingSalesVoucherId=' . rawurlencode($precedingSalesVoucherId));

$api->setRequest($api->getRequest()->withBody(Utils::createStream($data)));

return $api->getResponse();
}
}
34 changes: 34 additions & 0 deletions tests/Clients/CreditNoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,40 @@ public function testCreateFinalized(): void
);
}

public function testPursue(): void
{
[$api, $stub] = $this->createClientMockObject(CreditNote::class);

$response = $stub->pursue('resource-id', [
'version' => 0
]);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('POST', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/credit-notes?precedingSalesVoucherId=resource-id',
$api->getRequest()->getUri()->__toString()
);
}

public function testPursueinalized(): void
{
[$api, $stub] = $this->createClientMockObject(CreditNote::class);

$response = $stub->pursue('resource-id', [
'version' => 0
], true);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('POST', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/credit-notes?precedingSalesVoucherId=resource-id&finalize=true',
$api->getRequest()->getUri()->__toString()
);
}

public function testGetPage(): void
{
$this->expectDeprecationV1Warning('getPage');
Expand Down
17 changes: 17 additions & 0 deletions tests/Clients/DeliveryNoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ public function testCreate(): void
);
}

public function testPursue(): void
{
[$api, $stub] = $this->createClientMockObject(DeliveryNote::class);

$response = $stub->pursue('resource-id', [
'version' => 0
]);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('POST', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/delivery-notes?precedingSalesVoucherId=resource-id',
$api->getRequest()->getUri()->__toString()
);
}

public function testGetPage(): void
{
$this->expectDeprecationV1Warning('getPage');
Expand Down
34 changes: 34 additions & 0 deletions tests/Clients/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ public function testCreateFinalized(): void
);
}

public function testPursue(): void
{
[$api, $stub] = $this->createClientMockObject(Invoice::class);

$response = $stub->pursue('resource-id', [
'version' => 0
]);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('POST', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/invoices?precedingSalesVoucherId=resource-id',
$api->getRequest()->getUri()->__toString()
);
}

public function testPursueinalized(): void
{
[$api, $stub] = $this->createClientMockObject(Invoice::class);

$response = $stub->pursue('resource-id', [
'version' => 0
], true);

$this->assertInstanceOf(ResponseInterface::class, $response);

$this->assertEquals('POST', $api->getRequest()->getMethod());
$this->assertEquals(
$api->apiUrl . '/v1/invoices?precedingSalesVoucherId=resource-id&finalize=true',
$api->getRequest()->getUri()->__toString()
);
}

public function testGet(): void
{
[$api, $stub] = $this->createClientMockObject(Invoice::class);
Expand Down
Loading

0 comments on commit ff887e7

Please sign in to comment.