Skip to content

Commit 95eb933

Browse files
committed
First Draft Quote Requests
1 parent 93fbb9e commit 95eb933

33 files changed

+1409
-2
lines changed

src/Dto/ItemPositions/OfferItemPositionDTO.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public function __construct(
2121
?bool $is_percentual = null,
2222
?string $value = null,
2323
?bool $is_optional = null,
24-
public ?string $kb_document_type = 'kb_offer',
24+
?string $kb_document_type = null,
2525
) {
2626
parent::__construct(
27-
kb_document_type: $this->kb_document_type,
27+
kb_document_type: $kb_document_type ?? 'kb_offer',
2828
kb_position_id: $kb_position_id,
2929
type: $type,
3030
amount: $amount,
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\ItemPositions;
4+
5+
use CodebarAg\Bexio\Dto\ItemPositions\CreateEditItemPositionDTO;
6+
use CodebarAg\Bexio\Dto\ItemPositions\ItemPositionDTO;
7+
use Exception;
8+
use Illuminate\Support\Collection;
9+
use Saloon\Contracts\Body\HasBody;
10+
use Saloon\Enums\Method;
11+
use Saloon\Http\Request;
12+
use Saloon\Http\Response;
13+
use Saloon\Traits\Body\HasJsonBody;
14+
15+
class CreateAnItemPositionRequest extends Request implements HasBody
16+
{
17+
use HasJsonBody;
18+
19+
protected Method $method = Method::POST;
20+
21+
public function __construct(
22+
public readonly int $kb_document_id,
23+
public readonly ?CreateEditItemPositionDTO $itemPosition = null,
24+
) {}
25+
26+
public function resolveEndpoint(): string
27+
{
28+
return '/2.0/kb_position';
29+
}
30+
31+
public function defaultBody(): array
32+
{
33+
if ($this->itemPosition) {
34+
$itemPosition = collect($this->itemPosition->toArray());
35+
$itemPosition->put('kb_document_id', $this->kb_document_id);
36+
37+
return $this->filterItemPosition($itemPosition);
38+
}
39+
40+
return [];
41+
}
42+
43+
protected function filterItemPosition(Collection $itemPosition): array
44+
{
45+
$allowedKeys = [
46+
'KbPositionCustom' => [
47+
'kb_document_id',
48+
'kb_document_type',
49+
'amount',
50+
'unit_id',
51+
'account_id',
52+
'tax_id',
53+
'text',
54+
'unit_price',
55+
'discount_in_percent',
56+
],
57+
'KbPositionArticle' => [
58+
'kb_document_id',
59+
'kb_document_type',
60+
'amount',
61+
'unit_id',
62+
'account_id',
63+
'tax_id',
64+
'text',
65+
'unit_price',
66+
'discount_in_percent',
67+
'article_id',
68+
],
69+
'KbPositionText' => [
70+
'kb_document_id',
71+
'kb_document_type',
72+
'text',
73+
'show_pos_nr',
74+
],
75+
'KbPositionSubtotal' => [
76+
'kb_document_id',
77+
'kb_document_type',
78+
'text',
79+
],
80+
'KbPositionPagebreak' => [
81+
'kb_document_id',
82+
'kb_document_type',
83+
'pagebreak',
84+
],
85+
'KbPositionDiscount' => [
86+
'kb_document_id',
87+
'kb_document_type',
88+
'text',
89+
'is_percentual',
90+
'value',
91+
],
92+
];
93+
94+
$type = $itemPosition->get('type');
95+
$keys = array_merge(['type'], $allowedKeys[$type] ?? []);
96+
97+
return $itemPosition->only($keys)->toArray();
98+
}
99+
100+
public function createDtoFromResponse(Response $response): ItemPositionDTO
101+
{
102+
if (! $response->successful()) {
103+
throw new Exception('Request was not successful. Unable to create DTO.');
104+
}
105+
106+
$res = $response->json();
107+
108+
return ItemPositionDTO::fromArray($res);
109+
}
110+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\ItemPositions;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class DeleteAnItemPositionRequest extends Request
11+
{
12+
protected Method $method = Method::DELETE;
13+
14+
public function __construct(
15+
public readonly int $item_position_id,
16+
) {}
17+
18+
public function resolveEndpoint(): string
19+
{
20+
return '/2.0/kb_position/'.$this->item_position_id;
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+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\ItemPositions;
4+
5+
use CodebarAg\Bexio\Dto\ItemPositions\CreateEditItemPositionDTO;
6+
use CodebarAg\Bexio\Dto\ItemPositions\ItemPositionDTO;
7+
use Exception;
8+
use Illuminate\Support\Collection;
9+
use Saloon\Contracts\Body\HasBody;
10+
use Saloon\Enums\Method;
11+
use Saloon\Http\Request;
12+
use Saloon\Http\Response;
13+
use Saloon\Traits\Body\HasJsonBody;
14+
15+
class EditAnItemPositionRequest extends Request implements HasBody
16+
{
17+
use HasJsonBody;
18+
19+
protected Method $method = Method::POST;
20+
21+
public function __construct(
22+
public readonly int $item_position_id,
23+
public readonly ?CreateEditItemPositionDTO $itemPosition = null,
24+
) {}
25+
26+
public function resolveEndpoint(): string
27+
{
28+
return '/2.0/kb_position/'.$this->item_position_id;
29+
}
30+
31+
public function defaultBody(): array
32+
{
33+
if ($this->itemPosition) {
34+
$itemPosition = collect($this->itemPosition->toArray());
35+
36+
return $this->filterItemPosition($itemPosition);
37+
}
38+
39+
return [];
40+
}
41+
42+
protected function filterItemPosition(Collection $itemPosition): array
43+
{
44+
$allowedKeys = [
45+
'KbPositionCustom' => [
46+
'amount',
47+
'unit_id',
48+
'account_id',
49+
'tax_id',
50+
'text',
51+
'unit_price',
52+
'discount_in_percent',
53+
],
54+
'KbPositionArticle' => [
55+
'amount',
56+
'unit_id',
57+
'account_id',
58+
'tax_id',
59+
'text',
60+
'unit_price',
61+
'discount_in_percent',
62+
'article_id',
63+
],
64+
'KbPositionText' => [
65+
'text',
66+
'show_pos_nr',
67+
],
68+
'KbPositionSubtotal' => [
69+
'text',
70+
],
71+
'KbPositionPagebreak' => [
72+
'pagebreak',
73+
],
74+
'KbPositionDiscount' => [
75+
'text',
76+
'is_percentual',
77+
'value',
78+
],
79+
];
80+
81+
$type = $itemPosition->get('type');
82+
$keys = array_merge(['type'], $allowedKeys[$type] ?? []);
83+
84+
return $itemPosition->only($keys)->toArray();
85+
}
86+
87+
public function createDtoFromResponse(Response $response): ItemPositionDTO
88+
{
89+
if (! $response->successful()) {
90+
throw new Exception('Request was not successful. Unable to create DTO.');
91+
}
92+
93+
$res = $response->json();
94+
95+
return ItemPositionDTO::fromArray($res);
96+
}
97+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\ItemPositions;
4+
5+
use CodebarAg\Bexio\Dto\ItemPositions\ItemPositionDTO;
6+
use Exception;
7+
use Illuminate\Support\Collection;
8+
use Saloon\Enums\Method;
9+
use Saloon\Http\Request;
10+
use Saloon\Http\Response;
11+
12+
class FetchAListOfItemPositionsRequest extends Request
13+
{
14+
protected Method $method = Method::GET;
15+
16+
public function __construct(
17+
public readonly int $kb_document_id,
18+
public readonly string $kb_document_type,
19+
public readonly string $orderBy = 'id',
20+
public readonly int $limit = 500,
21+
public readonly int $offset = 0,
22+
) {}
23+
24+
public function resolveEndpoint(): string
25+
{
26+
return '/2.0/kb_position';
27+
}
28+
29+
public function defaultQuery(): array
30+
{
31+
return [
32+
'kb_document_id' => $this->kb_document_id,
33+
'kb_document_type' => $this->kb_document_type,
34+
'order_by' => $this->orderBy,
35+
'limit' => $this->limit,
36+
'offset' => $this->offset,
37+
];
38+
}
39+
40+
public function createDtoFromResponse(Response $response): Collection
41+
{
42+
if (! $response->successful()) {
43+
throw new Exception('Request was not successful. Unable to create DTO.');
44+
}
45+
46+
$res = $response->json();
47+
48+
$itemPositions = collect();
49+
50+
foreach ($res as $itemPosition) {
51+
$itemPositions->push(ItemPositionDTO::fromArray($itemPosition));
52+
}
53+
54+
return $itemPositions;
55+
}
56+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\ItemPositions;
4+
5+
use CodebarAg\Bexio\Dto\ItemPositions\ItemPositionDTO;
6+
use Exception;
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Http\Response;
10+
11+
class FetchAnItemPositionRequest extends Request
12+
{
13+
protected Method $method = Method::GET;
14+
15+
public function __construct(
16+
public readonly int $item_position_id,
17+
) {}
18+
19+
public function resolveEndpoint(): string
20+
{
21+
return '/2.0/kb_position/'.$this->item_position_id;
22+
}
23+
24+
public function createDtoFromResponse(Response $response): ItemPositionDTO
25+
{
26+
if (! $response->successful()) {
27+
throw new Exception('Request was not successful. Unable to create DTO.');
28+
}
29+
30+
$itemPosition = $response->json();
31+
32+
return ItemPositionDTO::fromArray($itemPosition);
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Quotes;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class AcceptAQuoteRequest extends Request
11+
{
12+
protected Method $method = Method::POST;
13+
14+
public function __construct(
15+
public readonly int $quote_id,
16+
) {}
17+
18+
public function resolveEndpoint(): string
19+
{
20+
return '/2.0/kb_offer/'.$this->quote_id.'/accept';
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+
}

0 commit comments

Comments
 (0)