text changes to registration mail content
[namibia] / module / Valuation / src / Valuation / Entity / Valuation.php
1 <?php
2 namespace Valuation\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * A valuation.
10  *
11  * @ORM\Entity
12  * @ORM\HasLifecycleCallbacks
13  * @ORM\Table(name="stock_valuation")
14  */
15 class Valuation
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 = 'Build';
26         /**
27          * Push Synchronization Strategy for this table.
28          */
29         const PUSH_SYNCH_STRATEGY = 'Update';
30         /**
31          * Post insert action must be called after new entity is flushed to database.
32          */
33         const HAVE_POST_INSERT = true;
34         /**
35          * Handle as a job queue for mobile devices.
36          */
37         const JOB_QUEUE = true;
38
39
40         /*
41          * Department options.
42          */
43         const DEPARTMENT_NEW  = 'New';
44         const DEPARTMENT_USED = 'Used';
45
46
47
48         /* ------------------------------------ Identification ------------------------------------ */
49         /**
50          * @ORM\Id
51          * @ORM\Column(type="integer");
52          * @ORM\GeneratedValue(strategy="AUTO")
53          */
54         protected $id;
55
56         /**
57          * @ORM\Column(type="string", length=12, nullable=true, name="valuation_number");
58          */
59         protected $valuationNumber;
60
61
62         /* ------------------------------------ Ownership ------------------------------------ */
63         /**
64          * @ORM\ManyToOne(targetEntity="\Stock\Entity\Stock", cascade={"all"})
65          * @ORM\JoinColumn(nullable=false, name="stock_id")
66          **/
67         protected $stock;
68
69         /**
70          * @ORM\ManyToOne(targetEntity="\Valuation\Entity\XmlRpc", cascade={"all"})
71          * @ORM\JoinColumn(nullable=true, name="xmlrpc_auth_id")
72          **/
73         protected $xmlRpcClient;
74
75
76         /* ------------------------------------ History ------------------------------------ */
77         /**
78          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
79          * @ORM\JoinColumn(nullable=false, name="created_by_profile_id")
80          **/
81         protected $createdBy;
82
83         /**
84          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
85          * @ORM\JoinColumn(nullable=true, name="valuated_by_profile_id")
86          **/
87         protected $valuatedBy;
88
89         /**
90          * @ORM\OneToMany(targetEntity="ValuationValuators", mappedBy="valuation", cascade={"all"}, fetch="EXTRA_LAZY")
91          **/
92         protected $valuators;
93
94         /**
95          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
96          * @ORM\JoinColumn(nullable=true, name="sales_profile_id")
97          **/
98         protected $salesProfile;
99
100         /**
101          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
102          * @ORM\JoinColumn(nullable=true, name="manager_profile_id")
103          **/
104         protected $managerProfile;
105
106         /**
107          * @ORM\Column(type="datetime", nullable=true, name="sent_to_sales");
108          */
109         protected $sentToSales;
110
111         /**
112          * @ORM\Column(type="string", length=50, nullable=true, name="sales_force_item_id")
113          */
114         protected $sfItemId;
115
116
117 //      /* ------------------------------------ Customer ------------------------------------ */
118         /**
119          * @ORM\Column(type="string", length=50, nullable=true, name="first_name")
120          */
121         protected $firstName;
122
123         /**
124          * @ORM\Column(type="string", length=50, nullable=true, name="family_name")
125          */
126         protected $familyName;
127
128         /**
129          * @ORM\Column(type="string", length=13, nullable=true, name="id_number")
130          */
131         protected $idNumber;
132
133         /**
134          * @ORM\Column(type="string", length=20, nullable=true)
135          */
136         protected $mobile;
137
138         /**
139          * @ORM\Column(type="string", length=255, nullable=true)
140          */
141         protected $email;
142
143         /**
144          * @ORM\Column(type="string", length=25, nullable=true, name="department");
145          */
146         protected $department;
147
148         /**
149          * @ORM\ManyToOne(targetEntity="\Location\Entity\Region")
150          * @ORM\JoinColumn(nullable=true, name="lib_region_id")
151          **/
152         protected $region;
153
154         /**
155          * @ORM\ManyToOne(targetEntity="\Location\Entity\Town")
156          * @ORM\JoinColumn(nullable=true, name="lib_city_id")
157          **/
158         protected $city;
159
160         /**
161          * @ORM\Column(type="string", length=150, nullable=true)
162          */
163         protected $street;
164
165         /**
166          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=true, name="required_price", options={"unsigned"=true});
167          */
168         protected $requiredPrice = 0.0;
169
170         /**
171          * @ORM\Column(type="boolean", nullable=true);
172          */
173         protected $sighted = true;
174
175     /**
176      *  @ORM\Column(nullable=true, type="boolean", name="license_disc_expired");
177      */
178     protected $licenseDiscExpired = true;
179
180         /**
181          * @ORM\Column(type="boolean", nullable=true, name="trade_retail_requested");
182          */
183         protected $tradeRetailRequested;
184
185
186         /* ------------------------------------ Pricing ------------------------------------ */
187         /**
188          * @ORM\Column(type="text", name="sales_comments")
189          */
190         protected $salesComments = '';
191
192     /**
193      * @ORM\Column(type="text", name="customer_comments")
194      */
195     protected $customerComments = '';
196
197         /**
198          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="amount_offered", options={"unsigned"=true});
199          */
200         protected $amountOffered = 0.0;
201
202     /**
203      * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="bank_settlement_amount", options={"unsigned"=true});
204      */
205     protected $bankSettlement = 0.0;
206
207         /**
208          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="over_allowance", options={"unsigned"=true});
209          */
210         protected $overAllowance = 0.0;
211
212         /**
213          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="stand_in_value", options={"unsigned"=true});
214          */
215         protected $standInValue = 0.0;
216
217         /**
218          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="projected_retail", options={"unsigned"=true});
219          */
220         protected $projectedRetail = 0.0;
221
222         /**
223          * @ORM\Column(type="decimal", scale=2, precision=11, nullable=false, name="planned_margin", options={"unsigned"=true});
224          */
225         protected $plannedMargin = 0.0;
226
227
228     /**
229      * @ORM\Column(type="string", length=150, nullable=true, name="customer_address_street")
230      */
231     protected $customerAddressStreet;
232
233     /**
234      * @ORM\Column(type="string", length=150, nullable=true, name="customer_address_street_name")
235      */
236     protected $customerAddressStreetName;
237
238     /**
239      * @ORM\Column(type="string", length=150, nullable=true, name="customer_address_suburb")
240      */
241     protected $customerAddressSuburb;
242
243     /**
244      * @ORM\Column(type="string", length=150, nullable=true, name="customer_address_city")
245      */
246     protected $customerAddressCity;
247
248     /**
249      * @ORM\Column(type="string", length=150, nullable=true, name="customer_address_postal_code")
250      */
251     protected $customerAddressPostalCode;
252
253     /**
254      * @ORM\ManyToOne(targetEntity="\Valuation\Entity\SendToStockFrom")
255      * @ORM\JoinColumn(nullable=true, name="send_to_stock_from_id")
256      **/
257     protected $sendToStockFrom;
258
259     /**
260      * @ORM\ManyToOne(targetEntity="\Valuation\Entity\DealNotDoneSelection")
261      * @ORM\JoinColumn(nullable=true, name="deal_not_done_selection_id")
262      **/
263     protected $dealNotDoneSelection;
264
265     /**
266      * @ORM\Column(type="text", name="deal_not_done_reason", nullable=true)
267      */
268     protected $dealNotDoneReason;
269
270
271         /* ------------------------------------ Tracking ------------------------------------ */
272         /**
273          * @ORM\Column(type="smallint", name="queue_status");
274          */
275         protected $queueStatus = 0;
276
277         /**
278          * @ORM\Column(nullable=true, type="string", length=25, nullable=true, name="internal_status");
279          */
280         protected $internalState;
281
282         /**
283          * @ORM\Column(type="string", length=25, nullable=true, name="previous_status");
284          */
285         protected $previousState;
286
287         /**
288          * @ORM\Column(nullable=true, type="string", length=25, name="status");
289          */
290         protected $jobState;
291
292         /**
293          * @ORM\Column(type="datetime");
294          */
295         protected $created;
296
297         /**
298          * @ORM\Column(type="datetime", nullable=true);
299          */
300         protected $updated;
301
302         /**
303          * @ORM\Column(type="boolean");
304          */
305         protected $archived = false;
306
307         /**
308          * @ORM\Column(type="boolean",  name="is_public");
309          */
310         protected $isPublic = false;
311
312     /**
313      * @ORM\Column(type="boolean",  name="is_public_customer");
314      */
315     protected $isPublicCustomer = false;
316
317         /**
318          * Initialize collections.
319          */
320         public function __construct()
321         {
322                 $this->valuators = new \Doctrine\Common\Collections\ArrayCollection();
323         }
324
325         /**
326          * Add a new Valuator to this Stock Valuation Item.
327          * @param \User\Entity\Profile $profile
328          * @return \Valuation\Entity\Valuation
329          */
330         public function addValuator(\User\Entity\Profile $profile)
331         {
332                 $this->valuators[] = $profile;
333                 return $this;
334         }
335
336
337
338         /**
339          * Magic getter to expose protected properties.
340          *
341          * @param string $property
342          * @return mixed
343          */
344         public function __get($property)
345         {
346                 return $this->$property;
347         }
348
349         /**
350          * Magic setter to save protected properties.
351          *
352          * @param string $property
353          * @param mixed $value
354          */
355         public function __set($property, $value)
356         {
357                 $this->$property = $value;
358         }
359
360         /**
361          * @ORM\PrePersist
362          */
363         public function setCreateTime()
364         {
365                 if (!\Utility\Registry::get('IsDeviceApiCall', false))
366                 {
367                         $this->createdBy = \Utility\Registry::isAuthenticated()
368                                 ? \Utility\Registry::resolveProfileContext($this->createdBy)
369                                 : \Utility\Registry::getEntityManager()
370                                         ->getReference('\User\Entity\Profile', 1);
371                 }
372                 $this->created = new \DateTime("now");
373         }
374
375         /**
376          * @ORM\PreUpdate
377          */
378         public function setUpdateTime()
379         {
380                 $this->updated = new \DateTime("now");
381                 ('New Valuation' != $this->jobState)
382                         && 0 < $this->queueStatus
383                         && $this->queueStatus = 0;
384                 if ('Price Guide' == $this->jobState
385                         && is_null($this->stock->loadedOnPriceGuide))
386                 {
387                         $this->stock->loadedOnPriceGuide = new \DateTime("now");
388                 }
389                 if (!is_null($this->salesProfile) && is_null($this->sentToSales))
390                 {
391                         $this->sentToSales = new \DateTime("now");
392                 }
393         }
394
395         /**
396          * Automatically called from DataBin if HAVE_POST_INSERT constant is set on entity.
397          * @return boolean
398          */
399         public function postInsert()
400         {
401                 if (is_null($this->id) || !is_numeric($this->id))
402                 {
403                         return false;
404                 }
405                 $this->valuationNumber = 'V' . str_pad($this->id, 7, '0', STR_PAD_LEFT);
406                 return true;
407         }
408
409         /**
410          * Convert the object to an array.
411          * @param array $expand
412          * @param array $intersect
413          * @param boolean $showIdentifiers
414          * @param integer $expandAll
415          * @return array
416          */
417         public function toArray(
418                         array $expand = array(), array $intersect = array(),
419                         $showIdentifiers = false, $expandAll = 0
420                         )
421         {
422                 $intersect = array_flip($intersect);
423                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
424                 $includeAll = empty($intersect);
425                 $data = array();
426                 ($includeAll || isset($intersect['id']))
427                         && $data['id'] = $this->id;
428                 ($includeAll || isset($intersect['valuationNumber']))
429                         && $data['valuationNumber'] = $this->valuationNumber;
430                 ($includeAll || isset($intersect['stock']))
431                         && $data['stock'] = (in_array('stock', $expand) || $expandAll || $showIdentifiers)
432                                                                         && !is_null($this->stock)
433                                 ? (!$showIdentifiers || in_array('stock', $expand) ? $this->stock->toArray(
434                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
435                                                 ) : $this->stock->id)
436                                 : null;
437                 ($includeAll || isset($intersect['xmlRpcClient']))
438                         && $data['xmlRpcClient'] = (in_array('xmlRpcClient', $expand) || $expandAll || $showIdentifiers)
439                                                                         && !is_null($this->xmlRpcClient)
440                                 ? (!$showIdentifiers || in_array('xmlRpcClient', $expand) ? $this->xmlRpcClient->toArray(
441                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
442                                                 ) : $this->xmlRpcClient->id)
443                                 : null;
444                 ($includeAll || isset($intersect['createdBy']))
445                         && $data['createdBy'] = (in_array('createdBy', $expand) || $expandAll || $showIdentifiers)
446                                                                         && !is_null($this->createdBy)
447                                 ? (!$showIdentifiers || in_array('createdBy', $expand) ? $this->createdBy->toArray(
448                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
449                                                 ) : $this->createdBy->id)
450                                 : null;
451                 ($includeAll || isset($intersect['valuatedBy']))
452                         && $data['valuatedBy'] = (in_array('valuatedBy', $expand) || $expandAll || $showIdentifiers)
453                                                                         && !is_null($this->valuatedBy)
454                                 ? (!$showIdentifiers || in_array('valuatedBy', $expand) ? $this->valuatedBy->toArray(
455                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
456                                                 ) : $this->valuatedBy->id)
457                                 : null;
458                 ($includeAll || isset($intersect['valuators']))
459                         && $data['valuators'] = (in_array('valuators', $expand) || $expandAll || $showIdentifiers)
460                                                                                 && !is_null($this->valuators)
461                                 ? $this->valuatorsToArray($showIdentifiers)
462                                 : null;
463                 ($includeAll || isset($intersect['salesProfile']))
464                         && $data['salesProfile'] = (in_array('salesProfile', $expand) || $expandAll || $showIdentifiers)
465                                                                         && !is_null($this->salesProfile)
466                                 ? (!$showIdentifiers || in_array('salesProfile', $expand) ? $this->salesProfile->toArray(
467                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
468                                                 ) : $this->salesProfile->id)
469                                 : null;
470                 ($includeAll || isset($intersect['managerProfile']))
471                         && $data['managerProfile'] = (in_array('managerProfile', $expand) || $expandAll || $showIdentifiers)
472                                                                         && !is_null($this->managerProfile)
473                                 ? (!$showIdentifiers || in_array('managerProfile', $expand) ? $this->managerProfile->toArray(
474                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
475                                                 ) : $this->managerProfile->id)
476                                 : null;
477                 ($includeAll || isset($intersect['firstName']))
478                         && $data['firstName'] = $this->firstName;
479                 ($includeAll || isset($intersect['familyName']))
480                         && $data['familyName'] = $this->familyName;
481                 ($includeAll || isset($intersect['idNumber']))
482                         && $data['idNumber'] = $this->idNumber;
483                 ($includeAll || isset($intersect['mobile']))
484                         && $data['mobile'] = $this->mobile;
485                 ($includeAll || isset($intersect['email']))
486                         && $data['email'] = $this->email;
487                 ($includeAll || isset($intersect['department']))
488                         && $data['department'] = $this->department;
489                 ($includeAll || isset($intersect['region']))
490                         && $data['region'] = (in_array('region', $expand) || $expandAll || $showIdentifiers)
491                                                                         && !is_null($this->region)
492                                 ? (!$showIdentifiers || in_array('region', $expand) ? $this->region->toArray(
493                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
494                                                 ) : $this->region->id)
495                                 : null;
496                 ($includeAll || isset($intersect['city']))
497                         && $data['city'] = (in_array('city', $expand) || $expandAll || $showIdentifiers)
498                                                                         && !is_null($this->city)
499                                 ? (!$showIdentifiers || in_array('city', $expand) ? $this->city->toArray(
500                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
501                                                 ) : $this->city->id)
502                                 : null;
503                 ($includeAll || isset($intersect['street']))
504                         && $data['street'] = $this->street;
505                 ($includeAll || isset($intersect['sighted']))
506                         && $data['sighted'] = $this->sighted;
507         ($includeAll || isset($intersect['licenseDiscExpired']))
508             && $data['licenseDiscExpired'] = $this->licenseDiscExpired;
509                 ($includeAll || isset($intersect['requiredPrice']))
510                         && $data['requiredPrice'] = $this->requiredPrice;
511                 ($includeAll || isset($intersect['amountOffered']))
512                         && $data['amountOffered'] = $this->amountOffered;
513         ($includeAll || isset($intersect['bankSettlement']))
514         && $data['bankSettlement'] = $this->bankSettlement;
515                 ($includeAll || isset($intersect['salesComments']))
516                         && $data['salesComments'] = $this->salesComments;
517
518         ($includeAll || isset($intersect['customerComments']))
519         && $data['customerComments'] = $this->customerComments;
520
521         ($includeAll || isset($intersect['customerAddressStreet']))
522         && $data['customerAddressStreet'] = $this->customerAddressStreet;
523         ($includeAll || isset($intersect['customerAddressStreetName']))
524         && $data['customerAddressStreetName'] = $this->customerAddressStreetName;
525         ($includeAll || isset($intersect['customerAddressSuburb']))
526         && $data['customerAddressSuburb'] = $this->customerAddressSuburb;
527         ($includeAll || isset($intersect['customerAddressCity']))
528         && $data['customerAddressCity'] = $this->customerAddressCity;
529         ($includeAll || isset($intersect['customerAddressPostalCode']))
530         && $data['customerAddressPostalCode'] = $this->customerAddressPostalCode;
531
532
533         ($includeAll || isset($intersect['sendToStockFrom']))
534         && $data['sendToStockFrom'] = (in_array('sendToStockFrom', $expand) || $expandAll || $showIdentifiers)
535         && !is_null($this->sendToStockFrom)
536             ? (!$showIdentifiers || in_array('sendToStockFrom', $expand) ? $this->sendToStockFrom->toArray(
537                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
538             ) : $this->sendToStockFrom->id)
539             : null;
540
541         ($includeAll || isset($intersect['dealNotDoneSelection']))
542         && $data['dealNotDoneSelection'] = (in_array('dealNotDoneSelection', $expand) || $expandAll || $showIdentifiers)
543         && !is_null($this->dealNotDoneSelection)
544             ? (!$showIdentifiers || in_array('dealNotDoneSelection', $expand) ? $this->dealNotDoneSelection->toArray(
545                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
546             ) : $this->dealNotDoneSelection->id)
547             : null;
548
549         ($includeAll || isset($intersect['dealNotDoneReason']))
550         && $data['dealNotDoneReason'] = $this->dealNotDoneReason;
551
552                 ($includeAll || isset($intersect['overAllowance']))
553                         && $data['overAllowance'] = $this->overAllowance;
554                 ($includeAll || isset($intersect['standInValue']))
555                         && $data['standInValue'] = $this->standInValue;
556                 ($includeAll || isset($intersect['projectedRetail']))
557                         && $data['projectedRetail'] = $this->projectedRetail;
558                 ($includeAll || isset($intersect['plannedMargin']))
559                         && $data['plannedMargin'] = $this->plannedMargin;
560                 ($includeAll || isset($intersect['queueStatus']))
561                         && $data['queueStatus'] = $this->queueStatus;
562                 ($includeAll || isset($intersect['jobState']))
563                         && $data['jobState'] = $this->jobState;
564
565                 ($includeAll || isset($intersect['previousState']))
566                 && $data['previousState'] = $this->previousState;
567         ($includeAll || isset($intersect['isPublicCustomer']))
568         && $data['isPublicCustomer'] = $this->isPublicCustomer;
569
570                 ($includeAll || isset($intersect['created']))
571                         && $data['created'] = !is_null($this->created)
572                                 ? $this->created->format($dateTimeFormat)
573                                 : null;
574                 ($includeAll || isset($intersect['updated']))
575                         && $data['updated'] = !is_null($this->updated)
576                                 ? $this->updated->format($dateTimeFormat)
577                                 : null;
578                 return $data;
579         }
580
581         /**
582          * Internal utility to change valuators collection into array.
583          * @param boolean $showIdentifiers
584          * @return array
585          */
586         protected function valuatorsToArray($showIdentifiers = false)
587         {
588                 $data = array();
589                 $iterator = $this->valuators->getIterator();
590                 foreach ($iterator as $valuator)
591                 {
592                         $data[] = $showIdentifiers
593                                 ? array('id' => $valuator->profile->id)
594                                 : $valuator->toArray();
595                 }
596                 return $data;
597         }
598
599         /**
600          * Convert the object to an array for queue synchronization.
601          * @return array
602          */
603         public function toQueueArray()
604         {
605                 return array(
606                                 'id'           => $this->id,
607                                 'created'      => $this->created->format('Y-m-d H:i'),
608                                 'created_by'   => $this->createdBy->firstName . ' ' . $this->createdBy->familyName,
609                                 'reg_number'   => $this->stock->registrationNumber,
610                                 'make'         => $this->stock->type->model->make->name,
611                                 'model'        => $this->stock->type->model->name,
612                                 'type'         => $this->stock->type->name,
613                                 'queue_status' => $this->queueStatus
614                 );
615         }
616
617         /**
618          * Convert the object to an array for synchronization.
619          * @return array
620          */
621         public function toSynchArray()
622         {
623                 return array(
624                                 'id'         => $this->id,
625                                 'created'    => $this->created->format(
626                                                 \Utility\Registry::getConfigParam('DateTimeFormat')
627                                                 ),
628                                 'created_by' => $this->createdBy->firstName . ' ' . $this->createdBy->familyName,
629                                 'reg_number' => $this->stock->registrationNumber,
630                                 'make'       => $this->stock->type->model->make->name,
631                                 'model'      => $this->stock->type->model->name,
632                                 'type'       => $this->stock->type->name
633                 );
634         }
635
636         /**
637          * Query to use for synchronization pull.
638          * @return array
639          */
640         public static function getSynchQuery()
641         {
642                 return    'SELECT valuation '
643                                 . 'FROM \Valuation\Entity\Valuation valuation '
644                                 . 'JOIN valuation.valuators valuator '
645                                 . 'WHERE (valuation.queueStatus = 1 AND IDENTITY(valuator.profile) = '
646                                         . \Utility\Registry::getAuthParam('id') . ')'
647                                 . ' OR (valuation.queueStatus = 2 AND IDENTITY(valuation.valuatedBy) = '
648                                         . \Utility\Registry::getAuthParam('id') . ')';
649         }
650
651         /**
652          * Claim job queue item.
653          * @param \Doctrine\ORM\EntityManager $em
654          * @return boolean
655          */
656         public function claimQueueItem(\Doctrine\ORM\EntityManager $em, $profileId = false)
657         {
658                 $profileId = false !== $profileId
659                         ? $profileId
660                         : \Utility\Registry::getAuthParam('id');
661                 if (2 == $this->queueStatus && $profileId != $this->valuatedBy->id)
662                 {
663                         return false;
664                 }
665                 $this->queueStatus = 2;
666                 $this->valuatedBy = $em->getReference(
667                                 '\User\Entity\Profile',
668                                 $profileId
669                                 );
670                 $em->flush();
671                 return true;
672         }
673
674         /**
675          * Unclaim job queue item.
676          * @param \Doctrine\ORM\EntityManager $em
677          * @return boolean
678          */
679         public function unclaimQueueItem(\Doctrine\ORM\EntityManager $em)
680         {
681                 $this->queueStatus = 1;
682                 $this->valuatedBy  = null;
683                 $em->flush();
684                 return true;
685         }
686
687         /**
688          * Complete claimed job queue item.
689          * @param \Doctrine\ORM\EntityManager $em
690          */
691         public function completeQueueItem(\Doctrine\ORM\EntityManager $em)
692         {
693                 $iterator = $this->valuators->getIterator();
694                 foreach ($iterator as $val)
695                 {
696                         $em->remove($val);
697                 }
698                 $em->flush();
699         }
700
701         /**
702          * Populate from an array.
703          * @param array $data
704          */
705         public function fromArray($data = array())
706         {
707                 isset($data['id'])
708                         && $this->id = $data['id'];
709                 isset($data['stock'])
710                         && $this->stock = $data['stock'];
711                 isset($data['xmlRpcClient'])
712                         && $this->xmlRpcClient = $data['xmlRpcClient'];
713                 isset($data['createdBy'])
714                         && $this->createdBy = $data['createdBy'];
715                 isset($data['valuatedBy'])
716                         && $this->valuatedBy = $data['valuatedBy'];
717                 isset($data['salesProfile'])
718                         && $this->salesProfile = $data['salesProfile'];
719                 isset($data['firstName'])
720                         && $this->firstName = $data['firstName'];
721                 isset($data['familyName'])
722                         && $this->familyName = $data['familyName'];
723                 isset($data['idNumber'])
724                         && $this->idNumber = $data['idNumber'];
725                 isset($data['mobile'])
726                         && $this->mobile = $data['mobile'];
727                 isset($data['email'])
728                         && $this->email = $data['email'];
729                 isset($data['department'])
730                         && $this->department = $data['department'];
731                 isset($data['region'])
732                         && $this->region = $data['region'];
733                 isset($data['city'])
734                         && $this->city = $data['city'];
735                 isset($data['street'])
736                         && $this->street = $data['street'];
737                 isset($data['sighted'])
738                         && $this->sighted = $data['sighted'];
739         isset($data['licenseDiscExpired'])
740             && $this->licenseDiscExpired = $data['licenseDiscExpired'];
741                 isset($data['requiredPrice'])
742                         && $this->requiredPrice = $data['requiredPrice'];
743                 isset($data['salesComments'])
744                         && $this->salesComments = $data['salesComments'];
745
746         isset($data['customerComments'])
747         && $this->customerComments = $data['customerComments'];
748
749         isset($data['customerAddressStreet'])
750         && $this->customerAddressStreet = $data['customerAddressStreet'];
751         isset($data['customerAddressStreetName'])
752         && $this->customerAddressStreetName = $data['customerAddressStreetName'];
753         isset($data['customerAddressSuburb'])
754         && $this->customerAddressSuburb = $data['customerAddressSuburb'];
755         isset($data['customerAddressCity'])
756         && $this->customerAddressCity = $data['customerAddressCity'];
757         isset($data['customerAddressPostalCode'])
758         && $this->customerAddressPostalCode = $data['customerAddressPostalCode'];
759
760         isset($data['sendToStockFrom'])
761         && $this->sendToStockFrom = $data['sendToStockFrom'];
762         isset($data['dealNotDoneSelection'])
763         && $this->dealNotDoneSelection = $data['dealNotDoneSelection'];
764         isset($data['dealNotDoneReason'])
765         && $this->dealNotDoneReason = $data['dealNotDoneReason'];
766
767                 isset($data['amountOffered'])
768                         && $this->amountOffered = $data['amountOffered'];
769         isset($data['bankSettlement'])
770         && $this->bankSettlement = $data['bankSettlement'];
771                 isset($data['overAllowance'])
772                         && $this->overAllowance = $data['overAllowance'];
773                 isset($data['standInValue'])
774                         && $this->standInValue = $data['standInValue'];
775                 isset($data['projectedRetail'])
776                         && $this->projectedRetail = $data['projectedRetail'];
777                 isset($data['plannedMargin'])
778                         && $this->plannedMargin = $data['plannedMargin'];
779                 isset($data['queueStatus'])
780                         && $this->queueStatus = $data['queueStatus'];
781                 if (isset($data['valuators']))
782                 {
783                         $em = \Utility\Registry::getEntityManager();
784                         $newVals = array();
785                         foreach ($data['valuators'] as $val)
786                         {
787                                 $newVals[$val['id']] = $val['id'];
788                         }
789                         $currentVals = array();
790                         $iterator = $this->valuators->getIterator();
791                         foreach ($iterator as $val)
792                         {
793                                 $currentVals[$val->id] = $val->id;
794                                 !isset($newVals[$val->id])
795                                         && $em->remove($val);
796                         }
797                         $newVals = array();
798                         foreach ($data['valuators'] as $val)
799                         {
800                                 if (!isset($currentVals[$val['id']]))
801                                 {
802                                         $valuator = new ValuationValuators();
803                                         $valuator->profile = $em->getReference('\User\Entity\Profile', $val['id']);
804                                         $valuator->valuation = $this;
805                                         $this->valuators->add($valuator);
806                                 }
807                         }
808                 }
809         }
810
811
812
813 }
814