diff --git a/CHANGELOG.md b/CHANGELOG.md index 464a299..ac5b9b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,4 +4,4 @@ Basic Invoicing workflow functionality -- Supports clients, invoices, line items, taxes +- Supports clients, invoices, line items, payments, taxes diff --git a/src/FreshBooksClient.php b/src/FreshBooksClient.php index 920c122..a4e36d2 100644 --- a/src/FreshBooksClient.php +++ b/src/FreshBooksClient.php @@ -18,6 +18,8 @@ use amcintosh\FreshBooks\Model\ClientList; use amcintosh\FreshBooks\Model\Invoice; use amcintosh\FreshBooks\Model\InvoiceList; +use amcintosh\FreshBooks\Model\Payment; +use amcintosh\FreshBooks\Model\PaymentList; use amcintosh\FreshBooks\Model\Tax; use amcintosh\FreshBooks\Model\TaxList; use amcintosh\FreshBooks\Resource\AccountingResource; @@ -88,6 +90,16 @@ public function invoices(): AccountingResource return new AccountingResource($this->httpClient, 'invoices/invoices', Invoice::class, InvoiceList::class); } + /** + * FreshBooks payments resource with calls to get, list, create, update, delete. + * + * @return AccountingResource + */ + public function payments(): AccountingResource + { + return new AccountingResource($this->httpClient, 'payments/payments', Payment::class, PaymentList::class); + } + /** * FreshBooks taxes resource with calls to get, list, create, update, delete. * diff --git a/src/Model/Client.php b/src/Model/Client.php index dc63b12..0a17488 100644 --- a/src/Model/Client.php +++ b/src/Model/Client.php @@ -252,13 +252,13 @@ class Client extends DataTransferObject implements DataModel public ?int $visState; /** - * Get the client data as an array to POST or PUT to FreshBooks, removing any read-only fields. + * Get the data as an array to POST or PUT to FreshBooks, removing any read-only fields. * * @return array */ public function getContent(): array { - return $this + $data = $this ->except('id') ->except('accountingSystemId') ->except('lastActivity') @@ -267,5 +267,11 @@ public function getContent(): array ->except('userId') ->except('visState') ->toArray(); + foreach ($data as $key => $value) { + if (is_null($value)) { + unset($data[$key]); + } + } + return $data; } } diff --git a/src/Model/Invoice.php b/src/Model/Invoice.php index 8d64c82..2f583ec 100644 --- a/src/Model/Invoice.php +++ b/src/Model/Invoice.php @@ -507,7 +507,7 @@ class Invoice extends DataTransferObject implements DataModel public ?int $visState; /** - * Get the invoice data as an array to POST or PUT to FreshBooks, removing any read-only fields. + * Get the data as an array to POST or PUT to FreshBooks, removing any read-only fields. * * @return array */ diff --git a/src/Model/Payment.php b/src/Model/Payment.php new file mode 100644 index 0000000..8f871c2 --- /dev/null +++ b/src/Model/Payment.php @@ -0,0 +1,180 @@ +except('id') + ->except('accountingSystemId') + ->except('paymentId') + ->except('clientId') + ->except('date') + ->except('gateway') + ->except('overpaymentId') + ->except('updated') + ->except('visState') + ->toArray(); + if (isset($this->date)) { + $data['date'] = $this->date->format('Y-m-d'); + } + foreach ($data as $key => $value) { + if (is_null($value)) { + unset($data[$key]); + } + } + return $data; + } +} diff --git a/src/Model/PaymentList.php b/src/Model/PaymentList.php new file mode 100644 index 0000000..f449a71 --- /dev/null +++ b/src/Model/PaymentList.php @@ -0,0 +1,34 @@ +except('id') ->except('accountingSystemId') ->except('taxId') ->except('updated') ->toArray(); + foreach ($data as $key => $value) { + if (is_null($value)) { + unset($data[$key]); + } + } + return $data; } } diff --git a/tests/Model/ClientTest.php b/tests/Model/ClientTest.php index cadc57b..db81ce4 100644 --- a/tests/Model/ClientTest.php +++ b/tests/Model/ClientTest.php @@ -133,8 +133,6 @@ public function testClientGetContent(): void $client = new Client($clientData['client']); $this->assertSame([ 'bus_phone' => '416-867-5309', - 'company_industry' => null, - 'company_size' => null, 'currency_code' => 'CAD', 'email' => 'gordon.shumway@AmericanCyanamid.com', 'fax' => '416-444-4444', @@ -156,9 +154,7 @@ public function testClientGetContent(): void 's_country' => '', 's_province' => '', 's_street' => '', - 's_street2' => '', - 'vat_name' => null, - 'vat_number' => null + 's_street2' => '' ], $client->getContent()); } } diff --git a/tests/Model/PaymentTest.php b/tests/Model/PaymentTest.php new file mode 100644 index 0000000..8b7910f --- /dev/null +++ b/tests/Model/PaymentTest.php @@ -0,0 +1,84 @@ +samplePaymentData, true); + + $payment = new Payment($paymentData[Payment::RESPONSE_FIELD]); + + $this->assertSame(235435, $payment->id); + $this->assertSame(235435, $payment->paymentId); + $this->assertSame('ACM123', $payment->accountingSystemId); + $this->assertEquals(Decimal::create('41.94'), $payment->amount->amount); + $this->assertSame('CAD', $payment->amount->code); + $this->assertSame(null, $payment->bulkPaymentId); + $this->assertSame(12345, $payment->clientId); + $this->assertSame(null, $payment->creditId); + $this->assertEquals(new DateTime('2021-04-16T00:00:00Z'), $payment->date); + $this->assertSame(false, $payment->fromCredit); + $this->assertSame(null, $payment->gateway); + $this->assertSame(987654, $payment->invoiceId); + $this->assertSame('Some note', $payment->note); + $this->assertSame(null, $payment->orderId); + $this->assertSame(null, $payment->overpaymentId); + $this->assertSame(null, $payment->sendClientNotification); + $this->assertSame('Check', $payment->type); + $this->assertEquals(new DateTime('2021-04-17T09:29:31Z'), $payment->updated); + $this->assertSame(VisState::ACTIVE, $payment->visState); + } + + public function testClientGetContent(): void + { + $paymentData = json_decode($this->samplePaymentData, true); + $payment = new Payment($paymentData['payment']); + $this->assertSame([ + 'amount' => [ + 'amount' => '41.94', + 'code' => 'CAD' + ], + 'from_credit' => false, + 'invoiceid' => 987654, + 'note' => 'Some note', + 'type' => 'Check', + 'date' => '2021-04-16' + ], $payment->getContent()); + } +}