click to refresh error debugging
[namibia] / module / Stock / src / Stock / Entity / Stock.php
1 <?php
2 namespace Stock\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * A stock item.
10  *
11  * @ORM\Entity
12  * @ORM\HasLifecycleCallbacks
13  * @ORM\Table(name="stock")
14  */
15 class Stock
16 {
17
18         /**
19          * Can archive records.
20          */
21         const ARCHIVE = true;
22         /**
23          * Pull Synchronization Strategy for this table.
24          */
25         const PULL_SYNCH_STRATEGY = false;
26         /**
27          * Push Synchronization Strategy for this table.
28          */
29         const PUSH_SYNCH_STRATEGY = false;
30         /**
31          * Post insert action must be called after new entity is flushed to database.
32          */
33         const HAVE_POST_INSERT = true;
34
35
36
37         /* ------------------------------------ Identification ------------------------------------ */
38         /**
39          * @ORM\Id
40          * @ORM\Column(type="integer");
41          * @ORM\GeneratedValue(strategy="AUTO")
42          */
43         protected $id;
44
45         /**
46          * @ORM\Column(type="string", length=40);
47          */
48         protected $uvi;
49
50
51         /* ------------------------------------ Ownership ------------------------------------ */
52         /**
53          * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
54          * @ORM\JoinColumn(nullable=false, name="company_id")
55          **/
56         protected $company;
57
58         /**
59          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
60          * @ORM\JoinColumn(nullable=false, name="created_by_profile_id")
61          **/
62         protected $createdBy;
63
64         /**
65          * @ORM\ManyToOne(targetEntity="\Valuation\Entity\Valuation", cascade={"all"})
66          * @ORM\JoinColumn(nullable=true, name="stock_valuation_id")
67          **/
68         protected $valuation;
69
70         /**
71          * @ORM\ManyToOne(targetEntity="\PriceGuide\Entity\PriceGuide")
72          * @ORM\JoinColumn(nullable=true, name="price_guide_id")
73          **/
74         protected $priceGuide;
75
76         /**
77          * @ORM\ManyToOne(targetEntity="\Auction\Entity\Auction")
78          * @ORM\JoinColumn(nullable=true, name="auction_id")
79          **/
80         protected $auction;
81
82
83         /* ------------------------------------ General vehicle details ------------------------------------ */
84         /**
85          * @ORM\ManyToOne(targetEntity="\Location\Entity\Region")
86          * @ORM\JoinColumn(nullable=true, name="lib_region_id")
87          **/
88         protected $region;
89
90         /**
91          * @ORM\ManyToOne(targetEntity="Type")
92          * @ORM\JoinColumn(nullable=true, name="vehicle_type_id")
93          **/
94         protected $type;
95
96         /**
97          * @ORM\Column(type="string", length=12, nullable=true, name="reference_no")
98          */
99         protected $referenceNumber;
100
101         /**
102          * @ORM\Column(type="string", length=20, nullable=true, name="stock_no")
103          */
104         protected $stockNumber;
105
106         /**
107          * @ORM\Column(type="string", length=15, nullable=true, name="registration_number")
108          */
109         protected $registrationNumber;
110
111         /**
112          * @ORM\Column(type="string", length=40, nullable=true, name="vin_number")
113          */
114         protected $vinNumber;
115
116         /**
117          * @ORM\Column(type="string", length=40, nullable=true, name="engine_number")
118          */
119         protected $engineNumber;
120
121         /**
122          * @ORM\ManyToOne(targetEntity="FuelType")
123          * @ORM\JoinColumn(nullable=true, name="vehicle_fuel_type_id")
124          **/
125         protected $fuelType;
126
127         /**
128          * @ORM\ManyToOne(targetEntity="TransmissionType")
129          * @ORM\JoinColumn(nullable=true, name="vehicle_transmission_type_id")
130          **/
131         protected $transmissionType;
132
133         /**
134          * @ORM\ManyToOne(targetEntity="ExteriorColour")
135          * @ORM\JoinColumn(nullable=true, name="vehicle_exterior_colour_id")
136          **/
137         protected $exteriorColour;
138
139         /**
140          * @ORM\ManyToOne(targetEntity="InteriorColour")
141          * @ORM\JoinColumn(nullable=true, name="vehicle_interior_colour_id")
142          **/
143         protected $interiorColour;
144
145         /**
146          * @ORM\ManyToOne(targetEntity="Condition")
147          * @ORM\JoinColumn(nullable=true, name="vehicle_condition_id")
148          **/
149         protected $condition;
150
151         /**
152          * @ORM\ManyToOne(targetEntity="Year")
153          * @ORM\JoinColumn(nullable=true, name="vehicle_year_id")
154          **/
155         protected $vehicleYear;
156
157         /**
158          * @ORM\Column(type="integer", nullable=true, options={"unsigned"=true})
159          */
160         protected $km;
161
162         /**
163          * @ORM\ManyToOne(targetEntity="Upholstery")
164          * @ORM\JoinColumn(nullable=true, name="vehicle_upholstery_id")
165          **/
166         protected $upholstery;
167
168         /**
169          * @ORM\ManyToOne(targetEntity="Paper")
170          * @ORM\JoinColumn(nullable=true, name="vehicle_paper_id")
171          **/
172         protected $papers;
173
174         /**
175          * @ORM\Column(type="text", name="papers_notes")
176          */
177         protected $papersNotes = '';
178
179         /**
180          * @ORM\ManyToOne(targetEntity="Natis")
181          * @ORM\JoinColumn(nullable=true, name="vehicle_natis_id")
182          **/
183         protected $natis;
184
185         /**
186          * @ORM\Column(nullable=true, type="boolean", name="spare_keys");
187          */
188         protected $spareKeys;
189
190         /**
191          * @ORM\ManyToOne(targetEntity="FullServiceHistory")
192          * @ORM\JoinColumn(nullable=true, name="vehicle_fsh_id")
193          **/
194         protected $fullServiceHistory;
195
196         /**
197          * @ORM\Column(type="text", name="fsh_notes")
198          */
199         protected $fshNotes = '';
200
201
202         /* ------------------------------------ Pricing ------------------------------------ */
203         /**
204          * @ORM\Column(type="datetime", name="trade_price_date", nullable=true);
205          */
206         protected $tradePriceDate;
207
208         /**
209          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="trade_price", options={"unsigned"=true});
210          */
211         protected $tradePrice = 0.0;
212
213         /**
214          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="retail_price", options={"unsigned"=true});
215          */
216         protected $retailPrice = 0.0;
217
218         /**
219          * @ORM\Column(type="datetime", name="old_trade_price_date", nullable=true);
220          */
221         protected $oldTradePriceDate;
222
223         /**
224          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="old_trade_price", options={"unsigned"=true});
225          */
226         protected $oldTradePrice = 0.0;
227
228         /**
229          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="old_retail_price", options={"unsigned"=true});
230          */
231         protected $oldRetailPrice = 0.0;
232
233         /**
234          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="retail_amount_required", options={"unsigned"=true});
235          */
236         protected $retailAmountRequired = 0.0;
237
238         /**
239          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="purchase_price", options={"unsigned"=true});
240          */
241         protected $purchasePrice = 0.0;
242
243         /**
244          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="sale_price", options={"unsigned"=true});
245          */
246         protected $salePrice = 0.0;
247
248
249     /**
250      * @ORM\Column(type="datetime", name="list_price_date", nullable=true);
251      */
252     protected $listPriceDate;
253
254     /**
255      * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="list_price", options={"unsigned"=true});
256      */
257     protected $listPrice = 0.0;
258
259     /**
260      * @ORM\Column(type="datetime", name="old_list_price_date", nullable=true);
261      */
262     protected $oldListPriceDate;
263
264     /**
265      * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="old_list_price", options={"unsigned"=true});
266      */
267     protected $oldListPrice = 0.0;
268
269
270         /* ------------------------------------ Vehicle photos ------------------------------------ */
271         /**
272          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
273          * @ORM\JoinColumn(nullable=true, name="main_lib_photo_id")
274          **/
275         protected $mainImage;
276
277         /**
278          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
279          * @ORM\JoinColumn(nullable=true, name="front_lib_photo_id")
280          **/
281         protected $frontImage;
282
283         /**
284          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
285          * @ORM\JoinColumn(nullable=true, name="right_lib_photo_id")
286          **/
287         protected $rightImage;
288
289         /**
290          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
291          * @ORM\JoinColumn(nullable=true, name="left_lib_photo_id")
292          **/
293         protected $leftImage;
294
295         /**
296          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
297          * @ORM\JoinColumn(nullable=true, name="back_lib_photo_id")
298          **/
299         protected $backImage;
300
301         /**
302          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
303          * @ORM\JoinColumn(nullable=true, name="interior_lib_photo_id")
304          **/
305         protected $interiorImage;
306
307         /**
308          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
309          * @ORM\JoinColumn(nullable=true, name="engine_lib_photo_id")
310          **/
311         protected $engineImage;
312
313         /**
314          * @ORM\ManyToOne(targetEntity="\Utility\Entity\Image")
315          * @ORM\JoinColumn(nullable=true, name="natis_lib_photo_id")
316          **/
317         protected $natisImage;
318
319
320         /* ------------------------------------ Accessories ------------------------------------ */
321         /**
322          * @ORM\OneToMany(targetEntity="StockAccessory", mappedBy="stock", cascade={"all"}, fetch="EXTRA_LAZY")
323          **/
324         protected $accessories;
325
326         /**
327          * @ORM\Column(type="text", name="accessory_notes")
328          */
329         protected $accessoryNotes = '';
330
331
332         /* ------------------------------------ Damages ------------------------------------ */
333         /**
334          * @ORM\OneToMany(targetEntity="StockDamage", mappedBy="stock", cascade={"all"}, fetch="EXTRA_LAZY")
335          **/
336         protected $damages;
337
338         /**
339          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="damage_total", options={"unsigned"=true});
340          */
341         protected $damageTotal = 0.0;
342
343         /**
344          * @ORM\Column(type="text", name="damage_notes")
345          */
346         protected $damageNotes = '';
347
348         /**
349          * @ORM\Column(type="boolean", name="previous_repairs_noted");
350          */
351         protected $previousRepairsNoted = false;
352
353         /**
354          * @ORM\Column(type="text", name="previous_repairs_notes")
355          */
356         protected $previousRepairsNotes = '';
357         
358         /* ------------------------------------ Cover Received ------------------------------------ */
359         
360         /**
361          * @ORM\Column(type="text", name="cover_received")
362          */
363         protected $coverReceived = '';
364
365
366         /* ------------------------------------ Price Guide ------------------------------------ */
367         /**
368          * @ORM\Column(type="integer", nullable=false, name="number_of_offers");
369          */
370         protected $numberOfOffers = 0;
371
372         /**
373          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="highest_offer", options={"unsigned"=true});
374          */
375         protected $highestOffer = 0.0;
376
377         /**
378          * @ORM\Column(type="datetime", nullable=true, name="loaded_on_priceguide");
379          */
380         protected $loadedOnPriceGuide;
381
382
383         /* ------------------------------------ Auction ------------------------------------ */
384         /**
385          * @ORM\Column(type="integer", nullable=false, name="times_listed");
386          */
387         protected $timesListed = 0;
388
389         /**
390          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="highest_bid", options={"unsigned"=true});
391          */
392         protected $highestBid = 0.0;
393
394
395         /* ------------------------------------ Adherence ------------------------------------ */
396         /**
397          * @ORM\OneToMany(targetEntity="\Adherence\Entity\AdherenceStock", mappedBy="stock", cascade={"all"}, fetch="EXTRA_LAZY")
398          **/
399         protected $adherence;
400
401
402
403         /* ------------------------------------ Tracking ------------------------------------ */
404
405         /**
406          * @ORM\Column(type="datetime", nullable=true);
407          */
408         protected $reset;
409
410         /**
411          * @ORM\Column(type="string", length=25, nullable=true, name="previous_status");
412          */
413         protected $previousState;
414
415         /**
416          * @ORM\Column(type="string", length=25, nullable=true, name="status");
417          */
418         protected $jobState;
419
420         /**
421          * @ORM\Column(type="datetime");
422          */
423         protected $created;
424
425         /**
426          * @ORM\Column(type="datetime", nullable=true);
427          */
428         protected $updated;
429
430         /**
431          * @ORM\Column(type="boolean");
432          */
433         protected $archived = false;
434
435
436
437         /**
438          * Initialize collections.
439          */
440         public function __construct()
441         {
442                 $this->accessories = new \Doctrine\Common\Collections\ArrayCollection();
443                 $this->damages = new \Doctrine\Common\Collections\ArrayCollection();
444                 $this->adherence = new \Doctrine\Common\Collections\ArrayCollection();
445         }
446
447         /**
448          * Add a new Accessory to this Stock Item.
449          * @param Accessory $accessory
450          * @return \Stock\Entity\Stock
451          */
452         public function addAccessory(Accessory $accessory)
453         {
454                 $this->accessories[] = $accessory;
455                 return $this;
456         }
457
458         /**
459          * Add a new Damage to this Stock Item.
460          * @param Damage $damage
461          * @return \Stock\Entity\Stock
462          */
463         public function addDamage(Damage $damage)
464         {
465                 $this->damages[] = $damage;
466                 return $this;
467         }
468
469
470
471         /**
472          * Magic getter to expose protected properties.
473          *
474          * @param string $property
475          * @return mixed
476          */
477         public function __get($property)
478         {
479                 return $this->$property;
480         }
481
482         /**
483          * Magic setter to save protected properties.
484          *
485          * @param string $property
486          * @param mixed $value
487          */
488         public function __set($property, $value)
489         {
490                 $this->$property = $value;
491         }
492
493         /**
494          * @ORM\PrePersist
495          */
496         public function setCreateTime()
497         {
498                 if (is_null($this->uvi))
499                 {
500                         list($usec, $sec) = explode(" ", microtime());
501                         $this->uvi = $sec . substr($usec, 2, 3);
502                 }
503                 if (\Utility\Registry::get('IsDeviceApiCall', false))
504                 {
505                         // Leave company and createdBy for controller to set.
506                 }
507         else if (\Utility\Registry::get('IsBulkImport', false))
508         {
509             // Leave company and createdBy for controller to set.
510         }
511                 else if (\Utility\Registry::isAuthenticated())
512                 {
513                         $this->company   = \Utility\Registry::resolveCompanyContext($this->company);
514                         $this->createdBy = \Utility\Registry::resolveProfileContext($this->createdBy);
515                 }
516                 else
517                 {
518                         $em = \Utility\Registry::getEntityManager();
519 //                      $companyId = IS_STAGE_ENV ? 1953 : 2283;
520                         $companyId = IS_STAGE_ENV ? 1 : 1;
521                         if (IS_DEV_ENV)
522                         {
523                                 $companyId = 1;
524                         }
525                         $this->company   = $em->getReference('\Company\Entity\Company', $companyId);
526                         $this->createdBy = $em->getReference('\User\Entity\Profile', 1);
527                 }
528                 $this->created = new \DateTime("now");
529                 if (0.0 != $this->tradePrice && is_null($this->tradePriceDate))
530                 {
531                         $this->tradePriceDate = new \DateTime('now');
532                 }
533                 if (0.0 != $this->oldTradePrice && is_null($this->oldTradePriceDate))
534                 {
535                         $this->oldTradePriceDate = new \DateTime('now');
536                 }
537
538         if (0.0 != $this->listPrice && is_null($this->listPriceDate))
539         {
540             $this->listPriceDate = new \DateTime('now');
541         }
542         if (0.0 != $this->oldListPrice && is_null($this->oldListPriceDate))
543         {
544             $this->oldListPriceDate = new \DateTime('now');
545         }
546         }
547
548         /**
549          * @ORM\PreUpdate
550          */
551         public function setUpdateTime()
552         {
553                 $this->updated = new \DateTime("now");
554                 if (0.0 != $this->tradePrice && is_null($this->tradePriceDate))
555                 {
556                         $this->tradePriceDate = new \DateTime('now');
557                 }
558                 if (0.0 != $this->oldTradePrice && is_null($this->oldTradePriceDate))
559                 {
560                         $this->oldTradePriceDate = new \DateTime('now');
561                 }
562
563         if (0.0 != $this->listPrice && is_null($this->listPriceDate))
564         {
565             $this->listPriceDate = new \DateTime('now');
566         }
567         if (0.0 != $this->oldListPrice && is_null($this->oldListPriceDate))
568         {
569             $this->oldListPriceDate = new \DateTime('now');
570         }
571                 /* if ('' == $this->jobState
572                         && is_null($this->loadedOnPriceGuide))
573                 {
574                         $this->loadedOnPriceGuide = new \DateTime("now");
575                 } */
576         }
577
578         /**
579          * Automatically called from DataBin if HAVE_POST_INSERT constant is set on entity.
580          * @return boolean
581          */
582         public function postInsert()
583         {
584                 if (is_null($this->id) || !is_numeric($this->id))
585                 {
586                         return false;
587                 }
588                 $this->referenceNumber = 'S' . str_pad($this->id, 7, '0', STR_PAD_LEFT);
589                 return true;
590         }
591
592         /**
593          * Convert the object to an array.
594          * @param array $expand
595          * @param array $intersect
596          * @param boolean $showIdentifiers
597          * @param integer $expandAll
598          * @return array
599          */
600         public function toArray(
601                         array $expand = array(), array $intersect = array(),
602                         $showIdentifiers = false, $expandAll = 0
603                         )
604         {
605                 $intersect = array_flip($intersect);
606                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
607                 $includeAll = empty($intersect);
608                 $data = array();
609                 ($includeAll || isset($intersect['id']))
610                         && $data['id'] = $this->id;
611                 ($includeAll || isset($intersect['uvi']))
612                         && $data['uvi'] = $this->uvi;
613                 ($includeAll || isset($intersect['company']))
614                         && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
615                                                                         && !is_null($this->company)
616                                 ? (!$showIdentifiers || in_array('company', $expand) ? $this->company->toArray(
617                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
618                                                 ) : $this->company->id)
619                                 : null;
620                 ($includeAll || isset($intersect['auction']))
621                         && $data['auction'] = (in_array('auction', $expand) || $expandAll || $showIdentifiers)
622                                                                         && !is_null($this->auction)
623                                 ? (!$showIdentifiers || in_array('auction', $expand) ? $this->auction->toArray(
624                                                 array(), array('id','company','soldToCompany','stock','endDate','reservePrice','jobState'), true, ($expandAll - 1)
625                                                 ) : $this->auction->id)
626                                 : null;
627                 if (isset($data['auction']))
628                 {
629                         $date = new \DateTime($data['auction']['endDate']);
630                         $data['auction']['Within30DaysOfEnd'] = time() < ($date->getTimestamp() + 2592000);
631                 }
632                 ($includeAll || isset($intersect['createdBy']))
633                         && $data['createdBy'] = (in_array('createdBy', $expand) || $expandAll || $showIdentifiers)
634                                                                         && !is_null($this->createdBy)
635                                 ? (!$showIdentifiers || in_array('createdBy', $expand) ? $this->createdBy->toArray(
636                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
637                                                 ) : $this->createdBy->id)
638                                 : null;
639                 ($includeAll || isset($intersect['valuation']))
640                         && $data['valuation'] = (in_array('valuation_', $expand) || $expandAll || $showIdentifiers)
641                                                                         && !is_null($this->valuation)
642                                 ? (!$showIdentifiers || in_array('valuation_', $expand) ? $this->valuation->toArray(
643                                                 /* $expand, $intersect, $showIdentifiers, ($expandAll - 1) */
644                                                 ) : $this->valuation->id)
645                                 : null;
646                 ($includeAll || isset($intersect['region']))
647                         && $data['region'] = (in_array('region', $expand) || $expandAll || $showIdentifiers)
648                                                                         && !is_null($this->region)
649                                 ? (!$showIdentifiers || in_array('region', $expand) ? $this->region->toArray(
650                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
651                                                 ) : $this->region->id)
652                                 : null;
653                 (!\Utility\Registry::checkOnce('Stock.IgnorePriceGuide', false) && ($includeAll || isset($intersect['priceGuide'])))
654                         && $data['priceGuide'] = (in_array('priceGuide', $expand) || $expandAll || $showIdentifiers)
655                                                                         && !is_null($this->priceGuide)
656                                 ? (!$showIdentifiers || in_array('priceGuide', $expand) ? $this->priceGuide->toArray(
657                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
658                                                 ) : $this->priceGuide->id)
659                                 : null;
660                 if (2 == $showIdentifiers)
661                 {
662                         $data['mmCode']   = $this->type->mmCode;
663                         $data['category'] = $this->type->category->id;
664                         $data['make']     = $this->type->model->make->id;
665                         $data['model']    = $this->type->model->id;
666                 }
667                 ($includeAll || isset($intersect['type']))
668                         && $data['type'] = (in_array('type', $expand) || $expandAll || $showIdentifiers)
669                                                                         && !is_null($this->type)
670                                 ? (!$showIdentifiers || in_array('type', $expand) ? $this->type->toArray(
671                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
672                                                 ) : $this->type->id)
673                                 : null;
674                 ($includeAll || isset($intersect['referenceNumber']))
675                         && $data['referenceNumber'] = $this->referenceNumber;
676                 ($includeAll || isset($intersect['stockNumber']))
677                         && $data['stockNumber'] = $this->stockNumber;
678                 ($includeAll || isset($intersect['registrationNumber']))
679                         && $data['registrationNumber'] = $this->registrationNumber;
680                 ($includeAll || isset($intersect['vinNumber']))
681                         && $data['vinNumber'] = $this->vinNumber;
682                 ($includeAll || isset($intersect['engineNumber']))
683                         && $data['engineNumber'] = $this->engineNumber;
684                 ($includeAll || isset($intersect['fuelType']))
685                         && $data['fuelType'] = (in_array('fuelType', $expand) || $expandAll || $showIdentifiers)
686                                                                         && !is_null($this->fuelType)
687                                 ? (!$showIdentifiers || in_array('fuelType', $expand) ? $this->fuelType->toArray(
688                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
689                                                 ) : $this->fuelType->id)
690                                 : null;
691                 ($includeAll || isset($intersect['transmissionType']))
692                         && $data['transmissionType'] = (in_array('transmissionType', $expand) || $expandAll || $showIdentifiers)
693                                                                         && !is_null($this->transmissionType)
694                                 ? (!$showIdentifiers || in_array('transmissionType', $expand) ? $this->transmissionType->toArray(
695                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
696                                                 ) : $this->transmissionType->id)
697                                 : null;
698                 ($includeAll || isset($intersect['exteriorColour']))
699                         && $data['exteriorColour'] = (in_array('exteriorColour', $expand) || $expandAll || $showIdentifiers)
700                                                                         && !is_null($this->exteriorColour)
701                                 ? (!$showIdentifiers || in_array('exteriorColour', $expand) ? $this->exteriorColour->toArray(
702                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
703                                                 ) : $this->exteriorColour->id)
704                                 : null;
705                 ($includeAll || isset($intersect['interiorColour']))
706                         && $data['interiorColour'] = (in_array('interiorColour', $expand) || $expandAll || $showIdentifiers)
707                                                                         && !is_null($this->interiorColour)
708                                 ? (!$showIdentifiers || in_array('interiorColour', $expand) ? $this->interiorColour->toArray(
709                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
710                                                 ) : $this->interiorColour->id)
711                                 : null;
712                 ($includeAll || isset($intersect['condition']))
713                         && $data['condition'] = (in_array('condition', $expand) || $expandAll || $showIdentifiers)
714                                                                                 && !is_null($this->condition)
715                                 ? (!$showIdentifiers || in_array('condition', $expand) ? $this->condition->toArray(
716                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
717                                                 ) : $this->condition->id)
718                                 : null;
719                 ($includeAll || isset($intersect['vehicleYear']))
720                         && $data['vehicleYear'] = (in_array('vehicleYear', $expand) || $expandAll || $showIdentifiers)
721                                                                                 && !is_null($this->vehicleYear)
722                                 ? (!$showIdentifiers || in_array('vehicleYear', $expand) ? $this->vehicleYear->toArray(
723                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
724                                                 ) : $this->vehicleYear->id)
725                                 : null;
726                 ($includeAll || isset($intersect['km']))
727                         && $data['km'] = $this->km;
728                 ($includeAll || isset($intersect['upholstery']))
729                         && $data['upholstery'] = (in_array('upholstery', $expand) || $expandAll || $showIdentifiers)
730                                                                                 && !is_null($this->upholstery)
731                                 ? (!$showIdentifiers || in_array('upholstery', $expand) ? $this->upholstery->toArray(
732                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
733                                                 ) : $this->upholstery->id)
734                                 : null;
735                 ($includeAll || isset($intersect['papers']))
736                         && $data['papers'] = (in_array('papers', $expand) || $expandAll || $showIdentifiers)
737                                                                                 && !is_null($this->papers)
738                                 ? (!$showIdentifiers || in_array('papers', $expand) ? $this->papers->toArray(
739                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
740                                                 ) : $this->papers->id)
741                                 : null;
742                 ($includeAll || isset($intersect['papersNotes']))
743                         && $data['papersNotes'] = $this->papersNotes;
744                 ($includeAll || isset($intersect['natis']))
745                         && $data['natis'] = (in_array('natis', $expand) || $expandAll || $showIdentifiers)
746                                                                                 && !is_null($this->natis)
747                                 ? (!$showIdentifiers || in_array('natis', $expand) ? $this->natis->toArray(
748                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
749                                                 ) : $this->natis->id)
750                                 : null;
751                 ($includeAll || isset($intersect['spareKeys']))
752                         && $data['spareKeys'] = $this->spareKeys;
753                 ($includeAll || isset($intersect['fullServiceHistory']))
754                         && $data['fullServiceHistory'] = (in_array('fullServiceHistory', $expand) || $expandAll || $showIdentifiers)
755                                                                                                 && !is_null($this->fullServiceHistory)
756                                 ? (!$showIdentifiers || in_array('fullServiceHistory', $expand) ? $this->fullServiceHistory->toArray(
757                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
758                                                 ) : $this->fullServiceHistory->id)
759                                 : null;
760                 ($includeAll || isset($intersect['fshNotes']))
761                         && $data['fshNotes'] = $this->fshNotes;
762                 ($includeAll || isset($intersect['tradePriceDate']))
763                         && $data['tradePriceDate'] = !is_null($this->tradePriceDate)
764                                 ? $this->tradePriceDate->format($dateTimeFormat)
765                                 : null;
766         ($includeAll || isset($intersect['listPriceDate']))
767             && $data['listPriceDate'] = !is_null($this->listPriceDate)
768                 ? $this->listPriceDate->format($dateTimeFormat)
769                 : null;
770                 ($includeAll || isset($intersect['tradePrice']))
771                         && $data['tradePrice'] = $this->tradePrice;
772                 ($includeAll || isset($intersect['retailPrice']))
773                         && $data['retailPrice'] = $this->retailPrice;
774         ($includeAll || isset($intersect['listPrice']))
775             && $data['listPrice'] = $this->listPrice;
776                 ($includeAll || isset($intersect['oldTradePriceDate']))
777                         && $data['oldTradePriceDate'] = !is_null($this->oldTradePriceDate)
778                                 ? $this->oldTradePriceDate->format($dateTimeFormat)
779                                 : null;
780         ($includeAll || isset($intersect['oldListPriceDate']))
781             && $data['oldListPriceDate'] = !is_null($this->oldListPriceDate)
782                 ? $this->oldListPriceDate->format($dateTimeFormat)
783                 : null;
784                 ($includeAll || isset($intersect['oldTradePrice']))
785                         && $data['oldTradePrice'] = $this->oldTradePrice;
786                 ($includeAll || isset($intersect['oldRetailPrice']))
787                         && $data['oldRetailPrice'] = $this->oldRetailPrice;
788         ($includeAll || isset($intersect['oldListPrice']))
789             && $data['oldListPrice'] = $this->oldListPrice;
790                 ($includeAll || isset($intersect['retailAmountRequired']))
791                         && $data['retailAmountRequired'] = $this->retailAmountRequired;
792                 ($includeAll || isset($intersect['mainImage']))
793                         && $data['mainImage'] = (in_array('mainImage', $expand) || $expandAll || $showIdentifiers)
794                                                                                 && !is_null($this->mainImage)
795                                 ? (!$showIdentifiers || in_array('mainImage', $expand) ? $this->mainImage->toArray(
796                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
797                                                 ) : $this->mainImage->id)
798                                 : null;
799                 ($includeAll || isset($intersect['frontImage']))
800                         && $data['frontImage'] = (in_array('frontImage', $expand) || $expandAll || $showIdentifiers)
801                                                                                 && !is_null($this->frontImage)
802                                 ? (!$showIdentifiers || in_array('frontImage', $expand) ? $this->frontImage->toArray(
803                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
804                                                 ) : $this->frontImage->id)
805                                 : null;
806                 ($includeAll || isset($intersect['rightImage']))
807                         && $data['rightImage'] = (in_array('rightImage', $expand) || $expandAll || $showIdentifiers)
808                                                                                 && !is_null($this->rightImage)
809                                 ? (!$showIdentifiers || in_array('rightImage', $expand) ? $this->rightImage->toArray(
810                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
811                                                 ) : $this->rightImage->id)
812                                 : null;
813                 ($includeAll || isset($intersect['leftImage']))
814                         && $data['leftImage'] = (in_array('leftImage', $expand) || $expandAll || $showIdentifiers)
815                                                                                 && !is_null($this->leftImage)
816                                 ? (!$showIdentifiers || in_array('leftImage', $expand) ? $this->leftImage->toArray(
817                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
818                                                 ) : $this->leftImage->id)
819                                 : null;
820                 ($includeAll || isset($intersect['backImage']))
821                         && $data['backImage'] = (in_array('backImage', $expand) || $expandAll || $showIdentifiers)
822                                                                                 && !is_null($this->backImage)
823                                 ? (!$showIdentifiers || in_array('backImage', $expand) ? $this->backImage->toArray(
824                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
825                                                 ) : $this->backImage->id)
826                                 : null;
827                 ($includeAll || isset($intersect['interiorImage']))
828                         && $data['interiorImage'] = (in_array('interiorImage', $expand) || $expandAll || $showIdentifiers)
829                                                                                 && !is_null($this->interiorImage)
830                                 ? (!$showIdentifiers || in_array('interiorImage', $expand) ? $this->interiorImage->toArray(
831                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
832                                                 ) : $this->interiorImage->id)
833                                 : null;
834                 ($includeAll || isset($intersect['engineImage']))
835                         && $data['engineImage'] = (in_array('engineImage', $expand) || $expandAll || $showIdentifiers)
836                                                                                 && !is_null($this->engineImage)
837                                 ? (!$showIdentifiers || in_array('engineImage', $expand) ? $this->engineImage->toArray(
838                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
839                                                 ) : $this->engineImage->id)
840                                 : null;
841                 ($includeAll || isset($intersect['natisImage']))
842                         && $data['natisImage'] = (in_array('natisImage', $expand) || $expandAll || $showIdentifiers)
843                                                                                 && !is_null($this->natisImage)
844                                 ? (!$showIdentifiers || in_array('natisImage', $expand) ? $this->natisImage->toArray(
845                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
846                                                 ) : $this->natisImage->id)
847                                 : null;
848                 ($includeAll || isset($intersect['accessories']))
849                         && $data['accessories'] = (in_array('accessories', $expand) || $expandAll || $showIdentifiers)
850                                                                                 && !is_null($this->accessories)
851                                 ? $this->accessoriesToArray($showIdentifiers)
852                                 : null;
853                 ($includeAll || isset($intersect['accessoryNotes']))
854                         && $data['accessoryNotes'] = $this->accessoryNotes;
855                 ($includeAll || isset($intersect['damages']))
856                         && $data['damages'] = (in_array('damages', $expand) || $expandAll || $showIdentifiers)
857                                                                                 && !is_null($this->damages)
858                                 ? $this->damagesToArray($showIdentifiers)
859                                 : null;
860                 ($includeAll || isset($intersect['damageTotal']))
861                         && $data['damageTotal'] = $this->damageTotal;
862                 ($includeAll || isset($intersect['damageNotes']))
863                         && $data['damageNotes'] = $this->damageNotes;
864                 ($includeAll || isset($intersect['previousRepairsNoted']))
865                         && $data['previousRepairsNoted'] = $this->previousRepairsNoted;
866                 ($includeAll || isset($intersect['previousRepairsNotes']))
867                         && $data['previousRepairsNotes'] = $this->previousRepairsNotes;
868                 ($includeAll || isset($intersect['coverReceived']))
869                         && $data['coverReceived'] = $this->coverReceived;
870                 ($includeAll || isset($intersect['numberOfOffers']))
871                         && $data['numberOfOffers'] = $this->numberOfOffers;
872                 ($includeAll || isset($intersect['highestOffer']))
873                         && $data['highestOffer'] = $this->highestOffer;
874                 ($includeAll || isset($intersect['loadedOnPriceGuide']))
875                         && $data['loadedOnPriceGuide'] = !is_null($this->loadedOnPriceGuide)
876                                 ? $this->loadedOnPriceGuide->format($dateTimeFormat)
877                                 : null;
878                 ($includeAll || isset($intersect['timesListed']))
879                         && $data['timesListed'] = $this->timesListed;
880                 ($includeAll || isset($intersect['highestBid']))
881                         && $data['highestBid'] = $this->highestBid;
882                 ($includeAll || isset($intersect['jobState']))
883                         && $data['jobState'] = $this->jobState;
884                 ($includeAll || isset($intersect['created']))
885                         && $data['created'] = !is_null($this->created)
886                                 ? $this->created->format($dateTimeFormat)
887                                 : null;
888                 ($includeAll || isset($intersect['updated']))
889                         && $data['updated'] = !is_null($this->updated)
890                                 ? $this->updated->format($dateTimeFormat)
891                                 : null;
892                 ($includeAll || isset($intersect['reset']))
893                         && $data['reset'] = !is_null($this->reset)
894                                 ? $this->reset->format($dateTimeFormat)
895                                 : null;
896                 return $data;
897         }
898
899         /**
900          * Internal utility to change accessories collection into array.
901          * @param boolean $showIdentifiers
902          * @return array
903          */
904         public function accessoriesToArray($showIdentifiers = false)
905         {
906                 $data = array();
907                 $iterator = $this->accessories->getIterator();
908                 if (2 == $showIdentifiers)
909                 {
910                         foreach ($iterator as $accessory)
911                         {
912                                 $data[] = $accessory->accessory->id;
913                         }
914                 }
915                 else
916                 {
917                         foreach ($iterator as $accessory)
918                         {
919                                 $data[] = $showIdentifiers
920                                         ? array('id' => $accessory->accessory->id)
921                                         : $accessory->toArray();
922                         }
923                 }
924                 return $data;
925         }
926
927         /**
928          * Internal utility to change damages collection into array.
929          * @param boolean $showIdentifiers
930          * @return array
931          */
932         public function damagesToArray($showIdentifiers = false)
933         {
934                 $data = array();
935                 $iterator = $this->damages->getIterator();
936                 foreach ($iterator as $damage)
937                 {
938                         $data[] = $damage->toArray(array(), array(), $showIdentifiers);
939                 }
940                 return $data;
941         }
942
943         /**
944          * Populate from an array.
945          * @param array $data
946          */
947         public function fromArray($data = array())
948         {
949                 isset($data['id'])
950                         && $this->id = $data['id'];
951                 isset($data['uvi'])
952                         && $this->uvi = $data['uvi'];
953                 isset($data['company'])
954                         && $this->company = $data['company'];
955                 isset($data['createdBy'])
956                         && $this->createdBy = $data['createdBy'];
957                 isset($data['region'])
958                         && $this->region = $data['region'];
959                 isset($data['type'])
960                         && $this->type = $data['type'];
961                 isset($data['stockNumber'])
962                         && $this->stockNumber = $data['stockNumber'];
963                 isset($data['registrationNumber'])
964                         && $this->registrationNumber = $data['registrationNumber'];
965                 isset($data['vinNumber'])
966                         && $this->vinNumber = $data['vinNumber'];
967                 isset($data['engineNumber'])
968                         && $this->engineNumber = $data['engineNumber'];
969                 isset($data['fuelType'])
970                         && $this->fuelType = $data['fuelType'];
971                 isset($data['transmissionType'])
972                         && $this->transmissionType = $data['transmissionType'];
973                 isset($data['exteriorColour'])
974                         && $this->exteriorColour = $data['exteriorColour'];
975                 isset($data['interiorColour'])
976                         && $this->interiorColour = $data['interiorColour'];
977                 isset($data['condition'])
978                         && $this->condition = $data['condition'];
979                 isset($data['vehicleYear'])
980                         && $this->vehicleYear = $data['vehicleYear'];
981                 isset($data['km'])
982                         && $this->km = $data['km'];
983                 isset($data['upholstery'])
984                         && $this->upholstery = $data['upholstery'];
985                 isset($data['papers'])
986                         && $this->papers = $data['papers'];
987                 isset($data['papersNotes'])
988                         && $this->papersNotes = $data['papersNotes'];
989                 isset($data['natis'])
990                         && $this->natis = $data['natis'];
991                 isset($data['spareKeys'])
992                         && $this->spareKeys = $data['spareKeys'];
993                 isset($data['fullServiceHistory'])
994                         && $this->fullServiceHistory = $data['fullServiceHistory'];
995                 isset($data['fshNotes'])
996                         && $this->fshNotes = $data['fshNotes'];
997                 isset($data['tradePrice'])
998                         && $this->tradePrice = $data['tradePrice'];
999                 isset($data['retailPrice'])
1000                         && $this->retailPrice = $data['retailPrice'];
1001         isset($data['listPrice'])
1002             && $this->listPrice = $data['listPrice'];
1003                 isset($data['oldTradePrice'])
1004                         && $this->oldTradePrice = $data['oldTradePrice'];
1005                 isset($data['oldRetailPrice'])
1006                         && $this->oldRetailPrice = $data['oldRetailPrice'];
1007         isset($data['oldListPrice'])
1008             && $this->oldListPrice = $data['oldListPrice'];
1009                 isset($data['retailAmountRequired'])
1010                         && $this->retailAmountRequired = $data['retailAmountRequired'];
1011                 isset($data['mainImage'])
1012                         && $this->mainImage = $data['mainImage'];
1013                 isset($data['frontImage'])
1014                         && $this->frontImage = $data['frontImage'];
1015                 isset($data['rightImage'])
1016                         && $this->rightImage = $data['rightImage'];
1017                 isset($data['leftImage'])
1018                         && $this->leftImage = $data['leftImage'];
1019                 isset($data['backImage'])
1020                         && $this->backImage = $data['backImage'];
1021                 isset($data['interiorImage'])
1022                         && $this->interiorImage = $data['interiorImage'];
1023                 isset($data['engineImage'])
1024                         && $this->engineImage = $data['engineImage'];
1025                 isset($data['natisImage'])
1026                         && $this->natisImage = $data['natisImage'];
1027                 isset($data['accessoryNotes'])
1028                         && $this->accessoryNotes = $data['accessoryNotes'];
1029                 isset($data['damageTotal'])
1030                         && $this->damageTotal = $data['damageTotal'];
1031                 isset($data['damageNotes'])
1032                         && $this->damageNotes = $data['damageNotes'];
1033                 isset($data['previousRepairsNoted'])
1034                         && $this->previousRepairsNoted = $data['previousRepairsNoted'];
1035                 isset($data['previousRepairsNotes'])
1036                         && $this->previousRepairsNotes = $data['previousRepairsNotes'];
1037                 
1038                 isset($data['coverReceived'])
1039                         && $this->coverReceived = $data['coverReceived'];
1040                 
1041                 isset($data['numberOfOffers'])
1042                         && $this->numberOfOffers = $data['numberOfOffers'];
1043                 isset($data['highestOffer'])
1044                         && $this->highestOffer = $data['highestOffer'];
1045                 isset($data['loadedOnPriceGuide'])
1046                         && $this->loadedOnPriceGuide = $data['loadedOnPriceGuide'];
1047         isset($data['jobState'])
1048             && $this->jobState = $data['jobState'];
1049                 if (isset($data['accessories']))
1050                 {
1051                         $em = \Utility\Registry::getEntityManager();
1052                         $newAccs = array();
1053                         foreach ($data['accessories'] as $acc)
1054                         {
1055                                 $newAccs[$acc['id']] = $acc['id'];
1056                         }
1057                         $currentAccs = array();
1058                         $iterator = $this->accessories->getIterator();
1059                         foreach ($iterator as $acc)
1060                         {
1061                                 $currentAccs[$acc->accessory->id] = $acc->accessory->id;
1062                                 !isset($newAccs[$acc->accessory->id])
1063                                         && $em->remove($acc);
1064                         }
1065                         foreach ($data['accessories'] as $acc)
1066                         {
1067                                 if (!isset($currentAccs[$acc['id']]))
1068                                 {
1069                                         $stockAcc = new \Stock\Entity\StockAccessory();
1070                                         $stockAcc->accessory = $em->getReference('\Stock\Entity\Accessory', $acc['id']);
1071                                         $stockAcc->stock = $this;
1072                                         $this->accessories->add($stockAcc);
1073                                 }
1074                         }
1075                 }
1076                 if (isset($data['damages']))
1077                 {
1078                         $em = \Utility\Registry::getEntityManager();
1079                         $newDmgs = array();
1080                         $total = 0.0;
1081                         foreach ($data['damages'] as $dmg)
1082                         {
1083                                 $total += $dmg['amount'];
1084                                 $newDmgs[$dmg['id']] = $dmg['amount'];
1085                         }
1086                         $this->damageTotal = $total;
1087                         $currentDmgs = array();
1088                         $iterator = $this->damages->getIterator();
1089                         foreach ($iterator as $dmg)
1090                         {
1091                                 $currentDmgs[$dmg->damage->id] = $dmg->damage->id;
1092                                 if (!isset($newDmgs[$dmg->damage->id]))
1093                                 {
1094                                         $em->remove($dmg);
1095                                 }
1096                                 else
1097                                 {
1098                                         $dmg->amount = $newDmgs[$dmg->damage->id];
1099                                 }
1100                         }
1101                         foreach ($data['damages'] as $dmg)
1102                         {
1103                                 if (!isset($currentDmgs[$dmg['id']]))
1104                                 {
1105                                         $stockDamage = new \Stock\Entity\StockDamage();
1106                                         $stockDamage->damage = $em->getReference('\Stock\Entity\Damage', $dmg['id']);
1107                                         $stockDamage->amount = $dmg['amount'];
1108                                         $stockDamage->stock = $this;
1109                                         $this->damages->add($stockDamage);
1110                                 }
1111                         }
1112                 }
1113
1114                 #-> Update de-normalized auction data.
1115                 \Utility\Registry::getEntityManager()->createQuery(
1116                         'UPDATE \Auction\Entity\Auction a '
1117                         . "SET a.referenceNumber = :referenceNumber, "
1118                         . "    a.registrationNumber = :registrationNumber, "
1119                         . "    a.makeId = :makeId, "
1120                         . "    a.makeName = :makeName, "
1121                         . "    a.modelName = :modelName, "
1122                         . "    a.typeName = :typeName, "
1123                         . "    a.yearName = :yearName, "
1124                         . "    a.stockKm = :km "
1125                         . 'WHERE a.jobState = :jobState '
1126                         . ' AND IDENTITY(a.stock) = :id'
1127                 )
1128                         ->setParameter('jobState', 'Active')
1129                         ->setParameter('id', $this->id)
1130                         ->setParameter('referenceNumber', $this->referenceNumber)
1131                         ->setParameter('registrationNumber', $this->registrationNumber)
1132                         ->setParameter('makeId', $this->type->model->make->id)
1133                         ->setParameter('makeName', $this->type->model->make->name)
1134                         ->setParameter('modelName', $this->type->model->name)
1135                         ->setParameter('typeName', $this->type->name)
1136                         ->setParameter('yearName', $this->vehicleYear->name)
1137                         ->setParameter('km', $this->km)
1138                         ->execute();
1139         }
1140
1141
1142 }
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189