1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telefonica\Services; |
4
|
|
|
|
5
|
|
|
use Informate\Models\Refund; |
6
|
|
|
use SierraTecnologia\Crypto\Services\Crypto; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use Telefonica\Repositories\PersonRepository; |
9
|
|
|
|
10
|
|
|
class PersonService |
11
|
|
|
{ |
12
|
|
|
public function __construct( |
13
|
|
|
PersonRepository $personRepository |
14
|
|
|
) { |
15
|
|
|
$this->repo = $personRepository; |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get all Persons. |
20
|
|
|
* |
21
|
|
|
* @return Collection |
22
|
|
|
*/ |
23
|
|
|
public function all() |
24
|
|
|
{ |
25
|
|
|
return $this->repo->all(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get all Persons. |
30
|
|
|
* |
31
|
|
|
* @return Illuminate\Pagination\LengthAwarePaginator |
32
|
|
|
*/ |
33
|
|
|
public function paginated() |
34
|
|
|
{ |
35
|
|
|
return $this->repo->paginated(\Illuminate\Support\Facades\Config::get('cms.pagination', 25)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Find the Person by ID. |
40
|
|
|
* |
41
|
|
|
* @param int $id |
42
|
|
|
* |
43
|
|
|
* @return Persons |
44
|
|
|
*/ |
45
|
|
|
public function find($id) |
46
|
|
|
{ |
47
|
|
|
return $this->repo->find($id); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Search the orders. |
52
|
|
|
* |
53
|
|
|
* @param array $payload |
54
|
|
|
* |
55
|
|
|
* @return Collection |
56
|
|
|
*/ |
57
|
|
|
public function search($payload) |
58
|
|
|
{ |
59
|
|
|
return $this->repo->search($payload, \Illuminate\Support\Facades\Config::get('cms.pagination', 25)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create an order. |
64
|
|
|
* |
65
|
|
|
* @param array $payload |
66
|
|
|
* |
67
|
|
|
* @return Persons |
68
|
|
|
*/ |
69
|
|
|
public function create($payload) |
70
|
|
|
{ |
71
|
|
|
$order = $this->repo->store($payload); |
72
|
|
|
|
73
|
|
|
$this->logistics->orderCreated($order); |
|
|
|
|
74
|
|
|
|
75
|
|
|
return $order; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Update an order. |
80
|
|
|
* |
81
|
|
|
* @param int $id |
82
|
|
|
* @param array $payload |
83
|
|
|
* |
84
|
|
|
* @return Persons |
85
|
|
|
*/ |
86
|
|
|
public function update($id, $payload) |
87
|
|
|
{ |
88
|
|
|
$order = $this->find($id); |
89
|
|
|
|
90
|
|
|
if (isset($payload['is_shipped']) && $payload['is_shipped'] !== false) { |
91
|
|
|
$this->logistics->shipPerson($order); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->repo->update($order, $payload); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Cancel an order. |
99
|
|
|
* |
100
|
|
|
* @param int $id |
|
|
|
|
101
|
|
|
* |
102
|
|
|
* @return Persons |
103
|
|
|
*/ |
104
|
|
|
public function cancel($orderId) |
105
|
|
|
{ |
106
|
|
|
$order = $this->repo->find($orderId); |
107
|
|
|
|
108
|
|
|
if ($order->status != 'complete') { |
109
|
|
|
$this->logistics->cancelPerson($order); |
110
|
|
|
|
111
|
|
|
if ($order->hasActivePersonItems()) { |
112
|
|
|
$refund = $this->transactions->refund($order->transaction('uuid'), $order->remainingValue()); |
|
|
|
|
113
|
|
|
|
114
|
|
|
if ($refund) { |
115
|
|
|
$refundRecord = app(Refund::class)->create( |
116
|
|
|
[ |
117
|
|
|
'transaction_id' => $order->transaction('id'), |
118
|
|
|
'provider_id' => $refund->id, |
119
|
|
|
'uuid' => Crypto::uuid(), |
120
|
|
|
'amount' => $refund->amount, |
121
|
|
|
'provider' => 'SierraTecnologia', |
122
|
|
|
'charge' => $refund->charge, |
123
|
|
|
'currency' => $refund->currency, |
124
|
|
|
] |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
foreach ($order->items as $item) { |
128
|
|
|
$item->update( |
129
|
|
|
[ |
130
|
|
|
'was_refunded' => true, |
131
|
|
|
'status' => 'cancelled', |
132
|
|
|
'refund_id' => $refundRecord->id, |
133
|
|
|
] |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->update( |
138
|
|
|
$order->id, [ |
139
|
|
|
'status' => 'cancelled', |
140
|
|
|
'is_shipped' => false, |
141
|
|
|
] |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: