1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* INJI |
4
|
|
|
* |
5
|
|
|
* @author Alexey Krupskiy <[email protected]> |
6
|
|
|
* @link http://inji.ru/ |
7
|
|
|
* @copyright 2017 Alexey Krupskiy |
8
|
|
|
* @license https://github.com/injitools/Inji/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ecommerce\DeliveryProvider; |
12
|
|
|
|
13
|
|
|
class CDEK extends \Ecommerce\DeliveryProvider { |
14
|
|
|
static $name = 'СДЭК - курьерская служба'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param \Ecommerce\Cart $cart |
18
|
|
|
*/ |
19
|
|
|
static function getTariff($cart) { |
|
|
|
|
20
|
|
|
$tariff = 138; |
21
|
|
|
$field = \Ecommerce\Delivery\Field::get('cdektype', 'code'); |
22
|
|
|
if (isset($cart->deliveryInfos[$field->id])) { |
23
|
|
|
$item = \Ecommerce\Delivery\Field\Item::get([['id', $cart->deliveryInfos[$field->id]->value], ['delivery_field_id', $field->id]]); |
24
|
|
|
if ($item) { |
25
|
|
|
$tariff = $item->data; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
return $tariff; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
static function request($cart) { |
|
|
|
|
32
|
|
|
$cityId = 0; |
33
|
|
|
$senderCity = 44; |
34
|
|
|
$cityItem = static::getCity($cart); |
35
|
|
|
if ($cityItem) { |
36
|
|
|
$cityId = json_decode($cityItem->data, true)['ID']; |
37
|
|
|
} |
38
|
|
|
if (!$cityId) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
$tariff = static::getTariff($cart); |
42
|
|
|
$config = self::config(); |
43
|
|
|
return \Cache::get('pickPointCalc', [ |
44
|
|
|
'login' => $config['authLogin'], |
45
|
|
|
'pass' => $config['authPassword'], |
46
|
|
|
'senderCity' => $senderCity, |
47
|
|
|
'cityId' => $cityId, |
48
|
|
|
'tariff' => $tariff, |
49
|
|
|
], function ($data) { |
50
|
|
|
$calc = new \Ecommerce\Vendor\CalculatePriceDeliveryCdek(); |
51
|
|
|
$calc->setAuth($data['login'], $data['pass']); |
52
|
|
|
$calc->setDateExecute(date('Y-m-d H:i:s')); |
53
|
|
|
$calc->setSenderCityId($data['senderCity']); |
54
|
|
|
//устанавливаем город-получатель |
55
|
|
|
$calc->setReceiverCityId($data['cityId']); |
56
|
|
|
$calc->setTariffId($data['tariff']); |
57
|
|
|
$calc->addGoodsItemBySize(3, 25, 25, 24); |
58
|
|
|
if ($calc->calculate()) { |
59
|
|
|
return $calc->getResult(); |
60
|
|
|
} else { |
61
|
|
|
//var_dump($tariff,$calc->getError()); |
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
static function calcPrice($cart) { |
|
|
|
|
70
|
|
|
$result = self::request($cart); |
71
|
|
|
$curId = $cart->delivery ? $cart->delivery->currency_id : 0; |
72
|
|
|
if (!empty($result['result']['price'])) { |
73
|
|
|
return new \Money\Sums([$curId => round($result['result']['price'] * 1.1, 2)]); |
74
|
|
|
} |
75
|
|
|
return new \Money\Sums([$curId => 0]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
static function deliveryTime($cart) { |
|
|
|
|
79
|
|
|
$result = self::request($cart); |
80
|
|
|
if (isset($result['result']['deliveryPeriodMin'])) { |
81
|
|
|
return [ |
82
|
|
|
'min' => $result['result']['deliveryPeriodMin'], |
83
|
|
|
'max' => $result['result']['deliveryPeriodMax'], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
return [ |
87
|
|
|
'min' => 0, |
88
|
|
|
'max' => 0, |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
static function availablePayTypeGroups($cart) { |
|
|
|
|
93
|
|
|
$tariff = static::getTariff($cart); |
94
|
|
|
if ($tariff == 139) { |
95
|
|
|
return ['*']; |
96
|
|
|
} |
97
|
|
|
return ['online']; |
98
|
|
|
} |
99
|
|
|
} |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.