forked from PrestaShopCorp/chronopost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postSkybill.php
executable file
·355 lines (288 loc) · 12.3 KB
/
postSkybill.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
<?php
/**
* MODULE PRESTASHOP OFFICIEL CHRONOPOST
*
* LICENSE : All rights reserved - COPY AND REDISTRIBUTION FORBIDDEN WITHOUT PRIOR CONSENT FROM OXILEO
* LICENCE : Tous droits réservés, le droit d'auteur s'applique - COPIE ET REDISTRIBUTION INTERDITES SANS ACCORD EXPRES D'OXILEO
*
* @author Oxileo SAS <contact@oxileo.eu>
* @copyright 2001-2015 Oxileo SAS
* @license Proprietary - no redistribution without authorization
*/
if (!defined('_MYDIR_')) define('_MYDIR_', dirname(__FILE__));
require('../../config/config.inc.php');
if (!Tools::getIsset('orderid') && !Tools::getIsset('orders') && !Tools::getIsset('orderid')) die('<h1>Informations de commande non transmises</h1>');
require_once('chronopost.php');
include('libraries/ShippingServiceWSService.php');
include_once _MYDIR_.'/libraries/PointRelaisServiceWSService.php';
$multi = array();
if (!Tools::getIsset('shared_secret') || Tools::getValue('shared_secret') != Configuration::get('CHRONOPOST_SECRET'))
die('Secret does not match.');
if (Tools::strlen(Configuration::get('CHRONOPOST_GENERAL_ACCOUNT')) < 8)
die('Erreur : veuillez configurer le module avant de procéder à l\'édition des étiquettes.');
$return = false;
if (Tools::getIsset('multi'))
{
$multi = Tools::getValue('multi');
$multi = Tools::jsonDecode($multi, true);
}
else $multi = array();
if (Tools::getIsset('orders'))
{
$orders = Tools::getValue('orders');
$orders = explode(';', $orders);
}
else
{
$orders = array(Tools::getValue('orderid'));
if (Tools::getIsset('return')) $return = true;
if (Tools::getIsset('multiOne')) $multi = array($orders[0]=>Tools::getValue('multiOne'));
}
if (count($orders) == 0) die('<h1>Aucune commande sélectionnée</h1>');
require_once('libraries/PDFMerger.php');
@$pdf = new PDFMerger;
foreach ($orders as $orderid)
{
if (array_key_exists($orderid, $multi))
$nb = $multi[$orderid];
else $nb = 1;
$totalnb = $nb;
while ($nb > 0)
{
$file = tempnam('temp', 'CHR');
$fp = fopen($file, 'w');
$lt = @createLT($orderid, $totalnb, $return);
if ($lt === null)
{
/* error, skip it */
$nb--;
continue;
}
fwrite($fp, $lt);
fclose($fp);
if (file_exists('custom_pdf/config.json'))
{
/* personnalize */
include_once('custom_pdf/CustomPDF.php');
$cust = new CustomPDF($file, $orderid);
$cust->generate();
}
@$pdf->addPDF($file, 'all');
$nb--;
}
}
try {
$pdf->merge('download', 'Chronopost-LT-'.date('Ymd-Hi').'.pdf');
} catch(Exception $e)
{
echo '<p>Le fichier généré est invalide.</p>';
echo '<p>Vérifiez la configuration du module et que les commandes visées disposent d\'adresses de livraison valides.</p>';
}
function createLT($orderid, $totalnb = 1, $isReturn = false)
{
$o = new Order($orderid);
$a = new Address($o->id_address_delivery);
$cust = new Customer($o->id_customer);
// at least 2 skybills for orders >= 30kg
$o = new Order($orderid);
if ($o->getTotalWeight() * Configuration::get('CHRONOPOST_GENERAL_WEIGHTCOEF') >= 30 && $totalnb == 1)
{
echo '<script>alert(\'Vous devez générer au moins 2 étiquettes pour les commandes de plus de 30kg\');history.back();</script>';
exit();
}
$recipient = new recipientValue();
$recipient->recipientAdress1 = Tools::substr($a->address1, 0, 35);
$recipient->recipientAdress2 = Tools::substr($a->address2, 0, 35);
$recipient->recipientCity = Tools::substr($a->city, 0, 30);
$recipient->recipientCivility = 'M';
$recipient->recipientContactName = Tools::substr($a->firstname.' '.$a->lastname, 0, 35);
$c = new Country($a->id_country);
$recipient->recipientCountry = $c->iso_code;
$recipient->recipientName = Tools::substr($a->company, 0, 35);
$recipient->recipientName2 = Tools::substr($a->firstname.' '.$a->lastname, 0, 35);
$recipient->recipientZipCode = $a->postcode;
$recipient->recipientPhone = $a->phone_mobile == null ? $a->phone : $a->phone_mobile;
$recipient->recipientMobilePhone = $a->phone_mobile;
$recipient->recipientEmail = $cust->email;
if ($isReturn)
{
$recipient->recipientAdress1 = Configuration::get('CHRONOPOST_SHIPPER_ADDRESS');
$recipient->recipientAdress2 = Configuration::get('CHRONOPOST_SHIPPER_ADDRESS2');
$recipient->recipientCity = Configuration::get('CHRONOPOST_SHIPPER_CITY');
$recipient->recipientCivility = Configuration::get('CHRONOPOST_SHIPPER_CIVILITY');
$recipient->recipientContactName = Configuration::get('CHRONOPOST_SHIPPER_CONTACTNAME');
$recipient->recipientCountry = 'FR';
$recipient->recipientName = Configuration::get('CHRONOPOST_SHIPPER_NAME');
$recipient->recipientName2 = Configuration::get('CHRONOPOST_SHIPPER_NAME2');
$recipient->recipientZipCode = Configuration::get('CHRONOPOST_SHIPPER_ZIPCODE');
}
$esd = new esdValue();
$esd->specificInstructions = 'aucune';
//$esd->retrievalDateTime=date('Y-m-d\TH:i:s', $retrievalDateTime);
//$esd->closingDateTime=>date('Y-m-d\TH:i:s', $closingDateTime);
/*$esd->height=(float)$_POST['height'];
$esd->width=(float)$_POST['width'];
$esd->length=(float)$_POST['length'];*/
$esd->height = '';
$esd->width = '';
$esd->length = '';
$header = new headerValue();
$params = new shipping();
$skybill = new skybillValue();
$skybill->evtCode = 'DC';
$skybill->objectType = 'MAR';
$skybill->productCode = '01'; // CHRONO 13
if (Tools::getIsset('advalorem') && Tools::getValue('advalorem') == 'yes')
$skybill->insuredValue = (int)Tools::getValue('advalorem_value');
$header->accountNumber = Configuration::get('CHRONOPOST_GENERAL_ACCOUNT');
$header->subAccount = Configuration::get('CHRONOPOST_GENERAL_SUBACCOUNT');
$params->password = Configuration::get('CHRONOPOST_GENERAL_PASSWORD');
$header->idEmit = 'PREST';
$shipper = new shipperValue();
$shipper->shipperAdress1 = Configuration::get('CHRONOPOST_SHIPPER_ADDRESS');
$shipper->shipperAdress2 = Configuration::get('CHRONOPOST_SHIPPER_ADDRESS2');
$shipper->shipperCity = Configuration::get('CHRONOPOST_SHIPPER_CITY');
$shipper->shipperCivility = Configuration::get('CHRONOPOST_SHIPPER_CIVILITY');
$shipper->shipperContactName = Configuration::get('CHRONOPOST_SHIPPER_CONTACTNAME');
$shipper->shipperCountry = 'FR';
$shipper->shipperName = Configuration::get('CHRONOPOST_SHIPPER_NAME');
$shipper->shipperName2 = Configuration::get('CHRONOPOST_SHIPPER_NAME2');
$shipper->shipperZipCode = Configuration::get('CHRONOPOST_SHIPPER_ZIPCODE');
if ($isReturn)
{
$shipper = new shipperValue();
$shipper->shipperAdress1 = Tools::substr($a->address1, 0, 35);
$shipper->shipperAdress2 = Tools::substr($a->address2, 0, 35);
$shipper->shipperCity = Tools::substr($a->city, 0, 30);
$shipper->shipperCivility = 'M';
$shipper->shipperContactName = Tools::substr($a->firstname.' '.$a->lastname, 0, 35);
$shipper->shipperCountry = 'FR';
$shipper->shipperName = Tools::substr($a->company, 0, 35);
$shipper->shipperName2 = Tools::substr($a->firstname.' '.$a->lastname, 0, 35);
$shipper->shipperZipCode = $a->postcode;
}
$customer = new customerValue();
$customer->customerAdress1 = Configuration::get('CHRONOPOST_CUSTOMER_ADDRESS');
$customer->customerAdress2 = Configuration::get('CHRONOPOST_CUSTOMER_ADDRESS2');
$customer->customerCity = Configuration::get('CHRONOPOST_CUSTOMER_CITY');
$customer->customerCivility = Configuration::get('CHRONOPOST_CUSTOMER_CIVILITY');
$customer->customerContactName = Configuration::get('CHRONOPOST_CUSTOMER_CONTACTNAME');
$customer->customerCountry = 'FR';
$customer->customerName = Configuration::get('CHRONOPOST_CUSTOMER_NAME');
$customer->customerName2 = Configuration::get('CHRONOPOST_CUSTOMER_NAME2');
$customer->customerZipCode = Configuration::get('CHRONOPOST_CUSTOMER_ZIPCODE');
$ref = new refValue();
$ref->recipientRef = $a->postcode;
// CARRIER-SPECIFIC
switch ($o->id_carrier)
{
case Configuration::get('CHRONORELAIS_CARRIER_ID'):
if ($isReturn) break; // returns are Chrono13
$skybill->productCode = Chronopost::$productCodes['CHRONORELAIS_CARRIER_ID'];
$row = Db::getInstance()->getRow('SELECT id_pr FROM '._DB_PREFIX_.'chrono_cart_relais WHERE id_cart='.$o->id_cart);
$ref->recipientRef = $row['id_pr'];
break;
case Configuration::get('CHRONOEXPRESS_CARRIER_ID'):
if ($isReturn) break; // returns are Chrono13
$skybill->productCode = Chronopost::$productCodes['CHRONOEXPRESS_CARRIER_ID'];
break;
case Configuration::get('CHRONOPOST_CARRIER_ID'):
$skybill->productCode = Chronopost::$productCodes['CHRONOPOST_CARRIER_ID'];
if (Configuration::get('CHRONOPOST_BAL_ENABLED') == 1 && !$isReturn)
$skybill->productCode = '58'; // CHRONO 13 + BAL
break;
case Configuration::get('CHRONO18_CARRIER_ID'):
if ($isReturn) break; // returns are Chrono13
$skybill->productCode = Chronopost::$productCodes['CHRONO18_CARRIER_ID'];
if (Configuration::get('CHRONOPOST_BAL_ENABLED') == 1)
$skybill->productCode = '2M'; // CHRONO 18/ + BAL
break;
case Configuration::get('CHRONO10_CARRIER_ID'):
if ($isReturn) break; // returns are Chrono13
$skybill->productCode = Chronopost::$productCodes['CHRONO10_CARRIER_ID'];
break;
case Configuration::get('CHRONOCLASSIC_CARRIER_ID'):
if ($isReturn) break; // returns are Chrono13
$skybill->productCode = Chronopost::$productCodes['CHRONOCLASSIC_CARRIER_ID'];
break;
}
$ref->shipperRef = sprintf('%06d', $orderid);
$skybill->service = '0';
if ($o->id_carrier == Configuration::get('CHRONORELAIS_CARRIER_ID') ||
$o->id_carrier == Configuration::get('CHRONOPOST_CARRIER_ID') ||
$o->id_carrier == Configuration::get('CHRONO10_CARRIER_ID') ||
$o->id_carrier == Configuration::get('CHRONO18_CARRIER_ID')) // Intl' service is always 0
{
// Called from hookAdminOrder
if (Tools::getIsset('shipSaturday'))
$skybill->service = '6';
// Called from export admin
if (Tools::getIsset('orders') && Chronopost::isSaturdayOptionApplicable())
$skybill->service = '6';
// Called from orders pane
if (Tools::getIsset('orderid') && Chronopost::isSaturdayOptionApplicable())
$skybill->service = '6';
// Could be shipping for saturday but is not
if (Chronopost::gettingReadyForSaturday() && $skybill->service != '6') $skybill->service = '1';
}
//echo Chronopost::gettingReadyForSaturday()." ".$skybill->service;
$skybill->shipDate = date('Y-m-d\TH:i:s');
$skybill->shipHour = date('H');
// weight 0 when multishipping
$skybill->weight = 0;
// Only 1 skybill, put real weight.
if ($totalnb == 1) $skybill->weight = $o->getTotalWeight() * Configuration::get('CHRONOPOST_GENERAL_WEIGHTCOEF');
$skybill->weightUnit = 'KGM';
$skybillParams = new skybillParamsValue();
$skybillParams->mode = Configuration::get('CHRONOPOST_GENERAL_PRINTMODE');
$params->esdValue = $esd;
$params->headerValue = $header;
$params->shipperValue = $shipper;
$params->customerValue = $customer;
$params->recipientValue = $recipient;
$params->refValue = $ref;
$params->skybillValue = $skybill;
$params->skybillParamsValue = $skybillParams;
$service = new ShippingServiceWSService();
$r = $service->shipping($params)->return;
/*
var_dump($params);
var_dump($service->shipping($params)->return);
*/
if ($r->errorCode != 0)
return null;
// MAIL::SEND is bugged in 1.5 !
// http://forge.prestashop.com/browse/PNM-754 (Unresolved as of 2013-04-15)
// Context fix (it's that easy)
Context::getContext()->link = new Link();
if ($isReturn)
{
$customer = new Customer($o->id_customer);
Mail::Send($o->id_lang, 'return', 'Lettre de transport Chronopost pour le retour de votre commande',
array('{id_order}' => $o->id, '{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname), $customer->email,
$customer->firstname.' '.$customer->lastname, null, null,
array('content' => $r->skybill, 'mime' => 'application/pdf', 'name' => $r->skybillNumber.'.pdf'),
null, _MYDIR_.'/mails/', true);
}
else
{
// Store LT for history
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'chrono_lt_history` VALUES (
'.(int)$o->id.',
"'.pSQL($r->skybillNumber).'",
"'.pSQL($skybill->productCode).'",
"'.pSQL($recipient->recipientZipCode).'",
"'.pSQL($recipient->recipientCountry).'",
"'.(isset($skybill->insuredValue) ? (int)$skybill->insuredValue : 0).'",
"'.pSQL($recipient->recipientCity).'"
)');
Chronopost::trackingStatus($o->id, $r->skybillNumber);
}
/*
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
echo $r->skybill;
die();*/
return $r->skybill;
}