Completed
Push — master ( 4cdff0...99da5f )
by Jean C.
02:24
created

Orders::getUpdatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Moip\Resource;
4
5
use ArrayIterator;
6
use stdClass;
7
8
class Orders extends MoipResource {
9
    /**
10
     * @const string
11
     */
12
    const PATH = 'orders';
13
14
    /**
15
     * Defines what kind of payee as pripmary.
16
     *
17
     * @const string
18
     */
19
    const RECEIVER_TYPE_PRIMARY = 'PRIMARY';
20
21
    /**
22
     * Defines what kind of payee as secundary.
23
     *
24
     * @const string
25
     */
26
    const RECEIVER_TYPE_SECONDARY = 'SECONDARY';
27
28
    /**
29
     * Currency used in the application.
30
     *
31
     * @const string
32
     */
33
    const AMOUNT_CURRENCY = 'BRL';
34
35
    /**
36
     * @var \Moip\Resource\Orders
37
     **/
38
    private $orders;
39
40
    /**
41
     * Adds a new item to order.
42
     *
43
     * @param string $product Name of the product.
44
     * @param integer $quantity Product Quantity.
45
     * @param string $detail Additional product description.
46
     * @param integer $price Initial value of the item.
47
     *
48
     * @return $this
49
     */
50
    public function addItem($product, $quantity, $detail, $price) {
51
        $item = new stdClass();
52
        $item->product = $product;
53
        $item->quantity = $quantity;
54
        $item->detail = $detail;
55
        $item->price = $price;
56
57
        $this->data->items[] = $item;
58
59
        return $this;
60
    }
61
62
    /**
63
     *  Adds a new receiver to order.
64
     *
65
     * @param string $moipAccount Id MoIP MoIP account that will receive payment values.
66
     * @param string $type Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY.
67
     *
68
     * @return $this
69
     */
70
    public function addReceiver($moipAccount, $type = self::RECEIVER_TYPE_PRIMARY) {
71
        $receiver = new stdClass();
72
        $receiver->moipAccount = new stdClass();
73
        $receiver->moipAccount->id = $moipAccount;
74
        $receiver->type = $type;
75
76
        $this->data->receivers[] = $receiver;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Initialize necessary used in some functions.
83
     */
84
    protected function initialize() {
85
        $this->data = new stdClass();
86
        $this->data->ownId = null;
87
        $this->data->amount = new stdClass();
88
        $this->data->amount->currency = self::AMOUNT_CURRENCY;
89
        $this->data->amount->subtotals = new stdClass();
90
        $this->data->items = [];
91
        $this->data->receivers = [];
92
    }
93
94
    /**
95
     * Initialize necessary used in some functions.
96
     */
97
    private function initializeSubtotals() {
98
        if (!isset($this->data->subtotals)) {
99
            $this->data->subtotals = new stdClass();
100
        }
101
    }
102
103
    /**
104
     * Mount the structure of order.
105
     *
106
     * @param \stdClass $response
107
     *
108
     * @return Orders Response order.
109
     */
110
    protected function populate(stdClass $response) {
111
        $this->orders = clone $this;
112
        $this->orders->data->id = $response->id;
113
        $this->orders->data->amount->total = $response->amount->total;
114
        $this->orders->data->amount->fees = $response->amount->fees;
115
        $this->orders->data->amount->refunds = $response->amount->refunds;
116
        $this->orders->data->amount->liquid = $response->amount->liquid;
117
        $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers;
118
        $this->orders->data->amount->subtotals = $response->amount->subtotals;
119
120
        $customer = new Customer($this->moip);
121
        $customer->populate($response->customer);
122
123
        $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class);
124
        $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class);
125
        $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class);
126
        $this->orders->data->events = $this->structure($response, Event::PATH, Event::class);
127
128
        $this->orders->data->items = $response->items;
129
        $this->orders->data->receivers = $response->receivers;
130
        $this->orders->data->createdAt = $response->createdAt;
131
        $this->orders->data->status = $response->status;
132
        $this->orders->data->_links = $response->_links;
133
134
        return $this->orders;
135
    }
136
137
    /**
138
     * Structure resource.
139
     *
140
     * @param stdClass $response
141
     * @param string $resource
142
     * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class
143
     *
144
     * @return array
145
     */
