forked from thelia-modules/ColissimoLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColissimoLabel.php
403 lines (330 loc) · 14.8 KB
/
ColissimoLabel.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
<?php
/* This file is part of the Thelia package. */
/* Copyright (c) OpenStudio */
/* email : [email protected] */
/* web : http://www.thelia.net */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
namespace ColissimoLabel;
use ColissimoHomeDelivery\ColissimoHomeDelivery;
use ColissimoLabel\Enum\AuthorizedModuleEnum;
use ColissimoLabel\Request\Helper\OutputFormat;
use ColissimoLabel\Request\Helper\Service;
use ColissimoPickupPoint\ColissimoPickupPoint;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use SplFileInfo;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Thelia\Install\Database;
use Thelia\Model\ConfigQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\Order;
use Thelia\Module\BaseModule;
/**
* @author Gilles Bourgeat >[email protected]>
*/
class ColissimoLabel extends BaseModule
{
/** Constants */
const DOMAIN_NAME = 'colissimolabel';
const LABEL_FOLDER = THELIA_LOCAL_DIR.'colissimo-label';
const BORDEREAU_FOLDER = self::LABEL_FOLDER.DIRECTORY_SEPARATOR.'bordereau';
const CONFIG_KEY_DEFAULT_LABEL_FORMAT = 'default-label-format';
const CONFIG_KEY_CONTRACT_NUMBER = 'contract-number';
const CONFIG_KEY_PASSWORD = 'password';
const CONFIG_KEY_LAST_BORDEREAU_DATE = 'last-bordereau-date';
const CONFIG_DEFAULT_KEY_LAST_BORDEREAU_DATE = 1970;
const CONFIG_KEY_DEFAULT_SIGNED = 'default-signed';
const CONFIG_DEFAULT_KEY_DEFAULT_SIGNED = true;
const CONFIG_KEY_GENERATE_BORDEREAU = 'generate-bordereau';
const CONFIG_DEFAULT_KEY_GENERATE_BORDEREAU = false;
const CONFIG_KEY_GET_INVOICES = 'get-invoices';
const CONFIG_DEFAULT_KEY_GET_INVOICES = true;
const CONFIG_KEY_GET_CUSTOMS_INVOICES = 'get-customs-invoices';
const CONFIG_DEFAULT_KEY_GET_CUSTOMS_INVOICES = false;
const CONFIG_KEY_CUSTOMS_PRODUCT_HSCODE = 'customs-product-hscode';
const CONFIG_DEFAULT_KEY_CUSTOMS_PRODUCT_HSCODE = '';
const CONFIG_KEY_STATUS_CHANGE = 'new_status';
const CONFIG_DEFAULT_KEY_STATUS_CHANGE = 'nochange';
const CONFIG_KEY_ENDPOINT = 'colissimolabel-endpoint';
const CONFIG_DEFAULT_KEY_ENDPOINT = 'https://ws.colissimo.fr/sls-ws/SlsServiceWS/2.0?wsdl';
const CONFIG_KEY_FROM_NAME = 'colissimolabel-company-name';
const CONFIG_KEY_FROM_ADDRESS_1 = 'colissimolabel-from-address-1';
const CONFIG_KEY_FROM_ADDRESS_2 = 'colissimolabel-from-address-2';
const CONFIG_KEY_FROM_CITY = 'colissimolabel-from-city';
const CONFIG_KEY_FROM_ZIPCODE = 'colissimolabel-from-zipcode';
const CONFIG_KEY_FROM_COUNTRY = 'colissimolabel-from-country';
const CONFIG_KEY_FROM_CONTACT_EMAIL = 'colissimolabel-from-contact-email';
const CONFIG_KEY_FROM_PHONE = 'colissimolabel-from-phone';
/**
* @param ConnectionInterface|null $con
*/
public function postActivation(ConnectionInterface $con = null): void
{
static::checkLabelFolder();
if (!self::getConfigValue('is_initialized', false)) {
$database = new Database($con);
$database->insertSql(null, [__DIR__.'/Config/TheliaMain.sql']);
self::setConfigValue('is_initialized', true);
}
$this->checkConfigurationsValues();
}
public function update($currentVersion, $newVersion, ConnectionInterface $con = null): void
{
$finder = Finder::create()
->name('*.sql')
->depth(0)
->sortByName()
->in(__DIR__.DS.'Config'.DS.'update');
$database = new Database($con);
/** @var SplFileInfo $file */
foreach ($finder as $file) {
if (version_compare($currentVersion, $file->getBasename('.sql'), '<')) {
$database->insertSql(null, [$file->getPathname()]);
}
}
}
/**
* Check if config values exist in the module config table exists. Creates them with a default value otherwise.
*/
public function checkConfigurationsValues()
{
/* Check if the default label format config value exists, and sets it to PDF_10x15_300dpi is it doesn't exists */
if (null === self::getConfigValue(self::CONFIG_KEY_DEFAULT_LABEL_FORMAT)) {
self::setConfigValue(
self::CONFIG_KEY_DEFAULT_LABEL_FORMAT,
OutputFormat::OUTPUT_PRINTING_TYPE_DEFAULT
);
}
/*
* Check if the contract number config value exists, and sets it to either of the following :
* The contract number of the ColissimoHomeDelivery config, if the module is installed
* Otherwise : the contract number of the ColissimoPickupPoint config, if the module is installed
* Otherwise : a blank string : ""
*/
if (null === self::getConfigValue(self::CONFIG_KEY_CONTRACT_NUMBER)) {
$contractNumber = '';
if (ModuleQuery::create()->findOneByCode(AuthorizedModuleEnum::ColissimoPickupPoint->value)) {
$contractNumber = ColissimoPickupPoint::getConfigValue('colissimo_pickup_point_username');
}
if (ModuleQuery::create()->findOneByCode(AuthorizedModuleEnum::ColissimoHomeDelivery->value)) {
$contractNumber = ColissimoHomeDelivery::getConfigValue('colissimo_home_delivery_username');
}
self::setConfigValue(
self::CONFIG_KEY_CONTRACT_NUMBER,
$contractNumber
);
}
/*
* Check if the contract password config value exists, and sets it to either of the following :
* The contract password of the ColissimoHomeDelivery config, if the module is activated
* Otherwise : the contract password of the ColissimoPickupPoint config, if the module is activated
* Otherwise : a blank string : ""
*/
if (null === self::getConfigValue(self::CONFIG_KEY_PASSWORD)) {
$contractPassword = '';
if (ModuleQuery::create()->findOneByCode(AuthorizedModuleEnum::ColissimoPickupPoint->value)) {
$contractPassword = ColissimoPickupPoint::getConfigValue('colissimo_pickup_point_password');
}
if (ModuleQuery::create()->findOneByCode(AuthorizedModuleEnum::ColissimoHomeDelivery->value)) {
$contractPassword = ColissimoHomeDelivery::getConfigValue('colissimo_home_delivery_password');
}
self::setConfigValue(
self::CONFIG_KEY_PASSWORD,
$contractPassword
);
}
/* Check if the config value for the status change exists, creates it with a default value of 'nochange' otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_STATUS_CHANGE)) {
self::setConfigValue(
self::CONFIG_KEY_STATUS_CHANGE,
self::CONFIG_DEFAULT_KEY_STATUS_CHANGE
);
}
/* Check if the config value for the endpoint exists, creates it with a default value otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_ENDPOINT)) {
self::setConfigValue(
self::CONFIG_KEY_ENDPOINT,
self::CONFIG_DEFAULT_KEY_ENDPOINT
);
}
/* Check if the config value for the last bordereau date exists, creates it with a default value of 1970 otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_LAST_BORDEREAU_DATE)) {
self::setConfigValue(
self::CONFIG_KEY_LAST_BORDEREAU_DATE,
self::CONFIG_DEFAULT_KEY_LAST_BORDEREAU_DATE
);
}
/* Check if the config value for the default signed state for labels exists, creates it with a value of true otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_DEFAULT_SIGNED)) {
self::setConfigValue(
self::CONFIG_KEY_DEFAULT_SIGNED,
self::CONFIG_DEFAULT_KEY_DEFAULT_SIGNED
);
}
/* Check if the config value for whether bordereau should be generated with labels exists, creates it with a value of false otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_GENERATE_BORDEREAU)) {
self::setConfigValue(
self::CONFIG_KEY_GENERATE_BORDEREAU,
(int) self::CONFIG_DEFAULT_KEY_GENERATE_BORDEREAU
);
}
/* Check if the config value for whether invoices should be automatically generated exists, creates it with a value of true otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_GET_INVOICES)) {
self::setConfigValue(
self::CONFIG_KEY_GET_INVOICES,
self::CONFIG_DEFAULT_KEY_GET_INVOICES
);
}
/* Check if the config value for whether customs invoices should be automatically generated exists, creates it with a value of false otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_GET_CUSTOMS_INVOICES)) {
self::setConfigValue(
self::CONFIG_KEY_GET_CUSTOMS_INVOICES,
self::CONFIG_DEFAULT_KEY_GET_CUSTOMS_INVOICES
);
}
/* Check if the config value for the customs product HsCode exists, creates it without value otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_CUSTOMS_PRODUCT_HSCODE)) {
self::setConfigValue(
self::CONFIG_KEY_CUSTOMS_PRODUCT_HSCODE,
self::CONFIG_DEFAULT_KEY_CUSTOMS_PRODUCT_HSCODE
);
}
/* Check if the config values for the sender address exist, create them otherwise with the store address values otherwise */
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_NAME)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_NAME,
ConfigQuery::read('store_name')
);
}
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_ADDRESS_1)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_ADDRESS_1,
ConfigQuery::read('store_address1')
);
}
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_ADDRESS_2)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_ADDRESS_2,
ConfigQuery::read('store_address2')
);
}
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_CITY)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_CITY,
ConfigQuery::read('store_city')
);
}
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_ZIPCODE)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_ZIPCODE,
ConfigQuery::read('store_zipcode')
);
}
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_COUNTRY)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_COUNTRY,
ConfigQuery::read('store_country')
);
}
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_CONTACT_EMAIL)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_CONTACT_EMAIL,
ConfigQuery::read('store_email')
);
}
if (null === self::getConfigValue(self::CONFIG_KEY_FROM_PHONE)) {
self::setConfigValue(
self::CONFIG_KEY_FROM_PHONE,
ConfigQuery::read('store_phone')
);
}
/* Sender address values check end here */
}
/**
* Check if the label and bordereau folders exists. Creates them otherwise.
*/
public static function checkLabelFolder()
{
$fileSystem = new Filesystem();
if (!$fileSystem->exists(self::LABEL_FOLDER)) {
$fileSystem->mkdir(self::LABEL_FOLDER);
}
if (!$fileSystem->exists(self::BORDEREAU_FOLDER)) {
$fileSystem->mkdir(self::BORDEREAU_FOLDER);
}
}
/** Get the path of a given label file, according to its number.
* @param $fileName
* @param $extension
*
* @return string
*/
public static function getLabelPath($fileName, $extension): string
{
return self::LABEL_FOLDER.DS.$fileName.'.'.$extension;
}
/** Get the path of a given CN23 customs file, according to the order ref.
* @param $fileName
* @param $extension
*
* @return string
*/
public static function getLabelCN23Path($fileName, $extension): string
{
return self::LABEL_FOLDER.DS.$fileName.'.'.$extension;
}
/** Get the path of a bordereau file, according to a date.
* @param $date
*
* @return string
*/
public static function getBordereauPath($date): string
{
return self::BORDEREAU_FOLDER.DS.$date.'.pdf';
}
/** Get the label files extension according to the file type indicated in the module config */
public static function getFileExtension(): string
{
return strtolower(substr(OutputFormat::OUTPUT_PRINTING_TYPE[self::getConfigValue(self::CONFIG_KEY_DEFAULT_LABEL_FORMAT)], 0, 3));
}
/**
* Check if order has to be signed or if it is optional (aka if its in Europe or not).
*
* @param Order $order
* @return bool
*
* @throws PropelException
*/
public static function canOrderBeNotSigned(Order $order): bool
{
$countryIsoCode = $order->getOrderAddressRelatedByDeliveryOrderAddressId()->getCountry()->getIsocode();
/* Checking if the delivery country is in Europe or a DOMTOM. If not, it HAS to be signed */
if (!in_array($countryIsoCode, Service::DOMTOM_ISOCODES)
&& !in_array($countryIsoCode, Service::EUROPE_ISOCODES)) {
return false;
}
return true;
}
/**
* Remove the accentuated and special characters from a string an replace them with
* latin ASCII characters. Does the same to cyrillic.
*
* @param $str
*
* @return false|string
*/
public static function removeAccents($str): bool|string
{
return iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', transliterator_transliterate('Any-Latin; Latin-ASCII; Upper()', $str));
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR.ucfirst(self::getModuleCode()).'/I18n/*'])
->autowire()
->autoconfigure();
}
}