valuators = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Add a new Valuator to this Stock Valuation Item. * @param \User\Entity\Profile $profile * @return \Valuation\Entity\Valuation */ public function addValuator(\User\Entity\Profile $profile) { $this->valuators[] = $profile; return $this; } /** * Magic getter to expose protected properties. * * @param string $property * @return mixed */ public function __get($property) { return $this->$property; } /** * Magic setter to save protected properties. * * @param string $property * @param mixed $value */ public function __set($property, $value) { $this->$property = $value; } /** * @ORM\PrePersist */ public function setCreateTime() { if (!\Utility\Registry::get('IsDeviceApiCall', false)) { $this->createdBy = \Utility\Registry::isAuthenticated() ? \Utility\Registry::resolveProfileContext($this->createdBy) : \Utility\Registry::getEntityManager() ->getReference('\User\Entity\Profile', 1); } $this->created = new \DateTime("now"); } /** * @ORM\PreUpdate */ public function setUpdateTime() { $this->updated = new \DateTime("now"); ('New Valuation' != $this->jobState) && 0 < $this->queueStatus && $this->queueStatus = 0; if ('Price Guide' == $this->jobState && is_null($this->stock->loadedOnPriceGuide)) { $this->stock->loadedOnPriceGuide = new \DateTime("now"); } if (!is_null($this->salesProfile) && is_null($this->sentToSales)) { $this->sentToSales = new \DateTime("now"); } } /** * Automatically called from DataBin if HAVE_POST_INSERT constant is set on entity. * @return boolean */ public function postInsert() { if (is_null($this->id) || !is_numeric($this->id)) { return false; } $this->valuationNumber = 'V' . str_pad($this->id, 7, '0', STR_PAD_LEFT); return true; } /** * Convert the object to an array. * @param array $expand * @param array $intersect * @param boolean $showIdentifiers * @param integer $expandAll * @return array */ public function toArray( array $expand = array(), array $intersect = array(), $showIdentifiers = false, $expandAll = 0 ) { $intersect = array_flip($intersect); $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat'); $includeAll = empty($intersect); $data = array(); ($includeAll || isset($intersect['id'])) && $data['id'] = $this->id; ($includeAll || isset($intersect['valuationNumber'])) && $data['valuationNumber'] = $this->valuationNumber; ($includeAll || isset($intersect['stock'])) && $data['stock'] = (in_array('stock', $expand) || $expandAll || $showIdentifiers) && !is_null($this->stock) ? (!$showIdentifiers || in_array('stock', $expand) ? $this->stock->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->stock->id) : null; ($includeAll || isset($intersect['xmlRpcClient'])) && $data['xmlRpcClient'] = (in_array('xmlRpcClient', $expand) || $expandAll || $showIdentifiers) && !is_null($this->xmlRpcClient) ? (!$showIdentifiers || in_array('xmlRpcClient', $expand) ? $this->xmlRpcClient->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->xmlRpcClient->id) : null; ($includeAll || isset($intersect['createdBy'])) && $data['createdBy'] = (in_array('createdBy', $expand) || $expandAll || $showIdentifiers) && !is_null($this->createdBy) ? (!$showIdentifiers || in_array('createdBy', $expand) ? $this->createdBy->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->createdBy->id) : null; ($includeAll || isset($intersect['valuatedBy'])) && $data['valuatedBy'] = (in_array('valuatedBy', $expand) || $expandAll || $showIdentifiers) && !is_null($this->valuatedBy) ? (!$showIdentifiers || in_array('valuatedBy', $expand) ? $this->valuatedBy->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->valuatedBy->id) : null; ($includeAll || isset($intersect['valuators'])) && $data['valuators'] = (in_array('valuators', $expand) || $expandAll || $showIdentifiers) && !is_null($this->valuators) ? $this->valuatorsToArray($showIdentifiers) : null; ($includeAll || isset($intersect['salesProfile'])) && $data['salesProfile'] = (in_array('salesProfile', $expand) || $expandAll || $showIdentifiers) && !is_null($this->salesProfile) ? (!$showIdentifiers || in_array('salesProfile', $expand) ? $this->salesProfile->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->salesProfile->id) : null; ($includeAll || isset($intersect['managerProfile'])) && $data['managerProfile'] = (in_array('managerProfile', $expand) || $expandAll || $showIdentifiers) && !is_null($this->managerProfile) ? (!$showIdentifiers || in_array('managerProfile', $expand) ? $this->managerProfile->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->managerProfile->id) : null; ($includeAll || isset($intersect['firstName'])) && $data['firstName'] = $this->firstName; ($includeAll || isset($intersect['familyName'])) && $data['familyName'] = $this->familyName; ($includeAll || isset($intersect['idNumber'])) && $data['idNumber'] = $this->idNumber; ($includeAll || isset($intersect['mobile'])) && $data['mobile'] = $this->mobile; ($includeAll || isset($intersect['email'])) && $data['email'] = $this->email; ($includeAll || isset($intersect['department'])) && $data['department'] = $this->department; ($includeAll || isset($intersect['region'])) && $data['region'] = (in_array('region', $expand) || $expandAll || $showIdentifiers) && !is_null($this->region) ? (!$showIdentifiers || in_array('region', $expand) ? $this->region->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->region->id) : null; ($includeAll || isset($intersect['city'])) && $data['city'] = (in_array('city', $expand) || $expandAll || $showIdentifiers) && !is_null($this->city) ? (!$showIdentifiers || in_array('city', $expand) ? $this->city->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->city->id) : null; ($includeAll || isset($intersect['street'])) && $data['street'] = $this->street; ($includeAll || isset($intersect['sighted'])) && $data['sighted'] = $this->sighted; ($includeAll || isset($intersect['licenseDiscExpired'])) && $data['licenseDiscExpired'] = $this->licenseDiscExpired; ($includeAll || isset($intersect['requiredPrice'])) && $data['requiredPrice'] = $this->requiredPrice; ($includeAll || isset($intersect['amountOffered'])) && $data['amountOffered'] = $this->amountOffered; ($includeAll || isset($intersect['bankSettlement'])) && $data['bankSettlement'] = $this->bankSettlement; ($includeAll || isset($intersect['salesComments'])) && $data['salesComments'] = $this->salesComments; ($includeAll || isset($intersect['customerComments'])) && $data['customerComments'] = $this->customerComments; ($includeAll || isset($intersect['customerAddressStreet'])) && $data['customerAddressStreet'] = $this->customerAddressStreet; ($includeAll || isset($intersect['customerAddressStreetName'])) && $data['customerAddressStreetName'] = $this->customerAddressStreetName; ($includeAll || isset($intersect['customerAddressSuburb'])) && $data['customerAddressSuburb'] = $this->customerAddressSuburb; ($includeAll || isset($intersect['customerAddressCity'])) && $data['customerAddressCity'] = $this->customerAddressCity; ($includeAll || isset($intersect['customerAddressPostalCode'])) && $data['customerAddressPostalCode'] = $this->customerAddressPostalCode; ($includeAll || isset($intersect['sendToStockFrom'])) && $data['sendToStockFrom'] = (in_array('sendToStockFrom', $expand) || $expandAll || $showIdentifiers) && !is_null($this->sendToStockFrom) ? (!$showIdentifiers || in_array('sendToStockFrom', $expand) ? $this->sendToStockFrom->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->sendToStockFrom->id) : null; ($includeAll || isset($intersect['dealNotDoneSelection'])) && $data['dealNotDoneSelection'] = (in_array('dealNotDoneSelection', $expand) || $expandAll || $showIdentifiers) && !is_null($this->dealNotDoneSelection) ? (!$showIdentifiers || in_array('dealNotDoneSelection', $expand) ? $this->dealNotDoneSelection->toArray( $expand, $intersect, $showIdentifiers, ($expandAll - 1) ) : $this->dealNotDoneSelection->id) : null; ($includeAll || isset($intersect['dealNotDoneReason'])) && $data['dealNotDoneReason'] = $this->dealNotDoneReason; ($includeAll || isset($intersect['overAllowance'])) && $data['overAllowance'] = $this->overAllowance; ($includeAll || isset($intersect['standInValue'])) && $data['standInValue'] = $this->standInValue; ($includeAll || isset($intersect['projectedRetail'])) && $data['projectedRetail'] = $this->projectedRetail; ($includeAll || isset($intersect['plannedMargin'])) && $data['plannedMargin'] = $this->plannedMargin; ($includeAll || isset($intersect['queueStatus'])) && $data['queueStatus'] = $this->queueStatus; ($includeAll || isset($intersect['jobState'])) && $data['jobState'] = $this->jobState; ($includeAll || isset($intersect['previousState'])) && $data['previousState'] = $this->previousState; ($includeAll || isset($intersect['isPublicCustomer'])) && $data['isPublicCustomer'] = $this->isPublicCustomer; ($includeAll || isset($intersect['created'])) && $data['created'] = !is_null($this->created) ? $this->created->format($dateTimeFormat) : null; ($includeAll || isset($intersect['updated'])) && $data['updated'] = !is_null($this->updated) ? $this->updated->format($dateTimeFormat) : null; return $data; } /** * Internal utility to change valuators collection into array. * @param boolean $showIdentifiers * @return array */ protected function valuatorsToArray($showIdentifiers = false) { $data = array(); $iterator = $this->valuators->getIterator(); foreach ($iterator as $valuator) { $data[] = $showIdentifiers ? array('id' => $valuator->profile->id) : $valuator->toArray(); } return $data; } /** * Convert the object to an array for queue synchronization. * @return array */ public function toQueueArray() { return array( 'id' => $this->id, 'created' => $this->created->format('Y-m-d H:i'), 'created_by' => $this->createdBy->firstName . ' ' . $this->createdBy->familyName, 'reg_number' => $this->stock->registrationNumber, 'make' => $this->stock->type->model->make->name, 'model' => $this->stock->type->model->name, 'type' => $this->stock->type->name, 'queue_status' => $this->queueStatus ); } /** * Convert the object to an array for synchronization. * @return array */ public function toSynchArray() { return array( 'id' => $this->id, 'created' => $this->created->format( \Utility\Registry::getConfigParam('DateTimeFormat') ), 'created_by' => $this->createdBy->firstName . ' ' . $this->createdBy->familyName, 'reg_number' => $this->stock->registrationNumber, 'make' => $this->stock->type->model->make->name, 'model' => $this->stock->type->model->name, 'type' => $this->stock->type->name ); } /** * Query to use for synchronization pull. * @return array */ public static function getSynchQuery() { return 'SELECT valuation ' . 'FROM \Valuation\Entity\Valuation valuation ' . 'JOIN valuation.valuators valuator ' . 'WHERE (valuation.queueStatus = 1 AND IDENTITY(valuator.profile) = ' . \Utility\Registry::getAuthParam('id') . ')' . ' OR (valuation.queueStatus = 2 AND IDENTITY(valuation.valuatedBy) = ' . \Utility\Registry::getAuthParam('id') . ')'; } /** * Claim job queue item. * @param \Doctrine\ORM\EntityManager $em * @return boolean */ public function claimQueueItem(\Doctrine\ORM\EntityManager $em, $profileId = false) { $profileId = false !== $profileId ? $profileId : \Utility\Registry::getAuthParam('id'); if (2 == $this->queueStatus && $profileId != $this->valuatedBy->id) { return false; } $this->queueStatus = 2; $this->valuatedBy = $em->getReference( '\User\Entity\Profile', $profileId ); $em->flush(); return true; } /** * Unclaim job queue item. * @param \Doctrine\ORM\EntityManager $em * @return boolean */ public function unclaimQueueItem(\Doctrine\ORM\EntityManager $em) { $this->queueStatus = 1; $this->valuatedBy = null; $em->flush(); return true; } /** * Complete claimed job queue item. * @param \Doctrine\ORM\EntityManager $em */ public function completeQueueItem(\Doctrine\ORM\EntityManager $em) { $iterator = $this->valuators->getIterator(); foreach ($iterator as $val) { $em->remove($val); } $em->flush(); } /** * Populate from an array. * @param array $data */ public function fromArray($data = array()) { isset($data['id']) && $this->id = $data['id']; isset($data['stock']) && $this->stock = $data['stock']; isset($data['xmlRpcClient']) && $this->xmlRpcClient = $data['xmlRpcClient']; isset($data['createdBy']) && $this->createdBy = $data['createdBy']; isset($data['valuatedBy']) && $this->valuatedBy = $data['valuatedBy']; isset($data['salesProfile']) && $this->salesProfile = $data['salesProfile']; isset($data['firstName']) && $this->firstName = $data['firstName']; isset($data['familyName']) && $this->familyName = $data['familyName']; isset($data['idNumber']) && $this->idNumber = $data['idNumber']; isset($data['mobile']) && $this->mobile = $data['mobile']; isset($data['email']) && $this->email = $data['email']; isset($data['department']) && $this->department = $data['department']; isset($data['region']) && $this->region = $data['region']; isset($data['city']) && $this->city = $data['city']; isset($data['street']) && $this->street = $data['street']; isset($data['sighted']) && $this->sighted = $data['sighted']; isset($data['licenseDiscExpired']) && $this->licenseDiscExpired = $data['licenseDiscExpired']; isset($data['requiredPrice']) && $this->requiredPrice = $data['requiredPrice']; isset($data['salesComments']) && $this->salesComments = $data['salesComments']; isset($data['customerComments']) && $this->customerComments = $data['customerComments']; isset($data['customerAddressStreet']) && $this->customerAddressStreet = $data['customerAddressStreet']; isset($data['customerAddressStreetName']) && $this->customerAddressStreetName = $data['customerAddressStreetName']; isset($data['customerAddressSuburb']) && $this->customerAddressSuburb = $data['customerAddressSuburb']; isset($data['customerAddressCity']) && $this->customerAddressCity = $data['customerAddressCity']; isset($data['customerAddressPostalCode']) && $this->customerAddressPostalCode = $data['customerAddressPostalCode']; isset($data['sendToStockFrom']) && $this->sendToStockFrom = $data['sendToStockFrom']; isset($data['dealNotDoneSelection']) && $this->dealNotDoneSelection = $data['dealNotDoneSelection']; isset($data['dealNotDoneReason']) && $this->dealNotDoneReason = $data['dealNotDoneReason']; isset($data['amountOffered']) && $this->amountOffered = $data['amountOffered']; isset($data['bankSettlement']) && $this->bankSettlement = $data['bankSettlement']; isset($data['overAllowance']) && $this->overAllowance = $data['overAllowance']; isset($data['standInValue']) && $this->standInValue = $data['standInValue']; isset($data['projectedRetail']) && $this->projectedRetail = $data['projectedRetail']; isset($data['plannedMargin']) && $this->plannedMargin = $data['plannedMargin']; isset($data['queueStatus']) && $this->queueStatus = $data['queueStatus']; if (isset($data['valuators'])) { $em = \Utility\Registry::getEntityManager(); $newVals = array(); foreach ($data['valuators'] as $val) { $newVals[$val['id']] = $val['id']; } $currentVals = array(); $iterator = $this->valuators->getIterator(); foreach ($iterator as $val) { $currentVals[$val->id] = $val->id; !isset($newVals[$val->id]) && $em->remove($val); } $newVals = array(); foreach ($data['valuators'] as $val) { if (!isset($currentVals[$val['id']])) { $valuator = new ValuationValuators(); $valuator->profile = $em->getReference('\User\Entity\Profile', $val['id']); $valuator->valuation = $this; $this->valuators->add($valuator); } } } } }