146
    private function structure(stdClass $response, $resource, $class) {
147
        $structures = [];
148
149
        foreach ($response->$resource as $responseResource) {
150
            $structure = new $class($this->orders->moip);
151
            $structure->populate($responseResource);
152
153
            $structures[] = $structure;
154
        }
155
156
        return $structures;
157
    }
158
159
    /**
160
     * Create a new order in MoIP.
161
     *
162
     * @return stdClass
163
     */
164
    public function create() {
165
        return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH));
166
    }
167
168
    /**
169
     * Get an order in MoIP.
170
     *
171
     * @param string $id Id MoIP order id
172
     *
173
     * @return stdClass
174
     */
175
    public function get($id) {
176
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id));
177
    }
178
179
    /**
180
     * Get MoIP order id.
181
     *
182
     * @return string
183
     */
184
    public function getId() {
185
        return $this->getIfSet('id');
186
    }
187
188
    /**
189
     * Get own request id. external reference.
190
     *
191
     * @return string
192
     */
193
    public function getOwnId() {
194
        return $this->getIfSet('ownId');
195
    }
196
197
    /**
198
     * Get total value of order.
199
     *
200
     * @return int|float
201
     */
202
    public function getAmountTotal() {
203
        return $this->getIfSet('total', $this->data->amount);
204
    }
205
206
    /**
207
     * Get total value of MoIP rate.
208
     *
209
     * @return int|float
210
     */
211
    public function getAmountFees() {
212
        return $this->getIfSet('feed', $this->data->amount);
213
    }
214
215
    /**
216
     * Get total amount of refunds.
217
     *
218
     * @return int|float
219
     */
220
    public function getAmountRefunds() {
221
        return $this->getIfSet('refunds', $this->data->amount);
222
    }
223
224
    /**
225
     * Get net total value.
226
     *
227
     * @return int|float
228
     */
229
    public function getAmountLiquid() {
230
        return $this->getIfSet('liquid', $this->data->amount);
231
    }
232
233
    /**
234
     * Get sum of amounts received by other recipients. Used in Marketplaces.
235
     *
236
     * @return int|float
237
     */
238
    public function getAmountOtherReceivers() {
239
        return $this->getIfSet('otherReceivers', $this->data->amount);
240
    }
241
242
    /**
243
     * Get currency used in the application. Possible values: BRL.
244
     *
245
     * @return string
246
     */
247
    public function getCurrenty() {
248
        return $this->getIfSet('currency', $this->data->amount);
249
    }
250
251
    /**
252
     * Get greight value of the item will be added to the value of the items.
253
     *
254
     * @return int|float
255
     */
256
    public function getSubtotalShipping() {
257
        $this->initializeSubtotals();
258
259
        return $this->getIfSet('shipping', $this->data->amount->subtotals);
260
    }
261
262
    /**
263
     * Get Additional value to the item will be added to the value of the items.
264
     *
265
     * @return int|float
266
     */
267
    public function getSubtotalAddition() {
268
        $this->initializeSubtotals();
269
270
        return $this->getIfSet('addition', $this->data->amount->subtotals);
271
    }
272
273
    /**
274
     * Get discounted value of the item will be subtracted from the total value of the items.
275
     *
276
     * @return int|float
277
     */
278
    public function getSubtotalDiscount() {
279
        $this->initializeSubtotals();
280
281
        return $this->getIfSet('discount', $this->data->amount->subtotals);
282
    }
283
284
    /**
285
     * Get summing the values of all items.
286
     *
287
     * @return int|float
288
     */
289
    public function getSubtotalItems() {
290
        $this->initializeSubtotals();
291
292
        return $this->getIfSet('items', $this->data->amount->subtotals);
293
    }
294
295
    /**
296
     * Ger structure item information request.
297
     *
298
     * @return \ArrayIterator
299
     */
300
    public function getItemIterator() {
301
        return new ArrayIterator($this->data->items);
302
    }
303
304
    /**
305
     * Get Customer associated with the request.
306
     *
307
     * @return \Moip\Resource\Customer
308
     */
309
    public function getCustomer() {
310
        return $this->data->customer;
311
    }
