/** * Enter description here... * * @param int $user_id * @param int $period_id * @return BalanceUser */ public static function getByUserIdAndPeriodId($user_id, $period_id = 0) { if ($period_id == 0) { $period_id = Period::getCurrentPeriod()->getId(); } $q = Doctrine_Query::create()->from('BalanceUser bu')->where('bu.id_user=?', $user_id)->andWhere('bu.id_period=?', $period_id)->limit(1); $balance = $q->fetchOne(); if (false === $balance) { BalanceUser::setInitialRecord($user_id, $period_id); $balance = self::getByUserIdAndPeriodId($user_id, $period_id); //throw new sfException('Cannot get Balance for User: '******' period: '.$period_id.'. Error in DB data'); } return $balance; }
public static function setInitialRecord($user_id, $period_id) { $userBalance = new BalanceUser(); $userBalance->setIdUser($user_id); $userBalance->setIdPeriod($period_id); $userBalance->setSellPurchaseCnt(0); $userBalance->setAmount(0); $userBalance->save(); }
/** * U_user purchase content: Transfer money from u_user to system * * @param <User> $user - instance of User purchased content * @param <int> $content_id - purchased article ID * @return bool */ public function purchaseContent(User $u_user, $content_id) { $article = ContentTable::getInstance()->getArticleForSale($content_id); if (!is_object($article)) { return false; } $p_user = $article->getTranslator(); $this->setOperation('purchase'); $this->setPeriod(Period::getCurrentPeriod()); $this->setAmount($article->getPrice()); $r2r_price = (int) ($article->getPrice() * Setting::getR2RPercent()) / 100; $p_user_price = $article->getPrice() - $r2r_price; $this->setIdSender($u_user->getId()); $this->setIdReceiver($p_user->getId()); // Update U_User balance $this->setSenderBalanceBefore($u_user->getBalans()); $u_user->setBalans($u_user->getBalans() - $this->getAmount()); $this->setSenderBalanceAfter($u_user->getBalans()); // Save U_User balance $u_user->save(); $u_user = null; $p_user = null; // System balance if (!($systemBalance = BalanceSystem::getCurrentBalanceInstance())) { return false; } // u_user Charges $systemBalance->setChargesUser($systemBalance->getChargesUser() + $this->getAmount()); // get p_user tariff plan $article_user_tariff = $article->getPUserTariff(); // p_user to pay and stats adn R2R summ if ($article_user_tariff == 'standart') { $systemBalance->setToPayStandart($systemBalance->getToPayStandart() + $p_user_price); $systemBalance->setSalesStandart($systemBalance->getSalesStandart() + 1); $systemBalance->setR2rStandart($systemBalance->getR2rStandart() + $r2r_price); } elseif ($article_user_tariff == 'expert') { $systemBalance->setToPayExpert($systemBalance->getToPayExpert() + $p_user_price); $systemBalance->setSalesExpert($systemBalance->getSalesExpert() + 1); $systemBalance->setR2rExpert($systemBalance->getR2rExpert() + $r2r_price); } elseif ($article_user_tariff == 'super') { $systemBalance->setToPaySuper($systemBalance->getToPaySuper() + $p_user_price); $systemBalance->setSalesSuper($systemBalance->getSalesSuper() + 1); $systemBalance->setR2rSuper($systemBalance->getR2rSuper() + $r2r_price); } // Save system balance $systemBalance->save(); $systemBalance = null; // BalanceUser u_user: update purchase stats if (!($userBalance = BalanceUserTable::getInstance()->getByUserIdAndPeriodId($this->getIdSender()))) { $userBalance = new BalanceUser(); $userBalance->setPeriod(Period::getCurrentPeriod()); $userBalance->setIdUser($this->getIdSender()); } // Save U_User purchase stats $userBalance->setSellPurchaseCnt($userBalance->getSellPurchaseCnt() + 1); $userBalance->setAmount($userBalance->getAmount() + $this->getAmount()); // save data $userBalance->save(); $userBalance = null; // BalanceUser p_user: update sell stats // Receiver: P_User balance if (!($userBalance = BalanceUserTable::getInstance()->getByUserIdAndPeriodId($this->getIdReceiver()))) { $userBalance = new BalanceUser(); $userBalance->setPeriod(Period::getCurrentPeriod()); $userBalance->setIdUser($this->getIdReceiver()); } // Transaction $this->setReceiverBalanceBefore($userBalance->getAmount()); // p_user $userBalance->setSellPurchaseCnt($userBalance->getSellPurchaseCnt() + 1); $userBalance->setAmount($userBalance->getAmount() + $this->getAmount()); $userBalance->setPayable($userBalance->getPayable() + $this->getAmount() - $r2r_price); // transaction $this->setReceiverBalanceAfter($userBalance->getAmount()); // save data $userBalance->save(); $userBalance = null; // Save transaction $this->save(); return $this; }
public static function addFundsFin(Transaction $transaction) { if ($transaction->getIsPaid()) { // транзакция уже была обработана return false; } else { $transaction->addFundsFin(); } // set new balance $user = UserTable::getInstance()->findOneById($transaction->getIdReceiver()); $user->setBalans($transaction->getReceiverBalanceAfter()); $user->save(); $amount = $transaction->getAmount(); // write to BalanceUser instance if (!($userBalance = BalanceUserTable::getByUserIdAndPeriodId($user->getId()))) { $userBalance = new BalanceUser(); $userBalance->setPeriod(Period::getCurrentPeriod()); $userBalance->setUser($user); } $userBalance->setAddFunds($userBalance->getAddFunds() + $amount); $userBalance->save(); if (!($systemBalance = BalanceSystem::getCurrentBalanceInstance())) { sfContext::getInstance()->getLogger()->info('aS4W Error: getCurrentBalanceInstance return false'); return false; } if ($user->getUSerTypePrefix() == 'u') { $systemBalance->setDepositUser($systemBalance->getDepositUser() + $amount); } else { switch ($user->getTariff()) { case 'standart': $systemBalance->setDepositStandart($systemBalance->getDepositStandart() + $amount); break; case 'super': $systemBalance->setDepositSuper($systemBalance->getDepositSuper() + $amount); break; case 'expert': $systemBalance->setDepositExpert($systemBalance->getDepositExpert() + $amount); break; } } $systemBalance->save(); // If user blocked try UnBlock if ($user->is_blocked) { $user->tryUnBlockUser(); } return true; }
/** * Saves form * * @param sfForm $form * @param <string> $utype * @return User $user */ protected function saveForm(sfForm $form) { $user = $form->save(); $group = UserGroupTable::getInstance()->findOneBy('name', $user->getUtype()); $userGroup = new User__Group(); $userGroup->setUser($user); $userGroup->setGroup($group); $userGroup->save(); $userBalance = new BalanceUser(); $userBalance->setPeriod(Period::getCurrentPeriod()); $userBalance->setUser($user); $userBalance->save(); $user->setIsBlocked(true); $user->setActivationCode(sha1($user->getEmail() . mt_rand(10, 15))); $user->save(); return $user; }