Ejemplo n.º 1
0
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
 public function executeIndex(sfWebRequest $request)
 {
     $this->getResponse()->setContentType('application/json');
     $this->setLayout(false);
     // first api only accepts posts
     if ($request->getMethod() != "POST") {
         $this->getResponse()->setStatusCode(405);
         return $this->renderPartial("wrong_method");
     }
     $access_token = $request->getParameter("access_token");
     // check access token
     if (!$access_token) {
         $this->getResponse()->setStatusCode(401);
         return $this->renderPartial("authentication_error");
     }
     $user = Doctrine_Core::getTable('sfGuardUser')->createQuery('u')->where('u.access_token = ?', $access_token)->addWhere('u.is_active = ?', true)->fetchOne();
     // check access token
     if (!$user) {
         $this->getResponse()->setStatusCode(401);
         return $this->renderPartial("authentication_error");
     }
     // check if account was setup properly
     // @todo add publisher commission
     if (!$user->getApiPriceLike() || !$user->getApiPriceMediaPenetration() || !$user->getApiPaymentMethod() || !$user->getApiCommissionPercentage()) {
         $this->getResponse()->setStatusCode(403);
         return $this->renderPartial("setup_failure");
     }
     // get json post
     $json_content = file_get_contents("php://input");
     sfContext::getInstance()->getLogger()->notice($json_content);
     $deal = new Deal();
     $deal->setSfGuardUser($user);
     $deal->setPaymentMethod($user->getApiPaymentMethod());
     $data = json_decode($json_content, true);
     // check if data is valid json
     if (!$data) {
         $this->getResponse()->setStatusCode(406);
         return $this->renderPartial("wrong_mimetype");
     }
     $deal->fromApiArray($data);
     // set commission values
     $deal->setCommissionPercentage($user->getApiCommissionPercentage());
     $pot = round($deal->getPrice() * $user->getApiCommissionPercentage() / 100, 2, PHP_ROUND_HALF_UP);
     $deal->setCommissionPot($pot);
     $unit = round($pot / $deal->getTargetQuantity(), 2, PHP_ROUND_HALF_UP);
     $deal->setCommissionPerUnit($unit);
     // validate request
     $validate = $deal->validate();
     if ($validate !== true) {
         $this->getResponse()->setStatusCode(406);
         return $this->renderPartial("deals/wrong_fields", array("errors" => $validate));
     } else {
         $deal->save();
         $deal->complete_campaign();
         $deal->complete_share();
         $deal->complete_coupon();
         $deal->complete_billing();
         $deal->submit();
         if (array_key_exists('activate', $data) && $data['activate'] === true) {
             $deal->approve();
         }
     }
 }
Ejemplo n.º 2
0
 private static function makeDealsWith(Deal $deal)
 {
     $order = new Order();
     $order->findById($deal->getOrderId());
     if ($order->getType() == OrderType::SELL) {
         $oppositeDeals = Deal::getOpenedBuyDeals($order->getPrice(), $order->getRateId());
     } else {
         $oppositeDeals = Deal::getOpenedSellDeals($order->getPrice(), $order->getRateId());
     }
     foreach ($oppositeDeals as $oppDeal) {
         $oppositeDeal = new Deal();
         $oppositeDeal->findById($oppDeal['id']);
         $oppositeOrder = new Order();
         $oppositeOrder->findById($oppositeDeal->getOrderId());
         $difference = $deal->getVolume() - $oppositeDeal->getVolume();
         if ($difference == 0) {
             $deal->setPrice($oppositeDeal->getPrice());
             $deal->setDone(1);
             $deal->save();
             $order->setPart(1.0);
             $order->setStatus(OrderStatus::DONE);
             $order->save();
             $oppositeDeal->setDone(1);
             $oppositeDeal->setDate($deal->getDate());
             $oppositeDeal->save();
             $oppositeOrder->setPart(1.0);
             $oppositeOrder->setStatus(OrderStatus::DONE);
             $oppositeOrder->setDate($deal->getDate());
             $oppositeOrder->save();
             self::transferMoney($deal, $oppositeDeal);
             break;
         }
         $intermediateDeal = new Deal();
         $intermediateDeal->setRateId($order->getRateId());
         $intermediateDeal->setDate($deal->getDate());
         $intermediateDeal->setDone(1);
         if ($difference < 0) {
             $deal->setPrice($oppositeDeal->getPrice());
             $deal->setDone(1);
             $deal->save();
             $order->setPart(1.0);
             $order->setStatus(OrderStatus::DONE);
             $order->save();
             $intermediateDeal->setOrderId($oppositeOrder->getId());
             $intermediateDeal->setUserId($oppositeOrder->getUserId());
             $intermediateDeal->setType($oppositeOrder->getType());
             $intermediateDeal->setPrice($oppositeDeal->getPrice());
             $intermediateDeal->setVolume($deal->getVolume());
             $intermediateDeal->insert();
             $oppositeOrder->updatePart();
             $oppositeOrder->setDate($deal->getDate());
             $oppositeOrder->save();
             $oppositeDeal->setVolume(-$difference);
             $oppositeDeal->setDate($deal->getDate());
             $oppositeDeal->save();
             self::transferMoney($deal, $intermediateDeal);
             break;
         } else {
             $deal->setVolume($difference);
             $deal->save();
             $intermediateDeal->setOrderId($order->getId());
             $intermediateDeal->setUserId($order->getUserId());
             $intermediateDeal->setType($order->getType());
             $intermediateDeal->setPrice($oppositeDeal->getPrice());
             $intermediateDeal->setVolume($oppositeDeal->getVolume());
             $intermediateDeal->insert();
             $order->updatePart();
             $order->save();
             $oppositeDeal->setDone(1);
             $oppositeDeal->setDate($deal->getDate());
             $oppositeDeal->save();
             $oppositeOrder->setPart(1.0);
             $oppositeOrder->setStatus(OrderStatus::DONE);
             $oppositeOrder->setDate($deal->getDate());
             $oppositeOrder->save();
             self::transferMoney($intermediateDeal, $oppositeDeal);
         }
     }
 }
 public static function rate($firstCurName, $secondCurName)
 {
     //  $firstCurName = Core::validate(self::getVar('firstCurrency'));
     //  $secondCurName = Core::validate(self::getVar('secondCurrency'));
     $rate = self::getRate($firstCurName, $secondCurName);
     if ($rate == null) {
         return;
     }
     $deal = new Deal();
     $deal->findLastDealByRate($rate->getId());
     $row = $deal->getPrice();
     return $row;
 }
Ejemplo n.º 4
0
 public function testGetPrice()
 {
     $deal = new Deal();
     $deal->dea_marketprice = 400000;
     $this->assertEquals(400000, $deal->getPrice());
 }