1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GGGGino\SkuskuCartBundle\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Criteria; |
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Skusku |
11
|
|
|
* |
12
|
|
|
* @ORM\Table(name="skusku_cart") |
13
|
|
|
* @ORM\Entity(repositoryClass="GGGGino\SkuskuCartBundle\Repository\CartRepository") |
14
|
|
|
*/ |
15
|
|
|
class SkuskuCart |
16
|
|
|
{ |
17
|
|
|
const STATUS_INITIAL = 0; |
18
|
|
|
const STATUS_ORDERED = 1; |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
* |
22
|
|
|
* @ORM\Column(name="id", type="integer") |
23
|
|
|
* @ORM\Id |
24
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
25
|
|
|
*/ |
26
|
|
|
private $id; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @ORM\OneToMany(targetEntity="SkuskuCartProduct", mappedBy="cart", fetch="EXTRA_LAZY") |
30
|
|
|
*/ |
31
|
|
|
private $products; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @ORM\OneToOne(targetEntity="SkuskuPayment", inversedBy="cart") |
35
|
|
|
* @ORM\JoinColumn(name="payment", referencedColumnName="id") |
36
|
|
|
*/ |
37
|
|
|
private $payment; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @ORM\OneToOne(targetEntity="SkuskuOrder", mappedBy="cart") |
41
|
|
|
*/ |
42
|
|
|
private $order; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @ORM\ManyToOne(targetEntity="SkuskuLangInterface") |
46
|
|
|
* @ORM\JoinColumn(name="lang_id", referencedColumnName="id") |
47
|
|
|
* @var SkuskuLangInterface |
48
|
|
|
*/ |
49
|
|
|
protected $lang; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @ORM\ManyToOne(targetEntity="SkuskuCurrencyInterface") |
53
|
|
|
* @ORM\JoinColumn(name="currency_id", referencedColumnName="id") |
54
|
|
|
* @var SkuskuCurrencyInterface |
55
|
|
|
*/ |
56
|
|
|
protected $currency; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @ORM\ManyToOne(targetEntity="SkuskuCustomerInterface") |
60
|
|
|
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id") |
61
|
|
|
* @var SkuskuCustomerInterface |
62
|
|
|
*/ |
63
|
|
|
protected $customer; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var \DateTime |
67
|
|
|
* |
68
|
|
|
* @ORM\Column(name="date_add", type="datetime") |
69
|
|
|
*/ |
70
|
|
|
private $dateAdd; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var \DateTime |
74
|
|
|
* |
75
|
|
|
* @ORM\Column(name="date_upd", type="datetime") |
76
|
|
|
*/ |
77
|
|
|
private $dateUpd; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var int |
81
|
|
|
* |
82
|
|
|
* @ORM\Column(name="status", type="integer") |
83
|
|
|
*/ |
84
|
|
|
private $status = self::STATUS_INITIAL; |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Cart constructor. |
89
|
|
|
*/ |
90
|
|
|
public function __construct() |
91
|
|
|
{ |
92
|
|
|
$this->products = new ArrayCollection(); |
93
|
|
|
$this->dateAdd = new \DateTime(); |
94
|
|
|
$this->dateUpd = new \DateTime(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get id. |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function getId() |
103
|
|
|
{ |
104
|
|
|
return $this->id; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set dateAdd. |
109
|
|
|
* |
110
|
|
|
* @param \DateTime $dateAdd |
111
|
|
|
* |
112
|
|
|
* @return SkuskuCart |
113
|
|
|
*/ |
114
|
|
|
public function setDateAdd($dateAdd) |
115
|
|
|
{ |
116
|
|
|
$this->dateAdd = $dateAdd; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get dateAdd. |
123
|
|
|
* |
124
|
|
|
* @return \DateTime |
125
|
|
|
*/ |
126
|
|
|
public function getDateAdd() |
127
|
|
|
{ |
128
|
|
|
return $this->dateAdd; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set dateUpd. |
133
|
|
|
* |
134
|
|
|
* @param \DateTime $dateUpd |
135
|
|
|
* |
136
|
|
|
* @return SkuskuCart |
137
|
|
|
*/ |
138
|
|
|
public function setDateUpd($dateUpd) |
139
|
|
|
{ |
140
|
|
|
$this->dateUpd = $dateUpd; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get dateUpd. |
147
|
|
|
* |
148
|
|
|
* @return \DateTime |
149
|
|
|
*/ |
150
|
|
|
public function getDateUpd() |
151
|
|
|
{ |
152
|
|
|
return $this->dateUpd; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return SkuskuCustomerInterface |
157
|
|
|
*/ |
158
|
|
|
public function getCustomer() |
159
|
|
|
{ |
160
|
|
|
return $this->customer; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return ArrayCollection |
165
|
|
|
*/ |
166
|
|
|
public function getProducts() |
167
|
|
|
{ |
168
|
|
|
return $this->products; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param SkuskuProductInterface $product |
173
|
|
|
* @return mixed |
174
|
|
|
*/ |
175
|
|
|
public function getProduct(SkuskuProductInterface $product) |
176
|
|
|
{ |
177
|
|
|
$criteria = Criteria::create()->where(Criteria::expr()->eq("product", $product)); |
178
|
|
|
|
179
|
|
|
return $this->products->matching($criteria); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param mixed $products |
184
|
|
|
* @return SkuskuCart |
185
|
|
|
*/ |
186
|
|
|
public function setProducts($products) |
187
|
|
|
{ |
188
|
|
|
$this->products = $products; |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param SkuskuCartProductInterface $product |
194
|
|
|
* @return SkuskuCart |
195
|
|
|
*/ |
196
|
|
|
public function addProduct(SkuskuCartProductInterface $product) |
197
|
|
|
{ |
198
|
|
|
$product->setCart($this); |
199
|
|
|
|
200
|
|
|
$this->products[] = $product; |
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the total amount of the cart |
206
|
|
|
* |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
|
|
public function getTotalPrice() |
210
|
|
|
{ |
211
|
|
|
return array_reduce($this->getProducts()->toArray(), function($carry, SkuskuCartProduct $product) { |
212
|
|
|
return $carry + ($product->getSubtotal()); |
213
|
|
|
}, 0); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get the number of products added to the cart. |
218
|
|
|
* Useful for cart preview |
219
|
|
|
* |
220
|
|
|
* @return integer |
221
|
|
|
*/ |
222
|
|
|
public function getTotalQuantity() |
223
|
|
|
{ |
224
|
|
|
return array_reduce($this->getProducts()->toArray(), function($carry, SkuskuCartProduct $product) { |
225
|
|
|
return $carry + ($product->getQuantity()); |
226
|
|
|
}, 0); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return SkuskuCurrencyInterface |
231
|
|
|
*/ |
232
|
|
|
public function getCurrency() |
233
|
|
|
{ |
234
|
|
|
return $this->currency; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param SkuskuCurrencyInterface $currency |
239
|
|
|
* @return SkuskuCart |
240
|
|
|
*/ |
241
|
|
|
public function setCurrency($currency) |
242
|
|
|
{ |
243
|
|
|
$this->currency = $currency; |
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return SkuskuLangInterface |
249
|
|
|
*/ |
250
|
|
|
public function getLang() |
251
|
|
|
{ |
252
|
|
|
return $this->lang; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param SkuskuLangInterface $lang |
257
|
|
|
* @return SkuskuCart |
258
|
|
|
*/ |
259
|
|
|
public function setLang($lang) |
260
|
|
|
{ |
261
|
|
|
$this->lang = $lang; |
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param SkuskuCustomerInterface $customer |
267
|
|
|
* @return SkuskuCart |
268
|
|
|
*/ |
269
|
|
|
public function setCustomer($customer) |
270
|
|
|
{ |
271
|
|
|
$this->customer = $customer; |
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return mixed |
277
|
|
|
*/ |
278
|
|
|
public function getPayment() |
279
|
|
|
{ |
280
|
|
|
return $this->payment; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param mixed $payment |
285
|
|
|
* @return SkuskuCart |
286
|
|
|
*/ |
287
|
|
|
public function setPayment($payment) |
288
|
|
|
{ |
289
|
|
|
$this->payment = $payment; |
290
|
|
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return int |
295
|
|
|
*/ |
296
|
|
|
public function getStatus() |
297
|
|
|
{ |
298
|
|
|
return $this->status; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param int $status |
303
|
|
|
* @return SkuskuCart |
304
|
|
|
*/ |
305
|
|
|
public function setStatus($status) |
306
|
|
|
{ |
307
|
|
|
$this->status = $status; |
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|