-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPredict.php
More file actions
executable file
·211 lines (176 loc) · 7.43 KB
/
Copy pathPredict.php
File metadata and controls
executable file
·211 lines (176 loc) · 7.43 KB
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
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* 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 Predict;
use Predict\Model\PricesQuery;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Thelia\Install\Database;
use Thelia\Model\Country;
use Thelia\Model\ModuleImageQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\OrderPostage;
use Thelia\Model\State;
use Thelia\Module\AbstractDeliveryModuleWithState;
use Thelia\Module\Exception\DeliveryException;
/**
* Class Predict
* @package Predict
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class Predict extends AbstractDeliveryModuleWithState
{
const MESSAGE_DOMAIN = 'predict';
const MESSAGE_DOMAIN_ADMIN = 'predict.ai';
const JSON_PRICE_RESOURCE = "/Config/prices.json";
const PREDICT_TAX_RULE_ID = 'predict_tax_rule_id';
/**
* 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.
*
* @param State|null $state
* @return boolean
* @throws PropelException
*/
public function isValidDelivery(Country $country, State $state = null)
{
$area = $this->getAreaForCountry($country, $state);
if (null === $area){
return false;
}
$areaId = $area->getId();
$prices = PricesQuery::getPrices();
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
/* check if Predict 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;
}
/**
*
* calculate and return delivery price
*
* @param Country $country
* @param State|null $state
* @return mixed
* @throws PropelException
*/
public function getPostage(Country $country, State $state = null)
{
$cart = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher());
$locale = $this->getRequest()->getSession()->getLang()->getLocale();
$cartWeight = $cart->getWeight();
$cartAmount = $cart->getTaxedAmount($country, true, $state);
$postage = $this->getOrderPostage(
$country,
$cartWeight,
$locale,
$cartAmount
);
return $postage;
}
/**
* @param $country
* @param $weight
* @param $locale
* @param $cartAmount
* @return OrderPostage
* @throws DeliveryException
*/
public function getOrderPostage($country, $weight, $locale, $cartAmount = 0)
{
$postage = PricesQuery::getPostageAmount(
$country,
$weight,
$cartAmount
);
return $this->buildOrderPostage($postage, $country, $locale, self::getConfigValue(self::PREDICT_TAX_RULE_ID));
}
public function getCode()
{
return 'Predict';
}
public function postActivation(ConnectionInterface $con = null): void
{
if (!self::getConfigValue(self::PREDICT_TAX_RULE_ID)) {
self::setConfigValue(self::PREDICT_TAX_RULE_ID, null);
}
$database = new Database($con);
$database->insertSql(null, [__DIR__ . '/Config/insert.sql']);
/* insert the images from image folder if first module activation */
$module = $this->getModuleModel();
if (ModuleImageQuery::create()->filterByModule($module)->count() == 0) {
$this->deployImageFolder($module, sprintf('%s/media', __DIR__), $con);
}
/* set module description */
$enUSDescription = <<<US_DESC
Discover delivery Predict:
<ul>
<li>You place your order and choose to have it delivered with Predict by Exapaq</li>
<li>Once your order is prepared, we send you an SMS with several choices of dates and delivery slots</li>
<li>You select the date and the time slot that suits you by answering directly by SMS (price of a standard SMS) or by going to the space available on <a href="http://destinataires.exapaq.com">http://destinataires.exapaq.com</a> Recipient</li>
<li>The day of delivery, you will receive an SMS reminding you the time slot.</li>
</ul>
US_DESC;
$frFRDescription = <<<FR_DESC
Découvrez la livraison Predict :
<ul>
<li>Vous faites votre commande et choisissez de vous faire livrer avec Predict par Exapaq</li>
<li>Une fois votre commande préparée, nous vous envoyons un SMS avec plusieurs choix de dates et créneaux horaires de livraison</li>
<li>Vous sélectionnez la date et le créneau qui vous conviennent le mieux en répondant directement par SMS (prix d’un SMS standard) ou en allant sur l’Espace Destinataire disponible sur <a href="http://destinataires.exapaq.com">http://destinataires.exapaq.com</a></li>
<li>Le jour de la livraison, vous recevez un SMS vous rappelant le créneau horaire.</li>
</ul>
FR_DESC;
$this->getModuleModel()
->setLocale()
->setDescription($enUSDescription)
->setLocale("fr_FR")
->setDescription($frFRDescription)
->save()
;
}
public static function getModuleId()
{
return ModuleQuery::create()->findOneByCode("Predict")->getId();
}
public static function getFreeShippingAmount()
{
if (!null !== $amount = self::getConfigValue('free_shipping_amount')) {
return (float) $amount;
}
return 0;
}
public static function setFreeShippingAmount($amount)
{
self::setConfigValue('free_shipping_amount', $amount);
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
}