Example #1
0
 public static function read($params, $currentUser, $con)
 {
     $page = isset($params->page) ? $params->page : 0;
     $limit = isset($params->limit) ? $params->limit : 100;
     $notifications = NotificationOnUserQuery::create()->filterByUserId($currentUser->id)->filterByStatus('Unread')->leftJoin('Notification')->withColumn('Notification.Time', 'time')->withColumn('Notification.Type', 'type')->withColumn('Notification.Data', 'data')->where('Notification.Status = ?', 'Active')->select(array('id'))->orderBy('time', 'DESC');
     $notifications = $notifications->paginate($page, $limit);
     $total = $notifications->getNbResults();
     $data = [];
     foreach ($notifications as $notification) {
         $notification = (object) $notification;
         switch ($notification->type) {
             case 'price':
                 $notification->data = json_decode($notification->data);
                 $stock = StockQuery::create()->findOneById($notification->data->stock_id, $con);
                 if ($stock) {
                     $notification->data->product_id = $stock->getProduct()->getId();
                     $notification->data->product_name = $stock->getProduct()->getName();
                     $notification->data->unit_name = $stock->getUnit()->getName();
                 } else {
                     $notification->delete($con);
                 }
         }
         $data[] = $notification;
     }
     $results['success'] = true;
     $results['data'] = $data;
     $results['total'] = $total;
     return $results;
 }
Example #2
0
 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;
 }
Example #3
0
 /**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this Notification is new, it will return
  * an empty collection; or if this Notification has previously
  * been saved, it will retrieve related OnUsers from storage.
  *
  * This method is protected by default in order to keep the public
  * api reasonable.  You can provide public methods for those you
  * actually need in Notification.
  *
  * @param      Criteria $criteria optional Criteria object to narrow the query
  * @param      ConnectionInterface $con optional connection object
  * @param      string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN)
  * @return ObjectCollection|ChildNotificationOnUser[] List of ChildNotificationOnUser objects
  */
 public function getOnUsersJoinUser(Criteria $criteria = null, ConnectionInterface $con = null, $joinBehavior = Criteria::LEFT_JOIN)
 {
     $query = ChildNotificationOnUserQuery::create(null, $criteria);
     $query->joinWith('User', $joinBehavior);
     return $this->getOnUsers($query, $con);
 }
Example #4
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      ConnectionInterface $con
  * @return void
  * @throws PropelException
  * @see NotificationOnUser::setDeleted()
  * @see NotificationOnUser::isDeleted()
  */
 public function delete(ConnectionInterface $con = null)
 {
     if ($this->isDeleted()) {
         throw new PropelException("This object has already been deleted.");
     }
     if ($con === null) {
         $con = Propel::getServiceContainer()->getWriteConnection(NotificationOnUserTableMap::DATABASE_NAME);
     }
     $con->transaction(function () use($con) {
         $deleteQuery = ChildNotificationOnUserQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             $this->setDeleted(true);
         }
     });
 }
 /**
  * Performs an INSERT on the database, given a NotificationOnUser or Criteria object.
  *
  * @param mixed               $criteria Criteria or NotificationOnUser object containing data that is used to create the INSERT statement.
  * @param ConnectionInterface $con the ConnectionInterface connection to use
  * @return mixed           The new primary key.
  * @throws PropelException Any exceptions caught during processing will be
  *                         rethrown wrapped into a PropelException.
  */
 public static function doInsert($criteria, ConnectionInterface $con = null)
 {
     if (null === $con) {
         $con = Propel::getServiceContainer()->getWriteConnection(NotificationOnUserTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from NotificationOnUser object
     }
     if ($criteria->containsKey(NotificationOnUserTableMap::COL_ID) && $criteria->keyContainsValue(NotificationOnUserTableMap::COL_ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . NotificationOnUserTableMap::COL_ID . ')');
     }
     // Set the correct dbName
     $query = NotificationOnUserQuery::create()->mergeWith($criteria);
     // use transaction because $criteria could contain info
     // for more than one table (I guess, conceivably)
     return $con->transaction(function () use($con, $query) {
         return $query->doInsert($con);
     });
 }