27eeef57068c0728b805f8cf1f26e076c7e952c4
[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                         if (IS_DEV_ENV)
521                         {
522                                 $companyId = 1;
523                         }
524                         $this->company   = $em->getReference('\Company\Entity\Company', $companyId);
525                         $this->createdBy = $em->getReference('\User\Entity\Profile', 1);
526                 }
527                 $this->created = new \DateTime("now");
528                 if (0.0 != $this->tradePrice && is_null($this->tradePriceDate))
529                 {
530                         $this->tradePriceDate = new \DateTime('now');
531                 }
532                 if (0.0 != $this->oldTradePrice && is_null($this->oldTradePriceDate))
533                 {
534                         $this->oldTradePriceDate = new \DateTime('now');
535                 }
536
537         if (0.0 != $this->listPrice && is_null($this->listPriceDate))
538         {
539             $this->listPriceDate = new \DateTime('now');
540         }
541         if (0.0 != $this->oldListPrice && is_null($this->oldListPriceDate))
542         {
543             $this->oldListPriceDate = new \DateTime('now');
544         }
545         }
546
547         /**
548          * @ORM\PreUpdate
549          */
550         public function setUpdateTime()
551         {
552                 $this->updated = new \DateTime("now");
553                 if (0.0 != $this->tradePrice && is_null($this->tradePriceDate))
554                 {
555                         $this->tradePriceDate = new \DateTime('now');
556                 }
557                 if (0.0 != $this->oldTradePrice && is_null($this->oldTradePriceDate))
558                 {
559                         $this->oldTradePriceDate = new \DateTime('now');
560                 }
561
562         if (0.0 != $this->listPrice && is_null($this->listPriceDate))
563         {
564             $this->listPriceDate = new \DateTime('now');
565         }
566         if (0.0 != $this->oldListPrice && is_null($this->oldListPriceDate))
567         {
568             $this->oldListPriceDate = new \DateTime('now');
569         }
570                 /* if ('' == $this->jobState
571                         && is_null($this->loadedOnPriceGuide))
572                 {
573                         $this->loadedOnPriceGuide = new \DateTime("now");
574                 } */
575         }
576
577         /**
578          * Automatically called from DataBin if HAVE_POST_INSERT constant is set on entity.
579          * @return boolean
580          */
581         public function postInsert()
582         {
583                 if (is_null($this->id) || !is_numeric($this->id))
584                 {
585                         return false;
586                 }
587                 $this->referenceNumber = 'S' . str_pad($this->id, 7, '0', STR_PAD_LEFT);
588                 return true;
589         }
590
591         /**
592          * Convert the object to an array.
593          * @param array $expand
594          * @param array $intersect
595          * @param boolean $showIdentifiers
596          * @param integer $expandAll
597          * @return array
598          */
599         public function toArray(
600                         array $expand = array(), array $intersect = array(),
601                         $showIdentifiers = false, $expandAll = 0
602                         )
603         {
604                 $intersect = array_flip($intersect);
605                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
606                 $includeAll = empty($intersect);
607                 $data = array();
608                 ($includeAll || isset($intersect['id']))
609                         && $data['id'] = $this->id;
610                 ($includeAll || isset($intersect['uvi']))
611                         && $data['uvi'] = $this->uvi;
612                 ($includeAll || isset($intersect['company']))
613                         && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
614                                                                         && !is_null($this->company)
615                                 ? (!$showIdentifiers || in_array('company', $expand) ? $this->company->toArray(
616                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
617                                                 ) : $this->company->id)
618                                 : null;
619                 ($includeAll || isset($intersect['auction']))
620                         && $data['auction'] = (in_array('auction', $expand) || $expandAll || $showIdentifiers)
621                                                                         && !is_null($this->auction)
622                                 ? (!$showIdentifiers || in_array('auction', $expand) ? $this->auction->toArray(
623                                                 array(), array('id','company','soldToCompany','stock','endDate','reservePrice','jobState'), true, ($expandAll - 1)
624                                                 ) : $this->auction->id)
625                                 : null;
626                 if (isset($data['auction']))
627                 {
628                         $date = new \DateTime($data['auction']['endDate']);
629                         $data['auction']['Within30DaysOfEnd'] = time() < ($date->getTimestamp() + 2592000);
630                 }
631                 ($includeAll || isset($intersect['createdBy']))
632                         && $data['createdBy'] = (in_array('createdBy', $expand) || $expandAll || $showIdentifiers)
633                                                                         && !is_null($this->createdBy)
634                                 ? (!$showIdentifiers || in_array('createdBy', $expand) ? $this->createdBy->toArray(
635                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
636                                                 ) : $this->createdBy->id)
637                                 : null;
638                 ($includeAll || isset($intersect['valuation']))
639                         && $data['valuation'] = (in_array('valuation_', $expand) || $expandAll || $showIdentifiers)
640                                                                         && !is_null($this->valuation)
641                                 ? (!$showIdentifiers || in_array('valuation_', $expand) ? $this->valuation->toArray(
642                                                 /* $expand, $intersect, $showIdentifiers, ($expandAll - 1) */
643                                                 ) : $this->valuation->id)
644                                 : null;
645                 ($includeAll || isset($intersect['region']))
646                         && $data['region'] = (in_array('region', $expand) || $expandAll || $showIdentifiers)
647                                                                         && !is_null($this->region)
648                                 ? (!$showIdentifiers || in_array('region', $expand) ? $this->region->toArray(
649                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
650                                                 ) : $this->region->id)
651                                 : null;
652                 (!\Utility\Registry::checkOnce('Stock.IgnorePriceGuide', false) && ($includeAll || isset($intersect['priceGuide'])))
653                         && $data['priceGuide'] = (in_array('priceGuide', $expand) || $expandAll || $showIdentifiers)
654                                                                         && !is_null($this->priceGuide)
655                                 ? (!$showIdentifiers || in_array('priceGuide', $expand) ? $this->priceGuide->toArray(
656                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
657                                                 ) : $this->priceGuide->id)
658                                 : null;
659                 if (2 == $showIdentifiers)
660                 {
661                         $data['mmCode']   = $this->type->mmCode;
662                         $data['category'] = $this->type->category->id;
663                         $data['make']     = $this->type->model->make->id;
664                         $data['model']    = $this->type->model->id;
665                 }
666                 ($includeAll || isset($intersect['type']))
667                         && $data['type'] = (in_array('type', $expand) || $expandAll || $showIdentifiers)
668                                                                         && !is_null($this->type)
669                                 ? (!$showIdentifiers || in_array('type', $expand) ? $this->type->toArray(
670                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
671                                                 ) : $this->type->id)
672                                 : null;
673                 ($includeAll || isset($intersect['referenceNumber']))
674                         && $data['referenceNumber'] = $this->referenceNumber;
675                 ($includeAll || isset($intersect['stockNumber']))
676                         && $data['stockNumber'] = $this->stockNumber;
677                 ($includeAll || isset($intersect['registrationNumber']))
678                         && $data['registrationNumber'] = $this->registrationNumber;
679                 ($includeAll || isset($intersect['vinNumber']))
680                         && $data['vinNumber'] = $this->vinNumber;
681                 ($includeAll || isset($intersect['engineNumber']))
682                         && $data['engineNumber'] = $this->engineNumber;
683                 ($includeAll || isset($intersect['fuelType']))
684                         && $data['fuelType'] = (in_array('fuelType', $expand) || $expandAll || $showIdentifiers)
685                                                                         && !is_null($this->fuelType)
686                                 ? (!$showIdentifiers || in_array('fuelType', $expand) ? $this->fuelType->toArray(
687                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
688                                                 ) : $this->fuelType->id)
689                                 : null;
690                 ($includeAll || isset($intersect['transmissionType']))
691                         && $data['transmissionType'] = (in_array('transmissionType', $expand) || $expandAll || $showIdentifiers)
692                                                                         && !is_null($this->transmissionType)
693                                 ? (!$showIdentifiers || in_array('transmissionType', $expand) ? $this->transmissionType->toArray(
694                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
695                                                 ) : $this->transmissionType->id)
696                                 : null;
697                 ($includeAll || isset($intersect['exteriorColour']))
698                         && $data['exteriorColour'] = (in_array('exteriorColour', $expand) || $expandAll || $showIdentifiers)
699                                                                         && !is_null($this->exteriorColour)
700                                 ? (!$showIdentifiers || in_array('exteriorColour', $expand) ? $this->exteriorColour->toArray(
701                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
702                                                 ) : $this->exteriorColour->id)
703                                 : null;
704                 ($includeAll || isset($intersect['interiorColour']))
705                         && $data['interiorColour'] = (in_array('interiorColour', $expand) || $expandAll || $showIdentifiers)
706                                                                         && !is_null($this->interiorColour)
707                                 ? (!$showIdentifiers || in_array('interiorColour', $expand) ? $this->interiorColour->toArray(
708                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
709                                                 ) : $this->interiorColour->id)
710                                 : null;
711                 ($includeAll || isset($intersect['condition']))
712                         && $data['condition'] = (in_array('condition', $expand) || $expandAll || $showIdentifiers)
713                                                                                 && !is_null($this->condition)
714                                 ? (!$showIdentifiers || in_array('condition', $expand) ? $this->condition->toArray(
715                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
716                                                 ) : $this->condition->id)
717                                 : null;
718                 ($includeAll || isset($intersect['vehicleYear']))
719                         && $data['vehicleYear'] = (in_array('vehicleYear', $expand) || $expandAll || $showIdentifiers)
720                                                                                 && !is_null($this->vehicleYear)
721                                 ? (!$showIdentifiers || in_array('vehicleYear', $expand) ? $this->vehicleYear->toArray(
722                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
723                                                 ) : $this->vehicleYear->id)
724                                 : null;
725                 ($includeAll || isset($intersect['km']))
726                         && $data['km'] = $this->km;
727                 ($includeAll || isset($intersect['upholstery']))
728                         && $data['upholstery'] = (in_array('upholstery', $expand) || $expandAll || $showIdentifiers)
729                                                                                 && !is_null($this->upholstery)
730                                 ? (!$showIdentifiers || in_array('upholstery', $expand) ? $this->upholstery->toArray(
731                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
732                                                 ) : $this->upholstery->id)
733                                 : null;
734                 ($includeAll || isset($intersect['papers']))
735                         && $data['papers'] = (in_array('papers', $expand) || $expandAll || $showIdentifiers)
736                                                                                 && !is_null($this->papers)
737                                 ? (!$showIdentifiers || in_array('papers', $expand) ? $this->papers->toArray(
738                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
739                                                 ) : $this->papers->id)
740                                 : null;
741                 ($includeAll || isset($intersect['papersNotes']))
742                         && $data['papersNotes'] = $this->papersNotes;
743                 ($includeAll || isset($intersect['natis']))
744                         && $data['natis'] = (in_array('natis', $expand) || $expandAll || $showIdentifiers)
745                                                                                 && !is_null($this->natis)
746                                 ? (!$showIdentifiers || in_array('natis', $expand) ? $this->natis->toArray(
747                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
748                                                 ) : $this->natis->id)
749                                 : null;
750                 ($includeAll || isset($intersect['spareKeys']))
751                         && $data['spareKeys'] = $this->spareKeys;
752                 ($includeAll || isset($intersect['fullServiceHistory']))
753                         && $data['fullServiceHistory'] = (in_array('fullServiceHistory', $expand) || $expandAll || $showIdentifiers)
754                                                                                                 && !is_null($this->fullServiceHistory)
755                                 ? (!$showIdentifiers || in_array('fullServiceHistory', $expand) ? $this->fullServiceHistory->toArray(
756                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
757                                                 ) : $this->fullServiceHistory->id)
758                                 : null;
759                 ($includeAll || isset($intersect['fshNotes']))
760                         && $data['fshNotes'] = $this->fshNotes;
761                 ($includeAll || isset($intersect['tradePriceDate']))
762                         && $data['tradePriceDate'] = !is_null($this->tradePriceDate)
763                                 ? $this->tradePriceDate->format($dateTimeFormat)
764                                 : null;
765         ($includeAll || isset($intersect['listPriceDate']))
766             && $data['listPriceDate'] = !is_null($this->listPriceDate)
767                 ? $this->listPriceDate->format($dateTimeFormat)
768                 : null;
769                 ($includeAll || isset($intersect['tradePrice']))
770                         && $data['tradePrice'] = $this->tradePrice;
771                 ($includeAll || isset($intersect['retailPrice']))
772                         && $data['retailPrice'] = $this->retailPrice;
773         ($includeAll || isset($intersect['listPrice']))
774             && $data['listPrice'] = $this->listPrice;
775                 ($includeAll || isset($intersect['oldTradePriceDate']))
776                         && $data['oldTradePriceDate'] = !is_null($this->oldTradePriceDate)
777                                 ? $this->oldTradePriceDate->format($dateTimeFormat)
778                                 : null;
779         ($includeAll || isset($intersect['oldListPriceDate']))
780             && $data['oldListPriceDate'] = !is_null($this->oldListPriceDate)
781                 ? $this->oldListPriceDate->format($dateTimeFormat)
782                 : null;
783                 ($includeAll || isset($intersect['oldTradePrice']))
784                         && $data['oldTradePrice'] = $this->oldTradePrice;
785                 ($includeAll || isset($intersect['oldRetailPrice']))
786                         && $data['oldRetailPrice'] = $this->oldRetailPrice;
787         ($includeAll || isset($intersect['oldListPrice']))
788             && $data['oldListPrice'] = $this->oldListPrice;
789                 ($includeAll || isset($intersect['retailAmountRequired']))
790                         && $data['retailAmountRequired'] = $this->retailAmountRequired;
791                 ($includeAll || isset($intersect['mainImage']))
792                         && $data['mainImage'] = (in_array('mainImage', $expand) || $expandAll || $showIdentifiers)
793                                                                                 && !is_null($this->mainImage)
794                                 ? (!$showIdentifiers || in_array('mainImage', $expand) ? $this->mainImage->toArray(
795                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
796                                                 ) : $this->mainImage->id)
797                                 : null;
798                 ($includeAll || isset($intersect['frontImage']))
799                         && $data['frontImage'] = (in_array('frontImage', $expand) || $expandAll || $showIdentifiers)
800                                                                                 && !is_null($this->frontImage)
801                                 ? (!$showIdentifiers || in_array('frontImage', $expand) ? $this->frontImage->toArray(
802                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
803                                                 ) : $this->frontImage->id)
804                                 : null;
805                 ($includeAll || isset($intersect['rightImage']))
806                         && $data['rightImage'] = (in_array('rightImage', $expand) || $expandAll || $showIdentifiers)
807                                                                                 && !is_null($this->rightImage)
808                                 ? (!$showIdentifiers || in_array('rightImage', $expand) ? $this->rightImage->toArray(
809                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
810                                                 ) : $this->rightImage->id)
811                                 : null;
812                 ($includeAll || isset($intersect['leftImage']))
813                         && $data['leftImage'] = (in_array('leftImage', $expand) || $expandAll || $showIdentifiers)
814                                                                                 && !is_null($this->leftImage)
815                                 ? (!$showIdentifiers || in_array('leftImage', $expand) ? $this->leftImage->toArray(
816                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
817                                                 ) : $this->leftImage->id)
818                                 : null;
819                 ($includeAll || isset($intersect['backImage']))
820                         && $data['backImage'] = (in_array('backImage', $expand) || $expandAll || $showIdentifiers)
821                                                                                 && !is_null($this->backImage)
822                                 ? (!$showIdentifiers || in_array('backImage', $expand) ? $this->backImage->toArray(
823                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
824                                                 ) : $this->backImage->id)
825                                 : null;
826                 ($includeAll || isset($intersect['interiorImage']))
827                         && $data['interiorImage'] = (in_array('interiorImage', $expand) || $expandAll || $showIdentifiers)
828                                                                                 && !is_null($this->interiorImage)
829                                 ? (!$showIdentifiers || in_array('interiorImage', $expand) ? $this->interiorImage->toArray(
830                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
831                                                 ) : $this->interiorImage->id)
832                                 : null;
833                 ($includeAll || isset($intersect['engineImage']))
834                         && $data['engineImage'] = (in_array('engineImage', $expand) || $expandAll || $showIdentifiers)
835                                                                                 && !is_null($this->engineImage)
836                                 ? (!$showIdentifiers || in_array('engineImage', $expand) ? $this->engineImage->toArray(
837                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
838                                                 ) : $this->engineImage->id)
839                                 : null;
840                 ($includeAll || isset($intersect['natisImage']))
841                         && $data['natisImage'] = (in_array('natisImage', $expand) || $expandAll || $showIdentifiers)
842                                                                                 && !is_null($this->natisImage)
843                                 ? (!$showIdentifiers || in_array('natisImage', $expand) ? $this->natisImage->toArray(
844                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
845                                                 ) : $this->natisImage->id)
846                                 : null;
847                 ($includeAll || isset($intersect['accessories']))
848                         && $data['accessories'] = (in_array('accessories', $expand) || $expandAll || $showIdentifiers)
849                                                                                 && !is_null($this->accessories)
850                                 ? $this->accessoriesToArray($showIdentifiers)
851                                 : null;
852                 ($includeAll || isset($intersect['accessoryNotes']))
853                         && $data['accessoryNotes'] = $this->accessoryNotes;
854                 ($includeAll || isset($intersect['damages']))
855                         && $data['damages'] = (in_array('damages', $expand) || $expandAll || $showIdentifiers)
856                                                                                 && !is_null($this->damages)
857                                 ? $this->damagesToArray($showIdentifiers)
858                                 : null;
859                 ($includeAll || isset($intersect['damageTotal']))
860                         && $data['damageTotal'] = $this->damageTotal;
861                 ($includeAll || isset($intersect['damageNotes']))
862                         && $data['damageNotes'] = $this->damageNotes;
863                 ($includeAll || isset($intersect['previousRepairsNoted']))
864                         && $data['previousRepairsNoted'] = $this->previousRepairsNoted;
865                 ($includeAll || isset($intersect['previousRepairsNotes']))
866                         && $data['previousRepairsNotes'] = $this->previousRepairsNotes;
867                 ($includeAll || isset($intersect['coverReceived']))
868                         && $data['coverReceived'] = $this->coverReceived;
869                 ($includeAll || isset($intersect['numberOfOffers']))
870                         && $data['numberOfOffers'] = $this->numberOfOffers;
871                 ($includeAll || isset($intersect['highestOffer']))
872                         && $data['highestOffer'] = $this->highestOffer;
873                 ($includeAll || isset($intersect['loadedOnPriceGuide']))
874                         && $data['loadedOnPriceGuide'] = !is_null($this->loadedOnPriceGuide)
875                                 ? $this->loadedOnPriceGuide->format($dateTimeFormat)
876                                 : null;
877                 ($includeAll || isset($intersect['timesListed']))
878                         && $data['timesListed'] = $this->timesListed;
879                 ($includeAll || isset($intersect['highestBid']))
880                         && $data['highestBid'] = $this->highestBid;
881                 ($includeAll || isset($intersect['jobState']))
882                         && $data['jobState'] = $this->jobState;
883                 ($includeAll || isset($intersect['created']))
884                         && $data['created'] = !is_null($this->created)
885                                 ? $this->created->format($dateTimeFormat)
886                                 : null;
887                 ($includeAll || isset($intersect['updated']))
888                         && $data['updated'] = !is_null($this->updated)
889                                 ? $this->updated->format($dateTimeFormat)
890                                 : null;
891                 ($includeAll || isset($intersect['reset']))
892                         && $data['reset'] = !is_null($this->reset)
893                                 ? $this->reset->format($dateTimeFormat)
894                                 : null;
895                 return $data;
896         }
897
898         /**
899          * Internal utility to change accessories collection into array.
900          * @param boolean $showIdentifiers
901          * @return array
902          */
903         public function accessoriesToArray($showIdentifiers = false)
904         {
905                 $data = array();
906                 $iterator = $this->accessories->getIterator();
907                 if (2 == $showIdentifiers)
908                 {
909                         foreach ($iterator as $accessory)
910                         {
911                                 $data[] = $accessory->accessory->id;
912                         }
913                 }
914                 else
915                 {
916                         foreach ($iterator as $accessory)
917                         {
918                                 $data[] = $showIdentifiers
919                                         ? array('id' => $accessory->accessory->id)
920                                         : $accessory->toArray();
921                         }
922                 }
923                 return $data;
924         }
925
926         /**
927          * Internal utility to change damages collection into array.
928          * @param boolean $showIdentifiers
929          * @return array
930          */
931         public function damagesToArray($showIdentifiers = false)
932         {
933                 $data = array();
934                 $iterator = $this->damages->getIterator();
935                 foreach ($iterator as $damage)
936                 {
937                         $data[] = $damage->toArray(array(), array(), $showIdentifiers);
938                 }
939                 return $data;
940         }
941
942         /**
943          * Populate from an array.
944          * @param array $data
945          */
946         public function fromArray($data = array())
947         {
948                 isset($data['id'])
949                         && $this->id = $data['id'];
950                 isset($data['uvi'])
951                         && $this->uvi = $data['uvi'];
952                 isset($data['company'])
953                         && $this->company = $data['company'];
954                 isset($data['createdBy'])
955                         && $this->createdBy = $data['createdBy'];
956                 isset($data['region'])
957                         && $this->region = $data['region'];
958                 isset($data['type'])
959                         && $this->type = $data['type'];
960                 isset($data['stockNumber'])
961                         && $this->stockNumber = $data['stockNumber'];
962                 isset($data['registrationNumber'])
963                         && $this->registrationNumber = $data['registrationNumber'];
964                 isset($data['vinNumber'])
965                         && $this->vinNumber = $data['vinNumber'];
966                 isset($data['engineNumber'])
967                         && $this->engineNumber = $data['engineNumber'];
968                 isset($data['fuelType'])
969                         && $this->fuelType = $data['fuelType'];
970                 isset($data['transmissionType'])
971                         && $this->transmissionType = $data['transmissionType'];
972                 isset($data['exteriorColour'])
973                         && $this->exteriorColour = $data['exteriorColour'];
974                 isset($data['interiorColour'])
975                         && $this->interiorColour = $data['interiorColour'];
976                 isset($data['condition'])
977                         && $this->condition = $data['condition'];
978                 isset($data['vehicleYear'])
979                         && $this->vehicleYear = $data['vehicleYear'];
980                 isset($data['km'])
981                         && $this->km = $data['km'];
982                 isset($data['upholstery'])
983                         && $this->upholstery = $data['upholstery'];
984                 isset($data['papers'])
985                         && $this->papers = $data['papers'];
986                 isset($data['papersNotes'])
987                         && $this->papersNotes = $data['papersNotes'];
988                 isset($data['natis'])
989                         && $this->natis = $data['natis'];
990                 isset($data['spareKeys'])
991                         && $this->spareKeys = $data['spareKeys'];
992                 isset($data['fullServiceHistory'])
993                         && $this->fullServiceHistory = $data['fullServiceHistory'];
994                 isset($data['fshNotes'])
995                         && $this->fshNotes = $data['fshNotes'];
996                 isset($data['tradePrice'])
997                         && $this->tradePrice = $data['tradePrice'];
998                 isset($data['retailPrice'])
999                         && $this->retailPrice = $data['retailPrice'];
1000         isset($data['listPrice'])
1001             && $this->listPrice = $data['listPrice'];
1002                 isset($data['oldTradePrice'])
1003                         && $this->oldTradePrice = $data['oldTradePrice'];
1004                 isset($data['oldRetailPrice'])
1005                         && $this->oldRetailPrice = $data['oldRetailPrice'];
1006         isset($data['oldListPrice'])
1007             && $this->oldListPrice = $data['oldListPrice'];
1008                 isset($data['retailAmountRequired'])
1009                         && $this->retailAmountRequired = $data['retailAmountRequired'];
1010                 isset($data['mainImage'])
1011                         && $this->mainImage = $data['mainImage'];
1012                 isset($data['frontImage'])
1013                         && $this->frontImage = $data['frontImage'];
1014                 isset($data['rightImage'])
1015                         && $this->rightImage = $data['rightImage'];
1016                 isset($data['leftImage'])
1017                         && $this->leftImage = $data['leftImage'];
1018                 isset($data['backImage'])
1019                         && $this->backImage = $data['backImage'];
1020                 isset($data['interiorImage'])
1021                         && $this->interiorImage = $data['interiorImage'];
1022                 isset($data['engineImage'])
1023                         && $this->engineImage = $data['engineImage'];
1024                 isset($data['natisImage'])
1025                         && $this->natisImage = $data['natisImage'];
1026                 isset($data['accessoryNotes'])
1027                         && $this->accessoryNotes = $data['accessoryNotes'];
1028                 isset($data['damageTotal'])
1029                         && $this->damageTotal = $data['damageTotal'];
1030                 isset($data['damageNotes'])
1031                         && $this->damageNotes = $data['damageNotes'];
1032                 isset($data['previousRepairsNoted'])
1033                         && $this->previousRepairsNoted = $data['previousRepairsNoted'];
1034                 isset($data['previousRepairsNotes'])
1035                         && $this->previousRepairsNotes = $data['previousRepairsNotes'];
1036                 
1037                 isset($data['coverReceived'])
1038                         && $this->coverReceived = $data['coverReceived'];
1039                 
1040                 isset($data['numberOfOffers'])
1041                         && $this->numberOfOffers = $data['numberOfOffers'];
1042                 isset($data['highestOffer'])
1043                         && $this->highestOffer = $data['highestOffer'];
1044                 isset($data['loadedOnPriceGuide'])
1045                         && $this->loadedOnPriceGuide = $data['loadedOnPriceGuide'];
1046         isset($data['jobState'])
1047             && $this->jobState = $data['jobState'];
1048                 if (isset($data['accessories']))
1049                 {
1050                         $em = \Utility\Registry::getEntityManager();
1051                         $newAccs = array();
1052                         foreach ($data['accessories'] as $acc)
1053                         {
1054                                 $newAccs[$acc['id']] = $acc['id'];
1055                         }
1056                         $currentAccs = array();
1057                         $iterator = $this->accessories->getIterator();
1058                         foreach ($iterator as $acc)
1059                         {
1060                                 $currentAccs[$acc->accessory->id] = $acc->accessory->id;
1061                                 !isset($newAccs[$acc->accessory->id])
1062                                         && $em->remove($acc);
1063                         }
1064                         foreach ($data['accessories'] as $acc)
1065                         {
1066                                 if (!isset($currentAccs[$acc['id']]))
1067                                 {
1068                                         $stockAcc = new \Stock\Entity\StockAccessory();
1069                                         $stockAcc->accessory = $em->getReference('\Stock\Entity\Accessory', $acc['id']);
1070                                         $stockAcc->stock = $this;
1071                                         $this->accessories->add($stockAcc);
1072                                 }
1073                         }
1074                 }
1075                 if (isset($data['damages']))
1076                 {
1077                         $em = \Utility\Registry::getEntityManager();
1078                         $newDmgs = array();
1079                         $total = 0.0;
1080                         foreach ($data['damages'] as $dmg)
1081                         {
1082                                 $total += $dmg['amount'];
1083                                 $newDmgs[$dmg['id']] = $dmg['amount'];
1084                         }
1085                         $this->damageTotal = $total;
1086                         $currentDmgs = array();
1087                         $iterator = $this->damages->getIterator();
1088                         foreach ($iterator as $dmg)
1089                         {
1090                                 $currentDmgs[$dmg->damage->id] = $dmg->damage->id;
1091                                 if (!isset($newDmgs[$dmg->damage->id]))
1092                                 {
1093                                         $em->remove($dmg);
1094                                 }
1095                                 else
1096                                 {
1097                                         $dmg->amount = $newDmgs[$dmg->damage->id];
1098                                 }
1099                         }
1100                         foreach ($data['damages'] as $dmg)
1101                         {
1102                                 if (!isset($currentDmgs[$dmg['id']]))
1103                                 {
1104                                         $stockDamage = new \Stock\Entity\StockDamage();
1105                                         $stockDamage->damage = $em->getReference('\Stock\Entity\Damage', $dmg['id']);
1106                                         $stockDamage->amount = $dmg['amount'];
1107                                         $stockDamage->stock = $this;
1108                                         $this->damages->add($stockDamage);
1109                                 }
1110                         }
1111                 }
1112
1113                 #-> Update de-normalized auction data.
1114                 \Utility\Registry::getEntityManager()->createQuery(
1115                         'UPDATE \Auction\Entity\Auction a '
1116                         . "SET a.referenceNumber = :referenceNumber, "
1117                         . "    a.registrationNumber = :registrationNumber, "
1118                         . "    a.makeId = :makeId, "
1119                         . "    a.makeName = :makeName, "
1120                         . "    a.modelName = :modelName, "
1121                         . "    a.typeName = :typeName, "
1122                         . "    a.yearName = :yearName, "
1123                         . "    a.stockKm = :km "
1124                         . 'WHERE a.jobState = :jobState '
1125                         . ' AND IDENTITY(a.stock) = :id'
1126                 )
1127                         ->setParameter('jobState', 'Active')
1128                         ->setParameter('id', $this->id)
1129                         ->setParameter('referenceNumber', $this->referenceNumber)
1130                         ->setParameter('registrationNumber', $this->registrationNumber)
1131                         ->setParameter('makeId', $this->type->model->make->id)
1132                         ->setParameter('makeName', $this->type->model->make->name)
1133                         ->setParameter('modelName', $this->type->model->name)
1134                         ->setParameter('typeName', $this->type->name)
1135                         ->setParameter('yearName', $this->vehicleYear->name)
1136                         ->setParameter('km', $this->km)
1137                         ->execute();
1138         }
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