|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GGGGino\SkuskuCartBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use GGGGino\SkuskuCartBundle\Entity\CartForm; |
|
7
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuCart; |
|
8
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuCartProduct; |
|
9
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuCustomerInterface; |
|
10
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuOrder; |
|
11
|
|
|
use GGGGino\SkuskuCartBundle\Model\SkuskuProductInterface; |
|
12
|
|
|
use Symfony\Component\Form\Form; |
|
13
|
|
|
use Symfony\Component\Form\FormInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
|
16
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
17
|
|
|
|
|
18
|
|
|
class CartManager |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EntityManager |
|
22
|
|
|
*/ |
|
23
|
|
|
private $em; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TokenStorage |
|
27
|
|
|
*/ |
|
28
|
|
|
private $tokenStorage; |
|
29
|
|
|
|
|
30
|
|
|
/** @var boolean */ |
|
31
|
|
|
private $handled = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
private $allowAnonymous; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var CurrencyManager |
|
40
|
|
|
*/ |
|
41
|
|
|
private $currencyManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var LangManager |
|
45
|
|
|
*/ |
|
46
|
|
|
private $langManager; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* CartExtension constructor. |
|
50
|
|
|
* @param EntityManager $em |
|
51
|
|
|
* @param TokenStorage $tokenStorage |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(EntityManager $em, TokenStorage $tokenStorage, $allowAnonymous = true) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->em = $em; |
|
56
|
|
|
$this->tokenStorage = $tokenStorage; |
|
57
|
|
|
$this->allowAnonymous = $allowAnonymous; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the cart from a given Customer. |
|
62
|
|
|
* If the customer is not passed, is taken from the session |
|
63
|
|
|
* |
|
64
|
|
|
* @api |
|
65
|
|
|
* |
|
66
|
|
|
* @param SkuskuCustomerInterface|null $customer |
|
67
|
|
|
* @return SkuskuCart|null |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getCartFromCustomer(SkuskuCustomerInterface $customer = null) |
|
70
|
|
|
{ |
|
71
|
|
|
if ( !$customer ) { |
|
72
|
|
|
$customer = $this->tokenStorage->getToken()->getUser(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// If the user is anon. the arg $customer is a string, so i check it |
|
76
|
|
|
if ( !($customer instanceof SkuskuCustomerInterface) ) { |
|
77
|
|
|
$customer = null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$cart = $this->em |
|
81
|
|
|
->getRepository('GGGGino\SkuskuCartBundle\Model\SkuskuCart') |
|
82
|
|
|
->getOneNonOrderedCartByCustomer($customer); |
|
83
|
|
|
|
|
84
|
|
|
if ( !$cart ) { |
|
85
|
|
|
$cart = $this->createNewCart($customer); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $cart; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Aggiungo il carrello alla coda delle entita da processare di doctrine |
|
93
|
|
|
* |
|
94
|
|
|
* @api |
|
95
|
|
|
* |
|
96
|
|
|
* @param SkuskuCart $cart |
|
97
|
|
|
*/ |
|
98
|
|
|
public function persistCart(SkuskuCart $cart) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->em->persist($cart); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Faccio il flush del carrello |
|
105
|
|
|
* |
|
106
|
|
|
* @api |
|
107
|
|
|
* |
|
108
|
|
|
* @param SkuskuCart $cart |
|
109
|
|
|
*/ |
|
110
|
|
|
public function flushCart(SkuskuCart $cart) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->em->flush($cart); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Empty the cart tables for development, garbage collector purposes |
|
117
|
|
|
* |
|
118
|
|
|
* @api |
|
119
|
|
|
*/ |
|
120
|
|
|
public function clearCart() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuCartProduct cp')->execute(); |
|
123
|
|
|
$this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuCart c')->execute(); |
|
124
|
|
|
$this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuPayment c')->execute(); |
|
125
|
|
|
$this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuPaymentToken c')->execute(); |
|
126
|
|
|
$this->em->createQuery('DELETE GGGGino\SkuskuCartBundle\Model\SkuskuOrder c')->execute(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Used from the mini form |
|
131
|
|
|
* |
|
132
|
|
|
* @param Request $request |
|
133
|
|
|
* @param FormInterface $form |
|
134
|
|
|
*/ |
|
135
|
|
|
public function addProductToCartForm(Request $request, FormInterface $form) |
|
136
|
|
|
{ |
|
137
|
|
|
if ( $this->handled ) { |
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$form->handleRequest($request); |
|
142
|
|
|
|
|
143
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
144
|
|
|
$this->handled = true; |
|
145
|
|
|
/** @var integer $idProduct */ |
|
146
|
|
|
$idProduct = intval($form->get('idProduct')->getData()); |
|
147
|
|
|
/** @var integer $quantity */ |
|
148
|
|
|
$quantity = intval($form->get('quantity')->getData()); |
|
149
|
|
|
/** @var SkuskuCustomerInterface $customer */ |
|
150
|
|
|
$customer = $this->tokenStorage->getToken()->getUser(); |
|
|
|
|
|
|
151
|
|
|
/** @var SkuskuProductInterface $productReference */ |
|
152
|
|
|
$productReference = $this->em->getReference(SkuskuProductInterface::class, $idProduct); |
|
153
|
|
|
|
|
154
|
|
|
//if( !($customer instanceof UserInterface) ) |
|
155
|
|
|
// $customer = null; |
|
156
|
|
|
|
|
157
|
|
|
$this->addProductToCart($productReference, $quantity); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Add the product to the cart checking that: |
|
163
|
|
|
* - the cart exist |
|
164
|
|
|
* -- if it not exist, I create it |
|
165
|
|
|
* - Update the cart date |
|
166
|
|
|
* - add & update the cart |
|
167
|
|
|
* |
|
168
|
|
|
* @api |
|
169
|
|
|
* |
|
170
|
|
|
* @param SkuskuProductInterface $product |
|
171
|
|
|
* @param $quantity |
|
172
|
|
|
*/ |
|
173
|
|
|
public function addProductToCart(SkuskuProductInterface $product, $quantity) |
|
174
|
|
|
{ |
|
175
|
|
|
/** @var SkuskuCustomerInterface $customer */ |
|
176
|
|
|
$customer = $this->tokenStorage->getToken()->getUser(); |
|
177
|
|
|
|
|
178
|
|
|
/** @var SkuskuCart $cart */ |
|
179
|
|
|
$cart = $this->getCartFromCustomer($customer); |
|
180
|
|
|
$cart->setDateUpd(new \DateTime()); |
|
181
|
|
|
|
|
182
|
|
|
$this->em->persist($cart); |
|
183
|
|
|
|
|
184
|
|
|
/** @var SkuskuCartProduct $productCart */ |
|
185
|
|
|
if ( $productCart = $cart->getProduct($product)->first() ) { |
|
186
|
|
|
// @todo what?? $productCart->getQuantity() + $quantity |
|
187
|
|
|
$productCart->setQuantity($productCart->getQuantity() + $quantity); |
|
188
|
|
|
} else { |
|
189
|
|
|
$productCart = new SkuskuCartProduct(); |
|
190
|
|
|
$productCart->setProduct($product); |
|
191
|
|
|
$productCart->setQuantity($quantity); |
|
192
|
|
|
|
|
193
|
|
|
$cart->addProduct($productCart); |
|
194
|
|
|
$this->em->persist($productCart); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$this->em->flush(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Create new cart |
|
202
|
|
|
* |
|
203
|
|
|
* @api |
|
204
|
|
|
* |
|
205
|
|
|
* @param SkuskuCustomerInterface|null $customer |
|
206
|
|
|
* @return SkuskuCart |
|
207
|
|
|
*/ |
|
208
|
|
|
public function createNewCart(SkuskuCustomerInterface $customer = null) |
|
209
|
|
|
{ |
|
210
|
|
|
$cart = new SkuskuCart(); |
|
211
|
|
|
$cart->setDateAdd(new \DateTime()); |
|
212
|
|
|
$cart->setCustomer($customer); |
|
213
|
|
|
$cart->setCurrency($this->currencyManager->getCurrentCurrency()); |
|
214
|
|
|
|
|
215
|
|
|
return $cart; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Build a new Order from a given cart. |
|
220
|
|
|
* Used for example when the payment gone good |
|
221
|
|
|
* |
|
222
|
|
|
* @api |
|
223
|
|
|
* |
|
224
|
|
|
* @param SkuskuCart $cart |
|
225
|
|
|
* @return SkuskuOrder |
|
226
|
|
|
*/ |
|
227
|
|
|
public function createNewOrderFromCart(SkuskuCart $cart) |
|
228
|
|
|
{ |
|
229
|
|
|
$totPrice = $cart->getTotalPrice(); |
|
230
|
|
|
|
|
231
|
|
|
$order = new SkuskuOrder(); |
|
232
|
|
|
$order->setCurrency($cart->getCurrency()); |
|
233
|
|
|
$order->setDateAdd(new \DateTime()); |
|
234
|
|
|
$order->setCustomer($cart->getCustomer()); |
|
235
|
|
|
$order->setCart($cart); |
|
236
|
|
|
$order->setLang($cart->getLang()); |
|
237
|
|
|
$order->setTotalPaid($totPrice); |
|
238
|
|
|
$order->setTotalPaidReal($totPrice); |
|
239
|
|
|
$order->setTotalProducts($totPrice); |
|
240
|
|
|
|
|
241
|
|
|
return $order; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param CartForm $formData |
|
246
|
|
|
*/ |
|
247
|
|
|
public function emptyCart(CartForm $formData) |
|
248
|
|
|
{ |
|
249
|
|
|
$formData->getCart()->emptyProducts(); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Check if the cart is temporary created or permanent |
|
254
|
|
|
* |
|
255
|
|
|
* @api |
|
256
|
|
|
* |
|
257
|
|
|
* @param SkuskuCart $cart |
|
258
|
|
|
* @return bool |
|
259
|
|
|
*/ |
|
260
|
|
|
public function isCartTemp(SkuskuCart $cart) |
|
261
|
|
|
{ |
|
262
|
|
|
return !((bool) $cart->getId()); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param CurrencyManager $currencyManager |
|
267
|
|
|
* @return CartManager |
|
268
|
|
|
*/ |
|
269
|
|
|
public function setCurrencyManager($currencyManager) |
|
270
|
|
|
{ |
|
271
|
|
|
$this->currencyManager = $currencyManager; |
|
272
|
|
|
return $this; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @param LangManager $langManager |
|
277
|
|
|
* @return CartManager |
|
278
|
|
|
*/ |
|
279
|
|
|
public function setLangManager($langManager) |
|
280
|
|
|
{ |
|
281
|
|
|
$this->langManager = $langManager; |
|
282
|
|
|
return $this; |
|
283
|
|
|
} |
|
284
|
|
|
} |