/** * Saves changes to an order * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); $statusmsg = ''; $data = array_map('trim', $_POST); $action = isset($data['action']) ? $data['action'] : ''; $id = $data['id'] ? $data['id'] : 0; $cost = intval($data['total']); if ($id) { // initiate extended database class $row = new Order($this->database); $row->load($id); $row->notes = \Hubzero\Utility\Sanitize::clean($data['notes']); $hold = $row->total; $row->total = $cost; // get user bank account $xprofile = User::getInstance($row->uid); $BTL_Q = new Teller($this->database, $xprofile->get('id')); switch ($action) { case 'complete_order': // adjust credit $credit = $BTL_Q->credit_summary(); $adjusted = $credit - $hold; $BTL_Q->credit_adjustment($adjusted); // remove hold $sql = "DELETE FROM `#__users_transactions` WHERE category='store' AND type='hold' AND referenceid='" . $id . "' AND uid=" . intval($row->uid); $this->database->setQuery($sql); if (!$this->database->query()) { throw new Exception($this->database->getErrorMsg(), 500); } // debit account if ($cost > 0) { $BTL_Q->withdraw($cost, Lang::txt('COM_STORE_BANKING_PURCHASE') . ' #' . $id, 'store', $id); } // update order information $row->status_changed = Date::toSql(); $row->status = 1; $statusmsg = Lang::txt('COM_STORE_ORDER') . ' #' . $id . ' ' . Lang::txt('COM_STORE_HAS_BEEN') . ' ' . strtolower(Lang::txt('COM_STORE_COMPLETED')) . '.'; break; case 'cancel_order': // adjust credit $credit = $BTL_Q->credit_summary(); $adjusted = $credit - $hold; $BTL_Q->credit_adjustment($adjusted); // remove hold $sql = "DELETE FROM `#__users_transactions` WHERE category='store' AND type='hold' AND referenceid='" . $id . "' AND uid=" . intval($row->uid); $this->database->setQuery($sql); if (!$this->database->query()) { throw new Exception($this->database->getErrorMsg(), 500); } // update order information $row->status_changed = Date::toSql(); $row->status = 2; $statusmsg = Lang::txt('COM_STORE_ORDER') . ' #' . $id . ' ' . Lang::txt('COM_STORE_HAS_BEEN') . ' ' . strtolower(Lang::txt('COM_STORE_CANCELLED')) . '.'; break; case 'message': $statusmsg = Lang::txt('COM_STORE_MSG_SENT') . '.'; break; default: $statusmsg = Lang::txt('COM_STORE_ORDER_DETAILS_UPDATED') . '.'; break; } // check content if (!$row->check()) { throw new Exception($row->getError(), 500); return; } // store new content if (!$row->store()) { throw new Exception($row->getError(), 500); } // send email if ($action || $data['message']) { if (\Hubzero\Utility\Validate::email($row->email)) { $message = new \Hubzero\Mail\Message(); $message->setSubject(Config::get('sitename') . ' ' . Lang::txt('COM_STORE_EMAIL_UPDATE_SHORT', $id)); $message->addFrom(Config::get('mailfrom'), Config::get('sitename') . ' ' . Lang::txt('COM_STORE_STORE')); // Plain text email $eview = new \Hubzero\Mail\View(array('name' => 'emails', 'layout' => '_plain')); $eview->option = $this->_option; $eview->controller = $this->_controller; $eview->orderid = $id; $eview->cost = $cost; $eview->row = $row; $eview->action = $action; $eview->message = \Hubzero\Utility\Sanitize::stripAll($data['message']); $plain = $eview->loadTemplate(false); $plain = str_replace("\n", "\r\n", $plain); $message->addPart($plain, 'text/plain'); // HTML email $eview->setLayout('_html'); $html = $eview->loadTemplate(); $html = str_replace("\n", "\r\n", $html); $message->addPart($html, 'text/html'); // Send e-mail $message->setTo(array($row->email)); $message->send(); } } } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $statusmsg); }
/** * Distribute points * * @param integer $qid Question ID * @param integer $Q_owner Question owner * @param integer $BA_owner Account owner * @param string $type Transaction type * @return void */ public function distribute_points($qid, $Q_owner, $BA_owner, $type) { if ($qid === NULL) { $qid = $this->qid; } $cat = 'answers'; require_once dirname(__DIR__) . DS . 'models' . DS . 'question.php'; $points = $this->calculate_marketvalue($qid, $type); $reward = Transaction::getAmount($cat, 'hold', $qid); $reward = $reward ? $reward : '0'; $share = $points / 3; $BA_owner_share = $share + $reward; $A_owner_share = 0; // Calculate commissions for other answers $results = Response::all()->whereEquals('question_id', $qid)->where('state', '!=', 2)->rows(); $n = $results->count(); $eligible = array(); if ($n > 1) { // More than one answer found foreach ($results as $result) { // Check if a regular answer has a good rating (at least 50% of positive votes) if ($result->get('helpful') + $result->get('nothelpful') >= 3 && $result->get('helpful') >= $result->get('nothelpful') && $result->get('state') == 0) { $eligible[] = $result->get('created_by'); } } if (count($eligible) > 0) { // We have eligible answers $A_owner_share = $share / $n; } else { // Best A owner gets remaining thrid $BA_owner_share += $share; } } else { // Best A owner gets remaining 3rd $BA_owner_share += $share; } // Reward asker $q_user = User::getInstance($Q_owner); if (is_object($q_user) && $q_user->get('id')) { $BTL_Q = new Teller($q_user->get('id')); //$BTL_Q->deposit($Q_owner_share, 'Commission for posting a question', $cat, $qid); // Separate comission and reward payment // Remove credit $credit = $BTL_Q->credit_summary(); $adjusted = $credit - $reward; $BTL_Q->credit_adjustment($adjusted); if (intval($share) > 0) { $share_msg = $type == 'royalty' ? Lang::txt('Royalty payment for posting question #%s', $qid) : Lang::txt('Commission for posting question #%s', $qid); $BTL_Q->deposit($share, $share_msg, $cat, $qid); } // withdraw reward amount if ($reward) { $BTL_Q->withdraw($reward, Lang::txt('Reward payment for your question #%s', $qid), $cat, $qid); } } // Reward others $ba_user = User::getInstance($BA_owner); if (is_object($ba_user) && $ba_user->get('id')) { // Reward other responders if (count($eligible) > 0) { foreach ($eligible as $e) { $auser = User::getInstance($e); if (is_object($auser) && $auser->get('id') && is_object($ba_user) && $ba_user->get('id') && $ba_user->get('id') != $auser->get('id')) { $BTL_A = new Teller($auser->get('id')); if (intval($A_owner_share) > 0) { $A_owner_share_msg = $type == 'royalty' ? Lang::txt('Royalty payment for answering question #%s', $qid) : Lang::txt('Answered question #%s that was recently closed', $qid); $BTL_A->deposit($A_owner_share, $A_owner_share_msg, $cat, $qid); } } // is best answer eligible for extra points? if (is_object($auser) && $auser->get('id') && is_object($ba_user) && $ba_user->get('id') && $ba_user->get('id') == $auser->get('id')) { $ba_extra = 1; } } } // Reward best answer $BTL_BA = new Teller($ba_user->get('id')); if (isset($ba_extra)) { $BA_owner_share += $A_owner_share; } if (intval($BA_owner_share) > 0) { $BA_owner_share_msg = $type == 'royalty' ? Lang::txt('Royalty payment for answering question #%s', $qid) : Lang::txt('Answer for question #%s was accepted', $qid); $BTL_BA->deposit($BA_owner_share, $BA_owner_share_msg, $cat, $qid); } } // Remove hold if exists if ($reward) { $BT = Transaction::deleteRecords('answers', 'hold', $qid); } }
/** * Distribute points * * @param integer $wishid Wish ID * @param string $type Transaction type * @param number $points Points to distribute * @return void */ public function distribute_points($wishid, $type = 'grant', $points = 0) { if (!$wishid) { return null; } require_once dirname(__DIR__) . DS . 'models' . DS . 'wishlist.php'; $objWish = new Tables\Wish($this->_db); $wish = $objWish->get_wish($wishid); $points = !$points ? $wish->bonus : $points; // Points for list owners if ($points > 0 && $type != 'royalty') { // Get the component parameters $wconfig = Component::params('com_wishlist'); $admingroup = $wconfig->get('group', 'hubadmin'); // get list owners $objOwner = new Tables\Owner($this->_db); $owners = $objOwner->get_owners($wish->wishlist, $admingroup, '', 0, $wishid); $owners = $owners['individuals']; $mainshare = $wish->assigned ? $points * 0.8 : 0; //80% $commonshare = $mainshare ? ($points - $mainshare) / count($owners) : $points / count($owners); // give the remaining 20% if ($owners && $commonshare) { foreach ($owners as $owner) { $o = User::getInstance($owner); if (!is_object($o) || !$o->get('id')) { continue; } $BTLO = new Teller($this->_db, $owner); if ($wish->assigned && $wish->assigned == $owner) { //$BTLO->deposit($mainshare, Lang::txt('Bonus for fulfilling assigned wish').' #'.$wishid.' '.Lang::txt('on list').' #'.$wish->wishlist, 'wish', $wishid); $mainshare += $commonshare; } else { $BTLO->deposit($commonshare, Lang::txt('Bonus for fulfilling wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } } else { $mainshare += $commonshare; } // give main share if ($wish->assigned && $mainshare) { $o = User::getInstance($wish->assigned); if (is_object($o) && $o->get('id')) { $BTLM = new Teller($this->_db, $wish->assigned); $BTLM->deposit($mainshare, Lang::txt('Bonus for fulfilling assigned wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } // Adjust credits $payees = $this->getPayees($wishid); if ($payees) { foreach ($payees as $p) { $o = User::getInstance($p->uid); if (!is_object($o) || !$o->get('id')) { continue; } $BTL = new Teller($this->_db, $p->uid); $hold = $this->getTotalPayment($wishid, $p->uid); if ($hold) { $credit = $BTL->credit_summary(); $adjusted = $credit - $hold; $BTL->credit_adjustment($adjusted); // withdraw bonus amount $BTL->withdraw($hold, Lang::txt('Bonus payment for granted wish #%s on list #%s', $wishid, $wish->wishlist), 'wish', $wishid); } } } // Remove holds if exist if ($wish->bonus) { $BT = new Transaction($this->_db); $BT->deleteRecords('wish', 'hold', $wishid); } } // Points for wish author (needs to be granted by another person) if ($wish->ranking > 0 && $wish->proposed_by != User::get('id') && $wish->proposed_by) { $o = User::getInstance($wish->proposed_by); if (is_object($o) && $o->get('id')) { $BTLA = new Teller($this->_db, $wish->proposed_by); $BTLA->deposit($wish->ranking, Lang::txt('Your wish #%s on list #%s was granted', $wishid, $wish->wishlist), 'wish', $wishid); } } }