/**
  * Prepare current and next badges.
  *
  * @throws \RuntimeException
  */
 public function prepareData()
 {
     $userPoints = $this->points->getPointsNumber();
     // Get current badge.
     $query = $this->db->getQuery(true);
     $query->select('a.id, a.title, a.points_number, a.image, a.description, a.published, a.points_id, a.group_id, ' . 'b.id AS user_badge_id')->from($this->db->quoteName('#__gfy_badges', 'a'))->innerJoin($this->db->quoteName('#__gfy_userbadges', 'b') . ' ON a.id = b.badge_id')->where('a.points_id = ' . (int) $this->points->getPointsId())->where('a.published = ' . (int) Constants::PUBLISHED)->where('a.points_number <= ' . (int) $userPoints)->order('a.points_number DESC');
     $this->db->setQuery($query, 0, 1);
     $result = (array) $this->db->loadAssoc();
     if (count($result) > 0) {
         $this->currentUnit = new BasicBadge($this->db);
         $this->currentUnit->bind($result);
     }
     // Get incoming badge.
     $query = $this->db->getQuery(true);
     $query->select('a.id, a.title, a.points_number, a.image, a.description, a.published, a.points_id, a.group_id')->from($this->db->quoteName('#__gfy_badges', 'a'))->where('a.points_id = ' . (int) $this->points->getPointsId())->where('a.published = 1')->where('a.points_number > ' . (int) $userPoints)->order('a.points_number ASC');
     $this->db->setQuery($query, 0, 1);
     $result = (array) $this->db->loadAssoc();
     if (count($result) > 0) {
         $this->nextUnit = new BasicBadge($this->db);
         $this->nextUnit->bind($result);
         $this->percentageCurrent = (int) MathHelper::calculatePercentage($userPoints, $this->nextUnit->getPointsNumber());
         $this->percentageNext = 100 - $this->percentageCurrent;
     } else {
         $this->percentageCurrent = 100;
         $this->percentageNext = 100;
     }
 }
 /**
  * Prepare the statuses of the items.
  *
  * @param array $data
  * @param array $options
  */
 public function handle(&$data, array $options = array())
 {
     foreach ($data as $key => $item) {
         // Calculate funding end date
         if (is_numeric($item->funding_days) and $item->funding_days > 0) {
             $fundingStartDate = new Crowdfunding\Date($item->funding_start);
             $endDate = $fundingStartDate->calculateEndDate($item->funding_days);
             $item->funding_end = $endDate->format(Prism\Constants::DATE_FORMAT_SQL_DATE);
         }
         // Calculate funded percentage.
         $item->funded_percents = (string) MathHelper::calculatePercentage($item->funded, $item->goal, 0);
         // Calculate days left
         $today = new Crowdfunding\Date();
         $item->days_left = $today->calculateDaysLeft($item->funding_days, $item->funding_start, $item->funding_end);
         // Decode parameters.
         if ($item->params === null) {
             $item->params = '{}';
         }
         if (is_string($item->params) and $item->params !== '') {
             $params = new Registry();
             $params->loadString($item->params);
             $item->params = $params;
         }
     }
 }
 /**
  * Prepare the statuses of the items.
  *
  * @param array $data
  * @param array $options
  */
 public function handle(&$data, array $options = array())
 {
     foreach ($data as $key => $item) {
         // Calculate funding end date
         if (is_numeric($item->funding_days) and $item->funding_days > 0) {
             $fundingStartDate = new Crowdfunding\Date($item->funding_start);
             $endDate = $fundingStartDate->calculateEndDate($item->funding_days);
             $item->funding_end = $endDate->format('Y-m-d');
         }
         // Calculate funded percentage.
         $item->funded_percents = (string) MathHelper::calculatePercentage($item->funded, $item->goal, 0);
         // Calculate days left
         $today = new Crowdfunding\Date();
         $item->days_left = $today->calculateDaysLeft($item->funding_days, $item->funding_start, $item->funding_end);
     }
 }
 /**
  * Prepare current and next ranks.
  *
  * @throws \RuntimeException
  */
 public function prepareData()
 {
     $userPoints = (int) $this->points->getPointsNumber();
     // Get current level.
     $keys = array('user_id' => $this->points->getUserId(), 'group_id' => $this->points->getPoints()->getGroupId());
     $userRank = new UserRank($this->db);
     $userRank->load($keys);
     $this->currentUnit = $userRank->getRank();
     // Get incoming level.
     $query = $this->db->getQuery(true);
     $query->select('a.id, a.title, a.description, a.image, a.activity_text, a.published, a.points_id, a.points_number, a.group_id')->from($this->db->quoteName('#__gfy_ranks', 'a'))->where('a.points_id = ' . (int) $this->points->getPointsId())->where('a.published = ' . (int) Constants::PUBLISHED)->where('a.points_number > ' . $userPoints)->order('a.points_number ASC');
     $this->db->setQuery($query, 0, 1);
     $result = (array) $this->db->loadAssoc();
     if (count($result) > 0) {
         $this->nextUnit = new BasicRank($this->db);
         $this->nextUnit->bind($result);
         $this->percentageCurrent = (int) MathHelper::calculatePercentage($userPoints, $this->nextUnit->getPointsNumber());
         $this->percentageNext = 100 - $this->percentageCurrent;
     } else {
         $this->percentageCurrent = 100;
         $this->percentageNext = 100;
     }
 }
 /**
  * Remove amount from current funded one.
  * Calculate funded percent.
  *
  * <code>
  * $projectId = 1;
  * $finds = 50;
  *
  * $project   = new Crowdfunding\Project(\JFactory::getDbo());
  * $project->load($projectId);
  * $project->removeFunds($finds);
  * $project->store();
  * </code>
  *
  * @param float $amount
  */
 public function removeFunds($amount)
 {
     $this->funded -= $amount;
     // Calculate new percentage
     $this->setFundedPercent((string) MathHelper::calculatePercentage($this->funded, $this->goal, 0));
 }
Esempio n. 6
0
 /**
  * This method calculates a fee which is set by Crowdfunding Finance.
  *
  * @param $fundingType
  * @param $fees
  * @param $txnAmount
  *
  * @return float
  */
 protected function calculateFee($fundingType, $fees, $txnAmount)
 {
     $result = 0;
     $feePercent = 0.0;
     $feeAmount = 0.0;
     switch ($fundingType) {
         case 'FIXED':
             $feePercent = ArrayHelper::getValue($fees, 'fixed_campaign_percent', 0.0, 'float');
             $feeAmount = ArrayHelper::getValue($fees, 'fixed_campaign_amount', 0.0, 'float');
             break;
         case 'FLEXIBLE':
             $feePercent = ArrayHelper::getValue($fees, 'flexible_campaign_percent', 0.0, 'float');
             $feeAmount = ArrayHelper::getValue($fees, 'flexible_campaign_amount', 0.0, 'float');
             break;
     }
     // Calculate fee based on percent.
     if ($feePercent > 0) {
         // Calculate amount.
         $feePercentAmount = Prism\Utilities\MathHelper::calculateValueFromPercent($feePercent, $txnAmount);
         if ($txnAmount > $feePercentAmount) {
             $result += (double) $feePercentAmount;
         }
     }
     // Calculate fees based on amount.
     if ($feeAmount > 0 and $txnAmount > $feeAmount) {
         $result += $feeAmount;
     }
     // Check for invalid value that is less than zero.
     if ($result < 0) {
         $result = 0;
     }
     return (double) $result;
 }