-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMdsColliveryService.php
More file actions
executable file
·382 lines (325 loc) · 10.4 KB
/
MdsColliveryService.php
File metadata and controls
executable file
·382 lines (325 loc) · 10.4 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* Copyright 2020 MDS Technologies (Pty) Ltd and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0).
* It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0
*
* @author MDS Collivery <integration@collivery.co.za>
* @copyright 2020 MDS Technologies (Pty) Ltd
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
namespace Mds;
use Exception;
use Mds\Prestashop\Collivery\ColliveryApi;
/**
* MdsColliveryService
*/
class MdsColliveryService
{
/**
* self
*/
private static $instance;
/**
* @type
*/
public $collivery;
/**
* @type
*/
public $cache;
/**
* @type
*/
public $validated_data;
/**
* @type
*/
public $settings;
/**
* @param array $settings
* @return MdsColliveryService
* @throws Exception
*/
public static function getInstance($settings = null)
{
if (! self::$instance) {
self::$instance = new self($settings);
}
return self::$instance;
}
private function __construct($settings)
{
$this->settings = $settings;
$this->converter = new UnitConverter();
$this->cache = new Cache();
$this->collivery = ColliveryApi::getInstance();
}
/**
* Adds the delivery request to MDS Collivery
*
* @param array $array
* @param bool $accept
* @return bool
*/
public function addCollivery(array $array, $accept = true)
{
$this->validated_data = $this->validateCollivery($array);
if (isset($this->validated_data['time_changed']) && $this->validated_data['time_changed'] == 1) {
$id = $this->validated_data['service'];
$services = $this->collivery->getServices();
if (!empty($this->settings["wording_$id"])) {
$reason = preg_replace(
'|' . preg_quote($services[$id]) . '|',
$this->settings["wording_$id"],
$this->validated_data['time_changed_reason']
);
} else {
$reason = $this->validated_data['time_changed_reason'];
}
$reason = preg_replace('|collivery|i', 'delivery', $reason);
$reason = preg_replace(
'|The delivery time has been CHANGED to|i',
'the approximate delivery day is',
$reason
);
}
$collivery_id = $this->collivery->addCollivery($this->validated_data);
if ($accept) {
return ($this->collivery->acceptCollivery($collivery_id)) ? $collivery_id : false;
}
return $collivery_id;
}
/**
* Validate delivery request before adding the request to MDS Collivery
*
* @param array $array
* @throws Exception
* @return bool|array
*/
public function validateCollivery(array $array)
{
if (empty($array['collivery_from'])) {
throw new Exception("Invalid collection address");
}
if (empty($array['collivery_to'])) {
throw new Exception("Invalid destination address");
}
if (empty($array['contact_from'])) {
throw new Exception("Invalid collection contact");
}
if (empty($array['contact_to'])) {
throw new Exception("Invalid destination contact");
}
if (empty($array['collivery_type'])) {
throw new Exception("Invalid parcel type");
}
if (empty($array['service'])) {
throw new Exception("Invalid service");
}
if ($array['cover'] != 1 && $array['cover'] != 0) {
throw new Exception("Invalid risk cover option");
}
if (empty($array['parcels']) || !is_array($array['parcels'])) {
throw new Exception("Invalid parcels");
}
return $this->collivery->validate($array);
}
/**
* Adds an address to MDS Collivery
*
* @param array $array
* @return array
* @throws Exception
*/
public function addColliveryAddress(array $array)
{
$towns = $this->collivery->getTowns();
$location_types = $this->collivery->getLocationTypes();
if (!is_numeric($array['town'])) {
$town_id = (int)array_search($array['town'], $towns);
} else {
$town_id = $array['town'];
}
$suburbs = $this->collivery->getSuburbs($town_id);
if (!is_numeric($array['suburb'])) {
$suburb_id = (int)array_search($array['suburb'], $suburbs);
} else {
$suburb_id = $array['suburb'];
}
if (!is_numeric($array['location_type'])) {
$location_type_id = (int)array_search($array['location_type'], $location_types);
} else {
$location_type_id = $array['location_type'];
}
if (empty($array['location_type']) || !isset($location_types[$location_type_id])) {
throw new Exception("Invalid location type");
}
if (empty($array['town']) || !isset($towns[$town_id])) {
throw new Exception("Invalid town");
}
if (empty($array['suburb']) || !isset($suburbs[$suburb_id])) {
throw new Exception("Invalid suburb");
}
if (empty($array['cellphone']) || !is_numeric($array['cellphone'])) {
throw new Exception("Invalid cellphone number");
}
if (empty($array['email']) || !filter_var($array['email'], FILTER_VALIDATE_EMAIL)) {
throw new Exception("Invalid email address");
}
$newAddress = array(
'company_name' => $array['company_name'],
'building' => $array['building'],
'street' => $array['street'],
'location_type' => $location_type_id,
'suburb_id' => $suburb_id,
'town_id' => $town_id,
'full_name' => $array['full_name'],
'phone' => (!empty($array['phone'])) ? $array['phone'] : '',
'cellphone' => $array['cellphone'],
'custom_id' => 'new_test_custom_id',
'email' => $array['email'],
);
// Before adding an address lets search MDS and see if we have already added this address
$searchAddresses = $this->searchAndMatchAddress(array(
'custom_id' => 'new_test_custom_id',
'suburb_id' => $suburb_id,
'town_id' => $town_id,
), $newAddress);
if (is_array($searchAddresses)) {
return $searchAddresses;
} else {
$this->cache->clear(array('addresses', 'contacts'));
return $this->collivery->addAddress($newAddress);
}
}
/**
* Searches for an address and matches each important field
*
* @param array $filters
* @param array $newAddress
* @return bool
*/
public function searchAndMatchAddress(array $filters, array $newAddress)
{
$searchAddresses = $this->collivery->getAddresses($filters);
if (!empty($searchAddresses)) {
$match = true;
$matchAddressFields = array(
'company_name' => 'company_name',
'building_details' => 'building',
'street' => 'street',
'location_type' => 'location_type',
'suburb_id' => 'suburb_id',
'town_id' => 'town_id',
'custom_id' => 'custom_id',
);
foreach ($searchAddresses as $address) {
foreach ($matchAddressFields as $mdsField => $newField) {
if ($address[$mdsField] != $newAddress[$newField]) {
$match = false;
}
}
if ($match) {
if (!isset($address['contact_id'])) {
$contacts = $this->collivery->getContacts($address['address_id']);
list($contact_id) = array_keys($contacts);
$address['contact_id'] = $contact_id;
}
return $address;
}
}
} else {
$this->collivery->clearErrors();
}
return false;
}
/**
* Get Town and Location Types for Checkout selects from MDS
*/
public function returnFieldDefaults()
{
$towns = $this->collivery->getTowns();
$location_types = $this->collivery->getLocationTypes();
return array(
'towns' => array_combine($towns, $towns),
'location_types' => array_combine($location_types, $location_types)
);
}
/**
* Returns the MDS Cache class
*
* @return \Mds\Cache
*/
public function returnCacheClass()
{
return $this->cache;
}
/**
* Returns the UnitConverter class
*
* @return UnitConverter
*/
public function returnConverterClass()
{
return $this->converter;
}
/**
* Gets default address of the MDS Account
*
* @return array
*/
public function returnDefaultAddress()
{
$default_address_id = $this->collivery->getDefaultAddressId();
$data = array(
'address' => $this->collivery->getAddress($default_address_id),
'default_address_id' => $default_address_id,
'contacts' => $this->collivery->getContacts($default_address_id)
);
return $data;
}
/**
* @return null|array
*/
public function returnColliveryValidatedData()
{
return $this->validated_data;
}
/**
* Adds markup to price
*
* @param $price
* @param $markup
* @return float|string
*/
public function addMarkup($price, $markup)
{
$price += $price * ($markup / 100);
return (isset($this->settings['round']) && $this->settings['round'] == 'yes') ?
$this->round($price) : $this->format($price);
}
/**
* Format a number with grouped thousands
*
* @param $price
* @return string
*/
public function format($price)
{
return number_format($price, 2, '.', '');
}
/**
* Rounds number up to the next highest integer
*
* @param $price
* @return float
*/
public function round($price)
{
return ceil($this->format($price));
}
}