forked from thelia-modules/SoColissimo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoColissimo.php
executable file
·183 lines (149 loc) · 6.49 KB
/
SoColissimo.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
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : [email protected] */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace SoColissimo;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Country;
use Thelia\Model\ModuleQuery;
use Thelia\Module\BaseModule;
use Thelia\Module\DeliveryModuleInterface;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Exception\OrderException;
use Propel\Runtime\Connection\ConnectionInterface;
use SoColissimo\Model\SocolissimoFreeshippingQuery;
use Thelia\Install\Database;
use Thelia\Module\AbstractDeliveryModule;
use Thelia\Module\Exception\DeliveryException;
class SoColissimo extends AbstractDeliveryModule
{
protected $request;
protected $dispatcher;
private static $prices = null;
const JSON_PRICE_RESOURCE = "/Config/prices.json";
const JSON_CONFIG_PATH = "/Config/config.json";
public static function getPrices()
{
if (null === self::$prices) {
self::$prices = json_decode(file_get_contents(sprintf('%s%s', __DIR__, self::JSON_PRICE_RESOURCE)), true);
}
return self::$prices;
}
/**
* This method is called by the Delivery loop, to check if the current module has to be displayed to the customer.
* Override it to implements your delivery rules/
*
* If you return true, the delivery method will de displayed to the customer
* If you return false, the delivery method will not be displayed
*
* @param Country $country the country to deliver to.
*
* @return boolean
*/
public function isValidDelivery(Country $country)
{
$cartWeight = $this->getRequest()->getSession()->getCart()->getWeight();
$areaId = $country->getAreaId();
$prices = self::getPrices();
/* check if Colissimo delivers the asked area */
if (isset($prices[$areaId]) && isset($prices[$areaId]["slices"])) {
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
/* check this weight is not too much */
end($areaPrices);
$maxWeight = key($areaPrices);
if ($cartWeight <= $maxWeight) {
return true;
}
}
return false;
}
/**
* @param $areaId
* @param $weight
*
* @return mixed
* @throws DeliveryException
*/
public static function getPostageAmount($areaId, $weight)
{
$freeshipping = SocolissimoFreeshippingQuery::create()->getLast();
$postage=0;
if (!$freeshipping) {
$prices = self::getPrices();
/* check if Colissimo delivers the asked area */
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
throw new DeliveryException("SoColissimo delivery unavailable for the chosen delivery country");
}
$areaPrices = $prices[$areaId]["slices"];
ksort($areaPrices);
/* check this weight is not too much */
end($areaPrices);
$maxWeight = key($areaPrices);
if ($weight > $maxWeight) {
throw new DeliveryException(sprintf("SoColissimo delivery unavailable for this cart weight (%s kg)", $weight));
}
$postage = current($areaPrices);
while (prev($areaPrices)) {
if ($weight > key($areaPrices)) {
break;
}
$postage = current($areaPrices);
}
}
return $postage;
}
/**
*
* calculate and return delivery price
*
* @param Country $country
* @return mixed
* @throws DeliveryException
*/
public function getPostage(Country $country)
{
$cartWeight = $this->getRequest()->getSession()->getCart()->getWeight();
$postage = self::getPostageAmount(
$country->getAreaId(),
$cartWeight
);
return $postage;
}
public function getCode()
{
return 'SoColissimo';
}
public function postActivation(ConnectionInterface $con = null)
{
$database = new Database($con);
$database->insertSql(null, [__DIR__ . '/Config/thelia.sql', __DIR__ . '/Config/insert.sql']);
ConfigQuery::write('socolissimo_login', null, 1, 1);
ConfigQuery::write('socolissimo_pwd', null, 1, 1);
ConfigQuery::write('socolissimo_url_prod', "https://ws.colissimo.fr/pointretrait-ws-cxf/PointRetraitServiceWS/2.0?wsdl", 1, 1);
ConfigQuery::write('socolissimo_url_test', "https://pfi.telintrans.fr/pointretrait-ws-cxf/PointRetraitServiceWS/2.0?wsdl", 1, 1);
ConfigQuery::write('socolissimo_test_mode', 1, 1, 1);
}
public static function getModCode()
{
return ModuleQuery::create()->findOneByCode("SoColissimo")->getId();
}
}