Skip to content

Commit f979c8f

Browse files
committed
Merge pull request #7 from picqer/salesInvoicePayment-entity
Created Sales Invoice Payments entity
2 parents e095596 + 72b4b95 commit f979c8f

File tree

6 files changed

+80
-8
lines changed

6 files changed

+80
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ var_dump($salesInvoices); // Array with filtered SalesInvoice objects
115115
// Example: Get import mappings for contacts
116116
$mappings = $moneybird->importMapping()->setType('contact')->get();
117117
var_dump($mappings); // Array with ImportMapping objects
118+
119+
// Example: Register a payment for a sales invoice
120+
$salesInvoicePayment = $moneybird->salesInvoicePayment();
121+
$salesInvoicePayment->price = 153.75;
122+
$salesInvoicePayment->payment_date = '2015-12-03';
123+
124+
$salesInvoice = $moneybird->salesInvoice()->find(3498576378625);
125+
$salesInvoice->registerPayment($salesInvoicePayment);
118126
```
119127

120128
## Code example

src/Picqer/Financials/Moneybird/Entities/SalesInvoice.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class SalesInvoice extends Model {
7979
'entity' => 'SalesInvoiceDetail',
8080
'type' => self::NESTING_TYPE_ARRAY_OF_OBJECTS,
8181
],
82+
'payments' => [
83+
'entity' => 'SalesInvoicePayment',
84+
'type' => self::NESTING_TYPE_ARRAY_OF_OBJECTS,
85+
],
8286
];
8387

8488
/**
@@ -112,25 +116,25 @@ public function findByInvoiceId($invoiceId)
112116

113117
return $this->makeFromResponse($result);
114118
}
115-
119+
116120
/**
117121
* Register a payment for the current invoice
118122
*
119-
* @param array $data required keys are payment_date and price
123+
* @param SalesInvoicePayment $salesInvoicePayment (payment_date and price are required)
120124
* @throws ApiException
121125
*/
122-
public function registerPayment(array $data)
126+
public function registerPayment(SalesInvoicePayment $salesInvoicePayment)
123127
{
124-
if (! isset($data['payment_date'])) {
128+
if (! isset($salesInvoicePayment->payment_date)) {
125129
throw new ApiException('Required [payment_date] is missing');
126130
}
127131

128-
if (! isset($data['price'])) {
132+
if (! isset($salesInvoicePayment->price)) {
129133
throw new ApiException('Required [price] is missing');
130134
}
131135

132-
$this->connection()->patch($this->url . '/' . $this->id . '/register_payment', json_encode([
133-
'payment' => $data
134-
]));
136+
$this->connection()->patch($this->url . '/' . $this->id . '/register_payment',
137+
$salesInvoicePayment->jsonWithNamespace()
138+
);
135139
}
136140
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace Picqer\Financials\Moneybird\Entities;
2+
3+
use Picqer\Financials\Moneybird\Model;
4+
5+
/**
6+
* Class SalesInvoicePayment
7+
* @package Picqer\Financials\Moneybird\Entities
8+
*/
9+
class SalesInvoicePayment extends Model {
10+
11+
/**
12+
* @var array
13+
*/
14+
protected $fillable = [
15+
'id',
16+
'invoice_type',
17+
'invoice_id',
18+
'financial_account_id',
19+
'user_id',
20+
'payment_transaction_id',
21+
'price',
22+
'price_base',
23+
'payment_date',
24+
'credit_invoice_id',
25+
'financial_mutation_id',
26+
'created_at',
27+
'updated_at',
28+
];
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $namespace = 'payment';
34+
35+
}

src/Picqer/Financials/Moneybird/Model.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,15 @@ public function getUrl()
337337
return $this->url;
338338
}
339339

340+
/**
341+
* Determine if an attribute exists on the model
342+
*
343+
* @param $name
344+
* @return bool
345+
*/
346+
public function __isset($name)
347+
{
348+
return (isset($this->attributes[$name]) && !is_null($this->attributes[$name]));
349+
}
350+
340351
}

src/Picqer/Financials/Moneybird/Moneybird.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Picqer\Financials\Moneybird\Entities\RecurringSalesInvoice;
1919
use Picqer\Financials\Moneybird\Entities\SalesInvoice;
2020
use Picqer\Financials\Moneybird\Entities\SalesInvoiceDetail;
21+
use Picqer\Financials\Moneybird\Entities\SalesInvoicePayment;
2122
use Picqer\Financials\Moneybird\Entities\TaxRate;
2223
use Picqer\Financials\Moneybird\Entities\TypelessDocument;
2324
use Picqer\Financials\Moneybird\Entities\Webhook;
@@ -190,6 +191,14 @@ public function salesInvoiceDetail()
190191
return new SalesInvoiceDetail($this->connection);
191192
}
192193

194+
/**
195+
* @return SalesInvoicePayment
196+
*/
197+
public function salesInvoicePayment()
198+
{
199+
return new SalesInvoicePayment($this->connection);
200+
}
201+
193202
/**
194203
* @return TaxRate
195204
*/

tests/EntityTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public function testSalesInvoiceDetailEntity()
104104
$this->performEntityTest('\Picqer\Financials\Moneybird\Entities\SalesInvoiceDetail');
105105
}
106106

107+
public function testSalesInvoicePaymentEntity()
108+
{
109+
$this->performEntityTest('\Picqer\Financials\Moneybird\Entities\SalesInvoicePayment');
110+
}
111+
107112
public function testTaxRateEntity()
108113
{
109114
$this->performEntityTest('\Picqer\Financials\Moneybird\Entities\TaxRate');

0 commit comments

Comments
 (0)