-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_rajaongkir.module
366 lines (348 loc) · 12.5 KB
/
commerce_rajaongkir.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
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
<?php
/**
* @file
* Defines Commerce RajaOngkir shipping method for Drupal Commerce.
*/
/**
* Implements hook_help().
*/
function commerce_rajaongkir_help($path, $arg) {
switch ($path) {
case 'admin/help#commerce_rajaongkir':
$output = FALSE;
$filepath = dirname(__FILE__) . '/README.txt';
if (file_exists($filepath)) {
$readme = file_get_contents($filepath);
$output = '<pre>' . $readme . '</pre>';
}
return $output;
}
}
/**
* Implements hook_menu().
*/
function commerce_rajaongkir_menu() {
$items = array();
$items['admin/commerce/config/shipping/methods/commerce-rajaongkir/edit'] = array(
'title' => 'Edit',
'description' => 'Configure Commerce RajaOngkir shipping method.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_rajaongkir_settings_form'),
'access arguments' => array('administer shipping'),
'file' => 'includes/commerce_rajaongkir.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
);
return $items;
}
/**
* Implements hook_commerce_shipping_method_info().
*/
function commerce_rajaongkir_commerce_shipping_method_info() {
$shipping_methods = array();
$shipping_methods['commerce_rajaongkir'] = array(
'title' => t('Commerce RajaOngkir'),
'description' => t('Integrates RajaOngkir shipping service API with Commerce shipping.'),
);
return $shipping_methods;
}
/**
* Implements hook_commerce_shipping_service_info().
*/
function commerce_rajaongkir_commerce_shipping_service_info() {
$services = commerce_rajaongkir_service_list();
$couriers = array_filter(variable_get('commerce_rajaongkir_courier', array()));
if (variable_get('commerce_rajaongkir_account_type', array()) === 'starter') {
$couriers = array_intersect($couriers, array('JNE', 'POS', 'TIKI'));
}
$services_couriers = array_intersect_key($services, $couriers);
$shipping_services = array();
if ($services_couriers) {
foreach ($services_couriers as $services_courier) {
foreach ($services_courier as $service_courier) {
$shipping_services[$service_courier] = array(
'title' => $service_courier,
'shipping_method' => 'commerce_rajaongkir',
'callbacks' => array(
'rate' => 'commerce_rajaongkir_service_rate',
),
);
}
unset($service_courier);
}
unset($services_courier);
}
return $shipping_services;
}
/**
* @return
* Service option list.
*/
function commerce_rajaongkir_service_list() {
return array(
'CAHAYA' => array(
t('CAHAYA REG (Regular Service)'),
),
'ESL' => array(
t('ESL RPX/RDX (Paket Dokumen/Barang)'),
),
'INDAH' => array(
t('INDAH Paket Darat (HARGA PER 1-10 KG PRIORITAS)'),
t('INDAH Paket Darat (PRIORITAS)'),
t('INDAH Paket Motor (MOTOR BESAR)'),
t('INDAH Paket Motor (MOTOR KECIL)'),
t('INDAH Paket Udara (UDARA REGULER)'),
t('INDAH Trucking (TRUCKING CDD)'),
t('INDAH Trucking (TRUCKING FUSO)'),
),
'JET' => array(
t('JET CRG (Cargo)'),
t('JET PRI (Priority)'),
t('JET REG (Regular)'),
),
'JNE' => array(
t('JNE CTC (JNE City Courier)'),
t('JNE CTCBDO (Unknown service)'),
t('JNE CTCOKE (JNE City Courier)'),
t('JNE CTCSPS (JNE City Courier)'),
t('JNE CTCYES (JNE City Courier)'),
t('JNE JTR<150 (JNE Trucking)'),
t('JNE JTR>250 (JNE Trucking)'),
t('JNE JTR250 (JNE Trucking)'),
t('JNE JTR (JNE Trucking)'),
t('JNE OKE (Ongkos Kirim Ekonomis)'),
t('JNE PELIKAN (Unknown service)'),
t('JNE POPBOX (Unknown service)'),
t('JNE REG (Layanan Reguler)'),
t('JNE SPS (Super Speed)'),
t('JNE YES (Yakin Esok Sampai)'),
),
'J&T' => array(
t('J&T EZ (Regular Service)'),
),
'PAHALA' => array(
t('PAHALA EXPRESS'),
t('PAHALA PRIMA EXPRESS'),
t('PAHALA SUPER EXPRESS'),
),
'PANDU' => array(
t('PANDU REG (Regular Service)'),
),
'PCP' => array(
t('PCP ECO (Economic Service)'),
t('PCP NFS (Next Flight Service)'),
t('PCP ONP (Overnight Service Premium)'),
t('PCP ONS (Overnight Service)'),
t('PCP REG (Regular Service)'),
),
'POS' => array(
t('POS EMS BARANG'),
t('POS EMS DOKUMEN'),
t('POS Express Next Day'),
t('POS Express Sameday Dokumen'),
t('POS PAKETPOS BIASA LN'),
t('POS PAKETPOS CEPAT LN'),
t('POS Surat Kilat Khusus'),
t('POS Surat R LN'),
),
'RPX' => array(
t('RPX ERP (Ecommerce Package)'),
t('RPX MDP (MidDay Package)'),
t('RPX NDP (Next Day Package)'),
t('RPX REP (Retail Package)'),
t('RPX RGP (Regular Package)'),
t('RPX SDP (SameDay Package)'),
),
'SAP' => array(
t('SAP ODS (One Day Service)'),
t('SAP REG (Regular Service)'),
t('SAP SDS (Same Day Service)'),
),
'TIKI' => array(
t('TIKI Dokumen'),
t('TIKI ECO (ECONOMY SERVICE)'),
t('TIKI HDS (HOLIDAY SERVICE)'),
t('TIKI ONS (OVER NIGHT SERVICE)'),
t('TIKI Paket'),
t('TIKI REG (REGULAR SERVICE)'),
t('TIKI SDS (SAMEDAY SERVICE)'),
),
'WAHANA' => array(
t('WAHANA DES (Domestic Express Service)'),
),
);
}
/**
* @param $shipping_service
* Selected shipping service.
* @param $order
* Current order.
* @return
* Shipping rates for a given order.
*/
function commerce_rajaongkir_service_rate($shipping_service, $order) {
module_load_include('inc', 'commerce_rajaongkir', 'includes/commerce_rajaongkir.restful');
$rates_dependent_locality = variable_get('commerce_rajaongkir_rates_dependent_locality', 0);
$administrative_area = variable_get('commerce_rajaongkir_administrative_area', array());
$locality = variable_get('commerce_rajaongkir_locality', array());
$dependent_locality = variable_get('commerce_rajaongkir_dependent_locality', array());
$origin = commerce_rajaongkir_locality_code($locality);
$origintype = 'city';
if ($rates_dependent_locality) {
$origin = commerce_rajaongkir_dependent_locality_code($administrative_area, $locality, $dependent_locality);
$origintype = 'subdistrict';
}
$weight = commerce_physical_order_weight($order, 'g');
$weight = $weight['weight'];
$courier = drupal_strtolower(strtok($shipping_service['title'], ' '));
if ($courier === 'j&t') {
$courier = 'jnt';
}
$length = '';
$width = '';
$height = '';
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
$line_item_dimensions = commerce_physical_product_line_item_dimensions($line_item_wrapper->value());
$line_item_dimensions = is_array($line_item_dimensions) ? reset($line_item_dimensions) : $line_item_dimensions;
if (!physical_field_is_empty($line_item_dimensions, array('type' => 'physical_dimensions'))) {
$converted_dimensions = physical_dimensions_convert($line_item_dimensions, 'cm');
$length = $converted_dimensions['length'] * $line_item_wrapper->quantity->value();
$width = $converted_dimensions['width'] * $line_item_wrapper->quantity->value();
$height = $converted_dimensions['height'] * $line_item_wrapper->quantity->value();
}
}
}
unset($line_item_wrapper);
//$volume = commerce_physical_order_volume($order, 'cm');
$diameter = ''; //$volume['volume']
$field_name = commerce_physical_order_shipping_field_name($order);
if (!empty($order_wrapper->{$field_name}->commerce_customer_address)) {
$address = $order_wrapper->{$field_name}->commerce_customer_address->value();
}
else {
$address = array(
'country' => '',
'name_line' => '',
'first_name' => '',
'last_name' => '',
'organisation_name' => '',
'administrative_area' => '',
'sub_administrative_area' => '',
'locality' => '',
'dependent_locality' => '',
'postal_code' => '',
'thoroughfare' => '',
'premise' => '',
'sub_premise' => '',
'data' => '',
);
}
if ($address['country'] === 'ID') {
$destination = commerce_rajaongkir_locality_code($address['locality']);
$destinationtype = 'city';
if ($rates_dependent_locality) {
$destination = commerce_rajaongkir_dependent_locality_code($address['administrative_area'], $address['locality'], $address['dependent_locality']);
$destinationtype = 'subdistrict';
}
$cost = commerce_rajaongkir_restful_cost($origin, $origintype, $destination, $destinationtype, $weight, $courier, $length, $width, $height, $diameter);
}
else {
if ($courier === 'pos' || $courier === 'tiki') {
$destination = commerce_rajaongkir_country_code($address['country']);
$cost = commerce_rajaongkir_restful_internationalcost($origin, $destination, $weight, $courier, $length, $width, $height);
}
}
$results = array();
if (isset($cost->data)) {
$cost_data = drupal_json_decode($cost->data);
if (isset($cost_data['rajaongkir']['results'])) {
$results = reset($cost_data['rajaongkir']['results']);
}
else {
if (isset($cost_data['rajaongkir']['status']['description'])) {
drupal_set_message(t('Commerce RajaOngkir error: @error :(', array('@error' => $cost_data['rajaongkir']['status']['description'])), 'error', FALSE);
}
}
}
else {
if (isset($cost->error)) {
drupal_set_message(t('Commerce RajaOngkir HTTP request error: @error :(', array('@error' => $cost->error)), 'error', FALSE);
}
}
$rates = commerce_shipping_rates_cache_get('commerce_rajaongkir' . '_' . $shipping_service['title'], $order, variable_get('commerce_rajaongkir_rates_timeout', 60));
if ($results && empty($rates)) {
$results_code = drupal_strtoupper($results['code']);
$rates = array();
foreach ($results['costs'] as $results_costs) {
$results_costs_service_description = $results_costs['service'];
if (is_array($results_costs['cost'])) {
if ($results_costs_service_description !== $results_costs['description']) {
$results_costs_service_description .= ' (' . $results_costs['description'] . ')';
}
$results_costs_cost = reset($results_costs['cost']);
$amount = $results_costs_cost['value'];
}
else {
$amount = $results_costs['cost'];
}
$rates[$results_code . ' ' . $results_costs_service_description] = array(
'amount' => commerce_currency_decimal_to_amount($amount, 'IDR'),
'currency_code' => 'IDR',
'data' => array(),
);
}
unset($results_costs);
commerce_shipping_rates_cache_set('commerce_rajaongkir' . '_' . $shipping_service['title'], $order, $rates);
}
return isset($rates[$shipping_service['title']]) ? $rates[$shipping_service['title']] : FALSE;
}
/**
* @param $country
* Selected country.
* @return
* Code for selected country.
*/
function commerce_rajaongkir_country_code($country) {
module_load_include('inc', 'commerce_rajaongkir', 'includes/commerce_rajaongkir.country');
$countries = commerce_rajaongkir_countries();
return isset($countries[$country]) ? $countries[$country] : FALSE;
}
/**
* @param $locality
* Selected Locality.
* @return
* Code for selected Locality.
*/
function commerce_rajaongkir_locality_code($city) {
module_load_include('inc', 'commerce_rajaongkir', 'includes/commerce_rajaongkir.locality_block_list');
$locality_blocks = commerce_rajaongkir_locality_block();
$localities = array();
foreach ($locality_blocks as $administrative_area) {
foreach ($administrative_area as $locality => $dependent_locality) {
$localities[$locality] = $dependent_locality;
}
unset($locality, $dependent_locality);
}
unset($administrative_area);
$localities = array_keys($localities);
sort($localities);
$localities = array_merge(array(0), $localities);
$localities = array_filter($localities);
$localities = array_flip($localities);
return isset($localities[$city]) ? $localities[$city] : FALSE;
}
/**
* @param $locality
* Selected Locality.
* @param $dependent_locality
* Selected dependent locality.
* @return
* Code for selected dependent locality.
*/
function commerce_rajaongkir_dependent_locality_code($province, $city, $district) {
module_load_include('inc', 'commerce_rajaongkir', 'includes/commerce_rajaongkir.locality_block_list');
$locality_blocks = commerce_rajaongkir_locality_block();
return isset($locality_blocks[$province][$city][$district]) ? $locality_blocks[$province][$city][$district] : FALSE;
}