Skip to content

Commit c029cbc

Browse files
committed
Add updateItem method
1 parent d193eb8 commit c029cbc

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "quaderno/quaderno",
33
"type": "library",
44
"description": "PHP wrapper for the Quaderno REST API",
5-
"version": "1.6.2",
5+
"version": "1.6.3",
66
"keywords": ["billing","receipts", "invoices", "stripe", "paypal", "braintree", "gocardless", "vat", "gst"],
77
"homepage": "https://github.com/quaderno/quaderno-php",
88
"license": "MIT",

quaderno_class.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function __get($name)
2828
return array_key_exists($name, $this->data) ? $this->data[$name] : null;
2929
}
3030

31-
public function __isset($name)
32-
{
33-
return array_key_exists($name, $this->data);
34-
}
31+
public function __isset($name)
32+
{
33+
return array_key_exists($name, $this->data);
34+
}
3535

3636
protected function getArray()
3737
{

quaderno_document.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,28 @@ public function addContact($contact)
3333
public function addItem($item)
3434
{
3535
$length = isset($this->data['items_attributes']) ? count($this->data['items_attributes']) : 0;
36-
$this->data['items_attributes'][$length] = $item->getArray();
37-
return count($this->data['items_attributes']) == $length + 1;
36+
37+
if (isset($item->id) && $length > 0)
38+
{
39+
$index = 0;
40+
41+
for($index; $index < $length; $index ++){
42+
if ($this->data['items_attributes'][$index]['id'] == $item->id) break;
43+
}
44+
45+
$this->data['items_attributes'][$index] = $item->getArray();
46+
return count($this->data['items_attributes']) == $length;
47+
}
48+
else
49+
{
50+
$this->data['items_attributes'][$length] = $item->getArray();
51+
return count($this->data['items_attributes']) == $length + 1;
52+
}
53+
}
54+
55+
public function updateItem($item)
56+
{
57+
return $this->addItem(new QuadernoDocumentItem($item));
3858
}
3959

4060
/* Interface - only subclasses which implement original ones (i.e. without exec-) can call these methods */

quaderno_model.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,19 @@ public function save()
113113
if ($new_data) $this->data = $new_data->data;
114114
}
115115

116+
$new_data = (isset($this->data['items_attributes']) && !$new_object);
117+
116118
/**
117119
* 3rd step - Update object
118120
* Update object - This is only necessary when it's not a new object, or new payments have been created.
119121
*/
120122
if (!$new_object || $new_data)
121123
{
122-
$response = QuadernoBase::save(static::$model, $this->data, $this->id);
124+
$body_data = $this->data;
125+
126+
if (isset($this->items)) unset($body_data['items']);
127+
128+
$response = QuadernoBase::save(static::$model, $body_data, $this->id);
123129

124130
if (QuadernoBase::responseIsValid($response))
125131
{

0 commit comments

Comments
 (0)