From ed9976c394fdf33fd58181fda6c3e9f45e2c3335 Mon Sep 17 00:00:00 2001 From: Andrew McIntosh Date: Sun, 21 Apr 2024 15:40:11 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20expense=20attachment=20docume?= =?UTF-8?q?ntation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And invoice attachment model. Closes #67 --- CHANGELOG.md | 1 + README.md | 2 +- docs/source/file-uploads.rst | 22 +++++++++- src/Model/ExpenseAttachment.php | 7 +--- src/Model/InvoiceAttachment.php | 74 +++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 src/Model/InvoiceAttachment.php diff --git a/CHANGELOG.md b/CHANGELOG.md index d24839a..5bcff17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Support for PHP 8.3 +- Handle file uploads and invoice, expense attachments - Handle new API version webhook event errors ## 0.7.0 diff --git a/README.md b/README.md index 23178bc..f9306e2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Packagist Version](https://badgen.net/packagist/v/amcintosh/freshbooks)](https://packagist.org/packages/amcintosh/freshbooks) ![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/amcintosh/freshbooks) -[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/amcintosh/freshbooks-php-sdk/Run%20Tests)](https://github.com/amcintosh/freshbooks-php-sdk/actions?query=workflow%3A%22Run+Tests%22) +[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/amcintosh/freshbooks-php-sdk/run-tests.yml?branch=main)](https://github.com/amcintosh/freshbooks-php-sdk/actions?query=workflow%3A%22Run+Tests%22) A FreshBooks PHP SDK to allow you to more easily utilize the [FreshBooks API](https://www.freshbooks.com/api). This library is not directly maintained by FreshBooks and [community contributions](CONTRIBUTING.md) are welcome. diff --git a/docs/source/file-uploads.rst b/docs/source/file-uploads.rst index 627446f..bc16b6d 100644 --- a/docs/source/file-uploads.rst +++ b/docs/source/file-uploads.rst @@ -21,7 +21,7 @@ Invoice Images and Attachments See `FreshBooks' API Documentation `_. The ``upload()`` function takes a `PHP resource `_. -Logo's and banners are added to the invoice presentation objec.t To include an uploaded attachment on +Logo's and banners are added to the invoice presentation object. To include an uploaded attachment on an invoice, the invoice request must include an attachments object. .. code-block:: php @@ -51,3 +51,23 @@ Expense Receipts ---------------- See `FreshBooks' API Documentation `_. + +Expenses have have images or PDFs of the associated receipt attached. The expense request must include +an attachments object. + +.. code-block:: php + $attachment = $freshBooksClient->attachments()->upload($accountId, fopen('./sample_receipt.pdf', 'r')); + + $expense->amount = new Money("6.49", "CAD"); + $expense->date = new DateTime(); + $expense->staffId = 1; + $expense->categoryId = 3436009; + + $expenseAttachment = new ExpenseAttachment(); + $expenseAttachment->jwt = $attachment->jwt; + $expenseAttachment->mediaType = $attachment->mediaType; + + $expense->attachment = $expenseAttachment; + + $includes = (new IncludesBuilder())->include('attachment'); + $expense = $freshBooksClient->expenses()->create($accountId, model: $expense, includes: $includes); diff --git a/src/Model/ExpenseAttachment.php b/src/Model/ExpenseAttachment.php index ac47ba8..1e1f762 100644 --- a/src/Model/ExpenseAttachment.php +++ b/src/Model/ExpenseAttachment.php @@ -4,16 +4,11 @@ namespace amcintosh\FreshBooks\Model; -use DateTime; -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; -use amcintosh\FreshBooks\Model\Caster\MoneyCaster; /** * Attached receipt image details for an expense. @@ -22,7 +17,7 @@ * present with the use of a corresponding "includes" filter. * * @package amcintosh\FreshBooks\Model - * @link https://www.freshbooks.com/api/expenses + * @link https://www.freshbooks.com/api/expense-attachments */ class ExpenseAttachment extends DataTransferObject implements DataModel { diff --git a/src/Model/InvoiceAttachment.php b/src/Model/InvoiceAttachment.php new file mode 100644 index 0000000..b56b335 --- /dev/null +++ b/src/Model/InvoiceAttachment.php @@ -0,0 +1,74 @@ +except('id') + ->except('attachmentId') + ->toArray(); + foreach ($data as $key => $value) { + if (is_null($value)) { + unset($data[$key]); + } + } + return $data; + } +}