-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_ems.module
239 lines (189 loc) · 6.67 KB
/
commerce_ems.module
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
<?php
/**
* @file
* EMS shipping module main file.
*/
/**
* Implementation of hook_menu().
*/
function commerce_ems_menu() {
$items = array();
$items['admin/commerce/config/shipping/methods/ems/edit'] = array(
'title' => 'Edit',
'description' => 'Configure the EMS shipping method.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_ems_settings_form'),
'access arguments' => array('administer shipping'),
'file' => 'commerce_ems.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 0,
);
return $items;
}
/**
* Implements hook_commerce_shipping_method_info().
*/
function commerce_ems_commerce_shipping_method_info() {
$shipping_methods = array();
$shipping_methods['ems'] = array(
'title' => t('EMS shipping method'),
'description' => t('EMS Russian Post.'),
);
return $shipping_methods;
}
/**
* Implements hook_commerce_shipping_service_info().
*/
function commerce_ems_commerce_shipping_service_info() {
$shipping_services = array();
$shipping_services['ems_shipping_service'] = array(
'title' => t('EMS shipping service'),
'description' => t('EMS Russian Post.'),
'display_title' => t('EMS Russian Post'),
'shipping_method' => 'ems',
'price_component' => 'shipping',
'callbacks' => array(
'rate' => 'commerce_ems_service_rate',
),
);
return $shipping_services;
}
/**
* Shipping service callback: returns a base price array for a shipping service
* calculated for the given order.
*/
function commerce_ems_service_rate($shipping_service, $order) {
$profile_id = $order->commerce_customer_billing['und'][0]['profile_id'];
$customer = commerce_customer_profile_load($profile_id);
$shipping_city = taxonomy_term_load($customer->field_region_and_city['und'][0]['tid']);
$shipping_region = array_shift(taxonomy_get_parents($shipping_city->tid));
$shipping_address = array (
'region' => _ems_cleanup_city(drupal_strtolower($shipping_region->name)),
'city' => drupal_strtolower($shipping_city->name),
);
$total_weight = 0;
$default_weight = variable_get('commerce_ems_default_weight', 0);
$total_weight = commerce_physical_order_weight($order, 'kg'); // this returns $total_weight['unit'] and $total_weight['weight']
$total_weight = round($total_weight['weight'], 2);
$store_default_addr = array(
'region' => variable_get('commerce_ems_region', ''),
'city' => variable_get('commerce_ems_city', ''),
);
$from = _commerce_ems_get_address($store_default_addr);
if (!$from) {
watchdog('ems', 'No EMS pickup address specified', array(), WATCHDOG_ERROR);
return array();
}
$to = _commerce_ems_get_address($shipping_address);
$reply = drupal_http_request('http://emspost.ru/api/rest?method=ems.calculate&from='. $from .'&to='. $to .'&weight=' . $total_weight . '&type=att');
if ($reply->code == 200) {
$ems_result = json_decode($reply->data);
$ems_result = $ems_result->rsp;
} else {
$ems_result = new StdClass();
$fixed = variable_get('commerce_ems_fixed_price', 0);
if ($fixed) {
$ems_result->price = $fixed;
} else {
// return no quotes for EMS
drupal_set_message(t('EMS website is down, so we can\'t calculate the delivery price. Try to call us. Sorry for the inconvenience!'), 'error');
return array();
}
}
if ($ems_result->stat == 'fail') {
watchdog('ems', $ems_result->err->msg, array(), WATCHDOG_ERROR);
return array();
}
$img = '<img src="/'. drupal_get_path('module', 'commerce_ems') . '/ems-icon.png' .'" />';
if ($reply->code == 200) {
$option_label = $img . t('EMS Russian Post');
} else {
$option_label = $img . t('EMS Russian Post: <span style="color:red">no connection to EMS website</span>, fixed price');
}
$extra_charge = (int) variable_get('commerce_ems_extra_charge', 0);
// Extra charge not added if request failed, as in that case fixed price
// can be used
if ($extra_charge && ($reply->code == 200)) {
$charge_type = variable_get('commerce_ems_extra_charge_type', 'percent');
if ($charge_type == 'percent') {
$ems_result->price += ($ems_result->price * 0.01 * $extra_charge);
} else {
// fixed amount
$ems_result->price += $extra_charge;
}
}
/* $insurance = variable_get('commerce_ems_insurance', TRUE);
// Insurance is not added if request to EMS failed, too
if ($insurance && ($reply->code == 200)) {
$subtotal = 0;
foreach ($products as $item) {
$product = node_load($item->nid);
$subtotal += ($product->sell_price * $item->qty);
}
$ems_result->price += $subtotal * 0.01;
}*/
$ems_result->price = ceil($ems_result->price / 10) * 10;
return array(
'amount' => commerce_currency_decimal_to_amount($ems_result->price, 'RUB'),
'currency_code' => 'RUB',
'data' => array(),
);
}
function _ems_get_points($type = 'regions') {
if ($type != 'regions' && $type != 'cities') return FALSE;
$r = drupal_http_request('http://emspost.ru/api/rest/?method=ems.get.locations&type='.$type.'&plain=true');
if ($r->code == 200) {
$o = json_decode($r->data);
$ems_russia = $o->rsp->locations;
}
$zones = array();
foreach ($ems_russia as $p) {
$p->name = _ems_cleanup_city(drupal_strtolower($p->name));
$zones[$p->name] = $p->value;
}
cache_set('ems_' . $type, $zones);
return $zones;
}
function _ems_cleanup_city($city) {
$w = variable_get('commerce_ems_city_cleanup', '');
if ($w) {
$w = explode('|', $w);
$city = strtolower($city);
foreach ($w as $remove) {
$city = preg_replace($remove, '', $city);
}
}
$city = trim($city);
return $city;
}
function _commerce_ems_get_address($addr) {
// EMS and Ubercart .cif addresses contain different region names
$ems_unique_regions = array(
// 'Region at site' => 'Region at EMS'
'саха /якутия/' => 'саха (якутия)',
'северная осетия - алания' => 'северная осетия-алания',
'ханты-мансийский - югра' => 'ханты-мансийский-югра',
'таймырский (долгано-ненецкий)' => 'таймырский долгано-ненецкий район',
'красноярский край' => 'красноярский край [кроме таймырского района]',
);
if (empty($ems_unique_regions[$addr['region']])) {
// echo $addr['region'];
$region = $addr['region'];
}
else {
$region = $ems_unique_regions[$addr['region']];
}
$addr['city'] = trim(drupal_strtolower($addr['city']));
$ems_regiones = _ems_get_points('regions');
$ems_cities = _ems_get_points('cities');
// var_dump($ems_cities);
if(!empty($ems_cities[$addr['city']])) {
// direct city mapping was succesfull
$ret = $ems_cities[$addr['city']];
} else {
// if fails, map to region
$ret = $ems_regiones[$addr['region']];
}
return $ret;
}