Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Credit Note Support #67

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Resources/CreditNotes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Dcblogdev\Xero\Resources;

use Dcblogdev\Xero\Enums\FilterOptions;
use Dcblogdev\Xero\Xero;
use InvalidArgumentException;

class CreditNotes extends Xero
{
protected array $queryString = [];

public function filter($key, $value): static
{
if (! FilterOptions::isValid($key)) {
throw new InvalidArgumentException("Filter option '$key' is not valid.");
}

$this->queryString[$key] = $value;

return $this;
}

public function get(): array
{
$queryString = $this->formatQueryStrings($this->queryString);

$result = parent::get('CreditNotes?'.$queryString);

return $result['body']['CreditNotes'];
}

public function find(string $contactId): array
{
$result = parent::get('CreditNotes/'.$contactId);

return $result['body']['CreditNotes'][0];
}

public function update(string $contactId, array $data): array
{
$result = $this->post('CreditNotes/'.$contactId, $data);

return $result['body']['CreditNotes'][0];
}

public function store(array $data): array
{
$result = $this->post('CreditNotes', $data);

return $result['body']['CreditNotes'][0];
}
}
6 changes: 6 additions & 0 deletions src/Xero.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Dcblogdev\Xero\Models\XeroToken;
use Dcblogdev\Xero\Resources\Contacts;
use Dcblogdev\Xero\Resources\CreditNotes;
use Dcblogdev\Xero\Resources\Invoices;
use Dcblogdev\Xero\Resources\Webhooks;
use Exception;
Expand Down Expand Up @@ -42,6 +43,11 @@ public function contacts(): Contacts
return new Contacts();
}

public function creditnotes(): CreditNotes
{
return new CreditNotes();
}

public function invoices(): Invoices
{
return new Invoices();
Expand Down
17 changes: 17 additions & 0 deletions tests/Resources/CreditNotesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Dcblogdev\Xero\Facades\Xero;
use Dcblogdev\Xero\Resources\CreditNotes;

test('invalid filter option throws exception', function(){
Xero::creditnotes()
->filter('bogus', 1)
->get();
})->throws(InvalidArgumentException::class, "Filter option 'bogus' is not valid.");

test('filter returns object', function(){

$filter = (new CreditNotes())->filter('ids', '1234');

expect($filter)->toBeObject();
});