This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSetDeliveryModule.php
More file actions
executable file
·194 lines (165 loc) · 7.77 KB
/
Copy pathSetDeliveryModule.php
File metadata and controls
executable file
·194 lines (165 loc) · 7.77 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
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* 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\Listener;
use SoColissimo\WebService\FindById;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Model\ConfigQuery;
use Thelia\Model\OrderAddressQuery;
use SoColissimo\SoColissimo;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Model\AddressQuery;
use SoColissimo\Model\AddressSocolissimoQuery;
use SoColissimo\Model\AddressSocolissimo;
use SoColissimo\Model\OrderAddressSocolissimo;
/**
* Class SetDeliveryModule
* @package SoColissimo\Listener
* @author Thelia <info@thelia.net>
*/
class SetDeliveryModule implements EventSubscriberInterface
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function getRequest()
{
return $this->request;
}
protected function check_module($id)
{
return $id == SoColissimo::getModCode();
}
public function isModuleSoColissimo(OrderEvent $event)
{
if ($this->check_module($event->getDeliveryModule())) {
$request = $this->getRequest();
$pr_code = $request->get('socolissimo_code');
$dom = $request->get('domicile');
$request->getSession()->set('SoColissimoDeliveryId', 0);
$request->getSession()->set('SoColissimoDomiciile', 0);
if ($dom) {
$request->getSession()->set('SoColissimoDomiciile', 1);
} elseif (!empty($pr_code)) {
$req = new FindById();
$req->setId($pr_code)
->setLangue("FR")
->setDate(date("d/m/Y"))
->setAccountNumber(ConfigQuery::read('socolissimo_login'))
->setPassword(ConfigQuery::read('socolissimo_pwd'))
;
$response = $req->exec();
$customer_name = AddressQuery::create()
->findPk($event->getDeliveryAddress());
$address = AddressSocolissimoQuery::create()
->findPk($event->getDeliveryAddress());
$request->getSession()->set('SoColissimoDeliveryId', $event->getDeliveryAddress());
if ($address === null) {
$address = new AddressSocolissimo();
$address->setId($event->getDeliveryAddress());
}
// France Métropolitaine
$address->setCode($pr_code)
->setType($response->typeDePoint)
->setCompany($response->nom)
->setAddress1($response->adresse1)
->setAddress2($response->adresse2)
->setAddress3($response->adresse3)
->setZipcode($response->codePostal)
->setCity($response->localite)
->setFirstname($customer_name->getFirstname())
->setLastname($customer_name->getLastname())
->setTitleId($customer_name->getTitleId())
->setCountryId($customer_name->getCountryId())
->save();
}
else {
throw new \ErrorException("No relay chosen for Socolissimo delivery module");
}
}
}
public function updateDeliveryAddress(OrderEvent $event)
{
if ($this->check_module($event->getOrder()->getDeliveryModuleId())) {
$request = $this->getRequest();
if ($request->getSession()->get('SoColissimoDomiciile') == 1) {
$savecode = new OrderAddressSocolissimo();
$savecode->setId($event->getOrder()->getDeliveryOrderAddressId())
->setCode(0)
->setType('DOM')
->save();
} else {
$tmp_address = AddressSoColissimoQuery::create()
->findPk($request->getSession()->get('SoColissimoDeliveryId'));
if ($tmp_address === null) {
throw new \ErrorException("Got an error with So Colissimo module. Please try again to checkout.");
}
$savecode = new OrderAddressSocolissimo();
$savecode->setId($event->getOrder()->getDeliveryOrderAddressId())
->setCode($tmp_address->getCode())
->setType($tmp_address->getType())
->save();
$update = OrderAddressQuery::create()
->findPK($event->getOrder()->getDeliveryOrderAddressId())
->setCompany($tmp_address->getCompany())
->setAddress1($tmp_address->getAddress1())
->setAddress2($tmp_address->getAddress2())
->setAddress3($tmp_address->getAddress3())
->setZipcode($tmp_address->getZipcode())
->setCity($tmp_address->getCity())
->save();
$tmp_address->delete();
}
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*
* @api
*/
public static function getSubscribedEvents()
{
return array(
TheliaEvents::ORDER_SET_DELIVERY_MODULE => array('isModuleSoColissimo', 64),
TheliaEvents::ORDER_BEFORE_PAYMENT => array('updateDeliveryAddress', 256)
);
}
}