Exemplo n.º 1
0
 public function gainslossesAction(Request $request)
 {
     $params = array();
     $totalNetProceeds = 0;
     $totalCost = 0;
     $longTermGainLoss = 0;
     $shortTermGainLoss = 0;
     $totalGainLoss = 0;
     $sort = $request->query->get('sort', 'securities.name');
     $direction = $request->query->get('direction', 'ASC');
     $client = $this->getUser();
     /** @var EntityManager $em */
     $em = $this->get('doctrine.orm.entity_manager');
     $em->getConfiguration()->addCustomDatetimeFunction('YEAR', 'Wealthbot\\MailerBundle\\DQL\\DatetimeFunction\\Year');
     $systemAccountManager = $this->get('wealthbot_client.system_account_manager');
     /** @var \Wealthbot\ClientBundle\Repository\ClientSettings $repository */
     $repository = $em->getRepository('WealthbotClientBundle:ClientSettings');
     /** @var \Wealthbot\ClientBundle\Repository\PositionRepository $positionsRepo */
     $positionsRepo = $em->getRepository('WealthbotClientBundle:Position');
     /** @var \Wealthbot\ClientBundle\Repository\LotRepository $lotsRepo */
     $lotsRepo = $em->getRepository('WealthbotClientBundle:Lot');
     $isClientView = $this->isRiaClientView();
     if ($isClientView) {
         $client = $this->getUser();
         $clientSettings = $repository->findOneBy(array('client' => $client));
         if (!$clientSettings) {
             $clientSettings = new ClientSettings();
             $clientSettings->setClient($client);
         }
         $stopTLHValueForm = $this->createForm(new ClientStopTLHValueFormType(), $clientSettings);
         $params = array('stop_tlh_form' => $stopTLHValueForm->createView());
         if ($request->isMethod("post")) {
             $stopTLHValueForm->bind($request);
             if ($stopTLHValueForm->isValid()) {
                 $em->persist($clientSettings);
                 $em->flush();
                 return $this->getJsonResponse(array("status" => "success"));
             } else {
                 $content = $this->renderView("WealthbotClientBundle:Dashboard:gainlosses_stop_tlh_form.html.twig", array("form" => $stopTLHValueForm->createView()));
                 return $this->getJsonResponse(array("status" => "error", "content" => $content));
             }
         }
     }
     if ($request->isMethod("get")) {
         $isClientView = !$this->isRiaClientView();
         $activeClientAccounts = $systemAccountManager->getAccountsForClient($client, $isClientView);
         $accounts = $activeClientAccounts;
         if ($accountId = $request->get('account_id')) {
             if ($account = $em->getRepository('WealthbotClientBundle:ClientAccount')->findBy(array('id' => $accountId, 'client' => $client))) {
                 /** @var ClientAccount $account */
                 if ($systemAccount = $account->getSystemAccount()) {
                     $accounts = array($systemAccount);
                 }
             }
         }
         $gainLossYears = $positionsRepo->getGainLossYears($accounts);
         $year = $request->get('year');
         if (empty($year)) {
             if (isset($gainLossYears[0])) {
                 $year = $gainLossYears[0]['year'];
             }
         }
         $lots = $lotsRepo->getRealizedLots($year, $accounts, $sort, $direction);
         $paginator = $this->get('knp_paginator');
         $pagination = $paginator->paginate($lots, $request->get('page', 1), 10);
         foreach ($pagination as $gainLoss) {
             $totalNetProceeds += $gainLoss->getAmount();
             $totalCost += $gainLoss->getInitial()->getAmount();
             $longTermGainLoss += $gainLoss->getLongTermGain();
             $shortTermGainLoss += $gainLoss->getShortTermGain();
             $totalGainLoss += $gainLoss->getRealizedGain();
         }
         $params = array_merge($params, array('pagination' => $pagination, 'gainLossYears' => $gainLossYears, 'lots' => $lots, 'totalNetProceeds' => $totalNetProceeds, 'totalCost' => $totalCost, 'longTermGainLoss' => $longTermGainLoss, 'shortTermGainLoss' => $shortTermGainLoss, 'totalGainLoss' => $totalGainLoss));
     }
     return $this->prepareResponse($request, 'gainslosses', 'Gains/Losses', $params);
 }