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); } } }