initial commit
[namibia] / module / Auction / src / Auction / Entity / Auction.php
1 <?php
2 namespace Auction\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7 /**
8  * An Auction item.
9  *
10  * @ORM\Entity
11  * @ORM\HasLifecycleCallbacks
12  * @ORM\Table(name="auction")
13  */
14 class Auction
15 {
16
17         /**
18          * Can archive records.
19          */
20         const ARCHIVE = true;
21         /**
22          * Pull Synchronization Strategy for this table.
23          */
24         const PULL_SYNCH_STRATEGY = false;
25         /**
26          * Push Synchronization Strategy for this table.
27          */
28         const PUSH_SYNCH_STRATEGY = false;
29
30
31
32         /* ------------------------------------ Identification ------------------------------------ */
33         /**
34          * @ORM\Id
35          * @ORM\Column(type="integer");
36          * @ORM\GeneratedValue(strategy="AUTO")
37          */
38         protected $id;
39
40
41         /* ------------------------------------ Ownership ------------------------------------ */
42         /**
43          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
44          * @ORM\JoinColumn(nullable=false, name="created_by_profile_id")
45          **/
46         protected $createdBy;
47
48         /**
49          * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
50          * @ORM\JoinColumn(nullable=false, name="company_id")
51          **/
52         protected $company;
53
54         /**
55          * @ORM\ManyToOne(targetEntity="\Stock\Entity\Stock")
56          * @ORM\JoinColumn(nullable=false, name="stock_id")
57          **/
58         protected $stock;
59
60         /**
61          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
62          * @ORM\JoinColumn(nullable=true, name="sold_to_profile_id")
63          **/
64         protected $soldToProfile;
65
66         /**
67          * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
68          * @ORM\JoinColumn(nullable=true, name="sold_to_company_id")
69          **/
70         protected $soldToCompany;
71
72
73         /* ------------------------------------ Winning bid ------------------------------------ */
74         /**
75          * @ORM\OneToOne(targetEntity="\Auction\Entity\Bid", cascade={"all"})
76          * @ORM\JoinColumn(nullable=true, name="auction_bid_id")
77          **/
78         protected $currentBid;
79
80         /**
81          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="bid_price", options={"unsigned"=true});
82          */
83         protected $currentBidPrice = 0.0;
84
85
86         /* ------------------------------------ Data ------------------------------------ */
87         /**
88          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="reserve_price",
89          *                             options={"unsigned"=true});
90          */
91         protected $reservePrice = 0.0;
92
93         /**
94          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="bid_increment",
95          *                             options={"unsigned"=true});
96          */
97         protected $bidIncrement = 0.0;
98
99         /**
100          * @ORM\Column(type="datetime", name="start_date");
101          */
102         protected $startDate;
103
104         /**
105          * @ORM\Column(type="datetime", name="end_date");
106          */
107         protected $endDate;
108
109         /**
110          * @ORM\Column(type="integer", nullable=false, name="number_of_bids");
111          */
112         protected $numberOfBids = 0;
113
114
115         /* ------------------------------------ Linkage ------------------------------------ */
116         /**
117          * @ORM\OneToMany(targetEntity="Basket", mappedBy="auction", cascade={"all"}, fetch="EXTRA_LAZY")
118          **/
119         protected $baskets;
120
121         /**
122          * @ORM\OneToMany(targetEntity="Bid", mappedBy="auction", cascade={"all"}, fetch="EXTRA_LAZY")
123          **/
124         protected $bids;
125
126
127         /* ------------------------------------ De-normalization ------------------------------------ */
128         /* ------------------------------------ Searchable */
129
130         /**
131          * @ORM\Column(type="integer", name="region_id", nullable=true);
132          */
133         protected $regionId;
134
135         /**
136          * @ORM\Column(type="integer", name="make_id", nullable=true);
137          */
138         protected $makeId;
139
140         /**
141          * @ORM\Column(type="integer", name="auction_company_id", nullable=true);
142          */
143         protected $auctionCompanyId;
144
145         /**
146          * @ORM\Column(type="string", name="auction_company_name", length=100, nullable=true)
147          */
148         protected $auctionCompanyName;
149
150         /**
151          * @ORM\Column(type="string", name="model_name", length=75, nullable=true)
152          */
153         protected $modelName;
154
155         /**
156          * @ORM\Column(type="string", name="type_name", length=75, nullable=true)
157          */
158         protected $typeName;
159
160         /**
161          * @ORM\Column(type="string", length=12, nullable=true, name="reference_no", nullable=true)
162          */
163         protected $referenceNumber;
164
165         /**
166          * @ORM\Column(type="string", length=15, name="registration_number", nullable=true)
167          */
168         protected $registrationNumber;
169
170         /**
171          * @ORM\Column(type="integer", nullable=true, options={"unsigned"=true}, nullable=true)
172          */
173         protected $stockKm;
174
175         /* ------------------------------------ Not Searchable */
176
177         /**
178          * @ORM\Column(type="integer", name="current_bid_company_id", nullable=true);
179          */
180         protected $currentBidCompanyId;
181
182         /**
183          * @ORM\Column(type="integer", name="stock_company_id", nullable=true);
184          */
185         protected $stockCompanyId;
186
187         /**
188          * @ORM\Column(type="string", name="region_name", length=75, nullable=true)
189          */
190         protected $regionName;
191
192         /**
193          * @ORM\Column(type="string", name="make_name", length=75, nullable=true)
194          */
195         protected $makeName;
196
197         /**
198          * @ORM\Column(type="string", name="year_name", length=4, nullable=true)
199          */
200         protected $yearName;
201
202         /**
203          * @ORM\Column(type="datetime", name="stock_created", nullable=true);
204          */
205         protected $stockCreated;
206
207
208
209         /* ------------------------------------ Tracking ------------------------------------ */
210         /**
211          * @ORM\Column(type="boolean", name="email_sent");
212          */
213         protected $emailSent = false;
214
215         /**
216          * @ORM\Column(nullable=true, type="string", length=25, name="status");
217          */
218         protected $jobState = 'Active';
219
220         /**
221          * @ORM\Column(type="datetime");
222          */
223         protected $created;
224
225         /**
226          * @ORM\Column(type="datetime", nullable=true);
227          */
228         protected $updated;
229
230         /**
231          * @ORM\Column(type="boolean");
232          */
233         protected $archived = false;
234
235
236
237         /**
238          * Initialize collections.
239          */
240         public function __construct()
241         {
242                 $this->baskets = new \Doctrine\Common\Collections\ArrayCollection();
243                 $this->bids    = new \Doctrine\Common\Collections\ArrayCollection();
244         }
245
246         /**
247          * Magic getter to expose protected properties.
248          *
249          * @param string $property
250          * @return mixed
251          */
252         public function __get($property)
253         {
254                 return $this->$property;
255         }
256
257         /**
258          * Magic setter to save protected properties.
259          *
260          * @param string $property
261          * @param mixed  $value
262          */
263         public function __set($property, $value)
264         {
265                 $this->$property = $value;
266         }
267
268         /**
269          * @ORM\PrePersist
270          */
271         public function setCreateTime()
272         {
273                 $this->created = new \DateTime("now");
274                 if (is_null($this->startDate))
275                 {
276                         $this->startDate = new \DateTime("now");
277                 }
278                 $this->regionId            = $this->company->city->region->id;
279                 $this->regionName          = $this->company->city->region->name;
280                 $this->auctionCompanyId    = $this->company->id;
281                 $this->auctionCompanyName  = $this->company->name;
282                 $this->makeId              = $this->stock->type->model->make->id;
283                 $this->makeName            = $this->stock->type->model->make->name;
284                 $this->modelName           = $this->stock->type->model->name;
285                 $this->typeName            = $this->stock->type->name;
286                 $this->yearName            = $this->stock->vehicleYear->name;
287                 $this->referenceNumber     = $this->stock->referenceNumber;
288                 $this->registrationNumber  = $this->stock->registrationNumber;
289                 $this->stockKm             = $this->stock->km;
290                 $this->stockCompanyId      = $this->stock->company->id;
291                 $this->stockCreated        = $this->stock->created;
292         }
293
294         /**
295          * @ORM\PreUpdate
296          */
297         public function setUpdateTime()
298         {
299                 $this->updated = new \DateTime("now");
300                 if (!is_null($this->currentBid))
301                 {
302                         $this->currentBidCompanyId = $this->currentBid->company->id;
303                 }
304         }
305
306         /**
307          * Convert the object to an array.
308          * @param array   $expand
309          * @param array   $intersect
310          * @param boolean $showIdentifiers
311          * @param integer $expandAll
312          * @return array
313          */
314         public function toArray(
315                 array $expand = array(), array $intersect = array(),
316                 $showIdentifiers = false, $expandAll = 0
317         )
318         {
319                 $intersect      = array_flip($intersect);
320                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
321                 $includeAll     = empty($intersect);
322                 $data           = array();
323                 ($includeAll || isset($intersect['id']))
324                 && $data['id'] = $this->id;
325                 ($includeAll || isset($intersect['createdBy']))
326                 && $data['createdBy'] = (in_array('createdBy', $expand) || $expandAll || $showIdentifiers)
327                                         && !is_null($this->createdBy)
328                         ? (!$showIdentifiers ? $this->createdBy->toArray(
329                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
330                         ) : $this->createdBy->id)
331                         : null;
332                 ($includeAll || isset($intersect['company']))
333                 && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
334                                       && !is_null($this->company)
335                         ? (!$showIdentifiers ? $this->company->toArray(
336                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
337                         ) : $this->company->id)
338                         : null;
339                 ($includeAll || isset($intersect['stock']))
340                 && $data['stock'] = (in_array('stock', $expand) || $expandAll || $showIdentifiers)
341                                     && !is_null($this->stock)
342                         ? (!$showIdentifiers ? $this->stock->toArray(
343                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
344                         ) : $this->stock->id)
345                         : null;
346                 ($includeAll || isset($intersect['soldToProfile']))
347                 && $data['soldToProfile'] = (in_array('soldToProfile', $expand) || $expandAll || $showIdentifiers)
348                                             && !is_null($this->soldToProfile)
349                         ? (!$showIdentifiers ? $this->soldToProfile->toArray(
350                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
351                         ) : $this->soldToProfile->id)
352                         : null;
353                 ($includeAll || isset($intersect['soldToCompany']))
354                 && $data['soldToCompany'] = (in_array('soldToCompany', $expand) || $expandAll || $showIdentifiers)
355                                             && !is_null($this->soldToCompany)
356                         ? (!$showIdentifiers ? $this->soldToCompany->toArray(
357                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
358                         ) : $this->soldToCompany->id)
359                         : null;
360                 ($includeAll || isset($intersect['currentBid']))
361                 && $data['currentBid'] = (in_array('currentBid', $expand) || $expandAll || $showIdentifiers)
362                                          && !is_null($this->currentBid)
363                         ? (!$showIdentifiers ? $this->currentBid->toArray(
364                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
365                         ) : $this->currentBid->id)
366                         : null;
367                 ($includeAll || isset($intersect['currentBidPrice']))
368                 && $data['currentBidPrice'] = $this->currentBidPrice;
369                 ($includeAll || isset($intersect['reservePrice']))
370                 && $data['reservePrice'] = $this->reservePrice;
371                 ($includeAll || isset($intersect['bidIncrement']))
372                 && $data['bidIncrement'] = $this->bidIncrement;
373                 ($includeAll || isset($intersect['startDate']))
374                 && $data['startDate'] = !is_null($this->startDate)
375                         ? $this->startDate->format($dateTimeFormat)
376                         : null;
377                 ($includeAll || isset($intersect['endDate']))
378                 && $data['endDate'] = !is_null($this->endDate)
379                         ? $this->endDate->format($dateTimeFormat)
380                         : null;
381                 ($includeAll || isset($intersect['numberOfBids']))
382                 && $data['numberOfBids'] = $this->numberOfBids;
383                 ($includeAll || isset($intersect['baskets']))
384                 && $data['baskets'] = (in_array('baskets', $expand) || $expandAll || $showIdentifiers)
385                                       && !is_null($this->baskets)
386                         ? $this->basketsToArray($showIdentifiers)
387                         : null;
388                 ($includeAll || isset($intersect['bids']))
389                 && $data['bids'] = (in_array('bids', $expand) || $expandAll || $showIdentifiers)
390                                    && !is_null($this->bids)
391                         ? $this->bidsToArray($showIdentifiers)
392                         : null;
393                 ($includeAll || isset($intersect['jobState']))
394                 && $data['jobState'] = $this->jobState;
395                 ($includeAll || isset($intersect['created']))
396                 && $data['created'] = !is_null($this->created)
397                         ? $this->created->format($dateTimeFormat)
398                         : null;
399                 ($includeAll || isset($intersect['updated']))
400                 && $data['updated'] = !is_null($this->updated)
401                         ? $this->updated->format($dateTimeFormat)
402                         : null;
403                 return $data;
404         }
405
406         /**
407          * Internal utility to change auction baskets collection into array.
408          * @param boolean $showIdentifiers
409          * @return array
410          */
411         protected function basketsToArray($showIdentifiers = false)
412         {
413                 $data     = array();
414                 $iterator = $this->baskets->getIterator();
415                 foreach ($iterator as $basket)
416                 {
417                         $data[] = $showIdentifiers
418                                 ? array('id' => $basket->id)
419                                 : $basket->toArray();
420                 }
421                 return $data;
422         }
423
424         /**
425          * Internal utility to change auction bids collection into array.
426          * @param boolean $showIdentifiers
427          * @return array
428          */
429         protected function bidsToArray($showIdentifiers = false)
430         {
431                 $data     = array();
432                 $iterator = $this->bids->getIterator();
433                 foreach ($iterator as $bid)
434                 {
435                         $data[] = $showIdentifiers
436                                 ? array('id' => $bid->id)
437                                 : $bid->toArray();
438                 }
439                 return $data;
440         }
441
442         /**
443          * Populate from an array.
444          * @param array $data
445          */
446         public function fromArray($data = array())
447         {
448                 isset($data['id'])
449                 && $this->id = $data['id'];
450                 isset($data['createdBy'])
451                 && $this->createdBy = $data['createdBy'];
452                 isset($data['company'])
453                 && $this->company = $data['company'];
454                 isset($data['stock'])
455                 && $this->stock = $data['stock'];
456                 isset($data['soldToProfile'])
457                 && $this->soldToProfile = $data['soldToProfile'];
458                 isset($data['soldToCompany'])
459                 && $this->soldToCompany = $data['soldToCompany'];
460                 isset($data['currentBid'])
461                 && $this->currentBid = $data['currentBid'];
462                 isset($data['currentBidPrice'])
463                 && $this->currentBidPrice = $data['currentBidPrice'];
464                 isset($data['reservePrice'])
465                 && $this->reservePrice = $data['reservePrice'];
466                 isset($data['bidIncrement'])
467                 && $this->bidIncrement = $data['bidIncrement'];
468                 isset($data['startDate'])
469                 && $this->startDate = $data['startDate'];
470                 isset($data['endDate'])
471                 && $this->endDate = $data['endDate'];
472                 isset($data['numberOfBids'])
473                 && $this->numberOfBids = $data['numberOfBids'];
474         }
475
476
477
478 }
479