Skip to content

Commit 872a1c0

Browse files
authored
Merge pull request #16 from codebar-ag/feature-new-requests
2 parents 8c775d1 + 6f12c47 commit 872a1c0

14 files changed

+413
-1
lines changed

.phpunit.cache/test-results

+1-1
Large diffs are not rendered by default.

src/Dto/Invoices/InvoiceDTO.php

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct(
4949
public ?string $template_slug,
5050
public Collection $taxs,
5151
public ?string $network_link,
52+
public ?Collection $positions,
5253
) {}
5354

5455
public static function fromResponse(Response $response): self
@@ -107,6 +108,7 @@ public static function fromArray(array $data): self
107108
template_slug: Arr::get($data, 'template_slug'),
108109
taxs: collect(Arr::get($data, 'taxs', []))->map(fn (array $tax) => InvoiceTaxDTO::fromArray($tax)),
109110
network_link: Arr::get($data, 'network_link'),
111+
positions: collect(Arr::get($data, 'positions', []))->map(fn (array $tax) => InvoicePositionDTO::fromArray($tax)),
110112
);
111113
}
112114
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\Invoices;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
class InvoicePositionDTO extends Data
11+
{
12+
public function __construct(
13+
public ?string $type,
14+
public ?int $id,
15+
public ?string $amount,
16+
public ?int $unit_id,
17+
public ?string $unit_name,
18+
public ?int $account_id,
19+
public ?int $tax_id,
20+
public ?string $tax_value,
21+
public ?string $text,
22+
public ?string $unit_price,
23+
public ?string $discount_in_percent,
24+
public ?string $position_total,
25+
public ?int $parent_id,
26+
public ?int $article_id,
27+
public ?bool $show_pos_nr,
28+
public ?bool $pagebreak,
29+
public ?bool $is_percentual,
30+
public ?string $value,
31+
public ?string $pos,
32+
public ?string $internal_pos,
33+
public ?bool $is_optional,
34+
) {}
35+
36+
public static function fromResponse(Response $response): self
37+
{
38+
if ($response->failed()) {
39+
throw new \Exception('Failed to create DTO from Response');
40+
}
41+
42+
$data = $response->json();
43+
44+
return self::fromArray($data);
45+
}
46+
47+
public static function fromArray(array $data): self
48+
{
49+
if (! $data) {
50+
throw new Exception('Unable to create DTO. Data missing from response.');
51+
}
52+
53+
return new self(
54+
type: Arr::get($data, 'type'),
55+
id: Arr::get($data, 'id'),
56+
amount: Arr::get($data, 'amount'),
57+
unit_id: Arr::get($data, 'unit_id'),
58+
unit_name: Arr::get($data, 'unit_name'),
59+
account_id: Arr::get($data, 'account_id'),
60+
tax_id: Arr::get($data, 'tax_id'),
61+
tax_value: Arr::get($data, 'tax_value'),
62+
text: Arr::get($data, 'text'),
63+
unit_price: Arr::get($data, 'unit_price'),
64+
discount_in_percent: Arr::get($data, 'discount_in_percent'),
65+
position_total: Arr::get($data, 'position_total'),
66+
parent_id: Arr::get($data, 'parent_id'),
67+
article_id: Arr::get($data, 'article_id'),
68+
show_pos_nr: Arr::get($data, 'show_pos_nr'),
69+
pagebreak: Arr::get($data, 'pagebreak'),
70+
is_percentual: Arr::get($data, 'is_percentual'),
71+
value: Arr::get($data, 'value'),
72+
pos: Arr::get($data, 'pos'),
73+
internal_pos: Arr::get($data, 'internal_pos'),
74+
is_optional: Arr::get($data, 'is_optional'),
75+
);
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class CancelAnInvoiceRequest extends Request
11+
{
12+
protected Method $method = Method::POST;
13+
14+
public function __construct(
15+
readonly int $invoiceId,
16+
) {}
17+
18+
public function resolveEndpoint(): string
19+
{
20+
return '/2.0/kb_invoice/'.$this->invoiceId.'/cancel';
21+
}
22+
23+
/**
24+
* @throws \JsonException
25+
*/
26+
public function createDtoFromResponse(Response $response): mixed
27+
{
28+
if (! $response->successful()) {
29+
throw new Exception('Request was not successful. Unable to create DTO.');
30+
}
31+
32+
return $response->json();
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices;
4+
5+
use CodebarAg\Bexio\Dto\Invoices\InvoiceDTO;
6+
use Exception;
7+
use Illuminate\Support\Collection;
8+
use Saloon\Contracts\Body\HasBody;
9+
use Saloon\Enums\Method;
10+
use Saloon\Http\Request;
11+
use Saloon\Http\Response;
12+
use Saloon\Traits\Body\HasJsonBody;
13+
14+
class CreateAnInvoiceRequest extends Request implements HasBody
15+
{
16+
use HasJsonBody;
17+
18+
protected Method $method = Method::POST;
19+
20+
public function __construct() {}
21+
22+
public function resolveEndpoint(): string
23+
{
24+
return '/2.0/kb_invoice';
25+
}
26+
27+
public function createDtoFromResponse(Response $response): InvoiceDTO
28+
{
29+
if (! $response->successful()) {
30+
throw new Exception('Request was not successful. Unable to create DTO.');
31+
}
32+
33+
$res = $response->json();
34+
35+
return InvoiceDTO::fromArray($res);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices\DefaultPositions;
4+
5+
use CodebarAg\Bexio\Dto\Invoices\InvoicePositionDTO;
6+
use Exception;
7+
use Saloon\Contracts\Body\HasBody;
8+
use Saloon\Enums\Method;
9+
use Saloon\Http\Request;
10+
use Saloon\Http\Response;
11+
use Saloon\Traits\Body\HasJsonBody;
12+
13+
class CreateADefaultPositionRequest extends Request implements HasBody
14+
{
15+
use HasJsonBody;
16+
17+
protected Method $method = Method::POST;
18+
19+
public function __construct(
20+
protected string $kb_document_type,
21+
protected int $invoice_id,
22+
) {}
23+
24+
public function resolveEndpoint(): string
25+
{
26+
return sprintf('/2.0/%s/%s/kb_position_custom', $this->kb_document_type, $this->invoice_id);
27+
}
28+
29+
public function createDtoFromResponse(Response $response): InvoicePositionDTO
30+
{
31+
if (! $response->successful()) {
32+
throw new Exception('Request was not successful. Unable to create DTO.');
33+
}
34+
35+
$res = $response->json();
36+
37+
return InvoicePositionDTO::fromArray($res);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class DeleteAnInvoiceRequest extends Request
11+
{
12+
protected Method $method = Method::DELETE;
13+
14+
public function __construct(
15+
readonly int $invoiceId,
16+
) {}
17+
18+
public function resolveEndpoint(): string
19+
{
20+
return '/2.0/kb_invoice/'.$this->invoiceId;
21+
}
22+
23+
/**
24+
* @throws \JsonException
25+
*/
26+
public function createDtoFromResponse(Response $response): mixed
27+
{
28+
if (! $response->successful()) {
29+
throw new Exception('Request was not successful. Unable to create DTO.');
30+
}
31+
32+
return $response->json();
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class IssueAnInvoiceRequest extends Request
11+
{
12+
protected Method $method = Method::POST;
13+
14+
public function __construct(
15+
readonly int $invoiceId,
16+
) {}
17+
18+
public function resolveEndpoint(): string
19+
{
20+
return '/2.0/kb_invoice/'.$this->invoiceId.'/issue';
21+
}
22+
23+
public function createDtoFromResponse(Response $response): mixed
24+
{
25+
if (! $response->successful()) {
26+
throw new Exception('Request was not successful. Unable to create DTO.');
27+
}
28+
29+
return $response->json();
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class SetIssuedInvoiceToDraftRequest extends Request
11+
{
12+
protected Method $method = Method::POST;
13+
14+
public function __construct(
15+
readonly int $invoiceId,
16+
) {}
17+
18+
public function resolveEndpoint(): string
19+
{
20+
return '/2.0/kb_invoice/'.$this->invoiceId.'/issue';
21+
}
22+
23+
public function createDtoFromResponse(Response $response): mixed
24+
{
25+
if (! $response->successful()) {
26+
throw new Exception('Request was not successful. Unable to create DTO.');
27+
}
28+
29+
return $response->json();
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Invoices\SubPositions;
4+
5+
use CodebarAg\Bexio\Dto\Invoices\InvoicePositionDTO;
6+
use Exception;
7+
use Saloon\Contracts\Body\HasBody;
8+
use Saloon\Enums\Method;
9+
use Saloon\Http\Request;
10+
use Saloon\Http\Response;
11+
use Saloon\Traits\Body\HasJsonBody;
12+
13+
class CreateASubPositionRequest extends Request implements HasBody
14+
{
15+
use HasJsonBody;
16+
17+
protected Method $method = Method::POST;
18+
19+
public function __construct(
20+
protected string $kb_document_type,
21+
protected int $invoice_id,
22+
) {}
23+
24+
public function resolveEndpoint(): string
25+
{
26+
return sprintf('/2.0/%s/%s/kb_position_subposition', $this->kb_document_type, $this->invoice_id);
27+
}
28+
29+
public function createDtoFromResponse(Response $response): InvoicePositionDTO
30+
{
31+
if (! $response->successful()) {
32+
throw new Exception('Request was not successful. Unable to create DTO.');
33+
}
34+
35+
$res = $response->json();
36+
37+
return InvoicePositionDTO::fromArray($res);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"statusCode": 200,
3+
"headers": {
4+
"Date": "Tue, 01 Oct 2024 18:24:11 GMT",
5+
"Content-Type": "application\/json",
6+
"Content-Length": "16",
7+
"Connection": "keep-alive",
8+
"x-ratelimit-remaining-minute": "999",
9+
"x-ratelimit-limit-minute": "1000",
10+
"ratelimit-remaining": "999",
11+
"ratelimit-reset": "50",
12+
"ratelimit-limit": "1000",
13+
"Cache-Control": "no-store",
14+
"pragma": "no-cache",
15+
"vary": "Origin",
16+
"access-control-allow-origin": "https:\/\/office.bexio.com",
17+
"via": "1.1 google",
18+
"CF-Cache-Status": "DYNAMIC",
19+
"Server": "cloudflare",
20+
"CF-RAY": "8cbe7cf34df271de-LHR"
21+
},
22+
"data": "{\"success\":true}"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"statusCode": 403,
3+
"headers": {
4+
"Date": "Tue, 01 Oct 2024 18:22:06 GMT",
5+
"Content-Type": "application\/json",
6+
"Transfer-Encoding": "chunked",
7+
"Connection": "keep-alive",
8+
"x-ratelimit-remaining-minute": "999",
9+
"x-ratelimit-limit-minute": "1000",
10+
"ratelimit-remaining": "999",
11+
"ratelimit-limit": "1000",
12+
"ratelimit-reset": "54",
13+
"Cache-Control": "no-store",
14+
"pragma": "no-cache",
15+
"vary": "Origin",
16+
"access-control-allow-origin": "https:\/\/office.bexio.com",
17+
"via": "1.1 google",
18+
"CF-Cache-Status": "DYNAMIC",
19+
"Server": "cloudflare",
20+
"CF-RAY": "8cbe79e9f85f3dca-LHR"
21+
},
22+
"data": "{\"error_code\":403,\"message\":\"You are not allowed to access this resource.\"}"
23+
}

0 commit comments

Comments
 (0)