Skip to content

Commit 14fefa3

Browse files
committed
feat: Add positions DTOs and enhance invoicing
1 parent ab3e88b commit 14fefa3

File tree

134 files changed

+4627
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+4627
-1
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\DefaultPositions;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Spatie\LaravelData\Data;
8+
9+
/**
10+
* DTO for adding a default (custom) position to a document (invoice, offer, order).
11+
* Used in the positions array when creating a document (invoice, offer, order) via the Bexio API.
12+
*/
13+
class AddDefaultPositionDTO extends Data
14+
{
15+
public function __construct(
16+
public string $amount,
17+
public int $unit_id,
18+
public int $account_id,
19+
public int $tax_id,
20+
public string $text,
21+
public string $unit_price,
22+
public string $discount_in_percent,
23+
public string $type = 'KbPositionCustom',
24+
) {}
25+
26+
public static function fromArray(array $data): self
27+
{
28+
if (! $data) {
29+
throw new Exception('Unable to create DTO. Data missing from response.');
30+
}
31+
return new self(
32+
amount: Arr::get($data, 'amount'),
33+
unit_id: Arr::get($data, 'unit_id'),
34+
account_id: Arr::get($data, 'account_id'),
35+
tax_id: Arr::get($data, 'tax_id'),
36+
text: Arr::get($data, 'text'),
37+
unit_price: Arr::get($data, 'unit_price'),
38+
discount_in_percent: Arr::get($data, 'discount_in_percent'),
39+
type: Arr::get($data, 'type', 'KbPositionCustom'),
40+
);
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\DefaultPositions;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Spatie\LaravelData\Data;
8+
9+
/**
10+
* DTO for creating or editing a default (custom) position for a document (invoice, offer, order).
11+
* Used as the request payload for the kb_position_custom endpoint.
12+
*/
13+
class CreateEditDefaultPositionDTO extends Data
14+
{
15+
public function __construct(
16+
public string $amount,
17+
public int $unit_id,
18+
public int $account_id,
19+
public int $tax_id,
20+
public string $text,
21+
public string $unit_price,
22+
public string $discount_in_percent,
23+
public ?int $parent_id = null,
24+
) {}
25+
26+
public static function fromArray(array $data): self
27+
{
28+
if (! $data) {
29+
throw new Exception('Unable to create DTO. Data missing from response.');
30+
}
31+
return new self(
32+
amount: Arr::get($data, 'amount'),
33+
unit_id: Arr::get($data, 'unit_id'),
34+
account_id: Arr::get($data, 'account_id'),
35+
tax_id: Arr::get($data, 'tax_id'),
36+
text: Arr::get($data, 'text'),
37+
unit_price: Arr::get($data, 'unit_price'),
38+
discount_in_percent: Arr::get($data, 'discount_in_percent'),
39+
parent_id: Arr::get($data, 'parent_id'),
40+
);
41+
}
42+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\DefaultPositions;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
/**
11+
* DTO for representing a default (custom) position as returned by the API.
12+
*/
13+
class DefaultPositionDTO extends Data
14+
{
15+
public function __construct(
16+
public ?int $id,
17+
public ?string $amount,
18+
public ?int $unit_id,
19+
public ?string $unit_name,
20+
public ?int $account_id,
21+
public ?int $tax_id,
22+
public ?string $tax_value,
23+
public ?string $text,
24+
public ?string $unit_price,
25+
public ?string $discount_in_percent,
26+
public ?string $position_total,
27+
public ?int $parent_id,
28+
public ?string $type = 'KbPositionCustom',
29+
public ?string $pos,
30+
public ?string $internal_pos,
31+
public ?bool $is_optional,
32+
) {}
33+
34+
public static function fromResponse(Response $response): self
35+
{
36+
if ($response->failed()) {
37+
throw new Exception('Failed to create DTO from Response');
38+
}
39+
$data = $response->json();
40+
return self::fromArray($data);
41+
}
42+
43+
public static function fromArray(array $data): self
44+
{
45+
if (! $data) {
46+
throw new Exception('Unable to create DTO. Data missing from response.');
47+
}
48+
return new self(
49+
id: Arr::get($data, 'id'),
50+
amount: Arr::get($data, 'amount'),
51+
unit_id: Arr::get($data, 'unit_id'),
52+
unit_name: Arr::get($data, 'unit_name'),
53+
account_id: Arr::get($data, 'account_id'),
54+
tax_id: Arr::get($data, 'tax_id'),
55+
tax_value: Arr::get($data, 'tax_value'),
56+
text: Arr::get($data, 'text'),
57+
unit_price: Arr::get($data, 'unit_price'),
58+
discount_in_percent: Arr::get($data, 'discount_in_percent'),
59+
position_total: Arr::get($data, 'position_total'),
60+
parent_id: Arr::get($data, 'parent_id'),
61+
type: Arr::get($data, 'type', 'KbPositionCustom'),
62+
pos: Arr::get($data, 'pos'),
63+
internal_pos: Arr::get($data, 'internal_pos'),
64+
is_optional: Arr::get($data, 'is_optional'),
65+
);
66+
}
67+
}
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\Dto\DiscountPositions;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Spatie\LaravelData\Data;
8+
9+
/**
10+
* DTO for adding a discount position to a document (invoice, offer, order).
11+
* Used in the positions array when creating a document (invoice, offer, order) via the Bexio API.
12+
*/
13+
class AddDiscountPositionDTO extends Data
14+
{
15+
public function __construct(
16+
public string $text,
17+
public bool $is_percentual,
18+
public string $value,
19+
public string $type = 'KbPositionDiscount',
20+
) {}
21+
22+
public static function fromArray(array $data): self
23+
{
24+
if (! $data) {
25+
throw new Exception('Unable to create DTO. Data missing from response.');
26+
}
27+
return new self(
28+
text: Arr::get($data, 'text'),
29+
is_percentual: Arr::get($data, 'is_percentual'),
30+
value: Arr::get($data, 'value'),
31+
type: Arr::get($data, 'type', 'KbPositionDiscount'),
32+
);
33+
}
34+
}
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\Dto\DiscountPositions;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Spatie\LaravelData\Data;
8+
9+
/**
10+
* DTO for creating or editing a discount position for a document (invoice, offer, order).
11+
* Used as the request payload for the kb_position_discount endpoint.
12+
*/
13+
class CreateEditDiscountPositionDTO extends Data
14+
{
15+
public function __construct(
16+
public string $text,
17+
public bool $is_percentual,
18+
public string $value,
19+
public ?int $parent_id = null,
20+
) {}
21+
22+
public static function fromArray(array $data): self
23+
{
24+
if (! $data) {
25+
throw new Exception('Unable to create DTO. Data missing from response.');
26+
}
27+
return new self(
28+
text: Arr::get($data, 'text'),
29+
is_percentual: Arr::get($data, 'is_percentual'),
30+
value: Arr::get($data, 'value'),
31+
parent_id: Arr::get($data, 'parent_id'),
32+
);
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\DiscountPositions;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
/**
11+
* DTO for representing a discount position as returned by the API.
12+
*/
13+
class DiscountPositionDTO extends Data
14+
{
15+
public function __construct(
16+
public ?int $id,
17+
public ?string $text,
18+
public ?bool $is_percentual,
19+
public ?string $value,
20+
public ?string $discount_total,
21+
public ?string $type = 'KbPositionDiscount',
22+
) {}
23+
24+
public static function fromResponse(Response $response): self
25+
{
26+
if ($response->failed()) {
27+
throw new Exception('Failed to create DTO from Response');
28+
}
29+
$data = $response->json();
30+
return self::fromArray($data);
31+
}
32+
33+
public static function fromArray(array $data): self
34+
{
35+
if (! $data) {
36+
throw new Exception('Unable to create DTO. Data missing from response.');
37+
}
38+
return new self(
39+
id: Arr::get($data, 'id'),
40+
text: Arr::get($data, 'text'),
41+
is_percentual: Arr::get($data, 'is_percentual'),
42+
value: Arr::get($data, 'value'),
43+
discount_total: Arr::get($data, 'discount_total'),
44+
type: Arr::get($data, 'type', 'KbPositionDiscount'),
45+
);
46+
}
47+
}

src/Dto/Invoices/CreateInvoiceDTO.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\Invoices;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Spatie\LaravelData\Data;
8+
9+
/**
10+
* DTO for creating an invoice (request payload).
11+
* Positions should be an array of Add*PositionDTOs.
12+
*/
13+
class CreateInvoiceDTO extends Data
14+
{
15+
public function __construct(
16+
public int $user_id,
17+
public int $language_id,
18+
public int $bank_account_id,
19+
public int $currency_id,
20+
public int $payment_type_id,
21+
public int $mwst_type,
22+
public bool $mwst_is_net,
23+
public bool $show_position_taxes,
24+
public string $is_valid_from,
25+
public string $is_valid_to,
26+
public array $positions,
27+
public ?string $header,
28+
public ?string $footer,
29+
public ?string $document_nr = null, // Can not be used if “automatic numbering” is activated in frontend-settings. Required if “automatic numbering” deactivated.
30+
public ?string $title = null,
31+
public ?int $contact_id = null,
32+
public ?int $contact_sub_id = null,
33+
public ?int $pr_project_id = null,
34+
/** @deprecated */
35+
public ?int $logopaper_id = null,
36+
public ?string $contact_address_manual = null,
37+
public ?string $reference = null,
38+
public ?string $api_reference = null,
39+
public ?string $template_slug = null,
40+
) {}
41+
42+
public static function fromArray(array $data): self
43+
{
44+
if (! $data) {
45+
throw new Exception('Unable to create DTO. Data missing from response.');
46+
}
47+
48+
return new self(
49+
user_id: Arr::get($data, 'user_id'),
50+
language_id: Arr::get($data, 'language_id'),
51+
bank_account_id: Arr::get($data, 'bank_account_id'),
52+
currency_id: Arr::get($data, 'currency_id'),
53+
payment_type_id: Arr::get($data, 'payment_type_id'),
54+
header: Arr::get($data, 'header'),
55+
footer: Arr::get($data, 'footer'),
56+
mwst_type: Arr::get($data, 'mwst_type'),
57+
mwst_is_net: Arr::get($data, 'mwst_is_net'),
58+
show_position_taxes: Arr::get($data, 'show_position_taxes'),
59+
is_valid_from: Arr::get($data, 'is_valid_from'),
60+
is_valid_to: Arr::get($data, 'is_valid_to'),
61+
positions: Arr::get($data, 'positions', []),
62+
document_nr: Arr::get($data, 'document_nr'),
63+
title: Arr::get($data, 'title'),
64+
contact_id: Arr::get($data, 'contact_id'),
65+
contact_sub_id: Arr::get($data, 'contact_sub_id'),
66+
pr_project_id: Arr::get($data, 'pr_project_id'),
67+
logopaper_id: Arr::get($data, 'logopaper_id'),
68+
contact_address_manual: Arr::get($data, 'contact_address_manual'),
69+
reference: Arr::get($data, 'reference'),
70+
api_reference: Arr::get($data, 'api_reference'),
71+
template_slug: Arr::get($data, 'template_slug'),
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)