stock->priceGuide) ? $valuation->stock->priceGuide->id : false; $record = $this->create(array( 'valuation' => $valuation->id, 'stock' => $valuation->stock->id, 'clubs' => $routingData['clubs'] )); $valuation->stock->priceGuide = $record; $valuation->stock->loadedOnPriceGuide = new \DateTime("now"); $valuation->stock->highestOffer = 0.0; $this->em->flush($valuation); $authData = \Utility\Registry::getAuthData(); $fromCompanyId = isset($authData['company']['id']) ? $authData['company']['id'] : null; $fromProfileId = isset($authData['id']) ? $authData['id'] : null; //$this->sendToMembers($record, $fromCompanyId, $fromProfileId); exec("php " . getcwd() . "/public/index.php priceguide notify " . $record->id . " $fromCompanyId $fromProfileId >>/log/php.log &"); // Send to the public user that his car has been send to price guide // do a check for isPublic if ($valuation->isPublic == true) { #-> Notify send to price guide - for the public user. $params = array( 'firstName' => $valuation->firstName, 'familyName' => $valuation->familyName, 'year' => $valuation->stock->vehicleYear->name, 'make' => $valuation->stock->type->model->make->name, 'model' => $valuation->stock->type->model->name, 'type' => $valuation->stock->type->name, 'regNo' => $valuation->stock->registrationNumber ); //\Utility\Debug::errorLog('send to priceguide from valuations', $params); $oNotify = new \Utility\Comms\Notification(); $oNotify->sendFromTemplate( $fromCompanyId, $fromProfileId, '', '', $valuation->email, null, null, 'new-public-to-priceguide', $params ); } if ($prevListingId) { $acceptedOffer = $this->em->getRepository('\PriceGuide\Entity\Offer') ->findOneBy(array( 'priceGuideStock' => $prevListingId, 'status' => 'Offer Accepted' )); #-> Expire all previous offers. $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.status = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $prevListingId ) ->execute(); if ($acceptedOffer) { #-> Re-create accepted offer as updateable. $newOffer = new \PriceGuide\Entity\Offer(); $newOffer->priceGuideStock = $record; $newOffer->company = $acceptedOffer->company; $newOffer->profile = $acceptedOffer->profile; $newOffer->amount = $acceptedOffer->amount; $newOffer->status = 'Updateable Offer'; $this->em->persist($newOffer); #-> Update stock entry with correct data. $valuation->stock->numberOfOffers = 1; $valuation->stock->highestOffer = $acceptedOffer->amount; #-> Set price guide entry to status updateable offers. $record->previousState = 'Open4Offers'; $record->jobState = 'Updateable Offers'; $this->em->flush(); #-> Notify accepted offer user of the relisting. $params = array( 'firstName' => $acceptedOffer->profile->firstName, 'familyName' => $acceptedOffer->profile->familyName, 'offerAmount' => 'R' . $acceptedOffer->amount, 'year' => $valuation->stock->vehicleYear->name, 'make' => $valuation->stock->type->model->make->name, 'model' => $valuation->stock->type->model->name, 'type' => $valuation->stock->type->name, 'regNo' => $valuation->stock->registrationNumber, 'referenceNumber' => $valuation->stock->referenceNumber ); $oNotify = new \Utility\Comms\Notification(); $oNotify->sendFromTemplate( $fromCompanyId, $fromProfileId, $acceptedOffer->company->id, $acceptedOffer->profile->id, $acceptedOffer->profile->email, null, null, 'price-guide-offer-relist', $params ); } } } /** * New Price Guide Item from existing Stock. * * @param \Stock\Entity\Stock $stock * @param string $previousState * @param array $routingData */ public function initNewItemFromStock( \Stock\Entity\Stock $stock, $previousState, array $routingData = array() ) { $prevListingId = is_object($stock->priceGuide) ? $stock->priceGuide->id : false; $record = $this->create(array( 'stock' => $stock->id, 'clubs' => $routingData['clubs'] )); $stock->priceGuide = $record; $stock->loadedOnPriceGuide = new \DateTime("now"); $stock->highestOffer = 0.0; $this->em->flush($stock); $authData = \Utility\Registry::getAuthData(); $fromCompanyId = isset($authData['company']['id']) ? $authData['company']['id'] : null; $fromProfileId = isset($authData['id']) ? $authData['id'] : null; exec("php /var/www/B4C2/public/index.php priceguide notify " . $record->id . " $fromCompanyId $fromProfileId > /dev/null &"); if ($prevListingId) { $acceptedOffer = $this->em->getRepository('\PriceGuide\Entity\Offer') ->findOneBy(array( 'priceGuideStock' => $prevListingId, 'status' => 'Offer Accepted' )); #-> Expire all previous offers. $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.status = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $prevListingId ) ->execute(); if ($acceptedOffer) { #-> Re-create accepted offer as updateable. $newOffer = new \PriceGuide\Entity\Offer(); $newOffer->priceGuideStock = $record; $newOffer->company = $acceptedOffer->company; $newOffer->profile = $acceptedOffer->profile; $newOffer->amount = $acceptedOffer->amount; $newOffer->status = 'Updateable Offer'; $this->em->persist($newOffer); #-> Update stock entry with correct data. $stock->numberOfOffers = 1; $stock->highestOffer = $acceptedOffer->amount; #-> Set price guide entry to status updateable offers. $record->previousState = 'Open4Offers'; $record->jobState = 'Updateable Offers'; $this->em->flush(); #-> Notify accepted offer user of the relisting. $params = array( 'firstName' => $acceptedOffer->profile->firstName, 'familyName' => $acceptedOffer->profile->familyName, 'offerAmount' => 'R' . $acceptedOffer->amount, 'year' => $stock->vehicleYear->name, 'make' => $stock->type->model->make->name, 'model' => $stock->type->model->name, 'type' => $stock->type->name, 'regNo' => $stock->registrationNumber, 'referenceNumber' => $stock->referenceNumber ); $oNotify = new \Utility\Comms\Notification(); $oNotify->sendFromTemplate( $fromCompanyId, $fromProfileId, $acceptedOffer->company->id, $acceptedOffer->profile->id, $acceptedOffer->profile->email, null, null, 'price-guide-offer-relist', $params ); } } } /** * Route newly added item to initial state. * * @param \PriceGuide\Entity\PriceGuide * @param string|null $previousState * @param array|null $routingData * @return string */ public function routeNewItem(\PriceGuide\Entity\PriceGuide $valuation, $previousState, $routingData) { $status = \Utility\Registry::checkOnce('NewPriceGuideItem.Status'); $status = is_null($status) ? 'This.NewPriceGuide' : $status; if ('This.NewPriceGuide' == $status) { $valuation->queueStatus = 1; } if ('PriceGuide.NewItem' == $status) { $valuation->previousState = 'Pending PriceGuide'; } if ('This.Stock' == $status) { $valuation->previousState = 'Pending PriceGuide'; } return $status; } /** * ExecuteAfter: Create. * Send sms notifications to linked members for relevant clubs. * * @param array $meta * @param object|null $jobRecord * @param object|null $record * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function sendToMembers($record, $fromCompanyId, $fromProfileId) { set_time_limit(0); ini_set('memory_limit','1024M'); //\Utility\Debug::errorLog('pg cron start', $record->toArray()); \Utility\Registry::setOnce('Stock.IgnorePriceGuide', true); $jobRecord = $record; $record = $record->toArray(array('stock', 'clubs', 'type', 'make', 'model', 'company')); $stock = $record['stock']; $clubs = $record['clubs']; $vehicleYear = $stock['vehicleYear']['id']; /* * Set template parameters */ $params = array(); $params['company.name'] = $stock['company']['name']; $params['seller'] = $stock['company']['name']; $params['make.name'] = $stock['type']['model']['make']['name']; $params['model.name'] = $stock['type']['model']['name']; $params['tradeRetail'] = 'Trade: R ' . $stock['tradePrice'] . ', Retail: R ' . $stock['retailPrice']; $templateName = 'club-stock-on-offer'; /* * Get club data for member notifications */ $clubIds = array(); foreach ($clubs as $club) { $clubIds[] = $club['club']['id']; } $query = 'SELECT m, p, make, fromYear, toYear, IDENTITY(p.company) as companyId FROM \PriceGuide\Entity\Member m JOIN m.profile p LEFT JOIN m.makes make LEFT JOIN m.fromYear fromYear LEFT JOIN m.toYear toYear WHERE IDENTITY(m.club) IN (:clubs) AND m.status=\'Active\''; $query = $this->em->createQuery($query); $query->setParameter('clubs', $clubIds); $result = $query->getArrayResult(); $recipientsSentTo = array(); //\Utility\Debug::errorLog('count($result)', count($result)); /* * Cycle through members and send notifications where neccessarry */ for($i = 0; $i < count($result); $i++) { /* * Duplicate notifiction check */ if(!isset($recipientsSentTo[$result[$i]['companyId']])) { /* * Set vehicle makes subscription preferences */ $allMakes = $result[$i][0]['allMakes']; $makes = array(); if(0 == $allMakes) { for($m = 0; $m < count($result[$i][0]['makes']); $m++) { $makes[] = $result[$i][0]['makes'][$m]['id']; } } $inMakes = (1 == $allMakes || in_array($stock['type']['model']['make']['id'], $makes)) ? true : false; /* * Set vehicle year subscription preferences */ $fromYear = $result[$i][0]['fromYear']['id']; $toYear = $result[$i][0]['toYear']['id']; if (!is_null($toYear) && $fromYear > $toYear) { $swap = $fromYear; $fromYear = $toYear; $toYear = $swap; } $inYears = ((null == $fromYear || $vehicleYear >= $fromYear) && (null == $toYear || $vehicleYear <= $toYear)) ? true : false; //\Utility\Debug::errorLog('$inMakes', $inMakes); //\Utility\Debug::errorLog('$inYears', $inYears); /* * Check if recipient is subscribed to vehicle make & year */ if(true == $inMakes && true == $inYears) { $insert = new \PriceGuide\Entity\MemberStock(); $params = array('priceGuideStock' => $jobRecord, 'member' => $this->em->getReference('\PriceGuide\Entity\Member', $result[$i][0]['id'])); $insert->fromArray($params); $this->em->persist($insert); //\Utility\Debug::errorLog('created link', $insert->id); $recipientsSentTo[$result[$i]['companyId']] = true; $params = array( 'company.name' => $record['company']['name'], 'make.name' => $record['stock']['type']['model']['make']['name'], 'model.name' => $record['stock']['type']['model']['name'], 'type.name' => $record['stock']['type']['name'], 'tradeRetail' => 'Trade: R ' . $record['stock']['tradePrice'] . ', Retail: R ' . $record['stock']['retailPrice'], 'seller' => $record['company']['name'], 'link' => '/#/offerview?pgId=' . $record['id'] . '&id=0' ); $subject = null; $company = $this->em->getRepository('Company\Entity\Company') ->find($result[$i]['companyId']); $iterator = $company->profiles->getIterator(); foreach ($iterator as $profile) { //\Utility\Debug::errorLog('a profile', $profile->id); if ($profile->override->pgMakeOffer) { //\Utility\Debug::errorLog('have permissions', 'sending notification'); $params['firstName'] = $profile->firstName; $params['familyName'] = $profile->familyName; $email = true == $result[$i][0]['emailNotification'] ? $profile->email : null; $mobile = true == $result[$i][0]['smsNotification'] ? $profile->mobile : null; if(null != $email || null != $mobile) { $oNotify = new \Utility\Comms\Notification(); $oNotify->sendFromTemplate( $fromCompanyId, $fromProfileId, $result[$i]['companyId'], $profile->id, $email, $mobile, $subject, $templateName, $params); } } } } } } /* * Commit inserts */ $this->em->flush(); } /** * ExecuteAfter: CreatePending, CreateComplete, CreatePriceGuide, CreateStock. * Set the valuator from session authentication data. * * @param array $meta * @param object|null $jobRecord * @param object|null $record * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function setValuatorFromAuth($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract) { $jobRecord->valuatedBy = $this->em->getReference( '\User\Entity\Profile', \Utility\Registry::getAuthParam('id') ); $this->em->flush(); } /** * ExecuteAfter: CreateComplete, RouteMoveToComplete. * * @param array $meta * @param object|null $jobRecord * @param object|null $record * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function setPreviousStateToComplete($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract) { $jobRecord->previousState = 'Complete PriceGuide'; $this->em->flush($jobRecord); } /** * ConditionalContract: Update. * * @param array $meta * @param object|null $jobRecord * @param object|null $record * @param \Workspace\Contract\AbstractBase $contract * @return array */ public function noEditOnPgItemWithOffers($meta, $jobRecord, $record, \Workspace\Contract\AbstractBase $contract) { if ('Price Guide' == $jobRecord->jobState && 0 < $jobRecord->stock->numberOfOffers) { throw new \Exception('PriceGuide in status Price Guide with offers may not be edited.'); } } /** * ConditionalContract: Update. * * @param array $meta * @param object|null $jobRecord * @param object|null $record * @param \Workspace\Contract\AbstractBase $contract * @return array */ public function noSendToSalesWithoutValuation($meta, $jobRecord, $record, \Workspace\Contract\AbstractBase $contract) { if (is_null($jobRecord->valuation)) { throw new \Exception('No valuation connected to this entry, cannot send to sales.'); } } /** * Contract to add company to all traders clubs. * @param object|null $jobRecord * @param array $input * @return \Workspace\Contract\UseOnce */ public function contractAddToTradeClub($jobRecord, array $input = array()) { $options = new \Workspace\UseCase\Options(); $requirement = new \Workspace\UseCase\Requirement(); $requirement->addRequiredInput(array( 'Company' => array( 'id' => 'Id' ) )); return new \Workspace\Contract\UseOnce($options, $requirement); } /** * Add company to all traders clubs * @param object|null $jobRecord * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function executeAddToTradeClub($jobRecord, \Workspace\Utility\ServiceInputParams $contract) { #-> Retrieve relevant company, profile and allowed club member to work with. $em = \Utility\Registry::getEntityManager(); $company = $em->getRepository('\Company\Entity\Company') ->find($contract->data->Company['id']); if (is_null($company)) { return $contract->error('Failure.', 'Could not find company.'); } if (1 == $company->group->id) { return $contract->error('Failure.', 'Imperial dealer, cannot add as trader.'); } $profile = $em->getRepository('\User\Entity\Profile') ->findOneBy(array('company' => $contract->data->Company['id']), array('id' => 'ASC')); if (is_null($profile)) { return $contract->error('Failure.', 'Could not find primary profile.'); } $allowedMember = $em->getRepository('\PriceGuide\Entity\AllowedMember') ->findOneBy(array('email' => $profile->email)); if (is_null($allowedMember)) { $allowedMember = new \PriceGuide\Entity\AllowedMember(); $allowedMember->name = $company->name; $allowedMember->email = $profile->email; $em->persist($allowedMember); $em->flush(); } #-> Find clubs we need to work with. $query = 'SELECT club, company ' . 'FROM \PriceGuide\Entity\Club club ' . 'JOIN club.company company ' . 'WHERE IDENTITY(company.group) = 1 ' . 'ORDER BY company.id, club.id ASC'; $query = $this->em->createQuery($query); $result = $query->getArrayResult(); $comp = 0; $club = 0; $clubs = array(); #-> Add new member to clubs. foreach ($result as $row) { if ($comp != $row['company']['id']) { $comp = $row['company']['id']; $club = 1; continue; } if (1 == $club) { $clubs[$row['id']] = $row['name']; $member = new \PriceGuide\Entity\Member(); $member->club = $em->getReference('\PriceGuide\Entity\Club', $row['id']); $member->company = $company; $member->profile = $profile; $member->allowedMember = $allowedMember; $member->allMakes = true; $member->status = 'Active'; $em->persist($member); $em->flush(); } $club++; } return $contract->success('Trader added to all trade clubs.', array()); } /** * Contract to add company to all traders clubs. * @param object|null $jobRecord * @param array $input * @return \Workspace\Contract\UseOnce */ public function contractSetupImperialClubs($jobRecord, array $input = array()) { $options = new \Workspace\UseCase\Options(); $requirement = new \Workspace\UseCase\Requirement(); $requirement->addRequiredInput(array( 'Company' => array( 'id' => 'Id' ) )); return new \Workspace\Contract\UseOnce($options, $requirement); } /** * Add company to all traders clubs * @param object|null $jobRecord * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function executeSetupImperialClubs($jobRecord, \Workspace\Utility\ServiceInputParams $contract) { #-> Retrieve relevant company, profile and allowed club member to work with. $em = \Utility\Registry::getEntityManager(); $company = $em->getRepository('\Company\Entity\Company') ->find($contract->data->Company['id']); if (is_null($company)) { return $contract->error('Failure.', 'Could not find company.'); } $profile = $em->getRepository('\User\Entity\Profile') ->findOneBy(array('company' => $contract->data->Company['id']), array('id' => 'ASC')); if (is_null($profile)) { return $contract->error('Failure.', 'Could not find primary profile.'); } #-> Create memberships for this company in other internal clubs. $query = 'SELECT club, company ' . 'FROM \PriceGuide\Entity\Club club ' . 'JOIN club.company company ' . 'WHERE IDENTITY(company.groupDivision) = :groupDivision ' . 'ORDER BY company.id, club.id ASC'; $query = $this->em->createQuery($query) ->setParameter('groupDivision', $company->groupDivision->id); $result = $query->getArrayResult(); $comp = 0; #-> Add this company to internal clubs from same division. foreach ($result as $row) { if ($comp != $row['company']['id']) { $comp = $row['company']['id']; if ($row['company']['id'] == $company->id) { continue; } $member = $em->getRepository('\PriceGuide\Entity\Member') ->findOneBy(array( 'club' => $row['id'], 'company' => $company->id )); if (is_object($member)) { continue; } $member = new \PriceGuide\Entity\Member(); $member->club = $em->getReference('\PriceGuide\Entity\Club', $row['id']); $member->company = $company; $member->profile = $profile; $member->allMakes = true; $member->status = 'Active'; $em->persist($member); $em->flush(); } } #-> Create internal club. $club = $em->getRepository('\PriceGuide\Entity\Club') ->findOneBy( array('company' => $company->id), array('id' => 'ASC') ); if (!is_object($club)) { $club = new \PriceGuide\Entity\Club(); $club->company = $company; $club->name = $company->name . ' Internal Club - IAR'; $club->useAsDefault = true; $em->persist($club); $em->flush(); } #-> Link companies from same division. $companies = $em->getRepository('\Company\Entity\Company') ->findBy(array('groupDivision' => $company->groupDivision->id)); foreach ($companies as $cmpMember) { if ($cmpMember->id == $company->id) { continue; } $member = $em->getRepository('\PriceGuide\Entity\Member') ->findOneBy(array( 'club' => $club->id, 'company' => $cmpMember->id )); if (is_object($member)) { continue; } $profile = $em->getRepository('\User\Entity\Profile') ->findOneBy(array('company' => $cmpMember->id), array('id' => 'ASC')); if (!is_object($profile)) { continue; } $member = new \PriceGuide\Entity\Member(); $member->club = $club; $member->company = $cmpMember; $member->profile = $profile; $member->allMakes = true; $member->status = 'Active'; $em->persist($member); $em->flush(); } #-> Create traders club. $clubs = $em->getRepository('\PriceGuide\Entity\Club') ->findBy( array('company' => $company->id), array('id' => 'ASC') ); if (!empty($clubs) && count($clubs) > 1) { array_shift($clubs); $club = array_shift($clubs); } else { $club = new \PriceGuide\Entity\Club(); $club->company = $company; $club->name = $company->name . ' Trader Club - IAR Approved'; $club->useAsDefault = false; $em->persist($club); $em->flush(); } #-> Link traders to club. $companies = array(); $allowedMembers = $em->getRepository('\PriceGuide\Entity\AllowedMember') ->findBy(array('archived' => 0)); foreach ($allowedMembers as $allowedMember) { $profile = $em->getRepository('\User\Entity\Profile') ->findOneBy(array('email' => $allowedMember->email)); if (!is_object($profile)) { continue; } if (isset($companies[$profile->company->id]) || 1 != $profile->company->group->id) { continue; } $member = $em->getRepository('\PriceGuide\Entity\Member') ->findOneBy(array( 'club' => $club->id, 'company' => $profile->company->id )); if (is_object($member)) { continue; } $companies[$profile->company->id] = true; $member = new \PriceGuide\Entity\Member(); $member->club = $club; $member->company = $profile->company; $member->profile = $profile; $member->allMakes = true; $member->status = 'Active'; $em->persist($member); $em->flush(); } return $contract->success('Added clubs, members and memberships.', array()); } /** * Contract to remove item from price guide. * @param object|null $jobRecord * @param array $input * @return \Workspace\Contract\UseOnce */ public function contractRemove($jobRecord, array $input = array()) { $options = new \Workspace\UseCase\Options(); $requirement = new \Workspace\UseCase\Requirement(); return new \Workspace\Contract\UseOnce($options, $requirement); } /** * Remove item from price guide. * @param object|null $jobRecord * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function executeRemove($jobRecord, \Workspace\Utility\ServiceInputParams $contract) { //\Utility\Debug::errorLog('jobRecord', $jobRecord->toArray()); try { if ($jobRecord->valuation) { $workflow = \Utility\Registry::getServiceManager() ->get('Valuation'); $workflow->handover('Price Guide', $jobRecord->valuation->id); } else { $workflow = \Utility\Registry::getServiceManager() ->get('Stock'); $workflow->handover('Price Guide', $jobRecord->stock->id); } } catch (\Exception $e) { \Utility\Debug::errorLog( 'Error on Price Guide single removal process for item ' . $jobRecord->id, $e->getMessage() ); } $this->workflowNode->changeState('This.ValidOffers'); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.status = 'Valid Offer' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $jobRecord->id . ' AND o.status != \'Archived\'' ) ->execute(); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.previousStatus = 'Valid Offer' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $jobRecord->id . ' AND o.status = \'Archived\'' ) ->execute(); return $contract->success('Vehicle removed from auction.', array()); } /** * Contract to remove item from price guide and expire. * @param object|null $jobRecord * @param array $input * @return \Workspace\Contract\UseOnce */ public function contractExpire($jobRecord, array $input = array()) { $options = new \Workspace\UseCase\Options(); $requirement = new \Workspace\UseCase\Requirement(); return new \Workspace\Contract\UseOnce($options, $requirement); } /** * Remove item from price guide and expire. * @param object|null $jobRecord * @param \Workspace\Utility\ServiceInputParams $contract * @return array */ public function executeExpire($jobRecord, \Workspace\Utility\ServiceInputParams $contract) { try { if ($jobRecord->valuation) { $workflow = \Utility\Registry::getServiceManager() ->get('Valuation'); $workflow->handover('Price Guide', $jobRecord->valuation->id); } else { $workflow = \Utility\Registry::getServiceManager() ->get('Stock'); $workflow->handover('Price Guide', $jobRecord->stock->id); } } catch (\Exception $e) { \Utility\Debug::errorLog( 'Error on Price Guide single removal process for item ' . $jobRecord->id, $e->getMessage() ); } $this->workflowNode->changeState('This.Archived'); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.status = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $jobRecord->id . ' AND o.status != \'Archived\'' ) ->execute(); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.previousStatus = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $jobRecord->id . ' AND o.status = \'Archived\'' ) ->execute(); return $contract->success('Vehicle removed from auction.', array()); } /** * CRON functionality: Move PriceGuide item and related Offers to ValidOffer(s) status. * @param arary $meta * @param \PriceGuide\Entity\PriceGuide $record * @param \Workspace\Contract\AbstractBase $contract */ public function cronProcess() { #-> Phase 1. $days = \Utility\Definitions\Locale::getPriceGuideOpenDays(); $date = date('Y-m-d H:i:s', time() - (3600 * $days)); $result = $this->em->createQuery( 'SELECT priceGuide, stock, vehicleYear, type, model, make, valuation ' . 'FROM \PriceGuide\Entity\PriceGuide priceGuide ' . 'JOIN priceGuide.stock stock ' . 'JOIN stock.vehicleYear vehicleYear ' . 'JOIN stock.type type ' . 'JOIN type.model model ' . 'JOIN model.make make ' . 'LEFT JOIN priceGuide.valuation valuation ' . 'WHERE (priceGuide.jobState = \'Open4Offers\' OR priceGuide.jobState = \'Updateable Offers\')' . ' AND priceGuide.created <= \'' . $date . '\'' ) ->getArrayResult(); foreach ($result as $item) { try { if (!is_null($item['valuation']['id'])) { $workflow = \Utility\Registry::getServiceManager() ->get('Valuation'); $workflow->handover('Price Guide', $item['valuation']['id']); } else { $workflow = \Utility\Registry::getServiceManager() ->get('Stock'); $workflow->handover('Price Guide', $item['stock']['id']); } } catch (\Exception $e) { \Utility\Debug::errorLog('Error on PriceGuide cron process for item ' . $item['id'], $e->getMessage()); } try { #-> Set all related offers to Valid Offer status. $this->workflowNode->loadJob($item['id']); if ('Updateable Offers' == $item['jobState']) { $this->workflowNode->changeState('This.ValidOffers'); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.status = 'Valid Offer' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $item['id'] . ' AND o.status != \'Archived\'' ) ->execute(); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.previousStatus = 'Valid Offer' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $item['id'] . ' AND o.status = \'Archived\'' ) ->execute(); $resultx = $this->em->createQuery( 'SELECT offer, company, profile ' . 'FROM \PriceGuide\Entity\Offer offer ' . 'JOIN offer.company company ' . 'JOIN offer.profile profile ' . 'WHERE IDENTITY(offer.priceGuideStock) = :priceGuideId' . ' AND offer.amount = :highestOffer' ) ->setParameter('priceGuideId', $item['id']) ->setParameter('highestOffer', $item['stock']['highestOffer']) ->getArrayResult(); $earliestDate = null; $earliestId = 0; foreach($resultx as $id => $itemx) { if (0 == $earliestId) { $earliestId = $id; $earliestDate = $itemx['created']->getTimestamp(); } elseif ($itemx['created']->getTimestamp() < $earliestDate) { $earliestId = $id; $earliestDate = $itemx['created']->getTimestamp(); } } foreach($resultx as $id => $itemx) { if ($earliestId != $id) { continue; } /* $oNotify = new \Utility\Comms\Notification(); $oNotify->sendFromTemplate( null, null, '1', $itemx['profile']['id'], $itemx['profile']['email'], null, 'Confirmation of Highest Offer on Price Guide', 'highest-price-guide', array( 'firstName' => $itemx['profile']['firstName'], 'familyName' => $itemx['profile']['familyName'], 'highest_offer_amount' => 'R' . $itemx['amount'], 'Dealership_Name' => $itemx['company']['name'], 'year' => $item['stock']['vehicleYear']['name'], 'make' => $item['stock']['type']['model']['make']['name'], 'model' => $item['stock']['type']['model']['name'], 'Trade' => 'Trade price: R' . $item['stock']['tradePrice'], 'Retail' => 'Retail price: R' . $item['stock']['retailPrice'], 'registration_number' => $item['stock']['registrationNumber'] )); */ } } else { $this->workflowNode->changeState('This.Archived'); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.status = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $item['id'] . ' AND o.status != \'Archived\'' ) ->execute(); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.previousStatus = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $item['id'] . ' AND o.status = \'Archived\'' ) ->execute(); } } catch (\Exception $e) { echo $e->getMessage(); \Utility\Debug::errorLog('Error on PriceGuide cron process for item ' . $item['id'], $e->getMessage()); } } #-> Phase 2. $days += \Utility\Definitions\Locale::getPriceGuideCompletionDays(); $date = date('Y-m-d H:i:s', time() - (86400 * $days)); $result = $this->em->createQuery( 'SELECT priceGuide, stock, valuation FROM \PriceGuide\Entity\PriceGuide priceGuide ' . 'JOIN priceGuide.stock stock LEFT JOIN priceGuide.valuation valuation ' . 'WHERE (priceGuide.jobState = \'Valid Offers\')' . ' AND priceGuide.created <= \'' . $date . '\'' ) ->getArrayResult(); foreach ($result as $item) { try { #-> Set all related offers to Expired status. $this->workflowNode->loadJob($item['id']); $this->workflowNode->changeState('This.Archived'); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.status = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $item['id'] . ' AND o.status != \'Archived\'' ) ->execute(); $this->em->createQuery( 'UPDATE \PriceGuide\Entity\Offer o ' . "SET o.previousStatus = 'Expired' " . 'WHERE IDENTITY(o.priceGuideStock) = ' . $item['id'] . ' AND o.status = \'Archived\'' ) ->execute(); } catch (\Exception $e) { \Utility\Debug::errorLog('Error on PriceGuide cron process for item ' . $item['id'], $e->getMessage()); } } } }