This repository has been archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AdminDPDFrance.php
687 lines (627 loc) · 31.5 KB
/
AdminDPDFrance.php
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<?php
/**
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author DPD S.A.S. <[email protected]>
* @copyright 2015 DPD S.A.S.
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
class AdminDPDFrance extends AdminTab
{
private $module = 'dpdfrance';
public $controller_type;
public function __construct()
{
$this->name = 'dpdfrance';
if (version_compare(_PS_VERSION_, '1.5.0.0 ', '>='))
{
$this->multishop_context = Shop::CONTEXT_ALL | Shop::CONTEXT_GROUP | Shop::CONTEXT_SHOP;
$this->multishop_context_group = Shop::CONTEXT_GROUP;
}
parent::__construct();
/* Backward compatibility */
if (_PS_VERSION_ < '1.5')
require_once(_PS_MODULE_DIR_.$this->name.'/backward_compatibility/backward.php');
if (_PS_VERSION_ < '1.4')
require_once(_PS_MODULE_DIR_.$this->module.'/'.Language::getIsoById((int)$this->context->language->id).'.php');
}
public function fetchTemplate($path, $name)
{
if (version_compare(_PS_VERSION_, '1.4', '<'))
$this->context->smarty->currentTemplate = $name;
return $this->context->smarty->fetch(dirname(__FILE__).$path.$name.'.tpl');
}
/* Converts country ISO code to DPD Station format */
public static function getIsoCodebyIdCountry($idcountry)
{
$sql = '
SELECT `iso_code`
FROM `'._DB_PREFIX_.'country`
WHERE `id_country` = \''.pSQL($idcountry).'\'';
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
$isops = array('DE', 'AD', 'AT', 'BE', 'BA', 'BG', 'HR', 'DK', 'ES', 'EE', 'FI', 'FR', 'GB', 'GR', 'GG', 'HU', 'IM', 'IE', 'IT', 'JE', 'LV', 'LI', 'LT', 'LU', 'NO', 'NL', 'PL', 'PT', 'CZ', 'RO', 'RS', 'SK', 'SI', 'SE', 'CH');
$isoep = array('D', 'AND', 'A', 'B', 'BA', 'BG', 'CRO', 'DK', 'E', 'EST', 'SF', 'F', 'GB', 'GR', 'GG', 'H', 'IM', 'IRL', 'I', 'JE', 'LET', 'LIE', 'LIT', 'L', 'N', 'NL', 'PL', 'P', 'CZ', 'RO', 'RS', 'SK', 'SLO', 'S', 'CH');
if (in_array($result['iso_code'], $isops)) // If the ISO code is in Europe, then convert it to DPD Station format
$code_iso = str_replace($isops, $isoep, $result['iso_code']);
else
$code_iso = str_replace($result['iso_code'], 'INT', $result['iso_code']); // If not, then it will be 'INT' (intercontinental)
return $code_iso;
}
/* Get all orders but statuses cancelled, delivered, error */
public static function getAllOrders($id_shop)
{
if ($id_shop == 0)
$id_shop = 'LIKE "%"';
else
$id_shop = '= '.(int)$id_shop;
$sql14 = ' SELECT id_order
FROM '._DB_PREFIX_.'orders o
WHERE (
SELECT id_order_state
FROM '._DB_PREFIX_.'order_history oh
WHERE oh.id_order = o .id_order
ORDER BY date_add DESC, id_order_history DESC
LIMIT 1)
NOT IN ('.(int)Configuration::get('DPDFRANCE_ETAPE_LIVRE', null, null, (int)$id_shop).',0,5,6,7,8)
ORDER BY invoice_date ASC';
$sql15 = ' SELECT id_order
FROM '._DB_PREFIX_.'orders o
WHERE `current_state` NOT IN('.(int)Configuration::get('DPDFRANCE_ETAPE_LIVRE', null, null, (int)$id_shop).',0,5,6,7,8) AND o.id_shop '.$id_shop.'
ORDER BY invoice_date ASC';
if (_PS_VERSION_ < '1.5')
$result = Db::getInstance()->ExecuteS($sql14);
else
$result = Db::getInstance()->ExecuteS($sql15);
$orders = array();
if (!empty($result))
{
foreach ($result as $order)
$orders[] = (int)$order['id_order'];
}
return $orders;
}
/* Formats GSM numbers */
public static function formatGSM($gsm_dest, $code_iso)
{
if ($code_iso == 'F')
{
$gsm_dest = str_replace(array(' ', '.', '-', ',', ';', '/', '\\', '(', ')'), '', $gsm_dest);
$gsm_dest = str_replace('+33', '0', $gsm_dest);
if (Tools::substr($gsm_dest, 0, 2) == 33) // Chrome autofill fix
$gsm_dest = substr_replace($gsm_dest, '0', 0, 2);
if (Tools::substr($gsm_dest, 0, 2) == 06 || Tools::substr($gsm_dest, 0, 2) == 07)
return $gsm_dest;
}
else
return $gsm_dest;
}
/* Get delivery service for a cart ID & checks if id_carrier matches */
public static function getService($order, $lang_id)
{
$sql = Db::getInstance()->getRow('SELECT `service` FROM `'._DB_PREFIX_.'dpdfrance_shipping` WHERE `id_cart` = '.(int)$order->id_cart.' AND `id_carrier` = '.(int)$order->id_carrier);
$service = $sql['service'];
// Service override, forcing Relais or Predict shipment on eligible orders
if (!$service)
{
$address_invoice = new Address($order->id_address_invoice, (int)$lang_id);
$address_delivery = new Address($order->id_address_delivery, (int)$lang_id);
$relay_id = Tools::substr($address_delivery->company, -7, 6);
$code_pays_dest = self::getIsoCodebyIdCountry((int)$address_delivery->id_country);
$tel_dest = (($address_delivery->phone_mobile)?$address_delivery->phone_mobile:(($address_invoice->phone_mobile)?$address_invoice->phone_mobile:(($address_delivery->phone)?$address_delivery->phone:(($address_invoice->phone)?$address_invoice->phone:''))));
$mobile = self::formatGSM($tel_dest, $code_pays_dest);
if (preg_match('/P\d{5}/i', $relay_id))
$service = 'REL';
elseif ($mobile && $code_pays_dest == 'F' && $order->id_carrier != Configuration::get('DPDFRANCE_CLASSIC_CARRIER_ID', null, null, (int)$order->id_shop))
$service = 'PRE';
}
return $service;
}
/* Get eligible orders and builds up display */
public function display()
{
// RSS stream
$stream = '';
if (_PS_VERSION_ < '1.4')
$rss = @simplexml_load_string(file_get_contents('http://www.dpd.fr/extensions/rss/flux_info_dpdfr.xml'));
else
$rss = @simplexml_load_string(Tools::file_get_contents('http://www.dpd.fr/extensions/rss/flux_info_dpdfr.xml'));
if (!empty($rss))
{
if (empty($rss->channel->item))
$stream = 'error';
else
{
foreach ($rss->channel->item as $item)
{
$i = 0;
$stream[$i] = array('category' => (string)$item->category,
'title' => (string)$item->title,
'description' => (string)$item->description,
'date' => strtotime((string)$item->pubDate)
);
if (strtotime('-30 day', strtotime(date('d-m-Y'))) > $stream[$i]['date'])
unset($stream[$i]);
$i++;
}
}
if (empty($stream))
$stream = 'error';
}
else
$stream = 'error';
// Update delivered orders
if (Tools::getIsset('updateDeliveredOrders'))
{
if (Tools::getIsset('checkbox'))
$orders = Tools::getValue('checkbox');
if (!empty($orders))
{
$sql = 'SELECT O.`id_order` AS id_order
FROM '._DB_PREFIX_.'orders AS O,
'._DB_PREFIX_.'carrier AS CA
WHERE CA.id_carrier=O.id_carrier AND
id_order IN ('.implode(',', array_map('intval', $orders)).')';
$orderlist = Db::getInstance()->ExecuteS($sql);
if (!empty($orderlist))
{ // Check if there are DPD orders
foreach ($orderlist as $orders)
{
$id_order = $orders['id_order'];
if (Validate::isLoadedObject($order = new Order($id_order)))
{
if (_PS_VERSION_ < '1.5')
$order->id_shop = '';
$history = new OrderHistory();
$history->id_order = (int)$id_order;
$history->changeIdOrderState(Configuration::get('DPDFRANCE_ETAPE_LIVRE', null, null, (int)$order->id_shop), $id_order);
$history->id_employee = (int)$this->context->employee->id;
$history->addWithemail();
}
}
echo '<div class="okmsg">'.$this->l('Delivered orders statuses were updated').'</div>';
}
else
echo '<div class="warnmsg">'.$this->l('No DPD trackings to generate.').'</div>';
}
else
echo '<div class="warnmsg">'.$this->l('No order selected.').'</div>';
}
// Update shipped orders
if (Tools::getIsset('updateShippedOrders'))
{
if (Tools::getIsset('checkbox'))
$orders = Tools::getValue('checkbox');
if (!empty($orders))
{
$sql = 'SELECT O.`id_order` AS id_order
FROM '._DB_PREFIX_.'orders AS O,
'._DB_PREFIX_.'carrier AS CA
WHERE CA.id_carrier=O.id_carrier AND
id_order IN ('.implode(',', array_map('intval', $orders)).')';
$orderlist = Db::getInstance()->ExecuteS($sql);
if (!empty($orderlist))
{ // Check if there are DPD orders
foreach ($orderlist as $orders)
{
$id_order = $orders['id_order'];
if (Validate::isLoadedObject($order = new Order($id_order)))
{
if (_PS_VERSION_ < '1.5')
{
$internalref_cleaned = $order->id;
$order->id_shop = '';
}
else
$internalref_cleaned = $order->reference;
$service = self::getService($order, $this->context->language->id);
switch ($service)
{
case 'PRE':
$compte_chargeur = Configuration::get('DPDFRANCE_PREDICT_SHIPPER_CODE', null, null, (int)$order->id_shop);
$depot_code = Configuration::get('DPDFRANCE_PREDICT_DEPOT_CODE', null, null, (int)$order->id_shop);
break;
case 'REL':
$compte_chargeur = Configuration::get('DPDFRANCE_RELAIS_SHIPPER_CODE', null, null, (int)$order->id_shop);
$depot_code = Configuration::get('DPDFRANCE_RELAIS_DEPOT_CODE', null, null, (int)$order->id_shop);
break;
default:
$compte_chargeur = Configuration::get('DPDFRANCE_CLASSIC_SHIPPER_CODE', null, null, (int)$order->id_shop);
$depot_code = Configuration::get('DPDFRANCE_CLASSIC_DEPOT_CODE', null, null, (int)$order->id_shop);
break;
}
$url = 'http://www.dpd.fr/tracer_'.$internalref_cleaned.'_'.$depot_code.$compte_chargeur;
$customer = new Customer((int)$order->id_customer);
$order->shipping_number = $internalref_cleaned.'_'.$depot_code.$compte_chargeur;
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET shipping_number = "'.pSQL($order->shipping_number).'" WHERE id_order = "'.$id_order.'"');
if (_PS_VERSION_ >= '1.5')
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'order_carrier SET tracking_number = "'.pSQL($order->shipping_number).'" WHERE id_order = "'.$id_order.'"');
$order->update();
$history = new OrderHistory();
$history->id_order = (int)$id_order;
$history->changeIdOrderState(Configuration::get('DPDFRANCE_ETAPE_EXPEDIEE', null, null, (int)$order->id_shop), $id_order);
$history->id_employee = (int)$this->context->employee->id;
$carrier = new Carrier((int)$order->id_carrier, (int)$this->context->language->id);
if (_PS_VERSION_ < '1.5')
$template_vars = array('{followup}' => $url, '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{id_order}' => (int)$order->id);
else
$template_vars = array('{followup}' => $url, '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{order_name}' => $order->reference, '{id_order}' => (int)$order->id);
switch (Language::getIsoById((int)$order->id_lang))
{
case 'fr':
$subject = 'Votre commande sera livrée par DPD';
break;
case 'en':
$subject = 'Your parcel will be delivered by DPD';
break;
case 'es':
$subject = 'Su pedido será enviado por DPD';
break;
case 'it':
$subject = 'Il vostro pacchetto sará trasportato da DPD';
break;
case 'de':
$subject = 'Ihre Bestellung wird per DPD geliefert werden';
break;
}
if (!$history->addWithemail(true, $template_vars))
$this->_errors[] = Tools::displayError('an error occurred while changing status or was unable to send e-mail to the customer');
if (!Validate::isLoadedObject($customer) || !Validate::isLoadedObject($carrier))
die(Tools::displayError());
Mail::Send((int)$order->id_lang, 'in_transit', $subject, $template_vars, $customer->email, $customer->firstname.' '.$customer->lastname);
}
}
echo '<div class="okmsg">'.$this->l('Shipped orders statuses were updated and tracking numbers added.').'</div>';
}
else
echo '<div class="warnmsg">'.$this->l('No trackings to generate.').'</div>';
}
else
echo '<div class="warnmsg">'.$this->l('No order selected.').'</div>';
}
// Export selected orders
if (Tools::getIsset('exportOrders'))
{
$fieldlist = array('O.`id_order`', 'AD.`lastname`', 'AD.`firstname`', 'AD.`postcode`', 'AD.`city`', 'CL.`iso_code`', 'C.`email`');
if (Tools::getIsset('checkbox'))
{
$orders = Tools::getValue('checkbox');
$liste_expeditions = 'O.id_order IN ('.implode(',', array_map('intval', $orders)).')';
if (!empty($orders))
{
$sql = 'SELECT '.implode(', ', $fieldlist).'
FROM '._DB_PREFIX_.'orders AS O,
'._DB_PREFIX_.'carrier AS CA,
'._DB_PREFIX_.'customer AS C,
'._DB_PREFIX_.'address AS AD,
'._DB_PREFIX_.'country AS CL
WHERE O.id_address_delivery=AD.id_address AND
C.id_customer=O.id_customer AND
CL.id_country=AD.id_country AND
CA.id_carrier=O.id_carrier AND
('.$liste_expeditions.')
ORDER BY id_order DESC';
$orderlist = Db::getInstance()->ExecuteS($sql);
if (!empty($orderlist))
{
// File creation
$record = new DPDStation();
foreach ($orderlist as $order_var)
{
// Shipper information retrieval
$order = new Order($order_var['id_order']);
$nom_exp = Configuration::get('DPDFRANCE_NOM_EXP', null, null, (int)$order->id_shop); // Raison sociale expéditeur
$address_exp = Configuration::get('DPDFRANCE_ADDRESS_EXP', null, null, (int)$order->id_shop); // Adresse
$address2_exp = Configuration::get('DPDFRANCE_ADDRESS2_EXP', null, null, (int)$order->id_shop); // Complément d'adresse
$cp_exp = Configuration::get('DPDFRANCE_CP_EXP', null, null, (int)$order->id_shop); // Code postal
$ville_exp = Configuration::get('DPDFRANCE_VILLE_EXP', null, null, (int)$order->id_shop); // Ville
$code_pays_exp = 'F'; // Code pays
$tel_exp = Configuration::get('DPDFRANCE_TEL_EXP', null, null, (int)$order->id_shop); // Téléphone
$email_exp = Configuration::get('DPDFRANCE_EMAIL_EXP', null, null, (int)$order->id_shop); // E-mail
$gsm_exp = Configuration::get('DPDFRANCE_GSM_EXP', null, null, (int)$order->id_shop); // N° GSM
// Backwards compatibility PS 1.4 and lower
if (_PS_VERSION_ < '1.5')
$order->reference = $order->id;
$customer = new Customer($order->id_customer);
$address_invoice = new Address($order->id_address_invoice, (int)$this->context->language->id);
$address_delivery = new Address($order->id_address_delivery, (int)$this->context->language->id);
$code_pays_dest = self::getIsoCodebyIdCountry((int)$address_delivery->id_country);
$instr_liv_cleaned = str_replace (array("\r\n", "\n", "\r", "\t"), ' ', $address_delivery->other);
$service = self::getService($order, $this->context->language->id);
$relay_id = Tools::substr($address_delivery->company, -7, 6);
$tel_dest = Db::getInstance()->getValue('SELECT gsm_dest FROM '._DB_PREFIX_.'dpdfrance_shipping WHERE id_cart ="'.$order->id_cart.'"');
if ($tel_dest == '')
$tel_dest = (($address_delivery->phone_mobile) ? $address_delivery->phone_mobile : (($address_invoice->phone_mobile) ? $address_invoice->phone_mobile : (($address_delivery->phone) ? $address_delivery->phone : (($address_invoice->phone) ? $address_invoice->phone : ''))));
$mobile = self::formatGSM($tel_dest, $code_pays_dest);
if (Tools::strtolower(Configuration::get('PS_WEIGHT_UNIT', null, null, (int)$order->id_shop)) == 'kg')
$poids = (int)($order->getTotalWeight() * 100);
if (Tools::strtolower(Configuration::get('PS_WEIGHT_UNIT', null, null, (int)$order->id_shop)) == 'g')
$poids = (int)($order->getTotalWeight() * 0.1);
switch ($service)
{
case 'PRE':
$compte_chargeur = Configuration::get('DPDFRANCE_PREDICT_SHIPPER_CODE', null, null, (int)$order->id_shop);
break;
case 'REL':
$compte_chargeur = Configuration::get('DPDFRANCE_RELAIS_SHIPPER_CODE', null, null, (int)$order->id_shop);
break;
default:
$compte_chargeur = Configuration::get('DPDFRANCE_CLASSIC_SHIPPER_CODE', null, null, (int)$order->id_shop);
break;
}
// DPD unified interface file structure
$record->add($order->reference, 0, 35); // Référence client N°1 - Référence Commande Prestashop 1.5
$record->add(str_pad((int)$poids, 8, '0', STR_PAD_LEFT), 37, 8); // Poids du colis sur 8 caractères
if ($service == 'REL')
{
$record->add($address_delivery->lastname, 60, 35); // Nom du destinataire
$record->add($address_delivery->firstname, 95, 35); // Prénom du destinataire
}
else
{
$record->add($address_delivery->lastname.' '.$address_delivery->firstname, 60, 35); // Nom et prénom du destinataire
$record->add($address_delivery->company, 95, 35); // Complément d'adresse 1
}
$record->add($address_delivery->address2, 130, 140); // Complément d’adresse 2 a 5
$record->add($address_delivery->postcode, 270, 10); // Code postal
$record->add($address_delivery->city, 280, 35); // Ville
$record->add($address_delivery->address1, 325, 35); // Rue
$record->add('', 360, 10); // Filler
$record->add($code_pays_dest, 370, 3); // Code Pays destinataire
$record->add($tel_dest, 373, 30); // Téléphone
$record->add($nom_exp, 418, 35); // Nom expéditeur
$record->add($address2_exp, 453, 35); // Complément d’adresse 1
$record->add($cp_exp, 628, 10); // Code postal
$record->add($ville_exp, 638, 35); // Ville
$record->add($address_exp, 683, 35); // Rue
$record->add($code_pays_exp, 728, 3); // Code Pays
$record->add($tel_exp, 731, 30); // Tél.
$record->add($instr_liv_cleaned, 761, 140); // Instructions de livraison
$record->add(date('d/m/Y'), 901, 10); // Date d'expédition théorique
$record->add(str_pad($compte_chargeur, 8, '0', STR_PAD_LEFT), 911, 8); // N° de compte chargeur DPD
$record->add($order->id, 919, 35); // Code à barres
$record->add($order->id, 954, 35); // N° de commande - Id Order Prestashop
if (Tools::getIsset('advalorem') && in_array($order->id, Tools::getValue('advalorem')))
$record->add(str_pad(number_format($order->total_paid, 2, '.', ''), 9, '0', STR_PAD_LEFT), 1018, 9); // Montant valeur colis
$record->add($order->id, 1035, 35); // Référence client N°2 - Id Order Prestashop
$record->add($email_exp, 1116, 80); // E-mail expéditeur
$record->add($gsm_exp, 1196, 35); // GSM expéditeur
$record->add($customer->email, 1231, 80); // E-mail destinataire
$record->add($mobile, 1311, 35); // GSM destinataire
if ($service == 'REL')
$record->add($relay_id, 1442, 8); // Identifiant relais Pickup
if ($service == 'PRE')
$record->add('+', 1568, 1); // Flag Predict
$record->add($address_delivery->lastname, 1569, 35); // Nom de famille du destinataire
$record->addLine();
}
$record->display();
}
else
echo '<div class="warnmsg">'.$this->l('No orders to export.').'</div>';
}
else
echo '<div class="warnmsg">'.$this->l('No orders to export.').'</div>';
}
else
echo '<div class="warnmsg">'.$this->l('No order selected.').'</div>';
}
// Display section
// Error message if shipper info is missing
if ((Configuration::get('DPDFRANCE_PARAM') == 0))
{
echo '<div class="warnmsg">'.$this->l('Warning! Your DPD Depot code and contract number are missing. You must configure the DPD module in order to use the export and tracking features.').'</div>';
exit;
}
// Add jQuery for Prestashop before 1.4
if (_PS_VERSION_ < '1.4')
echo '<script type="text/javascript" src="../modules/'.$this->name.'/views/js/admin/jquery/jquery-1.11.0.min.js"></script>';
// Calls function to get orders
$order_info = '';
$statuses_array = array();
$statuses = OrderState::getOrderStates((int)$this->context->language->id);
foreach ($statuses as $status)
$statuses_array[$status['id_order_state']] = $status['name'];
$fieldlist = array('O.`id_order`', 'O.`id_cart`', 'AD.`lastname`', 'AD.`firstname`', 'AD.`postcode`', 'AD.`city`', 'CL.`iso_code`', 'C.`email`', 'CA.`name`');
$orders = AdminDPDFrance::getAllOrders((int)Tools::substr($this->context->cookie->shopContext, 2));
$liste_expeditions = 'O.id_order IN ('.implode(',', $orders).')';
$predict_carrier_log = $classic_carrier_log = $relais_carrier_log = $europe_carrier_log = '';
if (Configuration::get('DPDFRANCE_PREDICT_CARRIER_LOG', null, null, (int)$this->context->shop->id))
$predict_carrier_log = 'CA.id_carrier IN ('.implode(',', array_map('intval', explode('|', Tools::substr(Configuration::get('DPDFRANCE_PREDICT_CARRIER_LOG', null, null, (int)$this->context->shop->id), 1)))).') OR ';
if (Configuration::get('DPDFRANCE_CLASSIC_CARRIER_LOG', null, null, (int)$this->context->shop->id))
$classic_carrier_log = 'CA.id_carrier IN ('.implode(',', array_map('intval', explode('|', Tools::substr(Configuration::get('DPDFRANCE_CLASSIC_CARRIER_LOG', null, null, (int)$this->context->shop->id), 1)))).') OR ';
if (Configuration::get('DPDFRANCE_RELAIS_CARRIER_LOG', null, null, (int)$this->context->shop->id))
$relais_carrier_log = 'CA.id_carrier IN ('.implode(',', array_map('intval', explode('|', Tools::substr(Configuration::get('DPDFRANCE_RELAIS_CARRIER_LOG', null, null, (int)$this->context->shop->id), 1)))).') OR ';
$europe_carrier_log = 'CA.name LIKE \'%DPD%\'';
if (!empty($orders))
{
$sql = 'SELECT '.implode(', ', $fieldlist).'
FROM '._DB_PREFIX_.'orders AS O,
'._DB_PREFIX_.'carrier AS CA,
'._DB_PREFIX_.'customer AS C,
'._DB_PREFIX_.'address AS AD,
'._DB_PREFIX_.'country AS CL
WHERE O.id_address_delivery=AD.id_address AND
C.id_customer=O.id_customer AND
CL.id_country=AD.id_country AND
CA.id_carrier=O.id_carrier AND
('.$predict_carrier_log.$classic_carrier_log.$relais_carrier_log.$europe_carrier_log.') AND
('.$liste_expeditions.')
ORDER BY id_order DESC';
$orderlist = Db::getInstance()->ExecuteS($sql);
if (!empty($orderlist))
{
foreach ($orderlist as $order_var)
{
$order = new Order($order_var['id_order']);
$address_delivery = new Address($order->id_address_delivery, (int)$this->context->language->id);
$orderstate = new OrderHistory($order_var['id_order']);
if (_PS_VERSION_ < '1.5')
{
$current_state_id = ($orderstate->getLastOrderState($order_var['id_order'])->id);
$current_state_name = $statuses_array[($orderstate->getLastOrderState($order_var['id_order'])->id)];
$order->reference = $order->id;
$order->id_shop = '';
}
else
{
$current_state_id = $order->current_state;
$current_state_name = $statuses_array[$order->current_state];
}
switch ($current_state_id)
{
default :
$dernierstatutcolis = '';
break;
case Configuration::get('DPDFRANCE_ETAPE_LIVRE', null, null, (int)$order->id_shop):
$dernierstatutcolis = '<img src="../modules/dpdfrance/views/img/admin/tracking.png" title="Trace du colis"/>';
break;
case Configuration::get('DPDFRANCE_ETAPE_EXPEDIEE', null, null, (int)$order->id_shop):
$dernierstatutcolis = '<img src="../modules/dpdfrance/views/img/admin/tracking.png" title="Trace du colis"/>';
}
$weight = number_format($order->getTotalWeight(), 2, '.', '.').' '.Configuration::get('PS_WEIGHT_UNIT', null, null, (int)$order->id_shop);
$amount = number_format($order->total_paid, 2, '.', '.').' €';
$service = self::getService($order, $this->context->language->id);
switch ($service)
{
case 'PRE':
$type = 'Predict<img src="../modules/dpdfrance/views/img/admin/service_predict.png" title="Predict"/>';
$compte_chargeur = Configuration::get('DPDFRANCE_PREDICT_SHIPPER_CODE', null, null, (int)$order->id_shop);
$depot_code = Configuration::get('DPDFRANCE_PREDICT_DEPOT_CODE', null, null, (int)$order->id_shop);
$address = '<a class="popup" href="http://maps.google.com/maps?f=q&hl=fr&geocode=&q='.str_replace(' ', '+', $address_delivery->address1).','.str_replace(' ', '+', $address_delivery->postcode).'+'.str_replace(' ', '+', $address_delivery->city).'&output=embed" target="_blank">'.($address_delivery->company ? $address_delivery->company.'<br/>' : '').$address_delivery->address1.'<br/>'.$address_delivery->postcode.' '.$address_delivery->city.'</a>';
break;
case 'REL':
$type = 'Relais<img src="../modules/dpdfrance/views/img/admin/service_relais.png" title="Relais"/>';
$compte_chargeur = Configuration::get('DPDFRANCE_RELAIS_SHIPPER_CODE', null, null, (int)$order->id_shop);
$depot_code = Configuration::get('DPDFRANCE_RELAIS_DEPOT_CODE', null, null, (int)$order->id_shop);
$address = '<a class="popup" href="http://www.dpd.fr/dpdrelais/id_'.Tools::substr($address_delivery->company, -7, 6).'" target="_blank">'.$address_delivery->company.'<br/>'.$address_delivery->postcode.' '.$address_delivery->city.'</a>';
break;
default:
if ($order_var['name'] == 'Livraison internationale par DPD')
$type = 'Intercontinental<img src="../modules/dpdfrance/views/img/admin/service_world.png" title="Intercontinental"/>';
else
$type = 'Classic<img src="../modules/dpdfrance/views/img/admin/service_dom.png" title="Classic"/>';
$compte_chargeur = Configuration::get('DPDFRANCE_CLASSIC_SHIPPER_CODE', null, null, (int)$order->id_shop);
$depot_code = Configuration::get('DPDFRANCE_CLASSIC_DEPOT_CODE', null, null, (int)$order->id_shop);
$address = '<a class="popup" href="http://maps.google.com/maps?f=q&hl=fr&geocode=&q='.str_replace(' ', '+', $address_delivery->address1).','.str_replace(' ', '+', $address_delivery->postcode).'+'.str_replace(' ', '+', $address_delivery->city).'&output=embed" target="_blank">'.($address_delivery->company ? $address_delivery->company.'<br/>' : '').$address_delivery->address1.'<br/>'.$address_delivery->postcode.' '.$address_delivery->city.'</a>';
break;
}
$order_info[] = array(
'checked' => ($current_state_id == Configuration::get('DPDFRANCE_ETAPE_EXPEDITION', null, null, (int)$order->id_shop) ? 'checked="checked"' : ''),
'id' => $order->id,
'reference' => $order->reference,
'date' => date('d/m/Y H:i:s', strtotime($order->date_add)),
'nom' => $address_delivery->firstname.' '.$address_delivery->lastname,
'type' => $type,
'address' => $address,
'id' => $order->id,
'poids' => $weight,
'prix' => $amount,
'advalorem_checked' => (Configuration::get('DPDFRANCE_AD_VALOREM', null, null, (int)$order->id_shop) == 1 ? 'checked="checked"' : ''),
'statut' => $current_state_name,
'depot_code' => $depot_code,
'shipper_code' => $compte_chargeur,
'dernier_statut_colis' => $dernierstatutcolis,
);
}
}
else
$order_info = 'error';
}
else
$order_info = 'error';
// Assign smarty variables and fetches template
$this->context->smarty->assign(array(
'stream' => $stream,
'token' => $this->token,
'order_info' => $order_info,
));
echo $this->fetchTemplate('/views/templates/admin/', 'AdminDPDFrance');
}
}
class DPDStation
{
var $line;
var $contenu_fichier;
public function __construct()
{
$this->line = str_pad('', 1634);
$this->contenu_fichier = '';
}
public function add($txt, $position, $length)
{
$txt = $this->stripAccents($txt);
$this->line = substr_replace($this->line, str_pad($txt, $length), $position, $length);
}
public function addLine()
{
if ($this->contenu_fichier != '')
{
$this->contenu_fichier = $this->contenu_fichier."\r\n".$this->line;
$this->line = '';
$this->line = str_pad('', 1634);
}
else
{
$this->contenu_fichier .= $this->line;
$this->line = '';
$this->line = str_pad('', 1634);
}
}
public function display()
{
while (@ob_end_clean());
header('Content-type: application/dat');
header('Content-Disposition: attachment; filename="DPDFRANCE_'.date('dmY-His').'.dat"');
echo '$VERSION=110'."\r\n";
echo $this->contenu_fichier."\r\n";
exit;
}
public function stripAccents($str)
{
$str = preg_replace('/[\x{00C0}\x{00C1}\x{00C2}\x{00C3}\x{00C4}\x{00C5}]/u', 'A', $str);
$str = preg_replace('/[\x{0105}\x{0104}\x{00E0}\x{00E1}\x{00E2}\x{00E3}\x{00E4}\x{00E5}]/u', 'a', $str);
$str = preg_replace('/[\x{00C7}\x{0106}\x{0108}\x{010A}\x{010C}]/u', 'C', $str);
$str = preg_replace('/[\x{00E7}\x{0107}\x{0109}\x{010B}\x{010D}}]/u', 'c', $str);
$str = preg_replace('/[\x{010E}\x{0110}]/u', 'D', $str);
$str = preg_replace('/[\x{010F}\x{0111}]/u', 'd', $str);
$str = preg_replace('/[\x{00C8}\x{00C9}\x{00CA}\x{00CB}\x{0112}\x{0114}\x{0116}\x{0118}\x{011A}]/u', 'E', $str);
$str = preg_replace('/[\x{00E8}\x{00E9}\x{00EA}\x{00EB}\x{0113}\x{0115}\x{0117}\x{0119}\x{011B}]/u', 'e', $str);
$str = preg_replace('/[\x{00CC}\x{00CD}\x{00CE}\x{00CF}\x{0128}\x{012A}\x{012C}\x{012E}\x{0130}]/u', 'I', $str);
$str = preg_replace('/[\x{00EC}\x{00ED}\x{00EE}\x{00EF}\x{0129}\x{012B}\x{012D}\x{012F}\x{0131}]/u', 'i', $str);
$str = preg_replace('/[\x{0142}\x{0141}\x{013E}\x{013A}]/u', 'l', $str);
$str = preg_replace('/[\x{00F1}\x{0148}]/u', 'n', $str);
$str = preg_replace('/[\x{00D2}\x{00D3}\x{00D4}\x{00D5}\x{00D6}\x{00D8}]/u', 'O', $str);
$str = preg_replace('/[\x{00F2}\x{00F3}\x{00F4}\x{00F5}\x{00F6}\x{00F8}]/u', 'o', $str);
$str = preg_replace('/[\x{0159}\x{0155}]/u', 'r', $str);
$str = preg_replace('/[\x{015B}\x{015A}\x{0161}]/u', 's', $str);
$str = preg_replace('/[\x{00DF}]/u', 'ss', $str);
$str = preg_replace('/[\x{0165}]/u', 't', $str);
$str = preg_replace('/[\x{00D9}\x{00DA}\x{00DB}\x{00DC}\x{016E}\x{0170}\x{0172}]/u', 'U', $str);
$str = preg_replace('/[\x{00F9}\x{00FA}\x{00FB}\x{00FC}\x{016F}\x{0171}\x{0173}]/u', 'u', $str);
$str = preg_replace('/[\x{00FD}\x{00FF}]/u', 'y', $str);
$str = preg_replace('/[\x{017C}\x{017A}\x{017B}\x{0179}\x{017E}]/u', 'z', $str);
$str = preg_replace('/[\x{00C6}]/u', 'AE', $str);
$str = preg_replace('/[\x{00E6}]/u', 'ae', $str);
$str = preg_replace('/[\x{0152}]/u', 'OE', $str);
$str = preg_replace('/[\x{0153}]/u', 'oe', $str);
$str = preg_replace('/[\x{0022}\x{0025}\x{0026}\x{0027}\x{00A1}\x{00A2}\x{00A3}\x{00A4}\x{00A5}\x{00A6}\x{00A7}\x{00A8}\x{00AA}\x{00AB}\x{00AC}\x{00AD}\x{00AE}\x{00AF}\x{00B0}\x{00B1}\x{00B2}\x{00B3}\x{00B4}\x{00B5}\x{00B6}\x{00B7}\x{00B8}\x{00BA}\x{00BB}\x{00BC}\x{00BD}\x{00BE}\x{00BF}]/u', ' ', $str);
return $str;
}
}
?>