Skip to content

Commit 0375ca8

Browse files
committed
feat: add financial mutations resource
1 parent 234cd27 commit 0375ca8

11 files changed

Lines changed: 457 additions & 0 deletions

src/Connectors/MoneybirdConnector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Sensson\Moneybird\Resources\AdministrationResource;
1616
use Sensson\Moneybird\Resources\ContactResource;
1717
use Sensson\Moneybird\Resources\CustomFieldResource;
18+
use Sensson\Moneybird\Resources\FinancialMutationResource;
1819
use Sensson\Moneybird\Resources\LedgerResource;
1920
use Sensson\Moneybird\Resources\SalesInvoiceResource;
2021
use Sensson\Moneybird\Resources\TaxRateResource;
@@ -85,6 +86,11 @@ public function ledgers(): LedgerResource
8586
return new LedgerResource($this);
8687
}
8788

89+
public function financialMutations(): FinancialMutationResource
90+
{
91+
return new FinancialMutationResource($this);
92+
}
93+
8894
public function taxRates(): TaxRateResource
8995
{
9096
return new TaxRateResource($this);

src/Data/Booking.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Data;
4+
5+
use Spatie\LaravelData\Data;
6+
7+
class Booking extends Data
8+
{
9+
public function __construct(
10+
public string $booking_type,
11+
public ?string $booking_id = null,
12+
public ?string $price_base = null,
13+
public ?string $price = null,
14+
public ?string $description = null,
15+
public ?string $payment_batch_identifier = null,
16+
public ?string $project_id = null,
17+
public bool|string|null $mark_open_sepa_transaction_as_paid = null,
18+
) {
19+
//
20+
}
21+
}

src/Data/FinancialMutation.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Data;
4+
5+
use Spatie\LaravelData\Attributes\DataCollectionOf;
6+
use Spatie\LaravelData\Data;
7+
8+
class FinancialMutation extends Data
9+
{
10+
public function __construct(
11+
public ?string $id = null,
12+
public ?string $administration_id = null,
13+
public ?string $amount = null,
14+
public ?string $code = null,
15+
public ?string $date = null,
16+
public ?string $message = null,
17+
public ?string $contra_account_name = null,
18+
public ?string $contra_account_number = null,
19+
public ?string $state = null,
20+
public ?string $settlement_state = null,
21+
public ?string $amount_open = null,
22+
public ?array $sepa_fields = null,
23+
public ?string $batch_reference = null,
24+
public ?string $financial_account_id = null,
25+
public ?string $currency = null,
26+
public ?string $original_amount = null,
27+
public ?string $created_at = null,
28+
public ?string $updated_at = null,
29+
public ?int $version = null,
30+
public ?string $financial_statement_id = null,
31+
public ?string $processed_at = null,
32+
public ?string $account_servicer_transaction_id = null,
33+
#[DataCollectionOf(Payment::class)]
34+
public ?array $payments = null,
35+
#[DataCollectionOf(LedgerAccountBooking::class)]
36+
public ?array $ledger_account_bookings = null,
37+
) {
38+
//
39+
}
40+
}

src/Data/LedgerAccountBooking.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Data;
4+
5+
use Spatie\LaravelData\Data;
6+
7+
class LedgerAccountBooking extends Data
8+
{
9+
public function __construct(
10+
public ?string $id = null,
11+
public ?string $administration_id = null,
12+
public ?string $financial_mutation_id = null,
13+
public ?string $ledger_account_id = null,
14+
public ?string $project_id = null,
15+
public ?string $description = null,
16+
public ?string $price = null,
17+
public ?string $created_at = null,
18+
public ?string $updated_at = null,
19+
) {
20+
//
21+
}
22+
}

src/Data/Payment.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Data;
4+
5+
use Spatie\LaravelData\Data;
6+
7+
class Payment extends Data
8+
{
9+
public function __construct(
10+
public ?string $id = null,
11+
public ?string $administration_id = null,
12+
public ?string $invoice_type = null,
13+
public ?string $invoice_id = null,
14+
public ?string $financial_account_id = null,
15+
public ?string $user_id = null,
16+
public ?string $payment_transaction_id = null,
17+
public ?string $transaction_identifier = null,
18+
public ?string $price = null,
19+
public ?string $price_base = null,
20+
public ?string $payment_date = null,
21+
public ?string $credit_invoice_id = null,
22+
public ?string $financial_mutation_id = null,
23+
public ?string $ledger_account_id = null,
24+
public ?string $linked_payment_id = null,
25+
public ?string $manual_payment_action = null,
26+
public ?string $created_at = null,
27+
public ?string $updated_at = null,
28+
) {
29+
//
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Requests\FinancialMutations;
4+
5+
use JsonException;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
use Sensson\Moneybird\Data\FinancialMutation;
10+
11+
class GetFinancialMutation extends Request
12+
{
13+
protected Method $method = Method::GET;
14+
15+
public function __construct(protected string $id)
16+
{
17+
//
18+
}
19+
20+
public function resolveEndpoint(): string
21+
{
22+
return "financial_mutations/{$this->id}.json";
23+
}
24+
25+
/**
26+
* @throws JsonException
27+
*/
28+
public function createDtoFromResponse(Response $response): FinancialMutation
29+
{
30+
return FinancialMutation::from($response->json());
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Requests\FinancialMutations;
4+
5+
use JsonException;
6+
use Saloon\Contracts\Body\HasBody;
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Http\Response;
10+
use Saloon\Traits\Body\HasJsonBody;
11+
use Sensson\Moneybird\Data\Booking;
12+
use Sensson\Moneybird\Data\FinancialMutation;
13+
14+
class LinkBookingFinancialMutation extends Request implements HasBody
15+
{
16+
use HasJsonBody;
17+
18+
protected Method $method = Method::PATCH;
19+
20+
public function __construct(
21+
protected string $id,
22+
protected Booking $booking,
23+
) {
24+
//
25+
}
26+
27+
public function resolveEndpoint(): string
28+
{
29+
return "financial_mutations/{$this->id}/link_booking.json";
30+
}
31+
32+
protected function defaultBody(): array
33+
{
34+
return collect($this->booking->toArray())
35+
->reject(fn (mixed $value): bool => $value === null)
36+
->toArray();
37+
}
38+
39+
/**
40+
* @throws JsonException
41+
*/
42+
public function createDtoFromResponse(Response $response): FinancialMutation
43+
{
44+
return FinancialMutation::from($response->json());
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Requests\FinancialMutations;
4+
5+
use JsonException;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
use Sensson\Moneybird\Data\FinancialMutation;
10+
11+
class ListFinancialMutations extends Request
12+
{
13+
protected Method $method = Method::GET;
14+
15+
public function resolveEndpoint(): string
16+
{
17+
return 'financial_mutations.json';
18+
}
19+
20+
/**
21+
* @return array{mixed: FinancialMutation}
22+
*
23+
* @throws JsonException
24+
*/
25+
public function createDtoFromResponse(Response $response): array
26+
{
27+
return FinancialMutation::collect($response->json());
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Requests\FinancialMutations;
4+
5+
use Saloon\Contracts\Body\HasBody;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Traits\Body\HasJsonBody;
9+
use Sensson\Moneybird\Data\Booking;
10+
11+
class UnlinkBookingFinancialMutation extends Request implements HasBody
12+
{
13+
use HasJsonBody;
14+
15+
protected Method $method = Method::DELETE;
16+
17+
public function __construct(
18+
protected string $id,
19+
protected Booking $booking,
20+
) {
21+
//
22+
}
23+
24+
public function resolveEndpoint(): string
25+
{
26+
return "financial_mutations/{$this->id}/unlink_booking.json";
27+
}
28+
29+
protected function defaultBody(): array
30+
{
31+
return [
32+
'booking_type' => $this->booking->booking_type,
33+
'booking_id' => $this->booking->booking_id,
34+
];
35+
}
36+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Sensson\Moneybird\Resources;
4+
5+
use Illuminate\Support\Collection;
6+
use Saloon\Exceptions\Request\FatalRequestException;
7+
use Saloon\Exceptions\Request\RequestException;
8+
use Saloon\Http\BaseResource;
9+
use Sensson\Moneybird\Data\Booking;
10+
use Sensson\Moneybird\Data\FinancialMutation;
11+
use Sensson\Moneybird\Requests\FinancialMutations\GetFinancialMutation;
12+
use Sensson\Moneybird\Requests\FinancialMutations\LinkBookingFinancialMutation;
13+
use Sensson\Moneybird\Requests\FinancialMutations\ListFinancialMutations;
14+
use Sensson\Moneybird\Requests\FinancialMutations\UnlinkBookingFinancialMutation;
15+
16+
class FinancialMutationResource extends BaseResource
17+
{
18+
/**
19+
* @return Collection<FinancialMutation>
20+
*
21+
* @throws RequestException|FatalRequestException
22+
*/
23+
public function all(?int $perPage = null, ?int $page = null): Collection
24+
{
25+
$request = new ListFinancialMutations;
26+
27+
$query = collect([
28+
'per_page' => $perPage,
29+
'page' => $page,
30+
])->reject(fn (mixed $value): bool => $value === null);
31+
32+
$request->query()->set($query->toArray());
33+
34+
return collect($this->connector->send($request)->dtoOrFail());
35+
}
36+
37+
/**
38+
* @throws RequestException|FatalRequestException
39+
*/
40+
public function get(string $id): FinancialMutation
41+
{
42+
return $this->connector->send(new GetFinancialMutation($id))->dtoOrFail();
43+
}
44+
45+
/**
46+
* @throws RequestException|FatalRequestException
47+
*/
48+
public function linkBooking(string $id, Booking $booking): FinancialMutation
49+
{
50+
$request = new LinkBookingFinancialMutation($id, $booking);
51+
52+
return $this->connector->send($request)->dtoOrFail();
53+
}
54+
55+
/**
56+
* @throws RequestException|FatalRequestException
57+
*/
58+
public function unlinkBooking(string $id, Booking $booking): void
59+
{
60+
$request = new UnlinkBookingFinancialMutation($id, $booking);
61+
62+
$this->connector->send($request);
63+
}
64+
}

0 commit comments

Comments
 (0)