-
Notifications
You must be signed in to change notification settings - Fork 12
/
quaderno_document.php
executable file
·136 lines (120 loc) · 2.99 KB
/
quaderno_document.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Quaderno Document
*
* @package Quaderno PHP
* @author Quaderno <support@quaderno.io>
* @copyright Copyright (c) 2021, Quaderno
* @license https://opensource.org/licenses/MIT The MIT License
*/
/* Interface that implements every document: invoices, expenses, credit notes, and estimates */
abstract class QuadernoDocument extends QuadernoModel
{
protected $payments_array = array();
/**
* @param array $newdata
*/
public function __construct($newdata)
{
parent::__construct($newdata);
if (isset($this->data['payments']))
{
foreach ($this->data['payments'] as $p)
array_push($this->payments_array, new QuadernoPayment($p));
}
}
/**
* @param QuadernoContact $contact
*
* @return bool
*/
public function addContact($contact)
{
$this->data['contact_id'] = $contact->id;
$this->data['contact_name'] = $contact->full_name;
return isset($this->data['contact_id']) && isset($this->data['contact_name']);
}
/**
* @param QuadernoDocumentItem $item
*
* @return bool
*/
public function addItem($item)
{
$length = isset($this->data['items_attributes']) ? count($this->data['items_attributes']) : 0;
if (isset($item->id) && $length > 0)
{
$index = 0;
for($index; $index < $length; $index ++){
if ($this->data['items_attributes'][$index]['id'] == $item->id) break;
}
$this->data['items_attributes'][$index] = $item->getArray();
return count($this->data['items_attributes']) == $length;
}
else
{
$this->data['items_attributes'][$length] = $item->getArray();
return count($this->data['items_attributes']) == $length + 1;
}
}
/**
* @param array|QuadernoDocumentItem $item
*
* @return bool
*/
public function updateItem($item)
{
if (is_array($item)) {
$item = new QuadernoDocumentItem($item);
}
return $this->addItem($item);
}
/**
* Interface - only subclasses which implement original ones (i.e. without exec-) can call these methods
*
* @param QuadernoPayment $payment
*
* @return bool
*/
protected function execAddPayment($payment)
{
$length = count($this->payments_array);
return array_push($this->payments_array, $payment) == $length + 1;
}
/**
* @return QuadernoPayment[]
*/
protected function execGetPayments()
{
return $this->payments_array;
}
/**
* @param QuadernoPayment $payment
*
* @return bool
*/
protected function execRemovePayment($payment)
{
$i = array_search($payment, $this->payments_array, true);
if ($i >= 0) $this->payments_array[$i]->mark_to_delete = true;
return ($i >= 0);
}
/**
* Deliver call for QuadernoDocument objects
* Deliver object to the contact email
* Returns true or false whether the request is accepted or not
*
* @return bool
*/
protected function execDeliver()
{
$return = false;
$response = QuadernoBase::deliver(static::$model, $this->id);
if (QuadernoBase::responseIsValid($response))
$return = true;
elseif (isset($response['data']['errors']))
$this->errors = $response['data']['errors'];
return $return;
}
}
?>