Skip to content

Commit

Permalink
✨ Add taxes resource
Browse files Browse the repository at this point in the history
  • Loading branch information
amcintosh committed Nov 19, 2021
1 parent fb3e451 commit 2ee2415
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# FreshBooks PHP SDK Changelog

## Unreleased

Basic Invoicing workflow functionality

- Supports clients, invoices, line items, taxes
12 changes: 12 additions & 0 deletions src/FreshBooksClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use amcintosh\FreshBooks\Model\ClientList;
use amcintosh\FreshBooks\Model\Invoice;
use amcintosh\FreshBooks\Model\InvoiceList;
use amcintosh\FreshBooks\Model\Tax;
use amcintosh\FreshBooks\Model\TaxList;
use amcintosh\FreshBooks\Resource\AccountingResource;

class FreshBooksClient
Expand Down Expand Up @@ -85,4 +87,14 @@ public function invoices(): AccountingResource
{
return new AccountingResource($this->httpClient, 'invoices/invoices', Invoice::class, InvoiceList::class);
}

/**
* FreshBooks taxes resource with calls to get, list, create, update, delete.
*
* @return AccountingResource
*/
public function taxes(): AccountingResource
{
return new AccountingResource($this->httpClient, 'taxes/taxes', Tax::class, TaxList::class);
}
}
80 changes: 80 additions & 0 deletions src/Model/Tax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace amcintosh\FreshBooks\Model;

use DateTimeImmutable;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Attributes\MapFrom;
use Spatie\DataTransferObject\Attributes\MapTo;
use Spatie\DataTransferObject\Caster;
use Spatie\DataTransferObject\DataTransferObject;
use amcintosh\FreshBooks\Model\DataModel;
use amcintosh\FreshBooks\Model\Caster\AccountingDateTimeImmutableCaster;

/**
* System-wide taxes for invoices.
*
* @package amcintosh\FreshBooks\Model
* @link https://www.freshbooks.com/api/taxes
*/
class Tax extends DataTransferObject implements DataModel
{
public const RESPONSE_FIELD = 'tax';

/**
* @var int Get the unique identifier of this tax within this business.
*/
public ?int $id;

/**
* @var string Unique identifier of account the tax exists on.
*/
#[MapFrom('accounting_systemid')]
public ?string $accountingSystemId;

/**
* @var string Percentage value of tax.
*/
public ?string $amount;

/**
* @var string Identifiable name for the tax.
*
* Eg. "GST"
*/
public ?string $name;

/**
* @var string An external number that identifies your tax submission.
*/
public ?string $number;

/**
* @var int Duplicate of id.
*/
#[MapFrom('taxid')]
public ?int $taxId;

/**
* @var DateTimeImmutable The time of last modification.
*/
#[CastWith(AccountingDateTimeImmutableCaster::class)]
public ?DateTimeImmutable $updated;

/**
* Get the client data as an array to POST or PUT to FreshBooks, removing any read-only fields.
*
* @return array
*/
public function getContent(): array
{
return $this
->except('id')
->except('accountingSystemId')
->except('taxId')
->except('updated')
->toArray();
}
}
34 changes: 34 additions & 0 deletions src/Model/TaxList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace amcintosh\FreshBooks\Model;

use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Attributes\MapFrom;
use Spatie\DataTransferObject\Casters\ArrayCaster;
use Spatie\DataTransferObject\DataTransferObject;
use amcintosh\FreshBooks\Model\Tax;

/**
* Results of taxes list call containing list of taxes and pagination data.
*
* @package amcintosh\FreshBooks\Model
* @link https://www.freshbooks.com/api/taxes
*/
class TaxList extends DataTransferObject
{
public const RESPONSE_FIELD = 'taxes';

public int $page;

public int $pages;

#[MapFrom('per_page')]
public int $perPage;

public int $total;

#[CastWith(ArrayCaster::class, itemType: Tax::class)]
public array $taxes;
}
49 changes: 49 additions & 0 deletions tests/Model/TaxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace amcintosh\FreshBooks\Tests\Model;

use DateTime;
use DateTimeZone;
use PHPUnit\Framework\TestCase;
use amcintosh\FreshBooks\Model\Tax;
use amcintosh\FreshBooks\Model\VisState;

final class TaxTest extends TestCase
{
private $sampleTaxData = '{"tax":{
"accounting_systemid": "ACM123",
"amount": "13",
"compound": false,
"id": 7840,
"name": "HST",
"number": "RT 1234",
"taxid": 7840,
"updated": "2020-06-16 10:04:37"}}';

public function testTaxFromResponse(): void
{
$taxData = json_decode($this->sampleTaxData, true);

$tax = new Tax($taxData[Tax::RESPONSE_FIELD]);

$this->assertSame(7840, $tax->id);
$this->assertSame('ACM123', $tax->accountingSystemId);
$this->assertSame('13', $tax->amount);
$this->assertSame('HST', $tax->name);
$this->assertSame('RT 1234', $tax->number);
$this->assertEquals(new DateTime('2020-06-16T14:04:37Z'), $tax->updated);
}

public function testClientGetContent(): void
{
$taxData = json_decode($this->sampleTaxData, true);
$tax = new Tax($taxData['tax']);
$this->assertSame([
'amount' => '13',
'name' => 'HST',
'number' => 'RT 1234'
], $tax->getContent());
}
}

0 comments on commit 2ee2415

Please sign in to comment.