private static function newPriceNotification($stock, $purchaseDetail, $con) { // check price change $priceDifference = $stock->getBuy() - $purchaseDetail->getTotalPrice() / $purchaseDetail->getAmount(); if ($priceDifference == 0) { $priceStatus = 'stagnant'; } elseif ($priceDifference < 0) { $priceStatus = 'up'; } elseif ($priceDifference > 0) { $priceStatus = 'down'; } // if price is not stagnant then make new notification if ($priceStatus != 'stagnant') { // build up notification data $notificationData = new \stdClass(); $notificationData->stock_id = $stock->getId(); $notificationData->status = $priceStatus; $notificationData->difference = abs($priceDifference); $notificationData->to_price = $stock->getBuy() - $priceDifference; // update stock buy price $stock->setBuy($notificationData->to_price)->save($con); // check whether price notification for this purchase detail is already there $oldNotification = $purchaseDetail->getNotification(); if ($oldNotification) { // if yes, assign the old one $isNew = false; $notification = $oldNotification; } else { // if not, create new notification $isNew = true; $notification = new Notification(); } $notification->setTime(time())->setType('price')->setData(json_encode($notificationData))->save($con); // if notification is new, then give notification to user // if not, then update notification's status to unread if ($isNew == true) { // find which role to send notification $roles = NotificationOptionQuery::create()->filterByType('price')->find($con); // iterate through each role to find users assigned to it foreach ($roles as $role) { $users = UserQuery::create()->filterByStatus('Active')->filterByRoleId($role->getRoleId())->find($con); // iterate through each user to give notification foreach ($users as $user) { $notifyUser = new NotificationOnUser(); $notifyUser->setUserId($user->getId())->setNotificationId($notification->getId())->save($con); } } } else { $notifyUsers = NotificationOnUserQuery::create()->filterByNotificationId($notification->getId())->find($con); foreach ($notifyUsers as $notifyUser) { $notifyUser->setStatus('Unread')->save($con); } } $notificationId = $notification->getId(); } else { $notificationId = 0; } return $notificationId; }
/** * Filter the query by a related \ORM\Notification object * * @param \ORM\Notification|ObjectCollection $notification The related object(s) to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return ChildPurchaseDetailQuery The current query, for fluid interface */ public function filterByNotification($notification, $comparison = null) { if ($notification instanceof \ORM\Notification) { return $this->addUsingAlias(PurchaseDetailTableMap::COL_NOTIFICATION_ID, $notification->getId(), $comparison); } elseif ($notification instanceof ObjectCollection) { if (null === $comparison) { $comparison = Criteria::IN; } return $this->addUsingAlias(PurchaseDetailTableMap::COL_NOTIFICATION_ID, $notification->toKeyValue('PrimaryKey', 'Id'), $comparison); } else { throw new PropelException('filterByNotification() only accepts arguments of type \\ORM\\Notification or Collection'); } }
/** * Declares an association between this object and a ChildNotification object. * * @param ChildNotification $v * @return $this|\ORM\PurchaseDetail The current object (for fluent API support) * @throws PropelException */ public function setNotification(ChildNotification $v = null) { if ($v === null) { $this->setNotificationId(NULL); } else { $this->setNotificationId($v->getId()); } $this->aNotification = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the ChildNotification object, it will not be re-added. if ($v !== null) { $v->addPurchaseDetail($this); } return $this; }
/** * Exclude object from result * * @param ChildNotification $notification Object to remove from the list of results * * @return $this|ChildNotificationQuery The current query, for fluid interface */ public function prune($notification = null) { if ($notification) { $this->addUsingAlias(NotificationTableMap::COL_ID, $notification->getId(), Criteria::NOT_EQUAL); } return $this; }