|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAShowroom\TaxJarBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Webmozart\Assert\Assert; |
|
6
|
|
|
|
|
7
|
|
|
class Order |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Address |
|
11
|
|
|
*/ |
|
12
|
|
|
private $fromAddress; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Address |
|
16
|
|
|
*/ |
|
17
|
|
|
private $toAddress; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var float |
|
21
|
|
|
*/ |
|
22
|
|
|
private $amount; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var float |
|
26
|
|
|
*/ |
|
27
|
|
|
private $shipping; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Address[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $nexusAddresses = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var LineItem[] |
|
36
|
|
|
*/ |
|
37
|
|
|
private $lineItems = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $transactionId; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var \DateTime |
|
46
|
|
|
*/ |
|
47
|
|
|
private $transactionDate; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var float |
|
51
|
|
|
*/ |
|
52
|
|
|
private $salesTax; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return Address |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function getFromAddress() |
|
58
|
|
|
{ |
|
59
|
2 |
|
return $this->fromAddress; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param Address $fromAddress |
|
64
|
|
|
*/ |
|
65
|
16 |
|
public function setFromAddress(Address $fromAddress) |
|
66
|
|
|
{ |
|
67
|
16 |
|
$this->fromAddress = $fromAddress; |
|
68
|
16 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return Address |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function getToAddress() |
|
74
|
|
|
{ |
|
75
|
2 |
|
return $this->toAddress; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Address $toAddress |
|
80
|
|
|
*/ |
|
81
|
16 |
|
public function setToAddress(Address $toAddress) |
|
82
|
|
|
{ |
|
83
|
16 |
|
$this->toAddress = $toAddress; |
|
84
|
16 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return float |
|
88
|
|
|
*/ |
|
89
|
16 |
|
public function getAmount() |
|
90
|
|
|
{ |
|
91
|
16 |
|
return $this->amount; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param float $amount |
|
96
|
|
|
*/ |
|
97
|
14 |
|
public function setAmount($amount) |
|
98
|
|
|
{ |
|
99
|
14 |
|
Assert::float($amount); |
|
100
|
|
|
|
|
101
|
14 |
|
$this->amount = $amount; |
|
102
|
14 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return float |
|
106
|
|
|
*/ |
|
107
|
16 |
|
public function getShipping() |
|
108
|
|
|
{ |
|
109
|
16 |
|
return $this->shipping; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param float $shipping |
|
114
|
|
|
*/ |
|
115
|
14 |
|
public function setShipping($shipping) |
|
116
|
|
|
{ |
|
117
|
14 |
|
Assert::float($shipping); |
|
118
|
|
|
|
|
119
|
14 |
|
$this->shipping = $shipping; |
|
120
|
14 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return Address[] |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function getNexusAddresses() |
|
126
|
|
|
{ |
|
127
|
2 |
|
return $this->nexusAddresses; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param Address[] $nexusAddresses |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function setNexusAddresses(array $nexusAddresses) |
|
134
|
|
|
{ |
|
135
|
2 |
|
Assert::allIsInstanceOf($nexusAddresses, Address::class); |
|
136
|
|
|
|
|
137
|
2 |
|
$this->nexusAddresses = $nexusAddresses; |
|
138
|
2 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return LineItem[] |
|
142
|
|
|
*/ |
|
143
|
2 |
|
public function getLineItems() |
|
144
|
|
|
{ |
|
145
|
2 |
|
return $this->lineItems; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param LineItem[] $lineItems |
|
150
|
|
|
*/ |
|
151
|
2 |
|
public function setLineItems(array $lineItems) |
|
152
|
|
|
{ |
|
153
|
2 |
|
Assert::allIsInstanceOf($lineItems, LineItem::class); |
|
154
|
|
|
|
|
155
|
2 |
|
$this->lineItems = $lineItems; |
|
156
|
2 |
|
} |
|
157
|
|
|
|
|
158
|
14 |
|
public function addLineItem(LineItem $lineItem) |
|
159
|
|
|
{ |
|
160
|
14 |
|
$this->lineItems[] = $lineItem; |
|
161
|
14 |
|
} |
|
162
|
|
|
|
|
163
|
14 |
|
public function addNexusAddress($id, Address $address) |
|
164
|
|
|
{ |
|
165
|
14 |
|
$this->nexusAddresses[$id] = $address; |
|
166
|
14 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
2 |
|
public function getTransactionId() |
|
172
|
|
|
{ |
|
173
|
2 |
|
return $this->transactionId; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param string $transactionId |
|
178
|
|
|
*/ |
|
179
|
4 |
|
public function setTransactionId($transactionId) |
|
180
|
|
|
{ |
|
181
|
4 |
|
$this->transactionId = $transactionId; |
|
182
|
4 |
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return \DateTime |
|
186
|
|
|
*/ |
|
187
|
2 |
|
public function getTransactionDate() |
|
188
|
|
|
{ |
|
189
|
2 |
|
return $this->transactionDate; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param \DateTime $transactionDate |
|
194
|
|
|
*/ |
|
195
|
4 |
|
public function setTransactionDate(\DateTime $transactionDate) |
|
196
|
|
|
{ |
|
197
|
4 |
|
$this->transactionDate = $transactionDate; |
|
198
|
4 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return float |
|
202
|
|
|
*/ |
|
203
|
2 |
|
public function getSalesTax() |
|
204
|
|
|
{ |
|
205
|
2 |
|
return $this->salesTax; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param float $salesTax |
|
210
|
|
|
*/ |
|
211
|
4 |
|
public function setSalesTax($salesTax) |
|
212
|
|
|
{ |
|
213
|
4 |
|
Assert::float($salesTax); |
|
214
|
|
|
|
|
215
|
4 |
|
$this->salesTax = $salesTax; |
|
216
|
4 |
|
} |
|
217
|
|
|
|
|
218
|
16 |
|
public function toArray() |
|
219
|
|
|
{ |
|
220
|
16 |
|
$result = []; |
|
221
|
|
|
|
|
222
|
16 |
|
if (!empty($this->fromAddress)) { |
|
223
|
14 |
|
$result = array_merge($result, $this->fromAddress->toArray('from_')); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
16 |
|
if (!empty($this->toAddress)) { |
|
227
|
14 |
|
$result = array_merge($result, $this->toAddress->toArray('to_')); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
16 |
|
if (!empty($this->salesTax)) { |
|
231
|
4 |
|
$result['sales_tax'] = $this->salesTax; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
16 |
|
$result = array_merge($result, [ |
|
235
|
|
|
'line_items' => array_map(function(LineItem $elem) { |
|
236
|
14 |
|
return $elem->toArray(); |
|
237
|
16 |
|
}, $this->lineItems), |
|
238
|
16 |
|
'amount' => $this->getAmount(), |
|
239
|
16 |
|
'shipping' => $this->getShipping(), |
|
240
|
16 |
|
'nexus_addresses' => array_map(function(Address $elem, $id) { |
|
241
|
14 |
|
return array_merge($elem->toArray(), ['id' => $id, ]); |
|
242
|
16 |
|
}, $this->nexusAddresses, array_keys($this->nexusAddresses)), |
|
243
|
|
|
]); |
|
244
|
|
|
|
|
245
|
16 |
|
if (!empty($this->transactionId)) { |
|
246
|
4 |
|
$result['transaction_id'] = $this->transactionId; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
16 |
|
if (!empty($this->transactionDate)) { |
|
250
|
4 |
|
$result['transaction_date'] = $this->transactionDate->format(\DateTime::ISO8601); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
16 |
|
return $result; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
8 |
|
public function getCacheKey() |
|
257
|
|
|
{ |
|
258
|
8 |
|
return sprintf('order_%s', md5(json_encode($this->toArray()))); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|