312
313
    /**
314
     * Get payments associated with the request.
315
     *
316
     * @return ArrayIterator
317
     */
318
    public function getPaymentIterator() {
319
        return new ArrayIterator($this->data->payments);
320
    }
321
322
    /**
323
     * Get recipient structure of payments.
324
     *
325
     * @return ArrayIterator
326
     */
327
    public function getReceiverIterator() {
328
        return new ArrayIterator($this->data->receivers);
329
    }
330
331
    /**
332
     * Get releases associated with the request.
333
     *
334
     * @return ArrayIterator
335
     */
336
    public function getEventIterator() {
337
        return new ArrayIterator($this->data->events);
338
    }
339
340
    /**
341
     * Get repayments associated with the request.
342
     *
343
     * @return ArrayIterator
344
     */
345
    public function getRefundIterator() {
346
        return new ArrayIterator($this->data->refunds);
347
    }
348
349
    /**
350
     * Get order status.
351
     * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED.
352
     *
353
     * @return string
354
     */
355
    public function getStatus() {
356
        return $this->getIfSet('status');
357
    }
358
359
    /**
360
     * Get date of resource creation.
361
     *
362
     * @return \DateTime
363
     */
364
    public function getCreatedAt() {
365
        return $this->getIfSetDateTime('createdAt');
366
    }
367
368
    /**
369
     * Get updated resource.
370
     *
371
     * @return \DateTime
372
     */
373
    public function getUpdatedAt() {
374
        return $this->getIfSetDateTime('updatedAt');
375
    }
376
377
    /**
378
     * Get hypermedia link structure (HATEOAS) resource Orders.
379
     *
380
     * @return \stdClass
381
     */
382
    public function getLinks() {
383
        return $this->getIfSet('_links');
384
    }
385
386
    /**
387
     * Structure of payment.
388
     *
389
     * @return \Moip\Resource\Payment
390
     */
391
    public function payments() {
392
        $payment = new Payment($this->moip);
393
        $payment->setOrder($this);
394
395
        return $payment;
396
    }
397
398
    /**
399
     * Structure of refund.
400
     *
401
     * @return \Moip\Resource\Refund
402
     */
403
    public function refunds() {
404
        $refund = new Refund($this->moip);
405
        $refund->setOrder($this);
406
407
        return $refund;
408
    }
409
410
    /**
411
     * Set additional value to the item will be added to the value of the items.
412
     *
413
     * @param int|float $value additional value to the item.
414
     * @return $this
415
     */
416
    public function setAddition($value) {
417
        $this->data->subtotals->addition = (float)$value;
418
419
        return $this;
420
    }
421
422
    /**
423
     * Set customer associated with the order.
424
     *
425
     * @param \Moip\Resource\Customer $customer customer associated with the request.
426
     * @return $this
427
     */
428
    public function setCustomer(Customer $customer) {
429
        $this->data->customer = $customer;
430
431
        return $this;
432
    }
433
434
    /**
435
     * Set discounted value of the item will be subtracted from the total value of the items.
436
     * @param int|float $value discounted value.
437
     * @return $this
438
     */
439
    public function setDiscount($value) {
440
441
        $this->data->amount->subtotals->discount = (float)$value;
442
443
        return $this;
444
445
446
    }
447
448
    /**
449
     * Set discounted value of the item will be subtracted from the total value of the items.
450
     * @deprecated
451
     * @param int|float $value discounted value.
452
     * @return $this
453
     */
454
    public function setDiscont($value) {
455
        $this->setDiscount($value);
456
457
        return $this;
458
    }
459
460
    /**
461
     * Set own request id. external reference.
462
     *
463
     * @param string $ownId external reference.
464
     * @return $this
465
     */
466
    public function setOwnId($ownId) {
467
        $this->data->ownId = $ownId;
468
469
        return $this;
470
    }
471
472
    /**
473
     * Set shipping Amount.
474
     *
475
     * @param float $value shipping Amount.
476
     *
477
     * @return $this
478
     */
479
    public function setShippingAmount($value) {
480
        $this->data->amount->subtotals->shipping = (float)$value;
481
482
        return $this;
483
    }
484
}
485